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
COMMENT ON RULE rtest_v1_bad ON rtest_v1 IS 'bad rule';
22
ERROR: rule "rtest_v1_bad" for relation "rtest_v1" does not exist
23
COMMENT ON RULE rtest_v1_del ON rtest_v1 IS 'delete rule';
24
COMMENT ON RULE rtest_v1_del ON rtest_v1 IS NULL;
26
-- Tables and rules for the constraint update/delete test
29
-- Now that we have multiple action rule support, we check
30
-- both possible syntaxes to define them (The last action
31
-- can but must not have a semicolon at the end).
33
create table rtest_system (sysname text, sysdesc text);
34
create table rtest_interface (sysname text, ifname text);
35
create table rtest_person (pname text, pdesc text);
36
create table rtest_admin (pname text, sysname text);
37
create rule rtest_sys_upd as on update to rtest_system do also (
38
update rtest_interface set sysname = new.sysname
39
where sysname = old.sysname;
40
update rtest_admin set sysname = new.sysname
41
where sysname = old.sysname
43
create rule rtest_sys_del as on delete to rtest_system do also (
44
delete from rtest_interface where sysname = old.sysname;
45
delete from rtest_admin where sysname = old.sysname;
47
create rule rtest_pers_upd as on update to rtest_person do also
48
update rtest_admin set pname = new.pname where pname = old.pname;
49
create rule rtest_pers_del as on delete to rtest_person do also
50
delete from rtest_admin where pname = old.pname;
52
-- Tables and rules for the logging test
54
create table rtest_emp (ename char(20), salary money);
55
create table rtest_emplog (ename char(20), who name, action char(10), newsal money, oldsal money);
56
create table rtest_empmass (ename char(20), salary money);
57
create rule rtest_emp_ins as on insert to rtest_emp do
58
insert into rtest_emplog values (new.ename, current_user,
59
'hired', new.salary, '0.00');
60
create rule rtest_emp_upd as on update to rtest_emp where new.salary != old.salary do
61
insert into rtest_emplog values (new.ename, current_user,
62
'honored', new.salary, old.salary);
63
create rule rtest_emp_del as on delete to rtest_emp do
64
insert into rtest_emplog values (old.ename, current_user,
65
'fired', '0.00', old.salary);
67
-- Tables and rules for the multiple cascaded qualified instead
70
create table rtest_t4 (a int4, b text);
71
create table rtest_t5 (a int4, b text);
72
create table rtest_t6 (a int4, b text);
73
create table rtest_t7 (a int4, b text);
74
create table rtest_t8 (a int4, b text);
75
create table rtest_t9 (a int4, b text);
76
create rule rtest_t4_ins1 as on insert to rtest_t4
77
where new.a >= 10 and new.a < 20 do instead
78
insert into rtest_t5 values (new.a, new.b);
79
create rule rtest_t4_ins2 as on insert to rtest_t4
80
where new.a >= 20 and new.a < 30 do
81
insert into rtest_t6 values (new.a, new.b);
82
create rule rtest_t5_ins as on insert to rtest_t5
84
insert into rtest_t7 values (new.a, new.b);
85
create rule rtest_t6_ins as on insert to rtest_t6
86
where new.a > 25 do instead
87
insert into rtest_t8 values (new.a, new.b);
89
-- Tables and rules for the rule fire order test
91
-- As of PG 7.3, the rules should fire in order by name, regardless
92
-- of INSTEAD attributes or creation order.
94
create table rtest_order1 (a int4);
95
create table rtest_order2 (a int4, b int4, c text);
96
create sequence rtest_seq;
97
create rule rtest_order_r3 as on insert to rtest_order1 do instead
98
insert into rtest_order2 values (new.a, nextval('rtest_seq'),
99
'rule 3 - this should run 3rd');
100
create rule rtest_order_r4 as on insert to rtest_order1
101
where a < 100 do instead
102
insert into rtest_order2 values (new.a, nextval('rtest_seq'),
103
'rule 4 - this should run 4th');
104
create rule rtest_order_r2 as on insert to rtest_order1 do
105
insert into rtest_order2 values (new.a, nextval('rtest_seq'),
106
'rule 2 - this should run 2nd');
107
create rule rtest_order_r1 as on insert to rtest_order1 do instead
108
insert into rtest_order2 values (new.a, nextval('rtest_seq'),
109
'rule 1 - this should run 1st');
111
-- Tables and rules for the instead nothing test
113
create table rtest_nothn1 (a int4, b text);
114
create table rtest_nothn2 (a int4, b text);
115
create table rtest_nothn3 (a int4, b text);
116
create table rtest_nothn4 (a int4, b text);
117
create rule rtest_nothn_r1 as on insert to rtest_nothn1
118
where new.a >= 10 and new.a < 20 do instead nothing;
119
create rule rtest_nothn_r2 as on insert to rtest_nothn1
120
where new.a >= 30 and new.a < 40 do instead nothing;
121
create rule rtest_nothn_r3 as on insert to rtest_nothn2
122
where new.a >= 100 do instead
123
insert into rtest_nothn3 values (new.a, new.b);
124
create rule rtest_nothn_r4 as on insert to rtest_nothn2
127
-- Tests on a view that is select * of a table
128
-- and has insert/update/delete instead rules to
129
-- behave close like the real table.
132
-- We need test date later
134
insert into rtest_t2 values (1, 21);
135
insert into rtest_t2 values (2, 22);
136
insert into rtest_t2 values (3, 23);
137
insert into rtest_t3 values (1, 31);
138
insert into rtest_t3 values (2, 32);
139
insert into rtest_t3 values (3, 33);
140
insert into rtest_t3 values (4, 34);
141
insert into rtest_t3 values (5, 35);
143
insert into rtest_v1 values (1, 11);
144
insert into rtest_v1 values (2, 12);
145
select * from rtest_v1;
152
-- delete with constant expression
153
delete from rtest_v1 where a = 1;
154
select * from rtest_v1;
160
insert into rtest_v1 values (1, 11);
161
delete from rtest_v1 where b = 12;
162
select * from rtest_v1;
168
insert into rtest_v1 values (2, 12);
169
insert into rtest_v1 values (2, 13);
170
select * from rtest_v1;
178
** Remember the delete rule on rtest_v1: It says
179
** DO INSTEAD DELETE FROM rtest_t1 WHERE a = old.a
180
** So this time both rows with a = 2 must get deleted
182
** Remember the delete rule on rtest_v1: It says
183
** DO INSTEAD DELETE FROM rtest_t1 WHERE a = old.a
184
** So this time both rows with a = 2 must get deleted
186
delete from rtest_v1 where b = 12;
187
select * from rtest_v1;
193
delete from rtest_v1;
195
insert into rtest_v1 select * from rtest_t2;
196
select * from rtest_v1;
204
delete from rtest_v1;
205
-- same with swapped targetlist
206
insert into rtest_v1 (b, a) select b, a from rtest_t2;
207
select * from rtest_v1;
215
-- now with only one target attribute
216
insert into rtest_v1 (a) select a from rtest_t3;
217
select * from rtest_v1;
230
select * from rtest_v1 where b isnull;
240
-- let attribute a differ (must be done on rtest_t1 - see above)
241
update rtest_t1 set a = a + 10 where b isnull;
242
delete from rtest_v1 where b isnull;
243
select * from rtest_v1;
251
-- now updates with constant expression
252
update rtest_v1 set b = 42 where a = 2;
253
select * from rtest_v1;
261
update rtest_v1 set b = 99 where b = 42;
262
select * from rtest_v1;
270
update rtest_v1 set b = 88 where b < 50;
271
select * from rtest_v1;
279
delete from rtest_v1;
280
insert into rtest_v1 select rtest_t2.a, rtest_t3.b where rtest_t2.a = rtest_t3.a;
281
select * from rtest_v1;
289
-- updates in a mergejoin
290
update rtest_v1 set b = rtest_t2.b where a = rtest_t2.a;
291
select * from rtest_v1;
299
insert into rtest_v1 select * from rtest_t3;
300
select * from rtest_v1;
313
update rtest_t1 set a = a + 10 where b > 30;
314
select * from rtest_v1;
327
update rtest_v1 set a = rtest_t3.a + 20 where b = rtest_t3.b;
328
select * from rtest_v1;
342
-- Test for constraint updates/deletes
344
insert into rtest_system values ('orion', 'Linux Jan Wieck');
345
insert into rtest_system values ('notjw', 'WinNT Jan Wieck (notebook)');
346
insert into rtest_system values ('neptun', 'Fileserver');
347
insert into rtest_interface values ('orion', 'eth0');
348
insert into rtest_interface values ('orion', 'eth1');
349
insert into rtest_interface values ('notjw', 'eth0');
350
insert into rtest_interface values ('neptun', 'eth0');
351
insert into rtest_person values ('jw', 'Jan Wieck');
352
insert into rtest_person values ('bm', 'Bruce Momjian');
353
insert into rtest_admin values ('jw', 'orion');
354
insert into rtest_admin values ('jw', 'notjw');
355
insert into rtest_admin values ('bm', 'neptun');
356
update rtest_system set sysname = 'pluto' where sysname = 'neptun';
357
select * from rtest_interface;
366
select * from rtest_admin;
374
update rtest_person set pname = 'jwieck' where pdesc = 'Jan Wieck';
375
-- Note: use ORDER BY here to ensure consistent output across all systems.
376
-- The above UPDATE affects two rows with equal keys, so they could be
377
-- updated in either order depending on the whim of the local qsort().
378
select * from rtest_admin order by pname, sysname;
386
delete from rtest_system where sysname = 'orion';
387
select * from rtest_interface;
394
select * from rtest_admin;
402
-- Rule qualification test
404
insert into rtest_emp values ('wiech', '5000.00');
405
insert into rtest_emp values ('gates', '80000.00');
406
update rtest_emp set ename = 'wiecx' where ename = 'wiech';
407
update rtest_emp set ename = 'wieck', salary = '6000.00' where ename = 'wiecx';
408
update rtest_emp set salary = '7000.00' where ename = 'wieck';
409
delete from rtest_emp where ename = 'gates';
410
select ename, who = current_user as "matches user", action, newsal, oldsal from rtest_emplog order by ename, action, newsal;
411
ename | matches user | action | newsal | oldsal
412
----------------------+--------------+------------+------------+------------
413
gates | t | fired | $0.00 | $80,000.00
414
gates | t | hired | $80,000.00 | $0.00
415
wiech | t | hired | $5,000.00 | $0.00
416
wieck | t | honored | $6,000.00 | $5,000.00
417
wieck | t | honored | $7,000.00 | $6,000.00
420
insert into rtest_empmass values ('meyer', '4000.00');
421
insert into rtest_empmass values ('maier', '5000.00');
422
insert into rtest_empmass values ('mayr', '6000.00');
423
insert into rtest_emp select * from rtest_empmass;
424
select ename, who = current_user as "matches user", action, newsal, oldsal from rtest_emplog order by ename, action, newsal;
425
ename | matches user | action | newsal | oldsal
426
----------------------+--------------+------------+------------+------------
427
gates | t | fired | $0.00 | $80,000.00
428
gates | t | hired | $80,000.00 | $0.00
429
maier | t | hired | $5,000.00 | $0.00
430
mayr | t | hired | $6,000.00 | $0.00
431
meyer | t | hired | $4,000.00 | $0.00
432
wiech | t | hired | $5,000.00 | $0.00
433
wieck | t | honored | $6,000.00 | $5,000.00
434
wieck | t | honored | $7,000.00 | $6,000.00
437
update rtest_empmass set salary = salary + '1000.00';
438
update rtest_emp set salary = rtest_empmass.salary where ename = rtest_empmass.ename;
439
select ename, who = current_user as "matches user", action, newsal, oldsal from rtest_emplog order by ename, action, newsal;
440
ename | matches user | action | newsal | oldsal
441
----------------------+--------------+------------+------------+------------
442
gates | t | fired | $0.00 | $80,000.00
443
gates | t | hired | $80,000.00 | $0.00
444
maier | t | hired | $5,000.00 | $0.00
445
maier | t | honored | $6,000.00 | $5,000.00
446
mayr | t | hired | $6,000.00 | $0.00
447
mayr | t | honored | $7,000.00 | $6,000.00
448
meyer | t | hired | $4,000.00 | $0.00
449
meyer | t | honored | $5,000.00 | $4,000.00
450
wiech | t | hired | $5,000.00 | $0.00
451
wieck | t | honored | $6,000.00 | $5,000.00
452
wieck | t | honored | $7,000.00 | $6,000.00
455
delete from rtest_emp where ename = rtest_empmass.ename;
456
select ename, who = current_user as "matches user", action, newsal, oldsal from rtest_emplog order by ename, action, newsal;
457
ename | matches user | action | newsal | oldsal
458
----------------------+--------------+------------+------------+------------
459
gates | t | fired | $0.00 | $80,000.00
460
gates | t | hired | $80,000.00 | $0.00
461
maier | t | fired | $0.00 | $6,000.00
462
maier | t | hired | $5,000.00 | $0.00
463
maier | t | honored | $6,000.00 | $5,000.00
464
mayr | t | fired | $0.00 | $7,000.00
465
mayr | t | hired | $6,000.00 | $0.00
466
mayr | t | honored | $7,000.00 | $6,000.00
467
meyer | t | fired | $0.00 | $5,000.00
468
meyer | t | hired | $4,000.00 | $0.00
469
meyer | t | honored | $5,000.00 | $4,000.00
470
wiech | t | hired | $5,000.00 | $0.00
471
wieck | t | honored | $6,000.00 | $5,000.00
472
wieck | t | honored | $7,000.00 | $6,000.00
476
-- Multiple cascaded qualified instead rule test
478
insert into rtest_t4 values (1, 'Record should go to rtest_t4');
479
insert into rtest_t4 values (2, 'Record should go to rtest_t4');
480
insert into rtest_t4 values (10, 'Record should go to rtest_t5');
481
insert into rtest_t4 values (15, 'Record should go to rtest_t5');
482
insert into rtest_t4 values (19, 'Record should go to rtest_t5 and t7');
483
insert into rtest_t4 values (20, 'Record should go to rtest_t4 and t6');
484
insert into rtest_t4 values (26, 'Record should go to rtest_t4 and t8');
485
insert into rtest_t4 values (28, 'Record should go to rtest_t4 and t8');
486
insert into rtest_t4 values (30, 'Record should go to rtest_t4');
487
insert into rtest_t4 values (40, 'Record should go to rtest_t4');
488
select * from rtest_t4;
490
----+-------------------------------------
491
1 | Record should go to rtest_t4
492
2 | Record should go to rtest_t4
493
20 | Record should go to rtest_t4 and t6
494
26 | Record should go to rtest_t4 and t8
495
28 | Record should go to rtest_t4 and t8
496
30 | Record should go to rtest_t4
497
40 | Record should go to rtest_t4
500
select * from rtest_t5;
502
----+-------------------------------------
503
10 | Record should go to rtest_t5
504
15 | Record should go to rtest_t5
505
19 | Record should go to rtest_t5 and t7
508
select * from rtest_t6;
510
----+-------------------------------------
511
20 | Record should go to rtest_t4 and t6
514
select * from rtest_t7;
516
----+-------------------------------------
517
19 | Record should go to rtest_t5 and t7
520
select * from rtest_t8;
522
----+-------------------------------------
523
26 | Record should go to rtest_t4 and t8
524
28 | Record should go to rtest_t4 and t8
527
delete from rtest_t4;
528
delete from rtest_t5;
529
delete from rtest_t6;
530
delete from rtest_t7;
531
delete from rtest_t8;
532
insert into rtest_t9 values (1, 'Record should go to rtest_t4');
533
insert into rtest_t9 values (2, 'Record should go to rtest_t4');
534
insert into rtest_t9 values (10, 'Record should go to rtest_t5');
535
insert into rtest_t9 values (15, 'Record should go to rtest_t5');
536
insert into rtest_t9 values (19, 'Record should go to rtest_t5 and t7');
537
insert into rtest_t9 values (20, 'Record should go to rtest_t4 and t6');
538
insert into rtest_t9 values (26, 'Record should go to rtest_t4 and t8');
539
insert into rtest_t9 values (28, 'Record should go to rtest_t4 and t8');
540
insert into rtest_t9 values (30, 'Record should go to rtest_t4');
541
insert into rtest_t9 values (40, 'Record should go to rtest_t4');
542
insert into rtest_t4 select * from rtest_t9 where a < 20;
543
select * from rtest_t4;
545
---+------------------------------
546
1 | Record should go to rtest_t4
547
2 | Record should go to rtest_t4
550
select * from rtest_t5;
552
----+-------------------------------------
553
10 | Record should go to rtest_t5
554
15 | Record should go to rtest_t5
555
19 | Record should go to rtest_t5 and t7
558
select * from rtest_t6;
563
select * from rtest_t7;
565
----+-------------------------------------
566
19 | Record should go to rtest_t5 and t7
569
select * from rtest_t8;
574
insert into rtest_t4 select * from rtest_t9 where b ~ 'and t8';
575
select * from rtest_t4;
577
----+-------------------------------------
578
1 | Record should go to rtest_t4
579
2 | Record should go to rtest_t4
580
26 | Record should go to rtest_t4 and t8
581
28 | Record should go to rtest_t4 and t8
584
select * from rtest_t5;
586
----+-------------------------------------
587
10 | Record should go to rtest_t5
588
15 | Record should go to rtest_t5
589
19 | Record should go to rtest_t5 and t7
592
select * from rtest_t6;
597
select * from rtest_t7;
599
----+-------------------------------------
600
19 | Record should go to rtest_t5 and t7
603
select * from rtest_t8;
605
----+-------------------------------------
606
26 | Record should go to rtest_t4 and t8
607
28 | Record should go to rtest_t4 and t8
610
insert into rtest_t4 select a + 1, b from rtest_t9 where a in (20, 30, 40);
611
select * from rtest_t4;
613
----+-------------------------------------
614
1 | Record should go to rtest_t4
615
2 | Record should go to rtest_t4
616
26 | Record should go to rtest_t4 and t8
617
28 | Record should go to rtest_t4 and t8
618
21 | Record should go to rtest_t4 and t6
619
31 | Record should go to rtest_t4
620
41 | Record should go to rtest_t4
623
select * from rtest_t5;
625
----+-------------------------------------
626
10 | Record should go to rtest_t5
627
15 | Record should go to rtest_t5
628
19 | Record should go to rtest_t5 and t7
631
select * from rtest_t6;
633
----+-------------------------------------
634
21 | Record should go to rtest_t4 and t6
637
select * from rtest_t7;
639
----+-------------------------------------
640
19 | Record should go to rtest_t5 and t7
643
select * from rtest_t8;
645
----+-------------------------------------
646
26 | Record should go to rtest_t4 and t8
647
28 | Record should go to rtest_t4 and t8
651
-- Check that the ordering of rules fired is correct
653
insert into rtest_order1 values (1);
654
select * from rtest_order2;
656
---+---+------------------------------
657
1 | 1 | rule 1 - this should run 1st
658
1 | 2 | rule 2 - this should run 2nd
659
1 | 3 | rule 3 - this should run 3rd
660
1 | 4 | rule 4 - this should run 4th
664
-- Check if instead nothing w/without qualification works
666
insert into rtest_nothn1 values (1, 'want this');
667
insert into rtest_nothn1 values (2, 'want this');
668
insert into rtest_nothn1 values (10, 'don''t want this');
669
insert into rtest_nothn1 values (19, 'don''t want this');
670
insert into rtest_nothn1 values (20, 'want this');
671
insert into rtest_nothn1 values (29, 'want this');
672
insert into rtest_nothn1 values (30, 'don''t want this');
673
insert into rtest_nothn1 values (39, 'don''t want this');
674
insert into rtest_nothn1 values (40, 'want this');
675
insert into rtest_nothn1 values (50, 'want this');
676
insert into rtest_nothn1 values (60, 'want this');
677
select * from rtest_nothn1;
689
insert into rtest_nothn2 values (10, 'too small');
690
insert into rtest_nothn2 values (50, 'too small');
691
insert into rtest_nothn2 values (100, 'OK');
692
insert into rtest_nothn2 values (200, 'OK');
693
select * from rtest_nothn2;
698
select * from rtest_nothn3;
705
delete from rtest_nothn1;
706
delete from rtest_nothn2;
707
delete from rtest_nothn3;
708
insert into rtest_nothn4 values (1, 'want this');
709
insert into rtest_nothn4 values (2, 'want this');
710
insert into rtest_nothn4 values (10, 'don''t want this');
711
insert into rtest_nothn4 values (19, 'don''t want this');
712
insert into rtest_nothn4 values (20, 'want this');
713
insert into rtest_nothn4 values (29, 'want this');
714
insert into rtest_nothn4 values (30, 'don''t want this');
715
insert into rtest_nothn4 values (39, 'don''t want this');
716
insert into rtest_nothn4 values (40, 'want this');
717
insert into rtest_nothn4 values (50, 'want this');
718
insert into rtest_nothn4 values (60, 'want this');
719
insert into rtest_nothn1 select * from rtest_nothn4;
720
select * from rtest_nothn1;
732
delete from rtest_nothn4;
733
insert into rtest_nothn4 values (10, 'too small');
734
insert into rtest_nothn4 values (50, 'too small');
735
insert into rtest_nothn4 values (100, 'OK');
736
insert into rtest_nothn4 values (200, 'OK');
737
insert into rtest_nothn2 select * from rtest_nothn4;
738
select * from rtest_nothn2;
743
select * from rtest_nothn3;
750
create table rtest_view1 (a int4, b text, v bool);
751
create table rtest_view2 (a int4);
752
create table rtest_view3 (a int4, b text);
753
create table rtest_view4 (a int4, b text, c int4);
754
create view rtest_vview1 as select a, b from rtest_view1 X
755
where 0 < (select count(*) from rtest_view2 Y where Y.a = X.a);
756
create view rtest_vview2 as select a, b from rtest_view1 where v;
757
create view rtest_vview3 as select a, b from rtest_vview2 X
758
where 0 < (select count(*) from rtest_view2 Y where Y.a = X.a);
759
create view rtest_vview4 as select X.a, X.b, count(Y.a) as refcount
760
from rtest_view1 X, rtest_view2 Y
763
create function rtest_viewfunc1(int4) returns int4 as
764
'select count(*)::int4 from rtest_view2 where a = $1'
766
create view rtest_vview5 as select a, b, rtest_viewfunc1(a) as refcount
768
insert into rtest_view1 values (1, 'item 1', 't');
769
insert into rtest_view1 values (2, 'item 2', 't');
770
insert into rtest_view1 values (3, 'item 3', 't');
771
insert into rtest_view1 values (4, 'item 4', 'f');
772
insert into rtest_view1 values (5, 'item 5', 't');
773
insert into rtest_view1 values (6, 'item 6', 'f');
774
insert into rtest_view1 values (7, 'item 7', 't');
775
insert into rtest_view1 values (8, 'item 8', 't');
776
insert into rtest_view2 values (2);
777
insert into rtest_view2 values (2);
778
insert into rtest_view2 values (4);
779
insert into rtest_view2 values (5);
780
insert into rtest_view2 values (7);
781
insert into rtest_view2 values (7);
782
insert into rtest_view2 values (7);
783
insert into rtest_view2 values (7);
784
select * from rtest_vview1;
793
select * from rtest_vview2;
804
select * from rtest_vview3;
812
select * from rtest_vview4 order by a, b;
814
---+--------+----------
821
select * from rtest_vview5;
823
---+--------+----------
834
insert into rtest_view3 select * from rtest_vview1 where a < 7;
835
select * from rtest_view3;
843
delete from rtest_view3;
844
insert into rtest_view3 select * from rtest_vview2 where a != 5 and b !~ '2';
845
select * from rtest_view3;
854
delete from rtest_view3;
855
insert into rtest_view3 select * from rtest_vview3;
856
select * from rtest_view3;
864
delete from rtest_view3;
865
insert into rtest_view4 select * from rtest_vview4 where 3 > refcount;
866
select * from rtest_view4 order by a, b;
874
delete from rtest_view4;
875
insert into rtest_view4 select * from rtest_vview5 where a > 2 and refcount = 0;
876
select * from rtest_view4;
884
delete from rtest_view4;
886
-- Test for computations in views
888
create table rtest_comp (
893
create table rtest_unitfact (
897
create view rtest_vcomp as
898
select X.part, (X.size * Y.factor) as size_in_cm
899
from rtest_comp X, rtest_unitfact Y
900
where X.unit = Y.unit;
901
insert into rtest_unitfact values ('m', 100.0);
902
insert into rtest_unitfact values ('cm', 1.0);
903
insert into rtest_unitfact values ('inch', 2.54);
904
insert into rtest_comp values ('p1', 'm', 5.0);
905
insert into rtest_comp values ('p2', 'm', 3.0);
906
insert into rtest_comp values ('p3', 'cm', 5.0);
907
insert into rtest_comp values ('p4', 'cm', 15.0);
908
insert into rtest_comp values ('p5', 'inch', 7.0);
909
insert into rtest_comp values ('p6', 'inch', 4.4);
910
select * from rtest_vcomp order by part;
921
select * from rtest_vcomp where size_in_cm > 10.0 order by size_in_cm using >;
932
-- In addition run the (slightly modified) queries from the
933
-- programmers manual section on the rule system.
935
CREATE TABLE shoe_data (
936
shoename char(10), -- primary key
937
sh_avail integer, -- available # of pairs
938
slcolor char(10), -- preferred shoelace color
939
slminlen float, -- miminum shoelace length
940
slmaxlen float, -- maximum shoelace length
941
slunit char(8) -- length unit
943
CREATE TABLE shoelace_data (
944
sl_name char(10), -- primary key
945
sl_avail integer, -- available # of pairs
946
sl_color char(10), -- shoelace color
947
sl_len float, -- shoelace length
948
sl_unit char(8) -- length unit
951
un_name char(8), -- the primary key
952
un_fact float -- factor to transform to cm
959
sh.slminlen * un.un_fact AS slminlen_cm,
961
sh.slmaxlen * un.un_fact AS slmaxlen_cm,
963
FROM shoe_data sh, unit un
964
WHERE sh.slunit = un.un_name;
965
CREATE VIEW shoelace AS
971
s.sl_len * u.un_fact AS sl_len_cm
972
FROM shoelace_data s, unit u
973
WHERE s.sl_unit = u.un_name;
974
CREATE VIEW shoe_ready AS
979
int4smaller(rsh.sh_avail, rsl.sl_avail) AS total_avail
980
FROM shoe rsh, shoelace rsl
981
WHERE rsl.sl_color = rsh.slcolor
982
AND rsl.sl_len_cm >= rsh.slminlen_cm
983
AND rsl.sl_len_cm <= rsh.slmaxlen_cm;
984
INSERT INTO unit VALUES ('cm', 1.0);
985
INSERT INTO unit VALUES ('m', 100.0);
986
INSERT INTO unit VALUES ('inch', 2.54);
987
INSERT INTO shoe_data VALUES ('sh1', 2, 'black', 70.0, 90.0, 'cm');
988
INSERT INTO shoe_data VALUES ('sh2', 0, 'black', 30.0, 40.0, 'inch');
989
INSERT INTO shoe_data VALUES ('sh3', 4, 'brown', 50.0, 65.0, 'cm');
990
INSERT INTO shoe_data VALUES ('sh4', 3, 'brown', 40.0, 50.0, 'inch');
991
INSERT INTO shoelace_data VALUES ('sl1', 5, 'black', 80.0, 'cm');
992
INSERT INTO shoelace_data VALUES ('sl2', 6, 'black', 100.0, 'cm');
993
INSERT INTO shoelace_data VALUES ('sl3', 0, 'black', 35.0 , 'inch');
994
INSERT INTO shoelace_data VALUES ('sl4', 8, 'black', 40.0 , 'inch');
995
INSERT INTO shoelace_data VALUES ('sl5', 4, 'brown', 1.0 , 'm');
996
INSERT INTO shoelace_data VALUES ('sl6', 0, 'brown', 0.9 , 'm');
997
INSERT INTO shoelace_data VALUES ('sl7', 7, 'brown', 60 , 'cm');
998
INSERT INTO shoelace_data VALUES ('sl8', 1, 'brown', 40 , 'inch');
1000
SELECT * FROM shoelace ORDER BY sl_name;
1001
sl_name | sl_avail | sl_color | sl_len | sl_unit | sl_len_cm
1002
------------+----------+------------+--------+----------+-----------
1003
sl1 | 5 | black | 80 | cm | 80
1004
sl2 | 6 | black | 100 | cm | 100
1005
sl3 | 0 | black | 35 | inch | 88.9
1006
sl4 | 8 | black | 40 | inch | 101.6
1007
sl5 | 4 | brown | 1 | m | 100
1008
sl6 | 0 | brown | 0.9 | m | 90
1009
sl7 | 7 | brown | 60 | cm | 60
1010
sl8 | 1 | brown | 40 | inch | 101.6
1013
SELECT * FROM shoe_ready WHERE total_avail >= 2 ORDER BY 1;
1014
shoename | sh_avail | sl_name | sl_avail | total_avail
1015
------------+----------+------------+----------+-------------
1016
sh1 | 2 | sl1 | 5 | 2
1017
sh3 | 4 | sl7 | 7 | 4
1020
CREATE TABLE shoelace_log (
1021
sl_name char(10), -- shoelace changed
1022
sl_avail integer, -- new available value
1023
log_who name, -- who did it
1024
log_when timestamp -- when
1026
-- Want "log_who" to be CURRENT_USER,
1027
-- but that is non-portable for the regression test
1028
-- - thomas 1999-02-21
1029
CREATE RULE log_shoelace AS ON UPDATE TO shoelace_data
1030
WHERE NEW.sl_avail != OLD.sl_avail
1031
DO INSERT INTO shoelace_log VALUES (
1037
UPDATE shoelace_data SET sl_avail = 6 WHERE sl_name = 'sl7';
1038
SELECT * FROM shoelace_log;
1039
sl_name | sl_avail | log_who | log_when
1040
------------+----------+----------+--------------------------
1041
sl7 | 6 | Al Bundy | Thu Jan 01 00:00:00 1970
1044
CREATE RULE shoelace_ins AS ON INSERT TO shoelace
1046
INSERT INTO shoelace_data VALUES (
1052
CREATE RULE shoelace_upd AS ON UPDATE TO shoelace
1054
UPDATE shoelace_data SET
1055
sl_name = NEW.sl_name,
1056
sl_avail = NEW.sl_avail,
1057
sl_color = NEW.sl_color,
1058
sl_len = NEW.sl_len,
1059
sl_unit = NEW.sl_unit
1060
WHERE sl_name = OLD.sl_name;
1061
CREATE RULE shoelace_del AS ON DELETE TO shoelace
1063
DELETE FROM shoelace_data
1064
WHERE sl_name = OLD.sl_name;
1065
CREATE TABLE shoelace_arrive (
1069
CREATE TABLE shoelace_ok (
1073
CREATE RULE shoelace_ok_ins AS ON INSERT TO shoelace_ok
1076
sl_avail = sl_avail + NEW.ok_quant
1077
WHERE sl_name = NEW.ok_name;
1078
INSERT INTO shoelace_arrive VALUES ('sl3', 10);
1079
INSERT INTO shoelace_arrive VALUES ('sl6', 20);
1080
INSERT INTO shoelace_arrive VALUES ('sl8', 20);
1081
SELECT * FROM shoelace ORDER BY sl_name;
1082
sl_name | sl_avail | sl_color | sl_len | sl_unit | sl_len_cm
1083
------------+----------+------------+--------+----------+-----------
1084
sl1 | 5 | black | 80 | cm | 80
1085
sl2 | 6 | black | 100 | cm | 100
1086
sl3 | 0 | black | 35 | inch | 88.9
1087
sl4 | 8 | black | 40 | inch | 101.6
1088
sl5 | 4 | brown | 1 | m | 100
1089
sl6 | 0 | brown | 0.9 | m | 90
1090
sl7 | 6 | brown | 60 | cm | 60
1091
sl8 | 1 | brown | 40 | inch | 101.6
1094
insert into shoelace_ok select * from shoelace_arrive;
1095
SELECT * FROM shoelace ORDER BY sl_name;
1096
sl_name | sl_avail | sl_color | sl_len | sl_unit | sl_len_cm
1097
------------+----------+------------+--------+----------+-----------
1098
sl1 | 5 | black | 80 | cm | 80
1099
sl2 | 6 | black | 100 | cm | 100
1100
sl3 | 10 | black | 35 | inch | 88.9
1101
sl4 | 8 | black | 40 | inch | 101.6
1102
sl5 | 4 | brown | 1 | m | 100
1103
sl6 | 20 | brown | 0.9 | m | 90
1104
sl7 | 6 | brown | 60 | cm | 60
1105
sl8 | 21 | brown | 40 | inch | 101.6
1108
SELECT * FROM shoelace_log ORDER BY sl_name;
1109
sl_name | sl_avail | log_who | log_when
1110
------------+----------+----------+--------------------------
1111
sl3 | 10 | Al Bundy | Thu Jan 01 00:00:00 1970
1112
sl6 | 20 | Al Bundy | Thu Jan 01 00:00:00 1970
1113
sl7 | 6 | Al Bundy | Thu Jan 01 00:00:00 1970
1114
sl8 | 21 | Al Bundy | Thu Jan 01 00:00:00 1970
1117
CREATE VIEW shoelace_obsolete AS
1118
SELECT * FROM shoelace WHERE NOT EXISTS
1119
(SELECT shoename FROM shoe WHERE slcolor = sl_color);
1120
CREATE VIEW shoelace_candelete AS
1121
SELECT * FROM shoelace_obsolete WHERE sl_avail = 0;
1122
insert into shoelace values ('sl9', 0, 'pink', 35.0, 'inch', 0.0);
1123
insert into shoelace values ('sl10', 1000, 'magenta', 40.0, 'inch', 0.0);
1124
SELECT * FROM shoelace_obsolete ORDER BY sl_len_cm;
1125
sl_name | sl_avail | sl_color | sl_len | sl_unit | sl_len_cm
1126
------------+----------+------------+--------+----------+-----------
1127
sl9 | 0 | pink | 35 | inch | 88.9
1128
sl10 | 1000 | magenta | 40 | inch | 101.6
1131
SELECT * FROM shoelace_candelete;
1132
sl_name | sl_avail | sl_color | sl_len | sl_unit | sl_len_cm
1133
------------+----------+------------+--------+----------+-----------
1134
sl9 | 0 | pink | 35 | inch | 88.9
1137
DELETE FROM shoelace WHERE EXISTS
1138
(SELECT * FROM shoelace_candelete
1139
WHERE sl_name = shoelace.sl_name);
1140
SELECT * FROM shoelace ORDER BY sl_name;
1141
sl_name | sl_avail | sl_color | sl_len | sl_unit | sl_len_cm
1142
------------+----------+------------+--------+----------+-----------
1143
sl1 | 5 | black | 80 | cm | 80
1144
sl10 | 1000 | magenta | 40 | inch | 101.6
1145
sl2 | 6 | black | 100 | cm | 100
1146
sl3 | 10 | black | 35 | inch | 88.9
1147
sl4 | 8 | black | 40 | inch | 101.6
1148
sl5 | 4 | brown | 1 | m | 100
1149
sl6 | 20 | brown | 0.9 | m | 90
1150
sl7 | 6 | brown | 60 | cm | 60
1151
sl8 | 21 | brown | 40 | inch | 101.6
1154
SELECT * FROM shoe ORDER BY shoename;
1155
shoename | sh_avail | slcolor | slminlen | slminlen_cm | slmaxlen | slmaxlen_cm | slunit
1156
------------+----------+------------+----------+-------------+----------+-------------+----------
1157
sh1 | 2 | black | 70 | 70 | 90 | 90 | cm
1158
sh2 | 0 | black | 30 | 76.2 | 40 | 101.6 | inch
1159
sh3 | 4 | brown | 50 | 50 | 65 | 65 | cm
1160
sh4 | 3 | brown | 40 | 101.6 | 50 | 127 | inch
1163
SELECT count(*) FROM shoe;
1170
-- Simple test of qualified ON INSERT ... this did not work in 7.0 ...
1172
create table foo (f1 int);
1173
create table foo2 (f1 int);
1174
create rule foorule as on insert to foo where f1 < 100
1176
insert into foo values(1);
1177
insert into foo values(1001);
1184
drop rule foorule on foo;
1185
-- this should fail because f1 is not exposed for unqualified reference:
1186
create rule foorule as on insert to foo where f1 < 100
1187
do instead insert into foo2 values (f1);
1188
ERROR: column "f1" does not exist
1189
-- this is the correct way:
1190
create rule foorule as on insert to foo where f1 < 100
1191
do instead insert into foo2 values (new.f1);
1192
insert into foo values(2);
1193
insert into foo values(100);
1207
drop rule foorule on foo;
1211
-- Test rules containing INSERT ... SELECT, which is a very ugly special
1212
-- case as of 7.1. Example is based on bug report from Joel Burton.
1214
create table pparent (pid int, txt text);
1215
insert into pparent values (1,'parent1');
1216
insert into pparent values (2,'parent2');
1217
create table cchild (pid int, descrip text);
1218
insert into cchild values (1,'descrip1');
1219
create view vview as
1220
select pparent.pid, txt, descrip from
1221
pparent left join cchild using (pid);
1222
create rule rrule as
1223
on update to vview do instead
1225
insert into cchild (pid, descrip)
1226
select old.pid, new.descrip where old.descrip isnull;
1227
update cchild set descrip = new.descrip where cchild.pid = old.pid;
1229
select * from vview;
1231
-----+---------+----------
1232
1 | parent1 | descrip1
1236
update vview set descrip='test1' where pid=1;
1237
select * from vview;
1239
-----+---------+---------
1244
update vview set descrip='test2' where pid=2;
1245
select * from vview;
1247
-----+---------+---------
1252
update vview set descrip='test3' where pid=3;
1253
select * from vview;
1255
-----+---------+---------
1260
select * from cchild;
1267
drop rule rrule on vview;
1272
-- Check that ruleutils are working
1274
SELECT viewname, definition FROM pg_views WHERE schemaname <> 'information_schema' ORDER BY viewname;
1275
viewname | definition
1276
--------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1277
iexit | SELECT ih.name, ih.thepath, interpt_pp(ih.thepath, r.thepath) AS exit FROM ihighway ih, ramp r WHERE (ih.thepath ## r.thepath);
1278
pg_indexes | SELECT n.nspname AS schemaname, c.relname AS tablename, i.relname AS indexname, t.spcname AS "tablespace", pg_get_indexdef(i.oid) AS indexdef FROM ((((pg_index x JOIN pg_class c ON ((c.oid = x.indrelid))) JOIN pg_class i ON ((i.oid = x.indexrelid))) LEFT JOIN pg_namespace n ON ((n.oid = c.relnamespace))) LEFT JOIN pg_tablespace t ON ((t.oid = i.reltablespace))) WHERE ((c.relkind = 'r'::"char") AND (i.relkind = 'i'::"char"));
1279
pg_locks | SELECT l.relation, l."database", l."transaction", l.pid, l."mode", l.granted FROM pg_lock_status() l(relation oid, "database" oid, "transaction" xid, pid integer, "mode" text, granted boolean);
1280
pg_rules | SELECT n.nspname AS schemaname, c.relname AS tablename, r.rulename, pg_get_ruledef(r.oid) AS definition FROM ((pg_rewrite r JOIN pg_class c ON ((c.oid = r.ev_class))) LEFT JOIN pg_namespace n ON ((n.oid = c.relnamespace))) WHERE (r.rulename <> '_RETURN'::name);
1281
pg_settings | SELECT a.name, a.setting, a.category, a.short_desc, a.extra_desc, a.context, a.vartype, a.source, a.min_val, a.max_val FROM pg_show_all_settings() a(name text, setting text, category text, short_desc text, extra_desc text, context text, vartype text, source text, min_val text, max_val text);
1282
pg_stat_activity | SELECT d.oid AS datid, d.datname, pg_stat_get_backend_pid(s.backendid) AS procpid, pg_stat_get_backend_userid(s.backendid) AS usesysid, u.usename, pg_stat_get_backend_activity(s.backendid) AS current_query, pg_stat_get_backend_activity_start(s.backendid) AS query_start FROM pg_database d, (SELECT pg_stat_get_backend_idset() AS backendid) s, pg_shadow u WHERE ((pg_stat_get_backend_dbid(s.backendid) = d.oid) AND (pg_stat_get_backend_userid(s.backendid) = u.usesysid));
1283
pg_stat_all_indexes | SELECT c.oid AS relid, i.oid AS indexrelid, n.nspname AS schemaname, c.relname, i.relname AS indexrelname, pg_stat_get_numscans(i.oid) AS idx_scan, pg_stat_get_tuples_returned(i.oid) AS idx_tup_read, pg_stat_get_tuples_fetched(i.oid) AS idx_tup_fetch FROM (((pg_class c JOIN pg_index x ON ((c.oid = x.indrelid))) JOIN pg_class i ON ((i.oid = x.indexrelid))) LEFT JOIN pg_namespace n ON ((n.oid = c.relnamespace))) WHERE (c.relkind = 'r'::"char");
1284
pg_stat_all_tables | SELECT c.oid AS relid, n.nspname AS schemaname, c.relname, pg_stat_get_numscans(c.oid) AS seq_scan, pg_stat_get_tuples_returned(c.oid) AS seq_tup_read, sum(pg_stat_get_numscans(i.indexrelid)) AS idx_scan, sum(pg_stat_get_tuples_fetched(i.indexrelid)) AS idx_tup_fetch, pg_stat_get_tuples_inserted(c.oid) AS n_tup_ins, pg_stat_get_tuples_updated(c.oid) AS n_tup_upd, pg_stat_get_tuples_deleted(c.oid) AS n_tup_del FROM ((pg_class c LEFT JOIN pg_index i ON ((c.oid = i.indrelid))) LEFT JOIN pg_namespace n ON ((n.oid = c.relnamespace))) WHERE (c.relkind = 'r'::"char") GROUP BY c.oid, n.nspname, c.relname;
1285
pg_stat_database | SELECT d.oid AS datid, d.datname, pg_stat_get_db_numbackends(d.oid) AS numbackends, pg_stat_get_db_xact_commit(d.oid) AS xact_commit, pg_stat_get_db_xact_rollback(d.oid) AS xact_rollback, (pg_stat_get_db_blocks_fetched(d.oid) - pg_stat_get_db_blocks_hit(d.oid)) AS blks_read, pg_stat_get_db_blocks_hit(d.oid) AS blks_hit FROM pg_database d;
1286
pg_stat_sys_indexes | SELECT pg_stat_all_indexes.relid, pg_stat_all_indexes.indexrelid, pg_stat_all_indexes.schemaname, pg_stat_all_indexes.relname, pg_stat_all_indexes.indexrelname, pg_stat_all_indexes.idx_scan, pg_stat_all_indexes.idx_tup_read, pg_stat_all_indexes.idx_tup_fetch FROM pg_stat_all_indexes WHERE (((pg_stat_all_indexes.schemaname = 'pg_catalog'::name) OR (pg_stat_all_indexes.schemaname = 'pg_toast'::name)) OR (pg_stat_all_indexes.schemaname = 'information_schema'::name));
1287
pg_stat_sys_tables | SELECT pg_stat_all_tables.relid, pg_stat_all_tables.schemaname, pg_stat_all_tables.relname, pg_stat_all_tables.seq_scan, pg_stat_all_tables.seq_tup_read, pg_stat_all_tables.idx_scan, pg_stat_all_tables.idx_tup_fetch, pg_stat_all_tables.n_tup_ins, pg_stat_all_tables.n_tup_upd, pg_stat_all_tables.n_tup_del FROM pg_stat_all_tables WHERE (((pg_stat_all_tables.schemaname = 'pg_catalog'::name) OR (pg_stat_all_tables.schemaname = 'pg_toast'::name)) OR (pg_stat_all_tables.schemaname = 'information_schema'::name));
1288
pg_stat_user_indexes | SELECT pg_stat_all_indexes.relid, pg_stat_all_indexes.indexrelid, pg_stat_all_indexes.schemaname, pg_stat_all_indexes.relname, pg_stat_all_indexes.indexrelname, pg_stat_all_indexes.idx_scan, pg_stat_all_indexes.idx_tup_read, pg_stat_all_indexes.idx_tup_fetch FROM pg_stat_all_indexes WHERE (((pg_stat_all_indexes.schemaname <> 'pg_catalog'::name) AND (pg_stat_all_indexes.schemaname <> 'pg_toast'::name)) AND (pg_stat_all_indexes.schemaname <> 'information_schema'::name));
1289
pg_stat_user_tables | SELECT pg_stat_all_tables.relid, pg_stat_all_tables.schemaname, pg_stat_all_tables.relname, pg_stat_all_tables.seq_scan, pg_stat_all_tables.seq_tup_read, pg_stat_all_tables.idx_scan, pg_stat_all_tables.idx_tup_fetch, pg_stat_all_tables.n_tup_ins, pg_stat_all_tables.n_tup_upd, pg_stat_all_tables.n_tup_del FROM pg_stat_all_tables WHERE (((pg_stat_all_tables.schemaname <> 'pg_catalog'::name) AND (pg_stat_all_tables.schemaname <> 'pg_toast'::name)) AND (pg_stat_all_tables.schemaname <> 'information_schema'::name));
1290
pg_statio_all_indexes | SELECT c.oid AS relid, i.oid AS indexrelid, n.nspname AS schemaname, c.relname, i.relname AS indexrelname, (pg_stat_get_blocks_fetched(i.oid) - pg_stat_get_blocks_hit(i.oid)) AS idx_blks_read, pg_stat_get_blocks_hit(i.oid) AS idx_blks_hit FROM (((pg_class c JOIN pg_index x ON ((c.oid = x.indrelid))) JOIN pg_class i ON ((i.oid = x.indexrelid))) LEFT JOIN pg_namespace n ON ((n.oid = c.relnamespace))) WHERE (c.relkind = 'r'::"char");
1291
pg_statio_all_sequences | SELECT c.oid AS relid, n.nspname AS schemaname, c.relname, (pg_stat_get_blocks_fetched(c.oid) - pg_stat_get_blocks_hit(c.oid)) AS blks_read, pg_stat_get_blocks_hit(c.oid) AS blks_hit FROM (pg_class c LEFT JOIN pg_namespace n ON ((n.oid = c.relnamespace))) WHERE (c.relkind = 'S'::"char");
1292
pg_statio_all_tables | SELECT c.oid AS relid, n.nspname AS schemaname, c.relname, (pg_stat_get_blocks_fetched(c.oid) - pg_stat_get_blocks_hit(c.oid)) AS heap_blks_read, pg_stat_get_blocks_hit(c.oid) AS heap_blks_hit, sum((pg_stat_get_blocks_fetched(i.indexrelid) - pg_stat_get_blocks_hit(i.indexrelid))) AS idx_blks_read, sum(pg_stat_get_blocks_hit(i.indexrelid)) AS idx_blks_hit, (pg_stat_get_blocks_fetched(t.oid) - pg_stat_get_blocks_hit(t.oid)) AS toast_blks_read, pg_stat_get_blocks_hit(t.oid) AS toast_blks_hit, (pg_stat_get_blocks_fetched(x.oid) - pg_stat_get_blocks_hit(x.oid)) AS tidx_blks_read, pg_stat_get_blocks_hit(x.oid) AS tidx_blks_hit FROM ((((pg_class c LEFT JOIN pg_index i ON ((c.oid = i.indrelid))) LEFT JOIN pg_class t ON ((c.reltoastrelid = t.oid))) LEFT JOIN pg_class x ON ((t.reltoastidxid = x.oid))) LEFT JOIN pg_namespace n ON ((n.oid = c.relnamespace))) WHERE (c.relkind = 'r'::"char") GROUP BY c.oid, n.nspname, c.relname, t.oid, x.oid;
1293
pg_statio_sys_indexes | SELECT pg_statio_all_indexes.relid, pg_statio_all_indexes.indexrelid, pg_statio_all_indexes.schemaname, pg_statio_all_indexes.relname, pg_statio_all_indexes.indexrelname, pg_statio_all_indexes.idx_blks_read, pg_statio_all_indexes.idx_blks_hit FROM pg_statio_all_indexes WHERE (((pg_statio_all_indexes.schemaname = 'pg_catalog'::name) OR (pg_statio_all_indexes.schemaname = 'pg_toast'::name)) OR (pg_statio_all_indexes.schemaname = 'information_schema'::name));
1294
pg_statio_sys_sequences | SELECT pg_statio_all_sequences.relid, pg_statio_all_sequences.schemaname, pg_statio_all_sequences.relname, pg_statio_all_sequences.blks_read, pg_statio_all_sequences.blks_hit FROM pg_statio_all_sequences WHERE (((pg_statio_all_sequences.schemaname = 'pg_catalog'::name) OR (pg_statio_all_sequences.schemaname = 'pg_toast'::name)) OR (pg_statio_all_sequences.schemaname = 'information_schema'::name));
1295
pg_statio_sys_tables | SELECT pg_statio_all_tables.relid, pg_statio_all_tables.schemaname, pg_statio_all_tables.relname, pg_statio_all_tables.heap_blks_read, pg_statio_all_tables.heap_blks_hit, pg_statio_all_tables.idx_blks_read, pg_statio_all_tables.idx_blks_hit, pg_statio_all_tables.toast_blks_read, pg_statio_all_tables.toast_blks_hit, pg_statio_all_tables.tidx_blks_read, pg_statio_all_tables.tidx_blks_hit FROM pg_statio_all_tables WHERE (((pg_statio_all_tables.schemaname = 'pg_catalog'::name) OR (pg_statio_all_tables.schemaname = 'pg_toast'::name)) OR (pg_statio_all_tables.schemaname = 'information_schema'::name));
1296
pg_statio_user_indexes | SELECT pg_statio_all_indexes.relid, pg_statio_all_indexes.indexrelid, pg_statio_all_indexes.schemaname, pg_statio_all_indexes.relname, pg_statio_all_indexes.indexrelname, pg_statio_all_indexes.idx_blks_read, pg_statio_all_indexes.idx_blks_hit FROM pg_statio_all_indexes WHERE (((pg_statio_all_indexes.schemaname <> 'pg_catalog'::name) AND (pg_statio_all_indexes.schemaname <> 'pg_toast'::name)) AND (pg_statio_all_indexes.schemaname <> 'information_schema'::name));
1297
pg_statio_user_sequences | SELECT pg_statio_all_sequences.relid, pg_statio_all_sequences.schemaname, pg_statio_all_sequences.relname, pg_statio_all_sequences.blks_read, pg_statio_all_sequences.blks_hit FROM pg_statio_all_sequences WHERE (((pg_statio_all_sequences.schemaname <> 'pg_catalog'::name) AND (pg_statio_all_sequences.schemaname <> 'pg_toast'::name)) AND (pg_statio_all_sequences.schemaname <> 'information_schema'::name));
1298
pg_statio_user_tables | SELECT pg_statio_all_tables.relid, pg_statio_all_tables.schemaname, pg_statio_all_tables.relname, pg_statio_all_tables.heap_blks_read, pg_statio_all_tables.heap_blks_hit, pg_statio_all_tables.idx_blks_read, pg_statio_all_tables.idx_blks_hit, pg_statio_all_tables.toast_blks_read, pg_statio_all_tables.toast_blks_hit, pg_statio_all_tables.tidx_blks_read, pg_statio_all_tables.tidx_blks_hit FROM pg_statio_all_tables WHERE (((pg_statio_all_tables.schemaname <> 'pg_catalog'::name) AND (pg_statio_all_tables.schemaname <> 'pg_toast'::name)) AND (pg_statio_all_tables.schemaname <> 'information_schema'::name));
1299
pg_stats | SELECT n.nspname AS schemaname, c.relname AS tablename, a.attname, s.stanullfrac AS null_frac, s.stawidth AS avg_width, s.stadistinct AS n_distinct, CASE 1 WHEN s.stakind1 THEN s.stavalues1 WHEN s.stakind2 THEN s.stavalues2 WHEN s.stakind3 THEN s.stavalues3 WHEN s.stakind4 THEN s.stavalues4 ELSE NULL::"unknown" END AS most_common_vals, CASE 1 WHEN s.stakind1 THEN s.stanumbers1 WHEN s.stakind2 THEN s.stanumbers2 WHEN s.stakind3 THEN s.stanumbers3 WHEN s.stakind4 THEN s.stanumbers4 ELSE NULL::real[] END AS most_common_freqs, CASE 2 WHEN s.stakind1 THEN s.stavalues1 WHEN s.stakind2 THEN s.stavalues2 WHEN s.stakind3 THEN s.stavalues3 WHEN s.stakind4 THEN s.stavalues4 ELSE NULL::"unknown" END AS histogram_bounds, CASE 3 WHEN s.stakind1 THEN s.stanumbers1[1] WHEN s.stakind2 THEN s.stanumbers2[1] WHEN s.stakind3 THEN s.stanumbers3[1] WHEN s.stakind4 THEN s.stanumbers4[1] ELSE NULL::real END AS correlation FROM (((pg_statistic s JOIN pg_class c ON ((c.oid = s.starelid))) JOIN pg_attribute a ON (((c.oid = a.attrelid) AND (a.attnum = s.staattnum)))) LEFT JOIN pg_namespace n ON ((n.oid = c.relnamespace))) WHERE has_table_privilege(c.oid, 'select'::text);
1300
pg_tables | SELECT n.nspname AS schemaname, c.relname AS tablename, pg_get_userbyid(c.relowner) AS tableowner, t.spcname AS "tablespace", c.relhasindex AS hasindexes, c.relhasrules AS hasrules, (c.reltriggers > 0) AS hastriggers FROM ((pg_class c LEFT JOIN pg_namespace n ON ((n.oid = c.relnamespace))) LEFT JOIN pg_tablespace t ON ((t.oid = c.reltablespace))) WHERE (c.relkind = 'r'::"char");
1301
pg_user | SELECT pg_shadow.usename, pg_shadow.usesysid, pg_shadow.usecreatedb, pg_shadow.usesuper, pg_shadow.usecatupd, '********'::text AS passwd, pg_shadow.valuntil, pg_shadow.useconfig FROM pg_shadow;
1302
pg_views | SELECT n.nspname AS schemaname, c.relname AS viewname, pg_get_userbyid(c.relowner) AS viewowner, pg_get_viewdef(c.oid) AS definition FROM (pg_class c LEFT JOIN pg_namespace n ON ((n.oid = c.relnamespace))) WHERE (c.relkind = 'v'::"char");
1303
rtest_v1 | SELECT rtest_t1.a, rtest_t1.b FROM rtest_t1;
1304
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);
1305
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)));
1306
rtest_vview2 | SELECT rtest_view1.a, rtest_view1.b FROM rtest_view1 WHERE rtest_view1.v;
1307
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)));
1308
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;
1309
rtest_vview5 | SELECT rtest_view1.a, rtest_view1.b, rtest_viewfunc1(rtest_view1.a) AS refcount FROM rtest_view1;
1310
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);
1311
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));
1312
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);
1313
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);
1314
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))));
1315
street | SELECT r.name, r.thepath, c.cname FROM ONLY road r, real_city c WHERE (c.outline ## r.thepath);
1316
toyemp | SELECT emp.name, emp.age, emp."location", (12 * emp.salary) AS annualsal FROM emp;
1319
SELECT tablename, rulename, definition FROM pg_rules
1320
ORDER BY tablename, rulename;
1321
tablename | rulename | definition
1322
---------------+-----------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1323
pg_settings | pg_settings_n | CREATE RULE pg_settings_n AS ON UPDATE TO pg_settings DO INSTEAD NOTHING;
1324
pg_settings | pg_settings_u | CREATE RULE pg_settings_u AS ON UPDATE TO pg_settings WHERE (new.name = old.name) DO SELECT set_config(old.name, new.setting, false) AS set_config;
1325
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);
1326
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);
1327
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);
1328
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 NOTHING;
1329
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;
1330
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);
1331
rtest_nothn2 | rtest_nothn_r4 | CREATE RULE rtest_nothn_r4 AS ON INSERT TO rtest_nothn2 DO INSTEAD NOTHING;
1332
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 1st'::text);
1333
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 2nd'::text);
1334
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'::text);
1335
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 4th'::text);
1336
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);
1337
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);
1338
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); );
1339
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); );
1340
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);
1341
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);
1342
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);
1343
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);
1344
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);
1345
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);
1346
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);
1347
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);
1348
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);
1349
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);
1350
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, 'Thu Jan 01 00:00:00 1970'::timestamp without time zone);
1351
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);
1355
-- CREATE OR REPLACE RULE
1357
CREATE TABLE ruletest_tbl (a int, b int);
1358
CREATE TABLE ruletest_tbl2 (a int, b int);
1359
CREATE OR REPLACE RULE myrule AS ON INSERT TO ruletest_tbl
1360
DO INSTEAD INSERT INTO ruletest_tbl2 VALUES (10, 10);
1361
INSERT INTO ruletest_tbl VALUES (99, 99);
1362
CREATE OR REPLACE RULE myrule AS ON INSERT TO ruletest_tbl
1363
DO INSTEAD INSERT INTO ruletest_tbl2 VALUES (1000, 1000);
1364
INSERT INTO ruletest_tbl VALUES (99, 99);
1365
SELECT * FROM ruletest_tbl2;
1372
-- Check that rewrite rules splitting one INSERT into multiple
1373
-- conditional statements does not disable FK checking.
1374
create table rule_and_refint_t1 (
1378
primary key (id1a, id1b)
1380
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "rule_and_refint_t1_pkey" for table "rule_and_refint_t1"
1381
create table rule_and_refint_t2 (
1385
primary key (id2a, id2c)
1387
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "rule_and_refint_t2_pkey" for table "rule_and_refint_t2"
1388
create table rule_and_refint_t3 (
1393
primary key (id3a, id3b, id3c),
1394
foreign key (id3a, id3b) references rule_and_refint_t1 (id1a, id1b),
1395
foreign key (id3a, id3c) references rule_and_refint_t2 (id2a, id2c)
1397
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "rule_and_refint_t3_pkey" for table "rule_and_refint_t3"
1398
insert into rule_and_refint_t1 values (1, 11);
1399
insert into rule_and_refint_t1 values (1, 12);
1400
insert into rule_and_refint_t1 values (2, 21);
1401
insert into rule_and_refint_t1 values (2, 22);
1402
insert into rule_and_refint_t2 values (1, 11);
1403
insert into rule_and_refint_t2 values (1, 12);
1404
insert into rule_and_refint_t2 values (2, 21);
1405
insert into rule_and_refint_t2 values (2, 22);
1406
insert into rule_and_refint_t3 values (1, 11, 11, 'row1');
1407
insert into rule_and_refint_t3 values (1, 11, 12, 'row2');
1408
insert into rule_and_refint_t3 values (1, 12, 11, 'row3');
1409
insert into rule_and_refint_t3 values (1, 12, 12, 'row4');
1410
insert into rule_and_refint_t3 values (1, 11, 13, 'row5');
1411
ERROR: insert or update on table "rule_and_refint_t3" violates foreign key constraint "rule_and_refint_t3_id3a_fkey1"
1412
DETAIL: Key (id3a,id3c)=(1,13) is not present in table "rule_and_refint_t2".
1413
insert into rule_and_refint_t3 values (1, 13, 11, 'row6');
1414
ERROR: insert or update on table "rule_and_refint_t3" violates foreign key constraint "rule_and_refint_t3_id3a_fkey"
1415
DETAIL: Key (id3a,id3b)=(1,13) is not present in table "rule_and_refint_t1".
1416
create rule rule_and_refint_t3_ins as on insert to rule_and_refint_t3
1417
where (exists (select 1 from rule_and_refint_t3
1418
where (((rule_and_refint_t3.id3a = new.id3a)
1419
and (rule_and_refint_t3.id3b = new.id3b))
1420
and (rule_and_refint_t3.id3c = new.id3c))))
1421
do instead update rule_and_refint_t3 set data = new.data
1422
where (((rule_and_refint_t3.id3a = new.id3a)
1423
and (rule_and_refint_t3.id3b = new.id3b))
1424
and (rule_and_refint_t3.id3c = new.id3c));
1425
insert into rule_and_refint_t3 values (1, 11, 13, 'row7');
1426
ERROR: insert or update on table "rule_and_refint_t3" violates foreign key constraint "rule_and_refint_t3_id3a_fkey1"
1427
DETAIL: Key (id3a,id3c)=(1,13) is not present in table "rule_and_refint_t2".
1428
insert into rule_and_refint_t3 values (1, 13, 11, 'row8');
1429
ERROR: insert or update on table "rule_and_refint_t3" violates foreign key constraint "rule_and_refint_t3_id3a_fkey"
1430
DETAIL: Key (id3a,id3b)=(1,13) is not present in table "rule_and_refint_t1".
1432
-- check for planner problems with complex inherited UPDATES
1434
create table id (id serial primary key, name text);
1435
NOTICE: CREATE TABLE will create implicit sequence "id_id_seq" for serial column "id.id"
1436
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "id_pkey" for table "id"
1437
-- currently, must respecify PKEY for each inherited subtable
1438
create table test_1 (id integer primary key) inherits (id);
1439
NOTICE: merging column "id" with inherited definition
1440
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "test_1_pkey" for table "test_1"
1441
create table test_2 (id integer primary key) inherits (id);
1442
NOTICE: merging column "id" with inherited definition
1443
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "test_2_pkey" for table "test_2"
1444
create table test_3 (id integer primary key) inherits (id);
1445
NOTICE: merging column "id" with inherited definition
1446
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "test_3_pkey" for table "test_3"
1447
insert into test_1 (name) values ('Test 1');
1448
insert into test_1 (name) values ('Test 2');
1449
insert into test_2 (name) values ('Test 3');
1450
insert into test_2 (name) values ('Test 4');
1451
insert into test_3 (name) values ('Test 5');
1452
insert into test_3 (name) values ('Test 6');
1453
create view id_ordered as select * from id order by id;
1454
create rule update_id_ordered as on update to id_ordered
1455
do instead update id set name = new.name where id = old.id;
1456
select * from id_ordered;
1467
update id_ordered set name = 'update 2' where id = 2;
1468
update id_ordered set name = 'update 4' where id = 4;
1469
update id_ordered set name = 'update 5' where id = 5;
1470
select * from id_ordered;
1481
set client_min_messages to warning; -- suppress cascade notices
1482
drop table id cascade;