Repository navigation
fix: a blocking detach locks the parent first, so a reader cannot deadlock it - #87
Merged
Merged
Conversation
…dlock it The blocking detach took ACCESS EXCLUSIVE on the partition and then waited for the parent in DETACH. A query through the parent locks the parent and then its partitions, so a reader that got the parent in between waited for the partition while the detach waited for the parent, and PostgreSQL ended one of them with a deadlock. Retention on a table with a DEFAULT partition takes this path every time, and with steady reads the deadlock was likely, not rare. The detach now takes ACCESS EXCLUSIVE on the parent alone first, the lock DETACH takes anyway, then the partition: the order readers and PostgreSQL's own DETACH use.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Found while measuring a pg-partsmith recipe for Outpost's docs: retention under a live reader failed with
deadlock detected.The blocking detach took
ACCESS EXCLUSIVEon the partition (_lock_partition, for the identity check and the marker) and only then asked for the parent inDETACH. A query through the parent locks the parent and then its partitions. A reader that got the parent in between waits for the partition, the detach waits for the parent, and afterdeadlock_timeoutPostgreSQL fails one of them. Retention on a table with a DEFAULT partition always takes the blocking path, sinceDETACH CONCURRENTLYis refused there, so any table like Outpost's hits it under steady reads.The detach now takes
ACCESS EXCLUSIVEon the parent alone (LOCK TABLE ONLY) first, the lockDETACHtakes anyway, and then the partition. That is the order readers use and the order PostgreSQL's ownDETACHuses, and the order the attach path already follows (_lock_for_attach). The parent's lock is held a few catalog queries longer than before; readers and writers queue behind it exactly as they did behind theDETACH.The test holds the parent with a reader that has yet to reach the partition, starts a blocking detach, waits until it is queued, then lets the reader read the partition and commit. Before the fix the detach fails with
DeadlockDetectedError; after it, both finish.