1 /*-------------------------------------------------------------------------
4 * A utility to "zero out" the xlog when it's corrupt beyond recovery.
5 * Can also rebuild pg_control if needed.
7 * The theory of operation is fairly simple:
8 * 1. Read the existing pg_control (which will include the last
9 * checkpoint record). If it is an old format then update to
11 * 2. If pg_control is corrupt, attempt to intuit reasonable values,
12 * by scanning the old xlog if necessary.
13 * 3. Modify pg_control to reflect a "shutdown" state with a checkpoint
14 * record at the start of xlog.
15 * 4. Flush the existing xlog files and write a new segment with
16 * just a checkpoint record in it. The new segment is positioned
17 * just past the end of the old xlog, so that existing LSNs in
18 * data pages will appear to be "in the past".
19 * This is all pretty straightforward except for the intuition part of
23 * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
24 * Portions Copyright (c) 1994, Regents of the University of California
26 * src/bin/pg_resetwal/pg_resetwal.c
28 *-------------------------------------------------------------------------
32 * We have to use postgres.h not postgres_fe.h here, because there's so much
33 * backend-only stuff in the XLOG include files we need. But we need a
34 * frontend-ish environment otherwise. Hence this ugly hack.
47 #include "access/transam.h"
48 #include "access/tuptoaster.h"
49 #include "access/multixact.h"
50 #include "access/xlog.h"
51 #include "access/xlog_internal.h"
52 #include "catalog/catversion.h"
53 #include "catalog/pg_control.h"
54 #include "common/fe_memutils.h"
55 #include "common/restricted_token.h"
56 #include "storage/large_object.h"
57 #include "pg_getopt.h"
60 static ControlFileData ControlFile; /* pg_control values */
61 static XLogSegNo newXlogSegNo; /* new XLOG segment # */
62 static bool guessed = false; /* T if we had to guess at any values */
63 static const char *progname;
64 static uint32 set_xid_epoch = (uint32) -1;
65 static TransactionId set_xid = 0;
66 static TransactionId set_oldest_commit_ts_xid = 0;
67 static TransactionId set_newest_commit_ts_xid = 0;
68 static Oid set_oid = 0;
69 static MultiXactId set_mxid = 0;
70 static MultiXactOffset set_mxoff = (MultiXactOffset) -1;
71 static uint32 minXlogTli = 0;
72 static XLogSegNo minXlogSegNo = 0;
75 static void CheckDataVersion(void);
76 static bool ReadControlFile(void);
77 static void GuessControlValues(void);
78 static void PrintControlValues(bool guessed);
79 static void PrintNewControlValues(void);
80 static void RewriteControlFile(void);
81 static void FindEndOfXLOG(void);
82 static void KillExistingXLOG(void);
83 static void KillExistingArchiveStatus(void);
84 static void WriteEmptyXLOG(void);
85 static void usage(void);
89 main(int argc, char *argv[])
91 static struct option long_options[] = {
92 {"commit-timestamp-ids", required_argument, NULL, 'c'},
93 {"pgdata", required_argument, NULL, 'D'},
94 {"epoch", required_argument, NULL, 'e'},
95 {"force", no_argument, NULL, 'f'},
96 {"next-wal-file", required_argument, NULL, 'l'},
97 {"multixact-ids", required_argument, NULL, 'm'},
98 {"dry-run", no_argument, NULL, 'n'},
99 {"next-oid", required_argument, NULL, 'o'},
100 {"multixact-offset", required_argument, NULL, 'O'},
101 {"next-transaction-id", required_argument, NULL, 'x'},
107 bool noupdate = false;
108 MultiXactId set_oldestmxid = 0;
111 char *DataDir = NULL;
112 char *log_fname = NULL;
115 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_resetwal"));
117 progname = get_progname(argv[0]);
121 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
126 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
128 puts("pg_resetwal (PostgreSQL) " PG_VERSION);
134 while ((c = getopt_long(argc, argv, "c:D:e:fl:m:no:O:x:", long_options, NULL)) != -1)
151 set_xid_epoch = strtoul(optarg, &endptr, 0);
152 if (endptr == optarg || *endptr != '\0')
155 translator: the second %s is a command line argument (-e, etc) */
156 fprintf(stderr, _("%s: invalid argument for option %s\n"), progname, "-e");
157 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
160 if (set_xid_epoch == -1)
162 fprintf(stderr, _("%s: transaction ID epoch (-e) must not be -1\n"), progname);
168 set_xid = strtoul(optarg, &endptr, 0);
169 if (endptr == optarg || *endptr != '\0')
171 fprintf(stderr, _("%s: invalid argument for option %s\n"), progname, "-x");
172 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
177 fprintf(stderr, _("%s: transaction ID (-x) must not be 0\n"), progname);
183 set_oldest_commit_ts_xid = strtoul(optarg, &endptr, 0);
184 if (endptr == optarg || *endptr != ',')
186 fprintf(stderr, _("%s: invalid argument for option %s\n"), progname, "-c");
187 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
190 set_newest_commit_ts_xid = strtoul(endptr + 1, &endptr2, 0);
191 if (endptr2 == endptr + 1 || *endptr2 != '\0')
193 fprintf(stderr, _("%s: invalid argument for option %s\n"), progname, "-c");
194 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
198 if (set_oldest_commit_ts_xid < 2 &&
199 set_oldest_commit_ts_xid != 0)
201 fprintf(stderr, _("%s: transaction ID (-c) must be either 0 or greater than or equal to 2\n"), progname);
205 if (set_newest_commit_ts_xid < 2 &&
206 set_newest_commit_ts_xid != 0)
208 fprintf(stderr, _("%s: transaction ID (-c) must be either 0 or greater than or equal to 2\n"), progname);
214 set_oid = strtoul(optarg, &endptr, 0);
215 if (endptr == optarg || *endptr != '\0')
217 fprintf(stderr, _("%s: invalid argument for option %s\n"), progname, "-o");
218 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
223 fprintf(stderr, _("%s: OID (-o) must not be 0\n"), progname);
229 set_mxid = strtoul(optarg, &endptr, 0);
230 if (endptr == optarg || *endptr != ',')
232 fprintf(stderr, _("%s: invalid argument for option %s\n"), progname, "-m");
233 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
237 set_oldestmxid = strtoul(endptr + 1, &endptr2, 0);
238 if (endptr2 == endptr + 1 || *endptr2 != '\0')
240 fprintf(stderr, _("%s: invalid argument for option %s\n"), progname, "-m");
241 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
246 fprintf(stderr, _("%s: multitransaction ID (-m) must not be 0\n"), progname);
251 * XXX It'd be nice to have more sanity checks here, e.g. so
252 * that oldest is not wrapped around w.r.t. nextMulti.
254 if (set_oldestmxid == 0)
256 fprintf(stderr, _("%s: oldest multitransaction ID (-m) must not be 0\n"),
263 set_mxoff = strtoul(optarg, &endptr, 0);
264 if (endptr == optarg || *endptr != '\0')
266 fprintf(stderr, _("%s: invalid argument for option %s\n"), progname, "-O");
267 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
272 fprintf(stderr, _("%s: multitransaction offset (-O) must not be -1\n"), progname);
278 if (strspn(optarg, "01234567890ABCDEFabcdef") != XLOG_FNAME_LEN)
280 fprintf(stderr, _("%s: invalid argument for option %s\n"), progname, "-l");
281 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
286 * XLogFromFileName requires wal segment size which is not yet
287 * set. Hence wal details are set later on.
289 log_fname = pg_strdup(optarg);
293 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
298 if (DataDir == NULL && optind < argc)
299 DataDir = argv[optind++];
301 /* Complain if any arguments remain */
304 fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
305 progname, argv[optind]);
306 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
313 fprintf(stderr, _("%s: no data directory specified\n"), progname);
314 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
319 * Don't allow pg_resetwal to be run as root, to avoid overwriting the
320 * ownership of files in the data directory. We need only check for root
321 * -- any other user won't have sufficient permissions to modify files in
322 * the data directory.
327 fprintf(stderr, _("%s: cannot be executed by \"root\"\n"),
329 fprintf(stderr, _("You must run %s as the PostgreSQL superuser.\n"),
335 get_restricted_token(progname);
337 if (chdir(DataDir) < 0)
339 fprintf(stderr, _("%s: could not change directory to \"%s\": %s\n"),
340 progname, DataDir, strerror(errno));
344 /* Check that data directory matches our server version */
348 * Check for a postmaster lock file --- if there is one, refuse to
349 * proceed, on grounds we might be interfering with a live installation.
351 if ((fd = open("postmaster.pid", O_RDONLY, 0)) < 0)
355 fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
356 progname, "postmaster.pid", strerror(errno));
362 fprintf(stderr, _("%s: lock file \"%s\" exists\n"
363 "Is a server running? If not, delete the lock file and try again.\n"),
364 progname, "postmaster.pid");
369 * Attempt to read the existing pg_control file
371 if (!ReadControlFile())
372 GuessControlValues();
374 if (log_fname != NULL)
375 XLogFromFileName(log_fname, &minXlogTli, &minXlogSegNo, WalSegSz);
378 * Also look at existing segment files to set up newXlogSegNo
383 * If we're not going to proceed with the reset, print the current control
386 if ((guessed && !force) || noupdate)
387 PrintControlValues(guessed);
390 * Adjust fields if required by switches. (Do this now so that printout,
391 * if any, includes these values.)
393 if (set_xid_epoch != -1)
394 ControlFile.checkPointCopy.nextXidEpoch = set_xid_epoch;
398 ControlFile.checkPointCopy.nextXid = set_xid;
401 * For the moment, just set oldestXid to a value that will force
402 * immediate autovacuum-for-wraparound. It's not clear whether adding
403 * user control of this is useful, so let's just do something that's
404 * reasonably safe. The magic constant here corresponds to the
405 * maximum allowed value of autovacuum_freeze_max_age.
407 ControlFile.checkPointCopy.oldestXid = set_xid - 2000000000;
408 if (ControlFile.checkPointCopy.oldestXid < FirstNormalTransactionId)
409 ControlFile.checkPointCopy.oldestXid += FirstNormalTransactionId;
410 ControlFile.checkPointCopy.oldestXidDB = InvalidOid;
413 if (set_oldest_commit_ts_xid != 0)
414 ControlFile.checkPointCopy.oldestCommitTsXid = set_oldest_commit_ts_xid;
415 if (set_newest_commit_ts_xid != 0)
416 ControlFile.checkPointCopy.newestCommitTsXid = set_newest_commit_ts_xid;
419 ControlFile.checkPointCopy.nextOid = set_oid;
423 ControlFile.checkPointCopy.nextMulti = set_mxid;
425 ControlFile.checkPointCopy.oldestMulti = set_oldestmxid;
426 if (ControlFile.checkPointCopy.oldestMulti < FirstMultiXactId)
427 ControlFile.checkPointCopy.oldestMulti += FirstMultiXactId;
428 ControlFile.checkPointCopy.oldestMultiDB = InvalidOid;
432 ControlFile.checkPointCopy.nextMultiOffset = set_mxoff;
434 if (minXlogTli > ControlFile.checkPointCopy.ThisTimeLineID)
436 ControlFile.checkPointCopy.ThisTimeLineID = minXlogTli;
437 ControlFile.checkPointCopy.PrevTimeLineID = minXlogTli;
440 if (minXlogSegNo > newXlogSegNo)
441 newXlogSegNo = minXlogSegNo;
444 * If we had to guess anything, and -f was not given, just print the
445 * guessed values and exit. Also print if -n is given.
447 if ((guessed && !force) || noupdate)
449 PrintNewControlValues();
452 printf(_("\nIf these values seem acceptable, use -f to force reset.\n"));
460 * Don't reset from a dirty pg_control without -f, either.
462 if (ControlFile.state != DB_SHUTDOWNED && !force)
464 printf(_("The database server was not shut down cleanly.\n"
465 "Resetting the write-ahead log might cause data to be lost.\n"
466 "If you want to proceed anyway, use -f to force reset.\n"));
471 * Else, do the dirty deed.
473 RewriteControlFile();
475 KillExistingArchiveStatus();
478 printf(_("Write-ahead log reset\n"));
484 * Look at the version string stored in PG_VERSION and decide if this utility
485 * can be run safely or not.
487 * We don't want to inject pg_control and WAL files that are for a different
488 * major version; that can't do anything good. Note that we don't treat
489 * mismatching version info in pg_control as a reason to bail out, because
490 * recovering from a corrupted pg_control is one of the main reasons for this
491 * program to exist at all. However, PG_VERSION is unlikely to get corrupted,
492 * and if it were it would be easy to fix by hand. So let's make this check
493 * to prevent simple user errors.
496 CheckDataVersion(void)
498 const char *ver_file = "PG_VERSION";
503 if ((ver_fd = fopen(ver_file, "r")) == NULL)
505 fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
506 progname, ver_file, strerror(errno));
510 /* version number has to be the first line read */
511 if (!fgets(rawline, sizeof(rawline), ver_fd))
515 fprintf(stderr, _("%s: unexpected empty file \"%s\"\n"),
520 fprintf(stderr, _("%s: could not read file \"%s\": %s\n"),
521 progname, ver_file, strerror(errno));
526 /* remove trailing newline, handling Windows newlines as well */
527 len = strlen(rawline);
528 if (len > 0 && rawline[len - 1] == '\n')
530 rawline[--len] = '\0';
531 if (len > 0 && rawline[len - 1] == '\r')
532 rawline[--len] = '\0';
535 if (strcmp(rawline, PG_MAJORVERSION) != 0)
537 fprintf(stderr, _("%s: data directory is of wrong version\n"
538 "File \"%s\" contains \"%s\", which is not compatible with this program's version \"%s\".\n"),
539 progname, ver_file, rawline, PG_MAJORVERSION);
548 * Try to read the existing pg_control file.
550 * This routine is also responsible for updating old pg_control versions
551 * to the current format. (Currently we don't do anything of the sort.)
554 ReadControlFile(void)
561 if ((fd = open(XLOG_CONTROL_FILE, O_RDONLY | PG_BINARY, 0)) < 0)
564 * If pg_control is not there at all, or we can't read it, the odds
565 * are we've been handed a bad DataDir path, so give up. User can do
566 * "touch pg_control" to force us to proceed.
568 fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
569 progname, XLOG_CONTROL_FILE, strerror(errno));
571 fprintf(stderr, _("If you are sure the data directory path is correct, execute\n"
578 /* Use malloc to ensure we have a maxaligned buffer */
579 buffer = (char *) pg_malloc(PG_CONTROL_FILE_SIZE);
581 len = read(fd, buffer, PG_CONTROL_FILE_SIZE);
584 fprintf(stderr, _("%s: could not read file \"%s\": %s\n"),
585 progname, XLOG_CONTROL_FILE, strerror(errno));
590 if (len >= sizeof(ControlFileData) &&
591 ((ControlFileData *) buffer)->pg_control_version == PG_CONTROL_VERSION)
597 offsetof(ControlFileData, crc));
600 if (!EQ_CRC32C(crc, ((ControlFileData *) buffer)->crc))
602 /* We will use the data but treat it as guessed. */
604 _("%s: pg_control exists but has invalid CRC; proceed with caution\n"),
609 memcpy(&ControlFile, buffer, sizeof(ControlFile));
610 WalSegSz = ControlFile.xlog_seg_size;
612 /* return false if WalSegSz is not valid */
613 if (!IsValidWalSegSize(WalSegSz))
616 _("%s: pg_control specifies invalid WAL segment size (%d bytes); proceed with caution \n"),
624 /* Looks like it's a mess. */
625 fprintf(stderr, _("%s: pg_control exists but is broken or wrong version; ignoring it\n"),
632 * Guess at pg_control values when we can't read the old ones.
635 GuessControlValues(void)
637 uint64 sysidentifier;
641 * Set up a completely default set of pg_control values.
644 memset(&ControlFile, 0, sizeof(ControlFile));
646 ControlFile.pg_control_version = PG_CONTROL_VERSION;
647 ControlFile.catalog_version_no = CATALOG_VERSION_NO;
650 * Create a new unique installation identifier, since we can no longer use
651 * any old XLOG records. See notes in xlog.c about the algorithm.
653 gettimeofday(&tv, NULL);
654 sysidentifier = ((uint64) tv.tv_sec) << 32;
655 sysidentifier |= ((uint64) tv.tv_usec) << 12;
656 sysidentifier |= getpid() & 0xFFF;
658 ControlFile.system_identifier = sysidentifier;
660 ControlFile.checkPointCopy.redo = SizeOfXLogLongPHD;
661 ControlFile.checkPointCopy.ThisTimeLineID = 1;
662 ControlFile.checkPointCopy.PrevTimeLineID = 1;
663 ControlFile.checkPointCopy.fullPageWrites = false;
664 ControlFile.checkPointCopy.nextXidEpoch = 0;
665 ControlFile.checkPointCopy.nextXid = FirstNormalTransactionId;
666 ControlFile.checkPointCopy.nextOid = FirstBootstrapObjectId;
667 ControlFile.checkPointCopy.nextMulti = FirstMultiXactId;
668 ControlFile.checkPointCopy.nextMultiOffset = 0;
669 ControlFile.checkPointCopy.oldestXid = FirstNormalTransactionId;
670 ControlFile.checkPointCopy.oldestXidDB = InvalidOid;
671 ControlFile.checkPointCopy.oldestMulti = FirstMultiXactId;
672 ControlFile.checkPointCopy.oldestMultiDB = InvalidOid;
673 ControlFile.checkPointCopy.time = (pg_time_t) time(NULL);
674 ControlFile.checkPointCopy.oldestActiveXid = InvalidTransactionId;
676 ControlFile.state = DB_SHUTDOWNED;
677 ControlFile.time = (pg_time_t) time(NULL);
678 ControlFile.checkPoint = ControlFile.checkPointCopy.redo;
679 ControlFile.unloggedLSN = 1;
681 /* minRecoveryPoint, backupStartPoint and backupEndPoint can be left zero */
683 ControlFile.wal_level = WAL_LEVEL_MINIMAL;
684 ControlFile.wal_log_hints = false;
685 ControlFile.track_commit_timestamp = false;
686 ControlFile.MaxConnections = 100;
687 ControlFile.max_worker_processes = 8;
688 ControlFile.max_prepared_xacts = 0;
689 ControlFile.max_locks_per_xact = 64;
691 ControlFile.maxAlign = MAXIMUM_ALIGNOF;
692 ControlFile.floatFormat = FLOATFORMAT_VALUE;
693 ControlFile.blcksz = BLCKSZ;
694 ControlFile.relseg_size = RELSEG_SIZE;
695 WalSegSz = ControlFile.xlog_blcksz = XLOG_BLCKSZ;
696 ControlFile.xlog_seg_size = DEFAULT_XLOG_SEG_SIZE;
697 ControlFile.nameDataLen = NAMEDATALEN;
698 ControlFile.indexMaxKeys = INDEX_MAX_KEYS;
699 ControlFile.toast_max_chunk_size = TOAST_MAX_CHUNK_SIZE;
700 ControlFile.loblksize = LOBLKSIZE;
701 ControlFile.float4ByVal = FLOAT4PASSBYVAL;
702 ControlFile.float8ByVal = FLOAT8PASSBYVAL;
705 * XXX eventually, should try to grovel through old XLOG to develop more
706 * accurate values for TimeLineID, nextXID, etc.
712 * Print the guessed pg_control values when we had to guess.
714 * NB: this display should be just those fields that will not be
715 * reset by RewriteControlFile().
718 PrintControlValues(bool guessed)
720 char sysident_str[32];
723 printf(_("Guessed pg_control values:\n\n"));
725 printf(_("Current pg_control values:\n\n"));
728 * Format system_identifier separately to keep platform-dependent format
729 * code out of the translatable message string.
731 snprintf(sysident_str, sizeof(sysident_str), UINT64_FORMAT,
732 ControlFile.system_identifier);
734 printf(_("pg_control version number: %u\n"),
735 ControlFile.pg_control_version);
736 printf(_("Catalog version number: %u\n"),
737 ControlFile.catalog_version_no);
738 printf(_("Database system identifier: %s\n"),
740 printf(_("Latest checkpoint's TimeLineID: %u\n"),
741 ControlFile.checkPointCopy.ThisTimeLineID);
742 printf(_("Latest checkpoint's full_page_writes: %s\n"),
743 ControlFile.checkPointCopy.fullPageWrites ? _("on") : _("off"));
744 printf(_("Latest checkpoint's NextXID: %u:%u\n"),
745 ControlFile.checkPointCopy.nextXidEpoch,
746 ControlFile.checkPointCopy.nextXid);
747 printf(_("Latest checkpoint's NextOID: %u\n"),
748 ControlFile.checkPointCopy.nextOid);
749 printf(_("Latest checkpoint's NextMultiXactId: %u\n"),
750 ControlFile.checkPointCopy.nextMulti);
751 printf(_("Latest checkpoint's NextMultiOffset: %u\n"),
752 ControlFile.checkPointCopy.nextMultiOffset);
753 printf(_("Latest checkpoint's oldestXID: %u\n"),
754 ControlFile.checkPointCopy.oldestXid);
755 printf(_("Latest checkpoint's oldestXID's DB: %u\n"),
756 ControlFile.checkPointCopy.oldestXidDB);
757 printf(_("Latest checkpoint's oldestActiveXID: %u\n"),
758 ControlFile.checkPointCopy.oldestActiveXid);
759 printf(_("Latest checkpoint's oldestMultiXid: %u\n"),
760 ControlFile.checkPointCopy.oldestMulti);
761 printf(_("Latest checkpoint's oldestMulti's DB: %u\n"),
762 ControlFile.checkPointCopy.oldestMultiDB);
763 printf(_("Latest checkpoint's oldestCommitTsXid:%u\n"),
764 ControlFile.checkPointCopy.oldestCommitTsXid);
765 printf(_("Latest checkpoint's newestCommitTsXid:%u\n"),
766 ControlFile.checkPointCopy.newestCommitTsXid);
767 printf(_("Maximum data alignment: %u\n"),
768 ControlFile.maxAlign);
769 /* we don't print floatFormat since can't say much useful about it */
770 printf(_("Database block size: %u\n"),
772 printf(_("Blocks per segment of large relation: %u\n"),
773 ControlFile.relseg_size);
774 printf(_("WAL block size: %u\n"),
775 ControlFile.xlog_blcksz);
776 printf(_("Bytes per WAL segment: %u\n"),
777 ControlFile.xlog_seg_size);
778 printf(_("Maximum length of identifiers: %u\n"),
779 ControlFile.nameDataLen);
780 printf(_("Maximum columns in an index: %u\n"),
781 ControlFile.indexMaxKeys);
782 printf(_("Maximum size of a TOAST chunk: %u\n"),
783 ControlFile.toast_max_chunk_size);
784 printf(_("Size of a large-object chunk: %u\n"),
785 ControlFile.loblksize);
786 /* This is no longer configurable, but users may still expect to see it: */
787 printf(_("Date/time type storage: %s\n"),
788 _("64-bit integers"));
789 printf(_("Float4 argument passing: %s\n"),
790 (ControlFile.float4ByVal ? _("by value") : _("by reference")));
791 printf(_("Float8 argument passing: %s\n"),
792 (ControlFile.float8ByVal ? _("by value") : _("by reference")));
793 printf(_("Data page checksum version: %u\n"),
794 ControlFile.data_checksum_version);
799 * Print the values to be changed.
802 PrintNewControlValues(void)
804 char fname[MAXFNAMELEN];
806 /* This will be always printed in order to keep format same. */
807 printf(_("\n\nValues to be changed:\n\n"));
809 XLogFileName(fname, ControlFile.checkPointCopy.ThisTimeLineID,
810 newXlogSegNo, WalSegSz);
811 printf(_("First log segment after reset: %s\n"), fname);
815 printf(_("NextMultiXactId: %u\n"),
816 ControlFile.checkPointCopy.nextMulti);
817 printf(_("OldestMultiXid: %u\n"),
818 ControlFile.checkPointCopy.oldestMulti);
819 printf(_("OldestMulti's DB: %u\n"),
820 ControlFile.checkPointCopy.oldestMultiDB);
825 printf(_("NextMultiOffset: %u\n"),
826 ControlFile.checkPointCopy.nextMultiOffset);
831 printf(_("NextOID: %u\n"),
832 ControlFile.checkPointCopy.nextOid);
837 printf(_("NextXID: %u\n"),
838 ControlFile.checkPointCopy.nextXid);
839 printf(_("OldestXID: %u\n"),
840 ControlFile.checkPointCopy.oldestXid);
841 printf(_("OldestXID's DB: %u\n"),
842 ControlFile.checkPointCopy.oldestXidDB);
845 if (set_xid_epoch != -1)
847 printf(_("NextXID epoch: %u\n"),
848 ControlFile.checkPointCopy.nextXidEpoch);
851 if (set_oldest_commit_ts_xid != 0)
853 printf(_("oldestCommitTsXid: %u\n"),
854 ControlFile.checkPointCopy.oldestCommitTsXid);
856 if (set_newest_commit_ts_xid != 0)
858 printf(_("newestCommitTsXid: %u\n"),
859 ControlFile.checkPointCopy.newestCommitTsXid);
865 * Write out the new pg_control file.
868 RewriteControlFile(void)
871 char buffer[PG_CONTROL_FILE_SIZE]; /* need not be aligned */
874 * For good luck, apply the same static assertions as in backend's
875 * WriteControlFile().
877 StaticAssertStmt(sizeof(ControlFileData) <= PG_CONTROL_MAX_SAFE_SIZE,
878 "pg_control is too large for atomic disk writes");
879 StaticAssertStmt(sizeof(ControlFileData) <= PG_CONTROL_FILE_SIZE,
880 "sizeof(ControlFileData) exceeds PG_CONTROL_FILE_SIZE");
883 * Adjust fields as needed to force an empty XLOG starting at
886 XLogSegNoOffsetToRecPtr(newXlogSegNo, SizeOfXLogLongPHD,
887 ControlFile.checkPointCopy.redo, WalSegSz);
888 ControlFile.checkPointCopy.time = (pg_time_t) time(NULL);
890 ControlFile.state = DB_SHUTDOWNED;
891 ControlFile.time = (pg_time_t) time(NULL);
892 ControlFile.checkPoint = ControlFile.checkPointCopy.redo;
893 ControlFile.minRecoveryPoint = 0;
894 ControlFile.minRecoveryPointTLI = 0;
895 ControlFile.backupStartPoint = 0;
896 ControlFile.backupEndPoint = 0;
897 ControlFile.backupEndRequired = false;
900 * Force the defaults for max_* settings. The values don't really matter
901 * as long as wal_level='minimal'; the postmaster will reset these fields
904 ControlFile.wal_level = WAL_LEVEL_MINIMAL;
905 ControlFile.wal_log_hints = false;
906 ControlFile.track_commit_timestamp = false;
907 ControlFile.MaxConnections = 100;
908 ControlFile.max_worker_processes = 8;
909 ControlFile.max_prepared_xacts = 0;
910 ControlFile.max_locks_per_xact = 64;
912 /* Now we can force the recorded xlog seg size to the right thing. */
913 ControlFile.xlog_seg_size = WalSegSz;
915 /* Contents are protected with a CRC */
916 INIT_CRC32C(ControlFile.crc);
917 COMP_CRC32C(ControlFile.crc,
918 (char *) &ControlFile,
919 offsetof(ControlFileData, crc));
920 FIN_CRC32C(ControlFile.crc);
923 * We write out PG_CONTROL_FILE_SIZE bytes into pg_control, zero-padding
924 * the excess over sizeof(ControlFileData). This reduces the odds of
925 * premature-EOF errors when reading pg_control. We'll still fail when we
926 * check the contents of the file, but hopefully with a more specific
927 * error than "couldn't read pg_control".
929 memset(buffer, 0, PG_CONTROL_FILE_SIZE);
930 memcpy(buffer, &ControlFile, sizeof(ControlFileData));
932 unlink(XLOG_CONTROL_FILE);
934 fd = open(XLOG_CONTROL_FILE,
935 O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
939 fprintf(stderr, _("%s: could not create pg_control file: %s\n"),
940 progname, strerror(errno));
945 if (write(fd, buffer, PG_CONTROL_FILE_SIZE) != PG_CONTROL_FILE_SIZE)
947 /* if write didn't set errno, assume problem is no disk space */
950 fprintf(stderr, _("%s: could not write pg_control file: %s\n"),
951 progname, strerror(errno));
957 fprintf(stderr, _("%s: fsync error: %s\n"), progname, strerror(errno));
966 * Scan existing XLOG files and determine the highest existing WAL address
968 * On entry, ControlFile.checkPointCopy.redo and ControlFile.xlog_seg_size
969 * are assumed valid (note that we allow the old xlog seg size to differ
970 * from what we're using). On exit, newXlogId and newXlogSeg are set to
971 * suitable values for the beginning of replacement WAL (in our seg size).
978 uint64 segs_per_xlogid;
982 * Initialize the max() computation using the last checkpoint address from
983 * old pg_control. Note that for the moment we are working with segment
984 * numbering according to the old xlog seg size.
986 segs_per_xlogid = (UINT64CONST(0x0000000100000000) / ControlFile.xlog_seg_size);
987 newXlogSegNo = ControlFile.checkPointCopy.redo / ControlFile.xlog_seg_size;
990 * Scan the pg_wal directory to find existing WAL segment files. We assume
991 * any present have been used; in most scenarios this should be
992 * conservative, because of xlog.c's attempts to pre-create files.
994 xldir = opendir(XLOGDIR);
997 fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
998 progname, XLOGDIR, strerror(errno));
1002 while (errno = 0, (xlde = readdir(xldir)) != NULL)
1004 if (IsXLogFileName(xlde->d_name) ||
1005 IsPartialXLogFileName(xlde->d_name))
1013 * Note: We don't use XLogFromFileName here, because we want to
1014 * use the segment size from the control file, not the size the
1015 * pg_resetwal binary was compiled with
1017 sscanf(xlde->d_name, "%08X%08X%08X", &tli, &log, &seg);
1018 segno = ((uint64) log) * segs_per_xlogid + seg;
1021 * Note: we take the max of all files found, regardless of their
1022 * timelines. Another possibility would be to ignore files of
1023 * timelines other than the target TLI, but this seems safer.
1024 * Better too large a result than too small...
1026 if (segno > newXlogSegNo)
1027 newXlogSegNo = segno;
1033 fprintf(stderr, _("%s: could not read directory \"%s\": %s\n"),
1034 progname, XLOGDIR, strerror(errno));
1038 if (closedir(xldir))
1040 fprintf(stderr, _("%s: could not close directory \"%s\": %s\n"),
1041 progname, XLOGDIR, strerror(errno));
1046 * Finally, convert to new xlog seg size, and advance by one to ensure we
1047 * are in virgin territory.
1049 xlogbytepos = newXlogSegNo * ControlFile.xlog_seg_size;
1050 newXlogSegNo = (xlogbytepos + WalSegSz - 1) / WalSegSz;
1056 * Remove existing XLOG files
1059 KillExistingXLOG(void)
1062 struct dirent *xlde;
1063 char path[MAXPGPATH + sizeof(XLOGDIR)];
1065 xldir = opendir(XLOGDIR);
1068 fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
1069 progname, XLOGDIR, strerror(errno));
1073 while (errno = 0, (xlde = readdir(xldir)) != NULL)
1075 if (IsXLogFileName(xlde->d_name) ||
1076 IsPartialXLogFileName(xlde->d_name))
1078 snprintf(path, sizeof(path), "%s/%s", XLOGDIR, xlde->d_name);
1079 if (unlink(path) < 0)
1081 fprintf(stderr, _("%s: could not delete file \"%s\": %s\n"),
1082 progname, path, strerror(errno));
1090 fprintf(stderr, _("%s: could not read directory \"%s\": %s\n"),
1091 progname, XLOGDIR, strerror(errno));
1095 if (closedir(xldir))
1097 fprintf(stderr, _("%s: could not close directory \"%s\": %s\n"),
1098 progname, XLOGDIR, strerror(errno));
1105 * Remove existing archive status files
1108 KillExistingArchiveStatus(void)
1110 #define ARCHSTATDIR XLOGDIR "/archive_status"
1113 struct dirent *xlde;
1114 char path[MAXPGPATH + sizeof(ARCHSTATDIR)];
1116 xldir = opendir(ARCHSTATDIR);
1119 fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
1120 progname, ARCHSTATDIR, strerror(errno));
1124 while (errno = 0, (xlde = readdir(xldir)) != NULL)
1126 if (strspn(xlde->d_name, "0123456789ABCDEF") == XLOG_FNAME_LEN &&
1127 (strcmp(xlde->d_name + XLOG_FNAME_LEN, ".ready") == 0 ||
1128 strcmp(xlde->d_name + XLOG_FNAME_LEN, ".done") == 0 ||
1129 strcmp(xlde->d_name + XLOG_FNAME_LEN, ".partial.ready") == 0 ||
1130 strcmp(xlde->d_name + XLOG_FNAME_LEN, ".partial.done") == 0))
1132 snprintf(path, sizeof(path), "%s/%s", ARCHSTATDIR, xlde->d_name);
1133 if (unlink(path) < 0)
1135 fprintf(stderr, _("%s: could not delete file \"%s\": %s\n"),
1136 progname, path, strerror(errno));
1144 fprintf(stderr, _("%s: could not read directory \"%s\": %s\n"),
1145 progname, ARCHSTATDIR, strerror(errno));
1149 if (closedir(xldir))
1151 fprintf(stderr, _("%s: could not close directory \"%s\": %s\n"),
1152 progname, ARCHSTATDIR, strerror(errno));
1159 * Write an empty XLOG file, containing only the checkpoint record
1160 * already set up in ControlFile.
1163 WriteEmptyXLOG(void)
1166 XLogPageHeader page;
1167 XLogLongPageHeader longpage;
1170 char path[MAXPGPATH];
1175 /* Use malloc() to ensure buffer is MAXALIGNED */
1176 buffer = (char *) pg_malloc(XLOG_BLCKSZ);
1177 page = (XLogPageHeader) buffer;
1178 memset(buffer, 0, XLOG_BLCKSZ);
1180 /* Set up the XLOG page header */
1181 page->xlp_magic = XLOG_PAGE_MAGIC;
1182 page->xlp_info = XLP_LONG_HEADER;
1183 page->xlp_tli = ControlFile.checkPointCopy.ThisTimeLineID;
1184 page->xlp_pageaddr = ControlFile.checkPointCopy.redo - SizeOfXLogLongPHD;
1185 longpage = (XLogLongPageHeader) page;
1186 longpage->xlp_sysid = ControlFile.system_identifier;
1187 longpage->xlp_seg_size = WalSegSz;
1188 longpage->xlp_xlog_blcksz = XLOG_BLCKSZ;
1190 /* Insert the initial checkpoint record */
1191 recptr = (char *) page + SizeOfXLogLongPHD;
1192 record = (XLogRecord *) recptr;
1193 record->xl_prev = 0;
1194 record->xl_xid = InvalidTransactionId;
1195 record->xl_tot_len = SizeOfXLogRecord + SizeOfXLogRecordDataHeaderShort + sizeof(CheckPoint);
1196 record->xl_info = XLOG_CHECKPOINT_SHUTDOWN;
1197 record->xl_rmid = RM_XLOG_ID;
1199 recptr += SizeOfXLogRecord;
1200 *(recptr++) = (char) XLR_BLOCK_ID_DATA_SHORT;
1201 *(recptr++) = sizeof(CheckPoint);
1202 memcpy(recptr, &ControlFile.checkPointCopy,
1203 sizeof(CheckPoint));
1206 COMP_CRC32C(crc, ((char *) record) + SizeOfXLogRecord, record->xl_tot_len - SizeOfXLogRecord);
1207 COMP_CRC32C(crc, (char *) record, offsetof(XLogRecord, xl_crc));
1209 record->xl_crc = crc;
1211 /* Write the first page */
1212 XLogFilePath(path, ControlFile.checkPointCopy.ThisTimeLineID,
1213 newXlogSegNo, WalSegSz);
1217 fd = open(path, O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
1221 fprintf(stderr, _("%s: could not open file \"%s\": %s\n"),
1222 progname, path, strerror(errno));
1227 if (write(fd, buffer, XLOG_BLCKSZ) != XLOG_BLCKSZ)
1229 /* if write didn't set errno, assume problem is no disk space */
1232 fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
1233 progname, path, strerror(errno));
1237 /* Fill the rest of the file with zeroes */
1238 memset(buffer, 0, XLOG_BLCKSZ);
1239 for (nbytes = XLOG_BLCKSZ; nbytes < WalSegSz; nbytes += XLOG_BLCKSZ)
1242 if (write(fd, buffer, XLOG_BLCKSZ) != XLOG_BLCKSZ)
1246 fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
1247 progname, path, strerror(errno));
1254 fprintf(stderr, _("%s: fsync error: %s\n"), progname, strerror(errno));
1265 printf(_("%s resets the PostgreSQL write-ahead log.\n\n"), progname);
1266 printf(_("Usage:\n %s [OPTION]... DATADIR\n\n"), progname);
1267 printf(_("Options:\n"));
1268 printf(_(" -c, --commit-timestamp-ids=XID,XID\n"
1269 " set oldest and newest transactions bearing\n"
1270 " commit timestamp (zero means no change)\n"));
1271 printf(_(" [-D, --pgdata=]DATADIR data directory\n"));
1272 printf(_(" -e, --epoch=XIDEPOCH set next transaction ID epoch\n"));
1273 printf(_(" -f, --force force update to be done\n"));
1274 printf(_(" -l, --next-wal-file=WALFILE set minimum starting location for new WAL\n"));
1275 printf(_(" -m, --multixact-ids=MXID,MXID set next and oldest multitransaction ID\n"));
1276 printf(_(" -n, --dry-run no update, just show what would be done\n"));
1277 printf(_(" -o, --next-oid=OID set next OID\n"));
1278 printf(_(" -O, --multixact-offset=OFFSET set next multitransaction offset\n"));
1279 printf(_(" -V, --version output version information, then exit\n"));
1280 printf(_(" -x, --next-transaction-id=XID set next transaction ID\n"));
1281 printf(_(" -?, --help show this help, then exit\n"));
1282 printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));