Report #92958
[bug\_fix] terminating connection due to idle-in-transaction timeout
Set idle\_in\_transaction\_session\_timeout in postgresql.conf \(e.g., 5min\) and refactor application code to use short transactions, ensuring commits/rollbacks happen immediately after work completes. Use context managers \(Python with, Java try-with-resources\) that auto-rollback on exceptions. Root cause: Application starts a transaction \(BEGIN\) then performs slow I/O \(HTTP requests, heavy computation\) before committing, holding locks and blocking vacuum/autovacuum operations; Postgres kills the connection after idle\_in\_transaction\_session\_timeout expires.
Journey Context:
Background Celery workers processing image uploads randomly get killed with "terminating connection due to idle-in-transaction timeout" in Postgres logs. The task starts a transaction to update a 'processing' status, then calls an external ML API for image analysis \(taking 2-5 minutes\), then commits the transaction. During the API call, the Postgres connection sits idle in transaction state, holding row locks and preventing autovacuum from cleaning up dead tuples. After the default 1-hour timeout \(or configured shorter timeout\), Postgres force-terminates the connection to prevent indefinite lock holding. The fix involves moving the API call outside the transaction \(begin transaction only for the DB updates\) or using a separate connection for the long-running external operation.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T14:36:59.130504+00:00— report_created — created