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-2005, 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.34 2005/06/08 15:50:27 tgl Exp $
28 *-------------------------------------------------------------------------
43 #include "access/multixact.h"
44 #include "access/xlog.h"
45 #include "access/xlog_internal.h"
46 #include "catalog/catversion.h"
47 #include "catalog/pg_control.h"
53 char XLogDir[MAXPGPATH]; /* not static, see xlog_internal.h */
54 static char ControlFilePath[MAXPGPATH];
56 static ControlFileData ControlFile; /* pg_control values */
57 static uint32 newXlogId,
58 newXlogSeg; /* ID/Segment of new XLOG segment */
59 static bool guessed = false; /* T if we had to guess at any values */
60 static const char *progname;
62 static bool ReadControlFile(void);
63 static void GuessControlValues(void);
64 static void PrintControlValues(bool guessed);
65 static void RewriteControlFile(void);
66 static void KillExistingXLOG(void);
67 static void WriteEmptyXLOG(void);
68 static void usage(void);
72 main(int argc, char *argv[])
76 bool noupdate = false;
77 TransactionId set_xid = 0;
79 MultiXactId set_mxid = 0;
80 MultiXactOffset set_mxoff = -1;
81 uint32 minXlogTli = 0,
91 set_pglocale_pgservice(argv[0], "pg_resetxlog");
93 progname = get_progname(argv[0]);
97 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
102 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
104 puts("pg_resetxlog (PostgreSQL) " PG_VERSION);
110 while ((c = getopt(argc, argv, "fl:m:no:O:x:")) != -1)
123 set_xid = strtoul(optarg, &endptr, 0);
124 if (endptr == optarg || *endptr != '\0')
126 fprintf(stderr, _("%s: invalid argument for option -x\n"), progname);
127 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
132 fprintf(stderr, _("%s: transaction ID (-x) must not be 0\n"), progname);
138 set_oid = strtoul(optarg, &endptr, 0);
139 if (endptr == optarg || *endptr != '\0')
141 fprintf(stderr, _("%s: invalid argument for option -o\n"), progname);
142 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
147 fprintf(stderr, _("%s: OID (-o) must not be 0\n"), progname);
153 set_mxid = strtoul(optarg, &endptr, 0);
154 if (endptr == optarg || *endptr != '\0')
156 fprintf(stderr, _("%s: invalid argument for option -m\n"), progname);
157 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
162 fprintf(stderr, _("%s: multi transaction ID (-m) must not be 0\n"), progname);
168 set_mxoff = strtoul(optarg, &endptr, 0);
169 if (endptr == optarg || *endptr != '\0')
171 fprintf(stderr, _("%s: invalid argument for option -O\n"), progname);
172 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
177 fprintf(stderr, _("%s: multi transaction offset (-O) must not be -1\n"), progname);
183 minXlogTli = strtoul(optarg, &endptr, 0);
184 if (endptr == optarg || *endptr != ',')
186 fprintf(stderr, _("%s: invalid argument for option -l\n"), progname);
187 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
190 minXlogId = strtoul(endptr + 1, &endptr2, 0);
191 if (endptr2 == endptr + 1 || *endptr2 != ',')
193 fprintf(stderr, _("%s: invalid argument for option -l\n"), progname);
194 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
197 minXlogSeg = strtoul(endptr2 + 1, &endptr3, 0);
198 if (endptr3 == endptr2 + 1 || *endptr3 != '\0')
200 fprintf(stderr, _("%s: invalid argument for option -l\n"), progname);
201 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
207 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
214 fprintf(stderr, _("%s: no data directory specified\n"), progname);
215 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
220 * Don't allow pg_resetxlog to be run as root, to avoid
221 * overwriting the ownership of files in the data directory. We
222 * need only check for root -- any other user won't have
223 * sufficient permissions to modify files in the data directory.
226 #ifndef __BEOS__ /* no root check on BeOS */
229 fprintf(stderr, _("%s: cannot be executed by \"root\"\n"),
231 fprintf(stderr, _("You must run %s as the PostgreSQL superuser.\n"),
238 DataDir = argv[optind];
239 snprintf(XLogDir, MAXPGPATH, "%s/pg_xlog", DataDir);
240 snprintf(ControlFilePath, MAXPGPATH, "%s/global/pg_control", DataDir);
243 * Check for a postmaster lock file --- if there is one, refuse to
244 * proceed, on grounds we might be interfering with a live
247 snprintf(path, MAXPGPATH, "%s/postmaster.pid", DataDir);
249 if ((fd = open(path, O_RDONLY)) < 0)
253 fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"), progname, path, strerror(errno));
259 fprintf(stderr, _("%s: lock file \"%s\" exists\n"
260 "Is a server running? If not, delete the lock file and try again.\n"),
266 * Attempt to read the existing pg_control file
268 if (!ReadControlFile())
269 GuessControlValues();
272 * Adjust fields if required by switches. (Do this now so that
273 * printout, if any, includes these values.)
276 ControlFile.checkPointCopy.nextXid = set_xid;
279 ControlFile.checkPointCopy.nextOid = set_oid;
282 ControlFile.checkPointCopy.nextMulti = set_mxid;
285 ControlFile.checkPointCopy.nextMultiOffset = set_mxoff;
287 if (minXlogTli > ControlFile.checkPointCopy.ThisTimeLineID)
288 ControlFile.checkPointCopy.ThisTimeLineID = minXlogTli;
290 if (minXlogId > ControlFile.logId ||
291 (minXlogId == ControlFile.logId &&
292 minXlogSeg > ControlFile.logSeg))
294 ControlFile.logId = minXlogId;
295 ControlFile.logSeg = minXlogSeg;
299 * If we had to guess anything, and -f was not given, just print the
300 * guessed values and exit. Also print if -n is given.
302 if ((guessed && !force) || noupdate)
304 PrintControlValues(guessed);
307 printf(_("\nIf these values seem acceptable, use -f to force reset.\n"));
315 * Don't reset from a dirty pg_control without -f, either.
317 if (ControlFile.state != DB_SHUTDOWNED && !force)
319 printf(_("The database server was not shut down cleanly.\n"
320 "Resetting the transaction log may cause data to be lost.\n"
321 "If you want to proceed anyway, use -f to force reset.\n"));
326 * Else, do the dirty deed.
328 RewriteControlFile();
332 printf(_("Transaction log reset\n"));
338 * Try to read the existing pg_control file.
340 * This routine is also responsible for updating old pg_control versions
341 * to the current format. (Currently we don't do anything of the sort.)
344 ReadControlFile(void)
351 if ((fd = open(ControlFilePath, O_RDONLY)) < 0)
354 * If pg_control is not there at all, or we can't read it, the
355 * odds are we've been handed a bad DataDir path, so give up. User
356 * can do "touch pg_control" to force us to proceed.
358 fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
359 progname, ControlFilePath, strerror(errno));
361 fprintf(stderr, _("If you are sure the data directory path is correct, execute\n"
368 /* Use malloc to ensure we have a maxaligned buffer */
369 buffer = (char *) malloc(BLCKSZ);
371 len = read(fd, buffer, BLCKSZ);
374 fprintf(stderr, _("%s: could not read file \"%s\": %s\n"),
375 progname, ControlFilePath, strerror(errno));
380 if (len >= sizeof(ControlFileData) &&
381 ((ControlFileData *) buffer)->pg_control_version == PG_CONTROL_VERSION)
387 offsetof(ControlFileData, crc));
390 if (EQ_CRC32(crc, ((ControlFileData *) buffer)->crc))
393 memcpy(&ControlFile, buffer, sizeof(ControlFile));
397 fprintf(stderr, _("%s: pg_control exists but has invalid CRC; proceed with caution\n"),
399 /* We will use the data anyway, but treat it as guessed. */
400 memcpy(&ControlFile, buffer, sizeof(ControlFile));
405 /* Looks like it's a mess. */
406 fprintf(stderr, _("%s: pg_control exists but is broken or unknown version; ignoring it\n"),
413 * Guess at pg_control values when we can't read the old ones.
416 GuessControlValues(void)
418 uint64 sysidentifier;
423 * Set up a completely default set of pg_control values.
426 memset(&ControlFile, 0, sizeof(ControlFile));
428 ControlFile.pg_control_version = PG_CONTROL_VERSION;
429 ControlFile.catalog_version_no = CATALOG_VERSION_NO;
432 * Create a new unique installation identifier, since we can no longer
433 * use any old XLOG records. See notes in xlog.c about the algorithm.
435 gettimeofday(&tv, NULL);
436 sysidentifier = ((uint64) tv.tv_sec) << 32;
437 sysidentifier |= (uint32) (tv.tv_sec | tv.tv_usec);
439 ControlFile.system_identifier = sysidentifier;
441 ControlFile.checkPointCopy.redo.xlogid = 0;
442 ControlFile.checkPointCopy.redo.xrecoff = SizeOfXLogLongPHD;
443 ControlFile.checkPointCopy.undo = ControlFile.checkPointCopy.redo;
444 ControlFile.checkPointCopy.ThisTimeLineID = 1;
445 ControlFile.checkPointCopy.nextXid = (TransactionId) 514; /* XXX */
446 ControlFile.checkPointCopy.nextOid = FirstBootstrapObjectId;
447 ControlFile.checkPointCopy.nextMulti = FirstMultiXactId;
448 ControlFile.checkPointCopy.nextMultiOffset = 0;
449 ControlFile.checkPointCopy.time = time(NULL);
451 ControlFile.state = DB_SHUTDOWNED;
452 ControlFile.time = time(NULL);
453 ControlFile.logId = 0;
454 ControlFile.logSeg = 1;
455 ControlFile.checkPoint = ControlFile.checkPointCopy.redo;
457 ControlFile.blcksz = BLCKSZ;
458 ControlFile.relseg_size = RELSEG_SIZE;
459 ControlFile.xlog_seg_size = XLOG_SEG_SIZE;
460 ControlFile.nameDataLen = NAMEDATALEN;
461 ControlFile.indexMaxKeys = INDEX_MAX_KEYS;
462 #ifdef HAVE_INT64_TIMESTAMP
463 ControlFile.enableIntTimes = TRUE;
465 ControlFile.enableIntTimes = FALSE;
467 ControlFile.localeBuflen = LOCALE_NAME_BUFLEN;
469 localeptr = setlocale(LC_COLLATE, "");
472 fprintf(stderr, _("%s: invalid LC_COLLATE setting\n"), progname);
475 StrNCpy(ControlFile.lc_collate, localeptr, LOCALE_NAME_BUFLEN);
476 localeptr = setlocale(LC_CTYPE, "");
479 fprintf(stderr, _("%s: invalid LC_CTYPE setting\n"), progname);
482 StrNCpy(ControlFile.lc_ctype, localeptr, LOCALE_NAME_BUFLEN);
485 * XXX eventually, should try to grovel through old XLOG to develop
486 * more accurate values for TimeLineID, nextXID, etc.
492 * Print the guessed pg_control values when we had to guess.
494 * NB: this display should be just those fields that will not be
495 * reset by RewriteControlFile().
498 PrintControlValues(bool guessed)
500 char sysident_str[32];
503 printf(_("Guessed pg_control values:\n\n"));
505 printf(_("pg_control values:\n\n"));
508 * Format system_identifier separately to keep platform-dependent
509 * format code out of the translatable message string.
511 snprintf(sysident_str, sizeof(sysident_str), UINT64_FORMAT,
512 ControlFile.system_identifier);
514 printf(_("pg_control version number: %u\n"), ControlFile.pg_control_version);
515 printf(_("Catalog version number: %u\n"), ControlFile.catalog_version_no);
516 printf(_("Database system identifier: %s\n"), sysident_str);
517 printf(_("Current log file ID: %u\n"), ControlFile.logId);
518 printf(_("Next log file segment: %u\n"), ControlFile.logSeg);
519 printf(_("Latest checkpoint's TimeLineID: %u\n"), ControlFile.checkPointCopy.ThisTimeLineID);
520 printf(_("Latest checkpoint's NextXID: %u\n"), ControlFile.checkPointCopy.nextXid);
521 printf(_("Latest checkpoint's NextOID: %u\n"), ControlFile.checkPointCopy.nextOid);
522 printf(_("Latest checkpoint's NextMultiXactId: %u\n"), ControlFile.checkPointCopy.nextMulti);
523 printf(_("Latest checkpoint's NextMultiOffset: %u\n"), ControlFile.checkPointCopy.nextMultiOffset);
524 printf(_("Database block size: %u\n"), ControlFile.blcksz);
525 printf(_("Blocks per segment of large relation: %u\n"), ControlFile.relseg_size);
526 printf(_("Maximum length of identifiers: %u\n"), ControlFile.nameDataLen);
527 printf(_("Maximum columns in an index: %u\n"), ControlFile.indexMaxKeys);
528 printf(_("Date/time type storage: %s\n"),
529 (ControlFile.enableIntTimes ? _("64-bit integers") : _("floating-point numbers")));
530 printf(_("Maximum length of locale name: %u\n"), ControlFile.localeBuflen);
531 printf(_("LC_COLLATE: %s\n"), ControlFile.lc_collate);
532 printf(_("LC_CTYPE: %s\n"), ControlFile.lc_ctype);
537 * Write out the new pg_control file.
540 RewriteControlFile(void)
543 char buffer[BLCKSZ]; /* need not be aligned */
546 * Adjust fields as needed to force an empty XLOG starting at the next
549 newXlogId = ControlFile.logId;
550 newXlogSeg = ControlFile.logSeg;
552 /* adjust in case we are changing segment size */
553 newXlogSeg *= ControlFile.xlog_seg_size;
554 newXlogSeg = (newXlogSeg + XLogSegSize - 1) / XLogSegSize;
556 /* be sure we wrap around correctly at end of a logfile */
557 NextLogSeg(newXlogId, newXlogSeg);
559 /* Now we can force the recorded xlog seg size to the right thing. */
560 ControlFile.xlog_seg_size = XLogSegSize;
562 ControlFile.checkPointCopy.redo.xlogid = newXlogId;
563 ControlFile.checkPointCopy.redo.xrecoff =
564 newXlogSeg * XLogSegSize + SizeOfXLogLongPHD;
565 ControlFile.checkPointCopy.undo = ControlFile.checkPointCopy.redo;
566 ControlFile.checkPointCopy.time = time(NULL);
568 ControlFile.state = DB_SHUTDOWNED;
569 ControlFile.time = time(NULL);
570 ControlFile.logId = newXlogId;
571 ControlFile.logSeg = newXlogSeg + 1;
572 ControlFile.checkPoint = ControlFile.checkPointCopy.redo;
573 ControlFile.prevCheckPoint.xlogid = 0;
574 ControlFile.prevCheckPoint.xrecoff = 0;
576 /* Contents are protected with a CRC */
577 INIT_CRC32(ControlFile.crc);
578 COMP_CRC32(ControlFile.crc,
579 (char *) &ControlFile,
580 offsetof(ControlFileData, crc));
581 FIN_CRC32(ControlFile.crc);
584 * We write out BLCKSZ bytes into pg_control, zero-padding the excess
585 * over sizeof(ControlFileData). This reduces the odds of
586 * premature-EOF errors when reading pg_control. We'll still fail
587 * when we check the contents of the file, but hopefully with a more
588 * specific error than "couldn't read pg_control".
590 if (sizeof(ControlFileData) > BLCKSZ)
593 _("%s: internal error -- sizeof(ControlFileData) is too large ... fix xlog.c\n"),
598 memset(buffer, 0, BLCKSZ);
599 memcpy(buffer, &ControlFile, sizeof(ControlFileData));
601 unlink(ControlFilePath);
603 fd = open(ControlFilePath, O_RDWR | O_CREAT | O_EXCL | PG_BINARY, S_IRUSR | S_IWUSR);
606 fprintf(stderr, _("%s: could not create pg_control file: %s\n"),
607 progname, strerror(errno));
612 if (write(fd, buffer, BLCKSZ) != BLCKSZ)
614 /* if write didn't set errno, assume problem is no disk space */
617 fprintf(stderr, _("%s: could not write pg_control file: %s\n"),
618 progname, strerror(errno));
624 fprintf(stderr, _("%s: fsync error: %s\n"), progname, strerror(errno));
633 * Remove existing XLOG files
636 KillExistingXLOG(void)
640 char path[MAXPGPATH];
642 xldir = opendir(XLogDir);
645 fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
646 progname, XLogDir, strerror(errno));
651 while ((xlde = readdir(xldir)) != NULL)
653 if (strlen(xlde->d_name) == 24 &&
654 strspn(xlde->d_name, "0123456789ABCDEF") == 24)
656 snprintf(path, MAXPGPATH, "%s/%s", XLogDir, xlde->d_name);
657 if (unlink(path) < 0)
659 fprintf(stderr, _("%s: could not delete file \"%s\": %s\n"),
660 progname, path, strerror(errno));
669 * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but
670 * not in released version
672 if (GetLastError() == ERROR_NO_MORE_FILES)
678 fprintf(stderr, _("%s: could not read from directory \"%s\": %s\n"),
679 progname, XLogDir, strerror(errno));
687 * Write an empty XLOG file, containing only the checkpoint record
688 * already set up in ControlFile.
695 XLogLongPageHeader longpage;
698 char path[MAXPGPATH];
702 /* Use malloc() to ensure buffer is MAXALIGNED */
703 buffer = (char *) malloc(BLCKSZ);
704 page = (XLogPageHeader) buffer;
705 memset(buffer, 0, BLCKSZ);
707 /* Set up the XLOG page header */
708 page->xlp_magic = XLOG_PAGE_MAGIC;
709 page->xlp_info = XLP_LONG_HEADER;
710 page->xlp_tli = ControlFile.checkPointCopy.ThisTimeLineID;
711 page->xlp_pageaddr.xlogid =
712 ControlFile.checkPointCopy.redo.xlogid;
713 page->xlp_pageaddr.xrecoff =
714 ControlFile.checkPointCopy.redo.xrecoff - SizeOfXLogLongPHD;
715 longpage = (XLogLongPageHeader) page;
716 longpage->xlp_sysid = ControlFile.system_identifier;
717 longpage->xlp_seg_size = XLogSegSize;
719 /* Insert the initial checkpoint record */
720 record = (XLogRecord *) ((char *) page + SizeOfXLogLongPHD);
721 record->xl_prev.xlogid = 0;
722 record->xl_prev.xrecoff = 0;
723 record->xl_xid = InvalidTransactionId;
724 record->xl_tot_len = SizeOfXLogRecord + sizeof(CheckPoint);
725 record->xl_len = sizeof(CheckPoint);
726 record->xl_info = XLOG_CHECKPOINT_SHUTDOWN;
727 record->xl_rmid = RM_XLOG_ID;
728 memcpy(XLogRecGetData(record), &ControlFile.checkPointCopy,
732 COMP_CRC32(crc, &ControlFile.checkPointCopy, sizeof(CheckPoint));
733 COMP_CRC32(crc, (char *) record + sizeof(pg_crc32),
734 SizeOfXLogRecord - sizeof(pg_crc32));
736 record->xl_crc = crc;
738 /* Write the first page */
739 XLogFilePath(path, ControlFile.checkPointCopy.ThisTimeLineID,
740 newXlogId, newXlogSeg);
744 fd = open(path, O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
748 fprintf(stderr, _("%s: could not open file \"%s\": %s\n"),
749 progname, path, strerror(errno));
754 if (write(fd, buffer, BLCKSZ) != BLCKSZ)
756 /* if write didn't set errno, assume problem is no disk space */
759 fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
760 progname, path, strerror(errno));
764 /* Fill the rest of the file with zeroes */
765 memset(buffer, 0, BLCKSZ);
766 for (nbytes = BLCKSZ; nbytes < XLogSegSize; nbytes += BLCKSZ)
769 if (write(fd, buffer, BLCKSZ) != BLCKSZ)
773 fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
774 progname, path, strerror(errno));
781 fprintf(stderr, _("%s: fsync error: %s\n"), progname, strerror(errno));
792 printf(_("%s resets the PostgreSQL transaction log.\n\n"), progname);
793 printf(_("Usage:\n %s [OPTION]... DATADIR\n\n"), progname);
794 printf(_("Options:\n"));
795 printf(_(" -f force update to be done\n"));
796 printf(_(" -l TLI,FILE,SEG force minimum WAL starting location for new transaction log\n"));
797 printf(_(" -n no update, just show extracted control values (for testing)\n"));
798 printf(_(" -o OID set next OID\n"));
799 printf(_(" -x XID set next transaction ID\n"));
800 printf(_(" -m multiXID set next multi transaction ID\n"));
801 printf(_(" -O multiOffset set next multi transaction offset\n"));
802 printf(_(" --help show this help, then exit\n"));
803 printf(_(" --version output version information, then exit\n"));
804 printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));