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-2013, 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.
51 #include "access/transam.h"
52 #include "access/tuptoaster.h"
53 #include "access/multixact.h"
54 #include "access/xlog_internal.h"
55 #include "catalog/catversion.h"
56 #include "catalog/pg_control.h"
57 #include "common/fe_memutils.h"
63 static ControlFileData ControlFile; /* pg_control values */
64 static XLogSegNo newXlogSegNo; /* new XLOG segment # */
65 static bool guessed = false; /* T if we had to guess at any values */
66 static const char *progname;
68 static bool ReadControlFile(void);
69 static void GuessControlValues(void);
70 static void PrintControlValues(bool guessed);
71 static void RewriteControlFile(void);
72 static void FindEndOfXLOG(void);
73 static void KillExistingXLOG(void);
74 static void KillExistingArchiveStatus(void);
75 static void WriteEmptyXLOG(void);
76 static void usage(void);
80 main(int argc, char *argv[])
84 bool noupdate = false;
85 uint32 set_xid_epoch = (uint32) -1;
86 TransactionId set_xid = 0;
88 MultiXactId set_mxid = 0;
89 MultiXactId set_oldestmxid = 0;
90 MultiXactOffset set_mxoff = (MultiXactOffset) -1;
91 uint32 minXlogTli = 0;
92 XLogSegNo minXlogSegNo = 0;
98 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_resetxlog"));
100 progname = get_progname(argv[0]);
104 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
109 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
111 puts("pg_resetxlog (PostgreSQL) " PG_VERSION);
117 while ((c = getopt(argc, argv, "fl:m:no:O:x:e:")) != -1)
130 set_xid_epoch = strtoul(optarg, &endptr, 0);
131 if (endptr == optarg || *endptr != '\0')
133 fprintf(stderr, _("%s: invalid argument for option -e\n"), progname);
134 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
137 if (set_xid_epoch == -1)
139 fprintf(stderr, _("%s: transaction ID epoch (-e) must not be -1\n"), progname);
145 set_xid = strtoul(optarg, &endptr, 0);
146 if (endptr == optarg || *endptr != '\0')
148 fprintf(stderr, _("%s: invalid argument for option -x\n"), progname);
149 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
154 fprintf(stderr, _("%s: transaction ID (-x) must not be 0\n"), progname);
160 set_oid = strtoul(optarg, &endptr, 0);
161 if (endptr == optarg || *endptr != '\0')
163 fprintf(stderr, _("%s: invalid argument for option -o\n"), progname);
164 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
169 fprintf(stderr, _("%s: OID (-o) must not be 0\n"), progname);
175 set_mxid = strtoul(optarg, &endptr, 0);
176 if (endptr == optarg || *endptr != ',')
178 fprintf(stderr, _("%s: invalid argument for option -m\n"), progname);
179 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
183 set_oldestmxid = strtoul(endptr + 1, &endptr2, 0);
184 if (endptr2 == endptr + 1 || *endptr2 != '\0')
186 fprintf(stderr, _("%s: invalid argument for option -m\n"), progname);
187 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
192 fprintf(stderr, _("%s: multitransaction ID (-m) must not be 0\n"), progname);
197 * XXX It'd be nice to have more sanity checks here, e.g. so
198 * that oldest is not wrapped around w.r.t. nextMulti.
200 if (set_oldestmxid == 0)
202 fprintf(stderr, _("%s: oldest multitransaction ID (-m) must not be 0\n"),
209 set_mxoff = strtoul(optarg, &endptr, 0);
210 if (endptr == optarg || *endptr != '\0')
212 fprintf(stderr, _("%s: invalid argument for option -O\n"), progname);
213 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
218 fprintf(stderr, _("%s: multitransaction offset (-O) must not be -1\n"), progname);
224 if (strspn(optarg, "01234567890ABCDEFabcdef") != 24)
226 fprintf(stderr, _("%s: invalid argument for option -l\n"), progname);
227 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
230 XLogFromFileName(optarg, &minXlogTli, &minXlogSegNo);
234 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
241 fprintf(stderr, _("%s: no data directory specified\n"), progname);
242 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
247 * Don't allow pg_resetxlog to be run as root, to avoid overwriting the
248 * ownership of files in the data directory. We need only check for root
249 * -- any other user won't have sufficient permissions to modify files in
250 * the data directory.
255 fprintf(stderr, _("%s: cannot be executed by \"root\"\n"),
257 fprintf(stderr, _("You must run %s as the PostgreSQL superuser.\n"),
263 DataDir = argv[optind];
265 if (chdir(DataDir) < 0)
267 fprintf(stderr, _("%s: could not change directory to \"%s\": %s\n"),
268 progname, DataDir, strerror(errno));
273 * Check for a postmaster lock file --- if there is one, refuse to
274 * proceed, on grounds we might be interfering with a live installation.
276 if ((fd = open("postmaster.pid", O_RDONLY, 0)) < 0)
280 fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
281 progname, "postmaster.pid", strerror(errno));
287 fprintf(stderr, _("%s: lock file \"%s\" exists\n"
288 "Is a server running? If not, delete the lock file and try again.\n"),
289 progname, "postmaster.pid");
294 * Attempt to read the existing pg_control file
296 if (!ReadControlFile())
297 GuessControlValues();
300 * Also look at existing segment files to set up newXlogSegNo
305 * Adjust fields if required by switches. (Do this now so that printout,
306 * if any, includes these values.)
308 if (set_xid_epoch != -1)
309 ControlFile.checkPointCopy.nextXidEpoch = set_xid_epoch;
313 ControlFile.checkPointCopy.nextXid = set_xid;
316 * For the moment, just set oldestXid to a value that will force
317 * immediate autovacuum-for-wraparound. It's not clear whether adding
318 * user control of this is useful, so let's just do something that's
319 * reasonably safe. The magic constant here corresponds to the
320 * maximum allowed value of autovacuum_freeze_max_age.
322 ControlFile.checkPointCopy.oldestXid = set_xid - 2000000000;
323 if (ControlFile.checkPointCopy.oldestXid < FirstNormalTransactionId)
324 ControlFile.checkPointCopy.oldestXid += FirstNormalTransactionId;
325 ControlFile.checkPointCopy.oldestXidDB = InvalidOid;
329 ControlFile.checkPointCopy.nextOid = set_oid;
333 ControlFile.checkPointCopy.nextMulti = set_mxid;
335 ControlFile.checkPointCopy.oldestMulti = set_oldestmxid;
336 if (ControlFile.checkPointCopy.oldestMulti < FirstMultiXactId)
337 ControlFile.checkPointCopy.oldestMulti += FirstMultiXactId;
338 ControlFile.checkPointCopy.oldestMultiDB = InvalidOid;
342 ControlFile.checkPointCopy.nextMultiOffset = set_mxoff;
344 if (minXlogTli > ControlFile.checkPointCopy.ThisTimeLineID)
346 ControlFile.checkPointCopy.ThisTimeLineID = minXlogTli;
347 ControlFile.checkPointCopy.PrevTimeLineID = minXlogTli;
350 if (minXlogSegNo > newXlogSegNo)
351 newXlogSegNo = minXlogSegNo;
354 * If we had to guess anything, and -f was not given, just print the
355 * guessed values and exit. Also print if -n is given.
357 if ((guessed && !force) || noupdate)
359 PrintControlValues(guessed);
362 printf(_("\nIf these values seem acceptable, use -f to force reset.\n"));
370 * Don't reset from a dirty pg_control without -f, either.
372 if (ControlFile.state != DB_SHUTDOWNED && !force)
374 printf(_("The database server was not shut down cleanly.\n"
375 "Resetting the transaction log might cause data to be lost.\n"
376 "If you want to proceed anyway, use -f to force reset.\n"));
381 * Else, do the dirty deed.
383 RewriteControlFile();
385 KillExistingArchiveStatus();
388 printf(_("Transaction log reset\n"));
394 * Try to read the existing pg_control file.
396 * This routine is also responsible for updating old pg_control versions
397 * to the current format. (Currently we don't do anything of the sort.)
400 ReadControlFile(void)
407 if ((fd = open(XLOG_CONTROL_FILE, O_RDONLY | PG_BINARY, 0)) < 0)
410 * If pg_control is not there at all, or we can't read it, the odds
411 * are we've been handed a bad DataDir path, so give up. User can do
412 * "touch pg_control" to force us to proceed.
414 fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
415 progname, XLOG_CONTROL_FILE, strerror(errno));
417 fprintf(stderr, _("If you are sure the data directory path is correct, execute\n"
424 /* Use malloc to ensure we have a maxaligned buffer */
425 buffer = (char *) pg_malloc(PG_CONTROL_SIZE);
427 len = read(fd, buffer, PG_CONTROL_SIZE);
430 fprintf(stderr, _("%s: could not read file \"%s\": %s\n"),
431 progname, XLOG_CONTROL_FILE, strerror(errno));
436 if (len >= sizeof(ControlFileData) &&
437 ((ControlFileData *) buffer)->pg_control_version == PG_CONTROL_VERSION)
443 offsetof(ControlFileData, crc));
446 if (EQ_CRC32(crc, ((ControlFileData *) buffer)->crc))
449 memcpy(&ControlFile, buffer, sizeof(ControlFile));
453 fprintf(stderr, _("%s: pg_control exists but has invalid CRC; proceed with caution\n"),
455 /* We will use the data anyway, but treat it as guessed. */
456 memcpy(&ControlFile, buffer, sizeof(ControlFile));
461 /* Looks like it's a mess. */
462 fprintf(stderr, _("%s: pg_control exists but is broken or unknown version; ignoring it\n"),
469 * Guess at pg_control values when we can't read the old ones.
472 GuessControlValues(void)
474 uint64 sysidentifier;
478 * Set up a completely default set of pg_control values.
481 memset(&ControlFile, 0, sizeof(ControlFile));
483 ControlFile.pg_control_version = PG_CONTROL_VERSION;
484 ControlFile.catalog_version_no = CATALOG_VERSION_NO;
487 * Create a new unique installation identifier, since we can no longer use
488 * any old XLOG records. See notes in xlog.c about the algorithm.
490 gettimeofday(&tv, NULL);
491 sysidentifier = ((uint64) tv.tv_sec) << 32;
492 sysidentifier |= (uint32) (tv.tv_sec | tv.tv_usec);
494 ControlFile.system_identifier = sysidentifier;
496 ControlFile.checkPointCopy.redo = SizeOfXLogLongPHD;
497 ControlFile.checkPointCopy.ThisTimeLineID = 1;
498 ControlFile.checkPointCopy.PrevTimeLineID = 1;
499 ControlFile.checkPointCopy.fullPageWrites = false;
500 ControlFile.checkPointCopy.nextXidEpoch = 0;
501 ControlFile.checkPointCopy.nextXid = FirstNormalTransactionId;
502 ControlFile.checkPointCopy.nextOid = FirstBootstrapObjectId;
503 ControlFile.checkPointCopy.nextMulti = FirstMultiXactId;
504 ControlFile.checkPointCopy.nextMultiOffset = 0;
505 ControlFile.checkPointCopy.oldestXid = FirstNormalTransactionId;
506 ControlFile.checkPointCopy.oldestXidDB = InvalidOid;
507 ControlFile.checkPointCopy.oldestMulti = FirstMultiXactId;
508 ControlFile.checkPointCopy.oldestMultiDB = InvalidOid;
509 ControlFile.checkPointCopy.time = (pg_time_t) time(NULL);
510 ControlFile.checkPointCopy.oldestActiveXid = InvalidTransactionId;
512 ControlFile.state = DB_SHUTDOWNED;
513 ControlFile.time = (pg_time_t) time(NULL);
514 ControlFile.checkPoint = ControlFile.checkPointCopy.redo;
515 ControlFile.unloggedLSN = 1;
517 /* minRecoveryPoint, backupStartPoint and backupEndPoint can be left zero */
519 ControlFile.wal_level = WAL_LEVEL_MINIMAL;
520 ControlFile.MaxConnections = 100;
521 ControlFile.max_worker_processes = 8;
522 ControlFile.max_prepared_xacts = 0;
523 ControlFile.max_locks_per_xact = 64;
525 ControlFile.maxAlign = MAXIMUM_ALIGNOF;
526 ControlFile.floatFormat = FLOATFORMAT_VALUE;
527 ControlFile.blcksz = BLCKSZ;
528 ControlFile.relseg_size = RELSEG_SIZE;
529 ControlFile.xlog_blcksz = XLOG_BLCKSZ;
530 ControlFile.xlog_seg_size = XLOG_SEG_SIZE;
531 ControlFile.nameDataLen = NAMEDATALEN;
532 ControlFile.indexMaxKeys = INDEX_MAX_KEYS;
533 ControlFile.toast_max_chunk_size = TOAST_MAX_CHUNK_SIZE;
534 #ifdef HAVE_INT64_TIMESTAMP
535 ControlFile.enableIntTimes = true;
537 ControlFile.enableIntTimes = false;
539 ControlFile.float4ByVal = FLOAT4PASSBYVAL;
540 ControlFile.float8ByVal = FLOAT8PASSBYVAL;
543 * XXX eventually, should try to grovel through old XLOG to develop more
544 * accurate values for TimeLineID, nextXID, etc.
550 * Print the guessed pg_control values when we had to guess.
552 * NB: this display should be just those fields that will not be
553 * reset by RewriteControlFile().
556 PrintControlValues(bool guessed)
558 char sysident_str[32];
559 char fname[MAXFNAMELEN];
562 printf(_("Guessed pg_control values:\n\n"));
564 printf(_("pg_control values:\n\n"));
567 * Format system_identifier separately to keep platform-dependent format
568 * code out of the translatable message string.
570 snprintf(sysident_str, sizeof(sysident_str), UINT64_FORMAT,
571 ControlFile.system_identifier);
573 XLogFileName(fname, ControlFile.checkPointCopy.ThisTimeLineID, newXlogSegNo);
575 printf(_("First log segment after reset: %s\n"),
577 printf(_("pg_control version number: %u\n"),
578 ControlFile.pg_control_version);
579 printf(_("Catalog version number: %u\n"),
580 ControlFile.catalog_version_no);
581 printf(_("Database system identifier: %s\n"),
583 printf(_("Latest checkpoint's TimeLineID: %u\n"),
584 ControlFile.checkPointCopy.ThisTimeLineID);
585 printf(_("Latest checkpoint's full_page_writes: %s\n"),
586 ControlFile.checkPointCopy.fullPageWrites ? _("on") : _("off"));
587 printf(_("Latest checkpoint's NextXID: %u/%u\n"),
588 ControlFile.checkPointCopy.nextXidEpoch,
589 ControlFile.checkPointCopy.nextXid);
590 printf(_("Latest checkpoint's NextOID: %u\n"),
591 ControlFile.checkPointCopy.nextOid);
592 printf(_("Latest checkpoint's NextMultiXactId: %u\n"),
593 ControlFile.checkPointCopy.nextMulti);
594 printf(_("Latest checkpoint's NextMultiOffset: %u\n"),
595 ControlFile.checkPointCopy.nextMultiOffset);
596 printf(_("Latest checkpoint's oldestXID: %u\n"),
597 ControlFile.checkPointCopy.oldestXid);
598 printf(_("Latest checkpoint's oldestXID's DB: %u\n"),
599 ControlFile.checkPointCopy.oldestXidDB);
600 printf(_("Latest checkpoint's oldestActiveXID: %u\n"),
601 ControlFile.checkPointCopy.oldestActiveXid);
602 printf(_("Latest checkpoint's oldestMultiXid: %u\n"),
603 ControlFile.checkPointCopy.oldestMulti);
604 printf(_("Latest checkpoint's oldestMulti's DB: %u\n"),
605 ControlFile.checkPointCopy.oldestMultiDB);
606 printf(_("Maximum data alignment: %u\n"),
607 ControlFile.maxAlign);
608 /* we don't print floatFormat since can't say much useful about it */
609 printf(_("Database block size: %u\n"),
611 printf(_("Blocks per segment of large relation: %u\n"),
612 ControlFile.relseg_size);
613 printf(_("WAL block size: %u\n"),
614 ControlFile.xlog_blcksz);
615 printf(_("Bytes per WAL segment: %u\n"),
616 ControlFile.xlog_seg_size);
617 printf(_("Maximum length of identifiers: %u\n"),
618 ControlFile.nameDataLen);
619 printf(_("Maximum columns in an index: %u\n"),
620 ControlFile.indexMaxKeys);
621 printf(_("Maximum size of a TOAST chunk: %u\n"),
622 ControlFile.toast_max_chunk_size);
623 printf(_("Date/time type storage: %s\n"),
624 (ControlFile.enableIntTimes ? _("64-bit integers") : _("floating-point numbers")));
625 printf(_("Float4 argument passing: %s\n"),
626 (ControlFile.float4ByVal ? _("by value") : _("by reference")));
627 printf(_("Float8 argument passing: %s\n"),
628 (ControlFile.float8ByVal ? _("by value") : _("by reference")));
629 printf(_("Data page checksum version: %u\n"),
630 ControlFile.data_checksum_version);
635 * Write out the new pg_control file.
638 RewriteControlFile(void)
641 char buffer[PG_CONTROL_SIZE]; /* need not be aligned */
644 * Adjust fields as needed to force an empty XLOG starting at
647 XLogSegNoOffsetToRecPtr(newXlogSegNo, SizeOfXLogLongPHD,
648 ControlFile.checkPointCopy.redo);
649 ControlFile.checkPointCopy.time = (pg_time_t) time(NULL);
651 ControlFile.state = DB_SHUTDOWNED;
652 ControlFile.time = (pg_time_t) time(NULL);
653 ControlFile.checkPoint = ControlFile.checkPointCopy.redo;
654 ControlFile.prevCheckPoint = 0;
655 ControlFile.minRecoveryPoint = 0;
656 ControlFile.minRecoveryPointTLI = 0;
657 ControlFile.backupStartPoint = 0;
658 ControlFile.backupEndPoint = 0;
659 ControlFile.backupEndRequired = false;
662 * Force the defaults for max_* settings. The values don't really matter
663 * as long as wal_level='minimal'; the postmaster will reset these fields
666 ControlFile.wal_level = WAL_LEVEL_MINIMAL;
667 ControlFile.MaxConnections = 100;
668 ControlFile.max_worker_processes = 8;
669 ControlFile.max_prepared_xacts = 0;
670 ControlFile.max_locks_per_xact = 64;
672 /* Now we can force the recorded xlog seg size to the right thing. */
673 ControlFile.xlog_seg_size = XLogSegSize;
675 /* Contents are protected with a CRC */
676 INIT_CRC32(ControlFile.crc);
677 COMP_CRC32(ControlFile.crc,
678 (char *) &ControlFile,
679 offsetof(ControlFileData, crc));
680 FIN_CRC32(ControlFile.crc);
683 * We write out PG_CONTROL_SIZE bytes into pg_control, zero-padding the
684 * excess over sizeof(ControlFileData). This reduces the odds of
685 * premature-EOF errors when reading pg_control. We'll still fail when we
686 * check the contents of the file, but hopefully with a more specific
687 * error than "couldn't read pg_control".
689 if (sizeof(ControlFileData) > PG_CONTROL_SIZE)
692 _("%s: internal error -- sizeof(ControlFileData) is too large ... fix PG_CONTROL_SIZE\n"),
697 memset(buffer, 0, PG_CONTROL_SIZE);
698 memcpy(buffer, &ControlFile, sizeof(ControlFileData));
700 unlink(XLOG_CONTROL_FILE);
702 fd = open(XLOG_CONTROL_FILE,
703 O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
707 fprintf(stderr, _("%s: could not create pg_control file: %s\n"),
708 progname, strerror(errno));
713 if (write(fd, buffer, PG_CONTROL_SIZE) != PG_CONTROL_SIZE)
715 /* if write didn't set errno, assume problem is no disk space */
718 fprintf(stderr, _("%s: could not write pg_control file: %s\n"),
719 progname, strerror(errno));
725 fprintf(stderr, _("%s: fsync error: %s\n"), progname, strerror(errno));
734 * Scan existing XLOG files and determine the highest existing WAL address
736 * On entry, ControlFile.checkPointCopy.redo and ControlFile.xlog_seg_size
737 * are assumed valid (note that we allow the old xlog seg size to differ
738 * from what we're using). On exit, newXlogId and newXlogSeg are set to
739 * suitable values for the beginning of replacement WAL (in our seg size).
746 uint64 segs_per_xlogid;
750 * Initialize the max() computation using the last checkpoint address from
751 * old pg_control. Note that for the moment we are working with segment
752 * numbering according to the old xlog seg size.
754 segs_per_xlogid = (UINT64CONST(0x0000000100000000) / ControlFile.xlog_seg_size);
755 newXlogSegNo = ControlFile.checkPointCopy.redo / ControlFile.xlog_seg_size;
758 * Scan the pg_xlog directory to find existing WAL segment files. We
759 * assume any present have been used; in most scenarios this should be
760 * conservative, because of xlog.c's attempts to pre-create files.
762 xldir = opendir(XLOGDIR);
765 fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
766 progname, XLOGDIR, strerror(errno));
771 while ((xlde = readdir(xldir)) != NULL)
773 if (strlen(xlde->d_name) == 24 &&
774 strspn(xlde->d_name, "0123456789ABCDEF") == 24)
781 sscanf(xlde->d_name, "%08X%08X%08X", &tli, &log, &seg);
782 segno = ((uint64) log) * segs_per_xlogid + seg;
785 * Note: we take the max of all files found, regardless of their
786 * timelines. Another possibility would be to ignore files of
787 * timelines other than the target TLI, but this seems safer.
788 * Better too large a result than too small...
790 if (segno > newXlogSegNo)
791 newXlogSegNo = segno;
798 * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
801 if (GetLastError() == ERROR_NO_MORE_FILES)
807 fprintf(stderr, _("%s: could not read from directory \"%s\": %s\n"),
808 progname, XLOGDIR, strerror(errno));
814 * Finally, convert to new xlog seg size, and advance by one to ensure we
815 * are in virgin territory.
817 xlogbytepos = newXlogSegNo * ControlFile.xlog_seg_size;
818 newXlogSegNo = (xlogbytepos + XLogSegSize - 1) / XLogSegSize;
824 * Remove existing XLOG files
827 KillExistingXLOG(void)
831 char path[MAXPGPATH];
833 xldir = opendir(XLOGDIR);
836 fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
837 progname, XLOGDIR, strerror(errno));
842 while ((xlde = readdir(xldir)) != NULL)
844 if (strlen(xlde->d_name) == 24 &&
845 strspn(xlde->d_name, "0123456789ABCDEF") == 24)
847 snprintf(path, MAXPGPATH, "%s/%s", XLOGDIR, xlde->d_name);
848 if (unlink(path) < 0)
850 fprintf(stderr, _("%s: could not delete file \"%s\": %s\n"),
851 progname, path, strerror(errno));
860 * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
863 if (GetLastError() == ERROR_NO_MORE_FILES)
869 fprintf(stderr, _("%s: could not read from directory \"%s\": %s\n"),
870 progname, XLOGDIR, strerror(errno));
878 * Remove existing archive status files
881 KillExistingArchiveStatus(void)
885 char path[MAXPGPATH];
887 #define ARCHSTATDIR XLOGDIR "/archive_status"
889 xldir = opendir(ARCHSTATDIR);
892 fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
893 progname, ARCHSTATDIR, strerror(errno));
898 while ((xlde = readdir(xldir)) != NULL)
900 if (strspn(xlde->d_name, "0123456789ABCDEF") == 24 &&
901 (strcmp(xlde->d_name + 24, ".ready") == 0 ||
902 strcmp(xlde->d_name + 24, ".done") == 0))
904 snprintf(path, MAXPGPATH, "%s/%s", ARCHSTATDIR, xlde->d_name);
905 if (unlink(path) < 0)
907 fprintf(stderr, _("%s: could not delete file \"%s\": %s\n"),
908 progname, path, strerror(errno));
917 * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
920 if (GetLastError() == ERROR_NO_MORE_FILES)
926 fprintf(stderr, _("%s: could not read from directory \"%s\": %s\n"),
927 progname, ARCHSTATDIR, strerror(errno));
935 * Write an empty XLOG file, containing only the checkpoint record
936 * already set up in ControlFile.
943 XLogLongPageHeader longpage;
946 char path[MAXPGPATH];
950 /* Use malloc() to ensure buffer is MAXALIGNED */
951 buffer = (char *) pg_malloc(XLOG_BLCKSZ);
952 page = (XLogPageHeader) buffer;
953 memset(buffer, 0, XLOG_BLCKSZ);
955 /* Set up the XLOG page header */
956 page->xlp_magic = XLOG_PAGE_MAGIC;
957 page->xlp_info = XLP_LONG_HEADER;
958 page->xlp_tli = ControlFile.checkPointCopy.ThisTimeLineID;
959 page->xlp_pageaddr = ControlFile.checkPointCopy.redo - SizeOfXLogLongPHD;
960 longpage = (XLogLongPageHeader) page;
961 longpage->xlp_sysid = ControlFile.system_identifier;
962 longpage->xlp_seg_size = XLogSegSize;
963 longpage->xlp_xlog_blcksz = XLOG_BLCKSZ;
965 /* Insert the initial checkpoint record */
966 record = (XLogRecord *) ((char *) page + SizeOfXLogLongPHD);
968 record->xl_xid = InvalidTransactionId;
969 record->xl_tot_len = SizeOfXLogRecord + sizeof(CheckPoint);
970 record->xl_len = sizeof(CheckPoint);
971 record->xl_info = XLOG_CHECKPOINT_SHUTDOWN;
972 record->xl_rmid = RM_XLOG_ID;
973 memcpy(XLogRecGetData(record), &ControlFile.checkPointCopy,
977 COMP_CRC32(crc, &ControlFile.checkPointCopy, sizeof(CheckPoint));
978 COMP_CRC32(crc, (char *) record, offsetof(XLogRecord, xl_crc));
980 record->xl_crc = crc;
982 /* Write the first page */
983 XLogFilePath(path, ControlFile.checkPointCopy.ThisTimeLineID, newXlogSegNo);
987 fd = open(path, O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
991 fprintf(stderr, _("%s: could not open file \"%s\": %s\n"),
992 progname, path, strerror(errno));
997 if (write(fd, buffer, XLOG_BLCKSZ) != XLOG_BLCKSZ)
999 /* if write didn't set errno, assume problem is no disk space */
1002 fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
1003 progname, path, strerror(errno));
1007 /* Fill the rest of the file with zeroes */
1008 memset(buffer, 0, XLOG_BLCKSZ);
1009 for (nbytes = XLOG_BLCKSZ; nbytes < XLogSegSize; nbytes += XLOG_BLCKSZ)
1012 if (write(fd, buffer, XLOG_BLCKSZ) != XLOG_BLCKSZ)
1016 fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
1017 progname, path, strerror(errno));
1024 fprintf(stderr, _("%s: fsync error: %s\n"), progname, strerror(errno));
1035 printf(_("%s resets the PostgreSQL transaction log.\n\n"), progname);
1036 printf(_("Usage:\n %s [OPTION]... DATADIR\n\n"), progname);
1037 printf(_("Options:\n"));
1038 printf(_(" -e XIDEPOCH set next transaction ID epoch\n"));
1039 printf(_(" -f force update to be done\n"));
1040 printf(_(" -l XLOGFILE force minimum WAL starting location for new transaction log\n"));
1041 printf(_(" -m MXID,MXID set next and oldest multitransaction ID\n"));
1042 printf(_(" -n no update, just show extracted control values (for testing)\n"));
1043 printf(_(" -o OID set next OID\n"));
1044 printf(_(" -O OFFSET set next multitransaction offset\n"));
1045 printf(_(" -V, --version output version information, then exit\n"));
1046 printf(_(" -x XID set next transaction ID\n"));
1047 printf(_(" -?, --help show this help, then exit\n"));
1048 printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));