Hi,
While fixing some HOT issues for BRIN (commit bae27968b76d), I've
noticed a separate issue with HOT and BRIN indexes, unrelated to the
indcheckxmin protection. I'm CCing Alvaro and Josef, who were involved
in 19d8e2308bc that relaxed the HOT handling for BRIN in PG16.
The problem is about "old" snapshots and index rebuilds (either from
REINDEX, or even just vacuum summarizing a page range).
Consider this example:
1) let's start with a table containing row (1), and a BRIN index
2) a REPEATABLE READ transaction starts, and gets a snapshot without
accessing the index yet (so no lock on it)
3) a different transaction updates 1 => 2
4) writer runs REINDEX, or re-summarizes the range in some way
5) the reader (with the REPEATABLE READ) snapshot reads the table
through the index, but does not find the "1" row
The reason is pretty simple - as coded, heapam_index_build_range_scan
only summarizes values from the root TID (in HOT chains). The other
values from that chain (on the same page) are skipped, but the index is
then used even for the old snapshots.
The bae27968b76d addresses the same issue for CREATE INDEX, by adding
the indcheckxmin handling. But that can't help with REINDEX or just
re-summarization of a range, because those don't do indcheckxmin (simply
because it's irrelevant for non-summarizing indexes, as those disable
HOT once there's an index on the updated column).
I think we need to fix heapam_index_build_range_scan(), so that it
indexes all values on the page, not just the root TID. Patch attached,
needs to be backpatched to 16 (where 19d8e2308bc landed). A reindex will
be necessary to fix existing indexes.
I wonder if we could do the same thing for CREATE INDEX, making
bae27968b76d mostly unnecessary. But I haven't tried that.
regards
--
Tomas Vondra
From 4c565ffda08a26ad1b8e41e3c53c9ea71de79fb3 Mon Sep 17 00:00:00 2001
From: Tomas Vondra <[email protected]>
Date: Wed, 25 Nov 2026 11:30:00 +0100
Subject: [PATCH v1 1/3] Add an isolation reproducer for BRIN misses after HOT
updates
Hold a repeatable-read snapshot while another session HOT-updates a BRIN
key and either rebuilds the index or has VACUUM resummarize the range.
On an affected server, a BRIN scan misses the row that a sequential scan
finds in the same snapshot.
Leave the spec out of the isolation schedule rather than recording the
incorrect result as expected.
---
.../isolation/expected/brin-hot-rebuild.out | 178 ++++++++++++++++++
src/test/isolation/isolation_schedule | 1 +
.../isolation/specs/brin-hot-rebuild.spec | 48 +++++
3 files changed, 227 insertions(+)
create mode 100644 src/test/isolation/expected/brin-hot-rebuild.out
create mode 100644 src/test/isolation/specs/brin-hot-rebuild.spec
diff --git a/src/test/isolation/expected/brin-hot-rebuild.out b/src/test/isolation/expected/brin-hot-rebuild.out
new file mode 100644
index 00000000000..6db84054a70
--- /dev/null
+++ b/src/test/isolation/expected/brin-hot-rebuild.out
@@ -0,0 +1,178 @@
+Parsed test spec with 2 sessions
+
+starting permutation: pin_snapshot change_key commit_update rebuild probe use_heap probe finish
+step pin_snapshot: SELECT pg_current_snapshot() IS NOT NULL AS snapshot_taken;
+snapshot_taken
+--------------
+t
+(1 row)
+
+step change_key:
+ BEGIN;
+ UPDATE brin_hot_rebuild SET k = 2;
+ SELECT pg_stat_get_xact_tuples_hot_updated('brin_hot_rebuild'::regclass)
+ AS hot_updates;
+
+hot_updates
+-----------
+ 1
+(1 row)
+
+step commit_update: COMMIT;
+step rebuild: REINDEX INDEX brin_hot_rebuild_idx;
+step probe:
+ EXPLAIN (COSTS OFF) SELECT count(*) FROM brin_hot_rebuild WHERE k = 1;
+ SELECT count(*) AS visible_ones FROM brin_hot_rebuild WHERE k = 1;
+
+QUERY PLAN
+-----------------------------------------------------
+Aggregate
+ -> Bitmap Heap Scan on brin_hot_rebuild
+ Recheck Cond: (k = 1)
+ -> Bitmap Index Scan on brin_hot_rebuild_idx
+ Index Cond: (k = 1)
+(5 rows)
+
+visible_ones
+------------
+ 1
+(1 row)
+
+step use_heap: SET LOCAL enable_seqscan = on; SET LOCAL enable_bitmapscan = off;
+step probe:
+ EXPLAIN (COSTS OFF) SELECT count(*) FROM brin_hot_rebuild WHERE k = 1;
+ SELECT count(*) AS visible_ones FROM brin_hot_rebuild WHERE k = 1;
+
+QUERY PLAN
+----------------------------------
+Aggregate
+ -> Seq Scan on brin_hot_rebuild
+ Filter: (k = 1)
+(3 rows)
+
+visible_ones
+------------
+ 1
+(1 row)
+
+step finish: COMMIT;
+
+starting permutation: pin_snapshot change_key rebuild commit_update probe use_heap probe finish
+step pin_snapshot: SELECT pg_current_snapshot() IS NOT NULL AS snapshot_taken;
+snapshot_taken
+--------------
+t
+(1 row)
+
+step change_key:
+ BEGIN;
+ UPDATE brin_hot_rebuild SET k = 2;
+ SELECT pg_stat_get_xact_tuples_hot_updated('brin_hot_rebuild'::regclass)
+ AS hot_updates;
+
+hot_updates
+-----------
+ 1
+(1 row)
+
+step rebuild: REINDEX INDEX brin_hot_rebuild_idx;
+step commit_update: COMMIT;
+step probe:
+ EXPLAIN (COSTS OFF) SELECT count(*) FROM brin_hot_rebuild WHERE k = 1;
+ SELECT count(*) AS visible_ones FROM brin_hot_rebuild WHERE k = 1;
+
+QUERY PLAN
+-----------------------------------------------------
+Aggregate
+ -> Bitmap Heap Scan on brin_hot_rebuild
+ Recheck Cond: (k = 1)
+ -> Bitmap Index Scan on brin_hot_rebuild_idx
+ Index Cond: (k = 1)
+(5 rows)
+
+visible_ones
+------------
+ 1
+(1 row)
+
+step use_heap: SET LOCAL enable_seqscan = on; SET LOCAL enable_bitmapscan = off;
+step probe:
+ EXPLAIN (COSTS OFF) SELECT count(*) FROM brin_hot_rebuild WHERE k = 1;
+ SELECT count(*) AS visible_ones FROM brin_hot_rebuild WHERE k = 1;
+
+QUERY PLAN
+----------------------------------
+Aggregate
+ -> Seq Scan on brin_hot_rebuild
+ Filter: (k = 1)
+(3 rows)
+
+visible_ones
+------------
+ 1
+(1 row)
+
+step finish: COMMIT;
+
+starting permutation: pin_snapshot change_key commit_update desummarize vacuum_range probe use_heap probe finish
+step pin_snapshot: SELECT pg_current_snapshot() IS NOT NULL AS snapshot_taken;
+snapshot_taken
+--------------
+t
+(1 row)
+
+step change_key:
+ BEGIN;
+ UPDATE brin_hot_rebuild SET k = 2;
+ SELECT pg_stat_get_xact_tuples_hot_updated('brin_hot_rebuild'::regclass)
+ AS hot_updates;
+
+hot_updates
+-----------
+ 1
+(1 row)
+
+step commit_update: COMMIT;
+step desummarize: SELECT brin_desummarize_range('brin_hot_rebuild_idx', 0);
+brin_desummarize_range
+----------------------
+
+(1 row)
+
+step vacuum_range: VACUUM (INDEX_CLEANUP ON) brin_hot_rebuild;
+step probe:
+ EXPLAIN (COSTS OFF) SELECT count(*) FROM brin_hot_rebuild WHERE k = 1;
+ SELECT count(*) AS visible_ones FROM brin_hot_rebuild WHERE k = 1;
+
+QUERY PLAN
+-----------------------------------------------------
+Aggregate
+ -> Bitmap Heap Scan on brin_hot_rebuild
+ Recheck Cond: (k = 1)
+ -> Bitmap Index Scan on brin_hot_rebuild_idx
+ Index Cond: (k = 1)
+(5 rows)
+
+visible_ones
+------------
+ 1
+(1 row)
+
+step use_heap: SET LOCAL enable_seqscan = on; SET LOCAL enable_bitmapscan = off;
+step probe:
+ EXPLAIN (COSTS OFF) SELECT count(*) FROM brin_hot_rebuild WHERE k = 1;
+ SELECT count(*) AS visible_ones FROM brin_hot_rebuild WHERE k = 1;
+
+QUERY PLAN
+----------------------------------
+Aggregate
+ -> Seq Scan on brin_hot_rebuild
+ Filter: (k = 1)
+(3 rows)
+
+visible_ones
+------------
+ 1
+(1 row)
+
+step finish: COMMIT;
diff --git a/src/test/isolation/isolation_schedule b/src/test/isolation/isolation_schedule
index 94ed7b2a630..67148ffd96f 100644
--- a/src/test/isolation/isolation_schedule
+++ b/src/test/isolation/isolation_schedule
@@ -139,3 +139,4 @@ test: tablespace-dependency-locking
test: pub-concurrent-drop
test: type-ddl-locking
test: drop-owned-grant
+test: brin-hot-rebuild
diff --git a/src/test/isolation/specs/brin-hot-rebuild.spec b/src/test/isolation/specs/brin-hot-rebuild.spec
new file mode 100644
index 00000000000..63e68b5d1b8
--- /dev/null
+++ b/src/test/isolation/specs/brin-hot-rebuild.spec
@@ -0,0 +1,48 @@
+# Both scans use the same snapshot and should find the one row with k = 1.
+
+setup
+{
+ CREATE TABLE brin_hot_rebuild (k int)
+ WITH (fillfactor = 50, autovacuum_enabled = off);
+ INSERT INTO brin_hot_rebuild VALUES (1);
+ CREATE INDEX brin_hot_rebuild_idx ON brin_hot_rebuild USING brin (k)
+ WITH (pages_per_range = 1);
+}
+
+teardown { DROP TABLE brin_hot_rebuild; }
+
+session reader
+setup
+{
+ BEGIN ISOLATION LEVEL REPEATABLE READ;
+ SET LOCAL enable_seqscan = off;
+ SET LOCAL enable_bitmapscan = on;
+}
+
+# Take the snapshot without locking the index: REINDEX needs an exclusive lock.
+step pin_snapshot { SELECT pg_current_snapshot() IS NOT NULL AS snapshot_taken; }
+step probe
+{
+ EXPLAIN (COSTS OFF) SELECT count(*) FROM brin_hot_rebuild WHERE k = 1;
+ SELECT count(*) AS visible_ones FROM brin_hot_rebuild WHERE k = 1;
+}
+step use_heap { SET LOCAL enable_seqscan = on; SET LOCAL enable_bitmapscan = off; }
+step finish { COMMIT; }
+
+session writer
+setup { SET max_parallel_maintenance_workers = 0; }
+step change_key
+{
+ BEGIN;
+ UPDATE brin_hot_rebuild SET k = 2;
+ SELECT pg_stat_get_xact_tuples_hot_updated('brin_hot_rebuild'::regclass)
+ AS hot_updates;
+}
+step commit_update { COMMIT; }
+step rebuild { REINDEX INDEX brin_hot_rebuild_idx; }
+step desummarize { SELECT brin_desummarize_range('brin_hot_rebuild_idx', 0); }
+step vacuum_range { VACUUM (INDEX_CLEANUP ON) brin_hot_rebuild; }
+
+permutation pin_snapshot change_key commit_update rebuild probe use_heap probe finish
+permutation pin_snapshot change_key rebuild commit_update probe use_heap probe finish
+permutation pin_snapshot change_key commit_update desummarize vacuum_range probe use_heap probe finish
--
2.56.1
From 666d82e2284641e953a470cdc7805f3c6d69a219 Mon Sep 17 00:00:00 2001
From: Tomas Vondra <[email protected]>
Date: Wed, 25 Nov 2026 11:30:00 +0100
Subject: [PATCH v1 2/3] Add a READ COMMITTED reproducer for BRIN misses after
HOT updates
Pause a statement after it takes its snapshot but before either scan
reads the table. While it waits, HOT-update the indexed key and let
VACUUM resummarize the range. On an affected server the BRIN scan
misses the old row that the sequential scan finds in the same statement.
The next statement correctly finds no matches with either scan.
Leave the standalone spec out of the isolation schedule, as with the
repeatable-read reproducer.
---
.../expected/brin-hot-read-committed.out | 76 +++++++++++++++++++
src/test/isolation/isolation_schedule | 1 +
.../specs/brin-hot-read-committed.spec | 62 +++++++++++++++
3 files changed, 139 insertions(+)
create mode 100644 src/test/isolation/expected/brin-hot-read-committed.out
create mode 100644 src/test/isolation/specs/brin-hot-read-committed.spec
diff --git a/src/test/isolation/expected/brin-hot-read-committed.out b/src/test/isolation/expected/brin-hot-read-committed.out
new file mode 100644
index 00000000000..76ecd1fbad5
--- /dev/null
+++ b/src/test/isolation/expected/brin-hot-read-committed.out
@@ -0,0 +1,76 @@
+Parsed test spec with 2 sessions
+
+starting permutation: plan_probe hold_probe probe change_key desummarize vacuum_range release_probe probe finish
+step plan_probe:
+ PREPARE count_ones AS
+ WITH probe_key AS MATERIALIZED (
+ SELECT 1 AS k FROM pg_advisory_xact_lock('brin_hot_rc'::regclass::int8)
+ )
+ SELECT (SELECT count(*) FROM brin_hot_rc t WHERE t.k = p.k) AS brin_ones,
+ (SELECT count(*) FILTER (WHERE t.k = p.k)
+ FROM brin_hot_rc t) AS heap_ones
+ FROM probe_key p;
+ EXPLAIN (COSTS OFF) EXECUTE count_ones;
+
+QUERY PLAN
+--------------------------------------------------------
+CTE Scan on probe_key p
+ CTE probe_key
+ -> Function Scan on pg_advisory_xact_lock
+ SubPlan expr_1
+ -> Aggregate
+ -> Bitmap Heap Scan on brin_hot_rc t
+ Recheck Cond: (k = p.k)
+ -> Bitmap Index Scan on brin_hot_rc_idx
+ Index Cond: (k = p.k)
+ SubPlan expr_2
+ -> Aggregate
+ -> Seq Scan on brin_hot_rc t_1
+ Disabled: true
+(13 rows)
+
+step hold_probe: SELECT pg_advisory_lock('brin_hot_rc'::regclass::int8);
+pg_advisory_lock
+----------------
+
+(1 row)
+
+step probe: EXECUTE count_ones; <waiting ...>
+step change_key:
+ BEGIN;
+ UPDATE brin_hot_rc SET k = 2;
+ SELECT pg_stat_get_xact_tuples_hot_updated('brin_hot_rc'::regclass)
+ AS hot_updates;
+ COMMIT;
+
+hot_updates
+-----------
+ 1
+(1 row)
+
+step desummarize: SELECT brin_desummarize_range('brin_hot_rc_idx', 0);
+brin_desummarize_range
+----------------------
+
+(1 row)
+
+step vacuum_range: VACUUM (INDEX_CLEANUP ON) brin_hot_rc;
+step release_probe: SELECT pg_advisory_unlock('brin_hot_rc'::regclass::int8);
+pg_advisory_unlock
+------------------
+t
+(1 row)
+
+step probe: <... completed>
+brin_ones|heap_ones
+---------+---------
+ 1| 1
+(1 row)
+
+step probe: EXECUTE count_ones;
+brin_ones|heap_ones
+---------+---------
+ 0| 0
+(1 row)
+
+step finish: COMMIT;
diff --git a/src/test/isolation/isolation_schedule b/src/test/isolation/isolation_schedule
index 67148ffd96f..d26b041b4f0 100644
--- a/src/test/isolation/isolation_schedule
+++ b/src/test/isolation/isolation_schedule
@@ -140,3 +140,4 @@ test: pub-concurrent-drop
test: type-ddl-locking
test: drop-owned-grant
test: brin-hot-rebuild
+test: brin-hot-read-committed
diff --git a/src/test/isolation/specs/brin-hot-read-committed.spec b/src/test/isolation/specs/brin-hot-read-committed.spec
new file mode 100644
index 00000000000..f380707f7b2
--- /dev/null
+++ b/src/test/isolation/specs/brin-hot-read-committed.spec
@@ -0,0 +1,62 @@
+# Standalone reproducer: isolationtester "dbname=scratch" < brin-hot-read-committed.spec
+# The first READ COMMITTED probe should count (1, 1). On an affected server
+# the BRIN scan misses the old row and the probe counts (0, 1). The next
+# statement sees the committed update and should count (0, 0) on either server.
+
+setup
+{
+ CREATE TABLE brin_hot_rc (k int)
+ WITH (fillfactor = 50, autovacuum_enabled = off);
+ INSERT INTO brin_hot_rc VALUES (1);
+ CREATE INDEX brin_hot_rc_idx ON brin_hot_rc USING brin (k)
+ WITH (pages_per_range = 1);
+}
+
+teardown { DROP TABLE brin_hot_rc; }
+
+session reader
+setup
+{
+ BEGIN ISOLATION LEVEL READ COMMITTED;
+ SET LOCAL enable_seqscan = off;
+ SET LOCAL enable_bitmapscan = on;
+}
+
+# Both scans need the key from the CTE, so neither can start before the
+# writer releases the lock. The aggregate FILTER leaves the heap scan
+# without an index condition.
+step plan_probe
+{
+ PREPARE count_ones AS
+ WITH probe_key AS MATERIALIZED (
+ SELECT 1 AS k FROM pg_advisory_xact_lock('brin_hot_rc'::regclass::int8)
+ )
+ SELECT (SELECT count(*) FROM brin_hot_rc t WHERE t.k = p.k) AS brin_ones,
+ (SELECT count(*) FILTER (WHERE t.k = p.k)
+ FROM brin_hot_rc t) AS heap_ones
+ FROM probe_key p;
+ EXPLAIN (COSTS OFF) EXECUTE count_ones;
+}
+step probe { EXECUTE count_ones; }
+step finish { COMMIT; }
+
+session writer
+setup { SET max_parallel_maintenance_workers = 0; }
+
+# A session lock survives the update's commit and VACUUM. Unlike REINDEX,
+# VACUUM can resummarize the range while the reader has the index open.
+step hold_probe { SELECT pg_advisory_lock('brin_hot_rc'::regclass::int8); }
+step change_key
+{
+ BEGIN;
+ UPDATE brin_hot_rc SET k = 2;
+ SELECT pg_stat_get_xact_tuples_hot_updated('brin_hot_rc'::regclass)
+ AS hot_updates;
+ COMMIT;
+}
+step desummarize { SELECT brin_desummarize_range('brin_hot_rc_idx', 0); }
+step vacuum_range { VACUUM (INDEX_CLEANUP ON) brin_hot_rc; }
+step release_probe { SELECT pg_advisory_unlock('brin_hot_rc'::regclass::int8); }
+
+permutation plan_probe hold_probe probe change_key desummarize vacuum_range
+ release_probe probe finish
--
2.56.1
From 9f0a0dcf4a6b2e1e1c3ca35c459c37cdc81dcc39 Mon Sep 17 00:00:00 2001
From: Tomas Vondra <[email protected]>
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