← Back to PostgreSQL, page 4

postgresql-0074 · Hard senior

Which lock does each common ALTER TABLE take? Inspect them in pg_locks inside a transaction.

Lock levels differ by operation: most ALTER TABLE forms take ACCESS EXCLUSIVE, which blocks even reads, while ADD CONSTRAINT ... NOT VALID for a foreign key, VALIDATE CONSTRAINT and CREATE INDEX CONCURRENTLY take weaker locks that allow reads and often writes. Checking pg_locks inside a transaction that runs the DDL shows exactly what a migration will hold.

Full answer

Schema and sample rows:

sql
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');
sql
BEGIN; ALTER TABLE children ADD COLUMN note text; SELECT rel, mode FROM my_locks ORDER BY 1, 2;

PostgreSQL returns:

text
   rel    |         mode
----------+---------------------
 children | AccessExclusiveLock
(1 row)
sql
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:

text
   rel    |          mode
----------+-----------------------
 children | AccessShareLock
 children | ShareRowExclusiveLock
 parents  | AccessShareLock
 parents  | ShareRowExclusiveLock
(4 rows)
sql
COMMIT; BEGIN; ALTER TABLE children VALIDATE CONSTRAINT children_parent_fk; SELECT rel, mode FROM my_locks ORDER BY 1, 2;

Result:

text
   rel    |           mode
----------+--------------------------
 children | AccessShareLock
 children | ShareUpdateExclusiveLock
 parents  | AccessShareLock
 parents  | RowShareLock
(4 rows)
sql
COMMIT; BEGIN; CREATE INDEX children_parent_idx ON children (parent_id); SELECT rel, mode FROM my_locks ORDER BY 1, 2;

Output:

text
   rel    |    mode
----------+-----------
 children | ShareLock
(1 row)
sql
ROLLBACK;

PostgreSQL returns:

text
ROLLBACK

Adding 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.

sql
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