]> granicus.if.org Git - postgresql/blob - src/bin/pg_resetxlog/pg_resetxlog.c
Change WAL-logging scheme for multixacts to be more like regular
[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-2005, 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.34 2005/06/08 15:50:27 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/multixact.h"
44 #include "access/xlog.h"
45 #include "access/xlog_internal.h"
46 #include "catalog/catversion.h"
47 #include "catalog/pg_control.h"
48
49 extern int      optind;
50 extern char *optarg;
51
52
53 char            XLogDir[MAXPGPATH]; /* not static, see xlog_internal.h */
54 static char ControlFilePath[MAXPGPATH];
55
56 static ControlFileData ControlFile;             /* pg_control values */
57 static uint32 newXlogId,
58                         newXlogSeg;                     /* ID/Segment of new XLOG segment */
59 static bool guessed = false;    /* T if we had to guess at any values */
60 static const char *progname;
61
62 static bool ReadControlFile(void);
63 static void GuessControlValues(void);
64 static void PrintControlValues(bool guessed);
65 static void RewriteControlFile(void);
66 static void KillExistingXLOG(void);
67 static void WriteEmptyXLOG(void);
68 static void usage(void);
69
70
71 int
72 main(int argc, char *argv[])
73 {
74         int                     c;
75         bool            force = false;
76         bool            noupdate = false;
77         TransactionId set_xid = 0;
78         Oid                     set_oid = 0;
79         MultiXactId     set_mxid = 0;
80         MultiXactOffset set_mxoff = -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:")) != -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 'x':
123                                 set_xid = strtoul(optarg, &endptr, 0);
124                                 if (endptr == optarg || *endptr != '\0')
125                                 {
126                                         fprintf(stderr, _("%s: invalid argument for option -x\n"), progname);
127                                         fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
128                                         exit(1);
129                                 }
130                                 if (set_xid == 0)
131                                 {
132                                         fprintf(stderr, _("%s: transaction ID (-x) must not be 0\n"), progname);
133                                         exit(1);
134                                 }
135                                 break;
136
137                         case 'o':
138                                 set_oid = strtoul(optarg, &endptr, 0);
139                                 if (endptr == optarg || *endptr != '\0')
140                                 {
141                                         fprintf(stderr, _("%s: invalid argument for option -o\n"), progname);
142                                         fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
143                                         exit(1);
144                                 }
145                                 if (set_oid == 0)
146                                 {
147                                         fprintf(stderr, _("%s: OID (-o) must not be 0\n"), progname);
148                                         exit(1);
149                                 }
150                                 break;
151
152                         case 'm':
153                                 set_mxid = strtoul(optarg, &endptr, 0);
154                                 if (endptr == optarg || *endptr != '\0')
155                                 {
156                                         fprintf(stderr, _("%s: invalid argument for option -m\n"), progname);
157                                         fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
158                                         exit(1);
159                                 }
160                                 if (set_mxid == 0)
161                                 {
162                                         fprintf(stderr, _("%s: multi transaction ID (-m) must not be 0\n"), progname);
163                                         exit(1);
164                                 }
165                                 break;
166
167                         case 'O':
168                                 set_mxoff = strtoul(optarg, &endptr, 0);
169                                 if (endptr == optarg || *endptr != '\0')
170                                 {
171                                         fprintf(stderr, _("%s: invalid argument for option -O\n"), progname);
172                                         fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
173                                         exit(1);
174                                 }
175                                 if (set_mxoff == -1)
176                                 {
177                                         fprintf(stderr, _("%s: multi transaction offset (-O) must not be -1\n"), progname);
178                                         exit(1);
179                                 }
180                                 break;
181
182                         case 'l':
183                                 minXlogTli = strtoul(optarg, &endptr, 0);
184                                 if (endptr == optarg || *endptr != ',')
185                                 {
186                                         fprintf(stderr, _("%s: invalid argument for option -l\n"), progname);
187                                         fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
188                                         exit(1);
189                                 }
190                                 minXlogId = strtoul(endptr + 1, &endptr2, 0);
191                                 if (endptr2 == endptr + 1 || *endptr2 != ',')
192                                 {
193                                         fprintf(stderr, _("%s: invalid argument for option -l\n"), progname);
194                                         fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
195                                         exit(1);
196                                 }
197                                 minXlogSeg = strtoul(endptr2 + 1, &endptr3, 0);
198                                 if (endptr3 == endptr2 + 1 || *endptr3 != '\0')
199                                 {
200                                         fprintf(stderr, _("%s: invalid argument for option -l\n"), progname);
201                                         fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
202                                         exit(1);
203                                 }
204                                 break;
205
206                         default:
207                                 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
208                                 exit(1);
209                 }
210         }
211
212         if (optind == argc)
213         {
214                 fprintf(stderr, _("%s: no data directory specified\n"), progname);
215                 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
216                 exit(1);
217         }
218
219         /*
220          * Don't allow pg_resetxlog to be run as root, to avoid
221          * overwriting the ownership of files in the data directory. We
222          * need only check for root -- any other user won't have
223          * sufficient permissions to modify files in the data directory.
224          */
225 #ifndef WIN32
226 #ifndef __BEOS__                                /* no root check on BeOS */
227         if (geteuid() == 0)
228         {
229                 fprintf(stderr, _("%s: cannot be executed by \"root\"\n"),
230                                 progname);
231                 fprintf(stderr, _("You must run %s as the PostgreSQL superuser.\n"),
232                                 progname);
233                 exit(1);
234         }
235 #endif
236 #endif
237
238         DataDir = argv[optind];
239         snprintf(XLogDir, MAXPGPATH, "%s/pg_xlog", DataDir);
240         snprintf(ControlFilePath, MAXPGPATH, "%s/global/pg_control", DataDir);
241
242         /*
243          * Check for a postmaster lock file --- if there is one, refuse to
244          * proceed, on grounds we might be interfering with a live
245          * installation.
246          */
247         snprintf(path, MAXPGPATH, "%s/postmaster.pid", DataDir);
248
249         if ((fd = open(path, O_RDONLY)) < 0)
250         {
251                 if (errno != ENOENT)
252                 {
253                         fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"), progname, path, strerror(errno));
254                         exit(1);
255                 }
256         }
257         else
258         {
259                 fprintf(stderr, _("%s: lock file \"%s\" exists\n"
260                                                   "Is a server running?  If not, delete the lock file and try again.\n"),
261                                 progname, path);
262                 exit(1);
263         }
264
265         /*
266          * Attempt to read the existing pg_control file
267          */
268         if (!ReadControlFile())
269                 GuessControlValues();
270
271         /*
272          * Adjust fields if required by switches.  (Do this now so that
273          * printout, if any, includes these values.)
274          */
275         if (set_xid != 0)
276                 ControlFile.checkPointCopy.nextXid = set_xid;
277
278         if (set_oid != 0)
279                 ControlFile.checkPointCopy.nextOid = set_oid;
280
281         if (set_mxid != 0)
282                 ControlFile.checkPointCopy.nextMulti = set_mxid;
283
284         if (set_mxoff != -1)
285                 ControlFile.checkPointCopy.nextMultiOffset = set_mxoff;
286
287         if (minXlogTli > ControlFile.checkPointCopy.ThisTimeLineID)
288                 ControlFile.checkPointCopy.ThisTimeLineID = minXlogTli;
289
290         if (minXlogId > ControlFile.logId ||
291                 (minXlogId == ControlFile.logId &&
292                  minXlogSeg > ControlFile.logSeg))
293         {
294                 ControlFile.logId = minXlogId;
295                 ControlFile.logSeg = minXlogSeg;
296         }
297
298         /*
299          * If we had to guess anything, and -f was not given, just print the
300          * guessed values and exit.  Also print if -n is given.
301          */
302         if ((guessed && !force) || noupdate)
303         {
304                 PrintControlValues(guessed);
305                 if (!noupdate)
306                 {
307                         printf(_("\nIf these values seem acceptable, use -f to force reset.\n"));
308                         exit(1);
309                 }
310                 else
311                         exit(0);
312         }
313
314         /*
315          * Don't reset from a dirty pg_control without -f, either.
316          */
317         if (ControlFile.state != DB_SHUTDOWNED && !force)
318         {
319                 printf(_("The database server was not shut down cleanly.\n"
320                          "Resetting the transaction log may cause data to be lost.\n"
321                          "If you want to proceed anyway, use -f to force reset.\n"));
322                 exit(1);
323         }
324
325         /*
326          * Else, do the dirty deed.
327          */
328         RewriteControlFile();
329         KillExistingXLOG();
330         WriteEmptyXLOG();
331
332         printf(_("Transaction log reset\n"));
333         return 0;
334 }
335
336
337 /*
338  * Try to read the existing pg_control file.
339  *
340  * This routine is also responsible for updating old pg_control versions
341  * to the current format.  (Currently we don't do anything of the sort.)
342  */
343 static bool
344 ReadControlFile(void)
345 {
346         int                     fd;
347         int                     len;
348         char       *buffer;
349         pg_crc32        crc;
350
351         if ((fd = open(ControlFilePath, O_RDONLY)) < 0)
352         {
353                 /*
354                  * If pg_control is not there at all, or we can't read it, the
355                  * odds are we've been handed a bad DataDir path, so give up. User
356                  * can do "touch pg_control" to force us to proceed.
357                  */
358                 fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
359                                 progname, ControlFilePath, strerror(errno));
360                 if (errno == ENOENT)
361                         fprintf(stderr, _("If you are sure the data directory path is correct, execute\n"
362                                                           "  touch %s\n"
363                                                           "and try again.\n"),
364                                         ControlFilePath);
365                 exit(1);
366         }
367
368         /* Use malloc to ensure we have a maxaligned buffer */
369         buffer = (char *) malloc(BLCKSZ);
370
371         len = read(fd, buffer, BLCKSZ);
372         if (len < 0)
373         {
374                 fprintf(stderr, _("%s: could not read file \"%s\": %s\n"),
375                                 progname, ControlFilePath, strerror(errno));
376                 exit(1);
377         }
378         close(fd);
379
380         if (len >= sizeof(ControlFileData) &&
381                 ((ControlFileData *) buffer)->pg_control_version == PG_CONTROL_VERSION)
382         {
383                 /* Check the CRC. */
384                 INIT_CRC32(crc);
385                 COMP_CRC32(crc,
386                                    buffer,
387                                    offsetof(ControlFileData, crc));
388                 FIN_CRC32(crc);
389
390                 if (EQ_CRC32(crc, ((ControlFileData *) buffer)->crc))
391                 {
392                         /* Valid data... */
393                         memcpy(&ControlFile, buffer, sizeof(ControlFile));
394                         return true;
395                 }
396
397                 fprintf(stderr, _("%s: pg_control exists but has invalid CRC; proceed with caution\n"),
398                                 progname);
399                 /* We will use the data anyway, but treat it as guessed. */
400                 memcpy(&ControlFile, buffer, sizeof(ControlFile));
401                 guessed = true;
402                 return true;
403         }
404
405         /* Looks like it's a mess. */
406         fprintf(stderr, _("%s: pg_control exists but is broken or unknown version; ignoring it\n"),
407                         progname);
408         return false;
409 }
410
411
412 /*
413  * Guess at pg_control values when we can't read the old ones.
414  */
415 static void
416 GuessControlValues(void)
417 {
418         uint64          sysidentifier;
419         struct timeval tv;
420         char       *localeptr;
421
422         /*
423          * Set up a completely default set of pg_control values.
424          */
425         guessed = true;
426         memset(&ControlFile, 0, sizeof(ControlFile));
427
428         ControlFile.pg_control_version = PG_CONTROL_VERSION;
429         ControlFile.catalog_version_no = CATALOG_VERSION_NO;
430
431         /*
432          * Create a new unique installation identifier, since we can no longer
433          * use any old XLOG records.  See notes in xlog.c about the algorithm.
434          */
435         gettimeofday(&tv, NULL);
436         sysidentifier = ((uint64) tv.tv_sec) << 32;
437         sysidentifier |= (uint32) (tv.tv_sec | tv.tv_usec);
438
439         ControlFile.system_identifier = sysidentifier;
440
441         ControlFile.checkPointCopy.redo.xlogid = 0;
442         ControlFile.checkPointCopy.redo.xrecoff = SizeOfXLogLongPHD;
443         ControlFile.checkPointCopy.undo = ControlFile.checkPointCopy.redo;
444         ControlFile.checkPointCopy.ThisTimeLineID = 1;
445         ControlFile.checkPointCopy.nextXid = (TransactionId) 514;       /* XXX */
446         ControlFile.checkPointCopy.nextOid = FirstBootstrapObjectId;
447         ControlFile.checkPointCopy.nextMulti = FirstMultiXactId;
448         ControlFile.checkPointCopy.nextMultiOffset = 0;
449         ControlFile.checkPointCopy.time = time(NULL);
450
451         ControlFile.state = DB_SHUTDOWNED;
452         ControlFile.time = time(NULL);
453         ControlFile.logId = 0;
454         ControlFile.logSeg = 1;
455         ControlFile.checkPoint = ControlFile.checkPointCopy.redo;
456
457         ControlFile.blcksz = BLCKSZ;
458         ControlFile.relseg_size = RELSEG_SIZE;
459         ControlFile.xlog_seg_size = XLOG_SEG_SIZE;
460         ControlFile.nameDataLen = NAMEDATALEN;
461         ControlFile.indexMaxKeys = INDEX_MAX_KEYS;
462 #ifdef HAVE_INT64_TIMESTAMP
463         ControlFile.enableIntTimes = TRUE;
464 #else
465         ControlFile.enableIntTimes = FALSE;
466 #endif
467         ControlFile.localeBuflen = LOCALE_NAME_BUFLEN;
468
469         localeptr = setlocale(LC_COLLATE, "");
470         if (!localeptr)
471         {
472                 fprintf(stderr, _("%s: invalid LC_COLLATE setting\n"), progname);
473                 exit(1);
474         }
475         StrNCpy(ControlFile.lc_collate, localeptr, LOCALE_NAME_BUFLEN);
476         localeptr = setlocale(LC_CTYPE, "");
477         if (!localeptr)
478         {
479                 fprintf(stderr, _("%s: invalid LC_CTYPE setting\n"), progname);
480                 exit(1);
481         }
482         StrNCpy(ControlFile.lc_ctype, localeptr, LOCALE_NAME_BUFLEN);
483
484         /*
485          * XXX eventually, should try to grovel through old XLOG to develop
486          * more accurate values for TimeLineID, nextXID, etc.
487          */
488 }
489
490
491 /*
492  * Print the guessed pg_control values when we had to guess.
493  *
494  * NB: this display should be just those fields that will not be
495  * reset by RewriteControlFile().
496  */
497 static void
498 PrintControlValues(bool guessed)
499 {
500         char            sysident_str[32];
501
502         if (guessed)
503                 printf(_("Guessed pg_control values:\n\n"));
504         else
505                 printf(_("pg_control values:\n\n"));
506
507         /*
508          * Format system_identifier separately to keep platform-dependent
509          * format code out of the translatable message string.
510          */
511         snprintf(sysident_str, sizeof(sysident_str), UINT64_FORMAT,
512                          ControlFile.system_identifier);
513
514         printf(_("pg_control version number:            %u\n"), ControlFile.pg_control_version);
515         printf(_("Catalog version number:               %u\n"), ControlFile.catalog_version_no);
516         printf(_("Database system identifier:           %s\n"), sysident_str);
517         printf(_("Current log file ID:                  %u\n"), ControlFile.logId);
518         printf(_("Next log file segment:                %u\n"), ControlFile.logSeg);
519         printf(_("Latest checkpoint's TimeLineID:       %u\n"), ControlFile.checkPointCopy.ThisTimeLineID);
520         printf(_("Latest checkpoint's NextXID:          %u\n"), ControlFile.checkPointCopy.nextXid);
521         printf(_("Latest checkpoint's NextOID:          %u\n"), ControlFile.checkPointCopy.nextOid);
522         printf(_("Latest checkpoint's NextMultiXactId:  %u\n"), ControlFile.checkPointCopy.nextMulti);
523         printf(_("Latest checkpoint's NextMultiOffset:  %u\n"), ControlFile.checkPointCopy.nextMultiOffset);
524         printf(_("Database block size:                  %u\n"), ControlFile.blcksz);
525         printf(_("Blocks per segment of large relation: %u\n"), ControlFile.relseg_size);
526         printf(_("Maximum length of identifiers:        %u\n"), ControlFile.nameDataLen);
527         printf(_("Maximum columns in an index:          %u\n"), ControlFile.indexMaxKeys);
528         printf(_("Date/time type storage:               %s\n"),
529                    (ControlFile.enableIntTimes ? _("64-bit integers") : _("floating-point numbers")));
530         printf(_("Maximum length of locale name:        %u\n"), ControlFile.localeBuflen);
531         printf(_("LC_COLLATE:                           %s\n"), ControlFile.lc_collate);
532         printf(_("LC_CTYPE:                             %s\n"), ControlFile.lc_ctype);
533 }
534
535
536 /*
537  * Write out the new pg_control file.
538  */
539 static void
540 RewriteControlFile(void)
541 {
542         int                     fd;
543         char            buffer[BLCKSZ]; /* need not be aligned */
544
545         /*
546          * Adjust fields as needed to force an empty XLOG starting at the next
547          * available segment.
548          */
549         newXlogId = ControlFile.logId;
550         newXlogSeg = ControlFile.logSeg;
551
552         /* adjust in case we are changing segment size */
553         newXlogSeg *= ControlFile.xlog_seg_size;
554         newXlogSeg = (newXlogSeg + XLogSegSize - 1) / XLogSegSize;
555
556         /* be sure we wrap around correctly at end of a logfile */
557         NextLogSeg(newXlogId, newXlogSeg);
558
559         /* Now we can force the recorded xlog seg size to the right thing. */
560         ControlFile.xlog_seg_size = XLogSegSize;
561
562         ControlFile.checkPointCopy.redo.xlogid = newXlogId;
563         ControlFile.checkPointCopy.redo.xrecoff =
564                 newXlogSeg * XLogSegSize + SizeOfXLogLongPHD;
565         ControlFile.checkPointCopy.undo = ControlFile.checkPointCopy.redo;
566         ControlFile.checkPointCopy.time = time(NULL);
567
568         ControlFile.state = DB_SHUTDOWNED;
569         ControlFile.time = time(NULL);
570         ControlFile.logId = newXlogId;
571         ControlFile.logSeg = newXlogSeg + 1;
572         ControlFile.checkPoint = ControlFile.checkPointCopy.redo;
573         ControlFile.prevCheckPoint.xlogid = 0;
574         ControlFile.prevCheckPoint.xrecoff = 0;
575
576         /* Contents are protected with a CRC */
577         INIT_CRC32(ControlFile.crc);
578         COMP_CRC32(ControlFile.crc,
579                            (char *) &ControlFile,
580                            offsetof(ControlFileData, crc));
581         FIN_CRC32(ControlFile.crc);
582
583         /*
584          * We write out BLCKSZ bytes into pg_control, zero-padding the excess
585          * over sizeof(ControlFileData).  This reduces the odds of
586          * premature-EOF errors when reading pg_control.  We'll still fail
587          * when we check the contents of the file, but hopefully with a more
588          * specific error than "couldn't read pg_control".
589          */
590         if (sizeof(ControlFileData) > BLCKSZ)
591         {
592                 fprintf(stderr,
593                                 _("%s: internal error -- sizeof(ControlFileData) is too large ... fix xlog.c\n"),
594                                 progname);
595                 exit(1);
596         }
597
598         memset(buffer, 0, BLCKSZ);
599         memcpy(buffer, &ControlFile, sizeof(ControlFileData));
600
601         unlink(ControlFilePath);
602
603         fd = open(ControlFilePath, O_RDWR | O_CREAT | O_EXCL | PG_BINARY, S_IRUSR | S_IWUSR);
604         if (fd < 0)
605         {
606                 fprintf(stderr, _("%s: could not create pg_control file: %s\n"),
607                                 progname, strerror(errno));
608                 exit(1);
609         }
610
611         errno = 0;
612         if (write(fd, buffer, BLCKSZ) != BLCKSZ)
613         {
614                 /* if write didn't set errno, assume problem is no disk space */
615                 if (errno == 0)
616                         errno = ENOSPC;
617                 fprintf(stderr, _("%s: could not write pg_control file: %s\n"),
618                                 progname, strerror(errno));
619                 exit(1);
620         }
621
622         if (fsync(fd) != 0)
623         {
624                 fprintf(stderr, _("%s: fsync error: %s\n"), progname, strerror(errno));
625                 exit(1);
626         }
627
628         close(fd);
629 }
630
631
632 /*
633  * Remove existing XLOG files
634  */
635 static void
636 KillExistingXLOG(void)
637 {
638         DIR                *xldir;
639         struct dirent *xlde;
640         char            path[MAXPGPATH];
641
642         xldir = opendir(XLogDir);
643         if (xldir == NULL)
644         {
645                 fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
646                                 progname, XLogDir, strerror(errno));
647                 exit(1);
648         }
649
650         errno = 0;
651         while ((xlde = readdir(xldir)) != NULL)
652         {
653                 if (strlen(xlde->d_name) == 24 &&
654                         strspn(xlde->d_name, "0123456789ABCDEF") == 24)
655                 {
656                         snprintf(path, MAXPGPATH, "%s/%s", XLogDir, xlde->d_name);
657                         if (unlink(path) < 0)
658                         {
659                                 fprintf(stderr, _("%s: could not delete file \"%s\": %s\n"),
660                                                 progname, path, strerror(errno));
661                                 exit(1);
662                         }
663                 }
664                 errno = 0;
665         }
666 #ifdef WIN32
667
668         /*
669          * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but
670          * not in released version
671          */
672         if (GetLastError() == ERROR_NO_MORE_FILES)
673                 errno = 0;
674 #endif
675
676         if (errno)
677         {
678                 fprintf(stderr, _("%s: could not read from directory \"%s\": %s\n"),
679                                 progname, XLogDir, strerror(errno));
680                 exit(1);
681         }
682         closedir(xldir);
683 }
684
685
686 /*
687  * Write an empty XLOG file, containing only the checkpoint record
688  * already set up in ControlFile.
689  */
690 static void
691 WriteEmptyXLOG(void)
692 {
693         char       *buffer;
694         XLogPageHeader page;
695         XLogLongPageHeader longpage;
696         XLogRecord *record;
697         pg_crc32        crc;
698         char            path[MAXPGPATH];
699         int                     fd;
700         int                     nbytes;
701
702         /* Use malloc() to ensure buffer is MAXALIGNED */
703         buffer = (char *) malloc(BLCKSZ);
704         page = (XLogPageHeader) buffer;
705         memset(buffer, 0, BLCKSZ);
706
707         /* Set up the XLOG page header */
708         page->xlp_magic = XLOG_PAGE_MAGIC;
709         page->xlp_info = XLP_LONG_HEADER;
710         page->xlp_tli = ControlFile.checkPointCopy.ThisTimeLineID;
711         page->xlp_pageaddr.xlogid =
712                 ControlFile.checkPointCopy.redo.xlogid;
713         page->xlp_pageaddr.xrecoff =
714                 ControlFile.checkPointCopy.redo.xrecoff - SizeOfXLogLongPHD;
715         longpage = (XLogLongPageHeader) page;
716         longpage->xlp_sysid = ControlFile.system_identifier;
717         longpage->xlp_seg_size = XLogSegSize;
718
719         /* Insert the initial checkpoint record */
720         record = (XLogRecord *) ((char *) page + SizeOfXLogLongPHD);
721         record->xl_prev.xlogid = 0;
722         record->xl_prev.xrecoff = 0;
723         record->xl_xid = InvalidTransactionId;
724         record->xl_tot_len = SizeOfXLogRecord + sizeof(CheckPoint);
725         record->xl_len = sizeof(CheckPoint);
726         record->xl_info = XLOG_CHECKPOINT_SHUTDOWN;
727         record->xl_rmid = RM_XLOG_ID;
728         memcpy(XLogRecGetData(record), &ControlFile.checkPointCopy,
729                    sizeof(CheckPoint));
730
731         INIT_CRC32(crc);
732         COMP_CRC32(crc, &ControlFile.checkPointCopy, sizeof(CheckPoint));
733         COMP_CRC32(crc, (char *) record + sizeof(pg_crc32),
734                            SizeOfXLogRecord - sizeof(pg_crc32));
735         FIN_CRC32(crc);
736         record->xl_crc = crc;
737
738         /* Write the first page */
739         XLogFilePath(path, ControlFile.checkPointCopy.ThisTimeLineID,
740                                  newXlogId, newXlogSeg);
741
742         unlink(path);
743
744         fd = open(path, O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
745                           S_IRUSR | S_IWUSR);
746         if (fd < 0)
747         {
748                 fprintf(stderr, _("%s: could not open file \"%s\": %s\n"),
749                                 progname, path, strerror(errno));
750                 exit(1);
751         }
752
753         errno = 0;
754         if (write(fd, buffer, BLCKSZ) != BLCKSZ)
755         {
756                 /* if write didn't set errno, assume problem is no disk space */
757                 if (errno == 0)
758                         errno = ENOSPC;
759                 fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
760                                 progname, path, strerror(errno));
761                 exit(1);
762         }
763
764         /* Fill the rest of the file with zeroes */
765         memset(buffer, 0, BLCKSZ);
766         for (nbytes = BLCKSZ; nbytes < XLogSegSize; nbytes += BLCKSZ)
767         {
768                 errno = 0;
769                 if (write(fd, buffer, BLCKSZ) != BLCKSZ)
770                 {
771                         if (errno == 0)
772                                 errno = ENOSPC;
773                         fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
774                                         progname, path, strerror(errno));
775                         exit(1);
776                 }
777         }
778
779         if (fsync(fd) != 0)
780         {
781                 fprintf(stderr, _("%s: fsync error: %s\n"), progname, strerror(errno));
782                 exit(1);
783         }
784
785         close(fd);
786 }
787
788
789 static void
790 usage(void)
791 {
792         printf(_("%s resets the PostgreSQL transaction log.\n\n"), progname);
793         printf(_("Usage:\n  %s [OPTION]... DATADIR\n\n"), progname);
794         printf(_("Options:\n"));
795         printf(_("  -f              force update to be done\n"));
796         printf(_("  -l TLI,FILE,SEG force minimum WAL starting location for new transaction log\n"));
797         printf(_("  -n              no update, just show extracted control values (for testing)\n"));
798         printf(_("  -o OID          set next OID\n"));
799         printf(_("  -x XID          set next transaction ID\n"));
800         printf(_("  -m multiXID     set next multi transaction ID\n"));
801         printf(_("  -O multiOffset  set next multi transaction offset\n"));
802         printf(_("  --help          show this help, then exit\n"));
803         printf(_("  --version       output version information, then exit\n"));
804         printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
805 }