From 6832a577f1404b882d92e3ff424ac0165196fe5b Mon Sep 17 00:00:00 2001 From: shihao zhong Date: Mon, 30 Nov 2026 19:31:07 -0500 Subject: [PATCH v1 1/3] Keep LP_DEAD tuples in hash bucket splits and squeezes Dropping them logs no snapshot conflict, so a standby query can miss rows. Reported-by: Michael Malis Discussion: https://postgr.es/m/CANVD+MDikAoTBK1ZCAd+_OuXdNO-kkcBke4E54MGfgWwdYmcGQ@mail.gmail.com --- src/backend/access/hash/hashovfl.c | 5 +-- src/backend/access/hash/hashpage.c | 4 +-- src/test/recovery/t/031_recovery_conflict.pl | 34 ++++++++++++++++++++ 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/backend/access/hash/hashovfl.c b/src/backend/access/hash/hashovfl.c index dbc57ef958c..3b4e247cb77 100644 --- a/src/backend/access/hash/hashovfl.c +++ b/src/backend/access/hash/hashovfl.c @@ -923,10 +923,7 @@ readpage: IndexTuple itup; Size itemsz; - /* skip dead tuples */ - if (ItemIdIsDead(PageGetItemId(rpage, roffnum))) - continue; - + /* LP_DEAD tuples move too, a standby might still need them. */ itup = (IndexTuple) PageGetItem(rpage, PageGetItemId(rpage, roffnum)); itemsz = IndexTupleSize(itup); diff --git a/src/backend/access/hash/hashpage.c b/src/backend/access/hash/hashpage.c index a0ab2528167..f846e66363c 100644 --- a/src/backend/access/hash/hashpage.c +++ b/src/backend/access/hash/hashpage.c @@ -1135,9 +1135,7 @@ _hash_splitbucket(Relation rel, Bucket bucket; bool found = false; - /* skip dead tuples */ - if (ItemIdIsDead(PageGetItemId(opage, ooffnum))) - continue; + /* LP_DEAD tuples move too, a standby might still need them. */ /* * Before inserting a tuple, probe the hash table containing TIDs diff --git a/src/test/recovery/t/031_recovery_conflict.pl b/src/test/recovery/t/031_recovery_conflict.pl index b17ce12ba35..e457f921e60 100644 --- a/src/test/recovery/t/031_recovery_conflict.pl +++ b/src/test/recovery/t/031_recovery_conflict.pl @@ -160,6 +160,40 @@ $psql_standby->reconnect_and_clear(); check_conflict_stat("snapshot"); +## No conflict: a hash bucket split keeps index entries marked dead +$sect = "hash bucket split"; + +# fillfactor 10 makes the last INSERT below split the bucket +$node_primary->safe_psql( + $test_db, + qq[ + CREATE TABLE test_hash(a int); + INSERT INTO test_hash SELECT generate_series(1, 80); + ANALYZE test_hash; + CREATE INDEX ON test_hash USING hash (a) WITH (fillfactor = 10); + ]); +$node_primary->wait_for_replay_catchup($node_standby); + +my $hash_query = qq[ + SET enable_seqscan = off; + SET enable_bitmapscan = off; + SELECT a FROM test_hash WHERE a = 1; + ]; +$psql_standby->query_safe( + "BEGIN ISOLATION LEVEL REPEATABLE READ; $hash_query"); +$node_primary->safe_psql( + $test_db, qq[ + DELETE FROM test_hash WHERE a = 1; + $hash_query + INSERT INTO test_hash VALUES (81); + ]); +$node_primary->wait_for_replay_catchup($node_standby); + +like($psql_standby->query_safe($hash_query), + qr/^1$/m, "$sect: row still found through the index"); +$psql_standby->query_safe("COMMIT; RESET ALL;"); + + ## RECOVERY CONFLICT 3: Lock conflict $sect = "lock conflict"; $expected_conflicts++; -- 2.37.1 (Apple Git-137.1)