Repository navigation
Spec reproducing the multi-row reserve lock on PostgreSQL (#143) - #265
Draft
dg-novicap wants to merge 1 commit into
Draft
dg-novicap wants to merge 1 commit into
dg-novicap wants to merge 1 commit into
Conversation
…ectiveidea#143) The optimized PostgreSQL reserve wraps the LIMIT 1 sub-select in `WHERE id IN (...)`. When the planner picks a Nested Loop Semi Join with the sub-select on the inner side, it re-executes the sub-select per candidate row and each execution skips the rows the UPDATE already changed, so one reserve locks the whole ready batch under one worker. The spec disables the other join strategies with SET LOCAL so the rescanning plan is chosen deterministically, asserts that plan through EXPLAIN, and then expects a single locked row after one reserve. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
dg-novicap
marked this pull request as draft
November 5, 2026 17:03
dg-novicap
commented
Nov 5, 2026
| described_class.transaction do | ||
| described_class.connection.execute( | ||
| %w[enable_hashjoin enable_mergejoin enable_material enable_hashagg enable_sort] | ||
| .map { |setting| "SET LOCAL #{setting} = off" }.join("; ") |
Author
There was a problem hiding this comment.
key idea: to disable all other strategies, and to only allow Nested Loop Semi Join, so that the query always locks more than 1 row
This branch has not been deployed
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.
Reproduction for #143 (also collectiveidea/delayed_job#915), the problem PR #177 addresses.
Problem
reserve_with_scope_using_optimized_postgresruns:When the planner executes the sub-select as the inner side of a Nested Loop Semi Join, it re-executes the sub-select for every candidate row of the outer scan.
FOR UPDATEreads the latest row versions, and each re-execution skips the rows this sameUPDATEalready changed (theirlocked_atis no longer NULL), so theLIMIT 1moves to the next ready row every time. One reserve then locks the whole ready batch under one worker name, andfind_by_sql(...).firstsilently discards the rest. Postgres considers this query shape unspecified rather than a bug (pgsql-bugs #15715, Tom Lane's reply).Which plan the planner picks depends on table statistics, so it is intermittent in practice. A high-churn
delayed_jobstable with stalereltuplesis enough. We hit it in production on PostgreSQL 14 with delayed_job_active_record 4.1.11: one reserve locked nine ready jobs at once, visible in the identicallocked_atandlocked_byon all nine rows.The test
spec/delayed/backend/postgresql_reserve_spec.rbmakes the plan deterministic by disabling every other join strategy for the example's transaction:EXPLAINof the reserve statement containsNested Loop Semi Joinunder those settings, so the reproduction cannot pass vacuously if a future planner stops choosing that plan.Delayed::Job.reserve(worker)once, and expects one row withlocked_by = worker.name.On current master, against PostgreSQL 14.20:
The spec skips on every other adapter. It is expected to fail on the PostgreSQL CI jobs until the fix below is applied.
Fix (tested, not included in this PR so the failing test stays visible)
A scalar sub-select is planned as an InitPlan and evaluated exactly once. If it ever returned more than one row, Postgres raises
more than one row returned by a subquery used as an expressioninstead of taking silent extra locks.SKIP LOCKEDis unchanged.With that one-token change applied locally:
EXPLAINshowsInitPlan 1withFilter: (id = $2)on the outer scan;PR #177 reaches the same single-evaluation guarantee with a CTE. On PostgreSQL 12 and later a CTE needs
AS MATERIALIZEDto keep that guarantee; the scalar sub-select needs no version-specific syntax. Happy to push the fix as a second commit on this PR if you prefer it bundled.🤖 Generated with Claude Code