← Back to PostgreSQL, page 4

postgresql-0064 · Medium mid

Is ALTER TABLE ... ADD COLUMN with a DEFAULT instant on a large table in PostgreSQL 11+?

Since PostgreSQL 11, adding a column with a constant (non-volatile) default is a catalog-only change: the default is stored as a 'missing value' and returned for existing rows without rewriting the table. A volatile default such as random() or clock_timestamp() still rewrites every row. The ALTER still needs a brief exclusive lock.

Full answer

Schema and sample rows:

sql
CREATE TABLE big (id int);
INSERT INTO big SELECT generate_series(1, 50000);
sql
CREATE TEMP TABLE file_before AS SELECT pg_relation_filenode('big') AS node; ALTER TABLE big ADD COLUMN status text NOT NULL DEFAULT 'new'; SELECT (SELECT node FROM file_before) = pg_relation_filenode('big') AS same_file, (SELECT atthasmissing FROM pg_attribute WHERE attrelid = 'big'::regclass AND attname = 'status') AS stored_as_missing;

Result:

text
 same_file | stored_as_missing
-----------+-------------------
 t         | t
(1 row)
sql
CREATE TEMP TABLE file_before2 AS SELECT pg_relation_filenode('big') AS node; ALTER TABLE big ADD COLUMN token float8 DEFAULT random(); SELECT (SELECT node FROM file_before2) = pg_relation_filenode('big') AS same_file;

Output:

text
 same_file
-----------
 f
(1 row)
sql
SELECT count(*) FILTER (WHERE status = 'new') AS defaulted FROM big;

PostgreSQL returns:

text
 defaulted
-----------
 50000
(1 row)

Adding status with a constant default kept the same data file and marked the column with a stored missing value, so 50,000 rows were not touched, yet all of them read back 'new'. Adding a column with random() as its default changed the file node, showing that the table was rewritten so each row could get its own value. On big tables, a rewrite holds the exclusive lock for the whole copy, so volatile defaults are better backfilled in batches after adding the column without a default.

sql
CREATE TABLE big (id int);
INSERT INTO big SELECT generate_series(1, 50000);

CREATE TEMP TABLE file_before AS SELECT pg_relation_filenode('big') AS node; ALTER TABLE big ADD COLUMN status text NOT NULL DEFAULT 'new'; SELECT (SELECT node FROM file_before) = pg_relation_filenode('big') AS same_file, (SELECT atthasmissing FROM pg_attribute WHERE attrelid = 'big'::regclass AND attname = 'status') AS stored_as_missing;

CREATE TEMP TABLE file_before2 AS SELECT pg_relation_filenode('big') AS node; ALTER TABLE big ADD COLUMN token float8 DEFAULT random(); SELECT (SELECT node FROM file_before2) = pg_relation_filenode('big') AS same_file;

SELECT count(*) FILTER (WHERE status = 'new') AS defaulted FROM big;

Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.

Key points

  • Constant defaults are catalog-only since PostgreSQL 11
  • Existing rows read the stored missing value
  • Volatile defaults rewrite the table
  • The ALTER still takes a brief exclusive lock
  • Backfill volatile values in batches

Common mistakes

  • Assuming every ADD COLUMN DEFAULT rewrites the table
  • Using volatile defaults on huge tables

Follow-ups

  • Is now() volatile for this purpose?
  • How would you add NOT NULL to an existing column safely?

Tips

  • Show the file node check