pgsql: Fix INSERT ON CONFLICT UPDATE through a view that isn't justSEL - Mailing list pgsql-committers

From Tom Lane
Subject pgsql: Fix INSERT ON CONFLICT UPDATE through a view that isn't justSEL
Date
Msg-id E1fm68p-0006wz-M5@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
Fix INSERT ON CONFLICT UPDATE through a view that isn't just SELECT *.

When expanding an updatable view that is an INSERT's target, the rewriter
failed to rewrite Vars in the ON CONFLICT UPDATE clause.  This accidentally
worked if the view was just "SELECT * FROM ...", as the transformation
would be a no-op in that case.  With more complicated view targetlists,
this omission would often lead to "attribute ... has the wrong type" errors
or even crashes, as reported by Mario De Frutos Dieguez.

Fix by adding code to rewriteTargetView to fix up the data structure
correctly.  The easiest way to update the exclRelTlist list is to rebuild
it from scratch looking at the new target relation, so factor the code
for that out of transformOnConflictClause to make it sharable.

In passing, avoid duplicate permissions checks against the EXCLUDED
pseudo-relation, and prevent useless view expansion of that relation's
dummy RTE.  The latter is only known to happen (after this patch) in cases
where the query would fail later due to not having any INSTEAD OF triggers
for the view.  But by exactly that token, it would create an unintended
and very poorly tested state of the query data structure, so it seems like
a good idea to prevent it from happening at all.

This has been broken since ON CONFLICT was introduced, so back-patch
to 9.5.

Dean Rasheed, based on an earlier patch by Amit Langote;
comment-kibitzing and back-patching by me

Discussion: https://postgr.es/m/CAFYwGJ0xfzy8jaK80hVN2eUWr6huce0RU8AgU04MGD00igqkTg@mail.gmail.com

Branch
------
REL9_6_STABLE

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

Modified Files
--------------
src/backend/parser/analyze.c                  | 136 ++++++++++--------
src/backend/rewrite/rewriteHandler.c          | 103 +++++++++++++-
src/include/parser/analyze.h                  |   3 +
src/test/regress/expected/updatable_views.out | 194 ++++++++++++++++++++++++++
src/test/regress/sql/updatable_views.sql      | 135 ++++++++++++++++++
5 files changed, 514 insertions(+), 57 deletions(-)


pgsql-committers by date:

Previous
From: Tom Lane
Date:
Subject: Re: pgsql: Address set of issues with errno handling
Next
From: Tom Lane
Date:
Subject: pgsql: Update version 11 release notes.