diff options
author | Dean Rasheed | 2025-01-16 14:57:35 +0000 |
---|---|---|
committer | Dean Rasheed | 2025-01-16 14:57:35 +0000 |
commit | 80feb727c869cc0b2e12bd1543bafa449be9c8e2 (patch) | |
tree | 27fb43ef4b09067e3d725e1b918539d492a8550c /doc/src/sgml/rules.sgml | |
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 'doc/src/sgml/rules.sgml')
-rw-r--r-- | doc/src/sgml/rules.sgml | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/doc/src/sgml/rules.sgml b/doc/src/sgml/rules.sgml index 7e98f5694b4..9fdf8b1d917 100644 --- a/doc/src/sgml/rules.sgml +++ b/doc/src/sgml/rules.sgml @@ -1646,6 +1646,23 @@ CREATE RULE shoelace_ins AS ON INSERT TO shoelace </para> <para> + Note that in the <literal>RETURNING</literal> clause of a rule, + <literal>OLD</literal> and <literal>NEW</literal> refer to the + pseudorelations added as extra range table entries to the rewritten + query, rather than old/new rows in the result relation. Thus, for + example, in a rule supporting <command>UPDATE</command> queries on this + view, if the <literal>RETURNING</literal> clause contained + <literal>old.sl_name</literal>, the old name would always be returned, + regardless of whether the <literal>RETURNING</literal> clause in the + query on the view specified <literal>OLD</literal> or <literal>NEW</literal>, + which might be confusing. To avoid this confusion, and support returning + old and new values in queries on the view, the <literal>RETURNING</literal> + clause in the rule definition should refer to entries from the result + relation such as <literal>shoelace_data.sl_name</literal>, without + specifying <literal>OLD</literal> or <literal>NEW</literal>. + </para> + + <para> Now assume that once in a while, a pack of shoelaces arrives at the shop and a big parts list along with it. But you don't want to manually update the <literal>shoelace</literal> view every |