2 -- Test inheritance features
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;
63 SELECT relname, b.* FROM b, pg_class where b.tableoid = pg_class.oid;
65 ---------+----------+----
80 SELECT relname, c.* FROM c, pg_class where c.tableoid = pg_class.oid;
82 ---------+----------+----
97 SELECT relname, d.* FROM d, pg_class where d.tableoid = pg_class.oid;
98 relname | aa | bb | cc | dd
99 ---------+----------+----+----+----
108 SELECT relname, a.* FROM ONLY a, pg_class where a.tableoid = pg_class.oid;
119 SELECT relname, b.* FROM ONLY b, pg_class where b.tableoid = pg_class.oid;
121 ---------+----------+----
130 SELECT relname, c.* FROM ONLY c, pg_class where c.tableoid = pg_class.oid;
132 ---------+----------+----
141 SELECT relname, d.* FROM ONLY d, pg_class where d.tableoid = pg_class.oid;
142 relname | aa | bb | cc | dd
143 ---------+----------+----+----+----
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;
186 SELECT relname, b.* FROM b, pg_class where b.tableoid = pg_class.oid;
188 ---------+----------+----
203 SELECT relname, c.* FROM c, pg_class where c.tableoid = pg_class.oid;
205 ---------+----------+----
220 SELECT relname, d.* FROM d, pg_class where d.tableoid = pg_class.oid;
221 relname | aa | bb | cc | dd
222 ---------+----------+----+----+----
231 SELECT relname, a.* FROM ONLY a, pg_class where a.tableoid = pg_class.oid;
242 SELECT relname, b.* FROM ONLY b, pg_class where b.tableoid = pg_class.oid;
244 ---------+----------+----
253 SELECT relname, c.* FROM ONLY c, pg_class where c.tableoid = pg_class.oid;
255 ---------+----------+----
264 SELECT relname, d.* FROM ONLY d, pg_class where d.tableoid = pg_class.oid;
265 relname | aa | bb | cc | dd
266 ---------+----------+----+----+----
275 UPDATE b SET aa='new';
276 SELECT relname, a.* FROM a, pg_class where a.tableoid = pg_class.oid;
305 SELECT relname, b.* FROM b, pg_class where b.tableoid = pg_class.oid;
322 SELECT relname, c.* FROM c, pg_class where c.tableoid = pg_class.oid;
324 ---------+----------+----
339 SELECT relname, d.* FROM d, pg_class where d.tableoid = pg_class.oid;
340 relname | aa | bb | cc | dd
341 ---------+-----+----+----+----
350 SELECT relname, a.* FROM ONLY a, pg_class where a.tableoid = pg_class.oid;
361 SELECT relname, b.* FROM ONLY b, pg_class where b.tableoid = pg_class.oid;
372 SELECT relname, c.* FROM ONLY c, pg_class where c.tableoid = pg_class.oid;
374 ---------+----------+----
383 SELECT relname, d.* FROM ONLY d, pg_class where d.tableoid = pg_class.oid;
384 relname | aa | bb | cc | dd
385 ---------+-----+----+----+----
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;
419 SELECT relname, b.* FROM b, pg_class where b.tableoid = pg_class.oid;
436 SELECT relname, c.* FROM c, pg_class where c.tableoid = pg_class.oid;
447 SELECT relname, d.* FROM d, pg_class where d.tableoid = pg_class.oid;
448 relname | aa | bb | cc | dd
449 ---------+-----+----+----+----
458 SELECT relname, a.* FROM ONLY a, pg_class where a.tableoid = pg_class.oid;
469 SELECT relname, b.* FROM ONLY b, pg_class where b.tableoid = pg_class.oid;
480 SELECT relname, c.* FROM ONLY c, pg_class where c.tableoid = pg_class.oid;
485 SELECT relname, d.* FROM ONLY d, pg_class where d.tableoid = pg_class.oid;
486 relname | aa | bb | cc | dd
487 ---------+-----+----+----+----
497 SELECT relname, a.* FROM a, pg_class where a.tableoid = pg_class.oid;
502 SELECT relname, b.* FROM b, pg_class where b.tableoid = pg_class.oid;
507 SELECT relname, c.* FROM c, pg_class where c.tableoid = pg_class.oid;
512 SELECT relname, d.* FROM d, pg_class where d.tableoid = pg_class.oid;
513 relname | aa | bb | cc | dd
514 ---------+----+----+----+----
517 SELECT relname, a.* FROM ONLY a, pg_class where a.tableoid = pg_class.oid;
522 SELECT relname, b.* FROM ONLY b, pg_class where b.tableoid = pg_class.oid;
527 SELECT relname, c.* FROM ONLY c, pg_class where c.tableoid = pg_class.oid;
532 SELECT relname, d.* FROM ONLY d, pg_class where d.tableoid = pg_class.oid;
533 relname | aa | bb | cc | dd
534 ---------+----+----+----+----
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
574 /* Test inheritance of structure (LIKE) */
575 CREATE TABLE inhx (xx text DEFAULT 'text');
577 * Test double inheritance
579 * Ensure that defaults are NOT included unless
580 * INCLUDING DEFAULTS is specified
582 CREATE TABLE inhe (ee text, LIKE inhx) inherits (b);
583 INSERT INTO inhe VALUES ('ee-col1', 'ee-col2', DEFAULT, 'ee-col4');
584 SELECT * FROM inhe; /* Columns aa, bb, xx value NULL, ee */
586 ---------+---------+----+---------
587 ee-col1 | ee-col2 | | ee-col4
590 SELECT * FROM inhx; /* Empty set since LIKE inherits structure only */
595 SELECT * FROM b; /* Has ee entry */
601 SELECT * FROM a; /* Has ee entry */
607 CREATE TABLE inhf (LIKE inhx, LIKE inhx); /* Throw error */
608 ERROR: column "xx" specified more than once
609 CREATE TABLE inhf (LIKE inhx INCLUDING DEFAULTS INCLUDING CONSTRAINTS);
610 INSERT INTO inhf DEFAULT VALUES;
611 SELECT * FROM inhf; /* Single entry with value 'text' */
617 ALTER TABLE inhx add constraint foo CHECK (xx = 'text');
618 ALTER TABLE inhx ADD PRIMARY KEY (xx);
619 NOTICE: ALTER TABLE / ADD PRIMARY KEY will create implicit index "inhx_pkey" for table "inhx"
620 CREATE TABLE inhg (LIKE inhx); /* Doesn't copy constraint */
621 INSERT INTO inhg VALUES ('foo');
623 CREATE TABLE inhg (x text, LIKE inhx INCLUDING CONSTRAINTS, y text); /* Copies constraints */
624 INSERT INTO inhg VALUES ('x', 'text', 'y'); /* Succeeds */
625 INSERT INTO inhg VALUES ('x', 'text', 'y'); /* Succeeds -- Unique constraints not copied */
626 INSERT INTO inhg VALUES ('x', 'foo', 'y'); /* fails due to constraint */
627 ERROR: new row for relation "inhg" violates check constraint "foo"
628 SELECT * FROM inhg; /* Two records with three columns in order x=x, xx=text, y=y */
636 CREATE TABLE inhg (x text, LIKE inhx INCLUDING INDEXES, y text); /* copies indexes */
637 NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "inhg_pkey" for table "inhg"
638 INSERT INTO inhg VALUES (5, 10);
639 INSERT INTO inhg VALUES (20, 10); -- should fail
640 ERROR: duplicate key value violates unique constraint "inhg_pkey"
642 /* Multiple primary keys creation should fail */
643 CREATE TABLE inhg (x text, LIKE inhx INCLUDING INDEXES, PRIMARY KEY(x)); /* fails */
644 ERROR: multiple primary keys for table "inhg" are not allowed
645 CREATE TABLE inhz (xx text DEFAULT 'text', yy int UNIQUE);
646 NOTICE: CREATE TABLE / UNIQUE will create implicit index "inhz_yy_key" for table "inhz"
647 CREATE UNIQUE INDEX inhz_xx_idx on inhz (xx) WHERE xx <> 'test';
648 /* Ok to create multiple unique indexes */
649 CREATE TABLE inhg (x text UNIQUE, LIKE inhz INCLUDING INDEXES);
650 NOTICE: CREATE TABLE / UNIQUE will create implicit index "inhg_x_key" for table "inhg"
651 NOTICE: CREATE TABLE / UNIQUE will create implicit index "inhg_yy_key" for table "inhg"
652 INSERT INTO inhg (xx, yy, x) VALUES ('test', 5, 10);
653 INSERT INTO inhg (xx, yy, x) VALUES ('test', 10, 15);
654 INSERT INTO inhg (xx, yy, x) VALUES ('foo', 10, 15); -- should fail
655 ERROR: duplicate key value violates unique constraint "inhg_x_key"
658 -- Test changing the type of inherited columns
659 insert into d values('test','one','two','three');
660 alter table a alter column aa type integer using bit_length(aa);
663 ----+-----+-----+-------
664 32 | one | two | three
667 -- Tests for casting between the rowtypes of parent and child
668 -- tables. See the pgsql-hackers thread beginning Dec. 4/04
669 create table base (i integer);
670 create table derived () inherits (base);
671 insert into derived (i) values (0);
672 select derived::base from derived;
680 create table p1(ff1 int);
681 create table p2(f1 text);
682 create function p2text(p2) returns text as 'select $1.f1' language sql;
683 create table c1(f3 int) inherits(p1,p2);
684 insert into c1 values(123456789, 'hi', 42);
685 select p2text(c1.*) from c1;
691 drop function p2text(p2);
695 CREATE TABLE ac (aa TEXT);
696 alter table ac add constraint ac_check check (aa is not null);
697 CREATE TABLE bc (bb TEXT) INHERITS (ac);
698 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;
699 relname | conname | contype | conislocal | coninhcount | consrc
700 ---------+----------+---------+------------+-------------+------------------
701 ac | ac_check | c | t | 0 | (aa IS NOT NULL)
702 bc | ac_check | c | f | 1 | (aa IS NOT NULL)
705 insert into ac (aa) values (NULL);
706 ERROR: new row for relation "ac" violates check constraint "ac_check"
707 insert into bc (aa) values (NULL);
708 ERROR: new row for relation "bc" violates check constraint "ac_check"
709 alter table bc drop constraint ac_check; -- fail, disallowed
710 ERROR: cannot drop inherited constraint "ac_check" of relation "bc"
711 alter table ac drop constraint ac_check;
712 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;
713 relname | conname | contype | conislocal | coninhcount | consrc
714 ---------+---------+---------+------------+-------------+--------
717 -- try the unnamed-constraint case
718 alter table ac add check (aa is not null);
719 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;
720 relname | conname | contype | conislocal | coninhcount | consrc
721 ---------+-------------+---------+------------+-------------+------------------
722 ac | ac_aa_check | c | t | 0 | (aa IS NOT NULL)
723 bc | ac_aa_check | c | f | 1 | (aa IS NOT NULL)
726 insert into ac (aa) values (NULL);
727 ERROR: new row for relation "ac" violates check constraint "ac_aa_check"
728 insert into bc (aa) values (NULL);
729 ERROR: new row for relation "bc" violates check constraint "ac_aa_check"
730 alter table bc drop constraint ac_aa_check; -- fail, disallowed
731 ERROR: cannot drop inherited constraint "ac_aa_check" of relation "bc"
732 alter table ac drop constraint ac_aa_check;
733 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;
734 relname | conname | contype | conislocal | coninhcount | consrc
735 ---------+---------+---------+------------+-------------+--------
738 alter table ac add constraint ac_check check (aa is not null);
739 alter table bc no inherit ac;
740 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;
741 relname | conname | contype | conislocal | coninhcount | consrc
742 ---------+----------+---------+------------+-------------+------------------
743 ac | ac_check | c | t | 0 | (aa IS NOT NULL)
744 bc | ac_check | c | t | 0 | (aa IS NOT NULL)
747 alter table bc drop constraint ac_check;
748 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;
749 relname | conname | contype | conislocal | coninhcount | consrc
750 ---------+----------+---------+------------+-------------+------------------
751 ac | ac_check | c | t | 0 | (aa IS NOT NULL)
754 alter table ac drop constraint ac_check;
755 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;
756 relname | conname | contype | conislocal | coninhcount | consrc
757 ---------+---------+---------+------------+-------------+--------
762 create table ac (a int constraint check_a check (a <> 0));
763 create table bc (a int constraint check_a check (a <> 0), b int constraint check_b check (b <> 0)) inherits (ac);
764 NOTICE: merging column "a" with inherited definition
765 NOTICE: merging constraint "check_a" with inherited definition
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') order by 1,2;
767 relname | conname | contype | conislocal | coninhcount | consrc
768 ---------+---------+---------+------------+-------------+----------
769 ac | check_a | c | t | 0 | (a <> 0)
770 bc | check_a | c | t | 1 | (a <> 0)
771 bc | check_b | c | t | 0 | (b <> 0)
776 create table ac (a int constraint check_a check (a <> 0));
777 create table bc (b int constraint check_b check (b <> 0));
778 create table cc (c int constraint check_c check (c <> 0)) inherits (ac, bc);
779 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;
780 relname | conname | contype | conislocal | coninhcount | consrc
781 ---------+---------+---------+------------+-------------+----------
782 ac | check_a | c | t | 0 | (a <> 0)
783 bc | check_b | c | t | 0 | (b <> 0)
784 cc | check_a | c | f | 1 | (a <> 0)
785 cc | check_b | c | f | 1 | (b <> 0)
786 cc | check_c | c | t | 0 | (c <> 0)
789 alter table cc no inherit bc;
790 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;
791 relname | conname | contype | conislocal | coninhcount | consrc
792 ---------+---------+---------+------------+-------------+----------
793 ac | check_a | c | t | 0 | (a <> 0)
794 bc | check_b | c | t | 0 | (b <> 0)
795 cc | check_a | c | f | 1 | (a <> 0)
796 cc | check_b | c | t | 0 | (b <> 0)
797 cc | check_c | c | t | 0 | (c <> 0)
803 create table p1(f1 int);
804 create table p2(f2 int);
805 create table c1(f3 int) inherits(p1,p2);
806 insert into c1 values(1,-1,2);
807 alter table p2 add constraint cc check (f2>0); -- fail
808 ERROR: check constraint "cc" is violated by some row
809 alter table p2 add check (f2>0); -- check it without a name, too
810 ERROR: check constraint "p2_f2_check" is violated by some row
812 insert into c1 values(1,1,2);
813 alter table p2 add check (f2>0);
814 insert into c1 values(1,-1,2); -- fail
815 ERROR: new row for relation "c1" violates check constraint "p2_f2_check"
816 create table c2(f3 int) inherits(p1,p2);
819 Column | Type | Modifiers
820 --------+---------+-----------
825 "p2_f2_check" CHECK (f2 > 0)
829 create table c3 (f4 int) inherits(c1,c2);
830 NOTICE: merging multiple inherited definitions of column "f1"
831 NOTICE: merging multiple inherited definitions of column "f2"
832 NOTICE: merging multiple inherited definitions of column "f3"
835 Column | Type | Modifiers
836 --------+---------+-----------
842 "p2_f2_check" CHECK (f2 > 0)
846 drop table p1 cascade;
847 NOTICE: drop cascades to 3 other objects
848 DETAIL: drop cascades to table c1
849 drop cascades to table c2
850 drop cascades to table c3
851 drop table p2 cascade;
852 create table pp1 (f1 int);
853 create table cc1 (f2 text, f3 int) inherits (pp1);
854 alter table pp1 add column a1 int check (a1 > 0);
857 Column | Type | Modifiers
858 --------+---------+-----------
864 "pp1_a1_check" CHECK (a1 > 0)
867 create table cc2(f4 float) inherits(pp1,cc1);
868 NOTICE: merging multiple inherited definitions of column "f1"
869 NOTICE: merging multiple inherited definitions of column "a1"
872 Column | Type | Modifiers
873 --------+------------------+-----------
878 f4 | double precision |
880 "pp1_a1_check" CHECK (a1 > 0)
884 alter table pp1 add column a2 int check (a2 > 0);
885 NOTICE: merging definition of column "a2" for child "cc2"
886 NOTICE: merging constraint "pp1_a2_check" with inherited definition
889 Column | Type | Modifiers
890 --------+------------------+-----------
895 f4 | double precision |
898 "pp1_a1_check" CHECK (a1 > 0)
899 "pp1_a2_check" CHECK (a2 > 0)
903 drop table pp1 cascade;
904 NOTICE: drop cascades to 2 other objects
905 DETAIL: drop cascades to table cc1
906 drop cascades to table cc2