Registry / database / django-pglock

django-pglock

JSON →
library1.8.0pypypi✓ verified 24d ago

django-pglock provides utilities for managing PostgreSQL locks within Django applications, including advisory locks, table locks, and tools for monitoring and managing blocking locks. It is actively maintained, with regular updates to support the latest versions of Python, Django, and PostgreSQL.

pip install django-pglock
INSTALL
IMPORT
SIG · DJANGO-PGLOCK
D
django-pglock
databasepythonv1.8.0
Install
3.6s avg
Import
Disk
66MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.8.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 66.6MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 3.6s · import 0.000s · 67MB
66MB installed
● package 66MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

advisory
from pglock import advisory
from pglock import advisory

Demonstrates how to use `pglock.advisory` as a context manager to ensure only one instance of a critical section runs at a time. The lock ID can be any string, which `django-pglock` hashes to a 64-bit integer for PostgreSQL.

import pglock def my_exclusive_task(): # Simulate a task that should only run one instance at a time print("Attempting to acquire lock...") with pglock.advisory("my_critical_section_lock") as acquired: if acquired: print("Lock acquired! Running critical section...") # Your critical section code here import time time.sleep(2) print("Critical section complete.") else: print("Could not acquire lock, another instance is running.") if __name__ == "__main__": # In a real Django app, this would run within a Django context # For quickstart, we just call the function directly. # You'd typically configure Django and a database connection first. # To test, run this script twice rapidly. my_exclusive_task()
Debug
Known issues
breakingVersion 1.8.0 dropped support for Python 3.9 and older Django/PostgreSQL versions, while adding support for newer ones (Python 3.14, Django 6.0, Postgres 18). Earlier versions also dropped support for older Python/Django versions.
fix
Upgrade your Python, Django, and PostgreSQL versions to match the library's requirements, or pin `django-pglock` to an older compatible version.
affects: >=1.8.0 (Python 3.9 dropped), >=1.7.0 (Python 3.8 dropped), >=1.6.0 (Django 3.2 dropped)
gotchaAdvisory locks can be either session-level or transaction-level. By default, `pglock.advisory` provides a session-level lock that persists until the database session disconnects. For transaction-level locks, which are released when the transaction terminates, you must explicitly set `xact=True`.
fix
For transaction-level locks, use `pglock.advisory(lock_id, xact=True)`. Understand the implications: session locks can persist longer than expected if not explicitly managed, especially in connection-pooling scenarios.
affects: All versions
gotchaWhen using `pglock.model` to lock an entire table, it must always be executed within a database transaction. The lock is held for the duration of that transaction.
fix
Wrap `pglock.model` calls within `django.db.transaction.atomic()` or similar transaction management. For example: `with transaction.atomic(): pglock.model('app_label.ModelName')`.
affects: All versions
gotchaAdvisory lock IDs are global across the entire PostgreSQL database. Ensure you use unique and descriptive lock IDs (e.g., namespaced strings) to avoid unintended conflicts with other parts of your application or other applications using the same database.
fix
Use a consistent naming convention for lock IDs, such as `app_name.module_name.function_name` or `app_name.resource_id.operation`.
affects: All versions
gotchaExplicit locking, especially with `pglock.model`, can increase the likelihood of deadlocks if not managed carefully. Ensure consistent lock acquisition order across concurrent operations.
fix
Design your locking strategy to acquire locks in a consistent order whenever multiple resources are involved. Use `timeout` and `side_effect` arguments to handle contention gracefully, such as raising an error (`pglock.Raise`) or skipping execution (`pglock.Skip`) rather than waiting indefinitely.
affects: All versions
Upgrade
Version history
1.8.0latest on PyPI · released Nov 30, 2025
Audit
Dependencies
DjangorequiredCore framework dependency; the library integrates directly with Django's ORM and database connections.
psycopg2-binaryoptionalPostgreSQL adapter for Python; required for Django to communicate with PostgreSQL.
Agent activity
28 hits · last 30 days
node
24
Resources