Fix CREATE TABLE LIKE with not-valid check constraint

In CREATE TABLE ... LIKE, any check constraints copied from the source
table should be set to valid if they are ENFORCED (the default).

Bug introduced in commit ca87c415e2f.

Author: jian he <jian.universality@gmail.com>
Discussion: https://www.postgresql.org/message-id/CACJufxH%3D%2Bod8Wy0P4L3_GpapNwLUP3oAes5UFRJ7yTxrM_M5kg%40mail.gmail.com
This commit is contained in:
Peter Eisentraut 2025-09-10 11:49:53 +02:00
parent e6da68a6e1
commit 33eec80940
3 changed files with 10 additions and 2 deletions

View File

@ -1461,7 +1461,6 @@ expandTableLikeClause(RangeVar *heapRel, TableLikeClause *table_like_clause)
char *ccname = constr->check[ccnum].ccname; char *ccname = constr->check[ccnum].ccname;
char *ccbin = constr->check[ccnum].ccbin; char *ccbin = constr->check[ccnum].ccbin;
bool ccenforced = constr->check[ccnum].ccenforced; bool ccenforced = constr->check[ccnum].ccenforced;
bool ccvalid = constr->check[ccnum].ccvalid;
bool ccnoinherit = constr->check[ccnum].ccnoinherit; bool ccnoinherit = constr->check[ccnum].ccnoinherit;
Node *ccbin_node; Node *ccbin_node;
bool found_whole_row; bool found_whole_row;
@ -1492,7 +1491,7 @@ expandTableLikeClause(RangeVar *heapRel, TableLikeClause *table_like_clause)
n->conname = pstrdup(ccname); n->conname = pstrdup(ccname);
n->location = -1; n->location = -1;
n->is_enforced = ccenforced; n->is_enforced = ccenforced;
n->initially_valid = ccvalid; n->initially_valid = ccenforced; /* sic */
n->is_no_inherit = ccnoinherit; n->is_no_inherit = ccnoinherit;
n->raw_expr = NULL; n->raw_expr = NULL;
n->cooked_expr = nodeToString(ccbin_node); n->cooked_expr = nodeToString(ccbin_node);

View File

@ -320,6 +320,7 @@ DROP TABLE inhz;
-- including storage and comments -- including storage and comments
CREATE TABLE ctlt1 (a text CHECK (length(a) > 2) ENFORCED PRIMARY KEY, CREATE TABLE ctlt1 (a text CHECK (length(a) > 2) ENFORCED PRIMARY KEY,
b text CHECK (length(b) > 100) NOT ENFORCED); b text CHECK (length(b) > 100) NOT ENFORCED);
ALTER TABLE ctlt1 ADD CONSTRAINT cc CHECK (length(b) > 100) NOT VALID;
CREATE INDEX ctlt1_b_key ON ctlt1 (b); CREATE INDEX ctlt1_b_key ON ctlt1 (b);
CREATE INDEX ctlt1_fnidx ON ctlt1 ((a || b)); CREATE INDEX ctlt1_fnidx ON ctlt1 ((a || b));
CREATE STATISTICS ctlt1_a_b_stat ON a,b FROM ctlt1; CREATE STATISTICS ctlt1_a_b_stat ON a,b FROM ctlt1;
@ -378,6 +379,7 @@ SELECT conname, description FROM pg_description, pg_constraint c WHERE classoid
CREATE TABLE ctlt1_inh (LIKE ctlt1 INCLUDING CONSTRAINTS INCLUDING COMMENTS) INHERITS (ctlt1); CREATE TABLE ctlt1_inh (LIKE ctlt1 INCLUDING CONSTRAINTS INCLUDING COMMENTS) INHERITS (ctlt1);
NOTICE: merging column "a" with inherited definition NOTICE: merging column "a" with inherited definition
NOTICE: merging column "b" with inherited definition NOTICE: merging column "b" with inherited definition
NOTICE: merging constraint "cc" with inherited definition
NOTICE: merging constraint "ctlt1_a_check" with inherited definition NOTICE: merging constraint "ctlt1_a_check" with inherited definition
NOTICE: merging constraint "ctlt1_b_check" with inherited definition NOTICE: merging constraint "ctlt1_b_check" with inherited definition
\d+ ctlt1_inh \d+ ctlt1_inh
@ -387,6 +389,7 @@ NOTICE: merging constraint "ctlt1_b_check" with inherited definition
a | text | | not null | | main | | A a | text | | not null | | main | | A
b | text | | | | extended | | B b | text | | | | extended | | B
Check constraints: Check constraints:
"cc" CHECK (length(b) > 100)
"ctlt1_a_check" CHECK (length(a) > 2) "ctlt1_a_check" CHECK (length(a) > 2)
"ctlt1_b_check" CHECK (length(b) > 100) NOT ENFORCED "ctlt1_b_check" CHECK (length(b) > 100) NOT ENFORCED
Not-null constraints: Not-null constraints:
@ -409,6 +412,7 @@ NOTICE: merging multiple inherited definitions of column "a"
b | text | | | | extended | | b | text | | | | extended | |
c | text | | | | external | | c | text | | | | external | |
Check constraints: Check constraints:
"cc" CHECK (length(b) > 100)
"ctlt1_a_check" CHECK (length(a) > 2) "ctlt1_a_check" CHECK (length(a) > 2)
"ctlt1_b_check" CHECK (length(b) > 100) NOT ENFORCED "ctlt1_b_check" CHECK (length(b) > 100) NOT ENFORCED
"ctlt3_a_check" CHECK (length(a) < 5) "ctlt3_a_check" CHECK (length(a) < 5)
@ -430,6 +434,7 @@ NOTICE: merging column "a" with inherited definition
Indexes: Indexes:
"ctlt13_like_expr_idx" btree ((a || c)) "ctlt13_like_expr_idx" btree ((a || c))
Check constraints: Check constraints:
"cc" CHECK (length(b) > 100)
"ctlt1_a_check" CHECK (length(a) > 2) "ctlt1_a_check" CHECK (length(a) > 2)
"ctlt1_b_check" CHECK (length(b) > 100) NOT ENFORCED "ctlt1_b_check" CHECK (length(b) > 100) NOT ENFORCED
"ctlt3_a_check" CHECK (length(a) < 5) "ctlt3_a_check" CHECK (length(a) < 5)
@ -456,6 +461,7 @@ Indexes:
"ctlt_all_b_idx" btree (b) "ctlt_all_b_idx" btree (b)
"ctlt_all_expr_idx" btree ((a || b)) "ctlt_all_expr_idx" btree ((a || b))
Check constraints: Check constraints:
"cc" CHECK (length(b) > 100)
"ctlt1_a_check" CHECK (length(a) > 2) "ctlt1_a_check" CHECK (length(a) > 2)
"ctlt1_b_check" CHECK (length(b) > 100) NOT ENFORCED "ctlt1_b_check" CHECK (length(b) > 100) NOT ENFORCED
Statistics objects: Statistics objects:
@ -499,6 +505,7 @@ Indexes:
"pg_attrdef_b_idx" btree (b) "pg_attrdef_b_idx" btree (b)
"pg_attrdef_expr_idx" btree ((a || b)) "pg_attrdef_expr_idx" btree ((a || b))
Check constraints: Check constraints:
"cc" CHECK (length(b) > 100)
"ctlt1_a_check" CHECK (length(a) > 2) "ctlt1_a_check" CHECK (length(a) > 2)
"ctlt1_b_check" CHECK (length(b) > 100) NOT ENFORCED "ctlt1_b_check" CHECK (length(b) > 100) NOT ENFORCED
Statistics objects: Statistics objects:
@ -524,6 +531,7 @@ Indexes:
"ctlt1_b_idx" btree (b) "ctlt1_b_idx" btree (b)
"ctlt1_expr_idx" btree ((a || b)) "ctlt1_expr_idx" btree ((a || b))
Check constraints: Check constraints:
"cc" CHECK (length(b) > 100)
"ctlt1_a_check" CHECK (length(a) > 2) "ctlt1_a_check" CHECK (length(a) > 2)
"ctlt1_b_check" CHECK (length(b) > 100) NOT ENFORCED "ctlt1_b_check" CHECK (length(b) > 100) NOT ENFORCED
Statistics objects: Statistics objects:

View File

@ -130,6 +130,7 @@ DROP TABLE inhz;
-- including storage and comments -- including storage and comments
CREATE TABLE ctlt1 (a text CHECK (length(a) > 2) ENFORCED PRIMARY KEY, CREATE TABLE ctlt1 (a text CHECK (length(a) > 2) ENFORCED PRIMARY KEY,
b text CHECK (length(b) > 100) NOT ENFORCED); b text CHECK (length(b) > 100) NOT ENFORCED);
ALTER TABLE ctlt1 ADD CONSTRAINT cc CHECK (length(b) > 100) NOT VALID;
CREATE INDEX ctlt1_b_key ON ctlt1 (b); CREATE INDEX ctlt1_b_key ON ctlt1 (b);
CREATE INDEX ctlt1_fnidx ON ctlt1 ((a || b)); CREATE INDEX ctlt1_fnidx ON ctlt1 ((a || b));
CREATE STATISTICS ctlt1_a_b_stat ON a,b FROM ctlt1; CREATE STATISTICS ctlt1_a_b_stat ON a,b FROM ctlt1;