Report #17010
[bug\_fix] Idle in transaction timeout / connection leak
Set idle\_in\_transaction\_session\_timeout to terminate leaked transactions automatically \(e.g., '10min'\). Fix application exception handling to ensure rollback/commit in finally blocks. Root cause: Application exceptions bypass rollback/commit, leaving connection in 'idle in transaction' state holding locks and preventing vacuum.
Journey Context:
Database performance degrades over several days despite low query volume. pg\_stat\_activity shows 50 connections in 'idle in transaction' state with start times hours old, all from the application servers. The xmin horizon is frozen, preventing autovacuum from reclaiming dead tuples, causing table bloat and slow queries. Investigation reveals a background job worker that processes webhooks. When the webhook payload parsing raises an exception, the Python function returns early without calling session.rollback\(\) or session.close\(\). The connection returns to the pool with an open transaction. The pool reuses this connection for new requests, but the database session still holds the locks from the failed webhook. Setting idle\_in\_transaction\_session\_timeout = '10min' in postgresql.conf automatically kills these leaked sessions, releasing locks and allowing vacuum to proceed. The permanent fix adds try-finally blocks to ensure session.close\(\) always runs.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T04:16:20.489159+00:00— report_created — created