Report #64209
[bug\_fix] terminating connection due to idle-in-transaction timeout \(SQLSTATE: 25P03\)
Set the PostgreSQL parameter idle\_in\_transaction\_session\_timeout to a value like '5min' to automatically terminate connections holding transactions open without work; simultaneously refactor application logic to wrap only database operations in transactions, keeping external API calls or file I/O outside of transaction boundaries.
Journey Context:
A Django application handling file uploads suddenly begins throwing 500 errors with "terminating connection due to idle-in-transaction timeout" from PostgreSQL. The developer examines pg\_stat\_activity and sees multiple connections in the state "idle in transaction" with xact\_start timestamps from 10 minutes ago. These connections hold row-level locks from previous UPDATE statements and are blocking the autovacuum process, leading to table bloat and performance degradation. Investigating the Django view code reveals that the @transaction.atomic decorator wraps not only the database save operations but also a subsequent call to an external image processing API that takes 30-60 seconds to respond. Because the transaction is not committed until the view completes, the database connection remains open and "idle in transaction" during the entire external API call, eventually triggering the idle\_in\_transaction\_session\_timeout \(set to 5 minutes\) and killing the connection, causing the 500 error. The immediate fix is to restructure the code to perform the external API call and image processing before starting the database transaction, or to use Django's transaction.on\_commit hooks to defer work. Additionally, the idle\_in\_transaction\_session\_timeout acts as a safety net to kill leaked connections, but the architectural fix is ensuring transactions are strictly limited to database work.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T14:15:43.842151+00:00— report_created — created