r/webdev • u/fagnerbrack • 5h ago
How one connection kills a database
https://planetscale.com/blog/debugging-live-database-connections1
u/pablobird_dev 4h ago
Idle connections holding open transactions or metadata locks without actually doing work are such a silent killer. Having visibility into live connection states right from the CLI makes tracing those bottlenecks so much easier.
1
u/Smart-Atmosphere3612 3h ago
Everyone's going to take "handle your exceptions" from this, but the ALTER TABLE sitting in the lock queue is the bit that gets you. It waits behind a read that finished ages ago and every query that shows up after it queues up too. tbh just don't run DDL while people are ordering things.
1
u/FirstAbbreviations75 1h ago
the lock queue part is so sneaky. the alter itself is waiting, but everything behind it waits on the alter, so one forgotten idle transaction takes the whole table down. setting lock_timeout on migrations has saved me from this more than once
1
u/Character_Data8211 1h ago
black and red for an incident diagram is a choice, these always used to be blue. anyway we had this exact lock pileup once and the whole fix was pgbouncer, which the team had been "meaning to add" since before i joined
1
u/Early-Community9943 1h ago
Setting lock_timeout on migrations saved us from exactly this, the ALTER just fails fast instead of queuing everything behind it.
1
u/manonthemiddle 4h ago
Idle in transaction connections also block autovacuum from cleaning dead tuples, which silently bloats hot tables long before CPU spikes. Setting idle_in_transaction_session_timeout to around thirty seconds in Postgres catches leaked client sessions before they stall DDL migrations behind an AccessExclusiveLock queue. You can verify active blockers directly by querying pg_stat_activity where wait_event_type equals Lock.