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-2010, PostgreSQL Global Development Group
24 * Portions Copyright (c) 1994, Regents of the University of California
26 * $PostgreSQL: pgsql/src/bin/pg_resetxlog/pg_resetxlog.c,v 1.80 2010/04/28 19:38:49 tgl Exp $
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"
62 static ControlFileData ControlFile; /* pg_control values */
63 static uint32 newXlogId,
64 newXlogSeg; /* ID/Segment of 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 MultiXactOffset set_mxoff = (MultiXactOffset) -1;
90 uint32 minXlogTli = 0,
100 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_resetxlog"));
102 progname = get_progname(argv[0]);
106 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
111 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
113 puts("pg_resetxlog (PostgreSQL) " PG_VERSION);
119 while ((c = getopt(argc, argv, "fl:m:no:O:x:e:")) != -1)
132 set_xid_epoch = strtoul(optarg, &endptr, 0);
133 if (endptr == optarg || *endptr != '\0')
135 fprintf(stderr, _("%s: invalid argument for option -e\n"), progname);
136 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
139 if (set_xid_epoch == -1)
141 fprintf(stderr, _("%s: transaction ID epoch (-e) must not be -1\n"), progname);
147 set_xid = strtoul(optarg, &endptr, 0);
148 if (endptr == optarg || *endptr != '\0')
150 fprintf(stderr, _("%s: invalid argument for option -x\n"), progname);
151 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
156 fprintf(stderr, _("%s: transaction ID (-x) must not be 0\n"), progname);
162 set_oid = strtoul(optarg, &endptr, 0);
163 if (endptr == optarg || *endptr != '\0')
165 fprintf(stderr, _("%s: invalid argument for option -o\n"), progname);
166 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
171 fprintf(stderr, _("%s: OID (-o) must not be 0\n"), progname);
177 set_mxid = strtoul(optarg, &endptr, 0);
178 if (endptr == optarg || *endptr != '\0')
180 fprintf(stderr, _("%s: invalid argument for option -m\n"), progname);
181 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
186 fprintf(stderr, _("%s: multitransaction ID (-m) must not be 0\n"), progname);
192 set_mxoff = strtoul(optarg, &endptr, 0);
193 if (endptr == optarg || *endptr != '\0')
195 fprintf(stderr, _("%s: invalid argument for option -O\n"), progname);
196 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
201 fprintf(stderr, _("%s: multitransaction offset (-O) must not be -1\n"), progname);
207 minXlogTli = strtoul(optarg, &endptr, 0);
208 if (endptr == optarg || *endptr != ',')
210 fprintf(stderr, _("%s: invalid argument for option -l\n"), progname);
211 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
214 minXlogId = strtoul(endptr + 1, &endptr2, 0);
215 if (endptr2 == endptr + 1 || *endptr2 != ',')
217 fprintf(stderr, _("%s: invalid argument for option -l\n"), progname);
218 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
221 minXlogSeg = strtoul(endptr2 + 1, &endptr3, 0);
222 if (endptr3 == endptr2 + 1 || *endptr3 != '\0')
224 fprintf(stderr, _("%s: invalid argument for option -l\n"), progname);
225 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
231 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
238 fprintf(stderr, _("%s: no data directory specified\n"), progname);
239 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
244 * Don't allow pg_resetxlog to be run as root, to avoid overwriting the
245 * ownership of files in the data directory. We need only check for root
246 * -- any other user won't have sufficient permissions to modify files in
247 * the data directory.
252 fprintf(stderr, _("%s: cannot be executed by \"root\"\n"),
254 fprintf(stderr, _("You must run %s as the PostgreSQL superuser.\n"),
260 DataDir = argv[optind];
262 if (chdir(DataDir) < 0)
264 fprintf(stderr, _("%s: could not change directory to \"%s\": %s\n"),
265 progname, DataDir, strerror(errno));
270 * Check for a postmaster lock file --- if there is one, refuse to
271 * proceed, on grounds we might be interfering with a live installation.
273 snprintf(path, MAXPGPATH, "%s/postmaster.pid", DataDir);
275 if ((fd = open(path, O_RDONLY, 0)) < 0)
279 fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"), progname, path, strerror(errno));
285 fprintf(stderr, _("%s: lock file \"%s\" exists\n"
286 "Is a server running? If not, delete the lock file and try again.\n"),
292 * Attempt to read the existing pg_control file
294 if (!ReadControlFile())
295 GuessControlValues();
298 * Also look at existing segment files to set up newXlogId/newXlogSeg
303 * Adjust fields if required by switches. (Do this now so that printout,
304 * if any, includes these values.)
306 if (set_xid_epoch != -1)
307 ControlFile.checkPointCopy.nextXidEpoch = set_xid_epoch;
311 ControlFile.checkPointCopy.nextXid = set_xid;
314 * For the moment, just set oldestXid to a value that will force
315 * immediate autovacuum-for-wraparound. It's not clear whether adding
316 * user control of this is useful, so let's just do something that's
317 * reasonably safe. The magic constant here corresponds to the
318 * maximum allowed value of autovacuum_freeze_max_age.
320 ControlFile.checkPointCopy.oldestXid = set_xid - 2000000000;
321 if (ControlFile.checkPointCopy.oldestXid < FirstNormalTransactionId)
322 ControlFile.checkPointCopy.oldestXid += FirstNormalTransactionId;
323 ControlFile.checkPointCopy.oldestXidDB = InvalidOid;
327 ControlFile.checkPointCopy.nextOid = set_oid;
330 ControlFile.checkPointCopy.nextMulti = set_mxid;
333 ControlFile.checkPointCopy.nextMultiOffset = set_mxoff;
335 if (minXlogTli > ControlFile.checkPointCopy.ThisTimeLineID)
336 ControlFile.checkPointCopy.ThisTimeLineID = minXlogTli;
338 if (minXlogId > newXlogId ||
339 (minXlogId == newXlogId &&
340 minXlogSeg > newXlogSeg))
342 newXlogId = minXlogId;
343 newXlogSeg = minXlogSeg;
347 * If we had to guess anything, and -f was not given, just print the
348 * guessed values and exit. Also print if -n is given.
350 if ((guessed && !force) || noupdate)
352 PrintControlValues(guessed);
355 printf(_("\nIf these values seem acceptable, use -f to force reset.\n"));
363 * Don't reset from a dirty pg_control without -f, either.
365 if (ControlFile.state != DB_SHUTDOWNED && !force)
367 printf(_("The database server was not shut down cleanly.\n"
368 "Resetting the transaction log might cause data to be lost.\n"
369 "If you want to proceed anyway, use -f to force reset.\n"));
374 * Else, do the dirty deed.
376 RewriteControlFile();
378 KillExistingArchiveStatus();
381 printf(_("Transaction log reset\n"));
387 * Try to read the existing pg_control file.
389 * This routine is also responsible for updating old pg_control versions
390 * to the current format. (Currently we don't do anything of the sort.)
393 ReadControlFile(void)
400 if ((fd = open(XLOG_CONTROL_FILE, O_RDONLY | PG_BINARY, 0)) < 0)
403 * If pg_control is not there at all, or we can't read it, the odds
404 * are we've been handed a bad DataDir path, so give up. User can do
405 * "touch pg_control" to force us to proceed.
407 fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
408 progname, XLOG_CONTROL_FILE, strerror(errno));
410 fprintf(stderr, _("If you are sure the data directory path is correct, execute\n"
417 /* Use malloc to ensure we have a maxaligned buffer */
418 buffer = (char *) malloc(PG_CONTROL_SIZE);
420 len = read(fd, buffer, PG_CONTROL_SIZE);
423 fprintf(stderr, _("%s: could not read file \"%s\": %s\n"),
424 progname, XLOG_CONTROL_FILE, strerror(errno));
429 if (len >= sizeof(ControlFileData) &&
430 ((ControlFileData *) buffer)->pg_control_version == PG_CONTROL_VERSION)
436 offsetof(ControlFileData, crc));
439 if (EQ_CRC32(crc, ((ControlFileData *) buffer)->crc))
442 memcpy(&ControlFile, buffer, sizeof(ControlFile));
446 fprintf(stderr, _("%s: pg_control exists but has invalid CRC; proceed with caution\n"),
448 /* We will use the data anyway, but treat it as guessed. */
449 memcpy(&ControlFile, buffer, sizeof(ControlFile));
454 /* Looks like it's a mess. */
455 fprintf(stderr, _("%s: pg_control exists but is broken or unknown version; ignoring it\n"),
462 * Guess at pg_control values when we can't read the old ones.
465 GuessControlValues(void)
467 uint64 sysidentifier;
471 * Set up a completely default set of pg_control values.
474 memset(&ControlFile, 0, sizeof(ControlFile));
476 ControlFile.pg_control_version = PG_CONTROL_VERSION;
477 ControlFile.catalog_version_no = CATALOG_VERSION_NO;
480 * Create a new unique installation identifier, since we can no longer use
481 * any old XLOG records. See notes in xlog.c about the algorithm.
483 gettimeofday(&tv, NULL);
484 sysidentifier = ((uint64) tv.tv_sec) << 32;
485 sysidentifier |= (uint32) (tv.tv_sec | tv.tv_usec);
487 ControlFile.system_identifier = sysidentifier;
489 ControlFile.checkPointCopy.redo.xlogid = 0;
490 ControlFile.checkPointCopy.redo.xrecoff = SizeOfXLogLongPHD;
491 ControlFile.checkPointCopy.ThisTimeLineID = 1;
492 ControlFile.checkPointCopy.nextXidEpoch = 0;
493 ControlFile.checkPointCopy.nextXid = FirstNormalTransactionId;
494 ControlFile.checkPointCopy.nextOid = FirstBootstrapObjectId;
495 ControlFile.checkPointCopy.nextMulti = FirstMultiXactId;
496 ControlFile.checkPointCopy.nextMultiOffset = 0;
497 ControlFile.checkPointCopy.oldestXid = FirstNormalTransactionId;
498 ControlFile.checkPointCopy.oldestXidDB = InvalidOid;
499 ControlFile.checkPointCopy.time = (pg_time_t) time(NULL);
500 ControlFile.checkPointCopy.oldestActiveXid = InvalidTransactionId;
502 ControlFile.state = DB_SHUTDOWNED;
503 ControlFile.time = (pg_time_t) time(NULL);
504 ControlFile.checkPoint = ControlFile.checkPointCopy.redo;
506 /* minRecoveryPoint and backupStartPoint can be left zero */
508 ControlFile.wal_level = WAL_LEVEL_MINIMAL;
509 ControlFile.MaxConnections = 100;
510 ControlFile.max_prepared_xacts = 0;
511 ControlFile.max_locks_per_xact = 64;
513 ControlFile.maxAlign = MAXIMUM_ALIGNOF;
514 ControlFile.floatFormat = FLOATFORMAT_VALUE;
515 ControlFile.blcksz = BLCKSZ;
516 ControlFile.relseg_size = RELSEG_SIZE;
517 ControlFile.xlog_blcksz = XLOG_BLCKSZ;
518 ControlFile.xlog_seg_size = XLOG_SEG_SIZE;
519 ControlFile.nameDataLen = NAMEDATALEN;
520 ControlFile.indexMaxKeys = INDEX_MAX_KEYS;
521 ControlFile.toast_max_chunk_size = TOAST_MAX_CHUNK_SIZE;
522 #ifdef HAVE_INT64_TIMESTAMP
523 ControlFile.enableIntTimes = true;
525 ControlFile.enableIntTimes = false;
527 ControlFile.float4ByVal = FLOAT4PASSBYVAL;
528 ControlFile.float8ByVal = FLOAT8PASSBYVAL;
531 * XXX eventually, should try to grovel through old XLOG to develop more
532 * accurate values for TimeLineID, nextXID, etc.
538 * Print the guessed pg_control values when we had to guess.
540 * NB: this display should be just those fields that will not be
541 * reset by RewriteControlFile().
544 PrintControlValues(bool guessed)
546 char sysident_str[32];
549 printf(_("Guessed pg_control values:\n\n"));
551 printf(_("pg_control values:\n\n"));
554 * Format system_identifier separately to keep platform-dependent format
555 * code out of the translatable message string.
557 snprintf(sysident_str, sizeof(sysident_str), UINT64_FORMAT,
558 ControlFile.system_identifier);
560 printf(_("First log file ID after reset: %u\n"),
562 printf(_("First log file segment after reset: %u\n"),
564 printf(_("pg_control version number: %u\n"),
565 ControlFile.pg_control_version);
566 printf(_("Catalog version number: %u\n"),
567 ControlFile.catalog_version_no);
568 printf(_("Database system identifier: %s\n"),
570 printf(_("Latest checkpoint's TimeLineID: %u\n"),
571 ControlFile.checkPointCopy.ThisTimeLineID);
572 printf(_("Latest checkpoint's NextXID: %u/%u\n"),
573 ControlFile.checkPointCopy.nextXidEpoch,
574 ControlFile.checkPointCopy.nextXid);
575 printf(_("Latest checkpoint's NextOID: %u\n"),
576 ControlFile.checkPointCopy.nextOid);
577 printf(_("Latest checkpoint's NextMultiXactId: %u\n"),
578 ControlFile.checkPointCopy.nextMulti);
579 printf(_("Latest checkpoint's NextMultiOffset: %u\n"),
580 ControlFile.checkPointCopy.nextMultiOffset);
581 printf(_("Latest checkpoint's oldestXID: %u\n"),
582 ControlFile.checkPointCopy.oldestXid);
583 printf(_("Latest checkpoint's oldestXID's DB: %u\n"),
584 ControlFile.checkPointCopy.oldestXidDB);
585 printf(_("Latest checkpoint's oldestActiveXID: %u\n"),
586 ControlFile.checkPointCopy.oldestActiveXid);
587 printf(_("Maximum data alignment: %u\n"),
588 ControlFile.maxAlign);
589 /* we don't print floatFormat since can't say much useful about it */
590 printf(_("Database block size: %u\n"),
592 printf(_("Blocks per segment of large relation: %u\n"),
593 ControlFile.relseg_size);
594 printf(_("WAL block size: %u\n"),
595 ControlFile.xlog_blcksz);
596 printf(_("Bytes per WAL segment: %u\n"),
597 ControlFile.xlog_seg_size);
598 printf(_("Maximum length of identifiers: %u\n"),
599 ControlFile.nameDataLen);
600 printf(_("Maximum columns in an index: %u\n"),
601 ControlFile.indexMaxKeys);
602 printf(_("Maximum size of a TOAST chunk: %u\n"),
603 ControlFile.toast_max_chunk_size);
604 printf(_("Date/time type storage: %s\n"),
605 (ControlFile.enableIntTimes ? _("64-bit integers") : _("floating-point numbers")));
606 printf(_("Float4 argument passing: %s\n"),
607 (ControlFile.float4ByVal ? _("by value") : _("by reference")));
608 printf(_("Float8 argument passing: %s\n"),
609 (ControlFile.float8ByVal ? _("by value") : _("by reference")));
614 * Write out the new pg_control file.
617 RewriteControlFile(void)
620 char buffer[PG_CONTROL_SIZE]; /* need not be aligned */
623 * Adjust fields as needed to force an empty XLOG starting at
624 * newXlogId/newXlogSeg.
626 ControlFile.checkPointCopy.redo.xlogid = newXlogId;
627 ControlFile.checkPointCopy.redo.xrecoff =
628 newXlogSeg * XLogSegSize + SizeOfXLogLongPHD;
629 ControlFile.checkPointCopy.time = (pg_time_t) time(NULL);
631 ControlFile.state = DB_SHUTDOWNED;
632 ControlFile.time = (pg_time_t) time(NULL);
633 ControlFile.checkPoint = ControlFile.checkPointCopy.redo;
634 ControlFile.prevCheckPoint.xlogid = 0;
635 ControlFile.prevCheckPoint.xrecoff = 0;
636 ControlFile.minRecoveryPoint.xlogid = 0;
637 ControlFile.minRecoveryPoint.xrecoff = 0;
638 ControlFile.backupStartPoint.xlogid = 0;
639 ControlFile.backupStartPoint.xrecoff = 0;
642 * Force the defaults for max_* settings. The values don't really matter
643 * as long as wal_level='minimal'; the postmaster will reset these fields
646 ControlFile.wal_level = WAL_LEVEL_MINIMAL;
647 ControlFile.MaxConnections = 100;
648 ControlFile.max_prepared_xacts = 0;
649 ControlFile.max_locks_per_xact = 64;
651 /* Now we can force the recorded xlog seg size to the right thing. */
652 ControlFile.xlog_seg_size = XLogSegSize;
654 /* Contents are protected with a CRC */
655 INIT_CRC32(ControlFile.crc);
656 COMP_CRC32(ControlFile.crc,
657 (char *) &ControlFile,
658 offsetof(ControlFileData, crc));
659 FIN_CRC32(ControlFile.crc);
662 * We write out PG_CONTROL_SIZE bytes into pg_control, zero-padding the
663 * excess over sizeof(ControlFileData). This reduces the odds of
664 * premature-EOF errors when reading pg_control. We'll still fail when we
665 * check the contents of the file, but hopefully with a more specific
666 * error than "couldn't read pg_control".
668 if (sizeof(ControlFileData) > PG_CONTROL_SIZE)
671 _("%s: internal error -- sizeof(ControlFileData) is too large ... fix PG_CONTROL_SIZE\n"),
676 memset(buffer, 0, PG_CONTROL_SIZE);
677 memcpy(buffer, &ControlFile, sizeof(ControlFileData));
679 unlink(XLOG_CONTROL_FILE);
681 fd = open(XLOG_CONTROL_FILE,
682 O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
686 fprintf(stderr, _("%s: could not create pg_control file: %s\n"),
687 progname, strerror(errno));
692 if (write(fd, buffer, PG_CONTROL_SIZE) != PG_CONTROL_SIZE)
694 /* if write didn't set errno, assume problem is no disk space */
697 fprintf(stderr, _("%s: could not write pg_control file: %s\n"),
698 progname, strerror(errno));
704 fprintf(stderr, _("%s: fsync error: %s\n"), progname, strerror(errno));
713 * Scan existing XLOG files and determine the highest existing WAL address
715 * On entry, ControlFile.checkPointCopy.redo and ControlFile.xlog_seg_size
716 * are assumed valid (note that we allow the old xlog seg size to differ
717 * from what we're using). On exit, newXlogId and newXlogSeg are set to
718 * suitable values for the beginning of replacement WAL (in our seg size).
727 * Initialize the max() computation using the last checkpoint address from
728 * old pg_control. Note that for the moment we are working with segment
729 * numbering according to the old xlog seg size.
731 newXlogId = ControlFile.checkPointCopy.redo.xlogid;
732 newXlogSeg = ControlFile.checkPointCopy.redo.xrecoff / ControlFile.xlog_seg_size;
735 * Scan the pg_xlog directory to find existing WAL segment files. We
736 * assume any present have been used; in most scenarios this should be
737 * conservative, because of xlog.c's attempts to pre-create files.
739 xldir = opendir(XLOGDIR);
742 fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
743 progname, XLOGDIR, strerror(errno));
748 while ((xlde = readdir(xldir)) != NULL)
750 if (strlen(xlde->d_name) == 24 &&
751 strspn(xlde->d_name, "0123456789ABCDEF") == 24)
757 sscanf(xlde->d_name, "%08X%08X%08X", &tli, &log, &seg);
760 * Note: we take the max of all files found, regardless of their
761 * timelines. Another possibility would be to ignore files of
762 * timelines other than the target TLI, but this seems safer.
763 * Better too large a result than too small...
765 if (log > newXlogId ||
766 (log == newXlogId && seg > newXlogSeg))
777 * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
780 if (GetLastError() == ERROR_NO_MORE_FILES)
786 fprintf(stderr, _("%s: could not read from directory \"%s\": %s\n"),
787 progname, XLOGDIR, strerror(errno));
793 * Finally, convert to new xlog seg size, and advance by one to ensure we
794 * are in virgin territory.
796 newXlogSeg *= ControlFile.xlog_seg_size;
797 newXlogSeg = (newXlogSeg + XLogSegSize - 1) / XLogSegSize;
799 /* be sure we wrap around correctly at end of a logfile */
800 NextLogSeg(newXlogId, newXlogSeg);
805 * Remove existing XLOG files
808 KillExistingXLOG(void)
812 char path[MAXPGPATH];
814 xldir = opendir(XLOGDIR);
817 fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
818 progname, XLOGDIR, strerror(errno));
823 while ((xlde = readdir(xldir)) != NULL)
825 if (strlen(xlde->d_name) == 24 &&
826 strspn(xlde->d_name, "0123456789ABCDEF") == 24)
828 snprintf(path, MAXPGPATH, "%s/%s", XLOGDIR, xlde->d_name);
829 if (unlink(path) < 0)
831 fprintf(stderr, _("%s: could not delete file \"%s\": %s\n"),
832 progname, path, strerror(errno));
841 * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
844 if (GetLastError() == ERROR_NO_MORE_FILES)
850 fprintf(stderr, _("%s: could not read from directory \"%s\": %s\n"),
851 progname, XLOGDIR, strerror(errno));
859 * Remove existing archive status files
862 KillExistingArchiveStatus(void)
866 char path[MAXPGPATH];
868 #define ARCHSTATDIR XLOGDIR "/archive_status"
870 xldir = opendir(ARCHSTATDIR);
873 fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
874 progname, ARCHSTATDIR, strerror(errno));
879 while ((xlde = readdir(xldir)) != NULL)
881 if (strspn(xlde->d_name, "0123456789ABCDEF") == 24 &&
882 (strcmp(xlde->d_name + 24, ".ready") == 0 ||
883 strcmp(xlde->d_name + 24, ".done") == 0))
885 snprintf(path, MAXPGPATH, "%s/%s", ARCHSTATDIR, xlde->d_name);
886 if (unlink(path) < 0)
888 fprintf(stderr, _("%s: could not delete file \"%s\": %s\n"),
889 progname, path, strerror(errno));
898 * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
901 if (GetLastError() == ERROR_NO_MORE_FILES)
907 fprintf(stderr, _("%s: could not read from directory \"%s\": %s\n"),
908 progname, ARCHSTATDIR, strerror(errno));
916 * Write an empty XLOG file, containing only the checkpoint record
917 * already set up in ControlFile.
924 XLogLongPageHeader longpage;
927 char path[MAXPGPATH];
931 /* Use malloc() to ensure buffer is MAXALIGNED */
932 buffer = (char *) malloc(XLOG_BLCKSZ);
933 page = (XLogPageHeader) buffer;
934 memset(buffer, 0, XLOG_BLCKSZ);
936 /* Set up the XLOG page header */
937 page->xlp_magic = XLOG_PAGE_MAGIC;
938 page->xlp_info = XLP_LONG_HEADER;
939 page->xlp_tli = ControlFile.checkPointCopy.ThisTimeLineID;
940 page->xlp_pageaddr.xlogid =
941 ControlFile.checkPointCopy.redo.xlogid;
942 page->xlp_pageaddr.xrecoff =
943 ControlFile.checkPointCopy.redo.xrecoff - SizeOfXLogLongPHD;
944 longpage = (XLogLongPageHeader) page;
945 longpage->xlp_sysid = ControlFile.system_identifier;
946 longpage->xlp_seg_size = XLogSegSize;
947 longpage->xlp_xlog_blcksz = XLOG_BLCKSZ;
949 /* Insert the initial checkpoint record */
950 record = (XLogRecord *) ((char *) page + SizeOfXLogLongPHD);
951 record->xl_prev.xlogid = 0;
952 record->xl_prev.xrecoff = 0;
953 record->xl_xid = InvalidTransactionId;
954 record->xl_tot_len = SizeOfXLogRecord + sizeof(CheckPoint);
955 record->xl_len = sizeof(CheckPoint);
956 record->xl_info = XLOG_CHECKPOINT_SHUTDOWN;
957 record->xl_rmid = RM_XLOG_ID;
958 memcpy(XLogRecGetData(record), &ControlFile.checkPointCopy,
962 COMP_CRC32(crc, &ControlFile.checkPointCopy, sizeof(CheckPoint));
963 COMP_CRC32(crc, (char *) record + sizeof(pg_crc32),
964 SizeOfXLogRecord - sizeof(pg_crc32));
966 record->xl_crc = crc;
968 /* Write the first page */
969 XLogFilePath(path, ControlFile.checkPointCopy.ThisTimeLineID,
970 newXlogId, newXlogSeg);
974 fd = open(path, O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
978 fprintf(stderr, _("%s: could not open file \"%s\": %s\n"),
979 progname, path, strerror(errno));
984 if (write(fd, buffer, XLOG_BLCKSZ) != XLOG_BLCKSZ)
986 /* if write didn't set errno, assume problem is no disk space */
989 fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
990 progname, path, strerror(errno));
994 /* Fill the rest of the file with zeroes */
995 memset(buffer, 0, XLOG_BLCKSZ);
996 for (nbytes = XLOG_BLCKSZ; nbytes < XLogSegSize; nbytes += XLOG_BLCKSZ)
999 if (write(fd, buffer, XLOG_BLCKSZ) != XLOG_BLCKSZ)
1003 fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
1004 progname, path, strerror(errno));
1011 fprintf(stderr, _("%s: fsync error: %s\n"), progname, strerror(errno));
1022 printf(_("%s resets the PostgreSQL transaction log.\n\n"), progname);
1023 printf(_("Usage:\n %s [OPTION]... DATADIR\n\n"), progname);
1024 printf(_("Options:\n"));
1025 printf(_(" -e XIDEPOCH set next transaction ID epoch\n"));
1026 printf(_(" -f force update to be done\n"));
1027 printf(_(" -l TLI,FILE,SEG force minimum WAL starting location for new transaction log\n"));
1028 printf(_(" -m XID set next multitransaction ID\n"));
1029 printf(_(" -n no update, just show extracted control values (for testing)\n"));
1030 printf(_(" -o OID set next OID\n"));
1031 printf(_(" -O OFFSET set next multitransaction offset\n"));
1032 printf(_(" -x XID set next transaction ID\n"));
1033 printf(_(" --help show this help, then exit\n"));
1034 printf(_(" --version output version information, then exit\n"));
1035 printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));