You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Max version check is wrong with high concurrency #1
With lots of operations coming in from multiple sites, it is possible in this adapter to commit an operation with a version that has already been committed.
Given a scenario where User A and User B concurrently submit operation version 2, the max version check here will pass for both operations, and one operation will not be rebased against the one before it.
In order to resolve this, you can move the max version check inside of your transaction and append FOR UPDATE to the max version query:
SELECTmax(version) AS max_version FROM ops WHERE collection = $1AND doc_id = $2 FOR UPDATE
This will create a lock on this row for the duration of the transaction, ensuring that the later operation's max version check query blocks until the previous operation is fully committed.
I'm not actually using this adapter because my schema is different from what sharedb-postgres assumes, but I just spent a lot of time debugging this, and hopefully it can help you, too!
@jclem I implemented row-locking in a knex port here (https://github.com/brainkim/sharedb-knex/blob/master/index.js#L35-L70). You can’t use for update in queries with aggregate functions (in postgres at least) so I moved the for update to a separate select. I have no idea what I'm doing when it comes to databases but it feels like this should fix the issue you describe, right?
:3
Activity
brainkim commentedon May 23, 2018
@jclem I implemented row-locking in a knex port here (https://github.com/brainkim/sharedb-knex/blob/master/index.js#L35-L70). You can’t use
for updatein queries with aggregate functions (in postgres at least) so I moved the for update to a separateselect. I have no idea what I'm doing when it comes to databases but it feels like this should fix the issue you describe, right?:3
jclem commentedon Jun 2, 2018
@brainkim I’m not involved with this project anymore, so I can’t offer much guidance here. If it helps, I can point you to my old company’s lib we used to commit ShareDB operations: https://github.com/usecanvas/realtime-v2/blob/master/sharedb/lib/sharedb-postgres-canvas.js
Sorry I can’t be of more help. Good luck!
Merge pull request #1 from smalllicheng/master