postgres.git / summary / log / commit / refs

commit    cfdb86434de5eb3f2bda0f8c35bbe9a34e423e0e
Author:   Amit Kapila <akapila@postgresql.org>
Date:     Wed Nov 18 03:24:44 2026 +0000

    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 hot standby scan runs in the bucket while
    tuples are removed or moved. VACUUM holds only a pin on that page, so
    these records do not modify it and carry no full-page image of it.
    
    If the on-disk page is torn and the full-page image that repairs it comes
    later in the WAL, this read fails with "invalid page". That can happen in
    crash recovery, and on a standby started from a base backup whose copy of
    the page is torn.
    
    The lock is needed only to serialize against hot standby scans, which can
    run only once recovery has reached consistency. Skip the read and lock
    while HotStandbyActiveInReplay() is false. This also avoids reading the
    page before its repairing image has been replayed, since that image always
    precedes the consistency point.
    
    Reported-by: shihao zhong <zhong950419@gmail.com>
    Author: Greg Burd <greg@burd.me>
    Co-authored-by: Vignesh C <vignesh21@gmail.com>
    Suggested-by: Amit Kapila <amit.kapila16@gmail.com>
    Reviewed-by: Joao Rito <joaovictor.rito@gmail.com>
    Reviewed-by: Amit Kapila <amit.kapila16@gmail.com>
    Discussion: https://postgr.es/m/CAGRkXqT_AiwcsWZg5hnR1L2oWO8UpQpkL0iAjoZR+bey=GOWaQ@mail.gmail.com
    Discussion: https://postgr.es/m/CAA4eK1LwZk0MaK0LncZz6hsqcHnamfJB47nA8xuwRp4CyLFKzw@mail.gmail.com
    Backpatch-through: 15


src/backend/access/hash/hash_xlog.c | 52 +++++++++++++++++++++++++------ src/backend/access/transam/xlogrecovery.c | 3 +- src/include/access/xlogrecovery.h | 1 + 3 files changed, 45 insertions(+), 11 deletions(-) diff --git a/src/backend/access/hash/hash_xlog.c b/src/backend/access/hash/hash_xlog.c index de88697f191..cddc0e4a552 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" @@ -496,10 +497,21 @@ 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 is needed only when hot standby is active, because + * scans cannot run before that. Skipping the read also avoids a torn + * copy of the primary page. This record does not modify that page, so + * it has no full-page image of it. In crash recovery, or on a standby + * started from a base backup, the page on disk can be torn and the + * image that repairs it can come later in the WAL. */ - (void) XLogReadBufferForRedoExtended(record, 0, RBM_NORMAL, true, &bucketbuf); + if (HotStandbyActiveInReplay()) + { + /* + * we don't care for return value as the purpose of reading + * bucketbuf is to ensure a cleanup lock on primary bucket page. + */ + (void) XLogReadBufferForRedoExtended(record, 0, RBM_NORMAL, true, &bucketbuf); + } action = XLogReadBufferForRedo(record, 1, &writebuf); } @@ -624,10 +636,21 @@ 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 is needed only when hot standby is active, because + * scans cannot run before that. Skipping the read also avoids a torn + * copy of the primary page. This record does not modify that page, so + * it has no full-page image of it. In crash recovery, or on a standby + * started from a base backup, the page on disk can be torn and the + * image that repairs it can come later in the WAL. */ - (void) XLogReadBufferForRedoExtended(record, 0, RBM_NORMAL, true, &bucketbuf); + if (HotStandbyActiveInReplay()) + { + /* + * we don't care for return value as the purpose of reading + * bucketbuf is to ensure a cleanup lock on primary bucket page. + */ + (void) XLogReadBufferForRedoExtended(record, 0, RBM_NORMAL, true, &bucketbuf); + } if (xldata->ntups > 0 || xldata->is_prev_bucket_same_wrt) action = XLogReadBufferForRedo(record, 1, &writebuf); @@ -856,10 +879,21 @@ 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 is needed only when hot standby is active, because + * scans cannot run before that. Skipping the read also avoids a torn + * copy of the primary page. This record does not modify that page, so + * it has no full-page image of it. In crash recovery, or on a standby + * started from a base backup, the page on disk can be torn and the + * image that repairs it can come later in the WAL. */ - (void) XLogReadBufferForRedoExtended(record, 0, RBM_NORMAL, true, &bucketbuf); + if (HotStandbyActiveInReplay()) + { + /* + * we don't care for return value as the purpose of reading + * bucketbuf is to ensure a cleanup lock on primary bucket page. + */ + (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 1ae2320a29f..d842c3fedc5 100644 --- a/src/backend/access/transam/xlogrecovery.c +++ b/src/backend/access/transam/xlogrecovery.c @@ -441,7 +441,6 @@ static int XLogFileReadAnyTLI(XLogSegNo segno, int emode, XLogSource source); static bool CheckForStandbyTrigger(void); static void SetPromoteIsTriggered(void); -static bool HotStandbyActiveInReplay(void); static void SetCurrentChunkStartTime(TimestampTz xtime); static void SetLatestXTime(TimestampTz xtime); @@ -4583,7 +4582,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 8d1a1277fbc..f86e11f3d5b 100644 --- a/src/include/access/xlogrecovery.h +++ b/src/include/access/xlogrecovery.h @@ -136,6 +136,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); [parent: 59f0c2be1ecc]