Report #50263
[bug\_fix] Postgres idle in transaction timeout \(Resource retention and lock contention\)
Set the PostgreSQL parameter idle\_in\_transaction\_session\_timeout to a reasonable value \(e.g., '5min'\) to automatically terminate backends that hold transactions open without work. Additionally, fix application code to ensure transactions are committed or rolled back immediately after database work completes, avoiding long gaps for HTTP requests or external API calls within transactions. Root cause: PostgreSQL holds locks and prevents vacuum from cleaning dead tuples while a transaction remains open, leading to table bloat and lock contention.
Journey Context:
Production monitoring showed steadily increasing table bloat on the orders table despite daily autovacuum runs. Checked pg\_stat\_activity and found multiple connections in 'idle in transaction' state for hours, holding AccessShareLocks on critical tables. Traced these to a Ruby on Rails API endpoint that opened a transaction, queried data, then made a 30-second call to a third-party payment processor before committing. The database connection was held idle during the HTTP request. Initially tried fixing all application code paths, but some were missed. Set idle\_in\_transaction\_session\_timeout=300000 in postgresql.conf. Idle transactions now get terminated after 5 minutes, locks are released, and autovacuum successfully reclaims dead space.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T14:50:49.045491+00:00— report_created — created