From 494284658f4df7d0724081954767ad804218b602 Mon Sep 17 00:00:00 2001 From: shihao zhong Date: Mon, 30 Nov 2026 19:31:07 -0500 Subject: [PATCH v1 2/3] 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 --- doc/src/sgml/high-availability.sgml | 8 +++++ src/backend/access/heap/heapam_handler.c | 14 +++++++++ src/backend/access/rmgrdesc/standbydesc.c | 13 ++++++++ src/backend/replication/logical/decode.c | 1 + src/backend/storage/ipc/standby.c | 33 ++++++++++++++++++++ src/include/storage/standby.h | 3 ++ src/include/storage/standbydefs.h | 15 +++++++++ src/test/recovery/t/031_recovery_conflict.pl | 20 ++++++++++++ src/tools/pgindent/typedefs.list | 1 + 9 files changed, 108 insertions(+) diff --git a/doc/src/sgml/high-availability.sgml b/doc/src/sgml/high-availability.sgml index 6e42e0eda49..ab0e434737e 100644 --- a/doc/src/sgml/high-availability.sgml +++ b/doc/src/sgml/high-availability.sgml @@ -1900,6 +1900,14 @@ postgres=# WAIT FOR LSN '0/306EE20'; the data to be removed is visible. + + + Building an index on the primary conflicts with standby transactions + whose snapshots can still see any of the rows left out + of the index. Rewriting a table conflicts with all standby snapshots + that are older than the ones in use on the primary. + + diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 1437274b0c6..2279a81c808 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -45,6 +45,7 @@ #include "storage/predicate.h" #include "storage/procarray.h" #include "storage/smgr.h" +#include "storage/standby.h" #include "utils/builtins.h" #include "utils/rel.h" #include "utils/tuplesort.h" @@ -636,8 +637,16 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, * needed in concurrent mode. */ if (!concurrent) + { + TransactionId conflict_xid = OldestXmin; + rwstate = begin_heap_rewrite(OldHeap, NewHeap, OldestXmin, *xid_cutoff, *multi_cutoff); + + /* Dead tuples are left out and older xmins get frozen */ + TransactionIdRetreat(conflict_xid); + LogStandbySnapshotConflict(OldHeap, conflict_xid); + } else rwstate = NULL; @@ -1158,6 +1167,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]; @@ -1380,6 +1390,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; @@ -1691,6 +1703,8 @@ heapam_index_build_range_scan(Relation heapRelation, table_endscan(scan); + LogStandbySnapshotConflict(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/backend/access/rmgrdesc/standbydesc.c b/src/backend/access/rmgrdesc/standbydesc.c index 0a291354ae2..e27920b4d6b 100644 --- a/src/backend/access/rmgrdesc/standbydesc.c +++ b/src/backend/access/rmgrdesc/standbydesc.c @@ -73,6 +73,16 @@ standby_desc(StringInfo buf, XLogReaderState *record) xlrec->dbId, xlrec->tsId, xlrec->relcacheInitFileInval); } + else if (info == XLOG_STANDBY_SNAPSHOT_CONFLICT) + { + xl_standby_snapshot_conflict *xlrec = (xl_standby_snapshot_conflict *) rec; + + appendStringInfo(buf, "rel: %u/%u/%u, snapshotConflictHorizon: %u, isCatalogRel: %c", + xlrec->locator.spcOid, xlrec->locator.dbOid, + xlrec->locator.relNumber, + xlrec->snapshotConflictHorizon, + xlrec->isCatalogRel ? 'T' : 'F'); + } } const char * @@ -91,6 +101,9 @@ standby_identify(uint8 info) case XLOG_INVALIDATIONS: id = "INVALIDATIONS"; break; + case XLOG_STANDBY_SNAPSHOT_CONFLICT: + id = "SNAPSHOT_CONFLICT"; + break; } return id; diff --git a/src/backend/replication/logical/decode.c b/src/backend/replication/logical/decode.c index 031bb1001bd..21791fef25e 100644 --- a/src/backend/replication/logical/decode.c +++ b/src/backend/replication/logical/decode.c @@ -402,6 +402,7 @@ standby_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf) } break; case XLOG_STANDBY_LOCK: + case XLOG_STANDBY_SNAPSHOT_CONFLICT: break; case XLOG_INVALIDATIONS: diff --git a/src/backend/storage/ipc/standby.c b/src/backend/storage/ipc/standby.c index 5923bdc60c1..6a21dccef06 100644 --- a/src/backend/storage/ipc/standby.c +++ b/src/backend/storage/ipc/standby.c @@ -34,6 +34,7 @@ #include "utils/hsearch.h" #include "utils/injection_point.h" #include "utils/ps_status.h" +#include "utils/rel.h" #include "utils/timeout.h" #include "utils/timestamp.h" #include "utils/wait_event.h" @@ -1243,6 +1244,16 @@ standby_redo(XLogReaderState *record) xlrec->dbId, xlrec->tsId); } + else if (info == XLOG_STANDBY_SNAPSHOT_CONFLICT) + { + xl_standby_snapshot_conflict *xlrec = + (xl_standby_snapshot_conflict *) XLogRecGetData(record); + + if (InHotStandby) + ResolveRecoveryConflictWithSnapshot(xlrec->snapshotConflictHorizon, + xlrec->isCatalogRel, + xlrec->locator); + } else elog(PANIC, "standby_redo: unknown op code %u", info); } @@ -1525,6 +1536,28 @@ LogAccessExclusiveLockPrepare(void) (void) GetCurrentTransactionId(); } +/* + * Log a snapshot conflict for removed or frozen tuples that no other WAL + * record has a conflict horizon for. + */ +void +LogStandbySnapshotConflict(Relation rel, TransactionId snapshotConflictHorizon) +{ + xl_standby_snapshot_conflict xlrec; + + if (!TransactionIdIsValid(snapshotConflictHorizon) || + !RelationNeedsWAL(rel) || !XLogStandbyInfoActive()) + return; + + xlrec.locator = rel->rd_locator; + xlrec.snapshotConflictHorizon = snapshotConflictHorizon; + xlrec.isCatalogRel = RelationIsAccessibleInLogicalDecoding(rel); + + XLogBeginInsert(); + XLogRegisterData(&xlrec, SizeOfStandbySnapshotConflict); + XLogInsert(RM_STANDBY_ID, XLOG_STANDBY_SNAPSHOT_CONFLICT); +} + /* * Emit WAL for invalidations. This currently is only used for commits without * an xid but which contain invalidations. diff --git a/src/include/storage/standby.h b/src/include/storage/standby.h index 6a314c693cd..ad144b4ecb3 100644 --- a/src/include/storage/standby.h +++ b/src/include/storage/standby.h @@ -20,6 +20,7 @@ #include "storage/standbydefs.h" typedef struct PGPROC PGPROC; +typedef struct RelationData *Relation; typedef struct VirtualTransactionId VirtualTransactionId; /* User-settable GUC parameters */ @@ -146,5 +147,7 @@ extern void LogAccessExclusiveLockPrepare(void); extern XLogRecPtr LogStandbySnapshot(void); extern void LogStandbyInvalidations(int nmsgs, SharedInvalidationMessage *msgs, bool relcacheInitFileInval); +extern void LogStandbySnapshotConflict(Relation rel, + TransactionId snapshotConflictHorizon); #endif /* STANDBY_H */ diff --git a/src/include/storage/standbydefs.h b/src/include/storage/standbydefs.h index 231d251fd51..183e6cc915a 100644 --- a/src/include/storage/standbydefs.h +++ b/src/include/storage/standbydefs.h @@ -17,6 +17,7 @@ #include "access/xlogreader.h" #include "lib/stringinfo.h" #include "storage/lockdefs.h" +#include "storage/relfilelocator.h" #include "storage/sinval.h" /* Recovery handlers for the Standby Rmgr (RM_STANDBY_ID) */ @@ -34,6 +35,7 @@ extern void standby_desc_invalidations(StringInfo buf, #define XLOG_STANDBY_LOCK 0x00 #define XLOG_RUNNING_XACTS 0x10 #define XLOG_INVALIDATIONS 0x20 +#define XLOG_STANDBY_SNAPSHOT_CONFLICT 0x30 typedef struct xl_standby_locks { @@ -71,4 +73,17 @@ typedef struct xl_invalidations #define MinSizeOfInvalidations offsetof(xl_invalidations, msgs) +/* + * Snapshot conflict not tied to any page. + */ +typedef struct xl_standby_snapshot_conflict +{ + RelFileLocator locator; + TransactionId snapshotConflictHorizon; + bool isCatalogRel; /* to handle recovery conflict during logical + * decoding on standby */ +} xl_standby_snapshot_conflict; + +#define SizeOfStandbySnapshotConflict (offsetof(xl_standby_snapshot_conflict, isCatalogRel) + sizeof(bool)) + #endif /* STANDBYDEFS_H */ diff --git a/src/test/recovery/t/031_recovery_conflict.pl b/src/test/recovery/t/031_recovery_conflict.pl index e457f921e60..0abc4dfd091 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++; diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list index f10eb469266..32821889b89 100644 --- a/src/tools/pgindent/typedefs.list +++ b/src/tools/pgindent/typedefs.list @@ -4521,6 +4521,7 @@ xl_smgr_create xl_smgr_truncate xl_standby_lock xl_standby_locks +xl_standby_snapshot_conflict xl_tblspc_create_rec xl_tblspc_drop_rec xl_testcustomrmgrs_message -- 2.37.1 (Apple Git-137.1)