Scrivio Interview library
DebuggingIntermediate7 min read · updated 12 November 2026

A simple ALTER TABLE ... ADD COLUMN migration caused a 5-minute outage on a busy PostgreSQL table, although the statement itself is fast. What happened?

ALTER TABLE needs an ACCESS EXCLUSIVE lock on the table, and it could not get one because a long-running transaction or query was already using the table. While the ALTER waited in the lock queue, every new query on the table queued behind it, so the table was effectively offline for five minutes. We prevent it by setting lock_timeout for migration sessions so the DDL gives up quickly and is retried, by checking for long transactions before migrating and running DDL at low traffic, and by monitoring lock waits during deployments.

Why this question is asked

This incident surprises even experienced teams, because the statement really is fast: adding a nullable column in PostgreSQL only changes the table's metadata and finishes in milliseconds. Yet the dashboards show every request touching that table hanging for five minutes, then everything recovering at once. Interviewers ask about it because the cause is not the DDL itself but how PostgreSQL queues locks, and that knowledge is needed for every safe migration. In this answer, we will recap PostgreSQL's table locks, reproduce the outage with three sessions, explain why the queue blocks innocent queries, and then go through lock_timeout, pre-migration checks and monitoring.

1A Quick Recap of Table Locks in PostgreSQL

Every statement in PostgreSQL takes a lock on the tables it touches, even a plain SELECT. These are table-level lock modes, and most of them are compatible with each other. A SELECT takes ACCESS SHARE, the weakest mode, and an UPDATE takes ROW EXCLUSIVE; both can run at the same time with thousands of others.

ALTER TABLE ... ADD COLUMN takes ACCESS EXCLUSIVE, the strongest mode, which conflicts with every other mode, including ACCESS SHARE. It needs the table to itself for a moment, because it changes the table's definition, and no query may be reading the table while its shape changes. The lock is held only for the few milliseconds the change takes, but it can only be granted when nobody else holds any lock on the table. And locks are held until the end of the transaction, not the statement.

2Reproducing the Outage

Three kinds of session are enough to reproduce it: a long-running transaction, the migration, and the normal traffic.

SQL
-- Session 1: a report that started at 09:00 and runs for 5 minutes
BEGIN;
SELECT sum(amount) FROM orders; -- holds ACCESS SHARE on orders until COMMIT
-- Session 2: the migration, at 09:00:10
ALTER TABLE orders ADD COLUMN note text; -- wants ACCESS EXCLUSIVE: waits for session 1
-- Session 3..N: ordinary requests, from 09:00:11
SELECT * FROM orders WHERE id = 42; -- wants ACCESS SHARE: waits behind session 2
-- ... every request touching orders now hangs, until 09:05 when session 1 commits

Notice that session 3 does not conflict with session 1 at all; two SELECTs share the table happily. It waits only because session 2 is ahead of it in the queue. When session 1 finally commits at 09:05, the ALTER gets its lock, finishes in milliseconds, and the hundreds of waiting queries all run. From outside, the table was simply unavailable for five minutes.

3Why Waiting DDL Blocks Everyone

PostgreSQL grants locks fairly. When a request has to wait, it joins a queue for that table, and later requests that conflict with anything in the queue line up behind it rather than jumping ahead. This prevents a heavy lock from waiting forever while a constant stream of light queries keeps the table busy. ACCESS EXCLUSIVE conflicts with everything, so once it is queued, every later query on the table, including plain SELECTs, conflicts with it and waits.

So the damage is caused by the combination of three things: a strong lock request, an earlier transaction that holds the table for a long time, and traffic arriving behind it. Any one of them alone is harmless. The long transaction might be a report, an analytics query, a batch job, or a session someone left idle in transaction in a SQL client.

Step throughA fast ALTER that blocks the table for five minutes
ReportPostgreSQLMigrationApp request
SELECT sum(...): holds ACCESS SHARE
ALTER TABLE: needs ACCESS EXCLUSIVE, queued
SELECT by id: queued behind the ALTER
more requests: all queued
COMMIT after 5 minutes
lock granted, ALTER done in 5 ms
queued requests finally run

7 messages in order. Play them, or step through them one at a time.

The application requests do not conflict with the report; they wait behind the queued ALTER.

4Fix 1: Set lock_timeout and Retry

The most important protection is lock_timeout, which makes a statement give up if it cannot get its lock within the given time. If the ALTER cannot get its lock in, say, 3 seconds, it fails, leaves the queue, and the traffic behind it flows again. The worst case becomes a 3 second hiccup instead of a 5 minute outage. We then retry the migration a little later, when the long transaction is gone.

SQL
-- At the top of the migration, or for every migration session
SET lock_timeout = '3s';
ALTER TABLE orders ADD COLUMN note text;
-- ERROR: canceling statement due to lock timeout (if the lock is not granted in 3 s)
# Spring Boot: run it for every Flyway connection
spring.flyway.init-sqls=SET lock_timeout = '3s'

In a Flyway migration on PostgreSQL, the script runs in a transaction, so a SET at the top applies to the rest of the script; SET LOCAL limits it to that transaction explicitly. Flyway's init SQL property sets it for every migration connection, so no migration can forget. A failed migration then fails the deploy cleanly, and a small retry loop in the pipeline, for example three tries a minute apart, usually succeeds once the long transaction ends. A statement_timeout as well guards against a DDL that unexpectedly rewrites the table.

5Fix 2: Check for Long Transactions and Pick the Time

Before running DDL on a busy table, the pipeline can check whether anything is holding the table, and wait or abort if so. pg_stat_activity shows every session with the start time of its transaction, and pg_locks shows which tables they hold.

SQL
SELECT pid, usename, state, now() - xact_start AS running_for, left(query, 60) AS query
FROM pg_stat_activity
WHERE xact_start < now() - interval '1 minute'
ORDER BY xact_start;
-- 4121 | reporting | active | 00:04:12 | SELECT sum(amount) FROM orders
-- 4407 | anna | idle in transaction | 01:20:05 | SELECT * FROM orders LIMIT 10

The second row is a classic: a session idle in transaction for over an hour, holding locks on orders since its last query. A setting such as idle_in_transaction_session_timeout ends such sessions automatically. Running DDL at low traffic helps too, not because the DDL is slow, but because fewer requests pile up during any wait, and long-running reports and batch jobs can be scheduled away from deployment windows.

6Fix 3: Monitor Lock Waits During Deployments

Finally, we make lock waits visible so the next incident is noticed in seconds. log_lock_waits = on logs any wait longer than deadlock_timeout, one second by default, with the blocking and waiting process ids. A dashboard of lock waits per table, drawn from pg_locks joined with pg_stat_activity, or the pg_blocking_pids() function, shows a migration stuck in the queue immediately. Marking deployments on the same dashboard links a sudden spike of waiting queries to the migration that caused it, which is exactly what was missing during the five minutes of this outage.

7Follow-up questions

Does ADD COLUMN with a default rewrite the table?

Since PostgreSQL 11, adding a column with a constant default does not rewrite the table; the default is stored in the catalog and the statement is fast. A volatile default, such as random() or clock_timestamp(), still forces a rewrite while holding ACCESS EXCLUSIVE, which is much worse than waiting. Either way, the lock queue problem applies, because the lock is needed first.

Which other statements need ACCESS EXCLUSIVE?

Most forms of ALTER TABLE, DROP TABLE, TRUNCATE and VACUUM FULL. Some ALTER TABLE forms take weaker locks, for example VALIDATE CONSTRAINT, a plain CREATE INDEX or REINDEX blocks writes but not reads, and CREATE INDEX CONCURRENTLY avoids blocking writes. The documentation lists the lock level of each form; check it for any DDL on a busy table.

Would MySQL behave the same way?

Similarly. MySQL's DDL needs an exclusive metadata lock, and a waiting metadata lock request blocks new queries on the table behind it, shown as Waiting for table metadata lock. The equivalent protection is lock_wait_timeout for the migration session, plus checking for long transactions before running DDL.

8Conclusion

In this answer, we saw why a fast ALTER TABLE ... ADD COLUMN caused a five-minute outage. It needed an ACCESS EXCLUSIVE lock, waited behind a long-running transaction, and, because PostgreSQL queues lock requests fairly, every new query on the table queued behind it. Setting lock_timeout on migration sessions and retrying, checking for long transactions and running DDL at quiet times, and monitoring lock waits during deployments turn this kind of outage into a short, visible hiccup.

What to say in the interviewAbout 80 seconds aloud

Open with

The ALTER itself is fast, but it needs an ACCESS EXCLUSIVE lock on the table, which conflicts with everything, even plain SELECTs. Most likely a long-running transaction was holding a lock on that table, for example a five-minute report or a session idle in transaction, so the ALTER had to wait.

Then, if they want more

The real damage comes from how PostgreSQL queues locks: once the ALTER is waiting, every new query on that table conflicts with it and lines up behind it, even though those queries do not conflict with the report. So the table is effectively down until the long transaction ends, then the ALTER runs in milliseconds and everything recovers at once.

To prevent it, the most important step is lock_timeout for migration sessions, for example three seconds, set through Flyway's init SQL so every migration has it. Then the DDL gives up quickly, traffic flows again, and the pipeline retries a bit later.

Before migrating busy tables I would check pg_stat_activity for old transactions, set idle_in_transaction_session_timeout, and run DDL at low traffic away from reports and batch jobs. And I would turn on log_lock_waits and watch lock waits on a dashboard with deploy markers, so a stuck migration is visible in seconds.

About this answer

Last updated
12 November 2026
Versions it names
PostgreSQL 11
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A simple ALTER TABLE ... ADD COLUMN migration caused a 5-minute outage on a busy...": https://scrivio-interview-library.netlify.app/databases/schema-migration/37cd9451c0/