1 /*-------------------------------------------------------------------------
4 * A utility to "zero out" the xlog when it's corrupt beyond recovery.
5 * Can also rebuild pg_control if needed.
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
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
23 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
24 * Portions Copyright (c) 1994, Regents of the University of California
28 *-------------------------------------------------------------------------
43 #include "access/transam.h"
44 #include "access/tuptoaster.h"
45 #include "access/multixact.h"
46 #include "access/xlog_internal.h"
47 #include "catalog/catversion.h"
48 #include "catalog/pg_control.h"
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
;
60 static bool ReadControlFile(void);
61 static void GuessControlValues(void);
62 static void PrintControlValues(bool guessed
);
63 static void RewriteControlFile(void);
64 static void FindEndOfXLOG(void);
65 static void KillExistingXLOG(void);
66 static void WriteEmptyXLOG(void);
67 static void usage(void);
71 main(int argc
, char *argv
[])
75 bool noupdate
= false;
76 uint32 set_xid_epoch
= (uint32
) -1;
77 TransactionId set_xid
= 0;
79 MultiXactId set_mxid
= 0;
80 MultiXactOffset set_mxoff
= (MultiXactOffset
) -1;
81 uint32 minXlogTli
= 0,
91 set_pglocale_pgservice(argv
[0], "pg_resetxlog");
93 progname
= get_progname(argv
[0]);
97 if (strcmp(argv
[1], "--help") == 0 || strcmp(argv
[1], "-?") == 0)
102 if (strcmp(argv
[1], "--version") == 0 || strcmp(argv
[1], "-V") == 0)
104 puts("pg_resetxlog (PostgreSQL) " PG_VERSION
);
110 while ((c
= getopt(argc
, argv
, "fl:m:no:O:x:e:")) != -1)
123 set_xid_epoch
= strtoul(optarg
, &endptr
, 0);
124 if (endptr
== optarg
|| *endptr
!= '\0')
126 fprintf(stderr
, _("%s: invalid argument for option -e\n"), progname
);
127 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"), progname
);
130 if (set_xid_epoch
== -1)
132 fprintf(stderr
, _("%s: transaction ID epoch (-e) must not be -1\n"), progname
);
138 set_xid
= strtoul(optarg
, &endptr
, 0);
139 if (endptr
== optarg
|| *endptr
!= '\0')
141 fprintf(stderr
, _("%s: invalid argument for option -x\n"), progname
);
142 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"), progname
);
147 fprintf(stderr
, _("%s: transaction ID (-x) must not be 0\n"), progname
);
153 set_oid
= strtoul(optarg
, &endptr
, 0);
154 if (endptr
== optarg
|| *endptr
!= '\0')
156 fprintf(stderr
, _("%s: invalid argument for option -o\n"), progname
);
157 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"), progname
);
162 fprintf(stderr
, _("%s: OID (-o) must not be 0\n"), progname
);
168 set_mxid
= strtoul(optarg
, &endptr
, 0);
169 if (endptr
== optarg
|| *endptr
!= '\0')
171 fprintf(stderr
, _("%s: invalid argument for option -m\n"), progname
);
172 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"), progname
);
177 fprintf(stderr
, _("%s: multitransaction ID (-m) must not be 0\n"), progname
);
183 set_mxoff
= strtoul(optarg
, &endptr
, 0);
184 if (endptr
== optarg
|| *endptr
!= '\0')
186 fprintf(stderr
, _("%s: invalid argument for option -O\n"), progname
);
187 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"), progname
);
192 fprintf(stderr
, _("%s: multitransaction offset (-O) must not be -1\n"), progname
);
198 minXlogTli
= strtoul(optarg
, &endptr
, 0);
199 if (endptr
== optarg
|| *endptr
!= ',')
201 fprintf(stderr
, _("%s: invalid argument for option -l\n"), progname
);
202 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"), progname
);
205 minXlogId
= strtoul(endptr
+ 1, &endptr2
, 0);
206 if (endptr2
== endptr
+ 1 || *endptr2
!= ',')
208 fprintf(stderr
, _("%s: invalid argument for option -l\n"), progname
);
209 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"), progname
);
212 minXlogSeg
= strtoul(endptr2
+ 1, &endptr3
, 0);
213 if (endptr3
== endptr2
+ 1 || *endptr3
!= '\0')
215 fprintf(stderr
, _("%s: invalid argument for option -l\n"), progname
);
216 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"), progname
);
222 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"), progname
);
229 fprintf(stderr
, _("%s: no data directory specified\n"), progname
);
230 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"), progname
);
235 * Don't allow pg_resetxlog to be run as root, to avoid overwriting the
236 * ownership of files in the data directory. We need only check for root
237 * -- any other user won't have sufficient permissions to modify files in
238 * the data directory.
243 fprintf(stderr
, _("%s: cannot be executed by \"root\"\n"),
245 fprintf(stderr
, _("You must run %s as the PostgreSQL superuser.\n"),
251 DataDir
= argv
[optind
];
253 if (chdir(DataDir
) < 0)
255 fprintf(stderr
, _("%s: could not change directory to \"%s\": %s\n"),
256 progname
, DataDir
, strerror(errno
));
261 * Check for a postmaster lock file --- if there is one, refuse to
262 * proceed, on grounds we might be interfering with a live installation.
264 snprintf(path
, MAXPGPATH
, "%s/postmaster.pid", DataDir
);
266 if ((fd
= open(path
, O_RDONLY
, 0)) < 0)
270 fprintf(stderr
, _("%s: could not open file \"%s\" for reading: %s\n"), progname
, path
, strerror(errno
));
276 fprintf(stderr
, _("%s: lock file \"%s\" exists\n"
277 "Is a server running? If not, delete the lock file and try again.\n"),
283 * Attempt to read the existing pg_control file
285 if (!ReadControlFile())
286 GuessControlValues();
289 * Also look at existing segment files to set up newXlogId/newXlogSeg
294 * Adjust fields if required by switches. (Do this now so that printout,
295 * if any, includes these values.)
297 if (set_xid_epoch
!= -1)
298 ControlFile
.checkPointCopy
.nextXidEpoch
= set_xid_epoch
;
301 ControlFile
.checkPointCopy
.nextXid
= set_xid
;
304 ControlFile
.checkPointCopy
.nextOid
= set_oid
;
307 ControlFile
.checkPointCopy
.nextMulti
= set_mxid
;
310 ControlFile
.checkPointCopy
.nextMultiOffset
= set_mxoff
;
312 if (minXlogTli
> ControlFile
.checkPointCopy
.ThisTimeLineID
)
313 ControlFile
.checkPointCopy
.ThisTimeLineID
= minXlogTli
;
315 if (minXlogId
> newXlogId
||
316 (minXlogId
== newXlogId
&&
317 minXlogSeg
> newXlogSeg
))
319 newXlogId
= minXlogId
;
320 newXlogSeg
= minXlogSeg
;
324 * If we had to guess anything, and -f was not given, just print the
325 * guessed values and exit. Also print if -n is given.
327 if ((guessed
&& !force
) || noupdate
)
329 PrintControlValues(guessed
);
332 printf(_("\nIf these values seem acceptable, use -f to force reset.\n"));
340 * Don't reset from a dirty pg_control without -f, either.
342 if (ControlFile
.state
!= DB_SHUTDOWNED
&& !force
)
344 printf(_("The database server was not shut down cleanly.\n"
345 "Resetting the transaction log might cause data to be lost.\n"
346 "If you want to proceed anyway, use -f to force reset.\n"));
351 * Else, do the dirty deed.
353 RewriteControlFile();
357 printf(_("Transaction log reset\n"));
363 * Try to read the existing pg_control file.
365 * This routine is also responsible for updating old pg_control versions
366 * to the current format. (Currently we don't do anything of the sort.)
369 ReadControlFile(void)
376 if ((fd
= open(XLOG_CONTROL_FILE
, O_RDONLY
| PG_BINARY
, 0)) < 0)
379 * If pg_control is not there at all, or we can't read it, the odds
380 * are we've been handed a bad DataDir path, so give up. User can do
381 * "touch pg_control" to force us to proceed.
383 fprintf(stderr
, _("%s: could not open file \"%s\" for reading: %s\n"),
384 progname
, XLOG_CONTROL_FILE
, strerror(errno
));
386 fprintf(stderr
, _("If you are sure the data directory path is correct, execute\n"
393 /* Use malloc to ensure we have a maxaligned buffer */
394 buffer
= (char *) malloc(PG_CONTROL_SIZE
);
396 len
= read(fd
, buffer
, PG_CONTROL_SIZE
);
399 fprintf(stderr
, _("%s: could not read file \"%s\": %s\n"),
400 progname
, XLOG_CONTROL_FILE
, strerror(errno
));
405 if (len
>= sizeof(ControlFileData
) &&
406 ((ControlFileData
*) buffer
)->pg_control_version
== PG_CONTROL_VERSION
)
412 offsetof(ControlFileData
, crc
));
415 if (EQ_CRC32(crc
, ((ControlFileData
*) buffer
)->crc
))
418 memcpy(&ControlFile
, buffer
, sizeof(ControlFile
));
422 fprintf(stderr
, _("%s: pg_control exists but has invalid CRC; proceed with caution\n"),
424 /* We will use the data anyway, but treat it as guessed. */
425 memcpy(&ControlFile
, buffer
, sizeof(ControlFile
));
430 /* Looks like it's a mess. */
431 fprintf(stderr
, _("%s: pg_control exists but is broken or unknown version; ignoring it\n"),
438 * Guess at pg_control values when we can't read the old ones.
441 GuessControlValues(void)
443 uint64 sysidentifier
;
447 * Set up a completely default set of pg_control values.
450 memset(&ControlFile
, 0, sizeof(ControlFile
));
452 ControlFile
.pg_control_version
= PG_CONTROL_VERSION
;
453 ControlFile
.catalog_version_no
= CATALOG_VERSION_NO
;
456 * Create a new unique installation identifier, since we can no longer use
457 * any old XLOG records. See notes in xlog.c about the algorithm.
459 gettimeofday(&tv
, NULL
);
460 sysidentifier
= ((uint64
) tv
.tv_sec
) << 32;
461 sysidentifier
|= (uint32
) (tv
.tv_sec
| tv
.tv_usec
);
463 ControlFile
.system_identifier
= sysidentifier
;
465 ControlFile
.checkPointCopy
.redo
.xlogid
= 0;
466 ControlFile
.checkPointCopy
.redo
.xrecoff
= SizeOfXLogLongPHD
;
467 ControlFile
.checkPointCopy
.ThisTimeLineID
= 1;
468 ControlFile
.checkPointCopy
.nextXidEpoch
= 0;
469 ControlFile
.checkPointCopy
.nextXid
= (TransactionId
) 514; /* XXX */
470 ControlFile
.checkPointCopy
.nextOid
= FirstBootstrapObjectId
;
471 ControlFile
.checkPointCopy
.nextMulti
= FirstMultiXactId
;
472 ControlFile
.checkPointCopy
.nextMultiOffset
= 0;
473 ControlFile
.checkPointCopy
.time
= (pg_time_t
) time(NULL
);
475 ControlFile
.state
= DB_SHUTDOWNED
;
476 ControlFile
.time
= (pg_time_t
) time(NULL
);
477 ControlFile
.checkPoint
= ControlFile
.checkPointCopy
.redo
;
479 ControlFile
.maxAlign
= MAXIMUM_ALIGNOF
;
480 ControlFile
.floatFormat
= FLOATFORMAT_VALUE
;
481 ControlFile
.blcksz
= BLCKSZ
;
482 ControlFile
.relseg_size
= RELSEG_SIZE
;
483 ControlFile
.xlog_blcksz
= XLOG_BLCKSZ
;
484 ControlFile
.xlog_seg_size
= XLOG_SEG_SIZE
;
485 ControlFile
.nameDataLen
= NAMEDATALEN
;
486 ControlFile
.indexMaxKeys
= INDEX_MAX_KEYS
;
487 ControlFile
.toast_max_chunk_size
= TOAST_MAX_CHUNK_SIZE
;
488 #ifdef HAVE_INT64_TIMESTAMP
489 ControlFile
.enableIntTimes
= true;
491 ControlFile
.enableIntTimes
= false;
493 ControlFile
.float4ByVal
= FLOAT4PASSBYVAL
;
494 ControlFile
.float8ByVal
= FLOAT8PASSBYVAL
;
497 * XXX eventually, should try to grovel through old XLOG to develop more
498 * accurate values for TimeLineID, nextXID, etc.
504 * Print the guessed pg_control values when we had to guess.
506 * NB: this display should be just those fields that will not be
507 * reset by RewriteControlFile().
510 PrintControlValues(bool guessed
)
512 char sysident_str
[32];
515 printf(_("Guessed pg_control values:\n\n"));
517 printf(_("pg_control values:\n\n"));
520 * Format system_identifier separately to keep platform-dependent format
521 * code out of the translatable message string.
523 snprintf(sysident_str
, sizeof(sysident_str
), UINT64_FORMAT
,
524 ControlFile
.system_identifier
);
526 printf(_("First log file ID after reset: %u\n"),
528 printf(_("First log file segment after reset: %u\n"),
530 printf(_("pg_control version number: %u\n"),
531 ControlFile
.pg_control_version
);
532 printf(_("Catalog version number: %u\n"),
533 ControlFile
.catalog_version_no
);
534 printf(_("Database system identifier: %s\n"),
536 printf(_("Latest checkpoint's TimeLineID: %u\n"),
537 ControlFile
.checkPointCopy
.ThisTimeLineID
);
538 printf(_("Latest checkpoint's NextXID: %u/%u\n"),
539 ControlFile
.checkPointCopy
.nextXidEpoch
,
540 ControlFile
.checkPointCopy
.nextXid
);
541 printf(_("Latest checkpoint's NextOID: %u\n"),
542 ControlFile
.checkPointCopy
.nextOid
);
543 printf(_("Latest checkpoint's NextMultiXactId: %u\n"),
544 ControlFile
.checkPointCopy
.nextMulti
);
545 printf(_("Latest checkpoint's NextMultiOffset: %u\n"),
546 ControlFile
.checkPointCopy
.nextMultiOffset
);
547 printf(_("Maximum data alignment: %u\n"),
548 ControlFile
.maxAlign
);
549 /* we don't print floatFormat since can't say much useful about it */
550 printf(_("Database block size: %u\n"),
552 printf(_("Blocks per segment of large relation: %u\n"),
553 ControlFile
.relseg_size
);
554 printf(_("WAL block size: %u\n"),
555 ControlFile
.xlog_blcksz
);
556 printf(_("Bytes per WAL segment: %u\n"),
557 ControlFile
.xlog_seg_size
);
558 printf(_("Maximum length of identifiers: %u\n"),
559 ControlFile
.nameDataLen
);
560 printf(_("Maximum columns in an index: %u\n"),
561 ControlFile
.indexMaxKeys
);
562 printf(_("Maximum size of a TOAST chunk: %u\n"),
563 ControlFile
.toast_max_chunk_size
);
564 printf(_("Date/time type storage: %s\n"),
565 (ControlFile
.enableIntTimes
? _("64-bit integers") : _("floating-point numbers")));
566 printf(_("Float4 argument passing: %s\n"),
567 (ControlFile
.float4ByVal
? _("by value") : _("by reference")));
568 printf(_("Float8 argument passing: %s\n"),
569 (ControlFile
.float8ByVal
? _("by value") : _("by reference")));
574 * Write out the new pg_control file.
577 RewriteControlFile(void)
580 char buffer
[PG_CONTROL_SIZE
]; /* need not be aligned */
583 * Adjust fields as needed to force an empty XLOG starting at
584 * newXlogId/newXlogSeg.
586 ControlFile
.checkPointCopy
.redo
.xlogid
= newXlogId
;
587 ControlFile
.checkPointCopy
.redo
.xrecoff
=
588 newXlogSeg
* XLogSegSize
+ SizeOfXLogLongPHD
;
589 ControlFile
.checkPointCopy
.time
= (pg_time_t
) time(NULL
);
591 ControlFile
.state
= DB_SHUTDOWNED
;
592 ControlFile
.time
= (pg_time_t
) time(NULL
);
593 ControlFile
.checkPoint
= ControlFile
.checkPointCopy
.redo
;
594 ControlFile
.prevCheckPoint
.xlogid
= 0;
595 ControlFile
.prevCheckPoint
.xrecoff
= 0;
596 ControlFile
.minRecoveryPoint
.xlogid
= 0;
597 ControlFile
.minRecoveryPoint
.xrecoff
= 0;
599 /* Now we can force the recorded xlog seg size to the right thing. */
600 ControlFile
.xlog_seg_size
= XLogSegSize
;
602 /* Contents are protected with a CRC */
603 INIT_CRC32(ControlFile
.crc
);
604 COMP_CRC32(ControlFile
.crc
,
605 (char *) &ControlFile
,
606 offsetof(ControlFileData
, crc
));
607 FIN_CRC32(ControlFile
.crc
);
610 * We write out PG_CONTROL_SIZE bytes into pg_control, zero-padding the
611 * excess over sizeof(ControlFileData). This reduces the odds of
612 * premature-EOF errors when reading pg_control. We'll still fail when we
613 * check the contents of the file, but hopefully with a more specific
614 * error than "couldn't read pg_control".
616 if (sizeof(ControlFileData
) > PG_CONTROL_SIZE
)
619 _("%s: internal error -- sizeof(ControlFileData) is too large ... fix PG_CONTROL_SIZE\n"),
624 memset(buffer
, 0, PG_CONTROL_SIZE
);
625 memcpy(buffer
, &ControlFile
, sizeof(ControlFileData
));
627 unlink(XLOG_CONTROL_FILE
);
629 fd
= open(XLOG_CONTROL_FILE
,
630 O_RDWR
| O_CREAT
| O_EXCL
| PG_BINARY
,
634 fprintf(stderr
, _("%s: could not create pg_control file: %s\n"),
635 progname
, strerror(errno
));
640 if (write(fd
, buffer
, PG_CONTROL_SIZE
) != PG_CONTROL_SIZE
)
642 /* if write didn't set errno, assume problem is no disk space */
645 fprintf(stderr
, _("%s: could not write pg_control file: %s\n"),
646 progname
, strerror(errno
));
652 fprintf(stderr
, _("%s: fsync error: %s\n"), progname
, strerror(errno
));
661 * Scan existing XLOG files and determine the highest existing WAL address
663 * On entry, ControlFile.checkPointCopy.redo and ControlFile.xlog_seg_size
664 * are assumed valid (note that we allow the old xlog seg size to differ
665 * from what we're using). On exit, newXlogId and newXlogSeg are set to
666 * suitable values for the beginning of replacement WAL (in our seg size).
675 * Initialize the max() computation using the last checkpoint address from
676 * old pg_control. Note that for the moment we are working with segment
677 * numbering according to the old xlog seg size.
679 newXlogId
= ControlFile
.checkPointCopy
.redo
.xlogid
;
680 newXlogSeg
= ControlFile
.checkPointCopy
.redo
.xrecoff
/ ControlFile
.xlog_seg_size
;
683 * Scan the pg_xlog directory to find existing WAL segment files. We
684 * assume any present have been used; in most scenarios this should be
685 * conservative, because of xlog.c's attempts to pre-create files.
687 xldir
= opendir(XLOGDIR
);
690 fprintf(stderr
, _("%s: could not open directory \"%s\": %s\n"),
691 progname
, XLOGDIR
, strerror(errno
));
696 while ((xlde
= readdir(xldir
)) != NULL
)
698 if (strlen(xlde
->d_name
) == 24 &&
699 strspn(xlde
->d_name
, "0123456789ABCDEF") == 24)
705 sscanf(xlde
->d_name
, "%08X%08X%08X", &tli
, &log
, &seg
);
708 * Note: we take the max of all files found, regardless of their
709 * timelines. Another possibility would be to ignore files of
710 * timelines other than the target TLI, but this seems safer.
711 * Better too large a result than too small...
713 if (log
> newXlogId
||
714 (log
== newXlogId
&& seg
> newXlogSeg
))
725 * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
728 if (GetLastError() == ERROR_NO_MORE_FILES
)
734 fprintf(stderr
, _("%s: could not read from directory \"%s\": %s\n"),
735 progname
, XLOGDIR
, strerror(errno
));
741 * Finally, convert to new xlog seg size, and advance by one to ensure we
742 * are in virgin territory.
744 newXlogSeg
*= ControlFile
.xlog_seg_size
;
745 newXlogSeg
= (newXlogSeg
+ XLogSegSize
- 1) / XLogSegSize
;
747 /* be sure we wrap around correctly at end of a logfile */
748 NextLogSeg(newXlogId
, newXlogSeg
);
753 * Remove existing XLOG files
756 KillExistingXLOG(void)
760 char path
[MAXPGPATH
];
762 xldir
= opendir(XLOGDIR
);
765 fprintf(stderr
, _("%s: could not open directory \"%s\": %s\n"),
766 progname
, XLOGDIR
, strerror(errno
));
771 while ((xlde
= readdir(xldir
)) != NULL
)
773 if (strlen(xlde
->d_name
) == 24 &&
774 strspn(xlde
->d_name
, "0123456789ABCDEF") == 24)
776 snprintf(path
, MAXPGPATH
, "%s/%s", XLOGDIR
, xlde
->d_name
);
777 if (unlink(path
) < 0)
779 fprintf(stderr
, _("%s: could not delete file \"%s\": %s\n"),
780 progname
, path
, strerror(errno
));
789 * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
792 if (GetLastError() == ERROR_NO_MORE_FILES
)
798 fprintf(stderr
, _("%s: could not read from directory \"%s\": %s\n"),
799 progname
, XLOGDIR
, strerror(errno
));
807 * Write an empty XLOG file, containing only the checkpoint record
808 * already set up in ControlFile.
815 XLogLongPageHeader longpage
;
818 char path
[MAXPGPATH
];
822 /* Use malloc() to ensure buffer is MAXALIGNED */
823 buffer
= (char *) malloc(XLOG_BLCKSZ
);
824 page
= (XLogPageHeader
) buffer
;
825 memset(buffer
, 0, XLOG_BLCKSZ
);
827 /* Set up the XLOG page header */
828 page
->xlp_magic
= XLOG_PAGE_MAGIC
;
829 page
->xlp_info
= XLP_LONG_HEADER
;
830 page
->xlp_tli
= ControlFile
.checkPointCopy
.ThisTimeLineID
;
831 page
->xlp_pageaddr
.xlogid
=
832 ControlFile
.checkPointCopy
.redo
.xlogid
;
833 page
->xlp_pageaddr
.xrecoff
=
834 ControlFile
.checkPointCopy
.redo
.xrecoff
- SizeOfXLogLongPHD
;
835 longpage
= (XLogLongPageHeader
) page
;
836 longpage
->xlp_sysid
= ControlFile
.system_identifier
;
837 longpage
->xlp_seg_size
= XLogSegSize
;
838 longpage
->xlp_xlog_blcksz
= XLOG_BLCKSZ
;
840 /* Insert the initial checkpoint record */
841 record
= (XLogRecord
*) ((char *) page
+ SizeOfXLogLongPHD
);
842 record
->xl_prev
.xlogid
= 0;
843 record
->xl_prev
.xrecoff
= 0;
844 record
->xl_xid
= InvalidTransactionId
;
845 record
->xl_tot_len
= SizeOfXLogRecord
+ sizeof(CheckPoint
);
846 record
->xl_len
= sizeof(CheckPoint
);
847 record
->xl_info
= XLOG_CHECKPOINT_SHUTDOWN
;
848 record
->xl_rmid
= RM_XLOG_ID
;
849 memcpy(XLogRecGetData(record
), &ControlFile
.checkPointCopy
,
853 COMP_CRC32(crc
, &ControlFile
.checkPointCopy
, sizeof(CheckPoint
));
854 COMP_CRC32(crc
, (char *) record
+ sizeof(pg_crc32
),
855 SizeOfXLogRecord
- sizeof(pg_crc32
));
857 record
->xl_crc
= crc
;
859 /* Write the first page */
860 XLogFilePath(path
, ControlFile
.checkPointCopy
.ThisTimeLineID
,
861 newXlogId
, newXlogSeg
);
865 fd
= open(path
, O_RDWR
| O_CREAT
| O_EXCL
| PG_BINARY
,
869 fprintf(stderr
, _("%s: could not open file \"%s\": %s\n"),
870 progname
, path
, strerror(errno
));
875 if (write(fd
, buffer
, XLOG_BLCKSZ
) != XLOG_BLCKSZ
)
877 /* if write didn't set errno, assume problem is no disk space */
880 fprintf(stderr
, _("%s: could not write file \"%s\": %s\n"),
881 progname
, path
, strerror(errno
));
885 /* Fill the rest of the file with zeroes */
886 memset(buffer
, 0, XLOG_BLCKSZ
);
887 for (nbytes
= XLOG_BLCKSZ
; nbytes
< XLogSegSize
; nbytes
+= XLOG_BLCKSZ
)
890 if (write(fd
, buffer
, XLOG_BLCKSZ
) != XLOG_BLCKSZ
)
894 fprintf(stderr
, _("%s: could not write file \"%s\": %s\n"),
895 progname
, path
, strerror(errno
));
902 fprintf(stderr
, _("%s: fsync error: %s\n"), progname
, strerror(errno
));
913 printf(_("%s resets the PostgreSQL transaction log.\n\n"), progname
);
914 printf(_("Usage:\n %s [OPTION]... DATADIR\n\n"), progname
);
915 printf(_("Options:\n"));
916 printf(_(" -f force update to be done\n"));
917 printf(_(" -l TLI,FILE,SEG force minimum WAL starting location for new transaction log\n"));
918 printf(_(" -m XID set next multitransaction ID\n"));
919 printf(_(" -n no update, just show extracted control values (for testing)\n"));
920 printf(_(" -o OID set next OID\n"));
921 printf(_(" -O OFFSET set next multitransaction offset\n"));
922 printf(_(" -x XID set next transaction ID\n"));
923 printf(_(" -e XIDEPOCH set next transaction ID epoch\n"));
924 printf(_(" --help show this help, then exit\n"));
925 printf(_(" --version output version information, then exit\n"));
926 printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));