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