postgresql/src/backend/rewrite
Tom Lane 362e2dcc46 Fix rewriter to set hasModifyingCTE correctly on rewritten queries.
If we copy data-modifying CTEs from the original query to a replacement
query (from a DO INSTEAD rule), we must set hasModifyingCTE properly
in the replacement query.  Failure to do this can cause various
unpleasantness, such as unsafe usage of parallel plans.  The code also
neglected to propagate hasRecursive, though that's only cosmetic at
the moment.

A difficulty arises if the rule action is an INSERT...SELECT.  We
attach the original query's RTEs and CTEs to the sub-SELECT Query, but
data-modifying CTEs are only allowed to appear in the topmost Query.
For the moment, throw an error in such cases.  It would probably be
possible to avoid this error by attaching the CTEs to the top INSERT
Query instead; but that would require a bunch of new code to adjust
ctelevelsup references.  Given the narrowness of the use-case, and
the need to back-patch this fix, it does not seem worth the trouble
for now.  We can revisit this if we get field complaints.

Per report from Greg Nancarrow.  Back-patch to all supported branches.
(The test case added here does not fail before v10, but there are
plenty of places checking top-level hasModifyingCTE in 9.6, so I have
no doubt that this code change is necessary there too.)

Greg Nancarrow and Tom Lane

Discussion: https://postgr.es/m/CAJcOf-f68DT=26YAMz_i0+Au3TcLO5oiHY5=fL6Sfuits6r+_w@mail.gmail.com
Discussion: https://postgr.es/m/CAJcOf-fAdj=nDKMsRhQzndm-O13NY4dL6xGcEvdX5Xvbbi0V7g@mail.gmail.com
2021-09-08 12:05:47 -04:00
..
Makefile SEARCH and CYCLE clauses 2021-02-01 14:32:51 +01:00
rewriteDefine.c Improve error messages about mismatching relkind 2021-07-08 09:44:51 +02:00
rewriteHandler.c Fix rewriter to set hasModifyingCTE correctly on rewritten queries. 2021-09-08 12:05:47 -04:00
rewriteManip.c Update copyright for 2021 2021-01-02 13:06:25 -05:00
rewriteRemove.c Update copyright for 2021 2021-01-02 13:06:25 -05:00
rewriteSearchCycle.c Disable anonymous record hash support except in special cases 2021-09-08 09:55:04 +02:00
rewriteSupport.c Update copyright for 2021 2021-01-02 13:06:25 -05:00
rowsecurity.c doc: Improve hyphenation consistency 2021-04-21 08:14:43 +02:00