Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcacef17

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 constrainthas properties different from the original, it seems that ignoringa 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 barelypossible somebody is depending on the current behavior.Ildar Musin and Chris Travers; reviewed by Amit Langote and myselfDiscussion:https://postgr.es/m/CAONYFtMC6C+3AWCVp7Yd8H87Zn0GxG1_iQG6_bQKbaqYZY0=-g@mail.gmail.com
1 parentdbf05a1 commitcacef17

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
intccnum;
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+
boolccnoinherit=constr->check[ccnum].ccnoinherit;
11421144
Constraint*n=makeNode(Constraint);
11431145
Node*ccbin_node;
11441146
boolfound_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
DROPTABLE 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+
CREATETABLEnoinh_con_copy (aintCHECK (a>0) NO INHERIT);
156+
CREATETABLEnoinh_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+
CREATETABLEnoinh_con_copy1_parted (LIKE noinh_con_copy INCLUDING ALL)
161+
PARTITION BY LIST (a);
162+
163+
DROPTABLE noinh_con_copy, noinh_con_copy1;
164+
154165

155166
/* LIKE with other relation kinds*/
156167

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp