2 -- Test INSERT/UPDATE/DELETE RETURNING
5 CREATE TEMP TABLE foo (f1 serial, f2 text, f3 int default 42);
6 INSERT INTO foo (f2,f3)
7 VALUES ('test', DEFAULT), ('More', 11), (upper('more'), 7+9)
8 RETURNING *, f1+f3 AS sum;
10 ----+------+----+-----
24 UPDATE foo SET f2 = lower(f2), f3 = DEFAULT RETURNING foo.*, f1+f3 AS sum13;
26 ----+------+----+-------
40 DELETE FROM foo WHERE f1 > 2 RETURNING f3, f2, f1, least(f1,f3);
42 ----+------+----+-------
53 -- Subplans and initplans in the RETURNING list
54 INSERT INTO foo SELECT f1+10, f2, f3+99 FROM foo
55 RETURNING *, f1+112 IN (SELECT q1 FROM int8_tbl) AS subplan,
56 EXISTS(SELECT * FROM int4_tbl) AS initplan;
57 f1 | f2 | f3 | subplan | initplan
58 ----+------+-----+---------+----------
59 11 | test | 141 | t | t
60 12 | more | 141 | f | t
63 UPDATE foo SET f3 = f3 * 2
65 RETURNING *, f1+112 IN (SELECT q1 FROM int8_tbl) AS subplan,
66 EXISTS(SELECT * FROM int4_tbl) AS initplan;
67 f1 | f2 | f3 | subplan | initplan
68 ----+------+-----+---------+----------
69 11 | test | 282 | t | t
70 12 | more | 282 | f | t
75 RETURNING *, f1+112 IN (SELECT q1 FROM int8_tbl) AS subplan,
76 EXISTS(SELECT * FROM int4_tbl) AS initplan;
77 f1 | f2 | f3 | subplan | initplan
78 ----+------+-----+---------+----------
79 11 | test | 282 | t | t
80 12 | more | 282 | f | t
84 UPDATE foo SET f3 = f3*2
86 WHERE foo.f1 + 123455 = i.f1
87 RETURNING foo.*, i.f1 as "i.f1";
89 ----+------+----+--------
90 1 | test | 84 | 123456
102 WHERE foo.f1 + 123455 = i.f1
103 RETURNING foo.*, i.f1 as "i.f1";
105 ----+------+----+--------
106 1 | test | 84 | 123456
115 -- Check inheritance cases
116 CREATE TEMP TABLE foochild (fc int) INHERITS (foo);
117 INSERT INTO foochild VALUES(123,'child',999,-123);
118 ALTER TABLE foo ADD COLUMN f4 int8 DEFAULT 99;
121 -----+-------+-----+----
123 123 | child | 999 | 99
126 SELECT * FROM foochild;
127 f1 | f2 | f3 | fc | f4
128 -----+-------+-----+------+----
129 123 | child | 999 | -123 | 99
132 UPDATE foo SET f4 = f4 + f3 WHERE f4 = 99 RETURNING *;
134 -----+-------+-----+------
136 123 | child | 999 | 1098
141 -----+-------+-----+------
143 123 | child | 999 | 1098
146 SELECT * FROM foochild;
147 f1 | f2 | f3 | fc | f4
148 -----+-------+-----+------+------
149 123 | child | 999 | -123 | 1098
152 UPDATE foo SET f3 = f3*2
156 f1 | f2 | f3 | f4 | q1 | q2
157 -----+-------+------+------+------------------+-----
158 123 | child | 1998 | 1098 | 4567890123456789 | 123
163 -----+-------+------+------
165 123 | child | 1998 | 1098
168 SELECT * FROM foochild;
169 f1 | f2 | f3 | fc | f4
170 -----+-------+------+------+------
171 123 | child | 1998 | -123 | 1098
178 f1 | f2 | f3 | f4 | q1 | q2
179 -----+-------+------+------+------------------+-----
180 123 | child | 1998 | 1098 | 4567890123456789 | 123
185 ----+------+----+-----
189 SELECT * FROM foochild;
190 f1 | f2 | f3 | fc | f4
191 ----+----+----+----+----
196 CREATE TEMP VIEW voo AS SELECT f1, f2 FROM foo;
197 CREATE RULE voo_i AS ON INSERT TO voo DO INSTEAD
198 INSERT INTO foo VALUES(new.*, 57);
199 INSERT INTO voo VALUES(11,'zit');
201 INSERT INTO voo VALUES(12,'zoo') RETURNING *, f1*2;
202 ERROR: cannot perform INSERT RETURNING on relation "voo"
203 HINT: You need an unconditional ON INSERT DO INSTEAD rule with a RETURNING clause.
204 -- fails, incompatible list:
205 CREATE OR REPLACE RULE voo_i AS ON INSERT TO voo DO INSTEAD
206 INSERT INTO foo VALUES(new.*, 57) RETURNING *;
207 ERROR: RETURNING list has too many entries
208 CREATE OR REPLACE RULE voo_i AS ON INSERT TO voo DO INSTEAD
209 INSERT INTO foo VALUES(new.*, 57) RETURNING f1, f2;
211 INSERT INTO voo VALUES(13,'zit2');
213 INSERT INTO voo VALUES(14,'zoo2') RETURNING *;
221 ----+------+----+-----
237 CREATE OR REPLACE RULE voo_u AS ON UPDATE TO voo DO INSTEAD
238 UPDATE foo SET f1 = new.f1, f2 = new.f2 WHERE f1 = old.f1
240 update voo set f1 = f1 + 1 where f2 = 'zoo2';
241 update voo set f1 = f1 + 1 where f2 = 'zoo2' RETURNING *, f1*2;
243 ----+------+----------
249 ----+------+----+-----
265 CREATE OR REPLACE RULE voo_d AS ON DELETE TO voo DO INSTEAD
266 DELETE FROM foo WHERE f1 = old.f1
268 DELETE FROM foo WHERE f1 = 13;
269 DELETE FROM foo WHERE f2 = 'zit' RETURNING *;
277 ----+------+----+-----
290 CREATE TEMP TABLE joinme (f2j text, other int);
291 INSERT INTO joinme VALUES('more', 12345);
292 INSERT INTO joinme VALUES('zoo2', 54321);
293 INSERT INTO joinme VALUES('other', 0);
294 CREATE TEMP VIEW joinview AS
295 SELECT foo.*, other FROM foo JOIN joinme ON (f2 = f2j);
296 SELECT * FROM joinview;
297 f1 | f2 | f3 | f4 | other
298 ----+------+----+-----+-------
299 2 | more | 42 | 141 | 12345
300 16 | zoo2 | 57 | 99 | 54321
303 CREATE RULE joinview_u AS ON UPDATE TO joinview DO INSTEAD
304 UPDATE foo SET f1 = new.f1, f3 = new.f3
305 FROM joinme WHERE f2 = f2j AND f2 = old.f2
306 RETURNING foo.*, other;
307 UPDATE joinview SET f1 = f1 + 1 WHERE f3 = 57 RETURNING *, other + 1;
308 f1 | f2 | f3 | f4 | other | ?column?
309 ----+------+----+----+-------+----------
310 17 | zoo2 | 57 | 99 | 54321 | 54322
313 SELECT * FROM joinview;
314 f1 | f2 | f3 | f4 | other
315 ----+------+----+-----+-------
316 2 | more | 42 | 141 | 12345
317 17 | zoo2 | 57 | 99 | 54321
322 ----+------+----+-----
334 -- Check aliased target relation
335 INSERT INTO foo AS bar DEFAULT VALUES RETURNING *; -- ok
341 INSERT INTO foo AS bar DEFAULT VALUES RETURNING foo.*; -- fails, wrong name
342 ERROR: invalid reference to FROM-clause entry for table "foo"
343 LINE 1: INSERT INTO foo AS bar DEFAULT VALUES RETURNING foo.*;
345 HINT: Perhaps you meant to reference the table alias "bar".
346 INSERT INTO foo AS bar DEFAULT VALUES RETURNING bar.*; -- ok
352 INSERT INTO foo AS bar DEFAULT VALUES RETURNING bar.f3; -- ok