Report #39639
[bug\_fix] idle in transaction \(Postgres connection leak\)
Set the PostgreSQL configuration parameter idle\_in\_transaction\_session\_timeout to automatically terminate connections that remain idle in transaction for too long \(e.g., 10 minutes\); fix application code to ensure transactions are always committed or rolled back in try/catch/finally blocks or using context managers. Root cause: Application exceptions or logic errors that exit a transaction block without calling COMMIT or ROLLBACK, leaving the connection holding locks and preventing vacuum.
Journey Context:
A web application's database performance degrades gradually over several days until restart. Monitoring pg\_stat\_activity reveals hundreds of connections in the 'idle in transaction' state, some hours old, holding row locks that prevent vacuum from cleaning up dead tuples. Tracing the application code reveals a rare exception path in a checkout handler that raises an error but fails to roll back the database transaction in the finally block. Fixing the exception handling and setting idle\_in\_transaction\_session\_timeout to 5 minutes eliminates the leak.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T21:00:32.980281+00:00— report_created — created