{"id":28439083,"url":"https://github.com/ambitioneng/django-pgtransaction","last_synced_at":"2025-10-12T06:47:16.979Z","repository":{"id":59780696,"uuid":"536628273","full_name":"AmbitionEng/django-pgtransaction","owner":"AmbitionEng","description":"A context manager/decorator which extends Django's atomic function with the ability to set isolation level and retries for a given transaction.","archived":false,"fork":false,"pushed_at":"2025-05-28T09:43:21.000Z","size":277,"stargazers_count":59,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-09-27T18:02:02.420Z","etag":null,"topics":["django","postgresql","transactions"],"latest_commit_sha":null,"homepage":"https://django-pgtransaction.readthedocs.io","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AmbitionEng.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-09-14T14:52:26.000Z","updated_at":"2025-05-28T09:43:25.000Z","dependencies_parsed_at":"2024-04-24T03:32:46.023Z","dependency_job_id":"7af13d19-2d6c-452b-97cc-470dd60551ee","html_url":"https://github.com/AmbitionEng/django-pgtransaction","commit_stats":{"total_commits":3,"total_committers":3,"mean_commits":1.0,"dds":0.6666666666666667,"last_synced_commit":"a5ed9a64cf2e10bc81b23fc4f0e76e6b17cac47e"},"previous_names":["ambitioneng/django-pgtransaction","opus10/django-pgtransaction"],"tags_count":13,"template":false,"template_full_name":null,"purl":"pkg:github/AmbitionEng/django-pgtransaction","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AmbitionEng%2Fdjango-pgtransaction","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AmbitionEng%2Fdjango-pgtransaction/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AmbitionEng%2Fdjango-pgtransaction/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AmbitionEng%2Fdjango-pgtransaction/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AmbitionEng","download_url":"https://codeload.github.com/AmbitionEng/django-pgtransaction/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AmbitionEng%2Fdjango-pgtransaction/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279010528,"owners_count":26084759,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-12T02:00:06.719Z","response_time":53,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["django","postgresql","transactions"],"created_at":"2025-06-06T02:06:39.679Z","updated_at":"2025-10-12T06:47:16.958Z","avatar_url":"https://github.com/AmbitionEng.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# django-pgtransaction\n\ndjango-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.\n\nAt present, django-pgtransaction offers an extension of the `django.db.transaction.atomic` context manager/decorator which allows one to dynamically set [transaction characteristics](https://www.postgresql.org/docs/current/sql-set-transaction.html) including:\n- [Isolation level](https://www.postgresql.org/docs/current/transaction-iso.html)\n- Read mode (READ WRITE/READ ONLY)\n- Deferrability (DEFERRABLE/NOT DEFERRABLE)\n- Retry policy for Postgres locking exceptions\n\nSee the quickstart below or [the docs](https://django-pgtransaction.readthedocs.io/) for examples.\n\n## Quickstart\n\nAfter installation, set transaction characteristics using `pgtransaction.atomic`:\n\n### Isolation Levels\n\nSet the isolation level for specific consistency guarantees:\n\n```python\nimport pgtransaction\n\nwith pgtransaction.atomic(isolation_level=pgtransaction.SERIALIZABLE):\n    # Do queries with SERIALIZABLE isolation...\n```\n\nThere 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\".\n\n### Read-Only Transactions\n\nRead-only mode can be used queries that don't modify data:\n\n```python\nwith pgtransaction.atomic(read_mode=pgtransaction.READ_ONLY):\n    # Can only read, not write\n    results = MyModel.objects.all()\n```\n\n### Deferrable Transactions\n\nPrevent serialization failures for long-running queries by blocking:\n\n```python\nwith pgtransaction.atomic(\n    isolation_level=pgtransaction.SERIALIZABLE,\n    read_mode=pgtransaction.READ_ONLY,\n    deferrable=pgtransaction.DEFERRABLE\n):\n    # Long-running read-only query that won't cause serialization conflicts\n    analytics_data = expensive_query()\n```\n\nNote: `DEFERRABLE` only works with `SERIALIZABLE` isolation level and `READ_ONLY` mode.\n\n### Retries for Concurrent Updates\n\nWhen 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:\n\n```python\n@pgtransaction.atomic(isolation_level=pgtransaction.SERIALIZABLE, retry=3)\ndef do_queries():\n    # Do queries...\n```\n\nNote that the `retry` argument will not work when used as a context manager. A `RuntimeError` will be thrown.\n\nBy 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`.\n\n### Nested Usage\n\n`pgtransaction.atomic` can be nested, but keep the following in mind:\n\n1. Isolation mode cannot be changed once a query has been performed.\n2. Read-write mode can not be changed to from within a read only block.\n3. The retry argument only works on the outermost invocation as a decorator, otherwise `RuntimeError` is raised.\n\n## Compatibility\n\n`django-pgtransaction` is compatible with Python 3.9 - 3.13, Django 4.2 - 5.1, Psycopg 2 - 3, and Postgres 13 - 17.\n\n## Documentation\n\nCheck out the [Postgres docs](https://www.postgresql.org/docs/current/transaction-iso.html) to learn about transaction isolation in Postgres. \n\n[View the django-pgtransaction docs here](https://django-pgtransaction.readthedocs.io/)\n\n## Installation\n\nInstall `django-pgtransaction` with:\n\n    pip3 install django-pgtransaction\nAfter this, add `pgtransaction` to the `INSTALLED_APPS` setting of your Django project.\n\n## Contributing Guide\n\nFor information on setting up django-pgtransaction for development and contributing changes, view [CONTRIBUTING.md](CONTRIBUTING.md).\n\n## Creators\n\n- [Paul Gilmartin](https://github.com/PaulGilmartin)\n- [Wes Kendall](https://github.com/wesleykendall)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fambitioneng%2Fdjango-pgtransaction","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fambitioneng%2Fdjango-pgtransaction","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fambitioneng%2Fdjango-pgtransaction/lists"}