1 /*-------------------------------------------------------------------------
2 *
3 * pg_resetwal.c
4 * A utility to "zero out" the xlog when it's corrupt beyond recovery.
5 * Can also rebuild pg_control if needed.
6 *
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 ...
21 *
22 *
23 * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
24 * Portions Copyright (c) 1994, Regents of the University of California
25 *
26 * src/bin/pg_resetwal/pg_resetwal.c
27 *
28 *-------------------------------------------------------------------------
29 */
30
31 /*
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.
35 */
36 #define FRONTEND 1
37
38 #include "postgres.h"
39
40 #include <dirent.h>
41 #include <fcntl.h>
42 #include <sys/stat.h>
43 #include <sys/time.h>
44 #include <time.h>
45 #include <unistd.h>
46
47 #include "access/transam.h"
48 #include "access/tuptoaster.h"
49 #include "access/multixact.h"
50 #include "access/xlog.h"
51 #include "access/xlog_internal.h"
52 #include "catalog/catversion.h"
53 #include "catalog/pg_control.h"
54 #include "common/fe_memutils.h"
55 #include "common/file_perm.h"
56 #include "common/restricted_token.h"
57 #include "storage/large_object.h"
58 #include "pg_getopt.h"
59 #include "getopt_long.h"
60
61
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;
66 static uint32 set_xid_epoch = (uint32) -1;
67 static TransactionId set_oldest_xid = 0;
68 static TransactionId set_xid = 0;
69 static TransactionId set_oldest_commit_ts_xid = 0;
70 static TransactionId set_newest_commit_ts_xid = 0;
71 static Oid set_oid = 0;
72 static MultiXactId set_mxid = 0;
73 static MultiXactOffset set_mxoff = (MultiXactOffset) -1;
74 static uint32 minXlogTli = 0;
75 static XLogSegNo minXlogSegNo = 0;
76 static int WalSegSz;
77 static int set_wal_segsize;
78
79 static void CheckDataVersion(void);
80 static bool ReadControlFile(void);
81 static void GuessControlValues(void);
82 static void PrintControlValues(bool guessed);
83 static void PrintNewControlValues(void);
84 static void RewriteControlFile(void);
85 static void FindEndOfXLOG(void);
86 static void KillExistingXLOG(void);
87 static void KillExistingArchiveStatus(void);
88 static void WriteEmptyXLOG(void);
89 static void usage(void);
90
91
92 int
main(int argc,char * argv[])93 main(int argc, char *argv[])
94 {
95 static struct option long_options[] = {
96 {"commit-timestamp-ids", required_argument, NULL, 'c'},
97 {"pgdata", required_argument, NULL, 'D'},
98 {"epoch", required_argument, NULL, 'e'},
99 {"force", no_argument, NULL, 'f'},
100 {"next-wal-file", required_argument, NULL, 'l'},
101 {"multixact-ids", required_argument, NULL, 'm'},
102 {"dry-run", no_argument, NULL, 'n'},
103 {"next-oid", required_argument, NULL, 'o'},
104 {"multixact-offset", required_argument, NULL, 'O'},
105 {"oldest-transaction-id", required_argument, NULL, 'u'},
106 {"next-transaction-id", required_argument, NULL, 'x'},
107 {"wal-segsize", required_argument, NULL, 1},
108 {NULL, 0, NULL, 0}
109 };
110
111 int c;
112 bool force = false;
113 bool noupdate = false;
114 MultiXactId set_oldestmxid = 0;
115 char *endptr;
116 char *endptr2;
117 char *DataDir = NULL;
118 char *log_fname = NULL;
119 int fd;
120
121 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_resetwal"));
122
123 progname = get_progname(argv[0]);
124
125 if (argc > 1)
126 {
127 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
128 {
129 usage();
130 exit(0);
131 }
132 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
133 {
134 puts("pg_resetwal (PostgreSQL) " PG_VERSION);
135 exit(0);
136 }
137 }
138
139
140 while ((c = getopt_long(argc, argv, "c:D:e:fl:m:no:O:u:x:", long_options, NULL)) != -1)
141 {
142 switch (c)
143 {
144 case 'D':
145 DataDir = optarg;
146 break;
147
148 case 'f':
149 force = true;
150 break;
151
152 case 'n':
153 noupdate = true;
154 break;
155
156 case 'e':
157 set_xid_epoch = strtoul(optarg, &endptr, 0);
158 if (endptr == optarg || *endptr != '\0')
159 {
160 /*------
161 translator: the second %s is a command line argument (-e, etc) */
162 fprintf(stderr, _("%s: invalid argument for option %s\n"), progname, "-e");
163 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
164 exit(1);
165 }
166 if (set_xid_epoch == -1)
167 {
168 fprintf(stderr, _("%s: transaction ID epoch (-e) must not be -1\n"), progname);
169 exit(1);
170 }
171 break;
172
173 case 'u':
174 set_oldest_xid = strtoul(optarg, &endptr, 0);
175 if (endptr == optarg || *endptr != '\0')
176 {
177 fprintf(stderr, _("invalid argument for option %s"), "-u");
178 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
179 exit(1);
180 }
181 if (!TransactionIdIsNormal(set_oldest_xid))
182 {
183 fprintf(stderr, _("oldest transaction ID (-u) must be greater than or equal to %u"), FirstNormalTransactionId);
184 exit(1);
185 }
186 break;
187
188 case 'x':
189 set_xid = strtoul(optarg, &endptr, 0);
190 if (endptr == optarg || *endptr != '\0')
191 {
192 fprintf(stderr, _("%s: invalid argument for option %s\n"), progname, "-x");
193 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
194 exit(1);
195 }
196 if (set_xid == 0)
197 {
198 fprintf(stderr, _("%s: transaction ID (-x) must not be 0\n"), progname);
199 exit(1);
200 }
201 break;
202
203 case 'c':
204 set_oldest_commit_ts_xid = strtoul(optarg, &endptr, 0);
205 if (endptr == optarg || *endptr != ',')
206 {
207 fprintf(stderr, _("%s: invalid argument for option %s\n"), progname, "-c");
208 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
209 exit(1);
210 }
211 set_newest_commit_ts_xid = strtoul(endptr + 1, &endptr2, 0);
212 if (endptr2 == endptr + 1 || *endptr2 != '\0')
213 {
214 fprintf(stderr, _("%s: invalid argument for option %s\n"), progname, "-c");
215 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
216 exit(1);
217 }
218
219 if (set_oldest_commit_ts_xid < 2 &&
220 set_oldest_commit_ts_xid != 0)
221 {
222 fprintf(stderr, _("%s: transaction ID (-c) must be either 0 or greater than or equal to 2\n"), progname);
223 exit(1);
224 }
225
226 if (set_newest_commit_ts_xid < 2 &&
227 set_newest_commit_ts_xid != 0)
228 {
229 fprintf(stderr, _("%s: transaction ID (-c) must be either 0 or greater than or equal to 2\n"), progname);
230 exit(1);
231 }
232 break;
233
234 case 'o':
235 set_oid = strtoul(optarg, &endptr, 0);
236 if (endptr == optarg || *endptr != '\0')
237 {
238 fprintf(stderr, _("%s: invalid argument for option %s\n"), progname, "-o");
239 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
240 exit(1);
241 }
242 if (set_oid == 0)
243 {
244 fprintf(stderr, _("%s: OID (-o) must not be 0\n"), progname);
245 exit(1);
246 }
247 break;
248
249 case 'm':
250 set_mxid = strtoul(optarg, &endptr, 0);
251 if (endptr == optarg || *endptr != ',')
252 {
253 fprintf(stderr, _("%s: invalid argument for option %s\n"), progname, "-m");
254 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
255 exit(1);
256 }
257
258 set_oldestmxid = strtoul(endptr + 1, &endptr2, 0);
259 if (endptr2 == endptr + 1 || *endptr2 != '\0')
260 {
261 fprintf(stderr, _("%s: invalid argument for option %s\n"), progname, "-m");
262 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
263 exit(1);
264 }
265 if (set_mxid == 0)
266 {
267 fprintf(stderr, _("%s: multitransaction ID (-m) must not be 0\n"), progname);
268 exit(1);
269 }
270
271 /*
272 * XXX It'd be nice to have more sanity checks here, e.g. so
273 * that oldest is not wrapped around w.r.t. nextMulti.
274 */
275 if (set_oldestmxid == 0)
276 {
277 fprintf(stderr, _("%s: oldest multitransaction ID (-m) must not be 0\n"),
278 progname);
279 exit(1);
280 }
281 break;
282
283 case 'O':
284 set_mxoff = strtoul(optarg, &endptr, 0);
285 if (endptr == optarg || *endptr != '\0')
286 {
287 fprintf(stderr, _("%s: invalid argument for option %s\n"), progname, "-O");
288 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
289 exit(1);
290 }
291 if (set_mxoff == -1)
292 {
293 fprintf(stderr, _("%s: multitransaction offset (-O) must not be -1\n"), progname);
294 exit(1);
295 }
296 break;
297
298 case 'l':
299 if (strspn(optarg, "01234567890ABCDEFabcdef") != XLOG_FNAME_LEN)
300 {
301 fprintf(stderr, _("%s: invalid argument for option %s\n"), progname, "-l");
302 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
303 exit(1);
304 }
305
306 /*
307 * XLogFromFileName requires wal segment size which is not yet
308 * set. Hence wal details are set later on.
309 */
310 log_fname = pg_strdup(optarg);
311 break;
312
313 case 1:
314 set_wal_segsize = strtol(optarg, &endptr, 10) * 1024 * 1024;
315 if (endptr == optarg || *endptr != '\0')
316 {
317 fprintf(stderr,
318 _("%s: argument of --wal-segsize must be a number\n"),
319 progname);
320 exit(1);
321 }
322 if (!IsValidWalSegSize(set_wal_segsize))
323 {
324 fprintf(stderr,
325 _("%s: argument of --wal-segsize must be a power of 2 between 1 and 1024\n"),
326 progname);
327 exit(1);
328 }
329 break;
330
331 default:
332 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
333 exit(1);
334 }
335 }
336
337 if (DataDir == NULL && optind < argc)
338 DataDir = argv[optind++];
339
340 /* Complain if any arguments remain */
341 if (optind < argc)
342 {
343 fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
344 progname, argv[optind]);
345 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
346 progname);
347 exit(1);
348 }
349
350 if (DataDir == NULL)
351 {
352 fprintf(stderr, _("%s: no data directory specified\n"), progname);
353 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
354 exit(1);
355 }
356
357 /*
358 * Don't allow pg_resetwal to be run as root, to avoid overwriting the
359 * ownership of files in the data directory. We need only check for root
360 * -- any other user won't have sufficient permissions to modify files in
361 * the data directory.
362 */
363 #ifndef WIN32
364 if (geteuid() == 0)
365 {
366 fprintf(stderr, _("%s: cannot be executed by \"root\"\n"),
367 progname);
368 fprintf(stderr, _("You must run %s as the PostgreSQL superuser.\n"),
369 progname);
370 exit(1);
371 }
372 #endif
373
374 get_restricted_token(progname);
375
376 /* Set mask based on PGDATA permissions */
377 if (!GetDataDirectoryCreatePerm(DataDir))
378 {
379 fprintf(stderr, _("%s: could not read permissions of directory \"%s\": %s\n"),
380 progname, DataDir, strerror(errno));
381 exit(1);
382 }
383
384 umask(pg_mode_mask);
385
386 if (chdir(DataDir) < 0)
387 {
388 fprintf(stderr, _("%s: could not change directory to \"%s\": %s\n"),
389 progname, DataDir, strerror(errno));
390 exit(1);
391 }
392
393 /* Check that data directory matches our server version */
394 CheckDataVersion();
395
396 /*
397 * Check for a postmaster lock file --- if there is one, refuse to
398 * proceed, on grounds we might be interfering with a live installation.
399 */
400 if ((fd = open("postmaster.pid", O_RDONLY, 0)) < 0)
401 {
402 if (errno != ENOENT)
403 {
404 fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
405 progname, "postmaster.pid", strerror(errno));
406 exit(1);
407 }
408 }
409 else
410 {
411 fprintf(stderr, _("%s: lock file \"%s\" exists\n"
412 "Is a server running? If not, delete the lock file and try again.\n"),
413 progname, "postmaster.pid");
414 exit(1);
415 }
416
417 /*
418 * Attempt to read the existing pg_control file
419 */
420 if (!ReadControlFile())
421 GuessControlValues();
422
423 /*
424 * If no new WAL segment size was specified, use the control file value.
425 */
426 if (set_wal_segsize != 0)
427 WalSegSz = set_wal_segsize;
428 else
429 WalSegSz = ControlFile.xlog_seg_size;
430
431 if (log_fname != NULL)
432 XLogFromFileName(log_fname, &minXlogTli, &minXlogSegNo, WalSegSz);
433
434 /*
435 * Also look at existing segment files to set up newXlogSegNo
436 */
437 FindEndOfXLOG();
438
439 /*
440 * If we're not going to proceed with the reset, print the current control
441 * file parameters.
442 */
443 if ((guessed && !force) || noupdate)
444 PrintControlValues(guessed);
445
446 /*
447 * Adjust fields if required by switches. (Do this now so that printout,
448 * if any, includes these values.)
449 */
450 if (set_xid_epoch != -1)
451 ControlFile.checkPointCopy.nextXidEpoch = set_xid_epoch;
452
453 if (set_oldest_xid != 0)
454 {
455 ControlFile.checkPointCopy.oldestXid = set_oldest_xid;
456 ControlFile.checkPointCopy.oldestXidDB = InvalidOid;
457 }
458
459 if (set_xid != 0)
460 ControlFile.checkPointCopy.nextXid = set_xid;
461
462 if (set_oldest_commit_ts_xid != 0)
463 ControlFile.checkPointCopy.oldestCommitTsXid = set_oldest_commit_ts_xid;
464 if (set_newest_commit_ts_xid != 0)
465 ControlFile.checkPointCopy.newestCommitTsXid = set_newest_commit_ts_xid;
466
467 if (set_oid != 0)
468 ControlFile.checkPointCopy.nextOid = set_oid;
469
470 if (set_mxid != 0)
471 {
472 ControlFile.checkPointCopy.nextMulti = set_mxid;
473
474 ControlFile.checkPointCopy.oldestMulti = set_oldestmxid;
475 if (ControlFile.checkPointCopy.oldestMulti < FirstMultiXactId)
476 ControlFile.checkPointCopy.oldestMulti += FirstMultiXactId;
477 ControlFile.checkPointCopy.oldestMultiDB = InvalidOid;
478 }
479
480 if (set_mxoff != -1)
481 ControlFile.checkPointCopy.nextMultiOffset = set_mxoff;
482
483 if (minXlogTli > ControlFile.checkPointCopy.ThisTimeLineID)
484 {
485 ControlFile.checkPointCopy.ThisTimeLineID = minXlogTli;
486 ControlFile.checkPointCopy.PrevTimeLineID = minXlogTli;
487 }
488
489 if (set_wal_segsize != 0)
490 ControlFile.xlog_seg_size = WalSegSz;
491
492 if (minXlogSegNo > newXlogSegNo)
493 newXlogSegNo = minXlogSegNo;
494
495 /*
496 * If we had to guess anything, and -f was not given, just print the
497 * guessed values and exit. Also print if -n is given.
498 */
499 if ((guessed && !force) || noupdate)
500 {
501 PrintNewControlValues();
502 if (!noupdate)
503 {
504 printf(_("\nIf these values seem acceptable, use -f to force reset.\n"));
505 exit(1);
506 }
507 else
508 exit(0);
509 }
510
511 /*
512 * Don't reset from a dirty pg_control without -f, either.
513 */
514 if (ControlFile.state != DB_SHUTDOWNED && !force)
515 {
516 printf(_("The database server was not shut down cleanly.\n"
517 "Resetting the write-ahead log might cause data to be lost.\n"
518 "If you want to proceed anyway, use -f to force reset.\n"));
519 exit(1);
520 }
521
522 /*
523 * Else, do the dirty deed.
524 */
525 RewriteControlFile();
526 KillExistingXLOG();
527 KillExistingArchiveStatus();
528 WriteEmptyXLOG();
529
530 printf(_("Write-ahead log reset\n"));
531 return 0;
532 }
533
534
535 /*
536 * Look at the version string stored in PG_VERSION and decide if this utility
537 * can be run safely or not.
538 *
539 * We don't want to inject pg_control and WAL files that are for a different
540 * major version; that can't do anything good. Note that we don't treat
541 * mismatching version info in pg_control as a reason to bail out, because
542 * recovering from a corrupted pg_control is one of the main reasons for this
543 * program to exist at all. However, PG_VERSION is unlikely to get corrupted,
544 * and if it were it would be easy to fix by hand. So let's make this check
545 * to prevent simple user errors.
546 */
547 static void
CheckDataVersion(void)548 CheckDataVersion(void)
549 {
550 const char *ver_file = "PG_VERSION";
551 FILE *ver_fd;
552 char rawline[64];
553 int len;
554
555 if ((ver_fd = fopen(ver_file, "r")) == NULL)
556 {
557 fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
558 progname, ver_file, strerror(errno));
559 exit(1);
560 }
561
562 /* version number has to be the first line read */
563 if (!fgets(rawline, sizeof(rawline), ver_fd))
564 {
565 if (!ferror(ver_fd))
566 {
567 fprintf(stderr, _("%s: unexpected empty file \"%s\"\n"),
568 progname, ver_file);
569 }
570 else
571 {
572 fprintf(stderr, _("%s: could not read file \"%s\": %s\n"),
573 progname, ver_file, strerror(errno));
574 }
575 exit(1);
576 }
577
578 /* remove trailing newline, handling Windows newlines as well */
579 len = strlen(rawline);
580 if (len > 0 && rawline[len - 1] == '\n')
581 {
582 rawline[--len] = '\0';
583 if (len > 0 && rawline[len - 1] == '\r')
584 rawline[--len] = '\0';
585 }
586
587 if (strcmp(rawline, PG_MAJORVERSION) != 0)
588 {
589 fprintf(stderr, _("%s: data directory is of wrong version\n"
590 "File \"%s\" contains \"%s\", which is not compatible with this program's version \"%s\".\n"),
591 progname, ver_file, rawline, PG_MAJORVERSION);
592 exit(1);
593 }
594
595 fclose(ver_fd);
596 }
597
598
599 /*
600 * Try to read the existing pg_control file.
601 *
602 * This routine is also responsible for updating old pg_control versions
603 * to the current format. (Currently we don't do anything of the sort.)
604 */
605 static bool
ReadControlFile(void)606 ReadControlFile(void)
607 {
608 int fd;
609 int len;
610 char *buffer;
611 pg_crc32c crc;
612
613 if ((fd = open(XLOG_CONTROL_FILE, O_RDONLY | PG_BINARY, 0)) < 0)
614 {
615 /*
616 * If pg_control is not there at all, or we can't read it, the odds
617 * are we've been handed a bad DataDir path, so give up. User can do
618 * "touch pg_control" to force us to proceed.
619 */
620 fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
621 progname, XLOG_CONTROL_FILE, strerror(errno));
622 if (errno == ENOENT)
623 fprintf(stderr, _("If you are sure the data directory path is correct, execute\n"
624 " touch %s\n"
625 "and try again.\n"),
626 XLOG_CONTROL_FILE);
627 exit(1);
628 }
629
630 /* Use malloc to ensure we have a maxaligned buffer */
631 buffer = (char *) pg_malloc(PG_CONTROL_FILE_SIZE);
632
633 len = read(fd, buffer, PG_CONTROL_FILE_SIZE);
634 if (len < 0)
635 {
636 fprintf(stderr, _("%s: could not read file \"%s\": %s\n"),
637 progname, XLOG_CONTROL_FILE, strerror(errno));
638 exit(1);
639 }
640 close(fd);
641
642 if (len >= sizeof(ControlFileData) &&
643 ((ControlFileData *) buffer)->pg_control_version == PG_CONTROL_VERSION)
644 {
645 /* Check the CRC. */
646 INIT_CRC32C(crc);
647 COMP_CRC32C(crc,
648 buffer,
649 offsetof(ControlFileData, crc));
650 FIN_CRC32C(crc);
651
652 if (!EQ_CRC32C(crc, ((ControlFileData *) buffer)->crc))
653 {
654 /* We will use the data but treat it as guessed. */
655 fprintf(stderr,
656 _("%s: pg_control exists but has invalid CRC; proceed with caution\n"),
657 progname);
658 guessed = true;
659 }
660
661 memcpy(&ControlFile, buffer, sizeof(ControlFile));
662
663 /* return false if WAL segment size is not valid */
664 if (!IsValidWalSegSize(ControlFile.xlog_seg_size))
665 {
666 fprintf(stderr,
667 ngettext("%s: pg_control specifies invalid WAL segment size (%d byte); proceed with caution\n",
668 "%s: pg_control specifies invalid WAL segment size (%d bytes); proceed with caution\n",
669 ControlFile.xlog_seg_size),
670 progname, ControlFile.xlog_seg_size);
671 return false;
672 }
673
674 return true;
675 }
676
677 /* Looks like it's a mess. */
678 fprintf(stderr, _("%s: pg_control exists but is broken or wrong version; ignoring it\n"),
679 progname);
680 return false;
681 }
682
683
684 /*
685 * Guess at pg_control values when we can't read the old ones.
686 */
687 static void
GuessControlValues(void)688 GuessControlValues(void)
689 {
690 uint64 sysidentifier;
691 struct timeval tv;
692
693 /*
694 * Set up a completely default set of pg_control values.
695 */
696 guessed = true;
697 memset(&ControlFile, 0, sizeof(ControlFile));
698
699 ControlFile.pg_control_version = PG_CONTROL_VERSION;
700 ControlFile.catalog_version_no = CATALOG_VERSION_NO;
701
702 /*
703 * Create a new unique installation identifier, since we can no longer use
704 * any old XLOG records. See notes in xlog.c about the algorithm.
705 */
706 gettimeofday(&tv, NULL);
707 sysidentifier = ((uint64) tv.tv_sec) << 32;
708 sysidentifier |= ((uint64) tv.tv_usec) << 12;
709 sysidentifier |= getpid() & 0xFFF;
710
711 ControlFile.system_identifier = sysidentifier;
712
713 ControlFile.checkPointCopy.redo = SizeOfXLogLongPHD;
714 ControlFile.checkPointCopy.ThisTimeLineID = 1;
715 ControlFile.checkPointCopy.PrevTimeLineID = 1;
716 ControlFile.checkPointCopy.fullPageWrites = false;
717 ControlFile.checkPointCopy.nextXidEpoch = 0;
718 ControlFile.checkPointCopy.nextXid = FirstNormalTransactionId;
719 ControlFile.checkPointCopy.nextOid = FirstBootstrapObjectId;
720 ControlFile.checkPointCopy.nextMulti = FirstMultiXactId;
721 ControlFile.checkPointCopy.nextMultiOffset = 0;
722 ControlFile.checkPointCopy.oldestXid = FirstNormalTransactionId;
723 ControlFile.checkPointCopy.oldestXidDB = InvalidOid;
724 ControlFile.checkPointCopy.oldestMulti = FirstMultiXactId;
725 ControlFile.checkPointCopy.oldestMultiDB = InvalidOid;
726 ControlFile.checkPointCopy.time = (pg_time_t) time(NULL);
727 ControlFile.checkPointCopy.oldestActiveXid = InvalidTransactionId;
728
729 ControlFile.state = DB_SHUTDOWNED;
730 ControlFile.time = (pg_time_t) time(NULL);
731 ControlFile.checkPoint = ControlFile.checkPointCopy.redo;
732 ControlFile.unloggedLSN = 1;
733
734 /* minRecoveryPoint, backupStartPoint and backupEndPoint can be left zero */
735
736 ControlFile.wal_level = WAL_LEVEL_MINIMAL;
737 ControlFile.wal_log_hints = false;
738 ControlFile.track_commit_timestamp = false;
739 ControlFile.MaxConnections = 100;
740 ControlFile.max_worker_processes = 8;
741 ControlFile.max_prepared_xacts = 0;
742 ControlFile.max_locks_per_xact = 64;
743
744 ControlFile.maxAlign = MAXIMUM_ALIGNOF;
745 ControlFile.floatFormat = FLOATFORMAT_VALUE;
746 ControlFile.blcksz = BLCKSZ;
747 ControlFile.relseg_size = RELSEG_SIZE;
748 ControlFile.xlog_blcksz = XLOG_BLCKSZ;
749 ControlFile.xlog_seg_size = DEFAULT_XLOG_SEG_SIZE;
750 ControlFile.nameDataLen = NAMEDATALEN;
751 ControlFile.indexMaxKeys = INDEX_MAX_KEYS;
752 ControlFile.toast_max_chunk_size = TOAST_MAX_CHUNK_SIZE;
753 ControlFile.loblksize = LOBLKSIZE;
754 ControlFile.float4ByVal = FLOAT4PASSBYVAL;
755 ControlFile.float8ByVal = FLOAT8PASSBYVAL;
756
757 /*
758 * XXX eventually, should try to grovel through old XLOG to develop more
759 * accurate values for TimeLineID, nextXID, etc.
760 */
761 }
762
763
764 /*
765 * Print the guessed pg_control values when we had to guess.
766 *
767 * NB: this display should be just those fields that will not be
768 * reset by RewriteControlFile().
769 */
770 static void
PrintControlValues(bool guessed)771 PrintControlValues(bool guessed)
772 {
773 char sysident_str[32];
774
775 if (guessed)
776 printf(_("Guessed pg_control values:\n\n"));
777 else
778 printf(_("Current pg_control values:\n\n"));
779
780 /*
781 * Format system_identifier separately to keep platform-dependent format
782 * code out of the translatable message string.
783 */
784 snprintf(sysident_str, sizeof(sysident_str), UINT64_FORMAT,
785 ControlFile.system_identifier);
786
787 printf(_("pg_control version number: %u\n"),
788 ControlFile.pg_control_version);
789 printf(_("Catalog version number: %u\n"),
790 ControlFile.catalog_version_no);
791 printf(_("Database system identifier: %s\n"),
792 sysident_str);
793 printf(_("Latest checkpoint's TimeLineID: %u\n"),
794 ControlFile.checkPointCopy.ThisTimeLineID);
795 printf(_("Latest checkpoint's full_page_writes: %s\n"),
796 ControlFile.checkPointCopy.fullPageWrites ? _("on") : _("off"));
797 printf(_("Latest checkpoint's NextXID: %u:%u\n"),
798 ControlFile.checkPointCopy.nextXidEpoch,
799 ControlFile.checkPointCopy.nextXid);
800 printf(_("Latest checkpoint's NextOID: %u\n"),
801 ControlFile.checkPointCopy.nextOid);
802 printf(_("Latest checkpoint's NextMultiXactId: %u\n"),
803 ControlFile.checkPointCopy.nextMulti);
804 printf(_("Latest checkpoint's NextMultiOffset: %u\n"),
805 ControlFile.checkPointCopy.nextMultiOffset);
806 printf(_("Latest checkpoint's oldestXID: %u\n"),
807 ControlFile.checkPointCopy.oldestXid);
808 printf(_("Latest checkpoint's oldestXID's DB: %u\n"),
809 ControlFile.checkPointCopy.oldestXidDB);
810 printf(_("Latest checkpoint's oldestActiveXID: %u\n"),
811 ControlFile.checkPointCopy.oldestActiveXid);
812 printf(_("Latest checkpoint's oldestMultiXid: %u\n"),
813 ControlFile.checkPointCopy.oldestMulti);
814 printf(_("Latest checkpoint's oldestMulti's DB: %u\n"),
815 ControlFile.checkPointCopy.oldestMultiDB);
816 printf(_("Latest checkpoint's oldestCommitTsXid:%u\n"),
817 ControlFile.checkPointCopy.oldestCommitTsXid);
818 printf(_("Latest checkpoint's newestCommitTsXid:%u\n"),
819 ControlFile.checkPointCopy.newestCommitTsXid);
820 printf(_("Maximum data alignment: %u\n"),
821 ControlFile.maxAlign);
822 /* we don't print floatFormat since can't say much useful about it */
823 printf(_("Database block size: %u\n"),
824 ControlFile.blcksz);
825 printf(_("Blocks per segment of large relation: %u\n"),
826 ControlFile.relseg_size);
827 printf(_("WAL block size: %u\n"),
828 ControlFile.xlog_blcksz);
829 printf(_("Bytes per WAL segment: %u\n"),
830 ControlFile.xlog_seg_size);
831 printf(_("Maximum length of identifiers: %u\n"),
832 ControlFile.nameDataLen);
833 printf(_("Maximum columns in an index: %u\n"),
834 ControlFile.indexMaxKeys);
835 printf(_("Maximum size of a TOAST chunk: %u\n"),
836 ControlFile.toast_max_chunk_size);
837 printf(_("Size of a large-object chunk: %u\n"),
838 ControlFile.loblksize);
839 /* This is no longer configurable, but users may still expect to see it: */
840 printf(_("Date/time type storage: %s\n"),
841 _("64-bit integers"));
842 printf(_("Float4 argument passing: %s\n"),
843 (ControlFile.float4ByVal ? _("by value") : _("by reference")));
844 printf(_("Float8 argument passing: %s\n"),
845 (ControlFile.float8ByVal ? _("by value") : _("by reference")));
846 printf(_("Data page checksum version: %u\n"),
847 ControlFile.data_checksum_version);
848 }
849
850
851 /*
852 * Print the values to be changed.
853 */
854 static void
PrintNewControlValues(void)855 PrintNewControlValues(void)
856 {
857 char fname[MAXFNAMELEN];
858
859 /* This will be always printed in order to keep format same. */
860 printf(_("\n\nValues to be changed:\n\n"));
861
862 XLogFileName(fname, ControlFile.checkPointCopy.ThisTimeLineID,
863 newXlogSegNo, WalSegSz);
864 printf(_("First log segment after reset: %s\n"), fname);
865
866 if (set_mxid != 0)
867 {
868 printf(_("NextMultiXactId: %u\n"),
869 ControlFile.checkPointCopy.nextMulti);
870 printf(_("OldestMultiXid: %u\n"),
871 ControlFile.checkPointCopy.oldestMulti);
872 printf(_("OldestMulti's DB: %u\n"),
873 ControlFile.checkPointCopy.oldestMultiDB);
874 }
875
876 if (set_mxoff != -1)
877 {
878 printf(_("NextMultiOffset: %u\n"),
879 ControlFile.checkPointCopy.nextMultiOffset);
880 }
881
882 if (set_oid != 0)
883 {
884 printf(_("NextOID: %u\n"),
885 ControlFile.checkPointCopy.nextOid);
886 }
887
888 if (set_xid != 0)
889 {
890 printf(_("NextXID: %u\n"),
891 ControlFile.checkPointCopy.nextXid);
892 printf(_("OldestXID: %u\n"),
893 ControlFile.checkPointCopy.oldestXid);
894 printf(_("OldestXID's DB: %u\n"),
895 ControlFile.checkPointCopy.oldestXidDB);
896 }
897
898 if (set_xid_epoch != -1)
899 {
900 printf(_("NextXID epoch: %u\n"),
901 ControlFile.checkPointCopy.nextXidEpoch);
902 }
903
904 if (set_oldest_commit_ts_xid != 0)
905 {
906 printf(_("oldestCommitTsXid: %u\n"),
907 ControlFile.checkPointCopy.oldestCommitTsXid);
908 }
909 if (set_newest_commit_ts_xid != 0)
910 {
911 printf(_("newestCommitTsXid: %u\n"),
912 ControlFile.checkPointCopy.newestCommitTsXid);
913 }
914
915 if (set_wal_segsize != 0)
916 {
917 printf(_("Bytes per WAL segment: %u\n"),
918 ControlFile.xlog_seg_size);
919 }
920 }
921
922
923 /*
924 * Write out the new pg_control file.
925 */
926 static void
RewriteControlFile(void)927 RewriteControlFile(void)
928 {
929 int fd;
930 char buffer[PG_CONTROL_FILE_SIZE]; /* need not be aligned */
931
932 /*
933 * For good luck, apply the same static assertions as in backend's
934 * WriteControlFile().
935 */
936 StaticAssertStmt(sizeof(ControlFileData) <= PG_CONTROL_MAX_SAFE_SIZE,
937 "pg_control is too large for atomic disk writes");
938 StaticAssertStmt(sizeof(ControlFileData) <= PG_CONTROL_FILE_SIZE,
939 "sizeof(ControlFileData) exceeds PG_CONTROL_FILE_SIZE");
940
941 /*
942 * Adjust fields as needed to force an empty XLOG starting at
943 * newXlogSegNo.
944 */
945 XLogSegNoOffsetToRecPtr(newXlogSegNo, SizeOfXLogLongPHD, WalSegSz,
946 ControlFile.checkPointCopy.redo);
947 ControlFile.checkPointCopy.time = (pg_time_t) time(NULL);
948
949 ControlFile.state = DB_SHUTDOWNED;
950 ControlFile.time = (pg_time_t) time(NULL);
951 ControlFile.checkPoint = ControlFile.checkPointCopy.redo;
952 ControlFile.minRecoveryPoint = 0;
953 ControlFile.minRecoveryPointTLI = 0;
954 ControlFile.backupStartPoint = 0;
955 ControlFile.backupEndPoint = 0;
956 ControlFile.backupEndRequired = false;
957
958 /*
959 * Force the defaults for max_* settings. The values don't really matter
960 * as long as wal_level='minimal'; the postmaster will reset these fields
961 * anyway at startup.
962 */
963 ControlFile.wal_level = WAL_LEVEL_MINIMAL;
964 ControlFile.wal_log_hints = false;
965 ControlFile.track_commit_timestamp = false;
966 ControlFile.MaxConnections = 100;
967 ControlFile.max_worker_processes = 8;
968 ControlFile.max_prepared_xacts = 0;
969 ControlFile.max_locks_per_xact = 64;
970
971 /* Contents are protected with a CRC */
972 INIT_CRC32C(ControlFile.crc);
973 COMP_CRC32C(ControlFile.crc,
974 (char *) &ControlFile,
975 offsetof(ControlFileData, crc));
976 FIN_CRC32C(ControlFile.crc);
977
978 /*
979 * We write out PG_CONTROL_FILE_SIZE bytes into pg_control, zero-padding
980 * the excess over sizeof(ControlFileData). This reduces the odds of
981 * premature-EOF errors when reading pg_control. We'll still fail when we
982 * check the contents of the file, but hopefully with a more specific
983 * error than "couldn't read pg_control".
984 */
985 memset(buffer, 0, PG_CONTROL_FILE_SIZE);
986 memcpy(buffer, &ControlFile, sizeof(ControlFileData));
987
988 unlink(XLOG_CONTROL_FILE);
989
990 fd = open(XLOG_CONTROL_FILE,
991 O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
992 pg_file_create_mode);
993 if (fd < 0)
994 {
995 fprintf(stderr, _("%s: could not create pg_control file: %s\n"),
996 progname, strerror(errno));
997 exit(1);
998 }
999
1000 errno = 0;
1001 if (write(fd, buffer, PG_CONTROL_FILE_SIZE) != PG_CONTROL_FILE_SIZE)
1002 {
1003 /* if write didn't set errno, assume problem is no disk space */
1004 if (errno == 0)
1005 errno = ENOSPC;
1006 fprintf(stderr, _("%s: could not write pg_control file: %s\n"),
1007 progname, strerror(errno));
1008 exit(1);
1009 }
1010
1011 if (fsync(fd) != 0)
1012 {
1013 fprintf(stderr, _("%s: fsync error: %s\n"), progname, strerror(errno));
1014 exit(1);
1015 }
1016
1017 close(fd);
1018 }
1019
1020
1021 /*
1022 * Scan existing XLOG files and determine the highest existing WAL address
1023 *
1024 * On entry, ControlFile.checkPointCopy.redo and ControlFile.xlog_seg_size
1025 * are assumed valid (note that we allow the old xlog seg size to differ
1026 * from what we're using). On exit, newXlogId and newXlogSeg are set to
1027 * suitable values for the beginning of replacement WAL (in our seg size).
1028 */
1029 static void
FindEndOfXLOG(void)1030 FindEndOfXLOG(void)
1031 {
1032 DIR *xldir;
1033 struct dirent *xlde;
1034 uint64 segs_per_xlogid;
1035 uint64 xlogbytepos;
1036
1037 /*
1038 * Initialize the max() computation using the last checkpoint address from
1039 * old pg_control. Note that for the moment we are working with segment
1040 * numbering according to the old xlog seg size.
1041 */
1042 segs_per_xlogid = (UINT64CONST(0x0000000100000000) / ControlFile.xlog_seg_size);
1043 newXlogSegNo = ControlFile.checkPointCopy.redo / ControlFile.xlog_seg_size;
1044
1045 /*
1046 * Scan the pg_wal directory to find existing WAL segment files. We assume
1047 * any present have been used; in most scenarios this should be
1048 * conservative, because of xlog.c's attempts to pre-create files.
1049 */
1050 xldir = opendir(XLOGDIR);
1051 if (xldir == NULL)
1052 {
1053 fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
1054 progname, XLOGDIR, strerror(errno));
1055 exit(1);
1056 }
1057
1058 while (errno = 0, (xlde = readdir(xldir)) != NULL)
1059 {
1060 if (IsXLogFileName(xlde->d_name) ||
1061 IsPartialXLogFileName(xlde->d_name))
1062 {
1063 unsigned int tli,
1064 log,
1065 seg;
1066 XLogSegNo segno;
1067
1068 /*
1069 * Note: We don't use XLogFromFileName here, because we want to
1070 * use the segment size from the control file, not the size the
1071 * pg_resetwal binary was compiled with
1072 */
1073 sscanf(xlde->d_name, "%08X%08X%08X", &tli, &log, &seg);
1074 segno = ((uint64) log) * segs_per_xlogid + seg;
1075
1076 /*
1077 * Note: we take the max of all files found, regardless of their
1078 * timelines. Another possibility would be to ignore files of
1079 * timelines other than the target TLI, but this seems safer.
1080 * Better too large a result than too small...
1081 */
1082 if (segno > newXlogSegNo)
1083 newXlogSegNo = segno;
1084 }
1085 }
1086
1087 if (errno)
1088 {
1089 fprintf(stderr, _("%s: could not read directory \"%s\": %s\n"),
1090 progname, XLOGDIR, strerror(errno));
1091 exit(1);
1092 }
1093
1094 if (closedir(xldir))
1095 {
1096 fprintf(stderr, _("%s: could not close directory \"%s\": %s\n"),
1097 progname, XLOGDIR, strerror(errno));
1098 exit(1);
1099 }
1100
1101 /*
1102 * Finally, convert to new xlog seg size, and advance by one to ensure we
1103 * are in virgin territory.
1104 */
1105 xlogbytepos = newXlogSegNo * ControlFile.xlog_seg_size;
1106 newXlogSegNo = (xlogbytepos + ControlFile.xlog_seg_size - 1) / WalSegSz;
1107 newXlogSegNo++;
1108 }
1109
1110
1111 /*
1112 * Remove existing XLOG files
1113 */
1114 static void
KillExistingXLOG(void)1115 KillExistingXLOG(void)
1116 {
1117 DIR *xldir;
1118 struct dirent *xlde;
1119 char path[MAXPGPATH + sizeof(XLOGDIR)];
1120
1121 xldir = opendir(XLOGDIR);
1122 if (xldir == NULL)
1123 {
1124 fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
1125 progname, XLOGDIR, strerror(errno));
1126 exit(1);
1127 }
1128
1129 while (errno = 0, (xlde = readdir(xldir)) != NULL)
1130 {
1131 if (IsXLogFileName(xlde->d_name) ||
1132 IsPartialXLogFileName(xlde->d_name))
1133 {
1134 snprintf(path, sizeof(path), "%s/%s", XLOGDIR, xlde->d_name);
1135 if (unlink(path) < 0)
1136 {
1137 fprintf(stderr, _("%s: could not delete file \"%s\": %s\n"),
1138 progname, path, strerror(errno));
1139 exit(1);
1140 }
1141 }
1142 }
1143
1144 if (errno)
1145 {
1146 fprintf(stderr, _("%s: could not read directory \"%s\": %s\n"),
1147 progname, XLOGDIR, strerror(errno));
1148 exit(1);
1149 }
1150
1151 if (closedir(xldir))
1152 {
1153 fprintf(stderr, _("%s: could not close directory \"%s\": %s\n"),
1154 progname, XLOGDIR, strerror(errno));
1155 exit(1);
1156 }
1157 }
1158
1159
1160 /*
1161 * Remove existing archive status files
1162 */
1163 static void
KillExistingArchiveStatus(void)1164 KillExistingArchiveStatus(void)
1165 {
1166 #define ARCHSTATDIR XLOGDIR "/archive_status"
1167
1168 DIR *xldir;
1169 struct dirent *xlde;
1170 char path[MAXPGPATH + sizeof(ARCHSTATDIR)];
1171
1172 xldir = opendir(ARCHSTATDIR);
1173 if (xldir == NULL)
1174 {
1175 fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
1176 progname, ARCHSTATDIR, strerror(errno));
1177 exit(1);
1178 }
1179
1180 while (errno = 0, (xlde = readdir(xldir)) != NULL)
1181 {
1182 if (strspn(xlde->d_name, "0123456789ABCDEF") == XLOG_FNAME_LEN &&
1183 (strcmp(xlde->d_name + XLOG_FNAME_LEN, ".ready") == 0 ||
1184 strcmp(xlde->d_name + XLOG_FNAME_LEN, ".done") == 0 ||
1185 strcmp(xlde->d_name + XLOG_FNAME_LEN, ".partial.ready") == 0 ||
1186 strcmp(xlde->d_name + XLOG_FNAME_LEN, ".partial.done") == 0))
1187 {
1188 snprintf(path, sizeof(path), "%s/%s", ARCHSTATDIR, xlde->d_name);
1189 if (unlink(path) < 0)
1190 {
1191 fprintf(stderr, _("%s: could not delete file \"%s\": %s\n"),
1192 progname, path, strerror(errno));
1193 exit(1);
1194 }
1195 }
1196 }
1197
1198 if (errno)
1199 {
1200 fprintf(stderr, _("%s: could not read directory \"%s\": %s\n"),
1201 progname, ARCHSTATDIR, strerror(errno));
1202 exit(1);
1203 }
1204
1205 if (closedir(xldir))
1206 {
1207 fprintf(stderr, _("%s: could not close directory \"%s\": %s\n"),
1208 progname, ARCHSTATDIR, strerror(errno));
1209 exit(1);
1210 }
1211 }
1212
1213
1214 /*
1215 * Write an empty XLOG file, containing only the checkpoint record
1216 * already set up in ControlFile.
1217 */
1218 static void
WriteEmptyXLOG(void)1219 WriteEmptyXLOG(void)
1220 {
1221 PGAlignedXLogBlock buffer;
1222 XLogPageHeader page;
1223 XLogLongPageHeader longpage;
1224 XLogRecord *record;
1225 pg_crc32c crc;
1226 char path[MAXPGPATH];
1227 int fd;
1228 int nbytes;
1229 char *recptr;
1230
1231 memset(buffer.data, 0, XLOG_BLCKSZ);
1232
1233 /* Set up the XLOG page header */
1234 page = (XLogPageHeader) buffer.data;
1235 page->xlp_magic = XLOG_PAGE_MAGIC;
1236 page->xlp_info = XLP_LONG_HEADER;
1237 page->xlp_tli = ControlFile.checkPointCopy.ThisTimeLineID;
1238 page->xlp_pageaddr = ControlFile.checkPointCopy.redo - SizeOfXLogLongPHD;
1239 longpage = (XLogLongPageHeader) page;
1240 longpage->xlp_sysid = ControlFile.system_identifier;
1241 longpage->xlp_seg_size = WalSegSz;
1242 longpage->xlp_xlog_blcksz = XLOG_BLCKSZ;
1243
1244 /* Insert the initial checkpoint record */
1245 recptr = (char *) page + SizeOfXLogLongPHD;
1246 record = (XLogRecord *) recptr;
1247 record->xl_prev = 0;
1248 record->xl_xid = InvalidTransactionId;
1249 record->xl_tot_len = SizeOfXLogRecord + SizeOfXLogRecordDataHeaderShort + sizeof(CheckPoint);
1250 record->xl_info = XLOG_CHECKPOINT_SHUTDOWN;
1251 record->xl_rmid = RM_XLOG_ID;
1252
1253 recptr += SizeOfXLogRecord;
1254 *(recptr++) = (char) XLR_BLOCK_ID_DATA_SHORT;
1255 *(recptr++) = sizeof(CheckPoint);
1256 memcpy(recptr, &ControlFile.checkPointCopy,
1257 sizeof(CheckPoint));
1258
1259 INIT_CRC32C(crc);
1260 COMP_CRC32C(crc, ((char *) record) + SizeOfXLogRecord, record->xl_tot_len - SizeOfXLogRecord);
1261 COMP_CRC32C(crc, (char *) record, offsetof(XLogRecord, xl_crc));
1262 FIN_CRC32C(crc);
1263 record->xl_crc = crc;
1264
1265 /* Write the first page */
1266 XLogFilePath(path, ControlFile.checkPointCopy.ThisTimeLineID,
1267 newXlogSegNo, WalSegSz);
1268
1269 unlink(path);
1270
1271 fd = open(path, O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
1272 pg_file_create_mode);
1273 if (fd < 0)
1274 {
1275 fprintf(stderr, _("%s: could not open file \"%s\": %s\n"),
1276 progname, path, strerror(errno));
1277 exit(1);
1278 }
1279
1280 errno = 0;
1281 if (write(fd, buffer.data, XLOG_BLCKSZ) != XLOG_BLCKSZ)
1282 {
1283 /* if write didn't set errno, assume problem is no disk space */
1284 if (errno == 0)
1285 errno = ENOSPC;
1286 fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
1287 progname, path, strerror(errno));
1288 exit(1);
1289 }
1290
1291 /* Fill the rest of the file with zeroes */
1292 memset(buffer.data, 0, XLOG_BLCKSZ);
1293 for (nbytes = XLOG_BLCKSZ; nbytes < WalSegSz; nbytes += XLOG_BLCKSZ)
1294 {
1295 errno = 0;
1296 if (write(fd, buffer.data, XLOG_BLCKSZ) != XLOG_BLCKSZ)
1297 {
1298 if (errno == 0)
1299 errno = ENOSPC;
1300 fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
1301 progname, path, strerror(errno));
1302 exit(1);
1303 }
1304 }
1305
1306 if (fsync(fd) != 0)
1307 {
1308 fprintf(stderr, _("%s: fsync error: %s\n"), progname, strerror(errno));
1309 exit(1);
1310 }
1311
1312 close(fd);
1313 }
1314
1315
1316 static void
usage(void)1317 usage(void)
1318 {
1319 printf(_("%s resets the PostgreSQL write-ahead log.\n\n"), progname);
1320 printf(_("Usage:\n %s [OPTION]... DATADIR\n\n"), progname);
1321 printf(_("Options:\n"));
1322 printf(_(" -c, --commit-timestamp-ids=XID,XID\n"
1323 " set oldest and newest transactions bearing\n"
1324 " commit timestamp (zero means no change)\n"));
1325 printf(_(" [-D, --pgdata=]DATADIR data directory\n"));
1326 printf(_(" -e, --epoch=XIDEPOCH set next transaction ID epoch\n"));
1327 printf(_(" -f, --force force update to be done\n"));
1328 printf(_(" -l, --next-wal-file=WALFILE set minimum starting location for new WAL\n"));
1329 printf(_(" -m, --multixact-ids=MXID,MXID set next and oldest multitransaction ID\n"));
1330 printf(_(" -n, --dry-run no update, just show what would be done\n"));
1331 printf(_(" -o, --next-oid=OID set next OID\n"));
1332 printf(_(" -O, --multixact-offset=OFFSET set next multitransaction offset\n"));
1333 printf(_(" -u, --oldest-transaction-id=XID set oldest transaction ID\n"));
1334 printf(_(" -V, --version output version information, then exit\n"));
1335 printf(_(" -x, --next-transaction-id=XID set next transaction ID\n"));
1336 printf(_(" --wal-segsize=SIZE size of WAL segments, in megabytes\n"));
1337 printf(_(" -?, --help show this help, then exit\n"));
1338 printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
1339 }
1340