Transaction Deadlines
Avoid long transactions.
Overview
Transaction deadlines bound how long a database transaction can run. Long transactions hold locks, prevent vacuum, and produce cascading issues; bounded deadlines catch the runaway cases before they convert into incidents.
- Avoid long transactions. Bound transaction duration; reduces lock retention and prevents the cascading failures long transactions cause.
- idle_in_transaction_session_timeout. Postgres aborts sessions that hold transactions open while idle; produces structural protection against connection leaks.
- Application-level deadlines.
context.WithTimeoutin Go, equivalent in other languages; matches the user-visible request budget to the database call. - lock_timeout plus monitoring. Bound how long a query waits for a lock and alert on transactions over threshold; catches contention and regressions before pages fire.
The approach
The practical approach is layered: server-side deadlines as the structural cap, application-level deadlines as the workload-shaped cap, monitoring as the verification. The team’s discipline produces real bounds.
- idle_in_transaction_session_timeout. 30s default, tuned per workload; catches connection leaks at the database layer.
- statement_timeout. Per-role bounded query duration; catches runaway queries before they exhaust connection slots.
- lock_timeout. Bound lock-wait duration; catches contention and surfaces the blocking transaction quickly.
- Application context plus monitoring. Pass deadlines through code for cancellation; alert on
pg_stat_activityfor long-running transactions to catch regressions.
Why this compounds
Transaction deadline discipline compounds across releases. Each enforced deadline produces ongoing protection; the team’s database posture grows; the runaway cases that would have caused incidents get caught structurally.
- Reduced lock contention. Bounded transactions release locks promptly; supports throughput under concurrent load.
- Better vacuuming. No long transactions block vacuum; reduces bloat and keeps the table statistics current.
- Reduced incident rate. Connection leaks caught early at the timeout; the rare runaway never converts into a saturated pool.
- Institutional knowledge. Long-running alerts teach application patterns; the team’s database engineering muscle grows.
Transaction deadlines are an operational discipline that pays off across years. Nova AI Ops integrates with database telemetry, surfaces patterns, and supports the team’s database engineering discipline.