Repository navigation
Conversation
Locking a missing row locks nothing on PostgreSQL and only a gap on MySQL, so concurrent increments or unless_exist writes creating the same key both saw nil. On PostgreSQL the second upsert overwrote the first and an increment was lost; on MySQL the two gap locks deadlocked and the failsafe returned nil. When the locked read finds no row, that transaction now ends, which also releases MySQL's gap lock. A second transaction inserts a placeholder, skipping on conflict, then locks the row. A concurrent creator waits on the unique index and computes from the committed value. If the block writes nothing, the placeholder is deleted. Existing rows keep the single transaction path.
Before Rails 8, SQLite transactions are deferred, so a writer that waited on another raises BusyException instead of retrying. The unpatched lock_and_write fails the same way there.
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.
Locking a missing row locks nothing on PostgreSQL and only a gap on MySQL, so concurrent increments or unless_exist writes creating the same key both saw nil. On PostgreSQL the second upsert overwrote the first and an increment was lost; on MySQL the two gap locks deadlocked and the failsafe returned nil.
When the locked read finds no row, that transaction now ends, which also releases MySQL's gap lock. A second transaction inserts a placeholder, skipping on conflict, then locks the row. A concurrent creator waits on the unique index and computes from the committed value. If the block writes nothing, the placeholder is deleted. Existing rows keep the single transaction path.
PS: we should probably drop 7.2 support .