Columns

ADD COLUMN ... DEFAULT now() (stable/current-value functions)

PostgreSQL 11+

Lock mode ACCESS EXCLUSIVE
Risk LOW
Blocks reads, writes, other DDL
ALTER TABLE orders ADD COLUMN created_at timestamptz DEFAULT now();

What this does

now(), transaction_timestamp(), statement_timestamp(), and the CURRENT_DATE/CURRENT_TIME/CURRENT_TIMESTAMP family evaluate to one value per statement, so Postgres treats them the same as a constant default for fast-default storage: instant on PG11+, no table rewrite.

Safe rewrite

Already safe on PostgreSQL 11+: treated as a constant for fast-default storage

-- This is safe on Postgres 11+: now() and similar stable functions use fast-default storage.

Read more

More in Columns