From dc42aef83ef03c2be06f40262717ba5e2d8a2c86 Mon Sep 17 00:00:00 2001 From: Andrey Borodin Date: Fri, 27 Nov 2026 23:29:19 +0500 Subject: [PATCH] Synchronize the standby buffer pin conflict test Observing a backend in ClientWrite does not guarantee that it will still hold its pin when the second reader arrives. Pause the first reader after it recognizes the conflict, and keep it pinned until another reader reports a buffer pin conflict. Since the second reader connected after the first broadcast, it must have received a resend. Use a local injection point for the first reader and enable this case on builds that support injection points. Discussion: https://postgr.es/m/awl1hvCD4zLn2eLP@alvherre.pgsql --- src/backend/tcop/postgres.c | 6 + src/test/recovery/t/031_recovery_conflict.pl | 118 +++++++------------ 2 files changed, 50 insertions(+), 74 deletions(-) diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index 1361dfa5526..6ca1d91a82a 100644 --- a/src/backend/tcop/postgres.c +++ b/src/backend/tcop/postgres.c @@ -3274,6 +3274,12 @@ ProcessRecoveryConflictInterrupt(RecoveryConflictReason reason) if (!HoldingBufferPinThatDelaysRecovery()) return; +#ifdef USE_INJECTION_POINTS + /* A repeated signal must not re-enter a waiting callback. */ + 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..4f7e0af0f14 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 @@ -279,8 +278,9 @@ check_conflict_stat("deadlock"); # clean up for next tests $node_primary->safe_psql($test_db, qq[ROLLBACK PREPARED 'lock';]); +# Exercise the expired-limit path on the first buffer pin conflict signal. $node_standby->adjust_conf('postgresql.conf', 'max_standby_streaming_delay', - '50ms'); + '0'); $node_standby->restart(); $psql_standby->reconnect_and_clear(); @@ -298,104 +298,74 @@ 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; +# Pause only the first reader after it sees the conflict, before releasing +# its pin. Keeping this injection point local prevents a later reader from +# getting stuck at the same point if recovery retries the signal quickly. +$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"); + +# Only the new reader can report a buffer pin conflict while the first +# is paused. The resend can arrive during FETCH or while it is idle. +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); - -check_conflict_log( - "\\[$standby2_pid\\].*User was holding shared buffer pin for too long"); +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"); -# 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 -- That's all, folks. May the source be with you.