89 static void usage(
void);
93 main(
int argc,
char *argv[])
95 static struct option long_options[] = {
113 bool noupdate =
false;
118 char *log_fname = NULL;
127 if (strcmp(argv[1],
"--help") == 0 || strcmp(argv[1],
"-?") == 0)
132 if (strcmp(argv[1],
"--version") == 0 || strcmp(argv[1],
"-V") == 0)
134 puts(
"pg_resetwal (PostgreSQL) " PG_VERSION);
140 while ((
c =
getopt_long(argc, argv,
"c:D:e:fl:m:no:O:u:x:", long_options, NULL)) != -1)
159 if (endptr ==
optarg || *endptr !=
'\0' || errno != 0)
168 pg_fatal(
"transaction ID epoch (-e) must not be -1");
174 if (endptr ==
optarg || *endptr !=
'\0' || errno != 0)
187 if (endptr ==
optarg || *endptr !=
'\0' || errno != 0)
200 if (endptr ==
optarg || *endptr !=
',' || errno != 0)
207 if (endptr2 == endptr + 1 || *endptr2 !=
'\0' || errno != 0)
216 pg_fatal(
"transaction ID (-c) must be either 0 or greater than or equal to 2");
220 pg_fatal(
"transaction ID (-c) must be either 0 or greater than or equal to 2");
226 if (endptr ==
optarg || *endptr !=
'\0' || errno != 0)
239 if (endptr ==
optarg || *endptr !=
',' || errno != 0)
246 set_oldestmxid = strtoul(endptr + 1, &endptr2, 0);
247 if (endptr2 == endptr + 1 || *endptr2 !=
'\0' || errno != 0)
254 pg_fatal(
"multitransaction ID (-m) must not be 0");
260 if (set_oldestmxid == 0)
261 pg_fatal(
"oldest multitransaction ID (-m) must not be 0");
267 if (endptr ==
optarg || *endptr !=
'\0' || errno != 0)
274 pg_fatal(
"multitransaction offset (-O) must not be -1");
295 if (endptr ==
optarg || *endptr !=
'\0' || errno != 0)
296 pg_fatal(
"argument of --wal-segsize must be a number");
298 pg_fatal(
"argument of --wal-segsize must be a power of 2 between 1 and 1024");
314 pg_log_error(
"too many command-line arguments (first is \"%s\")",
347 pg_fatal(
"could not read permissions of directory \"%s\": %m",
353 pg_fatal(
"could not change directory to \"%s\": %m",
363 if ((
fd = open(
"postmaster.pid", O_RDONLY, 0)) < 0)
366 pg_fatal(
"could not open file \"%s\" for reading: %m",
371 pg_log_error(
"lock file \"%s\" exists",
"postmaster.pid");
372 pg_log_error_hint(
"Is a server running? If not, delete the lock file and try again.");
390 if (log_fname != NULL)
402 if ((
guessed && !force) || noupdate)
462 if ((
guessed && !force) || noupdate)
467 printf(
_(
"\nIf these values seem acceptable, use -f to force reset.\n"));
479 printf(
_(
"The database server was not shut down cleanly.\n"
480 "Resetting the write-ahead log might cause data to be lost.\n"
481 "If you want to proceed anyway, use -f to force reset.\n"));
493 printf(
_(
"Write-ahead log reset\n"));
513 const char *ver_file =
"PG_VERSION";
517 if ((ver_fd = fopen(ver_file,
"r")) == NULL)
518 pg_fatal(
"could not open file \"%s\" for reading: %m",
522 if (!fgets(rawline,
sizeof(rawline), ver_fd))
525 pg_fatal(
"unexpected empty file \"%s\"", ver_file);
527 pg_fatal(
"could not read file \"%s\": %m", ver_file);
533 if (strcmp(rawline, PG_MAJORVERSION) != 0)
536 pg_log_error_detail(
"File \"%s\" contains \"%s\", which is not compatible with this program's version \"%s\".",
537 ver_file, rawline, PG_MAJORVERSION);
566 pg_log_error(
"could not open file \"%s\" for reading: %m",
569 pg_log_error_hint(
"If you are sure the data directory path is correct, execute\n"
597 pg_log_warning(
"pg_control exists but has invalid CRC; proceed with caution");
607 "pg_control specifies invalid WAL segment size (%d bytes); proceed with caution",
617 pg_log_warning(
"pg_control exists but is broken or wrong version; ignoring it");
628 uint64 sysidentifier;
645 sysidentifier = ((uint64) tv.tv_sec) << 32;
646 sysidentifier |= ((uint64) tv.tv_usec) << 12;
647 sysidentifier |= getpid() & 0xFFF;
712 printf(
_(
"Guessed pg_control values:\n\n"));
714 printf(
_(
"Current pg_control values:\n\n"));
716 printf(
_(
"pg_control version number: %u\n"),
718 printf(
_(
"Catalog version number: %u\n"),
720 printf(
_(
"Database system identifier: %llu\n"),
722 printf(
_(
"Latest checkpoint's TimeLineID: %u\n"),
724 printf(
_(
"Latest checkpoint's full_page_writes: %s\n"),
726 printf(
_(
"Latest checkpoint's NextXID: %u:%u\n"),
729 printf(
_(
"Latest checkpoint's NextOID: %u\n"),
731 printf(
_(
"Latest checkpoint's NextMultiXactId: %u\n"),
733 printf(
_(
"Latest checkpoint's NextMultiOffset: %u\n"),
735 printf(
_(
"Latest checkpoint's oldestXID: %u\n"),
737 printf(
_(
"Latest checkpoint's oldestXID's DB: %u\n"),
739 printf(
_(
"Latest checkpoint's oldestActiveXID: %u\n"),
741 printf(
_(
"Latest checkpoint's oldestMultiXid: %u\n"),
743 printf(
_(
"Latest checkpoint's oldestMulti's DB: %u\n"),
745 printf(
_(
"Latest checkpoint's oldestCommitTsXid:%u\n"),
747 printf(
_(
"Latest checkpoint's newestCommitTsXid:%u\n"),
749 printf(
_(
"Maximum data alignment: %u\n"),
752 printf(
_(
"Database block size: %u\n"),
754 printf(
_(
"Blocks per segment of large relation: %u\n"),
756 printf(
_(
"WAL block size: %u\n"),
758 printf(
_(
"Bytes per WAL segment: %u\n"),
760 printf(
_(
"Maximum length of identifiers: %u\n"),
762 printf(
_(
"Maximum columns in an index: %u\n"),
764 printf(
_(
"Maximum size of a TOAST chunk: %u\n"),
766 printf(
_(
"Size of a large-object chunk: %u\n"),
769 printf(
_(
"Date/time type storage: %s\n"),
770 _(
"64-bit integers"));
771 printf(
_(
"Float8 argument passing: %s\n"),
773 printf(
_(
"Data page checksum version: %u\n"),
787 printf(
_(
"\n\nValues to be changed:\n\n"));
791 printf(
_(
"First log segment after reset: %s\n"), fname);
795 printf(
_(
"NextMultiXactId: %u\n"),
797 printf(
_(
"OldestMultiXid: %u\n"),
799 printf(
_(
"OldestMulti's DB: %u\n"),
805 printf(
_(
"NextMultiOffset: %u\n"),
821 printf(
_(
"OldestXID's DB: %u\n"),
827 printf(
_(
"NextXID epoch: %u\n"),
833 printf(
_(
"oldestCommitTsXid: %u\n"),
838 printf(
_(
"newestCommitTsXid: %u\n"),
844 printf(
_(
"Bytes per WAL segment: %u\n"),
923 while (errno = 0, (xlde =
readdir(xldir)) != NULL)
976 while (errno = 0, (xlde =
readdir(xldir)) != NULL)
982 if (unlink(path) < 0)
983 pg_fatal(
"could not delete file \"%s\": %m", path);
1001 #define ARCHSTATDIR XLOGDIR "/archive_status"
1011 while (errno = 0, (xlde =
readdir(xldir)) != NULL)
1020 if (unlink(path) < 0)
1021 pg_fatal(
"could not delete file \"%s\": %m", path);
1050 memset(buffer.
data, 0, XLOG_BLCKSZ);
1090 fd = open(path, O_RDWR | O_CREAT | O_EXCL |
PG_BINARY,
1093 pg_fatal(
"could not open file \"%s\": %m", path);
1096 if (
write(
fd, buffer.
data, XLOG_BLCKSZ) != XLOG_BLCKSZ)
1101 pg_fatal(
"could not write file \"%s\": %m", path);
1105 memset(buffer.
data, 0, XLOG_BLCKSZ);
1106 for (nbytes = XLOG_BLCKSZ; nbytes <
WalSegSz; nbytes += XLOG_BLCKSZ)
1109 if (
write(
fd, buffer.
data, XLOG_BLCKSZ) != XLOG_BLCKSZ)
1113 pg_fatal(
"could not write file \"%s\": %m", path);
1130 printf(
_(
" -c, --commit-timestamp-ids=XID,XID\n"
1131 " set oldest and newest transactions bearing\n"
1132 " commit timestamp (zero means no change)\n"));
1133 printf(
_(
" [-D, --pgdata=]DATADIR data directory\n"));
1134 printf(
_(
" -e, --epoch=XIDEPOCH set next transaction ID epoch\n"));
1135 printf(
_(
" -f, --force force update to be done\n"));
1136 printf(
_(
" -l, --next-wal-file=WALFILE set minimum starting location for new WAL\n"));
1137 printf(
_(
" -m, --multixact-ids=MXID,MXID set next and oldest multitransaction ID\n"));
1138 printf(
_(
" -n, --dry-run no update, just show what would be done\n"));
1139 printf(
_(
" -o, --next-oid=OID set next OID\n"));
1140 printf(
_(
" -O, --multixact-offset=OFFSET set next multitransaction offset\n"));
1141 printf(
_(
" -u, --oldest-transaction-id=XID set oldest transaction ID\n"));
1142 printf(
_(
" -V, --version output version information, then exit\n"));
1143 printf(
_(
" -x, --next-transaction-id=XID set next transaction ID\n"));
1144 printf(
_(
" --wal-segsize=SIZE size of WAL segments, in megabytes\n"));
1145 printf(
_(
" -?, --help show this help, then exit\n"));
1146 printf(
_(
"\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
1147 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 *)
char * pg_strdup(const char *in)
void * pg_malloc(size_t size)
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(...)
#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 ControlFileData ControlFile
static void KillExistingXLOG(void)
static void CheckDataVersion(void)
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(...)
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
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