Skip to content

Commit 00d00b5

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/2038.1538335244@sss.pgh.pa.us
1 parent 08aad3c commit 00d00b5

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/backend/commands/tablecmds.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7949,8 +7949,7 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
79497949
* appropriate work queue entries. We do this before dropping because in
79507950
* the case of a FOREIGN KEY constraint, we might not yet have exclusive
79517951
* lock on the table the constraint is attached to, and we need to get
7952-
* that before dropping. It's safe because the parser won't actually look
7953-
* at the catalogs to detect the existing entry.
7952+
* that before reparsing/dropping.
79547953
*
79557954
* We can't rely on the output of deparsing to tell us which relation
79567955
* to operate on, because concurrent activity might have made the name
@@ -7966,6 +7965,7 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
79667965
Form_pg_constraint con;
79677966
Oid relid;
79687967
Oid confrelid;
7968+
char contype;
79697969
bool conislocal;
79707970

79717971
tup = SearchSysCache1(CONSTROID, ObjectIdGetDatum(oldId));
@@ -7974,6 +7974,7 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
79747974
con = (Form_pg_constraint) GETSTRUCT(tup);
79757975
relid = con->conrelid;
79767976
confrelid = con->confrelid;
7977+
contype = con->contype;
79777978
conislocal = con->conislocal;
79787979
ReleaseSysCache(tup);
79797980

@@ -7986,6 +7987,15 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
79867987
if (!conislocal)
79877988
continue;
79887989

7990+
/*
7991+
* When rebuilding an FK constraint that references the table we're
7992+
* modifying, we might not yet have any lock on the FK's table, so get
7993+
* one now. We'll need AccessExclusiveLock for the DROP CONSTRAINT
7994+
* step, so there's no value in asking for anything weaker.
7995+
*/
7996+
if (relid != tab->relid && contype == CONSTRAINT_FOREIGN)
7997+
LockRelationOid(relid, AccessExclusiveLock);
7998+
79897999
ATPostAlterTypeParse(oldId, relid, confrelid,
79908000
(char *) lfirst(def_item),
79918001
wqueue, lockmode, tab->rewrite);

0 commit comments

Comments
 (0)