]> granicus.if.org Git - postgresql/blob - src/test/regress/expected/inherit.out
psql: update "replica identity" display for \d+
[postgresql] / src / test / regress / expected / inherit.out
1 --
2 -- Test inheritance features
3 --
4 CREATE TABLE a (aa TEXT);
5 CREATE TABLE b (bb TEXT) INHERITS (a);
6 CREATE TABLE c (cc TEXT) INHERITS (a);
7 CREATE TABLE d (dd TEXT) INHERITS (b,c,a);
8 NOTICE:  merging multiple inherited definitions of column "aa"
9 NOTICE:  merging multiple inherited definitions of column "aa"
10 INSERT INTO a(aa) VALUES('aaa');
11 INSERT INTO a(aa) VALUES('aaaa');
12 INSERT INTO a(aa) VALUES('aaaaa');
13 INSERT INTO a(aa) VALUES('aaaaaa');
14 INSERT INTO a(aa) VALUES('aaaaaaa');
15 INSERT INTO a(aa) VALUES('aaaaaaaa');
16 INSERT INTO b(aa) VALUES('bbb');
17 INSERT INTO b(aa) VALUES('bbbb');
18 INSERT INTO b(aa) VALUES('bbbbb');
19 INSERT INTO b(aa) VALUES('bbbbbb');
20 INSERT INTO b(aa) VALUES('bbbbbbb');
21 INSERT INTO b(aa) VALUES('bbbbbbbb');
22 INSERT INTO c(aa) VALUES('ccc');
23 INSERT INTO c(aa) VALUES('cccc');
24 INSERT INTO c(aa) VALUES('ccccc');
25 INSERT INTO c(aa) VALUES('cccccc');
26 INSERT INTO c(aa) VALUES('ccccccc');
27 INSERT INTO c(aa) VALUES('cccccccc');
28 INSERT INTO d(aa) VALUES('ddd');
29 INSERT INTO d(aa) VALUES('dddd');
30 INSERT INTO d(aa) VALUES('ddddd');
31 INSERT INTO d(aa) VALUES('dddddd');
32 INSERT INTO d(aa) VALUES('ddddddd');
33 INSERT INTO d(aa) VALUES('dddddddd');
34 SELECT relname, a.* FROM a, pg_class where a.tableoid = pg_class.oid;
35  relname |    aa    
36 ---------+----------
37  a       | aaa
38  a       | aaaa
39  a       | aaaaa
40  a       | aaaaaa
41  a       | aaaaaaa
42  a       | aaaaaaaa
43  b       | bbb
44  b       | bbbb
45  b       | bbbbb
46  b       | bbbbbb
47  b       | bbbbbbb
48  b       | bbbbbbbb
49  c       | ccc
50  c       | cccc
51  c       | ccccc
52  c       | cccccc
53  c       | ccccccc
54  c       | cccccccc
55  d       | ddd
56  d       | dddd
57  d       | ddddd
58  d       | dddddd
59  d       | ddddddd
60  d       | dddddddd
61 (24 rows)
62
63 SELECT relname, b.* FROM b, pg_class where b.tableoid = pg_class.oid;
64  relname |    aa    | bb 
65 ---------+----------+----
66  b       | bbb      | 
67  b       | bbbb     | 
68  b       | bbbbb    | 
69  b       | bbbbbb   | 
70  b       | bbbbbbb  | 
71  b       | bbbbbbbb | 
72  d       | ddd      | 
73  d       | dddd     | 
74  d       | ddddd    | 
75  d       | dddddd   | 
76  d       | ddddddd  | 
77  d       | dddddddd | 
78 (12 rows)
79
80 SELECT relname, c.* FROM c, pg_class where c.tableoid = pg_class.oid;
81  relname |    aa    | cc 
82 ---------+----------+----
83  c       | ccc      | 
84  c       | cccc     | 
85  c       | ccccc    | 
86  c       | cccccc   | 
87  c       | ccccccc  | 
88  c       | cccccccc | 
89  d       | ddd      | 
90  d       | dddd     | 
91  d       | ddddd    | 
92  d       | dddddd   | 
93  d       | ddddddd  | 
94  d       | dddddddd | 
95 (12 rows)
96
97 SELECT relname, d.* FROM d, pg_class where d.tableoid = pg_class.oid;
98  relname |    aa    | bb | cc | dd 
99 ---------+----------+----+----+----
100  d       | ddd      |    |    | 
101  d       | dddd     |    |    | 
102  d       | ddddd    |    |    | 
103  d       | dddddd   |    |    | 
104  d       | ddddddd  |    |    | 
105  d       | dddddddd |    |    | 
106 (6 rows)
107
108 SELECT relname, a.* FROM ONLY a, pg_class where a.tableoid = pg_class.oid;
109  relname |    aa    
110 ---------+----------
111  a       | aaa
112  a       | aaaa
113  a       | aaaaa
114  a       | aaaaaa
115  a       | aaaaaaa
116  a       | aaaaaaaa
117 (6 rows)
118
119 SELECT relname, b.* FROM ONLY b, pg_class where b.tableoid = pg_class.oid;
120  relname |    aa    | bb 
121 ---------+----------+----
122  b       | bbb      | 
123  b       | bbbb     | 
124  b       | bbbbb    | 
125  b       | bbbbbb   | 
126  b       | bbbbbbb  | 
127  b       | bbbbbbbb | 
128 (6 rows)
129
130 SELECT relname, c.* FROM ONLY c, pg_class where c.tableoid = pg_class.oid;
131  relname |    aa    | cc 
132 ---------+----------+----
133  c       | ccc      | 
134  c       | cccc     | 
135  c       | ccccc    | 
136  c       | cccccc   | 
137  c       | ccccccc  | 
138  c       | cccccccc | 
139 (6 rows)
140
141 SELECT relname, d.* FROM ONLY d, pg_class where d.tableoid = pg_class.oid;
142  relname |    aa    | bb | cc | dd 
143 ---------+----------+----+----+----
144  d       | ddd      |    |    | 
145  d       | dddd     |    |    | 
146  d       | ddddd    |    |    | 
147  d       | dddddd   |    |    | 
148  d       | ddddddd  |    |    | 
149  d       | dddddddd |    |    | 
150 (6 rows)
151
152 UPDATE a SET aa='zzzz' WHERE aa='aaaa';
153 UPDATE ONLY a SET aa='zzzzz' WHERE aa='aaaaa';
154 UPDATE b SET aa='zzz' WHERE aa='aaa';
155 UPDATE ONLY b SET aa='zzz' WHERE aa='aaa';
156 UPDATE a SET aa='zzzzzz' WHERE aa LIKE 'aaa%';
157 SELECT relname, a.* FROM a, pg_class where a.tableoid = pg_class.oid;
158  relname |    aa    
159 ---------+----------
160  a       | zzzz
161  a       | zzzzz
162  a       | zzzzzz
163  a       | zzzzzz
164  a       | zzzzzz
165  a       | zzzzzz
166  b       | bbb
167  b       | bbbb
168  b       | bbbbb
169  b       | bbbbbb
170  b       | bbbbbbb
171  b       | bbbbbbbb
172  c       | ccc
173  c       | cccc
174  c       | ccccc
175  c       | cccccc
176  c       | ccccccc
177  c       | cccccccc
178  d       | ddd
179  d       | dddd
180  d       | ddddd
181  d       | dddddd
182  d       | ddddddd
183  d       | dddddddd
184 (24 rows)
185
186 SELECT relname, b.* FROM b, pg_class where b.tableoid = pg_class.oid;
187  relname |    aa    | bb 
188 ---------+----------+----
189  b       | bbb      | 
190  b       | bbbb     | 
191  b       | bbbbb    | 
192  b       | bbbbbb   | 
193  b       | bbbbbbb  | 
194  b       | bbbbbbbb | 
195  d       | ddd      | 
196  d       | dddd     | 
197  d       | ddddd    | 
198  d       | dddddd   | 
199  d       | ddddddd  | 
200  d       | dddddddd | 
201 (12 rows)
202
203 SELECT relname, c.* FROM c, pg_class where c.tableoid = pg_class.oid;
204  relname |    aa    | cc 
205 ---------+----------+----
206  c       | ccc      | 
207  c       | cccc     | 
208  c       | ccccc    | 
209  c       | cccccc   | 
210  c       | ccccccc  | 
211  c       | cccccccc | 
212  d       | ddd      | 
213  d       | dddd     | 
214  d       | ddddd    | 
215  d       | dddddd   | 
216  d       | ddddddd  | 
217  d       | dddddddd | 
218 (12 rows)
219
220 SELECT relname, d.* FROM d, pg_class where d.tableoid = pg_class.oid;
221  relname |    aa    | bb | cc | dd 
222 ---------+----------+----+----+----
223  d       | ddd      |    |    | 
224  d       | dddd     |    |    | 
225  d       | ddddd    |    |    | 
226  d       | dddddd   |    |    | 
227  d       | ddddddd  |    |    | 
228  d       | dddddddd |    |    | 
229 (6 rows)
230
231 SELECT relname, a.* FROM ONLY a, pg_class where a.tableoid = pg_class.oid;
232  relname |   aa   
233 ---------+--------
234  a       | zzzz
235  a       | zzzzz
236  a       | zzzzzz
237  a       | zzzzzz
238  a       | zzzzzz
239  a       | zzzzzz
240 (6 rows)
241
242 SELECT relname, b.* FROM ONLY b, pg_class where b.tableoid = pg_class.oid;
243  relname |    aa    | bb 
244 ---------+----------+----
245  b       | bbb      | 
246  b       | bbbb     | 
247  b       | bbbbb    | 
248  b       | bbbbbb   | 
249  b       | bbbbbbb  | 
250  b       | bbbbbbbb | 
251 (6 rows)
252
253 SELECT relname, c.* FROM ONLY c, pg_class where c.tableoid = pg_class.oid;
254  relname |    aa    | cc 
255 ---------+----------+----
256  c       | ccc      | 
257  c       | cccc     | 
258  c       | ccccc    | 
259  c       | cccccc   | 
260  c       | ccccccc  | 
261  c       | cccccccc | 
262 (6 rows)
263
264 SELECT relname, d.* FROM ONLY d, pg_class where d.tableoid = pg_class.oid;
265  relname |    aa    | bb | cc | dd 
266 ---------+----------+----+----+----
267  d       | ddd      |    |    | 
268  d       | dddd     |    |    | 
269  d       | ddddd    |    |    | 
270  d       | dddddd   |    |    | 
271  d       | ddddddd  |    |    | 
272  d       | dddddddd |    |    | 
273 (6 rows)
274
275 UPDATE b SET aa='new';
276 SELECT relname, a.* FROM a, pg_class where a.tableoid = pg_class.oid;
277  relname |    aa    
278 ---------+----------
279  a       | zzzz
280  a       | zzzzz
281  a       | zzzzzz
282  a       | zzzzzz
283  a       | zzzzzz
284  a       | zzzzzz
285  b       | new
286  b       | new
287  b       | new
288  b       | new
289  b       | new
290  b       | new
291  c       | ccc
292  c       | cccc
293  c       | ccccc
294  c       | cccccc
295  c       | ccccccc
296  c       | cccccccc
297  d       | new
298  d       | new
299  d       | new
300  d       | new
301  d       | new
302  d       | new
303 (24 rows)
304
305 SELECT relname, b.* FROM b, pg_class where b.tableoid = pg_class.oid;
306  relname | aa  | bb 
307 ---------+-----+----
308  b       | new | 
309  b       | new | 
310  b       | new | 
311  b       | new | 
312  b       | new | 
313  b       | new | 
314  d       | new | 
315  d       | new | 
316  d       | new | 
317  d       | new | 
318  d       | new | 
319  d       | new | 
320 (12 rows)
321
322 SELECT relname, c.* FROM c, pg_class where c.tableoid = pg_class.oid;
323  relname |    aa    | cc 
324 ---------+----------+----
325  c       | ccc      | 
326  c       | cccc     | 
327  c       | ccccc    | 
328  c       | cccccc   | 
329  c       | ccccccc  | 
330  c       | cccccccc | 
331  d       | new      | 
332  d       | new      | 
333  d       | new      | 
334  d       | new      | 
335  d       | new      | 
336  d       | new      | 
337 (12 rows)
338
339 SELECT relname, d.* FROM d, pg_class where d.tableoid = pg_class.oid;
340  relname | aa  | bb | cc | dd 
341 ---------+-----+----+----+----
342  d       | new |    |    | 
343  d       | new |    |    | 
344  d       | new |    |    | 
345  d       | new |    |    | 
346  d       | new |    |    | 
347  d       | new |    |    | 
348 (6 rows)
349
350 SELECT relname, a.* FROM ONLY a, pg_class where a.tableoid = pg_class.oid;
351  relname |   aa   
352 ---------+--------
353  a       | zzzz
354  a       | zzzzz
355  a       | zzzzzz
356  a       | zzzzzz
357  a       | zzzzzz
358  a       | zzzzzz
359 (6 rows)
360
361 SELECT relname, b.* FROM ONLY b, pg_class where b.tableoid = pg_class.oid;
362  relname | aa  | bb 
363 ---------+-----+----
364  b       | new | 
365  b       | new | 
366  b       | new | 
367  b       | new | 
368  b       | new | 
369  b       | new | 
370 (6 rows)
371
372 SELECT relname, c.* FROM ONLY c, pg_class where c.tableoid = pg_class.oid;
373  relname |    aa    | cc 
374 ---------+----------+----
375  c       | ccc      | 
376  c       | cccc     | 
377  c       | ccccc    | 
378  c       | cccccc   | 
379  c       | ccccccc  | 
380  c       | cccccccc | 
381 (6 rows)
382
383 SELECT relname, d.* FROM ONLY d, pg_class where d.tableoid = pg_class.oid;
384  relname | aa  | bb | cc | dd 
385 ---------+-----+----+----+----
386  d       | new |    |    | 
387  d       | new |    |    | 
388  d       | new |    |    | 
389  d       | new |    |    | 
390  d       | new |    |    | 
391  d       | new |    |    | 
392 (6 rows)
393
394 UPDATE a SET aa='new';
395 DELETE FROM ONLY c WHERE aa='new';
396 SELECT relname, a.* FROM a, pg_class where a.tableoid = pg_class.oid;
397  relname | aa  
398 ---------+-----
399  a       | new
400  a       | new
401  a       | new
402  a       | new
403  a       | new
404  a       | new
405  b       | new
406  b       | new
407  b       | new
408  b       | new
409  b       | new
410  b       | new
411  d       | new
412  d       | new
413  d       | new
414  d       | new
415  d       | new
416  d       | new
417 (18 rows)
418
419 SELECT relname, b.* FROM b, pg_class where b.tableoid = pg_class.oid;
420  relname | aa  | bb 
421 ---------+-----+----
422  b       | new | 
423  b       | new | 
424  b       | new | 
425  b       | new | 
426  b       | new | 
427  b       | new | 
428  d       | new | 
429  d       | new | 
430  d       | new | 
431  d       | new | 
432  d       | new | 
433  d       | new | 
434 (12 rows)
435
436 SELECT relname, c.* FROM c, pg_class where c.tableoid = pg_class.oid;
437  relname | aa  | cc 
438 ---------+-----+----
439  d       | new | 
440  d       | new | 
441  d       | new | 
442  d       | new | 
443  d       | new | 
444  d       | new | 
445 (6 rows)
446
447 SELECT relname, d.* FROM d, pg_class where d.tableoid = pg_class.oid;
448  relname | aa  | bb | cc | dd 
449 ---------+-----+----+----+----
450  d       | new |    |    | 
451  d       | new |    |    | 
452  d       | new |    |    | 
453  d       | new |    |    | 
454  d       | new |    |    | 
455  d       | new |    |    | 
456 (6 rows)
457
458 SELECT relname, a.* FROM ONLY a, pg_class where a.tableoid = pg_class.oid;
459  relname | aa  
460 ---------+-----
461  a       | new
462  a       | new
463  a       | new
464  a       | new
465  a       | new
466  a       | new
467 (6 rows)
468
469 SELECT relname, b.* FROM ONLY b, pg_class where b.tableoid = pg_class.oid;
470  relname | aa  | bb 
471 ---------+-----+----
472  b       | new | 
473  b       | new | 
474  b       | new | 
475  b       | new | 
476  b       | new | 
477  b       | new | 
478 (6 rows)
479
480 SELECT relname, c.* FROM ONLY c, pg_class where c.tableoid = pg_class.oid;
481  relname | aa | cc 
482 ---------+----+----
483 (0 rows)
484
485 SELECT relname, d.* FROM ONLY d, pg_class where d.tableoid = pg_class.oid;
486  relname | aa  | bb | cc | dd 
487 ---------+-----+----+----+----
488  d       | new |    |    | 
489  d       | new |    |    | 
490  d       | new |    |    | 
491  d       | new |    |    | 
492  d       | new |    |    | 
493  d       | new |    |    | 
494 (6 rows)
495
496 DELETE FROM a;
497 SELECT relname, a.* FROM a, pg_class where a.tableoid = pg_class.oid;
498  relname | aa 
499 ---------+----
500 (0 rows)
501
502 SELECT relname, b.* FROM b, pg_class where b.tableoid = pg_class.oid;
503  relname | aa | bb 
504 ---------+----+----
505 (0 rows)
506
507 SELECT relname, c.* FROM c, pg_class where c.tableoid = pg_class.oid;
508  relname | aa | cc 
509 ---------+----+----
510 (0 rows)
511
512 SELECT relname, d.* FROM d, pg_class where d.tableoid = pg_class.oid;
513  relname | aa | bb | cc | dd 
514 ---------+----+----+----+----
515 (0 rows)
516
517 SELECT relname, a.* FROM ONLY a, pg_class where a.tableoid = pg_class.oid;
518  relname | aa 
519 ---------+----
520 (0 rows)
521
522 SELECT relname, b.* FROM ONLY b, pg_class where b.tableoid = pg_class.oid;
523  relname | aa | bb 
524 ---------+----+----
525 (0 rows)
526
527 SELECT relname, c.* FROM ONLY c, pg_class where c.tableoid = pg_class.oid;
528  relname | aa | cc 
529 ---------+----+----
530 (0 rows)
531
532 SELECT relname, d.* FROM ONLY d, pg_class where d.tableoid = pg_class.oid;
533  relname | aa | bb | cc | dd 
534 ---------+----+----+----+----
535 (0 rows)
536
537 -- Confirm PRIMARY KEY adds NOT NULL constraint to child table
538 CREATE TEMP TABLE z (b TEXT, PRIMARY KEY(aa, b)) inherits (a);
539 INSERT INTO z VALUES (NULL, 'text'); -- should fail
540 ERROR:  null value in column "aa" violates not-null constraint
541 DETAIL:  Failing row contains (null, text).
542 -- Check UPDATE with inherited target and an inherited source table
543 create temp table foo(f1 int, f2 int);
544 create temp table foo2(f3 int) inherits (foo);
545 create temp table bar(f1 int, f2 int);
546 create temp table bar2(f3 int) inherits (bar);
547 insert into foo values(1,1);
548 insert into foo values(3,3);
549 insert into foo2 values(2,2,2);
550 insert into foo2 values(3,3,3);
551 insert into bar values(1,1);
552 insert into bar values(2,2);
553 insert into bar values(3,3);
554 insert into bar values(4,4);
555 insert into bar2 values(1,1,1);
556 insert into bar2 values(2,2,2);
557 insert into bar2 values(3,3,3);
558 insert into bar2 values(4,4,4);
559 update bar set f2 = f2 + 100 where f1 in (select f1 from foo);
560 select tableoid::regclass::text as relname, bar.* from bar order by 1,2;
561  relname | f1 | f2  
562 ---------+----+-----
563  bar     |  1 | 101
564  bar     |  2 | 102
565  bar     |  3 | 103
566  bar     |  4 |   4
567  bar2    |  1 | 101
568  bar2    |  2 | 102
569  bar2    |  3 | 103
570  bar2    |  4 |   4
571 (8 rows)
572
573 -- Check UPDATE with inherited target and an appendrel subquery
574 update bar set f2 = f2 + 100
575 from
576   ( select f1 from foo union all select f1+3 from foo ) ss
577 where bar.f1 = ss.f1;
578 select tableoid::regclass::text as relname, bar.* from bar order by 1,2;
579  relname | f1 | f2  
580 ---------+----+-----
581  bar     |  1 | 201
582  bar     |  2 | 202
583  bar     |  3 | 203
584  bar     |  4 | 104
585  bar2    |  1 | 201
586  bar2    |  2 | 202
587  bar2    |  3 | 203
588  bar2    |  4 | 104
589 (8 rows)
590
591 /* Test multiple inheritance of column defaults */
592 CREATE TABLE firstparent (tomorrow date default now()::date + 1);
593 CREATE TABLE secondparent (tomorrow date default  now() :: date  +  1);
594 CREATE TABLE jointchild () INHERITS (firstparent, secondparent);  -- ok
595 NOTICE:  merging multiple inherited definitions of column "tomorrow"
596 CREATE TABLE thirdparent (tomorrow date default now()::date - 1);
597 CREATE TABLE otherchild () INHERITS (firstparent, thirdparent);  -- not ok
598 NOTICE:  merging multiple inherited definitions of column "tomorrow"
599 ERROR:  column "tomorrow" inherits conflicting default values
600 HINT:  To resolve the conflict, specify a default explicitly.
601 CREATE TABLE otherchild (tomorrow date default now())
602   INHERITS (firstparent, thirdparent);  -- ok, child resolves ambiguous default
603 NOTICE:  merging multiple inherited definitions of column "tomorrow"
604 NOTICE:  merging column "tomorrow" with inherited definition
605 DROP TABLE firstparent, secondparent, jointchild, thirdparent, otherchild;
606 -- Test changing the type of inherited columns
607 insert into d values('test','one','two','three');
608 alter table a alter column aa type integer using bit_length(aa);
609 select * from d;
610  aa | bb  | cc  |  dd   
611 ----+-----+-----+-------
612  32 | one | two | three
613 (1 row)
614
615 -- Test non-inheritable parent constraints
616 create table p1(ff1 int);
617 alter table p1 add constraint p1chk check (ff1 > 0) no inherit;
618 alter table p1 add constraint p2chk check (ff1 > 10);
619 -- connoinherit should be true for NO INHERIT constraint
620 select pc.relname, pgc.conname, pgc.contype, pgc.conislocal, pgc.coninhcount, pgc.connoinherit from pg_class as pc inner join pg_constraint as pgc on (pgc.conrelid = pc.oid) where pc.relname = 'p1' order by 1,2;
621  relname | conname | contype | conislocal | coninhcount | connoinherit 
622 ---------+---------+---------+------------+-------------+--------------
623  p1      | p1chk   | c       | t          |           0 | t
624  p1      | p2chk   | c       | t          |           0 | f
625 (2 rows)
626
627 -- Test that child does not inherit NO INHERIT constraints
628 create table c1 () inherits (p1);
629 \d p1
630       Table "public.p1"
631  Column |  Type   | Modifiers 
632 --------+---------+-----------
633  ff1    | integer | 
634 Check constraints:
635     "p1chk" CHECK (ff1 > 0) NO INHERIT
636     "p2chk" CHECK (ff1 > 10)
637 Number of child tables: 1 (Use \d+ to list them.)
638
639 \d c1
640       Table "public.c1"
641  Column |  Type   | Modifiers 
642 --------+---------+-----------
643  ff1    | integer | 
644 Check constraints:
645     "p2chk" CHECK (ff1 > 10)
646 Inherits: p1
647
648 drop table p1 cascade;
649 NOTICE:  drop cascades to table c1
650 -- Tests for casting between the rowtypes of parent and child
651 -- tables. See the pgsql-hackers thread beginning Dec. 4/04
652 create table base (i integer);
653 create table derived () inherits (base);
654 insert into derived (i) values (0);
655 select derived::base from derived;
656  derived 
657 ---------
658  (0)
659 (1 row)
660
661 drop table derived;
662 drop table base;
663 create table p1(ff1 int);
664 create table p2(f1 text);
665 create function p2text(p2) returns text as 'select $1.f1' language sql;
666 create table c1(f3 int) inherits(p1,p2);
667 insert into c1 values(123456789, 'hi', 42);
668 select p2text(c1.*) from c1;
669  p2text 
670 --------
671  hi
672 (1 row)
673
674 drop function p2text(p2);
675 drop table c1;
676 drop table p2;
677 drop table p1;
678 CREATE TABLE ac (aa TEXT);
679 alter table ac add constraint ac_check check (aa is not null);
680 CREATE TABLE bc (bb TEXT) INHERITS (ac);
681 select pc.relname, pgc.conname, pgc.contype, pgc.conislocal, pgc.coninhcount, pgc.consrc from pg_class as pc inner join pg_constraint as pgc on (pgc.conrelid = pc.oid) where pc.relname in ('ac', 'bc') order by 1,2;
682  relname | conname  | contype | conislocal | coninhcount |      consrc      
683 ---------+----------+---------+------------+-------------+------------------
684  ac      | ac_check | c       | t          |           0 | (aa IS NOT NULL)
685  bc      | ac_check | c       | f          |           1 | (aa IS NOT NULL)
686 (2 rows)
687
688 insert into ac (aa) values (NULL);
689 ERROR:  new row for relation "ac" violates check constraint "ac_check"
690 DETAIL:  Failing row contains (null).
691 insert into bc (aa) values (NULL);
692 ERROR:  new row for relation "bc" violates check constraint "ac_check"
693 DETAIL:  Failing row contains (null, null).
694 alter table bc drop constraint ac_check;  -- fail, disallowed
695 ERROR:  cannot drop inherited constraint "ac_check" of relation "bc"
696 alter table ac drop constraint ac_check;
697 select pc.relname, pgc.conname, pgc.contype, pgc.conislocal, pgc.coninhcount, pgc.consrc from pg_class as pc inner join pg_constraint as pgc on (pgc.conrelid = pc.oid) where pc.relname in ('ac', 'bc') order by 1,2;
698  relname | conname | contype | conislocal | coninhcount | consrc 
699 ---------+---------+---------+------------+-------------+--------
700 (0 rows)
701
702 -- try the unnamed-constraint case
703 alter table ac add check (aa is not null);
704 select pc.relname, pgc.conname, pgc.contype, pgc.conislocal, pgc.coninhcount, pgc.consrc from pg_class as pc inner join pg_constraint as pgc on (pgc.conrelid = pc.oid) where pc.relname in ('ac', 'bc') order by 1,2;
705  relname |   conname   | contype | conislocal | coninhcount |      consrc      
706 ---------+-------------+---------+------------+-------------+------------------
707  ac      | ac_aa_check | c       | t          |           0 | (aa IS NOT NULL)
708  bc      | ac_aa_check | c       | f          |           1 | (aa IS NOT NULL)
709 (2 rows)
710
711 insert into ac (aa) values (NULL);
712 ERROR:  new row for relation "ac" violates check constraint "ac_aa_check"
713 DETAIL:  Failing row contains (null).
714 insert into bc (aa) values (NULL);
715 ERROR:  new row for relation "bc" violates check constraint "ac_aa_check"
716 DETAIL:  Failing row contains (null, null).
717 alter table bc drop constraint ac_aa_check;  -- fail, disallowed
718 ERROR:  cannot drop inherited constraint "ac_aa_check" of relation "bc"
719 alter table ac drop constraint ac_aa_check;
720 select pc.relname, pgc.conname, pgc.contype, pgc.conislocal, pgc.coninhcount, pgc.consrc from pg_class as pc inner join pg_constraint as pgc on (pgc.conrelid = pc.oid) where pc.relname in ('ac', 'bc') order by 1,2;
721  relname | conname | contype | conislocal | coninhcount | consrc 
722 ---------+---------+---------+------------+-------------+--------
723 (0 rows)
724
725 alter table ac add constraint ac_check check (aa is not null);
726 alter table bc no inherit ac;
727 select pc.relname, pgc.conname, pgc.contype, pgc.conislocal, pgc.coninhcount, pgc.consrc from pg_class as pc inner join pg_constraint as pgc on (pgc.conrelid = pc.oid) where pc.relname in ('ac', 'bc') order by 1,2;
728  relname | conname  | contype | conislocal | coninhcount |      consrc      
729 ---------+----------+---------+------------+-------------+------------------
730  ac      | ac_check | c       | t          |           0 | (aa IS NOT NULL)
731  bc      | ac_check | c       | t          |           0 | (aa IS NOT NULL)
732 (2 rows)
733
734 alter table bc drop constraint ac_check;
735 select pc.relname, pgc.conname, pgc.contype, pgc.conislocal, pgc.coninhcount, pgc.consrc from pg_class as pc inner join pg_constraint as pgc on (pgc.conrelid = pc.oid) where pc.relname in ('ac', 'bc') order by 1,2;
736  relname | conname  | contype | conislocal | coninhcount |      consrc      
737 ---------+----------+---------+------------+-------------+------------------
738  ac      | ac_check | c       | t          |           0 | (aa IS NOT NULL)
739 (1 row)
740
741 alter table ac drop constraint ac_check;
742 select pc.relname, pgc.conname, pgc.contype, pgc.conislocal, pgc.coninhcount, pgc.consrc from pg_class as pc inner join pg_constraint as pgc on (pgc.conrelid = pc.oid) where pc.relname in ('ac', 'bc') order by 1,2;
743  relname | conname | contype | conislocal | coninhcount | consrc 
744 ---------+---------+---------+------------+-------------+--------
745 (0 rows)
746
747 drop table bc;
748 drop table ac;
749 create table ac (a int constraint check_a check (a <> 0));
750 create table bc (a int constraint check_a check (a <> 0), b int constraint check_b check (b <> 0)) inherits (ac);
751 NOTICE:  merging column "a" with inherited definition
752 NOTICE:  merging constraint "check_a" with inherited definition
753 select pc.relname, pgc.conname, pgc.contype, pgc.conislocal, pgc.coninhcount, pgc.consrc from pg_class as pc inner join pg_constraint as pgc on (pgc.conrelid = pc.oid) where pc.relname in ('ac', 'bc') order by 1,2;
754  relname | conname | contype | conislocal | coninhcount |  consrc  
755 ---------+---------+---------+------------+-------------+----------
756  ac      | check_a | c       | t          |           0 | (a <> 0)
757  bc      | check_a | c       | t          |           1 | (a <> 0)
758  bc      | check_b | c       | t          |           0 | (b <> 0)
759 (3 rows)
760
761 drop table bc;
762 drop table ac;
763 create table ac (a int constraint check_a check (a <> 0));
764 create table bc (b int constraint check_b check (b <> 0));
765 create table cc (c int constraint check_c check (c <> 0)) inherits (ac, bc);
766 select pc.relname, pgc.conname, pgc.contype, pgc.conislocal, pgc.coninhcount, pgc.consrc from pg_class as pc inner join pg_constraint as pgc on (pgc.conrelid = pc.oid) where pc.relname in ('ac', 'bc', 'cc') order by 1,2;
767  relname | conname | contype | conislocal | coninhcount |  consrc  
768 ---------+---------+---------+------------+-------------+----------
769  ac      | check_a | c       | t          |           0 | (a <> 0)
770  bc      | check_b | c       | t          |           0 | (b <> 0)
771  cc      | check_a | c       | f          |           1 | (a <> 0)
772  cc      | check_b | c       | f          |           1 | (b <> 0)
773  cc      | check_c | c       | t          |           0 | (c <> 0)
774 (5 rows)
775
776 alter table cc no inherit bc;
777 select pc.relname, pgc.conname, pgc.contype, pgc.conislocal, pgc.coninhcount, pgc.consrc from pg_class as pc inner join pg_constraint as pgc on (pgc.conrelid = pc.oid) where pc.relname in ('ac', 'bc', 'cc') order by 1,2;
778  relname | conname | contype | conislocal | coninhcount |  consrc  
779 ---------+---------+---------+------------+-------------+----------
780  ac      | check_a | c       | t          |           0 | (a <> 0)
781  bc      | check_b | c       | t          |           0 | (b <> 0)
782  cc      | check_a | c       | f          |           1 | (a <> 0)
783  cc      | check_b | c       | t          |           0 | (b <> 0)
784  cc      | check_c | c       | t          |           0 | (c <> 0)
785 (5 rows)
786
787 drop table cc;
788 drop table bc;
789 drop table ac;
790 create table p1(f1 int);
791 create table p2(f2 int);
792 create table c1(f3 int) inherits(p1,p2);
793 insert into c1 values(1,-1,2);
794 alter table p2 add constraint cc check (f2>0);  -- fail
795 ERROR:  check constraint "cc" is violated by some row
796 alter table p2 add check (f2>0);  -- check it without a name, too
797 ERROR:  check constraint "p2_f2_check" is violated by some row
798 delete from c1;
799 insert into c1 values(1,1,2);
800 alter table p2 add check (f2>0);
801 insert into c1 values(1,-1,2);  -- fail
802 ERROR:  new row for relation "c1" violates check constraint "p2_f2_check"
803 DETAIL:  Failing row contains (1, -1, 2).
804 create table c2(f3 int) inherits(p1,p2);
805 \d c2
806       Table "public.c2"
807  Column |  Type   | Modifiers 
808 --------+---------+-----------
809  f1     | integer | 
810  f2     | integer | 
811  f3     | integer | 
812 Check constraints:
813     "p2_f2_check" CHECK (f2 > 0)
814 Inherits: p1,
815           p2
816
817 create table c3 (f4 int) inherits(c1,c2);
818 NOTICE:  merging multiple inherited definitions of column "f1"
819 NOTICE:  merging multiple inherited definitions of column "f2"
820 NOTICE:  merging multiple inherited definitions of column "f3"
821 \d c3
822       Table "public.c3"
823  Column |  Type   | Modifiers 
824 --------+---------+-----------
825  f1     | integer | 
826  f2     | integer | 
827  f3     | integer | 
828  f4     | integer | 
829 Check constraints:
830     "p2_f2_check" CHECK (f2 > 0)
831 Inherits: c1,
832           c2
833
834 drop table p1 cascade;
835 NOTICE:  drop cascades to 3 other objects
836 DETAIL:  drop cascades to table c1
837 drop cascades to table c2
838 drop cascades to table c3
839 drop table p2 cascade;
840 create table pp1 (f1 int);
841 create table cc1 (f2 text, f3 int) inherits (pp1);
842 alter table pp1 add column a1 int check (a1 > 0);
843 \d cc1
844       Table "public.cc1"
845  Column |  Type   | Modifiers 
846 --------+---------+-----------
847  f1     | integer | 
848  f2     | text    | 
849  f3     | integer | 
850  a1     | integer | 
851 Check constraints:
852     "pp1_a1_check" CHECK (a1 > 0)
853 Inherits: pp1
854
855 create table cc2(f4 float) inherits(pp1,cc1);
856 NOTICE:  merging multiple inherited definitions of column "f1"
857 NOTICE:  merging multiple inherited definitions of column "a1"
858 \d cc2
859           Table "public.cc2"
860  Column |       Type       | Modifiers 
861 --------+------------------+-----------
862  f1     | integer          | 
863  a1     | integer          | 
864  f2     | text             | 
865  f3     | integer          | 
866  f4     | double precision | 
867 Check constraints:
868     "pp1_a1_check" CHECK (a1 > 0)
869 Inherits: pp1,
870           cc1
871
872 alter table pp1 add column a2 int check (a2 > 0);
873 NOTICE:  merging definition of column "a2" for child "cc2"
874 NOTICE:  merging constraint "pp1_a2_check" with inherited definition
875 \d cc2
876           Table "public.cc2"
877  Column |       Type       | Modifiers 
878 --------+------------------+-----------
879  f1     | integer          | 
880  a1     | integer          | 
881  f2     | text             | 
882  f3     | integer          | 
883  f4     | double precision | 
884  a2     | integer          | 
885 Check constraints:
886     "pp1_a1_check" CHECK (a1 > 0)
887     "pp1_a2_check" CHECK (a2 > 0)
888 Inherits: pp1,
889           cc1
890
891 drop table pp1 cascade;
892 NOTICE:  drop cascades to 2 other objects
893 DETAIL:  drop cascades to table cc1
894 drop cascades to table cc2
895 -- Test for renaming in simple multiple inheritance
896 CREATE TABLE inht1 (a int, b int);
897 CREATE TABLE inhs1 (b int, c int);
898 CREATE TABLE inhts (d int) INHERITS (inht1, inhs1);
899 NOTICE:  merging multiple inherited definitions of column "b"
900 ALTER TABLE inht1 RENAME a TO aa;
901 ALTER TABLE inht1 RENAME b TO bb;                -- to be failed
902 ERROR:  cannot rename inherited column "b"
903 ALTER TABLE inhts RENAME aa TO aaa;      -- to be failed
904 ERROR:  cannot rename inherited column "aa"
905 ALTER TABLE inhts RENAME d TO dd;
906 \d+ inhts
907                         Table "public.inhts"
908  Column |  Type   | Modifiers | Storage | Stats target | Description 
909 --------+---------+-----------+---------+--------------+-------------
910  aa     | integer |           | plain   |              | 
911  b      | integer |           | plain   |              | 
912  c      | integer |           | plain   |              | 
913  dd     | integer |           | plain   |              | 
914 Inherits: inht1,
915           inhs1
916 Replica Identity: DEFAULT
917 Has OIDs: no
918
919 DROP TABLE inhts;
920 -- Test for renaming in diamond inheritance
921 CREATE TABLE inht2 (x int) INHERITS (inht1);
922 CREATE TABLE inht3 (y int) INHERITS (inht1);
923 CREATE TABLE inht4 (z int) INHERITS (inht2, inht3);
924 NOTICE:  merging multiple inherited definitions of column "aa"
925 NOTICE:  merging multiple inherited definitions of column "b"
926 ALTER TABLE inht1 RENAME aa TO aaa;
927 \d+ inht4
928                         Table "public.inht4"
929  Column |  Type   | Modifiers | Storage | Stats target | Description 
930 --------+---------+-----------+---------+--------------+-------------
931  aaa    | integer |           | plain   |              | 
932  b      | integer |           | plain   |              | 
933  x      | integer |           | plain   |              | 
934  y      | integer |           | plain   |              | 
935  z      | integer |           | plain   |              | 
936 Inherits: inht2,
937           inht3
938 Replica Identity: DEFAULT
939 Has OIDs: no
940
941 CREATE TABLE inhts (d int) INHERITS (inht2, inhs1);
942 NOTICE:  merging multiple inherited definitions of column "b"
943 ALTER TABLE inht1 RENAME aaa TO aaaa;
944 ALTER TABLE inht1 RENAME b TO bb;                -- to be failed
945 ERROR:  cannot rename inherited column "b"
946 \d+ inhts
947                         Table "public.inhts"
948  Column |  Type   | Modifiers | Storage | Stats target | Description 
949 --------+---------+-----------+---------+--------------+-------------
950  aaaa   | integer |           | plain   |              | 
951  b      | integer |           | plain   |              | 
952  x      | integer |           | plain   |              | 
953  c      | integer |           | plain   |              | 
954  d      | integer |           | plain   |              | 
955 Inherits: inht2,
956           inhs1
957 Replica Identity: DEFAULT
958 Has OIDs: no
959
960 WITH RECURSIVE r AS (
961   SELECT 'inht1'::regclass AS inhrelid
962 UNION ALL
963   SELECT c.inhrelid FROM pg_inherits c, r WHERE r.inhrelid = c.inhparent
964 )
965 SELECT a.attrelid::regclass, a.attname, a.attinhcount, e.expected
966   FROM (SELECT inhrelid, count(*) AS expected FROM pg_inherits
967         WHERE inhparent IN (SELECT inhrelid FROM r) GROUP BY inhrelid) e
968   JOIN pg_attribute a ON e.inhrelid = a.attrelid WHERE NOT attislocal
969   ORDER BY a.attrelid::regclass::name, a.attnum;
970  attrelid | attname | attinhcount | expected 
971 ----------+---------+-------------+----------
972  inht2    | aaaa    |           1 |        1
973  inht2    | b       |           1 |        1
974  inht3    | aaaa    |           1 |        1
975  inht3    | b       |           1 |        1
976  inht4    | aaaa    |           2 |        2
977  inht4    | b       |           2 |        2
978  inht4    | x       |           1 |        2
979  inht4    | y       |           1 |        2
980  inhts    | aaaa    |           1 |        1
981  inhts    | b       |           2 |        1
982  inhts    | x       |           1 |        1
983  inhts    | c       |           1 |        1
984 (12 rows)
985
986 DROP TABLE inht1, inhs1 CASCADE;
987 NOTICE:  drop cascades to 4 other objects
988 DETAIL:  drop cascades to table inht2
989 drop cascades to table inhts
990 drop cascades to table inht3
991 drop cascades to table inht4
992 -- Test non-inheritable indices [UNIQUE, EXCLUDE] contraints
993 CREATE TABLE test_constraints (id int, val1 varchar, val2 int, UNIQUE(val1, val2));
994 CREATE TABLE test_constraints_inh () INHERITS (test_constraints);
995 \d+ test_constraints
996                         Table "public.test_constraints"
997  Column |       Type        | Modifiers | Storage  | Stats target | Description 
998 --------+-------------------+-----------+----------+--------------+-------------
999  id     | integer           |           | plain    |              | 
1000  val1   | character varying |           | extended |              | 
1001  val2   | integer           |           | plain    |              | 
1002 Indexes:
1003     "test_constraints_val1_val2_key" UNIQUE CONSTRAINT, btree (val1, val2)
1004 Child tables: test_constraints_inh
1005 Replica Identity: DEFAULT
1006 Has OIDs: no
1007
1008 ALTER TABLE ONLY test_constraints DROP CONSTRAINT test_constraints_val1_val2_key;
1009 \d+ test_constraints
1010                         Table "public.test_constraints"
1011  Column |       Type        | Modifiers | Storage  | Stats target | Description 
1012 --------+-------------------+-----------+----------+--------------+-------------
1013  id     | integer           |           | plain    |              | 
1014  val1   | character varying |           | extended |              | 
1015  val2   | integer           |           | plain    |              | 
1016 Child tables: test_constraints_inh
1017 Replica Identity: DEFAULT
1018 Has OIDs: no
1019
1020 \d+ test_constraints_inh
1021                       Table "public.test_constraints_inh"
1022  Column |       Type        | Modifiers | Storage  | Stats target | Description 
1023 --------+-------------------+-----------+----------+--------------+-------------
1024  id     | integer           |           | plain    |              | 
1025  val1   | character varying |           | extended |              | 
1026  val2   | integer           |           | plain    |              | 
1027 Inherits: test_constraints
1028 Replica Identity: DEFAULT
1029 Has OIDs: no
1030
1031 DROP TABLE test_constraints_inh;
1032 DROP TABLE test_constraints;
1033 CREATE TABLE test_ex_constraints (
1034     c circle,
1035     EXCLUDE USING gist (c WITH &&)
1036 );
1037 CREATE TABLE test_ex_constraints_inh () INHERITS (test_ex_constraints);
1038 \d+ test_ex_constraints
1039                  Table "public.test_ex_constraints"
1040  Column |  Type  | Modifiers | Storage | Stats target | Description 
1041 --------+--------+-----------+---------+--------------+-------------
1042  c      | circle |           | plain   |              | 
1043 Indexes:
1044     "test_ex_constraints_c_excl" EXCLUDE USING gist (c WITH &&)
1045 Child tables: test_ex_constraints_inh
1046 Replica Identity: DEFAULT
1047 Has OIDs: no
1048
1049 ALTER TABLE test_ex_constraints DROP CONSTRAINT test_ex_constraints_c_excl;
1050 \d+ test_ex_constraints
1051                  Table "public.test_ex_constraints"
1052  Column |  Type  | Modifiers | Storage | Stats target | Description 
1053 --------+--------+-----------+---------+--------------+-------------
1054  c      | circle |           | plain   |              | 
1055 Child tables: test_ex_constraints_inh
1056 Replica Identity: DEFAULT
1057 Has OIDs: no
1058
1059 \d+ test_ex_constraints_inh
1060                Table "public.test_ex_constraints_inh"
1061  Column |  Type  | Modifiers | Storage | Stats target | Description 
1062 --------+--------+-----------+---------+--------------+-------------
1063  c      | circle |           | plain   |              | 
1064 Inherits: test_ex_constraints
1065 Replica Identity: DEFAULT
1066 Has OIDs: no
1067
1068 DROP TABLE test_ex_constraints_inh;
1069 DROP TABLE test_ex_constraints;
1070 -- Test non-inheritable foreign key contraints
1071 CREATE TABLE test_primary_constraints(id int PRIMARY KEY);
1072 CREATE TABLE test_foreign_constraints(id1 int REFERENCES test_primary_constraints(id));
1073 CREATE TABLE test_foreign_constraints_inh () INHERITS (test_foreign_constraints);
1074 \d+ test_primary_constraints
1075                Table "public.test_primary_constraints"
1076  Column |  Type   | Modifiers | Storage | Stats target | Description 
1077 --------+---------+-----------+---------+--------------+-------------
1078  id     | integer | not null  | plain   |              | 
1079 Indexes:
1080     "test_primary_constraints_pkey" PRIMARY KEY, btree (id)
1081 Referenced by:
1082     TABLE "test_foreign_constraints" CONSTRAINT "test_foreign_constraints_id1_fkey" FOREIGN KEY (id1) REFERENCES test_primary_constraints(id)
1083 Replica Identity: DEFAULT
1084 Has OIDs: no
1085
1086 \d+ test_foreign_constraints
1087                Table "public.test_foreign_constraints"
1088  Column |  Type   | Modifiers | Storage | Stats target | Description 
1089 --------+---------+-----------+---------+--------------+-------------
1090  id1    | integer |           | plain   |              | 
1091 Foreign-key constraints:
1092     "test_foreign_constraints_id1_fkey" FOREIGN KEY (id1) REFERENCES test_primary_constraints(id)
1093 Child tables: test_foreign_constraints_inh
1094 Replica Identity: DEFAULT
1095 Has OIDs: no
1096
1097 ALTER TABLE test_foreign_constraints DROP CONSTRAINT test_foreign_constraints_id1_fkey;
1098 \d+ test_foreign_constraints
1099                Table "public.test_foreign_constraints"
1100  Column |  Type   | Modifiers | Storage | Stats target | Description 
1101 --------+---------+-----------+---------+--------------+-------------
1102  id1    | integer |           | plain   |              | 
1103 Child tables: test_foreign_constraints_inh
1104 Replica Identity: DEFAULT
1105 Has OIDs: no
1106
1107 \d+ test_foreign_constraints_inh
1108              Table "public.test_foreign_constraints_inh"
1109  Column |  Type   | Modifiers | Storage | Stats target | Description 
1110 --------+---------+-----------+---------+--------------+-------------
1111  id1    | integer |           | plain   |              | 
1112 Inherits: test_foreign_constraints
1113 Replica Identity: DEFAULT
1114 Has OIDs: no
1115
1116 DROP TABLE test_foreign_constraints_inh;
1117 DROP TABLE test_foreign_constraints;
1118 DROP TABLE test_primary_constraints;
1119 --
1120 -- Test parameterized append plans for inheritance trees
1121 --
1122 create temp table patest0 (id, x) as
1123   select x, x from generate_series(0,1000) x;
1124 create temp table patest1() inherits (patest0);
1125 insert into patest1
1126   select x, x from generate_series(0,1000) x;
1127 create temp table patest2() inherits (patest0);
1128 insert into patest2
1129   select x, x from generate_series(0,1000) x;
1130 create index patest0i on patest0(id);
1131 create index patest1i on patest1(id);
1132 create index patest2i on patest2(id);
1133 analyze patest0;
1134 analyze patest1;
1135 analyze patest2;
1136 explain (costs off)
1137 select * from patest0 join (select f1 from int4_tbl limit 1) ss on id = f1;
1138                     QUERY PLAN                    
1139 --------------------------------------------------
1140  Nested Loop
1141    ->  Limit
1142          ->  Seq Scan on int4_tbl
1143    ->  Append
1144          ->  Index Scan using patest0i on patest0
1145                Index Cond: (id = int4_tbl.f1)
1146          ->  Index Scan using patest1i on patest1
1147                Index Cond: (id = int4_tbl.f1)
1148          ->  Index Scan using patest2i on patest2
1149                Index Cond: (id = int4_tbl.f1)
1150 (10 rows)
1151
1152 select * from patest0 join (select f1 from int4_tbl limit 1) ss on id = f1;
1153  id | x | f1 
1154 ----+---+----
1155   0 | 0 |  0
1156   0 | 0 |  0
1157   0 | 0 |  0
1158 (3 rows)
1159
1160 drop index patest2i;
1161 explain (costs off)
1162 select * from patest0 join (select f1 from int4_tbl limit 1) ss on id = f1;
1163                     QUERY PLAN                    
1164 --------------------------------------------------
1165  Nested Loop
1166    ->  Limit
1167          ->  Seq Scan on int4_tbl
1168    ->  Append
1169          ->  Index Scan using patest0i on patest0
1170                Index Cond: (id = int4_tbl.f1)
1171          ->  Index Scan using patest1i on patest1
1172                Index Cond: (id = int4_tbl.f1)
1173          ->  Seq Scan on patest2
1174                Filter: (int4_tbl.f1 = id)
1175 (10 rows)
1176
1177 select * from patest0 join (select f1 from int4_tbl limit 1) ss on id = f1;
1178  id | x | f1 
1179 ----+---+----
1180   0 | 0 |  0
1181   0 | 0 |  0
1182   0 | 0 |  0
1183 (3 rows)
1184
1185 drop table patest0 cascade;
1186 NOTICE:  drop cascades to 2 other objects
1187 DETAIL:  drop cascades to table patest1
1188 drop cascades to table patest2
1189 --
1190 -- Test merge-append plans for inheritance trees
1191 --
1192 create table matest0 (id serial primary key, name text);
1193 create table matest1 (id integer primary key) inherits (matest0);
1194 NOTICE:  merging column "id" with inherited definition
1195 create table matest2 (id integer primary key) inherits (matest0);
1196 NOTICE:  merging column "id" with inherited definition
1197 create table matest3 (id integer primary key) inherits (matest0);
1198 NOTICE:  merging column "id" with inherited definition
1199 create index matest0i on matest0 ((1-id));
1200 create index matest1i on matest1 ((1-id));
1201 -- create index matest2i on matest2 ((1-id));  -- intentionally missing
1202 create index matest3i on matest3 ((1-id));
1203 insert into matest1 (name) values ('Test 1');
1204 insert into matest1 (name) values ('Test 2');
1205 insert into matest2 (name) values ('Test 3');
1206 insert into matest2 (name) values ('Test 4');
1207 insert into matest3 (name) values ('Test 5');
1208 insert into matest3 (name) values ('Test 6');
1209 set enable_indexscan = off;  -- force use of seqscan/sort, so no merge
1210 explain (verbose, costs off) select * from matest0 order by 1-id;
1211                          QUERY PLAN                         
1212 ------------------------------------------------------------
1213  Sort
1214    Output: matest0.id, matest0.name, ((1 - matest0.id))
1215    Sort Key: ((1 - matest0.id))
1216    ->  Result
1217          Output: matest0.id, matest0.name, (1 - matest0.id)
1218          ->  Append
1219                ->  Seq Scan on public.matest0
1220                      Output: matest0.id, matest0.name
1221                ->  Seq Scan on public.matest1
1222                      Output: matest1.id, matest1.name
1223                ->  Seq Scan on public.matest2
1224                      Output: matest2.id, matest2.name
1225                ->  Seq Scan on public.matest3
1226                      Output: matest3.id, matest3.name
1227 (14 rows)
1228
1229 select * from matest0 order by 1-id;
1230  id |  name  
1231 ----+--------
1232   6 | Test 6
1233   5 | Test 5
1234   4 | Test 4
1235   3 | Test 3
1236   2 | Test 2
1237   1 | Test 1
1238 (6 rows)
1239
1240 explain (verbose, costs off) select min(1-id) from matest0;
1241                QUERY PLAN               
1242 ----------------------------------------
1243  Aggregate
1244    Output: min((1 - matest0.id))
1245    ->  Append
1246          ->  Seq Scan on public.matest0
1247                Output: matest0.id
1248          ->  Seq Scan on public.matest1
1249                Output: matest1.id
1250          ->  Seq Scan on public.matest2
1251                Output: matest2.id
1252          ->  Seq Scan on public.matest3
1253                Output: matest3.id
1254 (11 rows)
1255
1256 select min(1-id) from matest0;
1257  min 
1258 -----
1259   -5
1260 (1 row)
1261
1262 reset enable_indexscan;
1263 set enable_seqscan = off;  -- plan with fewest seqscans should be merge
1264 explain (verbose, costs off) select * from matest0 order by 1-id;
1265                             QUERY PLAN                            
1266 ------------------------------------------------------------------
1267  Merge Append
1268    Sort Key: ((1 - matest0.id))
1269    ->  Index Scan using matest0i on public.matest0
1270          Output: matest0.id, matest0.name, (1 - matest0.id)
1271    ->  Index Scan using matest1i on public.matest1
1272          Output: matest1.id, matest1.name, (1 - matest1.id)
1273    ->  Sort
1274          Output: matest2.id, matest2.name, ((1 - matest2.id))
1275          Sort Key: ((1 - matest2.id))
1276          ->  Seq Scan on public.matest2
1277                Output: matest2.id, matest2.name, (1 - matest2.id)
1278    ->  Index Scan using matest3i on public.matest3
1279          Output: matest3.id, matest3.name, (1 - matest3.id)
1280 (13 rows)
1281
1282 select * from matest0 order by 1-id;
1283  id |  name  
1284 ----+--------
1285   6 | Test 6
1286   5 | Test 5
1287   4 | Test 4
1288   3 | Test 3
1289   2 | Test 2
1290   1 | Test 1
1291 (6 rows)
1292
1293 explain (verbose, costs off) select min(1-id) from matest0;
1294                                 QUERY PLAN                                
1295 --------------------------------------------------------------------------
1296  Result
1297    Output: $0
1298    InitPlan 1 (returns $0)
1299      ->  Limit
1300            Output: ((1 - matest0.id))
1301            ->  Result
1302                  Output: ((1 - matest0.id))
1303                  ->  Merge Append
1304                        Sort Key: ((1 - matest0.id))
1305                        ->  Index Scan using matest0i on public.matest0
1306                              Output: matest0.id, (1 - matest0.id)
1307                              Index Cond: ((1 - matest0.id) IS NOT NULL)
1308                        ->  Index Scan using matest1i on public.matest1
1309                              Output: matest1.id, (1 - matest1.id)
1310                              Index Cond: ((1 - matest1.id) IS NOT NULL)
1311                        ->  Sort
1312                              Output: matest2.id, ((1 - matest2.id))
1313                              Sort Key: ((1 - matest2.id))
1314                              ->  Bitmap Heap Scan on public.matest2
1315                                    Output: matest2.id, (1 - matest2.id)
1316                                    Filter: ((1 - matest2.id) IS NOT NULL)
1317                                    ->  Bitmap Index Scan on matest2_pkey
1318                        ->  Index Scan using matest3i on public.matest3
1319                              Output: matest3.id, (1 - matest3.id)
1320                              Index Cond: ((1 - matest3.id) IS NOT NULL)
1321 (25 rows)
1322
1323 select min(1-id) from matest0;
1324  min 
1325 -----
1326   -5
1327 (1 row)
1328
1329 reset enable_seqscan;
1330 drop table matest0 cascade;
1331 NOTICE:  drop cascades to 3 other objects
1332 DETAIL:  drop cascades to table matest1
1333 drop cascades to table matest2
1334 drop cascades to table matest3
1335 --
1336 -- Test merge-append for UNION ALL append relations
1337 --
1338 set enable_seqscan = off;
1339 set enable_indexscan = on;
1340 set enable_bitmapscan = off;
1341 -- Check handling of duplicated, constant, or volatile targetlist items
1342 explain (costs off)
1343 SELECT thousand, tenthous FROM tenk1
1344 UNION ALL
1345 SELECT thousand, thousand FROM tenk1
1346 ORDER BY thousand, tenthous;
1347                                QUERY PLAN                                
1348 -------------------------------------------------------------------------
1349  Merge Append
1350    Sort Key: tenk1.thousand, tenk1.tenthous
1351    ->  Index Only Scan using tenk1_thous_tenthous on tenk1
1352    ->  Sort
1353          Sort Key: tenk1_1.thousand, tenk1_1.thousand
1354          ->  Index Only Scan using tenk1_thous_tenthous on tenk1 tenk1_1
1355 (6 rows)
1356
1357 explain (costs off)
1358 SELECT thousand, tenthous, thousand+tenthous AS x FROM tenk1
1359 UNION ALL
1360 SELECT 42, 42, hundred FROM tenk1
1361 ORDER BY thousand, tenthous;
1362                             QUERY PLAN                            
1363 ------------------------------------------------------------------
1364  Merge Append
1365    Sort Key: tenk1.thousand, tenk1.tenthous
1366    ->  Index Only Scan using tenk1_thous_tenthous on tenk1
1367    ->  Sort
1368          Sort Key: (42), (42)
1369          ->  Index Only Scan using tenk1_hundred on tenk1 tenk1_1
1370 (6 rows)
1371
1372 explain (costs off)
1373 SELECT thousand, tenthous FROM tenk1
1374 UNION ALL
1375 SELECT thousand, random()::integer FROM tenk1
1376 ORDER BY thousand, tenthous;
1377                                QUERY PLAN                                
1378 -------------------------------------------------------------------------
1379  Merge Append
1380    Sort Key: tenk1.thousand, tenk1.tenthous
1381    ->  Index Only Scan using tenk1_thous_tenthous on tenk1
1382    ->  Sort
1383          Sort Key: tenk1_1.thousand, ((random())::integer)
1384          ->  Index Only Scan using tenk1_thous_tenthous on tenk1 tenk1_1
1385 (6 rows)
1386
1387 -- Check min/max aggregate optimization
1388 explain (costs off)
1389 SELECT min(x) FROM
1390   (SELECT unique1 AS x FROM tenk1 a
1391    UNION ALL
1392    SELECT unique2 AS x FROM tenk1 b) s;
1393                              QUERY PLAN                             
1394 --------------------------------------------------------------------
1395  Result
1396    InitPlan 1 (returns $0)
1397      ->  Limit
1398            ->  Merge Append
1399                  Sort Key: a.unique1
1400                  ->  Index Only Scan using tenk1_unique1 on tenk1 a
1401                        Index Cond: (unique1 IS NOT NULL)
1402                  ->  Index Only Scan using tenk1_unique2 on tenk1 b
1403                        Index Cond: (unique2 IS NOT NULL)
1404 (9 rows)
1405
1406 explain (costs off)
1407 SELECT min(y) FROM
1408   (SELECT unique1 AS x, unique1 AS y FROM tenk1 a
1409    UNION ALL
1410    SELECT unique2 AS x, unique2 AS y FROM tenk1 b) s;
1411                              QUERY PLAN                             
1412 --------------------------------------------------------------------
1413  Result
1414    InitPlan 1 (returns $0)
1415      ->  Limit
1416            ->  Merge Append
1417                  Sort Key: a.unique1
1418                  ->  Index Only Scan using tenk1_unique1 on tenk1 a
1419                        Index Cond: (unique1 IS NOT NULL)
1420                  ->  Index Only Scan using tenk1_unique2 on tenk1 b
1421                        Index Cond: (unique2 IS NOT NULL)
1422 (9 rows)
1423
1424 -- XXX planner doesn't recognize that index on unique2 is sufficiently sorted
1425 explain (costs off)
1426 SELECT x, y FROM
1427   (SELECT thousand AS x, tenthous AS y FROM tenk1 a
1428    UNION ALL
1429    SELECT unique2 AS x, unique2 AS y FROM tenk1 b) s
1430 ORDER BY x, y;
1431                          QUERY PLAN                          
1432 -------------------------------------------------------------
1433  Merge Append
1434    Sort Key: a.thousand, a.tenthous
1435    ->  Index Only Scan using tenk1_thous_tenthous on tenk1 a
1436    ->  Sort
1437          Sort Key: b.unique2, b.unique2
1438          ->  Index Only Scan using tenk1_unique2 on tenk1 b
1439 (6 rows)
1440
1441 -- exercise rescan code path via a repeatedly-evaluated subquery
1442 explain (costs off)
1443 SELECT
1444     ARRAY(SELECT f.i FROM (
1445         (SELECT d + g.i FROM generate_series(4, 30, 3) d ORDER BY 1)
1446         UNION ALL
1447         (SELECT d + g.i FROM generate_series(0, 30, 5) d ORDER BY 1)
1448     ) f(i)
1449     ORDER BY f.i LIMIT 10)
1450 FROM generate_series(1, 3) g(i);
1451                            QUERY PLAN                           
1452 ----------------------------------------------------------------
1453  Function Scan on generate_series g
1454    SubPlan 1
1455      ->  Limit
1456            ->  Merge Append
1457                  Sort Key: ((d.d + g.i))
1458                  ->  Sort
1459                        Sort Key: ((d.d + g.i))
1460                        ->  Function Scan on generate_series d
1461                  ->  Sort
1462                        Sort Key: ((d_1.d + g.i))
1463                        ->  Function Scan on generate_series d_1
1464 (11 rows)
1465
1466 SELECT
1467     ARRAY(SELECT f.i FROM (
1468         (SELECT d + g.i FROM generate_series(4, 30, 3) d ORDER BY 1)
1469         UNION ALL
1470         (SELECT d + g.i FROM generate_series(0, 30, 5) d ORDER BY 1)
1471     ) f(i)
1472     ORDER BY f.i LIMIT 10)
1473 FROM generate_series(1, 3) g(i);
1474             array             
1475 ------------------------------
1476  {1,5,6,8,11,11,14,16,17,20}
1477  {2,6,7,9,12,12,15,17,18,21}
1478  {3,7,8,10,13,13,16,18,19,22}
1479 (3 rows)
1480
1481 reset enable_seqscan;
1482 reset enable_indexscan;
1483 reset enable_bitmapscan;