From 26447cb08770c337c7a1cf6189c9744c42ffb90f Mon Sep 17 00:00:00 2001
From: Melanie Plageman <melanieplageman@gmail.com>
Date: Mon, 23 Nov 2026 12:27:13 -0500
Subject: [PATCH v1] Skip uninitialized pages during WAL consistency checking

After a heap is truncated and re-extended, redo can find an all-zero
block and skip it, but wal consitency checking's reread passes it to the
heap mask, which rejects the zero header. Return InvalidBuffer instead,
as for a block beyond EOF. Redo still tracks the invalid reference.

RBM_NORMAL_NO_LOG originally returned zero pages for a btree cleanup
lock scan that did not inspect their contents. That caller is gone.
---
 src/backend/access/transam/xlogutils.c       | 10 +++----
 src/test/recovery/t/036_truncated_dropped.pl | 30 ++++++++++++++++++++
 2 files changed, 35 insertions(+), 5 deletions(-)

diff --git a/src/backend/access/transam/xlogutils.c b/src/backend/access/transam/xlogutils.c
index ff71e14015d..e14275f4bca 100644
--- a/src/backend/access/transam/xlogutils.c
+++ b/src/backend/access/transam/xlogutils.c
@@ -467,9 +467,8 @@ XLogReadBufferForRedoExtended(XLogReaderState *record,
  * In RBM_ZERO_* modes, if the page doesn't exist, the relation is extended
  * with all-zeroes pages up to the given block number.
  *
- * In RBM_NORMAL_NO_LOG mode, we return InvalidBuffer if the page doesn't
- * exist, and we don't check for all-zeroes.  Thus, no log entry is made
- * to imply that the page should be dropped or truncated later.
+ * RBM_NORMAL_NO_LOG also returns InvalidBuffer for a missing or uninitialized
+ * page, but does not record an invalid-page reference.
  *
  * Optionally, recent_buffer can be used to provide a hint about the location
  * of the page in the buffer pool; it does not have to be correct, but avoids
@@ -544,7 +543,7 @@ XLogReadBufferExtended(RelFileLocator rlocator, ForkNumber forknum,
 	}
 
 recent_buffer_fast_path:
-	if (mode == RBM_NORMAL)
+	if (mode == RBM_NORMAL || mode == RBM_NORMAL_NO_LOG)
 	{
 		/* check that page has been initialized */
 		Page		page = BufferGetPage(buffer);
@@ -557,7 +556,8 @@ XLogReadBufferExtended(RelFileLocator rlocator, ForkNumber forknum,
 		if (PageIsNew(page))
 		{
 			ReleaseBuffer(buffer);
-			log_invalid_page(rlocator, forknum, blkno, true);
+			if (mode == RBM_NORMAL)
+				log_invalid_page(rlocator, forknum, blkno, true);
 			return InvalidBuffer;
 		}
 	}
diff --git a/src/test/recovery/t/036_truncated_dropped.pl b/src/test/recovery/t/036_truncated_dropped.pl
index bf7bba39960..eb577a29d6e 100644
--- a/src/test/recovery/t/036_truncated_dropped.pl
+++ b/src/test/recovery/t/036_truncated_dropped.pl
@@ -133,4 +133,34 @@ is( $node->safe_psql(
 $node->safe_psql('postgres', 'DROP TABLE truncme');
 
 
+# Test WAL consistency checking when a truncated heap block is reused. The
+# DELETE must have a consistency image but no image to apply during redo.
+# Keep the background writer from flushing the re-inserted tuple before the
+# crash, so that redo of the DELETE finds the zero page left by extension.
+$node->append_conf(
+	'postgresql.conf', qq[
+full_page_writes = off
+bgwriter_lru_maxpages = 0
+]);
+$node->reload();
+
+is( $node->safe_psql(
+		'postgres', q[
+CREATE TABLE truncme(i int);
+INSERT INTO truncme VALUES (1);
+CHECKPOINT;
+SET wal_consistency_checking = 'heap';
+DELETE FROM truncme;
+VACUUM (TRUNCATE TRUE) truncme;
+SELECT pg_relation_size('truncme');
+]),
+	'0', 'VACUUM truncated the heap to zero blocks');
+
+$node->safe_psql('postgres', 'INSERT INTO truncme VALUES (2)');
+$node->stop('immediate');
+$node->start();
+
+is($node->safe_psql('postgres', 'TABLE truncme'),
+	'2', 'WAL consistency checking skips the uninitialized heap block');
+
 done_testing();
-- 
2.43.0

