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 psycopg.errors.SerializationError or psycopg.errors.DeadlockDetected errors are raised. Configure retried psycopg 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.

Compatibility

django-pgtransaction is compatible with Python 3.9 - 3.13, Django 4.2 - 5.1, Psycopg 2 - 3, and Postgres 13 - 17.

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

Creators

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.5.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

django_pgtransaction-1.5.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file django_pgtransaction-1.5.0.tar.gz.

File metadata

  • Download URL: django_pgtransaction-1.5.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.7 Linux/5.15.0-1057-aws

File hashes

Hashes for django_pgtransaction-1.5.0.tar.gz
Algorithm Hash digest
SHA256 39852edd7b1c4fd3cfaf9145e1f2c89f9817c2e45f599d05aba1ee162f79b5cf
MD5 14c13fb1b6e0d3e8a0e4a0b51aab1e1d
BLAKE2b-256 fd4cf6625398113abc1118ede19377aa7c6d6c0ea965f805dd4b1dfea90d2d7f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_pgtransaction-1.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a09e2db90084bd8d616821b91ac0430017b9e89ef74d42067ce772307262fc60
MD5 d5ec5c0475174d9ff829919ab4eacd98
BLAKE2b-256 424dc392a0f1a2e8f72f2c8dc2fa6a570cdba18edba68be1f4b5c54173865dee

See more details on using hashes here.

Supported by

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