INSERT INTO ft2 (c1,c2,c3) SELECT c1+1000,c2+100, c3 || c3 FROM ft2 LIMIT 20;
INSERT INTO ft2 (c1,c2,c3)
- VALUES (1101,201,'aaa'), (1102,202,'bbb'), (1103,203,'ccc') RETURNING *;
- c1 | c2 | c3 | c4 | c5 | c6 | c7 | c8
-------+-----+-----+----+----+----+------------+----
- 1101 | 201 | aaa | | | | ft2 |
- 1102 | 202 | bbb | | | | ft2 |
- 1103 | 203 | ccc | | | | ft2 |
+ VALUES (1101,201,'aaa'), (1102,202,'bbb'), (1103,203,'ccc') RETURNING old, new, old.*, new.*;
+ old | new | c1 | c2 | c3 | c4 | c5 | c6 | c7 | c8 | c1 | c2 | c3 | c4 | c5 | c6 | c7 | c8
+-----+---------------------------------+----+----+----+----+----+----+----+----+------+-----+-----+----+----+----+------------+----
+ | (1101,201,aaa,,,,"ft2 ",) | | | | | | | | | 1101 | 201 | aaa | | | | ft2 |
+ | (1102,202,bbb,,,,"ft2 ",) | | | | | | | | | 1102 | 202 | bbb | | | | ft2 |
+ | (1103,203,ccc,,,,"ft2 ",) | | | | | | | | | 1103 | 203 | ccc | | | | ft2 |
(3 rows)
INSERT INTO ft2 (c1,c2,c3) VALUES (1104,204,'ddd'), (1105,205,'eee');
1017 | 507 | 0001700017_update7 | | | | ft2 |
(102 rows)
+BEGIN;
+ EXPLAIN (verbose, costs off)
+ UPDATE ft2 SET c2 = c2 + 400, c3 = c3 || '_update7b' WHERE c1 % 10 = 7 AND c1 < 40
+ RETURNING old.*, new.*; -- can't be pushed down
+ QUERY PLAN
+----------------------------------------------------------------------------------------------------------------------------------------------
+ Update on public.ft2
+ Output: old.c1, old.c2, old.c3, old.c4, old.c5, old.c6, old.c7, old.c8, new.c1, new.c2, new.c3, new.c4, new.c5, new.c6, new.c7, new.c8
+ Remote SQL: UPDATE "S 1"."T 1" SET c2 = $2, c3 = $3 WHERE ctid = $1 RETURNING "C 1", c2, c3, c4, c5, c6, c7, c8
+ -> Foreign Scan on public.ft2
+ Output: (c2 + 400), (c3 || '_update7b'::text), ctid, ft2.*
+ Remote SQL: SELECT "C 1", c2, c3, c4, c5, c6, c7, c8, ctid FROM "S 1"."T 1" WHERE (("C 1" < 40)) AND ((("C 1" % 10) = 7)) FOR UPDATE
+(6 rows)
+
+ UPDATE ft2 SET c2 = c2 + 400, c3 = c3 || '_update7b' WHERE c1 % 10 = 7 AND c1 < 40
+ RETURNING old.*, new.*;
+ c1 | c2 | c3 | c4 | c5 | c6 | c7 | c8 | c1 | c2 | c3 | c4 | c5 | c6 | c7 | c8
+----+-----+---------------+------------------------------+--------------------------+----+------------+-----+----+-----+------------------------+------------------------------+--------------------------+----+------------+-----
+ 7 | 407 | 00007_update7 | Thu Jan 08 00:00:00 1970 PST | Thu Jan 08 00:00:00 1970 | 7 | 7 | foo | 7 | 807 | 00007_update7_update7b | Thu Jan 08 00:00:00 1970 PST | Thu Jan 08 00:00:00 1970 | 7 | 7 | foo
+ 17 | 407 | 00017_update7 | Sun Jan 18 00:00:00 1970 PST | Sun Jan 18 00:00:00 1970 | 7 | 7 | foo | 17 | 807 | 00017_update7_update7b | Sun Jan 18 00:00:00 1970 PST | Sun Jan 18 00:00:00 1970 | 7 | 7 | foo
+ 27 | 407 | 00027_update7 | Wed Jan 28 00:00:00 1970 PST | Wed Jan 28 00:00:00 1970 | 7 | 7 | foo | 27 | 807 | 00027_update7_update7b | Wed Jan 28 00:00:00 1970 PST | Wed Jan 28 00:00:00 1970 | 7 | 7 | foo
+ 37 | 407 | 00037_update7 | Sat Feb 07 00:00:00 1970 PST | Sat Feb 07 00:00:00 1970 | 7 | 7 | foo | 37 | 807 | 00037_update7_update7b | Sat Feb 07 00:00:00 1970 PST | Sat Feb 07 00:00:00 1970 | 7 | 7 | foo
+(4 rows)
+
+ROLLBACK;
EXPLAIN (verbose, costs off)
UPDATE ft2 SET c2 = ft2.c2 + 500, c3 = ft2.c3 || '_update9', c7 = DEFAULT
FROM ft1 WHERE ft1.c1 = ft2.c2 AND ft1.c1 % 10 = 9; -- can be pushed down
1105 |
(103 rows)
+BEGIN;
+ EXPLAIN (verbose, costs off)
+ DELETE FROM ft2 WHERE c1 % 10 = 6 AND c1 < 40 RETURNING old.c1, c4; -- can't be pushed down
+ QUERY PLAN
+-----------------------------------------------------------------------------------------------------------
+ Delete on public.ft2
+ Output: old.c1, c4
+ Remote SQL: DELETE FROM "S 1"."T 1" WHERE ctid = $1 RETURNING "C 1", c4
+ -> Foreign Scan on public.ft2
+ Output: ctid
+ Remote SQL: SELECT ctid FROM "S 1"."T 1" WHERE (("C 1" < 40)) AND ((("C 1" % 10) = 6)) FOR UPDATE
+(6 rows)
+
+ DELETE FROM ft2 WHERE c1 % 10 = 6 AND c1 < 40 RETURNING old.c1, c4;
+ c1 | c4
+----+------------------------------
+ 6 | Wed Jan 07 00:00:00 1970 PST
+ 16 | Sat Jan 17 00:00:00 1970 PST
+ 26 | Tue Jan 27 00:00:00 1970 PST
+ 36 | Fri Feb 06 00:00:00 1970 PST
+(4 rows)
+
+ROLLBACK;
EXPLAIN (verbose, costs off)
DELETE FROM ft2 USING ft1 WHERE ft1.c1 = ft2.c2 AND ft1.c1 % 10 = 2; -- can be pushed down
QUERY PLAN
(1296,96,foo,,,,"ft2 ",) | 1296 | 96 | foo | | | | ft2 | | (96,97,AAA096) | 96 | 97 | AAA096
(16 rows)
+BEGIN;
+ EXPLAIN (verbose, costs off)
+ UPDATE ft2 SET c3 = 'bar'
+ FROM ft4 INNER JOIN ft5 ON (ft4.c1 = ft5.c1)
+ WHERE ft2.c1 > 1200 AND ft2.c2 = ft4.c1
+ RETURNING old, new, ft2, ft2.*, ft4, ft4.*; -- can't be pushed down
+ QUERY PLAN
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ Update on public.ft2
+ Output: old.*, new.*, ft2.*, ft2.c1, ft2.c2, ft2.c3, ft2.c4, ft2.c5, ft2.c6, ft2.c7, ft2.c8, ft4.*, ft4.c1, ft4.c2, ft4.c3
+ Remote SQL: UPDATE "S 1"."T 1" SET c3 = $2 WHERE ctid = $1 RETURNING "C 1", c2, c3, c4, c5, c6, c7, c8
+ -> Foreign Scan
+ Output: 'bar'::text, ft2.ctid, ft2.*, ft4.*, ft5.*, ft4.c1, ft4.c2, ft4.c3
+ Relations: ((public.ft2) INNER JOIN (public.ft4)) INNER JOIN (public.ft5)
+ Remote SQL: SELECT r1.ctid, CASE WHEN (r1.*)::text IS NOT NULL THEN ROW(r1."C 1", r1.c2, r1.c3, r1.c4, r1.c5, r1.c6, r1.c7, r1.c8) END, CASE WHEN (r2.*)::text IS NOT NULL THEN ROW(r2.c1, r2.c2, r2.c3) END, CASE WHEN (r3.*)::text IS NOT NULL THEN ROW(r3.c1, r3.c2, r3.c3) END, r2.c1, r2.c2, r2.c3 FROM (("S 1"."T 1" r1 INNER JOIN "S 1"."T 3" r2 ON (((r1.c2 = r2.c1)) AND ((r1."C 1" > 1200)))) INNER JOIN "S 1"."T 4" r3 ON (((r2.c1 = r3.c1)))) FOR UPDATE OF r1
+ -> Nested Loop
+ Output: ft2.ctid, ft2.*, ft4.*, ft5.*, ft4.c1, ft4.c2, ft4.c3
+ Join Filter: (ft4.c1 = ft5.c1)
+ -> Sort
+ Output: ft2.ctid, ft2.*, ft2.c2, ft4.*, ft4.c1, ft4.c2, ft4.c3
+ Sort Key: ft2.c2
+ -> Hash Join
+ Output: ft2.ctid, ft2.*, ft2.c2, ft4.*, ft4.c1, ft4.c2, ft4.c3
+ Hash Cond: (ft2.c2 = ft4.c1)
+ -> Foreign Scan on public.ft2
+ Output: ft2.ctid, ft2.*, ft2.c2
+ Remote SQL: SELECT "C 1", c2, c3, c4, c5, c6, c7, c8, ctid FROM "S 1"."T 1" WHERE (("C 1" > 1200)) FOR UPDATE
+ -> Hash
+ Output: ft4.*, ft4.c1, ft4.c2, ft4.c3
+ -> Foreign Scan on public.ft4
+ Output: ft4.*, ft4.c1, ft4.c2, ft4.c3
+ Remote SQL: SELECT c1, c2, c3 FROM "S 1"."T 3"
+ -> Materialize
+ Output: ft5.*, ft5.c1
+ -> Foreign Scan on public.ft5
+ Output: ft5.*, ft5.c1
+ Remote SQL: SELECT c1, c2, c3 FROM "S 1"."T 4"
+(29 rows)
+
+ UPDATE ft2 SET c3 = 'bar'
+ FROM ft4 INNER JOIN ft5 ON (ft4.c1 = ft5.c1)
+ WHERE ft2.c1 > 1200 AND ft2.c2 = ft4.c1
+ RETURNING old, new, ft2, ft2.*, ft4, ft4.*;
+ old | new | ft2 | c1 | c2 | c3 | c4 | c5 | c6 | c7 | c8 | ft4 | c1 | c2 | c3
+--------------------------------+--------------------------------+--------------------------------+------+----+-----+----+----+----+------------+----+----------------+----+----+--------
+ (1206,6,foo,,,,"ft2 ",) | (1206,6,bar,,,,"ft2 ",) | (1206,6,bar,,,,"ft2 ",) | 1206 | 6 | bar | | | | ft2 | | (6,7,AAA006) | 6 | 7 | AAA006
+ (1212,12,foo,,,,"ft2 ",) | (1212,12,bar,,,,"ft2 ",) | (1212,12,bar,,,,"ft2 ",) | 1212 | 12 | bar | | | | ft2 | | (12,13,AAA012) | 12 | 13 | AAA012
+ (1224,24,foo,,,,"ft2 ",) | (1224,24,bar,,,,"ft2 ",) | (1224,24,bar,,,,"ft2 ",) | 1224 | 24 | bar | | | | ft2 | | (24,25,AAA024) | 24 | 25 | AAA024
+ (1230,30,foo,,,,"ft2 ",) | (1230,30,bar,,,,"ft2 ",) | (1230,30,bar,,,,"ft2 ",) | 1230 | 30 | bar | | | | ft2 | | (30,31,AAA030) | 30 | 31 | AAA030
+ (1242,42,foo,,,,"ft2 ",) | (1242,42,bar,,,,"ft2 ",) | (1242,42,bar,,,,"ft2 ",) | 1242 | 42 | bar | | | | ft2 | | (42,43,AAA042) | 42 | 43 | AAA042
+ (1248,48,foo,,,,"ft2 ",) | (1248,48,bar,,,,"ft2 ",) | (1248,48,bar,,,,"ft2 ",) | 1248 | 48 | bar | | | | ft2 | | (48,49,AAA048) | 48 | 49 | AAA048
+ (1260,60,foo,,,,"ft2 ",) | (1260,60,bar,,,,"ft2 ",) | (1260,60,bar,,,,"ft2 ",) | 1260 | 60 | bar | | | | ft2 | | (60,61,AAA060) | 60 | 61 | AAA060
+ (1266,66,foo,,,,"ft2 ",) | (1266,66,bar,,,,"ft2 ",) | (1266,66,bar,,,,"ft2 ",) | 1266 | 66 | bar | | | | ft2 | | (66,67,AAA066) | 66 | 67 | AAA066
+ (1278,78,foo,,,,"ft2 ",) | (1278,78,bar,,,,"ft2 ",) | (1278,78,bar,,,,"ft2 ",) | 1278 | 78 | bar | | | | ft2 | | (78,79,AAA078) | 78 | 79 | AAA078
+ (1284,84,foo,,,,"ft2 ",) | (1284,84,bar,,,,"ft2 ",) | (1284,84,bar,,,,"ft2 ",) | 1284 | 84 | bar | | | | ft2 | | (84,85,AAA084) | 84 | 85 | AAA084
+ (1296,96,foo,,,,"ft2 ",) | (1296,96,bar,,,,"ft2 ",) | (1296,96,bar,,,,"ft2 ",) | 1296 | 96 | bar | | | | ft2 | | (96,97,AAA096) | 96 | 97 | AAA096
+ (1218,18,foo,,,,"ft2 ",) | (1218,18,bar,,,,"ft2 ",) | (1218,18,bar,,,,"ft2 ",) | 1218 | 18 | bar | | | | ft2 | | (18,19,AAA018) | 18 | 19 | AAA018
+ (1236,36,foo,,,,"ft2 ",) | (1236,36,bar,,,,"ft2 ",) | (1236,36,bar,,,,"ft2 ",) | 1236 | 36 | bar | | | | ft2 | | (36,37,AAA036) | 36 | 37 | AAA036
+ (1254,54,foo,,,,"ft2 ",) | (1254,54,bar,,,,"ft2 ",) | (1254,54,bar,,,,"ft2 ",) | 1254 | 54 | bar | | | | ft2 | | (54,55,AAA054) | 54 | 55 | AAA054
+ (1272,72,foo,,,,"ft2 ",) | (1272,72,bar,,,,"ft2 ",) | (1272,72,bar,,,,"ft2 ",) | 1272 | 72 | bar | | | | ft2 | | (72,73,AAA072) | 72 | 73 | AAA072
+ (1290,90,foo,,,,"ft2 ",) | (1290,90,bar,,,,"ft2 ",) | (1290,90,bar,,,,"ft2 ",) | 1290 | 90 | bar | | | | ft2 | | (90,91,AAA090) | 90 | 91 | AAA090
+(16 rows)
+
+ROLLBACK;
EXPLAIN (verbose, costs off)
DELETE FROM ft2
USING ft4 LEFT JOIN ft5 ON (ft4.c1 = ft5.c1)
INSERT INTO ft2 (c1,c2,c3) SELECT c1+1000,c2+100, c3 || c3 FROM ft2 LIMIT 20;
INSERT INTO ft2 (c1,c2,c3) SELECT c1+1000,c2+100, c3 || c3 FROM ft2 LIMIT 20;
INSERT INTO ft2 (c1,c2,c3)
- VALUES (1101,201,'aaa'), (1102,202,'bbb'), (1103,203,'ccc') RETURNING *;
+ VALUES (1101,201,'aaa'), (1102,202,'bbb'), (1103,203,'ccc') RETURNING old, new, old.*, new.*;
INSERT INTO ft2 (c1,c2,c3) VALUES (1104,204,'ddd'), (1105,205,'eee');
EXPLAIN (verbose, costs off)
UPDATE ft2 SET c2 = c2 + 300, c3 = c3 || '_update3' WHERE c1 % 10 = 3; -- can be pushed down
EXPLAIN (verbose, costs off)
UPDATE ft2 SET c2 = c2 + 400, c3 = c3 || '_update7' WHERE c1 % 10 = 7 RETURNING *; -- can be pushed down
UPDATE ft2 SET c2 = c2 + 400, c3 = c3 || '_update7' WHERE c1 % 10 = 7 RETURNING *;
+BEGIN;
+ EXPLAIN (verbose, costs off)
+ UPDATE ft2 SET c2 = c2 + 400, c3 = c3 || '_update7b' WHERE c1 % 10 = 7 AND c1 < 40
+ RETURNING old.*, new.*; -- can't be pushed down
+ UPDATE ft2 SET c2 = c2 + 400, c3 = c3 || '_update7b' WHERE c1 % 10 = 7 AND c1 < 40
+ RETURNING old.*, new.*;
+ROLLBACK;
EXPLAIN (verbose, costs off)
UPDATE ft2 SET c2 = ft2.c2 + 500, c3 = ft2.c3 || '_update9', c7 = DEFAULT
FROM ft1 WHERE ft1.c1 = ft2.c2 AND ft1.c1 % 10 = 9; -- can be pushed down
EXPLAIN (verbose, costs off)
DELETE FROM ft2 WHERE c1 % 10 = 5 RETURNING c1, c4; -- can be pushed down
DELETE FROM ft2 WHERE c1 % 10 = 5 RETURNING c1, c4;
+BEGIN;
+ EXPLAIN (verbose, costs off)
+ DELETE FROM ft2 WHERE c1 % 10 = 6 AND c1 < 40 RETURNING old.c1, c4; -- can't be pushed down
+ DELETE FROM ft2 WHERE c1 % 10 = 6 AND c1 < 40 RETURNING old.c1, c4;
+ROLLBACK;
EXPLAIN (verbose, costs off)
DELETE FROM ft2 USING ft1 WHERE ft1.c1 = ft2.c2 AND ft1.c1 % 10 = 2; -- can be pushed down
DELETE FROM ft2 USING ft1 WHERE ft1.c1 = ft2.c2 AND ft1.c1 % 10 = 2;
FROM ft4 INNER JOIN ft5 ON (ft4.c1 = ft5.c1)
WHERE ft2.c1 > 1200 AND ft2.c2 = ft4.c1
RETURNING ft2, ft2.*, ft4, ft4.*;
+BEGIN;
+ EXPLAIN (verbose, costs off)
+ UPDATE ft2 SET c3 = 'bar'
+ FROM ft4 INNER JOIN ft5 ON (ft4.c1 = ft5.c1)
+ WHERE ft2.c1 > 1200 AND ft2.c2 = ft4.c1
+ RETURNING old, new, ft2, ft2.*, ft4, ft4.*; -- can't be pushed down
+ UPDATE ft2 SET c3 = 'bar'
+ FROM ft4 INNER JOIN ft5 ON (ft4.c1 = ft5.c1)
+ WHERE ft2.c1 > 1200 AND ft2.c2 = ft4.c1
+ RETURNING old, new, ft2, ft2.*, ft4, ft4.*;
+ROLLBACK;
EXPLAIN (verbose, costs off)
DELETE FROM ft2
USING ft4 LEFT JOIN ft5 ON (ft4.c1 = ft5.c1)
</para>
<para>
- In an <command>INSERT</command>, the data available to <literal>RETURNING</literal> is
+ In an <command>INSERT</command>, the default data available to
+ <literal>RETURNING</literal> is
the row as it was inserted. This is not so useful in trivial inserts,
since it would just repeat the data provided by the client. But it can
be very handy when relying on computed default values. For example,
</para>
<para>
- In an <command>UPDATE</command>, the data available to <literal>RETURNING</literal> is
+ In an <command>UPDATE</command>, the default data available to
+ <literal>RETURNING</literal> is
the new content of the modified row. For example:
<programlisting>
UPDATE products SET price = price * 1.10
</para>
<para>
- In a <command>DELETE</command>, the data available to <literal>RETURNING</literal> is
+ In a <command>DELETE</command>, the default data available to
+ <literal>RETURNING</literal> is
the content of the deleted row. For example:
<programlisting>
DELETE FROM products
</para>
<para>
- In a <command>MERGE</command>, the data available to <literal>RETURNING</literal> is
+ In a <command>MERGE</command>, the default data available to
+ <literal>RETURNING</literal> is
the content of the source row plus the content of the inserted, updated, or
deleted target row. Since it is quite common for the source and target to
have many of the same columns, specifying <literal>RETURNING *</literal>
</programlisting>
</para>
+ <para>
+ In each of these commands, it is also possible to explicitly return the
+ old and new content of the modified row. For example:
+<programlisting>
+UPDATE products SET price = price * 1.10
+ WHERE price <= 99.99
+ RETURNING name, old.price AS old_price, new.price AS new_price,
+ new.price - old.price AS price_change;
+</programlisting>
+ In this example, writing <literal>new.price</literal> is the same as
+ just writing <literal>price</literal>, but it makes the meaning clearer.
+ </para>
+
+ <para>
+ This syntax for returning old and new values is available in
+ <command>INSERT</command>, <command>UPDATE</command>,
+ <command>DELETE</command>, and <command>MERGE</command> commands, but
+ typically old values will be <literal>NULL</literal> for an
+ <command>INSERT</command>, and new values will be <literal>NULL</literal>
+ for a <command>DELETE</command>. However, there are situations where it
+ can still be useful for those commands. For example, in an
+ <command>INSERT</command> with an
+ <link linkend="sql-on-conflict"><literal>ON CONFLICT DO UPDATE</literal></link>
+ clause, the old values will be non-<literal>NULL</literal> for conflicting
+ rows. Similarly, if a <command>DELETE</command> is turned into an
+ <command>UPDATE</command> by a <link linkend="sql-createrule">rewrite rule</link>,
+ the new values may be non-<literal>NULL</literal>.
+ </para>
+
<para>
If there are triggers (<xref linkend="triggers"/>) on the target table,
the data available to <literal>RETURNING</literal> is the row as modified by
DELETE FROM [ ONLY ] <replaceable class="parameter">table_name</replaceable> [ * ] [ [ AS ] <replaceable class="parameter">alias</replaceable> ]
[ USING <replaceable class="parameter">from_item</replaceable> [, ...] ]
[ WHERE <replaceable class="parameter">condition</replaceable> | WHERE CURRENT OF <replaceable class="parameter">cursor_name</replaceable> ]
- [ RETURNING { * | <replaceable class="parameter">output_expression</replaceable> [ [ AS ] <replaceable class="parameter">output_name</replaceable> ] } [, ...] ]
+ [ RETURNING [ WITH ( { OLD | NEW } AS <replaceable class="parameter">output_alias</replaceable> [, ...] ) ]
+ { * | <replaceable class="parameter">output_expression</replaceable> [ [ AS ] <replaceable class="parameter">output_name</replaceable> ] } [, ...] ]
</synopsis>
</refsynopsisdiv>
</listitem>
</varlistentry>
+ <varlistentry>
+ <term><replaceable class="parameter">output_alias</replaceable></term>
+ <listitem>
+ <para>
+ An optional substitute name for <literal>OLD</literal> or
+ <literal>NEW</literal> rows in the <literal>RETURNING</literal> list.
+ </para>
+
+ <para>
+ By default, old values from the target table can be returned by writing
+ <literal>OLD.<replaceable class="parameter">column_name</replaceable></literal>
+ or <literal>OLD.*</literal>, and new values can be returned by writing
+ <literal>NEW.<replaceable class="parameter">column_name</replaceable></literal>
+ or <literal>NEW.*</literal>. When an alias is provided, these names are
+ hidden and the old or new rows must be referred to using the alias.
+ For example <literal>RETURNING WITH (OLD AS o, NEW AS n) o.*, n.*</literal>.
+ </para>
+ </listitem>
+ </varlistentry>
+
<varlistentry>
<term><replaceable class="parameter">output_expression</replaceable></term>
<listitem>
or table(s) listed in <literal>USING</literal>.
Write <literal>*</literal> to return all columns.
</para>
+
+ <para>
+ A column name or <literal>*</literal> may be qualified using
+ <literal>OLD</literal> or <literal>NEW</literal>, or the corresponding
+ <replaceable class="parameter">output_alias</replaceable> for
+ <literal>OLD</literal> or <literal>NEW</literal>, to cause old or new
+ values to be returned. An unqualified column name, or
+ <literal>*</literal>, or a column name or <literal>*</literal> qualified
+ using the target table name or alias will return old values.
+ </para>
+
+ <para>
+ For a simple <command>DELETE</command>, all new values will be
+ <literal>NULL</literal>. However, if an <literal>ON DELETE</literal>
+ rule causes an <command>INSERT</command> or <command>UPDATE</command>
+ to be executed instead, the new values may be non-<literal>NULL</literal>.
+ </para>
</listitem>
</varlistentry>
[ OVERRIDING { SYSTEM | USER } VALUE ]
{ DEFAULT VALUES | VALUES ( { <replaceable class="parameter">expression</replaceable> | DEFAULT } [, ...] ) [, ...] | <replaceable class="parameter">query</replaceable> }
[ ON CONFLICT [ <replaceable class="parameter">conflict_target</replaceable> ] <replaceable class="parameter">conflict_action</replaceable> ]
- [ RETURNING { * | <replaceable class="parameter">output_expression</replaceable> [ [ AS ] <replaceable class="parameter">output_name</replaceable> ] } [, ...] ]
+ [ RETURNING [ WITH ( { OLD | NEW } AS <replaceable class="parameter">output_alias</replaceable> [, ...] ) ]
+ { * | <replaceable class="parameter">output_expression</replaceable> [ [ AS ] <replaceable class="parameter">output_name</replaceable> ] } [, ...] ]
<phrase>where <replaceable class="parameter">conflict_target</replaceable> can be one of:</phrase>
</listitem>
</varlistentry>
+ <varlistentry>
+ <term><replaceable class="parameter">output_alias</replaceable></term>
+ <listitem>
+ <para>
+ An optional substitute name for <literal>OLD</literal> or
+ <literal>NEW</literal> rows in the <literal>RETURNING</literal> list.
+ </para>
+
+ <para>
+ By default, old values from the target table can be returned by writing
+ <literal>OLD.<replaceable class="parameter">column_name</replaceable></literal>
+ or <literal>OLD.*</literal>, and new values can be returned by writing
+ <literal>NEW.<replaceable class="parameter">column_name</replaceable></literal>
+ or <literal>NEW.*</literal>. When an alias is provided, these names are
+ hidden and the old or new rows must be referred to using the alias.
+ For example <literal>RETURNING WITH (OLD AS o, NEW AS n) o.*, n.*</literal>.
+ </para>
+ </listitem>
+ </varlistentry>
+
<varlistentry>
<term><replaceable class="parameter">output_expression</replaceable></term>
<listitem>
<literal>*</literal> to return all columns of the inserted or updated
row(s).
</para>
+
+ <para>
+ A column name or <literal>*</literal> may be qualified using
+ <literal>OLD</literal> or <literal>NEW</literal>, or the corresponding
+ <replaceable class="parameter">output_alias</replaceable> for
+ <literal>OLD</literal> or <literal>NEW</literal>, to cause old or new
+ values to be returned. An unqualified column name, or
+ <literal>*</literal>, or a column name or <literal>*</literal>
+ qualified using the target table name or alias will return new values.
+ </para>
+
+ <para>
+ For a simple <command>INSERT</command>, all old values will be
+ <literal>NULL</literal>. However, for an <command>INSERT</command>
+ with an <literal>ON CONFLICT DO UPDATE</literal> clause, the old
+ values may be non-<literal>NULL</literal>.
+ </para>
</listitem>
</varlistentry>
INSERT INTO distributors (did, dname)
VALUES (5, 'Gizmo Transglobal'), (6, 'Associated Computing, Inc')
ON CONFLICT (did) DO UPDATE SET dname = EXCLUDED.dname;
+</programlisting>
+ </para>
+ <para>
+ Insert or update new distributors as above, returning information
+ about any existing values that were updated, together with the new data
+ inserted. Note that the returned values for <literal>old_did</literal>
+ and <literal>old_dname</literal> will be <literal>NULL</literal> for
+ non-conflicting rows:
+<programlisting>
+INSERT INTO distributors (did, dname)
+ VALUES (5, 'Gizmo Transglobal'), (6, 'Associated Computing, Inc')
+ ON CONFLICT (did) DO UPDATE SET dname = EXCLUDED.dname
+ RETURNING old.did AS old_did, old.dname AS old_dname,
+ new.did AS new_did, new.dname AS new_dname;
</programlisting>
</para>
<para>
MERGE INTO [ ONLY ] <replaceable class="parameter">target_table_name</replaceable> [ * ] [ [ AS ] <replaceable class="parameter">target_alias</replaceable> ]
USING <replaceable class="parameter">data_source</replaceable> ON <replaceable class="parameter">join_condition</replaceable>
<replaceable class="parameter">when_clause</replaceable> [...]
-[ RETURNING { * | <replaceable class="parameter">output_expression</replaceable> [ [ AS ] <replaceable class="parameter">output_name</replaceable> ] } [, ...] ]
+[ RETURNING [ WITH ( { OLD | NEW } AS <replaceable class="parameter">output_alias</replaceable> [, ...] ) ]
+ { * | <replaceable class="parameter">output_expression</replaceable> [ [ AS ] <replaceable class="parameter">output_name</replaceable> ] } [, ...] ]
<phrase>where <replaceable class="parameter">data_source</replaceable> is:</phrase>
</listitem>
</varlistentry>
+ <varlistentry>
+ <term><replaceable class="parameter">output_alias</replaceable></term>
+ <listitem>
+ <para>
+ An optional substitute name for <literal>OLD</literal> or
+ <literal>NEW</literal> rows in the <literal>RETURNING</literal> list.
+ </para>
+ <para>
+ By default, old values from the target table can be returned by writing
+ <literal>OLD.<replaceable class="parameter">column_name</replaceable></literal>
+ or <literal>OLD.*</literal>, and new values can be returned by writing
+ <literal>NEW.<replaceable class="parameter">column_name</replaceable></literal>
+ or <literal>NEW.*</literal>. When an alias is provided, these names are
+ hidden and the old or new rows must be referred to using the alias.
+ For example <literal>RETURNING WITH (OLD AS o, NEW AS n) o.*, n.*</literal>.
+ </para>
+ </listitem>
+ </varlistentry>
+
<varlistentry>
<term><replaceable class="parameter">output_expression</replaceable></term>
<listitem>
qualifying the <literal>*</literal> with the name or alias of the source
or target table.
</para>
+ <para>
+ A column name or <literal>*</literal> may also be qualified using
+ <literal>OLD</literal> or <literal>NEW</literal>, or the corresponding
+ <replaceable class="parameter">output_alias</replaceable> for
+ <literal>OLD</literal> or <literal>NEW</literal>, to cause old or new
+ values from the target table to be returned. An unqualified column
+ name from the target table, or a column name or <literal>*</literal>
+ qualified using the target table name or alias will return new values
+ for <literal>INSERT</literal> and <literal>UPDATE</literal> actions, and
+ old values for <literal>DELETE</literal> actions.
+ </para>
</listitem>
</varlistentry>
UPDATE SET stock = w.stock + s.stock_delta
WHEN MATCHED THEN
DELETE
-RETURNING merge_action(), w.*;
+RETURNING merge_action(), w.winename, old.stock AS old_stock, new.stock AS new_stock;
</programlisting>
The <literal>wine_stock_changes</literal> table might be, for example, a
} [, ...]
[ FROM <replaceable class="parameter">from_item</replaceable> [, ...] ]
[ WHERE <replaceable class="parameter">condition</replaceable> | WHERE CURRENT OF <replaceable class="parameter">cursor_name</replaceable> ]
- [ RETURNING { * | <replaceable class="parameter">output_expression</replaceable> [ [ AS ] <replaceable class="parameter">output_name</replaceable> ] } [, ...] ]
+ [ RETURNING [ WITH ( { OLD | NEW } AS <replaceable class="parameter">output_alias</replaceable> [, ...] ) ]
+ { * | <replaceable class="parameter">output_expression</replaceable> [ [ AS ] <replaceable class="parameter">output_name</replaceable> ] } [, ...] ]
</synopsis>
</refsynopsisdiv>
</listitem>
</varlistentry>
+ <varlistentry>
+ <term><replaceable class="parameter">output_alias</replaceable></term>
+ <listitem>
+ <para>
+ An optional substitute name for <literal>OLD</literal> or
+ <literal>NEW</literal> rows in the <literal>RETURNING</literal> list.
+ </para>
+
+ <para>
+ By default, old values from the target table can be returned by writing
+ <literal>OLD.<replaceable class="parameter">column_name</replaceable></literal>
+ or <literal>OLD.*</literal>, and new values can be returned by writing
+ <literal>NEW.<replaceable class="parameter">column_name</replaceable></literal>
+ or <literal>NEW.*</literal>. When an alias is provided, these names are
+ hidden and the old or new rows must be referred to using the alias.
+ For example <literal>RETURNING WITH (OLD AS o, NEW AS n) o.*, n.*</literal>.
+ </para>
+ </listitem>
+ </varlistentry>
+
<varlistentry>
<term><replaceable class="parameter">output_expression</replaceable></term>
<listitem>
or table(s) listed in <literal>FROM</literal>.
Write <literal>*</literal> to return all columns.
</para>
+
+ <para>
+ A column name or <literal>*</literal> may be qualified using
+ <literal>OLD</literal> or <literal>NEW</literal>, or the corresponding
+ <replaceable class="parameter">output_alias</replaceable> for
+ <literal>OLD</literal> or <literal>NEW</literal>, to cause old or new
+ values to be returned. An unqualified column name, or
+ <literal>*</literal>, or a column name or <literal>*</literal> qualified
+ using the target table name or alias will return new values.
+ </para>
</listitem>
</varlistentry>
</para>
<para>
- Perform the same operation and return the updated entries:
+ Perform the same operation and return the updated entries, and the old
+ precipitation value:
<programlisting>
UPDATE weather SET temp_lo = temp_lo+1, temp_hi = temp_lo+15, prcp = DEFAULT
WHERE city = 'San Francisco' AND date = '2003-07-03'
- RETURNING temp_lo, temp_hi, prcp;
+ RETURNING temp_lo, temp_hi, prcp, old.prcp AS old_prcp;
</programlisting>
</para>
<literal>RETURNING</literal> clause is simply ignored for <command>INSERT</command>.
</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
typedef struct ExprSetupInfo
{
- /* Highest attribute numbers fetched from inner/outer/scan tuple slots: */
+ /*
+ * Highest attribute numbers fetched from inner/outer/scan/old/new tuple
+ * slots:
+ */
AttrNumber last_inner;
AttrNumber last_outer;
AttrNumber last_scan;
+ AttrNumber last_old;
+ AttrNumber last_new;
/* MULTIEXPR SubPlan nodes appearing in the expression: */
List *multiexpr_subplans;
} ExprSetupInfo;
/* INDEX_VAR is handled by default case */
default:
- /* get the tuple from the relation being scanned */
- scratch.opcode = EEOP_ASSIGN_SCAN_VAR;
+
+ /*
+ * Get the tuple from the relation being scanned, or the
+ * old/new tuple slot, if old/new values were requested.
+ */
+ switch (variable->varreturningtype)
+ {
+ case VAR_RETURNING_DEFAULT:
+ scratch.opcode = EEOP_ASSIGN_SCAN_VAR;
+ break;
+ case VAR_RETURNING_OLD:
+ scratch.opcode = EEOP_ASSIGN_OLD_VAR;
+ state->flags |= EEO_FLAG_HAS_OLD;
+ break;
+ case VAR_RETURNING_NEW:
+ scratch.opcode = EEOP_ASSIGN_NEW_VAR;
+ state->flags |= EEO_FLAG_HAS_NEW;
+ break;
+ }
break;
}
int nAssignableCols;
bool sawJunk;
Bitmapset *assignedCols;
- ExprSetupInfo deform = {0, 0, 0, NIL};
+ ExprSetupInfo deform = {0, 0, 0, 0, 0, NIL};
ExprEvalStep scratch = {0};
int outerattnum;
ListCell *lc,
/* system column */
scratch.d.var.attnum = variable->varattno;
scratch.d.var.vartype = variable->vartype;
+ scratch.d.var.varreturningtype = variable->varreturningtype;
switch (variable->varno)
{
case INNER_VAR:
/* INDEX_VAR is handled by default case */
default:
- scratch.opcode = EEOP_SCAN_SYSVAR;
+ switch (variable->varreturningtype)
+ {
+ case VAR_RETURNING_DEFAULT:
+ scratch.opcode = EEOP_SCAN_SYSVAR;
+ break;
+ case VAR_RETURNING_OLD:
+ scratch.opcode = EEOP_OLD_SYSVAR;
+ state->flags |= EEO_FLAG_HAS_OLD;
+ break;
+ case VAR_RETURNING_NEW:
+ scratch.opcode = EEOP_NEW_SYSVAR;
+ state->flags |= EEO_FLAG_HAS_NEW;
+ break;
+ }
break;
}
}
/* regular user column */
scratch.d.var.attnum = variable->varattno - 1;
scratch.d.var.vartype = variable->vartype;
+ scratch.d.var.varreturningtype = variable->varreturningtype;
switch (variable->varno)
{
case INNER_VAR:
/* INDEX_VAR is handled by default case */
default:
- scratch.opcode = EEOP_SCAN_VAR;
+ switch (variable->varreturningtype)
+ {
+ case VAR_RETURNING_DEFAULT:
+ scratch.opcode = EEOP_SCAN_VAR;
+ break;
+ case VAR_RETURNING_OLD:
+ scratch.opcode = EEOP_OLD_VAR;
+ state->flags |= EEO_FLAG_HAS_OLD;
+ break;
+ case VAR_RETURNING_NEW:
+ scratch.opcode = EEOP_NEW_VAR;
+ state->flags |= EEO_FLAG_HAS_NEW;
+ break;
+ }
break;
}
}
break;
}
+ case T_ReturningExpr:
+ {
+ ReturningExpr *rexpr = (ReturningExpr *) node;
+ int retstep;
+
+ /* Skip expression evaluation if OLD/NEW row doesn't exist */
+ scratch.opcode = EEOP_RETURNINGEXPR;
+ scratch.d.returningexpr.nullflag = rexpr->retold ?
+ EEO_FLAG_OLD_IS_NULL : EEO_FLAG_NEW_IS_NULL;
+ scratch.d.returningexpr.jumpdone = -1; /* set below */
+ ExprEvalPushStep(state, &scratch);
+ retstep = state->steps_len - 1;
+
+ /* Steps to evaluate expression to return */
+ ExecInitExprRec(rexpr->retexpr, state, resv, resnull);
+
+ /* Jump target used if OLD/NEW row doesn't exist */
+ state->steps[retstep].d.returningexpr.jumpdone = state->steps_len;
+
+ /* Update ExprState flags */
+ if (rexpr->retold)
+ state->flags |= EEO_FLAG_HAS_OLD;
+ else
+ state->flags |= EEO_FLAG_HAS_NEW;
+
+ break;
+ }
+
default:
elog(ERROR, "unrecognized node type: %d",
(int) nodeTag(node));
static void
ExecCreateExprSetupSteps(ExprState *state, Node *node)
{
- ExprSetupInfo info = {0, 0, 0, NIL};
+ ExprSetupInfo info = {0, 0, 0, 0, 0, NIL};
/* Prescan to find out what we need. */
expr_setup_walker(node, &info);
scratch.resnull = NULL;
/*
- * Add steps deforming the ExprState's inner/outer/scan slots as much as
- * required by any Vars appearing in the expression.
+ * Add steps deforming the ExprState's inner/outer/scan/old/new slots as
+ * much as required by any Vars appearing in the expression.
*/
if (info->last_inner > 0)
{
if (ExecComputeSlotInfo(state, &scratch))
ExprEvalPushStep(state, &scratch);
}
+ if (info->last_old > 0)
+ {
+ scratch.opcode = EEOP_OLD_FETCHSOME;
+ scratch.d.fetch.last_var = info->last_old;
+ scratch.d.fetch.fixed = false;
+ scratch.d.fetch.kind = NULL;
+ scratch.d.fetch.known_desc = NULL;
+ if (ExecComputeSlotInfo(state, &scratch))
+ ExprEvalPushStep(state, &scratch);
+ }
+ if (info->last_new > 0)
+ {
+ scratch.opcode = EEOP_NEW_FETCHSOME;
+ scratch.d.fetch.last_var = info->last_new;
+ scratch.d.fetch.fixed = false;
+ scratch.d.fetch.kind = NULL;
+ scratch.d.fetch.known_desc = NULL;
+ if (ExecComputeSlotInfo(state, &scratch))
+ ExprEvalPushStep(state, &scratch);
+ }
/*
* Add steps to execute any MULTIEXPR SubPlans appearing in the
/* INDEX_VAR is handled by default case */
default:
- info->last_scan = Max(info->last_scan, attnum);
+ switch (variable->varreturningtype)
+ {
+ case VAR_RETURNING_DEFAULT:
+ info->last_scan = Max(info->last_scan, attnum);
+ break;
+ case VAR_RETURNING_OLD:
+ info->last_old = Max(info->last_old, attnum);
+ break;
+ case VAR_RETURNING_NEW:
+ info->last_new = Max(info->last_new, attnum);
+ break;
+ }
break;
}
return false;
* evaluation of the expression will have the same type of slot, with an
* equivalent descriptor.
*
+ * EEOP_OLD_FETCHSOME and EEOP_NEW_FETCHSOME are used to process RETURNING, if
+ * OLD/NEW columns are referred to explicitly. In both cases, the tuple
+ * descriptor comes from the parent scan node, so we treat them the same as
+ * EEOP_SCAN_FETCHSOME.
+ *
* Returns true if the deforming step is required, false otherwise.
*/
static bool
Assert(opcode == EEOP_INNER_FETCHSOME ||
opcode == EEOP_OUTER_FETCHSOME ||
- opcode == EEOP_SCAN_FETCHSOME);
+ opcode == EEOP_SCAN_FETCHSOME ||
+ opcode == EEOP_OLD_FETCHSOME ||
+ opcode == EEOP_NEW_FETCHSOME);
if (op->d.fetch.known_desc != NULL)
{
desc = ExecGetResultType(os);
}
}
- else if (opcode == EEOP_SCAN_FETCHSOME)
+ else if (opcode == EEOP_SCAN_FETCHSOME ||
+ opcode == EEOP_OLD_FETCHSOME ||
+ opcode == EEOP_NEW_FETCHSOME)
{
desc = parent->scandesc;
scratch->d.wholerow.tupdesc = NULL; /* filled at runtime */
scratch->d.wholerow.junkFilter = NULL;
+ /* update ExprState flags if Var refers to OLD/NEW */
+ if (variable->varreturningtype == VAR_RETURNING_OLD)
+ state->flags |= EEO_FLAG_HAS_OLD;
+ else if (variable->varreturningtype == VAR_RETURNING_NEW)
+ state->flags |= EEO_FLAG_HAS_NEW;
+
/*
* If the input tuple came from a subquery, it might contain "resjunk"
* columns (such as GROUP BY or ORDER BY columns), which we don't want to
PlanState *parent = &aggstate->ss.ps;
ExprEvalStep scratch = {0};
bool isCombine = DO_AGGSPLIT_COMBINE(aggstate->aggsplit);
- ExprSetupInfo deform = {0, 0, 0, NIL};
+ ExprSetupInfo deform = {0, 0, 0, 0, 0, NIL};
state->expr = (Expr *) aggstate;
state->parent = parent;
scratch.resnull = &fcinfo->args[0].isnull;
scratch.d.var.attnum = attnum;
scratch.d.var.vartype = TupleDescAttr(desc, attnum)->atttypid;
+ scratch.d.var.varreturningtype = VAR_RETURNING_DEFAULT;
ExprEvalPushStep(state, &scratch);
scratch.opcode = EEOP_INNER_VAR;
scratch.d.var.attnum = attno - 1;
scratch.d.var.vartype = latt->atttypid;
+ scratch.d.var.varreturningtype = VAR_RETURNING_DEFAULT;
scratch.resvalue = &fcinfo->args[0].value;
scratch.resnull = &fcinfo->args[0].isnull;
ExprEvalPushStep(state, &scratch);
scratch.opcode = EEOP_OUTER_VAR;
scratch.d.var.attnum = attno - 1;
scratch.d.var.vartype = ratt->atttypid;
+ scratch.d.var.varreturningtype = VAR_RETURNING_DEFAULT;
scratch.resvalue = &fcinfo->args[1].value;
scratch.resnull = &fcinfo->args[1].isnull;
ExprEvalPushStep(state, &scratch);
scratch.opcode = EEOP_INNER_VAR;
scratch.d.var.attnum = attno;
scratch.d.var.vartype = att->atttypid;
+ scratch.d.var.varreturningtype = VAR_RETURNING_DEFAULT;
scratch.resvalue = &fcinfo->args[0].value;
scratch.resnull = &fcinfo->args[0].isnull;
ExprEvalPushStep(state, &scratch);
scratch.opcode = EEOP_OUTER_VAR;
scratch.d.var.attnum = attno;
scratch.d.var.vartype = att->atttypid;
+ scratch.d.var.varreturningtype = VAR_RETURNING_DEFAULT;
scratch.resvalue = &fcinfo->args[1].value;
scratch.resnull = &fcinfo->args[1].isnull;
ExprEvalPushStep(state, &scratch);
TupleTableSlot *innerslot;
TupleTableSlot *outerslot;
TupleTableSlot *scanslot;
+ TupleTableSlot *oldslot;
+ TupleTableSlot *newslot;
/*
* This array has to be in the same order as enum ExprEvalOp.
&&CASE_EEOP_INNER_FETCHSOME,
&&CASE_EEOP_OUTER_FETCHSOME,
&&CASE_EEOP_SCAN_FETCHSOME,
+ &&CASE_EEOP_OLD_FETCHSOME,
+ &&CASE_EEOP_NEW_FETCHSOME,
&&CASE_EEOP_INNER_VAR,
&&CASE_EEOP_OUTER_VAR,
&&CASE_EEOP_SCAN_VAR,
+ &&CASE_EEOP_OLD_VAR,
+ &&CASE_EEOP_NEW_VAR,
&&CASE_EEOP_INNER_SYSVAR,
&&CASE_EEOP_OUTER_SYSVAR,
&&CASE_EEOP_SCAN_SYSVAR,
+ &&CASE_EEOP_OLD_SYSVAR,
+ &&CASE_EEOP_NEW_SYSVAR,
&&CASE_EEOP_WHOLEROW,
&&CASE_EEOP_ASSIGN_INNER_VAR,
&&CASE_EEOP_ASSIGN_OUTER_VAR,
&&CASE_EEOP_ASSIGN_SCAN_VAR,
+ &&CASE_EEOP_ASSIGN_OLD_VAR,
+ &&CASE_EEOP_ASSIGN_NEW_VAR,
&&CASE_EEOP_ASSIGN_TMP,
&&CASE_EEOP_ASSIGN_TMP_MAKE_RO,
&&CASE_EEOP_CONST,
&&CASE_EEOP_SQLVALUEFUNCTION,
&&CASE_EEOP_CURRENTOFEXPR,
&&CASE_EEOP_NEXTVALUEEXPR,
+ &&CASE_EEOP_RETURNINGEXPR,
&&CASE_EEOP_ARRAYEXPR,
&&CASE_EEOP_ARRAYCOERCE,
&&CASE_EEOP_ROW,
innerslot = econtext->ecxt_innertuple;
outerslot = econtext->ecxt_outertuple;
scanslot = econtext->ecxt_scantuple;
+ oldslot = econtext->ecxt_oldtuple;
+ newslot = econtext->ecxt_newtuple;
#if defined(EEO_USE_COMPUTED_GOTO)
EEO_DISPATCH();
EEO_NEXT();
}
+ EEO_CASE(EEOP_OLD_FETCHSOME)
+ {
+ CheckOpSlotCompatibility(op, oldslot);
+
+ slot_getsomeattrs(oldslot, op->d.fetch.last_var);
+
+ EEO_NEXT();
+ }
+
+ EEO_CASE(EEOP_NEW_FETCHSOME)
+ {
+ CheckOpSlotCompatibility(op, newslot);
+
+ slot_getsomeattrs(newslot, op->d.fetch.last_var);
+
+ EEO_NEXT();
+ }
+
EEO_CASE(EEOP_INNER_VAR)
{
int attnum = op->d.var.attnum;
EEO_NEXT();
}
+ EEO_CASE(EEOP_OLD_VAR)
+ {
+ int attnum = op->d.var.attnum;
+
+ /* See EEOP_INNER_VAR comments */
+
+ Assert(attnum >= 0 && attnum < oldslot->tts_nvalid);
+ *op->resvalue = oldslot->tts_values[attnum];
+ *op->resnull = oldslot->tts_isnull[attnum];
+
+ EEO_NEXT();
+ }
+
+ EEO_CASE(EEOP_NEW_VAR)
+ {
+ int attnum = op->d.var.attnum;
+
+ /* See EEOP_INNER_VAR comments */
+
+ Assert(attnum >= 0 && attnum < newslot->tts_nvalid);
+ *op->resvalue = newslot->tts_values[attnum];
+ *op->resnull = newslot->tts_isnull[attnum];
+
+ EEO_NEXT();
+ }
+
EEO_CASE(EEOP_INNER_SYSVAR)
{
ExecEvalSysVar(state, op, econtext, innerslot);
EEO_NEXT();
}
+ EEO_CASE(EEOP_OLD_SYSVAR)
+ {
+ ExecEvalSysVar(state, op, econtext, oldslot);
+ EEO_NEXT();
+ }
+
+ EEO_CASE(EEOP_NEW_SYSVAR)
+ {
+ ExecEvalSysVar(state, op, econtext, newslot);
+ EEO_NEXT();
+ }
+
EEO_CASE(EEOP_WHOLEROW)
{
/* too complex for an inline implementation */
EEO_NEXT();
}
+ EEO_CASE(EEOP_ASSIGN_OLD_VAR)
+ {
+ int resultnum = op->d.assign_var.resultnum;
+ int attnum = op->d.assign_var.attnum;
+
+ /*
+ * We do not need CheckVarSlotCompatibility here; that was taken
+ * care of at compilation time. But see EEOP_INNER_VAR comments.
+ */
+ Assert(attnum >= 0 && attnum < oldslot->tts_nvalid);
+ Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts);
+ resultslot->tts_values[resultnum] = oldslot->tts_values[attnum];
+ resultslot->tts_isnull[resultnum] = oldslot->tts_isnull[attnum];
+
+ EEO_NEXT();
+ }
+
+ EEO_CASE(EEOP_ASSIGN_NEW_VAR)
+ {
+ int resultnum = op->d.assign_var.resultnum;
+ int attnum = op->d.assign_var.attnum;
+
+ /*
+ * We do not need CheckVarSlotCompatibility here; that was taken
+ * care of at compilation time. But see EEOP_INNER_VAR comments.
+ */
+ Assert(attnum >= 0 && attnum < newslot->tts_nvalid);
+ Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts);
+ resultslot->tts_values[resultnum] = newslot->tts_values[attnum];
+ resultslot->tts_isnull[resultnum] = newslot->tts_isnull[attnum];
+
+ EEO_NEXT();
+ }
+
EEO_CASE(EEOP_ASSIGN_TMP)
{
int resultnum = op->d.assign_tmp.resultnum;
EEO_NEXT();
}
+ EEO_CASE(EEOP_RETURNINGEXPR)
+ {
+ /*
+ * The next op actually evaluates the expression. If the OLD/NEW
+ * row doesn't exist, skip that and return NULL.
+ */
+ if (state->flags & op->d.returningexpr.nullflag)
+ {
+ *op->resvalue = (Datum) 0;
+ *op->resnull = true;
+
+ EEO_JUMP(op->d.returningexpr.jumpdone);
+ }
+
+ EEO_NEXT();
+ }
+
EEO_CASE(EEOP_ARRAYEXPR)
{
/* too complex for an inline implementation */
TupleTableSlot *innerslot;
TupleTableSlot *outerslot;
TupleTableSlot *scanslot;
+ TupleTableSlot *oldslot;
+ TupleTableSlot *newslot;
innerslot = econtext->ecxt_innertuple;
outerslot = econtext->ecxt_outertuple;
scanslot = econtext->ecxt_scantuple;
+ oldslot = econtext->ecxt_oldtuple;
+ newslot = econtext->ecxt_newtuple;
for (int i = 0; i < state->steps_len; i++)
{
CheckVarSlotCompatibility(scanslot, attnum + 1, op->d.var.vartype);
break;
}
+
+ case EEOP_OLD_VAR:
+ {
+ int attnum = op->d.var.attnum;
+
+ CheckVarSlotCompatibility(oldslot, attnum + 1, op->d.var.vartype);
+ break;
+ }
+
+ case EEOP_NEW_VAR:
+ {
+ int attnum = op->d.var.attnum;
+
+ CheckVarSlotCompatibility(newslot, attnum + 1, op->d.var.vartype);
+ break;
+ }
default:
break;
}
ExecEvalWholeRowVar(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
{
Var *variable = op->d.wholerow.var;
- TupleTableSlot *slot;
+ TupleTableSlot *slot = NULL;
TupleDesc output_tupdesc;
MemoryContext oldcontext;
HeapTupleHeader dtuple;
/* INDEX_VAR is handled by default case */
default:
- /* get the tuple from the relation being scanned */
- slot = econtext->ecxt_scantuple;
+
+ /*
+ * Get the tuple from the relation being scanned.
+ *
+ * By default, this uses the "scan" tuple slot, but a wholerow Var
+ * in the RETURNING list may explicitly refer to OLD/NEW. If the
+ * OLD/NEW row doesn't exist, we just return NULL.
+ */
+ switch (variable->varreturningtype)
+ {
+ case VAR_RETURNING_DEFAULT:
+ slot = econtext->ecxt_scantuple;
+ break;
+
+ case VAR_RETURNING_OLD:
+ if (state->flags & EEO_FLAG_OLD_IS_NULL)
+ {
+ *op->resvalue = (Datum) 0;
+ *op->resnull = true;
+ return;
+ }
+ slot = econtext->ecxt_oldtuple;
+ break;
+
+ case VAR_RETURNING_NEW:
+ if (state->flags & EEO_FLAG_NEW_IS_NULL)
+ {
+ *op->resvalue = (Datum) 0;
+ *op->resnull = true;
+ return;
+ }
+ slot = econtext->ecxt_newtuple;
+ break;
+ }
break;
}
{
Datum d;
+ /* OLD/NEW system attribute is NULL if OLD/NEW row is NULL */
+ if ((op->d.var.varreturningtype == VAR_RETURNING_OLD &&
+ state->flags & EEO_FLAG_OLD_IS_NULL) ||
+ (op->d.var.varreturningtype == VAR_RETURNING_NEW &&
+ state->flags & EEO_FLAG_NEW_IS_NULL))
+ {
+ *op->resvalue = (Datum) 0;
+ *op->resnull = true;
+ return;
+ }
+
/* slot_getsysattr has sufficient defenses against bad attnums */
d = slot_getsysattr(slot,
op->d.var.attnum,
resultRelInfo->ri_ReturningSlot = NULL;
resultRelInfo->ri_TrigOldSlot = NULL;
resultRelInfo->ri_TrigNewSlot = NULL;
+ resultRelInfo->ri_AllNullSlot = NULL;
resultRelInfo->ri_MergeActions[MERGE_WHEN_MATCHED] = NIL;
resultRelInfo->ri_MergeActions[MERGE_WHEN_NOT_MATCHED_BY_SOURCE] = NIL;
resultRelInfo->ri_MergeActions[MERGE_WHEN_NOT_MATCHED_BY_TARGET] = NIL;
return relInfo->ri_ReturningSlot;
}
+/*
+ * Return a relInfo's all-NULL tuple slot for processing returning tuples.
+ *
+ * Note: this slot is intentionally filled with NULLs in every column, and
+ * should be considered read-only --- the caller must not update it.
+ */
+TupleTableSlot *
+ExecGetAllNullSlot(EState *estate, ResultRelInfo *relInfo)
+{
+ if (relInfo->ri_AllNullSlot == NULL)
+ {
+ Relation rel = relInfo->ri_RelationDesc;
+ MemoryContext oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
+ TupleTableSlot *slot;
+
+ slot = ExecInitExtraTupleSlot(estate,
+ RelationGetDescr(rel),
+ table_slot_callbacks(rel));
+ ExecStoreAllNullTuple(slot);
+
+ relInfo->ri_AllNullSlot = slot;
+
+ MemoryContextSwitchTo(oldcontext);
+ }
+
+ return relInfo->ri_AllNullSlot;
+}
+
/*
* Return the map needed to convert given child result relation's tuples to
* the rowtype of the query's main target ("root") relation. Note that a
*/
TM_FailureData tmfd;
+ /*
+ * The tuple deleted when doing a cross-partition UPDATE with a RETURNING
+ * clause that refers to OLD columns (converted to the root's tuple
+ * descriptor).
+ */
+ TupleTableSlot *cpDeletedSlot;
+
/*
* The tuple projected by the INSERT's RETURNING clause, when doing a
* cross-partition UPDATE
/*
* ExecProcessReturning --- evaluate a RETURNING list
*
+ * context: context for the ModifyTable operation
* resultRelInfo: current result rel
- * tupleSlot: slot holding tuple actually inserted/updated/deleted
+ * cmdType: operation/merge action performed (INSERT, UPDATE, or DELETE)
+ * oldSlot: slot holding old tuple deleted or updated
+ * newSlot: slot holding new tuple inserted or updated
* planSlot: slot holding tuple returned by top subplan node
*
- * Note: If tupleSlot is NULL, the FDW should have already provided econtext's
- * scan tuple.
+ * Note: If oldSlot and newSlot are NULL, the FDW should have already provided
+ * econtext's scan tuple and its old & new tuples are not needed (FDW direct-
+ * modify is disabled if the RETURNING list refers to any OLD/NEW values).
*
* Returns a slot holding the result tuple
*/
static TupleTableSlot *
-ExecProcessReturning(ResultRelInfo *resultRelInfo,
- TupleTableSlot *tupleSlot,
+ExecProcessReturning(ModifyTableContext *context,
+ ResultRelInfo *resultRelInfo,
+ CmdType cmdType,
+ TupleTableSlot *oldSlot,
+ TupleTableSlot *newSlot,
TupleTableSlot *planSlot)
{
+ EState *estate = context->estate;
ProjectionInfo *projectReturning = resultRelInfo->ri_projectReturning;
ExprContext *econtext = projectReturning->pi_exprContext;
/* Make tuple and any needed join variables available to ExecProject */
- if (tupleSlot)
- econtext->ecxt_scantuple = tupleSlot;
+ switch (cmdType)
+ {
+ case CMD_INSERT:
+ case CMD_UPDATE:
+ /* return new tuple by default */
+ if (newSlot)
+ econtext->ecxt_scantuple = newSlot;
+ break;
+
+ case CMD_DELETE:
+ /* return old tuple by default */
+ if (oldSlot)
+ econtext->ecxt_scantuple = oldSlot;
+ break;
+
+ default:
+ elog(ERROR, "unrecognized commandType: %d", (int) cmdType);
+ }
econtext->ecxt_outertuple = planSlot;
+ /* Make old/new tuples available to ExecProject, if required */
+ if (oldSlot)
+ econtext->ecxt_oldtuple = oldSlot;
+ else if (projectReturning->pi_state.flags & EEO_FLAG_HAS_OLD)
+ econtext->ecxt_oldtuple = ExecGetAllNullSlot(estate, resultRelInfo);
+ else
+ econtext->ecxt_oldtuple = NULL; /* No references to OLD columns */
+
+ if (newSlot)
+ econtext->ecxt_newtuple = newSlot;
+ else if (projectReturning->pi_state.flags & EEO_FLAG_HAS_NEW)
+ econtext->ecxt_newtuple = ExecGetAllNullSlot(estate, resultRelInfo);
+ else
+ econtext->ecxt_newtuple = NULL; /* No references to NEW columns */
+
/*
- * RETURNING expressions might reference the tableoid column, so
- * reinitialize tts_tableOid before evaluating them.
+ * Tell ExecProject whether or not the OLD/NEW rows actually exist. This
+ * information is required to evaluate ReturningExpr nodes and also in
+ * ExecEvalSysVar() and ExecEvalWholeRowVar().
*/
- econtext->ecxt_scantuple->tts_tableOid =
- RelationGetRelid(resultRelInfo->ri_RelationDesc);
+ if (oldSlot == NULL)
+ projectReturning->pi_state.flags |= EEO_FLAG_OLD_IS_NULL;
+ else
+ projectReturning->pi_state.flags &= ~EEO_FLAG_OLD_IS_NULL;
+
+ if (newSlot == NULL)
+ projectReturning->pi_state.flags |= EEO_FLAG_NEW_IS_NULL;
+ else
+ projectReturning->pi_state.flags &= ~EEO_FLAG_NEW_IS_NULL;
/* Compute the RETURNING expressions */
return ExecProject(projectReturning);
/* Process RETURNING if present */
if (resultRelInfo->ri_projectReturning)
- result = ExecProcessReturning(resultRelInfo, slot, planSlot);
+ {
+ TupleTableSlot *oldSlot = NULL;
+
+ /*
+ * If this is part of a cross-partition UPDATE, and the RETURNING list
+ * refers to any OLD columns, ExecDelete() will have saved the tuple
+ * deleted from the original partition, which we must use here to
+ * compute the OLD column values. Otherwise, all OLD column values
+ * will be NULL.
+ */
+ if (context->cpDeletedSlot)
+ {
+ TupleConversionMap *tupconv_map;
+
+ /*
+ * Convert the OLD tuple to the new partition's format/slot, if
+ * needed. Note that ExceDelete() already converted it to the
+ * root's partition's format/slot.
+ */
+ oldSlot = context->cpDeletedSlot;
+ tupconv_map = ExecGetRootToChildMap(resultRelInfo, estate);
+ if (tupconv_map != NULL)
+ {
+ oldSlot = execute_attr_map_slot(tupconv_map->attrMap,
+ oldSlot,
+ ExecGetReturningSlot(estate,
+ resultRelInfo));
+
+ oldSlot->tts_tableOid = context->cpDeletedSlot->tts_tableOid;
+ ItemPointerCopy(&context->cpDeletedSlot->tts_tid, &oldSlot->tts_tid);
+ }
+ }
+
+ result = ExecProcessReturning(context, resultRelInfo, CMD_INSERT,
+ oldSlot, slot, planSlot);
+
+ /*
+ * For a cross-partition UPDATE, release the old tuple, first making
+ * sure that the result slot has a local copy of any pass-by-reference
+ * values.
+ */
+ if (context->cpDeletedSlot)
+ {
+ ExecMaterializeSlot(result);
+ ExecClearTuple(oldSlot);
+ if (context->cpDeletedSlot != oldSlot)
+ ExecClearTuple(context->cpDeletedSlot);
+ context->cpDeletedSlot = NULL;
+ }
+ }
if (inserted_tuple)
*inserted_tuple = slot;
Relation resultRelationDesc = resultRelInfo->ri_RelationDesc;
TupleTableSlot *slot = NULL;
TM_Result result;
+ bool saveOld;
if (tupleDeleted)
*tupleDeleted = false;
ExecDeleteEpilogue(context, resultRelInfo, tupleid, oldtuple, changingPart);
- /* Process RETURNING if present and if requested */
- if (processReturning && resultRelInfo->ri_projectReturning)
+ /*
+ * Process RETURNING if present and if requested.
+ *
+ * If this is part of a cross-partition UPDATE, and the RETURNING list
+ * refers to any OLD column values, save the old tuple here for later
+ * processing of the RETURNING list by ExecInsert().
+ */
+ saveOld = changingPart && resultRelInfo->ri_projectReturning &&
+ resultRelInfo->ri_projectReturning->pi_state.flags & EEO_FLAG_HAS_OLD;
+
+ if (resultRelInfo->ri_projectReturning && (processReturning || saveOld))
{
/*
* We have to put the target tuple into a slot, which means first we
}
}
- rslot = ExecProcessReturning(resultRelInfo, slot, context->planSlot);
+ /*
+ * If required, save the old tuple for later processing of the
+ * RETURNING list by ExecInsert().
+ */
+ if (saveOld)
+ {
+ TupleConversionMap *tupconv_map;
+
+ /*
+ * Convert the tuple into the root partition's format/slot, if
+ * needed. ExecInsert() will then convert it to the new
+ * partition's format/slot, if necessary.
+ */
+ tupconv_map = ExecGetChildToRootMap(resultRelInfo);
+ if (tupconv_map != NULL)
+ {
+ ResultRelInfo *rootRelInfo = context->mtstate->rootResultRelInfo;
+ TupleTableSlot *oldSlot = slot;
+
+ slot = execute_attr_map_slot(tupconv_map->attrMap,
+ slot,
+ ExecGetReturningSlot(estate,
+ rootRelInfo));
+
+ slot->tts_tableOid = oldSlot->tts_tableOid;
+ ItemPointerCopy(&oldSlot->tts_tid, &slot->tts_tid);
+ }
+
+ context->cpDeletedSlot = slot;
+
+ return NULL;
+ }
+
+ rslot = ExecProcessReturning(context, resultRelInfo, CMD_DELETE,
+ slot, NULL, context->planSlot);
/*
* Before releasing the target tuple again, make sure rslot has a
bool tuple_deleted;
TupleTableSlot *epqslot = NULL;
+ context->cpDeletedSlot = NULL;
context->cpUpdateReturningSlot = NULL;
*retry_slot = NULL;
* the planSlot. oldtuple is passed to foreign table triggers; it is
* NULL when the foreign table has no relevant triggers.
*
+ * oldSlot contains the old tuple value.
* slot contains the new tuple value to be stored.
* planSlot is the output of the ModifyTable's subplan; we use it
* to access values from other input tables (for RETURNING),
*/
static TupleTableSlot *
ExecUpdate(ModifyTableContext *context, ResultRelInfo *resultRelInfo,
- ItemPointer tupleid, HeapTuple oldtuple, TupleTableSlot *slot,
- bool canSetTag)
+ ItemPointer tupleid, HeapTuple oldtuple, TupleTableSlot *oldSlot,
+ TupleTableSlot *slot, bool canSetTag)
{
EState *estate = context->estate;
Relation resultRelationDesc = resultRelInfo->ri_RelationDesc;
{
TupleTableSlot *inputslot;
TupleTableSlot *epqslot;
- TupleTableSlot *oldSlot;
if (IsolationUsesXactSnapshot())
ereport(ERROR,
/* Process RETURNING if present */
if (resultRelInfo->ri_projectReturning)
- return ExecProcessReturning(resultRelInfo, slot, context->planSlot);
+ return ExecProcessReturning(context, resultRelInfo, CMD_UPDATE,
+ oldSlot, slot, context->planSlot);
return NULL;
}
/* Execute UPDATE with projection */
*returning = ExecUpdate(context, resultRelInfo,
- conflictTid, NULL,
+ conflictTid, NULL, existing,
resultRelInfo->ri_onConflict->oc_ProjSlot,
canSetTag);
/*
* Clear out existing tuple, as there might not be another conflict among
* the next input rows. Don't want to hold resources till the end of the
- * query.
+ * query. First though, make sure that the returning slot, if any, has a
+ * local copy of any OLD pass-by-reference values, if it refers to any OLD
+ * columns.
*/
+ if (*returning != NULL &&
+ resultRelInfo->ri_projectReturning->pi_state.flags & EEO_FLAG_HAS_OLD)
+ ExecMaterializeSlot(*returning);
+
ExecClearTuple(existing);
+
return true;
}
switch (commandType)
{
case CMD_UPDATE:
- rslot = ExecProcessReturning(resultRelInfo, newslot,
+ rslot = ExecProcessReturning(context,
+ resultRelInfo,
+ CMD_UPDATE,
+ resultRelInfo->ri_oldTupleSlot,
+ newslot,
context->planSlot);
break;
case CMD_DELETE:
- rslot = ExecProcessReturning(resultRelInfo,
+ rslot = ExecProcessReturning(context,
+ resultRelInfo,
+ CMD_DELETE,
resultRelInfo->ri_oldTupleSlot,
+ NULL,
context->planSlot);
break;
if (node->mt_merge_pending_not_matched != NULL)
{
context.planSlot = node->mt_merge_pending_not_matched;
+ context.cpDeletedSlot = NULL;
slot = ExecMergeNotMatched(&context, node->resultRelInfo,
node->canSetTag);
/* Fetch the next row from subplan */
context.planSlot = ExecProcNode(subplanstate);
+ context.cpDeletedSlot = NULL;
/* No more tuples to process? */
if (TupIsNull(context.planSlot))
* A scan slot containing the data that was actually inserted,
* updated or deleted has already been made available to
* ExecProcessReturning by IterateDirectModify, so no need to
- * provide it here.
+ * provide it here. The individual old and new slots are not
+ * needed, since direct-modify is disabled if the RETURNING list
+ * refers to OLD/NEW values.
*/
- slot = ExecProcessReturning(resultRelInfo, NULL, context.planSlot);
+ Assert((resultRelInfo->ri_projectReturning->pi_state.flags & EEO_FLAG_HAS_OLD) == 0 &&
+ (resultRelInfo->ri_projectReturning->pi_state.flags & EEO_FLAG_HAS_NEW) == 0);
+
+ slot = ExecProcessReturning(&context, resultRelInfo, operation,
+ NULL, NULL, context.planSlot);
return slot;
}
/* Now apply the update. */
slot = ExecUpdate(&context, resultRelInfo, tupleid, oldtuple,
- slot, node->canSetTag);
+ oldSlot, slot, node->canSetTag);
if (tuplock)
UnlockTuple(resultRelInfo->ri_RelationDesc, tupleid,
InplaceUpdateTupleLock);
LLVMValueRef v_innerslot;
LLVMValueRef v_outerslot;
LLVMValueRef v_scanslot;
+ LLVMValueRef v_oldslot;
+ LLVMValueRef v_newslot;
LLVMValueRef v_resultslot;
/* nulls/values of slots */
LLVMValueRef v_outernulls;
LLVMValueRef v_scanvalues;
LLVMValueRef v_scannulls;
+ LLVMValueRef v_oldvalues;
+ LLVMValueRef v_oldnulls;
+ LLVMValueRef v_newvalues;
+ LLVMValueRef v_newnulls;
LLVMValueRef v_resultvalues;
LLVMValueRef v_resultnulls;
v_econtext,
FIELDNO_EXPRCONTEXT_OUTERTUPLE,
"v_outerslot");
+ v_oldslot = l_load_struct_gep(b,
+ StructExprContext,
+ v_econtext,
+ FIELDNO_EXPRCONTEXT_OLDTUPLE,
+ "v_oldslot");
+ v_newslot = l_load_struct_gep(b,
+ StructExprContext,
+ v_econtext,
+ FIELDNO_EXPRCONTEXT_NEWTUPLE,
+ "v_newslot");
v_resultslot = l_load_struct_gep(b,
StructExprState,
v_state,
v_outerslot,
FIELDNO_TUPLETABLESLOT_ISNULL,
"v_outernulls");
+ v_oldvalues = l_load_struct_gep(b,
+ StructTupleTableSlot,
+ v_oldslot,
+ FIELDNO_TUPLETABLESLOT_VALUES,
+ "v_oldvalues");
+ v_oldnulls = l_load_struct_gep(b,
+ StructTupleTableSlot,
+ v_oldslot,
+ FIELDNO_TUPLETABLESLOT_ISNULL,
+ "v_oldnulls");
+ v_newvalues = l_load_struct_gep(b,
+ StructTupleTableSlot,
+ v_newslot,
+ FIELDNO_TUPLETABLESLOT_VALUES,
+ "v_newvalues");
+ v_newnulls = l_load_struct_gep(b,
+ StructTupleTableSlot,
+ v_newslot,
+ FIELDNO_TUPLETABLESLOT_ISNULL,
+ "v_newnulls");
v_resultvalues = l_load_struct_gep(b,
StructTupleTableSlot,
v_resultslot,
case EEOP_INNER_FETCHSOME:
case EEOP_OUTER_FETCHSOME:
case EEOP_SCAN_FETCHSOME:
+ case EEOP_OLD_FETCHSOME:
+ case EEOP_NEW_FETCHSOME:
{
TupleDesc desc = NULL;
LLVMValueRef v_slot;
v_slot = v_innerslot;
else if (opcode == EEOP_OUTER_FETCHSOME)
v_slot = v_outerslot;
- else
+ else if (opcode == EEOP_SCAN_FETCHSOME)
v_slot = v_scanslot;
+ else if (opcode == EEOP_OLD_FETCHSOME)
+ v_slot = v_oldslot;
+ else
+ v_slot = v_newslot;
/*
* Check if all required attributes are available, or
case EEOP_INNER_VAR:
case EEOP_OUTER_VAR:
case EEOP_SCAN_VAR:
+ case EEOP_OLD_VAR:
+ case EEOP_NEW_VAR:
{
LLVMValueRef value,
isnull;
v_values = v_outervalues;
v_nulls = v_outernulls;
}
- else
+ else if (opcode == EEOP_SCAN_VAR)
{
v_values = v_scanvalues;
v_nulls = v_scannulls;
}
+ else if (opcode == EEOP_OLD_VAR)
+ {
+ v_values = v_oldvalues;
+ v_nulls = v_oldnulls;
+ }
+ else
+ {
+ v_values = v_newvalues;
+ v_nulls = v_newnulls;
+ }
v_attnum = l_int32_const(lc, op->d.var.attnum);
value = l_load_gep1(b, TypeSizeT, v_values, v_attnum, "");
case EEOP_INNER_SYSVAR:
case EEOP_OUTER_SYSVAR:
case EEOP_SCAN_SYSVAR:
+ case EEOP_OLD_SYSVAR:
+ case EEOP_NEW_SYSVAR:
{
LLVMValueRef v_slot;
v_slot = v_innerslot;
else if (opcode == EEOP_OUTER_SYSVAR)
v_slot = v_outerslot;
- else
+ else if (opcode == EEOP_SCAN_SYSVAR)
v_slot = v_scanslot;
+ else if (opcode == EEOP_OLD_SYSVAR)
+ v_slot = v_oldslot;
+ else
+ v_slot = v_newslot;
build_EvalXFunc(b, mod, "ExecEvalSysVar",
v_state, op, v_econtext, v_slot);
case EEOP_ASSIGN_INNER_VAR:
case EEOP_ASSIGN_OUTER_VAR:
case EEOP_ASSIGN_SCAN_VAR:
+ case EEOP_ASSIGN_OLD_VAR:
+ case EEOP_ASSIGN_NEW_VAR:
{
LLVMValueRef v_value;
LLVMValueRef v_isnull;
v_values = v_outervalues;
v_nulls = v_outernulls;
}
- else
+ else if (opcode == EEOP_ASSIGN_SCAN_VAR)
{
v_values = v_scanvalues;
v_nulls = v_scannulls;
}
+ else if (opcode == EEOP_ASSIGN_OLD_VAR)
+ {
+ v_values = v_oldvalues;
+ v_nulls = v_oldnulls;
+ }
+ else
+ {
+ v_values = v_newvalues;
+ v_nulls = v_newnulls;
+ }
/* load data */
v_attnum = l_int32_const(lc, op->d.assign_var.attnum);
LLVMBuildBr(b, opblocks[opno + 1]);
break;
+ case EEOP_RETURNINGEXPR:
+ {
+ LLVMBasicBlockRef b_isnull;
+ LLVMValueRef v_flagsp;
+ LLVMValueRef v_flags;
+ LLVMValueRef v_nullflag;
+
+ b_isnull = l_bb_before_v(opblocks[opno + 1],
+ "op.%d.row.isnull", opno);
+
+ /*
+ * The next op actually evaluates the expression. If the
+ * OLD/NEW row doesn't exist, skip that and return NULL.
+ */
+ v_flagsp = l_struct_gep(b,
+ StructExprState,
+ v_state,
+ FIELDNO_EXPRSTATE_FLAGS,
+ "v.state.flags");
+ v_flags = l_load(b, TypeStorageBool, v_flagsp, "");
+
+ v_nullflag = l_int8_const(lc, op->d.returningexpr.nullflag);
+
+ LLVMBuildCondBr(b,
+ LLVMBuildICmp(b, LLVMIntEQ,
+ LLVMBuildAnd(b, v_flags,
+ v_nullflag, ""),
+ l_sbool_const(0), ""),
+ opblocks[opno + 1], b_isnull);
+
+ LLVMPositionBuilderAtEnd(b, b_isnull);
+
+ LLVMBuildStore(b, l_sizet_const(0), v_resvaluep);
+ LLVMBuildStore(b, l_sbool_const(1), v_resnullp);
+
+ LLVMBuildBr(b, opblocks[op->d.returningexpr.jumpdone]);
+ break;
+ }
+
case EEOP_ARRAYEXPR:
build_EvalXFunc(b, mod, "ExecEvalArrayExpr",
v_state, op);
var->varlevelsup = varlevelsup;
/*
- * Only a few callers need to make Var nodes with non-null varnullingrels,
- * or with varnosyn/varattnosyn different from varno/varattno. We don't
- * provide separate arguments for them, but just initialize them to NULL
- * and the given varno/varattno. This reduces code clutter and chance of
- * error for most callers.
+ * Only a few callers need to make Var nodes with varreturningtype
+ * different from VAR_RETURNING_DEFAULT, non-null varnullingrels, or with
+ * varnosyn/varattnosyn different from varno/varattno. We don't provide
+ * separate arguments for them, but just initialize them to sensible
+ * default values. This reduces code clutter and chance of error for most
+ * callers.
*/
+ var->varreturningtype = VAR_RETURNING_DEFAULT;
var->varnullingrels = NULL;
var->varnosyn = (Index) varno;
var->varattnosyn = varattno;
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;
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:
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;
* 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)
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);
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;
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;
case T_A_Const:
case T_A_Star:
case T_MergeSupportFunc:
+ case T_ReturningOption:
/* primitive node types with no subnodes */
break;
case T_Alias:
return true;
if (WALK(stmt->onConflictClause))
return true;
- if (WALK(stmt->returningList))
+ if (WALK(stmt->returningClause))
return true;
if (WALK(stmt->withClause))
return true;
return true;
if (WALK(stmt->whereClause))
return true;
- if (WALK(stmt->returningList))
+ if (WALK(stmt->returningClause))
return true;
if (WALK(stmt->withClause))
return true;
return true;
if (WALK(stmt->fromClause))
return true;
- if (WALK(stmt->returningList))
+ if (WALK(stmt->returningClause))
return true;
if (WALK(stmt->withClause))
return true;
return true;
if (WALK(stmt->mergeWhenClauses))
return true;
- if (WALK(stmt->returningList))
+ if (WALK(stmt->returningClause))
return true;
if (WALK(stmt->withClause))
return true;
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;
*/
qual = ReplaceVarsFromTargetList(qual, rti, 0, rte,
subquery->targetList,
+ subquery->resultRelation,
REPLACEVARS_REPORT_ERROR, 0,
&subquery->hasSubLinks);
int epqParam)
{
ModifyTable *node = makeNode(ModifyTable);
+ bool returning_old_or_new = false;
+ bool returning_old_or_new_valid = false;
List *fdw_private_list;
Bitmapset *direct_modify_plans;
ListCell *lc;
}
node->updateColnosLists = updateColnosLists;
node->withCheckOptionLists = withCheckOptionLists;
+ node->returningOldAlias = root->parse->returningOldAlias;
+ node->returningNewAlias = root->parse->returningNewAlias;
node->returningLists = returningLists;
node->rowMarks = rowMarks;
node->mergeActionLists = mergeActionLists;
* callback functions needed for that and (2) there are no local
* structures that need to be run for each modified row: row-level
* triggers on the foreign table, stored generated columns, WITH CHECK
- * OPTIONs from parent views.
+ * OPTIONs from parent views, or Vars returning OLD/NEW in the
+ * RETURNING list.
*/
direct_modify = false;
if (fdwroutine != NULL &&
withCheckOptionLists == NIL &&
!has_row_triggers(root, rti, operation) &&
!has_stored_generated_columns(root, rti))
- direct_modify = fdwroutine->PlanDirectModify(root, node, rti, i);
+ {
+ /* returning_old_or_new is the same for all result relations */
+ if (!returning_old_or_new_valid)
+ {
+ returning_old_or_new =
+ contain_vars_returning_old_or_new((Node *)
+ root->parse->returningList);
+ returning_old_or_new_valid = true;
+ }
+ if (!returning_old_or_new)
+ direct_modify = fdwroutine->PlanDirectModify(root, node, rti, i);
+ }
if (direct_modify)
direct_modify_plans = bms_add_member(direct_modify_plans, i);
{
Var *var = (Var *) node;
+ /*
+ * Verify that Vars with non-default varreturningtype only appear in
+ * the RETURNING list, and refer to the target relation.
+ */
+ if (var->varreturningtype != VAR_RETURNING_DEFAULT)
+ {
+ if (context->inner_itlist != NULL ||
+ context->outer_itlist == NULL ||
+ context->acceptable_rel == 0)
+ elog(ERROR, "variable returning old/new found outside RETURNING list");
+ if (var->varno != context->acceptable_rel)
+ elog(ERROR, "wrong varno %d (expected %d) for variable returning old/new",
+ var->varno, context->acceptable_rel);
+ }
+
/* Look for the var in the input tlists, first in the outer */
if (context->outer_itlist)
{
Node *arg = pitem->item;
/*
- * The Var, PlaceHolderVar, Aggref or GroupingFunc has already been
- * adjusted to have the correct varlevelsup, phlevelsup, or
- * agglevelsup.
+ * The Var, PlaceHolderVar, Aggref, GroupingFunc, or ReturningExpr has
+ * already been adjusted to have the correct varlevelsup, phlevelsup,
+ * agglevelsup, or retlevelsup.
*
- * If it's a PlaceHolderVar, Aggref or GroupingFunc, its arguments
- * might contain SubLinks, which have not yet been processed (see the
- * comments for SS_replace_correlation_vars). Do that now.
+ * If it's a PlaceHolderVar, Aggref, GroupingFunc, or ReturningExpr,
+ * its arguments might contain SubLinks, which have not yet been
+ * processed (see the comments for SS_replace_correlation_vars). Do
+ * that now.
*/
if (IsA(arg, PlaceHolderVar) ||
IsA(arg, Aggref) ||
- IsA(arg, GroupingFunc))
+ IsA(arg, GroupingFunc) ||
+ IsA(arg, ReturningExpr))
arg = SS_process_sublinks(root, arg, false);
splan->parParam = lappend_int(splan->parParam, pitem->paramId);
/*
* Replace correlation vars (uplevel vars) with Params.
*
- * Uplevel PlaceHolderVars, aggregates, GROUPING() expressions, and
- * MergeSupportFuncs are replaced, too.
+ * Uplevel PlaceHolderVars, aggregates, GROUPING() expressions,
+ * MergeSupportFuncs, and ReturningExprs are replaced, too.
*
* Note: it is critical that this runs immediately after SS_process_sublinks.
* Since we do not recurse into the arguments of uplevel PHVs and aggregates,
return (Node *) replace_outer_merge_support(root,
(MergeSupportFunc *) node);
}
+ if (IsA(node, ReturningExpr))
+ {
+ if (((ReturningExpr *) node)->retlevelsup > 0)
+ return (Node *) replace_outer_returning(root,
+ (ReturningExpr *) node);
+ }
return expression_tree_mutator(node, replace_correlation_vars_mutator, root);
}
}
/*
- * Don't recurse into the arguments of an outer PHV, Aggref or
- * GroupingFunc here. Any SubLinks in the arguments have to be dealt with
- * at the outer query level; they'll be handled when build_subplan
- * collects the PHV, Aggref or GroupingFunc into the arguments to be
- * passed down to the current subplan.
+ * Don't recurse into the arguments of an outer PHV, Aggref, GroupingFunc,
+ * or ReturningExpr here. Any SubLinks in the arguments have to be dealt
+ * with at the outer query level; they'll be handled when build_subplan
+ * collects the PHV, Aggref, GroupingFunc, or ReturningExpr into the
+ * arguments to be passed down to the current subplan.
*/
if (IsA(node, PlaceHolderVar))
{
if (((GroupingFunc *) node)->agglevelsup > 0)
return node;
}
+ else if (IsA(node, ReturningExpr))
+ {
+ if (((ReturningExpr *) node)->retlevelsup > 0)
+ return node;
+ }
/*
* We should never see a SubPlan expression in the input (since this is
outer_params = NULL;
for (proot = root->parent_root; proot != NULL; proot = proot->parent_root)
{
- /* Include ordinary Var/PHV/Aggref/GroupingFunc params */
+ /*
+ * Include ordinary Var/PHV/Aggref/GroupingFunc/ReturningExpr params.
+ */
foreach(l, proot->plan_params)
{
PlannerParamItem *pitem = (PlannerParamItem *) lfirst(l);
* expansion with varlevelsup = 0, and then adjust below if needed.
*/
expandRTE(rcon->target_rte,
- var->varno, 0 /* not varlevelsup */ , var->location,
+ var->varno, 0 /* not varlevelsup */ ,
+ var->varreturningtype, var->location,
(var->vartype != RECORDOID),
&colnames, &fields);
/* Expand the generated per-field Vars, but don't insert PHVs there */
* all non-Var outputs of such subqueries, and then we could look up
* the pre-existing PHV here. Or perhaps just wrap the translations
* that way to begin with?
+ *
+ * If var->varreturningtype is not VAR_RETURNING_DEFAULT, then that
+ * also needs to be copied to the translated Var. That too would fail
+ * if the translation wasn't a Var, but that should never happen since
+ * a non-default var->varreturningtype is only used for Vars referring
+ * to the result relation, which should never be a flattened UNION ALL
+ * subquery.
*/
for (cnt = 0; cnt < nappinfos; cnt++)
elog(ERROR, "attribute %d of relation \"%s\" does not exist",
var->varattno, get_rel_name(appinfo->parent_reloid));
if (IsA(newnode, Var))
+ {
+ ((Var *) newnode)->varreturningtype = var->varreturningtype;
((Var *) newnode)->varnullingrels = var->varnullingrels;
- else if (var->varnullingrels != NULL)
- elog(ERROR, "failed to apply nullingrels to a non-Var");
+ }
+ else
+ {
+ if (var->varreturningtype != VAR_RETURNING_DEFAULT)
+ elog(ERROR, "failed to apply returningtype to a non-Var");
+ if (var->varnullingrels != NULL)
+ elog(ERROR, "failed to apply nullingrels to a non-Var");
+ }
return newnode;
}
else if (var->varattno == 0)
rowexpr->colnames = copyObject(rte->eref->colnames);
rowexpr->location = -1;
+ if (var->varreturningtype != VAR_RETURNING_DEFAULT)
+ elog(ERROR, "failed to apply returningtype to a non-Var");
if (var->varnullingrels != NULL)
elog(ERROR, "failed to apply nullingrels to a non-Var");
case T_NullTest:
case T_BooleanTest:
case T_NextValueExpr:
+ case T_ReturningExpr:
case T_List:
/*
fselect->resulttypmod,
fselect->resultcollid,
((Var *) arg)->varlevelsup);
+ /* New Var has same OLD/NEW returning as old one */
+ newvar->varreturningtype = ((Var *) arg)->varreturningtype;
/* New Var is nullable by same rels as the old one */
newvar->varnullingrels = ((Var *) arg)->varnullingrels;
return (Node *) newvar;
pvar->vartype == var->vartype &&
pvar->vartypmod == var->vartypmod &&
pvar->varcollid == var->varcollid &&
+ pvar->varreturningtype == var->varreturningtype &&
bms_equal(pvar->varnullingrels, var->varnullingrels))
return pitem->paramId;
}
return retval;
}
+/*
+ * Generate a Param node to replace the given ReturningExpr expression which
+ * is expected to have retlevelsup > 0 (ie, it is not local). Record the need
+ * for the ReturningExpr in the proper upper-level root->plan_params.
+ */
+Param *
+replace_outer_returning(PlannerInfo *root, ReturningExpr *rexpr)
+{
+ Param *retval;
+ PlannerParamItem *pitem;
+ Index levelsup;
+ Oid ptype = exprType((Node *) rexpr->retexpr);
+
+ Assert(rexpr->retlevelsup > 0 && rexpr->retlevelsup < root->query_level);
+
+ /* Find the query level the ReturningExpr belongs to */
+ for (levelsup = rexpr->retlevelsup; levelsup > 0; levelsup--)
+ root = root->parent_root;
+
+ /*
+ * It does not seem worthwhile to try to de-duplicate references to outer
+ * ReturningExprs. Just make a new slot every time.
+ */
+ rexpr = copyObject(rexpr);
+ IncrementVarSublevelsUp((Node *) rexpr, -((int) rexpr->retlevelsup), 0);
+ Assert(rexpr->retlevelsup == 0);
+
+ pitem = makeNode(PlannerParamItem);
+ pitem->item = (Node *) rexpr;
+ pitem->paramId = list_length(root->glob->paramExecTypes);
+ root->glob->paramExecTypes = lappend_oid(root->glob->paramExecTypes,
+ ptype);
+
+ root->plan_params = lappend(root->plan_params, pitem);
+
+ retval = makeNode(Param);
+ retval->paramkind = PARAM_EXEC;
+ retval->paramid = pitem->paramId;
+ retval->paramtype = ptype;
+ retval->paramtypmod = exprTypmod((Node *) rexpr->retexpr);
+ retval->paramcollid = exprCollation((Node *) rexpr->retexpr);
+ retval->location = exprLocation((Node *) rexpr->retexpr);
+
+ return retval;
+}
+
/*
* Generate a Param node to replace the given Var,
* which is expected to come from some upper NestLoop plan node.
case RTE_NAMEDTUPLESTORE:
case RTE_RESULT:
/* Not all of these can have dropped cols, but share code anyway */
- expandRTE(rte, varno, 0, -1, true /* include dropped */ ,
- NULL, &colvars);
+ expandRTE(rte, varno, 0, VAR_RETURNING_DEFAULT, -1,
+ true /* include dropped */ , NULL, &colvars);
foreach(l, colvars)
{
var = (Var *) lfirst(l);
static bool pull_vars_walker(Node *node, pull_vars_context *context);
static bool contain_var_clause_walker(Node *node, void *context);
static bool contain_vars_of_level_walker(Node *node, int *sublevels_up);
+static bool contain_vars_returning_old_or_new_walker(Node *node, void *context);
static bool locate_var_of_level_walker(Node *node,
locate_var_of_level_context *context);
static bool pull_var_clause_walker(Node *node,
}
+/*
+ * contain_vars_returning_old_or_new
+ * Recursively scan a clause to discover whether it contains any Var nodes
+ * (of the current query level) whose varreturningtype is VAR_RETURNING_OLD
+ * or VAR_RETURNING_NEW.
+ *
+ * Returns true if any found.
+ *
+ * Any ReturningExprs are also detected --- if an OLD/NEW Var was rewritten,
+ * we still regard this as a clause that returns OLD/NEW values.
+ *
+ * Does not examine subqueries, therefore must only be used after reduction
+ * of sublinks to subplans!
+ */
+bool
+contain_vars_returning_old_or_new(Node *node)
+{
+ return contain_vars_returning_old_or_new_walker(node, NULL);
+}
+
+static bool
+contain_vars_returning_old_or_new_walker(Node *node, void *context)
+{
+ if (node == NULL)
+ return false;
+ if (IsA(node, Var))
+ {
+ if (((Var *) node)->varlevelsup == 0 &&
+ ((Var *) node)->varreturningtype != VAR_RETURNING_DEFAULT)
+ return true; /* abort the tree traversal and return true */
+ return false;
+ }
+ if (IsA(node, ReturningExpr))
+ {
+ if (((ReturningExpr *) node)->retlevelsup == 0)
+ return true; /* abort the tree traversal and return true */
+ return false;
+ }
+ return expression_tree_walker(node, contain_vars_returning_old_or_new_walker,
+ context);
+}
+
+
/*
* locate_var_of_level
* Find the parse location of any Var of the specified query level.
qual = transformWhereClause(pstate, stmt->whereClause,
EXPR_KIND_WHERE, "WHERE");
- qry->returningList = transformReturningList(pstate, stmt->returningList,
- EXPR_KIND_RETURNING);
+ transformReturningClause(pstate, qry, stmt->returningClause,
+ EXPR_KIND_RETURNING);
/* done building the range table and jointree */
qry->rtable = pstate->p_rtable;
* contain only the target relation, removing any entries added in a
* sub-SELECT or VALUES list.
*/
- if (stmt->onConflictClause || stmt->returningList)
+ if (stmt->onConflictClause || stmt->returningClause)
{
pstate->p_namespace = NIL;
addNSItemToQuery(pstate, pstate->p_target_nsitem,
stmt->onConflictClause);
/* Process RETURNING, if any. */
- if (stmt->returningList)
- qry->returningList = transformReturningList(pstate,
- stmt->returningList,
- EXPR_KIND_RETURNING);
+ if (stmt->returningClause)
+ transformReturningClause(pstate, qry, stmt->returningClause,
+ EXPR_KIND_RETURNING);
/* done building the range table and jointree */
qry->rtable = pstate->p_rtable;
qual = transformWhereClause(pstate, stmt->whereClause,
EXPR_KIND_WHERE, "WHERE");
- qry->returningList = transformReturningList(pstate, stmt->returningList,
- EXPR_KIND_RETURNING);
+ transformReturningClause(pstate, qry, stmt->returningClause,
+ EXPR_KIND_RETURNING);
/*
* Now we are done with SELECT-like processing, and can get on with
}
/*
- * transformReturningList -
+ * addNSItemForReturning -
+ * add a ParseNamespaceItem for the OLD or NEW alias in RETURNING.
+ */
+static void
+addNSItemForReturning(ParseState *pstate, const char *aliasname,
+ VarReturningType returning_type)
+{
+ List *colnames;
+ int numattrs;
+ ParseNamespaceColumn *nscolumns;
+ ParseNamespaceItem *nsitem;
+
+ /* copy per-column data from the target relation */
+ colnames = pstate->p_target_nsitem->p_rte->eref->colnames;
+ numattrs = list_length(colnames);
+
+ nscolumns = (ParseNamespaceColumn *)
+ palloc(numattrs * sizeof(ParseNamespaceColumn));
+
+ memcpy(nscolumns, pstate->p_target_nsitem->p_nscolumns,
+ numattrs * sizeof(ParseNamespaceColumn));
+
+ /* mark all columns as returning OLD/NEW */
+ for (int i = 0; i < numattrs; i++)
+ nscolumns[i].p_varreturningtype = returning_type;
+
+ /* build the nsitem, copying most fields from the target relation */
+ nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem));
+ nsitem->p_names = makeAlias(aliasname, colnames);
+ nsitem->p_rte = pstate->p_target_nsitem->p_rte;
+ nsitem->p_rtindex = pstate->p_target_nsitem->p_rtindex;
+ nsitem->p_perminfo = pstate->p_target_nsitem->p_perminfo;
+ nsitem->p_nscolumns = nscolumns;
+ nsitem->p_returning_type = returning_type;
+
+ /* add it to the query namespace as a table-only item */
+ addNSItemToQuery(pstate, nsitem, false, true, false);
+}
+
+/*
+ * transformReturningClause -
* handle a RETURNING clause in INSERT/UPDATE/DELETE/MERGE
*/
-List *
-transformReturningList(ParseState *pstate, List *returningList,
- ParseExprKind exprKind)
+void
+transformReturningClause(ParseState *pstate, Query *qry,
+ ReturningClause *returningClause,
+ ParseExprKind exprKind)
{
- List *rlist;
+ int save_nslen = list_length(pstate->p_namespace);
int save_next_resno;
- if (returningList == NIL)
- return NIL; /* nothing to do */
+ if (returningClause == NULL)
+ return; /* nothing to do */
+
+ /*
+ * Scan RETURNING WITH(...) options for OLD/NEW alias names. Complain if
+ * there is any conflict with existing relations.
+ */
+ foreach_node(ReturningOption, option, returningClause->options)
+ {
+ switch (option->option)
+ {
+ case RETURNING_OPTION_OLD:
+ if (qry->returningOldAlias != NULL)
+ ereport(ERROR,
+ errcode(ERRCODE_SYNTAX_ERROR),
+ /* translator: %s is OLD or NEW */
+ errmsg("%s cannot be specified multiple times", "OLD"),
+ parser_errposition(pstate, option->location));
+ qry->returningOldAlias = option->value;
+ break;
+
+ case RETURNING_OPTION_NEW:
+ if (qry->returningNewAlias != NULL)
+ ereport(ERROR,
+ errcode(ERRCODE_SYNTAX_ERROR),
+ /* translator: %s is OLD or NEW */
+ errmsg("%s cannot be specified multiple times", "NEW"),
+ parser_errposition(pstate, option->location));
+ qry->returningNewAlias = option->value;
+ break;
+
+ default:
+ elog(ERROR, "unrecognized returning option: %d", option->option);
+ }
+
+ if (refnameNamespaceItem(pstate, NULL, option->value, -1, NULL) != NULL)
+ ereport(ERROR,
+ errcode(ERRCODE_DUPLICATE_ALIAS),
+ errmsg("table name \"%s\" specified more than once",
+ option->value),
+ parser_errposition(pstate, option->location));
+
+ addNSItemForReturning(pstate, option->value,
+ option->option == RETURNING_OPTION_OLD ?
+ VAR_RETURNING_OLD : VAR_RETURNING_NEW);
+ }
+
+ /*
+ * If OLD/NEW alias names weren't explicitly specified, use "old"/"new"
+ * unless masked by existing relations.
+ */
+ if (qry->returningOldAlias == NULL &&
+ refnameNamespaceItem(pstate, NULL, "old", -1, NULL) == NULL)
+ {
+ qry->returningOldAlias = "old";
+ addNSItemForReturning(pstate, "old", VAR_RETURNING_OLD);
+ }
+ if (qry->returningNewAlias == NULL &&
+ refnameNamespaceItem(pstate, NULL, "new", -1, NULL) == NULL)
+ {
+ qry->returningNewAlias = "new";
+ addNSItemForReturning(pstate, "new", VAR_RETURNING_NEW);
+ }
/*
* We need to assign resnos starting at one in the RETURNING list. Save
save_next_resno = pstate->p_next_resno;
pstate->p_next_resno = 1;
- /* transform RETURNING identically to a SELECT targetlist */
- rlist = transformTargetList(pstate, returningList, exprKind);
+ /* transform RETURNING expressions identically to a SELECT targetlist */
+ qry->returningList = transformTargetList(pstate,
+ returningClause->exprs,
+ exprKind);
/*
* Complain if the nonempty tlist expanded to nothing (which is possible
* allow this, the parsed Query will look like it didn't have RETURNING,
* with results that would probably surprise the user.
*/
- if (rlist == NIL)
+ if (qry->returningList == NIL)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("RETURNING must have at least one column"),
parser_errposition(pstate,
- exprLocation(linitial(returningList)))));
+ exprLocation(linitial(returningClause->exprs)))));
/* mark column origins */
- markTargetListOrigins(pstate, rlist);
+ markTargetListOrigins(pstate, qry->returningList);
/* resolve any still-unresolved output columns as being type text */
if (pstate->p_resolve_unknowns)
- resolveTargetListUnknowns(pstate, rlist);
+ resolveTargetListUnknowns(pstate, qry->returningList);
/* restore state */
+ pstate->p_namespace = list_truncate(pstate->p_namespace, save_nslen);
pstate->p_next_resno = save_next_resno;
-
- return rlist;
}
MergeWhenClause *mergewhen;
struct KeyActions *keyactions;
struct KeyAction *keyaction;
+ ReturningClause *retclause;
+ ReturningOptionKind retoptionkind;
}
%type <node> stmt toplevel_stmt schema_stmt routine_body_stmt
opclass_purpose opt_opfamily transaction_mode_list_or_empty
OptTableFuncElementList TableFuncElementList opt_type_modifiers
prep_type_clause
- execute_param_clause using_clause returning_clause
+ execute_param_clause using_clause
+ returning_with_clause returning_options
opt_enum_val_list enum_val_list table_func_column_list
create_generic_options alter_generic_options
relation_expr_list dostmt_opt_list
vacuum_relation_list opt_vacuum_relation_list
drop_option_list pub_obj_list
+%type <retclause> returning_clause
+%type <node> returning_option
+%type <retoptionkind> returning_option_kind
%type <node> opt_routine_body
%type <groupclause> group_clause
%type <list> group_by_list
{
$5->relation = $4;
$5->onConflictClause = $6;
- $5->returningList = $7;
+ $5->returningClause = $7;
$5->withClause = $1;
$5->stmt_location = @$;
$$ = (Node *) $5;
;
returning_clause:
- RETURNING target_list { $$ = $2; }
- | /* EMPTY */ { $$ = NIL; }
+ RETURNING returning_with_clause target_list
+ {
+ ReturningClause *n = makeNode(ReturningClause);
+
+ n->options = $2;
+ n->exprs = $3;
+ $$ = n;
+ }
+ | /* EMPTY */
+ {
+ $$ = NULL;
+ }
+ ;
+
+returning_with_clause:
+ WITH '(' returning_options ')' { $$ = $3; }
+ | /* EMPTY */ { $$ = NIL; }
+ ;
+
+returning_options:
+ returning_option { $$ = list_make1($1); }
+ | returning_options ',' returning_option { $$ = lappend($1, $3); }
+ ;
+
+returning_option:
+ returning_option_kind AS ColId
+ {
+ ReturningOption *n = makeNode(ReturningOption);
+
+ n->option = $1;
+ n->value = $3;
+ n->location = @1;
+ $$ = (Node *) n;
+ }
+ ;
+
+returning_option_kind:
+ OLD { $$ = RETURNING_OPTION_OLD; }
+ | NEW { $$ = RETURNING_OPTION_NEW; }
;
n->relation = $4;
n->usingClause = $5;
n->whereClause = $6;
- n->returningList = $7;
+ n->returningClause = $7;
n->withClause = $1;
n->stmt_location = @$;
$$ = (Node *) n;
n->targetList = $5;
n->fromClause = $6;
n->whereClause = $7;
- n->returningList = $8;
+ n->returningClause = $8;
n->withClause = $1;
n->stmt_location = @$;
$$ = (Node *) n;
m->sourceRelation = $6;
m->joinCondition = $8;
m->mergeWhenClauses = $9;
- m->returningList = $10;
+ m->returningClause = $10;
m->stmt_location = @$;
$$ = (Node *) m;
jnsitem->p_cols_visible = true;
jnsitem->p_lateral_only = false;
jnsitem->p_lateral_ok = true;
+ jnsitem->p_returning_type = VAR_RETURNING_DEFAULT;
/* Per SQL, we must check for alias conflicts */
checkNameSpaceConflicts(pstate, list_make1(jnsitem), my_namespace);
my_namespace = lappend(my_namespace, jnsitem);
nscol->p_varcollid,
0);
/* makeVar doesn't offer parameters for these, so set by hand: */
+ var->varreturningtype = nscol->p_varreturningtype;
var->varnosyn = nscol->p_varnosyn;
var->varattnosyn = nscol->p_varattnosyn;
* point, there seems no harm in expanding it now rather than during
* planning.
*
+ * Note that if the nsitem is an OLD/NEW alias for the target RTE (as can
+ * appear in a RETURNING list), its alias won't match the target RTE's
+ * alias, but we still want to make a whole-row Var here rather than a
+ * RowExpr, for consistency with direct references to the target RTE, and
+ * so that any dropped columns are handled correctly. Thus we also check
+ * p_returning_type here.
+ *
* Note that if the RTE is a function returning scalar, we create just a
* plain reference to the function value, not a composite containing a
* single column. This is pretty inconsistent at first sight, but it's
* "rel.*" mean the same thing for composite relations, so why not for
* scalar functions...
*/
- if (nsitem->p_names == nsitem->p_rte->eref)
+ if (nsitem->p_names == nsitem->p_rte->eref ||
+ nsitem->p_returning_type != VAR_RETURNING_DEFAULT)
{
Var *result;
result = makeWholeRowVar(nsitem->p_rte, nsitem->p_rtindex,
sublevels_up, true);
+ /* mark Var for RETURNING OLD/NEW, as necessary */
+ result->varreturningtype = nsitem->p_returning_type;
+
/* location is not filled in by makeWholeRowVar */
result->location = location;
* are in the RTE. We needn't worry about marking the RTE for SELECT
* access, as the common columns are surely so marked already.
*/
- expandRTE(nsitem->p_rte, nsitem->p_rtindex,
- sublevels_up, location, false,
- NULL, &fields);
+ expandRTE(nsitem->p_rte, nsitem->p_rtindex, sublevels_up,
+ nsitem->p_returning_type, location, false, NULL, &fields);
rowexpr = makeNode(RowExpr);
rowexpr->args = list_truncate(fields,
list_length(nsitem->p_names->colnames));
qry->jointree = makeFromExpr(pstate->p_joinlist, NULL);
/* Transform the RETURNING list, if any */
- qry->returningList = transformReturningList(pstate, stmt->returningList,
- EXPR_KIND_MERGE_RETURNING);
+ transformReturningClause(pstate, qry, stmt->returningClause,
+ EXPR_KIND_MERGE_RETURNING);
/*
* We now have a good query shape, so now look at the WHEN conditions and
int rtindex, AttrNumber col);
static void expandRelation(Oid relid, Alias *eref,
int rtindex, int sublevels_up,
+ VarReturningType returning_type,
int location, bool include_dropped,
List **colnames, List **colvars);
static void expandTupleDesc(TupleDesc tupdesc, Alias *eref,
int count, int offset,
int rtindex, int sublevels_up,
+ VarReturningType returning_type,
int location, bool include_dropped,
List **colnames, List **colvars);
static int specialAttNum(const char *attname);
}
var->location = location;
+ /* Mark Var for RETURNING OLD/NEW, as necessary */
+ var->varreturningtype = nsitem->p_returning_type;
+
/* Mark Var if it's nulled by any outer joins */
markNullableIfNeeded(pstate, var);
nsitem->p_cols_visible = true;
nsitem->p_lateral_only = false;
nsitem->p_lateral_ok = true;
+ nsitem->p_returning_type = VAR_RETURNING_DEFAULT;
return nsitem;
}
nsitem->p_cols_visible = true;
nsitem->p_lateral_only = false;
nsitem->p_lateral_ok = true;
+ nsitem->p_returning_type = VAR_RETURNING_DEFAULT;
return nsitem;
}
nsitem->p_cols_visible = true;
nsitem->p_lateral_only = false;
nsitem->p_lateral_ok = true;
+ nsitem->p_returning_type = VAR_RETURNING_DEFAULT;
return nsitem;
}
* results. If include_dropped is true then empty strings and NULL constants
* (not Vars!) are returned for dropped columns.
*
- * rtindex, sublevels_up, and location are the varno, varlevelsup, and location
- * values to use in the created Vars. Ordinarily rtindex should match the
- * actual position of the RTE in its rangetable.
+ * rtindex, sublevels_up, returning_type, and location are the varno,
+ * varlevelsup, varreturningtype, and location values to use in the created
+ * Vars. Ordinarily rtindex should match the actual position of the RTE in
+ * its rangetable.
*
* The output lists go into *colnames and *colvars.
* If only one of the two kinds of output list is needed, pass NULL for the
*/
void
expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up,
+ VarReturningType returning_type,
int location, bool include_dropped,
List **colnames, List **colvars)
{
case RTE_RELATION:
/* Ordinary relation RTE */
expandRelation(rte->relid, rte->eref,
- rtindex, sublevels_up, location,
+ rtindex, sublevels_up, returning_type, location,
include_dropped, colnames, colvars);
break;
case RTE_SUBQUERY:
exprTypmod((Node *) te->expr),
exprCollation((Node *) te->expr),
sublevels_up);
+ varnode->varreturningtype = returning_type;
varnode->location = location;
*colvars = lappend(*colvars, varnode);
Assert(tupdesc);
expandTupleDesc(tupdesc, rte->eref,
rtfunc->funccolcount, atts_done,
- rtindex, sublevels_up, location,
+ rtindex, sublevels_up,
+ returning_type, location,
include_dropped, colnames, colvars);
}
else if (functypclass == TYPEFUNC_SCALAR)
exprTypmod(rtfunc->funcexpr),
exprCollation(rtfunc->funcexpr),
sublevels_up);
+ varnode->varreturningtype = returning_type;
varnode->location = location;
*colvars = lappend(*colvars, varnode);
attrtypmod,
attrcollation,
sublevels_up);
+ varnode->varreturningtype = returning_type;
varnode->location = location;
*colvars = lappend(*colvars, varnode);
}
InvalidOid,
sublevels_up);
+ varnode->varreturningtype = returning_type;
*colvars = lappend(*colvars, varnode);
}
}
exprTypmod(avar),
exprCollation(avar),
sublevels_up);
+ varnode->varreturningtype = returning_type;
varnode->location = location;
*colvars = lappend(*colvars, varnode);
varnode = makeVar(rtindex, varattno,
coltype, coltypmod, colcoll,
sublevels_up);
+ varnode->varreturningtype = returning_type;
varnode->location = location;
*colvars = lappend(*colvars, varnode);
*/
static void
expandRelation(Oid relid, Alias *eref, int rtindex, int sublevels_up,
+ VarReturningType returning_type,
int location, bool include_dropped,
List **colnames, List **colvars)
{
/* Get the tupledesc and turn it over to expandTupleDesc */
rel = relation_open(relid, AccessShareLock);
expandTupleDesc(rel->rd_att, eref, rel->rd_att->natts, 0,
- rtindex, sublevels_up,
+ rtindex, sublevels_up, returning_type,
location, include_dropped,
colnames, colvars);
relation_close(rel, AccessShareLock);
static void
expandTupleDesc(TupleDesc tupdesc, Alias *eref, int count, int offset,
int rtindex, int sublevels_up,
+ VarReturningType returning_type,
int location, bool include_dropped,
List **colnames, List **colvars)
{
attr->atttypid, attr->atttypmod,
attr->attcollation,
sublevels_up);
+ varnode->varreturningtype = returning_type;
varnode->location = location;
*colvars = lappend(*colvars, varnode);
nscol->p_varcollid,
sublevels_up);
/* makeVar doesn't offer parameters for these, so set by hand: */
+ var->varreturningtype = nscol->p_varreturningtype;
var->varnosyn = nscol->p_varnosyn;
var->varattnosyn = nscol->p_varattnosyn;
var->location = location;
*lvar;
int i;
- expandRTE(rte, var->varno, 0, var->location, false,
- &names, &vars);
+ expandRTE(rte, var->varno, 0, var->varreturningtype,
+ var->location, false, &names, &vars);
tupleDesc = CreateTemplateTupleDesc(list_length(vars));
i = 1;
0,
rt_fetch(new_varno, sub_action->rtable),
parsetree->targetList,
+ sub_action->resultRelation,
(event == CMD_UPDATE) ?
REPLACEVARS_CHANGE_VARNO :
REPLACEVARS_SUBSTITUTE_NULL,
rt_fetch(parsetree->resultRelation,
parsetree->rtable),
rule_action->returningList,
+ rule_action->resultRelation,
REPLACEVARS_REPORT_ERROR,
0,
&rule_action->hasSubLinks);
+ /* use triggering query's aliases for OLD and NEW in RETURNING list */
+ rule_action->returningOldAlias = parsetree->returningOldAlias;
+ rule_action->returningNewAlias = parsetree->returningNewAlias;
+
/*
* There could have been some SubLinks in parsetree's returningList,
* in which case we'd better mark the rule_action correctly.
rt_fetch(rt_index,
parsetree->rtable),
parsetree->targetList,
+ parsetree->resultRelation,
(event == CMD_UPDATE) ?
REPLACEVARS_CHANGE_VARNO :
REPLACEVARS_SUBSTITUTE_NULL,
0,
view_rte,
view_targetlist,
+ new_rt_index,
REPLACEVARS_REPORT_ERROR,
0,
NULL);
0,
view_rte,
tmp_tlist,
+ new_rt_index,
REPLACEVARS_REPORT_ERROR,
0,
&parsetree->hasSubLinks);
phv->phlevelsup += context->delta_sublevels_up;
/* fall through to recurse into argument */
}
+ if (IsA(node, ReturningExpr))
+ {
+ ReturningExpr *rexpr = (ReturningExpr *) node;
+
+ if (rexpr->retlevelsup >= context->min_sublevels_up)
+ rexpr->retlevelsup += context->delta_sublevels_up;
+ /* fall through to recurse into argument */
+ }
if (IsA(node, RangeTblEntry))
{
RangeTblEntry *rte = (RangeTblEntry *) node;
QTW_EXAMINE_RTES_BEFORE);
}
+/*
+ * SetVarReturningType - adjust Var nodes for a specified varreturningtype.
+ *
+ * Find all Var nodes referring to the specified result relation in the given
+ * expression and set their varreturningtype to the specified value.
+ *
+ * NOTE: although this has the form of a walker, we cheat and modify the
+ * Var nodes in-place. The given expression tree should have been copied
+ * earlier to ensure that no unwanted side-effects occur!
+ */
+
+typedef struct
+{
+ int result_relation;
+ int sublevels_up;
+ VarReturningType returning_type;
+} SetVarReturningType_context;
+
+static bool
+SetVarReturningType_walker(Node *node, SetVarReturningType_context *context)
+{
+ if (node == NULL)
+ return false;
+ if (IsA(node, Var))
+ {
+ Var *var = (Var *) node;
+
+ if (var->varno == context->result_relation &&
+ var->varlevelsup == context->sublevels_up)
+ var->varreturningtype = context->returning_type;
+
+ return false;
+ }
+
+ if (IsA(node, Query))
+ {
+ /* Recurse into subselects */
+ bool result;
+
+ context->sublevels_up++;
+ result = query_tree_walker((Query *) node, SetVarReturningType_walker,
+ context, 0);
+ context->sublevels_up--;
+ return result;
+ }
+ return expression_tree_walker(node, SetVarReturningType_walker, context);
+}
+
+static void
+SetVarReturningType(Node *node, int result_relation, int sublevels_up,
+ VarReturningType returning_type)
+{
+ SetVarReturningType_context context;
+
+ context.result_relation = result_relation;
+ context.sublevels_up = sublevels_up;
+ context.returning_type = returning_type;
+
+ /* Expect to start with an expression */
+ SetVarReturningType_walker(node, &context);
+}
/*
* rangeTableEntry_used - detect whether an RTE is referenced somewhere
* relation. This is needed to handle whole-row Vars referencing the target.
* We expand such Vars into RowExpr constructs.
*
+ * In addition, for INSERT/UPDATE/DELETE/MERGE queries, the caller must
+ * provide result_relation, the index of the result relation in the rewritten
+ * query. This is needed to handle OLD/NEW RETURNING list Vars referencing
+ * target_varno. When such Vars are expanded, their varreturningtype is
+ * copied onto any replacement Vars referencing result_relation. In addition,
+ * if the replacement expression from the targetlist is not simply a Var
+ * referencing result_relation, it is wrapped in a ReturningExpr node (causing
+ * the executor to return NULL if the OLD/NEW row doesn't exist).
+ *
* outer_hasSubLinks works the same as for replace_rte_variables().
*/
{
RangeTblEntry *target_rte;
List *targetlist;
+ int result_relation;
ReplaceVarsNoMatchOption nomatch_option;
int nomatch_varno;
} ReplaceVarsFromTargetList_context;
* dropped columns. If the var is RECORD (ie, this is a JOIN), then
* omit dropped columns. In the latter case, attach column names to
* the RowExpr for use of the executor and ruleutils.c.
+ *
+ * The varreturningtype is copied onto each individual field Var, so
+ * that it is handled correctly when we recurse.
*/
expandRTE(rcon->target_rte,
- var->varno, var->varlevelsup, var->location,
- (var->vartype != RECORDOID),
+ var->varno, var->varlevelsup, var->varreturningtype,
+ var->location, (var->vartype != RECORDOID),
&colnames, &fields);
/* Adjust the generated per-field Vars... */
fields = (List *) replace_rte_variables_mutator((Node *) fields,
rowexpr->colnames = (var->vartype == RECORDOID) ? colnames : NIL;
rowexpr->location = var->location;
+ /* Wrap it in a ReturningExpr, if needed, per comments above */
+ if (var->varreturningtype != VAR_RETURNING_DEFAULT)
+ {
+ ReturningExpr *rexpr = makeNode(ReturningExpr);
+
+ rexpr->retlevelsup = var->varlevelsup;
+ rexpr->retold = (var->varreturningtype == VAR_RETURNING_OLD);
+ rexpr->retexpr = (Expr *) rowexpr;
+
+ return (Node *) rexpr;
+ }
+
return (Node *) rowexpr;
}
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("NEW variables in ON UPDATE rules cannot reference columns that are part of a multiple assignment in the subject UPDATE command")));
+ /* Handle any OLD/NEW RETURNING list Vars */
+ if (var->varreturningtype != VAR_RETURNING_DEFAULT)
+ {
+ /*
+ * Copy varreturningtype onto any Vars in the tlist item that
+ * refer to result_relation (which had better be non-zero).
+ */
+ if (rcon->result_relation == 0)
+ elog(ERROR, "variable returning old/new found outside RETURNING list");
+
+ SetVarReturningType((Node *) newnode, rcon->result_relation,
+ var->varlevelsup, var->varreturningtype);
+
+ /* Wrap it in a ReturningExpr, if needed, per comments above */
+ if (!IsA(newnode, Var) ||
+ ((Var *) newnode)->varno != rcon->result_relation ||
+ ((Var *) newnode)->varlevelsup != var->varlevelsup)
+ {
+ ReturningExpr *rexpr = makeNode(ReturningExpr);
+
+ rexpr->retlevelsup = var->varlevelsup;
+ rexpr->retold = (var->varreturningtype == VAR_RETURNING_OLD);
+ rexpr->retexpr = newnode;
+
+ newnode = (Expr *) rexpr;
+ }
+ }
+
return (Node *) newnode;
}
}
int target_varno, int sublevels_up,
RangeTblEntry *target_rte,
List *targetlist,
+ int result_relation,
ReplaceVarsNoMatchOption nomatch_option,
int nomatch_varno,
bool *outer_hasSubLinks)
context.target_rte = target_rte;
context.targetlist = targetlist;
+ context.result_relation = result_relation;
context.nomatch_option = nomatch_option;
context.nomatch_varno = nomatch_varno;
List *subplans; /* List of Plan trees for SubPlans */
List *ctes; /* List of CommonTableExpr nodes */
AppendRelInfo **appendrels; /* Array of AppendRelInfo nodes, or NULL */
+ char *ret_old_alias; /* alias for OLD in RETURNING list */
+ char *ret_new_alias; /* alias for NEW in RETURNING list */
/* Workspace for column alias assignment: */
bool unique_using; /* Are we making USING names globally unique */
List *using_names; /* List of assigned names for USING columns */
static void get_utility_query_def(Query *query, deparse_context *context);
static void get_basic_select_query(Query *query, deparse_context *context);
static void get_target_list(List *targetList, deparse_context *context);
+static void get_returning_clause(Query *query, deparse_context *context);
static void get_setop_query(Node *setOp, Query *query,
deparse_context *context);
static Node *get_rule_sortgroupclause(Index ref, List *tlist,
* the most-closely-nested first. This is needed to resolve PARAM_EXEC
* Params. Note we assume that all the Plan nodes share the same rtable.
*
+ * For a ModifyTable plan, we might also need to resolve references to OLD/NEW
+ * variables in the RETURNING list, so we copy the alias names of the OLD and
+ * NEW rows from the ModifyTable plan node.
+ *
* Once this function has been called, deparse_expression() can be called on
* subsidiary expression(s) of the specified Plan node. To deparse
* expressions of a different Plan node in the same Plan tree, re-call this
dpns->ancestors = ancestors;
set_deparse_plan(dpns, plan);
+ /* For ModifyTable, set aliases for OLD and NEW in RETURNING */
+ if (IsA(plan, ModifyTable))
+ {
+ dpns->ret_old_alias = ((ModifyTable *) plan)->returningOldAlias;
+ dpns->ret_new_alias = ((ModifyTable *) plan)->returningNewAlias;
+ }
+
return dpcontext;
}
dpns->subplans = NIL;
dpns->ctes = query->cteList;
dpns->appendrels = NULL;
+ dpns->ret_old_alias = query->returningOldAlias;
+ dpns->ret_new_alias = query->returningNewAlias;
/* Assign a unique relation alias to each RTE */
set_rtable_names(dpns, parent_namespaces, NULL);
if (rte->rtekind == RTE_FUNCTION && rte->functions != NIL)
{
/* Since we're not creating Vars, rtindex etc. don't matter */
- expandRTE(rte, 1, 0, -1, true /* include dropped */ ,
- &colnames, NULL);
+ expandRTE(rte, 1, 0, VAR_RETURNING_DEFAULT, -1,
+ true /* include dropped */ , &colnames, NULL);
}
else
colnames = rte->eref->colnames;
pfree(targetbuf.data);
}
+static void
+get_returning_clause(Query *query, deparse_context *context)
+{
+ StringInfo buf = context->buf;
+
+ if (query->returningList)
+ {
+ bool have_with = false;
+
+ appendContextKeyword(context, " RETURNING",
+ -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
+
+ /* Add WITH (OLD/NEW) options, if they're not the defaults */
+ if (query->returningOldAlias && strcmp(query->returningOldAlias, "old") != 0)
+ {
+ appendStringInfo(buf, " WITH (OLD AS %s",
+ quote_identifier(query->returningOldAlias));
+ have_with = true;
+ }
+ if (query->returningNewAlias && strcmp(query->returningNewAlias, "new") != 0)
+ {
+ if (have_with)
+ appendStringInfo(buf, ", NEW AS %s",
+ quote_identifier(query->returningNewAlias));
+ else
+ {
+ appendStringInfo(buf, " WITH (NEW AS %s",
+ quote_identifier(query->returningNewAlias));
+ have_with = true;
+ }
+ }
+ if (have_with)
+ appendStringInfoChar(buf, ')');
+
+ /* Add the returning expressions themselves */
+ get_target_list(query->returningList, context);
+ }
+}
+
static void
get_setop_query(Node *setOp, Query *query, deparse_context *context)
{
/* Add RETURNING if present */
if (query->returningList)
- {
- appendContextKeyword(context, " RETURNING",
- -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
- get_target_list(query->returningList, context);
- }
+ get_returning_clause(query, context);
}
/* Add RETURNING if present */
if (query->returningList)
- {
- appendContextKeyword(context, " RETURNING",
- -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
- get_target_list(query->returningList, context);
- }
+ get_returning_clause(query, context);
}
/* Add RETURNING if present */
if (query->returningList)
- {
- appendContextKeyword(context, " RETURNING",
- -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
- get_target_list(query->returningList, context);
- }
+ get_returning_clause(query, context);
}
/* Add RETURNING if present */
if (query->returningList)
- {
- appendContextKeyword(context, " RETURNING",
- -PRETTYINDENT_STD, PRETTYINDENT_STD, 1);
- get_target_list(query->returningList, context);
- }
+ get_returning_clause(query, context);
}
}
rte = rt_fetch(varno, dpns->rtable);
- refname = (char *) list_nth(dpns->rtable_names, varno - 1);
+
+ /* might be returning old/new column value */
+ if (var->varreturningtype == VAR_RETURNING_OLD)
+ refname = dpns->ret_old_alias;
+ else if (var->varreturningtype == VAR_RETURNING_NEW)
+ refname = dpns->ret_new_alias;
+ else
+ refname = (char *) list_nth(dpns->rtable_names, varno - 1);
+
colinfo = deparse_columns_fetch(varno, dpns);
attnum = varattno;
}
attname = get_rte_attribute_name(rte, attnum);
}
- need_prefix = (context->varprefix || attname == NULL);
+ need_prefix = (context->varprefix || attname == NULL ||
+ var->varreturningtype != VAR_RETURNING_DEFAULT);
/*
* If we're considering a plain Var in an ORDER BY (but not GROUP BY)
case T_ConvertRowtypeExpr:
return isSimpleNode((Node *) ((ConvertRowtypeExpr *) node)->arg,
node, prettyFlags);
+ case T_ReturningExpr:
+ return isSimpleNode((Node *) ((ReturningExpr *) node)->retexpr,
+ node, prettyFlags);
case T_OpExpr:
{
}
break;
+ case T_ReturningExpr:
+ {
+ ReturningExpr *retExpr = (ReturningExpr *) node;
+
+ /*
+ * We cannot see a ReturningExpr in rule deparsing, only while
+ * EXPLAINing a query plan (ReturningExpr nodes are only ever
+ * adding during query rewriting). Just display the expression
+ * returned (an expanded view column).
+ */
+ get_rule_expr((Node *) retExpr->retexpr, context, showimplicit);
+ }
+ break;
+
case T_PartitionBoundSpec:
{
PartitionBoundSpec *spec = (PartitionBoundSpec *) node;
*/
/* yyyymmddN */
-#define CATALOG_VERSION_NO 202501401
+#define CATALOG_VERSION_NO 202501161
#endif
/* 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,
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,
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,
EEOP_SQLVALUEFUNCTION,
EEOP_CURRENTOFEXPR,
EEOP_NEXTVALUEEXPR,
+ EEOP_RETURNINGEXPR,
EEOP_ARRAYEXPR,
EEOP_ARRAYCOERCE,
EEOP_ROW,
*/
union
{
- /* for EEOP_INNER/OUTER/SCAN_FETCHSOME */
+ /* for EEOP_INNER/OUTER/SCAN/OLD/NEW_FETCHSOME */
struct
{
/* attribute number up to which to fetch (inclusive) */
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 */
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
{
extern TupleTableSlot *ExecGetTriggerOldSlot(EState *estate, ResultRelInfo *relInfo);
extern TupleTableSlot *ExecGetTriggerNewSlot(EState *estate, ResultRelInfo *relInfo);
extern TupleTableSlot *ExecGetReturningSlot(EState *estate, ResultRelInfo *relInfo);
+extern TupleTableSlot *ExecGetAllNullSlot(EState *estate, ResultRelInfo *relInfo);
extern TupleConversionMap *ExecGetChildToRootMap(ResultRelInfo *resultRelInfo);
extern TupleConversionMap *ExecGetRootToChildMap(ResultRelInfo *resultRelInfo, EState *estate);
/* 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 */
/*
#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;
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;
OnConflictExpr *onConflict; /* ON CONFLICT DO [NOTHING | UPDATE] */
+ /*
+ * The following three fields describe the contents of the RETURNING list
+ * for INSERT/UPDATE/DELETE/MERGE. returningOldAlias and returningNewAlias
+ * are the alias names for OLD and NEW, which may be user-supplied values,
+ * the defaults "old" and "new", or NULL (if the default "old"/"new" is
+ * already in use as the alias for some other relation).
+ */
+ char *returningOldAlias pg_node_attr(query_jumble_ignore);
+ char *returningNewAlias pg_node_attr(query_jumble_ignore);
List *returningList; /* return-values list (of TargetEntry) */
List *groupClause; /* a list of SortGroupClause's */
List *values; /* VALUES to INSERT, or NULL */
} MergeWhenClause;
+/*
+ * ReturningOptionKind -
+ * Possible kinds of option in RETURNING WITH(...) list
+ *
+ * Currently, this is used only for specifying OLD/NEW aliases.
+ */
+typedef enum ReturningOptionKind
+{
+ RETURNING_OPTION_OLD, /* specify alias for OLD in RETURNING */
+ RETURNING_OPTION_NEW, /* specify alias for NEW in RETURNING */
+} ReturningOptionKind;
+
+/*
+ * ReturningOption -
+ * An individual option in the RETURNING WITH(...) list
+ */
+typedef struct ReturningOption
+{
+ NodeTag type;
+ ReturningOptionKind option; /* specified option */
+ char *value; /* option's value */
+ ParseLoc location; /* token location, or -1 if unknown */
+} ReturningOption;
+
+/*
+ * ReturningClause -
+ * List of RETURNING expressions, together with any WITH(...) options
+ */
+typedef struct ReturningClause
+{
+ NodeTag type;
+ List *options; /* list of ReturningOption elements */
+ List *exprs; /* list of expressions to return */
+} ReturningClause;
+
/*
* TriggerTransition -
* representation of transition row or table naming clause
List *cols; /* optional: names of the target columns */
Node *selectStmt; /* the source SELECT/VALUES, or NULL */
OnConflictClause *onConflictClause; /* ON CONFLICT clause */
- List *returningList; /* list of expressions to return */
+ ReturningClause *returningClause; /* RETURNING clause */
WithClause *withClause; /* WITH clause */
OverridingKind override; /* OVERRIDING clause */
ParseLoc stmt_location; /* start location, or -1 if unknown */
RangeVar *relation; /* relation to delete from */
List *usingClause; /* optional using clause for more tables */
Node *whereClause; /* qualifications */
- List *returningList; /* list of expressions to return */
+ &nb