On Tue, Nov 17, 2026 at 10:29 AM Chao Li <[email protected]> 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;
}
--
jian
https://www.enterprisedb.com/