Why does a session that is running no query at all bring a busy Postgres database to a halt? Because "idle in transaction" means the client sent BEGIN, ran something, and then stopped talking without sending COMMIT or ROLLBACK. Every lock that transaction took is still held. Postgres cannot release them on its own, because it has no way to know whether the client will come back with one more statement in 5 milliseconds or never.
The eleven checks below go in the order you need them during an incident: understand the mechanism, find the holder, measure the damage, end it, then make sure it does not happen again.
How a session doing nothing blocks one doing everything
Check 1: look at the lock, not the CPU. A database blocked by an idle transaction often shows low CPU and low I/O while request latency climbs. Sessions are waiting, not working. In pg_stat_activity, the victims show wait_event_type = 'Lock', and the culprit shows state = 'idle in transaction' with no wait event at all.
Check 2: suspect the lock queue. Row locks are the obvious case: the idle transaction updated a row in orders, and every later update to that row waits. The nastier case involves a schema change. A transaction that merely read from a table holds an ACCESS SHARE lock on it until commit.
A migration running ALTER TABLE then requests ACCESS EXCLUSIVE, which conflicts with ACCESS SHARE, so the migration waits. Postgres grants locks in queue order, so every ordinary SELECT that arrives after the migration now waits behind it. One idle reader, one migration, and within seconds the whole table is unreadable. The original idle session may be a psql window someone left open at lunch.
The lock mode names matter here because they decide who blocks whom. ACCESS SHARE conflicts only with ACCESS EXCLUSIVE, which is why it is harmless until a DDL statement shows up, and why these incidents tend to happen during deploys.
Finding the holder in under a minute
Check 3: ask Postgres who is blocking whom. Since version 9.6, pg_blocking_pids() returns the process IDs blocking a given backend. This query lists the waiters, their blockers and how long each blocker's transaction has been open:
SELECT w.pid AS waiting_pid,
w.query AS waiting_query,
b.pid AS blocking_pid,
b.state AS blocking_state,
now() - b.xact_start AS blocker_xact_age,
now() - b.state_change AS blocker_idle_for,
b.application_name, b.client_addr
FROM pg_stat_activity w
JOIN LATERAL unnest(pg_blocking_pids(w.pid)) AS bp(pid) ON true
JOIN pg_stat_activity b ON b.pid = bp.pid
ORDER BY blocker_xact_age DESC;
Check 4: read the blocker's last query, carefully. The query column for an idle in transaction session shows the last statement it ran, not something it is running now. That last statement is your best clue to where in the application the transaction was opened. Combine it with application_name and client_addr, which is why setting application_name in every service's connection string pays for itself the first time this happens.
A chain is common. Session A is idle in transaction, B waits on A, and C through Z wait on B. The query returns B as a blocker of C, so sort by transaction age and look for the oldest session whose own state is idle in transaction: that is usually the root.
What an idle transaction costs beyond locks
Check 5: count the connection slots it pins. Every idle transaction holds a backend and, behind a pooler in transaction mode, a server connection. The arithmetic follows Little's law:
connections_held = arrival_rate * (query_time + idle_time_inside_txn)
dead_tuples_kept ≈ update_rate * age_of_oldest_open_transaction
Take a service handling 200 requests per second, where each request runs 15 ms of SQL inside a transaction. Without idle time it holds about 200 x 0.015 = 3 connections. Now suppose the code calls a payment API for 120 ms between the first query and the commit. Connections held become 200 x 0.135 = 27. A pool sized at 20 saturates, new requests queue in the application, and the database looks idle while the service times out.
Check 6: check the vacuum horizon. A transaction that has written anything, or that runs under REPEATABLE READ, holds back the oldest transaction ID that vacuum must respect. Look at backend_xid and backend_xmin in pg_stat_activity. While that session lives, dead rows created anywhere in the database after its start cannot be removed.
On a table taking 1,000 updates per second, a transaction left open for 6 hours keeps roughly 21.6 million dead row versions around, and indexes grow with them. The bloat stays after the session ends, until vacuum catches up.
Where idle transactions come from
Check 7: look for network calls inside transactions. The most common source is application code that opens a transaction, does a query, then calls something slow: an HTTP API, a message broker, an S3 upload, a template render. Django with ATOMIC_REQUESTS enabled wraps the whole view in a transaction, so any external call in the view runs while Postgres sees idle in transaction. Spring's @Transactional on a service method that also calls a remote client does the same.
Check 8: look for drivers that open transactions implicitly. Python's psycopg2 follows the DB-API convention: autocommit is off by default, so the first execute() opens a transaction that stays open until commit() or rollback(). A script that runs a SELECT, then sleeps or waits for user input, sits idle in transaction the whole time.
Long-lived worker processes and notebooks are frequent offenders. Exception paths are another: code that catches an error and continues without calling rollback() leaves the session in idle in transaction (aborted), still holding everything it locked before the error.
Timeouts at three layers, and which one to trust
Check 9: set a server-side limit. idle_in_transaction_session_timeout, available since Postgres 9.6, terminates any session that stays idle inside a transaction longer than the value. A setting of 30 to 60 seconds for application roles is a reasonable start; set it per role so that admin and migration roles can keep their own values. Postgres 14 added idle_session_timeout for sessions idle outside a transaction, and Postgres 17 added transaction_timeout, which caps total transaction duration whether the session is busy or idle.
statement_timeout does not help with this problem. It limits a single running statement, and an idle transaction has no running statement.
Poolers add a second layer. PgBouncer has idle_transaction_timeout, which closes a client connection that holds a server connection idle in transaction for too long, and it acts before Postgres sees anything. The third layer is the application: a transaction scope in code that is guaranteed to end, through context managers in Python, try/finally blocks or framework transaction templates. The server timeout is the safety net; application code is where the fix belongs, because a terminated session surfaces as an error the application must handle anyway.
Ending the session without making things worse
Check 10: terminate, do not cancel. pg_cancel_backend(pid) cancels the current query, and an idle transaction has none, so it does nothing useful. pg_terminate_backend(pid) ends the session; the open transaction rolls back, its locks are released and every waiter proceeds within milliseconds. Confirm what the session did before you terminate it: if it updated 50,000 rows, the rollback discards that work, which is usually the right outcome for an abandoned transaction but not for a half-finished data fix someone is still running by hand.
Check 11: cancel the migration first if one is queued. If the pile-up is a lock queue behind ALTER TABLE, cancelling the migration immediately frees the readers queued behind it, while the idle session can be handled a minute later. Run migrations with lock_timeout set to a few seconds so they give up rather than becoming the head of a queue. A migration that fails after 5 seconds and retries 3 times costs nothing; one that waits 4 minutes at the head of a queue costs an outage.
A deploy-day incident, minute by minute (illustrative)
A composite example shows how the checks fit together. A Lisbon fintech team deploys at 15:00. The migration adds a column with a default to a 40 GB transactions table. Three minutes later, checkout latency jumps from 80 ms to 30 seconds. The on-call engineer sees 190 sessions waiting on locks, finds the migration waiting on a single analytics session idle in transaction for 2 hours 14 minutes, started from a notebook.
She cancels the migration, and checkout recovers in 4 seconds. She then terminates the notebook session, adds idle_in_transaction_session_timeout = '60s' for the analytics role and lock_timeout = '5s' to the migration runner, and reruns the migration at 15:20 without incident. Total customer impact: about 6 minutes, nearly all of it spent finding the right session.
The review afterwards found two things worth copying. The notebook had been opened with psycopg2 defaults, so a single exploratory SELECT had started the transaction that sat open all afternoon. And the runbook had told on-call to look for long-running queries, which hid the culprit, because an idle session has no running query and sorts to the bottom of any list ordered by query duration. The team rewrote the runbook to sort by transaction age.
Questions engineers ask about idle in transaction sessions
Does a session in the idle in transaction (aborted) state still hold locks? Yes. The transaction failed on an error but has not been rolled back, so every lock taken before the error stays held until the client sends ROLLBACK or the session ends.
Does idle_in_transaction_session_timeout clean up prepared transactions? No. A transaction prepared with PREPARE TRANSACTION is detached from any session and survives restarts. Check pg_prepared_xacts and resolve old entries with COMMIT PREPARED or ROLLBACK PREPARED.
Can the timeout be set only for the application role? Yes. ALTER ROLE app_user SET idle_in_transaction_session_timeout = '30s' applies it to new sessions of that role, leaving migration and admin roles on their own settings.
Is a read-only transaction harmless if it sits idle? Not fully. It still holds ACCESS SHARE locks on every table it read, which blocks ALTER TABLE, and under REPEATABLE READ it keeps its snapshot and holds back vacuum for its whole life.
Timeouts and pg_blocking_pids handle the incident, but they leave a design question that each team answers differently: should a transaction ever be allowed to wait on something outside the database, or should every external call move before BEGIN or after COMMIT, even when that makes the code harder to read?