From 0294df2f1f842dfb0eed79007b21016f486a3c6c Mon Sep 17 00:00:00 2001 From: Dean Rasheed Date: Sat, 30 Mar 2024 10:00:26 +0000 Subject: 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 --- src/backend/executor/execPartition.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'src/backend/executor/execPartition.c') diff --git a/src/backend/executor/execPartition.c b/src/backend/executor/execPartition.c index 64fcb012dbe..bb14dcbe6fa 100644 --- a/src/backend/executor/execPartition.c +++ b/src/backend/executor/execPartition.c @@ -880,6 +880,7 @@ ExecInitPartitionInfo(ModifyTableState *mtstate, EState *estate, List *firstMergeActionList = linitial(node->mergeActionLists); ListCell *lc; ExprContext *econtext = mtstate->ps.ps_ExprContext; + Node *joinCondition; if (part_attmap == NULL) part_attmap = @@ -890,23 +891,31 @@ ExecInitPartitionInfo(ModifyTableState *mtstate, EState *estate, if (unlikely(!leaf_part_rri->ri_projectNewInfoValid)) ExecInitMergeTupleSlots(mtstate, leaf_part_rri); + /* Initialize state for join condition checking. */ + joinCondition = + map_variable_attnos(linitial(node->mergeJoinConditions), + firstVarno, 0, + part_attmap, + RelationGetForm(partrel)->reltype, + &found_whole_row); + /* We ignore the value of found_whole_row. */ + leaf_part_rri->ri_MergeJoinCondition = + ExecInitQual((List *) joinCondition, &mtstate->ps); + foreach(lc, firstMergeActionList) { /* Make a copy for this relation to be safe. */ MergeAction *action = copyObject(lfirst(lc)); MergeActionState *action_state; - List **list; /* Generate the action's state for this relation */ action_state = makeNode(MergeActionState); action_state->mas_action = action; /* And put the action in the appropriate list */ - if (action->matched) - list = &leaf_part_rri->ri_matchedMergeAction; - else - list = &leaf_part_rri->ri_notMatchedMergeAction; - *list = lappend(*list, action_state); + leaf_part_rri->ri_MergeActions[action->matchKind] = + lappend(leaf_part_rri->ri_MergeActions[action->matchKind], + action_state); switch (action->commandType) { -- cgit v1.2.3