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/include/nodes/execnodes.h | |
parent | 7407b2d48cf37bc8847ae6c47dde2164ef2faa34 (diff) |
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/include/nodes/execnodes.h')
-rw-r--r-- | src/include/nodes/execnodes.h | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.h index b3f7aa299f5..d0f2dca5928 100644 --- a/src/include/nodes/execnodes.h +++ b/src/include/nodes/execnodes.h @@ -74,11 +74,20 @@ typedef Datum (*ExprStateEvalFunc) (struct ExprState *expression, /* Bits in ExprState->flags (see also execExpr.h for private flag bits): */ /* expression is for use with ExecQual() */ #define EEO_FLAG_IS_QUAL (1 << 0) +/* expression refers to OLD table columns */ +#define EEO_FLAG_HAS_OLD (1 << 1) +/* expression refers to NEW table columns */ +#define EEO_FLAG_HAS_NEW (1 << 2) +/* OLD table row is NULL in RETURNING list */ +#define EEO_FLAG_OLD_IS_NULL (1 << 3) +/* NEW table row is NULL in RETURNING list */ +#define EEO_FLAG_NEW_IS_NULL (1 << 4) typedef struct ExprState { NodeTag type; +#define FIELDNO_EXPRSTATE_FLAGS 1 uint8 flags; /* bitmask of EEO_FLAG_* bits, see above */ /* @@ -290,6 +299,12 @@ typedef struct ExprContext #define FIELDNO_EXPRCONTEXT_DOMAINNULL 13 bool domainValue_isNull; + /* Tuples that OLD/NEW Var nodes in RETURNING may refer to */ +#define FIELDNO_EXPRCONTEXT_OLDTUPLE 14 + TupleTableSlot *ecxt_oldtuple; +#define FIELDNO_EXPRCONTEXT_NEWTUPLE 15 + TupleTableSlot *ecxt_newtuple; + /* Link to containing EState (NULL if a standalone ExprContext) */ struct EState *ecxt_estate; @@ -504,6 +519,7 @@ typedef struct ResultRelInfo TupleTableSlot *ri_ReturningSlot; /* for trigger output tuples */ TupleTableSlot *ri_TrigOldSlot; /* for a trigger's old tuple */ TupleTableSlot *ri_TrigNewSlot; /* for a trigger's new tuple */ + TupleTableSlot *ri_AllNullSlot; /* for RETURNING OLD/NEW */ /* FDW callback functions, if foreign table */ struct FdwRoutine *ri_FdwRoutine; |