diff options
author | Dean Rasheed <dean.a.rasheed@gmail.com> | 2024-03-30 10:00:26 +0000 |
---|---|---|
committer | Dean Rasheed <dean.a.rasheed@gmail.com> | 2024-03-30 10:00:26 +0000 |
commit | 0294df2f1f842dfb0eed79007b21016f486a3c6c (patch) | |
tree | d01da8c03b91a25a438f05dd7dccdb95d13f8f92 /src/backend/utils/adt/ruleutils.c | |
parent | 46e5441fa536b89c1123f270fdfeeb72c320b901 (diff) | |
download | postgresql-0294df2f1f842dfb0eed79007b21016f486a3c6c.tar.gz postgresql-0294df2f1f842dfb0eed79007b21016f486a3c6c.zip |
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
Diffstat (limited to 'src/backend/utils/adt/ruleutils.c')
-rw-r--r-- | src/backend/utils/adt/ruleutils.c | 41 |
1 files changed, 39 insertions, 2 deletions
diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index a51717e36ce..0f7f40c50f6 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -7112,6 +7112,7 @@ get_merge_query_def(Query *query, deparse_context *context, StringInfo buf = context->buf; RangeTblEntry *rte; ListCell *lc; + bool haveNotMatchedBySource; /* Insert the WITH clause if given */ get_with_clause(query, context); @@ -7137,7 +7138,26 @@ get_merge_query_def(Query *query, deparse_context *context, get_from_clause(query, " USING ", context); appendContextKeyword(context, " ON ", -PRETTYINDENT_STD, PRETTYINDENT_STD, 2); - get_rule_expr(query->jointree->quals, context, false); + get_rule_expr(query->mergeJoinCondition, context, false); + + /* + * Test for any NOT MATCHED BY SOURCE actions. If there are none, then + * any NOT MATCHED BY TARGET actions are output as "WHEN NOT MATCHED", per + * SQL standard. Otherwise, we have a non-SQL-standard query, so output + * "BY SOURCE" / "BY TARGET" qualifiers for all NOT MATCHED actions, to be + * more explicit. + */ + haveNotMatchedBySource = false; + foreach(lc, query->mergeActionList) + { + MergeAction *action = lfirst_node(MergeAction, lc); + + if (action->matchKind == MERGE_WHEN_NOT_MATCHED_BY_SOURCE) + { + haveNotMatchedBySource = true; + break; + } + } /* Print each merge action */ foreach(lc, query->mergeActionList) @@ -7146,7 +7166,24 @@ get_merge_query_def(Query *query, deparse_context *context, appendContextKeyword(context, " WHEN ", -PRETTYINDENT_STD, PRETTYINDENT_STD, 2); - appendStringInfo(buf, "%sMATCHED", action->matched ? "" : "NOT "); + switch (action->matchKind) + { + case MERGE_WHEN_MATCHED: + appendStringInfo(buf, "MATCHED"); + break; + case MERGE_WHEN_NOT_MATCHED_BY_SOURCE: + appendStringInfo(buf, "NOT MATCHED BY SOURCE"); + break; + case MERGE_WHEN_NOT_MATCHED_BY_TARGET: + if (haveNotMatchedBySource) + appendStringInfo(buf, "NOT MATCHED BY TARGET"); + else + appendStringInfo(buf, "NOT MATCHED"); + break; + default: + elog(ERROR, "unrecognized matchKind: %d", + (int) action->matchKind); + } if (action->qual) { |