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);
196 * XXX It'd be nice to have more sanity checks here, e.g. so
197 * that oldest is not wrapped around w.r.t. nextMulti.
199 if (set_oldestmxid == 0)
201 fprintf(stderr, _("%s: oldest multitransaction ID (-m) must not be 0\n"),
208 set_mxoff = strtoul(optarg, &endptr, 0);
209 if (endptr == optarg || *endptr != '\0')
211 fprintf(stderr, _("%s: invalid argument for option -O\n"), progname);
212 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
217 fprintf(stderr, _("%s: multitransaction offset (-O) must not be -1\n"), progname);
223 if (strspn(optarg, "01234567890ABCDEFabcdef") != 24)
225 fprintf(stderr, _("%s: invalid argument for option -l\n"), progname);
226 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
229 XLogFromFileName(optarg, &minXlogTli, &minXlogSegNo);
233 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
240 fprintf(stderr, _("%s: no data directory specified\n"), progname);
241 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
246 * Don't allow pg_resetxlog to be run as root, to avoid overwriting the
247 * ownership of files in the data directory. We need only check for root
248 * -- any other user won't have sufficient permissions to modify files in
249 * the data directory.
254 fprintf(stderr, _("%s: cannot be executed by \"root\"\n"),
256 fprintf(stderr, _("You must run %s as the PostgreSQL superuser.\n"),
262 DataDir = argv[optind];
264 if (chdir(DataDir) < 0)
266 fprintf(stderr, _("%s: could not change directory to \"%s\": %s\n"),
267 progname, DataDir, strerror(errno));
272 * Check for a postmaster lock file --- if there is one, refuse to
273 * proceed, on grounds we might be interfering with a live installation.
275 if ((fd = open("postmaster.pid", O_RDONLY, 0)) < 0)
279 fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
280 progname, "postmaster.pid", strerror(errno));
286 fprintf(stderr, _("%s: lock file \"%s\" exists\n"
287 "Is a server running? If not, delete the lock file and try again.\n"),
288 progname, "postmaster.pid");
293 * Attempt to read the existing pg_control file
295 if (!ReadControlFile())
296 GuessControlValues();
299 * Also look at existing segment files to set up newXlogSegNo
304 * Adjust fields if required by switches. (Do this now so that printout,
305 * if any, includes these values.)
307 if (set_xid_epoch != -1)
308 ControlFile.checkPointCopy.nextXidEpoch = set_xid_epoch;
312 ControlFile.checkPointCopy.nextXid = set_xid;
315 * For the moment, just set oldestXid to a value that will force
316 * immediate autovacuum-for-wraparound. It's not clear whether adding
317 * user control of this is useful, so let's just do something that's
318 * reasonably safe. The magic constant here corresponds to the
319 * maximum allowed value of autovacuum_freeze_max_age.
321 ControlFile.checkPointCopy.oldestXid = set_xid - 2000000000;
322 if (ControlFile.checkPointCopy.oldestXid < FirstNormalTransactionId)
323 ControlFile.checkPointCopy.oldestXid += FirstNormalTransactionId;
324 ControlFile.checkPointCopy.oldestXidDB = InvalidOid;
328 ControlFile.checkPointCopy.nextOid = set_oid;
332 ControlFile.checkPointCopy.nextMulti = set_mxid;
334 ControlFile.checkPointCopy.oldestMulti = set_oldestmxid;
335 if (ControlFile.checkPointCopy.oldestMulti < FirstMultiXactId)
336 ControlFile.checkPointCopy.oldestMulti += FirstMultiXactId;
337 ControlFile.checkPointCopy.oldestMultiDB = InvalidOid;
341 ControlFile.checkPointCopy.nextMultiOffset = set_mxoff;
343 if (minXlogTli > ControlFile.checkPointCopy.ThisTimeLineID)
345 ControlFile.checkPointCopy.ThisTimeLineID = minXlogTli;
346 ControlFile.checkPointCopy.PrevTimeLineID = minXlogTli;
349 if (minXlogSegNo > newXlogSegNo)
350 newXlogSegNo = minXlogSegNo;
353 * If we had to guess anything, and -f was not given, just print the
354 * guessed values and exit. Also print if -n is given.
356 if ((guessed && !force) || noupdate)
358 PrintControlValues(guessed);
361 printf(_("\nIf these values seem acceptable, use -f to force reset.\n"));
369 * Don't reset from a dirty pg_control without -f, either.
371 if (ControlFile.state != DB_SHUTDOWNED && !force)
373 printf(_("The database server was not shut down cleanly.\n"
374 "Resetting the transaction log might cause data to be lost.\n"
375 "If you want to proceed anyway, use -f to force reset.\n"));
380 * Else, do the dirty deed.
382 RewriteControlFile();
384 KillExistingArchiveStatus();
387 printf(_("Transaction log reset\n"));
393 * Try to read the existing pg_control file.
395 * This routine is also responsible for updating old pg_control versions
396 * to the current format. (Currently we don't do anything of the sort.)
399 ReadControlFile(void)
406 if ((fd = open(XLOG_CONTROL_FILE, O_RDONLY | PG_BINARY, 0)) < 0)
409 * If pg_control is not there at all, or we can't read it, the odds
410 * are we've been handed a bad DataDir path, so give up. User can do
411 * "touch pg_control" to force us to proceed.
413 fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
414 progname, XLOG_CONTROL_FILE, strerror(errno));
416 fprintf(stderr, _("If you are sure the data directory path is correct, execute\n"
423 /* Use malloc to ensure we have a maxaligned buffer */
424 buffer = (char *) pg_malloc(PG_CONTROL_SIZE);
426 len = read(fd, buffer, PG_CONTROL_SIZE);
429 fprintf(stderr, _("%s: could not read file \"%s\": %s\n"),
430 progname, XLOG_CONTROL_FILE, strerror(errno));
435 if (len >= sizeof(ControlFileData) &&
436 ((ControlFileData *) buffer)->pg_control_version == PG_CONTROL_VERSION)
442 offsetof(ControlFileData, crc));
445 if (EQ_CRC32(crc, ((ControlFileData *) buffer)->crc))
448 memcpy(&ControlFile, buffer, sizeof(ControlFile));
452 fprintf(stderr, _("%s: pg_control exists but has invalid CRC; proceed with caution\n"),
454 /* We will use the data anyway, but treat it as guessed. */
455 memcpy(&ControlFile, buffer, sizeof(ControlFile));
460 /* Looks like it's a mess. */
461 fprintf(stderr, _("%s: pg_control exists but is broken or unknown version; ignoring it\n"),
468 * Guess at pg_control values when we can't read the old ones.
471 GuessControlValues(void)
473 uint64 sysidentifier;
477 * Set up a completely default set of pg_control values.
480 memset(&ControlFile, 0, sizeof(ControlFile));
482 ControlFile.pg_control_version = PG_CONTROL_VERSION;
483 ControlFile.catalog_version_no = CATALOG_VERSION_NO;
486 * Create a new unique installation identifier, since we can no longer use
487 * any old XLOG records. See notes in xlog.c about the algorithm.
489 gettimeofday(&tv, NULL);
490 sysidentifier = ((uint64) tv.tv_sec) << 32;
491 sysidentifier |= (uint32) (tv.tv_sec | tv.tv_usec);
493 ControlFile.system_identifier = sysidentifier;
495 ControlFile.checkPointCopy.redo = SizeOfXLogLongPHD;
496 ControlFile.checkPointCopy.ThisTimeLineID = 1;
497 ControlFile.checkPointCopy.PrevTimeLineID = 1;
498 ControlFile.checkPointCopy.fullPageWrites = false;
499 ControlFile.checkPointCopy.nextXidEpoch = 0;
500 ControlFile.checkPointCopy.nextXid = FirstNormalTransactionId;
501 ControlFile.checkPointCopy.nextOid = FirstBootstrapObjectId;
502 ControlFile.checkPointCopy.nextMulti = FirstMultiXactId;
503 ControlFile.checkPointCopy.nextMultiOffset = 0;
504 ControlFile.checkPointCopy.oldestXid = FirstNormalTransactionId;
505 ControlFile.checkPointCopy.oldestXidDB = InvalidOid;
506 ControlFile.checkPointCopy.oldestMulti = FirstMultiXactId;
507 ControlFile.checkPointCopy.oldestMultiDB = InvalidOid;
508 ControlFile.checkPointCopy.time = (pg_time_t) time(NULL);
509 ControlFile.checkPointCopy.oldestActiveXid = InvalidTransactionId;
511 ControlFile.state = DB_SHUTDOWNED;
512 ControlFile.time = (pg_time_t) time(NULL);
513 ControlFile.checkPoint = ControlFile.checkPointCopy.redo;
514 ControlFile.unloggedLSN = 1;
516 /* minRecoveryPoint, backupStartPoint and backupEndPoint can be left zero */
518 ControlFile.wal_level = WAL_LEVEL_MINIMAL;
519 ControlFile.MaxConnections = 100;
520 ControlFile.max_prepared_xacts = 0;
521 ControlFile.max_locks_per_xact = 64;
523 ControlFile.maxAlign = MAXIMUM_ALIGNOF;
524 ControlFile.floatFormat = FLOATFORMAT_VALUE;
525 ControlFile.blcksz = BLCKSZ;
526 ControlFile.relseg_size = RELSEG_SIZE;
527 ControlFile.xlog_blcksz = XLOG_BLCKSZ;
528 ControlFile.xlog_seg_size = XLOG_SEG_SIZE;
529 ControlFile.nameDataLen = NAMEDATALEN;
530 ControlFile.indexMaxKeys = INDEX_MAX_KEYS;
531 ControlFile.toast_max_chunk_size = TOAST_MAX_CHUNK_SIZE;
532 #ifdef HAVE_INT64_TIMESTAMP
533 ControlFile.enableIntTimes = true;
535 ControlFile.enableIntTimes = false;
537 ControlFile.float4ByVal = FLOAT4PASSBYVAL;
538 ControlFile.float8ByVal = FLOAT8PASSBYVAL;
541 * XXX eventually, should try to grovel through old XLOG to develop more
542 * accurate values for TimeLineID, nextXID, etc.
548 * Print the guessed pg_control values when we had to guess.
550 * NB: this display should be just those fields that will not be
551 * reset by RewriteControlFile().
554 PrintControlValues(bool guessed)
556 char sysident_str[32];
557 char fname[MAXFNAMELEN];
560 printf(_("Guessed pg_control values:\n\n"));
562 printf(_("pg_control values:\n\n"));
565 * Format system_identifier separately to keep platform-dependent format
566 * code out of the translatable message string.
568 snprintf(sysident_str, sizeof(sysident_str), UINT64_FORMAT,
569 ControlFile.system_identifier);
571 XLogFileName(fname, ControlFile.checkPointCopy.ThisTimeLineID, newXlogSegNo);
573 printf(_("First log segment after reset: %s\n"),
575 printf(_("pg_control version number: %u\n"),
576 ControlFile.pg_control_version);
577 printf(_("Catalog version number: %u\n"),
578 ControlFile.catalog_version_no);
579 printf(_("Database system identifier: %s\n"),
581 printf(_("Latest checkpoint's TimeLineID: %u\n"),
582 ControlFile.checkPointCopy.ThisTimeLineID);
583 printf(_("Latest checkpoint's full_page_writes: %s\n"),
584 ControlFile.checkPointCopy.fullPageWrites ? _("on") : _("off"));
585 printf(_("Latest checkpoint's NextXID: %u/%u\n"),
586 ControlFile.checkPointCopy.nextXidEpoch,
587 ControlFile.checkPointCopy.nextXid);
588 printf(_("Latest checkpoint's NextOID: %u\n"),
589 ControlFile.checkPointCopy.nextOid);
590 printf(_("Latest checkpoint's NextMultiXactId: %u\n"),
591 ControlFile.checkPointCopy.nextMulti);
592 printf(_("Latest checkpoint's NextMultiOffset: %u\n"),
593 ControlFile.checkPointCopy.nextMultiOffset);
594 printf(_("Latest checkpoint's oldestXID: %u\n"),
595 ControlFile.checkPointCopy.oldestXid);
596 printf(_("Latest checkpoint's oldestXID's DB: %u\n"),
597 ControlFile.checkPointCopy.oldestXidDB);
598 printf(_("Latest checkpoint's oldestActiveXID: %u\n"),
599 ControlFile.checkPointCopy.oldestActiveXid);
600 printf(_("Latest checkpoint's oldestMultiXid: %u\n"),
601 ControlFile.checkPointCopy.oldestMulti);
602 printf(_("Latest checkpoint's oldestMulti's DB: %u\n"),
603 ControlFile.checkPointCopy.oldestMultiDB);
604 printf(_("Maximum data alignment: %u\n"),
605 ControlFile.maxAlign);
606 /* we don't print floatFormat since can't say much useful about it */
607 printf(_("Database block size: %u\n"),
609 printf(_("Blocks per segment of large relation: %u\n"),
610 ControlFile.relseg_size);
611 printf(_("WAL block size: %u\n"),
612 ControlFile.xlog_blcksz);
613 printf(_("Bytes per WAL segment: %u\n"),
614 ControlFile.xlog_seg_size);
615 printf(_("Maximum length of identifiers: %u\n"),
616 ControlFile.nameDataLen);
617 printf(_("Maximum columns in an index: %u\n"),
618 ControlFile.indexMaxKeys);
619 printf(_("Maximum size of a TOAST chunk: %u\n"),
620 ControlFile.toast_max_chunk_size);
621 printf(_("Date/time type storage: %s\n"),
622 (ControlFile.enableIntTimes ? _("64-bit integers") : _("floating-point numbers")));
623 printf(_("Float4 argument passing: %s\n"),
624 (ControlFile.float4ByVal ? _("by value") : _("by reference")));
625 printf(_("Float8 argument passing: %s\n"),
626 (ControlFile.float8ByVal ? _("by value") : _("by reference")));
627 printf(_("Data page checksums: %s\n"),
628 (ControlFile.data_checksums ? _("enabled") : _("disabled")));
633 * Write out the new pg_control file.
636 RewriteControlFile(void)
639 char buffer[PG_CONTROL_SIZE]; /* need not be aligned */
642 * Adjust fields as needed to force an empty XLOG starting at
645 XLogSegNoOffsetToRecPtr(newXlogSegNo, SizeOfXLogLongPHD,
646 ControlFile.checkPointCopy.redo);
647 ControlFile.checkPointCopy.time = (pg_time_t) time(NULL);
649 ControlFile.state = DB_SHUTDOWNED;
650 ControlFile.time = (pg_time_t) time(NULL);
651 ControlFile.checkPoint = ControlFile.checkPointCopy.redo;
652 ControlFile.prevCheckPoint = 0;
653 ControlFile.minRecoveryPoint = 0;
654 ControlFile.minRecoveryPointTLI = 0;
655 ControlFile.backupStartPoint = 0;
656 ControlFile.backupEndPoint = 0;
657 ControlFile.backupEndRequired = false;
660 * Force the defaults for max_* settings. The values don't really matter
661 * as long as wal_level='minimal'; the postmaster will reset these fields
664 ControlFile.wal_level = WAL_LEVEL_MINIMAL;
665 ControlFile.MaxConnections = 100;
666 ControlFile.max_prepared_xacts = 0;
667 ControlFile.max_locks_per_xact = 64;
669 /* Now we can force the recorded xlog seg size to the right thing. */
670 ControlFile.xlog_seg_size = XLogSegSize;
672 /* Contents are protected with a CRC */
673 INIT_CRC32(ControlFile.crc);
674 COMP_CRC32(ControlFile.crc,
675 (char *) &ControlFile,
676 offsetof(ControlFileData, crc));
677 FIN_CRC32(ControlFile.crc);
680 * We write out PG_CONTROL_SIZE bytes into pg_control, zero-padding the
681 * excess over sizeof(ControlFileData). This reduces the odds of
682 * premature-EOF errors when reading pg_control. We'll still fail when we
683 * check the contents of the file, but hopefully with a more specific
684 * error than "couldn't read pg_control".
686 if (sizeof(ControlFileData) > PG_CONTROL_SIZE)
689 _("%s: internal error -- sizeof(ControlFileData) is too large ... fix PG_CONTROL_SIZE\n"),
694 memset(buffer, 0, PG_CONTROL_SIZE);
695 memcpy(buffer, &ControlFile, sizeof(ControlFileData));
697 unlink(XLOG_CONTROL_FILE);
699 fd = open(XLOG_CONTROL_FILE,
700 O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
704 fprintf(stderr, _("%s: could not create pg_control file: %s\n"),
705 progname, strerror(errno));
710 if (write(fd, buffer, PG_CONTROL_SIZE) != PG_CONTROL_SIZE)
712 /* if write didn't set errno, assume problem is no disk space */
715 fprintf(stderr, _("%s: could not write pg_control file: %s\n"),
716 progname, strerror(errno));
722 fprintf(stderr, _("%s: fsync error: %s\n"), progname, strerror(errno));
731 * Scan existing XLOG files and determine the highest existing WAL address
733 * On entry, ControlFile.checkPointCopy.redo and ControlFile.xlog_seg_size
734 * are assumed valid (note that we allow the old xlog seg size to differ
735 * from what we're using). On exit, newXlogId and newXlogSeg are set to
736 * suitable values for the beginning of replacement WAL (in our seg size).
743 uint64 segs_per_xlogid;
747 * Initialize the max() computation using the last checkpoint address from
748 * old pg_control. Note that for the moment we are working with segment
749 * numbering according to the old xlog seg size.
751 segs_per_xlogid = (UINT64CONST(0x0000000100000000) / ControlFile.xlog_seg_size);
752 newXlogSegNo = ControlFile.checkPointCopy.redo / ControlFile.xlog_seg_size;
755 * Scan the pg_xlog directory to find existing WAL segment files. We
756 * assume any present have been used; in most scenarios this should be
757 * conservative, because of xlog.c's attempts to pre-create files.
759 xldir = opendir(XLOGDIR);
762 fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
763 progname, XLOGDIR, strerror(errno));
768 while ((xlde = readdir(xldir)) != NULL)
770 if (strlen(xlde->d_name) == 24 &&
771 strspn(xlde->d_name, "0123456789ABCDEF") == 24)
778 sscanf(xlde->d_name, "%08X%08X%08X", &tli, &log, &seg);
779 segno = ((uint64) log) * segs_per_xlogid + seg;
782 * Note: we take the max of all files found, regardless of their
783 * timelines. Another possibility would be to ignore files of
784 * timelines other than the target TLI, but this seems safer.
785 * Better too large a result than too small...
787 if (segno > newXlogSegNo)
788 newXlogSegNo = segno;
795 * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
798 if (GetLastError() == ERROR_NO_MORE_FILES)
804 fprintf(stderr, _("%s: could not read from directory \"%s\": %s\n"),
805 progname, XLOGDIR, strerror(errno));
811 * Finally, convert to new xlog seg size, and advance by one to ensure we
812 * are in virgin territory.
814 xlogbytepos = newXlogSegNo * ControlFile.xlog_seg_size;
815 newXlogSegNo = (xlogbytepos + XLogSegSize - 1) / XLogSegSize;
821 * Remove existing XLOG files
824 KillExistingXLOG(void)
828 char path[MAXPGPATH];
830 xldir = opendir(XLOGDIR);
833 fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
834 progname, XLOGDIR, strerror(errno));
839 while ((xlde = readdir(xldir)) != NULL)
841 if (strlen(xlde->d_name) == 24 &&
842 strspn(xlde->d_name, "0123456789ABCDEF") == 24)
844 snprintf(path, MAXPGPATH, "%s/%s", XLOGDIR, xlde->d_name);
845 if (unlink(path) < 0)
847 fprintf(stderr, _("%s: could not delete file \"%s\": %s\n"),
848 progname, path, strerror(errno));
857 * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
860 if (GetLastError() == ERROR_NO_MORE_FILES)
866 fprintf(stderr, _("%s: could not read from directory \"%s\": %s\n"),
867 progname, XLOGDIR, strerror(errno));
875 * Remove existing archive status files
878 KillExistingArchiveStatus(void)
882 char path[MAXPGPATH];
884 #define ARCHSTATDIR XLOGDIR "/archive_status"
886 xldir = opendir(ARCHSTATDIR);
889 fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
890 progname, ARCHSTATDIR, strerror(errno));
895 while ((xlde = readdir(xldir)) != NULL)
897 if (strspn(xlde->d_name, "0123456789ABCDEF") == 24 &&
898 (strcmp(xlde->d_name + 24, ".ready") == 0 ||
899 strcmp(xlde->d_name + 24, ".done") == 0))
901 snprintf(path, MAXPGPATH, "%s/%s", ARCHSTATDIR, 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, ARCHSTATDIR, strerror(errno));
932 * Write an empty XLOG file, containing only the checkpoint record
933 * already set up in ControlFile.
940 XLogLongPageHeader longpage;
943 char path[MAXPGPATH];
947 /* Use malloc() to ensure buffer is MAXALIGNED */
948 buffer = (char *) pg_malloc(XLOG_BLCKSZ);
949 page = (XLogPageHeader) buffer;
950 memset(buffer, 0, XLOG_BLCKSZ);
952 /* Set up the XLOG page header */
953 page->xlp_magic = XLOG_PAGE_MAGIC;
954 page->xlp_info = XLP_LONG_HEADER;
955 page->xlp_tli = ControlFile.checkPointCopy.ThisTimeLineID;
956 page->xlp_pageaddr = ControlFile.checkPointCopy.redo - SizeOfXLogLongPHD;
957 longpage = (XLogLongPageHeader) page;
958 longpage->xlp_sysid = ControlFile.system_identifier;
959 longpage->xlp_seg_size = XLogSegSize;
960 longpage->xlp_xlog_blcksz = XLOG_BLCKSZ;
962 /* Insert the initial checkpoint record */
963 record = (XLogRecord *) ((char *) page + SizeOfXLogLongPHD);
965 record->xl_xid = InvalidTransactionId;
966 record->xl_tot_len = SizeOfXLogRecord + sizeof(CheckPoint);
967 record->xl_len = sizeof(CheckPoint);
968 record->xl_info = XLOG_CHECKPOINT_SHUTDOWN;
969 record->xl_rmid = RM_XLOG_ID;
970 memcpy(XLogRecGetData(record), &ControlFile.checkPointCopy,
974 COMP_CRC32(crc, &ControlFile.checkPointCopy, sizeof(CheckPoint));
975 COMP_CRC32(crc, (char *) record, offsetof(XLogRecord, xl_crc));
977 record->xl_crc = crc;
979 /* Write the first page */
980 XLogFilePath(path, ControlFile.checkPointCopy.ThisTimeLineID, newXlogSegNo);
984 fd = open(path, O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
988 fprintf(stderr, _("%s: could not open file \"%s\": %s\n"),
989 progname, path, strerror(errno));
994 if (write(fd, buffer, XLOG_BLCKSZ) != XLOG_BLCKSZ)
996 /* if write didn't set errno, assume problem is no disk space */
999 fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
1000 progname, path, strerror(errno));
1004 /* Fill the rest of the file with zeroes */
1005 memset(buffer, 0, XLOG_BLCKSZ);
1006 for (nbytes = XLOG_BLCKSZ; nbytes < XLogSegSize; nbytes += XLOG_BLCKSZ)
1009 if (write(fd, buffer, XLOG_BLCKSZ) != XLOG_BLCKSZ)
1013 fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
1014 progname, path, strerror(errno));
1021 fprintf(stderr, _("%s: fsync error: %s\n"), progname, strerror(errno));
1032 printf(_("%s resets the PostgreSQL transaction log.\n\n"), progname);
1033 printf(_("Usage:\n %s [OPTION]... DATADIR\n\n"), progname);
1034 printf(_("Options:\n"));
1035 printf(_(" -e XIDEPOCH set next transaction ID epoch\n"));
1036 printf(_(" -f force update to be done\n"));
1037 printf(_(" -l XLOGFILE force minimum WAL starting location for new transaction log\n"));
1038 printf(_(" -m XID,OLDEST set next multitransaction ID and oldest value\n"));
1039 printf(_(" -n no update, just show extracted control values (for testing)\n"));
1040 printf(_(" -o OID set next OID\n"));
1041 printf(_(" -O OFFSET set next multitransaction offset\n"));
1042 printf(_(" -V, --version output version information, then exit\n"));
1043 printf(_(" -x XID set next transaction ID\n"));
1044 printf(_(" -?, --help show this help, then exit\n"));
1045 printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));