| From: | jian he <jian(dot)universality(at)gmail(dot)com> |
|---|---|
| To: | Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com> |
| Cc: | PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Andrew Dunstan <andrew(at)dunslane(dot)net> |
| Subject: | Re: Rework: Enable fast default for domains with non-volatile constraints |
| Date: | 2026-11-22 00:40:31 |
| Message-ID: | CACJufxED1YZzxyuRmof8ksBp=DghBBmV4kjbYvaCfoXa-cMhdg@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
On Tue, Nov 17, 2026 at 10:29 AM Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com> wrote:
>
> Hi,
>
> # Background
>
> A PG19 feature, “[a0b6ef29a] Enable fast default for domains with non-volatile constraints”, was reverted; see discussion [1]. This is a rework of the feature.
>
> In short, the problem was that:
> ```
> evantest=# create domain d_div as int check (1 / (value - 1) > 0);
> CREATE DOMAIN
> evantest=# create table t (a int);
> CREATE TABLE
> evantest=# alter table t add column b d_div default 1;
> ERROR: division by zero
> ```
>
> The “alter table” fails even when the table is empty because it evaluates the domain constraints against the default value, and that evaluation fails. But “create table” succeeds because it doesn’t perform this evaluation:
> ```
> create table t2 (a int, b d_div default 1);
> ```
>
> Tom pointed out that this behavior is inconsistent. He suggested adding a new TAM method to determine whether a relation is empty, see [2]. If it is, we can skip evaluating the default expression.
>
> # Implementation
>
> This patch follows that approach:
>
> * 0001 adds a new TAM method, relation_is_empty.
> * 0002 reimplements the feature “Enable fast default for domains with non-volatile constraints” using the new TAM method.
>
I think the function below relation_has_visible_rows can test whether
this table is empty.
(A TAM method may not be necessary).
* ALTER TABLE ADD COLUMN DEFAULT defexpr will acquire AccessExclusiveLock.
So, there's no need to worry about concurrency issues.
* INSERT followed by DELETE leaves no visible tuples (or other case
where table contains invisible rows).
relation_has_visible_rows() returns false and the table is treated as "empty".
We then skip the fast default path, and Phase 3 will perform the table
rewrite again.
Thus, the fast default is used in fewer cases, but that's the
conservative choice.
static bool
relation_has_visible_rows(Relation rel)
{
Snapshot snapshot;
TupleTableSlot *slot;
TableScanDesc scan;
bool found;
snapshot = RegisterSnapshot(GetLatestSnapshot());
slot = table_slot_create(rel, NULL);
scan = table_beginscan(rel, snapshot, 0, NULL, SO_NONE);
found = table_scan_getnextslot(scan, ForwardScanDirection, slot);
table_endscan(scan);
ExecDropSingleTupleTableSlot(slot);
UnregisterSnapshot(snapshot);
return found;
}
| From | Date | Subject | |
|---|---|---|---|
| Previous Message | Osama Abdul Qader | 2026-11-21 21:08:49 | Re: Fix standby logical slot creation during dynamic logical decoding activation |