3 -- From Jan's original setup_ruletest.sql and run_ruletest.sql
7 -- Tables and rules for the view test
9 create table rtest_t1 (a int4, b int4);
10 create table rtest_t2 (a int4, b int4);
11 create table rtest_t3 (a int4, b int4);
12 create view rtest_v1 as select * from rtest_t1;
13 create rule rtest_v1_ins as on insert to rtest_v1 do instead
14 insert into rtest_t1 values (new.a, new.b);
15 create rule rtest_v1_upd as on update to rtest_v1 do instead
16 update rtest_t1 set a = new.a, b = new.b
18 create rule rtest_v1_del as on delete to rtest_v1 do instead
19 delete from rtest_t1 where a = old.a;
21 -- Tables and rules for the constraint update/delete test
24 -- Now that we have multiple action rule support, we check
25 -- both possible syntaxes to define them (The last action
26 -- can but must not have a semicolon at the end).
28 create table rtest_system (sysname text, sysdesc text);
29 create table rtest_interface (sysname text, ifname text);
30 create table rtest_person (pname text, pdesc text);
31 create table rtest_admin (pname text, sysname text);
32 create rule rtest_sys_upd as on update to rtest_system do (
33 update rtest_interface set sysname = new.sysname
34 where sysname = old.sysname;
35 update rtest_admin set sysname = new.sysname
36 where sysname = old.sysname
38 create rule rtest_sys_del as on delete to rtest_system do (
39 delete from rtest_interface where sysname = old.sysname;
40 delete from rtest_admin where sysname = old.sysname;
42 create rule rtest_pers_upd as on update to rtest_person do
43 update rtest_admin set pname = new.pname where pname = old.pname;
44 create rule rtest_pers_del as on delete to rtest_person do
45 delete from rtest_admin where pname = old.pname;
47 -- Tables and rules for the logging test
49 create table rtest_emp (ename char(20), salary money);
50 create table rtest_emplog (ename char(20), who name, action char(10), newsal money, oldsal money);
51 create table rtest_empmass (ename char(20), salary money);
52 create rule rtest_emp_ins as on insert to rtest_emp do
53 insert into rtest_emplog values (new.ename, current_user,
54 'hired', new.salary, '0.00');
55 create rule rtest_emp_upd as on update to rtest_emp where new.salary != old.salary do
56 insert into rtest_emplog values (new.ename, current_user,
57 'honored', new.salary, old.salary);
58 create rule rtest_emp_del as on delete to rtest_emp do
59 insert into rtest_emplog values (old.ename, current_user,
60 'fired', '0.00', old.salary);
62 -- Tables and rules for the multiple cascaded qualified instead
65 create table rtest_t4 (a int4, b text);
66 create table rtest_t5 (a int4, b text);
67 create table rtest_t6 (a int4, b text);
68 create table rtest_t7 (a int4, b text);
69 create table rtest_t8 (a int4, b text);
70 create table rtest_t9 (a int4, b text);
71 create rule rtest_t4_ins1 as on insert to rtest_t4
72 where new.a >= 10 and new.a < 20 do instead
73 insert into rtest_t5 values (new.a, new.b);
74 create rule rtest_t4_ins2 as on insert to rtest_t4
75 where new.a >= 20 and new.a < 30 do
76 insert into rtest_t6 values (new.a, new.b);
77 create rule rtest_t5_ins as on insert to rtest_t5
79 insert into rtest_t7 values (new.a, new.b);
80 create rule rtest_t6_ins as on insert to rtest_t6
81 where new.a > 25 do instead
82 insert into rtest_t8 values (new.a, new.b);
84 -- Tables and rules for the rule fire order test
86 create table rtest_order1 (a int4);
87 create table rtest_order2 (a int4, b int4, c text);
88 create sequence rtest_seq;
89 create rule rtest_order_r3 as on insert to rtest_order1 do instead
90 insert into rtest_order2 values (new.a, nextval('rtest_seq'),
91 'rule 3 - this should run 3rd or 4th');
92 create rule rtest_order_r4 as on insert to rtest_order1
93 where a < 100 do instead
94 insert into rtest_order2 values (new.a, nextval('rtest_seq'),
95 'rule 4 - this should run 2nd');
96 create rule rtest_order_r2 as on insert to rtest_order1 do
97 insert into rtest_order2 values (new.a, nextval('rtest_seq'),
98 'rule 2 - this should run 1st');
99 create rule rtest_order_r1 as on insert to rtest_order1 do instead
100 insert into rtest_order2 values (new.a, nextval('rtest_seq'),
101 'rule 1 - this should run 3rd or 4th');
103 -- Tables and rules for the instead nothing test
105 create table rtest_nothn1 (a int4, b text);
106 create table rtest_nothn2 (a int4, b text);
107 create table rtest_nothn3 (a int4, b text);
108 create table rtest_nothn4 (a int4, b text);
109 create rule rtest_nothn_r1 as on insert to rtest_nothn1
110 where new.a >= 10 and new.a < 20 do instead (select 1);
111 create rule rtest_nothn_r2 as on insert to rtest_nothn1
112 where new.a >= 30 and new.a < 40 do instead nothing;
113 create rule rtest_nothn_r3 as on insert to rtest_nothn2
114 where new.a >= 100 do instead
115 insert into rtest_nothn3 values (new.a, new.b);
116 create rule rtest_nothn_r4 as on insert to rtest_nothn2
119 -- Tests on a view that is select * of a table
120 -- and has insert/update/delete instead rules to
121 -- behave close like the real table.
124 -- We need test date later
126 insert into rtest_t2 values (1, 21);
127 insert into rtest_t2 values (2, 22);
128 insert into rtest_t2 values (3, 23);
129 insert into rtest_t3 values (1, 31);
130 insert into rtest_t3 values (2, 32);
131 insert into rtest_t3 values (3, 33);
132 insert into rtest_t3 values (4, 34);
133 insert into rtest_t3 values (5, 35);
135 insert into rtest_v1 values (1, 11);
136 insert into rtest_v1 values (2, 12);
137 select * from rtest_v1;
144 -- delete with constant expression
145 delete from rtest_v1 where a = 1;
146 select * from rtest_v1;
152 insert into rtest_v1 values (1, 11);
153 delete from rtest_v1 where b = 12;
154 select * from rtest_v1;
160 insert into rtest_v1 values (2, 12);
161 insert into rtest_v1 values (2, 13);
162 select * from rtest_v1;
170 ** Remember the delete rule on rtest_v1: It says
171 ** DO INSTEAD DELETE FROM rtest_t1 WHERE a = old.a
172 ** So this time both rows with a = 2 must get deleted
174 ** Remember the delete rule on rtest_v1: It says
175 ** DO INSTEAD DELETE FROM rtest_t1 WHERE a = old.a
176 ** So this time both rows with a = 2 must get deleted
178 delete from rtest_v1 where b = 12;
179 select * from rtest_v1;
185 delete from rtest_v1;
187 insert into rtest_v1 select * from rtest_t2;
188 select * from rtest_v1;
196 delete from rtest_v1;
197 -- same with swapped targetlist
198 insert into rtest_v1 (b, a) select b, a from rtest_t2;
199 select * from rtest_v1;
207 -- now with only one target attribute
208 insert into rtest_v1 (a) select a from rtest_t3;
209 select * from rtest_v1;
222 select * from rtest_v1 where b isnull;
232 -- let attribute a differ (must be done on rtest_t1 - see above)
233 update rtest_t1 set a = a + 10 where b isnull;
234 delete from rtest_v1 where b isnull;
235 select * from rtest_v1;
243 -- now updates with constant expression
244 update rtest_v1 set b = 42 where a = 2;
245 select * from rtest_v1;
253 update rtest_v1 set b = 99 where b = 42;
254 select * from rtest_v1;
262 update rtest_v1 set b = 88 where b < 50;
263 select * from rtest_v1;
271 delete from rtest_v1;
272 insert into rtest_v1 select rtest_t2.a, rtest_t3.b where rtest_t2.a = rtest_t3.a;
273 select * from rtest_v1;
281 -- updates in a mergejoin
282 update rtest_v1 set b = rtest_t2.b where a = rtest_t2.a;
283 select * from rtest_v1;
291 insert into rtest_v1 select * from rtest_t3;
292 select * from rtest_v1;
305 update rtest_t1 set a = a + 10 where b > 30;
306 select * from rtest_v1;
319 update rtest_v1 set a = rtest_t3.a + 20 where b = rtest_t3.b;
320 select * from rtest_v1;
334 -- Test for constraint updates/deletes
336 insert into rtest_system values ('orion', 'Linux Jan Wieck');
337 insert into rtest_system values ('notjw', 'WinNT Jan Wieck (notebook)');
338 insert into rtest_system values ('neptun', 'Fileserver');
339 insert into rtest_interface values ('orion', 'eth0');
340 insert into rtest_interface values ('orion', 'eth1');
341 insert into rtest_interface values ('notjw', 'eth0');
342 insert into rtest_interface values ('neptun', 'eth0');
343 insert into rtest_person values ('jw', 'Jan Wieck');
344 insert into rtest_person values ('bm', 'Bruce Momjian');
345 insert into rtest_admin values ('jw', 'orion');
346 insert into rtest_admin values ('jw', 'notjw');
347 insert into rtest_admin values ('bm', 'neptun');
348 update rtest_system set sysname = 'pluto' where sysname = 'neptun';
349 select * from rtest_interface;
358 select * from rtest_admin;
366 update rtest_person set pname = 'jwieck' where pdesc = 'Jan Wieck';
367 -- Note: use ORDER BY here to ensure consistent output across all systems.
368 -- The above UPDATE affects two rows with equal keys, so they could be
369 -- updated in either order depending on the whim of the local qsort().
370 select * from rtest_admin order by pname, sysname;
378 delete from rtest_system where sysname = 'orion';
379 select * from rtest_interface;
386 select * from rtest_admin;
394 -- Rule qualification test
396 insert into rtest_emp values ('wiech', '5000.00');
397 insert into rtest_emp values ('gates', '80000.00');
398 update rtest_emp set ename = 'wiecx' where ename = 'wiech';
399 update rtest_emp set ename = 'wieck', salary = '6000.00' where ename = 'wiecx';
400 update rtest_emp set salary = '7000.00' where ename = 'wieck';
401 delete from rtest_emp where ename = 'gates';
402 select ename, who = current_user as "matches user", action, newsal, oldsal from rtest_emplog order by ename, action, newsal;
403 ename | matches user | action | newsal | oldsal
404 ----------------------+--------------+------------+------------+------------
405 gates | t | fired | $0.00 | $80,000.00
406 gates | t | hired | $80,000.00 | $0.00
407 wiech | t | hired | $5,000.00 | $0.00
408 wieck | t | honored | $6,000.00 | $5,000.00
409 wieck | t | honored | $7,000.00 | $6,000.00
412 insert into rtest_empmass values ('meyer', '4000.00');
413 insert into rtest_empmass values ('maier', '5000.00');
414 insert into rtest_empmass values ('mayr', '6000.00');
415 insert into rtest_emp select * from rtest_empmass;
416 select ename, who = current_user as "matches user", action, newsal, oldsal from rtest_emplog order by ename, action, newsal;
417 ename | matches user | action | newsal | oldsal
418 ----------------------+--------------+------------+------------+------------
419 gates | t | fired | $0.00 | $80,000.00
420 gates | t | hired | $80,000.00 | $0.00
421 maier | t | hired | $5,000.00 | $0.00
422 mayr | t | hired | $6,000.00 | $0.00
423 meyer | t | hired | $4,000.00 | $0.00
424 wiech | t | hired | $5,000.00 | $0.00
425 wieck | t | honored | $6,000.00 | $5,000.00
426 wieck | t | honored | $7,000.00 | $6,000.00
429 update rtest_empmass set salary = salary + '1000.00';
430 update rtest_emp set salary = rtest_empmass.salary where ename = rtest_empmass.ename;
431 select ename, who = current_user as "matches user", action, newsal, oldsal from rtest_emplog order by ename, action, newsal;
432 ename | matches user | action | newsal | oldsal
433 ----------------------+--------------+------------+------------+------------
434 gates | t | fired | $0.00 | $80,000.00
435 gates | t | hired | $80,000.00 | $0.00
436 maier | t | hired | $5,000.00 | $0.00
437 maier | t | honored | $6,000.00 | $5,000.00
438 mayr | t | hired | $6,000.00 | $0.00
439 mayr | t | honored | $7,000.00 | $6,000.00
440 meyer | t | hired | $4,000.00 | $0.00
441 meyer | t | honored | $5,000.00 | $4,000.00
442 wiech | t | hired | $5,000.00 | $0.00
443 wieck | t | honored | $6,000.00 | $5,000.00
444 wieck | t | honored | $7,000.00 | $6,000.00
447 delete from rtest_emp where ename = rtest_empmass.ename;
448 select ename, who = current_user as "matches user", action, newsal, oldsal from rtest_emplog order by ename, action, newsal;
449 ename | matches user | action | newsal | oldsal
450 ----------------------+--------------+------------+------------+------------
451 gates | t | fired | $0.00 | $80,000.00
452 gates | t | hired | $80,000.00 | $0.00
453 maier | t | fired | $0.00 | $6,000.00
454 maier | t | hired | $5,000.00 | $0.00
455 maier | t | honored | $6,000.00 | $5,000.00
456 mayr | t | fired | $0.00 | $7,000.00
457 mayr | t | hired | $6,000.00 | $0.00
458 mayr | t | honored | $7,000.00 | $6,000.00
459 meyer | t | fired | $0.00 | $5,000.00
460 meyer | t | hired | $4,000.00 | $0.00
461 meyer | t | honored | $5,000.00 | $4,000.00
462 wiech | t | hired | $5,000.00 | $0.00
463 wieck | t | honored | $6,000.00 | $5,000.00
464 wieck | t | honored | $7,000.00 | $6,000.00
468 -- Multiple cascaded qualified instead rule test
470 insert into rtest_t4 values (1, 'Record should go to rtest_t4');
471 insert into rtest_t4 values (2, 'Record should go to rtest_t4');
472 insert into rtest_t4 values (10, 'Record should go to rtest_t5');
473 insert into rtest_t4 values (15, 'Record should go to rtest_t5');
474 insert into rtest_t4 values (19, 'Record should go to rtest_t5 and t7');
475 insert into rtest_t4 values (20, 'Record should go to rtest_t4 and t6');
476 insert into rtest_t4 values (26, 'Record should go to rtest_t4 and t8');
477 insert into rtest_t4 values (28, 'Record should go to rtest_t4 and t8');
478 insert into rtest_t4 values (30, 'Record should go to rtest_t4');
479 insert into rtest_t4 values (40, 'Record should go to rtest_t4');
480 select * from rtest_t4;
482 ----+-------------------------------------
483 1 | Record should go to rtest_t4
484 2 | Record should go to rtest_t4
485 20 | Record should go to rtest_t4 and t6
486 26 | Record should go to rtest_t4 and t8
487 28 | Record should go to rtest_t4 and t8
488 30 | Record should go to rtest_t4
489 40 | Record should go to rtest_t4
492 select * from rtest_t5;
494 ----+-------------------------------------
495 10 | Record should go to rtest_t5
496 15 | Record should go to rtest_t5
497 19 | Record should go to rtest_t5 and t7
500 select * from rtest_t6;
502 ----+-------------------------------------
503 20 | Record should go to rtest_t4 and t6
506 select * from rtest_t7;
508 ----+-------------------------------------
509 19 | Record should go to rtest_t5 and t7
512 select * from rtest_t8;
514 ----+-------------------------------------
515 26 | Record should go to rtest_t4 and t8
516 28 | Record should go to rtest_t4 and t8
519 delete from rtest_t4;
520 delete from rtest_t5;
521 delete from rtest_t6;
522 delete from rtest_t7;
523 delete from rtest_t8;
524 insert into rtest_t9 values (1, 'Record should go to rtest_t4');
525 insert into rtest_t9 values (2, 'Record should go to rtest_t4');
526 insert into rtest_t9 values (10, 'Record should go to rtest_t5');
527 insert into rtest_t9 values (15, 'Record should go to rtest_t5');
528 insert into rtest_t9 values (19, 'Record should go to rtest_t5 and t7');
529 insert into rtest_t9 values (20, 'Record should go to rtest_t4 and t6');
530 insert into rtest_t9 values (26, 'Record should go to rtest_t4 and t8');
531 insert into rtest_t9 values (28, 'Record should go to rtest_t4 and t8');
532 insert into rtest_t9 values (30, 'Record should go to rtest_t4');
533 insert into rtest_t9 values (40, 'Record should go to rtest_t4');
534 insert into rtest_t4 select * from rtest_t9 where a < 20;
535 select * from rtest_t4;
537 ---+------------------------------
538 1 | Record should go to rtest_t4
539 2 | Record should go to rtest_t4
542 select * from rtest_t5;
544 ----+-------------------------------------
545 10 | Record should go to rtest_t5
546 15 | Record should go to rtest_t5
547 19 | Record should go to rtest_t5 and t7
550 select * from rtest_t6;
555 select * from rtest_t7;
557 ----+-------------------------------------
558 19 | Record should go to rtest_t5 and t7
561 select * from rtest_t8;
566 insert into rtest_t4 select * from rtest_t9 where b ~ 'and t8';
567 select * from rtest_t4;
569 ----+-------------------------------------
570 1 | Record should go to rtest_t4
571 2 | Record should go to rtest_t4
572 26 | Record should go to rtest_t4 and t8
573 28 | Record should go to rtest_t4 and t8
576 select * from rtest_t5;
578 ----+-------------------------------------
579 10 | Record should go to rtest_t5
580 15 | Record should go to rtest_t5
581 19 | Record should go to rtest_t5 and t7
584 select * from rtest_t6;
589 select * from rtest_t7;
591 ----+-------------------------------------
592 19 | Record should go to rtest_t5 and t7
595 select * from rtest_t8;
597 ----+-------------------------------------
598 26 | Record should go to rtest_t4 and t8
599 28 | Record should go to rtest_t4 and t8
602 insert into rtest_t4 select a + 1, b from rtest_t9 where a in (20, 30, 40);
603 select * from rtest_t4;
605 ----+-------------------------------------
606 1 | Record should go to rtest_t4
607 2 | Record should go to rtest_t4
608 26 | Record should go to rtest_t4 and t8
609 28 | Record should go to rtest_t4 and t8
610 21 | Record should go to rtest_t4 and t6
611 31 | Record should go to rtest_t4
612 41 | Record should go to rtest_t4
615 select * from rtest_t5;
617 ----+-------------------------------------
618 10 | Record should go to rtest_t5
619 15 | Record should go to rtest_t5
620 19 | Record should go to rtest_t5 and t7
623 select * from rtest_t6;
625 ----+-------------------------------------
626 21 | Record should go to rtest_t4 and t6
629 select * from rtest_t7;
631 ----+-------------------------------------
632 19 | Record should go to rtest_t5 and t7
635 select * from rtest_t8;
637 ----+-------------------------------------
638 26 | Record should go to rtest_t4 and t8
639 28 | Record should go to rtest_t4 and t8
643 -- Check that the ordering of rules fired is correct
645 insert into rtest_order1 values (1);
646 select * from rtest_order2;
648 ---+---+-------------------------------------
649 1 | 1 | rule 2 - this should run 1st
650 1 | 2 | rule 4 - this should run 2nd
651 1 | 3 | rule 3 - this should run 3rd or 4th
652 1 | 4 | rule 1 - this should run 3rd or 4th
656 -- Check if instead nothing w/without qualification works
658 insert into rtest_nothn1 values (1, 'want this');
659 insert into rtest_nothn1 values (2, 'want this');
660 insert into rtest_nothn1 values (10, 'don''t want this');
661 insert into rtest_nothn1 values (19, 'don''t want this');
662 insert into rtest_nothn1 values (20, 'want this');
663 insert into rtest_nothn1 values (29, 'want this');
664 insert into rtest_nothn1 values (30, 'don''t want this');
665 insert into rtest_nothn1 values (39, 'don''t want this');
666 insert into rtest_nothn1 values (40, 'want this');
667 insert into rtest_nothn1 values (50, 'want this');
668 insert into rtest_nothn1 values (60, 'want this');
669 select * from rtest_nothn1;
681 insert into rtest_nothn2 values (10, 'too small');
682 insert into rtest_nothn2 values (50, 'too small');
683 insert into rtest_nothn2 values (100, 'OK');
684 insert into rtest_nothn2 values (200, 'OK');
685 select * from rtest_nothn2;
690 select * from rtest_nothn3;
697 delete from rtest_nothn1;
698 delete from rtest_nothn2;
699 delete from rtest_nothn3;
700 insert into rtest_nothn4 values (1, 'want this');
701 insert into rtest_nothn4 values (2, 'want this');
702 insert into rtest_nothn4 values (10, 'don''t want this');
703 insert into rtest_nothn4 values (19, 'don''t want this');
704 insert into rtest_nothn4 values (20, 'want this');
705 insert into rtest_nothn4 values (29, 'want this');
706 insert into rtest_nothn4 values (30, 'don''t want this');
707 insert into rtest_nothn4 values (39, 'don''t want this');
708 insert into rtest_nothn4 values (40, 'want this');
709 insert into rtest_nothn4 values (50, 'want this');
710 insert into rtest_nothn4 values (60, 'want this');
711 insert into rtest_nothn1 select * from rtest_nothn4;
712 select * from rtest_nothn1;
724 delete from rtest_nothn4;
725 insert into rtest_nothn4 values (10, 'too small');
726 insert into rtest_nothn4 values (50, 'too small');
727 insert into rtest_nothn4 values (100, 'OK');
728 insert into rtest_nothn4 values (200, 'OK');
729 insert into rtest_nothn2 select * from rtest_nothn4;
730 select * from rtest_nothn2;
735 select * from rtest_nothn3;
742 create table rtest_view1 (a int4, b text, v bool);
743 create table rtest_view2 (a int4);
744 create table rtest_view3 (a int4, b text);
745 create table rtest_view4 (a int4, b text, c int4);
746 create view rtest_vview1 as select a, b from rtest_view1 X
747 where 0 < (select count(*) from rtest_view2 Y where Y.a = X.a);
748 create view rtest_vview2 as select a, b from rtest_view1 where v;
749 create view rtest_vview3 as select a, b from rtest_vview2 X
750 where 0 < (select count(*) from rtest_view2 Y where Y.a = X.a);
751 create view rtest_vview4 as select X.a, X.b, count(Y.a) as refcount
752 from rtest_view1 X, rtest_view2 Y
755 create function rtest_viewfunc1(int4) returns int4 as
756 'select count(*) from rtest_view2 where a = $1'
758 create view rtest_vview5 as select a, b, rtest_viewfunc1(a) as refcount
760 insert into rtest_view1 values (1, 'item 1', 't');
761 insert into rtest_view1 values (2, 'item 2', 't');
762 insert into rtest_view1 values (3, 'item 3', 't');
763 insert into rtest_view1 values (4, 'item 4', 'f');
764 insert into rtest_view1 values (5, 'item 5', 't');
765 insert into rtest_view1 values (6, 'item 6', 'f');
766 insert into rtest_view1 values (7, 'item 7', 't');
767 insert into rtest_view1 values (8, 'item 8', 't');
768 insert into rtest_view2 values (2);
769 insert into rtest_view2 values (2);
770 insert into rtest_view2 values (4);
771 insert into rtest_view2 values (5);
772 insert into rtest_view2 values (7);
773 insert into rtest_view2 values (7);
774 insert into rtest_view2 values (7);
775 insert into rtest_view2 values (7);
776 select * from rtest_vview1;
785 select * from rtest_vview2;
796 select * from rtest_vview3;
804 select * from rtest_vview4;
806 ---+--------+----------
813 select * from rtest_vview5;
815 ---+--------+----------
826 insert into rtest_view3 select * from rtest_vview1 where a < 7;
827 select * from rtest_view3;
835 delete from rtest_view3;
836 insert into rtest_view3 select * from rtest_vview2 where a != 5 and b !~ '2';
837 select * from rtest_view3;
846 delete from rtest_view3;
847 insert into rtest_view3 select * from rtest_vview3;
848 select * from rtest_view3;
856 delete from rtest_view3;
857 insert into rtest_view4 select * from rtest_vview4 where 3 > refcount;
858 select * from rtest_view4;
866 delete from rtest_view4;
867 insert into rtest_view4 select * from rtest_vview5 where a > 2 and refcount = 0;
868 select * from rtest_view4;
876 delete from rtest_view4;
878 -- Test for computations in views
880 create table rtest_comp (
885 create table rtest_unitfact (
889 create view rtest_vcomp as
890 select X.part, (X.size * Y.factor) as size_in_cm
891 from rtest_comp X, rtest_unitfact Y
892 where X.unit = Y.unit;
893 insert into rtest_unitfact values ('m', 100.0);
894 insert into rtest_unitfact values ('cm', 1.0);
895 insert into rtest_unitfact values ('inch', 2.54);
896 insert into rtest_comp values ('p1', 'm', 5.0);
897 insert into rtest_comp values ('p2', 'm', 3.0);
898 insert into rtest_comp values ('p3', 'cm', 5.0);
899 insert into rtest_comp values ('p4', 'cm', 15.0);
900 insert into rtest_comp values ('p5', 'inch', 7.0);
901 insert into rtest_comp values ('p6', 'inch', 4.4);
902 select * from rtest_vcomp order by part;
913 select * from rtest_vcomp where size_in_cm > 10.0 order by size_in_cm using >;
924 -- In addition run the (slightly modified) queries from the
925 -- programmers manual section on the rule system.
927 CREATE TABLE shoe_data (
928 shoename char(10), -- primary key
929 sh_avail integer, -- available # of pairs
930 slcolor char(10), -- preferred shoelace color
931 slminlen float, -- miminum shoelace length
932 slmaxlen float, -- maximum shoelace length
933 slunit char(8) -- length unit
935 CREATE TABLE shoelace_data (
936 sl_name char(10), -- primary key
937 sl_avail integer, -- available # of pairs
938 sl_color char(10), -- shoelace color
939 sl_len float, -- shoelace length
940 sl_unit char(8) -- length unit
943 un_name char(8), -- the primary key
944 un_fact float -- factor to transform to cm
951 sh.slminlen * un.un_fact AS slminlen_cm,
953 sh.slmaxlen * un.un_fact AS slmaxlen_cm,
955 FROM shoe_data sh, unit un
956 WHERE sh.slunit = un.un_name;
957 CREATE VIEW shoelace AS
963 s.sl_len * u.un_fact AS sl_len_cm
964 FROM shoelace_data s, unit u
965 WHERE s.sl_unit = u.un_name;
966 CREATE VIEW shoe_ready AS
971 int4smaller(rsh.sh_avail, rsl.sl_avail) AS total_avail
972 FROM shoe rsh, shoelace rsl
973 WHERE rsl.sl_color = rsh.slcolor
974 AND rsl.sl_len_cm >= rsh.slminlen_cm
975 AND rsl.sl_len_cm <= rsh.slmaxlen_cm;
976 INSERT INTO unit VALUES ('cm', 1.0);
977 INSERT INTO unit VALUES ('m', 100.0);
978 INSERT INTO unit VALUES ('inch', 2.54);
979 INSERT INTO shoe_data VALUES ('sh1', 2, 'black', 70.0, 90.0, 'cm');
980 INSERT INTO shoe_data VALUES ('sh2', 0, 'black', 30.0, 40.0, 'inch');
981 INSERT INTO shoe_data VALUES ('sh3', 4, 'brown', 50.0, 65.0, 'cm');
982 INSERT INTO shoe_data VALUES ('sh4', 3, 'brown', 40.0, 50.0, 'inch');
983 INSERT INTO shoelace_data VALUES ('sl1', 5, 'black', 80.0, 'cm');
984 INSERT INTO shoelace_data VALUES ('sl2', 6, 'black', 100.0, 'cm');
985 INSERT INTO shoelace_data VALUES ('sl3', 0, 'black', 35.0 , 'inch');
986 INSERT INTO shoelace_data VALUES ('sl4', 8, 'black', 40.0 , 'inch');
987 INSERT INTO shoelace_data VALUES ('sl5', 4, 'brown', 1.0 , 'm');
988 INSERT INTO shoelace_data VALUES ('sl6', 0, 'brown', 0.9 , 'm');
989 INSERT INTO shoelace_data VALUES ('sl7', 7, 'brown', 60 , 'cm');
990 INSERT INTO shoelace_data VALUES ('sl8', 1, 'brown', 40 , 'inch');
992 SELECT * FROM shoelace ORDER BY sl_name;
993 sl_name | sl_avail | sl_color | sl_len | sl_unit | sl_len_cm
994 ------------+----------+------------+--------+----------+-----------
995 sl1 | 5 | black | 80 | cm | 80
996 sl2 | 6 | black | 100 | cm | 100
997 sl3 | 0 | black | 35 | inch | 88.9
998 sl4 | 8 | black | 40 | inch | 101.6
999 sl5 | 4 | brown | 1 | m | 100
1000 sl6 | 0 | brown | 0.9 | m | 90
1001 sl7 | 7 | brown | 60 | cm | 60
1002 sl8 | 1 | brown | 40 | inch | 101.6
1005 SELECT * FROM shoe_ready WHERE total_avail >= 2;
1006 shoename | sh_avail | sl_name | sl_avail | total_avail
1007 ------------+----------+------------+----------+-------------
1008 sh1 | 2 | sl1 | 5 | 2
1009 sh3 | 4 | sl7 | 7 | 4
1012 CREATE TABLE shoelace_log (
1013 sl_name char(10), -- shoelace changed
1014 sl_avail integer, -- new available value
1015 log_who name, -- who did it
1016 log_when datetime -- when
1018 -- Want "log_who" to be CURRENT_USER,
1019 -- but that is non-portable for the regression test
1020 -- - thomas 1999-02-21
1021 CREATE RULE log_shoelace AS ON UPDATE TO shoelace_data
1022 WHERE NEW.sl_avail != OLD.sl_avail
1023 DO INSERT INTO shoelace_log VALUES (
1029 UPDATE shoelace_data SET sl_avail = 6 WHERE sl_name = 'sl7';
1030 SELECT * FROM shoelace_log;
1031 sl_name | sl_avail | log_who | log_when
1032 ------------+----------+----------+----------
1033 sl7 | 6 | Al Bundy | epoch
1036 CREATE RULE shoelace_ins AS ON INSERT TO shoelace
1038 INSERT INTO shoelace_data VALUES (
1044 CREATE RULE shoelace_upd AS ON UPDATE TO shoelace
1046 UPDATE shoelace_data SET
1047 sl_name = NEW.sl_name,
1048 sl_avail = NEW.sl_avail,
1049 sl_color = NEW.sl_color,
1050 sl_len = NEW.sl_len,
1051 sl_unit = NEW.sl_unit
1052 WHERE sl_name = OLD.sl_name;
1053 CREATE RULE shoelace_del AS ON DELETE TO shoelace
1055 DELETE FROM shoelace_data
1056 WHERE sl_name = OLD.sl_name;
1057 CREATE TABLE shoelace_arrive (
1061 CREATE TABLE shoelace_ok (
1065 CREATE RULE shoelace_ok_ins AS ON INSERT TO shoelace_ok
1068 sl_avail = sl_avail + NEW.ok_quant
1069 WHERE sl_name = NEW.ok_name;
1070 INSERT INTO shoelace_arrive VALUES ('sl3', 10);
1071 INSERT INTO shoelace_arrive VALUES ('sl6', 20);
1072 INSERT INTO shoelace_arrive VALUES ('sl8', 20);
1073 SELECT * FROM shoelace ORDER BY sl_name;
1074 sl_name | sl_avail | sl_color | sl_len | sl_unit | sl_len_cm
1075 ------------+----------+------------+--------+----------+-----------
1076 sl1 | 5 | black | 80 | cm | 80
1077 sl2 | 6 | black | 100 | cm | 100
1078 sl3 | 0 | black | 35 | inch | 88.9
1079 sl4 | 8 | black | 40 | inch | 101.6
1080 sl5 | 4 | brown | 1 | m | 100
1081 sl6 | 0 | brown | 0.9 | m | 90
1082 sl7 | 6 | brown | 60 | cm | 60
1083 sl8 | 1 | brown | 40 | inch | 101.6
1086 insert into shoelace_ok select * from shoelace_arrive;
1087 SELECT * FROM shoelace ORDER BY sl_name;
1088 sl_name | sl_avail | sl_color | sl_len | sl_unit | sl_len_cm
1089 ------------+----------+------------+--------+----------+-----------
1090 sl1 | 5 | black | 80 | cm | 80
1091 sl2 | 6 | black | 100 | cm | 100
1092 sl3 | 10 | black | 35 | inch | 88.9
1093 sl4 | 8 | black | 40 | inch | 101.6
1094 sl5 | 4 | brown | 1 | m | 100
1095 sl6 | 20 | brown | 0.9 | m | 90
1096 sl7 | 6 | brown | 60 | cm | 60
1097 sl8 | 21 | brown | 40 | inch | 101.6
1100 SELECT * FROM shoelace_log ORDER BY sl_name;
1101 sl_name | sl_avail | log_who | log_when
1102 ------------+----------+----------+----------
1103 sl3 | 10 | Al Bundy | epoch
1104 sl6 | 20 | Al Bundy | epoch
1105 sl7 | 6 | Al Bundy | epoch
1106 sl8 | 21 | Al Bundy | epoch
1109 CREATE VIEW shoelace_obsolete AS
1110 SELECT * FROM shoelace WHERE NOT EXISTS
1111 (SELECT shoename FROM shoe WHERE slcolor = sl_color);
1112 CREATE VIEW shoelace_candelete AS
1113 SELECT * FROM shoelace_obsolete WHERE sl_avail = 0;
1114 insert into shoelace values ('sl9', 0, 'pink', 35.0, 'inch', 0.0);
1115 insert into shoelace values ('sl10', 1000, 'magenta', 40.0, 'inch', 0.0);
1116 SELECT * FROM shoelace_obsolete;
1117 sl_name | sl_avail | sl_color | sl_len | sl_unit | sl_len_cm
1118 ------------+----------+------------+--------+----------+-----------
1119 sl9 | 0 | pink | 35 | inch | 88.9
1120 sl10 | 1000 | magenta | 40 | inch | 101.6
1123 SELECT * FROM shoelace_candelete;
1124 sl_name | sl_avail | sl_color | sl_len | sl_unit | sl_len_cm
1125 ------------+----------+------------+--------+----------+-----------
1126 sl9 | 0 | pink | 35 | inch | 88.9
1129 DELETE FROM shoelace WHERE EXISTS
1130 (SELECT * FROM shoelace_candelete
1131 WHERE sl_name = shoelace.sl_name);
1132 SELECT * FROM shoelace ORDER BY sl_name;
1133 sl_name | sl_avail | sl_color | sl_len | sl_unit | sl_len_cm
1134 ------------+----------+------------+--------+----------+-----------
1135 sl1 | 5 | black | 80 | cm | 80
1136 sl10 | 1000 | magenta | 40 | inch | 101.6
1137 sl2 | 6 | black | 100 | cm | 100
1138 sl3 | 10 | black | 35 | inch | 88.9
1139 sl4 | 8 | black | 40 | inch | 101.6
1140 sl5 | 4 | brown | 1 | m | 100
1141 sl6 | 20 | brown | 0.9 | m | 90
1142 sl7 | 6 | brown | 60 | cm | 60
1143 sl8 | 21 | brown | 40 | inch | 101.6
1146 SELECT * FROM shoe ORDER BY shoename;
1147 shoename | sh_avail | slcolor | slminlen | slminlen_cm | slmaxlen | slmaxlen_cm | slunit
1148 ------------+----------+------------+----------+-------------+----------+-------------+----------
1149 sh1 | 2 | black | 70 | 70 | 90 | 90 | cm
1150 sh2 | 0 | black | 30 | 76.2 | 40 | 101.6 | inch
1151 sh3 | 4 | brown | 50 | 50 | 65 | 65 | cm
1152 sh4 | 3 | brown | 40 | 101.6 | 50 | 127 | inch
1155 SELECT count(*) FROM shoe;
1162 -- Simple test of qualified ON INSERT ... this did not work in 7.0 ...
1164 create table foo (f1 int);
1165 create table foo2 (f1 int);
1166 create rule foorule as on insert to foo where f1 < 100
1168 insert into foo values(1);
1169 insert into foo values(1001);
1177 -- this should fail because f1 is not exposed for unqualified reference:
1178 create rule foorule as on insert to foo where f1 < 100
1179 do instead insert into foo2 values (f1);
1180 ERROR: Attribute 'f1' not found
1181 -- this is the correct way:
1182 create rule foorule as on insert to foo where f1 < 100
1183 do instead insert into foo2 values (new.f1);
1184 insert into foo values(2);
1185 insert into foo values(100);
1203 -- Check that ruleutils are working
1205 SELECT viewname, definition FROM pg_views ORDER BY viewname;
1206 viewname | definition
1207 --------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1208 iexit | SELECT ih.name, ih.thepath, interpt_pp(ih.thepath, r.thepath) AS exit FROM ihighway ih, ramp r WHERE (ih.thepath ## r.thepath);
1209 pg_indexes | SELECT c.relname AS tablename, i.relname AS indexname, pg_get_indexdef(x.indexrelid) AS indexdef FROM pg_index x, pg_class c, pg_class i WHERE ((((c.relkind = 'r'::"char") AND (i.relkind = 'i'::"char")) AND (c.oid = x.indrelid)) AND (i.oid = x.indexrelid));
1210 pg_rules | SELECT c.relname AS tablename, r.rulename, pg_get_ruledef(r.rulename) AS definition FROM pg_rewrite r, pg_class c WHERE ((r.rulename !~ '^_RET'::text) AND (c.oid = r.ev_class));
1211 pg_tables | SELECT c.relname AS tablename, pg_get_userbyid(c.relowner) AS tableowner, c.relhasindex AS hasindexes, c.relhasrules AS hasrules, (c.reltriggers > 0) AS hastriggers FROM pg_class c WHERE ((c.relkind = 'r'::"char") OR (c.relkind = 's'::"char"));
1212 pg_user | SELECT pg_shadow.usename, pg_shadow.usesysid, pg_shadow.usecreatedb, pg_shadow.usetrace, pg_shadow.usesuper, pg_shadow.usecatupd, '********'::text AS passwd, pg_shadow.valuntil FROM pg_shadow;
1213 pg_views | SELECT c.relname AS viewname, pg_get_userbyid(c.relowner) AS viewowner, pg_get_viewdef(c.relname) AS definition FROM pg_class c WHERE (c.relkind = 'v'::"char");
1214 rtest_v1 | SELECT rtest_t1.a, rtest_t1.b FROM rtest_t1;
1215 rtest_vcomp | SELECT x.part, (x.size * y.factor) AS size_in_cm FROM rtest_comp x, rtest_unitfact y WHERE (x.unit = y.unit);
1216 rtest_vview1 | SELECT x.a, x.b FROM rtest_view1 x WHERE (0 < (SELECT count(*) AS count FROM rtest_view2 y WHERE (y.a = x.a)));
1217 rtest_vview2 | SELECT rtest_view1.a, rtest_view1.b FROM rtest_view1 WHERE rtest_view1.v;
1218 rtest_vview3 | SELECT x.a, x.b FROM rtest_vview2 x WHERE (0 < (SELECT count(*) AS count FROM rtest_view2 y WHERE (y.a = x.a)));
1219 rtest_vview4 | SELECT x.a, x.b, count(y.a) AS refcount FROM rtest_view1 x, rtest_view2 y WHERE (x.a = y.a) GROUP BY x.a, x.b;
1220 rtest_vview5 | SELECT rtest_view1.a, rtest_view1.b, rtest_viewfunc1(rtest_view1.a) AS refcount FROM rtest_view1;
1221 shoe | SELECT sh.shoename, sh.sh_avail, sh.slcolor, sh.slminlen, (sh.slminlen * un.un_fact) AS slminlen_cm, sh.slmaxlen, (sh.slmaxlen * un.un_fact) AS slmaxlen_cm, sh.slunit FROM shoe_data sh, unit un WHERE (sh.slunit = un.un_name);
1222 shoe_ready | SELECT rsh.shoename, rsh.sh_avail, rsl.sl_name, rsl.sl_avail, int4smaller(rsh.sh_avail, rsl.sl_avail) AS total_avail FROM shoe rsh, shoelace rsl WHERE (((rsl.sl_color = rsh.slcolor) AND (rsl.sl_len_cm >= rsh.slminlen_cm)) AND (rsl.sl_len_cm <= rsh.slmaxlen_cm));
1223 shoelace | SELECT s.sl_name, s.sl_avail, s.sl_color, s.sl_len, s.sl_unit, (s.sl_len * u.un_fact) AS sl_len_cm FROM shoelace_data s, unit u WHERE (s.sl_unit = u.un_name);
1224 shoelace_candelete | SELECT shoelace_obsolete.sl_name, shoelace_obsolete.sl_avail, shoelace_obsolete.sl_color, shoelace_obsolete.sl_len, shoelace_obsolete.sl_unit, shoelace_obsolete.sl_len_cm FROM shoelace_obsolete WHERE (shoelace_obsolete.sl_avail = 0);
1225 shoelace_obsolete | SELECT shoelace.sl_name, shoelace.sl_avail, shoelace.sl_color, shoelace.sl_len, shoelace.sl_unit, shoelace.sl_len_cm FROM shoelace WHERE (NOT (EXISTS (SELECT shoe.shoename FROM shoe WHERE (shoe.slcolor = shoelace.sl_color))));
1226 street | SELECT r.name, r.thepath, c.cname FROM ONLY road r, real_city c WHERE (c.outline ## r.thepath);
1227 toyemp | SELECT emp.name, emp.age, emp."location", (12 * emp.salary) AS annualsal FROM emp;
1230 SELECT tablename, rulename, definition FROM pg_rules
1231 ORDER BY tablename, rulename;
1232 tablename | rulename | definition
1233 ---------------+-----------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1234 rtest_emp | rtest_emp_del | CREATE RULE rtest_emp_del AS ON DELETE TO rtest_emp DO INSERT INTO rtest_emplog (ename, who, "action", newsal, oldsal) VALUES (old.ename, "current_user"(), 'fired '::bpchar, '$0.00'::money, old.salary);
1235 rtest_emp | rtest_emp_ins | CREATE RULE rtest_emp_ins AS ON INSERT TO rtest_emp DO INSERT INTO rtest_emplog (ename, who, "action", newsal, oldsal) VALUES (new.ename, "current_user"(), 'hired '::bpchar, new.salary, '$0.00'::money);
1236 rtest_emp | rtest_emp_upd | CREATE RULE rtest_emp_upd AS ON UPDATE TO rtest_emp WHERE (new.salary <> old.salary) DO INSERT INTO rtest_emplog (ename, who, "action", newsal, oldsal) VALUES (new.ename, "current_user"(), 'honored '::bpchar, new.salary, old.salary);
1237 rtest_nothn1 | rtest_nothn_r1 | CREATE RULE rtest_nothn_r1 AS ON INSERT TO rtest_nothn1 WHERE ((new.a >= 10) AND (new.a < 20)) DO INSTEAD SELECT 1;
1238 rtest_nothn1 | rtest_nothn_r2 | CREATE RULE rtest_nothn_r2 AS ON INSERT TO rtest_nothn1 WHERE ((new.a >= 30) AND (new.a < 40)) DO INSTEAD NOTHING;
1239 rtest_nothn2 | rtest_nothn_r3 | CREATE RULE rtest_nothn_r3 AS ON INSERT TO rtest_nothn2 WHERE (new.a >= 100) DO INSTEAD INSERT INTO rtest_nothn3 (a, b) VALUES (new.a, new.b);
1240 rtest_nothn2 | rtest_nothn_r4 | CREATE RULE rtest_nothn_r4 AS ON INSERT TO rtest_nothn2 DO INSTEAD NOTHING;
1241 rtest_order1 | rtest_order_r1 | CREATE RULE rtest_order_r1 AS ON INSERT TO rtest_order1 DO INSTEAD INSERT INTO rtest_order2 (a, b, c) VALUES (new.a, nextval('rtest_seq'::text), 'rule 1 - this should run 3rd or 4th'::text);
1242 rtest_order1 | rtest_order_r2 | CREATE RULE rtest_order_r2 AS ON INSERT TO rtest_order1 DO INSERT INTO rtest_order2 (a, b, c) VALUES (new.a, nextval('rtest_seq'::text), 'rule 2 - this should run 1st'::text);
1243 rtest_order1 | rtest_order_r3 | CREATE RULE rtest_order_r3 AS ON INSERT TO rtest_order1 DO INSTEAD INSERT INTO rtest_order2 (a, b, c) VALUES (new.a, nextval('rtest_seq'::text), 'rule 3 - this should run 3rd or 4th'::text);
1244 rtest_order1 | rtest_order_r4 | CREATE RULE rtest_order_r4 AS ON INSERT TO rtest_order1 WHERE (new.a < 100) DO INSTEAD INSERT INTO rtest_order2 (a, b, c) VALUES (new.a, nextval('rtest_seq'::text), 'rule 4 - this should run 2nd'::text);
1245 rtest_person | rtest_pers_del | CREATE RULE rtest_pers_del AS ON DELETE TO rtest_person DO DELETE FROM rtest_admin WHERE (rtest_admin.pname = old.pname);
1246 rtest_person | rtest_pers_upd | CREATE RULE rtest_pers_upd AS ON UPDATE TO rtest_person DO UPDATE rtest_admin SET pname = new.pname WHERE (rtest_admin.pname = old.pname);
1247 rtest_system | rtest_sys_del | CREATE RULE rtest_sys_del AS ON DELETE TO rtest_system DO (DELETE FROM rtest_interface WHERE (rtest_interface.sysname = old.sysname); DELETE FROM rtest_admin WHERE (rtest_admin.sysname = old.sysname); );
1248 rtest_system | rtest_sys_upd | CREATE RULE rtest_sys_upd AS ON UPDATE TO rtest_system DO (UPDATE rtest_interface SET sysname = new.sysname WHERE (rtest_interface.sysname = old.sysname); UPDATE rtest_admin SET sysname = new.sysname WHERE (rtest_admin.sysname = old.sysname); );
1249 rtest_t4 | rtest_t4_ins1 | CREATE RULE rtest_t4_ins1 AS ON INSERT TO rtest_t4 WHERE ((new.a >= 10) AND (new.a < 20)) DO INSTEAD INSERT INTO rtest_t5 (a, b) VALUES (new.a, new.b);
1250 rtest_t4 | rtest_t4_ins2 | CREATE RULE rtest_t4_ins2 AS ON INSERT TO rtest_t4 WHERE ((new.a >= 20) AND (new.a < 30)) DO INSERT INTO rtest_t6 (a, b) VALUES (new.a, new.b);
1251 rtest_t5 | rtest_t5_ins | CREATE RULE rtest_t5_ins AS ON INSERT TO rtest_t5 WHERE (new.a > 15) DO INSERT INTO rtest_t7 (a, b) VALUES (new.a, new.b);
1252 rtest_t6 | rtest_t6_ins | CREATE RULE rtest_t6_ins AS ON INSERT TO rtest_t6 WHERE (new.a > 25) DO INSTEAD INSERT INTO rtest_t8 (a, b) VALUES (new.a, new.b);
1253 rtest_v1 | rtest_v1_del | CREATE RULE rtest_v1_del AS ON DELETE TO rtest_v1 DO INSTEAD DELETE FROM rtest_t1 WHERE (rtest_t1.a = old.a);
1254 rtest_v1 | rtest_v1_ins | CREATE RULE rtest_v1_ins AS ON INSERT TO rtest_v1 DO INSTEAD INSERT INTO rtest_t1 (a, b) VALUES (new.a, new.b);
1255 rtest_v1 | rtest_v1_upd | CREATE RULE rtest_v1_upd AS ON UPDATE TO rtest_v1 DO INSTEAD UPDATE rtest_t1 SET a = new.a, b = new.b WHERE (rtest_t1.a = old.a);
1256 shoelace | shoelace_del | CREATE RULE shoelace_del AS ON DELETE TO shoelace DO INSTEAD DELETE FROM shoelace_data WHERE (shoelace_data.sl_name = old.sl_name);
1257 shoelace | shoelace_ins | CREATE RULE shoelace_ins AS ON INSERT TO shoelace DO INSTEAD INSERT INTO shoelace_data (sl_name, sl_avail, sl_color, sl_len, sl_unit) VALUES (new.sl_name, new.sl_avail, new.sl_color, new.sl_len, new.sl_unit);
1258 shoelace | shoelace_upd | CREATE RULE shoelace_upd AS ON UPDATE TO shoelace DO INSTEAD UPDATE shoelace_data SET sl_name = new.sl_name, sl_avail = new.sl_avail, sl_color = new.sl_color, sl_len = new.sl_len, sl_unit = new.sl_unit WHERE (shoelace_data.sl_name = old.sl_name);
1259 shoelace_data | log_shoelace | CREATE RULE log_shoelace AS ON UPDATE TO shoelace_data WHERE (new.sl_avail <> old.sl_avail) DO INSERT INTO shoelace_log (sl_name, sl_avail, log_who, log_when) VALUES (new.sl_name, new.sl_avail, 'Al Bundy'::name, "timestamp"('epoch'::text));
1260 shoelace_ok | shoelace_ok_ins | CREATE RULE shoelace_ok_ins AS ON INSERT TO shoelace_ok DO INSTEAD UPDATE shoelace SET sl_avail = (shoelace.sl_avail + new.ok_quant) WHERE (shoelace.sl_name = new.ok_name);