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

From Andrew Dunstan
Subject pgsql: Enable fast default for domains with non-volatile constraints
Date
Msg-id E1w0oAp-003fmX-2Z@gemulon.postgresql.org
Whole thread Raw
List 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.universality@gmail.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Andrew Dunstan <andrew@dunslane.net>
Reviewed-by: Viktor Holmberg <viktor.holmberg@aiven.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(-)


pgsql-committers by date:

Previous
From: Álvaro Herrera
Date:
Subject: pgsql: Document the 'command' column of pg_stat_progress_repack
Next
From: Jeff Davis
Date:
Subject: Re: pgsql: CREATE SUBSCRIPTION ... SERVER.