-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoiding Postgres transaction deadlocks #268
Comments
There are several instances of deadlock that I have occurred for a deadlock: Conflicting updates on overlay flushThis suggest a possible conflict between two update made during the overlay flush operation, in which internally the Conflicting updates on the updateVideoRelevanceValue loopVideo relavence is update via transaction here orion/src/utils/VideoRelevanceManager.ts Line 30 in ed864f4
This is a big transaction that relies on postgres autocommit feature (i.e. transaction query is not wrapped in BEGING + COMMIT statements). I tried to add them to the and re run the synch process and I am getting weird errors like Without them appears to work smoothly but it's not reliable as I have seen the deadlock error happened on production deployment (but I haven't save the screeshot). I solution could be adding a separate table |
OverlayThe overlay feature was provided as a in memory cache to avoid constantly querying the database in order to speed up the processor synching and block processing. |
The The issue here, as I mentioned, are two: Potential conflicts in between Overlay save operation on entitiesIn the screenshot presented there are 2 TX both holding a shared lock on the
The result is that Tx1 and Tx2 are waiting for each other. Concurrency with Upgrade queryThe concurrency here is given by : What I am suggesting is that we split the two tables so that Overlay operates on any table except the relevancy score one and the Update loop query operates solely on the relevancy score. This should solve any concurrency issues between overlay and update query, which is what we are trying to solve here |
Correct, I have updated the query
I think not entirely accurate since as many transactions can hold Shared lock(read only locks) You are right that the conflict is happening b/w Overlay and Video relevance score update transactions, call them Tx1 and Tx2, respectively. Now on my attempt to explaining how adding
Now, you see that after step 4, both these transaction are waiting for each other (i.e. cyclic dependency), so the Postgres will detect that and abort one of the transaction.
This is not entirely true, we can prevent any deadlock scenario from happening in the first place. Considering above example, if the
Correct.
Yeah, this should work, but I don't think we should employ that, as this seems like an anti-pattern and this is not usually deadlock scenarios are fixed. |
Problem
From the Postgres documentation, why the deadlocks occur:
How to fix deadlocks
e.g. based on the above example, the following order of transactions execution won't cause any deadlock
Avoiding deadlocks in Orion code
For example, we know that at least two different connections are responsible for concurrent updates,
processor
that sends a bunch of update requests to DB in a single transactionvideo_relevance
scores for all videos.So taking the example of only the
video
table, when the processor submits a transaction to the DB to update all the videos (updates caused due to processing of mappings overN
blocks), we need to ensure that both transactions - the processor transaction and the video relevance transaction - has the video entities updates in the ascending order of their IDs.The text was updated successfully, but these errors were encountered: