Description
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:
SELECT max(version) AS max_version FROM ops WHERE collection = $1 AND 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!