Avoiding a PostgreSQL DROP TABLE deadlock by taking LOCK TABLE NOWAIT in a Flyway beforeEachMigrate callback
A migration that only dropped an old table deadlocked in production twice and rolled back. DROP TABLE also takes the strongest lock on the tables its foreign keys reference, and a running worker read those tables in the opposite order. After comparing lock_timeout and plain reruns, I left the applied migration untouched and took all the needed locks up front with NOWAIT in a Flyway beforeEachMigrate callback.