v1-0001-Fix-bug-in-pg_wal_summary_contents.patch

text/plain

Filename: v1-0001-Fix-bug-in-pg_wal_summary_contents.patch
Type: text/plain
Part: 0
Message: pg_wal_summary_contents() and pg_walsummary may return different results on the same WAL summary file

Patch

Format: format-patch
Series: patch v1-0001
Subject: Fix bug in pg_wal_summary_contents().
File+
src/backend/backup/walsummaryfuncs.c 17 17
From 79a1197ea3cdc69b7dda21287e612b0c9f532b55 Mon Sep 17 00:00:00 2001
From: Fujii Masao <fujii@postgresql.org>
Date: Wed, 3 Jul 2024 15:50:30 +0900
Subject: [PATCH v1] Fix bug in pg_wal_summary_contents().

Previously, pg_wal_summary_contents() might fail to report rows with
is_limit_block = true, leading to discrepancies between
pg_wal_summary_contents() and the pg_walsummary command
on the same WAL summary file. This commit resolves the issue.
---
 src/backend/backup/walsummaryfuncs.c | 34 ++++++++++++++--------------
 1 file changed, 17 insertions(+), 17 deletions(-)

diff --git a/src/backend/backup/walsummaryfuncs.c b/src/backend/backup/walsummaryfuncs.c
index f082488b33..7db7f131ce 100644
--- a/src/backend/backup/walsummaryfuncs.c
+++ b/src/backend/backup/walsummaryfuncs.c
@@ -118,6 +118,23 @@ pg_wal_summary_contents(PG_FUNCTION_ARGS)
 		values[2] = ObjectIdGetDatum(rlocator.dbOid);
 		values[3] = Int16GetDatum((int16) forknum);
 
+		/*
+		 * If the limit block is not InvalidBlockNumber, emit an extra row
+		 * with that block number and limit_block = true.
+		 *
+		 * There is no point in doing this when the limit_block is
+		 * InvalidBlockNumber, because no block with that number or any
+		 * higher number can ever exist.
+		 */
+		if (BlockNumberIsValid(limit_block))
+		{
+			values[4] = Int64GetDatum((int64) limit_block);
+			values[5] = BoolGetDatum(true);
+
+			tuple = heap_form_tuple(rsi->setDesc, values, nulls);
+			tuplestore_puttuple(rsi->setResult, tuple);
+		}
+
 		/* Loop over blocks within the current relation fork. */
 		while (1)
 		{
@@ -143,23 +160,6 @@ pg_wal_summary_contents(PG_FUNCTION_ARGS)
 				tuple = heap_form_tuple(rsi->setDesc, values, nulls);
 				tuplestore_puttuple(rsi->setResult, tuple);
 			}
-
-			/*
-			 * If the limit block is not InvalidBlockNumber, emit an extra row
-			 * with that block number and limit_block = true.
-			 *
-			 * There is no point in doing this when the limit_block is
-			 * InvalidBlockNumber, because no block with that number or any
-			 * higher number can ever exist.
-			 */
-			if (BlockNumberIsValid(limit_block))
-			{
-				values[4] = Int64GetDatum((int64) limit_block);
-				values[5] = BoolGetDatum(true);
-
-				tuple = heap_form_tuple(rsi->setDesc, values, nulls);
-				tuplestore_puttuple(rsi->setResult, tuple);
-			}
 		}
 	}
 
-- 
2.45.1