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