From cb42720f8f13188763f0839c2987d31353814e3d Mon Sep 17 00:00:00 2001 From: shihao zhong Date: Mon, 30 Nov 2026 19:49:53 -0500 Subject: [PATCH v1 2/2] Log a snapshot conflict for index builds and table rewrites They leave out dead tuples or freeze xmins that a standby snapshot may still need. Reported-by: Michael Malis Reported-by: Dave Sharpe Discussion: https://postgr.es/m/CANVD+MDikAoTBK1ZCAd+_OuXdNO-kkcBke4E54MGfgWwdYmcGQ@mail.gmail.com Discussion: https://postgr.es/m/CAKFmFJ9niQECQoJ8Jviitjq_KZRqzYKMY3=NfrVm3fUao4ia-g@mail.gmail.com --- src/backend/access/heap/heapam_handler.c | 39 ++++++++++++++++++++ src/test/recovery/t/031_recovery_conflict.pl | 20 ++++++++++ 2 files changed, 59 insertions(+) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index cb4bc35c93e..4112d21198e 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -23,12 +23,15 @@ #include "access/heapam.h" #include "access/heaptoast.h" #include "access/multixact.h" +#include "access/nbtxlog.h" #include "access/rewriteheap.h" #include "access/syncscan.h" #include "access/tableam.h" #include "access/tsmapi.h" #include "access/visibilitymap.h" #include "access/xact.h" +#include "access/xlog.h" +#include "access/xloginsert.h" #include "catalog/catalog.h" #include "catalog/index.h" #include "catalog/storage.h" @@ -49,6 +52,7 @@ static void reform_and_rewrite_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, Datum *values, bool *isnull, RewriteState rwstate); +static void log_heap_conflict(Relation rel, TransactionId conflict_xid); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -681,6 +685,31 @@ heapam_relation_copy_data(Relation rel, const RelFileLocator *newrlocator) smgrclose(dstrel); } +/* + * Log a snapshot conflict for tuples left out of a new heap or index. + * Borrow nbtree's record, as an older standby can't replay a new one. + */ +static void +log_heap_conflict(Relation rel, TransactionId conflict_xid) +{ + xl_btree_reuse_page xlrec; + + if (!TransactionIdIsValid(conflict_xid) || + !RelationNeedsWAL(rel) || !XLogStandbyInfoActive()) + return; + + xlrec.locator = rel->rd_locator; + xlrec.block = InvalidBlockNumber; + xlrec.snapshotConflictHorizon = + FullTransactionIdFromAllowableAt(ReadNextFullTransactionId(), + conflict_xid); + xlrec.isCatalogRel = RelationIsAccessibleInLogicalDecoding(rel); + + XLogBeginInsert(); + XLogRegisterData(&xlrec, SizeOfBtreeReusePage); + XLogInsert(RM_BTREE_ID, XLOG_BTREE_REUSE_PAGE); +} + static void heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, Relation OldIndex, bool use_sort, @@ -705,6 +734,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool *isnull; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; + TransactionId conflict_xid = OldestXmin; /* Remember if it's a system catalog */ is_system_catalog = IsSystemRelation(OldHeap); @@ -724,6 +754,10 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, rwstate = begin_heap_rewrite(OldHeap, NewHeap, OldestXmin, *xid_cutoff, *multi_cutoff); + /* Dead tuples are left out and older xmins get frozen */ + TransactionIdRetreat(conflict_xid); + log_heap_conflict(OldHeap, conflict_xid); + /* Set up sorting if wanted */ if (use_sort) @@ -1195,6 +1229,7 @@ heapam_index_build_range_scan(Relation heapRelation, Snapshot snapshot; bool need_unregister_snapshot = false; TransactionId OldestXmin; + TransactionId conflict_xid = InvalidTransactionId; BlockNumber previous_blkno = InvalidBlockNumber; BlockNumber root_blkno = InvalidBlockNumber; OffsetNumber root_offsets[MaxHeapTuplesPerPage]; @@ -1417,6 +1452,8 @@ heapam_index_build_range_scan(Relation heapRelation, { case HEAPTUPLE_DEAD: /* Definitely dead, we can ignore it */ + HeapTupleHeaderAdvanceConflictHorizon(heapTuple->t_data, + &conflict_xid); indexIt = false; tupleIsAlive = false; break; @@ -1728,6 +1765,8 @@ heapam_index_build_range_scan(Relation heapRelation, table_endscan(scan); + log_heap_conflict(heapRelation, conflict_xid); + /* we can now forget our snapshot, if set and registered by us */ if (need_unregister_snapshot) UnregisterSnapshot(snapshot); diff --git a/src/test/recovery/t/031_recovery_conflict.pl b/src/test/recovery/t/031_recovery_conflict.pl index d1392981db0..ddfe8d7e5a6 100644 --- a/src/test/recovery/t/031_recovery_conflict.pl +++ b/src/test/recovery/t/031_recovery_conflict.pl @@ -194,6 +194,26 @@ like($psql_standby->query_safe($hash_query), $psql_standby->query_safe("COMMIT; RESET ALL;"); +## RECOVERY CONFLICT 2b: Snapshot conflict in index build and table rewrite +foreach my $cmd ("CREATE INDEX ON $table1(a)", "VACUUM FULL $table1") +{ + $sect = "snapshot conflict ($cmd)"; + $expected_conflicts++; + + $psql_standby->query_safe( + "BEGIN ISOLATION LEVEL REPEATABLE READ; SELECT 1;"); + $node_primary->safe_psql($test_db, + "DELETE FROM $table1 WHERE a = (SELECT max(a) FROM $table1);"); + $node_primary->safe_psql($test_db, $cmd); + $node_primary->wait_for_replay_catchup($node_standby); + + check_conflict_log( + "User query might have needed to see row versions that must be removed" + ); + $psql_standby->reconnect_and_clear(); +} + + ## RECOVERY CONFLICT 3: Lock conflict $sect = "lock conflict"; $expected_conflicts++; -- 2.37.1 (Apple Git-137.1)