Use macros from xlog_internal.h for WAL segment logic in pg_resetwal
authorMichael Paquier <[email protected]>
Wed, 5 Oct 2022 05:10:13 +0000 (14:10 +0900)
committerMichael Paquier <[email protected]>
Wed, 5 Oct 2022 05:10:13 +0000 (14:10 +0900)
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

src/bin/pg_resetwal/pg_resetwal.c

index d4772a2965047a4677dcd50f2cdb3d62b973f3b9..089063f47149ce6521f16d75b6a667c0c344bfe7 100644 (file)
@@ -71,7 +71,7 @@ static TransactionId set_newest_commit_ts_xid = 0;
 static Oid set_oid = 0;
 static MultiXactId set_mxid = 0;
 static MultiXactOffset set_mxoff = (MultiXactOffset) -1;
-static uint32 minXlogTli = 0;
+static TimeLineID minXlogTli = 0;
 static XLogSegNo minXlogSegNo = 0;
 static int WalSegSz;
 static int set_wal_segsize;
@@ -901,7 +901,6 @@ FindEndOfXLOG(void)
 {
    DIR        *xldir;
    struct dirent *xlde;
-   uint64      segs_per_xlogid;
    uint64      xlogbytepos;
 
    /*
@@ -909,8 +908,8 @@ FindEndOfXLOG(void)
     * old pg_control.  Note that for the moment we are working with segment
     * numbering according to the old xlog seg size.
     */
-   segs_per_xlogid = (UINT64CONST(0x0000000100000000) / ControlFile.xlog_seg_size);
-   newXlogSegNo = ControlFile.checkPointCopy.redo / ControlFile.xlog_seg_size;
+   XLByteToSeg(ControlFile.checkPointCopy.redo, newXlogSegNo,
+               ControlFile.xlog_seg_size);
 
    /*
     * Scan the pg_wal directory to find existing WAL segment files. We assume
@@ -926,18 +925,12 @@ FindEndOfXLOG(void)
        if (IsXLogFileName(xlde->d_name) ||
            IsPartialXLogFileName(xlde->d_name))
        {
-           unsigned int tli,
-                       log,
-                       seg;
+           TimeLineID  tli;
            XLogSegNo   segno;
 
-           /*
-            * Note: We don't use XLogFromFileName here, because we want to
-            * use the segment size from the control file, not the size the
-            * pg_resetwal binary was compiled with
-            */
-           sscanf(xlde->d_name, "%08X%08X%08X", &tli, &log, &seg);
-           segno = ((uint64) log) * segs_per_xlogid + seg;
+           /* Use the segment size from the control file */
+           XLogFromFileName(xlde->d_name, &tli, &segno,
+                            ControlFile.xlog_seg_size);
 
            /*
             * Note: we take the max of all files found, regardless of their