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 -- Test updating of column numbers in statistics expressions (bug #18468)
265 CREATE TABLE test_like_6 (a int, c text, b text);
266 CREATE STATISTICS ext_stat ON (a || b) FROM test_like_6;
267 ALTER TABLE test_like_6 DROP COLUMN c;
268 CREATE TABLE test_like_6c (LIKE test_like_6 INCLUDING ALL);
270 Table "public.test_like_6c"
271 Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
272 --------+---------+-----------+----------+---------+----------+--------------+-------------
273 a | integer | | | | plain | |
274 b | text | | | | extended | |
276 "public.test_like_6c_expr_stat" ON (a || b) FROM test_like_6c
278 DROP TABLE test_like_4, test_like_4a, test_like_4b, test_like_4c, test_like_4d;
279 DROP TABLE test_like_5, test_like_5x, test_like_5c;
280 DROP TABLE test_like_6, test_like_6c;
281 CREATE TABLE inhg (x text, LIKE inhx INCLUDING INDEXES, y text); /* copies indexes */
282 INSERT INTO inhg VALUES (5, 10);
283 INSERT INTO inhg VALUES (20, 10); -- should fail
284 ERROR: duplicate key value violates unique constraint "inhg_pkey"
285 DETAIL: Key (xx)=(10) already exists.
287 /* Multiple primary keys creation should fail */
288 CREATE TABLE inhg (x text, LIKE inhx INCLUDING INDEXES, PRIMARY KEY(x)); /* fails */
289 ERROR: multiple primary keys for table "inhg" are not allowed
290 CREATE TABLE inhz (xx text DEFAULT 'text', yy int UNIQUE);
291 CREATE UNIQUE INDEX inhz_xx_idx on inhz (xx) WHERE xx <> 'test';
292 /* Ok to create multiple unique indexes */
293 CREATE TABLE inhg (x text UNIQUE, LIKE inhz INCLUDING INDEXES);
294 INSERT INTO inhg (xx, yy, x) VALUES ('test', 5, 10);
295 INSERT INTO inhg (xx, yy, x) VALUES ('test', 10, 15);
296 INSERT INTO inhg (xx, yy, x) VALUES ('foo', 10, 15); -- should fail
297 ERROR: duplicate key value violates unique constraint "inhg_x_key"
298 DETAIL: Key (x)=(15) already exists.
301 /* Use primary key imported by LIKE for self-referential FK constraint */
302 CREATE TABLE inhz (x text REFERENCES inhz, LIKE inhx INCLUDING INDEXES);
305 Column | Type | Collation | Nullable | Default
306 --------+------+-----------+----------+---------
308 xx | text | | not null |
310 "inhz_pkey" PRIMARY KEY, btree (xx)
311 Foreign-key constraints:
312 "inhz_x_fkey" FOREIGN KEY (x) REFERENCES inhz(xx)
314 TABLE "inhz" CONSTRAINT "inhz_x_fkey" FOREIGN KEY (x) REFERENCES inhz(xx)
317 -- including storage and comments
318 CREATE TABLE ctlt1 (a text CHECK (length(a) > 2) PRIMARY KEY, b text);
319 CREATE INDEX ctlt1_b_key ON ctlt1 (b);
320 CREATE INDEX ctlt1_fnidx ON ctlt1 ((a || b));
321 CREATE STATISTICS ctlt1_a_b_stat ON a,b FROM ctlt1;
322 CREATE STATISTICS ctlt1_expr_stat ON (a || b) FROM ctlt1;
323 COMMENT ON STATISTICS ctlt1_a_b_stat IS 'ab stats';
324 COMMENT ON STATISTICS ctlt1_expr_stat IS 'ab expr stats';
325 COMMENT ON COLUMN ctlt1.a IS 'A';
326 COMMENT ON COLUMN ctlt1.b IS 'B';
327 COMMENT ON CONSTRAINT ctlt1_a_check ON ctlt1 IS 't1_a_check';
328 COMMENT ON INDEX ctlt1_pkey IS 'index pkey';
329 COMMENT ON INDEX ctlt1_b_key IS 'index b_key';
330 ALTER TABLE ctlt1 ALTER COLUMN a SET STORAGE MAIN;
331 CREATE TABLE ctlt2 (c text);
332 ALTER TABLE ctlt2 ALTER COLUMN c SET STORAGE EXTERNAL;
333 COMMENT ON COLUMN ctlt2.c IS 'C';
334 CREATE TABLE ctlt3 (a text CHECK (length(a) < 5), c text CHECK (length(c) < 7));
335 ALTER TABLE ctlt3 ALTER COLUMN c SET STORAGE EXTERNAL;
336 ALTER TABLE ctlt3 ALTER COLUMN a SET STORAGE MAIN;
337 CREATE INDEX ctlt3_fnidx ON ctlt3 ((a || c));
338 COMMENT ON COLUMN ctlt3.a IS 'A3';
339 COMMENT ON COLUMN ctlt3.c IS 'C';
340 COMMENT ON CONSTRAINT ctlt3_a_check ON ctlt3 IS 't3_a_check';
341 CREATE TABLE ctlt4 (a text, c text);
342 ALTER TABLE ctlt4 ALTER COLUMN c SET STORAGE EXTERNAL;
343 CREATE TABLE ctlt12_storage (LIKE ctlt1 INCLUDING STORAGE, LIKE ctlt2 INCLUDING STORAGE);
345 Table "public.ctlt12_storage"
346 Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
347 --------+------+-----------+----------+---------+----------+--------------+-------------
348 a | text | | not null | | main | |
349 b | text | | | | extended | |
350 c | text | | | | external | |
351 Not-null constraints:
352 "ctlt1_a_not_null" NOT NULL "a"
354 CREATE TABLE ctlt12_comments (LIKE ctlt1 INCLUDING COMMENTS, LIKE ctlt2 INCLUDING COMMENTS);
356 Table "public.ctlt12_comments"
357 Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
358 --------+------+-----------+----------+---------+----------+--------------+-------------
359 a | text | | not null | | extended | | A
360 b | text | | | | extended | | B
361 c | text | | | | extended | | C
362 Not-null constraints:
363 "ctlt1_a_not_null" NOT NULL "a"
365 CREATE TABLE ctlt1_inh (LIKE ctlt1 INCLUDING CONSTRAINTS INCLUDING COMMENTS) INHERITS (ctlt1);
366 NOTICE: merging column "a" with inherited definition
367 NOTICE: merging column "b" with inherited definition
368 NOTICE: merging constraint "ctlt1_a_check" with inherited definition
370 Table "public.ctlt1_inh"
371 Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
372 --------+------+-----------+----------+---------+----------+--------------+-------------
373 a | text | | not null | | main | | A
374 b | text | | | | extended | | B
376 "ctlt1_a_check" CHECK (length(a) > 2)
377 Not-null constraints:
378 "ctlt1_a_not_null" NOT NULL "a" (local, inherited)
381 SELECT description FROM pg_description, pg_constraint c WHERE classoid = 'pg_constraint'::regclass AND objoid = c.oid AND c.conrelid = 'ctlt1_inh'::regclass;
387 CREATE TABLE ctlt13_inh () INHERITS (ctlt1, ctlt3);
388 NOTICE: merging multiple inherited definitions of column "a"
390 Table "public.ctlt13_inh"
391 Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
392 --------+------+-----------+----------+---------+----------+--------------+-------------
393 a | text | | not null | | main | |
394 b | text | | | | extended | |
395 c | text | | | | external | |
397 "ctlt1_a_check" CHECK (length(a) > 2)
398 "ctlt3_a_check" CHECK (length(a) < 5)
399 "ctlt3_c_check" CHECK (length(c) < 7)
400 Not-null constraints:
401 "ctlt1_a_not_null" NOT NULL "a" (inherited)
405 CREATE TABLE ctlt13_like (LIKE ctlt3 INCLUDING CONSTRAINTS INCLUDING INDEXES INCLUDING COMMENTS INCLUDING STORAGE) INHERITS (ctlt1);
406 NOTICE: merging column "a" with inherited definition
408 Table "public.ctlt13_like"
409 Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
410 --------+------+-----------+----------+---------+----------+--------------+-------------
411 a | text | | not null | | main | | A3
412 b | text | | | | extended | |
413 c | text | | | | external | | C
415 "ctlt13_like_expr_idx" btree ((a || c))
417 "ctlt1_a_check" CHECK (length(a) > 2)
418 "ctlt3_a_check" CHECK (length(a) < 5)
419 "ctlt3_c_check" CHECK (length(c) < 7)
420 Not-null constraints:
421 "ctlt1_a_not_null" NOT NULL "a" (inherited)
424 SELECT description FROM pg_description, pg_constraint c WHERE classoid = 'pg_constraint'::regclass AND objoid = c.oid AND c.conrelid = 'ctlt13_like'::regclass;
430 CREATE TABLE ctlt_all (LIKE ctlt1 INCLUDING ALL);
432 Table "public.ctlt_all"
433 Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
434 --------+------+-----------+----------+---------+----------+--------------+-------------
435 a | text | | not null | | main | | A
436 b | text | | | | extended | | B
438 "ctlt_all_pkey" PRIMARY KEY, btree (a)
439 "ctlt_all_b_idx" btree (b)
440 "ctlt_all_expr_idx" btree ((a || b))
442 "ctlt1_a_check" CHECK (length(a) > 2)
444 "public.ctlt_all_a_b_stat" ON a, b FROM ctlt_all
445 "public.ctlt_all_expr_stat" ON (a || b) FROM ctlt_all
446 Not-null constraints:
447 "ctlt1_a_not_null" NOT NULL "a"
449 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;
450 relname | objsubid | description
451 ----------------+----------+-------------
452 ctlt_all_b_idx | 0 | index b_key
453 ctlt_all_pkey | 0 | index pkey
456 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;
457 stxname | objsubid | description
458 --------------------+----------+---------------
459 ctlt_all_a_b_stat | 0 | ab stats
460 ctlt_all_expr_stat | 0 | ab expr stats
463 CREATE TABLE inh_error1 () INHERITS (ctlt1, ctlt4);
464 NOTICE: merging multiple inherited definitions of column "a"
465 ERROR: inherited column "a" has a storage parameter conflict
466 DETAIL: MAIN versus EXTENDED
467 CREATE TABLE inh_error2 (LIKE ctlt4 INCLUDING STORAGE) INHERITS (ctlt1);
468 NOTICE: merging column "a" with inherited definition
469 ERROR: column "a" has a storage parameter conflict
470 DETAIL: MAIN versus EXTENDED
471 -- Check that LIKE isn't confused by a system catalog of the same name
472 CREATE TABLE pg_attrdef (LIKE ctlt1 INCLUDING ALL);
473 \d+ public.pg_attrdef
474 Table "public.pg_attrdef"
475 Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
476 --------+------+-----------+----------+---------+----------+--------------+-------------
477 a | text | | not null | | main | | A
478 b | text | | | | extended | | B
480 "pg_attrdef_pkey" PRIMARY KEY, btree (a)
481 "pg_attrdef_b_idx" btree (b)
482 "pg_attrdef_expr_idx" btree ((a || b))
484 "ctlt1_a_check" CHECK (length(a) > 2)
486 "public.pg_attrdef_a_b_stat" ON a, b FROM public.pg_attrdef
487 "public.pg_attrdef_expr_stat" ON (a || b) FROM public.pg_attrdef
488 Not-null constraints:
489 "ctlt1_a_not_null" NOT NULL "a"
491 DROP TABLE public.pg_attrdef;
492 -- Check that LIKE isn't confused when new table masks the old, either
494 CREATE SCHEMA ctl_schema;
495 SET LOCAL search_path = ctl_schema, public;
496 CREATE TABLE ctlt1 (LIKE ctlt1 INCLUDING ALL);
498 Table "ctl_schema.ctlt1"
499 Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
500 --------+------+-----------+----------+---------+----------+--------------+-------------
501 a | text | | not null | | main | | A
502 b | text | | | | extended | | B
504 "ctlt1_pkey" PRIMARY KEY, btree (a)
505 "ctlt1_b_idx" btree (b)
506 "ctlt1_expr_idx" btree ((a || b))
508 "ctlt1_a_check" CHECK (length(a) > 2)
510 "ctl_schema.ctlt1_a_b_stat" ON a, b FROM ctlt1
511 "ctl_schema.ctlt1_expr_stat" ON (a || b) FROM ctlt1
512 Not-null constraints:
513 "ctlt1_a_not_null" NOT NULL "a"
516 DROP TABLE ctlt1, ctlt2, ctlt3, ctlt4, ctlt12_storage, ctlt12_comments, ctlt1_inh, ctlt13_inh, ctlt13_like, ctlt_all, ctla, ctlb CASCADE;
517 NOTICE: drop cascades to table inhe
518 -- LIKE must respect NO INHERIT property of constraints
519 CREATE TABLE noinh_con_copy (a int CHECK (a > 0) NO INHERIT, b int not null,
520 c int not null no inherit);
521 CREATE TABLE noinh_con_copy1 (LIKE noinh_con_copy INCLUDING CONSTRAINTS);
523 Table "public.noinh_con_copy1"
524 Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
525 --------+---------+-----------+----------+---------+---------+--------------+-------------
526 a | integer | | | | plain | |
527 b | integer | | not null | | plain | |
528 c | integer | | not null | | plain | |
530 "noinh_con_copy_a_check" CHECK (a > 0) NO INHERIT
531 Not-null constraints:
532 "noinh_con_copy_b_not_null" NOT NULL "b"
533 "noinh_con_copy_c_not_null" NOT NULL "c" NO INHERIT
535 -- fail, as partitioned tables don't allow NO INHERIT constraints
536 CREATE TABLE noinh_con_copy1_parted (LIKE noinh_con_copy INCLUDING ALL)
537 PARTITION BY LIST (a);
538 ERROR: cannot add NO INHERIT constraint to partitioned table "noinh_con_copy1_parted"
539 DROP TABLE noinh_con_copy, noinh_con_copy1;
540 /* LIKE with other relation kinds */
541 CREATE TABLE ctlt4 (a int, b text);
542 CREATE SEQUENCE ctlseq1;
543 CREATE TABLE ctlt10 (LIKE ctlseq1); -- fail
544 ERROR: relation "ctlseq1" is invalid in LIKE clause
545 LINE 1: CREATE TABLE ctlt10 (LIKE ctlseq1);
547 DETAIL: This operation is not supported for sequences.
548 CREATE VIEW ctlv1 AS SELECT * FROM ctlt4;
549 CREATE TABLE ctlt11 (LIKE ctlv1);
550 CREATE TABLE ctlt11a (LIKE ctlv1 INCLUDING ALL);
551 CREATE TYPE ctlty1 AS (a int, b text);
552 CREATE TABLE ctlt12 (LIKE ctlty1);
553 DROP SEQUENCE ctlseq1;
556 DROP TABLE IF EXISTS ctlt4, ctlt10, ctlt11, ctlt11a, ctlt12;
557 NOTICE: table "ctlt10" does not exist, skipping