From fb18dcec707f162f531dc28cf1d453a38b05028e Mon Sep 17 00:00:00 2001 From: Jan Nidzwetzki Date: Mon, 16 Nov 2026 12:52:05 +0100 Subject: [PATCH] Don't let unlogged hint bits reach disk on buffers dirtied in recovery pg_rewind copies only the blocks referenced in the target's WAL after the last common checkpoint. It uses full-page images for blocks first changed after that checkpoint's redo pointer. This method assumes that any buffer still dirty at the point of divergence was last changed after the redo pointer. On a primary, this works because the checkpoint flushes all older changes. But on a standby, checkpoint records are replayed without always doing a restart point. A hint bit set on this kind of buffer is not WAL-logged because the code only avoids making clean buffers dirty during recovery. If the hint shows a commit or abort that happened after the divergence, it stays after pg_rewind. This can conflict with the source's commit log and cause rows to appear visible or invisible when they shouldn't. The same problem can happen after promotion and before the first checkpoint, since the server compares page LSNs with the redo pointer from its last restartpoint until then. To fix this, keep track of the redo pointer and oldestActiveXid from the last four replayed checkpoints. oldestActiveXid is calculated before the redo pointer, so any transaction with a lower xid finished before that checkpoint started, and timelines that fork after the checkpoint agree on its result. For a permanent page whose LSN is not newer than the redo pointer of the last replayed checkpoint, a hint bit is only safe if its xid is before the oldestActiveXid of the first replayed checkpoint that started at or after the page's LSN. If the page is older than all remembered checkpoints, if that checkpoint is a shutdown checkpoint without oldestActiveXid, or if the caller doesn't pass an xid, the hint is unsafe. During recovery, an unsafe hint is not set on a dirty buffer. After promotion and until the first checkpoint finishes, an unsafe hint is WAL-logged. However, callers of BufferBeginSetHintBits() without an xid, like index LP_DEAD and FSM hints, skip this. If a hint makes a clean buffer dirty during this time, it is WAL-logged even if it is safe, because the buffer might have hints that were set in memory during recovery. Reported-by: Tom Pang Author: Jan Nidzwetzki Co-authored-by: Tom Pang Discussion: https://postgr.es/m/CAHebB-qQZaE9yhtUhMMS4ausPYp2PN+XyyhN7fCNe43x=-o8Ew@mail.gmail.com --- src/backend/access/heap/heapam_visibility.c | 46 +++-- src/backend/access/transam/xlog.c | 93 +++++++++ src/backend/access/transam/xloginsert.c | 4 +- src/backend/storage/buffer/bufmgr.c | 112 ++++++++++- src/bin/pg_rewind/Makefile | 5 + src/bin/pg_rewind/meson.build | 4 + src/bin/pg_rewind/t/012_hint_bits.pl | 210 ++++++++++++++++++++ src/include/access/xlog.h | 2 + src/include/access/xloginsert.h | 3 +- src/include/storage/bufmgr.h | 4 +- 10 files changed, 457 insertions(+), 26 deletions(-) create mode 100644 src/bin/pg_rewind/t/012_hint_bits.pl diff --git a/src/backend/access/heap/heapam_visibility.c b/src/backend/access/heap/heapam_visibility.c index 905444ae036..eb7474b7b91 100644 --- a/src/backend/access/heap/heapam_visibility.c +++ b/src/backend/access/heap/heapam_visibility.c @@ -96,8 +96,26 @@ typedef enum SetHintBitsState SHB_DISABLED, /* allowed to set hint bits */ SHB_ENABLED, + /* hint bits are restricted, set them one by one with their XID */ + SHB_PER_XID, } SetHintBitsState; +/* + * The transaction a hint bit is about, or InvalidTransactionId if that is + * not a single plain XID. + */ +static inline TransactionId +HintBitXid(HeapTupleHeader tuple, uint16 infomask) +{ + if (tuple->t_infomask & HEAP_MOVED) + return InvalidTransactionId; + if (infomask & (HEAP_XMIN_COMMITTED | HEAP_XMIN_INVALID)) + return HeapTupleHeaderGetRawXmin(tuple); + if (tuple->t_infomask & HEAP_XMAX_IS_MULTI) + return InvalidTransactionId; + return HeapTupleHeaderGetRawXmax(tuple); +} + /* * SetHintBitsExt() * @@ -165,29 +183,33 @@ SetHintBitsExt(HeapTupleHeader tuple, Buffer buffer, } } + if (state && *state == SHB_INITIAL) + { + if (BufferBeginSetHintBits(buffer)) + *state = SHB_ENABLED; + else if (BufferHintsNeedXid(buffer)) + *state = SHB_PER_XID; + else + { + *state = SHB_DISABLED; + return; + } + } + /* * If we're not operating in batch mode, use BufferSetHintBits16() to mark * the page dirty, that's cheaper than * BufferBeginSetHintBits()/BufferFinishSetHintBits(). That's important * for cases where we set a lot of hint bits on a page individually. */ - if (!state) + if (!state || unlikely(*state == SHB_PER_XID)) { BufferSetHintBits16(&tuple->t_infomask, - tuple->t_infomask | infomask, buffer); + tuple->t_infomask | infomask, buffer, + HintBitXid(tuple, infomask)); return; } - if (*state == SHB_INITIAL) - { - if (!BufferBeginSetHintBits(buffer)) - { - *state = SHB_DISABLED; - return; - } - - *state = SHB_ENABLED; - } tuple->t_infomask |= infomask; } diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fd37ea4f607..686e65482cc 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -451,6 +451,9 @@ typedef struct XLogCtlInsert WALInsertLockPadded *WALInsertLocks; } XLogCtlInsert; +/* number of replayed checkpoints remembered for hint bit decisions */ +#define NUM_REPLAYED_CKPTS 4 + /* * Total shared-memory state for XLOG. */ @@ -576,6 +579,19 @@ typedef struct XLogCtlData * since the server started. */ pg_atomic_uint64 lastChecksumChangeRecPtr; + + /* + * Redo pointer of the last checkpoint record replayed during recovery. + * Reset to InvalidXLogRecPtr by the first checkpoint after recovery. + */ + pg_atomic_uint64 lastReplayedCheckPointRedo; + + /* + * Redo pointer and oldestActiveXid of the last replayed checkpoints, + * newest first. Protected by info_lck. + */ + XLogRecPtr replayedCkptRedo[NUM_REPLAYED_CKPTS]; + TransactionId replayedCkptOldestXid[NUM_REPLAYED_CKPTS]; } XLogCtlData; /* @@ -773,6 +789,7 @@ static void UpdateLastRemovedPtr(char *filename); static void ValidateXLOGDirectoryStructure(void); static void CleanupBackupHistory(void); static void UpdateMinRecoveryPoint(XLogRecPtr lsn, bool force); +static void RememberReplayedCheckPoint(const CheckPoint *checkPoint); static bool PerformRecoveryXLogAction(void); static void CheckReplayedDataChecksumState(uint32 replayed_state); static void AdoptReplayedDataChecksumState(uint32 new_version, XLogRecPtr lsn); @@ -5648,6 +5665,7 @@ XLOGShmemInit(void *arg) pg_atomic_init_u64(&XLogCtl->logFlushResult, InvalidXLogRecPtr); pg_atomic_init_u64(&XLogCtl->unloggedLSN, InvalidXLogRecPtr); pg_atomic_init_u64(&XLogCtl->lastChecksumChangeRecPtr, InvalidXLogRecPtr); + pg_atomic_init_u64(&XLogCtl->lastReplayedCheckPointRedo, InvalidXLogRecPtr); } /* @@ -6398,6 +6416,8 @@ StartupXLOG(void) lastFullPageWrites = checkPoint.fullPageWrites; RedoRecPtr = XLogCtl->RedoRecPtr = XLogCtl->Insert.RedoRecPtr = checkPoint.redo; + if (InRecovery) + RememberReplayedCheckPoint(&checkPoint); doPageWrites = lastFullPageWrites; /* REDO */ @@ -7221,6 +7241,73 @@ LocalSetXLogInsertAllowed(void) return oldXLogAllowed; } +/* + * Remember a checkpoint record replayed in recovery. + */ +static void +RememberReplayedCheckPoint(const CheckPoint *checkPoint) +{ + SpinLockAcquire(&XLogCtl->info_lck); + memmove(&XLogCtl->replayedCkptRedo[1], &XLogCtl->replayedCkptRedo[0], + sizeof(XLogRecPtr) * (NUM_REPLAYED_CKPTS - 1)); + memmove(&XLogCtl->replayedCkptOldestXid[1], + &XLogCtl->replayedCkptOldestXid[0], + sizeof(TransactionId) * (NUM_REPLAYED_CKPTS - 1)); + XLogCtl->replayedCkptRedo[0] = checkPoint->redo; + XLogCtl->replayedCkptOldestXid[0] = checkPoint->oldestActiveXid; + SpinLockRelease(&XLogCtl->info_lck); + + pg_atomic_write_membarrier_u64(&XLogCtl->lastReplayedCheckPointRedo, + checkPoint->redo); +} + +/* + * Did transaction 'xid' end before the first replayed checkpoint that + * started after 'page_lsn'? + * + * If so, no checkpoint lies between the last change of the page and the end + * of the transaction, and a hint bit for it is safe even if the buffer is + * still dirty from before the last replayed checkpoint. + */ +bool +RecoveryHintXidIsSafe(TransactionId xid, XLogRecPtr page_lsn) +{ + static XLogRecPtr redo[NUM_REPLAYED_CKPTS]; + static TransactionId oldestXid[NUM_REPLAYED_CKPTS]; + TransactionId limit = InvalidTransactionId; + int i; + + if (!TransactionIdIsNormal(xid)) + return false; + + if (redo[0] != pg_atomic_read_u64(&XLogCtl->lastReplayedCheckPointRedo)) + { + SpinLockAcquire(&XLogCtl->info_lck); + memcpy(redo, XLogCtl->replayedCkptRedo, sizeof(redo)); + memcpy(oldestXid, XLogCtl->replayedCkptOldestXid, sizeof(oldestXid)); + SpinLockRelease(&XLogCtl->info_lck); + } + + for (i = 0; i < NUM_REPLAYED_CKPTS && redo[i] >= page_lsn; i++) + limit = oldestXid[i]; + + /* unknown if the page is older than all remembered checkpoints */ + if (i == NUM_REPLAYED_CKPTS || !XLogRecPtrIsValid(redo[i])) + return false; + + return TransactionIdIsValid(limit) && TransactionIdPrecedes(xid, limit); +} + +/* + * Return the redo pointer of the last checkpoint record replayed in recovery, + * or InvalidXLogRecPtr once a checkpoint has completed after recovery. + */ +XLogRecPtr +GetLastReplayedCheckPointRedo(void) +{ + return pg_atomic_read_u64(&XLogCtl->lastReplayedCheckPointRedo); +} + /* * Return the current Redo pointer from shared memory. * @@ -8154,6 +8241,10 @@ CreateCheckPoint(int flags) UpdateControlFile(); LWLockRelease(ControlFileLock); + /* all buffers dirtied during recovery have been written by now */ + pg_atomic_write_membarrier_u64(&XLogCtl->lastReplayedCheckPointRedo, + InvalidXLogRecPtr); + /* * We are now done with critical updates; no need for system panic if we * have trouble while fooling with old log segments. @@ -9412,6 +9503,7 @@ xlog_redo(XLogReaderState *record) (errmsg("unexpected timeline ID %u (should be %u) in shutdown checkpoint record", checkPoint.ThisTimeLineID, replayTLI))); + RememberReplayedCheckPoint(&checkPoint); RecoveryRestartPoint(&checkPoint, record); /* @@ -9472,6 +9564,7 @@ xlog_redo(XLogReaderState *record) (errmsg("unexpected timeline ID %u (should be %u) in online checkpoint record", checkPoint.ThisTimeLineID, replayTLI))); + RememberReplayedCheckPoint(&checkPoint); RecoveryRestartPoint(&checkPoint, record); /* diff --git a/src/backend/access/transam/xloginsert.c b/src/backend/access/transam/xloginsert.c index 341deb7b80d..9b6683ffc23 100644 --- a/src/backend/access/transam/xloginsert.c +++ b/src/backend/access/transam/xloginsert.c @@ -1153,7 +1153,7 @@ XLogCheckBufferNeedsBackup(Buffer buffer) * had to write, InvalidXLogRecPtr otherwise. */ XLogRecPtr -XLogSaveBufferForHint(Buffer buffer, bool buffer_std) +XLogSaveBufferForHint(Buffer buffer, bool buffer_std, bool force) { XLogRecPtr recptr = InvalidXLogRecPtr; XLogRecPtr lsn; @@ -1180,7 +1180,7 @@ XLogSaveBufferForHint(Buffer buffer, bool buffer_std) */ lsn = PageGetLSN(BufferGetPage(buffer)); - if (lsn <= RedoRecPtr) + if (lsn <= RedoRecPtr || force) { int flags = 0; diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index c17fc3590f0..ea212757f94 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -5713,6 +5713,41 @@ IncrBufferRefCount(Buffer buffer) ResourceOwnerRememberBuffer(CurrentResourceOwner, buffer); } +/* + * Has the first checkpoint after recovery completed? + * + * Until then, a buffer can still be dirty from before the last replayed + * checkpoint. A hint bit set on it would reach disk without pg_rewind + * noticing the block, so we skip the hint in recovery and WAL-log it after. + */ +static bool recovery_hints_done = false; + +/* + * Is this a buffer on which hint bits need special care, unless the + * transaction is known to be safe? In recovery such hints must not be set, + * after recovery they must be WAL-logged. + */ +static inline bool +RecoveryHintRestricted(Buffer buffer, uint64 buf_state) +{ + XLogRecPtr threshold; + + if (likely(recovery_hints_done) || !(buf_state & BM_PERMANENT) || + !XLogHintBitIsNeeded()) + return false; + + threshold = GetLastReplayedCheckPointRedo(); + if (!XLogRecPtrIsValid(threshold)) + { + recovery_hints_done = true; + return false; + } + + /* in recovery a clean buffer is fine, the hint does not dirty it */ + return BufferGetLSNAtomic(buffer) <= threshold && + ((buf_state & BM_DIRTY) || !RecoveryInProgress()); +} + /* * Shared-buffer only helper for MarkBufferDirtyHint() and * BufferSetHintBits16(). @@ -5723,7 +5758,7 @@ IncrBufferRefCount(Buffer buffer) */ static inline void MarkSharedBufferDirtyHint(Buffer buffer, BufferDesc *bufHdr, uint64 lockstate, - bool buffer_std) + bool buffer_std, bool force_wal) { Page page = BufferGetPage(buffer); @@ -5801,9 +5836,15 @@ MarkSharedBufferDirtyHint(Buffer buffer, BufferDesc *bufHdr, uint64 lockstate, * Note that aggressive cleaning of blocks dirtied by hint bit setting * would increase the call rate. Bulk setting of hint bits would * reduce the call rate... + * + * A clean buffer can carry hints set during recovery, which nobody + * checked. So until the first checkpoint after recovery, WAL-log + * regardless of the hint being set now. */ if (wal_log) - lsn = XLogSaveBufferForHint(buffer, buffer_std); + lsn = XLogSaveBufferForHint(buffer, buffer_std, + force_wal || + RecoveryHintRestricted(buffer, lockstate)); if (XLogRecPtrIsValid(lsn)) { @@ -5829,6 +5870,20 @@ MarkSharedBufferDirtyHint(Buffer buffer, BufferDesc *bufHdr, uint64 lockstate, if (VacuumCostActive) VacuumCostBalance += VacuumCostPageDirty; } + else if (unlikely(force_wal) && + !RelFileLocatorSkippingWAL(BufTagGetRelFileLocator(&bufHdr->tag))) + { + XLogRecPtr lsn; + + /* dirtied before the last replayed checkpoint, WAL-log the hint */ + lsn = XLogSaveBufferForHint(buffer, buffer_std, true); + if (XLogRecPtrIsValid(lsn)) + { + LockBufHdr(bufHdr); + PageSetLSN(page, lsn); + UnlockBufHdr(bufHdr); + } + } } /* @@ -5845,11 +5900,14 @@ MarkSharedBufferDirtyHint(Buffer buffer, BufferDesc *bufHdr, uint64 lockstate, * 3. This function does not guarantee that the buffer is always marked dirty * (it e.g. can't always on a hot standby), so it cannot be used for * important changes. + * 4. If the buffer is already dirty, a WAL record may still be needed until + * the first checkpoint after recovery has completed. */ inline void MarkBufferDirtyHint(Buffer buffer, bool buffer_std) { BufferDesc *bufHdr; + uint64 buf_state; if (!BufferIsValid(buffer)) elog(ERROR, "bad buffer ID: %d", buffer); @@ -5861,10 +5919,11 @@ MarkBufferDirtyHint(Buffer buffer, bool buffer_std) } bufHdr = GetBufferDescriptor(buffer - 1); + buf_state = pg_atomic_read_u64(&bufHdr->state); - MarkSharedBufferDirtyHint(buffer, bufHdr, - pg_atomic_read_u64(&bufHdr->state), - buffer_std); + MarkSharedBufferDirtyHint(buffer, bufHdr, buf_state, buffer_std, + RecoveryHintRestricted(buffer, buf_state) && + !RecoveryInProgress()); } /* @@ -7003,7 +7062,8 @@ IsBufferCleanupOK(Buffer buffer) * Updates *lockstate when returning true. */ static inline bool -SharedBufferBeginSetHintBits(Buffer buffer, BufferDesc *buf_hdr, uint64 *lockstate) +SharedBufferBeginSetHintBits(Buffer buffer, BufferDesc *buf_hdr, uint64 *lockstate, + TransactionId xid, bool *force_wal) { uint64 old_state; PrivateRefCountEntry *ref; @@ -7018,6 +7078,20 @@ SharedBufferBeginSetHintBits(Buffer buffer, BufferDesc *buf_hdr, uint64 *locksta if (mode == BUFFER_LOCK_UNLOCK) elog(ERROR, "buffer is not locked"); + /* + * Don't set hints on a page last changed before the last replayed + * checkpoint, unless the transaction ended early enough. After recovery, + * a single hint is WAL-logged instead. + */ + old_state = pg_atomic_read_u64(&buf_hdr->state); + if (RecoveryHintRestricted(buffer, old_state) && + !RecoveryHintXidIsSafe(xid, BufferGetLSNAtomic(buffer))) + { + if (force_wal == NULL || RecoveryInProgress()) + return false; + *force_wal = true; + } + /* we're done if we are already holding a sufficient lock level */ if (mode == BUFFER_LOCK_EXCLUSIVE || mode == BUFFER_LOCK_SHARE_EXCLUSIVE) { @@ -7110,7 +7184,23 @@ BufferBeginSetHintBits(Buffer buffer) buf_hdr = GetBufferDescriptor(buffer - 1); - return SharedBufferBeginSetHintBits(buffer, buf_hdr, &lockstate); + return SharedBufferBeginSetHintBits(buffer, buf_hdr, &lockstate, + InvalidTransactionId, NULL); +} + +/* + * Did BufferBeginSetHintBits() fail because hint bits are restricted on this + * buffer? If so, they can still be set one by one with + * BufferSetHintBits16(), which is told the transaction. + */ +bool +BufferHintsNeedXid(Buffer buffer) +{ + if (BufferIsLocal(buffer)) + return false; + + return RecoveryHintRestricted(buffer, + pg_atomic_read_u64(&GetBufferDescriptor(buffer - 1)->state)); } /* @@ -7145,10 +7235,11 @@ BufferFinishSetHintBits(Buffer buffer, bool mark_dirty, bool buffer_std) * than the former when setting hint bits multiple times in the same buffer. */ bool -BufferSetHintBits16(uint16 *ptr, uint16 val, Buffer buffer) +BufferSetHintBits16(uint16 *ptr, uint16 val, Buffer buffer, TransactionId xid) { BufferDesc *buf_hdr; uint64 lockstate; + bool force_wal = false; #ifdef USE_ASSERT_CHECKING char *page; @@ -7168,11 +7259,12 @@ BufferSetHintBits16(uint16 *ptr, uint16 val, Buffer buffer) buf_hdr = GetBufferDescriptor(buffer - 1); - if (SharedBufferBeginSetHintBits(buffer, buf_hdr, &lockstate)) + if (SharedBufferBeginSetHintBits(buffer, buf_hdr, &lockstate, xid, + &force_wal)) { *ptr = val; - MarkSharedBufferDirtyHint(buffer, buf_hdr, lockstate, true); + MarkSharedBufferDirtyHint(buffer, buf_hdr, lockstate, true, force_wal); return true; } diff --git a/src/bin/pg_rewind/Makefile b/src/bin/pg_rewind/Makefile index 32a35c57612..e0a39723b52 100644 --- a/src/bin/pg_rewind/Makefile +++ b/src/bin/pg_rewind/Makefile @@ -30,6 +30,11 @@ OBJS = \ timeline.o \ xlogreader.o +EXTRA_INSTALL = contrib/pageinspect contrib/pg_buffercache \ + src/test/modules/injection_points + +export enable_injection_points + EXTRA_CLEAN = xlogreader.c all: pg_rewind diff --git a/src/bin/pg_rewind/meson.build b/src/bin/pg_rewind/meson.build index 52a6ab0a515..16c92733b2e 100644 --- a/src/bin/pg_rewind/meson.build +++ b/src/bin/pg_rewind/meson.build @@ -33,6 +33,9 @@ tests += { 'sd': meson.current_source_dir(), 'bd': meson.current_build_dir(), 'tap': { + 'env': { + 'enable_injection_points': get_option('injection_points') ? 'yes' : 'no', + }, 'tests': [ 't/001_basic.pl', 't/002_databases.pl', @@ -45,6 +48,7 @@ tests += { 't/009_growing_files.pl', 't/010_keep_recycled_wals.pl', 't/011_wal_copy.pl', + 't/012_hint_bits.pl', ], }, } diff --git a/src/bin/pg_rewind/t/012_hint_bits.pl b/src/bin/pg_rewind/t/012_hint_bits.pl new file mode 100644 index 00000000000..68420eda646 --- /dev/null +++ b/src/bin/pg_rewind/t/012_hint_bits.pl @@ -0,0 +1,210 @@ +# Copyright (c) 2026, PostgreSQL Global Development Group + +# +# Hint bits set on a hot standby are not WAL-logged. If the standby replayed +# a commit or abort that the new primary never received, a hint bit for it +# must not survive pg_rewind. +# +# The same applies to hint bits set after a promotion, on pages that were +# last modified before the last replayed checkpoint, whether the buffer is +# still dirty from recovery or has been written out since. +# +use strict; +use warnings FATAL => 'all'; +use PostgreSQL::Test::Cluster; +use PostgreSQL::Test::Utils; +use File::Copy; +use Test::More; + +# Run the scenario. If $promote is true, the rewind target is promoted +# before the hint bit is set, otherwise it stays a standby. If $evict is +# true, the buffer is written out before that. +sub run_test +{ + my ($mode, $promote, $evict) = @_; + + my $node_a = PostgreSQL::Test::Cluster->new("a_$mode"); + $node_a->init(allows_streaming => 1, extra => ['-k']); + $node_a->append_conf( + 'postgresql.conf', qq( +wal_log_hints = on +max_prepared_transactions = 5 +shared_buffers = '128MB' +autovacuum = off +checkpoint_timeout = '1h' +max_wal_size = '1GB' +wal_keep_size = '100MB' +)); + $node_a->start; + $node_a->safe_psql('postgres', 'CREATE EXTENSION pageinspect'); + if ($evict) + { + $node_a->safe_psql('postgres', 'CREATE EXTENSION pg_buffercache'); + $node_a->safe_psql('postgres', 'CREATE EXTENSION injection_points'); + } + + $node_a->backup('b'); + my $node_s1 = PostgreSQL::Test::Cluster->new("s1_$mode"); + $node_s1->init_from_backup($node_a, 'b', has_streaming => 1); + $node_s1->start; + my $node_s2 = PostgreSQL::Test::Cluster->new("s2_$mode"); + $node_s2->init_from_backup($node_a, 'b', has_streaming => 1); + $node_s2->start; + + $node_a->safe_psql('postgres', + 'CREATE TABLE c(id int); INSERT INTO c SELECT generate_series(1,100); CHECKPOINT;' + ); + + my $sess = $node_a->background_psql('postgres'); + $sess->query_safe('BEGIN; INSERT INTO c VALUES (999999);'); + my $xid = $sess->query_safe('SELECT pg_current_xact_id()'); + $sess->query_safe("PREPARE TRANSACTION 'p'"); + + # A second transaction changes the page right before the checkpoint + # and aborts after the fork. That leaves the page LSN exactly at the + # redo pointer. + my $sess2 = $node_a->background_psql('postgres'); + $sess2->query_safe('BEGIN; INSERT INTO c VALUES (888888);'); + $node_a->safe_psql('postgres', 'CHECKPOINT'); + $node_a->wait_for_replay_catchup($node_s1); + $node_a->wait_for_replay_catchup($node_s2); + + # Detach S1 from A and promote it. This is the fork point. Without + # the checkpoint, replay on the rewound node would start at S1's old + # restartpoint and the INSERT's FPW would hide the problem. + $node_s1->append_conf('postgresql.conf', "primary_conninfo = ''"); + $node_s1->reload; + $node_s1->poll_query_until('postgres', + 'SELECT count(*) = 0 FROM pg_stat_wal_receiver') + or die 'S1 did not disconnect'; + $node_s1->promote; + $node_s1->safe_psql('postgres', 'CHECKPOINT'); + + # S2 keeps following A and replays the rollback, which S1 never sees. + $sess->quit; + $sess2->quit; + $node_a->safe_psql('postgres', "ROLLBACK PREPARED 'p'"); + $node_a->wait_for_replay_catchup($node_s2); + + # 512 is HEAP_XMIN_INVALID + my $hint_q = + "SELECT (t_infomask & 512) <> 0 FROM heap_page_items(get_raw_page('c', 0)) + WHERE t_xmin::text = '$xid'"; + + # Write out the buffer without a restartpoint, and hold back the + # checkpoint that follows the promotion. S2's redo pointer then stays + # older than the page, so a hint bit set after the promotion would not + # be WAL-logged on its own. + if ($evict) + { + $node_s2->safe_psql( + 'postgres', + "SELECT pg_buffercache_evict(bufferid) FROM pg_buffercache + WHERE relfilenode = pg_relation_filenode('c')"); + + # Set the hint bits of both transactions while still in recovery. + # The buffer is clean, so they stay in memory. After the promotion + # only harmless hint bits are left to set, and those must not take + # these two to disk unlogged. + $node_s2->safe_psql('postgres', + "SELECT 1 FROM c WHERE ctid IN ('(0,101)', '(0,102)')"); + is($node_s2->safe_psql('postgres', $hint_q), + 't', "$mode: hint bit set in memory before promotion"); + $node_s2->safe_psql('postgres', + "SELECT injection_points_attach('create-checkpoint-initial', 'wait')" + ); + } + + if ($promote) + { + # Let S2 pick timeline 3, otherwise both nodes end up on timeline + # 2 and pg_rewind sees nothing to do. + copy( + $node_s1->data_dir . '/pg_wal/00000002.history', + $node_s2->data_dir . '/pg_wal/00000002.history' + ) or die "could not copy history file: $!"; + $node_s2->promote; + $node_s2->wait_for_event('checkpointer', 'create-checkpoint-initial') + if $evict; + } + + # Reading c tries to set the hint bit. The page was last modified + # before the last replayed checkpoint. + is($node_s2->safe_psql('postgres', 'SELECT count(*) FROM c'), + '100', "$mode: S2 does not see the rolled back row"); + if ($promote) + { + is($node_s2->safe_psql('postgres', $hint_q), + 't', "$mode: hint bit set on S2"); + } + else + { + is($node_s2->safe_psql('postgres', $hint_q), + 'f', "$mode: hint bit not set on S2"); + + # The other rows were committed before the last checkpoint started, + # so their hint bits are fine. 256 is HEAP_XMIN_COMMITTED. + is( $node_s2->safe_psql( + 'postgres', + "SELECT bool_and((t_infomask & 256) <> 0) + FROM heap_page_items(get_raw_page('c', 0)) + WHERE lp <= 100"), + 't', + "$mode: hint bits of older transactions set on S2"); + } + + if ($evict) + { + $node_s2->safe_psql('postgres', + "SELECT injection_points_wakeup('create-checkpoint-initial')"); + $node_s2->safe_psql('postgres', + "SELECT injection_points_detach('create-checkpoint-initial')"); + } + + $node_s2->stop('fast'); + $node_a->stop('immediate'); + + # S1 recovered the prepared transaction and commits it on its timeline. + $node_s1->safe_psql('postgres', "COMMIT PREPARED 'p'"); + + command_ok( + [ + 'pg_rewind', + '--source-server' => $node_s1->connstr('postgres'), + '--target-pgdata' => $node_s2->data_dir, + '--write-recovery-conf', + ], + "$mode: pg_rewind runs"); + + # pg_rewind copied S1's postgresql.conf + $node_s2->append_conf('postgresql.conf', 'port = ' . $node_s2->port); + $node_s2->start; + $node_s1->wait_for_replay_catchup($node_s2); + + is($node_s2->safe_psql('postgres', $hint_q), + 'f', "$mode: no hint bit on S2 after rewind"); + is( $node_s2->safe_psql( + 'postgres', 'SELECT count(*) FROM c WHERE id = 999999'), + '1', + "$mode: committed row visible on S2 after rewind"); + + # Reading c on S1 emits an FPI_FOR_HINT that would mask the problem on + # S2, so do this last. + is( $node_s1->safe_psql( + 'postgres', 'SELECT count(*) FROM c WHERE id = 999999'), + '1', + "$mode: row visible on S1"); + + $node_s1->stop; + $node_s2->stop; + return; +} + +run_test('standby', 0, 0); +run_test('promoted', 1, 0); + +# The last scenario needs an injection point to hold back the checkpoint. +run_test('promoted_clean', 1, 1) + if $ENV{enable_injection_points} eq 'yes'; + +done_testing(); diff --git a/src/include/access/xlog.h b/src/include/access/xlog.h index 2bf5714b51e..b71e4d9aa52 100644 --- a/src/include/access/xlog.h +++ b/src/include/access/xlog.h @@ -293,6 +293,8 @@ extern XLogRecPtr XLogAssignLSN(void); extern void UpdateFullPageWrites(void); extern void GetFullPageWriteInfo(XLogRecPtr *RedoRecPtr_p, bool *doPageWrites_p); extern XLogRecPtr GetRedoRecPtr(void); +extern XLogRecPtr GetLastReplayedCheckPointRedo(void); +extern bool RecoveryHintXidIsSafe(TransactionId xid, XLogRecPtr page_lsn); extern XLogRecPtr GetInsertRecPtr(void); extern XLogRecPtr GetFlushRecPtr(TimeLineID *insertTLI); extern TimeLineID GetWALInsertionTimeLine(void); diff --git a/src/include/access/xloginsert.h b/src/include/access/xloginsert.h index 86264564ebb..8977f37af33 100644 --- a/src/include/access/xloginsert.h +++ b/src/include/access/xloginsert.h @@ -64,7 +64,8 @@ extern void log_newpages(RelFileLocator *rlocator, ForkNumber forknum, int num_p extern XLogRecPtr log_newpage_buffer(Buffer buffer, bool page_std); extern void log_newpage_range(Relation rel, ForkNumber forknum, BlockNumber startblk, BlockNumber endblk, bool page_std); -extern XLogRecPtr XLogSaveBufferForHint(Buffer buffer, bool buffer_std); +extern XLogRecPtr XLogSaveBufferForHint(Buffer buffer, bool buffer_std, + bool force); extern XLogRecPtr XLogGetFakeLSN(Relation rel); diff --git a/src/include/storage/bufmgr.h b/src/include/storage/bufmgr.h index 6837b35fc6d..3a2bd9122c3 100644 --- a/src/include/storage/bufmgr.h +++ b/src/include/storage/bufmgr.h @@ -316,7 +316,9 @@ extern void BufferGetTag(Buffer buffer, RelFileLocator *rlocator, extern void MarkBufferDirtyHint(Buffer buffer, bool buffer_std); -extern bool BufferSetHintBits16(uint16 *ptr, uint16 val, Buffer buffer); +extern bool BufferSetHintBits16(uint16 *ptr, uint16 val, Buffer buffer, + TransactionId xid); +extern bool BufferHintsNeedXid(Buffer buffer); extern bool BufferBeginSetHintBits(Buffer buffer); extern void BufferFinishSetHintBits(Buffer buffer, bool mark_dirty, bool buffer_std); -- 2.47.3