From f0eba8368d76a91990f3bdcf9fe4cedb5066a913 Mon Sep 17 00:00:00 2001
From: Tomas Vondra <tomas.vondra@postgresql.org>
Date: Sun, 6 Dec 2026 15:21:45 +0100
Subject: [PATCH v1] Fix BRIN minmax-multi losing concurrently inserted values

When summarize_range() summarizes a page range, it first inserts a
placeholder index tuple, so that concurrent inserts into the range add
their values to the placeholder while the heap is being scanned.  After
the scan it replaces the placeholder with the summary it built; if the
placeholder was modified in the meantime, it unions the two summaries
and tries again.

For minmax-multi, brin_minmax_multi_add_value() keeps the accumulated
values as expanded ranges in bv_mem_value and registers bv_serialize,
so that brin_form_tuple() serializes them into bv_values whenever an
index tuple is formed.  brin_minmax_multi_union() merged the serialized
values and stored the result in bv_values, but left the stale expanded
ranges in place, so the next brin_form_tuple() serialized those again
and overwrote the merged value.  Values inserted concurrently with the
summarization were thus missing from the final summary, and index scans
silently failed to return the rows containing them.

Fix by dropping the expanded ranges after the union, so that the merged
serialized value is used from then on, as for a summary deserialized
from an index tuple.  The other opclasses do not use bv_mem_value and
are not affected.

Add an isolation test using an injection point between the heap scan
and the placeholder update, covering the minmax, minmax-multi and bloom
opclasses.

This bug exists since minmax-multi opclasses were added, in 14.

Backpatch-through: 15
---
 src/backend/access/brin/brin.c                |   7 +
 src/backend/access/brin/brin_minmax_multi.c   |  10 ++
 src/test/modules/injection_points/Makefile    |   1 +
 .../expected/brin_summarize_insert.out        | 163 ++++++++++++++++++
 src/test/modules/injection_points/meson.build |   1 +
 .../specs/brin_summarize_insert.spec          |  93 ++++++++++
 6 files changed, 275 insertions(+)
 create mode 100644 src/test/modules/injection_points/expected/brin_summarize_insert.out
 create mode 100644 src/test/modules/injection_points/specs/brin_summarize_insert.spec

diff --git a/src/backend/access/brin/brin.c b/src/backend/access/brin/brin.c
index 5fc40845c17..a74798a0747 100644
--- a/src/backend/access/brin/brin.c
+++ b/src/backend/access/brin/brin.c
@@ -42,6 +42,7 @@
 #include "utils/fmgrprotos.h"
 #include "utils/guc.h"
 #include "utils/index_selfuncs.h"
+#include "utils/injection_point.h"
 #include "utils/memutils.h"
 #include "utils/rel.h"
 #include "utils/tuplesort.h"
@@ -1826,6 +1827,12 @@ summarize_range(IndexInfo *indexInfo, BrinBuildState *state, Relation heapRel,
 								 heapBlk, scanNumBlks,
 								 brinbuildCallback, state, NULL);
 
+	/*
+	 * Allow tests to insert into the range, and thereby update the
+	 * placeholder tuple, between the heap scan and the update below.
+	 */
+	INJECTION_POINT("brin-summarize-after-scan", NULL);
+
 	/*
 	 * Now we update the values obtained by the scan with the placeholder
 	 * tuple.  We do this in a loop which only terminates if we're able to
diff --git a/src/backend/access/brin/brin_minmax_multi.c b/src/backend/access/brin/brin_minmax_multi.c
index 22dd41392fa..38f0ca6eb59 100644
--- a/src/backend/access/brin/brin_minmax_multi.c
+++ b/src/backend/access/brin/brin_minmax_multi.c
@@ -2895,6 +2895,16 @@ brin_minmax_multi_union(PG_FUNCTION_ARGS)
 	pfree(serialized_a);
 	col_a->bv_values[0] = PointerGetDatum(brin_range_serialize(ranges_a));
 
+	/*
+	 * If "a" was built by brin_minmax_multi_add_value(), bv_mem_value still
+	 * holds its expanded ranges, and brin_form_tuple() would serialize those
+	 * into bv_values again, overwriting the merged value we just stored.
+	 * Forget the expanded ranges: from now on the serialized value is the
+	 * authoritative one, as for a summary deserialized from an index tuple.
+	 */
+	col_a->bv_mem_value = PointerGetDatum(NULL);
+	col_a->bv_serialize = NULL;
+
 	PG_RETURN_VOID();
 }
 
diff --git a/src/test/modules/injection_points/Makefile b/src/test/modules/injection_points/Makefile
index e30740ab124..9258bef4dc7 100644
--- a/src/test/modules/injection_points/Makefile
+++ b/src/test/modules/injection_points/Makefile
@@ -20,6 +20,7 @@ REGRESS_OPTS = --dlpath=$(top_builddir)/src/test/regress
 WAIT_CLEANUP_TST = $(if $(filter win32,$(PORTNAME)),,wait_cleanup)
 
 ISOLATION = basic \
+	    brin_summarize_insert \
 	    inplace \
 	    reindex_concurrently_deferred \
 	    reindex_concurrently_toast \
diff --git a/src/test/modules/injection_points/expected/brin_summarize_insert.out b/src/test/modules/injection_points/expected/brin_summarize_insert.out
new file mode 100644
index 00000000000..dc6dd495228
--- /dev/null
+++ b/src/test/modules/injection_points/expected/brin_summarize_insert.out
@@ -0,0 +1,163 @@
+Parsed test spec with 2 sessions
+
+starting permutation: s1_desumm_minmax s1_summ_minmax s2_insert_minmax s2_wakeup s2_check_minmax
+injection_points_attach
+-----------------------
+                       
+(1 row)
+
+step s1_desumm_minmax: SELECT brin_desummarize_range('brin_summ_minmax_idx', 0);
+brin_desummarize_range
+----------------------
+                      
+(1 row)
+
+step s1_summ_minmax: SELECT brin_summarize_range('brin_summ_minmax_idx', 0); <waiting ...>
+step s2_insert_minmax: INSERT INTO brin_summ_minmax VALUES (5000);
+step s2_wakeup: 
+	SELECT injection_points_detach('brin-summarize-after-scan');
+	SELECT injection_points_wakeup('brin-summarize-after-scan');
+ <waiting ...>
+step s1_summ_minmax: <... completed>
+brin_summarize_range
+--------------------
+                   1
+(1 row)
+
+step s2_wakeup: <... completed>
+injection_points_detach
+-----------------------
+                       
+(1 row)
+
+injection_points_wakeup
+-----------------------
+                       
+(1 row)
+
+step s2_check_minmax: 
+	EXPLAIN (COSTS OFF) SELECT count(*) FROM brin_summ_minmax WHERE v = 5000;
+	SELECT count(*) FROM brin_summ_minmax WHERE v = 5000;
+
+QUERY PLAN                                           
+-----------------------------------------------------
+Aggregate                                            
+  ->  Bitmap Heap Scan on brin_summ_minmax           
+        Recheck Cond: (v = 5000)                     
+        ->  Bitmap Index Scan on brin_summ_minmax_idx
+              Index Cond: (v = 5000)                 
+(5 rows)
+
+count
+-----
+    1
+(1 row)
+
+
+starting permutation: s1_desumm_multi s1_summ_multi s2_insert_multi s2_wakeup s2_check_multi
+injection_points_attach
+-----------------------
+                       
+(1 row)
+
+step s1_desumm_multi: SELECT brin_desummarize_range('brin_summ_multi_idx', 0);
+brin_desummarize_range
+----------------------
+                      
+(1 row)
+
+step s1_summ_multi: SELECT brin_summarize_range('brin_summ_multi_idx', 0); <waiting ...>
+step s2_insert_multi: INSERT INTO brin_summ_multi VALUES (5000);
+step s2_wakeup: 
+	SELECT injection_points_detach('brin-summarize-after-scan');
+	SELECT injection_points_wakeup('brin-summarize-after-scan');
+ <waiting ...>
+step s1_summ_multi: <... completed>
+brin_summarize_range
+--------------------
+                   1
+(1 row)
+
+step s2_wakeup: <... completed>
+injection_points_detach
+-----------------------
+                       
+(1 row)
+
+injection_points_wakeup
+-----------------------
+                       
+(1 row)
+
+step s2_check_multi: 
+	EXPLAIN (COSTS OFF) SELECT count(*) FROM brin_summ_multi WHERE v = 5000;
+	SELECT count(*) FROM brin_summ_multi WHERE v = 5000;
+
+QUERY PLAN                                          
+----------------------------------------------------
+Aggregate                                           
+  ->  Bitmap Heap Scan on brin_summ_multi           
+        Recheck Cond: (v = 5000)                    
+        ->  Bitmap Index Scan on brin_summ_multi_idx
+              Index Cond: (v = 5000)                
+(5 rows)
+
+count
+-----
+    1
+(1 row)
+
+
+starting permutation: s1_desumm_bloom s1_summ_bloom s2_insert_bloom s2_wakeup s2_check_bloom
+injection_points_attach
+-----------------------
+                       
+(1 row)
+
+step s1_desumm_bloom: SELECT brin_desummarize_range('brin_summ_bloom_idx', 0);
+brin_desummarize_range
+----------------------
+                      
+(1 row)
+
+step s1_summ_bloom: SELECT brin_summarize_range('brin_summ_bloom_idx', 0); <waiting ...>
+step s2_insert_bloom: INSERT INTO brin_summ_bloom VALUES (5000);
+step s2_wakeup: 
+	SELECT injection_points_detach('brin-summarize-after-scan');
+	SELECT injection_points_wakeup('brin-summarize-after-scan');
+ <waiting ...>
+step s1_summ_bloom: <... completed>
+brin_summarize_range
+--------------------
+                   1
+(1 row)
+
+step s2_wakeup: <... completed>
+injection_points_detach
+-----------------------
+                       
+(1 row)
+
+injection_points_wakeup
+-----------------------
+                       
+(1 row)
+
+step s2_check_bloom: 
+	EXPLAIN (COSTS OFF) SELECT count(*) FROM brin_summ_bloom WHERE v = 5000;
+	SELECT count(*) FROM brin_summ_bloom WHERE v = 5000;
+
+QUERY PLAN                                          
+----------------------------------------------------
+Aggregate                                           
+  ->  Bitmap Heap Scan on brin_summ_bloom           
+        Recheck Cond: (v = 5000)                    
+        ->  Bitmap Index Scan on brin_summ_bloom_idx
+              Index Cond: (v = 5000)                
+(5 rows)
+
+count
+-----
+    1
+(1 row)
+
diff --git a/src/test/modules/injection_points/meson.build b/src/test/modules/injection_points/meson.build
index a6a296030f4..6d9cafb3d6b 100644
--- a/src/test/modules/injection_points/meson.build
+++ b/src/test/modules/injection_points/meson.build
@@ -28,6 +28,7 @@ test_install_data += files(
 
 injection_points_isolation = [
   'basic',
+  'brin_summarize_insert',
   'inplace',
   'reindex_concurrently_deferred',
   'reindex_concurrently_toast',
diff --git a/src/test/modules/injection_points/specs/brin_summarize_insert.spec b/src/test/modules/injection_points/specs/brin_summarize_insert.spec
new file mode 100644
index 00000000000..ef72de52a7d
--- /dev/null
+++ b/src/test/modules/injection_points/specs/brin_summarize_insert.spec
@@ -0,0 +1,93 @@
+# Test that BRIN range summarization does not lose concurrently inserted
+# values.
+#
+# summarize_range() inserts a placeholder index tuple, scans the heap pages
+# of the range, and then replaces the placeholder with the summary it has
+# computed.  A concurrent insert into the range, between the scan and that
+# update, adds its value to the placeholder tuple instead.  The summarizer
+# must notice that the placeholder changed and union it with its own
+# summary, so that the final index tuple covers the inserted value too.
+#
+# Exercise this for the opclasses whose "union" support functions differ.
+
+setup
+{
+	CREATE EXTENSION injection_points;
+
+	CREATE TABLE brin_summ_minmax (v int) WITH (autovacuum_enabled = off);
+	INSERT INTO brin_summ_minmax SELECT generate_series(1, 100);
+	CREATE INDEX brin_summ_minmax_idx ON brin_summ_minmax
+		USING brin (v int4_minmax_ops) WITH (pages_per_range = 128);
+
+	CREATE TABLE brin_summ_multi (v int) WITH (autovacuum_enabled = off);
+	INSERT INTO brin_summ_multi SELECT generate_series(1, 100);
+	CREATE INDEX brin_summ_multi_idx ON brin_summ_multi
+		USING brin (v int4_minmax_multi_ops) WITH (pages_per_range = 128);
+
+	CREATE TABLE brin_summ_bloom (v int) WITH (autovacuum_enabled = off);
+	INSERT INTO brin_summ_bloom SELECT generate_series(1, 100);
+	CREATE INDEX brin_summ_bloom_idx ON brin_summ_bloom
+		USING brin (v int4_bloom_ops) WITH (pages_per_range = 128);
+}
+teardown
+{
+	DROP TABLE brin_summ_minmax, brin_summ_multi, brin_summ_bloom;
+	DROP EXTENSION injection_points;
+}
+
+# The summarizing session stops after the heap scan, before it tries to
+# replace the placeholder tuple.
+session s1
+setup	{
+	SELECT injection_points_set_local();
+	SELECT injection_points_attach('brin-summarize-after-scan', 'wait');
+}
+step s1_desumm_minmax	{ SELECT brin_desummarize_range('brin_summ_minmax_idx', 0); }
+step s1_summ_minmax		{ SELECT brin_summarize_range('brin_summ_minmax_idx', 0); }
+step s1_desumm_multi	{ SELECT brin_desummarize_range('brin_summ_multi_idx', 0); }
+step s1_summ_multi		{ SELECT brin_summarize_range('brin_summ_multi_idx', 0); }
+step s1_desumm_bloom	{ SELECT brin_desummarize_range('brin_summ_bloom_idx', 0); }
+step s1_summ_bloom		{ SELECT brin_summarize_range('brin_summ_bloom_idx', 0); }
+
+session s2
+setup	{ SET enable_seqscan = off; }
+step s2_wakeup	{
+	SELECT injection_points_detach('brin-summarize-after-scan');
+	SELECT injection_points_wakeup('brin-summarize-after-scan');
+}
+step s2_insert_minmax	{ INSERT INTO brin_summ_minmax VALUES (5000); }
+step s2_insert_multi	{ INSERT INTO brin_summ_multi VALUES (5000); }
+step s2_insert_bloom	{ INSERT INTO brin_summ_bloom VALUES (5000); }
+step s2_check_minmax	{
+	EXPLAIN (COSTS OFF) SELECT count(*) FROM brin_summ_minmax WHERE v = 5000;
+	SELECT count(*) FROM brin_summ_minmax WHERE v = 5000;
+}
+step s2_check_multi	{
+	EXPLAIN (COSTS OFF) SELECT count(*) FROM brin_summ_multi WHERE v = 5000;
+	SELECT count(*) FROM brin_summ_multi WHERE v = 5000;
+}
+step s2_check_bloom	{
+	EXPLAIN (COSTS OFF) SELECT count(*) FROM brin_summ_bloom WHERE v = 5000;
+	SELECT count(*) FROM brin_summ_bloom WHERE v = 5000;
+}
+
+permutation
+	s1_desumm_minmax
+	s1_summ_minmax
+	s2_insert_minmax
+	s2_wakeup(s1_summ_minmax)
+	s2_check_minmax
+
+permutation
+	s1_desumm_multi
+	s1_summ_multi
+	s2_insert_multi
+	s2_wakeup(s1_summ_multi)
+	s2_check_multi
+
+permutation
+	s1_desumm_bloom
+	s1_summ_bloom
+	s2_insert_bloom
+	s2_wakeup(s1_summ_bloom)
+	s2_check_bloom
-- 
2.56.1

