Hi,

Here's a slightly reworked version of the patches posted by Lukas
earlier. It mostly just splits the test/fix commits into the BRIN and
GIN part, as those need to be backpatched to different releases.

The "TEST" parts should be squashed into the following fix commits, I
kept it this way as it makes it easier to test without the fix.

The commit messages probably still need more work, but the code changes
seem correct to me (I've also confirmed it to what btree does).


regards

-- 
Tomas Vondra
From 39fba012eae4200a2f8f29fb2c5f75a77c06261c Mon Sep 17 00:00:00 2001
From: Tomas Vondra <[email protected]>
Date: Fri, 20 Nov 2026 12:32:20 +0100
Subject: [PATCH v3 4/4] Fix broken HOT chain handling in parallel GIN builds

When a non-concurrent index build encounters a RECENTLY_DEAD tuple that
was HOT-updated, heapam_index_build_range_scan() skips it, indexing only
the live tuple at the end of the HOT chain, and sets ii_BrokenHotChain.
index_build() then marks the index with indcheckxmin, so that transactions
with a snapshot older than index creation don't use the index, as it may
be missing tuple versions still visible to them.

In parallel index builds this requires each worker to report back to the
leader whether it encountered a broken HOT chain. btree correctly does
this, but BRIN and GIN parallel index builds do not. As a result,
indcheckxmin was never set, and a transaction holding an older snapshot
could use the new index and get wrong results.

Fix by tracking the brokenhotchain flag in the shared state of both AMs,
like btree does, and copying it back into the ambuild IndexInfo in the
leader after all participants are done.

Oversight when parallel index builds were added for BRIN (b4375717147)
and GIN (8492feb98f6) and backpatch to 17 and 18 respectively.

Author: Lukas Fittl <[email protected]>
Reviewed-by:
Discussion: https://postgr.es/m/CAP53Pkw3WcVbg61G29AmN=6xkuxgryowknhtyc1jmkt08i8...@mail.gmail.com
---
 src/backend/access/gin/gininsert.c | 32 +++++++++++++++++++++---------
 1 file changed, 23 insertions(+), 9 deletions(-)

diff --git a/src/backend/access/gin/gininsert.c b/src/backend/access/gin/gininsert.c
index 564b1509f1b..3a9e4f9fa9e 100644
--- a/src/backend/access/gin/gininsert.c
+++ b/src/backend/access/gin/gininsert.c
@@ -89,10 +89,14 @@ typedef struct GinBuildShared
 	 * reltuples is the total number of input heap tuples.
 	 *
 	 * indtuples is the total number of tuples that made it into the index.
+	 *
+	 * brokenhotchain indicates if any worker detected a broken HOT chain
+	 * during build.
 	 */
 	int			nparticipantsdone;
 	double		reltuples;
 	double		indtuples;
+	bool		brokenhotchain;
 
 	/*
 	 * ParallelTableScanDescData data follows. Can't directly embed here, as
@@ -188,8 +192,10 @@ static void _gin_begin_parallel(GinBuildState *buildstate, Relation heap, Relati
 								bool isconcurrent, int request);
 static void _gin_end_parallel(GinLeader *ginleader, GinBuildState *state);
 static Size _gin_parallel_estimate_shared(Relation heap, Snapshot snapshot);
-static double _gin_parallel_heapscan(GinBuildState *state);
-static double _gin_parallel_merge(GinBuildState *state);
+static double _gin_parallel_heapscan(GinBuildState *state,
+									 bool *brokenhotchain);
+static double _gin_parallel_merge(GinBuildState *state,
+								  IndexInfo *indexInfo);
 static void _gin_leader_participate_as_worker(GinBuildState *buildstate,
 											  Relation heap, Relation index);
 static void _gin_parallel_scan_and_build(GinBuildState *state,
@@ -747,7 +753,7 @@ ginbuild(Relation heap, Relation index, IndexInfo *indexInfo)
 									  TUPLESORT_NONE);
 
 		/* scan the relation in parallel and merge per-worker results */
-		reltuples = _gin_parallel_merge(state);
+		reltuples = _gin_parallel_merge(state, indexInfo);
 
 		_gin_end_parallel(state->bs_leader, state);
 	}
@@ -1039,6 +1045,7 @@ _gin_begin_parallel(GinBuildState *buildstate, Relation heap, Relation index,
 	ginshared->nparticipantsdone = 0;
 	ginshared->reltuples = 0.0;
 	ginshared->indtuples = 0.0;
+	ginshared->brokenhotchain = false;
 
 	table_parallelscan_initialize(heap,
 								  ParallelTableScanFromGinBuildShared(ginshared),
@@ -1141,10 +1148,11 @@ _gin_end_parallel(GinLeader *ginleader, GinBuildState *state)
  * already be underway within worker processes (when leader participates
  * as a worker, we should end up here just as workers are finishing).
  *
- * Returns the total number of heap tuples scanned.
+ * Returns the total number of heap tuples scanned.  Also sets *brokenhotchain
+ * to whether any participant detected a broken HOT chain.
  */
 static double
-_gin_parallel_heapscan(GinBuildState *state)
+_gin_parallel_heapscan(GinBuildState *state, bool *brokenhotchain)
 {
 	GinBuildShared *ginshared = state->bs_leader->ginshared;
 	int			nparticipanttuplesorts;
@@ -1158,6 +1166,7 @@ _gin_parallel_heapscan(GinBuildState *state)
 			/* copy the data into leader state */
 			state->bs_reltuples = ginshared->reltuples;
 			state->bs_numtuples = ginshared->indtuples;
+			*brokenhotchain = ginshared->brokenhotchain;
 
 			SpinLockRelease(&ginshared->mutex);
 			break;
@@ -1642,10 +1651,12 @@ GinBufferCanAddKey(GinBuffer *buffer, GinTuple *tup)
  * summaries for the same page range, and also fill-in empty summaries for
  * ranges without any tuples.
  *
- * Returns the total number of heap tuples scanned.
+ * Returns the total number of heap tuples scanned.  Also sets
+ * indexInfo->ii_BrokenHotChain if any participant detected a broken HOT chain,
+ * so that index.c can mark the index as unsafe for older snapshots.
  */
 static double
-_gin_parallel_merge(GinBuildState *state)
+_gin_parallel_merge(GinBuildState *state, IndexInfo *indexInfo)
 {
 	GinTuple   *tup;
 	Size		tuplen;
@@ -1656,7 +1667,7 @@ _gin_parallel_merge(GinBuildState *state)
 	double		numtuples = 0;
 
 	/* wait for workers to scan table and produce partial results */
-	reltuples = _gin_parallel_heapscan(state);
+	reltuples = _gin_parallel_heapscan(state, &indexInfo->ii_BrokenHotChain);
 
 	/* Execute the sort */
 	pgstat_progress_update_param(PROGRESS_CREATEIDX_SUBPHASE,
@@ -2084,12 +2095,15 @@ _gin_parallel_scan_and_build(GinBuildState *state,
 	state->bs_reltuples += reltuples;
 
 	/*
-	 * Done.  Record ambuild statistics.
+	 * Done.  Record ambuild statistics, and whether we encountered a broken
+	 * HOT chain.
 	 */
 	SpinLockAcquire(&ginshared->mutex);
 	ginshared->nparticipantsdone++;
 	ginshared->reltuples += state->bs_reltuples;
 	ginshared->indtuples += state->bs_numtuples;
+	if (indexInfo->ii_BrokenHotChain)
+		ginshared->brokenhotchain = true;
 	SpinLockRelease(&ginshared->mutex);
 
 	/* Notify leader */
-- 
2.56.1

From e07be6afbc07c28c22026070a60ee7c23bd49251 Mon Sep 17 00:00:00 2001
From: Tomas Vondra <[email protected]>
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

From 20f9a8f42222fee3f16e9fed37535225bd4dbc5a Mon Sep 17 00:00:00 2001
From: Lukas Fittl <[email protected]>
Date: Sat, 31 Oct 2026 11:53:59 -0700
Subject: [PATCH v3 2/4] Fix broken HOT chain handling in parallel BRIN builds

When a non-concurrent index build encounters a RECENTLY_DEAD tuple that
was HOT-updated, heapam_index_build_range_scan() skips it, indexing only
the live tuple at the end of the HOT chain, and sets ii_BrokenHotChain.
index_build() then marks the index with indcheckxmin, so that transactions
with a snapshot older than index creation don't use the index, as it may
be missing tuple versions still visible to them.

In parallel index builds this requires each worker to report back to the
leader whether it encountered a broken HOT chain. btree correctly does
this, but BRIN and GIN parallel index builds do not. As a result,
indcheckxmin was never set, and a transaction holding an older snapshot
could use the new index and get wrong results.

Fix by tracking the brokenhotchain flag in the shared state of both AMs,
like btree does, and copying it back into the ambuild IndexInfo in the
leader after all participants are done.

Oversight when parallel index builds were added for BRIN (b4375717147)
and GIN (8492feb98f6) and backpatch to 17 and 18 respectively.

Author: Lukas Fittl <[email protected]>
Reviewed-by:
Discussion: https://postgr.es/m/CAP53Pkw3WcVbg61G29AmN=6xkuxgryowknhtyc1jmkt08i8...@mail.gmail.com
---
 src/backend/access/brin/brin.c | 33 ++++++++++++++++++++++++---------
 1 file changed, 24 insertions(+), 9 deletions(-)

diff --git a/src/backend/access/brin/brin.c b/src/backend/access/brin/brin.c
index 5059da8dce7..fedc0387ca0 100644
--- a/src/backend/access/brin/brin.c
+++ b/src/backend/access/brin/brin.c
@@ -99,10 +99,14 @@ typedef struct BrinShared
 	 * reltuples is the total number of input heap tuples.
 	 *
 	 * indtuples is the total number of tuples that made it into the index.
+	 *
+	 * brokenhotchain indicates if any worker detected a broken HOT chain
+	 * during build.
 	 */
 	int			nparticipantsdone;
 	double		reltuples;
 	double		indtuples;
+	bool		brokenhotchain;
 
 	/*
 	 * ParallelTableScanDescData data follows. Can't directly embed here, as
@@ -236,8 +240,10 @@ static void _brin_begin_parallel(BrinBuildState *buildstate, Relation heap, Rela
 								 bool isconcurrent, int request);
 static void _brin_end_parallel(BrinLeader *brinleader, BrinBuildState *state);
 static Size _brin_parallel_estimate_shared(Relation heap, Snapshot snapshot);
-static double _brin_parallel_heapscan(BrinBuildState *state);
-static double _brin_parallel_merge(BrinBuildState *state);
+static double _brin_parallel_heapscan(BrinBuildState *state,
+									  bool *brokenhotchain);
+static double _brin_parallel_merge(BrinBuildState *state,
+								   IndexInfo *indexInfo);
 static void _brin_leader_participate_as_worker(BrinBuildState *buildstate,
 											   Relation heap, Relation index);
 static void _brin_parallel_scan_and_build(BrinBuildState *state,
@@ -1223,7 +1229,7 @@ brinbuild(Relation heap, Relation index, IndexInfo *indexInfo)
 									   TUPLESORT_NONE);
 
 		/* scan the relation and merge per-worker results */
-		reltuples = _brin_parallel_merge(state);
+		reltuples = _brin_parallel_merge(state, indexInfo);
 
 		_brin_end_parallel(state->bs_leader, state);
 	}
@@ -2483,6 +2489,7 @@ _brin_begin_parallel(BrinBuildState *buildstate, Relation heap, Relation index,
 	brinshared->nparticipantsdone = 0;
 	brinshared->reltuples = 0.0;
 	brinshared->indtuples = 0.0;
+	brinshared->brokenhotchain = false;
 
 	table_parallelscan_initialize(heap,
 								  ParallelTableScanFromBrinShared(brinshared),
@@ -2589,10 +2596,11 @@ _brin_end_parallel(BrinLeader *brinleader, BrinBuildState *state)
  * already be underway within worker processes (when leader participates
  * as a worker, we should end up here just as workers are finishing).
  *
- * Returns the total number of heap tuples scanned.
+ * Returns the total number of heap tuples scanned.  Also sets *brokenhotchain
+ * to whether any participant detected a broken HOT chain.
  */
 static double
-_brin_parallel_heapscan(BrinBuildState *state)
+_brin_parallel_heapscan(BrinBuildState *state, bool *brokenhotchain)
 {
 	BrinShared *brinshared = state->bs_leader->brinshared;
 	int			nparticipanttuplesorts;
@@ -2606,6 +2614,7 @@ _brin_parallel_heapscan(BrinBuildState *state)
 			/* copy the data into leader state */
 			state->bs_reltuples = brinshared->reltuples;
 			state->bs_numtuples = brinshared->indtuples;
+			*brokenhotchain = brinshared->brokenhotchain;
 
 			SpinLockRelease(&brinshared->mutex);
 			break;
@@ -2630,10 +2639,12 @@ _brin_parallel_heapscan(BrinBuildState *state)
  * summaries for the same page range, and also fill-in empty summaries for
  * ranges without any tuples.
  *
- * Returns the total number of heap tuples scanned.
+ * Returns the total number of heap tuples scanned.  Also sets
+ * indexInfo->ii_BrokenHotChain if any participant detected a broken HOT chain,
+ * so that index.c can mark the index as unsafe for older snapshots.
  */
 static double
-_brin_parallel_merge(BrinBuildState *state)
+_brin_parallel_merge(BrinBuildState *state, IndexInfo *indexInfo)
 {
 	BrinTuple  *btup;
 	BrinMemTuple *memtuple = NULL;
@@ -2644,7 +2655,8 @@ _brin_parallel_merge(BrinBuildState *state)
 	double		reltuples;
 
 	/* wait for workers to scan table and produce partial results */
-	reltuples = _brin_parallel_heapscan(state);
+	reltuples = _brin_parallel_heapscan(state,
+										&indexInfo->ii_BrokenHotChain);
 
 	/* do the actual sort in the leader */
 	tuplesort_performsort(state->bs_sortstate);
@@ -2859,12 +2871,15 @@ _brin_parallel_scan_and_build(BrinBuildState *state,
 	state->bs_reltuples += reltuples;
 
 	/*
-	 * Done.  Record ambuild statistics.
+	 * Done.  Record ambuild statistics, and whether we encountered a broken
+	 * HOT chain.
 	 */
 	SpinLockAcquire(&brinshared->mutex);
 	brinshared->nparticipantsdone++;
 	brinshared->reltuples += state->bs_reltuples;
 	brinshared->indtuples += state->bs_numtuples;
+	if (indexInfo->ii_BrokenHotChain)
+		brinshared->brokenhotchain = true;
 	SpinLockRelease(&brinshared->mutex);
 
 	/* Notify leader */
-- 
2.56.1

From 2f991d820491fd38cd0b0e912eae862a0ea4ae30 Mon Sep 17 00:00:00 2001
From: Peter Geoghegan <[email protected]>
Date: Thu, 19 Nov 2026 19:20:37 -0500
Subject: [PATCH v3 1/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   | 77 +++++++++++++++++++
 src/test/isolation/isolation_schedule         |  1 +
 .../isolation/specs/broken-hot-chain.spec     | 63 +++++++++++++++
 3 files changed, 141 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..b991a14c307
--- /dev/null
+++ b/src/test/isolation/expected/broken-hot-chain.out
@@ -0,0 +1,77 @@
+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;
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..79ec00018de
--- /dev/null
+++ b/src/test/isolation/specs/broken-hot-chain.spec
@@ -0,0 +1,63 @@
+# 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_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); }
+
+# 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
-- 
2.56.1

Reply via email to