]> granicus.if.org Git - postgresql/blob - src/test/regress/expected/rules.out
Extend pg_cast castimplicit column to a three-way value; this allows us
[postgresql] / src / test / regress / expected / rules.out
1 --
2 -- RULES
3 -- From Jan's original setup_ruletest.sql and run_ruletest.sql
4 -- - thomas 1998-09-13
5 --
6 --
7 -- Tables and rules for the view test
8 --
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
17         where a = old.a;
18 create rule rtest_v1_del as on delete to rtest_v1 do instead
19         delete from rtest_t1 where a = old.a;
20 --
21 -- Tables and rules for the constraint update/delete test
22 --
23 -- Note:
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).
27 --
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
37         );
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;
41         );
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;
46 --
47 -- Tables and rules for the logging test
48 --
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);
61 --
62 -- Tables and rules for the multiple cascaded qualified instead
63 -- rule test 
64 --
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
78                 where new.a > 15 do
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);
83 --
84 -- Tables and rules for the rule fire order test
85 --
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');
102 --
103 -- Tables and rules for the instead nothing test
104 --
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 nothing;
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
117         do instead nothing;
118 --
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.
122 --
123 --
124 -- We need test date later
125 --
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);
134 -- insert values
135 insert into rtest_v1 values (1, 11);
136 insert into rtest_v1 values (2, 12);
137 select * from rtest_v1;
138  a | b  
139 ---+----
140  1 | 11
141  2 | 12
142 (2 rows)
143
144 -- delete with constant expression
145 delete from rtest_v1 where a = 1;
146 select * from rtest_v1;
147  a | b  
148 ---+----
149  2 | 12
150 (1 row)
151
152 insert into rtest_v1 values (1, 11);
153 delete from rtest_v1 where b = 12;
154 select * from rtest_v1;
155  a | b  
156 ---+----
157  1 | 11
158 (1 row)
159
160 insert into rtest_v1 values (2, 12);
161 insert into rtest_v1 values (2, 13);
162 select * from rtest_v1;
163  a | b  
164 ---+----
165  1 | 11
166  2 | 12
167  2 | 13
168 (3 rows)
169
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
173 \p
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
177 \r
178 delete from rtest_v1 where b = 12;
179 select * from rtest_v1;
180  a | b  
181 ---+----
182  1 | 11
183 (1 row)
184
185 delete from rtest_v1;
186 -- insert select
187 insert into rtest_v1 select * from rtest_t2;
188 select * from rtest_v1;
189  a | b  
190 ---+----
191  1 | 21
192  2 | 22
193  3 | 23
194 (3 rows)
195
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;
200  a | b  
201 ---+----
202  1 | 21
203  2 | 22
204  3 | 23
205 (3 rows)
206
207 -- now with only one target attribute
208 insert into rtest_v1 (a) select a from rtest_t3;
209 select * from rtest_v1;
210  a | b  
211 ---+----
212  1 | 21
213  2 | 22
214  3 | 23
215  1 |   
216  2 |   
217  3 |   
218  4 |   
219  5 |   
220 (8 rows)
221
222 select * from rtest_v1 where b isnull;
223  a | b 
224 ---+---
225  1 |  
226  2 |  
227  3 |  
228  4 |  
229  5 |  
230 (5 rows)
231
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;
236  a | b  
237 ---+----
238  1 | 21
239  2 | 22
240  3 | 23
241 (3 rows)
242
243 -- now updates with constant expression
244 update rtest_v1 set b = 42 where a = 2;
245 select * from rtest_v1;
246  a | b  
247 ---+----
248  1 | 21
249  3 | 23
250  2 | 42
251 (3 rows)
252
253 update rtest_v1 set b = 99 where b = 42;
254 select * from rtest_v1;
255  a | b  
256 ---+----
257  1 | 21
258  3 | 23
259  2 | 99
260 (3 rows)
261
262 update rtest_v1 set b = 88 where b < 50;
263 select * from rtest_v1;
264  a | b  
265 ---+----
266  2 | 99
267  1 | 88
268  3 | 88
269 (3 rows)
270
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;
274  a | b  
275 ---+----
276  1 | 31
277  2 | 32
278  3 | 33
279 (3 rows)
280
281 -- updates in a mergejoin
282 update rtest_v1 set b = rtest_t2.b where a = rtest_t2.a;
283 select * from rtest_v1;
284  a | b  
285 ---+----
286  1 | 21
287  2 | 22
288  3 | 23
289 (3 rows)
290
291 insert into rtest_v1 select * from rtest_t3;
292 select * from rtest_v1;
293  a | b  
294 ---+----
295  1 | 21
296  2 | 22
297  3 | 23
298  1 | 31
299  2 | 32
300  3 | 33
301  4 | 34
302  5 | 35
303 (8 rows)
304
305 update rtest_t1 set a = a + 10 where b > 30;
306 select * from rtest_v1;
307  a  | b  
308 ----+----
309   1 | 21
310   2 | 22
311   3 | 23
312  11 | 31
313  12 | 32
314  13 | 33
315  14 | 34
316  15 | 35
317 (8 rows)
318
319 update rtest_v1 set a = rtest_t3.a + 20 where b = rtest_t3.b;
320 select * from rtest_v1;
321  a  | b  
322 ----+----
323   1 | 21
324   2 | 22
325   3 | 23
326  21 | 31
327  22 | 32
328  23 | 33
329  24 | 34
330  25 | 35
331 (8 rows)
332
333 --
334 -- Test for constraint updates/deletes
335 --
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;
350  sysname | ifname 
351 ---------+--------
352  orion   | eth0
353  orion   | eth1
354  notjw   | eth0
355  pluto   | eth0
356 (4 rows)
357
358 select * from rtest_admin;
359  pname | sysname 
360 -------+---------
361  jw    | orion
362  jw    | notjw
363  bm    | pluto
364 (3 rows)
365
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;
371  pname  | sysname 
372 --------+---------
373  bm     | pluto
374  jwieck | notjw
375  jwieck | orion
376 (3 rows)
377
378 delete from rtest_system where sysname = 'orion';
379 select * from rtest_interface;
380  sysname | ifname 
381 ---------+--------
382  notjw   | eth0
383  pluto   | eth0
384 (2 rows)
385
386 select * from rtest_admin;
387  pname  | sysname 
388 --------+---------
389  bm     | pluto
390  jwieck | notjw
391 (2 rows)
392
393 --
394 -- Rule qualification test
395 --
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
410 (5 rows)
411
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
427 (8 rows)
428
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
445 (11 rows)
446
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
465 (14 rows)
466
467 --
468 -- Multiple cascaded qualified instead rule test
469 --
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;
481  a  |                  b                  
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
490 (7 rows)
491
492 select * from rtest_t5;
493  a  |                  b                  
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
498 (3 rows)
499
500 select * from rtest_t6;
501  a  |                  b                  
502 ----+-------------------------------------
503  20 | Record should go to rtest_t4 and t6
504 (1 row)
505
506 select * from rtest_t7;
507  a  |                  b                  
508 ----+-------------------------------------
509  19 | Record should go to rtest_t5 and t7
510 (1 row)
511
512 select * from rtest_t8;
513  a  |                  b                  
514 ----+-------------------------------------
515  26 | Record should go to rtest_t4 and t8
516  28 | Record should go to rtest_t4 and t8
517 (2 rows)
518
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;
536  a |              b               
537 ---+------------------------------
538  1 | Record should go to rtest_t4
539  2 | Record should go to rtest_t4
540 (2 rows)
541
542 select * from rtest_t5;
543  a  |                  b                  
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
548 (3 rows)
549
550 select * from rtest_t6;
551  a | b 
552 ---+---
553 (0 rows)
554
555 select * from rtest_t7;
556  a  |                  b                  
557 ----+-------------------------------------
558  19 | Record should go to rtest_t5 and t7
559 (1 row)
560
561 select * from rtest_t8;
562  a | b 
563 ---+---
564 (0 rows)
565
566 insert into rtest_t4 select * from rtest_t9 where b ~ 'and t8';
567 select * from rtest_t4;
568  a  |                  b                  
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
574 (4 rows)
575
576 select * from rtest_t5;
577  a  |                  b                  
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
582 (3 rows)
583
584 select * from rtest_t6;
585  a | b 
586 ---+---
587 (0 rows)
588
589 select * from rtest_t7;
590  a  |                  b                  
591 ----+-------------------------------------
592  19 | Record should go to rtest_t5 and t7
593 (1 row)
594
595 select * from rtest_t8;
596  a  |                  b                  
597 ----+-------------------------------------
598  26 | Record should go to rtest_t4 and t8
599  28 | Record should go to rtest_t4 and t8
600 (2 rows)
601
602 insert into rtest_t4 select a + 1, b from rtest_t9 where a in (20, 30, 40);
603 select * from rtest_t4;
604  a  |                  b                  
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
613 (7 rows)
614
615 select * from rtest_t5;
616  a  |                  b                  
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
621 (3 rows)
622
623 select * from rtest_t6;
624  a  |                  b                  
625 ----+-------------------------------------
626  21 | Record should go to rtest_t4 and t6
627 (1 row)
628
629 select * from rtest_t7;
630  a  |                  b                  
631 ----+-------------------------------------
632  19 | Record should go to rtest_t5 and t7
633 (1 row)
634
635 select * from rtest_t8;
636  a  |                  b                  
637 ----+-------------------------------------
638  26 | Record should go to rtest_t4 and t8
639  28 | Record should go to rtest_t4 and t8
640 (2 rows)
641
642 --
643 -- Check that the ordering of rules fired is correct
644 --
645 insert into rtest_order1 values (1);
646 select * from rtest_order2;
647  a | b |                  c                  
648 ---+---+-------------------------------------
649  1 | 1 | rule 2 - this should run 1st
650  1 | 2 | rule 4 - this should run 2nd
651  1 | 3 | rule 1 - this should run 3rd or 4th
652  1 | 4 | rule 3 - this should run 3rd or 4th
653 (4 rows)
654
655 --
656 -- Check if instead nothing w/without qualification works
657 --
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;
670  a  |     b     
671 ----+-----------
672   1 | want this
673   2 | want this
674  20 | want this
675  29 | want this
676  40 | want this
677  50 | want this
678  60 | want this
679 (7 rows)
680
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;
686  a | b 
687 ---+---
688 (0 rows)
689
690 select * from rtest_nothn3;
691   a  | b  
692 -----+----
693  100 | OK
694  200 | OK
695 (2 rows)
696
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;
713  a  |     b     
714 ----+-----------
715   1 | want this
716   2 | want this
717  20 | want this
718  29 | want this
719  40 | want this
720  50 | want this
721  60 | want this
722 (7 rows)
723
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;
731  a | b 
732 ---+---
733 (0 rows)
734
735 select * from rtest_nothn3;
736   a  | b  
737 -----+----
738  100 | OK
739  200 | OK
740 (2 rows)
741
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
753         where X.a = Y.a
754         group by X.a, X.b;
755 create function rtest_viewfunc1(int4) returns int4 as
756         'select count(*)::int4 from rtest_view2 where a = $1'
757         language 'sql';
758 create view rtest_vview5 as select a, b, rtest_viewfunc1(a) as refcount
759         from rtest_view1;
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;
777  a |   b    
778 ---+--------
779  2 | item 2
780  4 | item 4
781  5 | item 5
782  7 | item 7
783 (4 rows)
784
785 select * from rtest_vview2;
786  a |   b    
787 ---+--------
788  1 | item 1
789  2 | item 2
790  3 | item 3
791  5 | item 5
792  7 | item 7
793  8 | item 8
794 (6 rows)
795
796 select * from rtest_vview3;
797  a |   b    
798 ---+--------
799  2 | item 2
800  5 | item 5
801  7 | item 7
802 (3 rows)
803
804 select * from rtest_vview4;
805  a |   b    | refcount 
806 ---+--------+----------
807  2 | item 2 |        2
808  4 | item 4 |        1
809  5 | item 5 |        1
810  7 | item 7 |        4
811 (4 rows)
812
813 select * from rtest_vview5;
814  a |   b    | refcount 
815 ---+--------+----------
816  1 | item 1 |        0
817  2 | item 2 |        2
818  3 | item 3 |        0
819  4 | item 4 |        1
820  5 | item 5 |        1
821  6 | item 6 |        0
822  7 | item 7 |        4
823  8 | item 8 |        0
824 (8 rows)
825
826 insert into rtest_view3 select * from rtest_vview1 where a < 7;
827 select * from rtest_view3;
828  a |   b    
829 ---+--------
830  2 | item 2
831  4 | item 4
832  5 | item 5
833 (3 rows)
834
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;
838  a |   b    
839 ---+--------
840  1 | item 1
841  3 | item 3
842  7 | item 7
843  8 | item 8
844 (4 rows)
845
846 delete from rtest_view3;
847 insert into rtest_view3 select * from rtest_vview3;
848 select * from rtest_view3;
849  a |   b    
850 ---+--------
851  2 | item 2
852  5 | item 5
853  7 | item 7
854 (3 rows)
855
856 delete from rtest_view3;
857 insert into rtest_view4 select * from rtest_vview4 where 3 > refcount;
858 select * from rtest_view4;
859  a |   b    | c 
860 ---+--------+---
861  2 | item 2 | 2
862  4 | item 4 | 1
863  5 | item 5 | 1
864 (3 rows)
865
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;
869  a |   b    | c 
870 ---+--------+---
871  3 | item 3 | 0
872  6 | item 6 | 0
873  8 | item 8 | 0
874 (3 rows)
875
876 delete from rtest_view4;
877 --
878 -- Test for computations in views
879 --
880 create table rtest_comp (
881         part    text,
882         unit    char(4),
883         size    float
884 );
885 create table rtest_unitfact (
886         unit    char(4),
887         factor  float
888 );
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;
903  part | size_in_cm 
904 ------+------------
905  p1   |        500
906  p2   |        300
907  p3   |          5
908  p4   |         15
909  p5   |      17.78
910  p6   |     11.176
911 (6 rows)
912
913 select * from rtest_vcomp where size_in_cm > 10.0 order by size_in_cm using >;
914  part | size_in_cm 
915 ------+------------
916  p1   |        500
917  p2   |        300
918  p5   |      17.78
919  p4   |         15
920  p6   |     11.176
921 (5 rows)
922
923 --
924 -- In addition run the (slightly modified) queries from the
925 -- programmers manual section on the rule system.
926 --
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
934 );
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
941 );
942 CREATE TABLE unit (
943         un_name    char(8),       -- the primary key
944         un_fact    float          -- factor to transform to cm
945 );
946 CREATE VIEW shoe AS
947         SELECT sh.shoename,
948                    sh.sh_avail,
949                    sh.slcolor,
950                    sh.slminlen,
951                    sh.slminlen * un.un_fact AS slminlen_cm,
952                    sh.slmaxlen,
953                    sh.slmaxlen * un.un_fact AS slmaxlen_cm,
954                    sh.slunit
955           FROM shoe_data sh, unit un
956          WHERE sh.slunit = un.un_name;
957 CREATE VIEW shoelace AS
958         SELECT s.sl_name,
959                    s.sl_avail,
960                    s.sl_color,
961                    s.sl_len,
962                    s.sl_unit,
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
967         SELECT rsh.shoename,
968                    rsh.sh_avail,
969                    rsl.sl_name,
970                    rsl.sl_avail,
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');
991 -- SELECTs in doc
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
1003 (8 rows)
1004
1005 SELECT * FROM shoe_ready WHERE total_avail >= 2 ORDER BY 1;
1006   shoename  | sh_avail |  sl_name   | sl_avail | total_avail 
1007 ------------+----------+------------+----------+-------------
1008  sh1        |        2 | sl1        |        5 |           2
1009  sh3        |        4 | sl7        |        7 |           4
1010 (2 rows)
1011
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   timestamp      -- when
1017     );
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 (
1024                                         NEW.sl_name,
1025                                         NEW.sl_avail,
1026                                         'Al Bundy',
1027                                         'epoch'
1028                                     );
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 | Thu Jan 01 00:00:00 1970
1034 (1 row)
1035
1036     CREATE RULE shoelace_ins AS ON INSERT TO shoelace
1037         DO INSTEAD
1038         INSERT INTO shoelace_data VALUES (
1039                NEW.sl_name,
1040                NEW.sl_avail,
1041                NEW.sl_color,
1042                NEW.sl_len,
1043                NEW.sl_unit);
1044     CREATE RULE shoelace_upd AS ON UPDATE TO shoelace
1045         DO INSTEAD
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
1054         DO INSTEAD
1055         DELETE FROM shoelace_data
1056          WHERE sl_name = OLD.sl_name;
1057     CREATE TABLE shoelace_arrive (
1058         arr_name    char(10),
1059         arr_quant   integer
1060     );
1061     CREATE TABLE shoelace_ok (
1062         ok_name     char(10),
1063         ok_quant    integer
1064     );
1065     CREATE RULE shoelace_ok_ins AS ON INSERT TO shoelace_ok
1066         DO INSTEAD
1067         UPDATE shoelace SET
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
1084 (8 rows)
1085
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
1098 (8 rows)
1099
1100 SELECT * FROM shoelace_log ORDER BY sl_name;
1101   sl_name   | sl_avail | log_who  |         log_when         
1102 ------------+----------+----------+--------------------------
1103  sl3        |       10 | Al Bundy | Thu Jan 01 00:00:00 1970
1104  sl6        |       20 | Al Bundy | Thu Jan 01 00:00:00 1970
1105  sl7        |        6 | Al Bundy | Thu Jan 01 00:00:00 1970
1106  sl8        |       21 | Al Bundy | Thu Jan 01 00:00:00 1970
1107 (4 rows)
1108
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
1121 (2 rows)
1122
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
1127 (1 row)
1128
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
1144 (9 rows)
1145
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    
1153 (4 rows)
1154
1155 SELECT count(*) FROM shoe;
1156  count 
1157 -------
1158      4
1159 (1 row)
1160
1161 --
1162 -- Simple test of qualified ON INSERT ... this did not work in 7.0 ...
1163 --
1164 create table foo (f1 int);
1165 create table foo2 (f1 int);
1166 create rule foorule as on insert to foo where f1 < 100
1167 do instead nothing;
1168 insert into foo values(1);
1169 insert into foo values(1001);
1170 select * from foo;
1171   f1  
1172 ------
1173  1001
1174 (1 row)
1175
1176 drop rule foorule on foo;
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);
1186 select * from foo;
1187   f1  
1188 ------
1189  1001
1190   100
1191 (2 rows)
1192
1193 select * from foo2;
1194  f1 
1195 ----
1196   2
1197 (1 row)
1198
1199 drop rule foorule on foo;
1200 drop table foo;
1201 drop table foo2;
1202 --
1203 -- Test rules containing INSERT ... SELECT, which is a very ugly special
1204 -- case as of 7.1.  Example is based on bug report from Joel Burton.
1205 --
1206 create table pparent (pid int, txt text);
1207 insert into pparent values (1,'parent1');
1208 insert into pparent values (2,'parent2');
1209 create table cchild (pid int, descrip text);
1210 insert into cchild values (1,'descrip1');
1211 create view vview as
1212   select pparent.pid, txt, descrip from
1213     pparent left join cchild using (pid);
1214 create rule rrule as
1215   on update to vview do instead
1216 (
1217   insert into cchild (pid, descrip)
1218     select old.pid, new.descrip where old.descrip isnull; 
1219   update cchild set descrip = new.descrip where cchild.pid = old.pid;
1220 );
1221 select * from vview;
1222  pid |   txt   | descrip  
1223 -----+---------+----------
1224    1 | parent1 | descrip1
1225    2 | parent2 | 
1226 (2 rows)
1227
1228 update vview set descrip='test1' where pid=1;
1229 select * from vview;
1230  pid |   txt   | descrip 
1231 -----+---------+---------
1232    1 | parent1 | test1
1233    2 | parent2 | 
1234 (2 rows)
1235
1236 update vview set descrip='test2' where pid=2;
1237 select * from vview;
1238  pid |   txt   | descrip 
1239 -----+---------+---------
1240    1 | parent1 | test1
1241    2 | parent2 | test2
1242 (2 rows)
1243
1244 update vview set descrip='test3' where pid=3;
1245 select * from vview;
1246  pid |   txt   | descrip 
1247 -----+---------+---------
1248    1 | parent1 | test1
1249    2 | parent2 | test2
1250 (2 rows)
1251
1252 select * from cchild;
1253  pid | descrip 
1254 -----+---------
1255    1 | test1
1256    2 | test2
1257 (2 rows)
1258
1259 drop rule rrule on vview;
1260 drop view vview;
1261 drop table pparent;
1262 drop table cchild;
1263 --
1264 -- Check that ruleutils are working
1265 --
1266 SELECT viewname, definition FROM pg_views ORDER BY viewname;
1267          viewname         |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   definition                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
1268 --------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1269  iexit                    | SELECT ih.name, ih.thepath, interpt_pp(ih.thepath, r.thepath) AS exit FROM ihighway ih, ramp r WHERE (ih.thepath ## r.thepath);
1270  pg_indexes               | SELECT n.nspname AS schemaname, c.relname AS tablename, i.relname AS indexname, 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))) WHERE ((c.relkind = 'r'::"char") AND (i.relkind = 'i'::"char"));
1271  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);
1272  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);
1273  pg_settings              | SELECT a.name, a.setting FROM pg_show_all_settings() a(name text, setting text);
1274  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 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));
1275  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");
1276  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;
1277  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;
1278  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));
1279  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));
1280  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));
1281  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));
1282  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");
1283  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");
1284  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;
1285  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));
1286  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));
1287  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));
1288  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));
1289  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));
1290  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));
1291  pg_stats                 | SELECT nspname AS schemaname, relname AS tablename, attname, stanullfrac AS null_frac, stawidth AS avg_width, stadistinct AS n_distinct, CASE WHEN (1 = stakind1) THEN stavalues1 WHEN (1 = stakind2) THEN stavalues2 WHEN (1 = stakind3) THEN stavalues3 WHEN (1 = stakind4) THEN stavalues4 ELSE NULL::text[] END AS most_common_vals, CASE WHEN (1 = stakind1) THEN stanumbers1 WHEN (1 = stakind2) THEN stanumbers2 WHEN (1 = stakind3) THEN stanumbers3 WHEN (1 = stakind4) THEN stanumbers4 ELSE NULL::real[] END AS most_common_freqs, CASE WHEN (2 = stakind1) THEN stavalues1 WHEN (2 = stakind2) THEN stavalues2 WHEN (2 = stakind3) THEN stavalues3 WHEN (2 = stakind4) THEN stavalues4 ELSE NULL::text[] END AS histogram_bounds, CASE WHEN (3 = stakind1) THEN stanumbers1[1] WHEN (3 = stakind2) THEN stanumbers2[1] WHEN (3 = stakind3) THEN stanumbers3[1] WHEN (3 = stakind4) THEN 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);
1292  pg_tables                | SELECT n.nspname AS schemaname, 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 LEFT JOIN pg_namespace n ON ((n.oid = c.relnamespace))) WHERE ((c.relkind = 'r'::"char") OR (c.relkind = 's'::"char"));
1293  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;
1294  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");
1295  rtest_v1                 | SELECT rtest_t1.a, rtest_t1.b FROM rtest_t1;
1296  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);
1297  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)));
1298  rtest_vview2             | SELECT rtest_view1.a, rtest_view1.b FROM rtest_view1 WHERE rtest_view1.v;
1299  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)));
1300  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;
1301  rtest_vview5             | SELECT rtest_view1.a, rtest_view1.b, rtest_viewfunc1(rtest_view1.a) AS refcount FROM rtest_view1;
1302  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);
1303  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));
1304  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);
1305  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);
1306  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))));
1307  street                   | SELECT r.name, r.thepath, c.cname FROM ONLY road r, real_city c WHERE (c.outline ## r.thepath);
1308  toyemp                   | SELECT emp.name, emp.age, emp."location", (12 * emp.salary) AS annualsal FROM emp;
1309 (40 rows)
1310
1311 SELECT tablename, rulename, definition FROM pg_rules 
1312         ORDER BY tablename, rulename;
1313    tablename   |    rulename     |                                                                                                                                  definition                                                                                                                                   
1314 ---------------+-----------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1315  pg_settings   | pg_settings_n   | CREATE RULE pg_settings_n AS ON UPDATE TO pg_settings DO INSTEAD NOTHING;
1316  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;
1317  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);
1318  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);
1319  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);
1320  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;
1321  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;
1322  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);
1323  rtest_nothn2  | rtest_nothn_r4  | CREATE RULE rtest_nothn_r4 AS ON INSERT TO rtest_nothn2 DO INSTEAD NOTHING;
1324  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);
1325  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);
1326  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);
1327  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);
1328  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);
1329  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);
1330  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); );
1331  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); );
1332  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);
1333  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);
1334  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);
1335  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);
1336  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);
1337  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);
1338  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);
1339  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);
1340  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);
1341  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);
1342  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);
1343  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);
1344 (29 rows)
1345
1346 --
1347 -- CREATE OR REPLACE RULE
1348 --
1349 CREATE TABLE ruletest_tbl (a int, b int);
1350 CREATE TABLE ruletest_tbl2 (a int, b int);
1351 CREATE OR REPLACE RULE myrule AS ON INSERT TO ruletest_tbl
1352         DO INSTEAD INSERT INTO ruletest_tbl2 VALUES (10, 10);
1353 INSERT INTO ruletest_tbl VALUES (99, 99);
1354 CREATE OR REPLACE RULE myrule AS ON INSERT TO ruletest_tbl
1355         DO INSTEAD INSERT INTO ruletest_tbl2 VALUES (1000, 1000);
1356 INSERT INTO ruletest_tbl VALUES (99, 99);
1357 SELECT * FROM ruletest_tbl2;
1358   a   |  b   
1359 ------+------
1360    10 |   10
1361  1000 | 1000
1362 (2 rows)
1363