mirror of
https://github.com/postgres/postgres.git
synced 2025-06-27 23:21:58 +03:00
Add RETURNING support to MERGE.
This allows a RETURNING clause to be appended to a MERGE query, to return values based on each row inserted, updated, or deleted. As with plain INSERT, UPDATE, and DELETE commands, the returned values are based on the new contents of the target table for INSERT and UPDATE actions, and on its old contents for DELETE actions. Values from the source relation may also be returned. As with INSERT/UPDATE/DELETE, the output of MERGE ... RETURNING may be used as the source relation for other operations such as WITH queries and COPY commands. Additionally, a special function merge_action() is provided, which returns 'INSERT', 'UPDATE', or 'DELETE', depending on the action executed for each row. The merge_action() function can be used anywhere in the RETURNING list, including in arbitrary expressions and subqueries, but it is an error to use it anywhere outside of a MERGE query's RETURNING list. Dean Rasheed, reviewed by Isaac Morland, Vik Fearing, Alvaro Herrera, Gurjeet Singh, Jian He, Jeff Davis, Merlin Moncure, Peter Eisentraut, and Wolfgang Walther. Discussion: http://postgr.es/m/CAEZATCWePEGQR5LBn-vD6SfeLZafzEm2Qy_L_Oky2=qw2w3Pzg@mail.gmail.com
This commit is contained in:
@ -54,6 +54,7 @@ static Node *transformAExprDistinct(ParseState *pstate, A_Expr *a);
|
||||
static Node *transformAExprNullIf(ParseState *pstate, A_Expr *a);
|
||||
static Node *transformAExprIn(ParseState *pstate, A_Expr *a);
|
||||
static Node *transformAExprBetween(ParseState *pstate, A_Expr *a);
|
||||
static Node *transformMergeSupportFunc(ParseState *pstate, MergeSupportFunc *f);
|
||||
static Node *transformBoolExpr(ParseState *pstate, BoolExpr *a);
|
||||
static Node *transformFuncCall(ParseState *pstate, FuncCall *fn);
|
||||
static Node *transformMultiAssignRef(ParseState *pstate, MultiAssignRef *maref);
|
||||
@ -227,6 +228,11 @@ transformExprRecurse(ParseState *pstate, Node *expr)
|
||||
result = transformGroupingFunc(pstate, (GroupingFunc *) expr);
|
||||
break;
|
||||
|
||||
case T_MergeSupportFunc:
|
||||
result = transformMergeSupportFunc(pstate,
|
||||
(MergeSupportFunc *) expr);
|
||||
break;
|
||||
|
||||
case T_NamedArgExpr:
|
||||
{
|
||||
NamedArgExpr *na = (NamedArgExpr *) expr;
|
||||
@ -541,6 +547,7 @@ transformColumnRef(ParseState *pstate, ColumnRef *cref)
|
||||
case EXPR_KIND_LIMIT:
|
||||
case EXPR_KIND_OFFSET:
|
||||
case EXPR_KIND_RETURNING:
|
||||
case EXPR_KIND_MERGE_RETURNING:
|
||||
case EXPR_KIND_VALUES:
|
||||
case EXPR_KIND_VALUES_SINGLE:
|
||||
case EXPR_KIND_CHECK_CONSTRAINT:
|
||||
@ -1353,6 +1360,31 @@ transformAExprBetween(ParseState *pstate, A_Expr *a)
|
||||
return transformExprRecurse(pstate, result);
|
||||
}
|
||||
|
||||
static Node *
|
||||
transformMergeSupportFunc(ParseState *pstate, MergeSupportFunc *f)
|
||||
{
|
||||
/*
|
||||
* All we need to do is check that we're in the RETURNING list of a MERGE
|
||||
* command. If so, we just return the node as-is.
|
||||
*/
|
||||
if (pstate->p_expr_kind != EXPR_KIND_MERGE_RETURNING)
|
||||
{
|
||||
ParseState *parent_pstate = pstate->parentParseState;
|
||||
|
||||
while (parent_pstate &&
|
||||
parent_pstate->p_expr_kind != EXPR_KIND_MERGE_RETURNING)
|
||||
parent_pstate = parent_pstate->parentParseState;
|
||||
|
||||
if (!parent_pstate)
|
||||
ereport(ERROR,
|
||||
errcode(ERRCODE_SYNTAX_ERROR),
|
||||
errmsg("MERGE_ACTION() can only be used in the RETURNING list of a MERGE command"),
|
||||
parser_errposition(pstate, f->location));
|
||||
}
|
||||
|
||||
return (Node *) f;
|
||||
}
|
||||
|
||||
static Node *
|
||||
transformBoolExpr(ParseState *pstate, BoolExpr *a)
|
||||
{
|
||||
@ -1767,6 +1799,7 @@ transformSubLink(ParseState *pstate, SubLink *sublink)
|
||||
case EXPR_KIND_LIMIT:
|
||||
case EXPR_KIND_OFFSET:
|
||||
case EXPR_KIND_RETURNING:
|
||||
case EXPR_KIND_MERGE_RETURNING:
|
||||
case EXPR_KIND_VALUES:
|
||||
case EXPR_KIND_VALUES_SINGLE:
|
||||
case EXPR_KIND_CYCLE_MARK:
|
||||
@ -3115,6 +3148,7 @@ ParseExprKindName(ParseExprKind exprKind)
|
||||
case EXPR_KIND_OFFSET:
|
||||
return "OFFSET";
|
||||
case EXPR_KIND_RETURNING:
|
||||
case EXPR_KIND_MERGE_RETURNING:
|
||||
return "RETURNING";
|
||||
case EXPR_KIND_VALUES:
|
||||
case EXPR_KIND_VALUES_SINGLE:
|
||||
|
Reference in New Issue
Block a user