3 -- From Jan's original setup_ruletest.sql and run_ruletest.sql
8 -- Tables and rules for the view test
10 create table rtest_t1 (a int4, b int4);
11 create table rtest_t2 (a int4, b int4);
12 create table rtest_t3 (a int4, b int4);
14 create view rtest_v1 as select * from rtest_t1;
15 create rule rtest_v1_ins as on insert to rtest_v1 do instead
16 insert into rtest_t1 values (new.a, new.b);
17 create rule rtest_v1_upd as on update to rtest_v1 do instead
18 update rtest_t1 set a = new.a, b = new.b
20 create rule rtest_v1_del as on delete to rtest_v1 do instead
21 delete from rtest_t1 where a = old.a;
23 COMMENT ON RULE rtest_v1_bad ON rtest_v1 IS 'bad rule';
24 COMMENT ON RULE rtest_v1_del ON rtest_v1 IS 'delete rule';
25 COMMENT ON RULE rtest_v1_del ON rtest_v1 IS NULL;
27 -- Tables and rules for the constraint update/delete test
30 -- Now that we have multiple action rule support, we check
31 -- both possible syntaxes to define them (The last action
32 -- can but must not have a semicolon at the end).
34 create table rtest_system (sysname text, sysdesc text);
35 create table rtest_interface (sysname text, ifname text);
36 create table rtest_person (pname text, pdesc text);
37 create table rtest_admin (pname text, sysname text);
39 create rule rtest_sys_upd as on update to rtest_system do also (
40 update rtest_interface set sysname = new.sysname
41 where sysname = old.sysname;
42 update rtest_admin set sysname = new.sysname
43 where sysname = old.sysname
46 create rule rtest_sys_del as on delete to rtest_system do also (
47 delete from rtest_interface where sysname = old.sysname;
48 delete from rtest_admin where sysname = old.sysname;
51 create rule rtest_pers_upd as on update to rtest_person do also
52 update rtest_admin set pname = new.pname where pname = old.pname;
54 create rule rtest_pers_del as on delete to rtest_person do also
55 delete from rtest_admin where pname = old.pname;
58 -- Tables and rules for the logging test
60 create table rtest_emp (ename char(20), salary money);
61 create table rtest_emplog (ename char(20), who name, action char(10), newsal money, oldsal money);
62 create table rtest_empmass (ename char(20), salary money);
64 create rule rtest_emp_ins as on insert to rtest_emp do
65 insert into rtest_emplog values (new.ename, current_user,
66 'hired', new.salary, '0.00');
68 create rule rtest_emp_upd as on update to rtest_emp where new.salary != old.salary do
69 insert into rtest_emplog values (new.ename, current_user,
70 'honored', new.salary, old.salary);
72 create rule rtest_emp_del as on delete to rtest_emp do
73 insert into rtest_emplog values (old.ename, current_user,
74 'fired', '0.00', old.salary);
77 -- Tables and rules for the multiple cascaded qualified instead
80 create table rtest_t4 (a int4, b text);
81 create table rtest_t5 (a int4, b text);
82 create table rtest_t6 (a int4, b text);
83 create table rtest_t7 (a int4, b text);
84 create table rtest_t8 (a int4, b text);
85 create table rtest_t9 (a int4, b text);
87 create rule rtest_t4_ins1 as on insert to rtest_t4
88 where new.a >= 10 and new.a < 20 do instead
89 insert into rtest_t5 values (new.a, new.b);
91 create rule rtest_t4_ins2 as on insert to rtest_t4
92 where new.a >= 20 and new.a < 30 do
93 insert into rtest_t6 values (new.a, new.b);
95 create rule rtest_t5_ins as on insert to rtest_t5
97 insert into rtest_t7 values (new.a, new.b);
99 create rule rtest_t6_ins as on insert to rtest_t6
100 where new.a > 25 do instead
101 insert into rtest_t8 values (new.a, new.b);
104 -- Tables and rules for the rule fire order test
106 -- As of PG 7.3, the rules should fire in order by name, regardless
107 -- of INSTEAD attributes or creation order.
109 create table rtest_order1 (a int4);
110 create table rtest_order2 (a int4, b int4, c text);
112 create sequence rtest_seq;
114 create rule rtest_order_r3 as on insert to rtest_order1 do instead
115 insert into rtest_order2 values (new.a, nextval('rtest_seq'),
116 'rule 3 - this should run 3rd');
118 create rule rtest_order_r4 as on insert to rtest_order1
119 where a < 100 do instead
120 insert into rtest_order2 values (new.a, nextval('rtest_seq'),
121 'rule 4 - this should run 4th');
123 create rule rtest_order_r2 as on insert to rtest_order1 do
124 insert into rtest_order2 values (new.a, nextval('rtest_seq'),
125 'rule 2 - this should run 2nd');
127 create rule rtest_order_r1 as on insert to rtest_order1 do instead
128 insert into rtest_order2 values (new.a, nextval('rtest_seq'),
129 'rule 1 - this should run 1st');
132 -- Tables and rules for the instead nothing test
134 create table rtest_nothn1 (a int4, b text);
135 create table rtest_nothn2 (a int4, b text);
136 create table rtest_nothn3 (a int4, b text);
137 create table rtest_nothn4 (a int4, b text);
139 create rule rtest_nothn_r1 as on insert to rtest_nothn1
140 where new.a >= 10 and new.a < 20 do instead nothing;
142 create rule rtest_nothn_r2 as on insert to rtest_nothn1
143 where new.a >= 30 and new.a < 40 do instead nothing;
145 create rule rtest_nothn_r3 as on insert to rtest_nothn2
146 where new.a >= 100 do instead
147 insert into rtest_nothn3 values (new.a, new.b);
149 create rule rtest_nothn_r4 as on insert to rtest_nothn2
153 -- Tests on a view that is select * of a table
154 -- and has insert/update/delete instead rules to
155 -- behave close like the real table.
159 -- We need test date later
161 insert into rtest_t2 values (1, 21);
162 insert into rtest_t2 values (2, 22);
163 insert into rtest_t2 values (3, 23);
165 insert into rtest_t3 values (1, 31);
166 insert into rtest_t3 values (2, 32);
167 insert into rtest_t3 values (3, 33);
168 insert into rtest_t3 values (4, 34);
169 insert into rtest_t3 values (5, 35);
172 insert into rtest_v1 values (1, 11);
173 insert into rtest_v1 values (2, 12);
174 select * from rtest_v1;
176 -- delete with constant expression
177 delete from rtest_v1 where a = 1;
178 select * from rtest_v1;
179 insert into rtest_v1 values (1, 11);
180 delete from rtest_v1 where b = 12;
181 select * from rtest_v1;
182 insert into rtest_v1 values (2, 12);
183 insert into rtest_v1 values (2, 13);
184 select * from rtest_v1;
185 ** Remember the delete rule on rtest_v1: It says
186 ** DO INSTEAD DELETE FROM rtest_t1 WHERE a = old.a
187 ** So this time both rows with a = 2 must get deleted
190 delete from rtest_v1 where b = 12;
191 select * from rtest_v1;
192 delete from rtest_v1;
195 insert into rtest_v1 select * from rtest_t2;
196 select * from rtest_v1;
197 delete from rtest_v1;
199 -- same with swapped targetlist
200 insert into rtest_v1 (b, a) select b, a from rtest_t2;
201 select * from rtest_v1;
203 -- now with only one target attribute
204 insert into rtest_v1 (a) select a from rtest_t3;
205 select * from rtest_v1;
206 select * from rtest_v1 where b isnull;
208 -- let attribute a differ (must be done on rtest_t1 - see above)
209 update rtest_t1 set a = a + 10 where b isnull;
210 delete from rtest_v1 where b isnull;
211 select * from rtest_v1;
213 -- now updates with constant expression
214 update rtest_v1 set b = 42 where a = 2;
215 select * from rtest_v1;
216 update rtest_v1 set b = 99 where b = 42;
217 select * from rtest_v1;
218 update rtest_v1 set b = 88 where b < 50;
219 select * from rtest_v1;
220 delete from rtest_v1;
221 insert into rtest_v1 select rtest_t2.a, rtest_t3.b
222 from rtest_t2, rtest_t3
223 where rtest_t2.a = rtest_t3.a;
224 select * from rtest_v1;
226 -- updates in a mergejoin
227 update rtest_v1 set b = rtest_t2.b from rtest_t2 where rtest_v1.a = rtest_t2.a;
228 select * from rtest_v1;
229 insert into rtest_v1 select * from rtest_t3;
230 select * from rtest_v1;
231 update rtest_t1 set a = a + 10 where b > 30;
232 select * from rtest_v1;
233 update rtest_v1 set a = rtest_t3.a + 20 from rtest_t3 where rtest_v1.b = rtest_t3.b;
234 select * from rtest_v1;
237 -- Test for constraint updates/deletes
239 insert into rtest_system values ('orion', 'Linux Jan Wieck');
240 insert into rtest_system values ('notjw', 'WinNT Jan Wieck (notebook)');
241 insert into rtest_system values ('neptun', 'Fileserver');
243 insert into rtest_interface values ('orion', 'eth0');
244 insert into rtest_interface values ('orion', 'eth1');
245 insert into rtest_interface values ('notjw', 'eth0');
246 insert into rtest_interface values ('neptun', 'eth0');
248 insert into rtest_person values ('jw', 'Jan Wieck');
249 insert into rtest_person values ('bm', 'Bruce Momjian');
251 insert into rtest_admin values ('jw', 'orion');
252 insert into rtest_admin values ('jw', 'notjw');
253 insert into rtest_admin values ('bm', 'neptun');
255 update rtest_system set sysname = 'pluto' where sysname = 'neptun';
257 select * from rtest_interface;
258 select * from rtest_admin;
260 update rtest_person set pname = 'jwieck' where pdesc = 'Jan Wieck';
262 -- Note: use ORDER BY here to ensure consistent output across all systems.
263 -- The above UPDATE affects two rows with equal keys, so they could be
264 -- updated in either order depending on the whim of the local qsort().
266 select * from rtest_admin order by pname, sysname;
268 delete from rtest_system where sysname = 'orion';
270 select * from rtest_interface;
271 select * from rtest_admin;
274 -- Rule qualification test
276 insert into rtest_emp values ('wiecc', '5000.00');
277 insert into rtest_emp values ('gates', '80000.00');
278 update rtest_emp set ename = 'wiecx' where ename = 'wiecc';
279 update rtest_emp set ename = 'wieck', salary = '6000.00' where ename = 'wiecx';
280 update rtest_emp set salary = '7000.00' where ename = 'wieck';
281 delete from rtest_emp where ename = 'gates';
283 select ename, who = current_user as "matches user", action, newsal, oldsal from rtest_emplog order by ename, action, newsal;
284 insert into rtest_empmass values ('meyer', '4000.00');
285 insert into rtest_empmass values ('maier', '5000.00');
286 insert into rtest_empmass values ('mayr', '6000.00');
287 insert into rtest_emp select * from rtest_empmass;
288 select ename, who = current_user as "matches user", action, newsal, oldsal from rtest_emplog order by ename, action, newsal;
289 update rtest_empmass set salary = salary + '1000.00';
290 update rtest_emp set salary = rtest_empmass.salary from rtest_empmass where rtest_emp.ename = rtest_empmass.ename;
291 select ename, who = current_user as "matches user", action, newsal, oldsal from rtest_emplog order by ename, action, newsal;
292 delete from rtest_emp using rtest_empmass where rtest_emp.ename = rtest_empmass.ename;
293 select ename, who = current_user as "matches user", action, newsal, oldsal from rtest_emplog order by ename, action, newsal;
296 -- Multiple cascaded qualified instead rule test
298 insert into rtest_t4 values (1, 'Record should go to rtest_t4');
299 insert into rtest_t4 values (2, 'Record should go to rtest_t4');
300 insert into rtest_t4 values (10, 'Record should go to rtest_t5');
301 insert into rtest_t4 values (15, 'Record should go to rtest_t5');
302 insert into rtest_t4 values (19, 'Record should go to rtest_t5 and t7');
303 insert into rtest_t4 values (20, 'Record should go to rtest_t4 and t6');
304 insert into rtest_t4 values (26, 'Record should go to rtest_t4 and t8');
305 insert into rtest_t4 values (28, 'Record should go to rtest_t4 and t8');
306 insert into rtest_t4 values (30, 'Record should go to rtest_t4');
307 insert into rtest_t4 values (40, 'Record should go to rtest_t4');
309 select * from rtest_t4;
310 select * from rtest_t5;
311 select * from rtest_t6;
312 select * from rtest_t7;
313 select * from rtest_t8;
315 delete from rtest_t4;
316 delete from rtest_t5;
317 delete from rtest_t6;
318 delete from rtest_t7;
319 delete from rtest_t8;
321 insert into rtest_t9 values (1, 'Record should go to rtest_t4');
322 insert into rtest_t9 values (2, 'Record should go to rtest_t4');
323 insert into rtest_t9 values (10, 'Record should go to rtest_t5');
324 insert into rtest_t9 values (15, 'Record should go to rtest_t5');
325 insert into rtest_t9 values (19, 'Record should go to rtest_t5 and t7');
326 insert into rtest_t9 values (20, 'Record should go to rtest_t4 and t6');
327 insert into rtest_t9 values (26, 'Record should go to rtest_t4 and t8');
328 insert into rtest_t9 values (28, 'Record should go to rtest_t4 and t8');
329 insert into rtest_t9 values (30, 'Record should go to rtest_t4');
330 insert into rtest_t9 values (40, 'Record should go to rtest_t4');
332 insert into rtest_t4 select * from rtest_t9 where a < 20;
334 select * from rtest_t4;
335 select * from rtest_t5;
336 select * from rtest_t6;
337 select * from rtest_t7;
338 select * from rtest_t8;
340 insert into rtest_t4 select * from rtest_t9 where b ~ 'and t8';
342 select * from rtest_t4;
343 select * from rtest_t5;
344 select * from rtest_t6;
345 select * from rtest_t7;
346 select * from rtest_t8;
348 insert into rtest_t4 select a + 1, b from rtest_t9 where a in (20, 30, 40);
350 select * from rtest_t4;
351 select * from rtest_t5;
352 select * from rtest_t6;
353 select * from rtest_t7;
354 select * from rtest_t8;
357 -- Check that the ordering of rules fired is correct
359 insert into rtest_order1 values (1);
360 select * from rtest_order2;
363 -- Check if instead nothing w/without qualification works
365 insert into rtest_nothn1 values (1, 'want this');
366 insert into rtest_nothn1 values (2, 'want this');
367 insert into rtest_nothn1 values (10, 'don''t want this');
368 insert into rtest_nothn1 values (19, 'don''t want this');
369 insert into rtest_nothn1 values (20, 'want this');
370 insert into rtest_nothn1 values (29, 'want this');
371 insert into rtest_nothn1 values (30, 'don''t want this');
372 insert into rtest_nothn1 values (39, 'don''t want this');
373 insert into rtest_nothn1 values (40, 'want this');
374 insert into rtest_nothn1 values (50, 'want this');
375 insert into rtest_nothn1 values (60, 'want this');
377 select * from rtest_nothn1;
379 insert into rtest_nothn2 values (10, 'too small');
380 insert into rtest_nothn2 values (50, 'too small');
381 insert into rtest_nothn2 values (100, 'OK');
382 insert into rtest_nothn2 values (200, 'OK');
384 select * from rtest_nothn2;
385 select * from rtest_nothn3;
387 delete from rtest_nothn1;
388 delete from rtest_nothn2;
389 delete from rtest_nothn3;
391 insert into rtest_nothn4 values (1, 'want this');
392 insert into rtest_nothn4 values (2, 'want this');
393 insert into rtest_nothn4 values (10, 'don''t want this');
394 insert into rtest_nothn4 values (19, 'don''t want this');
395 insert into rtest_nothn4 values (20, 'want this');
396 insert into rtest_nothn4 values (29, 'want this');
397 insert into rtest_nothn4 values (30, 'don''t want this');
398 insert into rtest_nothn4 values (39, 'don''t want this');
399 insert into rtest_nothn4 values (40, 'want this');
400 insert into rtest_nothn4 values (50, 'want this');
401 insert into rtest_nothn4 values (60, 'want this');
403 insert into rtest_nothn1 select * from rtest_nothn4;
405 select * from rtest_nothn1;
407 delete from rtest_nothn4;
409 insert into rtest_nothn4 values (10, 'too small');
410 insert into rtest_nothn4 values (50, 'too small');
411 insert into rtest_nothn4 values (100, 'OK');
412 insert into rtest_nothn4 values (200, 'OK');
414 insert into rtest_nothn2 select * from rtest_nothn4;
416 select * from rtest_nothn2;
417 select * from rtest_nothn3;
419 create table rtest_view1 (a int4, b text, v bool);
420 create table rtest_view2 (a int4);
421 create table rtest_view3 (a int4, b text);
422 create table rtest_view4 (a int4, b text, c int4);
423 create view rtest_vview1 as select a, b from rtest_view1 X
424 where 0 < (select count(*) from rtest_view2 Y where Y.a = X.a);
425 create view rtest_vview2 as select a, b from rtest_view1 where v;
426 create view rtest_vview3 as select a, b from rtest_vview2 X
427 where 0 < (select count(*) from rtest_view2 Y where Y.a = X.a);
428 create view rtest_vview4 as select X.a, X.b, count(Y.a) as refcount
429 from rtest_view1 X, rtest_view2 Y
432 create function rtest_viewfunc1(int4) returns int4 as
433 'select count(*)::int4 from rtest_view2 where a = $1'
435 create view rtest_vview5 as select a, b, rtest_viewfunc1(a) as refcount
438 insert into rtest_view1 values (1, 'item 1', 't');
439 insert into rtest_view1 values (2, 'item 2', 't');
440 insert into rtest_view1 values (3, 'item 3', 't');
441 insert into rtest_view1 values (4, 'item 4', 'f');
442 insert into rtest_view1 values (5, 'item 5', 't');
443 insert into rtest_view1 values (6, 'item 6', 'f');
444 insert into rtest_view1 values (7, 'item 7', 't');
445 insert into rtest_view1 values (8, 'item 8', 't');
447 insert into rtest_view2 values (2);
448 insert into rtest_view2 values (2);
449 insert into rtest_view2 values (4);
450 insert into rtest_view2 values (5);
451 insert into rtest_view2 values (7);
452 insert into rtest_view2 values (7);
453 insert into rtest_view2 values (7);
454 insert into rtest_view2 values (7);
456 select * from rtest_vview1;
457 select * from rtest_vview2;
458 select * from rtest_vview3;
459 select * from rtest_vview4 order by a, b;
460 select * from rtest_vview5;
462 insert into rtest_view3 select * from rtest_vview1 where a < 7;
463 select * from rtest_view3;
464 delete from rtest_view3;
466 insert into rtest_view3 select * from rtest_vview2 where a != 5 and b !~ '2';
467 select * from rtest_view3;
468 delete from rtest_view3;
470 insert into rtest_view3 select * from rtest_vview3;
471 select * from rtest_view3;
472 delete from rtest_view3;
474 insert into rtest_view4 select * from rtest_vview4 where 3 > refcount;
475 select * from rtest_view4 order by a, b;
476 delete from rtest_view4;
478 insert into rtest_view4 select * from rtest_vview5 where a > 2 and refcount = 0;
479 select * from rtest_view4;
480 delete from rtest_view4;
482 -- Test for computations in views
484 create table rtest_comp (
491 create table rtest_unitfact (
496 create view rtest_vcomp as
497 select X.part, (X.size * Y.factor) as size_in_cm
498 from rtest_comp X, rtest_unitfact Y
499 where X.unit = Y.unit;
502 insert into rtest_unitfact values ('m', 100.0);
503 insert into rtest_unitfact values ('cm', 1.0);
504 insert into rtest_unitfact values ('inch', 2.54);
506 insert into rtest_comp values ('p1', 'm', 5.0);
507 insert into rtest_comp values ('p2', 'm', 3.0);
508 insert into rtest_comp values ('p3', 'cm', 5.0);
509 insert into rtest_comp values ('p4', 'cm', 15.0);
510 insert into rtest_comp values ('p5', 'inch', 7.0);
511 insert into rtest_comp values ('p6', 'inch', 4.4);
513 select * from rtest_vcomp order by part;
515 select * from rtest_vcomp where size_in_cm > 10.0 order by size_in_cm using >;
518 -- In addition run the (slightly modified) queries from the
519 -- programmers manual section on the rule system.
521 CREATE TABLE shoe_data (
522 shoename char(10), -- primary key
523 sh_avail integer, -- available # of pairs
524 slcolor char(10), -- preferred shoelace color
525 slminlen float, -- minimum shoelace length
526 slmaxlen float, -- maximum shoelace length
527 slunit char(8) -- length unit
530 CREATE TABLE shoelace_data (
531 sl_name char(10), -- primary key
532 sl_avail integer, -- available # of pairs
533 sl_color char(10), -- shoelace color
534 sl_len float, -- shoelace length
535 sl_unit char(8) -- length unit
539 un_name char(8), -- the primary key
540 un_fact float -- factor to transform to cm
548 sh.slminlen * un.un_fact AS slminlen_cm,
550 sh.slmaxlen * un.un_fact AS slmaxlen_cm,
552 FROM shoe_data sh, unit un
553 WHERE sh.slunit = un.un_name;
555 CREATE VIEW shoelace AS
561 s.sl_len * u.un_fact AS sl_len_cm
562 FROM shoelace_data s, unit u
563 WHERE s.sl_unit = u.un_name;
565 CREATE VIEW shoe_ready AS
570 int4smaller(rsh.sh_avail, rsl.sl_avail) AS total_avail
571 FROM shoe rsh, shoelace rsl
572 WHERE rsl.sl_color = rsh.slcolor
573 AND rsl.sl_len_cm >= rsh.slminlen_cm
574 AND rsl.sl_len_cm <= rsh.slmaxlen_cm;
576 INSERT INTO unit VALUES ('cm', 1.0);
577 INSERT INTO unit VALUES ('m', 100.0);
578 INSERT INTO unit VALUES ('inch', 2.54);
580 INSERT INTO shoe_data VALUES ('sh1', 2, 'black', 70.0, 90.0, 'cm');
581 INSERT INTO shoe_data VALUES ('sh2', 0, 'black', 30.0, 40.0, 'inch');
582 INSERT INTO shoe_data VALUES ('sh3', 4, 'brown', 50.0, 65.0, 'cm');
583 INSERT INTO shoe_data VALUES ('sh4', 3, 'brown', 40.0, 50.0, 'inch');
585 INSERT INTO shoelace_data VALUES ('sl1', 5, 'black', 80.0, 'cm');
586 INSERT INTO shoelace_data VALUES ('sl2', 6, 'black', 100.0, 'cm');
587 INSERT INTO shoelace_data VALUES ('sl3', 0, 'black', 35.0 , 'inch');
588 INSERT INTO shoelace_data VALUES ('sl4', 8, 'black', 40.0 , 'inch');
589 INSERT INTO shoelace_data VALUES ('sl5', 4, 'brown', 1.0 , 'm');
590 INSERT INTO shoelace_data VALUES ('sl6', 0, 'brown', 0.9 , 'm');
591 INSERT INTO shoelace_data VALUES ('sl7', 7, 'brown', 60 , 'cm');
592 INSERT INTO shoelace_data VALUES ('sl8', 1, 'brown', 40 , 'inch');
595 SELECT * FROM shoelace ORDER BY sl_name;
596 SELECT * FROM shoe_ready WHERE total_avail >= 2 ORDER BY 1;
598 CREATE TABLE shoelace_log (
599 sl_name char(10), -- shoelace changed
600 sl_avail integer, -- new available value
601 log_who name, -- who did it
602 log_when timestamp -- when
605 -- Want "log_who" to be CURRENT_USER,
606 -- but that is non-portable for the regression test
607 -- - thomas 1999-02-21
609 CREATE RULE log_shoelace AS ON UPDATE TO shoelace_data
610 WHERE NEW.sl_avail != OLD.sl_avail
611 DO INSERT INTO shoelace_log VALUES (
618 UPDATE shoelace_data SET sl_avail = 6 WHERE sl_name = 'sl7';
620 SELECT * FROM shoelace_log;
622 CREATE RULE shoelace_ins AS ON INSERT TO shoelace
624 INSERT INTO shoelace_data VALUES (
631 CREATE RULE shoelace_upd AS ON UPDATE TO shoelace
633 UPDATE shoelace_data SET
634 sl_name = NEW.sl_name,
635 sl_avail = NEW.sl_avail,
636 sl_color = NEW.sl_color,
638 sl_unit = NEW.sl_unit
639 WHERE sl_name = OLD.sl_name;
641 CREATE RULE shoelace_del AS ON DELETE TO shoelace
643 DELETE FROM shoelace_data
644 WHERE sl_name = OLD.sl_name;
646 CREATE TABLE shoelace_arrive (
651 CREATE TABLE shoelace_ok (
656 CREATE RULE shoelace_ok_ins AS ON INSERT TO shoelace_ok
659 sl_avail = sl_avail + NEW.ok_quant
660 WHERE sl_name = NEW.ok_name;
662 INSERT INTO shoelace_arrive VALUES ('sl3', 10);
663 INSERT INTO shoelace_arrive VALUES ('sl6', 20);
664 INSERT INTO shoelace_arrive VALUES ('sl8', 20);
666 SELECT * FROM shoelace ORDER BY sl_name;
668 insert into shoelace_ok select * from shoelace_arrive;
670 SELECT * FROM shoelace ORDER BY sl_name;
672 SELECT * FROM shoelace_log ORDER BY sl_name;
674 CREATE VIEW shoelace_obsolete AS
675 SELECT * FROM shoelace WHERE NOT EXISTS
676 (SELECT shoename FROM shoe WHERE slcolor = sl_color);
678 CREATE VIEW shoelace_candelete AS
679 SELECT * FROM shoelace_obsolete WHERE sl_avail = 0;
681 insert into shoelace values ('sl9', 0, 'pink', 35.0, 'inch', 0.0);
682 insert into shoelace values ('sl10', 1000, 'magenta', 40.0, 'inch', 0.0);
683 -- Unsupported (even though a similar updatable view construct is)
684 insert into shoelace values ('sl10', 1000, 'magenta', 40.0, 'inch', 0.0)
685 on conflict do nothing;
687 SELECT * FROM shoelace_obsolete ORDER BY sl_len_cm;
688 SELECT * FROM shoelace_candelete;
690 DELETE FROM shoelace WHERE EXISTS
691 (SELECT * FROM shoelace_candelete
692 WHERE sl_name = shoelace.sl_name);
694 SELECT * FROM shoelace ORDER BY sl_name;
696 SELECT * FROM shoe ORDER BY shoename;
697 SELECT count(*) FROM shoe;
701 -- Simple test of qualified ON INSERT ... this did not work in 7.0 ...
703 create table rules_foo (f1 int);
704 create table rules_foo2 (f1 int);
706 create rule rules_foorule as on insert to rules_foo where f1 < 100
709 insert into rules_foo values(1);
710 insert into rules_foo values(1001);
711 select * from rules_foo;
713 drop rule rules_foorule on rules_foo;
715 -- this should fail because f1 is not exposed for unqualified reference:
716 create rule rules_foorule as on insert to rules_foo where f1 < 100
717 do instead insert into rules_foo2 values (f1);
718 -- this is the correct way:
719 create rule rules_foorule as on insert to rules_foo where f1 < 100
720 do instead insert into rules_foo2 values (new.f1);
722 insert into rules_foo values(2);
723 insert into rules_foo values(100);
725 select * from rules_foo;
726 select * from rules_foo2;
728 drop rule rules_foorule on rules_foo;
729 drop table rules_foo;
730 drop table rules_foo2;
734 -- Test rules containing INSERT ... SELECT, which is a very ugly special
735 -- case as of 7.1. Example is based on bug report from Joel Burton.
737 create table pparent (pid int, txt text);
738 insert into pparent values (1,'parent1');
739 insert into pparent values (2,'parent2');
741 create table cchild (pid int, descrip text);
742 insert into cchild values (1,'descrip1');
745 select pparent.pid, txt, descrip from
746 pparent left join cchild using (pid);
749 on update to vview do instead
751 insert into cchild (pid, descrip)
752 select old.pid, new.descrip where old.descrip isnull;
753 update cchild set descrip = new.descrip where cchild.pid = old.pid;
757 update vview set descrip='test1' where pid=1;
759 update vview set descrip='test2' where pid=2;
761 update vview set descrip='test3' where pid=3;
763 select * from cchild;
765 drop rule rrule on vview;
772 -- Check that ruleutils are working
775 -- temporarily disable fancy output, so view changes create less diff noise
778 SELECT viewname, definition FROM pg_views WHERE schemaname <> 'information_schema' ORDER BY viewname;
780 SELECT tablename, rulename, definition FROM pg_rules
781 ORDER BY tablename, rulename;
783 -- restore normal output mode
787 -- CREATE OR REPLACE RULE
790 CREATE TABLE ruletest_tbl (a int, b int);
791 CREATE TABLE ruletest_tbl2 (a int, b int);
793 CREATE OR REPLACE RULE myrule AS ON INSERT TO ruletest_tbl
794 DO INSTEAD INSERT INTO ruletest_tbl2 VALUES (10, 10);
796 INSERT INTO ruletest_tbl VALUES (99, 99);
798 CREATE OR REPLACE RULE myrule AS ON INSERT TO ruletest_tbl
799 DO INSTEAD INSERT INTO ruletest_tbl2 VALUES (1000, 1000);
801 INSERT INTO ruletest_tbl VALUES (99, 99);
803 SELECT * FROM ruletest_tbl2;
805 -- Check that rewrite rules splitting one INSERT into multiple
806 -- conditional statements does not disable FK checking.
807 create table rule_and_refint_t1 (
811 primary key (id1a, id1b)
814 create table rule_and_refint_t2 (
818 primary key (id2a, id2c)
821 create table rule_and_refint_t3 (
827 primary key (id3a, id3b, id3c),
829 foreign key (id3a, id3b) references rule_and_refint_t1 (id1a, id1b),
830 foreign key (id3a, id3c) references rule_and_refint_t2 (id2a, id2c)
834 insert into rule_and_refint_t1 values (1, 11);
835 insert into rule_and_refint_t1 values (1, 12);
836 insert into rule_and_refint_t1 values (2, 21);
837 insert into rule_and_refint_t1 values (2, 22);
839 insert into rule_and_refint_t2 values (1, 11);
840 insert into rule_and_refint_t2 values (1, 12);
841 insert into rule_and_refint_t2 values (2, 21);
842 insert into rule_and_refint_t2 values (2, 22);
844 insert into rule_and_refint_t3 values (1, 11, 11, 'row1');
845 insert into rule_and_refint_t3 values (1, 11, 12, 'row2');
846 insert into rule_and_refint_t3 values (1, 12, 11, 'row3');
847 insert into rule_and_refint_t3 values (1, 12, 12, 'row4');
848 insert into rule_and_refint_t3 values (1, 11, 13, 'row5');
849 insert into rule_and_refint_t3 values (1, 13, 11, 'row6');
851 insert into rule_and_refint_t3 values (1, 13, 11, 'row6')
852 on conflict do nothing;
853 -- rule not fired, so fk violation
854 insert into rule_and_refint_t3 values (1, 13, 11, 'row6')
855 on conflict (id3a, id3b, id3c) do update
856 set id3b = excluded.id3b;
857 -- rule fired, so unsupported
858 insert into shoelace values ('sl9', 0, 'pink', 35.0, 'inch', 0.0)
859 on conflict (sl_name) do update
860 set sl_avail = excluded.sl_avail;
862 create rule rule_and_refint_t3_ins as on insert to rule_and_refint_t3
863 where (exists (select 1 from rule_and_refint_t3
864 where (((rule_and_refint_t3.id3a = new.id3a)
865 and (rule_and_refint_t3.id3b = new.id3b))
866 and (rule_and_refint_t3.id3c = new.id3c))))
867 do instead update rule_and_refint_t3 set data = new.data
868 where (((rule_and_refint_t3.id3a = new.id3a)
869 and (rule_and_refint_t3.id3b = new.id3b))
870 and (rule_and_refint_t3.id3c = new.id3c));
872 insert into rule_and_refint_t3 values (1, 11, 13, 'row7');
873 insert into rule_and_refint_t3 values (1, 13, 11, 'row8');
876 -- disallow dropping a view's rule (bug #5072)
879 create view rules_fooview as select 'rules_foo'::text;
880 drop rule "_RETURN" on rules_fooview;
881 drop view rules_fooview;
884 -- test conversion of table to view (needed to load some pg_dump files)
887 create table rules_fooview (x int, y text);
888 select xmin, * from rules_fooview;
890 create rule "_RETURN" as on select to rules_fooview do instead
891 select 1 as x, 'aaa'::text as y;
893 select * from rules_fooview;
894 select xmin, * from rules_fooview; -- fail, views don't have such a column
896 select reltoastrelid, relkind, relfrozenxid
897 from pg_class where oid = 'rules_fooview'::regclass;
899 drop view rules_fooview;
901 -- trying to convert a partitioned table to view is not allowed
902 create table rules_fooview (x int, y text) partition by list (x);
903 create rule "_RETURN" as on select to rules_fooview do instead
904 select 1 as x, 'aaa'::text as y;
906 -- nor can one convert a partition to view
907 create table rules_fooview_part partition of rules_fooview for values in (1);
908 create rule "_RETURN" as on select to rules_fooview_part do instead
909 select 1 as x, 'aaa'::text as y;
912 -- check for planner problems with complex inherited UPDATES
915 create table id (id serial primary key, name text);
916 -- currently, must respecify PKEY for each inherited subtable
917 create table test_1 (id integer primary key) inherits (id);
918 create table test_2 (id integer primary key) inherits (id);
919 create table test_3 (id integer primary key) inherits (id);
921 insert into test_1 (name) values ('Test 1');
922 insert into test_1 (name) values ('Test 2');
923 insert into test_2 (name) values ('Test 3');
924 insert into test_2 (name) values ('Test 4');
925 insert into test_3 (name) values ('Test 5');
926 insert into test_3 (name) values ('Test 6');
928 create view id_ordered as select * from id order by id;
930 create rule update_id_ordered as on update to id_ordered
931 do instead update id set name = new.name where id = old.id;
933 select * from id_ordered;
934 update id_ordered set name = 'update 2' where id = 2;
935 update id_ordered set name = 'update 4' where id = 4;
936 update id_ordered set name = 'update 5' where id = 5;
937 select * from id_ordered;
939 \set VERBOSITY terse \\ -- suppress cascade details
940 drop table id cascade;
941 \set VERBOSITY default
944 -- check corner case where an entirely-dummy subplan is created by
945 -- constraint exclusion
948 create temp table t1 (a integer primary key);
950 create temp table t1_1 (check (a >= 0 and a < 10)) inherits (t1);
951 create temp table t1_2 (check (a >= 10 and a < 20)) inherits (t1);
953 create rule t1_ins_1 as on insert to t1
954 where new.a >= 0 and new.a < 10
956 insert into t1_1 values (new.a);
957 create rule t1_ins_2 as on insert to t1
958 where new.a >= 10 and new.a < 20
960 insert into t1_2 values (new.a);
962 create rule t1_upd_1 as on update to t1
963 where old.a >= 0 and old.a < 10
965 update t1_1 set a = new.a where a = old.a;
966 create rule t1_upd_2 as on update to t1
967 where old.a >= 10 and old.a < 20
969 update t1_2 set a = new.a where a = old.a;
971 set constraint_exclusion = on;
973 insert into t1 select * from generate_series(5,19,1) g;
974 update t1 set a = 4 where a = 5;
976 select * from only t1;
977 select * from only t1_1;
978 select * from only t1_2;
980 reset constraint_exclusion;
982 -- test various flavors of pg_get_viewdef()
984 select pg_get_viewdef('shoe'::regclass) as unpretty;
985 select pg_get_viewdef('shoe'::regclass,true) as pretty;
986 select pg_get_viewdef('shoe'::regclass,0) as prettier;
989 -- check multi-row VALUES in rules
992 create table rules_src(f1 int, f2 int);
993 create table rules_log(f1 int, f2 int, tag text);
994 insert into rules_src values(1,2), (11,12);
995 create rule r1 as on update to rules_src do also
996 insert into rules_log values(old.*, 'old'), (new.*, 'new');
997 update rules_src set f2 = f2 + 1;
998 update rules_src set f2 = f2 * 10;
999 select * from rules_src;
1000 select * from rules_log;
1001 create rule r2 as on update to rules_src do also
1002 values(old.*, 'old'), (new.*, 'new');
1003 update rules_src set f2 = f2 / 10;
1004 select * from rules_src;
1005 select * from rules_log;
1006 create rule r3 as on delete to rules_src do notify rules_src_deletion;
1010 -- Ensure an aliased target relation for insert is correctly deparsed.
1012 create rule r4 as on insert to rules_src do instead insert into rules_log AS trgt SELECT NEW.* RETURNING trgt.f1, trgt.f2;
1013 create rule r5 as on update to rules_src do instead UPDATE rules_log AS trgt SET tag = 'updated' WHERE trgt.f1 = new.f1;
1017 -- check alter rename rule
1019 CREATE TABLE rule_t1 (a INT);
1020 CREATE VIEW rule_v1 AS SELECT * FROM rule_t1;
1022 CREATE RULE InsertRule AS
1023 ON INSERT TO rule_v1
1025 INSERT INTO rule_t1 VALUES(new.a);
1027 ALTER RULE InsertRule ON rule_v1 RENAME to NewInsertRule;
1029 INSERT INTO rule_v1 VALUES(1);
1030 SELECT * FROM rule_v1;
1035 -- error conditions for alter rename rule
1037 ALTER RULE InsertRule ON rule_v1 RENAME TO NewInsertRule; -- doesn't exist
1038 ALTER RULE NewInsertRule ON rule_v1 RENAME TO "_RETURN"; -- already exists
1039 ALTER RULE "_RETURN" ON rule_v1 RENAME TO abc; -- ON SELECT rule cannot be renamed
1045 -- check display of VALUES in view definitions
1047 create view rule_v1 as values(1,2);
1050 create view rule_v1(x) as values(1,2);
1053 create view rule_v1(x) as select * from (values(1,2)) v;
1056 create view rule_v1(x) as select * from (values(1,2)) v(q,w);
1061 -- Check DO INSTEAD rules with ON CONFLICT
1064 hat_name char(10) primary key,
1065 hat_color char(10) -- hat color
1068 CREATE TABLE hat_data (
1070 hat_color char(10) -- hat color
1072 create unique index hat_data_unique_idx
1073 on hat_data (hat_name COLLATE "C" bpchar_pattern_ops);
1075 -- DO NOTHING with ON CONFLICT
1076 CREATE RULE hat_nosert AS ON INSERT TO hats
1078 INSERT INTO hat_data VALUES (
1081 ON CONFLICT (hat_name COLLATE "C" bpchar_pattern_ops) WHERE hat_color = 'green'
1084 SELECT definition FROM pg_rules WHERE tablename = 'hats' ORDER BY rulename;
1086 -- Works (projects row)
1087 INSERT INTO hats VALUES ('h7', 'black') RETURNING *;
1088 -- Works (does nothing)
1089 INSERT INTO hats VALUES ('h7', 'black') RETURNING *;
1090 SELECT tablename, rulename, definition FROM pg_rules
1091 WHERE tablename = 'hats';
1092 DROP RULE hat_nosert ON hats;
1094 -- DO NOTHING without ON CONFLICT
1095 CREATE RULE hat_nosert_all AS ON INSERT TO hats
1097 INSERT INTO hat_data VALUES (
1103 SELECT definition FROM pg_rules WHERE tablename = 'hats' ORDER BY rulename;
1104 DROP RULE hat_nosert_all ON hats;
1106 -- Works (does nothing)
1107 INSERT INTO hats VALUES ('h7', 'black') RETURNING *;
1109 -- DO UPDATE with a WHERE clause
1110 CREATE RULE hat_upsert AS ON INSERT TO hats
1112 INSERT INTO hat_data VALUES (
1115 ON CONFLICT (hat_name)
1117 SET hat_name = hat_data.hat_name, hat_color = excluded.hat_color
1118 WHERE excluded.hat_color <> 'forbidden' AND hat_data.* != excluded.*
1120 SELECT definition FROM pg_rules WHERE tablename = 'hats' ORDER BY rulename;
1122 -- Works (does upsert)
1123 INSERT INTO hats VALUES ('h8', 'black') RETURNING *;
1124 SELECT * FROM hat_data WHERE hat_name = 'h8';
1125 INSERT INTO hats VALUES ('h8', 'white') RETURNING *;
1126 SELECT * FROM hat_data WHERE hat_name = 'h8';
1127 INSERT INTO hats VALUES ('h8', 'forbidden') RETURNING *;
1128 SELECT * FROM hat_data WHERE hat_name = 'h8';
1129 SELECT tablename, rulename, definition FROM pg_rules
1130 WHERE tablename = 'hats';
1131 -- ensure explain works for on insert conflict rules
1132 explain (costs off) INSERT INTO hats VALUES ('h8', 'forbidden') RETURNING *;
1134 -- ensure upserting into a rule, with a CTE (different offsets!) works
1135 WITH data(hat_name, hat_color) AS (
1136 VALUES ('h8', 'green'),
1143 EXPLAIN (costs off) WITH data(hat_name, hat_color) AS (
1144 VALUES ('h8', 'green'),
1151 SELECT * FROM hat_data WHERE hat_name IN ('h8', 'h9', 'h7') ORDER BY hat_name;
1153 DROP RULE hat_upsert ON hats;
1156 drop table hat_data;
1158 -- test for pg_get_functiondef properly regurgitating SET parameters
1159 -- Note that the function is kept around to stress pg_dump.
1160 CREATE FUNCTION func_with_set_params() RETURNS integer
1163 SET search_path TO PG_CATALOG
1164 SET extra_float_digits TO 2
1165 SET work_mem TO '4MB'
1166 SET datestyle to iso, mdy
1167 SET local_preload_libraries TO "Mixed/Case", 'c:/"a"/path'
1169 SELECT pg_get_functiondef('func_with_set_params()'::regprocedure);
1171 -- tests for pg_get_*def with invalid objects
1172 SELECT pg_get_constraintdef(0);
1173 SELECT pg_get_functiondef(0);
1174 SELECT pg_get_indexdef(0);
1175 SELECT pg_get_ruledef(0);
1176 SELECT pg_get_statisticsobjdef(0);
1177 SELECT pg_get_triggerdef(0);
1178 SELECT pg_get_viewdef(0);
1179 SELECT pg_get_function_arguments(0);
1180 SELECT pg_get_function_identity_arguments(0);
1181 SELECT pg_get_function_result(0);
1182 SELECT pg_get_function_arg_default(0, 0);
1183 SELECT pg_get_function_arg_default('pg_class'::regclass, 0);
1184 SELECT pg_get_partkeydef(0);
1186 -- test rename for a rule defined on a partitioned table
1187 CREATE TABLE rules_parted_table (a int) PARTITION BY LIST (a);
1188 CREATE TABLE rules_parted_table_1 PARTITION OF rules_parted_table FOR VALUES IN (1);
1189 CREATE RULE rules_parted_table_insert AS ON INSERT to rules_parted_table
1190 DO INSTEAD INSERT INTO rules_parted_table_1 VALUES (NEW.*);
1191 ALTER RULE rules_parted_table_insert ON rules_parted_table RENAME TO rules_parted_table_insert_redirect;
1192 DROP TABLE rules_parted_table;
1195 -- Test enabling/disabling
1197 CREATE TABLE ruletest1 (a int);
1198 CREATE TABLE ruletest2 (b int);
1200 CREATE RULE rule1 AS ON INSERT TO ruletest1
1201 DO INSTEAD INSERT INTO ruletest2 VALUES (NEW.*);
1203 INSERT INTO ruletest1 VALUES (1);
1204 ALTER TABLE ruletest1 DISABLE RULE rule1;
1205 INSERT INTO ruletest1 VALUES (2);
1206 ALTER TABLE ruletest1 ENABLE RULE rule1;
1207 SET session_replication_role = replica;
1208 INSERT INTO ruletest1 VALUES (3);
1209 ALTER TABLE ruletest1 ENABLE REPLICA RULE rule1;
1210 INSERT INTO ruletest1 VALUES (4);
1211 RESET session_replication_role;
1212 INSERT INTO ruletest1 VALUES (5);
1214 SELECT * FROM ruletest1;
1215 SELECT * FROM ruletest2;
1217 DROP TABLE ruletest1;
1218 DROP TABLE ruletest2;