// SQL snippet

Find idle-in-transaction sessions in PostgreSQL

A session idle in transaction is doing nothing and still holding its locks and its snapshot. Here is how to find them.

Main statement
PostgreSQL

The task

A session in state idle in transaction has begun a transaction and issued no statement since state_change. Typically it is an application that opened a transaction, did part of its work, and then paused (waiting on a user, a webhook, a crashed worker). It holds every lock it took, pins the vacuum horizon with its snapshot, and can silently wedge other sessions behind lock queues.

The snippet

Written for PostgreSQL.

PostgreSQL
SELECT pid, usename, datname, application_name, client_addr,
       now() - xact_start AS xact_age,
       now() - state_change AS idle_for,
       LEFT(query, 120) AS last_query,
       backend_xid IS NOT NULL AS has_write_transaction
FROM pg_stat_activity
WHERE state = 'idle in transaction'
ORDER BY state_change
LIMIT 25;

Fonctionnement

state = 'idle in transaction' isolates sessions between statements inside an open transaction; idle in transaction (aborted) means an error occurred and the transaction still awaits a rollback. Ordering by state_change puts the longest-idle first. backend_xid is assigned only when a transaction first writes, so has_write_transaction = true means it holds row locks on what it changed and terminating it rolls that work back. A false value means a read-only transaction, which still holds a snapshot and any table locks its SELECTs took. The standing fix is idle_in_transaction_session_timeout, which ends these sessions automatically.

Variations

Terminate sessions idle in transaction for over 30 minutes

SELECT pid, usename, now() - state_change AS idle_for,
       pg_terminate_backend(pid) AS terminated
FROM pg_stat_activity
WHERE state = 'idle in transaction'
  AND now() - state_change > interval '30 minutes';

Questions that come up

Is it safe to kill idle-in-transaction sessions?

Usually. Terminating rolls back whatever the transaction changed and not yet committed, which is work the application believed was in progress. The application gets a connection error on its next statement and has to handle it. That is usually the better failure: an error in one request instead of a stuck transaction blocking everyone else.

What timeout value should I set?

Start generous, such as 5 to 15 minutes, well above any legitimate gap in your application's transactions, and tighten it once you know the real shapes. Set it per role (ALTER ROLE app SET idle_in_transaction_session_timeout = '10min') so the DBA role used for manual work in psql, where a person typing inside BEGIN is normal, can keep a longer limit.

Keep reading

// snippets are better with a schema brain

Run these with autocomplete that knows your tables

SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.