91static void usage(
void);
95main(
int argc,
char *argv[])
97 static struct option long_options[] = {
116 bool noupdate =
false;
121 char *log_fname = NULL;
130 if (strcmp(argv[1],
"--help") == 0 || strcmp(argv[1],
"-?") == 0)
135 if (strcmp(argv[1],
"--version") == 0 || strcmp(argv[1],
"-V") == 0)
137 puts(
"pg_resetwal (PostgreSQL) " PG_VERSION);
143 while ((
c =
getopt_long(argc, argv,
"c:D:e:fl:m:no:O:u:x:", long_options, NULL)) != -1)
162 if (endptr ==
optarg || *endptr !=
'\0' || errno != 0)
171 pg_fatal(
"transaction ID epoch (-e) must not be -1");
177 if (endptr ==
optarg || *endptr !=
'\0' || errno != 0)
190 if (endptr ==
optarg || *endptr !=
'\0' || errno != 0)
203 if (endptr ==
optarg || *endptr !=
',' || errno != 0)
210 if (endptr2 == endptr + 1 || *endptr2 !=
'\0' || errno != 0)
229 if (endptr ==
optarg || *endptr !=
'\0' || errno != 0)
242 if (endptr ==
optarg || *endptr !=
',' || errno != 0)
249 set_oldestmxid = strtoul(endptr + 1, &endptr2, 0);
250 if (endptr2 == endptr + 1 || *endptr2 !=
'\0' || errno != 0)
257 pg_fatal(
"multitransaction ID (-m) must not be 0");
263 if (set_oldestmxid == 0)
264 pg_fatal(
"oldest multitransaction ID (-m) must not be 0");
270 if (endptr ==
optarg || *endptr !=
'\0' || errno != 0)
277 pg_fatal(
"multitransaction offset (-O) must not be -1");
303 pg_fatal(
"argument of %s must be a power of two between 1 and 1024",
"--wal-segsize");
317 pg_log_error(
"invalid argument for option %s",
"--char-signedness");
337 pg_log_error(
"too many command-line arguments (first is \"%s\")",
370 pg_fatal(
"could not read permissions of directory \"%s\": %m",
376 pg_fatal(
"could not change directory to \"%s\": %m",
386 if ((
fd = open(
"postmaster.pid", O_RDONLY, 0)) < 0)
389 pg_fatal(
"could not open file \"%s\" for reading: %m",
394 pg_log_error(
"lock file \"%s\" exists",
"postmaster.pid");
395 pg_log_error_hint(
"Is a server running? If not, delete the lock file and try again.");
413 if (log_fname != NULL)
425 if ((
guessed && !force) || noupdate)
497 pg_log_error(
"not proceeding because control file values were guessed");
507 pg_log_error(
"database server was not shut down cleanly");
522 printf(
_(
"Write-ahead log reset\n"));
542 const char *ver_file =
"PG_VERSION";
546 if ((ver_fd = fopen(ver_file,
"r")) == NULL)
547 pg_fatal(
"could not open file \"%s\" for reading: %m",
551 if (!fgets(rawline,
sizeof(rawline), ver_fd))
554 pg_fatal(
"unexpected empty file \"%s\"", ver_file);
556 pg_fatal(
"could not read file \"%s\": %m", ver_file);
562 if (strcmp(rawline, PG_MAJORVERSION) != 0)
565 pg_log_error_detail(
"File \"%s\" contains \"%s\", which is not compatible with this program's version \"%s\".",
566 ver_file, rawline, PG_MAJORVERSION);
595 pg_log_error(
"could not open file \"%s\" for reading: %m",
598 pg_log_error_hint(
"If you are sure the data directory path is correct, execute\n"
626 pg_log_warning(
"pg_control exists but has invalid CRC; proceed with caution");
636 "pg_control specifies invalid WAL segment size (%d bytes); proceed with caution",
646 pg_log_warning(
"pg_control exists but is broken or wrong version; ignoring it");
674 sysidentifier = ((
uint64) tv.tv_sec) << 32;
675 sysidentifier |= ((
uint64) tv.tv_usec) << 12;
676 sysidentifier |= getpid() & 0xFFF;
741 printf(
_(
"Guessed pg_control values:\n\n"));
743 printf(
_(
"Current pg_control values:\n\n"));
745 printf(
_(
"pg_control version number: %u\n"),
747 printf(
_(
"Catalog version number: %u\n"),
749 printf(
_(
"Database system identifier: %" PRIu64
"\n"),
751 printf(
_(
"Latest checkpoint's TimeLineID: %u\n"),
753 printf(
_(
"Latest checkpoint's full_page_writes: %s\n"),
755 printf(
_(
"Latest checkpoint's NextXID: %u:%u\n"),
758 printf(
_(
"Latest checkpoint's NextOID: %u\n"),
760 printf(
_(
"Latest checkpoint's NextMultiXactId: %u\n"),
762 printf(
_(
"Latest checkpoint's NextMultiOffset: %u\n"),
764 printf(
_(
"Latest checkpoint's oldestXID: %u\n"),
766 printf(
_(
"Latest checkpoint's oldestXID's DB: %u\n"),
768 printf(
_(
"Latest checkpoint's oldestActiveXID: %u\n"),
770 printf(
_(
"Latest checkpoint's oldestMultiXid: %u\n"),
772 printf(
_(
"Latest checkpoint's oldestMulti's DB: %u\n"),
774 printf(
_(
"Latest checkpoint's oldestCommitTsXid:%u\n"),
776 printf(
_(
"Latest checkpoint's newestCommitTsXid:%u\n"),
778 printf(
_(
"Maximum data alignment: %u\n"),
781 printf(
_(
"Database block size: %u\n"),
783 printf(
_(
"Blocks per segment of large relation: %u\n"),
785 printf(
_(
"WAL block size: %u\n"),
787 printf(
_(
"Bytes per WAL segment: %u\n"),
789 printf(
_(
"Maximum length of identifiers: %u\n"),
791 printf(
_(
"Maximum columns in an index: %u\n"),
793 printf(
_(
"Maximum size of a TOAST chunk: %u\n"),
795 printf(
_(
"Size of a large-object chunk: %u\n"),
798 printf(
_(
"Date/time type storage: %s\n"),
799 _(
"64-bit integers"));
800 printf(
_(
"Float8 argument passing: %s\n"),
802 printf(
_(
"Data page checksum version: %u\n"),
804 printf(
_(
"Default char data signedness: %s\n"),
818 printf(
_(
"\n\nValues to be changed:\n\n"));
822 printf(
_(
"First log segment after reset: %s\n"), fname);
826 printf(
_(
"NextMultiXactId: %u\n"),
828 printf(
_(
"OldestMultiXid: %u\n"),
830 printf(
_(
"OldestMulti's DB: %u\n"),
836 printf(
_(
"NextMultiOffset: %u\n"),
852 printf(
_(
"OldestXID's DB: %u\n"),
858 printf(
_(
"NextXID epoch: %u\n"),
864 printf(
_(
"oldestCommitTsXid: %u\n"),
869 printf(
_(
"newestCommitTsXid: %u\n"),
875 printf(
_(
"Bytes per WAL segment: %u\n"),
954 while (errno = 0, (xlde =
readdir(xldir)) != NULL)
1007 while (errno = 0, (xlde =
readdir(xldir)) != NULL)
1013 if (unlink(path) < 0)
1014 pg_fatal(
"could not delete file \"%s\": %m", path);
1032#define ARCHSTATDIR XLOGDIR "/archive_status"
1042 while (errno = 0, (xlde =
readdir(xldir)) != NULL)
1051 if (unlink(path) < 0)
1052 pg_fatal(
"could not delete file \"%s\": %m", path);
1069#define WALSUMMARYDIR XLOGDIR "/summaries"
1070#define WALSUMMARY_NHEXCHARS 40
1080 while (errno = 0, (xlde =
readdir(xldir)) != NULL)
1086 if (unlink(path) < 0)
1087 pg_fatal(
"could not delete file \"%s\": %m", path);
1115 memset(buffer.
data, 0, XLOG_BLCKSZ);
1155 fd = open(path, O_RDWR | O_CREAT | O_EXCL |
PG_BINARY,
1158 pg_fatal(
"could not open file \"%s\": %m", path);
1161 if (
write(
fd, buffer.
data, XLOG_BLCKSZ) != XLOG_BLCKSZ)
1166 pg_fatal(
"could not write file \"%s\": %m", path);
1170 memset(buffer.
data, 0, XLOG_BLCKSZ);
1171 for (nbytes = XLOG_BLCKSZ; nbytes <
WalSegSz; nbytes += XLOG_BLCKSZ)
1174 if (
write(
fd, buffer.
data, XLOG_BLCKSZ) != XLOG_BLCKSZ)
1178 pg_fatal(
"could not write file \"%s\": %m", path);
1197 printf(
_(
" [-D, --pgdata=]DATADIR data directory\n"));
1198 printf(
_(
" -f, --force force update to be done even after unclean shutdown or\n"
1199 " if pg_control values had to be guessed\n"));
1200 printf(
_(
" -n, --dry-run no update, just show what would be done\n"));
1201 printf(
_(
" -V, --version output version information, then exit\n"));
1202 printf(
_(
" -?, --help show this help, then exit\n"));
1204 printf(
_(
"\nOptions to override control file values:\n"));
1205 printf(
_(
" -c, --commit-timestamp-ids=XID,XID\n"
1206 " set oldest and newest transactions bearing\n"
1207 " commit timestamp (zero means no change)\n"));
1208 printf(
_(
" -e, --epoch=XIDEPOCH set next transaction ID epoch\n"));
1209 printf(
_(
" -l, --next-wal-file=WALFILE set minimum starting location for new WAL\n"));
1210 printf(
_(
" -m, --multixact-ids=MXID,MXID set next and oldest multitransaction ID\n"));
1211 printf(
_(
" -o, --next-oid=OID set next OID\n"));
1212 printf(
_(
" -O, --multixact-offset=OFFSET set next multitransaction offset\n"));
1213 printf(
_(
" -u, --oldest-transaction-id=XID set oldest transaction ID\n"));
1214 printf(
_(
" -x, --next-transaction-id=XID set next transaction ID\n"));
1215 printf(
_(
" --char-signedness=OPTION set char signedness to \"signed\" or \"unsigned\"\n"));
1216 printf(
_(
" --wal-segsize=SIZE size of WAL segments, in megabytes\n"));
1218 printf(
_(
"\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
1219 printf(
_(
"%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
#define ngettext(s, p, n)
#define PG_TEXTDOMAIN(domain)
TransactionId MultiXactId
#define CATALOG_VERSION_NO
void set_pglocale_pgservice(const char *argv0, const char *app)
void update_controlfile(const char *DataDir, ControlFileData *ControlFile, bool do_sync)
struct dirent * readdir(DIR *)
DIR * opendir(const char *)
void * pg_malloc(size_t size)
char * pg_strdup(const char *in)
bool GetDataDirectoryCreatePerm(const char *dataDir)
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
#define required_argument
#define TOAST_MAX_CHUNK_SIZE
void pg_logging_init(const char *argv0)
#define pg_log_error(...)
#define pg_log_error_hint(...)
#define pg_log_error_detail(...)
bool option_parse_int(const char *optarg, const char *optname, int min_range, int max_range, int *result)
#define DEFAULT_XLOG_SEG_SIZE
#define FLOATFORMAT_VALUE
#define PG_CONTROL_VERSION
struct CheckPoint CheckPoint
#define XLOG_CHECKPOINT_SHUTDOWN
#define PG_CONTROL_FILE_SIZE
#define COMP_CRC32C(crc, data, len)
#define EQ_CRC32C(c1, c2)
PGDLLIMPORT char * optarg
static void FindEndOfXLOG(void)
static void RewriteControlFile(void)
int main(int argc, char *argv[])
static void KillExistingWALSummaries(void)
#define WALSUMMARY_NHEXCHARS
static ControlFileData ControlFile
static void KillExistingXLOG(void)
static void CheckDataVersion(void)
static int set_char_signedness
static XLogSegNo minXlogSegNo
static int set_wal_segsize
static MultiXactOffset set_mxoff
static MultiXactId set_mxid
static TimeLineID minXlogTli
static TransactionId set_oldest_xid
static TransactionId set_oldest_commit_ts_xid
static TransactionId set_xid
static void PrintNewControlValues(void)
static XLogSegNo newXlogSegNo
static bool read_controlfile(void)
static void KillExistingArchiveStatus(void)
static void WriteEmptyXLOG(void)
static const char * progname
static void PrintControlValues(bool guessed)
static TransactionId set_newest_commit_ts_xid
static uint32 set_xid_epoch
static void GuessControlValues(void)
#define pg_log_warning(...)
int pg_strcasecmp(const char *s1, const char *s2)
const char * get_progname(const char *argv0)
static int fd(const char *x, int i)
void get_restricted_token(void)
int pg_strip_crlf(char *str)
MultiXactOffset nextMultiOffset
TransactionId newestCommitTsXid
TimeLineID PrevTimeLineID
TimeLineID ThisTimeLineID
TransactionId oldestActiveXid
FullTransactionId nextXid
TransactionId oldestCommitTsXid
uint32 pg_control_version
XLogRecPtr backupStartPoint
bool track_commit_timestamp
CheckPoint checkPointCopy
XLogRecPtr backupEndPoint
XLogRecPtr minRecoveryPoint
uint32 data_checksum_version
bool default_char_signedness
uint32 catalog_version_no
TimeLineID minRecoveryPointTLI
uint32 toast_max_chunk_size
#define InvalidTransactionId
#define EpochFromFullTransactionId(x)
#define XidFromFullTransactionId(x)
#define FirstGenbkiObjectId
#define FirstNormalTransactionId
static FullTransactionId FullTransactionIdFromEpochAndXid(uint32 epoch, TransactionId xid)
#define TransactionIdIsNormal(xid)
int gettimeofday(struct timeval *tp, void *tzp)
#define IsValidWalSegSize(size)
#define XLOG_CONTROL_FILE
static bool IsXLogFileName(const char *fname)
static void XLogFromFileName(const char *fname, TimeLineID *tli, XLogSegNo *logSegNo, int wal_segsz_bytes)
#define XLogSegNoOffsetToRecPtr(segno, offset, wal_segsz_bytes, dest)
XLogPageHeaderData * XLogPageHeader
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)
static void XLogFilePath(char *path, TimeLineID tli, XLogSegNo logSegNo, int wal_segsz_bytes)
#define SizeOfXLogLongPHD
static void XLogFileName(char *fname, TimeLineID tli, XLogSegNo logSegNo, int wal_segsz_bytes)
static bool IsPartialXLogFileName(const char *fname)
#define FirstNormalUnloggedLSN
#define SizeOfXLogRecordDataHeaderShort
#define XLR_BLOCK_ID_DATA_SHORT