From 8d521ec4d347823cb858b869d70b42dfe401c548 Mon Sep 17 00:00:00 2001 From: Jingtang Zhang Date: Mon, 16 Nov 2026 14:13:43 +0800 Subject: [PATCH v2 1/2] Honor transaction timeout during blocked client reads and writes A transaction timeout can expire while the backend is waiting to read or write client data. Handle it in those waits alongside explicit session termination so a stalled client cannot prevent the backend from exiting. Give explicit termination first priority and transaction timeout precedence over recoverable query cancellation. Reviewed-by: Andrey Borodin Reviewed-by: Zsolt Parragi --- src/backend/tcop/postgres.c | 34 ++++++----- src/test/modules/test_misc/t/005_timeouts.pl | 64 ++++++++++++++++++++ 2 files changed, 82 insertions(+), 16 deletions(-) diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index d6761aeea9f..53c2dcb0521 100644 --- a/src/backend/tcop/postgres.c +++ b/src/backend/tcop/postgres.c @@ -529,7 +529,8 @@ ProcessClientReadInterrupt(bool blocked) if (notifyInterruptPending) ProcessNotifyInterrupt(true); } - else if (ProcDiePending) + else if (ProcDiePending || + (TransactionTimeoutPending && TransactionTimeout > 0)) { /* * We're dying. If there is no data available to read, then it's safe @@ -562,7 +563,8 @@ ProcessClientWriteInterrupt(bool blocked) { int save_errno = errno; - if (ProcDiePending) + if (ProcDiePending || + (TransactionTimeoutPending && TransactionTimeout > 0)) { /* * We're dying. If it's not possible to write, then we should handle @@ -577,7 +579,7 @@ ProcessClientWriteInterrupt(bool blocked) { /* * Don't mess with whereToSendOutput if ProcessInterrupts wouldn't - * service ProcDiePending. + * service the pending termination. */ if (InterruptHoldoffCount == 0 && CritSectionCount == 0) { @@ -3525,6 +3527,19 @@ ProcessInterrupts(void) ERRDETAIL_SIGNAL_SENDER(sender_pid, sender_uid))); } + if (TransactionTimeoutPending) + { + /* Ignore the signal if the GUC has been reset to zero. */ + TransactionTimeoutPending = false; + if (TransactionTimeout > 0) + { + INJECTION_POINT("transaction-timeout", NULL); + ereport(FATAL, + (errcode(ERRCODE_TRANSACTION_TIMEOUT), + errmsg("terminating connection due to transaction timeout"))); + } + } + if (CheckClientConnectionPending) { CheckClientConnectionPending = false; @@ -3657,19 +3672,6 @@ ProcessInterrupts(void) } } - if (TransactionTimeoutPending) - { - /* As above, ignore the signal if the GUC has been reset to zero. */ - TransactionTimeoutPending = false; - if (TransactionTimeout > 0) - { - INJECTION_POINT("transaction-timeout", NULL); - ereport(FATAL, - (errcode(ERRCODE_TRANSACTION_TIMEOUT), - errmsg("terminating connection due to transaction timeout"))); - } - } - if (IdleSessionTimeoutPending) { /* As above, ignore the signal if the GUC has been reset to zero. */ diff --git a/src/test/modules/test_misc/t/005_timeouts.pl b/src/test/modules/test_misc/t/005_timeouts.pl index c16b7dbf5e6..79ff980f1d6 100644 --- a/src/test/modules/test_misc/t/005_timeouts.pl +++ b/src/test/modules/test_misc/t/005_timeouts.pl @@ -139,4 +139,68 @@ $node->wait_for_log('terminating connection due to idle-session timeout', ok($psql_session->quit); +# +# 4. Test transaction timeout while blocked writing to the client. +# +$node->safe_psql('postgres', + "SELECT injection_points_detach('transaction-timeout');"); + +$psql_session = $node->background_psql('postgres'); +my $pid = $psql_session->query_safe('SELECT pg_backend_pid()'); +$log_offset = -s $node->logfile; + +# Stop draining psql's stdout at the marker. COPY streams its output, so psql +# will stop reading from the server once its stdout pipe fills up. +$psql_session->query_until( + qr/copy_starting/, q( + \echo copy_starting + BEGIN; + SET LOCAL transaction_timeout = '5s'; + COPY (SELECT repeat('x', 8192) FROM generate_series(1, 100000)) TO STDOUT; +)); +$node->wait_for_event('client backend', 'ClientWrite'); + +$node->wait_for_log( + qr/\[$pid\].*FATAL:\s+terminating connection due to transaction timeout/, + $log_offset); +ok( $node->poll_query_until( + 'postgres', qq[ + SELECT NOT EXISTS (SELECT FROM pg_stat_activity WHERE pid = $pid); + ]), + 'transaction timeout terminates the backend during ClientWrite'); +$psql_session->{run}->finish; + +# +# 5. Test transaction timeout while blocked reading COPY data from the client. +# +$psql_session = $node->background_psql('postgres'); +$pid = $psql_session->query_safe('SELECT pg_backend_pid()'); +$log_offset = -s $node->logfile; + +# Leave COPY waiting for input until the backend exits. +$psql_session->query_until( + qr/copy_starting/, q( + \echo copy_starting + CREATE TEMP TABLE copy_in (v int); + BEGIN; + SET LOCAL transaction_timeout = '5s'; + COPY copy_in FROM STDIN; +)); +$node->poll_query_until( + 'postgres', qq[ + SELECT EXISTS (SELECT FROM pg_stat_activity + WHERE pid = $pid AND state = 'active' + AND wait_event = 'ClientRead'); +]) or die "backend did not block reading COPY data from the client"; + +$node->wait_for_log( + qr/\[$pid\].*FATAL:\s+terminating connection due to transaction timeout/, + $log_offset); +ok( $node->poll_query_until( + 'postgres', qq[ + SELECT NOT EXISTS (SELECT FROM pg_stat_activity WHERE pid = $pid); + ]), + 'transaction timeout terminates the backend during ClientRead'); +$psql_session->{run}->finish; + done_testing(); base-commit: 5ffc82103773eea1ab5aa699edc301d4f03de4ce -- 2.43.7