Report #48088
[bug\_fix] FATAL: terminating connection due to idle-in-transaction timeout
Set idle\_in\_transaction\_session\_timeout to a reasonable value \(e.g., 60 seconds\) in postgresql.conf or per-session to automatically kill leaked transactions. In the application, ensure all database transactions are committed or rolled back promptly using context managers \(e.g., SQLAlchemy's session.begin\(\), Django's transaction.atomic\(\)\). Never hold transactions open during external API calls, sleep operations, or user input.
Journey Context:
A Python Flask application using SQLAlchemy with autocommit=False. An endpoint started a transaction, queried user data, then called an external payment gateway API that unexpectedly took 90 seconds to respond due to latency. The database connection sat idle in the 'idle in transaction' state, holding RowExclusiveLock on the queried rows. Postgres's idle\_in\_transaction\_session\_timeout was set to 60 seconds \(a common cloud provider default\). After 60 seconds, Postgres terminated the connection with 'FATAL: terminating connection due to idle-in-transaction timeout'. The Flask app received a 500 error, but the payment had already been processed. The transaction was rolled back by Postgres, but the application state was inconsistent \(payment made, order not recorded\).
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T11:11:56.998160+00:00— report_created — created