From 42948508910266685093c74d9fe61c3c5857c5ec Mon Sep 17 00:00:00 2001
From: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Date: Tue, 4 Oct 2022 05:04:40 +0000
Subject: [PATCH v1] Use XLogFromFileName() in pg_resetwal to parse position
 from file

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.
---
 src/bin/pg_resetwal/pg_resetwal.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)

diff --git a/src/bin/pg_resetwal/pg_resetwal.c b/src/bin/pg_resetwal/pg_resetwal.c
index d4772a2965..bad886ddf9 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,7 +908,6 @@ 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;
 
 	/*
@@ -926,18 +924,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

