pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedBUG #19896: transaction_timeout misbehaves after ROLLBACK TO SAVEPOINT and after errors
2+ messages / 2 participants
[nested] [flat]
* BUG #19896: transaction_timeout misbehaves after ROLLBACK TO SAVEPOINT and after errors
@ 2026-11-12 12:41 PG Bug reporting form <noreply@postgresql.org>
0 siblings, 1 reply; 2+ messages in thread
From: PG Bug reporting form @ 2026-11-12 12:41 UTC (permalink / raw)
To: pgsql-bugs@lists.postgresql.org; +Cc: nickevanoorschot@gmail.com
The following bug has been logged on the website:
Bug reference: 19896
Logged by: Nicke van Oorschot
Email address: nickevanoorschot@gmail.com
PostgreSQL version: 18.6
Operating system: Ubuntu 22.04.5 LTS
Description:
PostgreSQL versions: 18.6; 20devel (development snapshot
postgresql-snapshot.tar.bz2 of
2026-11-12 04:22 UTC, master at debfb3c8a8c93e7bcc3638523b7ffc0846cc2db8).
First observed
on 18.1 ("PostgreSQL 18.1 on x86_64-pc-linux-musl, compiled by gcc (Alpine
15.2.0) 15.2.0, 64-bit").
Documentation of transaction_timeout: "Terminate any session that spans
longer than the
specified amount of time in a transaction." The cases below differ from
that. Each script was
run with "psql -X -f <file>" against a freshly initialised cluster with
default settings.
Output below is from 18.6; 20devel gives the same output apart from the
LOCATION line numbers.
Case 1: a timeout set inside a rolled-back savepoint terminates a later
transaction
--- case1.sql
\set VERBOSITY verbose
-- Transaction 1 sets transaction_timeout inside a savepoint and rolls the
savepoint back.
BEGIN;
SELECT now() AS transaction_1_start;
SAVEPOINT s;
SET LOCAL transaction_timeout = '2s';
ROLLBACK TO SAVEPOINT s;
SHOW transaction_timeout;
COMMIT;
-- Transaction 2 sets a 10 s transaction_timeout and runs for 3 s.
BEGIN;
SELECT now() AS transaction_2_start;
SET LOCAL transaction_timeout = '10s';
SELECT pg_sleep(3);
COMMIT;
SELECT 'transaction 2 committed' AS result;
---
Output:
BEGIN
transaction_1_start
-------------------------------
2026-11-12 11:33:03.520214+00
(1 row)
SAVEPOINT
SET
ROLLBACK
transaction_timeout
---------------------
0
(1 row)
COMMIT
BEGIN
transaction_2_start
-------------------------------
2026-11-12 11:33:03.520757+00
(1 row)
SET
psql:case1.sql:14: FATAL: 25P04: terminating connection due to transaction
timeout
LOCATION: ProcessInterrupts, postgres.c:3498
psql:case1.sql:14: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
psql:case1.sql:14: error: connection to server was lost
Server log:
2026-11-12 11:33:05.520 UTC [297561] FATAL: terminating connection due to
transaction timeout
2026-11-12 11:33:05.520 UTC [297561] STATEMENT: SELECT pg_sleep(3);
The connection is terminated 2 s after transaction 1 started, while
transaction 2 has run for
less than 2 s with a 10 s transaction_timeout.
Expected: transaction 2 completes and "transaction 2 committed" is returned.
Transaction 1
ended with transaction_timeout = 0, and transaction 2 stays within its 10 s.
Case 2: after an error and ROLLBACK TO SAVEPOINT, the transaction is not
terminated
--- case2.sql
\set VERBOSITY verbose
-- The transaction sets a 2 s transaction_timeout, recovers from an error
with ROLLBACK TO SAVEPOINT, and runs for 3 s.
BEGIN;
SET LOCAL transaction_timeout = '2s';
SAVEPOINT s;
SELECT 1/0;
ROLLBACK TO SAVEPOINT s;
SHOW transaction_timeout;
SELECT pg_sleep(3);
COMMIT;
SELECT 'transaction committed after 3 s' AS result;
---
Output:
BEGIN
SET
SAVEPOINT
psql:case2.sql:6: ERROR: 22012: division by zero
LOCATION: int4div, int.c:870
ROLLBACK
transaction_timeout
---------------------
2s
(1 row)
pg_sleep
----------
(1 row)
COMMIT
result
---------------------------------
transaction committed after 3 s
(1 row)
Expected: the connection is terminated 2 s into the transaction, during
pg_sleep(3), and the
transaction does not commit.
The same script with "SELECT 1;" instead of "SELECT 1/0;" is terminated as
expected:
psql:case2_control.sql:8: FATAL: 25P04: terminating connection due to
transaction timeout
Setting the timeout for the session ("SET transaction_timeout = '2s';"
before BEGIN, no SET
LOCAL) gives the same result as case 2: the transaction commits after 3 s.
Case 3: an aborted transaction is not terminated
--- case3.sql
\set VERBOSITY verbose
-- The transaction sets a 2 s transaction_timeout, fails, and stays open
(aborted) for 3 s.
BEGIN;
SET LOCAL transaction_timeout = '2s';
SELECT 1/0;
\! sleep 3
ROLLBACK;
SELECT 'connection still open after 3 s' AS result;
---
Output:
BEGIN
SET
psql:case3.sql:5: ERROR: 22012: division by zero
LOCATION: int4div, int.c:870
ROLLBACK
result
---------------------------------
connection still open after 3 s
(1 row)
Expected: the connection is terminated 2 s into the transaction, while it is
open in the
aborted state.
Environment of the 18.6 and 20devel runs:
- Built from the source tarballs with
./configure --without-readline --without-zlib --without-icu
--without-openssl; make; make install
(no other deviation from the installation instructions)
- Cluster: initdb -A trust -U postgres, no changes to postgresql.conf
- Server started with: pg_ctl -o "-p <port> -k <socket dir> -c
listen_addresses=''" start
- psql from the same build, run with -X (no .psqlrc)
- OS: Ubuntu 22.04.5 LTS, Linux 7.0.0-43-generic x86_64, glibc 2.35
- Compiler: gcc (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0
- CPU: AMD Ryzen 9 7940HS (16 threads), 53 GB RAM
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: BUG #19896: transaction_timeout misbehaves after ROLLBACK TO SAVEPOINT and after errors
@ 2026-11-13 09:33 Andrey Borodin <x4mmm@yandex-team.ru>
parent: PG Bug reporting form <noreply@postgresql.org>
0 siblings, 0 replies; 2+ messages in thread
From: Andrey Borodin @ 2026-11-13 09:33 UTC (permalink / raw)
To: nickevanoorschot@gmail.com, PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>
Hi Nicke,
Thank you for the report. Yes, this looks like a bug.
I'm attaching draft fixes for cases 1 and 2. GUC restoration runs after
the subtransaction enters TRANS_ABORT, so IsTransactionState() prevented
the assign hook from updating the timer. Separately, PostgresMain
cancelled the surviving transaction's timer on ERROR. The draft
preserves its original deadline rather than restarting the timeout.
For case 3, I had already noted missing coverage for terminating a
session in "idle in transaction (aborted)" [0]. AbortTransaction()
currently stops the timer and rolls back SET LOCAL. These drafts still
leave that lifetime issue unresolved.
The last patch is particularly tentative. With transaction_timeout = 2s
and idle_in_transaction_session_timeout = 3s, both set at session level,
a full abort leaves neither timer running. Here time is measured from
BEGIN, with an error at t=1s and no further commands:
t=0s t=1s t=2s t=4s
|-----------|-----------|-----------------------|
BEGIN ERROR transaction deadline idle deadline
full abort FATAL if kept active FATAL with 0004
The patch would terminate the session at t=4s: the 3s idle wait starts
after the error at t=1s. Keeping the transaction timer effective would
terminate it at the original t=2s deadline.
We had deliberately suppressed longer or equal timeouts [1]. Should a
fully aborted transaction block be covered by transaction_timeout,
idle_in_transaction_session_timeout, or neither? I lean slightly
towards keeping transaction_timeout effective until the transaction
block ends, including while it is aborted.
First patch with test is for an exploration, maybe some of it will be
useful in future.
Feedback is welcome. I'll keep thinking about this.
Thank you!
Best regards, Andrey Borodin.
[0] https://postgr.es/m/4C30F833-C0F2-4664-B0FF-192FD8AB2E2E%40yandex-team.ru
[1] https://postgr.es/m/A8009945-7FB7-4E75-9239-CCC351F8050C%40yandex-team.ru
Attachments: download all (4 files, .tar.gz)
v1-0001-Add-coverage-for-transaction-timeout-cleanup.patch (application/octet-stream, 8.0K) download
From 2875c4c56f24ee7e8f20b024376ba627ebcab534 Mon Sep 17 00:00:00 2001
From: Andrey Borodin <amborodin@acm.org>
Date: Fri, 13 Nov 2026 11:35:37 +0500
Subject: [PATCH v1 1/4] Add coverage for transaction timeout cleanup
Inspect active deadlines to check GUC restoration and timer ownership
without relying on short timeouts. Exercise recoverable errors twice to
verify that error cleanup preserves the deadline rather than restarting
the timeout.
Also cover an idle timeout in an aborted block with a shorter, inactive
transaction timeout. Reload the settings after aborting, so the test does
not depend on beating the transaction timer.
---
.../injection_points--1.0.sql | 5 +
.../injection_points/regress_injection.c | 17 +++
src/test/modules/test_misc/t/005_timeouts.pl | 140 ++++++++++++++++++
3 files changed, 162 insertions(+)
diff --git a/src/test/modules/injection_points/injection_points--1.0.sql b/src/test/modules/injection_points/injection_points--1.0.sql
index 2efb307f5bf..542c00c989f 100644
--- a/src/test/modules/injection_points/injection_points--1.0.sql
+++ b/src/test/modules/injection_points/injection_points--1.0.sql
@@ -105,6 +105,11 @@ LANGUAGE C STRICT VOLATILE PARALLEL RESTRICTED;
--
-- regress_injection.c functions
--
+CREATE FUNCTION transaction_timeout_deadline()
+RETURNS timestamptz
+AS 'MODULE_PATHNAME'
+LANGUAGE C;
+
CREATE FUNCTION removable_cutoff(rel regclass)
RETURNS xid8
AS 'MODULE_PATHNAME'
diff --git a/src/test/modules/injection_points/regress_injection.c b/src/test/modules/injection_points/regress_injection.c
index ad9c792a551..76d20c9516d 100644
--- a/src/test/modules/injection_points/regress_injection.c
+++ b/src/test/modules/injection_points/regress_injection.c
@@ -21,12 +21,29 @@
#include "replication/logicalproto.h"
#include "storage/procarray.h"
#include "utils/rel.h"
+#include "utils/timeout.h"
+#include "utils/timestamp.h"
#include "utils/xid8.h"
extern PGDLLEXPORT void logicalrep_relation_redirect(const char *name,
const void *private_data,
void *arg);
+/*
+ * Inspect transaction timeout state without waiting for a timer to fire.
+ * Using a long timeout lets tests exercise transaction and GUC cleanup
+ * without depending on how quickly those operations finish.
+ */
+PG_FUNCTION_INFO_V1(transaction_timeout_deadline);
+Datum
+transaction_timeout_deadline(PG_FUNCTION_ARGS)
+{
+ if (!get_timeout_active(TRANSACTION_TIMEOUT))
+ PG_RETURN_NULL();
+
+ PG_RETURN_TIMESTAMPTZ(get_timeout_finish_time(TRANSACTION_TIMEOUT));
+}
+
/*
* Pretend that the publisher asked for a different target relation than it
* really did, so that relations which must never be replication targets can
diff --git a/src/test/modules/test_misc/t/005_timeouts.pl b/src/test/modules/test_misc/t/005_timeouts.pl
index c16b7dbf5e6..b3a89905308 100644
--- a/src/test/modules/test_misc/t/005_timeouts.pl
+++ b/src/test/modules/test_misc/t/005_timeouts.pl
@@ -35,6 +35,76 @@ if (!$node->check_extension('injection_points'))
$node->safe_psql('postgres', 'CREATE EXTENSION injection_points;');
+# Check timer ownership and GUC restoration with long timeouts, so that slow
+# machines cannot expire a timer before reaching the cleanup being tested.
+my $state_session = $node->background_psql('postgres', on_error_stop => 0);
+
+is( $state_session->query_safe(q{
+ BEGIN;
+ SAVEPOINT s;
+ SET LOCAL transaction_timeout = '1d';
+ ROLLBACK TO SAVEPOINT s;
+ SELECT transaction_timeout_deadline() IS NULL;
+}), 't', 'savepoint rollback disables a timeout restored to zero');
+
+is( $state_session->query_safe(q{
+ COMMIT;
+ SELECT transaction_timeout_deadline() IS NULL;
+}), 't', 'transaction timeout does not survive commit');
+
+is( $state_session->query_safe(q{
+ BEGIN;
+ SET LOCAL transaction_timeout = '2d';
+ SELECT transaction_timeout_deadline() >
+ clock_timestamp() + interval '47 hours';
+ COMMIT;
+}), 't', 'next transaction uses its own timeout');
+
+is( $state_session->query_safe(q{
+ BEGIN;
+ SET LOCAL transaction_timeout = '1d';
+ SAVEPOINT s;
+ SET LOCAL transaction_timeout = 0;
+ ROLLBACK TO SAVEPOINT s;
+ SELECT transaction_timeout_deadline() IS NOT NULL;
+ COMMIT;
+}), 't', 'savepoint rollback re-enables a restored positive timeout');
+
+is( $state_session->query_safe(q{
+ DO $$
+ BEGIN
+ PERFORM set_config('transaction_timeout', '1d', true);
+ RAISE EXCEPTION 'rollback';
+ EXCEPTION WHEN raise_exception THEN
+ NULL;
+ END
+ $$;
+ SELECT transaction_timeout_deadline() IS NULL;
+}), 't', 'caught PL/pgSQL exception restores the timeout');
+
+$state_session->query_safe(q{
+ BEGIN;
+ SET LOCAL transaction_timeout = '1d';
+ SELECT transaction_timeout_deadline() AS deadline \gset
+ SAVEPOINT s;
+});
+
+for my $attempt (1 .. 2)
+{
+ my $result = $state_session->query(q{
+ SELECT 1 / 0;
+ ROLLBACK TO SAVEPOINT s;
+ SELECT transaction_timeout_deadline() = :'deadline'::timestamptz;
+ });
+ like($state_session->{stderr}, qr/division by zero/,
+ "recoverable error $attempt occurred");
+ $state_session->{stderr} = '';
+ is($result, 't', "recoverable error $attempt preserves the deadline");
+}
+
+$state_session->query_safe('COMMIT');
+ok($state_session->quit);
+
#
# 1. Test of the transaction timeout
#
@@ -139,4 +209,74 @@ $node->wait_for_log('terminating connection due to idle-session timeout',
ok($psql_session->quit);
+# A reload can set transaction_timeout while a block is already aborted,
+# without arming its timer. This lets us check the idle timeout independently
+# of how quickly a transaction can be made to fail.
+$node->safe_psql('postgres', q{
+ SELECT injection_points_detach('transaction-timeout');
+});
+
+# Keep a control connection with a session-level setting that overrides the
+# shorter transaction timeout we will load from the configuration file.
+my $control_session = $node->background_psql('postgres',
+ timeout => 2 * $PostgreSQL::Test::Utils::timeout_default);
+$control_session->query_safe('SET transaction_timeout = 0');
+
+$psql_session = $node->background_psql('postgres',
+ on_error_stop => 0,
+ timeout => 2 * $PostgreSQL::Test::Utils::timeout_default);
+my $pid = $psql_session->query_safe('SELECT pg_backend_pid()');
+# Attach the callback's shared memory before releasing transaction resources.
+$psql_session->query_safe(q{
+ SELECT injection_points_load('idle-in-transaction-session-timeout');
+});
+$psql_session->query('BEGIN; SELECT 1 / 0;');
+like($psql_session->{stderr}, qr/division by zero/,
+ 'transaction is aborted before enabling the timeouts');
+$psql_session->{stderr} = '';
+
+$log_offset = -s $node->logfile;
+$node->append_conf('postgresql.conf', q{
+transaction_timeout = '1ms'
+idle_in_transaction_session_timeout = '10ms'
+});
+$node->reload;
+
+# Send commands until the backend processes the reload and reaches the idle
+# timeout injection point. Do not wait for a response after sending a command:
+# the timer could fire before the client receives it. The injection point
+# prevents FATAL from racing with the next command.
+my $idle_timeout_fired = 0;
+for my $attempt (1 .. $PostgreSQL::Test::Utils::timeout_default * 10)
+{
+ if ($control_session->query_safe(qq{
+ SELECT EXISTS (SELECT FROM pg_stat_activity WHERE pid = $pid
+ AND wait_event = 'idle-in-transaction-session-timeout');
+ }) eq 't')
+ {
+ $idle_timeout_fired = 1;
+ last;
+ }
+ $psql_session->{stdin} .= "SELECT 1;\n";
+ $psql_session->{run}->pump_nb;
+ usleep(100_000);
+}
+ok($idle_timeout_fired, 'inactive transaction timer does not suppress idle timeout');
+
+if ($idle_timeout_fired)
+{
+ $control_session->query_safe(q{
+ SELECT injection_points_wakeup('idle-in-transaction-session-timeout');
+ });
+ $node->wait_for_log(
+ qr/\[$pid\].*terminating connection due to idle-in-transaction timeout/,
+ $log_offset);
+}
+
+# A command may still be queued behind the injection point, so psql can exit
+# with a connection-lost error. The server log above checks the FATAL reason.
+$psql_session->{stdin} .= "\\q\n";
+$psql_session->{run}->finish;
+ok($control_session->quit);
+
done_testing();
base-commit: 87524d0dd8470819ce8aa3708a9bf3fa3f1d34fd
--
2.50.1 (Apple Git-155)
=
v1-0002-Fix-transaction-timeout-cleanup-during-GUC-restor.patch (application/octet-stream, 3.4K) download
From 8a36bd692b9fb2ec6a514ad42f94bce06d8148f0 Mon Sep 17 00:00:00 2001
From: Andrey Borodin <amborodin@acm.org>
Date: Fri, 13 Nov 2026 11:37:15 +0500
Subject: [PATCH v1 2/4] Fix transaction timeout cleanup during GUC restoration
An aborting subtransaction is not in IsTransactionState(), so the assign
hook failed to disable a timer when GUC restoration reset the timeout to
zero. Transaction end then skipped disabling it too, allowing the timer
to affect the next transaction. Restoring a positive value after disabling
the timer in a savepoint also failed to re-enable it.
Allow the assign hook to update the parent's timer during subtransaction
abort, but do not start a timer during top-level transaction cleanup.
Check actual timer activity when disabling it at transaction end.
Back-patch to v17, where transaction_timeout was introduced.
Reported-by: Nicke van Oorschot <nickevanoorschot@gmail.com>
Discussion: https://postgr.es/m/19896-555a806fe6352359@postgresql.org
---
src/backend/access/transam/xact.c | 6 +++---
src/backend/tcop/postgres.c | 18 ++++++++++--------
2 files changed, 13 insertions(+), 11 deletions(-)
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index 1227e3fbe24..4599aad5a90 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -2410,7 +2410,7 @@ CommitTransaction(void)
s->parallelChildXact = false; /* should be false already */
/* Disable transaction timeout */
- if (TransactionTimeout > 0)
+ if (get_timeout_active(TRANSACTION_TIMEOUT))
disable_timeout(TRANSACTION_TIMEOUT, false);
if (!is_parallel_worker)
@@ -2694,7 +2694,7 @@ PrepareTransaction(void)
s->state = TRANS_PREPARE;
/* Disable transaction timeout */
- if (TransactionTimeout > 0)
+ if (get_timeout_active(TRANSACTION_TIMEOUT))
disable_timeout(TRANSACTION_TIMEOUT, false);
prepared_at = GetCurrentTimestamp();
@@ -2877,7 +2877,7 @@ AbortTransaction(void)
HOLD_INTERRUPTS();
/* Disable transaction timeout */
- if (TransactionTimeout > 0)
+ if (get_timeout_active(TRANSACTION_TIMEOUT))
disable_timeout(TRANSACTION_TIMEOUT, false);
/* Make sure we have a valid memory context and resource owner */
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index d6761aeea9f..e3bceac8379 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -3769,17 +3769,19 @@ check_log_stats(bool *newval, void **extra, GucSource source)
void
assign_transaction_timeout(int newval, void *extra)
{
- if (IsTransactionState())
+ /*
+ * GUC restoration runs after the current subtransaction has entered
+ * TRANS_ABORT, but its parent's transaction timeout must still be
+ * updated. Do not enable a timer while restoring GUCs at top-level
+ * transaction end.
+ */
+ if (newval > 0 && (IsTransactionState() || IsSubTransaction()))
{
- /*
- * If transaction_timeout GUC has changed within the transaction block
- * enable or disable the timer correspondingly.
- */
- if (newval > 0 && !get_timeout_active(TRANSACTION_TIMEOUT))
+ if (!get_timeout_active(TRANSACTION_TIMEOUT))
enable_timeout_after(TRANSACTION_TIMEOUT, newval);
- else if (newval <= 0 && get_timeout_active(TRANSACTION_TIMEOUT))
- disable_timeout(TRANSACTION_TIMEOUT, false);
}
+ else if (newval <= 0 && get_timeout_active(TRANSACTION_TIMEOUT))
+ disable_timeout(TRANSACTION_TIMEOUT, false);
}
/*
--
2.50.1 (Apple Git-155)
=
v1-0003-Preserve-transaction-timeout-across-recoverable-e.patch (application/octet-stream, 3.3K) download
From 5517189c28bf447e46471f4c5be48d3eead2c525 Mon Sep 17 00:00:00 2001
From: Andrey Borodin <amborodin@acm.org>
Date: Fri, 13 Nov 2026 11:37:49 +0500
Subject: [PATCH v1 3/4] Preserve transaction timeout across recoverable errors
PostgresMain disabled all timeouts on ERROR, including the timer of a
transaction that can continue after ROLLBACK TO SAVEPOINT. With no GUC
change to invoke the assign hook, that transaction could then run without
its timeout.
Save the active timer's deadline before disabling the timeouts and rearm
it at that same deadline. AbortTransaction still disables the timer when
the whole transaction aborts. Keeping the deadline avoids granting a new
full timeout after every recoverable error.
Back-patch to v17, where transaction_timeout was introduced.
Reported-by: Nicke van Oorschot <nickevanoorschot@gmail.com>
Discussion: https://postgr.es/m/19896-555a806fe6352359@postgresql.org
---
src/backend/tcop/postgres.c | 26 ++++++++++++++++++--------
1 file changed, 18 insertions(+), 8 deletions(-)
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index e3bceac8379..20327194532 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -4571,6 +4571,9 @@ PostgresMain(const char *dbname, const char *username)
if (sigsetjmp(local_sigjmp_buf, 1) != 0)
{
+ bool transaction_timeout_active;
+ TimestampTz transaction_timeout_finish = 0;
+
/*
* NOTE: if you are tempted to add more code in this if-block,
* consider the high probability that it should be in
@@ -4587,16 +4590,23 @@ PostgresMain(const char *dbname, const char *username)
/*
* Forget any pending QueryCancel request, since we're returning to
- * the idle loop anyway, and cancel any active timeout requests. (In
- * future we might want to allow some timeout requests to survive, but
- * at minimum it'd be necessary to do reschedule_timeouts(), in case
- * we got here because of a query cancel interrupting the SIGALRM
- * interrupt handler.) Note in particular that we must clear the
- * statement and lock timeout indicators, to prevent any future plain
- * query cancels from being misreported as timeouts in case we're
- * forgetting a timeout cancel.
+ * the idle loop anyway, and cancel active timeout requests except for
+ * the transaction timeout. A transaction can survive this error
+ * through ROLLBACK TO SAVEPOINT, so preserve its existing deadline.
+ * AbortTransaction will disable the timer if the whole transaction is
+ * being aborted. Rearming it also repairs a possibly lost SIGALRM
+ * interrupt, if query cancel interrupted the SIGALRM handler.
+ *
+ * Clear the statement and lock timeout indicators, to prevent future
+ * plain query cancels from being misreported as timeouts in case
+ * we're forgetting a timeout cancel.
*/
+ transaction_timeout_active = get_timeout_active(TRANSACTION_TIMEOUT);
+ if (transaction_timeout_active)
+ transaction_timeout_finish = get_timeout_finish_time(TRANSACTION_TIMEOUT);
disable_all_timeouts(false); /* do first to avoid race condition */
+ if (transaction_timeout_active)
+ enable_timeout_at(TRANSACTION_TIMEOUT, transaction_timeout_finish);
QueryCancelPending = false;
idle_in_transaction_timeout_enabled = false;
idle_session_timeout_enabled = false;
--
2.50.1 (Apple Git-155)
=
v1-0004-Do-not-suppress-idle-timeout-for-an-inactive-tran.patch (application/octet-stream, 2.1K) download
From 4dc07be7417bfd6edc98a0f2766308d71d6b1084 Mon Sep 17 00:00:00 2001
From: Andrey Borodin <amborodin@acm.org>
Date: Fri, 13 Nov 2026 11:37:53 +0500
Subject: [PATCH v1 4/4] Do not suppress idle timeout for an inactive
transaction timer
The idle-in-transaction timeout was skipped when transaction_timeout had
a smaller positive value, even if its timer had already been disabled by
AbortTransaction. An aborted block could then remain connected without
either timeout taking effect.
Only skip the idle timeout when the transaction timer is actually active.
This preserves the existing behaviour of ending transaction_timeout at
AbortTransaction rather than extending it to an aborted SQL block.
Back-patch to v17, where transaction_timeout was introduced.
Discussion: https://postgr.es/m/19896-555a806fe6352359@postgresql.org
---
src/backend/tcop/postgres.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index 20327194532..ad61e5bef83 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -4760,7 +4760,8 @@ PostgresMain(const char *dbname, const char *username)
/* Start the idle-in-transaction timer */
if (IdleInTransactionSessionTimeout > 0
- && (IdleInTransactionSessionTimeout < TransactionTimeout || TransactionTimeout == 0))
+ && (IdleInTransactionSessionTimeout < TransactionTimeout ||
+ !get_timeout_active(TRANSACTION_TIMEOUT)))
{
idle_in_transaction_timeout_enabled = true;
enable_timeout_after(IDLE_IN_TRANSACTION_SESSION_TIMEOUT,
@@ -4774,7 +4775,8 @@ PostgresMain(const char *dbname, const char *username)
/* Start the idle-in-transaction timer */
if (IdleInTransactionSessionTimeout > 0
- && (IdleInTransactionSessionTimeout < TransactionTimeout || TransactionTimeout == 0))
+ && (IdleInTransactionSessionTimeout < TransactionTimeout ||
+ !get_timeout_active(TRANSACTION_TIMEOUT)))
{
idle_in_transaction_timeout_enabled = true;
enable_timeout_after(IDLE_IN_TRANSACTION_SESSION_TIMEOUT,
--
2.50.1 (Apple Git-155)
=
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2026-11-13 09:33 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-11-12 12:41 BUG #19896: transaction_timeout misbehaves after ROLLBACK TO SAVEPOINT and after errors PG Bug reporting form <noreply@postgresql.org>
2026-11-13 09:33 ` Andrey Borodin <x4mmm@yandex-team.ru>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox