From 1c04ae310c83cd8a87a30ec1d5902d854ceff316 Mon Sep 17 00:00:00 2001 From: alterego655 <824662526@qq.com> Date: Sat, 28 Nov 2026 20:59:34 +0800 Subject: [PATCH v2] 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. Stop the first reader with SIGSTOP after it pins the page, so that it keeps its pin and cannot react to the conflict. Since the second reader connected after the first broadcast, it must have received a resend. These branches lack injection points, so this uses SIGSTOP instead, and the case is skipped on Windows. The whole test file remains disabled on these branches. Also disable log_startup_progress_interval, whose timer would resend the signal and make the test pass without the fix. Discussion: https://postgr.es/m/awl1hvCD4zLn2eLP@alvherre.pgsql --- src/test/recovery/t/031_recovery_conflict.pl | 155 ++++++++----------- 1 file changed, 68 insertions(+), 87 deletions(-) diff --git a/src/test/recovery/t/031_recovery_conflict.pl b/src/test/recovery/t/031_recovery_conflict.pl index 0b87b0328f3..1549e084f99 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; use PostgreSQL::Test::Cluster; use PostgreSQL::Test::Utils; use Test::More; -use Time::HiRes qw(usleep); plan skip_all => "disabled due to instability"; @@ -277,8 +276,13 @@ 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'); +# The startup progress timeout would also wake up recovery and resend the +# signal, so disable it. +$node_standby->append_conf('postgresql.conf', + 'log_startup_progress_interval = 0'); $node_standby->restart(); $psql_standby->reconnect_and_clear(); @@ -291,107 +295,84 @@ is( $node_standby->safe_psql( $expected_conflicts, qq[$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 "SIGSTOP is not available on Windows", 3 if $windows_os; -## RECOVERY CONFLICT 6: Buffer pin conflict after standby limit expired -$sect = "buffer pin conflict after standby limit expired"; + ## 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) - 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; + # A single-page table with one dead tuple for VACUUM to remove. + my $table_pin = "bufferpin_test"; + $node_primary->safe_psql( + $test_db, + qq[ + CREATE TABLE $table_pin (id int) WITH (autovacuum_enabled = false); + INSERT INTO $table_pin VALUES (1), (2), (3); + DELETE FROM $table_pin WHERE id = 3; ]); -$node_primary->wait_for_replay_catchup($node_standby); + $node_primary->wait_for_catchup($node_standby, 'replay', + $node_primary->lsn('flush')); + + # Stop the first reader after it pins the page, so that it keeps its pin + # and cannot react to the conflict. + 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"); + kill 'STOP', $first_pid; -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 ''; + $log_location = -s $node_standby->logfile; -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; - -# 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); + # 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;]); -ok( $node_standby->poll_query_until( - $test_db, - qq[SELECT wait_event = 'BufferPin' FROM pg_stat_activity + # With a zero delay, the signal is sent before the startup process waits. + ok( $node_standby->poll_query_until( + $test_db, + qq[SELECT wait_event = 'BufferPin' FROM pg_stat_activity WHERE backend_type = 'startup';], - 't'), - "$sect: startup process is waiting on BufferPin"); - -# 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). -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( - qq[ + 't'), + "$sect: startup process is waiting on BufferPin"); + + # The second reader connected after the first broadcast, so only a + # resend can cancel it. The resend can arrive during FETCH or while it + # is idle. + my $psql_standby2 = + $node_standby->background_psql($test_db, on_error_stop => 0); + $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); + # Only the second reader can report a buffer pin conflict while the + # first is stopped. + eval { + $node_standby->wait_for_log( + qr/User was holding shared buffer pin for too long/, + $log_location); + }; -check_conflict_log( - "\\[$standby2_pid\\].*User was holding shared buffer pin for too long"); + # Resume the first reader even if the check failed. + kill 'CONT', $first_pid; -# Reset for next test -$psql_standby2->quit; -$psql_standby->reconnect_and_clear(); + is($@, '', "$sect: late pinner canceled by a resent signal"); -} # end SKIP + # Reset for next test + $psql_standby2->quit; + $node_primary->wait_for_catchup($node_standby, 'replay', + $node_primary->lsn('flush')); + $psql_standby->reconnect_and_clear(); +} ## RECOVERY CONFLICT 7: Database conflict $sect = "database conflict"; -- 2.51.0