From 9f0a0dcf4a6b2e1e1c3ca35c459c37cdc81dcc39 Mon Sep 17 00:00:00 2001
From: Tomas Vondra <tomas@vondra.me>
Date: Wed, 25 Nov 2026 11:30:00 +0100
Subject: [PATCH v1 3/3] Include still-visible HOT versions in BRIN summaries

A HOT update can change a BRIN-indexed value.  When REINDEX rebuilt the
index or VACUUM summarized a range, the heap scan skipped HOT-updated
versions that could still be visible to older snapshots.  The resulting
summary could cause a BRIN scan to skip a range containing matching rows.

Pass those versions to the build callback for summarizing indexes, even
when the updating transaction is the one building the index.  All members
of a HOT chain are on the same heap block, so their values can contribute
to the same summary without setting indcheckxmin.  Keep the existing HOT
handling for indexes that store per-tuple entries.

Add the BRIN reproducer to the isolation schedule, covering REINDEX both
before and after the updating transaction commits, and resummarization
by VACUUM.

The issue dates to 19d8e2308bc, which allowed HOT updates to change
BRIN-indexed columns.

Backpatch-through: 16
---
 src/backend/access/heap/README.HOT            | 15 ++++--
 src/backend/access/heap/heapam_handler.c      | 46 +++++++++----------
 src/include/access/tableam.h                  |  8 ++--
 .../isolation/expected/broken-hot-chain.out   |  2 +-
 src/test/isolation/isolation_schedule         |  1 +
 .../isolation/specs/broken-hot-chain.spec     | 16 +++----
 6 files changed, 44 insertions(+), 44 deletions(-)

diff --git a/src/backend/access/heap/README.HOT b/src/backend/access/heap/README.HOT
index 74e407f375a..b485bce57df 100644
--- a/src/backend/access/heap/README.HOT
+++ b/src/backend/access/heap/README.HOT
@@ -301,10 +301,11 @@ This area probably needs further work ...
 CREATE INDEX
 ------------
 
-CREATE INDEX presents a problem for HOT updates.  While the existing HOT
-chains all have the same index values for existing indexes, the columns
-in the new index might change within a pre-existing HOT chain, creating
-a "broken" chain that can't be indexed properly.
+CREATE INDEX presents a problem for indexes with per-tuple entries.
+While the members of a HOT chain have the same index values for existing
+indexes of this kind, the columns in a new index might change within a
+pre-existing HOT chain, creating a "broken" chain that can't be indexed
+properly.
 
 To address this issue, regular (non-concurrent) CREATE INDEX makes the
 new index usable only by new transactions and transactions that don't
@@ -361,6 +362,12 @@ The only exception at this writing is CLUSTER, which is okay because it
 does not require perfect ordering of the indexscan readout (and especially
 so because CLUSTER tends to write recently-dead tuples out of order anyway).
 
+A summarizing index does not need indcheckxmin for HOT chains: it can
+include more than one value for the same heap block.  HOT updates can
+change a summarized key even after the index exists.  A non-concurrent
+build, rebuild, or range summarization must therefore consider every HOT
+chain member that could still be visible, not just the end of the chain.
+
 
 CREATE INDEX CONCURRENTLY
 -------------------------
diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c
index 1437274b0c6..34c2e8d6aeb 100644
--- a/src/backend/access/heap/heapam_handler.c
+++ b/src/backend/access/heap/heapam_handler.c
@@ -1311,14 +1311,11 @@ heapam_index_build_range_scan(Relation heapRelation,
 		}
 
 		/*
-		 * When dealing with a HOT-chain of updated tuples, we want to index
-		 * the values of the live tuple (if any), but index it under the TID
-		 * of the chain's root tuple.  This approach is necessary to preserve
-		 * the HOT-chain structure in the heap. So we need to be able to find
-		 * the root item offset for every tuple that's in a HOT-chain.  When
-		 * first reaching a new page of the relation, call
-		 * heap_get_root_tuples() to build a map of root item offsets on the
-		 * page.
+		 * Pass the root TID to the callback for members of a HOT chain.
+		 * Indexes with per-tuple entries must point to the root, and for
+		 * summarizing indexes only the block number matters.  When first
+		 * reaching a new page, call heap_get_root_tuples() to build a map of
+		 * root item offsets on the page.
 		 *
 		 * It might look unsafe to use this information across buffer
 		 * lock/unlock.  However, we hold ShareLock on the table so no
@@ -1398,15 +1395,17 @@ heapam_index_build_range_scan(Relation heapRelation,
 					 * transactions could try to use the index after we finish
 					 * building it, and may need to see such tuples.)
 					 *
-					 * However, if it was HOT-updated then we must only index
-					 * the live tuple at the end of the HOT-chain.  Since this
-					 * breaks semantics for pre-existing snapshots, mark the
-					 * index as unusable for them.
+					 * A per-tuple index must skip HOT-updated versions to
+					 * avoid indexing different keys under the same root TID.
+					 * Mark it unsafe for old snapshots.  A summarizing index
+					 * must include these versions: a HOT update can change a
+					 * summarized key, even after the index was created.
 					 *
 					 * We don't count recently-dead tuples in reltuples, even
 					 * if we index them; see heapam_scan_analyze_next_tuple().
 					 */
-					if (HeapTupleIsHotUpdated(heapTuple))
+					if (!indexInfo->ii_Summarizing &&
+						HeapTupleIsHotUpdated(heapTuple))
 					{
 						indexIt = false;
 						/* mark the index as unsafe for old snapshots */
@@ -1511,16 +1510,13 @@ heapam_index_build_range_scan(Relation heapRelation,
 						 * uniqueness violation.  In that case we wait for the
 						 * deleting transaction to finish and check again.
 						 *
-						 * Also, if it's a HOT-updated tuple, we should not
-						 * index it but rather the live tuple at the end of
-						 * the HOT-chain.  However, the deleting transaction
-						 * could abort, possibly leaving this tuple as live
-						 * after all, in which case it has to be indexed. The
-						 * only way to know what to do is to wait for the
-						 * deleting transaction to finish and check again.
+						 * A per-tuple index must skip a HOT-updated tuple if
+						 * the update commits, but index it if the update
+						 * aborts.  Wait and recheck in this case, too.
 						 */
 						if (checking_uniqueness ||
-							HeapTupleIsHotUpdated(heapTuple))
+							(!indexInfo->ii_Summarizing &&
+							 HeapTupleIsHotUpdated(heapTuple)))
 						{
 							/*
 							 * Must drop the lock on the buffer before we wait
@@ -1548,7 +1544,8 @@ heapam_index_build_range_scan(Relation heapRelation,
 						 */
 						reltuples += 1;
 					}
-					else if (HeapTupleIsHotUpdated(heapTuple))
+					else if (!indexInfo->ii_Summarizing &&
+							 HeapTupleIsHotUpdated(heapTuple))
 					{
 						/*
 						 * It's a HOT-updated tuple deleted by our own xact.
@@ -1563,9 +1560,8 @@ heapam_index_build_range_scan(Relation heapRelation,
 					else
 					{
 						/*
-						 * It's a regular tuple deleted by our own xact. Index
-						 * it, but don't check for uniqueness nor count in
-						 * reltuples, the same as a RECENTLY_DEAD tuple.
+						 * Index it, but don't check for uniqueness or count
+						 * it in reltuples, just as for RECENTLY_DEAD.
 						 */
 						indexIt = true;
 					}
diff --git a/src/include/access/tableam.h b/src/include/access/tableam.h
index 9d2fc7f9acb..b5c626da67e 100644
--- a/src/include/access/tableam.h
+++ b/src/include/access/tableam.h
@@ -1857,11 +1857,9 @@ table_scan_analyze_next_tuple(TableScanDesc scan,
  * starting the scan, and PROGRESS_SCAN_BLOCKS_DONE is updated as we go along.
  *
  * A side effect is to set indexInfo->ii_BrokenHotChain to true if we detect
- * any potentially broken HOT chains.  Currently, we set this if there are any
- * RECENTLY_DEAD or DELETE_IN_PROGRESS entries in a HOT chain, without trying
- * very hard to detect whether they're really incompatible with the chain tip.
- * This only really makes sense for heap AM, it might need to be generalized
- * for other AMs later.
+ * any potentially broken HOT chains.  The heap AM sets this if it skips a
+ * HOT-updated version that is RECENTLY_DEAD or deleted by our own transaction.
+ * Summarizing indexes include those versions instead.
  */
 static inline double
 table_index_build_scan(Relation table_rel,
diff --git a/src/test/isolation/expected/broken-hot-chain.out b/src/test/isolation/expected/broken-hot-chain.out
index 39e54b8493c..07820084bda 100644
--- a/src/test/isolation/expected/broken-hot-chain.out
+++ b/src/test/isolation/expected/broken-hot-chain.out
@@ -59,7 +59,7 @@ step s2_indcheckxmin:
 
 indcheckxmin
 ------------
-t           
+f           
 (1 row)
 
 step s1_a_old: SELECT count(*) FROM broken_hot WHERE a = 1;
diff --git a/src/test/isolation/isolation_schedule b/src/test/isolation/isolation_schedule
index d26b041b4f0..e5029dc0adb 100644
--- a/src/test/isolation/isolation_schedule
+++ b/src/test/isolation/isolation_schedule
@@ -115,6 +115,7 @@ test: vacuum-skip-locked
 test: stats
 test: horizons
 test: broken-hot-chain
+test: brin-hot-rebuild
 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
index b877f2f2d59..468853ac9a9 100644
--- a/src/test/isolation/specs/broken-hot-chain.spec
+++ b/src/test/isolation/specs/broken-hot-chain.spec
@@ -1,12 +1,10 @@
-# Test that an index built over broken HOT chains is not used by a snapshot
-# that can still see the old row versions.
+# Test index builds over HOT chains with older row versions still visible.
 #
-# 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
-# requests one parallel worker per build.  In a parallel build, each
-# participant must tell the leader whether it skipped such a tuple.
+# An index with per-tuple entries uses the root TID and skips HOT-updated
+# versions.  If an old snapshot might still see them, the build must set
+# pg_index.indcheckxmin to keep it from using the index (see README.HOT).
+# A summarizing index can include both old and new values.  s2 requests a
+# parallel worker to exercise reporting of skipped versions to the leader.
 
 setup
 {
@@ -52,7 +50,7 @@ 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
+# Only the btree and GIN builds must set indcheckxmin.
 step s2_indcheckxmin
 {
     SELECT indcheckxmin FROM pg_index
-- 
2.56.1

