Cookbook recipe

Idle in transaction sessions accumulating

Applies to PostgreSQL 13–17 Last reviewed Nov 2025 Grounded in source
Estimated investigation4 min

Scenario

Your connection pool or monitoring shows a rising number of sessions stuck in the idle in transaction state — each one holds its transaction's locks and pin on shared memory, even while doing nothing. Diagnose it…

Investigation Path

Your connection pool or monitoring shows a rising number of sessions stuck in the idle in transaction state — each one holds its transaction’s locks and pin on shared memory, even while doing nothing.

Diagnose it

Query pg_stat_activity for these sessions, ordered by how long they have been idle:

SELECT pid,
       usename,
       application_name,
       state,
       wait_event_type,
       wait_event,
       now() - xact_start       AS transaction_age,
       now() - state_change     AS idle_in_txn_age,
       LEFT(query, 120)         AS last_query
FROM pg_stat_activity
WHERE state = 'idle in transaction'
ORDER BY transaction_age DESC NULLS LAST;

Also check whether any are blocking other backends:

This is a Pro lesson

Get every Learning Pathway and cookbook recipe — grounded in PostgreSQL source code, with diagnostics, fixes, and prevention for each topic.

Continue this lesson to learn:

  • Why it happens
  • How to fix it
  • Prevent it next time
  • Related & next steps
  • All 36 Learning Pathway lessons
  • 170+ cookbook recipes
  • Source-grounded diagnostics & fixes

Secure checkout Cancel anytime Source-grounded

Career Impact

This scenario builds production judgment and operational confidence under pressure.

Open Career Dashboard →

Keep going

Related & next steps

Was this helpful?

← All cookbook recipes