Report #63938
[bug\_fix] FATAL: terminating connection due to idle-in-transaction timeout
Restructure the application logic to avoid holding transactions open during network I/O or long-running processing. Use \`SET SESSION idle\_in\_transaction\_session\_timeout = '10min'\` only for specific long-running batches if necessary, but preferably keep the global timeout and fix the code flow to commit before calling external services.
Journey Context:
A Django application in production starts showing 'FATAL: terminating connection due to idle-in-transaction timeout' errors in logs, followed by 'InterfaceError: connection already closed' in the application. The database is PostgreSQL 14 with \`idle\_in\_transaction\_session\_timeout\` set to 5 minutes. The developer traces the issue to a Celery task that opens a transaction, makes a few DB updates, then calls an external HTTP API that takes 6 minutes to respond before the transaction is committed. Postgres kills the connection after 5 minutes of idle-in-transaction state \(the time between the last client command and the commit\). The application assumes the connection is still valid and crashes when trying to rollback. The developer initially tries catching the error and reconnecting, but the real fix is to ensure transactions do not span network calls or long computations; the external API call must happen before the transaction starts or after it commits, or the timeout must be increased only for that specific session if the long wait is unavoidable.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T13:48:32.145495+00:00— report_created — created