Re: not null constraints, again
jian he <jian.universality@gmail.com>
From: jian he <jian.universality@gmail.com>
To: Alvaro Herrera <alvherre@alvh.no-ip.org>
Cc: Pg Hackers <pgsql-hackers@lists.postgresql.org>,
Tender Wang <tndrwang@gmail.com>
Date: 2024-10-10T01:12:16Z
Lists: pgsql-hackers
Attachments
- v8-0001-transformColumnDefinition-minor-refactor.no-cfbot (application/octet-stream)
I did some refactoring on transformColumnDefinition
since transformColumnDefinition only deals with a single ColumnDef.
and serial/primary/identity cannot allow not-null no inherit.
We can preliminary iterate through ColumnDef->constraints to check
that ColumnDef can allow not-null no inherit or not.
if not allowed, then error out at CONSTR_NOTNULL.
please check attached.
in MergeConstraintsIntoExisting
we can
while (HeapTupleIsValid(child_tuple = systable_getnext(child_scan)))
{
Form_pg_constraint child_con = (Form_pg_constraint)
GETSTRUCT(child_tuple);
HeapTuple child_copy;
if (child_con->contype != parent_con->contype)
continue;
if (child_con->connoinherit)
ereport(ERROR,
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
errmsg("constraint \"%s\" conflicts with
non-inherited constraint on child table \"%s\"",
NameStr(child_con->conname),
RelationGetRelationName(child_rel))));
if (parent_con->convalidated && !child_con->convalidated)
ereport(ERROR,
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
errmsg("constraint \"%s\" conflicts with NOT
VALID constraint on child table \"%s\"",
NameStr(child_con->conname),
RelationGetRelationName(child_rel))));
}
error out earlier, save some cache search cycle.
MergeConstraintsIntoExisting comment says
" * XXX See MergeWithExistingConstraint too if you change this code."
we actually did change the MergeConstraintsIntoExisting, not change
MergeWithExistingConstraint
but it seems MergeWithExistingConstraint does not deal with CONSTRAINT_NOTNULL.
So I guess the comments are fine.
previously, we mentioned adding some domain tests at [1].
but it seems v8, we don't have domain related regression tests.
[1] https://www.postgresql.org/message-id/202409252014.74iepgsyuyws%40alvherre.pgsql
Commits
-
Suppress "may be used uninitialized" warnings from older compilers.
- fc5e966f73f0 18.0 landed
-
Elide not-null constraint checks on child tables during PK creation
- 11ff192b5bb7 18.0 landed
-
Remove unnecessary code to handle CONSTR_NOTNULL
- 5b291d1c9c09 18.0 landed
-
Silence compilers about extractNotNullColumn()
- ff239c3bf4e8 18.0 landed
-
Add pg_constraint rows for not-null constraints
- 14e87ffa5c54 18.0 landed