mirror of
https://github.com/postgres/postgres.git
synced 2025-07-28 23:42:10 +03:00
Support data-modifying commands (INSERT/UPDATE/DELETE) in WITH.
This patch implements data-modifying WITH queries according to the semantics that the updates all happen with the same command counter value, and in an unspecified order. Therefore one WITH clause can't see the effects of another, nor can the outer query see the effects other than through the RETURNING values. And attempts to do conflicting updates will have unpredictable results. We'll need to document all that. This commit just fixes the code; documentation updates are waiting on author. Marko Tiikkaja and Hitoshi Harada
This commit is contained in:
@ -163,6 +163,7 @@ standard_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
|
||||
glob->rewindPlanIDs = NULL;
|
||||
glob->finalrtable = NIL;
|
||||
glob->finalrowmarks = NIL;
|
||||
glob->resultRelations = NIL;
|
||||
glob->relationOids = NIL;
|
||||
glob->invalItems = NIL;
|
||||
glob->lastPHId = 0;
|
||||
@ -214,6 +215,7 @@ standard_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
|
||||
/* final cleanup of the plan */
|
||||
Assert(glob->finalrtable == NIL);
|
||||
Assert(glob->finalrowmarks == NIL);
|
||||
Assert(glob->resultRelations == NIL);
|
||||
top_plan = set_plan_references(glob, top_plan,
|
||||
root->parse->rtable,
|
||||
root->rowMarks);
|
||||
@ -239,11 +241,12 @@ standard_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
|
||||
|
||||
result->commandType = parse->commandType;
|
||||
result->hasReturning = (parse->returningList != NIL);
|
||||
result->hasModifyingCTE = parse->hasModifyingCTE;
|
||||
result->canSetTag = parse->canSetTag;
|
||||
result->transientPlan = glob->transientPlan;
|
||||
result->planTree = top_plan;
|
||||
result->rtable = glob->finalrtable;
|
||||
result->resultRelations = root->resultRelations;
|
||||
result->resultRelations = glob->resultRelations;
|
||||
result->utilityStmt = parse->utilityStmt;
|
||||
result->intoClause = parse->intoClause;
|
||||
result->subplans = glob->subplans;
|
||||
@ -571,7 +574,8 @@ subquery_planner(PlannerGlobal *glob, Query *parse,
|
||||
rowMarks = root->rowMarks;
|
||||
|
||||
plan = (Plan *) make_modifytable(parse->commandType,
|
||||
copyObject(root->resultRelations),
|
||||
parse->canSetTag,
|
||||
list_make1_int(parse->resultRelation),
|
||||
list_make1(plan),
|
||||
returningLists,
|
||||
rowMarks,
|
||||
@ -787,7 +791,7 @@ inheritance_planner(PlannerInfo *root)
|
||||
/* Make sure any initplans from this rel get into the outer list */
|
||||
root->init_plans = list_concat(root->init_plans, subroot.init_plans);
|
||||
|
||||
/* Build target-relations list for the executor */
|
||||
/* Build list of target-relation RT indexes */
|
||||
resultRelations = lappend_int(resultRelations, appinfo->child_relid);
|
||||
|
||||
/* Build list of per-relation RETURNING targetlists */
|
||||
@ -803,8 +807,6 @@ inheritance_planner(PlannerInfo *root)
|
||||
}
|
||||
}
|
||||
|
||||
root->resultRelations = resultRelations;
|
||||
|
||||
/* Mark result as unordered (probably unnecessary) */
|
||||
root->query_pathkeys = NIL;
|
||||
|
||||
@ -814,7 +816,6 @@ inheritance_planner(PlannerInfo *root)
|
||||
*/
|
||||
if (subplans == NIL)
|
||||
{
|
||||
root->resultRelations = list_make1_int(parentRTindex);
|
||||
/* although dummy, it must have a valid tlist for executor */
|
||||
tlist = preprocess_targetlist(root, parse->targetList);
|
||||
return (Plan *) make_result(root,
|
||||
@ -849,7 +850,8 @@ inheritance_planner(PlannerInfo *root)
|
||||
|
||||
/* And last, tack on a ModifyTable node to do the UPDATE/DELETE work */
|
||||
return (Plan *) make_modifytable(parse->commandType,
|
||||
copyObject(root->resultRelations),
|
||||
parse->canSetTag,
|
||||
resultRelations,
|
||||
subplans,
|
||||
returningLists,
|
||||
rowMarks,
|
||||
@ -1725,12 +1727,6 @@ grouping_planner(PlannerInfo *root, double tuple_fraction)
|
||||
count_est);
|
||||
}
|
||||
|
||||
/* Compute result-relations list if needed */
|
||||
if (parse->resultRelation)
|
||||
root->resultRelations = list_make1_int(parse->resultRelation);
|
||||
else
|
||||
root->resultRelations = NIL;
|
||||
|
||||
/*
|
||||
* Return the actual output ordering in query_pathkeys for possible use by
|
||||
* an outer query level.
|
||||
|
Reference in New Issue
Block a user