Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 81ce602

Browse files
MasaoFujiiÁlvaro Herrera
andcommitted
Make CREATE TABLE LIKE copy comments on NOT NULL constraints when requested.
Commit 14e87ff introduced support for adding comments to NOT NULL constraints. However, CREATE TABLE LIKE INCLUDING COMMENTS did not copy these comments to the new table. This was an oversight in that commit. This commit corrects the behavior by ensuring CREATE TABLE LIKE to also copy the comments on NOT NULL constraints when INCLUDING COMMENTS is specified. Author: Jian He <[email protected]> Co-authored-by: Álvaro Herrera <[email protected]> Reviewed-by: Fujii Masao <[email protected]> Discussion: https://postgr.es/m/[email protected]
1 parent 3ba9639 commit 81ce602

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

src/backend/parser/parse_utilcmd.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,28 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla
12791279
lst = RelationGetNotNullConstraints(RelationGetRelid(relation), false,
12801280
true);
12811281
cxt->nnconstraints = list_concat(cxt->nnconstraints, lst);
1282+
1283+
/* Copy comments on not-null constraints */
1284+
if (table_like_clause->options & CREATE_TABLE_LIKE_COMMENTS)
1285+
{
1286+
foreach_node(Constraint, nnconstr, lst)
1287+
{
1288+
if ((comment = GetComment(get_relation_constraint_oid(RelationGetRelid(relation),
1289+
nnconstr->conname, false),
1290+
ConstraintRelationId,
1291+
0)) != NULL)
1292+
{
1293+
CommentStmt *stmt = makeNode(CommentStmt);
1294+
1295+
stmt->objtype = OBJECT_TABCONSTRAINT;
1296+
stmt->object = (Node *) list_make3(makeString(cxt->relation->schemaname),
1297+
makeString(cxt->relation->relname),
1298+
makeString(nnconstr->conname));
1299+
stmt->comment = comment;
1300+
cxt->alist = lappend(cxt->alist, stmt);
1301+
}
1302+
}
1303+
}
12821304
}
12831305

12841306
/*

src/test/regress/expected/create_table_like.out

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,10 @@ COMMENT ON CONSTRAINT ctlt1_a_check ON ctlt1 IS 't1_a_check';
332332
COMMENT ON INDEX ctlt1_pkey IS 'index pkey';
333333
COMMENT ON INDEX ctlt1_b_key IS 'index b_key';
334334
ALTER TABLE ctlt1 ALTER COLUMN a SET STORAGE MAIN;
335-
CREATE TABLE ctlt2 (c text);
335+
CREATE TABLE ctlt2 (c text NOT NULL);
336336
ALTER TABLE ctlt2 ALTER COLUMN c SET STORAGE EXTERNAL;
337337
COMMENT ON COLUMN ctlt2.c IS 'C';
338+
COMMENT ON CONSTRAINT ctlt2_c_not_null ON ctlt2 IS 't2_c_not_null';
338339
CREATE TABLE ctlt3 (a text CHECK (length(a) < 5), c text CHECK (length(c) < 7));
339340
ALTER TABLE ctlt3 ALTER COLUMN c SET STORAGE EXTERNAL;
340341
ALTER TABLE ctlt3 ALTER COLUMN a SET STORAGE MAIN;
@@ -351,9 +352,10 @@ CREATE TABLE ctlt12_storage (LIKE ctlt1 INCLUDING STORAGE, LIKE ctlt2 INCLUDING
351352
--------+------+-----------+----------+---------+----------+--------------+-------------
352353
a | text | | not null | | main | |
353354
b | text | | | | extended | |
354-
c | text | | | | external | |
355+
c | text | | not null | | external | |
355356
Not-null constraints:
356357
"ctlt1_a_not_null" NOT NULL "a"
358+
"ctlt2_c_not_null" NOT NULL "c"
357359

358360
CREATE TABLE ctlt12_comments (LIKE ctlt1 INCLUDING COMMENTS, LIKE ctlt2 INCLUDING COMMENTS);
359361
\d+ ctlt12_comments
@@ -362,9 +364,16 @@ CREATE TABLE ctlt12_comments (LIKE ctlt1 INCLUDING COMMENTS, LIKE ctlt2 INCLUDIN
362364
--------+------+-----------+----------+---------+----------+--------------+-------------
363365
a | text | | not null | | extended | | A
364366
b | text | | | | extended | | B
365-
c | text | | | | extended | | C
367+
c | text | | not null | | extended | | C
366368
Not-null constraints:
367369
"ctlt1_a_not_null" NOT NULL "a"
370+
"ctlt2_c_not_null" NOT NULL "c"
371+
372+
SELECT conname, description FROM pg_description, pg_constraint c WHERE classoid = 'pg_constraint'::regclass AND objoid = c.oid AND c.conrelid = 'ctlt12_comments'::regclass;
373+
conname | description
374+
------------------+---------------
375+
ctlt2_c_not_null | t2_c_not_null
376+
(1 row)
368377

369378
CREATE TABLE ctlt1_inh (LIKE ctlt1 INCLUDING CONSTRAINTS INCLUDING COMMENTS) INHERITS (ctlt1);
370379
NOTICE: merging column "a" with inherited definition
@@ -529,7 +538,9 @@ NOTICE: drop cascades to table inhe
529538
-- LIKE must respect NO INHERIT property of constraints
530539
CREATE TABLE noinh_con_copy (a int CHECK (a > 0) NO INHERIT, b int not null,
531540
c int not null no inherit);
532-
CREATE TABLE noinh_con_copy1 (LIKE noinh_con_copy INCLUDING CONSTRAINTS);
541+
COMMENT ON CONSTRAINT noinh_con_copy_b_not_null ON noinh_con_copy IS 'not null b';
542+
COMMENT ON CONSTRAINT noinh_con_copy_c_not_null ON noinh_con_copy IS 'not null c no inherit';
543+
CREATE TABLE noinh_con_copy1 (LIKE noinh_con_copy INCLUDING CONSTRAINTS INCLUDING COMMENTS);
533544
\d+ noinh_con_copy1
534545
Table "public.noinh_con_copy1"
535546
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
@@ -543,6 +554,17 @@ Not-null constraints:
543554
"noinh_con_copy_b_not_null" NOT NULL "b"
544555
"noinh_con_copy_c_not_null" NOT NULL "c" NO INHERIT
545556

557+
SELECT conname, description
558+
FROM pg_description, pg_constraint c
559+
WHERE classoid = 'pg_constraint'::regclass
560+
AND objoid = c.oid AND c.conrelid = 'noinh_con_copy1'::regclass
561+
ORDER BY conname COLLATE "C";
562+
conname | description
563+
---------------------------+-----------------------
564+
noinh_con_copy_b_not_null | not null b
565+
noinh_con_copy_c_not_null | not null c no inherit
566+
(2 rows)
567+
546568
-- fail, as partitioned tables don't allow NO INHERIT constraints
547569
CREATE TABLE noinh_con_copy1_parted (LIKE noinh_con_copy INCLUDING ALL)
548570
PARTITION BY LIST (a);

src/test/regress/sql/create_table_like.sql

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,10 @@ COMMENT ON INDEX ctlt1_pkey IS 'index pkey';
143143
COMMENT ON INDEX ctlt1_b_key IS 'index b_key';
144144
ALTER TABLE ctlt1 ALTER COLUMN a SET STORAGE MAIN;
145145

146-
CREATE TABLE ctlt2 (c text);
146+
CREATE TABLE ctlt2 (c text NOT NULL);
147147
ALTER TABLE ctlt2 ALTER COLUMN c SET STORAGE EXTERNAL;
148148
COMMENT ON COLUMN ctlt2.c IS 'C';
149+
COMMENT ON CONSTRAINT ctlt2_c_not_null ON ctlt2 IS 't2_c_not_null';
149150

150151
CREATE TABLE ctlt3 (a text CHECK (length(a) < 5), c text CHECK (length(c) < 7));
151152
ALTER TABLE ctlt3 ALTER COLUMN c SET STORAGE EXTERNAL;
@@ -162,6 +163,7 @@ CREATE TABLE ctlt12_storage (LIKE ctlt1 INCLUDING STORAGE, LIKE ctlt2 INCLUDING
162163
\d+ ctlt12_storage
163164
CREATE TABLE ctlt12_comments (LIKE ctlt1 INCLUDING COMMENTS, LIKE ctlt2 INCLUDING COMMENTS);
164165
\d+ ctlt12_comments
166+
SELECT conname, description FROM pg_description, pg_constraint c WHERE classoid = 'pg_constraint'::regclass AND objoid = c.oid AND c.conrelid = 'ctlt12_comments'::regclass;
165167
CREATE TABLE ctlt1_inh (LIKE ctlt1 INCLUDING CONSTRAINTS INCLUDING COMMENTS) INHERITS (ctlt1);
166168
\d+ ctlt1_inh
167169
SELECT description FROM pg_description, pg_constraint c WHERE classoid = 'pg_constraint'::regclass AND objoid = c.oid AND c.conrelid = 'ctlt1_inh'::regclass;
@@ -197,9 +199,19 @@ DROP TABLE ctlt1, ctlt2, ctlt3, ctlt4, ctlt12_storage, ctlt12_comments, ctlt1_in
197199
-- LIKE must respect NO INHERIT property of constraints
198200
CREATE TABLE noinh_con_copy (a int CHECK (a > 0) NO INHERIT, b int not null,
199201
c int not null no inherit);
200-
CREATE TABLE noinh_con_copy1 (LIKE noinh_con_copy INCLUDING CONSTRAINTS);
202+
203+
COMMENT ON CONSTRAINT noinh_con_copy_b_not_null ON noinh_con_copy IS 'not null b';
204+
COMMENT ON CONSTRAINT noinh_con_copy_c_not_null ON noinh_con_copy IS 'not null c no inherit';
205+
206+
CREATE TABLE noinh_con_copy1 (LIKE noinh_con_copy INCLUDING CONSTRAINTS INCLUDING COMMENTS);
201207
\d+ noinh_con_copy1
202208

209+
SELECT conname, description
210+
FROM pg_description, pg_constraint c
211+
WHERE classoid = 'pg_constraint'::regclass
212+
AND objoid = c.oid AND c.conrelid = 'noinh_con_copy1'::regclass
213+
ORDER BY conname COLLATE "C";
214+
203215
-- fail, as partitioned tables don't allow NO INHERIT constraints
204216
CREATE TABLE noinh_con_copy1_parted (LIKE noinh_con_copy INCLUDING ALL)
205217
PARTITION BY LIST (a);

0 commit comments

Comments
 (0)