Repository navigation
Lock scope records without instantiating them - #75
Merged
Merged
Conversation
This was referenced Oct 16, 2026
brendon
marked this pull request as draft
November 9, 2026 02:29
brendon
force-pushed
the
perf/lock-without-loading-records
branch
2 times, most recently
from
November 20, 2026 00:42
eca96d3 to
4425cbb
Compare
When a scope has no belongs_to association, its rows were locked with lock.all.load, which builds an Active Record object for every row in the scope on every create, update and destroy. For a model with no scope, that's the whole table. Only the row locks are needed. They're now locked with reorder(primary_key).lock.ids. Ordering by primary key matters on MySQL. With plain lock.ids, MySQL can answer the query from the (scope, position) index, so a transaction waiting for the lock waits inside that index. InnoDB then stops our own UPDATEs from writing position values into the gap it's waiting on, and the two transactions deadlock. Ordering by primary key keeps those waits on the primary key. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
brendon
force-pushed
the
perf/lock-without-loading-records
branch
from
November 27, 2026 09:59
4425cbb to
38436f2
Compare
Active Record flattens the arguments to reorder in 7.2 and 8.1, so reorder(primary_key) gives the same SQL as reorder(*Array(primary_key)) for single and composite primary keys. Nothing ran this lock query with a composite primary key, so add a test that does, with a composite primary key item whose list is nil. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
brendon
marked this pull request as ready for review
November 27, 2026 22:54
brendon
added a commit
that referenced
this pull request
Nov 28, 2026
When a scope has no belongs_to association, or its parent is nil, the healer locks the scope's records with plain lock.ids. On MySQL, that deadlocks more often against saves to the same list than ordering the lock by primary key, which #75 does for positioning's own lock. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
brendon
added a commit
that referenced
this pull request
Nov 28, 2026
When a scope has no belongs_to association, or its parent is nil, the healer locks the scope's records with plain lock.ids. On MySQL, that deadlocks more often against saves to the same list than ordering the lock by primary key, which #75 does for positioning's own lock. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.
When a list has no parent row to lock, positioning locks every row in the list instead. That covers a model with no scope, a scope on plain columns, and (since #76) a list whose
belongs_toparent isnil, such as the roots of a tree. It did this withscope_of(record).lock.all.load, which runsSELECT * … FOR UPDATEand builds an Active Record object for every row in the list on every create, update and destroy. For a model with no scope, that's every row in the table. Only the row locks are needed.Fix
lock_scope_ofnow locks those rows withscope_of(record).reorder(primary_key).lock.ids, which runsSELECT id … ORDER BY id FOR UPDATE. The same rows are locked, but only their ids are fetched, so no records are built. For a composite primary key,primary_keyis an array, andreorderorders by each of its columns: Active Record 7.2 and 8.1 both flatten its arguments.The ordering is there because plain
lock.idsdeadlocks more often on MySQL:lock.idsfrom thepositionindex, and the moves test below raisedActiveRecord::Deadlockedin each of three runs. Ordering by id makes MySQL read the primary key instead.[scope, position]unique index, MySQL reads that index either way, and sorts the ids afterwards when they're ordered. Plainlock.idsstill deadlocked more often there (table below). I don't know why the sort makes the difference.Measurements
MySQL 9.7.4 and PostgreSQL 18, locally.
Which index MySQL reads to take the locks, from
EXPLAIN:mainProduct)Category,parent_id IS NULL)[parent_id, position]indexparent_idindex[scope, position]unique index[scope, position]index[scope, position]index, then sorts by idDeadlocks on MySQL, moving 10 records in one list to position 1, four at a time, 30 moves per run, three runs:
mainlock.ids[scope, position]unique indexPositions were correct after every run. Creating 21 tree roots, five at a time (the probe from #76), gave no deadlocks and no duplicate positions on either
mainor this PR.Time to take the lock, in its own transaction, for
Product(no scope), averaged over 30 runs:main, MySQLmain, PostgreSQLConsidered and dropped
1instead of the id, or counting the rows of a locking subquery, took about the same time as fetching ids: 9 to 10 ms for 10,000 rows on both databases. Nearly all of the time is the database finding and locking each row.LOCK TABLE … IN SHARE ROW EXCLUSIVE MODEtook 0.5 ms on PostgreSQL at any size. But it's database-specific, and it blocks every insert, update and delete on the table, for every list, until the transaction ends. MySQL'sLOCK TABLEScommits the open transaction, so it can't be used inside one.Who this changes things for
Only lists with no parent row to lock. The same rows are locked; they're no longer loaded. Which index MySQL reads depends on its optimizer and the app's indexes, and I've only measured MySQL 9.7.4.
Tests
main'slock.all.load, it builds 3.reorder(:id), the test fails.maintoo. It guards against the deadlock above: with plainlock.idsinstead, it raisedActiveRecord::Deadlockedon MySQL in each of three runs.Changelog: When a scope has no
belongs_toassociation, lock the records in it without loading them. Before, every create, update and destroy loaded every record in the scope (for a model with no scope, every record in the table) just to lock them.🤖 Generated with Claude Code