From: | jian he <jian(dot)universality(at)gmail(dot)com> |
---|---|
To: | Álvaro Herrera <alvherre(at)kurilemu(dot)de> |
Cc: | PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org> |
Subject: | Re: NOT NULL NOT ENFORCED |
Date: | 2025-09-04 15:20:42 |
Message-ID: | CACJufxH=+od8Wy0P4L3_GpapNwLUP3oAes5UFRJ7yTxrM_M5kg@mail.gmail.com |
Views: | Whole Thread | Raw Message | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-hackers |
On Thu, Sep 4, 2025 at 8:00 PM Álvaro Herrera <alvherre(at)kurilemu(dot)de> wrote:
>
> > @@ -1272,33 +1294,41 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla
> > * Reproduce not-null constraints, if any, by copying them. We do this
> > * regardless of options given.
> > */
> > - if (tupleDesc->constr && tupleDesc->constr->has_not_null)
> > - {
> > - List *lst;
> > + lst = RelationGetNotNullConstraints(RelationGetRelid(relation), false,
> > + true);
> > + cxt->nnconstraints = list_concat(cxt->nnconstraints, lst);
> >
> > - lst = RelationGetNotNullConstraints(RelationGetRelid(relation), false,
> > - true);
>
> > + /*
> > + * When creating a new relation, marking the enforced not-null constraint as
> > + * not valid doesn't make sense, so we treat it as valid.
> > + */
> > + foreach_node(Constraint, nnconstr, lst)
> > + {
> > + if (nnconstr->is_enforced)
> > + {
> > + nnconstr->skip_validation = false;
> > + nnconstr->initially_valid = true;
> > + }
> > + }
>
> Hmmm, this bit here (making constraints as valid if they're not valid in
> the source table) looks like a fix for the existing code. I think it
> should be a separate patch, perhaps back-patchable to 18. Or maybe I'm
> missing something ...?
>
it's indeed a bug, which was introduced
https://git.postgresql.org/cgit/postgresql.git/diff/src/backend/parser/parse_utilcmd.c?id=ca87c415e2fccf81cec6fd45698dde9fae0ab570
attached is the fix, also added a test on create_table_like.sql
Attachment | Content-Type | Size |
---|---|---|
v1-0001-fix-CREATE-TABLE-LIKE-with-INVALID-CHECK-CONSTRAINT.patch | text/x-patch | 5.5 KB |
From | Date | Subject | |
---|---|---|---|
Next Message | Nathan Bossart | 2025-09-04 15:44:00 | Re: Improve LWLock tranche name visibility across backends |
Previous Message | Maksim.Melnikov | 2025-09-04 15:18:30 | Incorrect checksum in control file with pg_rewind test |