From 6ad7c42513c982c5f328661c32427c6b6f1dae01 Mon Sep 17 00:00:00 2001
From: "Chao Li (Evan)" <lic@highgo.com>
Date: Tue, 2 Sep 2025 16:29:56 +0800
Subject: [PATCH v1] Fix pg_waldump to exit cleanly at end of WAL

Previously, running pg_waldump without the -e option would always
produce an error message when reaching the end of the WAL, because
the parser encountered a zero-length record. This patch changes
the behavior so that pg_waldump exits gracefully when the last
valid WAL record has been processed, without printing an unnecessary
error.

Author: Chao Li <lic@highgo.com>
---
 src/backend/access/transam/xlogreader.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/src/backend/access/transam/xlogreader.c b/src/backend/access/transam/xlogreader.c
index dcc8d4f9c1b..12a8317b301 100644
--- a/src/backend/access/transam/xlogreader.c
+++ b/src/backend/access/transam/xlogreader.c
@@ -646,6 +646,15 @@ restart:
 	record = (XLogRecord *) (state->readBuf + RecPtr % XLOG_BLCKSZ);
 	total_len = record->xl_tot_len;
 
+	/*
+	 * When xl_tot_len is zero, we must reach the end of WAL.  Let's fail
+	 * without setting a error message.
+	*/
+	if (total_len == 0)
+	{
+		goto err;
+	}
+
 	/*
 	 * If the whole record header is on this page, validate it immediately.
 	 * Otherwise do just a basic sanity check on xl_tot_len, and validate the
-- 
2.39.5 (Apple Git-154)

