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-2003, 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.18 2004/05/12 13:38:44 momjian Exp $
28 *-------------------------------------------------------------------------
41 #include "access/xlog.h"
42 #include "catalog/catversion.h"
43 #include "catalog/pg_control.h"
48 #define _(x) gettext((x))
51 /******************** stuff copied from xlog.c ********************/
53 /* Increment an xlogid/segment pair */
54 #define NextLogSeg(logId, logSeg) \
56 if ((logSeg) >= XLogSegsPerFile-1) \
65 #define XLogFileName(path, log, seg) \
66 snprintf(path, MAXPGPATH, "%s/%08X%08X", \
69 /******************** end of stuff copied from xlog.c ********************/
71 static char XLogDir[MAXPGPATH];
72 static char ControlFilePath[MAXPGPATH];
74 static ControlFileData ControlFile; /* pg_control values */
75 static uint32 newXlogId,
76 newXlogSeg; /* ID/Segment of new XLOG segment */
77 static bool guessed = false; /* T if we had to guess at any values */
78 static const char *progname;
80 static bool ReadControlFile(void);
81 static void GuessControlValues(void);
82 static void PrintControlValues(bool guessed);
83 static void RewriteControlFile(void);
84 static void KillExistingXLOG(void);
85 static void WriteEmptyXLOG(void);
86 static void usage(void);
90 main(int argc, char *argv[])
94 bool noupdate = false;
95 TransactionId set_xid = 0;
103 char path[MAXPGPATH];
105 setlocale(LC_ALL, "");
107 bindtextdomain("pg_resetxlog", LOCALEDIR);
108 textdomain("pg_resetxlog");
111 progname = get_progname(argv[0]);
115 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
120 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
122 puts("pg_resetxlog (PostgreSQL) " PG_VERSION);
128 while ((c = getopt(argc, argv, "fl:no:x:")) != -1)
141 set_xid = strtoul(optarg, &endptr, 0);
142 if (endptr == optarg || *endptr != '\0')
144 fprintf(stderr, _("%s: invalid argument for option -x\n"), progname);
145 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
150 fprintf(stderr, _("%s: transaction ID (-x) must not be 0\n"), progname);
156 set_oid = strtoul(optarg, &endptr, 0);
157 if (endptr == optarg || *endptr != '\0')
159 fprintf(stderr, _("%s: invalid argument for option -o\n"), progname);
160 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
165 fprintf(stderr, _("%s: OID (-o) must not be 0\n"), progname);
171 minXlogId = strtoul(optarg, &endptr, 0);
172 if (endptr == optarg || *endptr != ',')
174 fprintf(stderr, _("%s: invalid argument for option -l\n"), progname);
175 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
178 minXlogSeg = strtoul(endptr + 1, &endptr2, 0);
179 if (endptr2 == endptr + 1 || *endptr2 != '\0')
181 fprintf(stderr, _("%s: invalid argument for option -l\n"), progname);
182 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
188 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
195 fprintf(stderr, _("%s: no data directory specified\n"), progname);
196 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
200 DataDir = argv[optind];
201 snprintf(XLogDir, MAXPGPATH, "%s/pg_xlog", DataDir);
202 snprintf(ControlFilePath, MAXPGPATH, "%s/global/pg_control", DataDir);
205 * Check for a postmaster lock file --- if there is one, refuse to
206 * proceed, on grounds we might be interfering with a live
209 snprintf(path, MAXPGPATH, "%s/postmaster.pid", DataDir);
211 if ((fd = open(path, O_RDONLY)) < 0)
215 fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"), progname, path, strerror(errno));
221 fprintf(stderr, _("%s: lock file \"%s\" exists\n"
222 "Is a server running? If not, delete the lock file and try again.\n"),
228 * Attempt to read the existing pg_control file
230 if (!ReadControlFile())
231 GuessControlValues();
234 * Adjust fields if required by switches. (Do this now so that
235 * printout, if any, includes these values.)
238 ControlFile.checkPointCopy.nextXid = set_xid;
241 ControlFile.checkPointCopy.nextOid = set_oid;
243 if (minXlogId > ControlFile.logId ||
244 (minXlogId == ControlFile.logId &&
245 minXlogSeg > ControlFile.logSeg))
247 ControlFile.logId = minXlogId;
248 ControlFile.logSeg = minXlogSeg;
252 * If we had to guess anything, and -f was not given, just print the
253 * guessed values and exit. Also print if -n is given.
255 if ((guessed && !force) || noupdate)
257 PrintControlValues(guessed);
260 printf(_("\nIf these values seem acceptable, use -f to force reset.\n"));
268 * Don't reset from a dirty pg_control without -f, either.
270 if (ControlFile.state != DB_SHUTDOWNED && !force)
272 printf(_("The database server was not shut down cleanly.\n"
273 "Resetting the transaction log may cause data to be lost.\n"
274 "If you want to proceed anyway, use -f to force reset.\n"));
279 * Else, do the dirty deed.
281 RewriteControlFile();
285 printf(_("Transaction log reset\n"));
291 * Try to read the existing pg_control file.
293 * This routine is also responsible for updating old pg_control versions
294 * to the current format. (Currently we don't do anything of the sort.)
297 ReadControlFile(void)
304 if ((fd = open(ControlFilePath, O_RDONLY)) < 0)
307 * If pg_control is not there at all, or we can't read it, the
308 * odds are we've been handed a bad DataDir path, so give up. User
309 * can do "touch pg_control" to force us to proceed.
311 fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
312 progname, ControlFilePath, strerror(errno));
314 fprintf(stderr, _("If you are sure the data directory path is correct, execute\n"
321 /* Use malloc to ensure we have a maxaligned buffer */
322 buffer = (char *) malloc(BLCKSZ);
324 len = read(fd, buffer, BLCKSZ);
327 fprintf(stderr, _("%s: could not read file \"%s\": %s\n"),
328 progname, ControlFilePath, strerror(errno));
333 if (len >= sizeof(ControlFileData) &&
334 ((ControlFileData *) buffer)->pg_control_version == PG_CONTROL_VERSION)
339 buffer + sizeof(crc64),
340 sizeof(ControlFileData) - sizeof(crc64));
343 if (EQ_CRC64(crc, ((ControlFileData *) buffer)->crc))
346 memcpy(&ControlFile, buffer, sizeof(ControlFile));
350 fprintf(stderr, _("%s: pg_control exists but has invalid CRC; proceed with caution\n"),
352 /* We will use the data anyway, but treat it as guessed. */
353 memcpy(&ControlFile, buffer, sizeof(ControlFile));
358 /* Looks like it's a mess. */
359 fprintf(stderr, _("%s: pg_control exists but is broken or unknown version; ignoring it\n"),
366 * Guess at pg_control values when we can't read the old ones.
369 GuessControlValues(void)
371 uint64 sysidentifier;
376 * Set up a completely default set of pg_control values.
379 memset(&ControlFile, 0, sizeof(ControlFile));
381 ControlFile.pg_control_version = PG_CONTROL_VERSION;
382 ControlFile.catalog_version_no = CATALOG_VERSION_NO;
385 * Create a new unique installation identifier, since we can no longer
386 * use any old XLOG records. See notes in xlog.c about the algorithm.
388 gettimeofday(&tv, NULL);
389 sysidentifier = ((uint64) tv.tv_sec) << 32;
390 sysidentifier |= (uint32) (tv.tv_sec | tv.tv_usec);
392 ControlFile.system_identifier = sysidentifier;
394 ControlFile.checkPointCopy.redo.xlogid = 0;
395 ControlFile.checkPointCopy.redo.xrecoff = SizeOfXLogPHD + SizeOfXLogRecord + SizeOfXLogFHD;
396 ControlFile.checkPointCopy.undo = ControlFile.checkPointCopy.redo;
397 ControlFile.checkPointCopy.ThisStartUpID = 0;
398 ControlFile.checkPointCopy.nextXid = (TransactionId) 514; /* XXX */
399 ControlFile.checkPointCopy.nextOid = BootstrapObjectIdData;
400 ControlFile.checkPointCopy.time = time(NULL);
402 ControlFile.state = DB_SHUTDOWNED;
403 ControlFile.time = time(NULL);
404 ControlFile.logId = 0;
405 ControlFile.logSeg = 1;
406 ControlFile.checkPoint = ControlFile.checkPointCopy.redo;
408 ControlFile.blcksz = BLCKSZ;
409 ControlFile.relseg_size = RELSEG_SIZE;
410 ControlFile.xlog_seg_size = XLOG_SEG_SIZE;
411 ControlFile.nameDataLen = NAMEDATALEN;
412 ControlFile.funcMaxArgs = FUNC_MAX_ARGS;
413 #ifdef HAVE_INT64_TIMESTAMP
414 ControlFile.enableIntTimes = TRUE;
416 ControlFile.enableIntTimes = FALSE;
418 ControlFile.localeBuflen = LOCALE_NAME_BUFLEN;
420 localeptr = setlocale(LC_COLLATE, "");
423 fprintf(stderr, _("%s: invalid LC_COLLATE setting\n"), progname);
426 StrNCpy(ControlFile.lc_collate, localeptr, LOCALE_NAME_BUFLEN);
427 localeptr = setlocale(LC_CTYPE, "");
430 fprintf(stderr, _("%s: invalid LC_CTYPE setting\n"), progname);
433 StrNCpy(ControlFile.lc_ctype, localeptr, LOCALE_NAME_BUFLEN);
436 * XXX eventually, should try to grovel through old XLOG to develop
437 * more accurate values for startupid, nextXID, and nextOID.
443 * Print the guessed pg_control values when we had to guess.
445 * NB: this display should be just those fields that will not be
446 * reset by RewriteControlFile().
449 PrintControlValues(bool guessed)
451 char sysident_str[32];
454 printf(_("Guessed pg_control values:\n\n"));
456 printf(_("pg_control values:\n\n"));
459 * Format system_identifier separately to keep platform-dependent format
460 * code out of the translatable message string.
462 snprintf(sysident_str, sizeof(sysident_str), UINT64_FORMAT,
463 ControlFile.system_identifier);
465 printf(_("pg_control version number: %u\n"), ControlFile.pg_control_version);
466 printf(_("Catalog version number: %u\n"), ControlFile.catalog_version_no);
467 printf(_("Database system identifier: %s\n"), sysident_str);
468 printf(_("Current log file ID: %u\n"), ControlFile.logId);
469 printf(_("Next log file segment: %u\n"), ControlFile.logSeg);
470 printf(_("Latest checkpoint's StartUpID: %u\n"), ControlFile.checkPointCopy.ThisStartUpID);
471 printf(_("Latest checkpoint's NextXID: %u\n"), ControlFile.checkPointCopy.nextXid);
472 printf(_("Latest checkpoint's NextOID: %u\n"), ControlFile.checkPointCopy.nextOid);
473 printf(_("Database block size: %u\n"), ControlFile.blcksz);
474 printf(_("Blocks per segment of large relation: %u\n"), ControlFile.relseg_size);
475 printf(_("Maximum length of identifiers: %u\n"), ControlFile.nameDataLen);
476 printf(_("Maximum number of function arguments: %u\n"), ControlFile.funcMaxArgs);
477 printf(_("Date/time type storage: %s\n"),
478 (ControlFile.enableIntTimes ? _("64-bit integers") : _("floating-point numbers")));
479 printf(_("Maximum length of locale name: %u\n"), ControlFile.localeBuflen);
480 printf(_("LC_COLLATE: %s\n"), ControlFile.lc_collate);
481 printf(_("LC_CTYPE: %s\n"), ControlFile.lc_ctype);
486 * Write out the new pg_control file.
489 RewriteControlFile(void)
492 char buffer[BLCKSZ]; /* need not be aligned */
495 * Adjust fields as needed to force an empty XLOG starting at the next
498 newXlogId = ControlFile.logId;
499 newXlogSeg = ControlFile.logSeg;
501 /* adjust in case we are changing segment size */
502 newXlogSeg *= ControlFile.xlog_seg_size;
503 newXlogSeg = (newXlogSeg + XLogSegSize-1) / XLogSegSize;
505 /* be sure we wrap around correctly at end of a logfile */
506 NextLogSeg(newXlogId, newXlogSeg);
508 /* Now we can force the recorded xlog seg size to the right thing. */
509 ControlFile.xlog_seg_size = XLogSegSize;
511 ControlFile.checkPointCopy.redo.xlogid = newXlogId;
512 ControlFile.checkPointCopy.redo.xrecoff =
513 newXlogSeg * XLogSegSize + SizeOfXLogPHD + SizeOfXLogRecord + SizeOfXLogFHD;
514 ControlFile.checkPointCopy.undo = ControlFile.checkPointCopy.redo;
515 ControlFile.checkPointCopy.time = time(NULL);
517 ControlFile.state = DB_SHUTDOWNED;
518 ControlFile.time = time(NULL);
519 ControlFile.logId = newXlogId;
520 ControlFile.logSeg = newXlogSeg + 1;
521 ControlFile.checkPoint = ControlFile.checkPointCopy.redo;
522 ControlFile.prevCheckPoint.xlogid = 0;
523 ControlFile.prevCheckPoint.xrecoff = 0;
525 /* Contents are protected with a CRC */
526 INIT_CRC64(ControlFile.crc);
527 COMP_CRC64(ControlFile.crc,
528 (char *) &ControlFile + sizeof(crc64),
529 sizeof(ControlFileData) - sizeof(crc64));
530 FIN_CRC64(ControlFile.crc);
533 * We write out BLCKSZ bytes into pg_control, zero-padding the excess
534 * over sizeof(ControlFileData). This reduces the odds of
535 * premature-EOF errors when reading pg_control. We'll still fail
536 * when we check the contents of the file, but hopefully with a more
537 * specific error than "couldn't read pg_control".
539 if (sizeof(ControlFileData) > BLCKSZ)
542 _("%s: internal error -- sizeof(ControlFileData) is too large ... fix xlog.c\n"),
547 memset(buffer, 0, BLCKSZ);
548 memcpy(buffer, &ControlFile, sizeof(ControlFileData));
550 unlink(ControlFilePath);
552 fd = open(ControlFilePath, O_RDWR | O_CREAT | O_EXCL | PG_BINARY, S_IRUSR | S_IWUSR);
555 fprintf(stderr, _("%s: could not create pg_control file: %s\n"),
556 progname, strerror(errno));
561 if (write(fd, buffer, BLCKSZ) != BLCKSZ)
563 /* if write didn't set errno, assume problem is no disk space */
566 fprintf(stderr, _("%s: could not write pg_control file: %s\n"),
567 progname, strerror(errno));
573 fprintf(stderr, _("%s: fsync error: %s\n"), progname, strerror(errno));
582 * Remove existing XLOG files
585 KillExistingXLOG(void)
589 char path[MAXPGPATH];
591 xldir = opendir(XLogDir);
594 fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
595 progname, XLogDir, strerror(errno));
600 while ((xlde = readdir(xldir)) != NULL)
602 if (strlen(xlde->d_name) == 16 &&
603 strspn(xlde->d_name, "0123456789ABCDEF") == 16)
605 snprintf(path, MAXPGPATH, "%s/%s", XLogDir, xlde->d_name);
606 if (unlink(path) < 0)
608 fprintf(stderr, _("%s: could not delete file \"%s\": %s\n"),
609 progname, path, strerror(errno));
616 /* This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but
617 not in released version */
618 if (GetLastError() == ERROR_NO_MORE_FILES)
624 fprintf(stderr, _("%s: could not read from directory \"%s\": %s\n"),
625 progname, XLogDir, strerror(errno));
633 * Write an empty XLOG file, containing only the checkpoint record
634 * already set up in ControlFile.
642 XLogFileHeaderData *fhdr;
644 char path[MAXPGPATH];
648 /* Use malloc() to ensure buffer is MAXALIGNED */
649 buffer = (char *) malloc(BLCKSZ);
650 page = (XLogPageHeader) buffer;
651 memset(buffer, 0, BLCKSZ);
653 /* Set up the XLOG page header */
654 page->xlp_magic = XLOG_PAGE_MAGIC;
656 page->xlp_sui = ControlFile.checkPointCopy.ThisStartUpID;
657 page->xlp_pageaddr.xlogid =
658 ControlFile.checkPointCopy.redo.xlogid;
659 page->xlp_pageaddr.xrecoff =
660 ControlFile.checkPointCopy.redo.xrecoff -
661 (SizeOfXLogPHD + SizeOfXLogRecord + SizeOfXLogFHD);
663 /* Insert the file header record */
664 record = (XLogRecord *) ((char *) page + SizeOfXLogPHD);
665 record->xl_prev.xlogid = 0;
666 record->xl_prev.xrecoff = 0;
667 record->xl_xact_prev.xlogid = 0;
668 record->xl_xact_prev.xrecoff = 0;
669 record->xl_xid = InvalidTransactionId;
670 record->xl_len = SizeOfXLogFHD;
671 record->xl_info = XLOG_FILE_HEADER;
672 record->xl_rmid = RM_XLOG_ID;
673 fhdr = (XLogFileHeaderData *) XLogRecGetData(record);
674 fhdr->xlfhd_sysid = ControlFile.system_identifier;
675 fhdr->xlfhd_xlogid = page->xlp_pageaddr.xlogid;
676 fhdr->xlfhd_segno = page->xlp_pageaddr.xrecoff / XLogSegSize;
677 fhdr->xlfhd_seg_size = XLogSegSize;
680 COMP_CRC64(crc, fhdr, SizeOfXLogFHD);
681 COMP_CRC64(crc, (char *) record + sizeof(crc64),
682 SizeOfXLogRecord - sizeof(crc64));
684 record->xl_crc = crc;
686 /* Insert the initial checkpoint record */
687 record = (XLogRecord *) ((char *) page + SizeOfXLogPHD + SizeOfXLogRecord + SizeOfXLogFHD);
688 record->xl_prev.xlogid = page->xlp_pageaddr.xlogid;
689 record->xl_prev.xrecoff = page->xlp_pageaddr.xrecoff + SizeOfXLogPHD;
690 record->xl_xact_prev.xlogid = 0;
691 record->xl_xact_prev.xrecoff = 0;
692 record->xl_xid = InvalidTransactionId;
693 record->xl_len = sizeof(CheckPoint);
694 record->xl_info = XLOG_CHECKPOINT_SHUTDOWN;
695 record->xl_rmid = RM_XLOG_ID;
696 memcpy(XLogRecGetData(record), &ControlFile.checkPointCopy,
700 COMP_CRC64(crc, &ControlFile.checkPointCopy, sizeof(CheckPoint));
701 COMP_CRC64(crc, (char *) record + sizeof(crc64),
702 SizeOfXLogRecord - sizeof(crc64));
704 record->xl_crc = crc;
706 /* Write the first page */
707 XLogFileName(path, newXlogId, newXlogSeg);
711 fd = open(path, O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
715 fprintf(stderr, _("%s: could not open file \"%s\": %s\n"),
716 progname, path, strerror(errno));
721 if (write(fd, buffer, BLCKSZ) != BLCKSZ)
723 /* if write didn't set errno, assume problem is no disk space */
726 fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
727 progname, path, strerror(errno));
731 /* Fill the rest of the file with zeroes */
732 memset(buffer, 0, BLCKSZ);
733 for (nbytes = BLCKSZ; nbytes < XLogSegSize; nbytes += BLCKSZ)
736 if (write(fd, buffer, BLCKSZ) != BLCKSZ)
740 fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
741 progname, path, strerror(errno));
748 fprintf(stderr, _("%s: fsync error: %s\n"), progname, strerror(errno));
759 printf(_("%s resets the PostgreSQL transaction log.\n\n"), progname);
760 printf(_("Usage:\n %s [OPTION]... DATADIR\n\n"), progname);
761 printf(_("Options:\n"));
762 printf(_(" -f force update to be done\n"));
763 printf(_(" -l FILEID,SEG force minimum WAL starting location for new transaction log\n"));
764 printf(_(" -n no update, just show extracted control values (for testing)\n"));
765 printf(_(" -o OID set next OID\n"));
766 printf(_(" -x XID set next transaction ID\n"));
767 printf(_(" --help show this help, then exit\n"));
768 printf(_(" --version output version information, then exit\n"));
769 printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));