django-pglock
Postgres locking routines and lock table access.
What it is and what it does
django-pglock wraps Postgres's native locking mechanisms (advisory locks and table locks) as Django utilities, letting you prevent concurrent execution of code sections and manage lock contention. It provides decorators and context managers for application-level synchronization—useful for ensuring background tasks don't overlap, locking entire models during transactions, and forcibly terminating blocking locks to prioritize critical code like migrations.
The package depends on django and django-pgactivity and exposes a management command for querying and killing locks from the CLI. It supports Python 3.10–3.14 and Django 4.2–6.0, with compatibility across Postgres 14–18 and both Psycopg 2 and 3.
Use it for:
- Prevent duplicate execution of periodic background tasks or Celery jobs by wrapping them in advisory locks.
- Lock an entire model during a transaction to ensure exclusive access for critical data updates.
- Dynamically set Postgres lock timeouts to fail fast when lock acquisition takes too long.
- Terminate blocking locks from migrations or high-priority code to avoid deadlock situations.
- Query active and blocked locks via the PGLock and BlockedPGLock proxy models for monitoring.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Provides Postgres advisory locks, table locks, and lock management utilities for Django applications to prevent concurrent task execution and handle blocking locks.
Yes, if you use Django with Postgres and need application-level locking or lock management. The low install friction, permissive license, and zero known vulnerabilities make it safe to adopt. The aging maintenance status (257 days since last release) is a minor concern but not a blocker—the repository is active and the package is stable for current Django and Python versions.
Install
django-pglock on PyPI
pip
pip install django-pglockuv
uv add django-pglockpoetry
poetry add django-pglockInstalling django-pglock
Before you install
Low install friction with a pure Python wheel. Maintenance status is aging—last release was 257 days ago—but the repository remains active with recent commits and no archived status.
License in practice
BSD-3-Clause is permissive; you can use, modify, and distribute this package freely in commercial and private projects with minimal restrictions.
Quickstart
pip install django-pglock
import pglock
with pglock.advisory("my_lock_id"):
# Code here runs exclusively, blocking until lock is available
Requires Django 4.2–6.0, Postgres 14–18, and Psycopg 2–3. Must add 'pgactivity' and 'pglock' to Django's INSTALLED_APPS.
Verify before relying
- Whether the aging maintenance status (257 days since last release) affects stability for new Django/Python versions.
- Real-world performance impact of lock acquisition under high concurrency.
- Whether django-pgactivity dependency introduces additional constraints or maintenance risk.
Package facts
| License | BSD-3-Clause (permissive) |
| Python support | supports the current Python release (<4,>=3.10.0) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 2 — django, django-pgactivity |
| Maintenance | aging — 257 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 1,425,714/month — #3,916 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: django_pglock-1.8.0-py3-none-any.whl
Tags
More Database packages
psycopg2-binary is a PostgreSQL database…
copyleft · top 1,000 on PyPI
redisPython client library for connecting to and…
permissive · top 1,000 on PyPI
ydbYDB Python SDK is the official client library…
permissive · top 1,000 on PyPI
snowflake-connector-pythonConnects Python applications to Snowflake data…
permissive · top 1,000 on PyPI
sqlparsesqlparse tokenizes SQL text into a tree of…
permissive · top 1,000 on PyPI
dbt-adaptersProvides base adapter protocols and shared…
permissive · top 1,000 on PyPI
django-concurrencyAdds optimistic locking to Django models to…
permissive · top 15,000 on PyPI
django-pgactivityProvides Django management commands and…
permissive · top 5,000 on PyPI
django-pglocksProvides PostgreSQL advisory locks for Django…
permissive · top 15,000 on PyPI
django-pgmigratePrevents migration downtime by detecting and…
permissive · top 5,000 on PyPI
django-pg-zero-downtime-migrationsProvides a Django database backend for…
permissive · top 15,000 on PyPI
django-pg-migration-toolsProvides safer, production-ready Django…
permissive · top 15,000 on PyPI
PyMySQLLockProvides distributed locking primitives backed…
permissive · top 15,000 on PyPI
filelockProvides a platform-independent file locking…
permissive · top 100 on PyPI
python-dynamodb-lockProvides distributed locking on top of DynamoDB…
permissive · top 15,000 on PyPI
celery_oncePrevents duplicate execution and queuing of…
permissive · top 15,000 on PyPI