[BUG] ON CONFLICT DO UPDATE SET x = EXCLUDED.<virtual-generated-column> errors or silently writes NULL

Поиск
Список
Период
Сортировка
Искать

[BUG] ON CONFLICT DO UPDATE SET x = EXCLUDED.<virtual-generated-column> errors or silently writes NULL

От:
SATYANARAYANA NARLAPURAM <satyanarlapuram@gmail.com>
Дата:
Вложения:
Hi Hackers,

Virtual generated column (bgc) behavior for plain and partitioned tables is different
when EXCLUDED.<vgc> references inside for INSERT ... ON CONFLICT DO UPDATE.
For plain table it errors out with the message "unexpected virtual generated column reference"
and for partitioned tables, it silently writes NULL (wrong data).


Repro:

-- plan table

    DROP TABLE IF EXISTS t;
    CREATE TABLE t (id int PRIMARY KEY,
                    a  int,
                    c  int GENERATED ALWAYS AS (a * 10) VIRTUAL);
    INSERT INTO t VALUES (1, 5);

    INSERT INTO t VALUES (1, 7)
        ON CONFLICT (id) DO UPDATE SET a = EXCLUDED.c;
    -- ERROR:  unexpected virtual generated column reference
   

-- Partitioned table: 

    DROP TABLE IF EXISTS tp;
    CREATE TABLE tp (id int PRIMARY KEY,
                     a  int,
                     c  int GENERATED ALWAYS AS (a * 10) VIRTUAL)
        PARTITION BY RANGE (id);
    CREATE TABLE tp1 PARTITION OF tp FOR VALUES FROM (1) TO (100);
    INSERT INTO tp VALUES (1, 5);

    INSERT INTO tp VALUES (1, 7)
        ON CONFLICT (id) DO UPDATE SET a = EXCLUDED.c;

    SELECT * FROM tp

 id | a | c
----+---+---
  1 |   |  


We have two options to fix, (1) throw an error for partitioned tables similar to plain tables or
(2) support the scenario fixing for both the cases.

I tried fixing this by replacing build_tlist_index with build_tlist_index_other_vars . This fix
works because build_tlist_index_other_vars only indexes plain-Var TEs of exclRelTlist and 
leaves has_non_vars = false, so fix_join_expr skips whole-subtree matching and never collapses
 the VGC-expanded (EXCLUDED.a * 10) in onConflictSet back into a Var(INNER_VAR, vgc_attno).

I am not super familiar with this area so I am not sure if this breaks anything. Ran the existing 
tests and they seem to be passing.


Thanks,
Satya 

Re: [BUG] ON CONFLICT DO UPDATE SET x = EXCLUDED.<virtual-generated-column> errors or silently writes NULL

От:
Dean Rasheed <dean.a.rasheed@gmail.com>
Дата:
Вложения:
On Thu, 16 Apr 2026 at 21:49, SATYANARAYANA NARLAPURAM
 wrote:
>
> Virtual generated column (bgc) behavior for plain and partitioned tables is different
> when EXCLUDED. references inside for INSERT ... ON CONFLICT DO UPDATE.
> For plain table it errors out with the message "unexpected virtual generated column reference"
> and for partitioned tables, it silently writes NULL (wrong data).

Nice catch!

> I tried fixing this by replacing build_tlist_index with build_tlist_index_other_vars . This fix
> works because build_tlist_index_other_vars only indexes plain-Var TEs of exclRelTlist and
> leaves has_non_vars = false, so fix_join_expr skips whole-subtree matching and never collapses
>  the VGC-expanded (EXCLUDED.a * 10) in onConflictSet back into a Var(INNER_VAR, vgc_attno).

This doesn't quite work in all cases -- if the generated expression is
simply a Var, then it is found in the indexed tlist without the
non_var matching code, leading to the same problem. For example,
modifying your original test case to this:

CREATE TABLE t (id int PRIMARY KEY,
                c  int GENERATED ALWAYS AS (a) VIRTUAL, a int);

Admittedly, that's a rather silly example, but we really ought to have
a fix that works for all cases.

Looking more closely, I think the right fix is to not expand virtual
generated columns in the targetlist of EXCLUDED (exclRelTlist), so
then they will not be found as matching expressions in the setrefs.c
code.

I also noticed that there are already a couple of places in the
planner that claim that exclRelTlist contains only Vars, so this
approach makes that claim true (though I don't think those other
places represented actual bugs).

Attached is a v2 patch doing it that way, with the same tests, which all pass.

Regards,
Dean

Re: [BUG] ON CONFLICT DO UPDATE SET x = EXCLUDED.<virtual-generated-column> errors or silently writes NULL

От:
SATYANARAYANA NARLAPURAM <satyanarlapuram@gmail.com>
Дата:
HI,

On Sat, Apr 18, 2026 at 11:14 AM Dean Rasheed <dean.a.rasheed@gmail.com> wrote:
On Thu, 16 Apr 2026 at 21:49, SATYANARAYANA NARLAPURAM
<satyanarlapuram@gmail.com> wrote:
>
> Virtual generated column (bgc) behavior for plain and partitioned tables is different
> when EXCLUDED.<vgc> references inside for INSERT ... ON CONFLICT DO UPDATE.
> For plain table it errors out with the message "unexpected virtual generated column reference"
> and for partitioned tables, it silently writes NULL (wrong data).

Nice catch!

> I tried fixing this by replacing build_tlist_index with build_tlist_index_other_vars . This fix
> works because build_tlist_index_other_vars only indexes plain-Var TEs of exclRelTlist and
> leaves has_non_vars = false, so fix_join_expr skips whole-subtree matching and never collapses
>  the VGC-expanded (EXCLUDED.a * 10) in onConflictSet back into a Var(INNER_VAR, vgc_attno).

This doesn't quite work in all cases -- if the generated expression is
simply a Var, then it is found in the indexed tlist without the
non_var matching code, leading to the same problem. For example,
modifying your original test case to this:

CREATE TABLE t (id int PRIMARY KEY,
                c  int GENERATED ALWAYS AS (a) VIRTUAL, a int);

Admittedly, that's a rather silly example, but we really ought to have
a fix that works for all cases.

Agreed.
 

Looking more closely, I think the right fix is to not expand virtual
generated columns in the targetlist of EXCLUDED (exclRelTlist), so
then they will not be found as matching expressions in the setrefs.c
code.

I also noticed that there are already a couple of places in the
planner that claim that exclRelTlist contains only Vars, so this
approach makes that claim true (though I don't think those other
places represented actual bugs).

Attached is a v2 patch doing it that way, with the same tests, which all pass.

Reran the failing tests and they all passed. Additionally ran the regression tests.
 Patch looks good to me.
 

Regards,
Dean

Re: [BUG] ON CONFLICT DO UPDATE SET x = EXCLUDED.<virtual-generated-column> errors or silently writes NULL

От:
Dean Rasheed <dean.a.rasheed@gmail.com>
Дата:
On Mon, 20 Apr 2026 at 01:55, SATYANARAYANA NARLAPURAM
 wrote:
>
>> Attached is a v2 patch doing it that way, with the same tests, which all pass.
>
> Reran the failing tests and they all passed. Additionally ran the regression tests.
>  Patch looks good to me.
>

Thanks for checking. Patch pushed and back-patched to v18.

Regards,
Dean


Re: [BUG] ON CONFLICT DO UPDATE SET x = EXCLUDED.<virtual-generated-column> errors or silently writes NULL

От:
Dean Rasheed <dean.a.rasheed@gmail.com>
Дата:
Вложения:
On Sat, 12 Sept 2026 at 19:10, Ayush Tiwari  wrote:
>
> On Sat, 12 Sept 2026 at 23:10, Dean Rasheed  wrote:
> >
> > Hmm, I'm not sure that this is the right approach. For example,
> > consider this case, without a generated column:
> >
> > CREATE TABLE t (id int PRIMARY KEY, val int);
> > INSERT INTO t VALUES (1,2)
> >   ON CONFLICT (id) DO UPDATE SET val = 0 WHERE excluded.tableoid > 0;
> >
> > ERROR:  column excluded.tableoid does not exist
> >
> > That error is on point -- "excluded" is not a real table, it is a
> > pseudo-table constructed from the values proposed for insertion, not
> > the existing values. As such, it has no system columns.
> >
> > So, ISTM that when "c" is a virtual generated column, and "excluded.c"
> > is expanded to "excluded.tableoid", it *should* produce an error.
>
> My thinking was that excluded.c could use the table that the row would
> have been inserted into when evaluating its generation expression.
>
> Would that be a reasonable meaning for tableoid here, or should it be
> considered unavailable because EXCLUDED represents only a proposed row?
> That was the distinction I had in mind, but I may be missing something.
>

I think it shouldn't be allowed. Besides the fact that EXCLUDED is the
proposed row, and doesn't have a tableoid, it would be inconsistent to
allow a virtual generated column to magic-up EXCLUDED.tableoid, when
that isn't available when referenced directly in SQL.

Regards,
Dean


FAQ