From 34a3aae76e1bf0ac3ea5c0bada880880bc269c46 Mon Sep 17 00:00:00 2001
From: Andrey Borodin <amborodin@acm.org>
Date: Fri, 27 Nov 2026 23:29:19 +0500
Subject: [PATCH] Synchronize the standby buffer-pin conflict test

Use a local injection point to pause the first reader after it receives a
recovery conflict signal. Verify that retrying the signal cancels a second
reader, and skip the test when injection points are unavailable.
---
 src/backend/tcop/postgres.c                  |   5 +
 src/test/recovery/t/031_recovery_conflict.pl | 117 +++++++------------
 2 files changed, 44 insertions(+), 78 deletions(-)

diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index 1361dfa5526..1d50eeced55 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -3274,6 +3274,11 @@ ProcessRecoveryConflictInterrupt(RecoveryConflictReason reason)
 			if (!HoldingBufferPinThatDelaysRecovery())
 				return;
 
+#ifdef USE_INJECTION_POINTS
+			HOLD_INTERRUPTS();
+			INJECTION_POINT("recovery-buffer-pin-before-cancel", NULL);
+			RESUME_INTERRUPTS();
+#endif
 			report_recovery_conflict(reason);
 			return;
 
diff --git a/src/test/recovery/t/031_recovery_conflict.pl b/src/test/recovery/t/031_recovery_conflict.pl
index b17ce12ba35..72188ef7afd 100644
--- a/src/test/recovery/t/031_recovery_conflict.pl
+++ b/src/test/recovery/t/031_recovery_conflict.pl
@@ -9,7 +9,6 @@ use warnings FATAL => 'all';
 use PostgreSQL::Test::Cluster;
 use PostgreSQL::Test::Utils;
 use Test::More;
-use Time::HiRes qw(usleep);
 
 
 # Set up nodes
@@ -280,7 +279,7 @@ check_conflict_stat("deadlock");
 # clean up for next tests
 $node_primary->safe_psql($test_db, qq[ROLLBACK PREPARED 'lock';]);
 $node_standby->adjust_conf('postgresql.conf', 'max_standby_streaming_delay',
-	'50ms');
+	'0');
 $node_standby->restart();
 $psql_standby->reconnect_and_clear();
 
@@ -298,107 +297,69 @@ cmp_ok(
 	qq[at least $expected_conflicts recovery conflicts shown in pg_stat_database]
 );
 
-# Disable this for now. Not re-indented to avoid noise when putting it back.
 SKIP:
 {
-	skip "this test fails for several different reasons";
+	skip 'Injection points not available', 3
+	  unless $ENV{enable_injection_points} eq 'yes'
+	  && $node_primary->check_extension('injection_points');
 
-## RECOVERY CONFLICT 6: Buffer pin conflict after standby limit expired
 $sect = "buffer pin conflict after standby limit expired";
 
-# Set up a single-page table with 50 live tuples and 50 dead tuples.
 my $table_pin = "bufferpin_test";
 $node_primary->safe_psql(
 	$test_db,
 	qq[
-	CREATE TABLE $table_pin (id int PRIMARY KEY, pad text)
+	CREATE EXTENSION injection_points;
+	CREATE TABLE $table_pin (id int)
 		WITH (autovacuum_enabled = false);
-	INSERT INTO $table_pin
-		SELECT g, repeat('x', 20) FROM generate_series(1, 100) g;
-	DELETE FROM $table_pin WHERE id % 2 = 0;
+	INSERT INTO $table_pin VALUES (1), (2), (3);
+	DELETE FROM $table_pin WHERE id = 3;
 	]);
 $node_primary->wait_for_replay_catchup($node_standby);
 
-is( $node_standby->safe_psql($test_db, qq[SELECT count(*) FROM $table_pin;]),
-	50,
-	"$sect: standby sees 50 live tuples");
-
-# Start a reader on $psql_standby that holds a pin on $table_pin and gets
-# stuck in ClientWrite because we stop pumping its stdout pipe.
-my $clientwrite_pid = $psql_standby->query_safe('SELECT pg_backend_pid();');
-$psql_standby->{stdin} .= qq[
-	COPY (
-		SELECT t.id, g, repeat('x', 4000)
-		FROM $table_pin t
-		CROSS JOIN LATERAL generate_series(1, 10000 + t.id * 0) g
-	) TO STDOUT;
-];
-$psql_standby->{run}->pump() while $psql_standby->{stdin} ne '';
-
-ok( $node_standby->poll_query_until(
-		$test_db,
-		qq[SELECT wait_event = 'ClientWrite' FROM pg_stat_activity
-		   WHERE pid = $clientwrite_pid;],
-		't'),
-	"$sect: COPY session is blocked in ClientWrite");
-
-$log_location = -s $node_standby->logfile;
+$psql_standby->query_safe(q[
+	SELECT injection_points_set_local();
+	SELECT injection_points_attach('recovery-buffer-pin-before-cancel', 'wait');
+]);
+my $first_pid = $psql_standby->query_safe('SELECT pg_backend_pid()');
+$res = $psql_standby->query_safe(qq[
+	BEGIN;
+	DECLARE first_pin CURSOR FOR SELECT * FROM $table_pin;
+	FETCH 1 FROM first_pin;
+]);
+like($res, qr/^1$/m, "$sect: first cursor holds pin");
 
-# VACUUM on primary generates a Heap2 prune WAL record requiring a buffer
-# cleanup lock on the standby.
 $node_primary->safe_psql($test_db, qq[VACUUM $table_pin;]);
-
-# Wait for the startup process to block on the buffer cleanup lock and wait
-# past max_standby_streaming_delay (50ms) so that the startup process has
-# already attempted to cancel the ClientWrite session and gone back to waiting
-# on the buffer pin.
-$log_location = $node_standby->wait_for_log(
-	qr/recovery still waiting after .* recovery conflict on buffer pin/,
-	$log_location);
-usleep(50_000);
-
-ok( $node_standby->poll_query_until(
-		$test_db,
-		qq[SELECT wait_event = 'BufferCleanup' FROM pg_stat_activity
-		   WHERE backend_type = 'startup';],
-		't'),
-	"$sect: startup process is waiting on BufferCleanup");
-
-# Open a second session on standby to acquire another conflicting pin on the
-# same page and stay idle in transaction (it was never signaled yet).
+ok($node_standby->poll_query_until($test_db, qq[
+	SELECT wait_event = 'recovery-buffer-pin-before-cancel'
+	FROM pg_stat_activity WHERE pid = $first_pid], 't'),
+	"$sect: initial signal reached first pinner");
+
+my $conflicts = $node_standby->safe_psql($test_db, qq[
+	SELECT confl_bufferpin FROM pg_stat_database_conflicts
+	WHERE datname = '$test_db']);
 my $psql_standby2 =
   $node_standby->background_psql($test_db, on_error_stop => 0);
-my $standby2_pid = $psql_standby2->query_safe('SELECT pg_backend_pid();');
-$res = $psql_standby2->query_safe(
+$psql_standby2->query_until(
+	qr/^starting cursor$/m,
 	qq[
 	BEGIN;
+	\\echo starting cursor
 	DECLARE c CURSOR FOR SELECT * FROM $table_pin;
-	FETCH 5 FROM c;
+	FETCH 1 FROM c;
 	]);
-like($res, qr/^1\|x{20}$/m,
-	"$sect: second session with conflicting pin established");
-
-# Kill the ClientWrite client so its backend drops its buffer pin, leaving
-# only the second session's pin behind.
-$psql_standby->{run}->kill_kill;
-ok( $node_standby->poll_query_until(
-		$test_db,
-		qq[SELECT count(*) = 0 FROM pg_stat_activity
-		   WHERE pid = $clientwrite_pid;],
-		't'),
-	"$sect: ClientWrite session terminated");
-
-# Replay should cancel the second session and catch up.
-$node_primary->wait_for_replay_catchup($node_standby);
+ok($node_standby->poll_query_until($test_db, qq[
+	SELECT confl_bufferpin > $conflicts FROM pg_stat_database_conflicts
+	WHERE datname = '$test_db']), "$sect: second reader canceled");
 
-check_conflict_log(
-	"\\[$standby2_pid\\].*User was holding shared buffer pin for too long");
-
-# Reset for next test
+$node_standby->safe_psql($test_db, q[
+	SELECT injection_points_wakeup('recovery-buffer-pin-before-cancel');
+]);
 $psql_standby2->quit;
+$node_primary->wait_for_replay_catchup($node_standby);
 $psql_standby->reconnect_and_clear();
 
-}	# end SKIP
+}
 
 ## RECOVERY CONFLICT 7: Database conflict
 $sect = "database conflict";
-- 
2.50.1 (Apple Git-155)

