File: locking.rst

package info (click to toggle)
python-django-postgres-extra 2.0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,096 kB
  • sloc: python: 9,057; makefile: 17; sh: 7; sql: 1
file content (56 lines) | stat: -rw-r--r-- 1,899 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
.. include:: ./snippets/postgres_doc_links.rst

.. _locking_page:

Locking
=======

`Explicit table-level locks`_ are supported through the :meth:`psqlextra.locking.postgres_lock_model` and :meth:`psqlextra.locking.postgres_lock_table` methods. All table-level lock methods are supported.

Locks are always bound to the current transaction and are released when the transaction is committed or rolled back. There is no support (in PostgreSQL) for explicitly releasing a lock.

.. warning::

    Locks are only released when the *outer* transaction commits or when a nested transaction is rolled back. You can ensure that the transaction you created is the outermost one by passing the ``durable=True`` argument to ``transaction.atomic``.

.. note::

    Use `django-pglocks <https://pypi.org/project/django-pglocks/>`_ if you need a advisory lock.

Locking a model
---------------

Use :class:`psqlextra.locking.PostgresTableLockMode` to indicate the type of lock to acquire.

.. code-block:: python

    from django.db import transaction

    from psqlextra.locking import PostgresTableLockMode, postgres_lock_table

    with transaction.atomic(durable=True):
        postgres_lock_model(MyModel, PostgresTableLockMode.EXCLUSIVE)

    # locks are released here, when the transaction committed


Locking a table
---------------

Use :meth:`psqlextra.locking.postgres_lock_table` to lock arbitrary tables in arbitrary schemas.

.. code-block:: python

    from django.db import transaction

    from psqlextra.locking import PostgresTableLockMode, postgres_lock_table

    with transaction.atomic(durable=True):
        postgres_lock_table("mytable", PostgresTableLockMode.EXCLUSIVE)
        postgres_lock_table(
            "tableinotherschema",
            PostgresTableLockMode.EXCLUSIVE,
            schema_name="myschema"
        )

    # locks are released here, when the transaction committed