1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-10 17:42:29 +03:00

Add support for MERGE SQL command

MERGE performs actions that modify rows in the target table using a
source table or query. MERGE provides a single SQL statement that can
conditionally INSERT/UPDATE/DELETE rows -- a task that would otherwise
require multiple PL statements.  For example,

MERGE INTO target AS t
USING source AS s
ON t.tid = s.sid
WHEN MATCHED AND t.balance > s.delta THEN
  UPDATE SET balance = t.balance - s.delta
WHEN MATCHED THEN
  DELETE
WHEN NOT MATCHED AND s.delta > 0 THEN
  INSERT VALUES (s.sid, s.delta)
WHEN NOT MATCHED THEN
  DO NOTHING;

MERGE works with regular tables, partitioned tables and inheritance
hierarchies, including column and row security enforcement, as well as
support for row and statement triggers and transition tables therein.

MERGE is optimized for OLTP and is parameterizable, though also useful
for large scale ETL/ELT. MERGE is not intended to be used in preference
to existing single SQL commands for INSERT, UPDATE or DELETE since there
is some overhead.  MERGE can be used from PL/pgSQL.

MERGE does not support targetting updatable views or foreign tables, and
RETURNING clauses are not allowed either.  These limitations are likely
fixable with sufficient effort.  Rewrite rules are also not supported,
but it's not clear that we'd want to support them.

Author: Pavan Deolasee <pavan.deolasee@gmail.com>
Author: Álvaro Herrera <alvherre@alvh.no-ip.org>
Author: Amit Langote <amitlangote09@gmail.com>
Author: Simon Riggs <simon.riggs@enterprisedb.com>
Reviewed-by: Peter Eisentraut <peter.eisentraut@enterprisedb.com>
Reviewed-by: Andres Freund <andres@anarazel.de> (earlier versions)
Reviewed-by: Peter Geoghegan <pg@bowt.ie> (earlier versions)
Reviewed-by: Robert Haas <robertmhaas@gmail.com> (earlier versions)
Reviewed-by: Japin Li <japinli@hotmail.com>
Reviewed-by: Justin Pryzby <pryzby@telsasoft.com>
Reviewed-by: Tomas Vondra <tomas.vondra@enterprisedb.com>
Reviewed-by: Zhihong Yu <zyu@yugabyte.com>
Discussion: https://postgr.es/m/CANP8+jKitBSrB7oTgT9CY2i1ObfOt36z0XMraQc+Xrz8QB0nXA@mail.gmail.com
Discussion: https://postgr.es/m/CAH2-WzkJdBuxj9PO=2QaO9-3h3xGbQPZ34kJH=HukRekwM-GZg@mail.gmail.com
Discussion: https://postgr.es/m/20201231134736.GA25392@alvherre.pgsql
This commit is contained in:
Alvaro Herrera
2022-03-28 16:45:58 +02:00
parent ae63017bdb
commit 7103ebb7aa
95 changed files with 8726 additions and 167 deletions

View File

@@ -228,6 +228,7 @@ _copyModifyTable(const ModifyTable *from)
COPY_NODE_FIELD(onConflictWhere);
COPY_SCALAR_FIELD(exclRelRTI);
COPY_NODE_FIELD(exclRelTlist);
COPY_NODE_FIELD(mergeActionLists);
return newnode;
}
@@ -2888,6 +2889,35 @@ _copyCommonTableExpr(const CommonTableExpr *from)
return newnode;
}
static MergeWhenClause *
_copyMergeWhenClause(const MergeWhenClause *from)
{
MergeWhenClause *newnode = makeNode(MergeWhenClause);
COPY_SCALAR_FIELD(matched);
COPY_SCALAR_FIELD(commandType);
COPY_SCALAR_FIELD(override);
COPY_NODE_FIELD(condition);
COPY_NODE_FIELD(targetList);
COPY_NODE_FIELD(values);
return newnode;
}
static MergeAction *
_copyMergeAction(const MergeAction *from)
{
MergeAction *newnode = makeNode(MergeAction);
COPY_SCALAR_FIELD(matched);
COPY_SCALAR_FIELD(commandType);
COPY_SCALAR_FIELD(override);
COPY_NODE_FIELD(qual);
COPY_NODE_FIELD(targetList);
COPY_NODE_FIELD(updateColnos);
return newnode;
}
static A_Expr *
_copyA_Expr(const A_Expr *from)
{
@@ -3394,6 +3424,8 @@ _copyQuery(const Query *from)
COPY_NODE_FIELD(setOperations);
COPY_NODE_FIELD(constraintDeps);
COPY_NODE_FIELD(withCheckOptions);
COPY_NODE_FIELD(mergeActionList);
COPY_SCALAR_FIELD(mergeUseOuterJoin);
COPY_LOCATION_FIELD(stmt_location);
COPY_SCALAR_FIELD(stmt_len);
@@ -3457,6 +3489,20 @@ _copyUpdateStmt(const UpdateStmt *from)
return newnode;
}
static MergeStmt *
_copyMergeStmt(const MergeStmt *from)
{
MergeStmt *newnode = makeNode(MergeStmt);
COPY_NODE_FIELD(relation);
COPY_NODE_FIELD(sourceRelation);
COPY_NODE_FIELD(joinCondition);
COPY_NODE_FIELD(mergeWhenClauses);
COPY_NODE_FIELD(withClause);
return newnode;
}
static SelectStmt *
_copySelectStmt(const SelectStmt *from)
{
@@ -5662,6 +5708,9 @@ copyObjectImpl(const void *from)
case T_UpdateStmt:
retval = _copyUpdateStmt(from);
break;
case T_MergeStmt:
retval = _copyMergeStmt(from);
break;
case T_SelectStmt:
retval = _copySelectStmt(from);
break;
@@ -6136,6 +6185,12 @@ copyObjectImpl(const void *from)
case T_CommonTableExpr:
retval = _copyCommonTableExpr(from);
break;
case T_MergeWhenClause:
retval = _copyMergeWhenClause(from);
break;
case T_MergeAction:
retval = _copyMergeAction(from);
break;
case T_ObjectWithArgs:
retval = _copyObjectWithArgs(from);
break;

View File

@@ -1146,6 +1146,8 @@ _equalQuery(const Query *a, const Query *b)
COMPARE_NODE_FIELD(setOperations);
COMPARE_NODE_FIELD(constraintDeps);
COMPARE_NODE_FIELD(withCheckOptions);
COMPARE_NODE_FIELD(mergeActionList);
COMPARE_SCALAR_FIELD(mergeUseOuterJoin);
COMPARE_LOCATION_FIELD(stmt_location);
COMPARE_SCALAR_FIELD(stmt_len);
@@ -1201,6 +1203,18 @@ _equalUpdateStmt(const UpdateStmt *a, const UpdateStmt *b)
return true;
}
static bool
_equalMergeStmt(const MergeStmt *a, const MergeStmt *b)
{
COMPARE_NODE_FIELD(relation);
COMPARE_NODE_FIELD(sourceRelation);
COMPARE_NODE_FIELD(joinCondition);
COMPARE_NODE_FIELD(mergeWhenClauses);
COMPARE_NODE_FIELD(withClause);
return true;
}
static bool
_equalSelectStmt(const SelectStmt *a, const SelectStmt *b)
{
@@ -3118,6 +3132,32 @@ _equalCommonTableExpr(const CommonTableExpr *a, const CommonTableExpr *b)
return true;
}
static bool
_equalMergeWhenClause(const MergeWhenClause *a, const MergeWhenClause *b)
{
COMPARE_SCALAR_FIELD(matched);
COMPARE_SCALAR_FIELD(commandType);
COMPARE_SCALAR_FIELD(override);
COMPARE_NODE_FIELD(condition);
COMPARE_NODE_FIELD(targetList);
COMPARE_NODE_FIELD(values);
return true;
}
static bool
_equalMergeAction(const MergeAction *a, const MergeAction *b)
{
COMPARE_SCALAR_FIELD(matched);
COMPARE_SCALAR_FIELD(commandType);
COMPARE_SCALAR_FIELD(override);
COMPARE_NODE_FIELD(qual);
COMPARE_NODE_FIELD(targetList);
COMPARE_NODE_FIELD(updateColnos);
return true;
}
static bool
_equalXmlSerialize(const XmlSerialize *a, const XmlSerialize *b)
{
@@ -3576,6 +3616,9 @@ equal(const void *a, const void *b)
case T_UpdateStmt:
retval = _equalUpdateStmt(a, b);
break;
case T_MergeStmt:
retval = _equalMergeStmt(a, b);
break;
case T_SelectStmt:
retval = _equalSelectStmt(a, b);
break;
@@ -4050,6 +4093,12 @@ equal(const void *a, const void *b)
case T_CommonTableExpr:
retval = _equalCommonTableExpr(a, b);
break;
case T_MergeWhenClause:
retval = _equalMergeWhenClause(a, b);
break;
case T_MergeAction:
retval = _equalMergeAction(a, b);
break;
case T_ObjectWithArgs:
retval = _equalObjectWithArgs(a, b);
break;

View File

@@ -2303,6 +2303,16 @@ expression_tree_walker(Node *node,
return true;
}
break;
case T_MergeAction:
{
MergeAction *action = (MergeAction *) node;
if (walker(action->targetList, context))
return true;
if (walker(action->qual, context))
return true;
}
break;
case T_PartitionPruneStepOp:
{
PartitionPruneStepOp *opstep = (PartitionPruneStepOp *) node;
@@ -2463,6 +2473,8 @@ query_tree_walker(Query *query,
return true;
if (walker((Node *) query->onConflict, context))
return true;
if (walker((Node *) query->mergeActionList, context))
return true;
if (walker((Node *) query->returningList, context))
return true;
if (walker((Node *) query->jointree, context))
@@ -3252,6 +3264,18 @@ expression_tree_mutator(Node *node,
return (Node *) newnode;
}
break;
case T_MergeAction:
{
MergeAction *action = (MergeAction *) node;
MergeAction *newnode;
FLATCOPY(newnode, action, MergeAction);
MUTATE(newnode->qual, action->qual, Node *);
MUTATE(newnode->targetList, action->targetList, List *);
return (Node *) newnode;
}
break;
case T_PartitionPruneStepOp:
{
PartitionPruneStepOp *opstep = (PartitionPruneStepOp *) node;
@@ -3464,6 +3488,7 @@ query_tree_mutator(Query *query,
MUTATE(query->targetList, query->targetList, List *);
MUTATE(query->withCheckOptions, query->withCheckOptions, List *);
MUTATE(query->onConflict, query->onConflict, OnConflictExpr *);
MUTATE(query->mergeActionList, query->mergeActionList, List *);
MUTATE(query->returningList, query->returningList, List *);
MUTATE(query->jointree, query->jointree, FromExpr *);
MUTATE(query->setOperations, query->setOperations, Node *);
@@ -3656,9 +3681,9 @@ query_or_expression_tree_mutator(Node *node,
* boundaries: we descend to everything that's possibly interesting.
*
* Currently, the node type coverage here extends only to DML statements
* (SELECT/INSERT/UPDATE/DELETE) and nodes that can appear in them, because
* this is used mainly during analysis of CTEs, and only DML statements can
* appear in CTEs.
* (SELECT/INSERT/UPDATE/DELETE/MERGE) and nodes that can appear in them,
* because this is used mainly during analysis of CTEs, and only DML
* statements can appear in CTEs.
*/
bool
raw_expression_tree_walker(Node *node,
@@ -3839,6 +3864,34 @@ raw_expression_tree_walker(Node *node,
return true;
}
break;
case T_MergeStmt:
{
MergeStmt *stmt = (MergeStmt *) node;
if (walker(stmt->relation, context))
return true;
if (walker(stmt->sourceRelation, context))
return true;
if (walker(stmt->joinCondition, context))
return true;
if (walker(stmt->mergeWhenClauses, context))
return true;
if (walker(stmt->withClause, context))
return true;
}
break;
case T_MergeWhenClause:
{
MergeWhenClause *mergeWhenClause = (MergeWhenClause *) node;
if (walker(mergeWhenClause->condition, context))
return true;
if (walker(mergeWhenClause->targetList, context))
return true;
if (walker(mergeWhenClause->values, context))
return true;
}
break;
case T_SelectStmt:
{
SelectStmt *stmt = (SelectStmt *) node;

View File

@@ -429,6 +429,7 @@ _outModifyTable(StringInfo str, const ModifyTable *node)
WRITE_NODE_FIELD(onConflictWhere);
WRITE_UINT_FIELD(exclRelRTI);
WRITE_NODE_FIELD(exclRelTlist);
WRITE_NODE_FIELD(mergeActionLists);
}
static void
@@ -2250,6 +2251,7 @@ _outModifyTablePath(StringInfo str, const ModifyTablePath *node)
WRITE_NODE_FIELD(rowMarks);
WRITE_NODE_FIELD(onconflict);
WRITE_INT_FIELD(epqParam);
WRITE_NODE_FIELD(mergeActionLists);
}
static void
@@ -3143,6 +3145,8 @@ _outQuery(StringInfo str, const Query *node)
WRITE_NODE_FIELD(setOperations);
WRITE_NODE_FIELD(constraintDeps);
WRITE_NODE_FIELD(withCheckOptions);
WRITE_NODE_FIELD(mergeActionList);
WRITE_BOOL_FIELD(mergeUseOuterJoin);
WRITE_LOCATION_FIELD(stmt_location);
WRITE_INT_FIELD(stmt_len);
}
@@ -3271,6 +3275,32 @@ _outCommonTableExpr(StringInfo str, const CommonTableExpr *node)
WRITE_NODE_FIELD(ctecolcollations);
}
static void
_outMergeWhenClause(StringInfo str, const MergeWhenClause *node)
{
WRITE_NODE_TYPE("MERGEWHENCLAUSE");
WRITE_BOOL_FIELD(matched);
WRITE_ENUM_FIELD(commandType, CmdType);
WRITE_ENUM_FIELD(override, OverridingKind);
WRITE_NODE_FIELD(condition);
WRITE_NODE_FIELD(targetList);
WRITE_NODE_FIELD(values);
}
static void
_outMergeAction(StringInfo str, const MergeAction *node)
{
WRITE_NODE_TYPE("MERGEACTION");
WRITE_BOOL_FIELD(matched);
WRITE_ENUM_FIELD(commandType, CmdType);
WRITE_ENUM_FIELD(override, OverridingKind);
WRITE_NODE_FIELD(qual);
WRITE_NODE_FIELD(targetList);
WRITE_NODE_FIELD(updateColnos);
}
static void
_outSetOperationStmt(StringInfo str, const SetOperationStmt *node)
{
@@ -4480,6 +4510,12 @@ outNode(StringInfo str, const void *obj)
case T_CommonTableExpr:
_outCommonTableExpr(str, obj);
break;
case T_MergeWhenClause:
_outMergeWhenClause(str, obj);
break;
case T_MergeAction:
_outMergeAction(str, obj);
break;
case T_SetOperationStmt:
_outSetOperationStmt(str, obj);
break;

View File

@@ -283,6 +283,8 @@ _readQuery(void)
READ_NODE_FIELD(setOperations);
READ_NODE_FIELD(constraintDeps);
READ_NODE_FIELD(withCheckOptions);
READ_NODE_FIELD(mergeActionList);
READ_BOOL_FIELD(mergeUseOuterJoin);
READ_LOCATION_FIELD(stmt_location);
READ_INT_FIELD(stmt_len);
@@ -472,6 +474,42 @@ _readCommonTableExpr(void)
READ_DONE();
}
/*
* _readMergeWhenClause
*/
static MergeWhenClause *
_readMergeWhenClause(void)
{
READ_LOCALS(MergeWhenClause);
READ_BOOL_FIELD(matched);
READ_ENUM_FIELD(commandType, CmdType);
READ_NODE_FIELD(condition);
READ_NODE_FIELD(targetList);
READ_NODE_FIELD(values);
READ_ENUM_FIELD(override, OverridingKind);
READ_DONE();
}
/*
* _readMergeAction
*/
static MergeAction *
_readMergeAction(void)
{
READ_LOCALS(MergeAction);
READ_BOOL_FIELD(matched);
READ_ENUM_FIELD(commandType, CmdType);
READ_ENUM_FIELD(override, OverridingKind);
READ_NODE_FIELD(qual);
READ_NODE_FIELD(targetList);
READ_NODE_FIELD(updateColnos);
READ_DONE();
}
/*
* _readSetOperationStmt
*/
@@ -1765,6 +1803,7 @@ _readModifyTable(void)
READ_NODE_FIELD(onConflictWhere);
READ_UINT_FIELD(exclRelRTI);
READ_NODE_FIELD(exclRelTlist);
READ_NODE_FIELD(mergeActionLists);
READ_DONE();
}
@@ -2809,6 +2848,10 @@ parseNodeString(void)
return_value = _readCTECycleClause();
else if (MATCH("COMMONTABLEEXPR", 15))
return_value = _readCommonTableExpr();
else if (MATCH("MERGEWHENCLAUSE", 15))
return_value = _readMergeWhenClause();
else if (MATCH("MERGEACTION", 11))
return_value = _readMergeAction();
else if (MATCH("SETOPERATIONSTMT", 16))
return_value = _readSetOperationStmt();
else if (MATCH("ALIAS", 5))