]> granicus.if.org Git - postgresql/blob - src/bin/pg_resetxlog/pg_resetxlog.c
5d29ff94cf6c07cebafbf4597c458d33f21bf9cb
[postgresql] / src / bin / pg_resetxlog / pg_resetxlog.c
1 /*-------------------------------------------------------------------------
2  *
3  * pg_resetxlog.c
4  *        A utility to "zero out" the xlog when it's corrupt beyond recovery.
5  *        Can also rebuild pg_control if needed.
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-2007, PostgreSQL Global Development Group
24  * Portions Copyright (c) 1994, Regents of the University of California
25  *
26  * $PostgreSQL: pgsql/src/bin/pg_resetxlog/pg_resetxlog.c,v 1.60 2007/07/12 21:27:09 tgl Exp $
27  *
28  *-------------------------------------------------------------------------
29  */
30 #include "postgres.h"
31
32 #include <dirent.h>
33 #include <fcntl.h>
34 #include <locale.h>
35 #include <sys/stat.h>
36 #include <sys/time.h>
37 #include <time.h>
38 #include <unistd.h>
39 #ifdef HAVE_GETOPT_H
40 #include <getopt.h>
41 #endif
42
43 #include "access/transam.h"
44 #include "access/tuptoaster.h"
45 #include "access/multixact.h"
46 #include "access/xlog_internal.h"
47 #include "catalog/catversion.h"
48 #include "catalog/pg_control.h"
49
50 extern int      optind;
51 extern char *optarg;
52
53
54 static ControlFileData ControlFile;             /* pg_control values */
55 static uint32 newXlogId,
56                         newXlogSeg;                     /* ID/Segment of new XLOG segment */
57 static bool guessed = false;    /* T if we had to guess at any values */
58 static const char *progname;
59
60 static bool ReadControlFile(void);
61 static void GuessControlValues(void);
62 static void PrintControlValues(bool guessed);
63 static void RewriteControlFile(void);
64 static void FindEndOfXLOG(void);
65 static void KillExistingXLOG(void);
66 static void WriteEmptyXLOG(void);
67 static void usage(void);
68
69
70 int
71 main(int argc, char *argv[])
72 {
73         int                     c;
74         bool            force = false;
75         bool            noupdate = false;
76         uint32          set_xid_epoch = (uint32) -1;
77         TransactionId set_xid = 0;
78         Oid                     set_oid = 0;
79         MultiXactId set_mxid = 0;
80         MultiXactOffset set_mxoff = (MultiXactOffset) -1;
81         uint32          minXlogTli = 0,
82                                 minXlogId = 0,
83                                 minXlogSeg = 0;
84         char       *endptr;
85         char       *endptr2;
86         char       *endptr3;
87         char       *DataDir;
88         int                     fd;
89         char            path[MAXPGPATH];
90
91         set_pglocale_pgservice(argv[0], "pg_resetxlog");
92
93         progname = get_progname(argv[0]);
94
95         if (argc > 1)
96         {
97                 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
98                 {
99                         usage();
100                         exit(0);
101                 }
102                 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
103                 {
104                         puts("pg_resetxlog (PostgreSQL) " PG_VERSION);
105                         exit(0);
106                 }
107         }
108
109
110         while ((c = getopt(argc, argv, "fl:m:no:O:x:e:")) != -1)
111         {
112                 switch (c)
113                 {
114                         case 'f':
115                                 force = true;
116                                 break;
117
118                         case 'n':
119                                 noupdate = true;
120                                 break;
121
122                         case 'e':
123                                 set_xid_epoch = strtoul(optarg, &endptr, 0);
124                                 if (endptr == optarg || *endptr != '\0')
125                                 {
126                                         fprintf(stderr, _("%s: invalid argument for option -e\n"), progname);
127                                         fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
128                                         exit(1);
129                                 }
130                                 if (set_xid_epoch == -1)
131                                 {
132                                         fprintf(stderr, _("%s: transaction ID epoch (-e) must not be -1\n"), progname);
133                                         exit(1);
134                                 }
135                                 break;
136
137                         case 'x':
138                                 set_xid = strtoul(optarg, &endptr, 0);
139                                 if (endptr == optarg || *endptr != '\0')
140                                 {
141                                         fprintf(stderr, _("%s: invalid argument for option -x\n"), progname);
142                                         fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
143                                         exit(1);
144                                 }
145                                 if (set_xid == 0)
146                                 {
147                                         fprintf(stderr, _("%s: transaction ID (-x) must not be 0\n"), progname);
148                                         exit(1);
149                                 }
150                                 break;
151
152                         case 'o':
153                                 set_oid = strtoul(optarg, &endptr, 0);
154                                 if (endptr == optarg || *endptr != '\0')
155                                 {
156                                         fprintf(stderr, _("%s: invalid argument for option -o\n"), progname);
157                                         fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
158                                         exit(1);
159                                 }
160                                 if (set_oid == 0)
161                                 {
162                                         fprintf(stderr, _("%s: OID (-o) must not be 0\n"), progname);
163                                         exit(1);
164                                 }
165                                 break;
166
167                         case 'm':
168                                 set_mxid = strtoul(optarg, &endptr, 0);
169                                 if (endptr == optarg || *endptr != '\0')
170                                 {
171                                         fprintf(stderr, _("%s: invalid argument for option -m\n"), progname);
172                                         fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
173                                         exit(1);
174                                 }
175                                 if (set_mxid == 0)
176                                 {
177                                         fprintf(stderr, _("%s: multitransaction ID (-m) must not be 0\n"), progname);
178                                         exit(1);
179                                 }
180                                 break;
181
182                         case 'O':
183                                 set_mxoff = strtoul(optarg, &endptr, 0);
184                                 if (endptr == optarg || *endptr != '\0')
185                                 {
186                                         fprintf(stderr, _("%s: invalid argument for option -O\n"), progname);
187                                         fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
188                                         exit(1);
189                                 }
190                                 if (set_mxoff == -1)
191                                 {
192                                         fprintf(stderr, _("%s: multitransaction offset (-O) must not be -1\n"), progname);
193                                         exit(1);
194                                 }
195                                 break;
196
197                         case 'l':
198                                 minXlogTli = strtoul(optarg, &endptr, 0);
199                                 if (endptr == optarg || *endptr != ',')
200                                 {
201                                         fprintf(stderr, _("%s: invalid argument for option -l\n"), progname);
202                                         fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
203                                         exit(1);
204                                 }
205                                 minXlogId = strtoul(endptr + 1, &endptr2, 0);
206                                 if (endptr2 == endptr + 1 || *endptr2 != ',')
207                                 {
208                                         fprintf(stderr, _("%s: invalid argument for option -l\n"), progname);
209                                         fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
210                                         exit(1);
211                                 }
212                                 minXlogSeg = strtoul(endptr2 + 1, &endptr3, 0);
213                                 if (endptr3 == endptr2 + 1 || *endptr3 != '\0')
214                                 {
215                                         fprintf(stderr, _("%s: invalid argument for option -l\n"), progname);
216                                         fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
217                                         exit(1);
218                                 }
219                                 break;
220
221                         default:
222                                 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
223                                 exit(1);
224                 }
225         }
226
227         if (optind == argc)
228         {
229                 fprintf(stderr, _("%s: no data directory specified\n"), progname);
230                 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
231                 exit(1);
232         }
233
234         /*
235          * Don't allow pg_resetxlog to be run as root, to avoid overwriting the
236          * ownership of files in the data directory. We need only check for root
237          * -- any other user won't have sufficient permissions to modify files in
238          * the data directory.
239          */
240 #ifndef WIN32
241         if (geteuid() == 0)
242         {
243                 fprintf(stderr, _("%s: cannot be executed by \"root\"\n"),
244                                 progname);
245                 fprintf(stderr, _("You must run %s as the PostgreSQL superuser.\n"),
246                                 progname);
247                 exit(1);
248         }
249 #endif
250
251         DataDir = argv[optind];
252
253         if (chdir(DataDir) < 0)
254         {
255                 fprintf(stderr, _("%s: could not change directory to \"%s\": %s\n"),
256                                 progname, DataDir, strerror(errno));
257                 exit(1);
258         }
259
260         /*
261          * Check for a postmaster lock file --- if there is one, refuse to
262          * proceed, on grounds we might be interfering with a live installation.
263          */
264         snprintf(path, MAXPGPATH, "%s/postmaster.pid", DataDir);
265
266         if ((fd = open(path, O_RDONLY, 0)) < 0)
267         {
268                 if (errno != ENOENT)
269                 {
270                         fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"), progname, path, strerror(errno));
271                         exit(1);
272                 }
273         }
274         else
275         {
276                 fprintf(stderr, _("%s: lock file \"%s\" exists\n"
277                                                   "Is a server running?  If not, delete the lock file and try again.\n"),
278                                 progname, path);
279                 exit(1);
280         }
281
282         /*
283          * Attempt to read the existing pg_control file
284          */
285         if (!ReadControlFile())
286                 GuessControlValues();
287
288         /*
289          * Also look at existing segment files to set up newXlogId/newXlogSeg
290          */
291         FindEndOfXLOG();
292
293         /*
294          * Adjust fields if required by switches.  (Do this now so that printout,
295          * if any, includes these values.)
296          */
297         if (set_xid_epoch != -1)
298                 ControlFile.checkPointCopy.nextXidEpoch = set_xid_epoch;
299
300         if (set_xid != 0)
301                 ControlFile.checkPointCopy.nextXid = set_xid;
302
303         if (set_oid != 0)
304                 ControlFile.checkPointCopy.nextOid = set_oid;
305
306         if (set_mxid != 0)
307                 ControlFile.checkPointCopy.nextMulti = set_mxid;
308
309         if (set_mxoff != -1)
310                 ControlFile.checkPointCopy.nextMultiOffset = set_mxoff;
311
312         if (minXlogTli > ControlFile.checkPointCopy.ThisTimeLineID)
313                 ControlFile.checkPointCopy.ThisTimeLineID = minXlogTli;
314
315         if (minXlogId > newXlogId ||
316                 (minXlogId == newXlogId &&
317                  minXlogSeg > newXlogSeg))
318         {
319                 newXlogId = minXlogId;
320                 newXlogSeg = minXlogSeg;
321         }
322
323         /*
324          * If we had to guess anything, and -f was not given, just print the
325          * guessed values and exit.  Also print if -n is given.
326          */
327         if ((guessed && !force) || noupdate)
328         {
329                 PrintControlValues(guessed);
330                 if (!noupdate)
331                 {
332                         printf(_("\nIf these values seem acceptable, use -f to force reset.\n"));
333                         exit(1);
334                 }
335                 else
336                         exit(0);
337         }
338
339         /*
340          * Don't reset from a dirty pg_control without -f, either.
341          */
342         if (ControlFile.state != DB_SHUTDOWNED && !force)
343         {
344                 printf(_("The database server was not shut down cleanly.\n"
345                                  "Resetting the transaction log might cause data to be lost.\n"
346                                  "If you want to proceed anyway, use -f to force reset.\n"));
347                 exit(1);
348         }
349
350         /*
351          * Else, do the dirty deed.
352          */
353         RewriteControlFile();
354         KillExistingXLOG();
355         WriteEmptyXLOG();
356
357         printf(_("Transaction log reset\n"));
358         return 0;
359 }
360
361
362 /*
363  * Try to read the existing pg_control file.
364  *
365  * This routine is also responsible for updating old pg_control versions
366  * to the current format.  (Currently we don't do anything of the sort.)
367  */
368 static bool
369 ReadControlFile(void)
370 {
371         int                     fd;
372         int                     len;
373         char       *buffer;
374         pg_crc32        crc;
375
376         if ((fd = open(XLOG_CONTROL_FILE, O_RDONLY, 0)) < 0)
377         {
378                 /*
379                  * If pg_control is not there at all, or we can't read it, the odds
380                  * are we've been handed a bad DataDir path, so give up. User can do
381                  * "touch pg_control" to force us to proceed.
382                  */
383                 fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
384                                 progname, XLOG_CONTROL_FILE, strerror(errno));
385                 if (errno == ENOENT)
386                         fprintf(stderr, _("If you are sure the data directory path is correct, execute\n"
387                                                           "  touch %s\n"
388                                                           "and try again.\n"),
389                                         XLOG_CONTROL_FILE);
390                 exit(1);
391         }
392
393         /* Use malloc to ensure we have a maxaligned buffer */
394         buffer = (char *) malloc(PG_CONTROL_SIZE);
395
396         len = read(fd, buffer, PG_CONTROL_SIZE);
397         if (len < 0)
398         {
399                 fprintf(stderr, _("%s: could not read file \"%s\": %s\n"),
400                                 progname, XLOG_CONTROL_FILE, strerror(errno));
401                 exit(1);
402         }
403         close(fd);
404
405         if (len >= sizeof(ControlFileData) &&
406           ((ControlFileData *) buffer)->pg_control_version == PG_CONTROL_VERSION)
407         {
408                 /* Check the CRC. */
409                 INIT_CRC32(crc);
410                 COMP_CRC32(crc,
411                                    buffer,
412                                    offsetof(ControlFileData, crc));
413                 FIN_CRC32(crc);
414
415                 if (EQ_CRC32(crc, ((ControlFileData *) buffer)->crc))
416                 {
417                         /* Valid data... */
418                         memcpy(&ControlFile, buffer, sizeof(ControlFile));
419                         return true;
420                 }
421
422                 fprintf(stderr, _("%s: pg_control exists but has invalid CRC; proceed with caution\n"),
423                                 progname);
424                 /* We will use the data anyway, but treat it as guessed. */
425                 memcpy(&ControlFile, buffer, sizeof(ControlFile));
426                 guessed = true;
427                 return true;
428         }
429
430         /* Looks like it's a mess. */
431         fprintf(stderr, _("%s: pg_control exists but is broken or unknown version; ignoring it\n"),
432                         progname);
433         return false;
434 }
435
436
437 /*
438  * Guess at pg_control values when we can't read the old ones.
439  */
440 static void
441 GuessControlValues(void)
442 {
443         uint64          sysidentifier;
444         struct timeval tv;
445         char       *localeptr;
446
447         /*
448          * Set up a completely default set of pg_control values.
449          */
450         guessed = true;
451         memset(&ControlFile, 0, sizeof(ControlFile));
452
453         ControlFile.pg_control_version = PG_CONTROL_VERSION;
454         ControlFile.catalog_version_no = CATALOG_VERSION_NO;
455
456         /*
457          * Create a new unique installation identifier, since we can no longer use
458          * any old XLOG records.  See notes in xlog.c about the algorithm.
459          */
460         gettimeofday(&tv, NULL);
461         sysidentifier = ((uint64) tv.tv_sec) << 32;
462         sysidentifier |= (uint32) (tv.tv_sec | tv.tv_usec);
463
464         ControlFile.system_identifier = sysidentifier;
465
466         ControlFile.checkPointCopy.redo.xlogid = 0;
467         ControlFile.checkPointCopy.redo.xrecoff = SizeOfXLogLongPHD;
468         ControlFile.checkPointCopy.ThisTimeLineID = 1;
469         ControlFile.checkPointCopy.nextXidEpoch = 0;
470         ControlFile.checkPointCopy.nextXid = (TransactionId) 514;       /* XXX */
471         ControlFile.checkPointCopy.nextOid = FirstBootstrapObjectId;
472         ControlFile.checkPointCopy.nextMulti = FirstMultiXactId;
473         ControlFile.checkPointCopy.nextMultiOffset = 0;
474         ControlFile.checkPointCopy.time = time(NULL);
475
476         ControlFile.state = DB_SHUTDOWNED;
477         ControlFile.time = time(NULL);
478         ControlFile.checkPoint = ControlFile.checkPointCopy.redo;
479
480         ControlFile.maxAlign = MAXIMUM_ALIGNOF;
481         ControlFile.floatFormat = FLOATFORMAT_VALUE;
482         ControlFile.blcksz = BLCKSZ;
483         ControlFile.relseg_size = RELSEG_SIZE;
484         ControlFile.xlog_blcksz = XLOG_BLCKSZ;
485         ControlFile.xlog_seg_size = XLOG_SEG_SIZE;
486         ControlFile.nameDataLen = NAMEDATALEN;
487         ControlFile.indexMaxKeys = INDEX_MAX_KEYS;
488         ControlFile.toast_max_chunk_size = TOAST_MAX_CHUNK_SIZE;
489 #ifdef HAVE_INT64_TIMESTAMP
490         ControlFile.enableIntTimes = TRUE;
491 #else
492         ControlFile.enableIntTimes = FALSE;
493 #endif
494         ControlFile.localeBuflen = LOCALE_NAME_BUFLEN;
495
496         localeptr = setlocale(LC_COLLATE, "");
497         if (!localeptr)
498         {
499                 fprintf(stderr, _("%s: invalid LC_COLLATE setting\n"), progname);
500                 exit(1);
501         }
502         strlcpy(ControlFile.lc_collate, localeptr, sizeof(ControlFile.lc_collate));
503         localeptr = setlocale(LC_CTYPE, "");
504         if (!localeptr)
505         {
506                 fprintf(stderr, _("%s: invalid LC_CTYPE setting\n"), progname);
507                 exit(1);
508         }
509         strlcpy(ControlFile.lc_ctype, localeptr, sizeof(ControlFile.lc_ctype));
510
511         /*
512          * XXX eventually, should try to grovel through old XLOG to develop more
513          * accurate values for TimeLineID, nextXID, etc.
514          */
515 }
516
517
518 /*
519  * Print the guessed pg_control values when we had to guess.
520  *
521  * NB: this display should be just those fields that will not be
522  * reset by RewriteControlFile().
523  */
524 static void
525 PrintControlValues(bool guessed)
526 {
527         char            sysident_str[32];
528
529         if (guessed)
530                 printf(_("Guessed pg_control values:\n\n"));
531         else
532                 printf(_("pg_control values:\n\n"));
533
534         /*
535          * Format system_identifier separately to keep platform-dependent format
536          * code out of the translatable message string.
537          */
538         snprintf(sysident_str, sizeof(sysident_str), UINT64_FORMAT,
539                          ControlFile.system_identifier);
540
541         printf(_("First log file ID for new XLOG:       %u\n"),
542                    newXlogId);
543         printf(_("First log file segment for new XLOG:  %u\n"),
544                    newXlogSeg);
545         printf(_("pg_control version number:            %u\n"),
546                    ControlFile.pg_control_version);
547         printf(_("Catalog version number:               %u\n"),
548                    ControlFile.catalog_version_no);
549         printf(_("Database system identifier:           %s\n"),
550                    sysident_str);
551         printf(_("Latest checkpoint's TimeLineID:       %u\n"),
552                    ControlFile.checkPointCopy.ThisTimeLineID);
553         printf(_("Latest checkpoint's NextXID:          %u/%u\n"),
554                    ControlFile.checkPointCopy.nextXidEpoch,
555                    ControlFile.checkPointCopy.nextXid);
556         printf(_("Latest checkpoint's NextOID:          %u\n"),
557                    ControlFile.checkPointCopy.nextOid);
558         printf(_("Latest checkpoint's NextMultiXactId:  %u\n"),
559                    ControlFile.checkPointCopy.nextMulti);
560         printf(_("Latest checkpoint's NextMultiOffset:  %u\n"),
561                    ControlFile.checkPointCopy.nextMultiOffset);
562         printf(_("Maximum data alignment:               %u\n"),
563                    ControlFile.maxAlign);
564         /* we don't print floatFormat since can't say much useful about it */
565         printf(_("Database block size:                  %u\n"),
566                    ControlFile.blcksz);
567         printf(_("Blocks per segment of large relation: %u\n"),
568                    ControlFile.relseg_size);
569         printf(_("WAL block size:                       %u\n"),
570                    ControlFile.xlog_blcksz);
571         printf(_("Bytes per WAL segment:                %u\n"),
572                    ControlFile.xlog_seg_size);
573         printf(_("Maximum length of identifiers:        %u\n"),
574                    ControlFile.nameDataLen);
575         printf(_("Maximum columns in an index:          %u\n"),
576                    ControlFile.indexMaxKeys);
577         printf(_("Maximum size of a TOAST chunk:        %u\n"),
578                    ControlFile.toast_max_chunk_size);
579         printf(_("Date/time type storage:               %s\n"),
580                    (ControlFile.enableIntTimes ? _("64-bit integers") : _("floating-point numbers")));
581         printf(_("Maximum length of locale name:        %u\n"),
582                    ControlFile.localeBuflen);
583         printf(_("LC_COLLATE:                           %s\n"),
584                    ControlFile.lc_collate);
585         printf(_("LC_CTYPE:                             %s\n"),
586                    ControlFile.lc_ctype);
587 }
588
589
590 /*
591  * Write out the new pg_control file.
592  */
593 static void
594 RewriteControlFile(void)
595 {
596         int                     fd;
597         char            buffer[PG_CONTROL_SIZE];                /* need not be aligned */
598
599         /*
600          * Adjust fields as needed to force an empty XLOG starting at
601          * newXlogId/newXlogSeg.
602          */
603         ControlFile.checkPointCopy.redo.xlogid = newXlogId;
604         ControlFile.checkPointCopy.redo.xrecoff =
605                 newXlogSeg * XLogSegSize + SizeOfXLogLongPHD;
606         ControlFile.checkPointCopy.time = time(NULL);
607
608         ControlFile.state = DB_SHUTDOWNED;
609         ControlFile.time = time(NULL);
610         ControlFile.checkPoint = ControlFile.checkPointCopy.redo;
611         ControlFile.prevCheckPoint.xlogid = 0;
612         ControlFile.prevCheckPoint.xrecoff = 0;
613         ControlFile.minRecoveryPoint.xlogid = 0;
614         ControlFile.minRecoveryPoint.xrecoff = 0;
615
616         /* Now we can force the recorded xlog seg size to the right thing. */
617         ControlFile.xlog_seg_size = XLogSegSize;
618
619         /* Contents are protected with a CRC */
620         INIT_CRC32(ControlFile.crc);
621         COMP_CRC32(ControlFile.crc,
622                            (char *) &ControlFile,
623                            offsetof(ControlFileData, crc));
624         FIN_CRC32(ControlFile.crc);
625
626         /*
627          * We write out PG_CONTROL_SIZE bytes into pg_control, zero-padding the
628          * excess over sizeof(ControlFileData).  This reduces the odds of
629          * premature-EOF errors when reading pg_control.  We'll still fail when we
630          * check the contents of the file, but hopefully with a more specific
631          * error than "couldn't read pg_control".
632          */
633         if (sizeof(ControlFileData) > PG_CONTROL_SIZE)
634         {
635                 fprintf(stderr,
636                                 _("%s: internal error -- sizeof(ControlFileData) is too large ... fix PG_CONTROL_SIZE\n"),
637                                 progname);
638                 exit(1);
639         }
640
641         memset(buffer, 0, PG_CONTROL_SIZE);
642         memcpy(buffer, &ControlFile, sizeof(ControlFileData));
643
644         unlink(XLOG_CONTROL_FILE);
645
646         fd = open(XLOG_CONTROL_FILE,
647                           O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
648                           S_IRUSR | S_IWUSR);
649         if (fd < 0)
650         {
651                 fprintf(stderr, _("%s: could not create pg_control file: %s\n"),
652                                 progname, strerror(errno));
653                 exit(1);
654         }
655
656         errno = 0;
657         if (write(fd, buffer, PG_CONTROL_SIZE) != PG_CONTROL_SIZE)
658         {
659                 /* if write didn't set errno, assume problem is no disk space */
660                 if (errno == 0)
661                         errno = ENOSPC;
662                 fprintf(stderr, _("%s: could not write pg_control file: %s\n"),
663                                 progname, strerror(errno));
664                 exit(1);
665         }
666
667         if (fsync(fd) != 0)
668         {
669                 fprintf(stderr, _("%s: fsync error: %s\n"), progname, strerror(errno));
670                 exit(1);
671         }
672
673         close(fd);
674 }
675
676
677 /*
678  * Scan existing XLOG files and determine the highest existing WAL address
679  *
680  * On entry, ControlFile.checkPointCopy.redo and ControlFile.xlog_seg_size
681  * are assumed valid (note that we allow the old xlog seg size to differ
682  * from what we're using).  On exit, newXlogId and newXlogSeg are set to
683  * suitable values for the beginning of replacement WAL (in our seg size).
684  */
685 static void
686 FindEndOfXLOG(void)
687 {
688         DIR                *xldir;
689         struct dirent *xlde;
690
691         /*
692          * Initialize the max() computation using the last checkpoint address
693          * from old pg_control.  Note that for the moment we are working with
694          * segment numbering according to the old xlog seg size.
695          */
696         newXlogId = ControlFile.checkPointCopy.redo.xlogid;
697         newXlogSeg = ControlFile.checkPointCopy.redo.xrecoff / ControlFile.xlog_seg_size;
698
699         /*
700          * Scan the pg_xlog directory to find existing WAL segment files.
701          * We assume any present have been used; in most scenarios this should
702          * be conservative, because of xlog.c's attempts to pre-create files.
703          */
704         xldir = opendir(XLOGDIR);
705         if (xldir == NULL)
706         {
707                 fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
708                                 progname, XLOGDIR, strerror(errno));
709                 exit(1);
710         }
711
712         errno = 0;
713         while ((xlde = readdir(xldir)) != NULL)
714         {
715                 if (strlen(xlde->d_name) == 24 &&
716                         strspn(xlde->d_name, "0123456789ABCDEF") == 24)
717                 {
718                         unsigned int    tli,
719                                                         log,
720                                                         seg;
721
722                         sscanf(xlde->d_name, "%08X%08X%08X", &tli, &log, &seg);
723                         /*
724                          * Note: we take the max of all files found, regardless of their
725                          * timelines.  Another possibility would be to ignore files of
726                          * timelines other than the target TLI, but this seems safer.
727                          * Better too large a result than too small...
728                          */
729                         if (log > newXlogId ||
730                                 (log == newXlogId && seg > newXlogSeg))
731                         {
732                                 newXlogId = log;
733                                 newXlogSeg = seg;
734                         }
735                 }
736                 errno = 0;
737         }
738 #ifdef WIN32
739
740         /*
741          * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
742          * released version
743          */
744         if (GetLastError() == ERROR_NO_MORE_FILES)
745                 errno = 0;
746 #endif
747
748         if (errno)
749         {
750                 fprintf(stderr, _("%s: could not read from directory \"%s\": %s\n"),
751                                 progname, XLOGDIR, strerror(errno));
752                 exit(1);
753         }
754         closedir(xldir);
755
756         /*
757          * Finally, convert to new xlog seg size, and advance by one to ensure
758          * we are in virgin territory.
759          */
760         newXlogSeg *= ControlFile.xlog_seg_size;
761         newXlogSeg = (newXlogSeg + XLogSegSize - 1) / XLogSegSize;
762
763         /* be sure we wrap around correctly at end of a logfile */
764         NextLogSeg(newXlogId, newXlogSeg);
765 }
766
767
768 /*
769  * Remove existing XLOG files
770  */
771 static void
772 KillExistingXLOG(void)
773 {
774         DIR                *xldir;
775         struct dirent *xlde;
776         char            path[MAXPGPATH];
777
778         xldir = opendir(XLOGDIR);
779         if (xldir == NULL)
780         {
781                 fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
782                                 progname, XLOGDIR, strerror(errno));
783                 exit(1);
784         }
785
786         errno = 0;
787         while ((xlde = readdir(xldir)) != NULL)
788         {
789                 if (strlen(xlde->d_name) == 24 &&
790                         strspn(xlde->d_name, "0123456789ABCDEF") == 24)
791                 {
792                         snprintf(path, MAXPGPATH, "%s/%s", XLOGDIR, xlde->d_name);
793                         if (unlink(path) < 0)
794                         {
795                                 fprintf(stderr, _("%s: could not delete file \"%s\": %s\n"),
796                                                 progname, path, strerror(errno));
797                                 exit(1);
798                         }
799                 }
800                 errno = 0;
801         }
802 #ifdef WIN32
803
804         /*
805          * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
806          * released version
807          */
808         if (GetLastError() == ERROR_NO_MORE_FILES)
809                 errno = 0;
810 #endif
811
812         if (errno)
813         {
814                 fprintf(stderr, _("%s: could not read from directory \"%s\": %s\n"),
815                                 progname, XLOGDIR, strerror(errno));
816                 exit(1);
817         }
818         closedir(xldir);
819 }
820
821
822 /*
823  * Write an empty XLOG file, containing only the checkpoint record
824  * already set up in ControlFile.
825  */
826 static void
827 WriteEmptyXLOG(void)
828 {
829         char       *buffer;
830         XLogPageHeader page;
831         XLogLongPageHeader longpage;
832         XLogRecord *record;
833         pg_crc32        crc;
834         char            path[MAXPGPATH];
835         int                     fd;
836         int                     nbytes;
837
838         /* Use malloc() to ensure buffer is MAXALIGNED */
839         buffer = (char *) malloc(XLOG_BLCKSZ);
840         page = (XLogPageHeader) buffer;
841         memset(buffer, 0, XLOG_BLCKSZ);
842
843         /* Set up the XLOG page header */
844         page->xlp_magic = XLOG_PAGE_MAGIC;
845         page->xlp_info = XLP_LONG_HEADER;
846         page->xlp_tli = ControlFile.checkPointCopy.ThisTimeLineID;
847         page->xlp_pageaddr.xlogid =
848                 ControlFile.checkPointCopy.redo.xlogid;
849         page->xlp_pageaddr.xrecoff =
850                 ControlFile.checkPointCopy.redo.xrecoff - SizeOfXLogLongPHD;
851         longpage = (XLogLongPageHeader) page;
852         longpage->xlp_sysid = ControlFile.system_identifier;
853         longpage->xlp_seg_size = XLogSegSize;
854         longpage->xlp_xlog_blcksz = XLOG_BLCKSZ;
855
856         /* Insert the initial checkpoint record */
857         record = (XLogRecord *) ((char *) page + SizeOfXLogLongPHD);
858         record->xl_prev.xlogid = 0;
859         record->xl_prev.xrecoff = 0;
860         record->xl_xid = InvalidTransactionId;
861         record->xl_tot_len = SizeOfXLogRecord + sizeof(CheckPoint);
862         record->xl_len = sizeof(CheckPoint);
863         record->xl_info = XLOG_CHECKPOINT_SHUTDOWN;
864         record->xl_rmid = RM_XLOG_ID;
865         memcpy(XLogRecGetData(record), &ControlFile.checkPointCopy,
866                    sizeof(CheckPoint));
867
868         INIT_CRC32(crc);
869         COMP_CRC32(crc, &ControlFile.checkPointCopy, sizeof(CheckPoint));
870         COMP_CRC32(crc, (char *) record + sizeof(pg_crc32),
871                            SizeOfXLogRecord - sizeof(pg_crc32));
872         FIN_CRC32(crc);
873         record->xl_crc = crc;
874
875         /* Write the first page */
876         XLogFilePath(path, ControlFile.checkPointCopy.ThisTimeLineID,
877                                  newXlogId, newXlogSeg);
878
879         unlink(path);
880
881         fd = open(path, O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
882                           S_IRUSR | S_IWUSR);
883         if (fd < 0)
884         {
885                 fprintf(stderr, _("%s: could not open file \"%s\": %s\n"),
886                                 progname, path, strerror(errno));
887                 exit(1);
888         }
889
890         errno = 0;
891         if (write(fd, buffer, XLOG_BLCKSZ) != XLOG_BLCKSZ)
892         {
893                 /* if write didn't set errno, assume problem is no disk space */
894                 if (errno == 0)
895                         errno = ENOSPC;
896                 fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
897                                 progname, path, strerror(errno));
898                 exit(1);
899         }
900
901         /* Fill the rest of the file with zeroes */
902         memset(buffer, 0, XLOG_BLCKSZ);
903         for (nbytes = XLOG_BLCKSZ; nbytes < XLogSegSize; nbytes += XLOG_BLCKSZ)
904         {
905                 errno = 0;
906                 if (write(fd, buffer, XLOG_BLCKSZ) != XLOG_BLCKSZ)
907                 {
908                         if (errno == 0)
909                                 errno = ENOSPC;
910                         fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
911                                         progname, path, strerror(errno));
912                         exit(1);
913                 }
914         }
915
916         if (fsync(fd) != 0)
917         {
918                 fprintf(stderr, _("%s: fsync error: %s\n"), progname, strerror(errno));
919                 exit(1);
920         }
921
922         close(fd);
923 }
924
925
926 static void
927 usage(void)
928 {
929         printf(_("%s resets the PostgreSQL transaction log.\n\n"), progname);
930         printf(_("Usage:\n  %s [OPTION]... DATADIR\n\n"), progname);
931         printf(_("Options:\n"));
932         printf(_("  -f              force update to be done\n"));
933         printf(_("  -l TLI,FILE,SEG force minimum WAL starting location for new transaction log\n"));
934         printf(_("  -m XID          set next multitransaction ID\n"));
935         printf(_("  -n              no update, just show extracted control values (for testing)\n"));
936         printf(_("  -o OID          set next OID\n"));
937         printf(_("  -O OFFSET       set next multitransaction offset\n"));
938         printf(_("  -x XID          set next transaction ID\n"));
939         printf(_("  -e XIDEPOCH     set next transaction ID epoch\n"));
940         printf(_("  --help          show this help, then exit\n"));
941         printf(_("  --version       output version information, then exit\n"));
942         printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
943 }