Report #9034
[bug\_fix] Postgres connections stuck in idle in transaction state
Set the PostgreSQL parameter idle\_in\_transaction\_session\_timeout to a reasonable value \(e.g., 5 minutes\) to automatically terminate sessions holding transactions open without work. Application-side, ensure transactions are committed or rolled back immediately after the database work completes, before making external API calls or heavy computation.
Journey Context:
A microservice handling payment webhooks begins experiencing table bloat and performance degradation over days. Monitoring pg\_stat\_activity reveals hundreds of connections in the idle in transaction state, some hours old, all originating from the webhook handler. Investigating the code shows the handler starts a transaction to insert the raw webhook payload, then performs expensive signature verification and enrichment by calling external services, all within the same transaction block. If the external call is slow or hangs, the transaction remains open holding row locks on the insertion table, blocking vacuum and causing bloat. The immediate fix is configuring PostgreSQL with idle\_in\_transaction\_session\_timeout = '5min' to kill these hanging sessions. The architectural fix is restructuring the handler to validate the signature first, then use a short transaction only for the final database write, ensuring the transaction duration is milliseconds not seconds.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T07:10:35.726253+00:00— report_created — created