From 4c565ffda08a26ad1b8e41e3c53c9ea71de79fb3 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 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

