A context manager/decorator which extends Django's atomic function with the ability to set isolation level and retries for a given transaction.
Project description
django-pgtransaction
django-pgtransaction offers a drop-in replacement for the default django.db.transaction module which, when used on top of a PostgreSQL database, extends the functionality of that module with Postgres-specific features.
At present, django-pgtransaction offers an extension of the django.db.transaction.atomic context manager/decorator which allows one to dynamically set the isolation level when opening a transaction, as well as specifying a retry policy for when an operation in that transaction results in a Postgres locking exception. See the quickstart below or the docs for examples.
Quickstart
Set the isolation level of a transaction by using pgtransaction.atomic:
import pgtransaction
with pgtransaction.atomic(isolation_level=pgtransaction.SERIALIZABLE):
# Do queries...
There are three isolation levels: pgtransaction.READ_COMMITTED, pgtransaction.REPEATABLE_READ, and pgtransaction.SERIALIZABLE. By default it inherits the parent isolation level, which is Django’s default of “READ COMMITTED”.
When using stricter isolation levels like pgtransaction.SERIALIZABLE, Postgres will throw serialization errors upon concurrent updates to rows. Use the retry argument with the decorator to retry these failures:
@pgtransaction.atomic(isolation_level=pgtransaction.SERIALIZABLE, retry=3)
def do_queries():
# Do queries...
Note that the retry argument will not work when used as a context manager. A RuntimeError will be thrown.
By default, retries are only performed when psycopg2.errors.SerializationError or psycopg2.errors.DeadlockDetected errors are raised. Configure retried psycopg2 errors with settings.PGTRANSACTION_RETRY_EXCEPTIONS. You can set a default retry amount with settings.PGTRANSACTION_RETRY.
pgtransaction.atomic can be nested, but keep the following in mind:
The isolation level cannot be changed once a query has been performed.
The retry argument only works on the outermost invocation as a decorator, otherwise RuntimeError is raised.
Documentation
Check out the Postgres docs to learn about transaction isolation in Postgres.
Installation
Install django-pgtransaction with:
pip3 install django-pgtransaction
After this, add pgtransaction to the INSTALLED_APPS setting of your Django project.
Contributing Guide
For information on setting up django-pgtransaction for development and contributing changes, view CONTRIBUTING.rst.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django-pgtransaction-1.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 631a825b997a72c80dd254161986b58de62f1595e3ebe103604485145f367b8e |
|
MD5 | bcd0a3835150755a2c8bc239258b333e |
|
BLAKE2b-256 | 713e0e4e2cd856581cedcc16a074575c3c4261d7143ab90bcf8f2ba912b776b8 |
Hashes for django_pgtransaction-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4a692fd199586dc5a3c641cd905d874c8c14c36b9bc8abe7f409faf0a9f152dc |
|
MD5 | 3de1677f752cd993c80a5d2be24b4c55 |
|
BLAKE2b-256 | 09d93507cc38222eab27566037e56b8607642a4d51e852989ef07d35a7fbf762 |