pgsql: Fix rewriter to cope (more or less) with CTEs in the query being

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)postgresql(dot)org
Subject: pgsql: Fix rewriter to cope (more or less) with CTEs in the query being
Date: 2011-06-07 04:09:11
Message-ID: E1QTnbL-0005Wz-TN@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix rewriter to cope (more or less) with CTEs in the query being rewritten.

Since the original implementation of CTEs only allowed them in SELECT
queries, the rule rewriter did not expect to find any CTEs in statements
being rewritten by ON INSERT/UPDATE/DELETE rules. We had dealt with this
to some extent but the code was still several bricks shy of a load, as
illustrated in bug #6051 from Jehan-Guillaume de Rorthais.

In particular, we have to be able to copy CTEs from the original query's
cteList into that of a rule action, in case the rule action references the
CTE (which it pretty much always will). This also implies we were doing
things in the wrong order in RewriteQuery: we have to recursively rewrite
the CTE queries before expanding the main query, so that we have the
rewritten queries available to copy.

There are unpleasant limitations yet to resolve here, but at least we now
throw understandable FEATURE_NOT_SUPPORTED errors for them instead of just
failing with bizarre implementation-dependent errors. In particular, we
can't handle propagating the same CTE into multiple post-rewrite queries
(because then the CTE would be evaluated multiple times), and we can't cope
with conflicts between CTE names in the original query and in the rule
actions.

Branch
------
master

Details
-------
http://git.postgresql.org/pg/commitdiff/fc1286d3cb92adad2eae69924bead12cfeea5cc6

Modified Files
--------------
src/backend/rewrite/rewriteHandler.c | 187 +++++++++++++++++++++++-----------
src/test/regress/expected/with.out | 40 +++++++
src/test/regress/sql/with.sql | 23 ++++
3 files changed, 189 insertions(+), 61 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Heikki Linnakangas 2011-06-07 07:29:56 pgsql: Make ascii-art in comments pgindent-safe, and some other formatt
Previous Message Tom Lane 2011-06-06 18:25:41 Re: pgsql: Make decompilation of optimized CASE constructs more robust.