Columns

ADD COLUMN ... DEFAULT <constant>

PostgreSQL 11+

Lock mode ACCESS EXCLUSIVE
Risk LOW
Blocks reads, writes, other DDL
ALTER TABLE orders ADD COLUMN currency text DEFAULT 'usd';

What this does

A literal constant default is stored as catalog metadata and applied lazily on read, so this is instant on PostgreSQL 11 and later, no table rewrite. The ACCESS EXCLUSIVE lock is still taken, but held only briefly for the metadata change.

Safe rewrite

Already safe on PostgreSQL 11+: the DEFAULT is stored metadata-only, no table rewrite

-- This is safe on Postgres 11+: the DEFAULT is stored metadata-only, no additional steps needed.

Read more

More in Columns