From 8aeea51f353214d1c98a2d21a51984a6875f3cde Mon Sep 17 00:00:00 2001
From: Melanie Plageman <melanieplageman@gmail.com>
Date: Wed, 29 Apr 2026 12:08:26 -0400
Subject: [PATCH v7] Assert that redo accounts for each WAL block reference

A redo routine that overlooks a registered block can miss a full-page
image that must be restored. In assertion builds, verify that redo
accounts for each registered block ID exactly once, either by reading it
or explicitly marking it as skipped.

Author: Andres Freund <andres@anarazel.de>
Co-authored-by: Melanie Plageman <melanieplageman@gmail.com>
Discussion: https://postgr.es/m/oqcsevg35xjan2327x5kdfth6q4fgeqboxfo3v3imeyih2uiny%406sez5dzxl6nt
---
 src/backend/access/hash/hash_xlog.c       |  6 ++++++
 src/backend/access/transam/xlog.c         |  2 ++
 src/backend/access/transam/xlogreader.c   |  4 ++++
 src/backend/access/transam/xlogrecovery.c | 14 ++++++++++++++
 src/backend/access/transam/xlogutils.c    |  6 ++++++
 src/include/access/xlogreader.h           |  4 ++++
 src/include/access/xlogutils.h            | 13 +++++++++++++
 7 files changed, 49 insertions(+)

diff --git a/src/backend/access/hash/hash_xlog.c b/src/backend/access/hash/hash_xlog.c
index 46047c6ea27..92a35b1c410 100644
--- a/src/backend/access/hash/hash_xlog.c
+++ b/src/backend/access/hash/hash_xlog.c
@@ -510,6 +510,8 @@ hash_xlog_move_page_contents(XLogReaderState *record)
 			 */
 			(void) XLogReadBufferForRedoExtended(record, 0, RBM_NORMAL, true, &bucketbuf);
 		}
+		else
+			XLogSkipBlockForRedo(record, 0);
 
 		action = XLogReadBufferForRedo(record, 1, &writebuf);
 	}
@@ -648,6 +650,8 @@ hash_xlog_squeeze_page(XLogReaderState *record)
 			 */
 			(void) XLogReadBufferForRedoExtended(record, 0, RBM_NORMAL, true, &bucketbuf);
 		}
+		else
+			XLogSkipBlockForRedo(record, 0);
 
 		if (xldata->ntups > 0 || xldata->is_prev_bucket_same_wrt)
 			action = XLogReadBufferForRedo(record, 1, &writebuf);
@@ -890,6 +894,8 @@ hash_xlog_delete(XLogReaderState *record)
 			 */
 			(void) XLogReadBufferForRedoExtended(record, 0, RBM_NORMAL, true, &bucketbuf);
 		}
+		else
+			XLogSkipBlockForRedo(record, 0);
 
 		action = XLogReadBufferForRedo(record, 1, &deletebuf);
 	}
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fd37ea4f607..f84f42ef158 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -9551,6 +9551,8 @@ xlog_redo(XLogReaderState *record)
 			{
 				if (info == XLOG_FPI)
 					elog(ERROR, "XLOG_FPI record did not contain a full-page image");
+
+				XLogSkipBlockForRedo(record, block_id);
 				continue;
 			}
 
diff --git a/src/backend/access/transam/xlogreader.c b/src/backend/access/transam/xlogreader.c
index e03667fda68..9ff82df8136 100644
--- a/src/backend/access/transam/xlogreader.c
+++ b/src/backend/access/transam/xlogreader.c
@@ -1897,6 +1897,10 @@ DecodeXLogRecord(XLogReaderState *state,
 
 			blk = &decoded->blocks[block_id];
 			blk->in_use = true;
+#ifdef USE_ASSERT_CHECKING
+			blk->handled = false;
+#endif
+
 			blk->apply_image = false;
 
 			COPY_HEADER_FIELD(&fork_flags, sizeof(uint8));
diff --git a/src/backend/access/transam/xlogrecovery.c b/src/backend/access/transam/xlogrecovery.c
index e091a3e178b..da5718b0b6d 100644
--- a/src/backend/access/transam/xlogrecovery.c
+++ b/src/backend/access/transam/xlogrecovery.c
@@ -1987,6 +1987,20 @@ ApplyWalRecord(XLogReaderState *xlogreader, XLogRecord *record, TimeLineID *repl
 	/* Now apply the WAL record itself */
 	GetRmgr(record->xl_rmid).rm_redo(xlogreader);
 
+	/*
+	 * Verify that redo accounted for every block reference. Ignoring a block
+	 * reference can leave a full-page image unrestored or a relation shorter
+	 * than it should be.
+	 */
+#ifdef USE_ASSERT_CHECKING
+	for (int block_id = 0; block_id <= xlogreader->record->max_block_id; block_id++)
+	{
+		DecodedBkpBlock *blk = &xlogreader->record->blocks[block_id];
+
+		Assert(!blk->in_use || blk->handled);
+	}
+#endif
+
 	/*
 	 * After redo, check whether the backup pages associated with the WAL
 	 * record are consistent with the existing pages. This check is done only
diff --git a/src/backend/access/transam/xlogutils.c b/src/backend/access/transam/xlogutils.c
index ff71e14015d..f56435dc9c8 100644
--- a/src/backend/access/transam/xlogutils.c
+++ b/src/backend/access/transam/xlogutils.c
@@ -381,6 +381,12 @@ XLogReadBufferForRedoExtended(XLogReaderState *record,
 			 block_id);
 	}
 
+#ifdef USE_ASSERT_CHECKING
+	/* Shouldn't have multiple read references to block */
+	Assert(!XLogRecGetBlock(record, block_id)->handled);
+	XLogRecGetBlock(record, block_id)->handled = true;
+#endif
+
 	/*
 	 * Make sure that if the block is marked with WILL_INIT, the caller is
 	 * going to initialize it. And vice versa.
diff --git a/src/include/access/xlogreader.h b/src/include/access/xlogreader.h
index e48a0db9ce9..ab15b863bfe 100644
--- a/src/include/access/xlogreader.h
+++ b/src/include/access/xlogreader.h
@@ -147,6 +147,10 @@ typedef struct
 	bool		has_data;
 	char	   *data;
 	uint16		data_len;
+
+#ifdef USE_ASSERT_CHECKING
+	bool		handled;		/* redo accounted for this block reference */
+#endif
 } DecodedBkpBlock;
 
 /*
diff --git a/src/include/access/xlogutils.h b/src/include/access/xlogutils.h
index 0c6c7410069..67ba908b347 100644
--- a/src/include/access/xlogutils.h
+++ b/src/include/access/xlogutils.h
@@ -94,6 +94,19 @@ extern XLogRedoAction XLogReadBufferForRedoExtended(XLogReaderState *record,
 													ReadBufferMode mode, bool get_cleanup_lock,
 													Buffer *buf);
 
+/* Account for a block reference that redo deliberately need not read. */
+static inline void
+XLogSkipBlockForRedo(XLogReaderState *record, uint8 block_id)
+{
+#ifdef USE_ASSERT_CHECKING
+	Assert(XLogRecHasBlockRef(record, block_id));
+	Assert(!XLogRecBlockImageApply(record, block_id));
+	Assert(!XLogRecHasBlockData(record, block_id));
+	Assert(!XLogRecGetBlock(record, block_id)->handled);
+	XLogRecGetBlock(record, block_id)->handled = true;
+#endif
+}
+
 extern Buffer XLogReadBufferExtended(RelFileLocator rlocator, ForkNumber forknum,
 									 BlockNumber blkno, ReadBufferMode mode,
 									 Buffer recent_buffer);
-- 
2.43.0

