pgsql: Enable fast default for domains with non-volatile constraints

From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Enable fast default for domains with non-volatile constraints
Date: 2026-03-12 22:06:31
Message-ID: E1w0oAp-003fmX-2Z@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Enable fast default for domains with non-volatile constraints

Previously, ALTER TABLE ADD COLUMN always forced a table rewrite when
the column type was a domain with constraints (CHECK or NOT NULL), even
if the default value satisfied those constraints. This was because
contain_volatile_functions() considers CoerceToDomain immutable, so
the code conservatively assumed any constrained domain might fail.

Improve this by using soft error handling (ErrorSaveContext) to evaluate
the CoerceToDomain expression at ALTER TABLE time. If the default value
passes the domain's constraints, the value is stored as a "missing"
attribute default and no table rewrite is needed. If the constraint
check fails, we fall back to a table rewrite, preserving the historical
behavior that constraint violations are only raised when the table
actually contains rows.

Domains with volatile constraint expressions always require a table
rewrite since the constraint result could differ per evaluation and
cannot be cached.

Author: Jian He <jian(dot)universality(at)gmail(dot)com>
Reviewed-by: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Reviewed-by: Andrew Dunstan <andrew(at)dunslane(dot)net>
Reviewed-by: Viktor Holmberg <viktor(dot)holmberg(at)aiven(dot)io>
Discussion: https://postgr.es/m/CACJufxE_+iZBR1i49k_AHigppPwLTJi6km8NOsC7FWvKdEmmXg@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/a0b6ef29a51818a4073a5f390ed10ef6453d5c11

Modified Files
--------------
src/backend/commands/tablecmds.c | 59 ++++++++++++++++++++---------
src/backend/executor/execExpr.c | 35 ++++++++++++++++-
src/include/executor/executor.h | 2 +
src/test/regress/expected/fast_default.out | 61 ++++++++++++++++++++++++++++++
src/test/regress/sql/fast_default.sql | 51 +++++++++++++++++++++++++
5 files changed, 190 insertions(+), 18 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Jeff Davis 2026-03-13 01:15:09 Re: pgsql: CREATE SUBSCRIPTION ... SERVER.
Previous Message Álvaro Herrera 2026-03-12 18:23:35 pgsql: Document the 'command' column of pg_stat_progress_repack