1
0
mirror of https://github.com/postgres/postgres.git synced 2025-09-09 13:09:39 +03:00

Add support for MERGE ... WHEN NOT MATCHED BY SOURCE.

This allows MERGE commands to include WHEN NOT MATCHED BY SOURCE
actions, which operate on rows that exist in the target relation, but
not in the data source. These actions can execute UPDATE, DELETE, or
DO NOTHING sub-commands.

This is in contrast to already-supported WHEN NOT MATCHED actions,
which operate on rows that exist in the data source, but not in the
target relation. To make this distinction clearer, such actions may
now be written as WHEN NOT MATCHED BY TARGET.

Writing WHEN NOT MATCHED without specifying BY SOURCE or BY TARGET is
equivalent to writing WHEN NOT MATCHED BY TARGET.

Dean Rasheed, reviewed by Alvaro Herrera, Ted Yu and Vik Fearing.

Discussion: https://postgr.es/m/CAEZATCWqnKGc57Y_JanUBHQXNKcXd7r=0R4NEZUVwP+syRkWbA@mail.gmail.com
This commit is contained in:
Dean Rasheed
2024-03-30 10:00:26 +00:00
parent 46e5441fa5
commit 0294df2f1f
33 changed files with 1228 additions and 362 deletions

View File

@@ -312,7 +312,8 @@ static ModifyTable *make_modifytable(PlannerInfo *root, Plan *subplan,
List *updateColnosLists,
List *withCheckOptionLists, List *returningLists,
List *rowMarks, OnConflictExpr *onconflict,
List *mergeActionLists, int epqParam);
List *mergeActionLists, List *mergeJoinConditions,
int epqParam);
static GatherMerge *create_gather_merge_plan(PlannerInfo *root,
GatherMergePath *best_path);
@@ -2836,6 +2837,7 @@ create_modifytable_plan(PlannerInfo *root, ModifyTablePath *best_path)
best_path->rowMarks,
best_path->onconflict,
best_path->mergeActionLists,
best_path->mergeJoinConditions,
best_path->epqParam);
copy_generic_path_info(&plan->plan, &best_path->path);
@@ -7031,7 +7033,8 @@ make_modifytable(PlannerInfo *root, Plan *subplan,
List *updateColnosLists,
List *withCheckOptionLists, List *returningLists,
List *rowMarks, OnConflictExpr *onconflict,
List *mergeActionLists, int epqParam)
List *mergeActionLists, List *mergeJoinConditions,
int epqParam)
{
ModifyTable *node = makeNode(ModifyTable);
List *fdw_private_list;
@@ -7101,6 +7104,7 @@ make_modifytable(PlannerInfo *root, Plan *subplan,
node->returningLists = returningLists;
node->rowMarks = rowMarks;
node->mergeActionLists = mergeActionLists;
node->mergeJoinConditions = mergeJoinConditions;
node->epqParam = epqParam;
/*

View File

@@ -911,6 +911,9 @@ subquery_planner(PlannerGlobal *glob, Query *parse,
EXPRKIND_QUAL);
}
parse->mergeJoinCondition =
preprocess_expression(root, parse->mergeJoinCondition, EXPRKIND_QUAL);
root->append_rel_list = (List *)
preprocess_expression(root, (Node *) root->append_rel_list,
EXPRKIND_APPINFO);
@@ -1805,6 +1808,7 @@ grouping_planner(PlannerInfo *root, double tuple_fraction)
List *withCheckOptionLists = NIL;
List *returningLists = NIL;
List *mergeActionLists = NIL;
List *mergeJoinConditions = NIL;
List *rowMarks;
if (bms_membership(root->all_result_relids) == BMS_MULTIPLE)
@@ -1911,6 +1915,19 @@ grouping_planner(PlannerInfo *root, double tuple_fraction)
mergeActionLists = lappend(mergeActionLists,
mergeActionList);
}
if (parse->commandType == CMD_MERGE)
{
Node *mergeJoinCondition = parse->mergeJoinCondition;
if (this_result_rel != top_result_rel)
mergeJoinCondition =
adjust_appendrel_attrs_multilevel(root,
mergeJoinCondition,
this_result_rel,
top_result_rel);
mergeJoinConditions = lappend(mergeJoinConditions,
mergeJoinCondition);
}
}
if (resultRelations == NIL)
@@ -1935,6 +1952,8 @@ grouping_planner(PlannerInfo *root, double tuple_fraction)
returningLists = list_make1(parse->returningList);
if (parse->mergeActionList)
mergeActionLists = list_make1(parse->mergeActionList);
if (parse->commandType == CMD_MERGE)
mergeJoinConditions = list_make1(parse->mergeJoinCondition);
}
}
else
@@ -1950,6 +1969,8 @@ grouping_planner(PlannerInfo *root, double tuple_fraction)
returningLists = list_make1(parse->returningList);
if (parse->mergeActionList)
mergeActionLists = list_make1(parse->mergeActionList);
if (parse->commandType == CMD_MERGE)
mergeJoinConditions = list_make1(parse->mergeJoinCondition);
}
/*
@@ -1977,6 +1998,7 @@ grouping_planner(PlannerInfo *root, double tuple_fraction)
rowMarks,
parse->onConflict,
mergeActionLists,
mergeJoinConditions,
assign_special_exec_param(root));
}

View File

@@ -1143,7 +1143,9 @@ set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
*/
if (splan->mergeActionLists != NIL)
{
List *newMJC = NIL;
ListCell *lca,
*lcj,
*lcr;
/*
@@ -1164,10 +1166,12 @@ set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
itlist = build_tlist_index(subplan->targetlist);
forboth(lca, splan->mergeActionLists,
lcr, splan->resultRelations)
forthree(lca, splan->mergeActionLists,
lcj, splan->mergeJoinConditions,
lcr, splan->resultRelations)
{
List *mergeActionList = lfirst(lca);
Node *mergeJoinCondition = lfirst(lcj);
Index resultrel = lfirst_int(lcr);
foreach(l, mergeActionList)
@@ -1192,7 +1196,19 @@ set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
NRM_EQUAL,
NUM_EXEC_QUAL(plan));
}
/* Fix join condition too. */
mergeJoinCondition = (Node *)
fix_join_expr(root,
(List *) mergeJoinCondition,
NULL, itlist,
resultrel,
rtoffset,
NRM_EQUAL,
NUM_EXEC_QUAL(plan));
newMJC = lappend(newMJC, mergeJoinCondition);
}
splan->mergeJoinConditions = newMJC;
}
splan->nominalRelation += rtoffset;

View File

@@ -153,9 +153,11 @@ transform_MERGE_to_join(Query *parse)
{
RangeTblEntry *joinrte;
JoinExpr *joinexpr;
bool have_action[3];
JoinType jointype;
int joinrti;
List *vars;
RangeTblRef *rtr;
if (parse->commandType != CMD_MERGE)
return;
@@ -164,11 +166,27 @@ transform_MERGE_to_join(Query *parse)
vars = NIL;
/*
* When any WHEN NOT MATCHED THEN INSERT clauses exist, we need to use an
* outer join so that we process all unmatched tuples from the source
* relation. If none exist, we can use an inner join.
* Work out what kind of join is required. If there any WHEN NOT MATCHED
* BY SOURCE/TARGET actions, an outer join is required so that we process
* all unmatched tuples from the source and/or target relations.
* Otherwise, we can use an inner join.
*/
if (parse->mergeUseOuterJoin)
have_action[MERGE_WHEN_MATCHED] = false;
have_action[MERGE_WHEN_NOT_MATCHED_BY_SOURCE] = false;
have_action[MERGE_WHEN_NOT_MATCHED_BY_TARGET] = false;
foreach_node(MergeAction, action, parse->mergeActionList)
{
if (action->commandType != CMD_NOTHING)
have_action[action->matchKind] = true;
}
if (have_action[MERGE_WHEN_NOT_MATCHED_BY_SOURCE] &&
have_action[MERGE_WHEN_NOT_MATCHED_BY_TARGET])
jointype = JOIN_FULL;
else if (have_action[MERGE_WHEN_NOT_MATCHED_BY_SOURCE])
jointype = JOIN_LEFT;
else if (have_action[MERGE_WHEN_NOT_MATCHED_BY_TARGET])
jointype = JOIN_RIGHT;
else
jointype = JOIN_INNER;
@@ -203,17 +221,21 @@ transform_MERGE_to_join(Query *parse)
* regular table, this will equal parse->resultRelation, but for a
* trigger-updatable view, it will be the expanded view subquery that we
* need to pull data from.
*
* The source relation is in parse->jointree->fromlist, but any quals in
* parse->jointree->quals are restrictions on the target relation (if the
* target relation is an auto-updatable view).
*/
rtr = makeNode(RangeTblRef);
rtr->rtindex = parse->mergeTargetRelation;
joinexpr = makeNode(JoinExpr);
joinexpr->jointype = jointype;
joinexpr->isNatural = false;
joinexpr->larg = (Node *) makeNode(RangeTblRef);
((RangeTblRef *) joinexpr->larg)->rtindex = parse->mergeTargetRelation;
joinexpr->rarg = linitial(parse->jointree->fromlist); /* original join */
joinexpr->larg = (Node *) makeFromExpr(list_make1(rtr), parse->jointree->quals);
joinexpr->rarg = linitial(parse->jointree->fromlist); /* source rel */
joinexpr->usingClause = NIL;
joinexpr->join_using_alias = NULL;
/* The quals are removed from the jointree and into this specific join */
joinexpr->quals = parse->jointree->quals;
joinexpr->quals = parse->mergeJoinCondition;
joinexpr->alias = NULL;
joinexpr->rtindex = joinrti;
@@ -233,6 +255,15 @@ transform_MERGE_to_join(Query *parse)
add_nulling_relids((Node *) parse->targetList,
bms_make_singleton(parse->mergeTargetRelation),
bms_make_singleton(joinrti));
/*
* If there are any WHEN NOT MATCHED BY SOURCE actions, the executor will
* use the join condition to distinguish between MATCHED and NOT MATCHED
* BY SOURCE cases. Otherwise, it's no longer needed, and we set it to
* NULL, saving cycles during planning and execution.
*/
if (!have_action[MERGE_WHEN_NOT_MATCHED_BY_SOURCE])
parse->mergeJoinCondition = NULL;
}
/*
@@ -2173,6 +2204,8 @@ perform_pullup_replace_vars(PlannerInfo *root,
pullup_replace_vars((Node *) action->targetList, rvcontext);
}
}
parse->mergeJoinCondition = pullup_replace_vars(parse->mergeJoinCondition,
rvcontext);
replace_vars_in_jointree((Node *) parse->jointree, rvcontext);
Assert(parse->setOperations == NULL);
parse->havingQual = pullup_replace_vars(parse->havingQual, rvcontext);

View File

@@ -134,6 +134,7 @@ preprocess_targetlist(PlannerInfo *root)
if (command_type == CMD_MERGE)
{
ListCell *l;
List *vars;
/*
* For MERGE, handle targetlist of each MergeAction separately. Give
@@ -144,7 +145,6 @@ preprocess_targetlist(PlannerInfo *root)
foreach(l, parse->mergeActionList)
{
MergeAction *action = (MergeAction *) lfirst(l);
List *vars;
ListCell *l2;
if (action->commandType == CMD_INSERT)
@@ -182,6 +182,30 @@ preprocess_targetlist(PlannerInfo *root)
}
list_free(vars);
}
/*
* Add resjunk entries for any Vars and PlaceHolderVars used in the
* join condition that belong to relations other than the target. We
* don't expect to see any aggregates or window functions here.
*/
vars = pull_var_clause(parse->mergeJoinCondition,
PVC_INCLUDE_PLACEHOLDERS);
foreach(l, vars)
{
Var *var = (Var *) lfirst(l);
TargetEntry *tle;
if (IsA(var, Var) && var->varno == result_relation)
continue; /* don't need it */
if (tlist_member((Expr *) var, tlist))
continue; /* already got it */
tle = makeTargetEntry((Expr *) var,
list_length(tlist) + 1,
NULL, true);
tlist = lappend(tlist, tle);
}
}
/*

View File

@@ -3716,6 +3716,7 @@ create_lockrows_path(PlannerInfo *root, RelOptInfo *rel,
* 'onconflict' is the ON CONFLICT clause, or NULL
* 'epqParam' is the ID of Param for EvalPlanQual re-eval
* 'mergeActionLists' is a list of lists of MERGE actions (one per rel)
* 'mergeJoinConditions' is a list of join conditions for MERGE (one per rel)
*/
ModifyTablePath *
create_modifytable_path(PlannerInfo *root, RelOptInfo *rel,
@@ -3727,7 +3728,8 @@ create_modifytable_path(PlannerInfo *root, RelOptInfo *rel,
List *updateColnosLists,
List *withCheckOptionLists, List *returningLists,
List *rowMarks, OnConflictExpr *onconflict,
List *mergeActionLists, int epqParam)
List *mergeActionLists, List *mergeJoinConditions,
int epqParam)
{
ModifyTablePath *pathnode = makeNode(ModifyTablePath);
@@ -3795,6 +3797,7 @@ create_modifytable_path(PlannerInfo *root, RelOptInfo *rel,
pathnode->onconflict = onconflict;
pathnode->epqParam = epqParam;
pathnode->mergeActionLists = mergeActionLists;
pathnode->mergeJoinConditions = mergeJoinConditions;
return pathnode;
}