2012-01-07 13:58:13 +01:00
|
|
|
/* 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);
|
|
|
|
|
2012-03-03 15:03:05 +01:00
|
|
|
CREATE TABLE foo (LIKE nonexistent);
|
|
|
|
|
2012-01-07 13:58:13 +01:00
|
|
|
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;
|
|
|
|
|
2018-03-07 20:38:35 +01:00
|
|
|
CREATE TABLE test_like_id_1 (a bigint GENERATED ALWAYS AS IDENTITY, b text);
|
2017-04-06 14:33:16 +02:00
|
|
|
\d test_like_id_1
|
|
|
|
INSERT INTO test_like_id_1 (b) VALUES ('b1');
|
|
|
|
SELECT * FROM test_like_id_1;
|
|
|
|
CREATE TABLE test_like_id_2 (LIKE test_like_id_1);
|
|
|
|
\d test_like_id_2
|
|
|
|
INSERT INTO test_like_id_2 (b) VALUES ('b2');
|
|
|
|
SELECT * FROM test_like_id_2; -- identity was not copied
|
|
|
|
CREATE TABLE test_like_id_3 (LIKE test_like_id_1 INCLUDING IDENTITY);
|
|
|
|
\d test_like_id_3
|
|
|
|
INSERT INTO test_like_id_3 (b) VALUES ('b3');
|
|
|
|
SELECT * FROM test_like_id_3; -- identity was copied and applied
|
|
|
|
DROP TABLE test_like_id_1, test_like_id_2, test_like_id_3;
|
|
|
|
|
2012-01-07 13:58:13 +01:00
|
|
|
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));
|
Clone extended stats in CREATE TABLE (LIKE INCLUDING ALL)
The LIKE INCLUDING ALL clause to CREATE TABLE intuitively indicates
cloning of extended statistics on the source table, but it failed to do
so. Patch it up so that it does. Also include an INCLUDING STATISTICS
option to the LIKE clause, so that the behavior can be requested
individually, or excluded individually.
While at it, reorder the INCLUDING options, both in code and in docs, in
alphabetical order which makes more sense than feature-implementation
order that was previously used.
Backpatch this to Postgres 10, where extended statistics were
introduced, because this is seen as an oversight in a fresh feature
which is better to get consistent from the get-go instead of changing
only in pg11.
In pg11, comments on statistics objects are cloned too. In pg10 they
are not, because I (Álvaro) was too coward to change the parse node as
required to support it. Also, in pg10 I chose not to renumber the
parser symbols for the various INCLUDING options in LIKE, for the same
reason. Any corresponding user-visible changes (docs) are backpatched,
though.
Reported-by: Stephen Froehlich
Author: David Rowley
Reviewed-by: Álvaro Herrera, Tomas Vondra
Discussion: https://postgr.es/m/CY1PR0601MB1927315B45667A1B679D0FD5E5EF0@CY1PR0601MB1927.namprd06.prod.outlook.com
2018-03-05 23:37:19 +01:00
|
|
|
CREATE STATISTICS ctlt1_a_b_stat ON a,b FROM ctlt1;
|
|
|
|
COMMENT ON STATISTICS ctlt1_a_b_stat IS 'ab stats';
|
2012-01-07 13:58:13 +01:00
|
|
|
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;
|
Clone extended stats in CREATE TABLE (LIKE INCLUDING ALL)
The LIKE INCLUDING ALL clause to CREATE TABLE intuitively indicates
cloning of extended statistics on the source table, but it failed to do
so. Patch it up so that it does. Also include an INCLUDING STATISTICS
option to the LIKE clause, so that the behavior can be requested
individually, or excluded individually.
While at it, reorder the INCLUDING options, both in code and in docs, in
alphabetical order which makes more sense than feature-implementation
order that was previously used.
Backpatch this to Postgres 10, where extended statistics were
introduced, because this is seen as an oversight in a fresh feature
which is better to get consistent from the get-go instead of changing
only in pg11.
In pg11, comments on statistics objects are cloned too. In pg10 they
are not, because I (Álvaro) was too coward to change the parse node as
required to support it. Also, in pg10 I chose not to renumber the
parser symbols for the various INCLUDING options in LIKE, for the same
reason. Any corresponding user-visible changes (docs) are backpatched,
though.
Reported-by: Stephen Froehlich
Author: David Rowley
Reviewed-by: Álvaro Herrera, Tomas Vondra
Discussion: https://postgr.es/m/CY1PR0601MB1927315B45667A1B679D0FD5E5EF0@CY1PR0601MB1927.namprd06.prod.outlook.com
2018-03-05 23:37:19 +01:00
|
|
|
SELECT s.stxname, objsubid, description FROM pg_description, pg_statistic_ext s WHERE classoid = 'pg_statistic_ext'::regclass AND objoid = s.oid AND s.stxrelid = 'ctlt_all'::regclass ORDER BY s.stxname, objsubid;
|
2012-01-07 13:58:13 +01:00
|
|
|
|
|
|
|
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;
|
2012-01-10 20:46:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* LIKE with other relation kinds */
|
|
|
|
|
|
|
|
CREATE TABLE ctlt4 (a int, b text);
|
|
|
|
|
|
|
|
CREATE SEQUENCE ctlseq1;
|
|
|
|
CREATE TABLE ctlt10 (LIKE ctlseq1); -- fail
|
|
|
|
|
|
|
|
CREATE VIEW ctlv1 AS SELECT * FROM ctlt4;
|
|
|
|
CREATE TABLE ctlt11 (LIKE ctlv1);
|
|
|
|
CREATE TABLE ctlt11a (LIKE ctlv1 INCLUDING ALL);
|
|
|
|
|
|
|
|
CREATE TYPE ctlty1 AS (a int, b text);
|
2012-03-03 15:03:05 +01:00
|
|
|
CREATE TABLE ctlt12 (LIKE ctlty1);
|
2012-01-10 20:46:29 +01:00
|
|
|
|
|
|
|
DROP SEQUENCE ctlseq1;
|
|
|
|
DROP TYPE ctlty1;
|
|
|
|
DROP VIEW ctlv1;
|
|
|
|
DROP TABLE IF EXISTS ctlt4, ctlt10, ctlt11, ctlt11a, ctlt12;
|