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 <[email protected]>
Reviewed-by: Tom Lane <[email protected]>
Reviewed-by: Andrew Dunstan <[email protected]>
Reviewed-by: Viktor Holmberg <[email protected]>
Discussion: 
https://postgr.es/m/cacjufxe_+izbr1i49k_ahigpppwltji6km8nosc7fwvkdem...@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(-)

Reply via email to