aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/backend/commands/tablecmds.c18
1 files changed, 14 insertions, 4 deletions
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 955d8a630c2..b0cbf2e1378 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -9857,8 +9857,7 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
* appropriate work queue entries. We do this before dropping because in
* the case of a FOREIGN KEY constraint, we might not yet have exclusive
* lock on the table the constraint is attached to, and we need to get
- * that before dropping. It's safe because the parser won't actually look
- * at the catalogs to detect the existing entry.
+ * that before reparsing/dropping.
*
* We can't rely on the output of deparsing to tell us which relation to
* operate on, because concurrent activity might have made the name
@@ -9874,6 +9873,7 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
Form_pg_constraint con;
Oid relid;
Oid confrelid;
+ char contype;
bool conislocal;
tup = SearchSysCache1(CONSTROID, ObjectIdGetDatum(oldId));
@@ -9890,10 +9890,11 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
elog(ERROR, "could not identify relation associated with constraint %u", oldId);
}
confrelid = con->confrelid;
+ contype = con->contype;
conislocal = con->conislocal;
ReleaseSysCache(tup);
- ObjectAddressSet(obj, ConstraintRelationId, lfirst_oid(oid_item));
+ ObjectAddressSet(obj, ConstraintRelationId, oldId);
add_exact_object_address(&obj, objects);
/*
@@ -9905,6 +9906,15 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
if (!conislocal)
continue;
+ /*
+ * When rebuilding an FK constraint that references the table we're
+ * modifying, we might not yet have any lock on the FK's table, so get
+ * one now. We'll need AccessExclusiveLock for the DROP CONSTRAINT
+ * step, so there's no value in asking for anything weaker.
+ */
+ if (relid != tab->relid && contype == CONSTRAINT_FOREIGN)
+ LockRelationOid(relid, AccessExclusiveLock);
+
ATPostAlterTypeParse(oldId, relid, confrelid,
(char *) lfirst(def_item),
wqueue, lockmode, tab->rewrite);
@@ -9920,7 +9930,7 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
(char *) lfirst(def_item),
wqueue, lockmode, tab->rewrite);
- ObjectAddressSet(obj, RelationRelationId, lfirst_oid(oid_item));
+ ObjectAddressSet(obj, RelationRelationId, oldId);
add_exact_object_address(&obj, objects);
}