pgsql: Fix expansion of EXCLUDED virtual generated columns.

From: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix expansion of EXCLUDED virtual generated columns.
Date: 2026-04-22 08:07:15
Message-ID: E1wFSc6-0027yS-37@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix expansion of EXCLUDED virtual generated columns.

If the SET or WHERE clause of an INSERT ... ON CONFLICT command
references EXCLUDED.col, where col is a virtual generated column, the
column was not properly expanded, leading to an "unexpected virtual
generated column reference" error, or incorrect results.

The problem was that expand_virtual_generated_columns() would expand
virtual generated columns in both the SET and WHERE clauses and in the
targetlist of the EXCLUDED pseudo-relation (exclRelTlist). Then
fix_join_expr() from set_plan_refs() would turn the expanded
expressions in the SET and WHERE clauses back into Vars, because they
would be found to match the expression entries in the indexed tlist
produced from exclRelTlist.

To fix this, arrange for expand_virtual_generated_columns() to not
expand virtual generated columns in exclRelTlist. This forces
set_plan_refs() to resolve generation expressions in the query using
non-virtual columns, as required by the executor.

In addition, exclRelTlist now always contains only Vars. That was
something already claimed in a couple of existing comments in the
planner, which relied on that fact to skip some processing, though
those did not appear to constitute active bugs.

Reported-by: Satyanarayana Narlapuram <satyanarlapuram(at)gmail(dot)com>
Author: Satyanarayana Narlapuram <satyanarlapuram(at)gmail(dot)com>
Author: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
Discussion: https://postgr.es/m/CAHg+QDf7wTLz_vqb1wi1EJ_4Uh+Vxm75+b4c-Ky=6P+yOAHjbQ@mail.gmail.com
Backpatch-through: 18

Branch
------
REL_18_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/cf38dedf693a17f9317d8ed85ab7468afebf8cbf

Modified Files
--------------
src/backend/optimizer/prep/prepjointree.c | 19 ++++++++++
src/test/regress/expected/generated_virtual.out | 48 +++++++++++++++++++++++++
src/test/regress/sql/generated_virtual.sql | 25 +++++++++++++
3 files changed, 92 insertions(+)

Browse pgsql-committers by date

  From Date Subject
Next Message Dean Rasheed 2026-04-22 10:54:06 pgsql: Fix UPDATE/DELETE ... WHERE CURRENT OF on a table with virtual c
Previous Message Jeff Davis 2026-04-22 04:48:17 Re: pgsql: Perform provider-specific initialization in new functions.