Full answer
Schema and sample rows:
CREATE TABLE parents (id int PRIMARY KEY);
CREATE TABLE children (id int, parent_id int);
INSERT INTO parents VALUES (1);
INSERT INTO children VALUES (1, 1);
CREATE VIEW my_locks AS
SELECT c.relname::text AS rel, l.mode
FROM pg_locks l JOIN pg_class c ON c.oid = l.relation
WHERE l.pid = pg_backend_pid() AND c.relname IN ('parents', 'children');BEGIN; ALTER TABLE children ADD COLUMN note text; SELECT rel, mode FROM my_locks ORDER BY 1, 2;PostgreSQL returns:
rel | mode
----------+---------------------
children | AccessExclusiveLock
(1 row)ROLLBACK; BEGIN; ALTER TABLE children ADD CONSTRAINT children_parent_fk FOREIGN KEY (parent_id) REFERENCES parents(id) NOT VALID; SELECT rel, mode FROM my_locks ORDER BY 1, 2;Result on PostgreSQL 17:
rel | mode
----------+-----------------------
children | AccessShareLock
children | ShareRowExclusiveLock
parents | AccessShareLock
parents | ShareRowExclusiveLock
(4 rows)COMMIT; BEGIN; ALTER TABLE children VALIDATE CONSTRAINT children_parent_fk; SELECT rel, mode FROM my_locks ORDER BY 1, 2;Result:
rel | mode
----------+--------------------------
children | AccessShareLock
children | ShareUpdateExclusiveLock
parents | AccessShareLock
parents | RowShareLock
(4 rows)COMMIT; BEGIN; CREATE INDEX children_parent_idx ON children (parent_id); SELECT rel, mode FROM my_locks ORDER BY 1, 2;Output:
rel | mode
----------+-----------
children | ShareLock
(1 row)ROLLBACK;PostgreSQL returns:
ROLLBACKAdding a column takes AccessExclusiveLock on children, so even SELECTs on the table queue behind it, briefly if nothing blocks it. Adding the foreign key as NOT VALID takes ShareRowExclusiveLock on both tables, which blocks writes but not reads and finishes quickly because no rows are checked. VALIDATE CONSTRAINT then needs only ShareUpdateExclusiveLock on children, compatible with normal reads and writes, while it scans the table, plus a RowShareLock on parents. A plain CREATE INDEX takes ShareLock, which blocks writes for the whole build; CREATE INDEX CONCURRENTLY avoids that but cannot run inside a transaction, so it is not shown in this transaction-based check. The extra AccessShareLock entries are ordinary read locks taken while the statements read the tables. Splitting migrations into these weaker steps is the basis of zero-downtime schema changes.
CREATE TABLE parents (id int PRIMARY KEY);
CREATE TABLE children (id int, parent_id int);
INSERT INTO parents VALUES (1);
INSERT INTO children VALUES (1, 1);
CREATE VIEW my_locks AS
SELECT c.relname::text AS rel, l.mode
FROM pg_locks l JOIN pg_class c ON c.oid = l.relation
WHERE l.pid = pg_backend_pid() AND c.relname IN ('parents', 'children');
BEGIN; ALTER TABLE children ADD COLUMN note text; SELECT rel, mode FROM my_locks ORDER BY 1, 2;
ROLLBACK; BEGIN; ALTER TABLE children ADD CONSTRAINT children_parent_fk FOREIGN KEY (parent_id) REFERENCES parents(id) NOT VALID; SELECT rel, mode FROM my_locks ORDER BY 1, 2;
COMMIT; BEGIN; ALTER TABLE children VALIDATE CONSTRAINT children_parent_fk; SELECT rel, mode FROM my_locks ORDER BY 1, 2;
COMMIT; BEGIN; CREATE INDEX children_parent_idx ON children (parent_id); SELECT rel, mode FROM my_locks ORDER BY 1, 2;
ROLLBACK;Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.
Key points
- Most ALTER TABLE forms take ACCESS EXCLUSIVE
- NOT VALID foreign keys lock briefly
- VALIDATE uses a weaker lock
- Plain CREATE INDEX blocks writes
- Inspect pg_locks to verify a migration
Common mistakes
- Assuming DDL only blocks writes
- Building indexes without CONCURRENTLY on busy tables
Follow-ups
- Which lock does DROP INDEX CONCURRENTLY take?
- How do you simulate migration locking in CI?
Tips
- Name the lock modes