From 8bb8dc6afe753f885520429613966f8cedc2b477 Mon Sep 17 00:00:00 2001
From: Amul Sul <sulamul@gmail.com>
Date: Wed, 4 Feb 2026 15:31:51 +0530
Subject: [PATCH v14 05/11] Refactor: pg_waldump: Move WAL segment size to
 XLogDumpPrivate.

Relocate the WAL segment size variable to the XLogDumpPrivate
structure and rename it to segsize for consistency. This change is
required to make the segment size accessible to the archive streamer
code, where passing it as a function argument is not feasible.
---
 src/bin/pg_waldump/pg_waldump.c | 26 +++++++++++++-------------
 src/bin/pg_waldump/pg_waldump.h |  1 +
 2 files changed, 14 insertions(+), 13 deletions(-)

diff --git a/src/bin/pg_waldump/pg_waldump.c b/src/bin/pg_waldump/pg_waldump.c
index 958a71a01cf..5d31b15dbd8 100644
--- a/src/bin/pg_waldump/pg_waldump.c
+++ b/src/bin/pg_waldump/pg_waldump.c
@@ -811,7 +811,6 @@ main(int argc, char **argv)
 	XLogRecPtr	first_record;
 	char	   *waldir = NULL;
 	char	   *errormsg;
-	int			WalSegSz;
 
 	static struct option long_options[] = {
 		{"bkp-details", no_argument, NULL, 'b'},
@@ -865,6 +864,7 @@ main(int argc, char **argv)
 	memset(&stats, 0, sizeof(XLogStats));
 
 	private.timeline = 1;
+	private.segsize = 0;
 	private.startptr = InvalidXLogRecPtr;
 	private.endptr = InvalidXLogRecPtr;
 	private.endptr_reached = false;
@@ -1138,18 +1138,18 @@ main(int argc, char **argv)
 				pg_fatal("could not open directory \"%s\": %m", waldir);
 		}
 
-		waldir = identify_target_directory(waldir, fname, &WalSegSz);
+		waldir = identify_target_directory(waldir, fname, &private.segsize);
 		fd = open_file_in_directory(waldir, fname);
 		if (fd < 0)
 			pg_fatal("could not open file \"%s\"", fname);
 		close(fd);
 
 		/* parse position from file */
-		XLogFromFileName(fname, &private.timeline, &segno, WalSegSz);
+		XLogFromFileName(fname, &private.timeline, &segno, private.segsize);
 
 		if (!XLogRecPtrIsValid(private.startptr))
-			XLogSegNoOffsetToRecPtr(segno, 0, WalSegSz, private.startptr);
-		else if (!XLByteInSeg(private.startptr, segno, WalSegSz))
+			XLogSegNoOffsetToRecPtr(segno, 0, private.segsize, private.startptr);
+		else if (!XLByteInSeg(private.startptr, segno, private.segsize))
 		{
 			pg_log_error("start WAL location %X/%08X is not inside file \"%s\"",
 						 LSN_FORMAT_ARGS(private.startptr),
@@ -1159,7 +1159,7 @@ main(int argc, char **argv)
 
 		/* no second file specified, set end position */
 		if (!(optind + 1 < argc) && !XLogRecPtrIsValid(private.endptr))
-			XLogSegNoOffsetToRecPtr(segno + 1, 0, WalSegSz, private.endptr);
+			XLogSegNoOffsetToRecPtr(segno + 1, 0, private.segsize, private.endptr);
 
 		/* parse ENDSEG if passed */
 		if (optind + 1 < argc)
@@ -1175,14 +1175,14 @@ main(int argc, char **argv)
 			close(fd);
 
 			/* parse position from file */
-			XLogFromFileName(fname, &private.timeline, &endsegno, WalSegSz);
+			XLogFromFileName(fname, &private.timeline, &endsegno, private.segsize);
 
 			if (endsegno < segno)
 				pg_fatal("ENDSEG %s is before STARTSEG %s",
 						 argv[optind + 1], argv[optind]);
 
 			if (!XLogRecPtrIsValid(private.endptr))
-				XLogSegNoOffsetToRecPtr(endsegno + 1, 0, WalSegSz,
+				XLogSegNoOffsetToRecPtr(endsegno + 1, 0, private.segsize,
 										private.endptr);
 
 			/* set segno to endsegno for check of --end */
@@ -1190,8 +1190,8 @@ main(int argc, char **argv)
 		}
 
 
-		if (!XLByteInSeg(private.endptr, segno, WalSegSz) &&
-			private.endptr != (segno + 1) * WalSegSz)
+		if (!XLByteInSeg(private.endptr, segno, private.segsize) &&
+			private.endptr != (segno + 1) * private.segsize)
 		{
 			pg_log_error("end WAL location %X/%08X is not inside file \"%s\"",
 						 LSN_FORMAT_ARGS(private.endptr),
@@ -1200,7 +1200,7 @@ main(int argc, char **argv)
 		}
 	}
 	else
-		waldir = identify_target_directory(waldir, NULL, &WalSegSz);
+		waldir = identify_target_directory(waldir, NULL, &private.segsize);
 
 	/* we don't know what to print */
 	if (!XLogRecPtrIsValid(private.startptr))
@@ -1213,7 +1213,7 @@ main(int argc, char **argv)
 
 	/* we have everything we need, start reading */
 	xlogreader_state =
-		XLogReaderAllocate(WalSegSz, waldir,
+		XLogReaderAllocate(private.segsize, waldir,
 						   XL_ROUTINE(.page_read = WALDumpReadPage,
 									  .segment_open = WALDumpOpenSegment,
 									  .segment_close = WALDumpCloseSegment),
@@ -1234,7 +1234,7 @@ main(int argc, char **argv)
 	 * a segment (e.g. we were used in file mode).
 	 */
 	if (first_record != private.startptr &&
-		XLogSegmentOffset(private.startptr, WalSegSz) != 0)
+		XLogSegmentOffset(private.startptr, private.segsize) != 0)
 		pg_log_info(ngettext("first record is after %X/%08X, at %X/%08X, skipping over %u byte",
 							 "first record is after %X/%08X, at %X/%08X, skipping over %u bytes",
 							 (first_record - private.startptr)),
diff --git a/src/bin/pg_waldump/pg_waldump.h b/src/bin/pg_waldump/pg_waldump.h
index 64a9109229e..013b051506f 100644
--- a/src/bin/pg_waldump/pg_waldump.h
+++ b/src/bin/pg_waldump/pg_waldump.h
@@ -17,6 +17,7 @@
 typedef struct XLogDumpPrivate
 {
 	TimeLineID	timeline;
+	int			segsize;
 	XLogRecPtr	startptr;
 	XLogRecPtr	endptr;
 	bool		endptr_reached;
-- 
2.47.1

