From 666d82e2284641e953a470cdc7805f3c6d69a219 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 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

