skillfed

django-pglock

Postgres locking routines and lock table access.

django-pglock v1.8.0 1.4M downloads/30d#3,916 on PyPI70
Permissive license BSD-3-Clause AGING released

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-pglock

uv

uv add django-pglock

poetry

poetry add django-pglock

Installing 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

Framework :: DjangoFramework :: Django :: 4.2Framework :: Django :: 5.0Framework :: Django :: 5.1Framework :: Django :: 5.2Framework :: Django :: 6.0Intended Audience :: DevelopersLicense :: OSI Approved :: BSD LicenseOperating System :: OS IndependentProgramming Language :: PythonProgramming Language :: Python :: 3Programming Language :: Python :: 3 :: OnlyProgramming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.14

Tags

django postgres lockingadvisory locks djangoprevent concurrent taskspostgres lock managementdjango database lockingtask overlap preventionblocking lock detection
postgres-lockingconcurrency-controldjango-extension

More Database packages

psycopg2-binary

psycopg2-binary is a PostgreSQL database…

copyleft · top 1,000 on PyPI

redis

Python client library for connecting to and…

permissive · top 1,000 on PyPI

ydb

YDB Python SDK is the official client library…

permissive · top 1,000 on PyPI

snowflake-connector-python

Connects Python applications to Snowflake data…

permissive · top 1,000 on PyPI

sqlparse

sqlparse tokenizes SQL text into a tree of…

permissive · top 1,000 on PyPI

dbt-adapters

Provides base adapter protocols and shared…

permissive · top 1,000 on PyPI

django-concurrency

Adds optimistic locking to Django models to…

permissive · top 15,000 on PyPI

django-pgactivity

Provides Django management commands and…

permissive · top 5,000 on PyPI

django-pglocks

Provides PostgreSQL advisory locks for Django…

permissive · top 15,000 on PyPI

django-pgmigrate

Prevents migration downtime by detecting and…

permissive · top 5,000 on PyPI

django-pg-zero-downtime-migrations

Provides a Django database backend for…

permissive · top 15,000 on PyPI

django-pg-migration-tools

Provides safer, production-ready Django…

permissive · top 15,000 on PyPI

PyMySQLLock

Provides distributed locking primitives backed…

permissive · top 15,000 on PyPI

filelock

Provides a platform-independent file locking…

permissive · top 100 on PyPI

python-dynamodb-lock

Provides distributed locking on top of DynamoDB…

permissive · top 15,000 on PyPI

celery_once

Prevents duplicate execution and queuing of…

permissive · top 15,000 on PyPI