Skip to main content

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 transaction characteristics including:

  • Isolation level
  • Read mode (READ WRITE/READ ONLY)
  • Deferrability (DEFERRABLE/NOT DEFERRABLE)
  • Retry policy for Postgres locking exceptions

See the quickstart below or the docs for examples.

Quickstart

After installation, set transaction characteristics using pgtransaction.atomic:

Isolation Levels

Set the isolation level for specific consistency guarantees:

import pgtransaction

with pgtransaction.atomic(isolation_level=pgtransaction.SERIALIZABLE):
    # Do queries with SERIALIZABLE isolation...

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

Read-Only Transactions

Read-only mode can be used queries that don't modify data:

with pgtransaction.atomic(read_mode=pgtransaction.READ_ONLY):
    # Can only read, not write
    results = MyModel.objects.all()

Deferrable Transactions

Prevent serialization failures for long-running queries by blocking:

with pgtransaction.atomic(
    isolation_level=pgtransaction.SERIALIZABLE,
    read_mode=pgtransaction.READ_ONLY,
    deferrable=pgtransaction.DEFERRABLE
):
    # Long-running read-only query that won't cause serialization conflicts
    analytics_data = expensive_query()

Note: DEFERRABLE only works with SERIALIZABLE isolation level and READ_ONLY mode.

Retries for Concurrent Updates

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.

Nested Usage

pgtransaction.atomic can be nested, but keep the following in mind:

  1. Isolation mode cannot be changed once a query has been performed.
  2. Read-write mode can not be changed to from within a read only block.
  3. The retry argument only works on the outermost invocation as a decorator, otherwise RuntimeError is raised.

Compatibility

django-pgtransaction is compatible with Python 3.10 - 3.14, Django 4.2 - 6.0, Psycopg 2 - 3, and Postgres 14 - 18.

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

Metadata

Release files for django-pgtransaction 2.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-pgtransaction 2.1.0
File Size Uploaded Provenance
django_pgtransaction-2.1.0.tar.gz 7.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for django-pgtransaction 2.1.0
File Interpreter ABI Platform
django_pgtransaction-2.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 16.2 kB

Release files / django_pgtransaction-2.1.0.tar.gz

Download URL django_pgtransaction-2.1.0.tar.gz
Size 7.6 kB
Tags Source
SHA-256 checksum
How to use checksums
405005611f02d610724e5a72d26f77ac2c5b250531eb766ffe5a7603bac0c769
BLAKE2b-256 checksum
How to use checksums
c6a21d27aaab82fa8342f9c8f90fec32b4595820478caa207ccf3abdcf635821
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.14.0 Linux/6.8.0-1040-aws

Release files / django_pgtransaction-2.1.0-py3-none-any.whl

Download URL django_pgtransaction-2.1.0-py3-none-any.whl
Size 8.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
69b38505a4eac5df56a48045b9b6c7f8a4e2c34d50f55e89201cbef57d3a725b
BLAKE2b-256 checksum
How to use checksums
ab75f9d33972ddb859b210f017f0ce2b5e8904248e91e9ba2f4b71d395d9eac2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.14.0 Linux/6.8.0-1040-aws

Security for django-pgtransaction 2.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page