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 <[email protected]> Author: Satyanarayana Narlapuram <[email protected]> Author: Dean Rasheed <[email protected]> Discussion: https://postgr.es/m/CAHg+QDf7wTLz_vqb1wi1EJ_4Uh+Vxm75+b4c-Ky=6p+yoah...@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(+)
