diff options
Diffstat (limited to 'src/backend/catalog/pg_constraint.c')
-rw-r--r-- | src/backend/catalog/pg_constraint.c | 46 |
1 files changed, 30 insertions, 16 deletions
diff --git a/src/backend/catalog/pg_constraint.c b/src/backend/catalog/pg_constraint.c index b97960d2766..2f73085961b 100644 --- a/src/backend/catalog/pg_constraint.c +++ b/src/backend/catalog/pg_constraint.c @@ -576,8 +576,8 @@ ChooseConstraintName(const char *name1, const char *name2, /* * Find and return a copy of the pg_constraint tuple that implements a - * validated not-null constraint for the given column of the given relation. - * If no such constraint exists, return NULL. + * (possibly not valid) not-null constraint for the given column of the + * given relation. If no such constraint exists, return NULL. * * XXX This would be easier if we had pg_attribute.notnullconstr with the OID * of the constraint that implements the not-null constraint for that column. @@ -606,13 +606,11 @@ findNotNullConstraintAttnum(Oid relid, AttrNumber attnum) AttrNumber conkey; /* - * We're looking for a NOTNULL constraint that's marked validated, - * with the column we're looking for as the sole element in conkey. + * We're looking for a NOTNULL constraint with the column we're + * looking for as the sole element in conkey. */ if (con->contype != CONSTRAINT_NOTNULL) continue; - if (!con->convalidated) - continue; conkey = extractNotNullColumn(conTup); if (conkey != attnum) @@ -630,9 +628,10 @@ findNotNullConstraintAttnum(Oid relid, AttrNumber attnum) } /* - * Find and return the pg_constraint tuple that implements a validated - * not-null constraint for the given column of the given relation. If - * no such column or no such constraint exists, return NULL. + * Find and return a copy of the pg_constraint tuple that implements a + * (possibly not valid) not-null constraint for the given column of the + * given relation. + * If no such column or no such constraint exists, return NULL. */ HeapTuple findNotNullConstraint(Oid relid, const char *colname) @@ -723,15 +722,19 @@ extractNotNullColumn(HeapTuple constrTup) * * If no not-null constraint is found for the column, return false. * Caller can create one. + * * If a constraint exists but the connoinherit flag is not what the caller - * wants, throw an error about the incompatibility. Otherwise, we adjust - * conislocal/coninhcount and return true. - * In the latter case, if is_local is true we flip conislocal true, or do - * nothing if it's already true; otherwise we increment coninhcount by 1. + * wants, throw an error about the incompatibility. If the desired + * constraint is valid but the existing constraint is not valid, also + * throw an error about that (the opposite case is acceptable). + * + * If everything checks out, we adjust conislocal/coninhcount and return + * true. If is_local is true we flip conislocal true, or do nothing if + * it's already true; otherwise we increment coninhcount by 1. */ bool AdjustNotNullInheritance(Oid relid, AttrNumber attnum, - bool is_local, bool is_no_inherit) + bool is_local, bool is_no_inherit, bool is_notvalid) { HeapTuple tup; @@ -755,6 +758,17 @@ AdjustNotNullInheritance(Oid relid, AttrNumber attnum, errmsg("cannot change NO INHERIT status of NOT NULL constraint \"%s\" on relation \"%s\"", NameStr(conform->conname), get_rel_name(relid))); + /* + * Throw an error if the existing constraint is NOT VALID and caller + * wants a valid one. + */ + if (!is_notvalid && !conform->convalidated) + ereport(ERROR, + errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), + errmsg("incompatible NOT VALID constraint \"%s\" on relation \"%s\"", + NameStr(conform->conname), get_rel_name(relid)), + errhint("You will need to use ALTER TABLE ... VALIDATE CONSTRAINT to validate it.")); + if (!is_local) { if (pg_add_s16_overflow(conform->coninhcount, 1, @@ -832,7 +846,7 @@ RelationGetNotNullConstraints(Oid relid, bool cooked, bool include_noinh) cooked->attnum = colnum; cooked->expr = NULL; cooked->is_enforced = true; - cooked->skip_validation = false; + cooked->skip_validation = !conForm->convalidated; cooked->is_local = true; cooked->inhcount = 0; cooked->is_no_inherit = conForm->connoinherit; @@ -852,7 +866,7 @@ RelationGetNotNullConstraints(Oid relid, bool cooked, bool include_noinh) constr->keys = list_make1(makeString(get_attname(relid, colnum, false))); constr->is_enforced = true; - constr->skip_validation = false; + constr->skip_validation = !conForm->convalidated; constr->initially_valid = true; constr->is_no_inherit = conForm->connoinherit; notnulls = lappend(notnulls, constr); |