Skip to main content

Postgres locking routines and lock table access.

Project description

django-pglock

django-pglock performs advisory locks, table locks, and helps manage blocking locks. Here's some of the functionality at a glance:

  • pglock.advisory for application-level locking, for example, ensuring that tasks don't overlap.
  • pglock.model for locking an entire model.
  • pglock.timeout for dynamically setting the timeout to acquire a lock.
  • pglock.prioritize to kill blocking locks for critical code, such as migrations.
  • The PGLock and BlockedPGLock models for querying active and blocked locks.
  • The pglock management command that wraps the models and provides other utilities.

Quickstart

Advisory Locks

Use pglock.advisory to acquire a Postgres advisory lock:

import pglock

with pglock.advisory("my_lock_id"):
    # This code blocks until the "my_lock_id" lock is available

Above our code will block until the lock is available, meaning no instances of the function will run simultaneously. Use the timeout argument to configure how long to wait for the lock. A timeout of zero will return immediately:

with pglock.advisory("my_lock_id", timeout=0) as acquired:
    if acquired:
        # The lock is acquired

Use side_effect=pglock.Raise to raise a django.db.utils.OperationalError if the lock can't be acquired. When using the decorator, you can also use side_effect=pglock.Skip to skip the function if the lock can't be acquired:

@pglock.advisory(timeout=0, side_effect=pglock.Skip)
def non_overlapping_func():
    # This function will not run if there's another one already running.
    # The decorator lock ID defaults to <module_name>.<function_name>

Model Locks

pglock.model can take a lock on an entire model during a transaction. For example:

from django.db import transaction
import pglock

with transaction.atomic():
    pglock.model("auth.User")

    # Any operations on auth.User will be exclusive here. Even read access
    # for other transactions is blocked

pglock.model uses Postgres's LOCK statement, and it accepts the lock mode as a argument. See the Postgres docs for more information.

Note pglock.model is similar to pglock.advisory. Use the timeout argument to avoid waiting for locks, and supply the appropriate side_effect to adjust runtime behavior.

Prioritizing Blocked Code

pglock.prioritize will terminate any locks blocking the wrapped code:

import pglock

@pglock.prioritize()
def my_func():
    # Any other statements that have conflicting locks will be killed on a
    # periodic interval.
    MyModel.objects.update(val="value")

pglock.prioritize is useful for prioritizing code, such as migrations, to avoid situations where locks are held for too long.

Setting the Lock Timeout

Use pglock.timeout to dynamically set Postgres's lock_timeout runtime setting:

import pglock

@pglock.timeout(1)
def do_stuff():
    # This function will throw an exception if any code takes longer than
        # one second to acquire a lock

Querying Locks

Use pglock.models.PGLock to query active locks. It wraps Postgres's pg_locks view. Use pglock.models.BlockedPGLock to query locks and join the activity that's blocking them.

Use python manage.py pglock to view and kill locks from the command line. It has several options for dynamic filters and re-usable configuration.

Compatibility

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

Documentation

View the django-pglock docs here to learn more about:

  • Using advisory locks.
  • Locking models.
  • Setting dynamic lock timeouts.
  • Killing blocking locks.
  • The proxy models and custom queryset methods.
  • Using and configuring the management command.

Installation

Install django-pglock with:

pip3 install django-pglock

After this, add both pgactivity and pglock to the INSTALLED_APPS setting of your Django project.

Contributing Guide

For information on setting up django-pglock for development and contributing changes, view CONTRIBUTING.md.

Creators

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_pglock-1.8.0.tar.gz (16.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_pglock-1.8.0-py3-none-any.whl (17.7 kB view details)

Uploaded Python 3

File details

Details for the file django_pglock-1.8.0.tar.gz.

File metadata

  • Download URL: django_pglock-1.8.0.tar.gz
  • Upload date:
  • Size: 16.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.14.0 Linux/6.8.0-1040-aws

File hashes

Hashes for django_pglock-1.8.0.tar.gz
Algorithm Hash digest
SHA256 6144d96f52ab1d0b39ee295c6baa05d8de6ed5c59ae550413fe30ab668492e25
MD5 ea378d2a04efdebc0df7e3847c3da84e
BLAKE2b-256 0f28cacf31c7c15beb609b50b5b0daa873da6384ce794230159289f737d80d57

See more details on using hashes here.

File details

Details for the file django_pglock-1.8.0-py3-none-any.whl.

File metadata

  • Download URL: django_pglock-1.8.0-py3-none-any.whl
  • Upload date:
  • Size: 17.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.14.0 Linux/6.8.0-1040-aws

File hashes

Hashes for django_pglock-1.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8bbfcb5d732a87e377f69d0fef5697042311c787912ad7baa3f0e00dcff06c41
MD5 a041db07b7971539ea01f5f0430db8ad
BLAKE2b-256 44750c145fbf2dc92cde0ad1b0d20891a41ea4843a8a6ed592ce35ec46d76e87

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page