diff options
author | Dean Rasheed <dean.a.rasheed@gmail.com> | 2025-01-16 14:57:35 +0000 |
---|---|---|
committer | Dean Rasheed <dean.a.rasheed@gmail.com> | 2025-01-16 14:57:35 +0000 |
commit | 80feb727c869cc0b2e12bd1543bafa449be9c8e2 (patch) | |
tree | 27fb43ef4b09067e3d725e1b918539d492a8550c /src/backend/nodes/nodeFuncs.c | |
parent | 7407b2d48cf37bc8847ae6c47dde2164ef2faa34 (diff) | |
download | postgresql-80feb727c869cc0b2e12bd1543bafa449be9c8e2.tar.gz postgresql-80feb727c869cc0b2e12bd1543bafa449be9c8e2.zip |
Add OLD/NEW support to RETURNING in DML queries.
This allows the RETURNING list of INSERT/UPDATE/DELETE/MERGE queries
to explicitly return old and new values by using the special aliases
"old" and "new", which are automatically added to the query (if not
already defined) while parsing its RETURNING list, allowing things
like:
RETURNING old.colname, new.colname, ...
RETURNING old.*, new.*
Additionally, a new syntax is supported, allowing the names "old" and
"new" to be changed to user-supplied alias names, e.g.:
RETURNING WITH (OLD AS o, NEW AS n) o.colname, n.colname, ...
This is useful when the names "old" and "new" are already defined,
such as inside trigger functions, allowing backwards compatibility to
be maintained -- the interpretation of any existing queries that
happen to already refer to relations called "old" or "new", or use
those as aliases for other relations, is not changed.
For an INSERT, old values will generally be NULL, and for a DELETE,
new values will generally be NULL, but that may change for an INSERT
with an ON CONFLICT ... DO UPDATE clause, or if a query rewrite rule
changes the command type. Therefore, we put no restrictions on the use
of old and new in any DML queries.
Dean Rasheed, reviewed by Jian He and Jeff Davis.
Discussion: https://postgr.es/m/CAEZATCWx0J0-v=Qjc6gXzR=KtsdvAE7Ow=D=mu50AgOe+pvisQ@mail.gmail.com
Diffstat (limited to 'src/backend/nodes/nodeFuncs.c')
-rw-r--r-- | src/backend/nodes/nodeFuncs.c | 44 |
1 files changed, 39 insertions, 5 deletions
diff --git a/src/backend/nodes/nodeFuncs.c b/src/backend/nodes/nodeFuncs.c index df779137c9d..7bc823507f1 100644 --- a/src/backend/nodes/nodeFuncs.c +++ b/src/backend/nodes/nodeFuncs.c @@ -278,6 +278,9 @@ exprType(const Node *expr) type = exprType((Node *) n->expr); } break; + case T_ReturningExpr: + type = exprType((Node *) ((const ReturningExpr *) expr)->retexpr); + break; case T_PlaceHolderVar: type = exprType((Node *) ((const PlaceHolderVar *) expr)->phexpr); break; @@ -529,6 +532,8 @@ exprTypmod(const Node *expr) return ((const CoerceToDomainValue *) expr)->typeMod; case T_SetToDefault: return ((const SetToDefault *) expr)->typeMod; + case T_ReturningExpr: + return exprTypmod((Node *) ((const ReturningExpr *) expr)->retexpr); case T_PlaceHolderVar: return exprTypmod((Node *) ((const PlaceHolderVar *) expr)->phexpr); default: @@ -1047,6 +1052,9 @@ exprCollation(const Node *expr) case T_InferenceElem: coll = exprCollation((Node *) ((const InferenceElem *) expr)->expr); break; + case T_ReturningExpr: + coll = exprCollation((Node *) ((const ReturningExpr *) expr)->retexpr); + break; case T_PlaceHolderVar: coll = exprCollation((Node *) ((const PlaceHolderVar *) expr)->phexpr); break; @@ -1110,7 +1118,7 @@ exprInputCollation(const Node *expr) * Assign collation information to an expression tree node. * * Note: since this is only used during parse analysis, we don't need to - * worry about subplans or PlaceHolderVars. + * worry about subplans, PlaceHolderVars, or ReturningExprs. */ void exprSetCollation(Node *expr, Oid collation) @@ -1624,6 +1632,9 @@ exprLocation(const Node *expr) case T_SetToDefault: loc = ((const SetToDefault *) expr)->location; break; + case T_ReturningExpr: + loc = exprLocation((Node *) ((const ReturningExpr *) expr)->retexpr); + break; case T_TargetEntry: /* just use argument's location */ loc = exprLocation((Node *) ((const TargetEntry *) expr)->expr); @@ -2613,6 +2624,8 @@ expression_tree_walker_impl(Node *node, return WALK(((PlaceHolderVar *) node)->phexpr); case T_InferenceElem: return WALK(((InferenceElem *) node)->expr); + case T_ReturningExpr: + return WALK(((ReturningExpr *) node)->retexpr); case T_AppendRelInfo: { AppendRelInfo *appinfo = (AppendRelInfo *) node; @@ -3454,6 +3467,16 @@ expression_tree_mutator_impl(Node *node, return (Node *) newnode; } break; + case T_ReturningExpr: + { + ReturningExpr *rexpr = (ReturningExpr *) node; + ReturningExpr *newnode; + + FLATCOPY(newnode, rexpr, ReturningExpr); + MUTATE(newnode->retexpr, rexpr->retexpr, Expr *); + return (Node *) newnode; + } + break; case T_TargetEntry: { TargetEntry *targetentry = (TargetEntry *) node; @@ -4005,6 +4028,7 @@ raw_expression_tree_walker_impl(Node *node, case T_A_Const: case T_A_Star: case T_MergeSupportFunc: + case T_ReturningOption: /* primitive node types with no subnodes */ break; case T_Alias: @@ -4233,7 +4257,7 @@ raw_expression_tree_walker_impl(Node *node, return true; if (WALK(stmt->onConflictClause)) return true; - if (WALK(stmt->returningList)) + if (WALK(stmt->returningClause)) return true; if (WALK(stmt->withClause)) return true; @@ -4249,7 +4273,7 @@ raw_expression_tree_walker_impl(Node *node, return true; if (WALK(stmt->whereClause)) return true; - if (WALK(stmt->returningList)) + if (WALK(stmt->returningClause)) return true; if (WALK(stmt->withClause)) return true; @@ -4267,7 +4291,7 @@ raw_expression_tree_walker_impl(Node *node, return true; if (WALK(stmt->fromClause)) return true; - if (WALK(stmt->returningList)) + if (WALK(stmt->returningClause)) return true; if (WALK(stmt->withClause)) return true; @@ -4285,7 +4309,7 @@ raw_expression_tree_walker_impl(Node *node, return true; if (WALK(stmt->mergeWhenClauses)) return true; - if (WALK(stmt->returningList)) + if (WALK(stmt->returningClause)) return true; if (WALK(stmt->withClause)) return true; @@ -4303,6 +4327,16 @@ raw_expression_tree_walker_impl(Node *node, return true; } break; + case T_ReturningClause: + { + ReturningClause *returning = (ReturningClause *) node; + + if (WALK(returning->options)) + return true; + if (WALK(returning->exprs)) + return true; + } + break; case T_SelectStmt: { SelectStmt *stmt = (SelectStmt *) node; |