aboutsummaryrefslogtreecommitdiff
path: root/src/backend
diff options
context:
space:
mode:
Diffstat (limited to 'src/backend')
-rw-r--r--src/backend/catalog/heap.c20
-rw-r--r--src/backend/catalog/pg_constraint.c15
-rw-r--r--src/backend/commands/tablecmds.c17
3 files changed, 41 insertions, 11 deletions
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index f0278b9c017..136cc42a92f 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -2549,13 +2549,23 @@ AddRelationNewConstraints(Relation rel,
/*
* If the column already has an inheritable not-null constraint,
- * we need only adjust its inheritance status and we're done. If
- * the constraint is there but marked NO INHERIT, then it is
- * updated in the same way, but we also recurse to the children
- * (if any) to add the constraint there as well.
+ * we need only adjust its coninhcount and we're done. In certain
+ * cases (see below), if the constraint is there but marked NO
+ * INHERIT, then we mark it as no longer such and coninhcount
+ * updated, plus we must also recurse to the children (if any) to
+ * add the constraint there.
+ *
+ * We only allow the inheritability status to change during binary
+ * upgrade (where it's used to add the not-null constraints for
+ * children of tables with primary keys), or when we're recursing
+ * processing a table down an inheritance hierarchy; directly
+ * allowing a constraint to change from NO INHERIT to INHERIT
+ * during ALTER TABLE ADD CONSTRAINT would be far too surprising
+ * behavior.
*/
existing = AdjustNotNullInheritance1(RelationGetRelid(rel), colnum,
- cdef->inhcount, cdef->is_no_inherit);
+ cdef->inhcount, cdef->is_no_inherit,
+ IsBinaryUpgrade || allow_merge);
if (existing == 1)
continue; /* all done! */
else if (existing == -1)
diff --git a/src/backend/catalog/pg_constraint.c b/src/backend/catalog/pg_constraint.c
index aaf3537d3f5..6b8496e0850 100644
--- a/src/backend/catalog/pg_constraint.c
+++ b/src/backend/catalog/pg_constraint.c
@@ -721,7 +721,7 @@ extractNotNullColumn(HeapTuple constrTup)
*/
int
AdjustNotNullInheritance1(Oid relid, AttrNumber attnum, int count,
- bool is_no_inherit)
+ bool is_no_inherit, bool allow_noinherit_change)
{
HeapTuple tup;
@@ -744,16 +744,23 @@ AdjustNotNullInheritance1(Oid relid, AttrNumber attnum, int count,
if (is_no_inherit && !conform->connoinherit)
ereport(ERROR,
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
- errmsg("cannot change NO INHERIT status of inherited NOT NULL constraint \"%s\" on relation \"%s\"",
+ errmsg("cannot change NO INHERIT status of NOT NULL constraint \"%s\" on relation \"%s\"",
NameStr(conform->conname), get_rel_name(relid)));
/*
* If the constraint already exists in this relation but it's marked
- * NO INHERIT, we can just remove that flag, and instruct caller to
- * recurse to add the constraint to children.
+ * NO INHERIT, we can just remove that flag (provided caller allows
+ * such a change), and instruct caller to recurse to add the
+ * constraint to children.
*/
if (!is_no_inherit && conform->connoinherit)
{
+ if (!allow_noinherit_change)
+ ereport(ERROR,
+ errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+ errmsg("cannot change NO INHERIT status of NOT NULL constraint \"%s\" in relation \"%s\"",
+ NameStr(conform->conname), get_rel_name(relid)));
+
conform->connoinherit = false;
retval = -1; /* caller must add constraint on child rels */
}
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 08c87e60293..c31783373f0 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -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 */
@@ -7893,6 +7895,17 @@ ATExecSetNotNull(List **wqueue, Relation rel, char *conName, char *colName,
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
* coninhcount; if not, set conislocal if not already set.