Skip to content

Commit 9a30e15

Browse files
committed
Use macros from xlog_internal.h for WAL segment logic in pg_resetwal
When scanning for the end of WAL, pg_resetwal has been maintaining its own internal logic to calculate segment numbers and to parse a WAL segment name for its timeline and segment number. The code claimed for example that XLogFromFileName() cannot be used because it lacks the possibility of specifying a WAL segment size, which is not the case since fc49e24, that has made the WAL segment size configurable at initialization time, extending this routine to do so. Similarly, this switches one segment number calculation to use XLByteToSeg() rather than the same logic present in xlog_internal.h. While on it, switch to TimeLineID in pg_resetwal.c for TLI numbers parsed from segment names, to be more consistent with XLogFromFileName(). The maths are exactly the same, but the code gets simplified. Author: Bharath Rupireddy Reviewed-by: Kyotaro Horiguchi Discussion: https://postgr.es/m/CALj2ACX+E_jnwqH_jmjhNG8BczJTNRTOLpw8K1CB1OcB48MJ8w@mail.gmail.com
1 parent 9aa58d4 commit 9a30e15

File tree

1 file changed

+7
-14
lines changed

1 file changed

+7
-14
lines changed

src/bin/pg_resetwal/pg_resetwal.c

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ static TransactionId set_newest_commit_ts_xid = 0;
7171
static Oid set_oid = 0;
7272
static MultiXactId set_mxid = 0;
7373
static MultiXactOffset set_mxoff = (MultiXactOffset) -1;
74-
static uint32 minXlogTli = 0;
74+
static TimeLineID minXlogTli = 0;
7575
static XLogSegNo minXlogSegNo = 0;
7676
static int WalSegSz;
7777
static int set_wal_segsize;
@@ -901,16 +901,15 @@ FindEndOfXLOG(void)
901901
{
902902
DIR *xldir;
903903
struct dirent *xlde;
904-
uint64 segs_per_xlogid;
905904
uint64 xlogbytepos;
906905

907906
/*
908907
* Initialize the max() computation using the last checkpoint address from
909908
* old pg_control. Note that for the moment we are working with segment
910909
* numbering according to the old xlog seg size.
911910
*/
912-
segs_per_xlogid = (UINT64CONST(0x0000000100000000) / ControlFile.xlog_seg_size);
913-
newXlogSegNo = ControlFile.checkPointCopy.redo / ControlFile.xlog_seg_size;
911+
XLByteToSeg(ControlFile.checkPointCopy.redo, newXlogSegNo,
912+
ControlFile.xlog_seg_size);
914913

915914
/*
916915
* Scan the pg_wal directory to find existing WAL segment files. We assume
@@ -926,18 +925,12 @@ FindEndOfXLOG(void)
926925
if (IsXLogFileName(xlde->d_name) ||
927926
IsPartialXLogFileName(xlde->d_name))
928927
{
929-
unsigned int tli,
930-
log,
931-
seg;
928+
TimeLineID tli;
932929
XLogSegNo segno;
933930

934-
/*
935-
* Note: We don't use XLogFromFileName here, because we want to
936-
* use the segment size from the control file, not the size the
937-
* pg_resetwal binary was compiled with
938-
*/
939-
sscanf(xlde->d_name, "%08X%08X%08X", &tli, &log, &seg);
940-
segno = ((uint64) log) * segs_per_xlogid + seg;
931+
/* Use the segment size from the control file */
932+
XLogFromFileName(xlde->d_name, &tli, &segno,
933+
ControlFile.xlog_seg_size);
941934

942935
/*
943936
* Note: we take the max of all files found, regardless of their

0 commit comments

Comments
 (0)