v3-0001-archive_waldump-skip-hash-lookup-and-tighten-writ.patch

application/x-patch

Filename: v3-0001-archive_waldump-skip-hash-lookup-and-tighten-writ.patch
Type: application/x-patch
Part: 0
Message: Re: pg_waldump: support decoding of WAL inside tarfile

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: format-patch
Series: patch v3-0001
Subject: archive_waldump: skip hash lookup and tighten write_fp invariant
File+
src/bin/pg_waldump/archive_waldump.c 11 1
From b3bfdac9e425f4cb9fd7d7b6c698dd1607b737ee Mon Sep 17 00:00:00 2001
From: Amul Sul <sulamul@gmail.com>
Date: Sat, 21 Mar 2026 22:27:22 +0530
Subject: [PATCH v3 1/3] archive_waldump: skip hash lookup and tighten write_fp
 invariant

In get_archive_wal_entry(), when the streamer is still mid-segment
(entry == cur_file), jump directly to read_more instead of looping back
to the top and performing a hash table lookup that is guaranteed to fail.
---
 src/bin/pg_waldump/archive_waldump.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/src/bin/pg_waldump/archive_waldump.c b/src/bin/pg_waldump/archive_waldump.c
index b078c2d6960..ee292b6dc8d 100644
--- a/src/bin/pg_waldump/archive_waldump.c
+++ b/src/bin/pg_waldump/archive_waldump.c
@@ -484,6 +484,7 @@ get_archive_wal_entry(const char *fname, XLogDumpPrivate *privateInfo)
 		 */
 		entry = privateInfo->cur_file;
 
+read_more:
 		/*
 		 * Fetch more data either when no current file is being tracked or
 		 * when its buffer has been fully flushed to the temporary file.
@@ -525,11 +526,20 @@ get_archive_wal_entry(const char *fname, XLogDumpPrivate *privateInfo)
 		 * file handle so data is flushed to disk before the next segment
 		 * starts writing to a different handle.
 		 */
-		if (entry != privateInfo->cur_file && write_fp != NULL)
+		if (entry != privateInfo->cur_file)
 		{
+			Assert(write_fp);
 			fclose(write_fp);
 			write_fp = NULL;
 		}
+		else
+			/*
+			 * The file being written hasn't been completed. We must finish
+			 * extracting it before performing the hash lookup; otherwise, the
+			 * lookup might return without flushing the current segment buffer,
+			 * leaving the file open and incomplete on disk.
+			 */
+			goto read_more;
 	}
 
 	/* Requested WAL segment not found */
-- 
2.47.1