Re: support fast default for domain with constraints - Mailing list pgsql-hackers

From jian he
Subject Re: support fast default for domain with constraints
Date
Msg-id CACJufxGZqTUqOCmwVveq4TGG6UXrMnODkMw7x5LV4DM4RkNRcw@mail.gmail.com
Whole thread Raw
In response to Re: support fast default for domain with constraints  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: support fast default for domain with constraints
List pgsql-hackers
On Wed, Mar 5, 2025 at 11:13 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:
>
> This patch appears to summarily throw away a couple of
> backwards-compatibility concerns that the previous round
> took care to preserve:
>
> * not throwing an error if the default would fail the domain
> constraints, but the table is empty so there is no need to
> instantiate the default.
>
hi. Thanks for pointing this out.
I noticed an empty table scarenio, but didn't check it thoroughly.
The attached patch preserves this backwards-compatibility.
now it's aligned with master behavior, i think.

main gotcha is:
ALTER TABLE ADD COLUMN...
If no explicitly DEFAULT, the defval either comes from pg_type.typdefaultbin,
or constructed via makeNullConst branch.
In that case, we need to use soft error evaluation, because we allow
these cases for an empty table;
In other cases, we can directly evaluate explicitly the DEFAULT clause.


> * not assuming that the domain constraints are immutable.
>
> Now it's fair to question how important the second point is
> considering that we mostly treat domain constraints as immutable
> elsewhere.  But I think the first point has actual practical uses
> --- for example, if you want to set things up so that inserts must
> specify that column explicitly.  So I don't think it's okay to
> discard that behavior.
>

in v2-0003. I created a new function:
bool DomainHaveVolatileConstraints(Oid type_id, bool *have_volatile)
within DomainHaveVolatileConstraints
i use contain_volatile_functions to test whether check_expr is volatile or not.
contain_volatile_functions won't be expensive, i think.

if true then have_volatile is set to true.
if have_volatile is true then we need table rewrite.

Attachment

pgsql-hackers by date:

Previous
From: Magnus Hagander
Date:
Subject: Re: doc: expand note about pg_upgrade's --jobs option
Next
From: Alvaro Herrera
Date:
Subject: Re: Support for NO INHERIT to INHERIT state change with named NOT NULL constraints