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