2 -- Test INSERT/UPDATE/DELETE RETURNING
5 CREATE TEMP TABLE foo (f1 serial, f2 text, f3 int default 42);
6 NOTICE: CREATE TABLE will create implicit sequence "foo_f1_seq" for serial column "foo.f1"
7 INSERT INTO foo (f2,f3)
8 VALUES ('test', DEFAULT), ('More', 11), (upper('more'), 7+9)
9 RETURNING *, f1+f3 AS sum;
11 ----+------+----+-----
25 UPDATE foo SET f2 = lower(f2), f3 = DEFAULT RETURNING foo.*, f1+f3 AS sum13;
27 ----+------+----+-------
41 DELETE FROM foo WHERE f1 > 2 RETURNING f3, f2, f1, least(f1,f3);
43 ----+------+----+-------
54 -- Subplans and initplans in the RETURNING list
55 INSERT INTO foo SELECT f1+10, f2, f3+99 FROM foo
56 RETURNING *, f1+112 IN (SELECT q1 FROM int8_tbl) AS subplan,
57 EXISTS(SELECT * FROM int4_tbl) AS initplan;
58 f1 | f2 | f3 | subplan | initplan
59 ----+------+-----+---------+----------
60 11 | test | 141 | t | t
61 12 | more | 141 | f | t
64 UPDATE foo SET f3 = f3 * 2
66 RETURNING *, f1+112 IN (SELECT q1 FROM int8_tbl) AS subplan,
67 EXISTS(SELECT * FROM int4_tbl) AS initplan;
68 f1 | f2 | f3 | subplan | initplan
69 ----+------+-----+---------+----------
70 11 | test | 282 | t | t
71 12 | more | 282 | f | t
76 RETURNING *, f1+112 IN (SELECT q1 FROM int8_tbl) AS subplan,
77 EXISTS(SELECT * FROM int4_tbl) AS initplan;
78 f1 | f2 | f3 | subplan | initplan
79 ----+------+-----+---------+----------
80 11 | test | 282 | t | t
81 12 | more | 282 | f | t
85 UPDATE foo SET f3 = f3*2
87 WHERE foo.f1 + 123455 = i.f1
88 RETURNING foo.*, i.f1 as "i.f1";
90 ----+------+----+--------
91 1 | test | 84 | 123456
103 WHERE foo.f1 + 123455 = i.f1
104 RETURNING foo.*, i.f1 as "i.f1";
106 ----+------+----+--------
107 1 | test | 84 | 123456
116 -- Check inheritance cases
117 CREATE TEMP TABLE foochild (fc int) INHERITS (foo);
118 INSERT INTO foochild VALUES(123,'child',999,-123);
119 ALTER TABLE foo ADD COLUMN f4 int8 DEFAULT 99;
122 -----+-------+-----+----
124 123 | child | 999 | 99
127 SELECT * FROM foochild;
128 f1 | f2 | f3 | fc | f4
129 -----+-------+-----+------+----
130 123 | child | 999 | -123 | 99
133 UPDATE foo SET f4 = f4 + f3 WHERE f4 = 99 RETURNING *;
135 -----+-------+-----+------
137 123 | child | 999 | 1098
142 -----+-------+-----+------
144 123 | child | 999 | 1098
147 SELECT * FROM foochild;
148 f1 | f2 | f3 | fc | f4
149 -----+-------+-----+------+------
150 123 | child | 999 | -123 | 1098
153 UPDATE foo SET f3 = f3*2
157 f1 | f2 | f3 | f4 | q1 | q2
158 -----+-------+------+------+------------------+-----
159 123 | child | 1998 | 1098 | 4567890123456789 | 123
164 -----+-------+------+------
166 123 | child | 1998 | 1098
169 SELECT * FROM foochild;
170 f1 | f2 | f3 | fc | f4
171 -----+-------+------+------+------
172 123 | child | 1998 | -123 | 1098
179 f1 | f2 | f3 | f4 | q1 | q2
180 -----+-------+------+------+------------------+-----
181 123 | child | 1998 | 1098 | 4567890123456789 | 123
186 ----+------+----+-----
190 SELECT * FROM foochild;
191 f1 | f2 | f3 | fc | f4
192 ----+----+----+----+----
197 CREATE TEMP VIEW voo AS SELECT f1, f2 FROM foo;
198 CREATE RULE voo_i AS ON INSERT TO voo DO INSTEAD
199 INSERT INTO foo VALUES(new.*, 57);
200 INSERT INTO voo VALUES(11,'zit');
202 INSERT INTO voo VALUES(12,'zoo') RETURNING *, f1*2;
203 ERROR: cannot perform INSERT RETURNING on relation "voo"
204 HINT: You need an unconditional ON INSERT DO INSTEAD rule with a RETURNING clause.
205 -- fails, incompatible list:
206 CREATE OR REPLACE RULE voo_i AS ON INSERT TO voo DO INSTEAD
207 INSERT INTO foo VALUES(new.*, 57) RETURNING *;
208 ERROR: RETURNING list has too many entries
209 CREATE OR REPLACE RULE voo_i AS ON INSERT TO voo DO INSTEAD
210 INSERT INTO foo VALUES(new.*, 57) RETURNING f1, f2;
212 INSERT INTO voo VALUES(13,'zit2');
214 INSERT INTO voo VALUES(14,'zoo2') RETURNING *;
222 ----+------+----+-----
238 CREATE OR REPLACE RULE voo_u AS ON UPDATE TO voo DO INSTEAD
239 UPDATE foo SET f1 = new.f1, f2 = new.f2 WHERE f1 = old.f1
241 update voo set f1 = f1 + 1 where f2 = 'zoo2';
242 update voo set f1 = f1 + 1 where f2 = 'zoo2' RETURNING *, f1*2;
244 ----+------+----------
250 ----+------+----+-----
266 CREATE OR REPLACE RULE voo_d AS ON DELETE TO voo DO INSTEAD
267 DELETE FROM foo WHERE f1 = old.f1
269 DELETE FROM foo WHERE f1 = 13;
270 DELETE FROM foo WHERE f2 = 'zit' RETURNING *;
278 ----+------+----+-----
291 CREATE TEMP TABLE joinme (f2j text, other int);
292 INSERT INTO joinme VALUES('more', 12345);
293 INSERT INTO joinme VALUES('zoo2', 54321);
294 INSERT INTO joinme VALUES('other', 0);
295 CREATE TEMP VIEW joinview AS
296 SELECT foo.*, other FROM foo JOIN joinme ON (f2 = f2j);
297 SELECT * FROM joinview;
298 f1 | f2 | f3 | f4 | other
299 ----+------+----+-----+-------
300 2 | more | 42 | 141 | 12345
301 16 | zoo2 | 57 | 99 | 54321
304 CREATE RULE joinview_u AS ON UPDATE TO joinview DO INSTEAD
305 UPDATE foo SET f1 = new.f1, f3 = new.f3
306 FROM joinme WHERE f2 = f2j AND f2 = old.f2
307 RETURNING foo.*, other;
308 UPDATE joinview SET f1 = f1 + 1 WHERE f3 = 57 RETURNING *, other + 1;
309 f1 | f2 | f3 | f4 | other | ?column?
310 ----+------+----+----+-------+----------
311 17 | zoo2 | 57 | 99 | 54321 | 54322
314 SELECT * FROM joinview;
315 f1 | f2 | f3 | f4 | other
316 ----+------+----+-----+-------
317 2 | more | 42 | 141 | 12345
318 17 | zoo2 | 57 | 99 | 54321
323 ----+------+----+-----