Hello,
Thanks Vignesh for catching that, and the 068b test.
Also, thanks Hayato-san for the review and comments.
Agreed, InHotStandby becomes true as soon as the first
XLOG_RUNNING_XACTS is replayed, well before recovery reaches
consistency, so with hot_standby = on redo can still read the torn page
in that window. Thanks for the 068b reproducer.
v7 uses HotStandbyActiveInReplay() instead, as you suggested. That is
true only after recovery has reached consistency and the postmaster has
been told connections can start, so there is no scan to serialize
against before then, and by then any full-page image that repairs the
page has been replayed. I made HotStandbyActiveInReplay() extern for
this. 068b now passes, and so does 068a.
I folded your 068b into 0001 next to 068a, with one change: the primary
bucket page is found from blkref #0 or #1, since after 0002 the page is
registered as block 0 rather than 1 when it is the modified page, and
the test runs against the whole series as well as master.
I've applied both of Hayato-san's comments in 0002:
hash_xlog_lock_bucket() is renamed to hash_xlog_lock_primary_bucket(),
and the "block 0 is absent" comment now reads:
/*
* If the primary bucket page is modified, it is registered as block 0
* and read for redo. Otherwise, acquire its cleanup lock separately.
*/
v7:
0001 Fix hash index redo reading a torn primary bucket page
(now with 068a and 068b, and HotStandbyActiveInReplay)
0002 Don't register pin-only primary bucket pages in hash WAL records
(hash_xlog_lock_primary_bucket, carries the same guard)
0003 Add a recovery test for the hash WAL pin-only bucket page fix
0004 Remove REGBUF_NO_CHANGE
0001 stands on its own against master; the rest build on it.
best.
-greg
From 29eb96c75c70d9b593f8acad54c5aec1704c81c8 Mon Sep 17 00:00:00 2001
From: Greg Burd <[email protected]>
Date: Thu, 29 Oct 2026 10:30:14 -0400
Subject: [PATCH v7 2/4] Don't register pin-only primary bucket pages in hash
WAL records
Replay of XLOG_HASH_DELETE, XLOG_HASH_MOVE_PAGE_CONTENTS and
XLOG_HASH_SQUEEZE_PAGE must take a cleanup lock on the primary bucket
page so that no scan on a hot standby is in progress in the bucket, and
none can start, while tuples are removed or moved. That lock was
obtained by registering the page (pin-only) with REGBUF_NO_CHANGE; redo
never reads data from the block, the registration existed only for the
lock. That broke the rule that a registered buffer is one the caller
holds exclusively locked and dirty, and under wal_consistency_checking
it forced a full-page image of a page a concurrent inserter could still
modify, so the record CRC was computed over shifting data.
Stop registering the primary bucket page when the record does not change
it. When the page is not one of the pages the record changes, the
operation holds only a pin on it, so the record carries the page's block
number in its main data and replay reads that block and takes a cleanup
lock on it directly, at the same point and in the same order as before.
The block number is appended only in that case, so the records are no
larger than before in the common case where the primary bucket page is
registered.
After this change hash registers only buffers it holds exclusively
locked and dirty, and there are no REGBUF_NO_CHANGE users left.
The three record formats change, so bump XLOG_PAGE_MAGIC; master only.
Co-authored-by: Vignesh C <[email protected]>
Suggested-by: Amit Kapila <[email protected]>
Discussion: https://postgr.es/m/auNs8b_vS2OPdTL8@floki
---
src/backend/access/hash/README | 9 ++
src/backend/access/hash/hash.c | 17 ++--
src/backend/access/hash/hash_xlog.c | 139 ++++++++++++++++------------
src/backend/access/hash/hashovfl.c | 100 +++++++++-----------
src/include/access/hash_xlog.h | 37 +++++---
src/include/access/xlog_internal.h | 2 +-
6 files changed, 166 insertions(+), 138 deletions(-)
diff --git a/src/backend/access/hash/README b/src/backend/access/hash/README
index fc9031117c9..7774e1d2fe0 100644
--- a/src/backend/access/hash/README
+++ b/src/backend/access/hash/README
@@ -641,6 +641,15 @@ entire bucket. After recovery, the operations will work correctly, but
the index will remain bloated and this can impact performance of read and
insert operations until the next vacuum squeeze the bucket completely.
+Replay of the delete, move-page-contents and squeeze-page records takes a
+cleanup lock on the primary bucket page before touching any other page, so
+that no scan on a hot standby is in progress in the bucket or can start in
+it while tuples are removed or moved; such a scan could otherwise miss
+tuples or return them twice. When the primary bucket page is not one of
+the pages the record changes, the operation holds only a pin on it, so it
+is not registered in the record. The record then carries its block number
+instead, and replay locks that block directly.
+
Other Notes
-----------
diff --git a/src/backend/access/hash/hash.c b/src/backend/access/hash/hash.c
index 63846c10840..7a79030303e 100644
--- a/src/backend/access/hash/hash.c
+++ b/src/backend/access/hash/hash.c
@@ -909,19 +909,16 @@ hashbucketcleanup(Relation rel, Bucket cur_bucket, Buffer bucket_buf,
XLogRegisterData(&xlrec, SizeOfHashDelete);
/*
- * bucket buffer was not changed, but still needs to be
- * registered to ensure that we can acquire a cleanup lock on
- * it during replay.
+ * If buf is an overflow page, the primary bucket page is not
+ * changed and we hold only a pin on it, so it is not
+ * registered. Append its block number instead, and replay
+ * takes a cleanup lock on it. It is not needed otherwise.
*/
if (!xlrec.is_primary_bucket_page)
- {
- uint8 flags = REGBUF_STANDARD | REGBUF_NO_IMAGE | REGBUF_NO_CHANGE;
-
- XLogRegisterBuffer(0, bucket_buf, flags);
- }
+ XLogRegisterData(&bucket_blkno, sizeof(BlockNumber));
- XLogRegisterBuffer(1, buf, REGBUF_STANDARD);
- XLogRegisterBufData(1, deletable,
+ XLogRegisterBuffer(0, buf, REGBUF_STANDARD);
+ XLogRegisterBufData(0, deletable,
ndeletable * sizeof(OffsetNumber));
recptr = XLogInsert(RM_HASH_ID, XLOG_HASH_DELETE);
diff --git a/src/backend/access/hash/hash_xlog.c b/src/backend/access/hash/hash_xlog.c
index e1318bf97db..bad5bccb4e1 100644
--- a/src/backend/access/hash/hash_xlog.c
+++ b/src/backend/access/hash/hash_xlog.c
@@ -470,6 +470,52 @@ hash_xlog_split_complete(XLogReaderState *record)
UnlockReleaseBuffer(newbuf);
}
+/*
+ * Take a cleanup lock on the primary bucket page of the bucket that a
+ * squeeze or delete record operates on, when that page is not one of the
+ * pages the record changes and so is not registered in it. Its block number
+ * is stored in the main data of the record, right after the fixed-size struct
+ * of rec_size bytes.
+ *
+ * This serializes replay against scans on a hot standby, which pin the
+ * primary bucket page for their whole duration; see the comments in the
+ * callers. The page is not modified, so nothing is done with it besides
+ * locking it. block_id must be a block of the same index registered in
+ * the record.
+ */
+static Buffer
+hash_xlog_lock_primary_bucket(XLogReaderState *record, uint8 block_id, Size rec_size)
+{
+ RelFileLocator rlocator;
+ ForkNumber forknum;
+ BlockNumber bucket_blkno;
+ Buffer buf;
+
+ Assert(XLogRecGetDataLen(record) == rec_size + sizeof(BlockNumber));
+ memcpy(&bucket_blkno, XLogRecGetData(record) + rec_size, sizeof(BlockNumber));
+
+ /*
+ * The cleanup lock on the primary bucket page serializes replay against
+ * scans on a hot standby, which pin the page for their whole duration.
+ * Scans can run only once hot standby accepts queries, so until then we
+ * need not read or lock the page. Skipping the read also sidesteps a torn
+ * copy of the page whose repairing full-page image comes later in the WAL:
+ * that hazard exists only before consistency is reached, and queries are
+ * accepted only after that, so once HotStandbyActiveInReplay() is true any
+ * earlier image has already been replayed. Redo never uses the page's
+ * contents here in any case.
+ */
+ if (!HotStandbyActiveInReplay())
+ return InvalidBuffer;
+
+ XLogRecGetBlockTag(record, block_id, &rlocator, &forknum, NULL);
+ buf = XLogReadBufferExtended(rlocator, forknum, bucket_blkno,
+ RBM_NORMAL, InvalidBuffer);
+ if (BufferIsValid(buf))
+ LockBufferForCleanup(buf);
+ return buf;
+}
+
/*
* replay move of page contents for squeeze operation of hash index
*/
@@ -491,24 +537,12 @@ hash_xlog_move_page_contents(XLogReaderState *record)
* can miss some records or show the same record multiple times.
*/
if (xldata->is_prim_bucket_same_wrt)
- action = XLogReadBufferForRedoExtended(record, 1, RBM_NORMAL, true, &writebuf);
+ action = XLogReadBufferForRedoExtended(record, 0, RBM_NORMAL, true, &writebuf);
else
{
- /*
- * The cleanup lock on the primary bucket page serializes replay
- * against scans on a hot standby, which pin the page for their whole
- * duration. Scans can run only once hot standby accepts queries, so
- * until then we need not read or lock the page. Skipping the read
- * also sidesteps a torn copy of the page whose repairing full-page
- * image comes later in the WAL: that hazard exists only before
- * consistency is reached, and queries are accepted only after that,
- * so once HotStandbyActiveInReplay() is true any earlier image has
- * already been replayed. We do not use the page contents here.
- */
- if (HotStandbyActiveInReplay())
- (void) XLogReadBufferForRedoExtended(record, 0, RBM_NORMAL, true, &bucketbuf);
+ bucketbuf = hash_xlog_lock_primary_bucket(record, 0, SizeOfHashMovePageContents);
- action = XLogReadBufferForRedo(record, 1, &writebuf);
+ action = XLogReadBufferForRedo(record, 0, &writebuf);
}
/* replay the record for adding entries in overflow buffer */
@@ -520,7 +554,7 @@ hash_xlog_move_page_contents(XLogReaderState *record)
Size datalen;
uint16 ninserted = 0;
- data = begin = XLogRecGetBlockData(record, 1, &datalen);
+ data = begin = XLogRecGetBlockData(record, 0, &datalen);
writepage = BufferGetPage(writebuf);
@@ -559,13 +593,13 @@ hash_xlog_move_page_contents(XLogReaderState *record)
}
/* replay the record for deleting entries from overflow buffer */
- if (XLogReadBufferForRedo(record, 2, &deletebuf) == BLK_NEEDS_REDO)
+ if (XLogReadBufferForRedo(record, 1, &deletebuf) == BLK_NEEDS_REDO)
{
Page page;
char *ptr;
Size len;
- ptr = XLogRecGetBlockData(record, 2, &len);
+ ptr = XLogRecGetBlockData(record, 1, &len);
page = BufferGetPage(deletebuf);
@@ -626,25 +660,26 @@ hash_xlog_squeeze_page(XLogReaderState *record)
* can miss some records or show the same record multiple times.
*/
if (xldata->is_prim_bucket_same_wrt)
- action = XLogReadBufferForRedoExtended(record, 1, RBM_NORMAL, true, &writebuf);
- else
{
/*
- * The cleanup lock on the primary bucket page serializes replay
- * against scans on a hot standby, which pin the page for their whole
- * duration. Scans can run only once hot standby accepts queries, so
- * until then we need not read or lock the page. Skipping the read
- * also sidesteps a torn copy of the page whose repairing full-page
- * image comes later in the WAL: that hazard exists only before
- * consistency is reached, and queries are accepted only after that,
- * so once HotStandbyActiveInReplay() is true any earlier image has
- * already been replayed. We do not use the page contents here.
+ * If the primary bucket page is modified, it is registered as block
+ * 0 and read for redo. Otherwise, acquire its cleanup lock
+ * separately.
*/
- if (HotStandbyActiveInReplay())
- (void) XLogReadBufferForRedoExtended(record, 0, RBM_NORMAL, true, &bucketbuf);
+ if (XLogRecHasBlockRef(record, 0))
+ action = XLogReadBufferForRedoExtended(record, 0, RBM_NORMAL, true, &writebuf);
+ else
+ {
+ bucketbuf = hash_xlog_lock_primary_bucket(record, 1, SizeOfHashSqueezePage);
+ action = BLK_NOTFOUND;
+ }
+ }
+ else
+ {
+ bucketbuf = hash_xlog_lock_primary_bucket(record, 1, SizeOfHashSqueezePage);
if (xldata->ntups > 0 || xldata->is_prev_bucket_same_wrt)
- action = XLogReadBufferForRedo(record, 1, &writebuf);
+ action = XLogReadBufferForRedo(record, 0, &writebuf);
else
action = BLK_NOTFOUND;
}
@@ -659,7 +694,7 @@ hash_xlog_squeeze_page(XLogReaderState *record)
uint16 ninserted = 0;
bool mod_wbuf = false;
- data = begin = XLogRecGetBlockData(record, 1, &datalen);
+ data = begin = XLogRecGetBlockData(record, 0, &datalen);
writepage = BufferGetPage(writebuf);
@@ -725,7 +760,7 @@ hash_xlog_squeeze_page(XLogReaderState *record)
}
/* replay the record for initializing overflow buffer */
- if (XLogReadBufferForRedo(record, 2, &ovflbuf) == BLK_NEEDS_REDO)
+ if (XLogReadBufferForRedo(record, 1, &ovflbuf) == BLK_NEEDS_REDO)
{
Page ovflpage;
HashPageOpaque ovflopaque;
@@ -750,7 +785,7 @@ hash_xlog_squeeze_page(XLogReaderState *record)
/* replay the record for page previous to the freed overflow page */
if (!xldata->is_prev_bucket_same_wrt &&
- XLogReadBufferForRedo(record, 3, &prevbuf) == BLK_NEEDS_REDO)
+ XLogReadBufferForRedo(record, 2, &prevbuf) == BLK_NEEDS_REDO)
{
Page prevpage = BufferGetPage(prevbuf);
HashPageOpaque prevopaque = HashPageGetOpaque(prevpage);
@@ -764,11 +799,11 @@ hash_xlog_squeeze_page(XLogReaderState *record)
UnlockReleaseBuffer(prevbuf);
/* replay the record for page next to the freed overflow page */
- if (XLogRecHasBlockRef(record, 4))
+ if (XLogRecHasBlockRef(record, 3))
{
Buffer nextbuf;
- if (XLogReadBufferForRedo(record, 4, &nextbuf) == BLK_NEEDS_REDO)
+ if (XLogReadBufferForRedo(record, 3, &nextbuf) == BLK_NEEDS_REDO)
{
Page nextpage = BufferGetPage(nextbuf);
HashPageOpaque nextopaque = HashPageGetOpaque(nextpage);
@@ -795,7 +830,7 @@ hash_xlog_squeeze_page(XLogReaderState *record)
* index updates can be happening concurrently.
*/
/* replay the record for bitmap page */
- if (XLogReadBufferForRedo(record, 5, &mapbuf) == BLK_NEEDS_REDO)
+ if (XLogReadBufferForRedo(record, 4, &mapbuf) == BLK_NEEDS_REDO)
{
Page mappage = BufferGetPage(mapbuf);
uint32 *freep = NULL;
@@ -805,7 +840,7 @@ hash_xlog_squeeze_page(XLogReaderState *record)
freep = HashPageGetBitmap(mappage);
- data = XLogRecGetBlockData(record, 5, &datalen);
+ data = XLogRecGetBlockData(record, 4, &datalen);
bitmap_page_bit = (uint32 *) data;
CLRBIT(freep, *bitmap_page_bit);
@@ -817,11 +852,11 @@ hash_xlog_squeeze_page(XLogReaderState *record)
UnlockReleaseBuffer(mapbuf);
/* replay the record for meta page */
- if (XLogRecHasBlockRef(record, 6))
+ if (XLogRecHasBlockRef(record, 5))
{
Buffer metabuf;
- if (XLogReadBufferForRedo(record, 6, &metabuf) == BLK_NEEDS_REDO)
+ if (XLogReadBufferForRedo(record, 5, &metabuf) == BLK_NEEDS_REDO)
{
HashMetaPage metap;
Page page;
@@ -829,7 +864,7 @@ hash_xlog_squeeze_page(XLogReaderState *record)
uint32 *firstfree_ovflpage;
Size datalen;
- data = XLogRecGetBlockData(record, 6, &datalen);
+ data = XLogRecGetBlockData(record, 5, &datalen);
firstfree_ovflpage = (uint32 *) data;
page = BufferGetPage(metabuf);
@@ -865,24 +900,12 @@ hash_xlog_delete(XLogReaderState *record)
* can miss some records or show the same record multiple times.
*/
if (xldata->is_primary_bucket_page)
- action = XLogReadBufferForRedoExtended(record, 1, RBM_NORMAL, true, &deletebuf);
+ action = XLogReadBufferForRedoExtended(record, 0, RBM_NORMAL, true, &deletebuf);
else
{
- /*
- * The cleanup lock on the primary bucket page serializes replay
- * against scans on a hot standby, which pin the page for their whole
- * duration. Scans can run only once hot standby accepts queries, so
- * until then we need not read or lock the page. Skipping the read
- * also sidesteps a torn copy of the page whose repairing full-page
- * image comes later in the WAL: that hazard exists only before
- * consistency is reached, and queries are accepted only after that,
- * so once HotStandbyActiveInReplay() is true any earlier image has
- * already been replayed. We do not use the page contents here.
- */
- if (HotStandbyActiveInReplay())
- (void) XLogReadBufferForRedoExtended(record, 0, RBM_NORMAL, true, &bucketbuf);
+ bucketbuf = hash_xlog_lock_primary_bucket(record, 0, SizeOfHashDelete);
- action = XLogReadBufferForRedo(record, 1, &deletebuf);
+ action = XLogReadBufferForRedo(record, 0, &deletebuf);
}
/* replay the record for deleting entries in bucket page */
@@ -891,7 +914,7 @@ hash_xlog_delete(XLogReaderState *record)
char *ptr;
Size len;
- ptr = XLogRecGetBlockData(record, 1, &len);
+ ptr = XLogRecGetBlockData(record, 0, &len);
page = BufferGetPage(deletebuf);
diff --git a/src/backend/access/hash/hashovfl.c b/src/backend/access/hash/hashovfl.c
index dbc57ef958c..7ad934ca38f 100644
--- a/src/backend/access/hash/hashovfl.c
+++ b/src/backend/access/hash/hashovfl.c
@@ -580,9 +580,13 @@ _hash_freeovflpage(Relation rel, Buffer bucketbuf, Buffer ovflbuf,
/* Get write-lock on metapage to update firstfree */
LockBuffer(metabuf, BUFFER_LOCK_EXCLUSIVE);
- /* This operation needs to log multiple tuples, prepare WAL for that */
+ /*
+ * This operation needs to log multiple tuples, prepare WAL for that. One
+ * extra data segment accounts for the primary bucket page's block number,
+ * appended to the record when that page is pin-only (not registered).
+ */
if (RelationNeedsWAL(rel))
- XLogEnsureRecordSpace(HASH_XLOG_FREE_OVFL_BUFS, 4 + nitups);
+ XLogEnsureRecordSpace(HASH_XLOG_FREE_OVFL_BUFS, 5 + nitups);
START_CRIT_SECTION();
@@ -656,6 +660,7 @@ _hash_freeovflpage(Relation rel, Buffer bucketbuf, Buffer ovflbuf,
if (RelationNeedsWAL(rel))
{
xl_hash_squeeze_page xlrec;
+ BlockNumber bucket_blkno = BufferGetBlockNumber(bucketbuf);
xlrec.prevblkno = prevblkno;
xlrec.nextblkno = nextblkno;
@@ -667,45 +672,27 @@ _hash_freeovflpage(Relation rel, Buffer bucketbuf, Buffer ovflbuf,
XLogRegisterData(&xlrec, SizeOfHashSqueezePage);
/*
- * bucket buffer was not changed, but still needs to be registered to
- * ensure that we can acquire a cleanup lock on it during replay.
+ * Register only the pages this record changes. The primary bucket
+ * page is registered only if it is the write page and is modified;
+ * otherwise we may hold only a pin on it, so append its block number
+ * and replay takes a cleanup lock on it.
*/
- if (!is_prim_bucket_same_wrt)
- {
- uint8 flags = REGBUF_STANDARD | REGBUF_NO_IMAGE | REGBUF_NO_CHANGE;
-
- XLogRegisterBuffer(0, bucketbuf, flags);
- }
+ if (!(is_prim_bucket_same_wrt && mod_wbuf))
+ XLogRegisterData(&bucket_blkno, sizeof(BlockNumber));
- if (nitups > 0)
- {
- XLogRegisterBuffer(1, wbuf, REGBUF_STANDARD);
- XLogRegisterBufData(1, itup_offsets,
- nitups * sizeof(OffsetNumber));
- for (int i = 0; i < nitups; i++)
- XLogRegisterBufData(1, itups[i], tups_size[i]);
- }
- else if (is_prim_bucket_same_wrt || is_prev_bucket_same_wrt)
+ if (mod_wbuf)
{
- uint8 wbuf_flags;
-
- /*
- * A write buffer needs to be registered even if no tuples are
- * added to it to ensure that we can acquire a cleanup lock on it
- * if it is the same as primary bucket buffer or update the
- * nextblkno if it is same as the previous bucket buffer.
- */
- Assert(nitups == 0);
-
- wbuf_flags = REGBUF_STANDARD;
- if (!is_prev_bucket_same_wrt)
- wbuf_flags |= REGBUF_NO_CHANGE;
- else
- Assert(mod_wbuf);
- XLogRegisterBuffer(1, wbuf, wbuf_flags);
+ XLogRegisterBuffer(0, wbuf, REGBUF_STANDARD);
+ if (nitups > 0)
+ {
+ XLogRegisterBufData(0, itup_offsets,
+ nitups * sizeof(OffsetNumber));
+ for (int i = 0; i < nitups; i++)
+ XLogRegisterBufData(0, itups[i], tups_size[i]);
+ }
}
- XLogRegisterBuffer(2, ovflbuf, REGBUF_STANDARD);
+ XLogRegisterBuffer(1, ovflbuf, REGBUF_STANDARD);
/*
* If prevpage and the writepage (block in which we are moving tuples
@@ -714,18 +701,18 @@ _hash_freeovflpage(Relation rel, Buffer bucketbuf, Buffer ovflbuf,
* writepage.
*/
if (BufferIsValid(prevbuf) && !is_prev_bucket_same_wrt)
- XLogRegisterBuffer(3, prevbuf, REGBUF_STANDARD);
+ XLogRegisterBuffer(2, prevbuf, REGBUF_STANDARD);
if (BufferIsValid(nextbuf))
- XLogRegisterBuffer(4, nextbuf, REGBUF_STANDARD);
+ XLogRegisterBuffer(3, nextbuf, REGBUF_STANDARD);
- XLogRegisterBuffer(5, mapbuf, REGBUF_STANDARD);
- XLogRegisterBufData(5, &bitmapbit, sizeof(uint32));
+ XLogRegisterBuffer(4, mapbuf, REGBUF_STANDARD);
+ XLogRegisterBufData(4, &bitmapbit, sizeof(uint32));
if (update_metap)
{
- XLogRegisterBuffer(6, metabuf, REGBUF_STANDARD);
- XLogRegisterBufData(6, &metap->hashm_firstfree, sizeof(uint32));
+ XLogRegisterBuffer(5, metabuf, REGBUF_STANDARD);
+ XLogRegisterBufData(5, &metap->hashm_firstfree, sizeof(uint32));
}
recptr = XLogInsert(RM_HASH_ID, XLOG_HASH_SQUEEZE_PAGE);
@@ -966,10 +953,12 @@ readpage:
/*
* This operation needs to log multiple tuples, prepare
- * WAL for that.
+ * WAL for that. One extra data segment accounts for the
+ * primary bucket page's block number, appended to the
+ * record when that page is pin-only (not registered).
*/
if (RelationNeedsWAL(rel))
- XLogEnsureRecordSpace(0, 3 + nitups);
+ XLogEnsureRecordSpace(0, 4 + nitups);
START_CRIT_SECTION();
@@ -998,25 +987,22 @@ readpage:
XLogRegisterData(&xlrec, SizeOfHashMovePageContents);
/*
- * bucket buffer was not changed, but still needs to
- * be registered to ensure that we can acquire a
- * cleanup lock on it during replay.
+ * If wbuf is an overflow page, the primary bucket
+ * page is not changed and we hold only a pin on it,
+ * so it is not registered. Append its block number
+ * instead, and replay takes a cleanup lock on it.
*/
if (!xlrec.is_prim_bucket_same_wrt)
- {
- int flags = REGBUF_STANDARD | REGBUF_NO_IMAGE | REGBUF_NO_CHANGE;
-
- XLogRegisterBuffer(0, bucket_buf, flags);
- }
+ XLogRegisterData(&bucket_blkno, sizeof(BlockNumber));
- XLogRegisterBuffer(1, wbuf, REGBUF_STANDARD);
- XLogRegisterBufData(1, itup_offsets,
+ XLogRegisterBuffer(0, wbuf, REGBUF_STANDARD);
+ XLogRegisterBufData(0, itup_offsets,
nitups * sizeof(OffsetNumber));
for (i = 0; i < nitups; i++)
- XLogRegisterBufData(1, itups[i], tups_size[i]);
+ XLogRegisterBufData(0, itups[i], tups_size[i]);
- XLogRegisterBuffer(2, rbuf, REGBUF_STANDARD);
- XLogRegisterBufData(2, deletable,
+ XLogRegisterBuffer(1, rbuf, REGBUF_STANDARD);
+ XLogRegisterBufData(1, deletable,
ndeletable * sizeof(OffsetNumber));
recptr = XLogInsert(RM_HASH_ID, XLOG_HASH_MOVE_PAGE_CONTENTS);
diff --git a/src/include/access/hash_xlog.h b/src/include/access/hash_xlog.h
index 149aed69dc5..0bb0d0590f5 100644
--- a/src/include/access/hash_xlog.h
+++ b/src/include/access/hash_xlog.h
@@ -129,9 +129,13 @@ typedef struct xl_hash_split_complete
*
* This data record is used for XLOG_HASH_MOVE_PAGE_CONTENTS
*
- * Backup Blk 0: primary bucket page
- * Backup Blk 1: page containing moved tuples
- * Backup Blk 2: page from which tuples will be removed
+ * Backup Blk 0: page containing moved tuples
+ * Backup Blk 1: page from which tuples will be removed
+ *
+ * The primary bucket page is not registered. If it is not the page moved to
+ * (is_prim_bucket_same_wrt is false), the block number of the primary bucket
+ * page follows the struct as a BlockNumber, and replay takes a cleanup lock
+ * on that block.
*/
typedef struct xl_hash_move_page_contents
{
@@ -149,13 +153,18 @@ typedef struct xl_hash_move_page_contents
*
* This data record is used for XLOG_HASH_SQUEEZE_PAGE
*
- * Backup Blk 0: primary bucket page
- * Backup Blk 1: page containing tuples moved from freed overflow page
- * Backup Blk 2: freed overflow page
- * Backup Blk 3: page previous to the freed overflow page
- * Backup Blk 4: page next to the freed overflow page
- * Backup Blk 5: bitmap page containing info of freed overflow page
- * Backup Blk 6: meta page
+ * Backup Blk 0: page containing tuples moved from freed overflow page, if
+ * it is modified
+ * Backup Blk 1: freed overflow page
+ * Backup Blk 2: page previous to the freed overflow page
+ * Backup Blk 3: page next to the freed overflow page
+ * Backup Blk 4: bitmap page containing info of freed overflow page
+ * Backup Blk 5: meta page
+ *
+ * The primary bucket page is registered only as block 0, when it is the
+ * modified write page. Otherwise the block number of the primary bucket
+ * page follows the struct as a BlockNumber, and replay takes a cleanup lock
+ * on that block.
*/
typedef struct xl_hash_squeeze_page
{
@@ -179,8 +188,12 @@ typedef struct xl_hash_squeeze_page
*
* This data record is used for XLOG_HASH_DELETE
*
- * Backup Blk 0: primary bucket page
- * Backup Blk 1: page from which tuples are deleted
+ * Backup Blk 0: page from which tuples are deleted
+ *
+ * If block 0 is not the primary bucket page (is_primary_bucket_page is
+ * false), the block number of the primary bucket page follows the struct as a
+ * BlockNumber, and replay takes a cleanup lock on that block. The primary
+ * bucket page is not registered.
*/
typedef struct xl_hash_delete
{
diff --git a/src/include/access/xlog_internal.h b/src/include/access/xlog_internal.h
index bf609c3c703..ed8f488dda5 100644
--- a/src/include/access/xlog_internal.h
+++ b/src/include/access/xlog_internal.h
@@ -32,7 +32,7 @@
/*
* Each page of XLOG file has a header like this:
*/
-#define XLOG_PAGE_MAGIC 0xD122 /* can be used as WAL version indicator */
+#define XLOG_PAGE_MAGIC 0xD123 /* can be used as WAL version indicator */
typedef struct XLogPageHeaderData
{
--
2.47.3
From 31a4b2f750df645949c8e624c29ee2071da538ab Mon Sep 17 00:00:00 2001
From: Greg Burd <[email protected]>
Date: Fri, 30 Oct 2026 10:10:03 -0400
Subject: [PATCH v7 3/4] Add a recovery test for the hash WAL pin-only bucket
page fix
Add recovery/t/061_hash_vacuum_insert_crc.pl, which exercises the
interleaving the previous commits make safe: a hash index VACUUM logging
an overflow-page delete while a concurrent INSERT changes the primary
bucket page it holds pin-only, with wal_consistency_checking='hash'.
The test adds a general, test-only injection point in
XLogRecordAssemble(), right after the record CRC is computed and before
the record is copied into the WAL buffers. Its argument names the
record as "rmid/info/mainlen" so a test can wait on exactly one kind of
record; here the test waits on the overflow-page XLOG_HASH_DELETE (whose
main data carries the appended primary bucket block number) while VACUUM
holds the primary bucket page pin-only, then lets an INSERT change that
page, then crashes and checks that pg_waldump and crash recovery both
accept the WAL with no "incorrect resource manager data checksum".
Before 27caf4ca5ae5 and this rework, the record's CRC was computed over
the concurrently-modified primary bucket page and replay failed; the
injection point makes that window deterministic so the case is covered
from now on.
Co-authored-by: Vignesh C <[email protected]>
Discussion: https://postgr.es/m/CALDaNm14vRb2TZWyhjSHx=SyT7R=e7+ackrmxuojdwwee40...@mail.gmail.com
---
src/backend/access/transam/xloginsert.c | 22 +++
src/test/recovery/meson.build | 1 +
.../recovery/t/061_hash_vacuum_insert_crc.pl | 181 ++++++++++++++++++
3 files changed, 204 insertions(+)
create mode 100644 src/test/recovery/t/061_hash_vacuum_insert_crc.pl
diff --git a/src/backend/access/transam/xloginsert.c b/src/backend/access/transam/xloginsert.c
index 341deb7b80d..cd3359434e1 100644
--- a/src/backend/access/transam/xloginsert.c
+++ b/src/backend/access/transam/xloginsert.c
@@ -39,6 +39,7 @@
#include "replication/origin.h"
#include "storage/bufmgr.h"
#include "storage/proc.h"
+#include "utils/injection_point.h"
#include "utils/memutils.h"
#include "utils/pgstat_internal.h"
#include "utils/rel.h"
@@ -991,6 +992,27 @@ XLogRecordAssemble(RmgrId rmid, uint8 info,
for (rdt = hdr_rdt.next; rdt != NULL; rdt = rdt->next)
COMP_CRC32C(rdata_crc, rdt->data, rdt->len);
+ /*
+ * Test hook: let a test modify a buffer after the record's CRC has been
+ * computed but before the record is copied into the WAL buffers, to
+ * reproduce the window in which a concurrently modified, pin-only
+ * registered buffer could make the on-disk CRC not match the record. The
+ * argument names the record as "rmid/info/mainlen" (resource manager, its
+ * info bits, and the length of the main data) so a test can wait on
+ * exactly one kind of record. This runs inside a critical section, so it
+ * must not allocate; it fires only in a backend that loaded the point
+ * beforehand.
+ */
+#ifdef USE_INJECTION_POINTS
+ {
+ char pointarg[24];
+
+ snprintf(pointarg, sizeof(pointarg), "%d/%02X/" UINT64_FORMAT,
+ rmid, info, mainrdata_len);
+ INJECTION_POINT_CACHED("xlog-assemble-after-crc", pointarg);
+ }
+#endif
+
/*
* Ensure that the XLogRecord is not too large.
*
diff --git a/src/test/recovery/meson.build b/src/test/recovery/meson.build
index e7ff07ca880..43bf2ef3aa4 100644
--- a/src/test/recovery/meson.build
+++ b/src/test/recovery/meson.build
@@ -69,6 +69,7 @@ tests += {
't/058_shutdown_crash_restart.pl',
't/059_remote_apply_status_interval.pl',
't/060_multixact_crashed_updater.pl',
+ 't/061_hash_vacuum_insert_crc.pl',
't/068a_hash_torn_primary_page_delete_first.pl',
't/068b_hash_torn_primary_page_standby.pl',
],
diff --git a/src/test/recovery/t/061_hash_vacuum_insert_crc.pl b/src/test/recovery/t/061_hash_vacuum_insert_crc.pl
new file mode 100644
index 00000000000..aa030abd587
--- /dev/null
+++ b/src/test/recovery/t/061_hash_vacuum_insert_crc.pl
@@ -0,0 +1,181 @@
+# Copyright (c) 2026, PostgreSQL Global Development Group
+
+# Test that the WAL record written by hash index VACUUM for an overflow page is
+# intact when a concurrent INSERT changes the primary bucket page.
+#
+# hashbucketcleanup() keeps only a pin on the primary bucket page while it
+# walks the overflow chain. Before the hash WAL rework it registered that page
+# (REGBUF_STANDARD | REGBUF_NO_IMAGE | REGBUF_NO_CHANGE) with the
+# XLOG_HASH_DELETE record it writes for each overflow page. With
+# wal_consistency_checking enabled for the hash resource manager,
+# XLogRecordAssemble() included a full image of every registered block, so it
+# read the unlocked primary bucket page. If an INSERT added a tuple to that
+# page after the record's CRC was computed but before the page was copied into
+# the WAL buffers, the record was written with a CRC that did not match its
+# contents, and replay stopped at it with "incorrect resource manager data
+# checksum".
+#
+# The window is a few microseconds wide, so the test widens it with an
+# injection point in XLogRecordAssemble(), placed right after the CRC is
+# computed. It is attached to the VACUUM session only and matches just the
+# overflow-page delete, identified by "rmgr id/xl_info/main-data length". The
+# point still fires after the fix, so the test exercises the same interleaving
+# on a correct server.
+#
+# The assertions state the correct behavior (all WAL is valid and replays), so
+# the test fails while the bug exists.
+
+use strict;
+use warnings FATAL => 'all';
+
+use IPC::Run;
+use PostgreSQL::Test::Cluster;
+use PostgreSQL::Test::Utils;
+use Test::More;
+
+if ($ENV{enable_injection_points} ne 'yes')
+{
+ plan skip_all => 'Injection points not supported by this build';
+}
+
+my $node = PostgreSQL::Test::Cluster->new('primary');
+$node->init;
+$node->append_conf(
+ 'postgresql.conf', qq{
+wal_consistency_checking = 'hash'
+autovacuum = off
+fsync = off
+checkpoint_timeout = 1h
+});
+$node->start;
+
+if (!$node->check_extension('injection_points'))
+{
+ plan skip_all => 'Extension injection_points not installed';
+}
+$node->safe_psql('postgres', 'CREATE EXTENSION injection_points');
+
+my $hash_rmid = $node->safe_psql('postgres',
+ q{SELECT rm_id FROM pg_get_wal_resource_managers() WHERE rm_name = 'Hash'}
+);
+
+# Build a single bucket whose primary page stays live and whose overflow page
+# is all dead, so VACUUM logs an XLOG_HASH_DELETE for the overflow page only:
+# the primary page has no dead index entries, so no delete record is written
+# for it, and the only consistency-checked hash DELETE in the window is the
+# overflow one, logged while the primary page is held pin-only. All rows share
+# the key, so they land in one bucket; the first batch fills most of the
+# primary page and stays live, the second spills onto the overflow page and is
+# deleted. Free slots are left on the primary page so the concurrent INSERT
+# below lands there.
+#
+# hashbuild() sizes the index from estimate_rel_size() on the heap, which for a
+# table that has never been vacuumed applies a floor of 10 pages. Vacuuming
+# first sets reltuples to 0, so the estimate is believed and the index starts
+# with the minimum of 2 buckets.
+$node->safe_psql(
+ 'postgres', q{
+CREATE TABLE hash_t (k int, tag int) WITH (autovacuum_enabled = false);
+VACUUM hash_t;
+CREATE INDEX hash_i ON hash_t USING hash (k);
+INSERT INTO hash_t SELECT 1, 0 FROM generate_series(1, 390);
+INSERT INTO hash_t SELECT 1, 1 FROM generate_series(1, 210);
+DELETE FROM hash_t WHERE tag = 1;
+});
+
+# Premise: metapage, two bucket pages, a bitmap page and one overflow page.
+is( $node->safe_psql(
+ 'postgres',
+ q{SELECT pg_relation_size('hash_i') / current_setting('block_size')::int}
+ ),
+ '5',
+ 'premise: the index has a primary bucket page and one overflow page');
+
+# Start from a checkpoint, so crash recovery replays everything below and
+# pg_waldump has a record boundary to start from.
+$node->safe_psql('postgres', 'CHECKPOINT');
+my $start_lsn = $node->safe_psql('postgres', 'SELECT pg_current_wal_lsn()');
+
+# The VACUUM session. The point is attached to this session only and must be
+# loaded before the critical section in which it runs.
+#
+# Wait on the XLOG_HASH_DELETE record for the overflow page, not the one for
+# the primary page. Both are hash records (rm id $hash_rmid) with info 0x92
+# (XLOG_HASH_DELETE 0x90 | XLR_CHECK_CONSISTENCY 0x02). They differ in main
+# data length: the primary-page delete carries just xl_hash_delete
+# (SizeOfHashDelete, two bools = 2 bytes), while the overflow-page delete also
+# appends the primary bucket page's block number (a 4-byte BlockNumber), for 6
+# bytes. Waiting on the 6-byte one parks VACUUM while it holds the primary
+# bucket page pin-only, which is the page the INSERT below changes.
+my $overflow_delete = "$hash_rmid/92/6";
+my $vacuum = $node->background_psql('postgres');
+my $vacuum_pid = $vacuum->query_safe('SELECT pg_backend_pid()');
+$vacuum->query_safe(q{SELECT injection_points_set_local()});
+$vacuum->query_safe(
+ qq{SELECT injection_points_attach('xlog-assemble-after-crc', 'wait', '$overflow_delete')}
+);
+$vacuum->query_safe(
+ q{SELECT injection_points_load('xlog-assemble-after-crc')});
+$vacuum->query_until(
+ qr/starting_vacuum/,
+ q(\echo starting_vacuum
+VACUUM (INDEX_CLEANUP on) hash_t;
+));
+
+# VACUUM has removed the dead entries from the primary page, released its lock
+# (keeping the pin), and is now logging the removal from the overflow page: the
+# CRC of that record is computed and the unlocked primary page is not yet
+# copied.
+$node->poll_query_until('postgres',
+ "SELECT wait_event = 'xlog-assemble-after-crc' FROM pg_stat_activity WHERE pid = $vacuum_pid"
+) or die "VACUUM did not reach the injection point";
+note('VACUUM is between computing the CRC and copying the record');
+
+# Now there is room in the primary page, so this INSERT changes it while VACUUM
+# holds it pin-only during the overflow delete.
+$node->safe_psql('postgres', q{INSERT INTO hash_t VALUES (1, 0)});
+
+$node->safe_psql('postgres',
+ q{SELECT injection_points_wakeup('xlog-assemble-after-crc')});
+$vacuum->quit;
+
+my $end_lsn =
+ $node->safe_psql('postgres', 'SELECT pg_current_wal_insert_lsn()');
+$node->poll_query_until('postgres',
+ "SELECT pg_current_wal_flush_lsn() >= '$end_lsn'")
+ or die "timed out waiting for WAL flush";
+
+# Crash, so that start-up has to replay (and CRC-check) the workload.
+$node->stop('immediate');
+
+# 1. Every record in the range must have a valid CRC. pg_waldump verifies it
+# for every record it reads, whatever the --rmgr filter.
+my $stderr;
+my $ok = IPC::Run::run(
+ [
+ 'pg_waldump',
+ '--path' => $node->data_dir . '/pg_wal',
+ '--start' => $start_lsn,
+ '--end' => $end_lsn,
+ '--rmgr' => 'Hash',
+ ],
+ '>' => '/dev/null',
+ '2>' => \$stderr);
+
+ok($ok, 'pg_waldump reads the whole workload without errors');
+unlike(
+ $stderr,
+ qr/incorrect resource manager data checksum/,
+ 'no WAL record has a mismatching CRC');
+
+# 2. Crash recovery must replay it without complaint.
+my $log_offset = -s $node->logfile;
+$node->start;
+my $log = slurp_file($node->logfile, $log_offset);
+unlike(
+ $log,
+ qr/incorrect resource manager data checksum|inconsistent page found/,
+ 'crash recovery replays the workload without CRC or consistency errors');
+
+$node->stop;
+done_testing();
--
2.47.3
From 8423b47a045ee3626be5f4ffd518d55fd5a9afe9 Mon Sep 17 00:00:00 2001
From: Greg Burd <[email protected]>
Date: Thu, 12 Nov 2026 12:58:59 +0000
Subject: [PATCH v7 1/4] Fix hash index redo reading a torn primary bucket page
Replay of XLOG_HASH_DELETE, XLOG_HASH_MOVE_PAGE_CONTENTS and
XLOG_HASH_SQUEEZE_PAGE reads the primary bucket page only to take a
cleanup lock on it, so that no scan on a hot standby runs in the bucket
while tuples are removed or moved. VACUUM holds only a pin on that page,
so the record does not modify it and carries no full-page image of it;
redo reads it from disk with RBM_NORMAL.
If the page is torn on disk and the record that repairs it with a
full-page image comes later in the WAL, recovery fails when it reaches
one of these records before the image, because the RBM_NORMAL read of
the torn page raises "invalid page". This happens both in crash
recovery (the image is in WAL generated after the last checkpoint) and
on a standby started from a base backup whose copy of the page is torn
(the image is in WAL generated during the backup).
Robert Haas noted this torn-page hazard during the original hash WAL
work in 2017; redo was switched to XLogReadBufferForRedoExtended() so it
restores an image when the record has one, but these records register
the primary bucket page pin-only with REGBUF_NO_IMAGE, so there is no
image to restore and the RBM_NORMAL read still reaches the torn page.
The cleanup lock matters only to serialize against hot-standby scans,
which can run only once the standby accepts queries, that is once
recovery has reached consistency. Until then the read and lock are
unnecessary, so skip them when HotStandbyActiveInReplay() is false.
That also avoids reading the page in the window where its torn on-disk
copy has not yet been repaired, since the repairing image always
precedes the consistency point. nbtree and gist already gate redo
cleanup-lock work on the hot-standby state for the same reason.
Reported-by: shihao zhong <[email protected]>
Author: Greg Burd <[email protected]>
Co-authored-by: Vignesh C <[email protected]>
Suggested-by: Amit Kapila <[email protected]>
Discussion: https://postgr.es/m/CAGRkXqT_AiwcsWZg5hnR1L2oWO8UpQpkL0iAjoZR+bey=go...@mail.gmail.com
Discussion: https://postgr.es/m/caa4ek1lwzk0mak0lnczz6hsqchnamfjb47na8xuwrp4cylf...@mail.gmail.com
---
src/backend/access/hash/hash_xlog.c | 43 ++++-
src/backend/access/transam/xlogrecovery.c | 3 +-
src/include/access/xlogrecovery.h | 1 +
src/test/recovery/meson.build | 2 +
...68a_hash_torn_primary_page_delete_first.pl | 142 +++++++++++++++
.../t/068b_hash_torn_primary_page_standby.pl | 171 ++++++++++++++++++
6 files changed, 351 insertions(+), 11 deletions(-)
create mode 100644 src/test/recovery/t/068a_hash_torn_primary_page_delete_first.pl
create mode 100644 src/test/recovery/t/068b_hash_torn_primary_page_standby.pl
diff --git a/src/backend/access/hash/hash_xlog.c b/src/backend/access/hash/hash_xlog.c
index e9a2b9aa9a7..e1318bf97db 100644
--- a/src/backend/access/hash/hash_xlog.c
+++ b/src/backend/access/hash/hash_xlog.c
@@ -17,6 +17,7 @@
#include "access/bufmask.h"
#include "access/hash.h"
#include "access/hash_xlog.h"
+#include "access/xlogrecovery.h"
#include "access/xlogutils.h"
#include "storage/standby.h"
@@ -494,10 +495,18 @@ hash_xlog_move_page_contents(XLogReaderState *record)
else
{
/*
- * we don't care for return value as the purpose of reading bucketbuf
- * is to ensure a cleanup lock on primary bucket page.
+ * The cleanup lock on the primary bucket page serializes replay
+ * against scans on a hot standby, which pin the page for their whole
+ * duration. Scans can run only once hot standby accepts queries, so
+ * until then we need not read or lock the page. Skipping the read
+ * also sidesteps a torn copy of the page whose repairing full-page
+ * image comes later in the WAL: that hazard exists only before
+ * consistency is reached, and queries are accepted only after that,
+ * so once HotStandbyActiveInReplay() is true any earlier image has
+ * already been replayed. We do not use the page contents here.
*/
- (void) XLogReadBufferForRedoExtended(record, 0, RBM_NORMAL, true, &bucketbuf);
+ if (HotStandbyActiveInReplay())
+ (void) XLogReadBufferForRedoExtended(record, 0, RBM_NORMAL, true, &bucketbuf);
action = XLogReadBufferForRedo(record, 1, &writebuf);
}
@@ -621,10 +630,18 @@ hash_xlog_squeeze_page(XLogReaderState *record)
else
{
/*
- * we don't care for return value as the purpose of reading bucketbuf
- * is to ensure a cleanup lock on primary bucket page.
+ * The cleanup lock on the primary bucket page serializes replay
+ * against scans on a hot standby, which pin the page for their whole
+ * duration. Scans can run only once hot standby accepts queries, so
+ * until then we need not read or lock the page. Skipping the read
+ * also sidesteps a torn copy of the page whose repairing full-page
+ * image comes later in the WAL: that hazard exists only before
+ * consistency is reached, and queries are accepted only after that,
+ * so once HotStandbyActiveInReplay() is true any earlier image has
+ * already been replayed. We do not use the page contents here.
*/
- (void) XLogReadBufferForRedoExtended(record, 0, RBM_NORMAL, true, &bucketbuf);
+ if (HotStandbyActiveInReplay())
+ (void) XLogReadBufferForRedoExtended(record, 0, RBM_NORMAL, true, &bucketbuf);
if (xldata->ntups > 0 || xldata->is_prev_bucket_same_wrt)
action = XLogReadBufferForRedo(record, 1, &writebuf);
@@ -852,10 +869,18 @@ hash_xlog_delete(XLogReaderState *record)
else
{
/*
- * we don't care for return value as the purpose of reading bucketbuf
- * is to ensure a cleanup lock on primary bucket page.
+ * The cleanup lock on the primary bucket page serializes replay
+ * against scans on a hot standby, which pin the page for their whole
+ * duration. Scans can run only once hot standby accepts queries, so
+ * until then we need not read or lock the page. Skipping the read
+ * also sidesteps a torn copy of the page whose repairing full-page
+ * image comes later in the WAL: that hazard exists only before
+ * consistency is reached, and queries are accepted only after that,
+ * so once HotStandbyActiveInReplay() is true any earlier image has
+ * already been replayed. We do not use the page contents here.
*/
- (void) XLogReadBufferForRedoExtended(record, 0, RBM_NORMAL, true, &bucketbuf);
+ if (HotStandbyActiveInReplay())
+ (void) XLogReadBufferForRedoExtended(record, 0, RBM_NORMAL, true, &bucketbuf);
action = XLogReadBufferForRedo(record, 1, &deletebuf);
}
diff --git a/src/backend/access/transam/xlogrecovery.c b/src/backend/access/transam/xlogrecovery.c
index 52ac6d58423..e091a3e178b 100644
--- a/src/backend/access/transam/xlogrecovery.c
+++ b/src/backend/access/transam/xlogrecovery.c
@@ -390,7 +390,6 @@ static int XLogFileReadAnyTLI(XLogSegNo segno, XLogSource source);
static bool CheckForStandbyTrigger(void);
static void SetPromoteIsTriggered(void);
-static bool HotStandbyActiveInReplay(void);
static void SetCurrentChunkStartTime(TimestampTz xtime);
static void SetLatestXTime(TimestampTz xtime);
@@ -4589,7 +4588,7 @@ HotStandbyActive(void)
* Like HotStandbyActive(), but to be used only in WAL replay code,
* where we don't need to ask any other process what the state is.
*/
-static bool
+bool
HotStandbyActiveInReplay(void)
{
Assert(AmStartupProcess() || !IsPostmasterEnvironment);
diff --git a/src/include/access/xlogrecovery.h b/src/include/access/xlogrecovery.h
index 8786b6d3a8e..11d3eadef3e 100644
--- a/src/include/access/xlogrecovery.h
+++ b/src/include/access/xlogrecovery.h
@@ -216,6 +216,7 @@ extern void ShutdownWalRecovery(void);
extern void RemovePromoteSignalFiles(void);
extern bool HotStandbyActive(void);
+extern bool HotStandbyActiveInReplay(void);
extern XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI);
extern RecoveryPauseState GetRecoveryPauseState(void);
extern void SetRecoveryPause(bool recoveryPause);
diff --git a/src/test/recovery/meson.build b/src/test/recovery/meson.build
index b6ac9dd07a7..e7ff07ca880 100644
--- a/src/test/recovery/meson.build
+++ b/src/test/recovery/meson.build
@@ -69,6 +69,8 @@ tests += {
't/058_shutdown_crash_restart.pl',
't/059_remote_apply_status_interval.pl',
't/060_multixact_crashed_updater.pl',
+ 't/068a_hash_torn_primary_page_delete_first.pl',
+ 't/068b_hash_torn_primary_page_standby.pl',
],
},
}
diff --git a/src/test/recovery/t/068a_hash_torn_primary_page_delete_first.pl b/src/test/recovery/t/068a_hash_torn_primary_page_delete_first.pl
new file mode 100644
index 00000000000..e61a0b98885
--- /dev/null
+++ b/src/test/recovery/t/068a_hash_torn_primary_page_delete_first.pl
@@ -0,0 +1,142 @@
+# Copyright (c) 2026, PostgreSQL Global Development Group
+
+# Test crash recovery when the primary bucket page of a hash bucket is torn,
+# and the full-page image that repairs it comes after the record that reads it.
+#
+
+use strict;
+use warnings FATAL => 'all';
+
+use IPC::Run;
+use PostgreSQL::Test::Cluster;
+use PostgreSQL::Test::Utils;
+use Test::More;
+
+my $node = PostgreSQL::Test::Cluster->new('primary');
+$node->init;
+$node->append_conf(
+ 'postgresql.conf', qq{
+autovacuum = off
+fsync = off
+full_page_writes = on
+checkpoint_timeout = 1h
+});
+$node->start;
+
+is($node->safe_psql('postgres', 'SHOW data_checksums'),
+ 'on', 'premise: data checksums are on');
+
+# One bucket receives every key: a primary page holding about 407 entries and
+# an overflow page with the rest.
+$node->safe_psql(
+ 'postgres', q{
+CREATE TABLE t (id int, k int) WITH (autovacuum_enabled = false);
+CREATE INDEX t_i ON t USING hash (k);
+INSERT INTO t SELECT i, 1 FROM generate_series(1, 600) i;
+});
+$node->safe_psql('postgres', 'CHECKPOINT');
+my $redo = $node->safe_psql('postgres',
+ 'SELECT redo_lsn FROM pg_control_checkpoint()');
+
+# The entries of ids 451-600 are on the overflow page, those of ids 1-50 on the
+# primary page. The DELETE for the overflow page comes first; the VACUUM of
+# the primary page then changes it for the first time since the checkpoint, so
+# its record carries the full-page image of the page.
+$node->safe_psql('postgres', 'DELETE FROM t WHERE id > 450');
+$node->safe_psql('postgres', 'VACUUM t');
+$node->safe_psql('postgres', 'DELETE FROM t WHERE id <= 50');
+$node->safe_psql('postgres', 'VACUUM t');
+
+my $relpath = $node->safe_psql('postgres',
+ q{SELECT pg_relation_filepath('t_i')});
+my $bs = $node->safe_psql('postgres', 'SHOW block_size');
+$node->safe_psql('postgres', 'SELECT pg_switch_wal()');
+
+# Read the Hash records after the checkpoint.
+my ($stdout, $stderr);
+IPC::Run::run(
+ [
+ 'pg_waldump',
+ '--path' => $node->data_dir . '/pg_wal',
+ '--start' => $redo,
+ '--rmgr' => 'Hash',
+ ],
+ '>' => \$stdout,
+ '2>' => \$stderr);
+my @records = grep { /^rmgr: Hash/ } split(/^(?=rmgr: )/m, $stdout);
+
+# The primary bucket page's block number is not a registered block of the
+# overflow-page DELETE (the record carries it only as appended main data), so
+# find it from a record that does register it: a DELETE or SQUEEZE_PAGE marked
+# is_primary T references the primary bucket page as its first block.
+my $pblk;
+for my $r (@records)
+{
+ if ($r =~ /desc: (?:DELETE|SQUEEZE_PAGE) .*is_primary T/
+ && $r =~ /blkref #0: rel \S+ blk (\d+)/)
+ {
+ $pblk = $1;
+ last;
+ }
+}
+ok(defined $pblk, 'premise: found the primary bucket page')
+ or diag $stdout;
+
+# The overflow-page DELETE (is_primary F) reads the primary bucket page
+# pin-only to take the cleanup lock; it must come before the primary page's
+# first full-page image.
+my ($idx_delete) =
+ grep { $records[$_] =~ /desc: DELETE .*is_primary F/ } 0 .. $#records;
+ok(defined $idx_delete,
+ 'premise: VACUUM wrote a DELETE for an overflow page')
+ or diag $stdout;
+
+# Where the first full-page image of the primary page is.
+my ($idx_fpi) =
+ defined $pblk
+ ? grep { $records[$_] =~ /blkref #\d+: rel \S+ blk $pblk FPW/ }
+ 0 .. $#records
+ : ();
+ok(defined $idx_fpi,
+ 'premise: the primary page got a full-page image after the checkpoint')
+ or diag $stdout;
+if (defined $idx_delete && defined $idx_fpi)
+{
+ cmp_ok($idx_delete, '<', $idx_fpi,
+ 'premise: the DELETE comes before the image of the primary page');
+}
+
+$node->stop('immediate');
+
+# Simulate a torn write of the primary page: keep the first half of its block
+# and overwrite the second half.
+if (defined $pblk)
+{
+ my $path = $node->data_dir . '/' . $relpath;
+ open(my $fh, '+<:raw', $path) or die "could not open $path: $!";
+ sysseek($fh, $pblk * $bs + $bs / 2, 0) or die "seek: $!";
+ my $junk = join('', map { chr(int(rand(256))) } 1 .. $bs / 2);
+ syswrite($fh, $junk) == $bs / 2 or die "write: $!";
+ close($fh);
+}
+
+my $started = $node->start(fail_ok => 1);
+ok($started, 'crash recovery succeeds');
+if ($started)
+{
+ is( $node->safe_psql(
+ 'postgres',
+ q{SET enable_seqscan = off; SET enable_bitmapscan = off;
+ SELECT count(*) FROM t WHERE k = 1}),
+ '400',
+ 'the hash index gives the right result after recovery');
+ $node->stop;
+}
+else
+{
+ my $log = slurp_file($node->logfile);
+ diag("the server did not start; the log has:\n"
+ . join('', grep { /invalid page|FATAL|WAL redo/ } split(/^/m, $log)));
+}
+
+done_testing();
diff --git a/src/test/recovery/t/068b_hash_torn_primary_page_standby.pl b/src/test/recovery/t/068b_hash_torn_primary_page_standby.pl
new file mode 100644
index 00000000000..d9a548a931e
--- /dev/null
+++ b/src/test/recovery/t/068b_hash_torn_primary_page_standby.pl
@@ -0,0 +1,171 @@
+# Copyright (c) 2026, PostgreSQL Global Development Group
+
+# Test a standby that starts from a base backup whose copy of the primary
+# bucket page of a hash bucket is torn, and the full-page image that repairs it
+# comes after the record that reads it.
+#
+
+use strict;
+use warnings FATAL => 'all';
+
+use File::Path qw(rmtree);
+use IPC::Run;
+use PostgreSQL::Test::Cluster;
+use PostgreSQL::Test::Utils;
+use Test::More;
+
+my $primary = PostgreSQL::Test::Cluster->new('primary');
+$primary->init(allows_streaming => 1);
+$primary->append_conf(
+ 'postgresql.conf', qq{
+autovacuum = off
+fsync = off
+full_page_writes = on
+checkpoint_timeout = 1h
+});
+$primary->start;
+
+is($primary->safe_psql('postgres', 'SHOW data_checksums'),
+ 'on', 'premise: data checksums are on');
+
+# One bucket receives every key: a primary page holding about 407 entries and
+# an overflow page with the rest.
+$primary->safe_psql(
+ 'postgres', q{
+CREATE TABLE t (id int, k int) WITH (autovacuum_enabled = false);
+CREATE INDEX t_i ON t USING hash (k);
+INSERT INTO t SELECT i, 1 FROM generate_series(1, 600) i;
+});
+my $relpath = $primary->safe_psql('postgres',
+ q{SELECT pg_relation_filepath('t_i')});
+my $bs = $primary->safe_psql('postgres', 'SHOW block_size');
+
+# Start a low-level backup, which does a checkpoint, and copy the files.
+my $backup_name = 'torn';
+my $backup_dir = $primary->backup_dir . '/' . $backup_name;
+my $psql = $primary->background_psql('postgres');
+$psql->query_safe("SET client_min_messages TO WARNING");
+$psql->set_query_timer_restart;
+$psql->query_safe("select pg_backup_start('torn primary page', true)");
+my $redo = $primary->safe_psql('postgres',
+ 'SELECT redo_lsn FROM pg_control_checkpoint()');
+
+PostgreSQL::Test::RecursiveCopy::copypath($primary->data_dir, $backup_dir);
+unlink("$backup_dir/postmaster.pid")
+ or BAIL_OUT("unable to unlink $backup_dir/postmaster.pid");
+unlink("$backup_dir/postmaster.opts")
+ or BAIL_OUT("unable to unlink $backup_dir/postmaster.opts");
+rmtree("$backup_dir/pg_wal")
+ or BAIL_OUT("unable to unlink contents of $backup_dir/pg_wal");
+mkdir("$backup_dir/pg_wal");
+
+# While the backup is in progress: the entries of ids 451-600 are on the
+# overflow page, those of ids 1-50 on the primary page. The DELETE for the
+# overflow page comes first; the VACUUM of the primary page then changes it for
+# the first time since the checkpoint of the backup, so its record carries the
+# full-page image of the page.
+$primary->safe_psql('postgres', 'DELETE FROM t WHERE id > 450');
+$primary->safe_psql('postgres', 'VACUUM t');
+$primary->safe_psql('postgres', 'DELETE FROM t WHERE id <= 50');
+$primary->safe_psql('postgres', 'VACUUM t');
+$primary->safe_psql('postgres', 'SELECT pg_switch_wal()');
+
+# Read the Hash records after the redo pointer of the backup.
+my ($stdout, $stderr);
+IPC::Run::run(
+ [
+ 'pg_waldump',
+ '--path' => $primary->data_dir . '/pg_wal',
+ '--start' => $redo,
+ '--rmgr' => 'Hash',
+ ],
+ '>' => \$stdout,
+ '2>' => \$stderr);
+my @records = grep { /^rmgr: Hash/ } split(/^(?=rmgr: )/m, $stdout);
+
+# The primary page: a DELETE or SQUEEZE_PAGE marked is_primary T has it as
+# block 1, the page that is changed.
+my $pblk;
+for my $r (@records)
+{
+ if ($r =~ /desc: (?:DELETE|SQUEEZE_PAGE) .*is_primary T/
+ && $r =~ /blkref #[01]: rel \S+ blk (\d+)/)
+ {
+ $pblk = $1;
+ last;
+ }
+}
+ok(defined $pblk, 'premise: found the primary bucket page')
+ or diag $stdout;
+
+# The first DELETE for an overflow page.
+my ($idx_delete) =
+ grep { $records[$_] =~ /desc: DELETE .*is_primary F/ } 0 .. $#records;
+ok(defined $idx_delete,
+ 'premise: VACUUM wrote a DELETE for an overflow page')
+ or diag $stdout;
+
+# Where the first full-page image of the primary page is, relative to the
+# DELETE for the overflow page.
+my ($idx_fpi) =
+ defined $pblk
+ ? grep { $records[$_] =~ /blkref #\d+: rel \S+ blk $pblk FPW/ }
+ 0 .. $#records
+ : ();
+ok(defined $idx_fpi,
+ 'premise: the primary page got a full-page image after the checkpoint')
+ or diag $stdout;
+if (defined $idx_delete && defined $idx_fpi)
+{
+ cmp_ok($idx_delete, '<', $idx_fpi,
+ 'premise: the DELETE comes before the image of the primary page');
+}
+
+# The copy of the primary page in the backup is torn: keep the first half of
+# its block and overwrite the second half.
+if (defined $pblk)
+{
+ my $path = "$backup_dir/$relpath";
+ open(my $fh, '+<:raw', $path) or die "could not open $path: $!";
+ sysseek($fh, $pblk * $bs + $bs / 2, 0) or die "seek: $!";
+ my $junk = join('', map { chr(int(rand(256))) } 1 .. $bs / 2);
+ syswrite($fh, $junk) == $bs / 2 or die "write: $!";
+ close($fh);
+}
+
+# End the backup, and save backup_label into the backup.
+my $backup_label = $psql->query_safe("select labelfile from pg_backup_stop()");
+$psql->quit;
+open(my $fh, '>', "$backup_dir/backup_label")
+ or BAIL_OUT("unable to create $backup_dir/backup_label: $!");
+print $fh $backup_label;
+close($fh);
+
+# Start a hot standby from the backup. Replay starts at the redo pointer of
+# the backup, replays the DELETE for the overflow page first, and reaches the
+# consistency point only after the end of the backup, after the full-page
+# image of the primary page.
+my $standby = PostgreSQL::Test::Cluster->new('standby');
+$standby->init_from_backup($primary, $backup_name, has_streaming => 1);
+$standby->append_conf('postgresql.conf', 'hot_standby = on');
+
+my $started = $standby->start(fail_ok => 1);
+ok($started, 'the standby starts');
+if ($started)
+{
+ $primary->wait_for_catchup($standby);
+ is( $standby->safe_psql(
+ 'postgres',
+ q{SET enable_seqscan = off; SET enable_bitmapscan = off;
+ SELECT count(*) FROM t WHERE k = 1}),
+ '400',
+ 'the hash index gives the right result on the standby');
+}
+else
+{
+ my $log = slurp_file($standby->logfile);
+ diag("the standby did not start; the log has:\n"
+ . join('', grep { /invalid page|FATAL|WAL redo|consistent recovery/ } split(/^/m, $log)));
+}
+
+done_testing();
--
2.47.3
From c5f1ab6eab88124df18b46b2d91fab94f78d84f7 Mon Sep 17 00:00:00 2001
From: Greg Burd <[email protected]>
Date: Fri, 30 Oct 2026 10:17:46 -0400
Subject: [PATCH v7 4/4] Remove REGBUF_NO_CHANGE
With the previous commits, the hash index AM no longer registers any
pin-only or otherwise-unchanged buffer, so REGBUF_NO_CHANGE has no users
left. Remove the flag and restore the invariant that every registered
buffer is content-locked and dirty: XLogRegisterBuffer() asserts it
unconditionally again.
This also reverts the defensive measures added in 27caf4ca5ae5, which
let a REGBUF_NO_CHANGE buffer skip the full-page image forced by
wal_consistency_checking: XLogRecordAssemble() no longer special-cases
the flag, and verifyBackupPageConsistency() again asserts that a
registered block carries an image. 27caf4ca5ae5 remains in the back
branches, where this hash rework is not applied.
Discussion: https://postgr.es/m/[email protected]
---
src/backend/access/transam/README | 5 ----
src/backend/access/transam/xloginsert.c | 28 ++++++-----------------
src/backend/access/transam/xlogrecovery.c | 7 +-----
src/include/access/xloginsert.h | 3 ---
4 files changed, 8 insertions(+), 35 deletions(-)
diff --git a/src/backend/access/transam/README b/src/backend/access/transam/README
index 595afa01fa7..231106270fd 100644
--- a/src/backend/access/transam/README
+++ b/src/backend/access/transam/README
@@ -586,11 +586,6 @@ void XLogRegisterBuffer(uint8 block_id, Buffer buf, uint8 flags);
XLogRegisterBufData() is included in the WAL record even if a full-page
image is taken.
- The REGBUF_NO_CHANGE flag indicates that the buffer is registered without
- being modified by this record, and possibly without an exclusive content
- lock. wal_consistency_checking does not force a full-page image for such
- a buffer.
-
void XLogRegisterData(const void *data, int len);
XLogRegisterData is used to include arbitrary data in the WAL record. If
diff --git a/src/backend/access/transam/xloginsert.c b/src/backend/access/transam/xloginsert.c
index cd3359434e1..2d031e38594 100644
--- a/src/backend/access/transam/xloginsert.c
+++ b/src/backend/access/transam/xloginsert.c
@@ -257,22 +257,13 @@ XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Assert(begininsert_called);
/*
- * Ordinarily, the buffer should be exclusive-locked (or share-exclusive
- * in case of hint bits) and marked dirty before we get here, otherwise we
- * could end up violating one of the rules in access/transam/README.
- *
- * Some callers intentionally register a clean page and never update that
- * page's LSN; in that case they can pass the flag REGBUF_NO_CHANGE to
- * bypass these checks.
+ * The buffer should be exclusive-locked (or share-exclusive in case of
+ * hint bits) and marked dirty before we get here, otherwise we could end
+ * up violating one of the rules in access/transam/README.
*/
-#ifdef USE_ASSERT_CHECKING
- if (!(flags & REGBUF_NO_CHANGE))
- {
- Assert(BufferIsDirty(buffer));
- Assert(BufferIsLockedByMeInMode(buffer, BUFFER_LOCK_EXCLUSIVE) ||
- BufferIsLockedByMeInMode(buffer, BUFFER_LOCK_SHARE_EXCLUSIVE));
- }
-#endif
+ Assert(BufferIsDirty(buffer));
+ Assert(BufferIsLockedByMeInMode(buffer, BUFFER_LOCK_EXCLUSIVE) ||
+ BufferIsLockedByMeInMode(buffer, BUFFER_LOCK_SHARE_EXCLUSIVE));
if (block_id >= max_registered_block_id)
{
@@ -722,13 +713,8 @@ XLogRecordAssemble(RmgrId rmid, uint8 info,
/*
* If needs_backup is true or WAL checking is enabled for current
* resource manager, log a full-page write for the current block.
- *
- * Do not force an image for a REGBUF_NO_CHANGE buffer.
*/
- if (regbuf->flags & REGBUF_NO_CHANGE)
- include_image = needs_backup;
- else
- include_image = needs_backup || (info & XLR_CHECK_CONSISTENCY) != 0;
+ include_image = needs_backup || (info & XLR_CHECK_CONSISTENCY) != 0;
if (include_image)
{
diff --git a/src/backend/access/transam/xlogrecovery.c b/src/backend/access/transam/xlogrecovery.c
index e091a3e178b..1b2c9cc78e9 100644
--- a/src/backend/access/transam/xlogrecovery.c
+++ b/src/backend/access/transam/xlogrecovery.c
@@ -2487,12 +2487,7 @@ verifyBackupPageConsistency(XLogReaderState *record)
continue;
}
- /*
- * Blocks registered with REGBUF_NO_CHANGE may have no image, see
- * XLogRecordAssemble(). Nothing to compare for these.
- */
- if (!XLogRecHasBlockImage(record, block_id))
- continue;
+ Assert(XLogRecHasBlockImage(record, block_id));
if (XLogRecBlockImageApply(record, block_id))
{
diff --git a/src/include/access/xloginsert.h b/src/include/access/xloginsert.h
index 86264564ebb..ddb1a85db59 100644
--- a/src/include/access/xloginsert.h
+++ b/src/include/access/xloginsert.h
@@ -38,9 +38,6 @@
* will be skipped) */
#define REGBUF_KEEP_DATA 0x10 /* include data even if a full-page image
* is taken */
-#define REGBUF_NO_CHANGE 0x20 /* intentionally register clean buffer; no
- * image is forced for WAL consistency
- * checking */
/* prototypes for public functions in xloginsert.c: */
extern void XLogBeginInsert(void);
--
2.47.3