]> granicus.if.org Git - postgresql/blob - src/test/regress/expected/inherit.out
Remove useless whitespace at end of lines
[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 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "z_pkey" for table "z"
540 INSERT INTO z VALUES (NULL, 'text'); -- should fail
541 ERROR:  null value in column "aa" violates not-null constraint
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 relname, bar.* FROM bar, pg_class where bar.tableoid = pg_class.oid
561 order by 1,2;
562  relname | f1 | f2  
563 ---------+----+-----
564  bar     |  1 | 101
565  bar     |  2 | 102
566  bar     |  3 | 103
567  bar     |  4 |   4
568  bar2    |  1 | 101
569  bar2    |  2 | 102
570  bar2    |  3 | 103
571  bar2    |  4 |   4
572 (8 rows)
573
574 /* Test multiple inheritance of column defaults */
575 CREATE TABLE firstparent (tomorrow date default now()::date + 1);
576 CREATE TABLE secondparent (tomorrow date default  now() :: date  +  1);
577 CREATE TABLE jointchild () INHERITS (firstparent, secondparent);  -- ok
578 NOTICE:  merging multiple inherited definitions of column "tomorrow"
579 CREATE TABLE thirdparent (tomorrow date default now()::date - 1);
580 CREATE TABLE otherchild () INHERITS (firstparent, thirdparent);  -- not ok
581 NOTICE:  merging multiple inherited definitions of column "tomorrow"
582 ERROR:  column "tomorrow" inherits conflicting default values
583 HINT:  To resolve the conflict, specify a default explicitly.
584 CREATE TABLE otherchild (tomorrow date default now())
585   INHERITS (firstparent, thirdparent);  -- ok, child resolves ambiguous default
586 NOTICE:  merging multiple inherited definitions of column "tomorrow"
587 NOTICE:  merging column "tomorrow" with inherited definition
588 DROP TABLE firstparent, secondparent, jointchild, thirdparent, otherchild;
589 /* Test inheritance of structure (LIKE) */
590 CREATE TABLE inhx (xx text DEFAULT 'text');
591 /*
592  * Test double inheritance
593  *
594  * Ensure that defaults are NOT included unless
595  * INCLUDING DEFAULTS is specified
596  */
597 CREATE TABLE inhe (ee text, LIKE inhx) inherits (b);
598 INSERT INTO inhe VALUES ('ee-col1', 'ee-col2', DEFAULT, 'ee-col4');
599 SELECT * FROM inhe; /* Columns aa, bb, xx value NULL, ee */
600    aa    |   bb    | ee |   xx    
601 ---------+---------+----+---------
602  ee-col1 | ee-col2 |    | ee-col4
603 (1 row)
604
605 SELECT * FROM inhx; /* Empty set since LIKE inherits structure only */
606  xx 
607 ----
608 (0 rows)
609
610 SELECT * FROM b; /* Has ee entry */
611    aa    |   bb    
612 ---------+---------
613  ee-col1 | ee-col2
614 (1 row)
615
616 SELECT * FROM a; /* Has ee entry */
617    aa    
618 ---------
619  ee-col1
620 (1 row)
621
622 CREATE TABLE inhf (LIKE inhx, LIKE inhx); /* Throw error */
623 ERROR:  column "xx" specified more than once
624 CREATE TABLE inhf (LIKE inhx INCLUDING DEFAULTS INCLUDING CONSTRAINTS);
625 INSERT INTO inhf DEFAULT VALUES;
626 SELECT * FROM inhf; /* Single entry with value 'text' */
627   xx  
628 ------
629  text
630 (1 row)
631
632 ALTER TABLE inhx add constraint foo CHECK (xx = 'text');
633 ALTER TABLE inhx ADD PRIMARY KEY (xx);
634 NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index "inhx_pkey" for table "inhx"
635 CREATE TABLE inhg (LIKE inhx); /* Doesn't copy constraint */
636 INSERT INTO inhg VALUES ('foo');
637 DROP TABLE inhg;
638 CREATE TABLE inhg (x text, LIKE inhx INCLUDING CONSTRAINTS, y text); /* Copies constraints */
639 INSERT INTO inhg VALUES ('x', 'text', 'y'); /* Succeeds */
640 INSERT INTO inhg VALUES ('x', 'text', 'y'); /* Succeeds -- Unique constraints not copied */
641 INSERT INTO inhg VALUES ('x', 'foo',  'y');  /* fails due to constraint */
642 ERROR:  new row for relation "inhg" violates check constraint "foo"
643 SELECT * FROM inhg; /* Two records with three columns in order x=x, xx=text, y=y */
644  x |  xx  | y 
645 ---+------+---
646  x | text | y
647  x | text | y
648 (2 rows)
649
650 DROP TABLE inhg;
651 CREATE TABLE inhg (x text, LIKE inhx INCLUDING INDEXES, y text); /* copies indexes */
652 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "inhg_pkey" for table "inhg"
653 INSERT INTO inhg VALUES (5, 10);
654 INSERT INTO inhg VALUES (20, 10); -- should fail
655 ERROR:  duplicate key value violates unique constraint "inhg_pkey"
656 DETAIL:  Key (xx)=(10) already exists.
657 DROP TABLE inhg;
658 /* Multiple primary keys creation should fail */
659 CREATE TABLE inhg (x text, LIKE inhx INCLUDING INDEXES, PRIMARY KEY(x)); /* fails */
660 ERROR:  multiple primary keys for table "inhg" are not allowed
661 CREATE TABLE inhz (xx text DEFAULT 'text', yy int UNIQUE);
662 NOTICE:  CREATE TABLE / UNIQUE will create implicit index "inhz_yy_key" for table "inhz"
663 CREATE UNIQUE INDEX inhz_xx_idx on inhz (xx) WHERE xx <> 'test';
664 /* Ok to create multiple unique indexes */
665 CREATE TABLE inhg (x text UNIQUE, LIKE inhz INCLUDING INDEXES);
666 NOTICE:  CREATE TABLE / UNIQUE will create implicit index "inhg_x_key" for table "inhg"
667 NOTICE:  CREATE TABLE / UNIQUE will create implicit index "inhg_yy_key" for table "inhg"
668 INSERT INTO inhg (xx, yy, x) VALUES ('test', 5, 10);
669 INSERT INTO inhg (xx, yy, x) VALUES ('test', 10, 15);
670 INSERT INTO inhg (xx, yy, x) VALUES ('foo', 10, 15); -- should fail
671 ERROR:  duplicate key value violates unique constraint "inhg_x_key"
672 DETAIL:  Key (x)=(15) already exists.
673 DROP TABLE inhg;
674 DROP TABLE inhz;
675 -- Test changing the type of inherited columns
676 insert into d values('test','one','two','three');
677 alter table a alter column aa type integer using bit_length(aa);
678 select * from d;
679  aa | bb  | cc  |  dd   
680 ----+-----+-----+-------
681  32 | one | two | three
682 (1 row)
683
684 -- Tests for casting between the rowtypes of parent and child
685 -- tables. See the pgsql-hackers thread beginning Dec. 4/04
686 create table base (i integer);
687 create table derived () inherits (base);
688 insert into derived (i) values (0);
689 select derived::base from derived;
690  derived 
691 ---------
692  (0)
693 (1 row)
694
695 drop table derived;
696 drop table base;
697 create table p1(ff1 int);
698 create table p2(f1 text);
699 create function p2text(p2) returns text as 'select $1.f1' language sql;
700 create table c1(f3 int) inherits(p1,p2);
701 insert into c1 values(123456789, 'hi', 42);
702 select p2text(c1.*) from c1;
703  p2text 
704 --------
705  hi
706 (1 row)
707
708 drop function p2text(p2);
709 drop table c1;
710 drop table p2;
711 drop table p1;
712 CREATE TABLE ac (aa TEXT);
713 alter table ac add constraint ac_check check (aa is not null);
714 CREATE TABLE bc (bb TEXT) INHERITS (ac);
715 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;
716  relname | conname  | contype | conislocal | coninhcount |      consrc      
717 ---------+----------+---------+------------+-------------+------------------
718  ac      | ac_check | c       | t          |           0 | (aa IS NOT NULL)
719  bc      | ac_check | c       | f          |           1 | (aa IS NOT NULL)
720 (2 rows)
721
722 insert into ac (aa) values (NULL);
723 ERROR:  new row for relation "ac" violates check constraint "ac_check"
724 insert into bc (aa) values (NULL);
725 ERROR:  new row for relation "bc" violates check constraint "ac_check"
726 alter table bc drop constraint ac_check;  -- fail, disallowed
727 ERROR:  cannot drop inherited constraint "ac_check" of relation "bc"
728 alter table ac drop constraint ac_check;
729 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;
730  relname | conname | contype | conislocal | coninhcount | consrc 
731 ---------+---------+---------+------------+-------------+--------
732 (0 rows)
733
734 -- try the unnamed-constraint case
735 alter table ac add check (aa is not null);
736 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;
737  relname |   conname   | contype | conislocal | coninhcount |      consrc      
738 ---------+-------------+---------+------------+-------------+------------------
739  ac      | ac_aa_check | c       | t          |           0 | (aa IS NOT NULL)
740  bc      | ac_aa_check | c       | f          |           1 | (aa IS NOT NULL)
741 (2 rows)
742
743 insert into ac (aa) values (NULL);
744 ERROR:  new row for relation "ac" violates check constraint "ac_aa_check"
745 insert into bc (aa) values (NULL);
746 ERROR:  new row for relation "bc" violates check constraint "ac_aa_check"
747 alter table bc drop constraint ac_aa_check;  -- fail, disallowed
748 ERROR:  cannot drop inherited constraint "ac_aa_check" of relation "bc"
749 alter table ac drop constraint ac_aa_check;
750 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;
751  relname | conname | contype | conislocal | coninhcount | consrc 
752 ---------+---------+---------+------------+-------------+--------
753 (0 rows)
754
755 alter table ac add constraint ac_check check (aa is not null);
756 alter table bc no inherit ac;
757 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;
758  relname | conname  | contype | conislocal | coninhcount |      consrc      
759 ---------+----------+---------+------------+-------------+------------------
760  ac      | ac_check | c       | t          |           0 | (aa IS NOT NULL)
761  bc      | ac_check | c       | t          |           0 | (aa IS NOT NULL)
762 (2 rows)
763
764 alter table bc drop constraint ac_check;
765 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;
766  relname | conname  | contype | conislocal | coninhcount |      consrc      
767 ---------+----------+---------+------------+-------------+------------------
768  ac      | ac_check | c       | t          |           0 | (aa IS NOT NULL)
769 (1 row)
770
771 alter table ac drop constraint ac_check;
772 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;
773  relname | conname | contype | conislocal | coninhcount | consrc 
774 ---------+---------+---------+------------+-------------+--------
775 (0 rows)
776
777 drop table bc;
778 drop table ac;
779 create table ac (a int constraint check_a check (a <> 0));
780 create table bc (a int constraint check_a check (a <> 0), b int constraint check_b check (b <> 0)) inherits (ac);
781 NOTICE:  merging column "a" with inherited definition
782 NOTICE:  merging constraint "check_a" with inherited definition
783 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;
784  relname | conname | contype | conislocal | coninhcount |  consrc  
785 ---------+---------+---------+------------+-------------+----------
786  ac      | check_a | c       | t          |           0 | (a <> 0)
787  bc      | check_a | c       | t          |           1 | (a <> 0)
788  bc      | check_b | c       | t          |           0 | (b <> 0)
789 (3 rows)
790
791 drop table bc;
792 drop table ac;
793 create table ac (a int constraint check_a check (a <> 0));
794 create table bc (b int constraint check_b check (b <> 0));
795 create table cc (c int constraint check_c check (c <> 0)) inherits (ac, bc);
796 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;
797  relname | conname | contype | conislocal | coninhcount |  consrc  
798 ---------+---------+---------+------------+-------------+----------
799  ac      | check_a | c       | t          |           0 | (a <> 0)
800  bc      | check_b | c       | t          |           0 | (b <> 0)
801  cc      | check_a | c       | f          |           1 | (a <> 0)
802  cc      | check_b | c       | f          |           1 | (b <> 0)
803  cc      | check_c | c       | t          |           0 | (c <> 0)
804 (5 rows)
805
806 alter table cc no inherit bc;
807 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;
808  relname | conname | contype | conislocal | coninhcount |  consrc  
809 ---------+---------+---------+------------+-------------+----------
810  ac      | check_a | c       | t          |           0 | (a <> 0)
811  bc      | check_b | c       | t          |           0 | (b <> 0)
812  cc      | check_a | c       | f          |           1 | (a <> 0)
813  cc      | check_b | c       | t          |           0 | (b <> 0)
814  cc      | check_c | c       | t          |           0 | (c <> 0)
815 (5 rows)
816
817 drop table cc;
818 drop table bc;
819 drop table ac;
820 create table p1(f1 int);
821 create table p2(f2 int);
822 create table c1(f3 int) inherits(p1,p2);
823 insert into c1 values(1,-1,2);
824 alter table p2 add constraint cc check (f2>0);  -- fail
825 ERROR:  check constraint "cc" is violated by some row
826 alter table p2 add check (f2>0);  -- check it without a name, too
827 ERROR:  check constraint "p2_f2_check" is violated by some row
828 delete from c1;
829 insert into c1 values(1,1,2);
830 alter table p2 add check (f2>0);
831 insert into c1 values(1,-1,2);  -- fail
832 ERROR:  new row for relation "c1" violates check constraint "p2_f2_check"
833 create table c2(f3 int) inherits(p1,p2);
834 \d c2
835       Table "public.c2"
836  Column |  Type   | Modifiers 
837 --------+---------+-----------
838  f1     | integer | 
839  f2     | integer | 
840  f3     | integer | 
841 Check constraints:
842     "p2_f2_check" CHECK (f2 > 0)
843 Inherits: p1,
844           p2
845
846 create table c3 (f4 int) inherits(c1,c2);
847 NOTICE:  merging multiple inherited definitions of column "f1"
848 NOTICE:  merging multiple inherited definitions of column "f2"
849 NOTICE:  merging multiple inherited definitions of column "f3"
850 \d c3
851       Table "public.c3"
852  Column |  Type   | Modifiers 
853 --------+---------+-----------
854  f1     | integer | 
855  f2     | integer | 
856  f3     | integer | 
857  f4     | integer | 
858 Check constraints:
859     "p2_f2_check" CHECK (f2 > 0)
860 Inherits: c1,
861           c2
862
863 drop table p1 cascade;
864 NOTICE:  drop cascades to 3 other objects
865 DETAIL:  drop cascades to table c1
866 drop cascades to table c2
867 drop cascades to table c3
868 drop table p2 cascade;
869 create table pp1 (f1 int);
870 create table cc1 (f2 text, f3 int) inherits (pp1);
871 alter table pp1 add column a1 int check (a1 > 0);
872 \d cc1
873       Table "public.cc1"
874  Column |  Type   | Modifiers 
875 --------+---------+-----------
876  f1     | integer | 
877  f2     | text    | 
878  f3     | integer | 
879  a1     | integer | 
880 Check constraints:
881     "pp1_a1_check" CHECK (a1 > 0)
882 Inherits: pp1
883
884 create table cc2(f4 float) inherits(pp1,cc1);
885 NOTICE:  merging multiple inherited definitions of column "f1"
886 NOTICE:  merging multiple inherited definitions of column "a1"
887 \d cc2
888           Table "public.cc2"
889  Column |       Type       | Modifiers 
890 --------+------------------+-----------
891  f1     | integer          | 
892  a1     | integer          | 
893  f2     | text             | 
894  f3     | integer          | 
895  f4     | double precision | 
896 Check constraints:
897     "pp1_a1_check" CHECK (a1 > 0)
898 Inherits: pp1,
899           cc1
900
901 alter table pp1 add column a2 int check (a2 > 0);
902 NOTICE:  merging definition of column "a2" for child "cc2"
903 NOTICE:  merging constraint "pp1_a2_check" with inherited definition
904 \d cc2
905           Table "public.cc2"
906  Column |       Type       | Modifiers 
907 --------+------------------+-----------
908  f1     | integer          | 
909  a1     | integer          | 
910  f2     | text             | 
911  f3     | integer          | 
912  f4     | double precision | 
913  a2     | integer          | 
914 Check constraints:
915     "pp1_a1_check" CHECK (a1 > 0)
916     "pp1_a2_check" CHECK (a2 > 0)
917 Inherits: pp1,
918           cc1
919
920 drop table pp1 cascade;
921 NOTICE:  drop cascades to 2 other objects
922 DETAIL:  drop cascades to table cc1
923 drop cascades to table cc2
924 -- including storage and comments
925 CREATE TABLE t1 (a text CHECK (length(a) > 2) PRIMARY KEY, b text);
926 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "t1_pkey" for table "t1"
927 CREATE INDEX t1_b_key ON t1 (b);
928 CREATE INDEX t1_fnidx ON t1 ((a || b));
929 COMMENT ON COLUMN t1.a IS 'A';
930 COMMENT ON COLUMN t1.b IS 'B';
931 COMMENT ON CONSTRAINT t1_a_check ON t1 IS 't1_a_check';
932 COMMENT ON INDEX t1_pkey IS 'index pkey';
933 COMMENT ON INDEX t1_b_key IS 'index b_key';
934 ALTER TABLE t1 ALTER COLUMN a SET STORAGE MAIN;
935 CREATE TABLE t2 (c text);
936 ALTER TABLE t2 ALTER COLUMN c SET STORAGE EXTERNAL;
937 COMMENT ON COLUMN t2.c IS 'C';
938 CREATE TABLE t3 (a text CHECK (length(a) < 5), c text);
939 ALTER TABLE t3 ALTER COLUMN c SET STORAGE EXTERNAL;
940 ALTER TABLE t3 ALTER COLUMN a SET STORAGE MAIN;
941 COMMENT ON COLUMN t3.a IS 'A3';
942 COMMENT ON COLUMN t3.c IS 'C';
943 COMMENT ON CONSTRAINT t3_a_check ON t3 IS 't3_a_check';
944 CREATE TABLE t4 (a text, c text);
945 ALTER TABLE t4 ALTER COLUMN c SET STORAGE EXTERNAL;
946 CREATE TABLE t12_storage (LIKE t1 INCLUDING STORAGE, LIKE t2 INCLUDING STORAGE);
947 \d+ t12_storage
948              Table "public.t12_storage"
949  Column | Type | Modifiers | Storage  | Description 
950 --------+------+-----------+----------+-------------
951  a      | text | not null  | main     | 
952  b      | text |           | extended | 
953  c      | text |           | external | 
954 Has OIDs: no
955
956 CREATE TABLE t12_comments (LIKE t1 INCLUDING COMMENTS, LIKE t2 INCLUDING COMMENTS);
957 \d+ t12_comments
958             Table "public.t12_comments"
959  Column | Type | Modifiers | Storage  | Description 
960 --------+------+-----------+----------+-------------
961  a      | text | not null  | extended | A
962  b      | text |           | extended | B
963  c      | text |           | extended | C
964 Has OIDs: no
965
966 CREATE TABLE t1_inh (LIKE t1 INCLUDING CONSTRAINTS INCLUDING COMMENTS) INHERITS (t1);
967 NOTICE:  merging column "a" with inherited definition
968 NOTICE:  merging column "b" with inherited definition
969 NOTICE:  merging constraint "t1_a_check" with inherited definition
970 \d+ t1_inh
971                Table "public.t1_inh"
972  Column | Type | Modifiers | Storage  | Description 
973 --------+------+-----------+----------+-------------
974  a      | text | not null  | main     | A
975  b      | text |           | extended | B
976 Check constraints:
977     "t1_a_check" CHECK (length(a) > 2)
978 Inherits: t1
979 Has OIDs: no
980
981 SELECT description FROM pg_description, pg_constraint c WHERE classoid = 'pg_constraint'::regclass AND objoid = c.oid AND c.conrelid = 't1_inh'::regclass;
982  description 
983 -------------
984  t1_a_check
985 (1 row)
986
987 CREATE TABLE t13_inh () INHERITS (t1, t3);
988 NOTICE:  merging multiple inherited definitions of column "a"
989 \d+ t13_inh
990                Table "public.t13_inh"
991  Column | Type | Modifiers | Storage  | Description 
992 --------+------+-----------+----------+-------------
993  a      | text | not null  | main     | 
994  b      | text |           | extended | 
995  c      | text |           | external | 
996 Check constraints:
997     "t1_a_check" CHECK (length(a) > 2)
998     "t3_a_check" CHECK (length(a) < 5)
999 Inherits: t1,
1000           t3
1001 Has OIDs: no
1002
1003 CREATE TABLE t13_like (LIKE t3 INCLUDING CONSTRAINTS INCLUDING COMMENTS INCLUDING STORAGE) INHERITS (t1);
1004 NOTICE:  merging column "a" with inherited definition
1005 \d+ t13_like
1006               Table "public.t13_like"
1007  Column | Type | Modifiers | Storage  | Description 
1008 --------+------+-----------+----------+-------------
1009  a      | text | not null  | main     | A3
1010  b      | text |           | extended | 
1011  c      | text |           | external | C
1012 Check constraints:
1013     "t1_a_check" CHECK (length(a) > 2)
1014     "t3_a_check" CHECK (length(a) < 5)
1015 Inherits: t1
1016 Has OIDs: no
1017
1018 SELECT description FROM pg_description, pg_constraint c WHERE classoid = 'pg_constraint'::regclass AND objoid = c.oid AND c.conrelid = 't13_like'::regclass;
1019  description 
1020 -------------
1021  t3_a_check
1022 (1 row)
1023
1024 CREATE TABLE t_all (LIKE t1 INCLUDING ALL);
1025 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "t_all_pkey" for table "t_all"
1026 \d+ t_all
1027                 Table "public.t_all"
1028  Column | Type | Modifiers | Storage  | Description 
1029 --------+------+-----------+----------+-------------
1030  a      | text | not null  | main     | A
1031  b      | text |           | extended | B
1032 Indexes:
1033     "t_all_pkey" PRIMARY KEY, btree (a)
1034     "t_all_b_idx" btree (b)
1035     "t_all_expr_idx" btree ((a || b))
1036 Check constraints:
1037     "t1_a_check" CHECK (length(a) > 2)
1038 Has OIDs: no
1039
1040 SELECT c.relname, objsubid, description FROM pg_description, pg_index i, pg_class c WHERE classoid = 'pg_class'::regclass AND objoid = i.indexrelid AND c.oid = i.indexrelid AND i.indrelid = 't_all'::regclass ORDER BY c.relname, objsubid;
1041    relname   | objsubid | description 
1042 -------------+----------+-------------
1043  t_all_b_idx |        0 | index b_key
1044  t_all_pkey  |        0 | index pkey
1045 (2 rows)
1046
1047 CREATE TABLE inh_error1 () INHERITS (t1, t4);
1048 NOTICE:  merging multiple inherited definitions of column "a"
1049 ERROR:  inherited column "a" has a storage parameter conflict
1050 DETAIL:  MAIN versus EXTENDED
1051 CREATE TABLE inh_error2 (LIKE t4 INCLUDING STORAGE) INHERITS (t1);
1052 NOTICE:  merging column "a" with inherited definition
1053 ERROR:  column "a" has a storage parameter conflict
1054 DETAIL:  MAIN versus EXTENDED
1055 DROP TABLE t1, t2, t3, t4, t12_storage, t12_comments, t1_inh, t13_inh, t13_like, t_all;
1056 -- Test for renaming in simple multiple inheritance
1057 CREATE TABLE t1 (a int, b int);
1058 CREATE TABLE s1 (b int, c int);
1059 CREATE TABLE ts (d int) INHERITS (t1, s1);
1060 NOTICE:  merging multiple inherited definitions of column "b"
1061 ALTER TABLE t1 RENAME a TO aa;
1062 ALTER TABLE t1 RENAME b TO bb;                -- to be failed
1063 ERROR:  cannot rename inherited column "b"
1064 ALTER TABLE ts RENAME aa TO aaa;      -- to be failed
1065 ERROR:  cannot rename inherited column "aa"
1066 ALTER TABLE ts RENAME d TO dd;
1067 \d+ ts
1068                   Table "public.ts"
1069  Column |  Type   | Modifiers | Storage | Description 
1070 --------+---------+-----------+---------+-------------
1071  aa     | integer |           | plain   | 
1072  b      | integer |           | plain   | 
1073  c      | integer |           | plain   | 
1074  dd     | integer |           | plain   | 
1075 Inherits: t1,
1076           s1
1077 Has OIDs: no
1078
1079 DROP TABLE ts;
1080 -- Test for renaming in diamond inheritance
1081 CREATE TABLE t2 (x int) INHERITS (t1);
1082 CREATE TABLE t3 (y int) INHERITS (t1);
1083 CREATE TABLE t4 (z int) INHERITS (t2, t3);
1084 NOTICE:  merging multiple inherited definitions of column "aa"
1085 NOTICE:  merging multiple inherited definitions of column "b"
1086 ALTER TABLE t1 RENAME aa TO aaa;
1087 \d+ t4
1088                   Table "public.t4"
1089  Column |  Type   | Modifiers | Storage | Description 
1090 --------+---------+-----------+---------+-------------
1091  aaa    | integer |           | plain   | 
1092  b      | integer |           | plain   | 
1093  x      | integer |           | plain   | 
1094  y      | integer |           | plain   | 
1095  z      | integer |           | plain   | 
1096 Inherits: t2,
1097           t3
1098 Has OIDs: no
1099
1100 CREATE TABLE ts (d int) INHERITS (t2, s1);
1101 NOTICE:  merging multiple inherited definitions of column "b"
1102 ALTER TABLE t1 RENAME aaa TO aaaa;
1103 ALTER TABLE t1 RENAME b TO bb;                -- to be failed
1104 ERROR:  cannot rename inherited column "b"
1105 \d+ ts
1106                   Table "public.ts"
1107  Column |  Type   | Modifiers | Storage | Description 
1108 --------+---------+-----------+---------+-------------
1109  aaaa   | integer |           | plain   | 
1110  b      | integer |           | plain   | 
1111  x      | integer |           | plain   | 
1112  c      | integer |           | plain   | 
1113  d      | integer |           | plain   | 
1114 Inherits: t2,
1115           s1
1116 Has OIDs: no
1117
1118 WITH RECURSIVE r AS (
1119   SELECT 't1'::regclass AS inhrelid
1120 UNION ALL
1121   SELECT c.inhrelid FROM pg_inherits c, r WHERE r.inhrelid = c.inhparent
1122 )
1123 SELECT a.attrelid::regclass, a.attname, a.attinhcount, e.expected
1124   FROM (SELECT inhrelid, count(*) AS expected FROM pg_inherits
1125         WHERE inhparent IN (SELECT inhrelid FROM r) GROUP BY inhrelid) e
1126   JOIN pg_attribute a ON e.inhrelid = a.attrelid WHERE NOT attislocal
1127   ORDER BY a.attrelid::regclass::name, a.attnum;
1128  attrelid | attname | attinhcount | expected 
1129 ----------+---------+-------------+----------
1130  t2       | aaaa    |           1 |        1
1131  t2       | b       |           1 |        1
1132  t3       | aaaa    |           1 |        1
1133  t3       | b       |           1 |        1
1134  t4       | aaaa    |           2 |        2
1135  t4       | b       |           2 |        2
1136  t4       | x       |           1 |        2
1137  t4       | y       |           1 |        2
1138  ts       | aaaa    |           1 |        1
1139  ts       | b       |           2 |        1
1140  ts       | x       |           1 |        1
1141  ts       | c       |           1 |        1
1142 (12 rows)
1143
1144 DROP TABLE t1, s1 CASCADE;
1145 NOTICE:  drop cascades to 4 other objects
1146 DETAIL:  drop cascades to table t2
1147 drop cascades to table ts
1148 drop cascades to table t3
1149 drop cascades to table t4
1150 --
1151 -- Test merge-append plans for inheritance trees
1152 --
1153 create table matest0 (id serial primary key, name text);
1154 NOTICE:  CREATE TABLE will create implicit sequence "matest0_id_seq" for serial column "matest0.id"
1155 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "matest0_pkey" for table "matest0"
1156 create table matest1 (id integer primary key) inherits (matest0);
1157 NOTICE:  merging column "id" with inherited definition
1158 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "matest1_pkey" for table "matest1"
1159 create table matest2 (id integer primary key) inherits (matest0);
1160 NOTICE:  merging column "id" with inherited definition
1161 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "matest2_pkey" for table "matest2"
1162 create table matest3 (id integer primary key) inherits (matest0);
1163 NOTICE:  merging column "id" with inherited definition
1164 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "matest3_pkey" for table "matest3"
1165 create index matest0i on matest0 ((1-id));
1166 create index matest1i on matest1 ((1-id));
1167 -- create index matest2i on matest2 ((1-id));  -- intentionally missing
1168 create index matest3i on matest3 ((1-id));
1169 insert into matest1 (name) values ('Test 1');
1170 insert into matest1 (name) values ('Test 2');
1171 insert into matest2 (name) values ('Test 3');
1172 insert into matest2 (name) values ('Test 4');
1173 insert into matest3 (name) values ('Test 5');
1174 insert into matest3 (name) values ('Test 6');
1175 set enable_indexscan = off;  -- force use of seqscan/sort, so no merge
1176 explain (verbose, costs off) select * from matest0 order by 1-id;
1177                                    QUERY PLAN                                    
1178 ---------------------------------------------------------------------------------
1179  Sort
1180    Output: public.matest0.id, public.matest0.name, ((1 - public.matest0.id))
1181    Sort Key: ((1 - public.matest0.id))
1182    ->  Result
1183          Output: public.matest0.id, public.matest0.name, (1 - public.matest0.id)
1184          ->  Append
1185                ->  Seq Scan on public.matest0
1186                      Output: public.matest0.id, public.matest0.name
1187                ->  Seq Scan on public.matest1 matest0
1188                      Output: public.matest0.id, public.matest0.name
1189                ->  Seq Scan on public.matest2 matest0
1190                      Output: public.matest0.id, public.matest0.name
1191                ->  Seq Scan on public.matest3 matest0
1192                      Output: public.matest0.id, public.matest0.name
1193 (14 rows)
1194
1195 select * from matest0 order by 1-id;
1196  id |  name  
1197 ----+--------
1198   6 | Test 6
1199   5 | Test 5
1200   4 | Test 4
1201   3 | Test 3
1202   2 | Test 2
1203   1 | Test 1
1204 (6 rows)
1205
1206 reset enable_indexscan;
1207 set enable_seqscan = off;  -- plan with fewest seqscans should be merge
1208 explain (verbose, costs off) select * from matest0 order by 1-id;
1209                                          QUERY PLAN                                          
1210 ---------------------------------------------------------------------------------------------
1211  Result
1212    Output: public.matest0.id, public.matest0.name, ((1 - public.matest0.id))
1213    ->  Merge Append
1214          Sort Key: ((1 - public.matest0.id))
1215          ->  Index Scan using matest0i on public.matest0
1216                Output: public.matest0.id, public.matest0.name, (1 - public.matest0.id)
1217          ->  Index Scan using matest1i on public.matest1 matest0
1218                Output: public.matest0.id, public.matest0.name, (1 - public.matest0.id)
1219          ->  Sort
1220                Output: public.matest0.id, public.matest0.name, ((1 - public.matest0.id))
1221                Sort Key: ((1 - public.matest0.id))
1222                ->  Seq Scan on public.matest2 matest0
1223                      Output: public.matest0.id, public.matest0.name, (1 - public.matest0.id)
1224          ->  Index Scan using matest3i on public.matest3 matest0
1225                Output: public.matest0.id, public.matest0.name, (1 - public.matest0.id)
1226 (15 rows)
1227
1228 select * from matest0 order by 1-id;
1229  id |  name  
1230 ----+--------
1231   6 | Test 6
1232   5 | Test 5
1233   4 | Test 4
1234   3 | Test 3
1235   2 | Test 2
1236   1 | Test 1
1237 (6 rows)
1238
1239 reset enable_seqscan;
1240 drop table matest0 cascade;
1241 NOTICE:  drop cascades to 3 other objects
1242 DETAIL:  drop cascades to table matest1
1243 drop cascades to table matest2
1244 drop cascades to table matest3