Skip to content

Commit 906f27d

Browse files
committed
Make DROP INDEX lock the parent table before locking the index. This behavior
is necessary to avoid deadlock against ordinary queries, but we'd broken it with recent changes that made the DROP machinery lock the index before arriving at index_drop. Per intermittent buildfarm failures.
1 parent 71ff461 commit 906f27d

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/backend/commands/tablecmds.c

+23-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.257 2008/06/15 01:25:53 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.258 2008/06/15 16:29:05 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -676,6 +676,28 @@ RemoveRelations(DropStmt *drop)
676676
continue;
677677
}
678678

679+
/*
680+
* In DROP INDEX, attempt to acquire lock on the parent table before
681+
* locking the index. index_drop() will need this anyway, and since
682+
* regular queries lock tables before their indexes, we risk deadlock
683+
* if we do it the other way around. No error if we don't find a
684+
* pg_index entry, though --- that most likely means it isn't an
685+
* index, and we'll fail below.
686+
*/
687+
if (relkind == RELKIND_INDEX)
688+
{
689+
tuple = SearchSysCache(INDEXRELID,
690+
ObjectIdGetDatum(relOid),
691+
0, 0, 0);
692+
if (HeapTupleIsValid(tuple))
693+
{
694+
Form_pg_index index = (Form_pg_index) GETSTRUCT(tuple);
695+
696+
LockRelationOid(index->indrelid, AccessExclusiveLock);
697+
ReleaseSysCache(tuple);
698+
}
699+
}
700+
679701
/* Get the lock before trying to fetch the syscache entry */
680702
LockRelationOid(relOid, AccessExclusiveLock);
681703

0 commit comments

Comments
 (0)