From 9bbe73863002f982c3948d4b47d563063de4d22b Mon Sep 17 00:00:00 2001
From: Tomas Vondra <tomas@vondra.me>
Date: Sat, 5 Dec 2026 20:00:54 -0500
Subject: [PATCH v1 1/2] Add a standby reproducer for wrong results after BRIN
 summarization

Summarizing a page range of a BRIN index leaves out the row versions
that are dead on the primary, like an index build does.  If a standby
runs with hot_standby_feedback off, a snapshot there can still see such
a row version.  The summarization does not remove it from the table, so
replaying it does not conflict with that snapshot, and a query using the
snapshot no longer finds the row through the index.

The test takes a snapshot on a standby and checks that it finds a row
through the BRIN index.  The primary then deletes the row and summarizes
its page range again, with brin_desummarize_range() and
brin_summarize_range().  The new summary does not cover the key of the
deleted row.  Once the standby has replayed that, the snapshot must
still find the row.

That last check fails for now.

The test waits for the standby to replay the WAL up to the insert
position of the primary.  The summarization does not commit a
transaction that has an XID, so its WAL records are not flushed right
away, and waiting for the write position would let the check run before
the standby has replayed them.

VACUUM, brin_summarize_new_values() and autosummarization summarize
page ranges with the same function, summarize_range(), but the test
does not cover them.
---
 src/test/recovery/meson.build                 |  1 +
 .../t/068_brin_summarize_standby_snapshot.pl  | 77 +++++++++++++++++++
 2 files changed, 78 insertions(+)
 create mode 100644 src/test/recovery/t/068_brin_summarize_standby_snapshot.pl

diff --git a/src/test/recovery/meson.build b/src/test/recovery/meson.build
index cb3df6ed895..736195f55ac 100644
--- a/src/test/recovery/meson.build
+++ b/src/test/recovery/meson.build
@@ -70,6 +70,7 @@ tests += {
       't/059_remote_apply_status_interval.pl',
       't/060_multixact_crashed_updater.pl',
       't/061_recovery_target_prefetch.pl',
+      't/068_brin_summarize_standby_snapshot.pl',
     ],
   },
 }
diff --git a/src/test/recovery/t/068_brin_summarize_standby_snapshot.pl b/src/test/recovery/t/068_brin_summarize_standby_snapshot.pl
new file mode 100644
index 00000000000..70d506942c4
--- /dev/null
+++ b/src/test/recovery/t/068_brin_summarize_standby_snapshot.pl
@@ -0,0 +1,77 @@
+
+# Copyright (c) 2026, PostgreSQL Global Development Group
+
+# Test that a snapshot on a standby gets correct results through a BRIN index
+# after a page range was summarized again on the primary.
+#
+# Summarizing a page range leaves out the row versions that are dead on the
+# primary.  With hot_standby_feedback off, the primary does not know about
+# the snapshots on a standby, and one of them might still see such a row
+# version.  The summarization does not remove the row version from the table,
+# so replaying it does not conflict with that snapshot.
+use strict;
+use warnings FATAL => 'all';
+
+use PostgreSQL::Test::Cluster;
+use Test::More;
+
+my $primary = PostgreSQL::Test::Cluster->new('primary');
+$primary->init(allows_streaming => 1);
+$primary->append_conf('postgresql.conf', 'autovacuum = off');
+$primary->start;
+
+# The table has a single page, so the index has a single page range.  The
+# page has a lot of free space, so nothing prunes it.
+$primary->safe_psql(
+	'postgres', q{
+	CREATE TABLE tab (k int);
+	INSERT INTO tab VALUES (1), (5);
+	CREATE INDEX tab_idx ON tab USING brin (k);
+});
+
+# Avoid unrelated recovery conflicts from pruning system catalogs.
+$primary->safe_psql('postgres', 'VACUUM FREEZE');
+
+$primary->backup('backup');
+my $standby = PostgreSQL::Test::Cluster->new('standby');
+$standby->init_from_backup($primary, 'backup', has_streaming => 1);
+$standby->append_conf('postgresql.conf', 'hot_standby_feedback = off');
+$standby->start;
+
+# Take a snapshot on the standby.  This session has no choice but the index,
+# and the summary of the page range covers the key it looks for.
+my $reader = $standby->background_psql('postgres');
+$reader->query_safe('SET enable_seqscan = off');
+$reader->query_safe('BEGIN ISOLATION LEVEL REPEATABLE READ');
+like(
+	$reader->query_safe(q{EXPLAIN (COSTS OFF) SELECT k FROM tab WHERE k = 1}),
+	qr/tab_idx/, 'standby snapshot uses the index');
+is($reader->query_safe(q{SELECT k FROM tab WHERE k = 1}),
+	'1', 'standby snapshot finds the row before the changes on the primary');
+
+# Delete the row with the lowest key.  No snapshot on the primary can see it
+# after that.
+$primary->safe_psql('postgres', q{DELETE FROM tab WHERE k = 1});
+
+# Summarize the page range again.  The new summary is built from the rows
+# that are not dead on the primary, so it does not cover the deleted key.
+$primary->safe_psql('postgres',
+	q{SELECT brin_desummarize_range('tab_idx', 0)});
+is( $primary->safe_psql(
+		'postgres', q{SELECT brin_summarize_range('tab_idx', 0)}),
+	'1',
+	'the page range is summarized again');
+
+# The summarization does not commit a transaction that has an XID, so its
+# WAL records are not flushed right away.  Wait until the standby has
+# replayed all the WAL inserted so far, not just what was written out.
+$primary->wait_for_catchup($standby, 'replay', $primary->lsn('insert'));
+
+# The standby snapshot still sees the deleted row.
+is($reader->query_safe(q{SELECT k FROM tab WHERE k = 1}), '1',
+	'standby snapshot finds a row deleted before the range was summarized again'
+);
+$reader->query_safe('COMMIT');
+$reader->quit;
+
+done_testing();
-- 
2.56.1

