Skip to content

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

Key points

  • They hold locks and block other queries.
  • They hold back vacuum, leading to table bloat.
  • The cause is in application code: work done between BEGIN and COMMIT.
  • A server timeout is a cheap safety net.

What it means

The session ran BEGIN, perhaps some queries, and is now waiting for the client to send the next statement. It is doing nothing, and yet everything it touched remains locked, and the oldest visible snapshot cannot advance.

Common causes

  • A network or HTTP call inside a transaction, waiting for a third-party API.
  • An ORM that opens a transaction per request and holds it during view rendering.
  • An exception path that never rolls back.
  • A human who left a psql session open with an uncommitted BEGIN.

Diagnose it

Sort by transaction age, and identify which other sessions it blocks.

bash
-- 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.

Frequently asked questions

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.

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.