1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-03 20:02:46 +03:00

Fix not-null constraint test

When a partitioned table has a primary key, trying to find the
corresponding not-null constraint for that column would come up empty,
causing code that's trying to check said not-null constraint to crash.
Fix by only running the check when the not-null constraint exists.

Reported-by: Alexander Lakhin <exclusion@gmail.com>
Discussion: https://postgr.es/m/d57b4a69-7394-3146-5976-9a1ef27e7972@gmail.com
This commit is contained in:
Alvaro Herrera
2023-09-01 19:49:20 +02:00
parent e09d763e25
commit d0ec2ddbe0
3 changed files with 41 additions and 7 deletions

View File

@ -15746,9 +15746,10 @@ MergeAttributesIntoExisting(Relation child_rel, Relation parent_rel)
attributeName)));
/*
* Check child doesn't discard NOT NULL property. (Other
* constraints are checked elsewhere.) However, if the constraint
* is NO INHERIT in the parent, this is allowed.
* If the parent has a not-null constraint that's not NO INHERIT,
* make sure the child has one too.
*
* Other constraints are checked elsewhere.
*/
if (attribute->attnotnull && !childatt->attnotnull)
{
@ -15756,11 +15757,12 @@ MergeAttributesIntoExisting(Relation child_rel, Relation parent_rel)
contup = findNotNullConstraintAttnum(RelationGetRelid(parent_rel),
attribute->attnum);
if (!((Form_pg_constraint) GETSTRUCT(contup))->connoinherit)
if (HeapTupleIsValid(contup) &&
!((Form_pg_constraint) GETSTRUCT(contup))->connoinherit)
ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH),
errmsg("column \"%s\" in child table must be marked NOT NULL",
attributeName)));
errcode(ERRCODE_DATATYPE_MISMATCH),
errmsg("column \"%s\" in child table must be marked NOT NULL",
attributeName));
}
/*
@ -15981,10 +15983,20 @@ MergeConstraintsIntoExisting(Relation child_rel, Relation parent_rel)
systable_endscan(child_scan);
if (!found)
{
if (parent_con->contype == CONSTRAINT_NOTNULL)
ereport(ERROR,
errcode(ERRCODE_DATATYPE_MISMATCH),
errmsg("column \"%s\" in child table must be marked NOT NULL",
get_attname(parent_relid,
extractNotNullColumn(parent_tuple),
false)));
ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH),
errmsg("child table is missing constraint \"%s\"",
NameStr(parent_con->conname))));
}
}
systable_endscan(parent_scan);