From c6dda1f48e573cc60fe25d8d470a05bfabed0252 Mon Sep 17 00:00:00 2001 From: Robert Haas Date: Wed, 16 Mar 2016 11:30:45 -0400 Subject: Add idle_in_transaction_session_timeout. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Vik Fearing, reviewed by Stéphane Schildknecht and me, and revised slightly by me. --- src/backend/tcop/postgres.c | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) (limited to 'src/backend/tcop/postgres.c') diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index 115166b6f6a..68811f1f217 100644 --- a/src/backend/tcop/postgres.c +++ b/src/backend/tcop/postgres.c @@ -2978,6 +2978,18 @@ ProcessInterrupts(void) } } + if (IdleInTransactionSessionTimeoutPending) + { + /* Has the timeout setting changed since last we looked? */ + if (IdleInTransactionSessionTimeout > 0) + ereport(FATAL, + (errcode(ERRCODE_IDLE_IN_TRANSACTION_SESSION_TIMEOUT), + errmsg("terminating connection due to idle-in-transaction timeout"))); + else + IdleInTransactionSessionTimeoutPending = false; + + } + if (ParallelMessagePending) HandleParallelMessages(); } @@ -3553,6 +3565,7 @@ PostgresMain(int argc, char *argv[], StringInfoData input_message; sigjmp_buf local_sigjmp_buf; volatile bool send_ready_for_query = true; + bool disable_idle_in_transaction_timeout = false; /* Initialize startup process environment if necessary. */ if (!IsUnderPostmaster) @@ -3942,11 +3955,27 @@ PostgresMain(int argc, char *argv[], { set_ps_display("idle in transaction (aborted)", false); pgstat_report_activity(STATE_IDLEINTRANSACTION_ABORTED, NULL); + + /* Start the idle-in-transaction timer */ + if (IdleInTransactionSessionTimeout > 0) + { + disable_idle_in_transaction_timeout = true; + enable_timeout_after(IDLE_IN_TRANSACTION_SESSION_TIMEOUT, + IdleInTransactionSessionTimeout); + } } else if (IsTransactionOrTransactionBlock()) { set_ps_display("idle in transaction", false); pgstat_report_activity(STATE_IDLEINTRANSACTION, NULL); + + /* Start the idle-in-transaction timer */ + if (IdleInTransactionSessionTimeout > 0) + { + disable_idle_in_transaction_timeout = true; + enable_timeout_after(IDLE_IN_TRANSACTION_SESSION_TIMEOUT, + IdleInTransactionSessionTimeout); + } } else { @@ -3987,7 +4016,16 @@ PostgresMain(int argc, char *argv[], DoingCommandRead = false; /* - * (5) check for any other interesting events that happened while we + * (5) turn off the idle-in-transaction timeout + */ + if (disable_idle_in_transaction_timeout) + { + disable_timeout(IDLE_IN_TRANSACTION_SESSION_TIMEOUT, false); + disable_idle_in_transaction_timeout = false; + } + + /* + * (6) check for any other interesting events that happened while we * slept. */ if (got_SIGHUP) @@ -3997,7 +4035,7 @@ PostgresMain(int argc, char *argv[], } /* - * (6) process the command. But ignore it if we're skipping till + * (7) process the command. But ignore it if we're skipping till * Sync. */ if (ignore_till_sync && firstchar != EOF) -- cgit v1.2.3