Skip to main content

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:

  1. The isolation level cannot be changed once a query has been performed.

  2. 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.

View the django-pgtransaction docs here.

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.

Primary Authors

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

django-pgtransaction-1.0.0.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_pgtransaction-1.0.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file django-pgtransaction-1.0.0.tar.gz.

File metadata

  • Download URL: django-pgtransaction-1.0.0.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.0 CPython/3.10.6 Linux/5.13.0-1023-aws

File hashes

Hashes for django-pgtransaction-1.0.0.tar.gz
Algorithm Hash digest
SHA256 631a825b997a72c80dd254161986b58de62f1595e3ebe103604485145f367b8e
MD5 bcd0a3835150755a2c8bc239258b333e
BLAKE2b-256 713e0e4e2cd856581cedcc16a074575c3c4261d7143ab90bcf8f2ba912b776b8

See more details on using hashes here.

File details

Details for the file django_pgtransaction-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: django_pgtransaction-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.0 CPython/3.10.6 Linux/5.13.0-1023-aws

File hashes

Hashes for django_pgtransaction-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4a692fd199586dc5a3c641cd905d874c8c14c36b9bc8abe7f409faf0a9f152dc
MD5 3de1677f752cd993c80a5d2be24b4c55
BLAKE2b-256 09d93507cc38222eab27566037e56b8607642a4d51e852989ef07d35a7fbf762

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page