116static void usage(
void);
124 static struct option long_options[] = {
143 bool noupdate =
false;
147 char *log_fname = NULL;
156 if (strcmp(argv[1],
"--help") == 0 || strcmp(argv[1],
"-?") == 0)
161 if (strcmp(argv[1],
"--version") == 0 || strcmp(argv[1],
"-V") == 0)
163 puts(
"pg_resetwal (PostgreSQL) " PG_VERSION);
169 while ((
c =
getopt_long(argc, argv,
"c:D:e:fl:m:no:O:u:x:", long_options, NULL)) != -1)
188 if (endptr ==
optarg || *endptr !=
'\0' || errno != 0)
202 if (endptr ==
optarg || *endptr !=
'\0' || errno != 0)
216 if (endptr ==
optarg || *endptr !=
'\0' || errno != 0)
230 if (endptr ==
optarg || *endptr !=
',' || errno != 0)
237 if (endptr2 == endptr + 1 || *endptr2 !=
'\0' || errno != 0)
257 if (endptr ==
optarg || *endptr !=
'\0' || errno != 0)
271 if (endptr ==
optarg || *endptr !=
',' || errno != 0)
279 if (endptr2 == endptr + 1 || *endptr2 !=
'\0' || errno != 0)
291 pg_fatal(
"next multitransaction ID (-m) must not be 0");
293 pg_fatal(
"oldest multitransaction ID (-m) must not be 0");
300 if (endptr ==
optarg || *endptr !=
'\0' || errno != 0)
332 pg_fatal(
"argument of %s must be a power of two between 1 and 1024",
"--wal-segsize");
347 pg_log_error(
"invalid argument for option %s",
"--char-signedness");
368 pg_log_error(
"too many command-line arguments (first is \"%s\")",
401 pg_fatal(
"could not read permissions of directory \"%s\": %m",
407 pg_fatal(
"could not change directory to \"%s\": %m",
417 if ((
fd = open(
"postmaster.pid", O_RDONLY, 0)) < 0)
420 pg_fatal(
"could not open file \"%s\" for reading: %m",
425 pg_log_error(
"lock file \"%s\" exists",
"postmaster.pid");
426 pg_log_error_hint(
"Is a server running? If not, delete the lock file and try again.");
444 if (log_fname != NULL)
456 if ((
guessed && !force) || noupdate)
529 pg_log_error(
"not proceeding because control file values were guessed");
539 pg_log_error(
"database server was not shut down cleanly");
554 printf(
_(
"Write-ahead log reset\n"));
580 pg_log_error_detail(
"File \"%s\" contains \"%s\", which is not compatible with this program's version \"%s\".",
610 pg_log_error(
"could not open file \"%s\" for reading: %m",
613 pg_log_error_hint(
"If you are sure the data directory path is correct, execute\n"
641 pg_log_warning(
"pg_control exists but has invalid CRC; proceed with caution");
651 "pg_control specifies invalid WAL segment size (%d bytes); proceed with caution",
661 pg_log_warning(
"pg_control exists but is broken or wrong version; ignoring it");
689 sysidentifier = ((
uint64) tv.tv_sec) << 32;
690 sysidentifier |= ((
uint64) tv.tv_usec) << 12;
691 sysidentifier |= getpid() & 0xFFF;
757 printf(
_(
"Guessed pg_control values:\n\n"));
759 printf(
_(
"Current pg_control values:\n\n"));
761 printf(
_(
"pg_control version number: %u\n"),
763 printf(
_(
"Catalog version number: %u\n"),
765 printf(
_(
"Database system identifier: %" PRIu64
"\n"),
767 printf(
_(
"Latest checkpoint's TimeLineID: %u\n"),
769 printf(
_(
"Latest checkpoint's full_page_writes: %s\n"),
771 printf(
_(
"Latest checkpoint's NextXID: %u:%u\n"),
774 printf(
_(
"Latest checkpoint's NextOID: %u\n"),
776 printf(
_(
"Latest checkpoint's NextMultiXactId: %u\n"),
778 printf(
_(
"Latest checkpoint's NextMultiOffset: %" PRIu64
"\n"),
780 printf(
_(
"Latest checkpoint's oldestXID: %u\n"),
782 printf(
_(
"Latest checkpoint's oldestXID's DB: %u\n"),
784 printf(
_(
"Latest checkpoint's oldestActiveXID: %u\n"),
786 printf(
_(
"Latest checkpoint's oldestMultiXid: %u\n"),
788 printf(
_(
"Latest checkpoint's oldestMulti's DB: %u\n"),
790 printf(
_(
"Latest checkpoint's oldestCommitTsXid:%u\n"),
792 printf(
_(
"Latest checkpoint's newestCommitTsXid:%u\n"),
794 printf(
_(
"Maximum data alignment: %u\n"),
797 printf(
_(
"Database block size: %u\n"),
799 printf(
_(
"Blocks per segment of large relation: %u\n"),
801 printf(
_(
"Pages per SLRU segment: %u\n"),
803 printf(
_(
"WAL block size: %u\n"),
805 printf(
_(
"Bytes per WAL segment: %u\n"),
807 printf(
_(
"Maximum length of identifiers: %u\n"),
809 printf(
_(
"Maximum columns in an index: %u\n"),
811 printf(
_(
"Maximum size of a TOAST chunk: %u\n"),
813 printf(
_(
"Size of a large-object chunk: %u\n"),
816 printf(
_(
"Date/time type storage: %s\n"),
817 _(
"64-bit integers"));
818 printf(
_(
"Float8 argument passing: %s\n"),
820 printf(
_(
"Data page checksum version: %u\n"),
822 printf(
_(
"Default char data signedness: %s\n"),
836 printf(
_(
"\n\nValues to be changed:\n\n"));
840 printf(
_(
"First log segment after reset: %s\n"), fname);
844 printf(
_(
"NextMultiXactId: %u\n"),
846 printf(
_(
"OldestMultiXid: %u\n"),
848 printf(
_(
"OldestMulti's DB: %u\n"),
854 printf(
_(
"NextMultiOffset: %" PRIu64
"\n"),
874 printf(
_(
"OldestXID's DB: %u\n"),
880 printf(
_(
"NextXID epoch: %u\n"),
886 printf(
_(
"oldestCommitTsXid: %u\n"),
888 printf(
_(
"newestCommitTsXid: %u\n"),
894 printf(
_(
"Bytes per WAL segment: %u\n"),
973 while (errno = 0, (xlde =
readdir(xldir)) != NULL)
1026 while (errno = 0, (xlde =
readdir(xldir)) != NULL)
1032 if (unlink(path) < 0)
1033 pg_fatal(
"could not delete file \"%s\": %m", path);
1051#define ARCHSTATDIR XLOGDIR "/archive_status"
1061 while (errno = 0, (xlde =
readdir(xldir)) != NULL)
1070 if (unlink(path) < 0)
1071 pg_fatal(
"could not delete file \"%s\": %m", path);
1088#define WALSUMMARYDIR XLOGDIR "/summaries"
1089#define WALSUMMARY_NHEXCHARS 40
1099 while (errno = 0, (xlde =
readdir(xldir)) != NULL)
1105 if (unlink(path) < 0)
1106 pg_fatal(
"could not delete file \"%s\": %m", path);
1134 memset(buffer.
data, 0, XLOG_BLCKSZ);
1174 fd = open(path, O_RDWR | O_CREAT | O_EXCL |
PG_BINARY,
1177 pg_fatal(
"could not open file \"%s\": %m", path);
1180 if (
write(
fd, buffer.
data, XLOG_BLCKSZ) != XLOG_BLCKSZ)
1185 pg_fatal(
"could not write file \"%s\": %m", path);
1189 memset(buffer.
data, 0, XLOG_BLCKSZ);
1190 for (nbytes = XLOG_BLCKSZ; nbytes <
WalSegSz; nbytes += XLOG_BLCKSZ)
1193 if (
write(
fd, buffer.
data, XLOG_BLCKSZ) != XLOG_BLCKSZ)
1197 pg_fatal(
"could not write file \"%s\": %m", path);
1216 printf(
_(
" [-D, --pgdata=]DATADIR data directory\n"));
1217 printf(
_(
" -f, --force force update to be done even after unclean shutdown or\n"
1218 " if pg_control values had to be guessed\n"));
1219 printf(
_(
" -n, --dry-run no update, just show what would be done\n"));
1220 printf(
_(
" -V, --version output version information, then exit\n"));
1221 printf(
_(
" -?, --help show this help, then exit\n"));
1223 printf(
_(
"\nOptions to override control file values:\n"));
1224 printf(
_(
" -c, --commit-timestamp-ids=XID,XID\n"
1225 " set oldest and newest transactions bearing\n"
1226 " commit timestamp (zero means no change)\n"));
1227 printf(
_(
" -e, --epoch=XIDEPOCH set next transaction ID epoch\n"));
1228 printf(
_(
" -l, --next-wal-file=WALFILE set minimum starting location for new WAL\n"));
1229 printf(
_(
" -m, --multixact-ids=MXID,MXID set next and oldest multitransaction ID\n"));
1230 printf(
_(
" -o, --next-oid=OID set next OID\n"));
1231 printf(
_(
" -O, --multixact-offset=OFFSET set next multitransaction offset\n"));
1232 printf(
_(
" -u, --oldest-transaction-id=XID set oldest transaction ID\n"));
1233 printf(
_(
" -x, --next-transaction-id=XID set next transaction ID\n"));
1234 printf(
_(
" --char-signedness=OPTION set char signedness to \"signed\" or \"unsigned\"\n"));
1235 printf(
_(
" --wal-segsize=SIZE size of WAL segments, in megabytes\n"));
1237 printf(
_(
"\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
1238 printf(
_(
"%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
1252 while (isspace((
unsigned char) *s))
1259 is_neg = (*s ==
'-');
1261 val = strtoul(s, endptr, base);
1264 if (errno == 0 && is_neg)
1293 while (isspace((
unsigned char) *s))
1300 is_neg = (*s ==
'-');
1302 val = strtou64(s, endptr, base);
1305 if (errno == 0 && is_neg)
#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)
uint32 get_pg_version(const char *datadir, char **version_str)
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 SLRU_PAGES_PER_SEGMENT
#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)
static bool wal_segsize_given
#define WALSUMMARY_NHEXCHARS
static TransactionId oldest_commit_ts_xid_val
static uint32 next_xid_epoch_val
static ControlFileData ControlFile
static MultiXactId oldest_mxid_val
static void KillExistingXLOG(void)
static void CheckDataVersion(void)
static bool char_signedness_given
static bool next_xid_epoch_given
static bool commit_ts_xids_given
static uint32 strtouint32_strict(const char *restrict s, char **restrict endptr, int base)
static XLogSegNo minXlogSegNo
static bool next_oid_given
static TransactionId newest_commit_ts_xid_val
static TimeLineID minXlogTli
static bool next_xid_given
static TransactionId oldest_xid_val
static void PrintNewControlValues(void)
static uint64 strtouint64_strict(const char *restrict s, char **restrict endptr, int base)
static XLogSegNo newXlogSegNo
static bool char_signedness_val
static MultiXactOffset next_mxoff_val
static bool read_controlfile(void)
static int wal_segsize_val
static void KillExistingArchiveStatus(void)
static void WriteEmptyXLOG(void)
static const char * progname
static void PrintControlValues(bool guessed)
static bool oldest_xid_given
static TransactionId next_xid_val
static bool next_mxoff_given
static MultiXactId next_mxid_val
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)
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
uint32 slru_pages_per_segment
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)
#define GET_PG_MAJORVERSION_NUM(v)
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