Troubleshooting guide
PostgreSQL idle in transaction: find and fix it
A session that opened a transaction and then went quiet can quietly damage the whole database. It holds locks, and it stops vacuum from cleaning up.
3 min read10 sectionsUpdated
On this page
-- Longest idle-in-transaction sessions
SELECT pid, usename, now() - state_change AS idle_for, now() - xact_start AS tx_age, left(query, 80) AS last_query
FROM pg_stat_activity WHERE state LIKE 'idle in transaction%'
ORDER BY xact_start LIMIT 10;
-- Who is blocked, and by whom
SELECT pid, pg_blocking_pids(pid) AS blocked_by, left(query, 60)
FROM pg_stat_activity WHERE cardinality(pg_blocking_pids(pid)) > 0;Fixes
Keep transactions short: do remote calls and any slow computation before BEGIN or after COMMIT. Make rollback automatic with context managers. Set a limit:
ALTER DATABASE app SET idle_in_transaction_session_timeout = '60s';
To end a stuck session immediately, use SELECT pg_terminate_backend(pid);. The last query shown is the last one that ran, so it tells you where in the code the transaction was left open.
The hidden cost: vacuum
Postgres keeps old row versions while any open transaction might still need them. One session that stays open for six hours means dead rows from those six hours cannot be removed from any table. On a busy table, this shows up as growing size and slower scans long after the session ended. If bloat appears from nowhere, look for a long transaction in that period. Check also for abandoned replication slots and long-running pg_dump jobs, because those hold the horizon back in the same way.
Catch it automatically
Alert on the age of the oldest transaction, not the count of sessions. In APM, a span that holds a database transaction while calling an external service is easy to see on the trace. PostgreSQL monitoring charts oldest transaction age over time.
Are idle connections a problem?
Plain idle connections cost memory but hold no locks. Idle in transaction is the harmful state.
What timeout should I set?
A minute is a sensible starting point for web applications. Reporting jobs may need a role with a different setting.
Does this cause deadlocks?
It can contribute by holding locks for longer. It more commonly causes lock queues.
Related reading
- PostgreSQL replication lag: causes and how to fix it: Diagnose PostgreSQL replication lag: use pg_stat_replication to tell send, write and replay delays apart, and fix slow replicas, long queries and WAL pile-ups.
- PostgreSQL "too many clients already": how to fix it: Fix PostgreSQL FATAL: sorry, too many clients already. See who holds connections, stop leaks, add PgBouncer and size max_connections the right way.
- MySQL "Lock wait timeout exceeded": how to fix it: Fix MySQL error 1205 Lock wait timeout exceeded: find the blocking transaction with the performance schema, shorten transactions and index your updates.
- PostgreSQL high CPU usage: how to find the cause: Diagnose PostgreSQL high CPU: use pg_stat_statements to rank queries by total time, spot missing indexes and stale statistics, and read EXPLAIN properly.
- PostgreSQL connection refused: how to fix it: Fix PostgreSQL connection refused on port 5432: server not running, listen_addresses, pg_hba.conf, Docker hostnames and firewalls, with commands for every case.
- Kubernetes readiness probe failed: how to fix it: Fix Kubernetes readiness and liveness probe failures: slow startup, wrong ports, overloaded health endpoints and dependency checks that cause cascading outages.
Watch your own site and services with VitalRix
Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.