Skip to content

Commit e27453b

Browse files
committed
Fix ALTER COLUMN TYPE to not open a relation without any lock.
If the column being modified is referenced by a foreign key constraint of another table, ALTER TABLE would open the other table (to re-parse the constraint's definition) without having first obtained a lock on it. This was evidently intentional, but that doesn't mean it's really safe. It's especially not safe in 9.3, which pre-dates use of MVCC scans for catalog reads, but even in current releases it doesn't seem like a good idea. We know we'll need AccessExclusiveLock shortly to drop the obsoleted constraint, so just get that a little sooner to close the hole. Per testing with a patch that complains if we open a relation without holding any lock on it. I don't plan to back-patch that patch, but we should close the holes it identifies in all supported branches. Discussion: https://postgr.es/m/[email protected]
1 parent a6949ca commit e27453b

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/backend/commands/tablecmds.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9888,8 +9888,7 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
98889888
* appropriate work queue entries. We do this before dropping because in
98899889
* the case of a FOREIGN KEY constraint, we might not yet have exclusive
98909890
* lock on the table the constraint is attached to, and we need to get
9891-
* that before dropping. It's safe because the parser won't actually look
9892-
* at the catalogs to detect the existing entry.
9891+
* that before reparsing/dropping.
98939892
*
98949893
* We can't rely on the output of deparsing to tell us which relation to
98959894
* operate on, because concurrent activity might have made the name
@@ -9905,6 +9904,7 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
99059904
Form_pg_constraint con;
99069905
Oid relid;
99079906
Oid confrelid;
9907+
char contype;
99089908
bool conislocal;
99099909

99109910
tup = SearchSysCache1(CONSTROID, ObjectIdGetDatum(oldId));
@@ -9921,10 +9921,11 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
99219921
elog(ERROR, "could not identify relation associated with constraint %u", oldId);
99229922
}
99239923
confrelid = con->confrelid;
9924+
contype = con->contype;
99249925
conislocal = con->conislocal;
99259926
ReleaseSysCache(tup);
99269927

9927-
ObjectAddressSet(obj, ConstraintRelationId, lfirst_oid(oid_item));
9928+
ObjectAddressSet(obj, ConstraintRelationId, oldId);
99289929
add_exact_object_address(&obj, objects);
99299930

99309931
/*
@@ -9936,6 +9937,15 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
99369937
if (!conislocal)
99379938
continue;
99389939

9940+
/*
9941+
* When rebuilding an FK constraint that references the table we're
9942+
* modifying, we might not yet have any lock on the FK's table, so get
9943+
* one now. We'll need AccessExclusiveLock for the DROP CONSTRAINT
9944+
* step, so there's no value in asking for anything weaker.
9945+
*/
9946+
if (relid != tab->relid && contype == CONSTRAINT_FOREIGN)
9947+
LockRelationOid(relid, AccessExclusiveLock);
9948+
99399949
ATPostAlterTypeParse(oldId, relid, confrelid,
99409950
(char *) lfirst(def_item),
99419951
wqueue, lockmode, tab->rewrite);
@@ -9951,7 +9961,7 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
99519961
(char *) lfirst(def_item),
99529962
wqueue, lockmode, tab->rewrite);
99539963

9954-
ObjectAddressSet(obj, RelationRelationId, lfirst_oid(oid_item));
9964+
ObjectAddressSet(obj, RelationRelationId, oldId);
99559965
add_exact_object_address(&obj, objects);
99569966
}
99579967

0 commit comments

Comments
 (0)