On Wed, Jul 15, 2026 at 5:58 PM Melanie Plageman
<[email protected]> wrote:
>
> There are
> three patches remaining that I proposed for master that do assorted
> validations: visibility map and PD_ALL_VISIBLE flag validations and
> some validation in the WAL machinery that registered buffers get used.
> I plan to commit 0001 and 0003 once my other patches have gone through
> the buildfarm. I'm considering holding onto 0002 until I add
> WAL-logging to the VM corruption repair code in
> heap_page_fix_vm_corruption() -- without which,
> wal_consistency_checking could fail for pages that had corruption
> fixed.

0001 in this set is pushed. 0002 can't be pushed because we don't
always WAL-log updates to only PD_ALL_VISIBLE and for other reasons
documented in [1]. A new version of 0003 is attached here as v7. I
plan to push later today since the earlier version was already
reviewed.

- Melanie

[1] 
https://www.postgresql.org/message-id/flat/CAAKRu_bozJHE%3DZa4o1yZaPmW_rjG5DrUSDfEjOZkjvmaeN%2BEEA%40mail.gmail.com
From 8aeea51f353214d1c98a2d21a51984a6875f3cde Mon Sep 17 00:00:00 2001
From: Melanie Plageman <[email protected]>
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 <[email protected]>
Co-authored-by: Melanie Plageman <[email protected]>
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

Reply via email to