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

Skip to content

Commit cacef17

Browse files
committed
Ensure that CREATE TABLE LIKE copies any NO INHERIT constraint property.
Since the documentation about LIKE doesn't say that a copied constraint has properties different from the original, it seems that ignoring a NO INHERIT property doesn't meet the principle of least surprise. So make it copy that. (Note, however, that we still don't copy a NOT VALID property; CREATE TABLE offers no way to do that, plus it seems pointless.) Arguably this is a bug fix; but no back-patch, as it seems barely possible somebody is depending on the current behavior. Ildar Musin and Chris Travers; reviewed by Amit Langote and myself Discussion: https://postgr.es/m/CAONYFtMC6C+3AWCVp7Yd8H87Zn0GxG1_iQG6_bQKbaqYZY0=-g@mail.gmail.com
1 parent dbf05a1 commit cacef17

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

src/backend/parser/parse_utilcmd.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,12 +1133,14 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla
11331133
if ((table_like_clause->options & CREATE_TABLE_LIKE_CONSTRAINTS) &&
11341134
tupleDesc->constr)
11351135
{
1136+
TupleConstr *constr = tupleDesc->constr;
11361137
int ccnum;
11371138

1138-
for (ccnum = 0; ccnum < tupleDesc->constr->num_check; ccnum++)
1139+
for (ccnum = 0; ccnum < constr->num_check; ccnum++)
11391140
{
1140-
char *ccname = tupleDesc->constr->check[ccnum].ccname;
1141-
char *ccbin = tupleDesc->constr->check[ccnum].ccbin;
1141+
char *ccname = constr->check[ccnum].ccname;
1142+
char *ccbin = constr->check[ccnum].ccbin;
1143+
bool ccnoinherit = constr->check[ccnum].ccnoinherit;
11421144
Constraint *n = makeNode(Constraint);
11431145
Node *ccbin_node;
11441146
bool found_whole_row;
@@ -1163,8 +1165,9 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla
11631165
RelationGetRelationName(relation))));
11641166

11651167
n->contype = CONSTR_CHECK;
1166-
n->location = -1;
11671168
n->conname = pstrdup(ccname);
1169+
n->location = -1;
1170+
n->is_no_inherit = ccnoinherit;
11681171
n->raw_expr = NULL;
11691172
n->cooked_expr = nodeToString(ccbin_node);
11701173
cxt->ckconstraints = lappend(cxt->ckconstraints, n);

src/test/regress/expected/create_table_like.out

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,22 @@ ERROR: column "a" has a storage parameter conflict
388388
DETAIL: MAIN versus EXTENDED
389389
DROP TABLE ctlt1, ctlt2, ctlt3, ctlt4, ctlt12_storage, ctlt12_comments, ctlt1_inh, ctlt13_inh, ctlt13_like, ctlt_all, ctla, ctlb CASCADE;
390390
NOTICE: drop cascades to table inhe
391+
-- LIKE must respect NO INHERIT property of constraints
392+
CREATE TABLE noinh_con_copy (a int CHECK (a > 0) NO INHERIT);
393+
CREATE TABLE noinh_con_copy1 (LIKE noinh_con_copy INCLUDING CONSTRAINTS);
394+
\d noinh_con_copy1
395+
Table "public.noinh_con_copy1"
396+
Column | Type | Collation | Nullable | Default
397+
--------+---------+-----------+----------+---------
398+
a | integer | | |
399+
Check constraints:
400+
"noinh_con_copy_a_check" CHECK (a > 0) NO INHERIT
401+
402+
-- fail, as partitioned tables don't allow NO INHERIT constraints
403+
CREATE TABLE noinh_con_copy1_parted (LIKE noinh_con_copy INCLUDING ALL)
404+
PARTITION BY LIST (a);
405+
ERROR: cannot add NO INHERIT constraint to partitioned table "noinh_con_copy1_parted"
406+
DROP TABLE noinh_con_copy, noinh_con_copy1;
391407
/* LIKE with other relation kinds */
392408
CREATE TABLE ctlt4 (a int, b text);
393409
CREATE SEQUENCE ctlseq1;

src/test/regress/sql/create_table_like.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,17 @@ CREATE TABLE inh_error2 (LIKE ctlt4 INCLUDING STORAGE) INHERITS (ctlt1);
151151

152152
DROP TABLE ctlt1, ctlt2, ctlt3, ctlt4, ctlt12_storage, ctlt12_comments, ctlt1_inh, ctlt13_inh, ctlt13_like, ctlt_all, ctla, ctlb CASCADE;
153153

154+
-- LIKE must respect NO INHERIT property of constraints
155+
CREATE TABLE noinh_con_copy (a int CHECK (a > 0) NO INHERIT);
156+
CREATE TABLE noinh_con_copy1 (LIKE noinh_con_copy INCLUDING CONSTRAINTS);
157+
\d noinh_con_copy1
158+
159+
-- fail, as partitioned tables don't allow NO INHERIT constraints
160+
CREATE TABLE noinh_con_copy1_parted (LIKE noinh_con_copy INCLUDING ALL)
161+
PARTITION BY LIST (a);
162+
163+
DROP TABLE noinh_con_copy, noinh_con_copy1;
164+
154165

155166
/* LIKE with other relation kinds */
156167

0 commit comments

Comments
 (0)