1 /* Test inheritance of structure (LIKE) */
2 CREATE TABLE inhx (xx text DEFAULT 'text');
4 * Test double inheritance
6 * Ensure that defaults are NOT included unless
7 * INCLUDING DEFAULTS is specified
9 CREATE TABLE ctla (aa TEXT);
10 CREATE TABLE ctlb (bb TEXT) INHERITS (ctla);
11 CREATE TABLE foo (LIKE nonexistent);
12 ERROR: relation "nonexistent" does not exist
13 LINE 1: CREATE TABLE foo (LIKE nonexistent);
15 CREATE TABLE inhe (ee text, LIKE inhx) inherits (ctlb);
16 INSERT INTO inhe VALUES ('ee-col1', 'ee-col2', DEFAULT, 'ee-col4');
17 SELECT * FROM inhe; /* Columns aa, bb, xx value NULL, ee */
19 ---------+---------+----+---------
20 ee-col1 | ee-col2 | | ee-col4
23 SELECT * FROM inhx; /* Empty set since LIKE inherits structure only */
28 SELECT * FROM ctlb; /* Has ee entry */
34 SELECT * FROM ctla; /* Has ee entry */
40 CREATE TABLE inhf (LIKE inhx, LIKE inhx); /* Throw error */
41 ERROR: column "xx" specified more than once
42 CREATE TABLE inhf (LIKE inhx INCLUDING DEFAULTS INCLUDING CONSTRAINTS);
43 INSERT INTO inhf DEFAULT VALUES;
44 SELECT * FROM inhf; /* Single entry with value 'text' */
50 ALTER TABLE inhx add constraint foo CHECK (xx = 'text');
51 ALTER TABLE inhx ADD PRIMARY KEY (xx);
52 CREATE TABLE inhg (LIKE inhx); /* Doesn't copy constraint */
53 INSERT INTO inhg VALUES ('foo');
55 CREATE TABLE inhg (x text, LIKE inhx INCLUDING CONSTRAINTS, y text); /* Copies constraints */
56 INSERT INTO inhg VALUES ('x', 'text', 'y'); /* Succeeds */
57 INSERT INTO inhg VALUES ('x', 'text', 'y'); /* Succeeds -- Unique constraints not copied */
58 INSERT INTO inhg VALUES ('x', 'foo', 'y'); /* fails due to constraint */
59 ERROR: new row for relation "inhg" violates check constraint "foo"
60 DETAIL: Failing row contains (x, foo, y).
61 SELECT * FROM inhg; /* Two records with three columns in order x=x, xx=text, y=y */
69 CREATE TABLE test_like_id_1 (a bigint GENERATED ALWAYS AS IDENTITY, b text);
71 Table "public.test_like_id_1"
72 Column | Type | Collation | Nullable | Default
73 --------+--------+-----------+----------+------------------------------
74 a | bigint | | not null | generated always as identity
77 INSERT INTO test_like_id_1 (b) VALUES ('b1');
78 SELECT * FROM test_like_id_1;
84 CREATE TABLE test_like_id_2 (LIKE test_like_id_1);
86 Table "public.test_like_id_2"
87 Column | Type | Collation | Nullable | Default
88 --------+--------+-----------+----------+---------
89 a | bigint | | not null |
92 INSERT INTO test_like_id_2 (b) VALUES ('b2');
93 ERROR: null value in column "a" of relation "test_like_id_2" violates not-null constraint
94 DETAIL: Failing row contains (null, b2).
95 SELECT * FROM test_like_id_2; -- identity was not copied
100 CREATE TABLE test_like_id_3 (LIKE test_like_id_1 INCLUDING IDENTITY);
102 Table "public.test_like_id_3"
103 Column | Type | Collation | Nullable | Default
104 --------+--------+-----------+----------+------------------------------
105 a | bigint | | not null | generated always as identity
108 INSERT INTO test_like_id_3 (b) VALUES ('b3');
109 SELECT * FROM test_like_id_3; -- identity was copied and applied
115 DROP TABLE test_like_id_1, test_like_id_2, test_like_id_3;
116 CREATE TABLE test_like_gen_1 (a int, b int GENERATED ALWAYS AS (a * 2) STORED);
118 Table "public.test_like_gen_1"
119 Column | Type | Collation | Nullable | Default
120 --------+---------+-----------+----------+------------------------------------
122 b | integer | | | generated always as (a * 2) stored
124 INSERT INTO test_like_gen_1 (a) VALUES (1);
125 SELECT * FROM test_like_gen_1;
131 CREATE TABLE test_like_gen_2 (LIKE test_like_gen_1);
133 Table "public.test_like_gen_2"
134 Column | Type | Collation | Nullable | Default
135 --------+---------+-----------+----------+---------
139 INSERT INTO test_like_gen_2 (a) VALUES (1);
140 SELECT * FROM test_like_gen_2;
146 CREATE TABLE test_like_gen_3 (LIKE test_like_gen_1 INCLUDING GENERATED);
148 Table "public.test_like_gen_3"
149 Column | Type | Collation | Nullable | Default
150 --------+---------+-----------+----------+------------------------------------
152 b | integer | | | generated always as (a * 2) stored
154 INSERT INTO test_like_gen_3 (a) VALUES (1);
155 SELECT * FROM test_like_gen_3;
161 DROP TABLE test_like_gen_1, test_like_gen_2, test_like_gen_3;
162 -- also test generated column with a "forward" reference (bug #16342)
163 CREATE TABLE test_like_4 (b int DEFAULT 42,
164 c int GENERATED ALWAYS AS (a * 2) STORED,
165 a int CHECK (a > 0));
167 Table "public.test_like_4"
168 Column | Type | Collation | Nullable | Default
169 --------+---------+-----------+----------+------------------------------------
171 c | integer | | | generated always as (a * 2) stored
174 "test_like_4_a_check" CHECK (a > 0)
176 CREATE TABLE test_like_4a (LIKE test_like_4);
177 CREATE TABLE test_like_4b (LIKE test_like_4 INCLUDING DEFAULTS);
178 CREATE TABLE test_like_4c (LIKE test_like_4 INCLUDING GENERATED);
179 CREATE TABLE test_like_4d (LIKE test_like_4 INCLUDING DEFAULTS INCLUDING GENERATED);
181 Table "public.test_like_4a"
182 Column | Type | Collation | Nullable | Default
183 --------+---------+-----------+----------+---------
188 INSERT INTO test_like_4a (a) VALUES(11);
189 SELECT a, b, c FROM test_like_4a;
196 Table "public.test_like_4b"
197 Column | Type | Collation | Nullable | Default
198 --------+---------+-----------+----------+---------
203 INSERT INTO test_like_4b (a) VALUES(11);
204 SELECT a, b, c FROM test_like_4b;
211 Table "public.test_like_4c"
212 Column | Type | Collation | Nullable | Default
213 --------+---------+-----------+----------+------------------------------------
215 c | integer | | | generated always as (a * 2) stored
218 INSERT INTO test_like_4c (a) VALUES(11);
219 SELECT a, b, c FROM test_like_4c;
226 Table "public.test_like_4d"
227 Column | Type | Collation | Nullable | Default
228 --------+---------+-----------+----------+------------------------------------
230 c | integer | | | generated always as (a * 2) stored
233 INSERT INTO test_like_4d (a) VALUES(11);
234 SELECT a, b, c FROM test_like_4d;
240 -- Test renumbering of Vars when combining LIKE with inheritance
241 CREATE TABLE test_like_5 (x point, y point, z point);
242 CREATE TABLE test_like_5x (p int CHECK (p > 0),
243 q int GENERATED ALWAYS AS (p * 2) STORED);
244 CREATE TABLE test_like_5c (LIKE test_like_4 INCLUDING ALL)
245 INHERITS (test_like_5, test_like_5x);
247 Table "public.test_like_5c"
248 Column | Type | Collation | Nullable | Default
249 --------+---------+-----------+----------+------------------------------------
254 q | integer | | | generated always as (p * 2) stored
256 c | integer | | | generated always as (a * 2) stored
259 "test_like_4_a_check" CHECK (a > 0)
260 "test_like_5x_p_check" CHECK (p > 0)
261 Inherits: test_like_5,
264 DROP TABLE test_like_4, test_like_4a, test_like_4b, test_like_4c, test_like_4d;
265 DROP TABLE test_like_5, test_like_5x, test_like_5c;
266 CREATE TABLE inhg (x text, LIKE inhx INCLUDING INDEXES, y text); /* copies indexes */
267 INSERT INTO inhg VALUES (5, 10);
268 INSERT INTO inhg VALUES (20, 10); -- should fail
269 ERROR: duplicate key value violates unique constraint "inhg_pkey"
270 DETAIL: Key (xx)=(10) already exists.
272 /* Multiple primary keys creation should fail */
273 CREATE TABLE inhg (x text, LIKE inhx INCLUDING INDEXES, PRIMARY KEY(x)); /* fails */
274 ERROR: multiple primary keys for table "inhg" are not allowed
275 CREATE TABLE inhz (xx text DEFAULT 'text', yy int UNIQUE);
276 CREATE UNIQUE INDEX inhz_xx_idx on inhz (xx) WHERE xx <> 'test';
277 /* Ok to create multiple unique indexes */
278 CREATE TABLE inhg (x text UNIQUE, LIKE inhz INCLUDING INDEXES);
279 INSERT INTO inhg (xx, yy, x) VALUES ('test', 5, 10);
280 INSERT INTO inhg (xx, yy, x) VALUES ('test', 10, 15);
281 INSERT INTO inhg (xx, yy, x) VALUES ('foo', 10, 15); -- should fail
282 ERROR: duplicate key value violates unique constraint "inhg_x_key"
283 DETAIL: Key (x)=(15) already exists.
286 /* Use primary key imported by LIKE for self-referential FK constraint */
287 CREATE TABLE inhz (x text REFERENCES inhz, LIKE inhx INCLUDING INDEXES);
290 Column | Type | Collation | Nullable | Default
291 --------+------+-----------+----------+---------
293 xx | text | | not null |
295 "inhz_pkey" PRIMARY KEY, btree (xx)
296 Foreign-key constraints:
297 "inhz_x_fkey" FOREIGN KEY (x) REFERENCES inhz(xx)
299 TABLE "inhz" CONSTRAINT "inhz_x_fkey" FOREIGN KEY (x) REFERENCES inhz(xx)
302 -- including storage and comments
303 CREATE TABLE ctlt1 (a text CHECK (length(a) > 2) PRIMARY KEY, b text);
304 CREATE INDEX ctlt1_b_key ON ctlt1 (b);
305 CREATE INDEX ctlt1_fnidx ON ctlt1 ((a || b));
306 CREATE STATISTICS ctlt1_a_b_stat ON a,b FROM ctlt1;
307 CREATE STATISTICS ctlt1_expr_stat ON (a || b) FROM ctlt1;
308 COMMENT ON STATISTICS ctlt1_a_b_stat IS 'ab stats';
309 COMMENT ON STATISTICS ctlt1_expr_stat IS 'ab expr stats';
310 COMMENT ON COLUMN ctlt1.a IS 'A';
311 COMMENT ON COLUMN ctlt1.b IS 'B';
312 COMMENT ON CONSTRAINT ctlt1_a_check ON ctlt1 IS 't1_a_check';
313 COMMENT ON INDEX ctlt1_pkey IS 'index pkey';
314 COMMENT ON INDEX ctlt1_b_key IS 'index b_key';
315 ALTER TABLE ctlt1 ALTER COLUMN a SET STORAGE MAIN;
316 CREATE TABLE ctlt2 (c text);
317 ALTER TABLE ctlt2 ALTER COLUMN c SET STORAGE EXTERNAL;
318 COMMENT ON COLUMN ctlt2.c IS 'C';
319 CREATE TABLE ctlt3 (a text CHECK (length(a) < 5), c text CHECK (length(c) < 7));
320 ALTER TABLE ctlt3 ALTER COLUMN c SET STORAGE EXTERNAL;
321 ALTER TABLE ctlt3 ALTER COLUMN a SET STORAGE MAIN;
322 CREATE INDEX ctlt3_fnidx ON ctlt3 ((a || c));
323 COMMENT ON COLUMN ctlt3.a IS 'A3';
324 COMMENT ON COLUMN ctlt3.c IS 'C';
325 COMMENT ON CONSTRAINT ctlt3_a_check ON ctlt3 IS 't3_a_check';
326 CREATE TABLE ctlt4 (a text, c text);
327 ALTER TABLE ctlt4 ALTER COLUMN c SET STORAGE EXTERNAL;
328 CREATE TABLE ctlt12_storage (LIKE ctlt1 INCLUDING STORAGE, LIKE ctlt2 INCLUDING STORAGE);
330 Table "public.ctlt12_storage"
331 Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
332 --------+------+-----------+----------+---------+----------+--------------+-------------
333 a | text | | not null | | main | |
334 b | text | | | | extended | |
335 c | text | | | | external | |
337 CREATE TABLE ctlt12_comments (LIKE ctlt1 INCLUDING COMMENTS, LIKE ctlt2 INCLUDING COMMENTS);
339 Table "public.ctlt12_comments"
340 Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
341 --------+------+-----------+----------+---------+----------+--------------+-------------
342 a | text | | not null | | extended | | A
343 b | text | | | | extended | | B
344 c | text | | | | extended | | C
346 CREATE TABLE ctlt1_inh (LIKE ctlt1 INCLUDING CONSTRAINTS INCLUDING COMMENTS) INHERITS (ctlt1);
347 NOTICE: merging column "a" with inherited definition
348 NOTICE: merging column "b" with inherited definition
349 NOTICE: merging constraint "ctlt1_a_check" with inherited definition
351 Table "public.ctlt1_inh"
352 Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
353 --------+------+-----------+----------+---------+----------+--------------+-------------
354 a | text | | not null | | main | | A
355 b | text | | | | extended | | B
357 "ctlt1_a_check" CHECK (length(a) > 2)
360 SELECT description FROM pg_description, pg_constraint c WHERE classoid = 'pg_constraint'::regclass AND objoid = c.oid AND c.conrelid = 'ctlt1_inh'::regclass;
366 CREATE TABLE ctlt13_inh () INHERITS (ctlt1, ctlt3);
367 NOTICE: merging multiple inherited definitions of column "a"
369 Table "public.ctlt13_inh"
370 Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
371 --------+------+-----------+----------+---------+----------+--------------+-------------
372 a | text | | not null | | main | |
373 b | text | | | | extended | |
374 c | text | | | | external | |
376 "ctlt1_a_check" CHECK (length(a) > 2)
377 "ctlt3_a_check" CHECK (length(a) < 5)
378 "ctlt3_c_check" CHECK (length(c) < 7)
382 CREATE TABLE ctlt13_like (LIKE ctlt3 INCLUDING CONSTRAINTS INCLUDING INDEXES INCLUDING COMMENTS INCLUDING STORAGE) INHERITS (ctlt1);
383 NOTICE: merging column "a" with inherited definition
385 Table "public.ctlt13_like"
386 Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
387 --------+------+-----------+----------+---------+----------+--------------+-------------
388 a | text | | not null | | main | | A3
389 b | text | | | | extended | |
390 c | text | | | | external | | C
392 "ctlt13_like_expr_idx" btree ((a || c))
394 "ctlt1_a_check" CHECK (length(a) > 2)
395 "ctlt3_a_check" CHECK (length(a) < 5)
396 "ctlt3_c_check" CHECK (length(c) < 7)
399 SELECT description FROM pg_description, pg_constraint c WHERE classoid = 'pg_constraint'::regclass AND objoid = c.oid AND c.conrelid = 'ctlt13_like'::regclass;
405 CREATE TABLE ctlt_all (LIKE ctlt1 INCLUDING ALL);
407 Table "public.ctlt_all"
408 Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
409 --------+------+-----------+----------+---------+----------+--------------+-------------
410 a | text | | not null | | main | | A
411 b | text | | | | extended | | B
413 "ctlt_all_pkey" PRIMARY KEY, btree (a)
414 "ctlt_all_b_idx" btree (b)
415 "ctlt_all_expr_idx" btree ((a || b))
417 "ctlt1_a_check" CHECK (length(a) > 2)
419 "public.ctlt_all_a_b_stat" ON a, b FROM ctlt_all
420 "public.ctlt_all_expr_stat" ON (a || b) FROM ctlt_all
422 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;
423 relname | objsubid | description
424 ----------------+----------+-------------
425 ctlt_all_b_idx | 0 | index b_key
426 ctlt_all_pkey | 0 | index pkey
429 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;
430 stxname | objsubid | description
431 --------------------+----------+---------------
432 ctlt_all_a_b_stat | 0 | ab stats
433 ctlt_all_expr_stat | 0 | ab expr stats
436 CREATE TABLE inh_error1 () INHERITS (ctlt1, ctlt4);
437 NOTICE: merging multiple inherited definitions of column "a"
438 ERROR: inherited column "a" has a storage parameter conflict
439 DETAIL: MAIN versus EXTENDED
440 CREATE TABLE inh_error2 (LIKE ctlt4 INCLUDING STORAGE) INHERITS (ctlt1);
441 NOTICE: merging column "a" with inherited definition
442 ERROR: column "a" has a storage parameter conflict
443 DETAIL: MAIN versus EXTENDED
444 -- Check that LIKE isn't confused by a system catalog of the same name
445 CREATE TABLE pg_attrdef (LIKE ctlt1 INCLUDING ALL);
446 \d+ public.pg_attrdef
447 Table "public.pg_attrdef"
448 Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
449 --------+------+-----------+----------+---------+----------+--------------+-------------
450 a | text | | not null | | main | | A
451 b | text | | | | extended | | B
453 "pg_attrdef_pkey" PRIMARY KEY, btree (a)
454 "pg_attrdef_b_idx" btree (b)
455 "pg_attrdef_expr_idx" btree ((a || b))
457 "ctlt1_a_check" CHECK (length(a) > 2)
459 "public.pg_attrdef_a_b_stat" ON a, b FROM public.pg_attrdef
460 "public.pg_attrdef_expr_stat" ON (a || b) FROM public.pg_attrdef
462 DROP TABLE public.pg_attrdef;
463 -- Check that LIKE isn't confused when new table masks the old, either
465 CREATE SCHEMA ctl_schema;
466 SET LOCAL search_path = ctl_schema, public;
467 CREATE TABLE ctlt1 (LIKE ctlt1 INCLUDING ALL);
469 Table "ctl_schema.ctlt1"
470 Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
471 --------+------+-----------+----------+---------+----------+--------------+-------------
472 a | text | | not null | | main | | A
473 b | text | | | | extended | | B
475 "ctlt1_pkey" PRIMARY KEY, btree (a)
476 "ctlt1_b_idx" btree (b)
477 "ctlt1_expr_idx" btree ((a || b))
479 "ctlt1_a_check" CHECK (length(a) > 2)
481 "ctl_schema.ctlt1_a_b_stat" ON a, b FROM ctlt1
482 "ctl_schema.ctlt1_expr_stat" ON (a || b) FROM ctlt1
485 DROP TABLE ctlt1, ctlt2, ctlt3, ctlt4, ctlt12_storage, ctlt12_comments, ctlt1_inh, ctlt13_inh, ctlt13_like, ctlt_all, ctla, ctlb CASCADE;
486 NOTICE: drop cascades to table inhe
487 -- LIKE must respect NO INHERIT property of constraints
488 CREATE TABLE noinh_con_copy (a int CHECK (a > 0) NO INHERIT);
489 CREATE TABLE noinh_con_copy1 (LIKE noinh_con_copy INCLUDING CONSTRAINTS);
491 Table "public.noinh_con_copy1"
492 Column | Type | Collation | Nullable | Default
493 --------+---------+-----------+----------+---------
496 "noinh_con_copy_a_check" CHECK (a > 0) NO INHERIT
498 -- fail, as partitioned tables don't allow NO INHERIT constraints
499 CREATE TABLE noinh_con_copy1_parted (LIKE noinh_con_copy INCLUDING ALL)
500 PARTITION BY LIST (a);
501 ERROR: cannot add NO INHERIT constraint to partitioned table "noinh_con_copy1_parted"
502 DROP TABLE noinh_con_copy, noinh_con_copy1;
503 /* LIKE with other relation kinds */
504 CREATE TABLE ctlt4 (a int, b text);
505 CREATE SEQUENCE ctlseq1;
506 CREATE TABLE ctlt10 (LIKE ctlseq1); -- fail
507 ERROR: relation "ctlseq1" is invalid in LIKE clause
508 LINE 1: CREATE TABLE ctlt10 (LIKE ctlseq1);
510 DETAIL: This operation is not supported for sequences.
511 CREATE VIEW ctlv1 AS SELECT * FROM ctlt4;
512 CREATE TABLE ctlt11 (LIKE ctlv1);
513 CREATE TABLE ctlt11a (LIKE ctlv1 INCLUDING ALL);
514 CREATE TYPE ctlty1 AS (a int, b text);
515 CREATE TABLE ctlt12 (LIKE ctlty1);
516 DROP SEQUENCE ctlseq1;
519 DROP TABLE IF EXISTS ctlt4, ctlt10, ctlt11, ctlt11a, ctlt12;
520 NOTICE: table "ctlt10" does not exist, skipping