mirror of https://github.com/postgres/postgres
The original implementation of this interpreted it as a kind of "inheritance" facility and named all the internal structures accordingly. This turned out to be very confusing, because it has nothing to do with the INHERITS feature. So rename all the internal parser infrastructure, update the comments, adjust the error messages, and split up the regression tests.pull/1/head
parent
0a41e86584
commit
db49517c62
@ -0,0 +1,222 @@ |
||||
/* Test inheritance of structure (LIKE) */ |
||||
CREATE TABLE inhx (xx text DEFAULT 'text'); |
||||
/* |
||||
* Test double inheritance |
||||
* |
||||
* Ensure that defaults are NOT included unless |
||||
* INCLUDING DEFAULTS is specified |
||||
*/ |
||||
CREATE TABLE ctla (aa TEXT); |
||||
CREATE TABLE ctlb (bb TEXT) INHERITS (ctla); |
||||
CREATE TABLE inhe (ee text, LIKE inhx) inherits (ctlb); |
||||
INSERT INTO inhe VALUES ('ee-col1', 'ee-col2', DEFAULT, 'ee-col4'); |
||||
SELECT * FROM inhe; /* Columns aa, bb, xx value NULL, ee */ |
||||
aa | bb | ee | xx |
||||
---------+---------+----+--------- |
||||
ee-col1 | ee-col2 | | ee-col4 |
||||
(1 row) |
||||
|
||||
SELECT * FROM inhx; /* Empty set since LIKE inherits structure only */ |
||||
xx |
||||
---- |
||||
(0 rows) |
||||
|
||||
SELECT * FROM ctlb; /* Has ee entry */ |
||||
aa | bb |
||||
---------+--------- |
||||
ee-col1 | ee-col2 |
||||
(1 row) |
||||
|
||||
SELECT * FROM ctla; /* Has ee entry */ |
||||
aa |
||||
--------- |
||||
ee-col1 |
||||
(1 row) |
||||
|
||||
CREATE TABLE inhf (LIKE inhx, LIKE inhx); /* Throw error */ |
||||
ERROR: column "xx" specified more than once |
||||
CREATE TABLE inhf (LIKE inhx INCLUDING DEFAULTS INCLUDING CONSTRAINTS); |
||||
INSERT INTO inhf DEFAULT VALUES; |
||||
SELECT * FROM inhf; /* Single entry with value 'text' */ |
||||
xx |
||||
------ |
||||
text |
||||
(1 row) |
||||
|
||||
ALTER TABLE inhx add constraint foo CHECK (xx = 'text'); |
||||
ALTER TABLE inhx ADD PRIMARY KEY (xx); |
||||
NOTICE: ALTER TABLE / ADD PRIMARY KEY will create implicit index "inhx_pkey" for table "inhx" |
||||
CREATE TABLE inhg (LIKE inhx); /* Doesn't copy constraint */ |
||||
INSERT INTO inhg VALUES ('foo'); |
||||
DROP TABLE inhg; |
||||
CREATE TABLE inhg (x text, LIKE inhx INCLUDING CONSTRAINTS, y text); /* Copies constraints */ |
||||
INSERT INTO inhg VALUES ('x', 'text', 'y'); /* Succeeds */ |
||||
INSERT INTO inhg VALUES ('x', 'text', 'y'); /* Succeeds -- Unique constraints not copied */ |
||||
INSERT INTO inhg VALUES ('x', 'foo', 'y'); /* fails due to constraint */ |
||||
ERROR: new row for relation "inhg" violates check constraint "foo" |
||||
DETAIL: Failing row contains (x, foo, y). |
||||
SELECT * FROM inhg; /* Two records with three columns in order x=x, xx=text, y=y */ |
||||
x | xx | y |
||||
---+------+--- |
||||
x | text | y |
||||
x | text | y |
||||
(2 rows) |
||||
|
||||
DROP TABLE inhg; |
||||
CREATE TABLE inhg (x text, LIKE inhx INCLUDING INDEXES, y text); /* copies indexes */ |
||||
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "inhg_pkey" for table "inhg" |
||||
INSERT INTO inhg VALUES (5, 10); |
||||
INSERT INTO inhg VALUES (20, 10); -- should fail |
||||
ERROR: duplicate key value violates unique constraint "inhg_pkey" |
||||
DETAIL: Key (xx)=(10) already exists. |
||||
DROP TABLE inhg; |
||||
/* Multiple primary keys creation should fail */ |
||||
CREATE TABLE inhg (x text, LIKE inhx INCLUDING INDEXES, PRIMARY KEY(x)); /* fails */ |
||||
ERROR: multiple primary keys for table "inhg" are not allowed |
||||
CREATE TABLE inhz (xx text DEFAULT 'text', yy int UNIQUE); |
||||
NOTICE: CREATE TABLE / UNIQUE will create implicit index "inhz_yy_key" for table "inhz" |
||||
CREATE UNIQUE INDEX inhz_xx_idx on inhz (xx) WHERE xx <> 'test'; |
||||
/* Ok to create multiple unique indexes */ |
||||
CREATE TABLE inhg (x text UNIQUE, LIKE inhz INCLUDING INDEXES); |
||||
NOTICE: CREATE TABLE / UNIQUE will create implicit index "inhg_x_key" for table "inhg" |
||||
NOTICE: CREATE TABLE / UNIQUE will create implicit index "inhg_yy_key" for table "inhg" |
||||
INSERT INTO inhg (xx, yy, x) VALUES ('test', 5, 10); |
||||
INSERT INTO inhg (xx, yy, x) VALUES ('test', 10, 15); |
||||
INSERT INTO inhg (xx, yy, x) VALUES ('foo', 10, 15); -- should fail |
||||
ERROR: duplicate key value violates unique constraint "inhg_x_key" |
||||
DETAIL: Key (x)=(15) already exists. |
||||
DROP TABLE inhg; |
||||
DROP TABLE inhz; |
||||
-- including storage and comments |
||||
CREATE TABLE ctlt1 (a text CHECK (length(a) > 2) PRIMARY KEY, b text); |
||||
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "ctlt1_pkey" for table "ctlt1" |
||||
CREATE INDEX ctlt1_b_key ON ctlt1 (b); |
||||
CREATE INDEX ctlt1_fnidx ON ctlt1 ((a || b)); |
||||
COMMENT ON COLUMN ctlt1.a IS 'A'; |
||||
COMMENT ON COLUMN ctlt1.b IS 'B'; |
||||
COMMENT ON CONSTRAINT ctlt1_a_check ON ctlt1 IS 't1_a_check'; |
||||
COMMENT ON INDEX ctlt1_pkey IS 'index pkey'; |
||||
COMMENT ON INDEX ctlt1_b_key IS 'index b_key'; |
||||
ALTER TABLE ctlt1 ALTER COLUMN a SET STORAGE MAIN; |
||||
CREATE TABLE ctlt2 (c text); |
||||
ALTER TABLE ctlt2 ALTER COLUMN c SET STORAGE EXTERNAL; |
||||
COMMENT ON COLUMN ctlt2.c IS 'C'; |
||||
CREATE TABLE ctlt3 (a text CHECK (length(a) < 5), c text); |
||||
ALTER TABLE ctlt3 ALTER COLUMN c SET STORAGE EXTERNAL; |
||||
ALTER TABLE ctlt3 ALTER COLUMN a SET STORAGE MAIN; |
||||
COMMENT ON COLUMN ctlt3.a IS 'A3'; |
||||
COMMENT ON COLUMN ctlt3.c IS 'C'; |
||||
COMMENT ON CONSTRAINT ctlt3_a_check ON ctlt3 IS 't3_a_check'; |
||||
CREATE TABLE ctlt4 (a text, c text); |
||||
ALTER TABLE ctlt4 ALTER COLUMN c SET STORAGE EXTERNAL; |
||||
CREATE TABLE ctlt12_storage (LIKE ctlt1 INCLUDING STORAGE, LIKE ctlt2 INCLUDING STORAGE); |
||||
\d+ ctlt12_storage |
||||
Table "public.ctlt12_storage" |
||||
Column | Type | Modifiers | Storage | Stats target | Description |
||||
--------+------+-----------+----------+--------------+------------- |
||||
a | text | not null | main | | |
||||
b | text | | extended | | |
||||
c | text | | external | | |
||||
Has OIDs: no |
||||
|
||||
CREATE TABLE ctlt12_comments (LIKE ctlt1 INCLUDING COMMENTS, LIKE ctlt2 INCLUDING COMMENTS); |
||||
\d+ ctlt12_comments |
||||
Table "public.ctlt12_comments" |
||||
Column | Type | Modifiers | Storage | Stats target | Description |
||||
--------+------+-----------+----------+--------------+------------- |
||||
a | text | not null | extended | | A |
||||
b | text | | extended | | B |
||||
c | text | | extended | | C |
||||
Has OIDs: no |
||||
|
||||
CREATE TABLE ctlt1_inh (LIKE ctlt1 INCLUDING CONSTRAINTS INCLUDING COMMENTS) INHERITS (ctlt1); |
||||
NOTICE: merging column "a" with inherited definition |
||||
NOTICE: merging column "b" with inherited definition |
||||
NOTICE: merging constraint "ctlt1_a_check" with inherited definition |
||||
\d+ ctlt1_inh |
||||
Table "public.ctlt1_inh" |
||||
Column | Type | Modifiers | Storage | Stats target | Description |
||||
--------+------+-----------+----------+--------------+------------- |
||||
a | text | not null | main | | A |
||||
b | text | | extended | | B |
||||
Check constraints: |
||||
"ctlt1_a_check" CHECK (length(a) > 2) |
||||
Inherits: ctlt1 |
||||
Has OIDs: no |
||||
|
||||
SELECT description FROM pg_description, pg_constraint c WHERE classoid = 'pg_constraint'::regclass AND objoid = c.oid AND c.conrelid = 'ctlt1_inh'::regclass; |
||||
description |
||||
------------- |
||||
t1_a_check |
||||
(1 row) |
||||
|
||||
CREATE TABLE ctlt13_inh () INHERITS (ctlt1, ctlt3); |
||||
NOTICE: merging multiple inherited definitions of column "a" |
||||
\d+ ctlt13_inh |
||||
Table "public.ctlt13_inh" |
||||
Column | Type | Modifiers | Storage | Stats target | Description |
||||
--------+------+-----------+----------+--------------+------------- |
||||
a | text | not null | main | | |
||||
b | text | | extended | | |
||||
c | text | | external | | |
||||
Check constraints: |
||||
"ctlt1_a_check" CHECK (length(a) > 2) |
||||
"ctlt3_a_check" CHECK (length(a) < 5) |
||||
Inherits: ctlt1, |
||||
ctlt3 |
||||
Has OIDs: no |
||||
|
||||
CREATE TABLE ctlt13_like (LIKE ctlt3 INCLUDING CONSTRAINTS INCLUDING COMMENTS INCLUDING STORAGE) INHERITS (ctlt1); |
||||
NOTICE: merging column "a" with inherited definition |
||||
\d+ ctlt13_like |
||||
Table "public.ctlt13_like" |
||||
Column | Type | Modifiers | Storage | Stats target | Description |
||||
--------+------+-----------+----------+--------------+------------- |
||||
a | text | not null | main | | A3 |
||||
b | text | | extended | | |
||||
c | text | | external | | C |
||||
Check constraints: |
||||
"ctlt1_a_check" CHECK (length(a) > 2) |
||||
"ctlt3_a_check" CHECK (length(a) < 5) |
||||
Inherits: ctlt1 |
||||
Has OIDs: no |
||||
|
||||
SELECT description FROM pg_description, pg_constraint c WHERE classoid = 'pg_constraint'::regclass AND objoid = c.oid AND c.conrelid = 'ctlt13_like'::regclass; |
||||
description |
||||
------------- |
||||
t3_a_check |
||||
(1 row) |
||||
|
||||
CREATE TABLE ctlt_all (LIKE ctlt1 INCLUDING ALL); |
||||
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "ctlt_all_pkey" for table "ctlt_all" |
||||
\d+ ctlt_all |
||||
Table "public.ctlt_all" |
||||
Column | Type | Modifiers | Storage | Stats target | Description |
||||
--------+------+-----------+----------+--------------+------------- |
||||
a | text | not null | main | | A |
||||
b | text | | extended | | B |
||||
Indexes: |
||||
"ctlt_all_pkey" PRIMARY KEY, btree (a) |
||||
"ctlt_all_b_idx" btree (b) |
||||
"ctlt_all_expr_idx" btree ((a || b)) |
||||
Check constraints: |
||||
"ctlt1_a_check" CHECK (length(a) > 2) |
||||
Has OIDs: no |
||||
|
||||
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 = 'ctlt_all'::regclass ORDER BY c.relname, objsubid; |
||||
relname | objsubid | description |
||||
----------------+----------+------------- |
||||
ctlt_all_b_idx | 0 | index b_key |
||||
ctlt_all_pkey | 0 | index pkey |
||||
(2 rows) |
||||
|
||||
CREATE TABLE inh_error1 () INHERITS (ctlt1, ctlt4); |
||||
NOTICE: merging multiple inherited definitions of column "a" |
||||
ERROR: inherited column "a" has a storage parameter conflict |
||||
DETAIL: MAIN versus EXTENDED |
||||
CREATE TABLE inh_error2 (LIKE ctlt4 INCLUDING STORAGE) INHERITS (ctlt1); |
||||
NOTICE: merging column "a" with inherited definition |
||||
ERROR: column "a" has a storage parameter conflict |
||||
DETAIL: MAIN versus EXTENDED |
||||
DROP TABLE ctlt1, ctlt2, ctlt3, ctlt4, ctlt12_storage, ctlt12_comments, ctlt1_inh, ctlt13_inh, ctlt13_like, ctlt_all, ctla, ctlb CASCADE; |
||||
NOTICE: drop cascades to table inhe |
@ -0,0 +1,99 @@ |
||||
/* Test inheritance of structure (LIKE) */ |
||||
CREATE TABLE inhx (xx text DEFAULT 'text'); |
||||
|
||||
/* |
||||
* Test double inheritance |
||||
* |
||||
* Ensure that defaults are NOT included unless |
||||
* INCLUDING DEFAULTS is specified |
||||
*/ |
||||
CREATE TABLE ctla (aa TEXT); |
||||
CREATE TABLE ctlb (bb TEXT) INHERITS (ctla); |
||||
|
||||
CREATE TABLE inhe (ee text, LIKE inhx) inherits (ctlb); |
||||
INSERT INTO inhe VALUES ('ee-col1', 'ee-col2', DEFAULT, 'ee-col4'); |
||||
SELECT * FROM inhe; /* Columns aa, bb, xx value NULL, ee */ |
||||
SELECT * FROM inhx; /* Empty set since LIKE inherits structure only */ |
||||
SELECT * FROM ctlb; /* Has ee entry */ |
||||
SELECT * FROM ctla; /* Has ee entry */ |
||||
|
||||
CREATE TABLE inhf (LIKE inhx, LIKE inhx); /* Throw error */ |
||||
|
||||
CREATE TABLE inhf (LIKE inhx INCLUDING DEFAULTS INCLUDING CONSTRAINTS); |
||||
INSERT INTO inhf DEFAULT VALUES; |
||||
SELECT * FROM inhf; /* Single entry with value 'text' */ |
||||
|
||||
ALTER TABLE inhx add constraint foo CHECK (xx = 'text'); |
||||
ALTER TABLE inhx ADD PRIMARY KEY (xx); |
||||
CREATE TABLE inhg (LIKE inhx); /* Doesn't copy constraint */ |
||||
INSERT INTO inhg VALUES ('foo'); |
||||
DROP TABLE inhg; |
||||
CREATE TABLE inhg (x text, LIKE inhx INCLUDING CONSTRAINTS, y text); /* Copies constraints */ |
||||
INSERT INTO inhg VALUES ('x', 'text', 'y'); /* Succeeds */ |
||||
INSERT INTO inhg VALUES ('x', 'text', 'y'); /* Succeeds -- Unique constraints not copied */ |
||||
INSERT INTO inhg VALUES ('x', 'foo', 'y'); /* fails due to constraint */ |
||||
SELECT * FROM inhg; /* Two records with three columns in order x=x, xx=text, y=y */ |
||||
DROP TABLE inhg; |
||||
|
||||
CREATE TABLE inhg (x text, LIKE inhx INCLUDING INDEXES, y text); /* copies indexes */ |
||||
INSERT INTO inhg VALUES (5, 10); |
||||
INSERT INTO inhg VALUES (20, 10); -- should fail |
||||
DROP TABLE inhg; |
||||
/* Multiple primary keys creation should fail */ |
||||
CREATE TABLE inhg (x text, LIKE inhx INCLUDING INDEXES, PRIMARY KEY(x)); /* fails */ |
||||
CREATE TABLE inhz (xx text DEFAULT 'text', yy int UNIQUE); |
||||
CREATE UNIQUE INDEX inhz_xx_idx on inhz (xx) WHERE xx <> 'test'; |
||||
/* Ok to create multiple unique indexes */ |
||||
CREATE TABLE inhg (x text UNIQUE, LIKE inhz INCLUDING INDEXES); |
||||
INSERT INTO inhg (xx, yy, x) VALUES ('test', 5, 10); |
||||
INSERT INTO inhg (xx, yy, x) VALUES ('test', 10, 15); |
||||
INSERT INTO inhg (xx, yy, x) VALUES ('foo', 10, 15); -- should fail |
||||
DROP TABLE inhg; |
||||
DROP TABLE inhz; |
||||
|
||||
-- including storage and comments |
||||
CREATE TABLE ctlt1 (a text CHECK (length(a) > 2) PRIMARY KEY, b text); |
||||
CREATE INDEX ctlt1_b_key ON ctlt1 (b); |
||||
CREATE INDEX ctlt1_fnidx ON ctlt1 ((a || b)); |
||||
COMMENT ON COLUMN ctlt1.a IS 'A'; |
||||
COMMENT ON COLUMN ctlt1.b IS 'B'; |
||||
COMMENT ON CONSTRAINT ctlt1_a_check ON ctlt1 IS 't1_a_check'; |
||||
COMMENT ON INDEX ctlt1_pkey IS 'index pkey'; |
||||
COMMENT ON INDEX ctlt1_b_key IS 'index b_key'; |
||||
ALTER TABLE ctlt1 ALTER COLUMN a SET STORAGE MAIN; |
||||
|
||||
CREATE TABLE ctlt2 (c text); |
||||
ALTER TABLE ctlt2 ALTER COLUMN c SET STORAGE EXTERNAL; |
||||
COMMENT ON COLUMN ctlt2.c IS 'C'; |
||||
|
||||
CREATE TABLE ctlt3 (a text CHECK (length(a) < 5), c text); |
||||
ALTER TABLE ctlt3 ALTER COLUMN c SET STORAGE EXTERNAL; |
||||
ALTER TABLE ctlt3 ALTER COLUMN a SET STORAGE MAIN; |
||||
COMMENT ON COLUMN ctlt3.a IS 'A3'; |
||||
COMMENT ON COLUMN ctlt3.c IS 'C'; |
||||
COMMENT ON CONSTRAINT ctlt3_a_check ON ctlt3 IS 't3_a_check'; |
||||
|
||||
CREATE TABLE ctlt4 (a text, c text); |
||||
ALTER TABLE ctlt4 ALTER COLUMN c SET STORAGE EXTERNAL; |
||||
|
||||
CREATE TABLE ctlt12_storage (LIKE ctlt1 INCLUDING STORAGE, LIKE ctlt2 INCLUDING STORAGE); |
||||
\d+ ctlt12_storage |
||||
CREATE TABLE ctlt12_comments (LIKE ctlt1 INCLUDING COMMENTS, LIKE ctlt2 INCLUDING COMMENTS); |
||||
\d+ ctlt12_comments |
||||
CREATE TABLE ctlt1_inh (LIKE ctlt1 INCLUDING CONSTRAINTS INCLUDING COMMENTS) INHERITS (ctlt1); |
||||
\d+ ctlt1_inh |
||||
SELECT description FROM pg_description, pg_constraint c WHERE classoid = 'pg_constraint'::regclass AND objoid = c.oid AND c.conrelid = 'ctlt1_inh'::regclass; |
||||
CREATE TABLE ctlt13_inh () INHERITS (ctlt1, ctlt3); |
||||
\d+ ctlt13_inh |
||||
CREATE TABLE ctlt13_like (LIKE ctlt3 INCLUDING CONSTRAINTS INCLUDING COMMENTS INCLUDING STORAGE) INHERITS (ctlt1); |
||||
\d+ ctlt13_like |
||||
SELECT description FROM pg_description, pg_constraint c WHERE classoid = 'pg_constraint'::regclass AND objoid = c.oid AND c.conrelid = 'ctlt13_like'::regclass; |
||||
|
||||
CREATE TABLE ctlt_all (LIKE ctlt1 INCLUDING ALL); |
||||
\d+ ctlt_all |
||||
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 = 'ctlt_all'::regclass ORDER BY c.relname, objsubid; |
||||
|
||||
CREATE TABLE inh_error1 () INHERITS (ctlt1, ctlt4); |
||||
CREATE TABLE inh_error2 (LIKE ctlt4 INCLUDING STORAGE) INHERITS (ctlt1); |
||||
|
||||
DROP TABLE ctlt1, ctlt2, ctlt3, ctlt4, ctlt12_storage, ctlt12_comments, ctlt1_inh, ctlt13_inh, ctlt13_like, ctlt_all, ctla, ctlb CASCADE; |
Loading…
Reference in new issue