From 9392f34f78c4e3f58ca6dace9c0326a798a81c28 Mon Sep 17 00:00:00 2001 From: Peter Geoghegan Date: Thu, 19 Nov 2026 19:20:37 -0500 Subject: [PATCH v2 1/2] Add isolation test for index builds over broken HOT chains. An index build that skips a HOT chain member which some snapshot can still see must set pg_index.indcheckxmin, so that such a snapshot does not use the index. The heap scan reports this in ii_BrokenHotChain. A parallel build scans the heap with a separate IndexInfo in each participant, so the participants must pass the flag on to the leader. nbtsort.c does that. The parallel BRIN build (commit b43757171) and the parallel GIN build (commit 8492feb98) do not, so index_build() never sets indcheckxmin after either of them. The new test fails on master for BRIN and for GIN, and there is no fix here. A REPEATABLE READ session counts the rows of a table, a second session HOT-updates every row and builds an index with a parallel build, and the first session counts again. After a BRIN or a GIN build indcheckxmin is false and the first session uses the index: its snapshot's rows are no longer found, and with GIN a search for the new value returns them instead. The btree permutation passes. --- .../isolation/expected/broken-hot-chain.out | 115 ++++++++++++++++++ src/test/isolation/isolation_schedule | 1 + .../isolation/specs/broken-hot-chain.spec | 68 +++++++++++ 3 files changed, 184 insertions(+) create mode 100644 src/test/isolation/expected/broken-hot-chain.out create mode 100644 src/test/isolation/specs/broken-hot-chain.spec diff --git a/src/test/isolation/expected/broken-hot-chain.out b/src/test/isolation/expected/broken-hot-chain.out new file mode 100644 index 00000000000..39e54b8493c --- /dev/null +++ b/src/test/isolation/expected/broken-hot-chain.out @@ -0,0 +1,115 @@ +Parsed test spec with 2 sessions + +starting permutation: s1_a_old s1_a_new s2_update s2_btree s2_indcheckxmin s1_a_old s1_a_new s1_commit +step s1_a_old: SELECT count(*) FROM broken_hot WHERE a = 1; +count +----- + 1000 +(1 row) + +step s1_a_new: SELECT count(*) FROM broken_hot WHERE a = 2; +count +----- + 0 +(1 row) + +step s2_update: UPDATE broken_hot SET a = 2, b = ARRAY[2]; +step s2_btree: CREATE INDEX broken_hot_idx ON broken_hot USING btree (a); +step s2_indcheckxmin: + SELECT indcheckxmin FROM pg_index + WHERE indexrelid = 'broken_hot_idx'::regclass; + +indcheckxmin +------------ +t +(1 row) + +step s1_a_old: SELECT count(*) FROM broken_hot WHERE a = 1; +count +----- + 1000 +(1 row) + +step s1_a_new: SELECT count(*) FROM broken_hot WHERE a = 2; +count +----- + 0 +(1 row) + +step s1_commit: COMMIT; + +starting permutation: s1_a_old s1_a_new s2_update s2_brin s2_indcheckxmin s1_a_old s1_a_new s1_commit +step s1_a_old: SELECT count(*) FROM broken_hot WHERE a = 1; +count +----- + 1000 +(1 row) + +step s1_a_new: SELECT count(*) FROM broken_hot WHERE a = 2; +count +----- + 0 +(1 row) + +step s2_update: UPDATE broken_hot SET a = 2, b = ARRAY[2]; +step s2_brin: CREATE INDEX broken_hot_idx ON broken_hot USING brin (a); +step s2_indcheckxmin: + SELECT indcheckxmin FROM pg_index + WHERE indexrelid = 'broken_hot_idx'::regclass; + +indcheckxmin +------------ +t +(1 row) + +step s1_a_old: SELECT count(*) FROM broken_hot WHERE a = 1; +count +----- + 1000 +(1 row) + +step s1_a_new: SELECT count(*) FROM broken_hot WHERE a = 2; +count +----- + 0 +(1 row) + +step s1_commit: COMMIT; + +starting permutation: s1_b_old s1_b_new s2_update s2_gin s2_indcheckxmin s1_b_old s1_b_new s1_commit +step s1_b_old: SELECT count(*) FROM broken_hot WHERE b @> ARRAY[1]; +count +----- + 1000 +(1 row) + +step s1_b_new: SELECT count(*) FROM broken_hot WHERE b @> ARRAY[2]; +count +----- + 0 +(1 row) + +step s2_update: UPDATE broken_hot SET a = 2, b = ARRAY[2]; +step s2_gin: CREATE INDEX broken_hot_idx ON broken_hot USING gin (b); +step s2_indcheckxmin: + SELECT indcheckxmin FROM pg_index + WHERE indexrelid = 'broken_hot_idx'::regclass; + +indcheckxmin +------------ +t +(1 row) + +step s1_b_old: SELECT count(*) FROM broken_hot WHERE b @> ARRAY[1]; +count +----- + 1000 +(1 row) + +step s1_b_new: SELECT count(*) FROM broken_hot WHERE b @> ARRAY[2]; +count +----- + 0 +(1 row) + +step s1_commit: COMMIT; diff --git a/src/test/isolation/isolation_schedule b/src/test/isolation/isolation_schedule index f448e4d6e13..8220bd36d57 100644 --- a/src/test/isolation/isolation_schedule +++ b/src/test/isolation/isolation_schedule @@ -113,6 +113,7 @@ test: vacuum-conflict test: vacuum-skip-locked test: stats test: horizons +test: broken-hot-chain test: predicate-hash test: predicate-gist test: predicate-gin diff --git a/src/test/isolation/specs/broken-hot-chain.spec b/src/test/isolation/specs/broken-hot-chain.spec new file mode 100644 index 00000000000..e4a127416a8 --- /dev/null +++ b/src/test/isolation/specs/broken-hot-chain.spec @@ -0,0 +1,68 @@ +# Test that an index built over broken HOT chains is not used by a snapshot +# that can still see the old row versions. +# +# CREATE INDEX indexes only the live member of a HOT chain, under the TID of +# the chain's root. When the heap scan of the build skips an older member +# that some snapshot can still see, the build must set pg_index.indcheckxmin, +# which keeps such a snapshot from using the index (see README.HOT). s2's +# builds are parallel builds, so each participant in the heap scan must pass +# what it skipped on to the leader. + +setup +{ + -- No index and a low fillfactor: every UPDATE is a HOT update + CREATE TABLE broken_hot (a int4, b int4[]) + WITH (autovacuum_enabled = off, fillfactor = 50); + INSERT INTO broken_hot SELECT 1, ARRAY[1] FROM generate_series(1, 1000); +} + +teardown +{ + DROP TABLE broken_hot; +} + + +session s1 + +# s1 uses an index whenever it is allowed to +setup +{ + SET enable_seqscan = off; + BEGIN ISOLATION LEVEL REPEATABLE READ; +} + +# s1's snapshot sees 1000 rows with the old values and none with the new +# values, before and after s2 builds its index +step s1_a_old { SELECT count(*) FROM broken_hot WHERE a = 1; } +step s1_a_new { SELECT count(*) FROM broken_hot WHERE a = 2; } +step s1_b_old { SELECT count(*) FROM broken_hot WHERE b @> ARRAY[1]; } +step s1_b_new { SELECT count(*) FROM broken_hot WHERE b @> ARRAY[2]; } +step s1_commit { COMMIT; } + + +session s2 + +setup +{ + SET min_parallel_table_scan_size = 0; + SET max_parallel_maintenance_workers = 4; +} + +step s2_update { UPDATE broken_hot SET a = 2, b = ARRAY[2]; } +step s2_btree { CREATE INDEX broken_hot_idx ON broken_hot USING btree (a); } +step s2_brin { CREATE INDEX broken_hot_idx ON broken_hot USING brin (a); } +step s2_gin { CREATE INDEX broken_hot_idx ON broken_hot USING gin (b); } + +# The build must set indcheckxmin +step s2_indcheckxmin +{ + SELECT indcheckxmin FROM pg_index + WHERE indexrelid = 'broken_hot_idx'::regclass; +} + +permutation s1_a_old s1_a_new s2_update s2_btree s2_indcheckxmin + s1_a_old s1_a_new s1_commit +permutation s1_a_old s1_a_new s2_update s2_brin s2_indcheckxmin + s1_a_old s1_a_new s1_commit +permutation s1_b_old s1_b_new s2_update s2_gin s2_indcheckxmin + s1_b_old s1_b_new s1_commit -- 2.47.1