]> granicus.if.org Git - postgresql/blob - src/test/regress/expected/rules.out
Repair two constraint-exclusion corner cases triggered by proving that an
[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 -- Test comments
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;
25 --
26 -- Tables and rules for the constraint update/delete test
27 --
28 -- Note:
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).
32 --
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
42         );
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;
46         );
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;
51 --
52 -- Tables and rules for the logging test
53 --
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);
66 --
67 -- Tables and rules for the multiple cascaded qualified instead
68 -- rule test 
69 --
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
83                 where new.a > 15 do
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);
88 --
89 -- Tables and rules for the rule fire order test
90 --
91 -- As of PG 7.3, the rules should fire in order by name, regardless
92 -- of INSTEAD attributes or creation order.
93 --
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');
110 --
111 -- Tables and rules for the instead nothing test
112 --
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
125         do instead nothing;
126 --
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.
130 --
131 --
132 -- We need test date later
133 --
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);
142 -- insert values
143 insert into rtest_v1 values (1, 11);
144 insert into rtest_v1 values (2, 12);
145 select * from rtest_v1;
146  a | b  
147 ---+----
148  1 | 11
149  2 | 12
150 (2 rows)
151
152 -- delete with constant expression
153 delete from rtest_v1 where a = 1;
154 select * from rtest_v1;
155  a | b  
156 ---+----
157  2 | 12
158 (1 row)
159
160 insert into rtest_v1 values (1, 11);
161 delete from rtest_v1 where b = 12;
162 select * from rtest_v1;
163  a | b  
164 ---+----
165  1 | 11
166 (1 row)
167
168 insert into rtest_v1 values (2, 12);
169 insert into rtest_v1 values (2, 13);
170 select * from rtest_v1;
171  a | b  
172 ---+----
173  1 | 11
174  2 | 12
175  2 | 13
176 (3 rows)
177
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
181 \p
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
185 \r
186 delete from rtest_v1 where b = 12;
187 select * from rtest_v1;
188  a | b  
189 ---+----
190  1 | 11
191 (1 row)
192
193 delete from rtest_v1;
194 -- insert select
195 insert into rtest_v1 select * from rtest_t2;
196 select * from rtest_v1;
197  a | b  
198 ---+----
199  1 | 21
200  2 | 22
201  3 | 23
202 (3 rows)
203
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;
208  a | b  
209 ---+----
210  1 | 21
211  2 | 22
212  3 | 23
213 (3 rows)
214
215 -- now with only one target attribute
216 insert into rtest_v1 (a) select a from rtest_t3;
217 select * from rtest_v1;
218  a | b  
219 ---+----
220  1 | 21
221  2 | 22
222  3 | 23
223  1 |   
224  2 |   
225  3 |   
226  4 |   
227  5 |   
228 (8 rows)
229
230 select * from rtest_v1 where b isnull;
231  a | b 
232 ---+---
233  1 |  
234  2 |  
235  3 |  
236  4 |  
237  5 |  
238 (5 rows)
239
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;
244  a | b  
245 ---+----
246  1 | 21
247  2 | 22
248  3 | 23
249 (3 rows)
250
251 -- now updates with constant expression
252 update rtest_v1 set b = 42 where a = 2;
253 select * from rtest_v1;
254  a | b  
255 ---+----
256  1 | 21
257  3 | 23
258  2 | 42
259 (3 rows)
260
261 update rtest_v1 set b = 99 where b = 42;
262 select * from rtest_v1;
263  a | b  
264 ---+----
265  1 | 21
266  3 | 23
267  2 | 99
268 (3 rows)
269
270 update rtest_v1 set b = 88 where b < 50;
271 select * from rtest_v1;
272  a | b  
273 ---+----
274  2 | 99
275  1 | 88
276  3 | 88
277 (3 rows)
278
279 delete from rtest_v1;
280 insert into rtest_v1 select rtest_t2.a, rtest_t3.b
281     from rtest_t2, rtest_t3
282     where rtest_t2.a = rtest_t3.a;
283 select * from rtest_v1;
284  a | b  
285 ---+----
286  1 | 31
287  2 | 32
288  3 | 33
289 (3 rows)
290
291 -- updates in a mergejoin
292 update rtest_v1 set b = rtest_t2.b from rtest_t2 where rtest_v1.a = rtest_t2.a;
293 select * from rtest_v1;
294  a | b  
295 ---+----
296  1 | 21
297  2 | 22
298  3 | 23
299 (3 rows)
300
301 insert into rtest_v1 select * from rtest_t3;
302 select * from rtest_v1;
303  a | b  
304 ---+----
305  1 | 21
306  2 | 22
307  3 | 23
308  1 | 31
309  2 | 32
310  3 | 33
311  4 | 34
312  5 | 35
313 (8 rows)
314
315 update rtest_t1 set a = a + 10 where b > 30;
316 select * from rtest_v1;
317  a  | b  
318 ----+----
319   1 | 21
320   2 | 22
321   3 | 23
322  11 | 31
323  12 | 32
324  13 | 33
325  14 | 34
326  15 | 35
327 (8 rows)
328
329 update rtest_v1 set a = rtest_t3.a + 20 from rtest_t3 where rtest_v1.b = rtest_t3.b;
330 select * from rtest_v1;
331  a  | b  
332 ----+----
333   1 | 21
334   2 | 22
335   3 | 23
336  21 | 31
337  22 | 32
338  23 | 33
339  24 | 34
340  25 | 35
341 (8 rows)
342
343 --
344 -- Test for constraint updates/deletes
345 --
346 insert into rtest_system values ('orion', 'Linux Jan Wieck');
347 insert into rtest_system values ('notjw', 'WinNT Jan Wieck (notebook)');
348 insert into rtest_system values ('neptun', 'Fileserver');
349 insert into rtest_interface values ('orion', 'eth0');
350 insert into rtest_interface values ('orion', 'eth1');
351 insert into rtest_interface values ('notjw', 'eth0');
352 insert into rtest_interface values ('neptun', 'eth0');
353 insert into rtest_person values ('jw', 'Jan Wieck');
354 insert into rtest_person values ('bm', 'Bruce Momjian');
355 insert into rtest_admin values ('jw', 'orion');
356 insert into rtest_admin values ('jw', 'notjw');
357 insert into rtest_admin values ('bm', 'neptun');
358 update rtest_system set sysname = 'pluto' where sysname = 'neptun';
359 select * from rtest_interface;
360  sysname | ifname 
361 ---------+--------
362  orion   | eth0
363  orion   | eth1
364  notjw   | eth0
365  pluto   | eth0
366 (4 rows)
367
368 select * from rtest_admin;
369  pname | sysname 
370 -------+---------
371  jw    | orion
372  jw    | notjw
373  bm    | pluto
374 (3 rows)
375
376 update rtest_person set pname = 'jwieck' where pdesc = 'Jan Wieck';
377 -- Note: use ORDER BY here to ensure consistent output across all systems.
378 -- The above UPDATE affects two rows with equal keys, so they could be
379 -- updated in either order depending on the whim of the local qsort().
380 select * from rtest_admin order by pname, sysname;
381  pname  | sysname 
382 --------+---------
383  bm     | pluto
384  jwieck | notjw
385  jwieck | orion
386 (3 rows)
387
388 delete from rtest_system where sysname = 'orion';
389 select * from rtest_interface;
390  sysname | ifname 
391 ---------+--------
392  notjw   | eth0
393  pluto   | eth0
394 (2 rows)
395
396 select * from rtest_admin;
397  pname  | sysname 
398 --------+---------
399  bm     | pluto
400  jwieck | notjw
401 (2 rows)
402
403 --
404 -- Rule qualification test
405 --
406 insert into rtest_emp values ('wiech', '5000.00');
407 insert into rtest_emp values ('gates', '80000.00');
408 update rtest_emp set ename = 'wiecx' where ename = 'wiech';
409 update rtest_emp set ename = 'wieck', salary = '6000.00' where ename = 'wiecx';
410 update rtest_emp set salary = '7000.00' where ename = 'wieck';
411 delete from rtest_emp where ename = 'gates';
412 select ename, who = current_user as "matches user", action, newsal, oldsal from rtest_emplog order by ename, action, newsal;
413         ename         | matches user |   action   |   newsal   |   oldsal   
414 ----------------------+--------------+------------+------------+------------
415  gates                | t            | fired      |      $0.00 | $80,000.00
416  gates                | t            | hired      | $80,000.00 |      $0.00
417  wiech                | t            | hired      |  $5,000.00 |      $0.00
418  wieck                | t            | honored    |  $6,000.00 |  $5,000.00
419  wieck                | t            | honored    |  $7,000.00 |  $6,000.00
420 (5 rows)
421
422 insert into rtest_empmass values ('meyer', '4000.00');
423 insert into rtest_empmass values ('maier', '5000.00');
424 insert into rtest_empmass values ('mayr', '6000.00');
425 insert into rtest_emp select * from rtest_empmass;
426 select ename, who = current_user as "matches user", action, newsal, oldsal from rtest_emplog order by ename, action, newsal;
427         ename         | matches user |   action   |   newsal   |   oldsal   
428 ----------------------+--------------+------------+------------+------------
429  gates                | t            | fired      |      $0.00 | $80,000.00
430  gates                | t            | hired      | $80,000.00 |      $0.00
431  maier                | t            | hired      |  $5,000.00 |      $0.00
432  mayr                 | t            | hired      |  $6,000.00 |      $0.00
433  meyer                | t            | hired      |  $4,000.00 |      $0.00
434  wiech                | t            | hired      |  $5,000.00 |      $0.00
435  wieck                | t            | honored    |  $6,000.00 |  $5,000.00
436  wieck                | t            | honored    |  $7,000.00 |  $6,000.00
437 (8 rows)
438
439 update rtest_empmass set salary = salary + '1000.00';
440 update rtest_emp set salary = rtest_empmass.salary from rtest_empmass where rtest_emp.ename = rtest_empmass.ename;
441 select ename, who = current_user as "matches user", action, newsal, oldsal from rtest_emplog order by ename, action, newsal;
442         ename         | matches user |   action   |   newsal   |   oldsal   
443 ----------------------+--------------+------------+------------+------------
444  gates                | t            | fired      |      $0.00 | $80,000.00
445  gates                | t            | hired      | $80,000.00 |      $0.00
446  maier                | t            | hired      |  $5,000.00 |      $0.00
447  maier                | t            | honored    |  $6,000.00 |  $5,000.00
448  mayr                 | t            | hired      |  $6,000.00 |      $0.00
449  mayr                 | t            | honored    |  $7,000.00 |  $6,000.00
450  meyer                | t            | hired      |  $4,000.00 |      $0.00
451  meyer                | t            | honored    |  $5,000.00 |  $4,000.00
452  wiech                | t            | hired      |  $5,000.00 |      $0.00
453  wieck                | t            | honored    |  $6,000.00 |  $5,000.00
454  wieck                | t            | honored    |  $7,000.00 |  $6,000.00
455 (11 rows)
456
457 delete from rtest_emp using rtest_empmass where rtest_emp.ename = rtest_empmass.ename;
458 select ename, who = current_user as "matches user", action, newsal, oldsal from rtest_emplog order by ename, action, newsal;
459         ename         | matches user |   action   |   newsal   |   oldsal   
460 ----------------------+--------------+------------+------------+------------
461  gates                | t            | fired      |      $0.00 | $80,000.00
462  gates                | t            | hired      | $80,000.00 |      $0.00
463  maier                | t            | fired      |      $0.00 |  $6,000.00
464  maier                | t            | hired      |  $5,000.00 |      $0.00
465  maier                | t            | honored    |  $6,000.00 |  $5,000.00
466  mayr                 | t            | fired      |      $0.00 |  $7,000.00
467  mayr                 | t            | hired      |  $6,000.00 |      $0.00
468  mayr                 | t            | honored    |  $7,000.00 |  $6,000.00
469  meyer                | t            | fired      |      $0.00 |  $5,000.00
470  meyer                | t            | hired      |  $4,000.00 |      $0.00
471  meyer                | t            | honored    |  $5,000.00 |  $4,000.00
472  wiech                | t            | hired      |  $5,000.00 |      $0.00
473  wieck                | t            | honored    |  $6,000.00 |  $5,000.00
474  wieck                | t            | honored    |  $7,000.00 |  $6,000.00
475 (14 rows)
476
477 --
478 -- Multiple cascaded qualified instead rule test
479 --
480 insert into rtest_t4 values (1, 'Record should go to rtest_t4');
481 insert into rtest_t4 values (2, 'Record should go to rtest_t4');
482 insert into rtest_t4 values (10, 'Record should go to rtest_t5');
483 insert into rtest_t4 values (15, 'Record should go to rtest_t5');
484 insert into rtest_t4 values (19, 'Record should go to rtest_t5 and t7');
485 insert into rtest_t4 values (20, 'Record should go to rtest_t4 and t6');
486 insert into rtest_t4 values (26, 'Record should go to rtest_t4 and t8');
487 insert into rtest_t4 values (28, 'Record should go to rtest_t4 and t8');
488 insert into rtest_t4 values (30, 'Record should go to rtest_t4');
489 insert into rtest_t4 values (40, 'Record should go to rtest_t4');
490 select * from rtest_t4;
491  a  |                  b                  
492 ----+-------------------------------------
493   1 | Record should go to rtest_t4
494   2 | Record should go to rtest_t4
495  20 | Record should go to rtest_t4 and t6
496  26 | Record should go to rtest_t4 and t8
497  28 | Record should go to rtest_t4 and t8
498  30 | Record should go to rtest_t4
499  40 | Record should go to rtest_t4
500 (7 rows)
501
502 select * from rtest_t5;
503  a  |                  b                  
504 ----+-------------------------------------
505  10 | Record should go to rtest_t5
506  15 | Record should go to rtest_t5
507  19 | Record should go to rtest_t5 and t7
508 (3 rows)
509
510 select * from rtest_t6;
511  a  |                  b                  
512 ----+-------------------------------------
513  20 | Record should go to rtest_t4 and t6
514 (1 row)
515
516 select * from rtest_t7;
517  a  |                  b                  
518 ----+-------------------------------------
519  19 | Record should go to rtest_t5 and t7
520 (1 row)
521
522 select * from rtest_t8;
523  a  |                  b                  
524 ----+-------------------------------------
525  26 | Record should go to rtest_t4 and t8
526  28 | Record should go to rtest_t4 and t8
527 (2 rows)
528
529 delete from rtest_t4;
530 delete from rtest_t5;
531 delete from rtest_t6;
532 delete from rtest_t7;
533 delete from rtest_t8;
534 insert into rtest_t9 values (1, 'Record should go to rtest_t4');
535 insert into rtest_t9 values (2, 'Record should go to rtest_t4');
536 insert into rtest_t9 values (10, 'Record should go to rtest_t5');
537 insert into rtest_t9 values (15, 'Record should go to rtest_t5');
538 insert into rtest_t9 values (19, 'Record should go to rtest_t5 and t7');
539 insert into rtest_t9 values (20, 'Record should go to rtest_t4 and t6');
540 insert into rtest_t9 values (26, 'Record should go to rtest_t4 and t8');
541 insert into rtest_t9 values (28, 'Record should go to rtest_t4 and t8');
542 insert into rtest_t9 values (30, 'Record should go to rtest_t4');
543 insert into rtest_t9 values (40, 'Record should go to rtest_t4');
544 insert into rtest_t4 select * from rtest_t9 where a < 20;
545 select * from rtest_t4;
546  a |              b               
547 ---+------------------------------
548  1 | Record should go to rtest_t4
549  2 | Record should go to rtest_t4
550 (2 rows)
551
552 select * from rtest_t5;
553  a  |                  b                  
554 ----+-------------------------------------
555  10 | Record should go to rtest_t5
556  15 | Record should go to rtest_t5
557  19 | Record should go to rtest_t5 and t7
558 (3 rows)
559
560 select * from rtest_t6;
561  a | b 
562 ---+---
563 (0 rows)
564
565 select * from rtest_t7;
566  a  |                  b                  
567 ----+-------------------------------------
568  19 | Record should go to rtest_t5 and t7
569 (1 row)
570
571 select * from rtest_t8;
572  a | b 
573 ---+---
574 (0 rows)
575
576 insert into rtest_t4 select * from rtest_t9 where b ~ 'and t8';
577 select * from rtest_t4;
578  a  |                  b                  
579 ----+-------------------------------------
580   1 | Record should go to rtest_t4
581   2 | Record should go to rtest_t4
582  26 | Record should go to rtest_t4 and t8
583  28 | Record should go to rtest_t4 and t8
584 (4 rows)
585
586 select * from rtest_t5;
587  a  |                  b                  
588 ----+-------------------------------------
589  10 | Record should go to rtest_t5
590  15 | Record should go to rtest_t5
591  19 | Record should go to rtest_t5 and t7
592 (3 rows)
593
594 select * from rtest_t6;
595  a | b 
596 ---+---
597 (0 rows)
598
599 select * from rtest_t7;
600  a  |                  b                  
601 ----+-------------------------------------
602  19 | Record should go to rtest_t5 and t7
603 (1 row)
604
605 select * from rtest_t8;
606  a  |                  b                  
607 ----+-------------------------------------
608  26 | Record should go to rtest_t4 and t8
609  28 | Record should go to rtest_t4 and t8
610 (2 rows)
611
612 insert into rtest_t4 select a + 1, b from rtest_t9 where a in (20, 30, 40);
613 select * from rtest_t4;
614  a  |                  b                  
615 ----+-------------------------------------
616   1 | Record should go to rtest_t4
617   2 | Record should go to rtest_t4
618  26 | Record should go to rtest_t4 and t8
619  28 | Record should go to rtest_t4 and t8
620  21 | Record should go to rtest_t4 and t6
621  31 | Record should go to rtest_t4
622  41 | Record should go to rtest_t4
623 (7 rows)
624
625 select * from rtest_t5;
626  a  |                  b                  
627 ----+-------------------------------------
628  10 | Record should go to rtest_t5
629  15 | Record should go to rtest_t5
630  19 | Record should go to rtest_t5 and t7
631 (3 rows)
632
633 select * from rtest_t6;
634  a  |                  b                  
635 ----+-------------------------------------
636  21 | Record should go to rtest_t4 and t6
637 (1 row)
638
639 select * from rtest_t7;
640  a  |                  b                  
641 ----+-------------------------------------
642  19 | Record should go to rtest_t5 and t7
643 (1 row)
644
645 select * from rtest_t8;
646  a  |                  b                  
647 ----+-------------------------------------
648  26 | Record should go to rtest_t4 and t8
649  28 | Record should go to rtest_t4 and t8
650 (2 rows)
651
652 --
653 -- Check that the ordering of rules fired is correct
654 --
655 insert into rtest_order1 values (1);
656 select * from rtest_order2;
657  a | b |              c               
658 ---+---+------------------------------
659  1 | 1 | rule 1 - this should run 1st
660  1 | 2 | rule 2 - this should run 2nd
661  1 | 3 | rule 3 - this should run 3rd
662  1 | 4 | rule 4 - this should run 4th
663 (4 rows)
664
665 --
666 -- Check if instead nothing w/without qualification works
667 --
668 insert into rtest_nothn1 values (1, 'want this');
669 insert into rtest_nothn1 values (2, 'want this');
670 insert into rtest_nothn1 values (10, 'don''t want this');
671 insert into rtest_nothn1 values (19, 'don''t want this');
672 insert into rtest_nothn1 values (20, 'want this');
673 insert into rtest_nothn1 values (29, 'want this');
674 insert into rtest_nothn1 values (30, 'don''t want this');
675 insert into rtest_nothn1 values (39, 'don''t want this');
676 insert into rtest_nothn1 values (40, 'want this');
677 insert into rtest_nothn1 values (50, 'want this');
678 insert into rtest_nothn1 values (60, 'want this');
679 select * from rtest_nothn1;
680  a  |     b     
681 ----+-----------
682   1 | want this
683   2 | want this
684  20 | want this
685  29 | want this
686  40 | want this
687  50 | want this
688  60 | want this
689 (7 rows)
690
691 insert into rtest_nothn2 values (10, 'too small');
692 insert into rtest_nothn2 values (50, 'too small');
693 insert into rtest_nothn2 values (100, 'OK');
694 insert into rtest_nothn2 values (200, 'OK');
695 select * from rtest_nothn2;
696  a | b 
697 ---+---
698 (0 rows)
699
700 select * from rtest_nothn3;
701   a  | b  
702 -----+----
703  100 | OK
704  200 | OK
705 (2 rows)
706
707 delete from rtest_nothn1;
708 delete from rtest_nothn2;
709 delete from rtest_nothn3;
710 insert into rtest_nothn4 values (1, 'want this');
711 insert into rtest_nothn4 values (2, 'want this');
712 insert into rtest_nothn4 values (10, 'don''t want this');
713 insert into rtest_nothn4 values (19, 'don''t want this');
714 insert into rtest_nothn4 values (20, 'want this');
715 insert into rtest_nothn4 values (29, 'want this');
716 insert into rtest_nothn4 values (30, 'don''t want this');
717 insert into rtest_nothn4 values (39, 'don''t want this');
718 insert into rtest_nothn4 values (40, 'want this');
719 insert into rtest_nothn4 values (50, 'want this');
720 insert into rtest_nothn4 values (60, 'want this');
721 insert into rtest_nothn1 select * from rtest_nothn4;
722 select * from rtest_nothn1;
723  a  |     b     
724 ----+-----------
725   1 | want this
726   2 | want this
727  20 | want this
728  29 | want this
729  40 | want this
730  50 | want this
731  60 | want this
732 (7 rows)
733
734 delete from rtest_nothn4;
735 insert into rtest_nothn4 values (10, 'too small');
736 insert into rtest_nothn4 values (50, 'too small');
737 insert into rtest_nothn4 values (100, 'OK');
738 insert into rtest_nothn4 values (200, 'OK');
739 insert into rtest_nothn2 select * from rtest_nothn4;
740 select * from rtest_nothn2;
741  a | b 
742 ---+---
743 (0 rows)
744
745 select * from rtest_nothn3;
746   a  | b  
747 -----+----
748  100 | OK
749  200 | OK
750 (2 rows)
751
752 create table rtest_view1 (a int4, b text, v bool);
753 create table rtest_view2 (a int4);
754 create table rtest_view3 (a int4, b text);
755 create table rtest_view4 (a int4, b text, c int4);
756 create view rtest_vview1 as select a, b from rtest_view1 X 
757         where 0 < (select count(*) from rtest_view2 Y where Y.a = X.a);
758 create view rtest_vview2 as select a, b from rtest_view1 where v;
759 create view rtest_vview3 as select a, b from rtest_vview2 X
760         where 0 < (select count(*) from rtest_view2 Y where Y.a = X.a);
761 create view rtest_vview4 as select X.a, X.b, count(Y.a) as refcount
762         from rtest_view1 X, rtest_view2 Y
763         where X.a = Y.a
764         group by X.a, X.b;
765 create function rtest_viewfunc1(int4) returns int4 as
766         'select count(*)::int4 from rtest_view2 where a = $1'
767         language sql;
768 create view rtest_vview5 as select a, b, rtest_viewfunc1(a) as refcount
769         from rtest_view1;
770 insert into rtest_view1 values (1, 'item 1', 't');
771 insert into rtest_view1 values (2, 'item 2', 't');
772 insert into rtest_view1 values (3, 'item 3', 't');
773 insert into rtest_view1 values (4, 'item 4', 'f');
774 insert into rtest_view1 values (5, 'item 5', 't');
775 insert into rtest_view1 values (6, 'item 6', 'f');
776 insert into rtest_view1 values (7, 'item 7', 't');
777 insert into rtest_view1 values (8, 'item 8', 't');
778 insert into rtest_view2 values (2);
779 insert into rtest_view2 values (2);
780 insert into rtest_view2 values (4);
781 insert into rtest_view2 values (5);
782 insert into rtest_view2 values (7);
783 insert into rtest_view2 values (7);
784 insert into rtest_view2 values (7);
785 insert into rtest_view2 values (7);
786 select * from rtest_vview1;
787  a |   b    
788 ---+--------
789  2 | item 2
790  4 | item 4
791  5 | item 5
792  7 | item 7
793 (4 rows)
794
795 select * from rtest_vview2;
796  a |   b    
797 ---+--------
798  1 | item 1
799  2 | item 2
800  3 | item 3
801  5 | item 5
802  7 | item 7
803  8 | item 8
804 (6 rows)
805
806 select * from rtest_vview3;
807  a |   b    
808 ---+--------
809  2 | item 2
810  5 | item 5
811  7 | item 7
812 (3 rows)
813
814 select * from rtest_vview4 order by a, b;
815  a |   b    | refcount 
816 ---+--------+----------
817  2 | item 2 |        2
818  4 | item 4 |        1
819  5 | item 5 |        1
820  7 | item 7 |        4
821 (4 rows)
822
823 select * from rtest_vview5;
824  a |   b    | refcount 
825 ---+--------+----------
826  1 | item 1 |        0
827  2 | item 2 |        2
828  3 | item 3 |        0
829  4 | item 4 |        1
830  5 | item 5 |        1
831  6 | item 6 |        0
832  7 | item 7 |        4
833  8 | item 8 |        0
834 (8 rows)
835
836 insert into rtest_view3 select * from rtest_vview1 where a < 7;
837 select * from rtest_view3;
838  a |   b    
839 ---+--------
840  2 | item 2
841  4 | item 4
842  5 | item 5
843 (3 rows)
844
845 delete from rtest_view3;
846 insert into rtest_view3 select * from rtest_vview2 where a != 5 and b !~ '2';
847 select * from rtest_view3;
848  a |   b    
849 ---+--------
850  1 | item 1
851  3 | item 3
852  7 | item 7
853  8 | item 8
854 (4 rows)
855
856 delete from rtest_view3;
857 insert into rtest_view3 select * from rtest_vview3;
858 select * from rtest_view3;
859  a |   b    
860 ---+--------
861  2 | item 2
862  5 | item 5
863  7 | item 7
864 (3 rows)
865
866 delete from rtest_view3;
867 insert into rtest_view4 select * from rtest_vview4 where 3 > refcount;
868 select * from rtest_view4 order by a, b;
869  a |   b    | c 
870 ---+--------+---
871  2 | item 2 | 2
872  4 | item 4 | 1
873  5 | item 5 | 1
874 (3 rows)
875
876 delete from rtest_view4;
877 insert into rtest_view4 select * from rtest_vview5 where a > 2 and refcount = 0;
878 select * from rtest_view4;
879  a |   b    | c 
880 ---+--------+---
881  3 | item 3 | 0
882  6 | item 6 | 0
883  8 | item 8 | 0
884 (3 rows)
885
886 delete from rtest_view4;
887 --
888 -- Test for computations in views
889 --
890 create table rtest_comp (
891         part    text,
892         unit    char(4),
893         size    float
894 );
895 create table rtest_unitfact (
896         unit    char(4),
897         factor  float
898 );
899 create view rtest_vcomp as 
900         select X.part, (X.size * Y.factor) as size_in_cm
901                         from rtest_comp X, rtest_unitfact Y
902                         where X.unit = Y.unit;
903 insert into rtest_unitfact values ('m', 100.0);
904 insert into rtest_unitfact values ('cm', 1.0);
905 insert into rtest_unitfact values ('inch', 2.54);
906 insert into rtest_comp values ('p1', 'm', 5.0);
907 insert into rtest_comp values ('p2', 'm', 3.0);
908 insert into rtest_comp values ('p3', 'cm', 5.0);
909 insert into rtest_comp values ('p4', 'cm', 15.0);
910 insert into rtest_comp values ('p5', 'inch', 7.0);
911 insert into rtest_comp values ('p6', 'inch', 4.4);
912 select * from rtest_vcomp order by part;
913  part | size_in_cm 
914 ------+------------
915  p1   |        500
916  p2   |        300
917  p3   |          5
918  p4   |         15
919  p5   |      17.78
920  p6   |     11.176
921 (6 rows)
922
923 select * from rtest_vcomp where size_in_cm > 10.0 order by size_in_cm using >;
924  part | size_in_cm 
925 ------+------------
926  p1   |        500
927  p2   |        300
928  p5   |      17.78
929  p4   |         15
930  p6   |     11.176
931 (5 rows)
932
933 --
934 -- In addition run the (slightly modified) queries from the
935 -- programmers manual section on the rule system.
936 --
937 CREATE TABLE shoe_data (
938         shoename   char(10),      -- primary key
939         sh_avail   integer,       -- available # of pairs
940         slcolor    char(10),      -- preferred shoelace color
941         slminlen   float,         -- miminum shoelace length
942         slmaxlen   float,         -- maximum shoelace length
943         slunit     char(8)        -- length unit
944 );
945 CREATE TABLE shoelace_data (
946         sl_name    char(10),      -- primary key
947         sl_avail   integer,       -- available # of pairs
948         sl_color   char(10),      -- shoelace color
949         sl_len     float,         -- shoelace length
950         sl_unit    char(8)        -- length unit
951 );
952 CREATE TABLE unit (
953         un_name    char(8),       -- the primary key
954         un_fact    float          -- factor to transform to cm
955 );
956 CREATE VIEW shoe AS
957         SELECT sh.shoename,
958                    sh.sh_avail,
959                    sh.slcolor,
960                    sh.slminlen,
961                    sh.slminlen * un.un_fact AS slminlen_cm,
962                    sh.slmaxlen,
963                    sh.slmaxlen * un.un_fact AS slmaxlen_cm,
964                    sh.slunit
965           FROM shoe_data sh, unit un
966          WHERE sh.slunit = un.un_name;
967 CREATE VIEW shoelace AS
968         SELECT s.sl_name,
969                    s.sl_avail,
970                    s.sl_color,
971                    s.sl_len,
972                    s.sl_unit,
973                    s.sl_len * u.un_fact AS sl_len_cm
974           FROM shoelace_data s, unit u
975          WHERE s.sl_unit = u.un_name;
976 CREATE VIEW shoe_ready AS
977         SELECT rsh.shoename,
978                    rsh.sh_avail,
979                    rsl.sl_name,
980                    rsl.sl_avail,
981                    int4smaller(rsh.sh_avail, rsl.sl_avail) AS total_avail
982           FROM shoe rsh, shoelace rsl
983          WHERE rsl.sl_color = rsh.slcolor
984            AND rsl.sl_len_cm >= rsh.slminlen_cm
985            AND rsl.sl_len_cm <= rsh.slmaxlen_cm;
986 INSERT INTO unit VALUES ('cm', 1.0);
987 INSERT INTO unit VALUES ('m', 100.0);
988 INSERT INTO unit VALUES ('inch', 2.54);
989 INSERT INTO shoe_data VALUES ('sh1', 2, 'black', 70.0, 90.0, 'cm');
990 INSERT INTO shoe_data VALUES ('sh2', 0, 'black', 30.0, 40.0, 'inch');
991 INSERT INTO shoe_data VALUES ('sh3', 4, 'brown', 50.0, 65.0, 'cm');
992 INSERT INTO shoe_data VALUES ('sh4', 3, 'brown', 40.0, 50.0, 'inch');
993 INSERT INTO shoelace_data VALUES ('sl1', 5, 'black', 80.0, 'cm');
994 INSERT INTO shoelace_data VALUES ('sl2', 6, 'black', 100.0, 'cm');
995 INSERT INTO shoelace_data VALUES ('sl3', 0, 'black', 35.0 , 'inch');
996 INSERT INTO shoelace_data VALUES ('sl4', 8, 'black', 40.0 , 'inch');
997 INSERT INTO shoelace_data VALUES ('sl5', 4, 'brown', 1.0 , 'm');
998 INSERT INTO shoelace_data VALUES ('sl6', 0, 'brown', 0.9 , 'm');
999 INSERT INTO shoelace_data VALUES ('sl7', 7, 'brown', 60 , 'cm');
1000 INSERT INTO shoelace_data VALUES ('sl8', 1, 'brown', 40 , 'inch');
1001 -- SELECTs in doc
1002 SELECT * FROM shoelace ORDER BY sl_name;
1003   sl_name   | sl_avail |  sl_color  | sl_len | sl_unit  | sl_len_cm 
1004 ------------+----------+------------+--------+----------+-----------
1005  sl1        |        5 | black      |     80 | cm       |        80
1006  sl2        |        6 | black      |    100 | cm       |       100
1007  sl3        |        0 | black      |     35 | inch     |      88.9
1008  sl4        |        8 | black      |     40 | inch     |     101.6
1009  sl5        |        4 | brown      |      1 | m        |       100
1010  sl6        |        0 | brown      |    0.9 | m        |        90
1011  sl7        |        7 | brown      |     60 | cm       |        60
1012  sl8        |        1 | brown      |     40 | inch     |     101.6
1013 (8 rows)
1014
1015 SELECT * FROM shoe_ready WHERE total_avail >= 2 ORDER BY 1;
1016   shoename  | sh_avail |  sl_name   | sl_avail | total_avail 
1017 ------------+----------+------------+----------+-------------
1018  sh1        |        2 | sl1        |        5 |           2
1019  sh3        |        4 | sl7        |        7 |           4
1020 (2 rows)
1021
1022     CREATE TABLE shoelace_log (
1023         sl_name    char(10),      -- shoelace changed
1024         sl_avail   integer,       -- new available value
1025         log_who    name,          -- who did it
1026         log_when   timestamp      -- when
1027     );
1028 -- Want "log_who" to be CURRENT_USER,
1029 -- but that is non-portable for the regression test
1030 -- - thomas 1999-02-21
1031     CREATE RULE log_shoelace AS ON UPDATE TO shoelace_data
1032         WHERE NEW.sl_avail != OLD.sl_avail
1033         DO INSERT INTO shoelace_log VALUES (
1034                                         NEW.sl_name,
1035                                         NEW.sl_avail,
1036                                         'Al Bundy',
1037                                         'epoch'
1038                                     );
1039 UPDATE shoelace_data SET sl_avail = 6 WHERE  sl_name = 'sl7';
1040 SELECT * FROM shoelace_log;
1041   sl_name   | sl_avail | log_who  |         log_when         
1042 ------------+----------+----------+--------------------------
1043  sl7        |        6 | Al Bundy | Thu Jan 01 00:00:00 1970
1044 (1 row)
1045
1046     CREATE RULE shoelace_ins AS ON INSERT TO shoelace
1047         DO INSTEAD
1048         INSERT INTO shoelace_data VALUES (
1049                NEW.sl_name,
1050                NEW.sl_avail,
1051                NEW.sl_color,
1052                NEW.sl_len,
1053                NEW.sl_unit);
1054     CREATE RULE shoelace_upd AS ON UPDATE TO shoelace
1055         DO INSTEAD
1056         UPDATE shoelace_data SET
1057                sl_name = NEW.sl_name,
1058                sl_avail = NEW.sl_avail,
1059                sl_color = NEW.sl_color,
1060                sl_len = NEW.sl_len,
1061                sl_unit = NEW.sl_unit
1062          WHERE sl_name = OLD.sl_name;
1063     CREATE RULE shoelace_del AS ON DELETE TO shoelace
1064         DO INSTEAD
1065         DELETE FROM shoelace_data
1066          WHERE sl_name = OLD.sl_name;
1067     CREATE TABLE shoelace_arrive (
1068         arr_name    char(10),
1069         arr_quant   integer
1070     );
1071     CREATE TABLE shoelace_ok (
1072         ok_name     char(10),
1073         ok_quant    integer
1074     );
1075     CREATE RULE shoelace_ok_ins AS ON INSERT TO shoelace_ok
1076         DO INSTEAD
1077         UPDATE shoelace SET
1078                sl_avail = sl_avail + NEW.ok_quant
1079          WHERE sl_name = NEW.ok_name;
1080 INSERT INTO shoelace_arrive VALUES ('sl3', 10);
1081 INSERT INTO shoelace_arrive VALUES ('sl6', 20);
1082 INSERT INTO shoelace_arrive VALUES ('sl8', 20);
1083 SELECT * FROM shoelace ORDER BY sl_name;
1084   sl_name   | sl_avail |  sl_color  | sl_len | sl_unit  | sl_len_cm 
1085 ------------+----------+------------+--------+----------+-----------
1086  sl1        |        5 | black      |     80 | cm       |        80
1087  sl2        |        6 | black      |    100 | cm       |       100
1088  sl3        |        0 | black      |     35 | inch     |      88.9
1089  sl4        |        8 | black      |     40 | inch     |     101.6
1090  sl5        |        4 | brown      |      1 | m        |       100
1091  sl6        |        0 | brown      |    0.9 | m        |        90
1092  sl7        |        6 | brown      |     60 | cm       |        60
1093  sl8        |        1 | brown      |     40 | inch     |     101.6
1094 (8 rows)
1095
1096 insert into shoelace_ok select * from shoelace_arrive;
1097 SELECT * FROM shoelace ORDER BY sl_name;
1098   sl_name   | sl_avail |  sl_color  | sl_len | sl_unit  | sl_len_cm 
1099 ------------+----------+------------+--------+----------+-----------
1100  sl1        |        5 | black      |     80 | cm       |        80
1101  sl2        |        6 | black      |    100 | cm       |       100
1102  sl3        |       10 | black      |     35 | inch     |      88.9
1103  sl4        |        8 | black      |     40 | inch     |     101.6
1104  sl5        |        4 | brown      |      1 | m        |       100
1105  sl6        |       20 | brown      |    0.9 | m        |        90
1106  sl7        |        6 | brown      |     60 | cm       |        60
1107  sl8        |       21 | brown      |     40 | inch     |     101.6
1108 (8 rows)
1109
1110 SELECT * FROM shoelace_log ORDER BY sl_name;
1111   sl_name   | sl_avail | log_who  |         log_when         
1112 ------------+----------+----------+--------------------------
1113  sl3        |       10 | Al Bundy | Thu Jan 01 00:00:00 1970
1114  sl6        |       20 | Al Bundy | Thu Jan 01 00:00:00 1970
1115  sl7        |        6 | Al Bundy | Thu Jan 01 00:00:00 1970
1116  sl8        |       21 | Al Bundy | Thu Jan 01 00:00:00 1970
1117 (4 rows)
1118
1119     CREATE VIEW shoelace_obsolete AS
1120         SELECT * FROM shoelace WHERE NOT EXISTS
1121             (SELECT shoename FROM shoe WHERE slcolor = sl_color);
1122     CREATE VIEW shoelace_candelete AS
1123         SELECT * FROM shoelace_obsolete WHERE sl_avail = 0;
1124 insert into shoelace values ('sl9', 0, 'pink', 35.0, 'inch', 0.0);
1125 insert into shoelace values ('sl10', 1000, 'magenta', 40.0, 'inch', 0.0);
1126 SELECT * FROM shoelace_obsolete ORDER BY sl_len_cm;
1127   sl_name   | sl_avail |  sl_color  | sl_len | sl_unit  | sl_len_cm 
1128 ------------+----------+------------+--------+----------+-----------
1129  sl9        |        0 | pink       |     35 | inch     |      88.9
1130  sl10       |     1000 | magenta    |     40 | inch     |     101.6
1131 (2 rows)
1132
1133 SELECT * FROM shoelace_candelete;
1134   sl_name   | sl_avail |  sl_color  | sl_len | sl_unit  | sl_len_cm 
1135 ------------+----------+------------+--------+----------+-----------
1136  sl9        |        0 | pink       |     35 | inch     |      88.9
1137 (1 row)
1138
1139 DELETE FROM shoelace WHERE EXISTS
1140     (SELECT * FROM shoelace_candelete
1141              WHERE sl_name = shoelace.sl_name);
1142 SELECT * FROM shoelace ORDER BY sl_name;
1143   sl_name   | sl_avail |  sl_color  | sl_len | sl_unit  | sl_len_cm 
1144 ------------+----------+------------+--------+----------+-----------
1145  sl1        |        5 | black      |     80 | cm       |        80
1146  sl10       |     1000 | magenta    |     40 | inch     |     101.6
1147  sl2        |        6 | black      |    100 | cm       |       100
1148  sl3        |       10 | black      |     35 | inch     |      88.9
1149  sl4        |        8 | black      |     40 | inch     |     101.6
1150  sl5        |        4 | brown      |      1 | m        |       100
1151  sl6        |       20 | brown      |    0.9 | m        |        90
1152  sl7        |        6 | brown      |     60 | cm       |        60
1153  sl8        |       21 | brown      |     40 | inch     |     101.6
1154 (9 rows)
1155
1156 SELECT * FROM shoe ORDER BY shoename;
1157   shoename  | sh_avail |  slcolor   | slminlen | slminlen_cm | slmaxlen | slmaxlen_cm |  slunit  
1158 ------------+----------+------------+----------+-------------+----------+-------------+----------
1159  sh1        |        2 | black      |       70 |          70 |       90 |          90 | cm      
1160  sh2        |        0 | black      |       30 |        76.2 |       40 |       101.6 | inch    
1161  sh3        |        4 | brown      |       50 |          50 |       65 |          65 | cm      
1162  sh4        |        3 | brown      |       40 |       101.6 |       50 |         127 | inch    
1163 (4 rows)
1164
1165 SELECT count(*) FROM shoe;
1166  count 
1167 -------
1168      4
1169 (1 row)
1170
1171 --
1172 -- Simple test of qualified ON INSERT ... this did not work in 7.0 ...
1173 --
1174 create table foo (f1 int);
1175 create table foo2 (f1 int);
1176 create rule foorule as on insert to foo where f1 < 100
1177 do instead nothing;
1178 insert into foo values(1);
1179 insert into foo values(1001);
1180 select * from foo;
1181   f1  
1182 ------
1183  1001
1184 (1 row)
1185
1186 drop rule foorule on foo;
1187 -- this should fail because f1 is not exposed for unqualified reference:
1188 create rule foorule as on insert to foo where f1 < 100
1189 do instead insert into foo2 values (f1);
1190 ERROR:  column "f1" does not exist
1191 LINE 2: do instead insert into foo2 values (f1);
1192                                             ^
1193 -- this is the correct way:
1194 create rule foorule as on insert to foo where f1 < 100
1195 do instead insert into foo2 values (new.f1);
1196 insert into foo values(2);
1197 insert into foo values(100);
1198 select * from foo;
1199   f1  
1200 ------
1201  1001
1202   100
1203 (2 rows)
1204
1205 select * from foo2;
1206  f1 
1207 ----
1208   2
1209 (1 row)
1210
1211 drop rule foorule on foo;
1212 drop table foo;
1213 drop table foo2;
1214 --
1215 -- Test rules containing INSERT ... SELECT, which is a very ugly special
1216 -- case as of 7.1.  Example is based on bug report from Joel Burton.
1217 --
1218 create table pparent (pid int, txt text);
1219 insert into pparent values (1,'parent1');
1220 insert into pparent values (2,'parent2');
1221 create table cchild (pid int, descrip text);
1222 insert into cchild values (1,'descrip1');
1223 create view vview as
1224   select pparent.pid, txt, descrip from
1225     pparent left join cchild using (pid);
1226 create rule rrule as
1227   on update to vview do instead
1228 (
1229   insert into cchild (pid, descrip)
1230     select old.pid, new.descrip where old.descrip isnull; 
1231   update cchild set descrip = new.descrip where cchild.pid = old.pid;
1232 );
1233 select * from vview;
1234  pid |   txt   | descrip  
1235 -----+---------+----------
1236    1 | parent1 | descrip1
1237    2 | parent2 | 
1238 (2 rows)
1239
1240 update vview set descrip='test1' where pid=1;
1241 select * from vview;
1242  pid |   txt   | descrip 
1243 -----+---------+---------
1244    1 | parent1 | test1
1245    2 | parent2 | 
1246 (2 rows)
1247
1248 update vview set descrip='test2' where pid=2;
1249 select * from vview;
1250  pid |   txt   | descrip 
1251 -----+---------+---------
1252    1 | parent1 | test1
1253    2 | parent2 | test2
1254 (2 rows)
1255
1256 update vview set descrip='test3' where pid=3;
1257 select * from vview;
1258  pid |   txt   | descrip 
1259 -----+---------+---------
1260    1 | parent1 | test1
1261    2 | parent2 | test2
1262 (2 rows)
1263
1264 select * from cchild;
1265  pid | descrip 
1266 -----+---------
1267    1 | test1
1268    2 | test2
1269 (2 rows)
1270
1271 drop rule rrule on vview;
1272 drop view vview;
1273 drop table pparent;
1274 drop table cchild;
1275 --
1276 -- Check that ruleutils are working
1277 --
1278 SELECT viewname, definition FROM pg_views WHERE schemaname <> 'information_schema' ORDER BY viewname;
1279          viewname         |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               definition                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
1280 --------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1281  iexit                    | SELECT ih."name", ih.thepath, interpt_pp(ih.thepath, r.thepath) AS exit FROM ihighway ih, ramp r WHERE (ih.thepath ## r.thepath);
1282  pg_cursors               | SELECT c."name", c."statement", c.is_holdable, c.is_binary, c.is_scrollable, c.creation_time FROM pg_cursor() c("name" text, "statement" text, is_holdable boolean, is_binary boolean, is_scrollable boolean, creation_time timestamp with time zone);
1283  pg_group                 | SELECT pg_authid.rolname AS groname, pg_authid.oid AS grosysid, ARRAY(SELECT pg_auth_members.member FROM pg_auth_members WHERE (pg_auth_members.roleid = pg_authid.oid)) AS grolist FROM pg_authid WHERE (NOT pg_authid.rolcanlogin);
1284  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"));
1285  pg_locks                 | SELECT l.locktype, l."database", l.relation, l.page, l.tuple, l.transactionid, l.classid, l.objid, l.objsubid, l."transaction", l.pid, l."mode", l."granted" FROM pg_lock_status() l(locktype text, "database" oid, relation oid, page integer, tuple smallint, transactionid xid, classid oid, objid oid, objsubid smallint, "transaction" xid, pid integer, "mode" text, "granted" boolean);
1286  pg_prepared_statements   | SELECT p."name", p."statement", p.prepare_time, p.parameter_types, p.from_sql FROM pg_prepared_statement() p("name" text, "statement" text, prepare_time timestamp with time zone, parameter_types regtype[], from_sql boolean);
1287  pg_prepared_xacts        | SELECT p."transaction", p.gid, p."prepared", u.rolname AS "owner", d.datname AS "database" FROM ((pg_prepared_xact() p("transaction" xid, gid text, "prepared" timestamp with time zone, ownerid oid, dbid oid) LEFT JOIN pg_authid u ON ((p.ownerid = u.oid))) LEFT JOIN pg_database d ON ((p.dbid = d.oid)));
1288  pg_roles                 | SELECT pg_authid.rolname, pg_authid.rolsuper, pg_authid.rolinherit, pg_authid.rolcreaterole, pg_authid.rolcreatedb, pg_authid.rolcatupdate, pg_authid.rolcanlogin, pg_authid.rolconnlimit, '********'::text AS rolpassword, pg_authid.rolvaliduntil, pg_authid.rolconfig, pg_authid.oid FROM pg_authid;
1289  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");
1290  pg_settings              | SELECT a."name", a.setting, a.unit, 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, unit text, category text, short_desc text, extra_desc text, context text, vartype text, source text, min_val text, max_val text);
1291  pg_shadow                | SELECT pg_authid.rolname AS usename, pg_authid.oid AS usesysid, pg_authid.rolcreatedb AS usecreatedb, pg_authid.rolsuper AS usesuper, pg_authid.rolcatupdate AS usecatupd, pg_authid.rolpassword AS passwd, (pg_authid.rolvaliduntil)::abstime AS valuntil, pg_authid.rolconfig AS useconfig FROM pg_authid WHERE pg_authid.rolcanlogin;
1292  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.rolname AS usename, pg_stat_get_backend_activity(s.backendid) AS current_query, pg_stat_get_backend_waiting(s.backendid) AS waiting, pg_stat_get_backend_txn_start(s.backendid) AS txn_start, pg_stat_get_backend_activity_start(s.backendid) AS query_start, pg_stat_get_backend_start(s.backendid) AS backend_start, pg_stat_get_backend_client_addr(s.backendid) AS client_addr, pg_stat_get_backend_client_port(s.backendid) AS client_port FROM pg_database d, (SELECT pg_stat_get_backend_idset() AS backendid) s, pg_authid u WHERE ((pg_stat_get_backend_dbid(s.backendid) = d.oid) AND (pg_stat_get_backend_userid(s.backendid) = u.oid));
1293  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 = ANY (ARRAY['r'::"char", 't'::"char"]));
1294  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)))::bigint AS idx_scan, ((sum(pg_stat_get_tuples_fetched(i.indexrelid)))::bigint + pg_stat_get_tuples_fetched(c.oid)) 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, pg_stat_get_live_tuples(c.oid) AS n_live_tup, pg_stat_get_dead_tuples(c.oid) AS n_dead_tup, pg_stat_get_last_vacuum_time(c.oid) AS last_vacuum, pg_stat_get_last_autovacuum_time(c.oid) AS last_autovacuum, pg_stat_get_last_analyze_time(c.oid) AS last_analyze, pg_stat_get_last_autoanalyze_time(c.oid) AS last_autoanalyze 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 = ANY (ARRAY['r'::"char", 't'::"char"])) GROUP BY c.oid, n.nspname, c.relname;
1295  pg_stat_bgwriter         | SELECT pg_stat_get_bgwriter_timed_checkpoints() AS checkpoints_timed, pg_stat_get_bgwriter_requested_checkpoints() AS checkpoints_req, pg_stat_get_bgwriter_buf_written_checkpoints() AS buffers_checkpoint, pg_stat_get_bgwriter_buf_written_lru() AS buffers_lru, pg_stat_get_bgwriter_buf_written_all() AS buffers_all, pg_stat_get_bgwriter_maxwritten_lru() AS maxwritten_lru, pg_stat_get_bgwriter_maxwritten_all() AS maxwritten_all;
1296  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, pg_stat_get_db_tuples_returned(d.oid) AS tup_returned, pg_stat_get_db_tuples_fetched(d.oid) AS tup_fetched, pg_stat_get_db_tuples_inserted(d.oid) AS tup_inserted, pg_stat_get_db_tuples_updated(d.oid) AS tup_updated, pg_stat_get_db_tuples_deleted(d.oid) AS tup_deleted FROM pg_database d;
1297  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 = ANY (ARRAY['pg_catalog'::"name", 'pg_toast'::"name", 'information_schema'::"name"]));
1298  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, pg_stat_all_tables.n_live_tup, pg_stat_all_tables.n_dead_tup, pg_stat_all_tables.last_vacuum, pg_stat_all_tables.last_autovacuum, pg_stat_all_tables.last_analyze, pg_stat_all_tables.last_autoanalyze FROM pg_stat_all_tables WHERE (pg_stat_all_tables.schemaname = ANY (ARRAY['pg_catalog'::"name", 'pg_toast'::"name", 'information_schema'::"name"]));
1299  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 <> ALL (ARRAY['pg_catalog'::"name", 'pg_toast'::"name", 'information_schema'::"name"]));
1300  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, pg_stat_all_tables.n_live_tup, pg_stat_all_tables.n_dead_tup, pg_stat_all_tables.last_vacuum, pg_stat_all_tables.last_autovacuum, pg_stat_all_tables.last_analyze, pg_stat_all_tables.last_autoanalyze FROM pg_stat_all_tables WHERE (pg_stat_all_tables.schemaname <> ALL (ARRAY['pg_catalog'::"name", 'pg_toast'::"name", 'information_schema'::"name"]));
1301  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 = ANY (ARRAY['r'::"char", 't'::"char"]));
1302  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");
1303  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))))::bigint AS idx_blks_read, (sum(pg_stat_get_blocks_hit(i.indexrelid)))::bigint 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 = ANY (ARRAY['r'::"char", 't'::"char"])) GROUP BY c.oid, n.nspname, c.relname, t.oid, x.oid;
1304  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 = ANY (ARRAY['pg_catalog'::"name", 'pg_toast'::"name", 'information_schema'::"name"]));
1305  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 = ANY (ARRAY['pg_catalog'::"name", 'pg_toast'::"name", 'information_schema'::"name"]));
1306  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 = ANY (ARRAY['pg_catalog'::"name", 'pg_toast'::"name", 'information_schema'::"name"]));
1307  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 <> ALL (ARRAY['pg_catalog'::"name", 'pg_toast'::"name", 'information_schema'::"name"]));
1308  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 <> ALL (ARRAY['pg_catalog'::"name", 'pg_toast'::"name", 'information_schema'::"name"]));
1309  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 <> ALL (ARRAY['pg_catalog'::"name", 'pg_toast'::"name", 'information_schema'::"name"]));
1310  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::anyarray 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::anyarray 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);
1311  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");
1312  pg_timezone_abbrevs      | SELECT pg_timezone_abbrevs.abbrev, pg_timezone_abbrevs.utc_offset, pg_timezone_abbrevs.is_dst FROM pg_timezone_abbrevs() pg_timezone_abbrevs(abbrev, utc_offset, is_dst);
1313  pg_timezone_names        | SELECT pg_timezone_names."name", pg_timezone_names.abbrev, pg_timezone_names.utc_offset, pg_timezone_names.is_dst FROM pg_timezone_names() pg_timezone_names("name", abbrev, utc_offset, is_dst);
1314  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;
1315  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");
1316  rtest_v1                 | SELECT rtest_t1.a, rtest_t1.b FROM rtest_t1;
1317  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);
1318  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)));
1319  rtest_vview2             | SELECT rtest_view1.a, rtest_view1.b FROM rtest_view1 WHERE rtest_view1.v;
1320  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)));
1321  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;
1322  rtest_vview5             | SELECT rtest_view1.a, rtest_view1.b, rtest_viewfunc1(rtest_view1.a) AS refcount FROM rtest_view1;
1323  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);
1324  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));
1325  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);
1326  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);
1327  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))));
1328  street                   | SELECT r."name", r.thepath, c.cname FROM ONLY road r, real_city c WHERE (c.outline ## r.thepath);
1329  toyemp                   | SELECT emp."name", emp.age, emp."location", (12 * emp.salary) AS annualsal FROM emp;
1330 (49 rows)
1331
1332 SELECT tablename, rulename, definition FROM pg_rules 
1333         ORDER BY tablename, rulename;
1334    tablename   |    rulename     |                                                                                                                                   definition                                                                                                                                    
1335 ---------------+-----------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1336  pg_settings   | pg_settings_n   | CREATE RULE pg_settings_n AS ON UPDATE TO pg_settings DO INSTEAD NOTHING;
1337  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;
1338  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);
1339  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);
1340  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);
1341  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;
1342  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;
1343  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);
1344  rtest_nothn2  | rtest_nothn_r4  | CREATE RULE rtest_nothn_r4 AS ON INSERT TO rtest_nothn2 DO INSTEAD NOTHING;
1345  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'::regclass), 'rule 1 - this should run 1st'::text);
1346  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'::regclass), 'rule 2 - this should run 2nd'::text);
1347  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'::regclass), 'rule 3 - this should run 3rd'::text);
1348  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'::regclass), 'rule 4 - this should run 4th'::text);
1349  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);
1350  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);
1351  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); );
1352  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); );
1353  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);
1354  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);
1355  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);
1356  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);
1357  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);
1358  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);
1359  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);
1360  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);
1361  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);
1362  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);
1363  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);
1364  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);
1365 (29 rows)
1366
1367 --
1368 -- CREATE OR REPLACE RULE
1369 --
1370 CREATE TABLE ruletest_tbl (a int, b int);
1371 CREATE TABLE ruletest_tbl2 (a int, b int);
1372 CREATE OR REPLACE RULE myrule AS ON INSERT TO ruletest_tbl
1373         DO INSTEAD INSERT INTO ruletest_tbl2 VALUES (10, 10);
1374 INSERT INTO ruletest_tbl VALUES (99, 99);
1375 CREATE OR REPLACE RULE myrule AS ON INSERT TO ruletest_tbl
1376         DO INSTEAD INSERT INTO ruletest_tbl2 VALUES (1000, 1000);
1377 INSERT INTO ruletest_tbl VALUES (99, 99);
1378 SELECT * FROM ruletest_tbl2;
1379   a   |  b   
1380 ------+------
1381    10 |   10
1382  1000 | 1000
1383 (2 rows)
1384
1385 -- Check that rewrite rules splitting one INSERT into multiple
1386 -- conditional statements does not disable FK checking.
1387 create table rule_and_refint_t1 (
1388         id1a integer,
1389         id1b integer,
1390         
1391         primary key (id1a, id1b)
1392 );
1393 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "rule_and_refint_t1_pkey" for table "rule_and_refint_t1"
1394 create table rule_and_refint_t2 (
1395         id2a integer,
1396         id2c integer,
1397         
1398         primary key (id2a, id2c)
1399 );
1400 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "rule_and_refint_t2_pkey" for table "rule_and_refint_t2"
1401 create table rule_and_refint_t3 (
1402         id3a integer,
1403         id3b integer,
1404         id3c integer,
1405         data text,
1406         primary key (id3a, id3b, id3c),
1407         foreign key (id3a, id3b) references rule_and_refint_t1 (id1a, id1b),
1408         foreign key (id3a, id3c) references rule_and_refint_t2 (id2a, id2c)
1409 );
1410 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "rule_and_refint_t3_pkey" for table "rule_and_refint_t3"
1411 insert into rule_and_refint_t1 values (1, 11);
1412 insert into rule_and_refint_t1 values (1, 12);
1413 insert into rule_and_refint_t1 values (2, 21);
1414 insert into rule_and_refint_t1 values (2, 22);
1415 insert into rule_and_refint_t2 values (1, 11);
1416 insert into rule_and_refint_t2 values (1, 12);
1417 insert into rule_and_refint_t2 values (2, 21);
1418 insert into rule_and_refint_t2 values (2, 22);
1419 insert into rule_and_refint_t3 values (1, 11, 11, 'row1');
1420 insert into rule_and_refint_t3 values (1, 11, 12, 'row2');
1421 insert into rule_and_refint_t3 values (1, 12, 11, 'row3');
1422 insert into rule_and_refint_t3 values (1, 12, 12, 'row4');
1423 insert into rule_and_refint_t3 values (1, 11, 13, 'row5');
1424 ERROR:  insert or update on table "rule_and_refint_t3" violates foreign key constraint "rule_and_refint_t3_id3a_fkey1"
1425 DETAIL:  Key (id3a,id3c)=(1,13) is not present in table "rule_and_refint_t2".
1426 insert into rule_and_refint_t3 values (1, 13, 11, 'row6');
1427 ERROR:  insert or update on table "rule_and_refint_t3" violates foreign key constraint "rule_and_refint_t3_id3a_fkey"
1428 DETAIL:  Key (id3a,id3b)=(1,13) is not present in table "rule_and_refint_t1".
1429 create rule rule_and_refint_t3_ins as on insert to rule_and_refint_t3
1430         where (exists (select 1 from rule_and_refint_t3
1431                         where (((rule_and_refint_t3.id3a = new.id3a)
1432                         and (rule_and_refint_t3.id3b = new.id3b))
1433                         and (rule_and_refint_t3.id3c = new.id3c))))
1434         do instead update rule_and_refint_t3 set data = new.data
1435         where (((rule_and_refint_t3.id3a = new.id3a)
1436         and (rule_and_refint_t3.id3b = new.id3b))
1437         and (rule_and_refint_t3.id3c = new.id3c));
1438 insert into rule_and_refint_t3 values (1, 11, 13, 'row7');
1439 ERROR:  insert or update on table "rule_and_refint_t3" violates foreign key constraint "rule_and_refint_t3_id3a_fkey1"
1440 DETAIL:  Key (id3a,id3c)=(1,13) is not present in table "rule_and_refint_t2".
1441 insert into rule_and_refint_t3 values (1, 13, 11, 'row8');
1442 ERROR:  insert or update on table "rule_and_refint_t3" violates foreign key constraint "rule_and_refint_t3_id3a_fkey"
1443 DETAIL:  Key (id3a,id3b)=(1,13) is not present in table "rule_and_refint_t1".
1444 --
1445 -- check for planner problems with complex inherited UPDATES
1446 --
1447 create table id (id serial primary key, name text);
1448 NOTICE:  CREATE TABLE will create implicit sequence "id_id_seq" for serial column "id.id"
1449 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "id_pkey" for table "id"
1450 -- currently, must respecify PKEY for each inherited subtable
1451 create table test_1 (id integer primary key) inherits (id);
1452 NOTICE:  merging column "id" with inherited definition
1453 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "test_1_pkey" for table "test_1"
1454 create table test_2 (id integer primary key) inherits (id);
1455 NOTICE:  merging column "id" with inherited definition
1456 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "test_2_pkey" for table "test_2"
1457 create table test_3 (id integer primary key) inherits (id);
1458 NOTICE:  merging column "id" with inherited definition
1459 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "test_3_pkey" for table "test_3"
1460 insert into test_1 (name) values ('Test 1');
1461 insert into test_1 (name) values ('Test 2');
1462 insert into test_2 (name) values ('Test 3');
1463 insert into test_2 (name) values ('Test 4');
1464 insert into test_3 (name) values ('Test 5');
1465 insert into test_3 (name) values ('Test 6');
1466 create view id_ordered as select * from id order by id;
1467 create rule update_id_ordered as on update to id_ordered
1468         do instead update id set name = new.name where id = old.id;
1469 select * from id_ordered;
1470  id |  name  
1471 ----+--------
1472   1 | Test 1
1473   2 | Test 2
1474   3 | Test 3
1475   4 | Test 4
1476   5 | Test 5
1477   6 | Test 6
1478 (6 rows)
1479
1480 update id_ordered set name = 'update 2' where id = 2;
1481 update id_ordered set name = 'update 4' where id = 4;
1482 update id_ordered set name = 'update 5' where id = 5;
1483 select * from id_ordered;
1484  id |   name   
1485 ----+----------
1486   1 | Test 1
1487   2 | update 2
1488   3 | Test 3
1489   4 | update 4
1490   5 | update 5
1491   6 | Test 6
1492 (6 rows)
1493
1494 set client_min_messages to warning; -- suppress cascade notices
1495 drop table id cascade;
1496 reset client_min_messages;
1497 --
1498 -- check corner case where an entirely-dummy subplan is created by
1499 -- constraint exclusion
1500 --
1501 create temp table t1 (a integer primary key);
1502 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "t1_pkey" for table "t1"
1503 create temp table t1_1 (check (a >= 0 and a < 10)) inherits (t1);
1504 create temp table t1_2 (check (a >= 10 and a < 20)) inherits (t1);
1505 create rule t1_ins_1 as on insert to t1 
1506         where new.a >= 0 and new.a < 10
1507         do instead
1508         insert into t1_1 values (new.a);
1509 create rule t1_ins_2 as on insert to t1 
1510         where new.a >= 10 and new.a < 20
1511         do instead
1512         insert into t1_2 values (new.a);
1513 create rule t1_upd_1 as on update to t1
1514         where old.a >= 0 and old.a < 10
1515         do instead
1516         update t1_1 set a = new.a where a = old.a;
1517 create rule t1_upd_2 as on update to t1
1518         where old.a >= 10 and old.a < 20
1519         do instead
1520         update t1_2 set a = new.a where a = old.a;
1521 set constraint_exclusion = on;
1522 insert into t1 select * from generate_series(5,19,1) g;
1523 update t1 set a = 4 where a = 5;
1524 select * from only t1;
1525  a 
1526 ---
1527 (0 rows)
1528
1529 select * from only t1_1;
1530  a 
1531 ---
1532  6
1533  7
1534  8
1535  9
1536  4
1537 (5 rows)
1538
1539 select * from only t1_2;
1540  a  
1541 ----
1542  10
1543  11
1544  12
1545  13
1546  14
1547  15
1548  16
1549  17
1550  18
1551  19
1552 (10 rows)
1553