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-2012, 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"
62 static ControlFileData ControlFile; /* pg_control values */
63 static XLogSegNo newXlogSegNo; /* new XLOG segment # */
64 static bool guessed = false; /* T if we had to guess at any values */
65 static const char *progname;
67 static bool ReadControlFile(void);
68 static void GuessControlValues(void);
69 static void PrintControlValues(bool guessed);
70 static void RewriteControlFile(void);
71 static void FindEndOfXLOG(void);
72 static void KillExistingXLOG(void);
73 static void KillExistingArchiveStatus(void);
74 static void WriteEmptyXLOG(void);
75 static void usage(void);
79 main(int argc, char *argv[])
83 bool noupdate = false;
84 uint32 set_xid_epoch = (uint32) -1;
85 TransactionId set_xid = 0;
87 MultiXactId set_mxid = 0;
88 MultiXactOffset set_mxoff = (MultiXactOffset) -1;
89 uint32 minXlogTli = 0;
90 XLogSegNo minXlogSegNo = 0;
95 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_resetxlog"));
97 progname = get_progname(argv[0]);
101 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
106 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
108 puts("pg_resetxlog (PostgreSQL) " PG_VERSION);
114 while ((c = getopt(argc, argv, "fl:m:no:O:x:e:")) != -1)
127 set_xid_epoch = strtoul(optarg, &endptr, 0);
128 if (endptr == optarg || *endptr != '\0')
130 fprintf(stderr, _("%s: invalid argument for option -e\n"), progname);
131 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
134 if (set_xid_epoch == -1)
136 fprintf(stderr, _("%s: transaction ID epoch (-e) must not be -1\n"), progname);
142 set_xid = strtoul(optarg, &endptr, 0);
143 if (endptr == optarg || *endptr != '\0')
145 fprintf(stderr, _("%s: invalid argument for option -x\n"), progname);
146 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
151 fprintf(stderr, _("%s: transaction ID (-x) must not be 0\n"), progname);
157 set_oid = strtoul(optarg, &endptr, 0);
158 if (endptr == optarg || *endptr != '\0')
160 fprintf(stderr, _("%s: invalid argument for option -o\n"), progname);
161 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
166 fprintf(stderr, _("%s: OID (-o) must not be 0\n"), progname);
172 set_mxid = strtoul(optarg, &endptr, 0);
173 if (endptr == optarg || *endptr != '\0')
175 fprintf(stderr, _("%s: invalid argument for option -m\n"), progname);
176 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
181 fprintf(stderr, _("%s: multitransaction ID (-m) must not be 0\n"), progname);
187 set_mxoff = strtoul(optarg, &endptr, 0);
188 if (endptr == optarg || *endptr != '\0')
190 fprintf(stderr, _("%s: invalid argument for option -O\n"), progname);
191 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
196 fprintf(stderr, _("%s: multitransaction offset (-O) must not be -1\n"), progname);
202 if (strspn(optarg, "01234567890ABCDEFabcdef") != 24)
204 fprintf(stderr, _("%s: invalid argument for option -l\n"), progname);
205 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
208 XLogFromFileName(optarg, &minXlogTli, &minXlogSegNo);
212 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
219 fprintf(stderr, _("%s: no data directory specified\n"), progname);
220 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
225 * Don't allow pg_resetxlog to be run as root, to avoid overwriting the
226 * ownership of files in the data directory. We need only check for root
227 * -- any other user won't have sufficient permissions to modify files in
228 * the data directory.
233 fprintf(stderr, _("%s: cannot be executed by \"root\"\n"),
235 fprintf(stderr, _("You must run %s as the PostgreSQL superuser.\n"),
241 DataDir = argv[optind];
243 if (chdir(DataDir) < 0)
245 fprintf(stderr, _("%s: could not change directory to \"%s\": %s\n"),
246 progname, DataDir, strerror(errno));
251 * Check for a postmaster lock file --- if there is one, refuse to
252 * proceed, on grounds we might be interfering with a live installation.
254 if ((fd = open("postmaster.pid", O_RDONLY, 0)) < 0)
258 fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
259 progname, "postmaster.pid", strerror(errno));
265 fprintf(stderr, _("%s: lock file \"%s\" exists\n"
266 "Is a server running? If not, delete the lock file and try again.\n"),
267 progname, "postmaster.pid");
272 * Attempt to read the existing pg_control file
274 if (!ReadControlFile())
275 GuessControlValues();
278 * Also look at existing segment files to set up newXlogSegNo
283 * Adjust fields if required by switches. (Do this now so that printout,
284 * if any, includes these values.)
286 if (set_xid_epoch != -1)
287 ControlFile.checkPointCopy.nextXidEpoch = set_xid_epoch;
291 ControlFile.checkPointCopy.nextXid = set_xid;
294 * For the moment, just set oldestXid to a value that will force
295 * immediate autovacuum-for-wraparound. It's not clear whether adding
296 * user control of this is useful, so let's just do something that's
297 * reasonably safe. The magic constant here corresponds to the
298 * maximum allowed value of autovacuum_freeze_max_age.
300 ControlFile.checkPointCopy.oldestXid = set_xid - 2000000000;
301 if (ControlFile.checkPointCopy.oldestXid < FirstNormalTransactionId)
302 ControlFile.checkPointCopy.oldestXid += FirstNormalTransactionId;
303 ControlFile.checkPointCopy.oldestXidDB = InvalidOid;
307 ControlFile.checkPointCopy.nextOid = set_oid;
310 ControlFile.checkPointCopy.nextMulti = set_mxid;
313 ControlFile.checkPointCopy.nextMultiOffset = set_mxoff;
315 if (minXlogTli > ControlFile.checkPointCopy.ThisTimeLineID)
316 ControlFile.checkPointCopy.ThisTimeLineID = minXlogTli;
318 if (minXlogSegNo > newXlogSegNo)
319 newXlogSegNo = minXlogSegNo;
322 * If we had to guess anything, and -f was not given, just print the
323 * guessed values and exit. Also print if -n is given.
325 if ((guessed && !force) || noupdate)
327 PrintControlValues(guessed);
330 printf(_("\nIf these values seem acceptable, use -f to force reset.\n"));
338 * Don't reset from a dirty pg_control without -f, either.
340 if (ControlFile.state != DB_SHUTDOWNED && !force)
342 printf(_("The database server was not shut down cleanly.\n"
343 "Resetting the transaction log might cause data to be lost.\n"
344 "If you want to proceed anyway, use -f to force reset.\n"));
349 * Else, do the dirty deed.
351 RewriteControlFile();
353 KillExistingArchiveStatus();
356 printf(_("Transaction log reset\n"));
362 * Try to read the existing pg_control file.
364 * This routine is also responsible for updating old pg_control versions
365 * to the current format. (Currently we don't do anything of the sort.)
368 ReadControlFile(void)
375 if ((fd = open(XLOG_CONTROL_FILE, O_RDONLY | PG_BINARY, 0)) < 0)
378 * If pg_control is not there at all, or we can't read it, the odds
379 * are we've been handed a bad DataDir path, so give up. User can do
380 * "touch pg_control" to force us to proceed.
382 fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
383 progname, XLOG_CONTROL_FILE, strerror(errno));
385 fprintf(stderr, _("If you are sure the data directory path is correct, execute\n"
392 /* Use malloc to ensure we have a maxaligned buffer */
393 buffer = (char *) malloc(PG_CONTROL_SIZE);
395 len = read(fd, buffer, PG_CONTROL_SIZE);
398 fprintf(stderr, _("%s: could not read file \"%s\": %s\n"),
399 progname, XLOG_CONTROL_FILE, strerror(errno));
404 if (len >= sizeof(ControlFileData) &&
405 ((ControlFileData *) buffer)->pg_control_version == PG_CONTROL_VERSION)
411 offsetof(ControlFileData, crc));
414 if (EQ_CRC32(crc, ((ControlFileData *) buffer)->crc))
417 memcpy(&ControlFile, buffer, sizeof(ControlFile));
421 fprintf(stderr, _("%s: pg_control exists but has invalid CRC; proceed with caution\n"),
423 /* We will use the data anyway, but treat it as guessed. */
424 memcpy(&ControlFile, buffer, sizeof(ControlFile));
429 /* Looks like it's a mess. */
430 fprintf(stderr, _("%s: pg_control exists but is broken or unknown version; ignoring it\n"),
437 * Guess at pg_control values when we can't read the old ones.
440 GuessControlValues(void)
442 uint64 sysidentifier;
446 * Set up a completely default set of pg_control values.
449 memset(&ControlFile, 0, sizeof(ControlFile));
451 ControlFile.pg_control_version = PG_CONTROL_VERSION;
452 ControlFile.catalog_version_no = CATALOG_VERSION_NO;
455 * Create a new unique installation identifier, since we can no longer use
456 * any old XLOG records. See notes in xlog.c about the algorithm.
458 gettimeofday(&tv, NULL);
459 sysidentifier = ((uint64) tv.tv_sec) << 32;
460 sysidentifier |= (uint32) (tv.tv_sec | tv.tv_usec);
462 ControlFile.system_identifier = sysidentifier;
464 ControlFile.checkPointCopy.redo = SizeOfXLogLongPHD;
465 ControlFile.checkPointCopy.ThisTimeLineID = 1;
466 ControlFile.checkPointCopy.fullPageWrites = false;
467 ControlFile.checkPointCopy.nextXidEpoch = 0;
468 ControlFile.checkPointCopy.nextXid = FirstNormalTransactionId;
469 ControlFile.checkPointCopy.nextOid = FirstBootstrapObjectId;
470 ControlFile.checkPointCopy.nextMulti = FirstMultiXactId;
471 ControlFile.checkPointCopy.nextMultiOffset = 0;
472 ControlFile.checkPointCopy.oldestXid = FirstNormalTransactionId;
473 ControlFile.checkPointCopy.oldestXidDB = InvalidOid;
474 ControlFile.checkPointCopy.time = (pg_time_t) time(NULL);
475 ControlFile.checkPointCopy.oldestActiveXid = InvalidTransactionId;
477 ControlFile.state = DB_SHUTDOWNED;
478 ControlFile.time = (pg_time_t) time(NULL);
479 ControlFile.checkPoint = ControlFile.checkPointCopy.redo;
481 /* minRecoveryPoint, backupStartPoint and backupEndPoint can be left zero */
483 ControlFile.wal_level = WAL_LEVEL_MINIMAL;
484 ControlFile.MaxConnections = 100;
485 ControlFile.max_prepared_xacts = 0;
486 ControlFile.max_locks_per_xact = 64;
488 ControlFile.maxAlign = MAXIMUM_ALIGNOF;
489 ControlFile.floatFormat = FLOATFORMAT_VALUE;
490 ControlFile.blcksz = BLCKSZ;
491 ControlFile.relseg_size = RELSEG_SIZE;
492 ControlFile.xlog_blcksz = XLOG_BLCKSZ;
493 ControlFile.xlog_seg_size = XLOG_SEG_SIZE;
494 ControlFile.nameDataLen = NAMEDATALEN;
495 ControlFile.indexMaxKeys = INDEX_MAX_KEYS;
496 ControlFile.toast_max_chunk_size = TOAST_MAX_CHUNK_SIZE;
497 #ifdef HAVE_INT64_TIMESTAMP
498 ControlFile.enableIntTimes = true;
500 ControlFile.enableIntTimes = false;
502 ControlFile.float4ByVal = FLOAT4PASSBYVAL;
503 ControlFile.float8ByVal = FLOAT8PASSBYVAL;
506 * XXX eventually, should try to grovel through old XLOG to develop more
507 * accurate values for TimeLineID, nextXID, etc.
513 * Print the guessed pg_control values when we had to guess.
515 * NB: this display should be just those fields that will not be
516 * reset by RewriteControlFile().
519 PrintControlValues(bool guessed)
521 char sysident_str[32];
522 char fname[MAXFNAMELEN];
525 printf(_("Guessed pg_control values:\n\n"));
527 printf(_("pg_control values:\n\n"));
530 * Format system_identifier separately to keep platform-dependent format
531 * code out of the translatable message string.
533 snprintf(sysident_str, sizeof(sysident_str), UINT64_FORMAT,
534 ControlFile.system_identifier);
536 XLogFileName(fname, ControlFile.checkPointCopy.ThisTimeLineID, newXlogSegNo);
538 printf(_("First log segment after reset: %s\n"),
540 printf(_("pg_control version number: %u\n"),
541 ControlFile.pg_control_version);
542 printf(_("Catalog version number: %u\n"),
543 ControlFile.catalog_version_no);
544 printf(_("Database system identifier: %s\n"),
546 printf(_("Latest checkpoint's TimeLineID: %u\n"),
547 ControlFile.checkPointCopy.ThisTimeLineID);
548 printf(_("Latest checkpoint's full_page_writes: %s\n"),
549 ControlFile.checkPointCopy.fullPageWrites ? _("on") : _("off"));
550 printf(_("Latest checkpoint's NextXID: %u/%u\n"),
551 ControlFile.checkPointCopy.nextXidEpoch,
552 ControlFile.checkPointCopy.nextXid);
553 printf(_("Latest checkpoint's NextOID: %u\n"),
554 ControlFile.checkPointCopy.nextOid);
555 printf(_("Latest checkpoint's NextMultiXactId: %u\n"),
556 ControlFile.checkPointCopy.nextMulti);
557 printf(_("Latest checkpoint's NextMultiOffset: %u\n"),
558 ControlFile.checkPointCopy.nextMultiOffset);
559 printf(_("Latest checkpoint's oldestXID: %u\n"),
560 ControlFile.checkPointCopy.oldestXid);
561 printf(_("Latest checkpoint's oldestXID's DB: %u\n"),
562 ControlFile.checkPointCopy.oldestXidDB);
563 printf(_("Latest checkpoint's oldestActiveXID: %u\n"),
564 ControlFile.checkPointCopy.oldestActiveXid);
565 printf(_("Maximum data alignment: %u\n"),
566 ControlFile.maxAlign);
567 /* we don't print floatFormat since can't say much useful about it */
568 printf(_("Database block size: %u\n"),
570 printf(_("Blocks per segment of large relation: %u\n"),
571 ControlFile.relseg_size);
572 printf(_("WAL block size: %u\n"),
573 ControlFile.xlog_blcksz);
574 printf(_("Bytes per WAL segment: %u\n"),
575 ControlFile.xlog_seg_size);
576 printf(_("Maximum length of identifiers: %u\n"),
577 ControlFile.nameDataLen);
578 printf(_("Maximum columns in an index: %u\n"),
579 ControlFile.indexMaxKeys);
580 printf(_("Maximum size of a TOAST chunk: %u\n"),
581 ControlFile.toast_max_chunk_size);
582 printf(_("Date/time type storage: %s\n"),
583 (ControlFile.enableIntTimes ? _("64-bit integers") : _("floating-point numbers")));
584 printf(_("Float4 argument passing: %s\n"),
585 (ControlFile.float4ByVal ? _("by value") : _("by reference")));
586 printf(_("Float8 argument passing: %s\n"),
587 (ControlFile.float8ByVal ? _("by value") : _("by reference")));
592 * Write out the new pg_control file.
595 RewriteControlFile(void)
598 char buffer[PG_CONTROL_SIZE]; /* need not be aligned */
601 * Adjust fields as needed to force an empty XLOG starting at
604 XLogSegNoOffsetToRecPtr(newXlogSegNo, SizeOfXLogLongPHD,
605 ControlFile.checkPointCopy.redo);
606 ControlFile.checkPointCopy.time = (pg_time_t) time(NULL);
608 ControlFile.state = DB_SHUTDOWNED;
609 ControlFile.time = (pg_time_t) time(NULL);
610 ControlFile.checkPoint = ControlFile.checkPointCopy.redo;
611 ControlFile.prevCheckPoint = 0;
612 ControlFile.minRecoveryPoint = 0;
613 ControlFile.minRecoveryPointTLI = 0;
614 ControlFile.backupStartPoint = 0;
615 ControlFile.backupEndPoint = 0;
616 ControlFile.backupEndRequired = false;
619 * Force the defaults for max_* settings. The values don't really matter
620 * as long as wal_level='minimal'; the postmaster will reset these fields
623 ControlFile.wal_level = WAL_LEVEL_MINIMAL;
624 ControlFile.MaxConnections = 100;
625 ControlFile.max_prepared_xacts = 0;
626 ControlFile.max_locks_per_xact = 64;
628 /* Now we can force the recorded xlog seg size to the right thing. */
629 ControlFile.xlog_seg_size = XLogSegSize;
631 /* Contents are protected with a CRC */
632 INIT_CRC32(ControlFile.crc);
633 COMP_CRC32(ControlFile.crc,
634 (char *) &ControlFile,
635 offsetof(ControlFileData, crc));
636 FIN_CRC32(ControlFile.crc);
639 * We write out PG_CONTROL_SIZE bytes into pg_control, zero-padding the
640 * excess over sizeof(ControlFileData). This reduces the odds of
641 * premature-EOF errors when reading pg_control. We'll still fail when we
642 * check the contents of the file, but hopefully with a more specific
643 * error than "couldn't read pg_control".
645 if (sizeof(ControlFileData) > PG_CONTROL_SIZE)
648 _("%s: internal error -- sizeof(ControlFileData) is too large ... fix PG_CONTROL_SIZE\n"),
653 memset(buffer, 0, PG_CONTROL_SIZE);
654 memcpy(buffer, &ControlFile, sizeof(ControlFileData));
656 unlink(XLOG_CONTROL_FILE);
658 fd = open(XLOG_CONTROL_FILE,
659 O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
663 fprintf(stderr, _("%s: could not create pg_control file: %s\n"),
664 progname, strerror(errno));
669 if (write(fd, buffer, PG_CONTROL_SIZE) != PG_CONTROL_SIZE)
671 /* if write didn't set errno, assume problem is no disk space */
674 fprintf(stderr, _("%s: could not write pg_control file: %s\n"),
675 progname, strerror(errno));
681 fprintf(stderr, _("%s: fsync error: %s\n"), progname, strerror(errno));
690 * Scan existing XLOG files and determine the highest existing WAL address
692 * On entry, ControlFile.checkPointCopy.redo and ControlFile.xlog_seg_size
693 * are assumed valid (note that we allow the old xlog seg size to differ
694 * from what we're using). On exit, newXlogId and newXlogSeg are set to
695 * suitable values for the beginning of replacement WAL (in our seg size).
702 uint64 segs_per_xlogid;
706 * Initialize the max() computation using the last checkpoint address from
707 * old pg_control. Note that for the moment we are working with segment
708 * numbering according to the old xlog seg size.
710 segs_per_xlogid = (UINT64CONST(0x0000000100000000) / ControlFile.xlog_seg_size);
711 newXlogSegNo = ControlFile.checkPointCopy.redo / ControlFile.xlog_seg_size;
714 * Scan the pg_xlog directory to find existing WAL segment files. We
715 * assume any present have been used; in most scenarios this should be
716 * conservative, because of xlog.c's attempts to pre-create files.
718 xldir = opendir(XLOGDIR);
721 fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
722 progname, XLOGDIR, strerror(errno));
727 while ((xlde = readdir(xldir)) != NULL)
729 if (strlen(xlde->d_name) == 24 &&
730 strspn(xlde->d_name, "0123456789ABCDEF") == 24)
737 sscanf(xlde->d_name, "%08X%08X%08X", &tli, &log, &seg);
738 segno = ((uint64) log) * segs_per_xlogid + seg;
741 * Note: we take the max of all files found, regardless of their
742 * timelines. Another possibility would be to ignore files of
743 * timelines other than the target TLI, but this seems safer.
744 * Better too large a result than too small...
746 if (segno > newXlogSegNo)
747 newXlogSegNo = segno;
754 * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
757 if (GetLastError() == ERROR_NO_MORE_FILES)
763 fprintf(stderr, _("%s: could not read from directory \"%s\": %s\n"),
764 progname, XLOGDIR, strerror(errno));
770 * Finally, convert to new xlog seg size, and advance by one to ensure we
771 * are in virgin territory.
773 xlogbytepos = newXlogSegNo * ControlFile.xlog_seg_size;
774 newXlogSegNo = (xlogbytepos + XLogSegSize - 1) / XLogSegSize;
780 * Remove existing XLOG files
783 KillExistingXLOG(void)
787 char path[MAXPGPATH];
789 xldir = opendir(XLOGDIR);
792 fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
793 progname, XLOGDIR, strerror(errno));
798 while ((xlde = readdir(xldir)) != NULL)
800 if (strlen(xlde->d_name) == 24 &&
801 strspn(xlde->d_name, "0123456789ABCDEF") == 24)
803 snprintf(path, MAXPGPATH, "%s/%s", XLOGDIR, xlde->d_name);
804 if (unlink(path) < 0)
806 fprintf(stderr, _("%s: could not delete file \"%s\": %s\n"),
807 progname, path, strerror(errno));
816 * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
819 if (GetLastError() == ERROR_NO_MORE_FILES)
825 fprintf(stderr, _("%s: could not read from directory \"%s\": %s\n"),
826 progname, XLOGDIR, strerror(errno));
834 * Remove existing archive status files
837 KillExistingArchiveStatus(void)
841 char path[MAXPGPATH];
843 #define ARCHSTATDIR XLOGDIR "/archive_status"
845 xldir = opendir(ARCHSTATDIR);
848 fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
849 progname, ARCHSTATDIR, strerror(errno));
854 while ((xlde = readdir(xldir)) != NULL)
856 if (strspn(xlde->d_name, "0123456789ABCDEF") == 24 &&
857 (strcmp(xlde->d_name + 24, ".ready") == 0 ||
858 strcmp(xlde->d_name + 24, ".done") == 0))
860 snprintf(path, MAXPGPATH, "%s/%s", ARCHSTATDIR, xlde->d_name);
861 if (unlink(path) < 0)
863 fprintf(stderr, _("%s: could not delete file \"%s\": %s\n"),
864 progname, path, strerror(errno));
873 * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
876 if (GetLastError() == ERROR_NO_MORE_FILES)
882 fprintf(stderr, _("%s: could not read from directory \"%s\": %s\n"),
883 progname, ARCHSTATDIR, strerror(errno));
891 * Write an empty XLOG file, containing only the checkpoint record
892 * already set up in ControlFile.
899 XLogLongPageHeader longpage;
902 char path[MAXPGPATH];
906 /* Use malloc() to ensure buffer is MAXALIGNED */
907 buffer = (char *) malloc(XLOG_BLCKSZ);
908 page = (XLogPageHeader) buffer;
909 memset(buffer, 0, XLOG_BLCKSZ);
911 /* Set up the XLOG page header */
912 page->xlp_magic = XLOG_PAGE_MAGIC;
913 page->xlp_info = XLP_LONG_HEADER;
914 page->xlp_tli = ControlFile.checkPointCopy.ThisTimeLineID;
915 page->xlp_pageaddr = ControlFile.checkPointCopy.redo - SizeOfXLogLongPHD;
916 longpage = (XLogLongPageHeader) page;
917 longpage->xlp_sysid = ControlFile.system_identifier;
918 longpage->xlp_seg_size = XLogSegSize;
919 longpage->xlp_xlog_blcksz = XLOG_BLCKSZ;
921 /* Insert the initial checkpoint record */
922 record = (XLogRecord *) ((char *) page + SizeOfXLogLongPHD);
924 record->xl_xid = InvalidTransactionId;
925 record->xl_tot_len = SizeOfXLogRecord + sizeof(CheckPoint);
926 record->xl_len = sizeof(CheckPoint);
927 record->xl_info = XLOG_CHECKPOINT_SHUTDOWN;
928 record->xl_rmid = RM_XLOG_ID;
929 memcpy(XLogRecGetData(record), &ControlFile.checkPointCopy,
933 COMP_CRC32(crc, &ControlFile.checkPointCopy, sizeof(CheckPoint));
934 COMP_CRC32(crc, (char *) record, offsetof(XLogRecord, xl_crc));
936 record->xl_crc = crc;
938 /* Write the first page */
939 XLogFilePath(path, ControlFile.checkPointCopy.ThisTimeLineID, newXlogSegNo);
943 fd = open(path, O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
947 fprintf(stderr, _("%s: could not open file \"%s\": %s\n"),
948 progname, path, strerror(errno));
953 if (write(fd, buffer, XLOG_BLCKSZ) != XLOG_BLCKSZ)
955 /* if write didn't set errno, assume problem is no disk space */
958 fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
959 progname, path, strerror(errno));
963 /* Fill the rest of the file with zeroes */
964 memset(buffer, 0, XLOG_BLCKSZ);
965 for (nbytes = XLOG_BLCKSZ; nbytes < XLogSegSize; nbytes += XLOG_BLCKSZ)
968 if (write(fd, buffer, XLOG_BLCKSZ) != XLOG_BLCKSZ)
972 fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
973 progname, path, strerror(errno));
980 fprintf(stderr, _("%s: fsync error: %s\n"), progname, strerror(errno));
991 printf(_("%s resets the PostgreSQL transaction log.\n\n"), progname);
992 printf(_("Usage:\n %s [OPTION]... DATADIR\n\n"), progname);
993 printf(_("Options:\n"));
994 printf(_(" -e XIDEPOCH set next transaction ID epoch\n"));
995 printf(_(" -f force update to be done\n"));
996 printf(_(" -l xlogfile force minimum WAL starting location for new transaction log\n"));
997 printf(_(" -m XID set next multitransaction ID\n"));
998 printf(_(" -n no update, just show extracted control values (for testing)\n"));
999 printf(_(" -o OID set next OID\n"));
1000 printf(_(" -O OFFSET set next multitransaction offset\n"));
1001 printf(_(" -V, --version output version information, then exit\n"));
1002 printf(_(" -x XID set next transaction ID\n"));
1003 printf(_(" -?, --help show this help, then exit\n"));
1004 printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));