1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-11 10:01:57 +03:00

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.
This commit is contained in:
Tom Lane
2012-04-25 20:20:33 -04:00
parent c62b8eaae1
commit 9fa82c9809
6 changed files with 116 additions and 50 deletions

View File

@ -529,22 +529,10 @@ subquery_planner(PlannerGlobal *glob, Query *parse,
List *rowMarks;
/*
* Deal with the RETURNING clause if any. It's convenient to pass
* the returningList through setrefs.c now rather than at top
* level (if we waited, handling inherited UPDATE/DELETE would be
* much harder).
* Set up the RETURNING list-of-lists, if needed.
*/
if (parse->returningList)
{
List *rlist;
Assert(parse->resultRelation);
rlist = set_returning_clause_references(root,
parse->returningList,
plan,
parse->resultRelation);
returningLists = list_make1(rlist);
}
returningLists = list_make1(parse->returningList);
else
returningLists = NIL;
@ -889,15 +877,8 @@ inheritance_planner(PlannerInfo *root)
/* Build list of per-relation RETURNING targetlists */
if (parse->returningList)
{
List *rlist;
rlist = set_returning_clause_references(&subroot,
subroot.parse->returningList,
subplan,
appinfo->child_relid);
returningLists = lappend(returningLists, rlist);
}
returningLists = lappend(returningLists,
subroot.parse->returningList);
}
/* Mark result as unordered (probably unnecessary) */