Cookbook recipe

Avoid idle-in-transaction bloat

Applies to PostgreSQL 13–17 Last reviewed May 2026 Grounded in source
Estimated investigation4 min

Scenario

An app opens a transaction, does one query, then sits idle while holding it open. This pins the vacuum horizon and causes bloat. Diagnose it Find idle-in-transaction sessions and their age: SELECT pid, now()-xact_start AS xact_age,…

Investigation Path

An app opens a transaction, does one query, then sits idle while holding it open. This pins the vacuum horizon and causes bloat.

Diagnose it

Find idle-in-transaction sessions and their age:

SELECT pid, now()-xact_start AS xact_age, state, left(query,60)
FROM pg_stat_activity
WHERE state = 'idle in transaction'
ORDER BY xact_age DESC;

Why it happens

An open transaction holds back xmin — VACUUM cannot remove dead tuples newer than the oldest running transaction. A long idle-in-transaction session therefore blocks cleanup cluster-wide and bloats tables.

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:

  • 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