From e07be6afbc07c28c22026070a60ee7c23bd49251 Mon Sep 17 00:00:00 2001
From: Tomas Vondra <tomas@vondra.me>
Date: Fri, 20 Nov 2026 12:48:22 +0100
Subject: [PATCH v3 3/4] 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   | 38 +++++++++++++++++++
 .../isolation/specs/broken-hot-chain.spec     |  5 +++
 2 files changed, 43 insertions(+)

diff --git a/src/test/isolation/expected/broken-hot-chain.out b/src/test/isolation/expected/broken-hot-chain.out
index b991a14c307..39e54b8493c 100644
--- a/src/test/isolation/expected/broken-hot-chain.out
+++ b/src/test/isolation/expected/broken-hot-chain.out
@@ -75,3 +75,41 @@ count
 (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/specs/broken-hot-chain.spec b/src/test/isolation/specs/broken-hot-chain.spec
index 79ec00018de..e4a127416a8 100644
--- a/src/test/isolation/specs/broken-hot-chain.spec
+++ b/src/test/isolation/specs/broken-hot-chain.spec
@@ -35,6 +35,8 @@ setup
 # 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; }
 
 
@@ -49,6 +51,7 @@ setup
 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
@@ -61,3 +64,5 @@ 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.56.1

