mirror of
https://github.com/postgres/postgres.git
synced 2025-06-14 18:42:34 +03:00
Disallow direct change of NO INHERIT of not-null constraints
We support changing NO INHERIT constraint to INHERIT for constraints in child relations when adding a constraint to some ancestor relation, and also during pg_upgrade's schema restore; but other than those special cases, command ALTER TABLE ADD CONSTRAINT should not be allowed to change an existing constraint from NO INHERIT to INHERIT, as that would require to process child relations so that they also acquire an appropriate constraint, which we may not be in a position to do. (It'd also be surprising behavior.) It is conceivable that we want to allow ALTER TABLE SET NOT NULL to make such a change; but in that case some more code is needed to implement it correctly, so for now I've made that throw the same error message. Also, during the prep phase of ALTER TABLE ADD CONSTRAINT, acquire locks on all descendant tables; otherwise we might operate on child tables on which no locks are held, particularly in the mode where a primary key causes not-null constraints to be created on children. Reported-by: Alexander Lakhin <exclusion@gmail.com> Discussion: https://postgr.es/m/7d923a66-55f0-3395-cd40-81c142b5448b@gmail.com
This commit is contained in:
@ -4980,10 +4980,12 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd,
|
||||
break;
|
||||
case AT_AddConstraint: /* ADD CONSTRAINT */
|
||||
ATSimplePermissions(cmd->subtype, rel, ATT_TABLE | ATT_FOREIGN_TABLE);
|
||||
/* Recursion occurs during execution phase */
|
||||
/* No command-specific prep needed except saving recurse flag */
|
||||
if (recurse)
|
||||
{
|
||||
/* recurses at exec time; lock descendants and set flag */
|
||||
(void) find_all_inheritors(RelationGetRelid(rel), lockmode, NULL);
|
||||
cmd->recurse = true;
|
||||
}
|
||||
pass = AT_PASS_ADD_CONSTR;
|
||||
break;
|
||||
case AT_AddIndexConstraint: /* ADD CONSTRAINT USING INDEX */
|
||||
@ -7892,6 +7894,17 @@ ATExecSetNotNull(List **wqueue, Relation rel, char *conName, char *colName,
|
||||
copytup = heap_copytuple(tuple);
|
||||
conForm = (Form_pg_constraint) GETSTRUCT(copytup);
|
||||
|
||||
/*
|
||||
* Don't let a NO INHERIT constraint be changed into inherit.
|
||||
*/
|
||||
if (conForm->connoinherit && recurse)
|
||||
ereport(ERROR,
|
||||
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
||||
errmsg("cannot change NO INHERIT status of NOT NULL constraint \"%s\" in relation \"%s\"",
|
||||
NameStr(conForm->conname),
|
||||
RelationGetRelationName(rel)));
|
||||
|
||||
|
||||
/*
|
||||
* If we find an appropriate constraint, we're almost done, but just
|
||||
* need to change some properties on it: if we're recursing, increment
|
||||
|
Reference in New Issue
Block a user