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/executor/execExpr.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/executor/execExpr.h')
| -rw-r--r-- | src/include/executor/execExpr.h | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/src/include/executor/execExpr.h b/src/include/executor/execExpr.h index 1e42c131781..5371e344ecd 100644 --- a/src/include/executor/execExpr.h +++ b/src/include/executor/execExpr.h @@ -26,9 +26,9 @@ struct JsonConstructorExprState; /* Bits in ExprState->flags (see also execnodes.h for public flag bits): */ /* expression's interpreter has been initialized */ -#define EEO_FLAG_INTERPRETER_INITIALIZED (1 << 1) +#define EEO_FLAG_INTERPRETER_INITIALIZED (1 << 5) /* jump-threading is in use */ -#define EEO_FLAG_DIRECT_THREADED (1 << 2) +#define EEO_FLAG_DIRECT_THREADED (1 << 6) /* Typical API for out-of-line evaluation subroutines */ typedef void (*ExecEvalSubroutine) (ExprState *state, @@ -72,16 +72,22 @@ typedef enum ExprEvalOp EEOP_INNER_FETCHSOME, EEOP_OUTER_FETCHSOME, EEOP_SCAN_FETCHSOME, + EEOP_OLD_FETCHSOME, + EEOP_NEW_FETCHSOME, /* compute non-system Var value */ EEOP_INNER_VAR, EEOP_OUTER_VAR, EEOP_SCAN_VAR, + EEOP_OLD_VAR, + EEOP_NEW_VAR, /* compute system Var value */ EEOP_INNER_SYSVAR, EEOP_OUTER_SYSVAR, EEOP_SCAN_SYSVAR, + EEOP_OLD_SYSVAR, + EEOP_NEW_SYSVAR, /* compute wholerow Var */ EEOP_WHOLEROW, @@ -94,6 +100,8 @@ typedef enum ExprEvalOp EEOP_ASSIGN_INNER_VAR, EEOP_ASSIGN_OUTER_VAR, EEOP_ASSIGN_SCAN_VAR, + EEOP_ASSIGN_OLD_VAR, + EEOP_ASSIGN_NEW_VAR, /* assign ExprState's resvalue/resnull to a column of its resultslot */ EEOP_ASSIGN_TMP, @@ -178,6 +186,7 @@ typedef enum ExprEvalOp EEOP_SQLVALUEFUNCTION, EEOP_CURRENTOFEXPR, EEOP_NEXTVALUEEXPR, + EEOP_RETURNINGEXPR, EEOP_ARRAYEXPR, EEOP_ARRAYCOERCE, EEOP_ROW, @@ -301,7 +310,7 @@ typedef struct ExprEvalStep */ union { - /* for EEOP_INNER/OUTER/SCAN_FETCHSOME */ + /* for EEOP_INNER/OUTER/SCAN/OLD/NEW_FETCHSOME */ struct { /* attribute number up to which to fetch (inclusive) */ @@ -314,13 +323,14 @@ typedef struct ExprEvalStep const TupleTableSlotOps *kind; } fetch; - /* for EEOP_INNER/OUTER/SCAN_[SYS]VAR[_FIRST] */ + /* for EEOP_INNER/OUTER/SCAN/OLD/NEW_[SYS]VAR */ struct { /* attnum is attr number - 1 for regular VAR ... */ /* but it's just the normal (negative) attr number for SYSVAR */ int attnum; Oid vartype; /* type OID of variable */ + VarReturningType varreturningtype; /* return old/new/default */ } var; /* for EEOP_WHOLEROW */ @@ -349,6 +359,13 @@ typedef struct ExprEvalStep int resultnum; } assign_tmp; + /* for EEOP_RETURNINGEXPR */ + struct + { + uint8 nullflag; /* flag to test if OLD/NEW row is NULL */ + int jumpdone; /* jump here if OLD/NEW row is NULL */ + } returningexpr; + /* for EEOP_CONST */ struct { |
