Skip to main content

PostgreSQL Advisory Locks (PALs)

nox codecov

PALs makes it easy to use PostgreSQL advisory locks for distributed, application-level locking.

Do not confuse this type of locking with table or row locking in PostgreSQL. Distributed application-level locking can also be implemented with Redis, Memcache, ZeroMQ, and other services. For applications already using PostgreSQL, PALs avoids setting up and managing another service.

Usage

Install with:

pip install PALs

Then use a Locker as a lock factory:

import datetime as dt

import pals

locker = pals.Locker('my-app-name', 'postgresql://user:pass@server/dbname')

lock1 = locker.lock('my-lock')
lock2 = locker.lock('my-lock')

assert lock1.acquire() is True
assert lock2.acquire(blocking=False) is False

start = dt.datetime.now()
acquired = lock2.acquire(acquire_timeout=300)
waited_ms = (dt.datetime.now() - start).total_seconds() * 1000
assert acquired is False
assert 300 <= waited_ms < 350

lock1.release()

if lock1.acquire(blocking=False):
    try:
        # Do your work here.
        pass
    finally:
        lock1.release()

try:
    with lock1:
        # Do your work here.
        pass
except pals.AcquireFailure:
    # The acquire timeout was reached before the lock was acquired.
    pass

Lock releasing and expiration

Unlike locking systems backed by cache services, PostgreSQL advisory locks do not expire. If a client holds a lock and then sleeps or hangs, no other client can acquire the lock until it is released.

PALs helps prevent accidental lock retention in several ways:

  • Locks work as context managers, which guarantees release when the context exits.
  • Locks release their connection when garbage collected.
  • By default, PALs calls pg_advisory_unlock_all() whenever a connection returns to its dedicated SQLAlchemy pool.

PostgreSQL also invokes pg_advisory_unlock_all() when a session ends, including an ungraceful client disconnect. The extra pool check-in call has a cost and can be disabled with unlock_all_on_checkin=False for high-frequency workloads. Applications that disable it should monitor locks and recycle connections regularly.

Development

The project structure and tooling derive from Coppy. To set up and test the project:

  1. Install the Coppy host dependencies, including Mise and uv.
  2. Start PostgreSQL with docker compose up -d.
  3. Set PALS_DB_URL=postgresql://postgres:password@localhost:54321/postgres.
  4. Run uv sync and prek install.
  5. Run nox to test Python 3.12 through 3.14 and execute the project checks.

Use mise run version show to show the current version and mise run version -- --help for the version-management commands. See Updating a Coppy project before applying template updates.

The implementation and tests are intentionally compact. See changelog.rst for release history.

See also

Metadata

Release files for PALs 0.20261116.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for PALs 0.20261116.1
File Size Uploaded Provenance Actions
pals-0.20261116.1.tar.gz 94.3 kB With provenance Details for pals-0.20261116.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for PALs 0.20261116.1
File Interpreter ABI Platform Actions
pals-0.20261116.1-py3-none-any.whl Python 3 none any Details for pals-0.20261116.1-py3-none-any.whl

Total release size: 101.2 kB

Release files / pals-0.20261116.1.tar.gz

Download URL pals-0.20261116.1.tar.gz
Size 94.3 kB
Tags Source
SHA-256 checksum
How to use checksums
5aa34f24ea52b6fe588f15f6f426470207a3e794a10669c2110a57f8cbffed23
BLAKE2b-256 checksum
How to use checksums
d6e8bd801a58d864e62cf0a681f7e0ef0474838a02d51efdb0f89e06094b1c22
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 16, 2026.

Transparency log

Release files / pals-0.20261116.1-py3-none-any.whl

Download URL pals-0.20261116.1-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
546a94642272c58d9681455eee11d24de26744ff95fb3d2dc75ce37e37c6a6e8
BLAKE2b-256 checksum
How to use checksums
f2945cf7676cd3d60c855b9affb2ecad2c025b43d7fb976f606bf8deb0b4feed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 16, 2026.

Transparency log

Security for PALs 0.20261116.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pals-0.20261116.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 16, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page