[1c] Rename select_for_update() to for_update() and add lock-mode variants - #87
Open
davegaeddert wants to merge 2 commits into
Open
[1c] Rename select_for_update() to for_update() and add lock-mode variants#87davegaeddert wants to merge 2 commits into
davegaeddert wants to merge 2 commits into
Conversation
Expose all four Postgres row-lock strengths as QuerySet methods: for_update(), for_no_key_update(), for_share(), and for_key_share(). Each takes the same nowait=, skip_locked=, and of= options. The lock strength is carried on the Query as the literal SQL clause (lock_mode) and emitted by the compiler, replacing the select_for_update / select_for_no_key_update boolean flags.
QuerySet.for_update() and friends now pass a constrained LockMode
token ("update", "no_key_update", "share", "key_share") instead of
raw SQL text. dialect.lock_sql() maps the token to its SQL keywords,
keeping the locking-clause spelling in one place.
davegaeddert
marked this pull request as ready for review
July 23, 2026 17:19
|
Next steps:
|
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.
Postgres exposes four row-level lock strengths, but the ORM only offered
FOR UPDATE(viaselect_for_update(), with ano_key=kwarg for the NO KEY variant). This adds all four as explicit QuerySet methods and drops the old name.API
All four take the same
nowait=,skip_locked=, andof=options. The oldselect_for_update()method and itsno_key=kwarg are removed (no alias/shim — renames are handled by the upgrade agent).nowait+skip_lockedtogether still raisesValueError; using any lock outside a transaction still raisesTransactionManagementError. Chaining two lock modes keeps the last one.Internals
The four booleans on
Query(select_for_update,select_for_update_nowait,select_for_update_skip_locked,select_for_update_of,select_for_no_key_update) collapse into a singlelock_mode: LockMode | None(aLiteral["update", "no_key_update", "share", "key_share"]) pluslock_nowait,lock_skip_locked,lock_of. The compiler passeslock_modestraight through todialect.lock_sql(mode, ...), which maps the token to its actual SQL keywords viaLOCK_MODE_SQL— the queryset itself never carries raw SQL, only the mode token.Callers updated
plain-jobsworker claim query (.for_update(skip_locked=True))plain-oauthservertoken exchange / refresh (.for_update())update_or_createanddeletepathsTests
New
plain-postgres/tests/public/test_row_locking.py: each mode emits its clause,nowait/skip_locked/ofrender,nowait+skip_lockedraises, last-mode-wins, lock-requires-transaction, and a behavioral check that a lock works insideatomic().Docs
New "Row-level locking" section in the plain-postgres README under Transactions.