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-2014, PostgreSQL Global Development Group
24 * Portions Copyright (c) 1994, Regents of the University of California
26 * src/bin/pg_resetxlog/pg_resetxlog.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.
48 #include "access/transam.h"
49 #include "access/tuptoaster.h"
50 #include "access/multixact.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 "pg_getopt.h"
58 static ControlFileData ControlFile; /* pg_control values */
59 static XLogSegNo newXlogSegNo; /* new XLOG segment # */
60 static bool guessed = false; /* T if we had to guess at any values */
61 static const char *progname;
62 static uint32 set_xid_epoch = (uint32) -1;
63 static TransactionId set_xid = 0;
64 static Oid set_oid = 0;
65 static MultiXactId set_mxid = 0;
66 static MultiXactOffset set_mxoff = (MultiXactOffset) -1;
67 static uint32 minXlogTli = 0;
68 static XLogSegNo minXlogSegNo = 0;
70 static bool ReadControlFile(void);
71 static void GuessControlValues(void);
72 static void PrintControlValues(bool guessed);
73 static void PrintNewControlValues(void);
74 static void RewriteControlFile(void);
75 static void FindEndOfXLOG(void);
76 static void KillExistingXLOG(void);
77 static void KillExistingArchiveStatus(void);
78 static void WriteEmptyXLOG(void);
79 static void usage(void);
83 main(int argc, char *argv[])
87 bool noupdate = false;
88 MultiXactId set_oldestmxid = 0;
94 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_resetxlog"));
96 progname = get_progname(argv[0]);
100 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
105 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
107 puts("pg_resetxlog (PostgreSQL) " PG_VERSION);
113 while ((c = getopt(argc, argv, "fl:m:no:O:x:e:")) != -1)
126 set_xid_epoch = strtoul(optarg, &endptr, 0);
127 if (endptr == optarg || *endptr != '\0')
129 fprintf(stderr, _("%s: invalid argument for option -e\n"), progname);
130 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
133 if (set_xid_epoch == -1)
135 fprintf(stderr, _("%s: transaction ID epoch (-e) must not be -1\n"), progname);
141 set_xid = strtoul(optarg, &endptr, 0);
142 if (endptr == optarg || *endptr != '\0')
144 fprintf(stderr, _("%s: invalid argument for option -x\n"), progname);
145 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
150 fprintf(stderr, _("%s: transaction ID (-x) must not be 0\n"), progname);
156 set_oid = strtoul(optarg, &endptr, 0);
157 if (endptr == optarg || *endptr != '\0')
159 fprintf(stderr, _("%s: invalid argument for option -o\n"), progname);
160 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
165 fprintf(stderr, _("%s: OID (-o) must not be 0\n"), progname);
171 set_mxid = strtoul(optarg, &endptr, 0);
172 if (endptr == optarg || *endptr != ',')
174 fprintf(stderr, _("%s: invalid argument for option -m\n"), progname);
175 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
179 set_oldestmxid = strtoul(endptr + 1, &endptr2, 0);
180 if (endptr2 == endptr + 1 || *endptr2 != '\0')
182 fprintf(stderr, _("%s: invalid argument for option -m\n"), progname);
183 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
188 fprintf(stderr, _("%s: multitransaction ID (-m) must not be 0\n"), progname);
193 * XXX It'd be nice to have more sanity checks here, e.g. so
194 * that oldest is not wrapped around w.r.t. nextMulti.
196 if (set_oldestmxid == 0)
198 fprintf(stderr, _("%s: oldest multitransaction ID (-m) must not be 0\n"),
205 set_mxoff = strtoul(optarg, &endptr, 0);
206 if (endptr == optarg || *endptr != '\0')
208 fprintf(stderr, _("%s: invalid argument for option -O\n"), progname);
209 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
214 fprintf(stderr, _("%s: multitransaction offset (-O) must not be -1\n"), progname);
220 if (strspn(optarg, "01234567890ABCDEFabcdef") != 24)
222 fprintf(stderr, _("%s: invalid argument for option -l\n"), progname);
223 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
226 XLogFromFileName(optarg, &minXlogTli, &minXlogSegNo);
230 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
237 fprintf(stderr, _("%s: no data directory specified\n"), progname);
238 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
243 * Don't allow pg_resetxlog to be run as root, to avoid overwriting the
244 * ownership of files in the data directory. We need only check for root
245 * -- any other user won't have sufficient permissions to modify files in
246 * the data directory.
251 fprintf(stderr, _("%s: cannot be executed by \"root\"\n"),
253 fprintf(stderr, _("You must run %s as the PostgreSQL superuser.\n"),
259 DataDir = argv[optind];
261 if (chdir(DataDir) < 0)
263 fprintf(stderr, _("%s: could not change directory to \"%s\": %s\n"),
264 progname, DataDir, strerror(errno));
269 * Check for a postmaster lock file --- if there is one, refuse to
270 * proceed, on grounds we might be interfering with a live installation.
272 if ((fd = open("postmaster.pid", O_RDONLY, 0)) < 0)
276 fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
277 progname, "postmaster.pid", strerror(errno));
283 fprintf(stderr, _("%s: lock file \"%s\" exists\n"
284 "Is a server running? If not, delete the lock file and try again.\n"),
285 progname, "postmaster.pid");
290 * Attempt to read the existing pg_control file
292 if (!ReadControlFile())
293 GuessControlValues();
296 * Also look at existing segment files to set up newXlogSegNo
301 * If we're not going to proceed with the reset, print the current control
304 if ((guessed && !force) || noupdate)
305 PrintControlValues(guessed);
308 * Adjust fields if required by switches. (Do this now so that printout,
309 * if any, includes these values.)
311 if (set_xid_epoch != -1)
312 ControlFile.checkPointCopy.nextXidEpoch = set_xid_epoch;
316 ControlFile.checkPointCopy.nextXid = set_xid;
319 * For the moment, just set oldestXid to a value that will force
320 * immediate autovacuum-for-wraparound. It's not clear whether adding
321 * user control of this is useful, so let's just do something that's
322 * reasonably safe. The magic constant here corresponds to the
323 * maximum allowed value of autovacuum_freeze_max_age.
325 ControlFile.checkPointCopy.oldestXid = set_xid - 2000000000;
326 if (ControlFile.checkPointCopy.oldestXid < FirstNormalTransactionId)
327 ControlFile.checkPointCopy.oldestXid += FirstNormalTransactionId;
328 ControlFile.checkPointCopy.oldestXidDB = InvalidOid;
332 ControlFile.checkPointCopy.nextOid = set_oid;
336 ControlFile.checkPointCopy.nextMulti = set_mxid;
338 ControlFile.checkPointCopy.oldestMulti = set_oldestmxid;
339 if (ControlFile.checkPointCopy.oldestMulti < FirstMultiXactId)
340 ControlFile.checkPointCopy.oldestMulti += FirstMultiXactId;
341 ControlFile.checkPointCopy.oldestMultiDB = InvalidOid;
345 ControlFile.checkPointCopy.nextMultiOffset = set_mxoff;
347 if (minXlogTli > ControlFile.checkPointCopy.ThisTimeLineID)
349 ControlFile.checkPointCopy.ThisTimeLineID = minXlogTli;
350 ControlFile.checkPointCopy.PrevTimeLineID = minXlogTli;
353 if (minXlogSegNo > newXlogSegNo)
354 newXlogSegNo = minXlogSegNo;
357 * If we had to guess anything, and -f was not given, just print the
358 * guessed values and exit. Also print if -n is given.
360 if ((guessed && !force) || noupdate)
362 PrintNewControlValues();
365 printf(_("\nIf these values seem acceptable, use -f to force reset.\n"));
373 * Don't reset from a dirty pg_control without -f, either.
375 if (ControlFile.state != DB_SHUTDOWNED && !force)
377 printf(_("The database server was not shut down cleanly.\n"
378 "Resetting the transaction log might cause data to be lost.\n"
379 "If you want to proceed anyway, use -f to force reset.\n"));
384 * Else, do the dirty deed.
386 RewriteControlFile();
388 KillExistingArchiveStatus();
391 printf(_("Transaction log reset\n"));
397 * Try to read the existing pg_control file.
399 * This routine is also responsible for updating old pg_control versions
400 * to the current format. (Currently we don't do anything of the sort.)
403 ReadControlFile(void)
410 if ((fd = open(XLOG_CONTROL_FILE, O_RDONLY | PG_BINARY, 0)) < 0)
413 * If pg_control is not there at all, or we can't read it, the odds
414 * are we've been handed a bad DataDir path, so give up. User can do
415 * "touch pg_control" to force us to proceed.
417 fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
418 progname, XLOG_CONTROL_FILE, strerror(errno));
420 fprintf(stderr, _("If you are sure the data directory path is correct, execute\n"
427 /* Use malloc to ensure we have a maxaligned buffer */
428 buffer = (char *) pg_malloc(PG_CONTROL_SIZE);
430 len = read(fd, buffer, PG_CONTROL_SIZE);
433 fprintf(stderr, _("%s: could not read file \"%s\": %s\n"),
434 progname, XLOG_CONTROL_FILE, strerror(errno));
439 if (len >= sizeof(ControlFileData) &&
440 ((ControlFileData *) buffer)->pg_control_version == PG_CONTROL_VERSION)
446 offsetof(ControlFileData, crc));
449 if (EQ_CRC32(crc, ((ControlFileData *) buffer)->crc))
452 memcpy(&ControlFile, buffer, sizeof(ControlFile));
456 fprintf(stderr, _("%s: pg_control exists but has invalid CRC; proceed with caution\n"),
458 /* We will use the data anyway, but treat it as guessed. */
459 memcpy(&ControlFile, buffer, sizeof(ControlFile));
464 /* Looks like it's a mess. */
465 fprintf(stderr, _("%s: pg_control exists but is broken or unknown version; ignoring it\n"),
472 * Guess at pg_control values when we can't read the old ones.
475 GuessControlValues(void)
477 uint64 sysidentifier;
481 * Set up a completely default set of pg_control values.
484 memset(&ControlFile, 0, sizeof(ControlFile));
486 ControlFile.pg_control_version = PG_CONTROL_VERSION;
487 ControlFile.catalog_version_no = CATALOG_VERSION_NO;
490 * Create a new unique installation identifier, since we can no longer use
491 * any old XLOG records. See notes in xlog.c about the algorithm.
493 gettimeofday(&tv, NULL);
494 sysidentifier = ((uint64) tv.tv_sec) << 32;
495 sysidentifier |= (uint32) (tv.tv_sec | tv.tv_usec);
497 ControlFile.system_identifier = sysidentifier;
499 ControlFile.checkPointCopy.redo = SizeOfXLogLongPHD;
500 ControlFile.checkPointCopy.ThisTimeLineID = 1;
501 ControlFile.checkPointCopy.PrevTimeLineID = 1;
502 ControlFile.checkPointCopy.fullPageWrites = false;
503 ControlFile.checkPointCopy.nextXidEpoch = 0;
504 ControlFile.checkPointCopy.nextXid = FirstNormalTransactionId;
505 ControlFile.checkPointCopy.nextOid = FirstBootstrapObjectId;
506 ControlFile.checkPointCopy.nextMulti = FirstMultiXactId;
507 ControlFile.checkPointCopy.nextMultiOffset = 0;
508 ControlFile.checkPointCopy.oldestXid = FirstNormalTransactionId;
509 ControlFile.checkPointCopy.oldestXidDB = InvalidOid;
510 ControlFile.checkPointCopy.oldestMulti = FirstMultiXactId;
511 ControlFile.checkPointCopy.oldestMultiDB = InvalidOid;
512 ControlFile.checkPointCopy.time = (pg_time_t) time(NULL);
513 ControlFile.checkPointCopy.oldestActiveXid = InvalidTransactionId;
515 ControlFile.state = DB_SHUTDOWNED;
516 ControlFile.time = (pg_time_t) time(NULL);
517 ControlFile.checkPoint = ControlFile.checkPointCopy.redo;
518 ControlFile.unloggedLSN = 1;
520 /* minRecoveryPoint, backupStartPoint and backupEndPoint can be left zero */
522 ControlFile.wal_level = WAL_LEVEL_MINIMAL;
523 ControlFile.wal_log_hints = false;
524 ControlFile.MaxConnections = 100;
525 ControlFile.max_worker_processes = 8;
526 ControlFile.max_prepared_xacts = 0;
527 ControlFile.max_locks_per_xact = 64;
529 ControlFile.maxAlign = MAXIMUM_ALIGNOF;
530 ControlFile.floatFormat = FLOATFORMAT_VALUE;
531 ControlFile.blcksz = BLCKSZ;
532 ControlFile.relseg_size = RELSEG_SIZE;
533 ControlFile.xlog_blcksz = XLOG_BLCKSZ;
534 ControlFile.xlog_seg_size = XLOG_SEG_SIZE;
535 ControlFile.nameDataLen = NAMEDATALEN;
536 ControlFile.indexMaxKeys = INDEX_MAX_KEYS;
537 ControlFile.toast_max_chunk_size = TOAST_MAX_CHUNK_SIZE;
538 #ifdef HAVE_INT64_TIMESTAMP
539 ControlFile.enableIntTimes = true;
541 ControlFile.enableIntTimes = false;
543 ControlFile.float4ByVal = FLOAT4PASSBYVAL;
544 ControlFile.float8ByVal = FLOAT8PASSBYVAL;
547 * XXX eventually, should try to grovel through old XLOG to develop more
548 * accurate values for TimeLineID, nextXID, etc.
554 * Print the guessed pg_control values when we had to guess.
556 * NB: this display should be just those fields that will not be
557 * reset by RewriteControlFile().
560 PrintControlValues(bool guessed)
562 char sysident_str[32];
565 printf(_("Guessed pg_control values:\n\n"));
567 printf(_("Current pg_control values:\n\n"));
570 * Format system_identifier separately to keep platform-dependent format
571 * code out of the translatable message string.
573 snprintf(sysident_str, sizeof(sysident_str), UINT64_FORMAT,
574 ControlFile.system_identifier);
576 printf(_("pg_control version number: %u\n"),
577 ControlFile.pg_control_version);
578 printf(_("Catalog version number: %u\n"),
579 ControlFile.catalog_version_no);
580 printf(_("Database system identifier: %s\n"),
582 printf(_("Latest checkpoint's TimeLineID: %u\n"),
583 ControlFile.checkPointCopy.ThisTimeLineID);
584 printf(_("Latest checkpoint's full_page_writes: %s\n"),
585 ControlFile.checkPointCopy.fullPageWrites ? _("on") : _("off"));
586 printf(_("Latest checkpoint's NextXID: %u/%u\n"),
587 ControlFile.checkPointCopy.nextXidEpoch,
588 ControlFile.checkPointCopy.nextXid);
589 printf(_("Latest checkpoint's NextOID: %u\n"),
590 ControlFile.checkPointCopy.nextOid);
591 printf(_("Latest checkpoint's NextMultiXactId: %u\n"),
592 ControlFile.checkPointCopy.nextMulti);
593 printf(_("Latest checkpoint's NextMultiOffset: %u\n"),
594 ControlFile.checkPointCopy.nextMultiOffset);
595 printf(_("Latest checkpoint's oldestXID: %u\n"),
596 ControlFile.checkPointCopy.oldestXid);
597 printf(_("Latest checkpoint's oldestXID's DB: %u\n"),
598 ControlFile.checkPointCopy.oldestXidDB);
599 printf(_("Latest checkpoint's oldestActiveXID: %u\n"),
600 ControlFile.checkPointCopy.oldestActiveXid);
601 printf(_("Latest checkpoint's oldestMultiXid: %u\n"),
602 ControlFile.checkPointCopy.oldestMulti);
603 printf(_("Latest checkpoint's oldestMulti's DB: %u\n"),
604 ControlFile.checkPointCopy.oldestMultiDB);
605 printf(_("Maximum data alignment: %u\n"),
606 ControlFile.maxAlign);
607 /* we don't print floatFormat since can't say much useful about it */
608 printf(_("Database block size: %u\n"),
610 printf(_("Blocks per segment of large relation: %u\n"),
611 ControlFile.relseg_size);
612 printf(_("WAL block size: %u\n"),
613 ControlFile.xlog_blcksz);
614 printf(_("Bytes per WAL segment: %u\n"),
615 ControlFile.xlog_seg_size);
616 printf(_("Maximum length of identifiers: %u\n"),
617 ControlFile.nameDataLen);
618 printf(_("Maximum columns in an index: %u\n"),
619 ControlFile.indexMaxKeys);
620 printf(_("Maximum size of a TOAST chunk: %u\n"),
621 ControlFile.toast_max_chunk_size);
622 printf(_("Date/time type storage: %s\n"),
623 (ControlFile.enableIntTimes ? _("64-bit integers") : _("floating-point numbers")));
624 printf(_("Float4 argument passing: %s\n"),
625 (ControlFile.float4ByVal ? _("by value") : _("by reference")));
626 printf(_("Float8 argument passing: %s\n"),
627 (ControlFile.float8ByVal ? _("by value") : _("by reference")));
628 printf(_("Data page checksum version: %u\n"),
629 ControlFile.data_checksum_version);
634 * Print the values to be changed.
637 PrintNewControlValues()
639 char fname[MAXFNAMELEN];
641 /* This will be always printed in order to keep format same. */
642 printf(_("\n\nValues to be changed:\n\n"));
644 XLogFileName(fname, ControlFile.checkPointCopy.ThisTimeLineID, newXlogSegNo);
645 printf(_("First log segment after reset: %s\n"), fname);
649 printf(_("NextMultiXactId: %u\n"),
650 ControlFile.checkPointCopy.nextMulti);
651 printf(_("OldestMultiXid: %u\n"),
652 ControlFile.checkPointCopy.oldestMulti);
653 printf(_("OldestMulti's DB: %u\n"),
654 ControlFile.checkPointCopy.oldestMultiDB);
659 printf(_("NextMultiOffset: %u\n"),
660 ControlFile.checkPointCopy.nextMultiOffset);
665 printf(_("NextOID: %u\n"),
666 ControlFile.checkPointCopy.nextOid);
671 printf(_("NextXID: %u\n"),
672 ControlFile.checkPointCopy.nextXid);
673 printf(_("OldestXID: %u\n"),
674 ControlFile.checkPointCopy.oldestXid);
675 printf(_("OldestXID's DB: %u\n"),
676 ControlFile.checkPointCopy.oldestXidDB);
679 if (set_xid_epoch != -1)
681 printf(_("NextXID Epoch: %u\n"),
682 ControlFile.checkPointCopy.nextXidEpoch);
688 * Write out the new pg_control file.
691 RewriteControlFile(void)
694 char buffer[PG_CONTROL_SIZE]; /* need not be aligned */
697 * Adjust fields as needed to force an empty XLOG starting at
700 XLogSegNoOffsetToRecPtr(newXlogSegNo, SizeOfXLogLongPHD,
701 ControlFile.checkPointCopy.redo);
702 ControlFile.checkPointCopy.time = (pg_time_t) time(NULL);
704 ControlFile.state = DB_SHUTDOWNED;
705 ControlFile.time = (pg_time_t) time(NULL);
706 ControlFile.checkPoint = ControlFile.checkPointCopy.redo;
707 ControlFile.prevCheckPoint = 0;
708 ControlFile.minRecoveryPoint = 0;
709 ControlFile.minRecoveryPointTLI = 0;
710 ControlFile.backupStartPoint = 0;
711 ControlFile.backupEndPoint = 0;
712 ControlFile.backupEndRequired = false;
715 * Force the defaults for max_* settings. The values don't really matter
716 * as long as wal_level='minimal'; the postmaster will reset these fields
719 ControlFile.wal_level = WAL_LEVEL_MINIMAL;
720 ControlFile.wal_log_hints = false;
721 ControlFile.MaxConnections = 100;
722 ControlFile.max_worker_processes = 8;
723 ControlFile.max_prepared_xacts = 0;
724 ControlFile.max_locks_per_xact = 64;
726 /* Now we can force the recorded xlog seg size to the right thing. */
727 ControlFile.xlog_seg_size = XLogSegSize;
729 /* Contents are protected with a CRC */
730 INIT_CRC32(ControlFile.crc);
731 COMP_CRC32(ControlFile.crc,
732 (char *) &ControlFile,
733 offsetof(ControlFileData, crc));
734 FIN_CRC32(ControlFile.crc);
737 * We write out PG_CONTROL_SIZE bytes into pg_control, zero-padding the
738 * excess over sizeof(ControlFileData). This reduces the odds of
739 * premature-EOF errors when reading pg_control. We'll still fail when we
740 * check the contents of the file, but hopefully with a more specific
741 * error than "couldn't read pg_control".
743 if (sizeof(ControlFileData) > PG_CONTROL_SIZE)
746 _("%s: internal error -- sizeof(ControlFileData) is too large ... fix PG_CONTROL_SIZE\n"),
751 memset(buffer, 0, PG_CONTROL_SIZE);
752 memcpy(buffer, &ControlFile, sizeof(ControlFileData));
754 unlink(XLOG_CONTROL_FILE);
756 fd = open(XLOG_CONTROL_FILE,
757 O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
761 fprintf(stderr, _("%s: could not create pg_control file: %s\n"),
762 progname, strerror(errno));
767 if (write(fd, buffer, PG_CONTROL_SIZE) != PG_CONTROL_SIZE)
769 /* if write didn't set errno, assume problem is no disk space */
772 fprintf(stderr, _("%s: could not write pg_control file: %s\n"),
773 progname, strerror(errno));
779 fprintf(stderr, _("%s: fsync error: %s\n"), progname, strerror(errno));
788 * Scan existing XLOG files and determine the highest existing WAL address
790 * On entry, ControlFile.checkPointCopy.redo and ControlFile.xlog_seg_size
791 * are assumed valid (note that we allow the old xlog seg size to differ
792 * from what we're using). On exit, newXlogId and newXlogSeg are set to
793 * suitable values for the beginning of replacement WAL (in our seg size).
800 uint64 segs_per_xlogid;
804 * Initialize the max() computation using the last checkpoint address from
805 * old pg_control. Note that for the moment we are working with segment
806 * numbering according to the old xlog seg size.
808 segs_per_xlogid = (UINT64CONST(0x0000000100000000) / ControlFile.xlog_seg_size);
809 newXlogSegNo = ControlFile.checkPointCopy.redo / ControlFile.xlog_seg_size;
812 * Scan the pg_xlog directory to find existing WAL segment files. We
813 * assume any present have been used; in most scenarios this should be
814 * conservative, because of xlog.c's attempts to pre-create files.
816 xldir = opendir(XLOGDIR);
819 fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
820 progname, XLOGDIR, strerror(errno));
825 while ((xlde = readdir(xldir)) != NULL)
827 if (strlen(xlde->d_name) == 24 &&
828 strspn(xlde->d_name, "0123456789ABCDEF") == 24)
835 sscanf(xlde->d_name, "%08X%08X%08X", &tli, &log, &seg);
836 segno = ((uint64) log) * segs_per_xlogid + seg;
839 * Note: we take the max of all files found, regardless of their
840 * timelines. Another possibility would be to ignore files of
841 * timelines other than the target TLI, but this seems safer.
842 * Better too large a result than too small...
844 if (segno > newXlogSegNo)
845 newXlogSegNo = segno;
852 * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
855 if (GetLastError() == ERROR_NO_MORE_FILES)
861 fprintf(stderr, _("%s: could not read from directory \"%s\": %s\n"),
862 progname, XLOGDIR, strerror(errno));
868 * Finally, convert to new xlog seg size, and advance by one to ensure we
869 * are in virgin territory.
871 xlogbytepos = newXlogSegNo * ControlFile.xlog_seg_size;
872 newXlogSegNo = (xlogbytepos + XLogSegSize - 1) / XLogSegSize;
878 * Remove existing XLOG files
881 KillExistingXLOG(void)
885 char path[MAXPGPATH];
887 xldir = opendir(XLOGDIR);
890 fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
891 progname, XLOGDIR, strerror(errno));
896 while ((xlde = readdir(xldir)) != NULL)
898 if (strlen(xlde->d_name) == 24 &&
899 strspn(xlde->d_name, "0123456789ABCDEF") == 24)
901 snprintf(path, MAXPGPATH, "%s/%s", XLOGDIR, xlde->d_name);
902 if (unlink(path) < 0)
904 fprintf(stderr, _("%s: could not delete file \"%s\": %s\n"),
905 progname, path, strerror(errno));
914 * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
917 if (GetLastError() == ERROR_NO_MORE_FILES)
923 fprintf(stderr, _("%s: could not read from directory \"%s\": %s\n"),
924 progname, XLOGDIR, strerror(errno));
932 * Remove existing archive status files
935 KillExistingArchiveStatus(void)
939 char path[MAXPGPATH];
941 #define ARCHSTATDIR XLOGDIR "/archive_status"
943 xldir = opendir(ARCHSTATDIR);
946 fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
947 progname, ARCHSTATDIR, strerror(errno));
952 while ((xlde = readdir(xldir)) != NULL)
954 if (strspn(xlde->d_name, "0123456789ABCDEF") == 24 &&
955 (strcmp(xlde->d_name + 24, ".ready") == 0 ||
956 strcmp(xlde->d_name + 24, ".done") == 0))
958 snprintf(path, MAXPGPATH, "%s/%s", ARCHSTATDIR, xlde->d_name);
959 if (unlink(path) < 0)
961 fprintf(stderr, _("%s: could not delete file \"%s\": %s\n"),
962 progname, path, strerror(errno));
971 * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
974 if (GetLastError() == ERROR_NO_MORE_FILES)
980 fprintf(stderr, _("%s: could not read from directory \"%s\": %s\n"),
981 progname, ARCHSTATDIR, strerror(errno));
989 * Write an empty XLOG file, containing only the checkpoint record
990 * already set up in ControlFile.
997 XLogLongPageHeader longpage;
1000 char path[MAXPGPATH];
1004 /* Use malloc() to ensure buffer is MAXALIGNED */
1005 buffer = (char *) pg_malloc(XLOG_BLCKSZ);
1006 page = (XLogPageHeader) buffer;
1007 memset(buffer, 0, XLOG_BLCKSZ);
1009 /* Set up the XLOG page header */
1010 page->xlp_magic = XLOG_PAGE_MAGIC;
1011 page->xlp_info = XLP_LONG_HEADER;
1012 page->xlp_tli = ControlFile.checkPointCopy.ThisTimeLineID;
1013 page->xlp_pageaddr = ControlFile.checkPointCopy.redo - SizeOfXLogLongPHD;
1014 longpage = (XLogLongPageHeader) page;
1015 longpage->xlp_sysid = ControlFile.system_identifier;
1016 longpage->xlp_seg_size = XLogSegSize;
1017 longpage->xlp_xlog_blcksz = XLOG_BLCKSZ;
1019 /* Insert the initial checkpoint record */
1020 record = (XLogRecord *) ((char *) page + SizeOfXLogLongPHD);
1021 record->xl_prev = 0;
1022 record->xl_xid = InvalidTransactionId;
1023 record->xl_tot_len = SizeOfXLogRecord + sizeof(CheckPoint);
1024 record->xl_len = sizeof(CheckPoint);
1025 record->xl_info = XLOG_CHECKPOINT_SHUTDOWN;
1026 record->xl_rmid = RM_XLOG_ID;
1027 memcpy(XLogRecGetData(record), &ControlFile.checkPointCopy,
1028 sizeof(CheckPoint));
1031 COMP_CRC32(crc, &ControlFile.checkPointCopy, sizeof(CheckPoint));
1032 COMP_CRC32(crc, (char *) record, offsetof(XLogRecord, xl_crc));
1034 record->xl_crc = crc;
1036 /* Write the first page */
1037 XLogFilePath(path, ControlFile.checkPointCopy.ThisTimeLineID, newXlogSegNo);
1041 fd = open(path, O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
1045 fprintf(stderr, _("%s: could not open file \"%s\": %s\n"),
1046 progname, path, strerror(errno));
1051 if (write(fd, buffer, XLOG_BLCKSZ) != XLOG_BLCKSZ)
1053 /* if write didn't set errno, assume problem is no disk space */
1056 fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
1057 progname, path, strerror(errno));
1061 /* Fill the rest of the file with zeroes */
1062 memset(buffer, 0, XLOG_BLCKSZ);
1063 for (nbytes = XLOG_BLCKSZ; nbytes < XLogSegSize; nbytes += XLOG_BLCKSZ)
1066 if (write(fd, buffer, XLOG_BLCKSZ) != XLOG_BLCKSZ)
1070 fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
1071 progname, path, strerror(errno));
1078 fprintf(stderr, _("%s: fsync error: %s\n"), progname, strerror(errno));
1089 printf(_("%s resets the PostgreSQL transaction log.\n\n"), progname);
1090 printf(_("Usage:\n %s [OPTION]... DATADIR\n\n"), progname);
1091 printf(_("Options:\n"));
1092 printf(_(" -e XIDEPOCH set next transaction ID epoch\n"));
1093 printf(_(" -f force update to be done\n"));
1094 printf(_(" -l XLOGFILE force minimum WAL starting location for new transaction log\n"));
1095 printf(_(" -m MXID,MXID set next and oldest multitransaction ID\n"));
1096 printf(_(" -n no update, just show what would be done (for testing)\n"));
1097 printf(_(" -o OID set next OID\n"));
1098 printf(_(" -O OFFSET set next multitransaction offset\n"));
1099 printf(_(" -V, --version output version information, then exit\n"));
1100 printf(_(" -x XID set next transaction ID\n"));
1101 printf(_(" -?, --help show this help, then exit\n"));
1102 printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));