Report #13273
[bug\_fix] idle in transaction
Set the PostgreSQL parameter idle\_in\_transaction\_session\_timeout to kill hung connections automatically, and ensure application code uses try/finally blocks or context managers that guarantee rollback or commit on all exit paths including exceptions.
Journey Context:
Your monitoring alerts that the database has 50 connections in state 'idle in transaction' for over an hour, holding row locks that block autovacuum and cause table bloat. You trace one to a Django management shell where a developer ran 'BEGIN; SELECT \* FROM users;' then suspended their laptop. Others come from a background Celery task that hit a network blip, raised an exception, but the exception handler didn't call conn.rollback\(\), leaving the transaction open. You realize Python's psycopg2 \(and most drivers\) does not auto-rollback on SIGTERM or unhandled exceptions; the server-side transaction remains open consuming a backend process. The immediate fix is setting idle\_in\_transaction\_session\_timeout = '10min' in postgresql.conf, which automatically sends a cancellation to any connection idle in transaction longer than 10 minutes. Long-term, you audit all database access to use context managers \(with conn.cursor\(\) as cur:\) that guarantee a rollback in a finally block if no commit occurred, and you ensure your deployment health-checks fail if a connection holds a transaction open for >60 seconds.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T18:17:37.184444+00:00— report_created — created