Columns

ADD COLUMN using a constrained domain type

Lock mode ACCESS EXCLUSIVE
Risk HIGH
Blocks reads, writes, other DDL
ALTER TABLE orders ADD COLUMN status order_status_domain;

What this does

When the column's type is a CREATE DOMAIN type that carries a CHECK constraint, Postgres validates that constraint against every existing row as part of the ADD COLUMN, which can force a table rewrite under ACCESS EXCLUSIVE, the same risk profile as an inline CHECK.

Safe rewrite

Add a base-type column first, backfill, then enforce the domain constraint in a follow-up migration

ALTER TABLE orders ADD COLUMN IF NOT EXISTS status text;
-- Backfill and validate in a follow-up migration before switching to the constrained domain.

Read more

More in Columns