From 39fba012eae4200a2f8f29fb2c5f75a77c06261c Mon Sep 17 00:00:00 2001
From: Tomas Vondra <tomas@vondra.me>
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 <lukas@fittl.com>
Reviewed-by:
Discussion: https://postgr.es/m/CAP53Pkw3WcVbg61G29AmN=6XkuxGryoWKnhTyC1JmkT08i8qVA@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

