From 5c858cc75ac70f939453fd0899f8aa4f310fd747 Mon Sep 17 00:00:00 2001 From: shihao zhong Date: Mon, 30 Nov 2026 21:16:10 -0500 Subject: [PATCH v1 3/3] Log a snapshot conflict for concurrent index builds They leave out rows deleted before the reference snapshot, and only wait for older snapshots on the primary. Reported-by: Michael Malis Discussion: https://postgr.es/m/CANVD+MDikAoTBK1ZCAd+_OuXdNO-kkcBke4E54MGfgWwdYmcGQ@mail.gmail.com --- doc/src/sgml/high-availability.sgml | 7 +++-- src/backend/access/heap/heapam_handler.c | 29 ++++++++++++++++---- src/backend/catalog/index.c | 3 +- src/test/recovery/t/031_recovery_conflict.pl | 22 +++++++++++++++ 4 files changed, 53 insertions(+), 8 deletions(-) diff --git a/doc/src/sgml/high-availability.sgml b/doc/src/sgml/high-availability.sgml index ab0e434737e..f0e22661829 100644 --- a/doc/src/sgml/high-availability.sgml +++ b/doc/src/sgml/high-availability.sgml @@ -1904,8 +1904,11 @@ postgres=# WAIT FOR LSN '0/306EE20'; 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. + of the index. does not + prevent this conflict for CREATE INDEX CONCURRENTLY + and REINDEX CONCURRENTLY. 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 2279a81c808..97be9bdfe15 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -1740,6 +1740,7 @@ heapam_index_validate_scan(Relation heapRelation, OffsetNumber root_offsets[MaxHeapTuplesPerPage]; bool in_index[MaxHeapTuplesPerPage]; BlockNumber previous_blkno = InvalidBlockNumber; + TransactionId conflict_xid = InvalidTransactionId; /* state variables for the merge */ ItemPointer indexcursor = NULL; @@ -1768,12 +1769,13 @@ heapam_index_validate_scan(Relation heapRelation, /* * Prepare for scan of the base relation. We need just those tuples - * satisfying the passed-in reference snapshot. We must disable syncscan - * here, because it's critical that we read from block zero forward to - * match the sorted TIDs. + * satisfying the passed-in reference snapshot, but also look at the ones + * it treats as deleted, for conflict_xid. We must disable syncscan here, + * because it's critical that we read from block zero forward to match the + * sorted TIDs. */ scan = table_beginscan_strat(heapRelation, /* relation */ - snapshot, /* snapshot */ + SnapshotAny, /* snapshot */ 0, /* number of keys */ NULL, /* scan key */ true, /* buffer access strategy OK */ @@ -1784,16 +1786,31 @@ heapam_index_validate_scan(Relation heapRelation, hscan->rs_nblocks); /* - * Scan all tuples matching the snapshot. + * Scan all tuples, and use the ones matching the snapshot. */ while ((heapTuple = heap_getnext(scan, ForwardScanDirection)) != NULL) { ItemPointer heapcursor = &heapTuple->t_self; ItemPointerData rootTuple; OffsetNumber root_offnum; + bool visible; CHECK_FOR_INTERRUPTS(); + LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_SHARE); + visible = HeapTupleSatisfiesVisibility(heapTuple, snapshot, + hscan->rs_cbuf); + /* If inserted before the snapshot, then it was deleted before it */ + if (!visible && + !XidInMVCCSnapshot(HeapTupleHeaderGetXmin(heapTuple->t_data), + snapshot)) + HeapTupleHeaderAdvanceConflictHorizon(heapTuple->t_data, + &conflict_xid); + LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK); + + if (!visible) + continue; + state->htups += 1; if ((previous_blkno == InvalidBlockNumber) || @@ -1956,6 +1973,8 @@ heapam_index_validate_scan(Relation heapRelation, table_endscan(scan); + LogStandbySnapshotConflict(heapRelation, conflict_xid); + ExecDropSingleTupleTableSlot(slot); FreeExecutorState(estate); diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c index 99413556572..092077bb977 100644 --- a/src/backend/catalog/index.c +++ b/src/backend/catalog/index.c @@ -3438,7 +3438,8 @@ IndexCheckExclusion(Relation heapRelation, * (Any tuples committed live after the snap will be inserted into the * index by their originating transaction. Any tuples committed dead before * the snap need not be indexed, because we will wait out all transactions - * that might care about them before we mark the index valid.) + * that might care about them before we mark the index valid. On a standby, + * a snapshot conflict logged by the table scan cancels them instead.) * * validate_index() works by first gathering all the TIDs currently in the * index, using a bulkdelete callback that just stores the TIDs and doesn't diff --git a/src/test/recovery/t/031_recovery_conflict.pl b/src/test/recovery/t/031_recovery_conflict.pl index 0abc4dfd091..e6cf55229f9 100644 --- a/src/test/recovery/t/031_recovery_conflict.pl +++ b/src/test/recovery/t/031_recovery_conflict.pl @@ -213,6 +213,28 @@ foreach my $cmd ("CREATE INDEX ON $table1(a)", "VACUUM FULL $table1") $psql_standby->reconnect_and_clear(); } +# CREATE INDEX CONCURRENTLY needs the conflict even with feedback +$sect = "snapshot conflict (CREATE INDEX CONCURRENTLY)"; +$expected_conflicts++; + +$node_standby->append_conf('postgresql.conf', 'hot_standby_feedback = on'); +$node_standby->reload; +$psql_standby->query_safe( + "BEGIN ISOLATION LEVEL REPEATABLE READ; SELECT 1;"); +$node_primary->poll_query_until($test_db, + "SELECT backend_xmin IS NOT NULL FROM pg_stat_replication"); +$node_primary->safe_psql($test_db, + "DELETE FROM $table1 WHERE a = (SELECT max(a) FROM $table1);"); +$node_primary->safe_psql($test_db, + "CREATE INDEX CONCURRENTLY ON $table1(a)"); +$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(); +$node_standby->append_conf('postgresql.conf', 'hot_standby_feedback = off'); +$node_standby->reload; + ## RECOVERY CONFLICT 3: Lock conflict $sect = "lock conflict"; -- 2.37.1 (Apple Git-137.1)