pgsql: Fix planner's handling of RETURNING lists in writable CTEs.

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)postgresql(dot)org
Subject: pgsql: Fix planner's handling of RETURNING lists in writable CTEs.
Date: 2012-04-26 00:21:04
Message-ID: E1SNCSG-0008NT-Vu@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix planner's handling of RETURNING lists in writable CTEs.

setrefs.c failed to do "rtoffset" adjustment of Vars in RETURNING lists,
which meant they were left with the wrong varnos when the RETURNING list
was in a subquery. That was never possible before writable CTEs, of
course, but now it's broken. The executor fails to notice any problem
because ExecEvalVar just references the ecxt_scantuple for any normal
varno; but EXPLAIN breaks when the varno is wrong, as illustrated in a
recent complaint from Bartosz Dmytrak.

Since the eventual rtoffset of the subquery is not known at the time
we are preparing its plan node, the previous scheme of executing
set_returning_clause_references() at that time cannot handle this
adjustment. Fortunately, it turns out that we don't really need to do it
that way, because all the needed information is available during normal
setrefs.c execution; we just have to dig it out of the ModifyTable node.
So, do that, and get rid of the kluge of early setrefs processing of
RETURNING lists. (This is a little bit of a cheat in the case of inherited
UPDATE/DELETE, because we are not passing a "root" struct that corresponds
exactly to what the subplan was built with. But that doesn't matter, and
anyway this is less ugly than early setrefs processing was.)

Back-patch to 9.1, where the problem became possible to hit.

Branch
------
master

Details
-------
http://git.postgresql.org/pg/commitdiff/9fa82c980935ef4aee18fabe8da20ae2198b052a

Modified Files
--------------
src/backend/optimizer/plan/createplan.c | 12 +----
src/backend/optimizer/plan/planner.c | 27 ++---------
src/backend/optimizer/plan/setrefs.c | 75 +++++++++++++++++++++++++-----
src/include/optimizer/planmain.h | 4 --
src/test/regress/expected/with.out | 42 +++++++++++++++++
src/test/regress/sql/with.sql | 6 +++
6 files changed, 116 insertions(+), 50 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Tom Lane 2012-04-26 00:21:05 pgsql: Fix planner's handling of RETURNING lists in writable CTEs.
Previous Message Tom Lane 2012-04-25 21:26:25 pgsql: Fix edge-case behavior of pg_next_dst_boundary().