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 | |
336 Not-null constraints:
337 "ctlt12_storage_a_not_null" NOT NULL "a"
339 CREATE TABLE ctlt12_comments (LIKE ctlt1 INCLUDING COMMENTS, LIKE ctlt2 INCLUDING COMMENTS);
341 Table "public.ctlt12_comments"
342 Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
343 --------+------+-----------+----------+---------+----------+--------------+-------------
344 a | text | | not null | | extended | | A
345 b | text | | | | extended | | B
346 c | text | | | | extended | | C
347 Not-null constraints:
348 "ctlt12_comments_a_not_null" NOT NULL "a"
350 CREATE TABLE ctlt1_inh (LIKE ctlt1 INCLUDING CONSTRAINTS INCLUDING COMMENTS) INHERITS (ctlt1);
351 NOTICE: merging column "a" with inherited definition
352 NOTICE: merging column "b" with inherited definition
353 NOTICE: merging constraint "ctlt1_a_check" with inherited definition
355 Table "public.ctlt1_inh"
356 Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
357 --------+------+-----------+----------+---------+----------+--------------+-------------
358 a | text | | not null | | main | | A
359 b | text | | | | extended | | B
361 "ctlt1_a_check" CHECK (length(a) > 2)
362 Not-null constraints:
363 "ctlt1_inh_a_not_null" NOT NULL "a" (local, inherited)
366 SELECT description FROM pg_description, pg_constraint c WHERE classoid = 'pg_constraint'::regclass AND objoid = c.oid AND c.conrelid = 'ctlt1_inh'::regclass;
372 CREATE TABLE ctlt13_inh () INHERITS (ctlt1, ctlt3);
373 NOTICE: merging multiple inherited definitions of column "a"
375 Table "public.ctlt13_inh"
376 Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
377 --------+------+-----------+----------+---------+----------+--------------+-------------
378 a | text | | not null | | main | |
379 b | text | | | | extended | |
380 c | text | | | | external | |
382 "ctlt1_a_check" CHECK (length(a) > 2)
383 "ctlt3_a_check" CHECK (length(a) < 5)
384 "ctlt3_c_check" CHECK (length(c) < 7)
385 Not-null constraints:
386 "ctlt13_inh_a_not_null" NOT NULL "a" (inherited)
390 CREATE TABLE ctlt13_like (LIKE ctlt3 INCLUDING CONSTRAINTS INCLUDING INDEXES INCLUDING COMMENTS INCLUDING STORAGE) INHERITS (ctlt1);
391 NOTICE: merging column "a" with inherited definition
393 Table "public.ctlt13_like"
394 Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
395 --------+------+-----------+----------+---------+----------+--------------+-------------
396 a | text | | not null | | main | | A3
397 b | text | | | | extended | |
398 c | text | | | | external | | C
400 "ctlt13_like_expr_idx" btree ((a || c))
402 "ctlt1_a_check" CHECK (length(a) > 2)
403 "ctlt3_a_check" CHECK (length(a) < 5)
404 "ctlt3_c_check" CHECK (length(c) < 7)
405 Not-null constraints:
406 "ctlt13_like_a_not_null" NOT NULL "a" (inherited)
409 SELECT description FROM pg_description, pg_constraint c WHERE classoid = 'pg_constraint'::regclass AND objoid = c.oid AND c.conrelid = 'ctlt13_like'::regclass;
415 CREATE TABLE ctlt_all (LIKE ctlt1 INCLUDING ALL);
417 Table "public.ctlt_all"
418 Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
419 --------+------+-----------+----------+---------+----------+--------------+-------------
420 a | text | | not null | | main | | A
421 b | text | | | | extended | | B
423 "ctlt_all_pkey" PRIMARY KEY, btree (a)
424 "ctlt_all_b_idx" btree (b)
425 "ctlt_all_expr_idx" btree ((a || b))
427 "ctlt1_a_check" CHECK (length(a) > 2)
429 "public.ctlt_all_a_b_stat" ON a, b FROM ctlt_all
430 "public.ctlt_all_expr_stat" ON (a || b) FROM ctlt_all
432 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;
433 relname | objsubid | description
434 ----------------+----------+-------------
435 ctlt_all_b_idx | 0 | index b_key
436 ctlt_all_pkey | 0 | index pkey
439 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;
440 stxname | objsubid | description
441 --------------------+----------+---------------
442 ctlt_all_a_b_stat | 0 | ab stats
443 ctlt_all_expr_stat | 0 | ab expr stats
446 CREATE TABLE inh_error1 () INHERITS (ctlt1, ctlt4);
447 NOTICE: merging multiple inherited definitions of column "a"
448 ERROR: inherited column "a" has a storage parameter conflict
449 DETAIL: MAIN versus EXTENDED
450 CREATE TABLE inh_error2 (LIKE ctlt4 INCLUDING STORAGE) INHERITS (ctlt1);
451 NOTICE: merging column "a" with inherited definition
452 ERROR: column "a" has a storage parameter conflict
453 DETAIL: MAIN versus EXTENDED
454 -- Check that LIKE isn't confused by a system catalog of the same name
455 CREATE TABLE pg_attrdef (LIKE ctlt1 INCLUDING ALL);
456 \d+ public.pg_attrdef
457 Table "public.pg_attrdef"
458 Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
459 --------+------+-----------+----------+---------+----------+--------------+-------------
460 a | text | | not null | | main | | A
461 b | text | | | | extended | | B
463 "pg_attrdef_pkey" PRIMARY KEY, btree (a)
464 "pg_attrdef_b_idx" btree (b)
465 "pg_attrdef_expr_idx" btree ((a || b))
467 "ctlt1_a_check" CHECK (length(a) > 2)
469 "public.pg_attrdef_a_b_stat" ON a, b FROM public.pg_attrdef
470 "public.pg_attrdef_expr_stat" ON (a || b) FROM public.pg_attrdef
472 DROP TABLE public.pg_attrdef;
473 -- Check that LIKE isn't confused when new table masks the old, either
475 CREATE SCHEMA ctl_schema;
476 SET LOCAL search_path = ctl_schema, public;
477 CREATE TABLE ctlt1 (LIKE ctlt1 INCLUDING ALL);
479 Table "ctl_schema.ctlt1"
480 Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
481 --------+------+-----------+----------+---------+----------+--------------+-------------
482 a | text | | not null | | main | | A
483 b | text | | | | extended | | B
485 "ctlt1_pkey" PRIMARY KEY, btree (a)
486 "ctlt1_b_idx" btree (b)
487 "ctlt1_expr_idx" btree ((a || b))
489 "ctlt1_a_check" CHECK (length(a) > 2)
491 "ctl_schema.ctlt1_a_b_stat" ON a, b FROM ctlt1
492 "ctl_schema.ctlt1_expr_stat" ON (a || b) FROM ctlt1
495 DROP TABLE ctlt1, ctlt2, ctlt3, ctlt4, ctlt12_storage, ctlt12_comments, ctlt1_inh, ctlt13_inh, ctlt13_like, ctlt_all, ctla, ctlb CASCADE;
496 NOTICE: drop cascades to table inhe
497 -- LIKE must respect NO INHERIT property of constraints
498 CREATE TABLE noinh_con_copy (a int CHECK (a > 0) NO INHERIT);
499 CREATE TABLE noinh_con_copy1 (LIKE noinh_con_copy INCLUDING CONSTRAINTS);
501 Table "public.noinh_con_copy1"
502 Column | Type | Collation | Nullable | Default
503 --------+---------+-----------+----------+---------
506 "noinh_con_copy_a_check" CHECK (a > 0) NO INHERIT
508 -- fail, as partitioned tables don't allow NO INHERIT constraints
509 CREATE TABLE noinh_con_copy1_parted (LIKE noinh_con_copy INCLUDING ALL)
510 PARTITION BY LIST (a);
511 ERROR: cannot add NO INHERIT constraint to partitioned table "noinh_con_copy1_parted"
512 DROP TABLE noinh_con_copy, noinh_con_copy1;
513 /* LIKE with other relation kinds */
514 CREATE TABLE ctlt4 (a int, b text);
515 CREATE SEQUENCE ctlseq1;
516 CREATE TABLE ctlt10 (LIKE ctlseq1); -- fail
517 ERROR: relation "ctlseq1" is invalid in LIKE clause
518 LINE 1: CREATE TABLE ctlt10 (LIKE ctlseq1);
520 DETAIL: This operation is not supported for sequences.
521 CREATE VIEW ctlv1 AS SELECT * FROM ctlt4;
522 CREATE TABLE ctlt11 (LIKE ctlv1);
523 CREATE TABLE ctlt11a (LIKE ctlv1 INCLUDING ALL);
524 CREATE TYPE ctlty1 AS (a int, b text);
525 CREATE TABLE ctlt12 (LIKE ctlty1);
526 DROP SEQUENCE ctlseq1;
529 DROP TABLE IF EXISTS ctlt4, ctlt10, ctlt11, ctlt11a, ctlt12;
530 NOTICE: table "ctlt10" does not exist, skipping