| From: | Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com> |
|---|---|
| To: | jian he <jian(dot)universality(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-24 03:14:31 |
| Message-ID: | 0DEFEE3B-5A83-44F8-8441-AC20FCD2CC2B@gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
> On Nov 22, 2026, at 08:40, jian he <jian(dot)universality(at)gmail(dot)com> wrote:
>
> 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;
> }
>
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/
| From | Date | Subject | |
|---|---|---|---|
| Previous Message | Zhijie Hou | 2026-11-24 03:11:56 | Re: Fix LogStandbySnapshot() race during logical decoding activation |