Fix a few errors in comments. Patch by Fujii Masao, plus the one in
[PostgreSQL.git] / src / bin / pg_resetxlog / pg_resetxlog.c
blob3b66b90d103373e5886d55fca55e7067421233a8
1 /*-------------------------------------------------------------------------
3 * pg_resetxlog.c
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
10 * current format.
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
20 * step 2 ...
23 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
24 * Portions Copyright (c) 1994, Regents of the University of California
26 * $PostgreSQL$
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.
36 #define FRONTEND 1
38 #include "postgres.h"
40 #include <dirent.h>
41 #include <fcntl.h>
42 #include <locale.h>
43 #include <sys/stat.h>
44 #include <sys/time.h>
45 #include <time.h>
46 #include <unistd.h>
47 #ifdef HAVE_GETOPT_H
48 #include <getopt.h>
49 #endif
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"
58 extern int optind;
59 extern char *optarg;
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);
79 int
80 main(int argc, char *argv[])
82 int c;
83 bool force = false;
84 bool noupdate = false;
85 uint32 set_xid_epoch = (uint32) -1;
86 TransactionId set_xid = 0;
87 Oid set_oid = 0;
88 MultiXactId set_mxid = 0;
89 MultiXactOffset set_mxoff = (MultiXactOffset) -1;
90 uint32 minXlogTli = 0,
91 minXlogId = 0,
92 minXlogSeg = 0;
93 char *endptr;
94 char *endptr2;
95 char *endptr3;
96 char *DataDir;
97 int fd;
98 char path[MAXPGPATH];
100 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_resetxlog"));
102 progname = get_progname(argv[0]);
104 if (argc > 1)
106 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
108 usage();
109 exit(0);
111 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
113 puts("pg_resetxlog (PostgreSQL) " PG_VERSION);
114 exit(0);
119 while ((c = getopt(argc, argv, "fl:m:no:O:x:e:")) != -1)
121 switch (c)
123 case 'f':
124 force = true;
125 break;
127 case 'n':
128 noupdate = true;
129 break;
131 case 'e':
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);
137 exit(1);
139 if (set_xid_epoch == -1)
141 fprintf(stderr, _("%s: transaction ID epoch (-e) must not be -1\n"), progname);
142 exit(1);
144 break;
146 case 'x':
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);
152 exit(1);
154 if (set_xid == 0)
156 fprintf(stderr, _("%s: transaction ID (-x) must not be 0\n"), progname);
157 exit(1);
159 break;
161 case 'o':
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);
167 exit(1);
169 if (set_oid == 0)
171 fprintf(stderr, _("%s: OID (-o) must not be 0\n"), progname);
172 exit(1);
174 break;
176 case 'm':
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);
182 exit(1);
184 if (set_mxid == 0)
186 fprintf(stderr, _("%s: multitransaction ID (-m) must not be 0\n"), progname);
187 exit(1);
189 break;
191 case 'O':
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);
197 exit(1);
199 if (set_mxoff == -1)
201 fprintf(stderr, _("%s: multitransaction offset (-O) must not be -1\n"), progname);
202 exit(1);
204 break;
206 case 'l':
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);
212 exit(1);
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);
219 exit(1);
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);
226 exit(1);
228 break;
230 default:
231 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
232 exit(1);
236 if (optind == argc)
238 fprintf(stderr, _("%s: no data directory specified\n"), progname);
239 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
240 exit(1);
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.
249 #ifndef WIN32
250 if (geteuid() == 0)
252 fprintf(stderr, _("%s: cannot be executed by \"root\"\n"),
253 progname);
254 fprintf(stderr, _("You must run %s as the PostgreSQL superuser.\n"),
255 progname);
256 exit(1);
258 #endif
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));
266 exit(1);
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)
277 if (errno != ENOENT)
279 fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"), progname, path, strerror(errno));
280 exit(1);
283 else
285 fprintf(stderr, _("%s: lock file \"%s\" exists\n"
286 "Is a server running? If not, delete the lock file and try again.\n"),
287 progname, path);
288 exit(1);
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
300 FindEndOfXLOG();
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;
309 if (set_xid != 0)
310 ControlFile.checkPointCopy.nextXid = set_xid;
312 if (set_oid != 0)
313 ControlFile.checkPointCopy.nextOid = set_oid;
315 if (set_mxid != 0)
316 ControlFile.checkPointCopy.nextMulti = set_mxid;
318 if (set_mxoff != -1)
319 ControlFile.checkPointCopy.nextMultiOffset = set_mxoff;
321 if (minXlogTli > ControlFile.checkPointCopy.ThisTimeLineID)
322 ControlFile.checkPointCopy.ThisTimeLineID = minXlogTli;
324 if (minXlogId > newXlogId ||
325 (minXlogId == newXlogId &&
326 minXlogSeg > newXlogSeg))
328 newXlogId = minXlogId;
329 newXlogSeg = minXlogSeg;
333 * If we had to guess anything, and -f was not given, just print the
334 * guessed values and exit. Also print if -n is given.
336 if ((guessed && !force) || noupdate)
338 PrintControlValues(guessed);
339 if (!noupdate)
341 printf(_("\nIf these values seem acceptable, use -f to force reset.\n"));
342 exit(1);
344 else
345 exit(0);
349 * Don't reset from a dirty pg_control without -f, either.
351 if (ControlFile.state != DB_SHUTDOWNED && !force)
353 printf(_("The database server was not shut down cleanly.\n"
354 "Resetting the transaction log might cause data to be lost.\n"
355 "If you want to proceed anyway, use -f to force reset.\n"));
356 exit(1);
360 * Else, do the dirty deed.
362 RewriteControlFile();
363 KillExistingXLOG();
364 KillExistingArchiveStatus();
365 WriteEmptyXLOG();
367 printf(_("Transaction log reset\n"));
368 return 0;
373 * Try to read the existing pg_control file.
375 * This routine is also responsible for updating old pg_control versions
376 * to the current format. (Currently we don't do anything of the sort.)
378 static bool
379 ReadControlFile(void)
381 int fd;
382 int len;
383 char *buffer;
384 pg_crc32 crc;
386 if ((fd = open(XLOG_CONTROL_FILE, O_RDONLY | PG_BINARY, 0)) < 0)
389 * If pg_control is not there at all, or we can't read it, the odds
390 * are we've been handed a bad DataDir path, so give up. User can do
391 * "touch pg_control" to force us to proceed.
393 fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
394 progname, XLOG_CONTROL_FILE, strerror(errno));
395 if (errno == ENOENT)
396 fprintf(stderr, _("If you are sure the data directory path is correct, execute\n"
397 " touch %s\n"
398 "and try again.\n"),
399 XLOG_CONTROL_FILE);
400 exit(1);
403 /* Use malloc to ensure we have a maxaligned buffer */
404 buffer = (char *) malloc(PG_CONTROL_SIZE);
406 len = read(fd, buffer, PG_CONTROL_SIZE);
407 if (len < 0)
409 fprintf(stderr, _("%s: could not read file \"%s\": %s\n"),
410 progname, XLOG_CONTROL_FILE, strerror(errno));
411 exit(1);
413 close(fd);
415 if (len >= sizeof(ControlFileData) &&
416 ((ControlFileData *) buffer)->pg_control_version == PG_CONTROL_VERSION)
418 /* Check the CRC. */
419 INIT_CRC32(crc);
420 COMP_CRC32(crc,
421 buffer,
422 offsetof(ControlFileData, crc));
423 FIN_CRC32(crc);
425 if (EQ_CRC32(crc, ((ControlFileData *) buffer)->crc))
427 /* Valid data... */
428 memcpy(&ControlFile, buffer, sizeof(ControlFile));
429 return true;
432 fprintf(stderr, _("%s: pg_control exists but has invalid CRC; proceed with caution\n"),
433 progname);
434 /* We will use the data anyway, but treat it as guessed. */
435 memcpy(&ControlFile, buffer, sizeof(ControlFile));
436 guessed = true;
437 return true;
440 /* Looks like it's a mess. */
441 fprintf(stderr, _("%s: pg_control exists but is broken or unknown version; ignoring it\n"),
442 progname);
443 return false;
448 * Guess at pg_control values when we can't read the old ones.
450 static void
451 GuessControlValues(void)
453 uint64 sysidentifier;
454 struct timeval tv;
457 * Set up a completely default set of pg_control values.
459 guessed = true;
460 memset(&ControlFile, 0, sizeof(ControlFile));
462 ControlFile.pg_control_version = PG_CONTROL_VERSION;
463 ControlFile.catalog_version_no = CATALOG_VERSION_NO;
466 * Create a new unique installation identifier, since we can no longer use
467 * any old XLOG records. See notes in xlog.c about the algorithm.
469 gettimeofday(&tv, NULL);
470 sysidentifier = ((uint64) tv.tv_sec) << 32;
471 sysidentifier |= (uint32) (tv.tv_sec | tv.tv_usec);
473 ControlFile.system_identifier = sysidentifier;
475 ControlFile.checkPointCopy.redo.xlogid = 0;
476 ControlFile.checkPointCopy.redo.xrecoff = SizeOfXLogLongPHD;
477 ControlFile.checkPointCopy.ThisTimeLineID = 1;
478 ControlFile.checkPointCopy.nextXidEpoch = 0;
479 ControlFile.checkPointCopy.nextXid = (TransactionId) 514; /* XXX */
480 ControlFile.checkPointCopy.nextOid = FirstBootstrapObjectId;
481 ControlFile.checkPointCopy.nextMulti = FirstMultiXactId;
482 ControlFile.checkPointCopy.nextMultiOffset = 0;
483 ControlFile.checkPointCopy.time = (pg_time_t) time(NULL);
485 ControlFile.state = DB_SHUTDOWNED;
486 ControlFile.time = (pg_time_t) time(NULL);
487 ControlFile.checkPoint = ControlFile.checkPointCopy.redo;
489 ControlFile.maxAlign = MAXIMUM_ALIGNOF;
490 ControlFile.floatFormat = FLOATFORMAT_VALUE;
491 ControlFile.blcksz = BLCKSZ;
492 ControlFile.relseg_size = RELSEG_SIZE;
493 ControlFile.xlog_blcksz = XLOG_BLCKSZ;
494 ControlFile.xlog_seg_size = XLOG_SEG_SIZE;
495 ControlFile.nameDataLen = NAMEDATALEN;
496 ControlFile.indexMaxKeys = INDEX_MAX_KEYS;
497 ControlFile.toast_max_chunk_size = TOAST_MAX_CHUNK_SIZE;
498 #ifdef HAVE_INT64_TIMESTAMP
499 ControlFile.enableIntTimes = true;
500 #else
501 ControlFile.enableIntTimes = false;
502 #endif
503 ControlFile.float4ByVal = FLOAT4PASSBYVAL;
504 ControlFile.float8ByVal = FLOAT8PASSBYVAL;
507 * XXX eventually, should try to grovel through old XLOG to develop more
508 * accurate values for TimeLineID, nextXID, etc.
514 * Print the guessed pg_control values when we had to guess.
516 * NB: this display should be just those fields that will not be
517 * reset by RewriteControlFile().
519 static void
520 PrintControlValues(bool guessed)
522 char sysident_str[32];
524 if (guessed)
525 printf(_("Guessed pg_control values:\n\n"));
526 else
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 printf(_("First log file ID after reset: %u\n"),
537 newXlogId);
538 printf(_("First log file segment after reset: %u\n"),
539 newXlogSeg);
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"),
545 sysident_str);
546 printf(_("Latest checkpoint's TimeLineID: %u\n"),
547 ControlFile.checkPointCopy.ThisTimeLineID);
548 printf(_("Latest checkpoint's NextXID: %u/%u\n"),
549 ControlFile.checkPointCopy.nextXidEpoch,
550 ControlFile.checkPointCopy.nextXid);
551 printf(_("Latest checkpoint's NextOID: %u\n"),
552 ControlFile.checkPointCopy.nextOid);
553 printf(_("Latest checkpoint's NextMultiXactId: %u\n"),
554 ControlFile.checkPointCopy.nextMulti);
555 printf(_("Latest checkpoint's NextMultiOffset: %u\n"),
556 ControlFile.checkPointCopy.nextMultiOffset);
557 printf(_("Maximum data alignment: %u\n"),
558 ControlFile.maxAlign);
559 /* we don't print floatFormat since can't say much useful about it */
560 printf(_("Database block size: %u\n"),
561 ControlFile.blcksz);
562 printf(_("Blocks per segment of large relation: %u\n"),
563 ControlFile.relseg_size);
564 printf(_("WAL block size: %u\n"),
565 ControlFile.xlog_blcksz);
566 printf(_("Bytes per WAL segment: %u\n"),
567 ControlFile.xlog_seg_size);
568 printf(_("Maximum length of identifiers: %u\n"),
569 ControlFile.nameDataLen);
570 printf(_("Maximum columns in an index: %u\n"),
571 ControlFile.indexMaxKeys);
572 printf(_("Maximum size of a TOAST chunk: %u\n"),
573 ControlFile.toast_max_chunk_size);
574 printf(_("Date/time type storage: %s\n"),
575 (ControlFile.enableIntTimes ? _("64-bit integers") : _("floating-point numbers")));
576 printf(_("Float4 argument passing: %s\n"),
577 (ControlFile.float4ByVal ? _("by value") : _("by reference")));
578 printf(_("Float8 argument passing: %s\n"),
579 (ControlFile.float8ByVal ? _("by value") : _("by reference")));
584 * Write out the new pg_control file.
586 static void
587 RewriteControlFile(void)
589 int fd;
590 char buffer[PG_CONTROL_SIZE]; /* need not be aligned */
593 * Adjust fields as needed to force an empty XLOG starting at
594 * newXlogId/newXlogSeg.
596 ControlFile.checkPointCopy.redo.xlogid = newXlogId;
597 ControlFile.checkPointCopy.redo.xrecoff =
598 newXlogSeg * XLogSegSize + SizeOfXLogLongPHD;
599 ControlFile.checkPointCopy.time = (pg_time_t) time(NULL);
601 ControlFile.state = DB_SHUTDOWNED;
602 ControlFile.time = (pg_time_t) time(NULL);
603 ControlFile.checkPoint = ControlFile.checkPointCopy.redo;
604 ControlFile.prevCheckPoint.xlogid = 0;
605 ControlFile.prevCheckPoint.xrecoff = 0;
606 ControlFile.minRecoveryPoint.xlogid = 0;
607 ControlFile.minRecoveryPoint.xrecoff = 0;
609 /* Now we can force the recorded xlog seg size to the right thing. */
610 ControlFile.xlog_seg_size = XLogSegSize;
612 /* Contents are protected with a CRC */
613 INIT_CRC32(ControlFile.crc);
614 COMP_CRC32(ControlFile.crc,
615 (char *) &ControlFile,
616 offsetof(ControlFileData, crc));
617 FIN_CRC32(ControlFile.crc);
620 * We write out PG_CONTROL_SIZE bytes into pg_control, zero-padding the
621 * excess over sizeof(ControlFileData). This reduces the odds of
622 * premature-EOF errors when reading pg_control. We'll still fail when we
623 * check the contents of the file, but hopefully with a more specific
624 * error than "couldn't read pg_control".
626 if (sizeof(ControlFileData) > PG_CONTROL_SIZE)
628 fprintf(stderr,
629 _("%s: internal error -- sizeof(ControlFileData) is too large ... fix PG_CONTROL_SIZE\n"),
630 progname);
631 exit(1);
634 memset(buffer, 0, PG_CONTROL_SIZE);
635 memcpy(buffer, &ControlFile, sizeof(ControlFileData));
637 unlink(XLOG_CONTROL_FILE);
639 fd = open(XLOG_CONTROL_FILE,
640 O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
641 S_IRUSR | S_IWUSR);
642 if (fd < 0)
644 fprintf(stderr, _("%s: could not create pg_control file: %s\n"),
645 progname, strerror(errno));
646 exit(1);
649 errno = 0;
650 if (write(fd, buffer, PG_CONTROL_SIZE) != PG_CONTROL_SIZE)
652 /* if write didn't set errno, assume problem is no disk space */
653 if (errno == 0)
654 errno = ENOSPC;
655 fprintf(stderr, _("%s: could not write pg_control file: %s\n"),
656 progname, strerror(errno));
657 exit(1);
660 if (fsync(fd) != 0)
662 fprintf(stderr, _("%s: fsync error: %s\n"), progname, strerror(errno));
663 exit(1);
666 close(fd);
671 * Scan existing XLOG files and determine the highest existing WAL address
673 * On entry, ControlFile.checkPointCopy.redo and ControlFile.xlog_seg_size
674 * are assumed valid (note that we allow the old xlog seg size to differ
675 * from what we're using). On exit, newXlogId and newXlogSeg are set to
676 * suitable values for the beginning of replacement WAL (in our seg size).
678 static void
679 FindEndOfXLOG(void)
681 DIR *xldir;
682 struct dirent *xlde;
685 * Initialize the max() computation using the last checkpoint address from
686 * old pg_control. Note that for the moment we are working with segment
687 * numbering according to the old xlog seg size.
689 newXlogId = ControlFile.checkPointCopy.redo.xlogid;
690 newXlogSeg = ControlFile.checkPointCopy.redo.xrecoff / ControlFile.xlog_seg_size;
693 * Scan the pg_xlog directory to find existing WAL segment files. We
694 * assume any present have been used; in most scenarios this should be
695 * conservative, because of xlog.c's attempts to pre-create files.
697 xldir = opendir(XLOGDIR);
698 if (xldir == NULL)
700 fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
701 progname, XLOGDIR, strerror(errno));
702 exit(1);
705 errno = 0;
706 while ((xlde = readdir(xldir)) != NULL)
708 if (strlen(xlde->d_name) == 24 &&
709 strspn(xlde->d_name, "0123456789ABCDEF") == 24)
711 unsigned int tli,
712 log,
713 seg;
715 sscanf(xlde->d_name, "%08X%08X%08X", &tli, &log, &seg);
718 * Note: we take the max of all files found, regardless of their
719 * timelines. Another possibility would be to ignore files of
720 * timelines other than the target TLI, but this seems safer.
721 * Better too large a result than too small...
723 if (log > newXlogId ||
724 (log == newXlogId && seg > newXlogSeg))
726 newXlogId = log;
727 newXlogSeg = seg;
730 errno = 0;
732 #ifdef WIN32
735 * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
736 * released version
738 if (GetLastError() == ERROR_NO_MORE_FILES)
739 errno = 0;
740 #endif
742 if (errno)
744 fprintf(stderr, _("%s: could not read from directory \"%s\": %s\n"),
745 progname, XLOGDIR, strerror(errno));
746 exit(1);
748 closedir(xldir);
751 * Finally, convert to new xlog seg size, and advance by one to ensure we
752 * are in virgin territory.
754 newXlogSeg *= ControlFile.xlog_seg_size;
755 newXlogSeg = (newXlogSeg + XLogSegSize - 1) / XLogSegSize;
757 /* be sure we wrap around correctly at end of a logfile */
758 NextLogSeg(newXlogId, newXlogSeg);
763 * Remove existing XLOG files
765 static void
766 KillExistingXLOG(void)
768 DIR *xldir;
769 struct dirent *xlde;
770 char path[MAXPGPATH];
772 xldir = opendir(XLOGDIR);
773 if (xldir == NULL)
775 fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
776 progname, XLOGDIR, strerror(errno));
777 exit(1);
780 errno = 0;
781 while ((xlde = readdir(xldir)) != NULL)
783 if (strlen(xlde->d_name) == 24 &&
784 strspn(xlde->d_name, "0123456789ABCDEF") == 24)
786 snprintf(path, MAXPGPATH, "%s/%s", XLOGDIR, xlde->d_name);
787 if (unlink(path) < 0)
789 fprintf(stderr, _("%s: could not delete file \"%s\": %s\n"),
790 progname, path, strerror(errno));
791 exit(1);
794 errno = 0;
796 #ifdef WIN32
799 * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
800 * released version
802 if (GetLastError() == ERROR_NO_MORE_FILES)
803 errno = 0;
804 #endif
806 if (errno)
808 fprintf(stderr, _("%s: could not read from directory \"%s\": %s\n"),
809 progname, XLOGDIR, strerror(errno));
810 exit(1);
812 closedir(xldir);
817 * Remove existing archive status files
819 static void
820 KillExistingArchiveStatus(void)
822 DIR *xldir;
823 struct dirent *xlde;
824 char path[MAXPGPATH];
826 #define ARCHSTATDIR XLOGDIR "/archive_status"
828 xldir = opendir(ARCHSTATDIR);
829 if (xldir == NULL)
831 fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
832 progname, ARCHSTATDIR, strerror(errno));
833 exit(1);
836 errno = 0;
837 while ((xlde = readdir(xldir)) != NULL)
839 if (strspn(xlde->d_name, "0123456789ABCDEF") == 24 &&
840 (strcmp(xlde->d_name + 24, ".ready") == 0 ||
841 strcmp(xlde->d_name + 24, ".done") == 0))
843 snprintf(path, MAXPGPATH, "%s/%s", ARCHSTATDIR, xlde->d_name);
844 if (unlink(path) < 0)
846 fprintf(stderr, _("%s: could not delete file \"%s\": %s\n"),
847 progname, path, strerror(errno));
848 exit(1);
851 errno = 0;
853 #ifdef WIN32
856 * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
857 * released version
859 if (GetLastError() == ERROR_NO_MORE_FILES)
860 errno = 0;
861 #endif
863 if (errno)
865 fprintf(stderr, _("%s: could not read from directory \"%s\": %s\n"),
866 progname, ARCHSTATDIR, strerror(errno));
867 exit(1);
869 closedir(xldir);
874 * Write an empty XLOG file, containing only the checkpoint record
875 * already set up in ControlFile.
877 static void
878 WriteEmptyXLOG(void)
880 char *buffer;
881 XLogPageHeader page;
882 XLogLongPageHeader longpage;
883 XLogRecord *record;
884 pg_crc32 crc;
885 char path[MAXPGPATH];
886 int fd;
887 int nbytes;
889 /* Use malloc() to ensure buffer is MAXALIGNED */
890 buffer = (char *) malloc(XLOG_BLCKSZ);
891 page = (XLogPageHeader) buffer;
892 memset(buffer, 0, XLOG_BLCKSZ);
894 /* Set up the XLOG page header */
895 page->xlp_magic = XLOG_PAGE_MAGIC;
896 page->xlp_info = XLP_LONG_HEADER;
897 page->xlp_tli = ControlFile.checkPointCopy.ThisTimeLineID;
898 page->xlp_pageaddr.xlogid =
899 ControlFile.checkPointCopy.redo.xlogid;
900 page->xlp_pageaddr.xrecoff =
901 ControlFile.checkPointCopy.redo.xrecoff - SizeOfXLogLongPHD;
902 longpage = (XLogLongPageHeader) page;
903 longpage->xlp_sysid = ControlFile.system_identifier;
904 longpage->xlp_seg_size = XLogSegSize;
905 longpage->xlp_xlog_blcksz = XLOG_BLCKSZ;
907 /* Insert the initial checkpoint record */
908 record = (XLogRecord *) ((char *) page + SizeOfXLogLongPHD);
909 record->xl_prev.xlogid = 0;
910 record->xl_prev.xrecoff = 0;
911 record->xl_xid = InvalidTransactionId;
912 record->xl_tot_len = SizeOfXLogRecord + sizeof(CheckPoint);
913 record->xl_len = sizeof(CheckPoint);
914 record->xl_info = XLOG_CHECKPOINT_SHUTDOWN;
915 record->xl_rmid = RM_XLOG_ID;
916 memcpy(XLogRecGetData(record), &ControlFile.checkPointCopy,
917 sizeof(CheckPoint));
919 INIT_CRC32(crc);
920 COMP_CRC32(crc, &ControlFile.checkPointCopy, sizeof(CheckPoint));
921 COMP_CRC32(crc, (char *) record + sizeof(pg_crc32),
922 SizeOfXLogRecord - sizeof(pg_crc32));
923 FIN_CRC32(crc);
924 record->xl_crc = crc;
926 /* Write the first page */
927 XLogFilePath(path, ControlFile.checkPointCopy.ThisTimeLineID,
928 newXlogId, newXlogSeg);
930 unlink(path);
932 fd = open(path, O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
933 S_IRUSR | S_IWUSR);
934 if (fd < 0)
936 fprintf(stderr, _("%s: could not open file \"%s\": %s\n"),
937 progname, path, strerror(errno));
938 exit(1);
941 errno = 0;
942 if (write(fd, buffer, XLOG_BLCKSZ) != XLOG_BLCKSZ)
944 /* if write didn't set errno, assume problem is no disk space */
945 if (errno == 0)
946 errno = ENOSPC;
947 fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
948 progname, path, strerror(errno));
949 exit(1);
952 /* Fill the rest of the file with zeroes */
953 memset(buffer, 0, XLOG_BLCKSZ);
954 for (nbytes = XLOG_BLCKSZ; nbytes < XLogSegSize; nbytes += XLOG_BLCKSZ)
956 errno = 0;
957 if (write(fd, buffer, XLOG_BLCKSZ) != XLOG_BLCKSZ)
959 if (errno == 0)
960 errno = ENOSPC;
961 fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
962 progname, path, strerror(errno));
963 exit(1);
967 if (fsync(fd) != 0)
969 fprintf(stderr, _("%s: fsync error: %s\n"), progname, strerror(errno));
970 exit(1);
973 close(fd);
977 static void
978 usage(void)
980 printf(_("%s resets the PostgreSQL transaction log.\n\n"), progname);
981 printf(_("Usage:\n %s [OPTION]... DATADIR\n\n"), progname);
982 printf(_("Options:\n"));
983 printf(_(" -e XIDEPOCH set next transaction ID epoch\n"));
984 printf(_(" -f force update to be done\n"));
985 printf(_(" -l TLI,FILE,SEG force minimum WAL starting location for new transaction log\n"));
986 printf(_(" -m XID set next multitransaction ID\n"));
987 printf(_(" -n no update, just show extracted control values (for testing)\n"));
988 printf(_(" -o OID set next OID\n"));
989 printf(_(" -O OFFSET set next multitransaction offset\n"));
990 printf(_(" -x XID set next transaction ID\n"));
991 printf(_(" --help show this help, then exit\n"));
992 printf(_(" --version output version information, then exit\n"));
993 printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));