-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add for_update clause * Skip testing for_update for SQLite * Fix typos in exception messages * Rename method to 'lock_for' * Format 'test_select' * wip doc changes * rename `lock_for` to `lock_rows` --------- Co-authored-by: Denis Kopitsa <[email protected]>
- Loading branch information
1 parent
b17e335
commit d3d0d30
Showing
8 changed files
with
286 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
.. _lock_rows: | ||
|
||
lock_rows | ||
========= | ||
|
||
You can use the ``lock_rows`` clause with the following queries: | ||
|
||
* :ref:`Objects` | ||
* :ref:`Select` | ||
|
||
It returns a query that locks rows until the end of the transaction, generating a ``SELECT ... FOR UPDATE`` SQL statement or similar with other lock strengths. | ||
|
||
.. note:: Postgres and CockroachDB only. | ||
|
||
------------------------------------------------------------------------------- | ||
|
||
Basic Usage | ||
----------- | ||
|
||
Basic usage without parameters: | ||
|
||
.. code-block:: python | ||
await Band.select(Band.name == 'Pythonistas').lock_rows() | ||
Equivalent to: | ||
|
||
.. code-block:: sql | ||
SELECT ... FOR UPDATE | ||
lock_strength | ||
------------- | ||
|
||
The parameter ``lock_strength`` controls the strength of the row lock when performing an operation in PostgreSQL. | ||
The value can be a predefined constant from the ``LockStrength`` enum or one of the following strings (case-insensitive): | ||
|
||
* ``UPDATE`` (default): Acquires an exclusive lock on the selected rows, preventing other transactions from modifying or locking them until the current transaction is complete. | ||
* ``NO KEY UPDATE`` (Postgres only): Similar to ``UPDATE``, but allows other transactions to insert or delete rows that do not affect the primary key or unique constraints. | ||
* ``KEY SHARE`` (Postgres only): Permits other transactions to acquire key-share or share locks, allowing non-key modifications while preventing updates or deletes. | ||
* ``SHARE``: Acquires a shared lock, allowing other transactions to read the rows but not modify or lock them. | ||
|
||
You can specify a different lock strength: | ||
|
||
.. code-block:: python | ||
await Band.select(Band.name == 'Pythonistas').lock_rows('SHARE') | ||
Which is equivalent to: | ||
|
||
.. code-block:: sql | ||
SELECT ... FOR SHARE | ||
nowait | ||
------ | ||
|
||
If another transaction has already acquired a lock on one or more selected rows, an exception will be raised instead of | ||
waiting for the other transaction to release the lock. | ||
|
||
.. code-block:: python | ||
await Band.select(Band.name == 'Pythonistas').lock_rows('UPDATE', nowait=True) | ||
skip_locked | ||
----------- | ||
|
||
Ignore locked rows. | ||
|
||
.. code-block:: python | ||
await Band.select(Band.name == 'Pythonistas').lock_rows('UPDATE', skip_locked=True) | ||
of | ||
-- | ||
|
||
By default, if there are many tables in a query (e.g. when joining), all tables will be locked. | ||
Using ``of``, you can specify which tables should be locked. | ||
|
||
.. code-block:: python | ||
await Band.select().where(Band.manager.name == 'Guido').lock_rows('UPDATE', of=(Band, )) | ||
Learn more | ||
---------- | ||
|
||
* `Postgres docs <https://www.postgresql.org/docs/current/sql-select.html#SQL-FOR-UPDATE-SHARE>`_ | ||
* `CockroachDB docs <https://www.cockroachlabs.com/docs/stable/select-for-update#lock-strengths>`_ |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -377,6 +377,11 @@ limit | |
|
||
See :ref:`limit`. | ||
|
||
lock_rows | ||
~~~~~~~~ | ||
|
||
See :ref:`lock_rows`. | ||
|
||
offset | ||
~~~~~~ | ||
|
||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -379,6 +379,12 @@ limit | |
|
||
See :ref:`limit`. | ||
|
||
|
||
lock_rows | ||
~~~~~~~~ | ||
|
||
See :ref:`lock_rows`. | ||
|
||
offset | ||
~~~~~~ | ||
|
||
|
This file contains 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
This file contains 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
This file contains 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
This file contains 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