From 5e7d7b145cceeb1799e8948236a0b3127ccbd5b5 Mon Sep 17 00:00:00 2001
From: Tomas Vondra <tomas@vondra.me>
Date: Tue, 1 Dec 2026 14:43:40 -0500
Subject: [PATCH v1 3/7] Add a TAP test for an interrupted BRIN page evacuation

Cancel a revmap extension while it evacuates a page, which leaves the
page flagged for evacuation with its live tuples.  An update that fits
on that page then retries forever: brininsert() ignores the flag, but
brin_doupdate() refuses the same-page update.  The test is expected to
fail until such an update moves the tuple instead.

The issue dates back to commit 7516f525941, which added BRIN in
PostgreSQL 9.5.
---
 src/backend/access/brin/brin_pageops.c        |   2 +
 src/test/modules/brin/meson.build             |   1 +
 .../brin/t/05_interrupted_evacuation.pl       | 110 ++++++++++++++++++
 3 files changed, 113 insertions(+)
 create mode 100644 src/test/modules/brin/t/05_interrupted_evacuation.pl

diff --git a/src/backend/access/brin/brin_pageops.c b/src/backend/access/brin/brin_pageops.c
index 48210163d55..485119ee722 100644
--- a/src/backend/access/brin/brin_pageops.c
+++ b/src/backend/access/brin/brin_pageops.c
@@ -593,6 +593,8 @@ brin_evacuate_page(Relation idxRel, BlockNumber pagesPerRange,
 
 			LockBuffer(buf, BUFFER_LOCK_UNLOCK);
 
+			INJECTION_POINT("brin-evacuate-page-before-move", NULL);
+
 			if (!brin_doupdate(idxRel, pagesPerRange, revmap, tup->bt_blkno,
 							   buf, off, tup, sz, tup, sz, false))
 				off--;			/* retry */
diff --git a/src/test/modules/brin/meson.build b/src/test/modules/brin/meson.build
index 800840f7cf7..03ea590e487 100644
--- a/src/test/modules/brin/meson.build
+++ b/src/test/modules/brin/meson.build
@@ -17,6 +17,7 @@ tests += {
       't/01_workitems.pl',
       't/02_wal_consistency.pl',
       't/04_desummarize_lock_order.pl',
+      't/05_interrupted_evacuation.pl',
     ],
   },
 }
diff --git a/src/test/modules/brin/t/05_interrupted_evacuation.pl b/src/test/modules/brin/t/05_interrupted_evacuation.pl
new file mode 100644
index 00000000000..7e177b06bd3
--- /dev/null
+++ b/src/test/modules/brin/t/05_interrupted_evacuation.pl
@@ -0,0 +1,110 @@
+# Copyright (c) 2026, PostgreSQL Global Development Group
+
+# Check that an interrupted page evacuation does not make a later summary
+# update on that page retry forever.
+#
+# Extending the revmap over a regular page sets BRIN_EVACUATE_PAGE on it and
+# then moves the live tuples elsewhere.  If the extension fails partway, the
+# flag stays set and the remaining tuples stay on the page.  brininsert()
+# decides whether an updated summary fits on the old page without checking
+# the flag, but brin_doupdate() refuses to replace a tuple on a flagged page
+# and asks the caller to retry.  The caller then makes the same decision.
+
+use strict;
+use warnings FATAL => 'all';
+
+use PostgreSQL::Test::Cluster;
+use PostgreSQL::Test::Utils;
+use Test::More;
+
+plan skip_all => 'Injection points not supported by this build'
+  unless $ENV{enable_injection_points} eq 'yes';
+
+my $node = PostgreSQL::Test::Cluster->new('main');
+$node->init;
+$node->append_conf('postgresql.conf', 'autovacuum = off');
+$node->start;
+
+plan skip_all => 'Extension injection_points not installed'
+  unless $node->check_extension('injection_points');
+
+# One row per heap page, with room to keep an updated row on its page.  The
+# summaries of ranges 0 to 2 are on index block 2, the first block the revmap
+# will need when it grows.
+$node->safe_psql(
+	'postgres', q{
+CREATE EXTENSION injection_points;
+CREATE EXTENSION pageinspect;
+CREATE TABLE t (a int, pad text STORAGE PLAIN) WITH (fillfactor = 10);
+INSERT INTO t SELECT i, repeat('x', 500) FROM generate_series(0, 2) i;
+CREATE INDEX idx ON t USING brin (a) WITH (pages_per_range = 1);
+});
+
+# Make the heap long enough that summarizing its last range needs a second
+# revmap page.
+my $ranges = $node->safe_psql('postgres',
+	q{SELECT count(*) FROM brin_revmap_data(get_raw_page('idx', 1))});
+$node->safe_psql('postgres',
+	"INSERT INTO t SELECT i, repeat('x', 500) FROM generate_series(3, $ranges) i"
+);
+is( $node->safe_psql(
+		'postgres',
+		q{SELECT pg_relation_size('t') / current_setting('block_size')::int}),
+	$ranges + 1,
+	'one heap page per row');
+
+# Cancel the revmap extension before it moves the first live tuple.
+my $extender = $node->background_psql('postgres', on_error_stop => 0);
+my $extender_pid = $extender->query_safe('SELECT pg_backend_pid()');
+$extender->query_safe(
+	q{
+SELECT injection_points_set_local();
+SELECT injection_points_attach('brin-evacuate-page-before-move', 'wait');
+});
+$extender->query_until(
+	qr/^extending$/m, qq{
+\\echo extending
+SELECT brin_summarize_range('idx', $ranges);
+});
+$node->wait_for_event('client backend', 'brin-evacuate-page-before-move');
+$node->safe_psql('postgres', "SELECT pg_cancel_backend($extender_pid)");
+$extender->query('SELECT 1');
+like(
+	$extender->{stderr},
+	qr/canceling statement due to user request/,
+	'revmap extension was canceled during evacuation');
+$extender->quit;
+
+# BrinSpecialSpace ends with the flags and the page type, two bytes each.
+# BRIN_EVACUATE_PAGE is the only flag, so either flag byte holds it.
+is( $node->safe_psql(
+		'postgres', q{
+SELECT brin_page_type(p), get_byte(p, bs - 4) | get_byte(p, bs - 3)
+FROM (SELECT get_raw_page('idx', 2) AS p,
+             current_setting('block_size')::int AS bs) s
+}),
+	'regular|1',
+	'block 2 is still a regular page, marked for evacuation');
+is( $node->safe_psql(
+		'postgres', q{
+SELECT array_agg(blknum ORDER BY blknum)
+FROM brin_page_items(get_raw_page('idx', 2), 'idx')
+}),
+	'{0,1,2}',
+	'the live summaries are still on block 2');
+
+# Widening the range 1 summary from {1 .. 1} to {-1 .. 1} does not change
+# its size.  The retry loop never ends, so the timeout only has to cover one
+# single-row update.
+my ($ret, $stdout, $stderr) = $node->psql(
+	'postgres', q{
+SET statement_timeout = '10s';
+UPDATE t SET a = -1 WHERE a = 1 RETURNING ctid;
+});
+is($ret, 0, 'updating a summary on the flagged page finishes')
+  or diag $stderr;
+like($stdout, qr/^\(1,\d+\)$/, 'updated row stayed in range 1');
+
+$node->stop;
+
+done_testing();
-- 
2.56.1

