]> granicus.if.org Git - postgresql/blob - src/bin/pg_resetxlog/pg_resetxlog.c
Fix pg_resetxlog to use correct path to postmaster.pid.
[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-2012, 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.backupStartPoint = 0;
614         ControlFile.backupEndPoint = 0;
615         ControlFile.backupEndRequired = false;
616
617         /*
618          * Force the defaults for max_* settings. The values don't really matter
619          * as long as wal_level='minimal'; the postmaster will reset these fields
620          * anyway at startup.
621          */
622         ControlFile.wal_level = WAL_LEVEL_MINIMAL;
623         ControlFile.MaxConnections = 100;
624         ControlFile.max_prepared_xacts = 0;
625         ControlFile.max_locks_per_xact = 64;
626
627         /* Now we can force the recorded xlog seg size to the right thing. */
628         ControlFile.xlog_seg_size = XLogSegSize;
629
630         /* Contents are protected with a CRC */
631         INIT_CRC32(ControlFile.crc);
632         COMP_CRC32(ControlFile.crc,
633                            (char *) &ControlFile,
634                            offsetof(ControlFileData, crc));
635         FIN_CRC32(ControlFile.crc);
636
637         /*
638          * We write out PG_CONTROL_SIZE bytes into pg_control, zero-padding the
639          * excess over sizeof(ControlFileData).  This reduces the odds of
640          * premature-EOF errors when reading pg_control.  We'll still fail when we
641          * check the contents of the file, but hopefully with a more specific
642          * error than "couldn't read pg_control".
643          */
644         if (sizeof(ControlFileData) > PG_CONTROL_SIZE)
645         {
646                 fprintf(stderr,
647                                 _("%s: internal error -- sizeof(ControlFileData) is too large ... fix PG_CONTROL_SIZE\n"),
648                                 progname);
649                 exit(1);
650         }
651
652         memset(buffer, 0, PG_CONTROL_SIZE);
653         memcpy(buffer, &ControlFile, sizeof(ControlFileData));
654
655         unlink(XLOG_CONTROL_FILE);
656
657         fd = open(XLOG_CONTROL_FILE,
658                           O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
659                           S_IRUSR | S_IWUSR);
660         if (fd < 0)
661         {
662                 fprintf(stderr, _("%s: could not create pg_control file: %s\n"),
663                                 progname, strerror(errno));
664                 exit(1);
665         }
666
667         errno = 0;
668         if (write(fd, buffer, PG_CONTROL_SIZE) != PG_CONTROL_SIZE)
669         {
670                 /* if write didn't set errno, assume problem is no disk space */
671                 if (errno == 0)
672                         errno = ENOSPC;
673                 fprintf(stderr, _("%s: could not write pg_control file: %s\n"),
674                                 progname, strerror(errno));
675                 exit(1);
676         }
677
678         if (fsync(fd) != 0)
679         {
680                 fprintf(stderr, _("%s: fsync error: %s\n"), progname, strerror(errno));
681                 exit(1);
682         }
683
684         close(fd);
685 }
686
687
688 /*
689  * Scan existing XLOG files and determine the highest existing WAL address
690  *
691  * On entry, ControlFile.checkPointCopy.redo and ControlFile.xlog_seg_size
692  * are assumed valid (note that we allow the old xlog seg size to differ
693  * from what we're using).  On exit, newXlogId and newXlogSeg are set to
694  * suitable values for the beginning of replacement WAL (in our seg size).
695  */
696 static void
697 FindEndOfXLOG(void)
698 {
699         DIR                *xldir;
700         struct dirent *xlde;
701         uint64          segs_per_xlogid;
702         uint64          xlogbytepos;
703
704         /*
705          * Initialize the max() computation using the last checkpoint address from
706          * old pg_control.      Note that for the moment we are working with segment
707          * numbering according to the old xlog seg size.
708          */
709         segs_per_xlogid = (UINT64CONST(0x0000000100000000) / ControlFile.xlog_seg_size);
710         newXlogSegNo = ControlFile.checkPointCopy.redo / ControlFile.xlog_seg_size;
711
712         /*
713          * Scan the pg_xlog directory to find existing WAL segment files. We
714          * assume any present have been used; in most scenarios this should be
715          * conservative, because of xlog.c's attempts to pre-create files.
716          */
717         xldir = opendir(XLOGDIR);
718         if (xldir == NULL)
719         {
720                 fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
721                                 progname, XLOGDIR, strerror(errno));
722                 exit(1);
723         }
724
725         errno = 0;
726         while ((xlde = readdir(xldir)) != NULL)
727         {
728                 if (strlen(xlde->d_name) == 24 &&
729                         strspn(xlde->d_name, "0123456789ABCDEF") == 24)
730                 {
731                         unsigned int tli,
732                                                 log,
733                                                 seg;
734                         XLogSegNo       segno;
735
736                         sscanf(xlde->d_name, "%08X%08X%08X", &tli, &log, &seg);
737                         segno = ((uint64) log) * segs_per_xlogid + seg;
738
739                         /*
740                          * Note: we take the max of all files found, regardless of their
741                          * timelines.  Another possibility would be to ignore files of
742                          * timelines other than the target TLI, but this seems safer.
743                          * Better too large a result than too small...
744                          */
745                         if (segno > newXlogSegNo)
746                                 newXlogSegNo = segno;
747                 }
748                 errno = 0;
749         }
750 #ifdef WIN32
751
752         /*
753          * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
754          * released version
755          */
756         if (GetLastError() == ERROR_NO_MORE_FILES)
757                 errno = 0;
758 #endif
759
760         if (errno)
761         {
762                 fprintf(stderr, _("%s: could not read from directory \"%s\": %s\n"),
763                                 progname, XLOGDIR, strerror(errno));
764                 exit(1);
765         }
766         closedir(xldir);
767
768         /*
769          * Finally, convert to new xlog seg size, and advance by one to ensure we
770          * are in virgin territory.
771          */
772         xlogbytepos = newXlogSegNo * ControlFile.xlog_seg_size;
773         newXlogSegNo = (xlogbytepos + XLogSegSize - 1) / XLogSegSize;
774         newXlogSegNo++;
775 }
776
777
778 /*
779  * Remove existing XLOG files
780  */
781 static void
782 KillExistingXLOG(void)
783 {
784         DIR                *xldir;
785         struct dirent *xlde;
786         char            path[MAXPGPATH];
787
788         xldir = opendir(XLOGDIR);
789         if (xldir == NULL)
790         {
791                 fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
792                                 progname, XLOGDIR, strerror(errno));
793                 exit(1);
794         }
795
796         errno = 0;
797         while ((xlde = readdir(xldir)) != NULL)
798         {
799                 if (strlen(xlde->d_name) == 24 &&
800                         strspn(xlde->d_name, "0123456789ABCDEF") == 24)
801                 {
802                         snprintf(path, MAXPGPATH, "%s/%s", XLOGDIR, xlde->d_name);
803                         if (unlink(path) < 0)
804                         {
805                                 fprintf(stderr, _("%s: could not delete file \"%s\": %s\n"),
806                                                 progname, path, strerror(errno));
807                                 exit(1);
808                         }
809                 }
810                 errno = 0;
811         }
812 #ifdef WIN32
813
814         /*
815          * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
816          * released version
817          */
818         if (GetLastError() == ERROR_NO_MORE_FILES)
819                 errno = 0;
820 #endif
821
822         if (errno)
823         {
824                 fprintf(stderr, _("%s: could not read from directory \"%s\": %s\n"),
825                                 progname, XLOGDIR, strerror(errno));
826                 exit(1);
827         }
828         closedir(xldir);
829 }
830
831
832 /*
833  * Remove existing archive status files
834  */
835 static void
836 KillExistingArchiveStatus(void)
837 {
838         DIR                *xldir;
839         struct dirent *xlde;
840         char            path[MAXPGPATH];
841
842 #define ARCHSTATDIR XLOGDIR "/archive_status"
843
844         xldir = opendir(ARCHSTATDIR);
845         if (xldir == NULL)
846         {
847                 fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
848                                 progname, ARCHSTATDIR, strerror(errno));
849                 exit(1);
850         }
851
852         errno = 0;
853         while ((xlde = readdir(xldir)) != NULL)
854         {
855                 if (strspn(xlde->d_name, "0123456789ABCDEF") == 24 &&
856                         (strcmp(xlde->d_name + 24, ".ready") == 0 ||
857                          strcmp(xlde->d_name + 24, ".done") == 0))
858                 {
859                         snprintf(path, MAXPGPATH, "%s/%s", ARCHSTATDIR, xlde->d_name);
860                         if (unlink(path) < 0)
861                         {
862                                 fprintf(stderr, _("%s: could not delete file \"%s\": %s\n"),
863                                                 progname, path, strerror(errno));
864                                 exit(1);
865                         }
866                 }
867                 errno = 0;
868         }
869 #ifdef WIN32
870
871         /*
872          * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
873          * released version
874          */
875         if (GetLastError() == ERROR_NO_MORE_FILES)
876                 errno = 0;
877 #endif
878
879         if (errno)
880         {
881                 fprintf(stderr, _("%s: could not read from directory \"%s\": %s\n"),
882                                 progname, ARCHSTATDIR, strerror(errno));
883                 exit(1);
884         }
885         closedir(xldir);
886 }
887
888
889 /*
890  * Write an empty XLOG file, containing only the checkpoint record
891  * already set up in ControlFile.
892  */
893 static void
894 WriteEmptyXLOG(void)
895 {
896         char       *buffer;
897         XLogPageHeader page;
898         XLogLongPageHeader longpage;
899         XLogRecord *record;
900         pg_crc32        crc;
901         char            path[MAXPGPATH];
902         int                     fd;
903         int                     nbytes;
904
905         /* Use malloc() to ensure buffer is MAXALIGNED */
906         buffer = (char *) malloc(XLOG_BLCKSZ);
907         page = (XLogPageHeader) buffer;
908         memset(buffer, 0, XLOG_BLCKSZ);
909
910         /* Set up the XLOG page header */
911         page->xlp_magic = XLOG_PAGE_MAGIC;
912         page->xlp_info = XLP_LONG_HEADER;
913         page->xlp_tli = ControlFile.checkPointCopy.ThisTimeLineID;
914         page->xlp_pageaddr = ControlFile.checkPointCopy.redo - SizeOfXLogLongPHD;
915         longpage = (XLogLongPageHeader) page;
916         longpage->xlp_sysid = ControlFile.system_identifier;
917         longpage->xlp_seg_size = XLogSegSize;
918         longpage->xlp_xlog_blcksz = XLOG_BLCKSZ;
919
920         /* Insert the initial checkpoint record */
921         record = (XLogRecord *) ((char *) page + SizeOfXLogLongPHD);
922         record->xl_prev = 0;
923         record->xl_xid = InvalidTransactionId;
924         record->xl_tot_len = SizeOfXLogRecord + sizeof(CheckPoint);
925         record->xl_len = sizeof(CheckPoint);
926         record->xl_info = XLOG_CHECKPOINT_SHUTDOWN;
927         record->xl_rmid = RM_XLOG_ID;
928         memcpy(XLogRecGetData(record), &ControlFile.checkPointCopy,
929                    sizeof(CheckPoint));
930
931         INIT_CRC32(crc);
932         COMP_CRC32(crc, &ControlFile.checkPointCopy, sizeof(CheckPoint));
933         COMP_CRC32(crc, (char *) record, offsetof(XLogRecord, xl_crc));
934         FIN_CRC32(crc);
935         record->xl_crc = crc;
936
937         /* Write the first page */
938         XLogFilePath(path, ControlFile.checkPointCopy.ThisTimeLineID, newXlogSegNo);
939
940         unlink(path);
941
942         fd = open(path, O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
943                           S_IRUSR | S_IWUSR);
944         if (fd < 0)
945         {
946                 fprintf(stderr, _("%s: could not open file \"%s\": %s\n"),
947                                 progname, path, strerror(errno));
948                 exit(1);
949         }
950
951         errno = 0;
952         if (write(fd, buffer, XLOG_BLCKSZ) != XLOG_BLCKSZ)
953         {
954                 /* if write didn't set errno, assume problem is no disk space */
955                 if (errno == 0)
956                         errno = ENOSPC;
957                 fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
958                                 progname, path, strerror(errno));
959                 exit(1);
960         }
961
962         /* Fill the rest of the file with zeroes */
963         memset(buffer, 0, XLOG_BLCKSZ);
964         for (nbytes = XLOG_BLCKSZ; nbytes < XLogSegSize; nbytes += XLOG_BLCKSZ)
965         {
966                 errno = 0;
967                 if (write(fd, buffer, XLOG_BLCKSZ) != XLOG_BLCKSZ)
968                 {
969                         if (errno == 0)
970                                 errno = ENOSPC;
971                         fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
972                                         progname, path, strerror(errno));
973                         exit(1);
974                 }
975         }
976
977         if (fsync(fd) != 0)
978         {
979                 fprintf(stderr, _("%s: fsync error: %s\n"), progname, strerror(errno));
980                 exit(1);
981         }
982
983         close(fd);
984 }
985
986
987 static void
988 usage(void)
989 {
990         printf(_("%s resets the PostgreSQL transaction log.\n\n"), progname);
991         printf(_("Usage:\n  %s [OPTION]... DATADIR\n\n"), progname);
992         printf(_("Options:\n"));
993         printf(_("  -e XIDEPOCH      set next transaction ID epoch\n"));
994         printf(_("  -f               force update to be done\n"));
995         printf(_("  -l xlogfile      force minimum WAL starting location for new transaction log\n"));
996         printf(_("  -m XID           set next multitransaction ID\n"));
997         printf(_("  -n               no update, just show extracted control values (for testing)\n"));
998         printf(_("  -o OID           set next OID\n"));
999         printf(_("  -O OFFSET        set next multitransaction offset\n"));
1000         printf(_("  -V, --version    output version information, then exit\n"));
1001         printf(_("  -x XID           set next transaction ID\n"));
1002         printf(_("  -?, --help       show this help, then exit\n"));
1003         printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
1004 }