> On Nov 22, 2026, at 08:40, jian he <[email protected]> wrote:
> 
> 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;
> }
> 

Hi Jian,

Thanks for proposing the function. For this patch alone, it should be good 
enough, since table_beginscan() already uses the table AM interface.

One potential advantage of a new TAM method is that each TAM could provide its 
own optimized implementation, and the method might also be reused elsewhere. 
Since Tom suggested adding a new TAM method earlier, let’s wait a while to see 
what he, Andrew, or other folks think about this. If there is no further input, 
I can switch to use your function.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/






Reply via email to