Skip to content

Commit 2b3a026

Browse files
committed
Merge pull request #56 from amitlan/issue-55-fix
Make repack_drop() processing robust against deadlocks.
2 parents 87dca1c + cbe0272 commit 2b3a026

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

lib/repack.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,28 @@ repack_drop(PG_FUNCTION_ARGS)
949949
/* connect to SPI manager */
950950
repack_init();
951951

952+
/*
953+
* To prevent concurrent lockers of the repack target table from causing
954+
* deadlocks, take an exclusive lock on it. Consider that the following
955+
* commands take exclusive lock on tables log_xxx and the target table
956+
* itself when deleting the z_repack_trigger on it, while concurrent
957+
* updaters require row exclusive lock on the target table and in
958+
* addition, on the log_xxx table, because of the trigger.
959+
*
960+
* Consider how a deadlock could occur - if the DROP TABLE repack.log_%u
961+
* gets a lock on log_%u table before a concurrent updater could get it
962+
* but after the updater has obtained a lock on the target table, the
963+
* subsequent DROP TRIGGER ... ON target-table would report a deadlock as
964+
* it finds itself waiting for a lock on target-table held by the updater,
965+
* which in turn, is waiting for lock on log_%u table.
966+
*
967+
* Fixes deadlock mentioned in the Github issue #55.
968+
*/
969+
execute_with_format(
970+
SPI_OK_UTILITY,
971+
"LOCK TABLE %s.%s IN ACCESS EXCLUSIVE MODE",
972+
nspname, relname);
973+
952974
/* drop log table: must be done before dropping the pk type,
953975
* since the log table is dependent on the pk type. (That's
954976
* why we check numobj > 1 here.)

0 commit comments

Comments
 (0)