One SQL line prevents database outages during table migrations, yet few use it
A common PostgreSQL migration pattern — adding a nullable column with no default — can silently take an entire table offline, not because the operation is slow, but because it queues behind any idle transaction holding a lock. Since ALTER TABLE requires an ACCESS EXCLUSIVE lock, it blocks in the queue, and every subsequent read or write then stacks behind it, effectively freezing application traffic. Engineers at DEV Community reproduced this on PostgreSQL 16, where a 26-millisecond point-read waited over 26 seconds due to an idle session holding an unrelated lock. The fix is a single line added before the migration: SET lock_timeout = '2s', which causes the ALTER to abort after two seconds of waiting rather than clogging the lock queue. Unlike statement_timeout, lock_timeout only limits how long a statement waits for a lock, making it safe to use without risking cancellation of legitimate long-running operations like index builds.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in