From cd7d7d01f73b47f61b08e202c02167f469a800b3 Mon Sep 17 00:00:00 2001 From: Bharath Rupireddy Date: Tue, 4 Oct 2022 06:45:07 +0000 Subject: [PATCH v2] Use WAL file related inline functions/macros in pg_resetwal Replace explicit WAL file parsing code with XLogFromFileName() in pg_resetwal.c. This was not done then (in PG 10) because the XLogFromFileName() wasn't accepting file size as an input parameter and pg_resetwal needed to use WAL file size from the controlfile. Thanks to the commit fc49e24fa69a15efacd5b8958115ed9c43c48f9a which added the wal_segsz_bytes parameter to XLogFromFileName(). This removes using extra variables in pg_resetwal.c and a bit of duplicate code too. Also, use XLByteToSeg() macro instead of explicit calculation of segment number. --- src/bin/pg_resetwal/pg_resetwal.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/bin/pg_resetwal/pg_resetwal.c b/src/bin/pg_resetwal/pg_resetwal.c index d4772a2965..aaa4840920 100644 --- a/src/bin/pg_resetwal/pg_resetwal.c +++ b/src/bin/pg_resetwal/pg_resetwal.c @@ -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,15 @@ FindEndOfXLOG(void) if (IsXLogFileName(xlde->d_name) || IsPartialXLogFileName(xlde->d_name)) { - unsigned int tli, - log, - seg; + unsigned int 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 + * We use segment size from the control file here, 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; + XLogFromFileName(xlde->d_name, &tli, &segno, + ControlFile.xlog_seg_size); /* * Note: we take the max of all files found, regardless of their -- 2.34.1