@@ -2978,6 +2978,18 @@ ProcessInterrupts(void)
29782978 }
29792979 }
29802980
2981+ if (IdleInTransactionSessionTimeoutPending )
2982+ {
2983+ /* Has the timeout setting changed since last we looked? */
2984+ if (IdleInTransactionSessionTimeout > 0 )
2985+ ereport (FATAL ,
2986+ (errcode (ERRCODE_IDLE_IN_TRANSACTION_SESSION_TIMEOUT ),
2987+ errmsg ("terminating connection due to idle-in-transaction timeout" )));
2988+ else
2989+ IdleInTransactionSessionTimeoutPending = false;
2990+
2991+ }
2992+
29812993 if (ParallelMessagePending )
29822994 HandleParallelMessages ();
29832995}
@@ -3553,6 +3565,7 @@ PostgresMain(int argc, char *argv[],
35533565 StringInfoData input_message ;
35543566 sigjmp_buf local_sigjmp_buf ;
35553567 volatile bool send_ready_for_query = true;
3568+ bool disable_idle_in_transaction_timeout = false;
35563569
35573570 /* Initialize startup process environment if necessary. */
35583571 if (!IsUnderPostmaster )
@@ -3942,11 +3955,27 @@ PostgresMain(int argc, char *argv[],
39423955 {
39433956 set_ps_display ("idle in transaction (aborted)" , false);
39443957 pgstat_report_activity (STATE_IDLEINTRANSACTION_ABORTED , NULL );
3958+
3959+ /* Start the idle-in-transaction timer */
3960+ if (IdleInTransactionSessionTimeout > 0 )
3961+ {
3962+ disable_idle_in_transaction_timeout = true;
3963+ enable_timeout_after (IDLE_IN_TRANSACTION_SESSION_TIMEOUT ,
3964+ IdleInTransactionSessionTimeout );
3965+ }
39453966 }
39463967 else if (IsTransactionOrTransactionBlock ())
39473968 {
39483969 set_ps_display ("idle in transaction" , false);
39493970 pgstat_report_activity (STATE_IDLEINTRANSACTION , NULL );
3971+
3972+ /* Start the idle-in-transaction timer */
3973+ if (IdleInTransactionSessionTimeout > 0 )
3974+ {
3975+ disable_idle_in_transaction_timeout = true;
3976+ enable_timeout_after (IDLE_IN_TRANSACTION_SESSION_TIMEOUT ,
3977+ IdleInTransactionSessionTimeout );
3978+ }
39503979 }
39513980 else
39523981 {
@@ -3987,7 +4016,16 @@ PostgresMain(int argc, char *argv[],
39874016 DoingCommandRead = false;
39884017
39894018 /*
3990- * (5) check for any other interesting events that happened while we
4019+ * (5) turn off the idle-in-transaction timeout
4020+ */
4021+ if (disable_idle_in_transaction_timeout )
4022+ {
4023+ disable_timeout (IDLE_IN_TRANSACTION_SESSION_TIMEOUT , false);
4024+ disable_idle_in_transaction_timeout = false;
4025+ }
4026+
4027+ /*
4028+ * (6) check for any other interesting events that happened while we
39914029 * slept.
39924030 */
39934031 if (got_SIGHUP )
@@ -3997,7 +4035,7 @@ PostgresMain(int argc, char *argv[],
39974035 }
39984036
39994037 /*
4000- * (6 ) process the command. But ignore it if we're skipping till
4038+ * (7 ) process the command. But ignore it if we're skipping till
40014039 * Sync.
40024040 */
40034041 if (ignore_till_sync && firstchar != EOF )
0 commit comments