Skip to content

Commit 4eb5e27

Browse files
committed
Resetting the XLOG can't include rewinding it to logical position zero,
because we need page LSNs stored in the main database to be less than the current XLOG position. Hence, generate the new XLOG segment at last old segment number plus one.
1 parent 9242e6c commit 4eb5e27

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

contrib/pg_resetxlog/pg_resetxlog.c

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@
1212
* by scanning the old xlog if necessary.
1313
* 3. Modify pg_control to reflect a "shutdown" state with a checkpoint
1414
* record at the start of xlog.
15-
* 4. Flush the existing xlog files and write a new segment 0 with
16-
* just a checkpoint record in it.
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".
1719
* This is all pretty straightforward except for the intuition part of
1820
* step 2 ...
1921
*
2022
*
2123
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
2224
* Portions Copyright (c) 1994, Regents of the University of California
2325
*
24-
* $Header: /cvsroot/pgsql/contrib/pg_resetxlog/Attic/pg_resetxlog.c,v 1.1 2001/03/14 00:57:43 tgl Exp $
26+
* $Header: /cvsroot/pgsql/contrib/pg_resetxlog/Attic/pg_resetxlog.c,v 1.2 2001/03/16 05:08:39 tgl Exp $
2527
*
2628
*-------------------------------------------------------------------------
2729
*/
@@ -108,6 +110,7 @@ static char XLogDir[MAXPGPATH];
108110
static char ControlFilePath[MAXPGPATH];
109111

110112
static ControlFileData ControlFile; /* pg_control values */
113+
static uint32 newXlogId, newXlogSeg; /* ID/Segment of new XLOG segment */
111114
static bool guessed = false; /* T if we had to guess at any values */
112115

113116

@@ -390,6 +393,8 @@ CheckControlVersion0(char *buffer, int len)
390393
ControlFile.catalog_version_no = oldfile->catalog_version_no;
391394

392395
ControlFile.state = oldfile->state;
396+
ControlFile.logId = oldfile->logId;
397+
ControlFile.logSeg = oldfile->logSeg;
393398

394399
ControlFile.blcksz = oldfile->blcksz;
395400
ControlFile.relseg_size = oldfile->relseg_size;
@@ -671,6 +676,8 @@ PrintControlValues(void)
671676
printf("Guessed-at pg_control values:\n\n"
672677
"pg_control version number: %u\n"
673678
"Catalog version number: %u\n"
679+
"Current log file id: %u\n"
680+
"Next log file segment: %u\n"
674681
"Latest checkpoint's StartUpID: %u\n"
675682
"Latest checkpoint's NextXID: %u\n"
676683
"Latest checkpoint's NextOID: %u\n"
@@ -681,6 +688,8 @@ PrintControlValues(void)
681688

682689
ControlFile.pg_control_version,
683690
ControlFile.catalog_version_no,
691+
ControlFile.logId,
692+
ControlFile.logSeg,
684693
ControlFile.checkPointCopy.ThisStartUpID,
685694
ControlFile.checkPointCopy.nextXid,
686695
ControlFile.checkPointCopy.nextOid,
@@ -701,17 +710,24 @@ RewriteControlFile(void)
701710
char buffer[BLCKSZ]; /* need not be aligned */
702711

703712
/*
704-
* Adjust fields as needed to force an empty XLOG.
713+
* Adjust fields as needed to force an empty XLOG starting at the
714+
* next available segment.
705715
*/
706-
ControlFile.checkPointCopy.redo.xlogid = 0;
707-
ControlFile.checkPointCopy.redo.xrecoff = SizeOfXLogPHD;
716+
newXlogId = ControlFile.logId;
717+
newXlogSeg = ControlFile.logSeg;
718+
/* be sure we wrap around correctly at end of a logfile */
719+
NextLogSeg(newXlogId, newXlogSeg);
720+
721+
ControlFile.checkPointCopy.redo.xlogid = newXlogId;
722+
ControlFile.checkPointCopy.redo.xrecoff =
723+
newXlogSeg * XLogSegSize + SizeOfXLogPHD;
708724
ControlFile.checkPointCopy.undo = ControlFile.checkPointCopy.redo;
709725
ControlFile.checkPointCopy.time = time(NULL);
710726

711727
ControlFile.state = DB_SHUTDOWNED;
712728
ControlFile.time = time(NULL);
713-
ControlFile.logId = 0;
714-
ControlFile.logSeg = 1;
729+
ControlFile.logId = newXlogId;
730+
ControlFile.logSeg = newXlogSeg + 1;
715731
ControlFile.checkPoint = ControlFile.checkPointCopy.redo;
716732
ControlFile.prevCheckPoint.xlogid = 0;
717733
ControlFile.prevCheckPoint.xrecoff = 0;
@@ -848,7 +864,7 @@ WriteEmptyXLOG(void)
848864
record->xl_crc = crc;
849865

850866
/* Write the first page */
851-
XLogFileName(path, 0, 0);
867+
XLogFileName(path, newXlogId, newXlogSeg);
852868

853869
unlink(path);
854870

0 commit comments

Comments
 (0)