The idea · Lab 3.5.4A connection that opens a transaction and then goes idle — an application that crashed, a developer who stepped away mid-debugging — holds whatever locks that transaction acquired for as long as the connection stays open, blocking anything else that needs them. idle_in_transaction_session_timeout terminates a connection that has been idle inside an open transaction for too long, genuinely closing it rather than leaving it to block the rest of the system indefinitely. statement_timeout and lock_timeout apply the same idea to two narrower situations: a single query running too long, or a single statement waiting too long for a lock someone else is holding.
A deadlock is a different, structural problem no timeout alone prevents: two sessions each hold a lock the other one needs, and neither can proceed — without detection, they would simply wait forever. deadlock_timeout controls how long PostgreSQL waits before actively checking for this specific situation; once found, it always resolves a deadlock by forcibly rolling back one of the two transactions as the "victim," letting the other proceed. This lab reproduces all four scenarios for real — including using psql's own \! shell-escape to create genuine wall-clock idle time inside a real open transaction, since a script sending commands back-to-back would never trigger an idle timeout at all.
idle_in_transaction_session_timeout: Real Idle Time, Genuinely Terminated
SET idle_in_transaction_session_timeout = '5s';
BEGIN;
\! sleep 7 -- psql's own shell-escape creates real wall-clock idle time
SELECT 1;
FATAL: terminating connection due to idle-in-transaction timeout
server closed the connection unexpectedly
A real 7-second idle gap, created with psql's own \! shell-escape rather than a script sending commands back-to-back (which would never actually go idle) — and the connection is genuinely terminated by the server itself.
statement_timeout: A Real Long-Running Query, Genuinely Cancelled
SET statement_timeout = '1s';
SELECT pg_sleep(10);
ERROR: canceling statement due to statement timeout
lock_timeout: Real Contention, Genuinely Cancelled
-- Session A holds a real lock for 5 real seconds:
BEGIN; LOCK TABLE sql_lock_demo IN ACCESS EXCLUSIVE MODE; \! sleep 5; COMMIT;
-- Session B, started while A still holds the lock:
SET lock_timeout = '2s';
SELECT * FROM sql_lock_demo WHERE id = 1;
ERROR: canceling statement due to lock timeout
Session B genuinely waited on the real lock Session A was holding, and was genuinely cancelled after 2 seconds — well before Session A's 5-second hold even finished.
A Real Deadlock, Detected and Reported
-- Session A: UPDATE table_a, then (after a delay) UPDATE table_b
-- Session B, concurrently: UPDATE table_b, then UPDATE table_a (opposite order)
ERROR: deadlock detected
DETAIL: Process 138 waits for ShareLock on transaction 897; blocked by process 141.
Process 141 waits for ShareLock on transaction 896; blocked by process 138.
HINT: See server log for query details.
CONTEXT: while updating tuple (0,1) in relation "sql_deadlock_b"
ROLLBACK
Two real sessions, real process IDs, a genuine circular wait — PostgreSQL detected it and rolled back one transaction (the "victim") automatically, letting the other one commit successfully.
Key concepts
idle_in_transaction_session_timeout
Terminates a database session that has an open transaction but is not currently running any query, once that idle period exceeds the configured duration. This is different from statement_timeout, which only bounds an actively-running query — idle_in_transaction_session_timeout specifically catches a transaction that was opened and then simply abandoned, holding its locks with nothing happening at all.
deadlock_timeout
How long PostgreSQL waits while a session appears blocked on a lock before actively running its (relatively expensive) deadlock detection check. A short deadlock_timeout catches real deadlocks faster but spends more CPU checking for them on every ordinary lock wait; a longer one is cheaper per-wait but leaves a genuine deadlock uncaught for longer. It defaults to 1 second, rarely needing to change.
What to remember
- idle_in_transaction_session_timeout terminates a connection idle inside an open transaction — different from statement_timeout, which only bounds an actively-running query
- lock_timeout bounds how long a statement waits for a lock someone else holds, independent of how long the statement itself would otherwise take to run
- A deadlock is a structural circular wait no single timeout prevents — deadlock_timeout controls how promptly PostgreSQL actively checks for one before it would otherwise wait indefinitely
- PostgreSQL always resolves a detected deadlock by rolling back one transaction (the "victim") and letting the other proceed — never by waiting the situation out
- Testing an idle-based timeout requires genuine wall-clock idle time inside one continuous session — a script sending commands back-to-back never actually goes idle, which is exactly why this lab used psql's own
\! shell-escape