Skip to main content

pg-advisory-lock

Pythonic PostgreSQL advisory locking for SQLAlchemy, with structured keys and explicit contention handling.

Provides a high-level interface for acquiring PostgreSQL advisory locks with structured lock arguments and timeout support. Advisory locks are application-level locks that are automatically released when the transaction commits or rolls back.

Install

pip install pg-advisory-lock

Requires Python 3.12+ and SQLAlchemy 2.x.

Key features:

  • Deterministic lock keys composed from str, int, and uuid.UUID components
  • Non-blocking immediate lock acquisition
  • Blocking lock acquisition with configurable timeout
  • Automatic lock release on transaction end (uses pg_advisory_xact_lock functions)
  • Configurable handling for expected lock contention: raise exceptions (default) or return a boolean
  • Database and infrastructure errors are propagated unchanged
  • Inline type hints shipped with the package (PEP 561)

Failure handling

raise_on_failure controls expected lock contention only:

  • raise_on_failure=True (default):
    • lock_immediate() raises PgAdvisoryLockNotAcquired when the lock is already held.
    • lock() raises PgAdvisoryLockTimeout when its timeout expires.
  • raise_on_failure=False:
    • Both methods return False for expected contention or timeout.
  • Unexpected database and infrastructure errors are always propagated.

raise_on_failure must be a boolean. Other values raise PgAdvisoryLockError.

PgAdvisoryLockTimeout is a subclass of PgAdvisoryLockNotAcquired.

Boolean mode discards the originating database error. Use the default exception mode when the cause matters: the underlying DBAPIError is chained as __cause__.

Usage examples:

Import the public API from the installed package:

from pg_advisory_lock import (
    PgAdvisoryLock,
    PgAdvisoryLockError,
    PgAdvisoryLockNotAcquired,
    PgAdvisoryLockTimeout,
)

Default behavior: raises exception on failure (safe by default):

locker = PgAdvisoryLock(lock_args=[UserModel.__table__.name, user_id])

with session.begin():
    locker.lock(session=session)
    process_payment()

Boolean mode: explicit handling of lock contention:

locker = PgAdvisoryLock(lock_args=[UserModel.__table__.name, user_id], raise_on_failure=False)

with session.begin():
    if locker.lock(session=session, timeout_in_ms=5000):
        process_payment()
    else:
        schedule_retry()

Immediate (non-blocking) attempts:

locker = PgAdvisoryLock(
    lock_args=['payment_processing', order_id],
    raise_on_failure=False,
)

with session.begin():
    if locker.lock_immediate(session=session):
        process_payment()
    else:
        schedule_retry()

Lock on multiple components:

locker = PgAdvisoryLock(lock_args=['user_sync', region, user_id])

with session.begin():
    locker.lock(session=session)
    synchronize_user()

Transaction behavior

The library uses transaction-scoped advisory locks. The critical section must remain inside the same open transaction in which the lock was acquired. Committing or rolling back that transaction releases the lock automatically.

When lock() reaches its timeout, the failed PostgreSQL statement is isolated within a savepoint. After False is returned or PgAdvisoryLockTimeout is caught, the caller's outer transaction remains usable.

lock() temporarily changes the transaction-local PostgreSQL lock_timeout. After a successful acquisition or handled timeout, the caller's previous lock_timeout value is restored.

A connection or infrastructure failure cannot be repaired by a savepoint and is propagated to the caller.

PostgreSQL advisory lock caveats

PostgreSQL advisory locks have a few well-known caveats. This library inherits them; it does not remove or wrap them away. You should account for them when taking more than one lock or holding many distinct keys in one transaction.

Details are in the PostgreSQL advisory-lock documentation.

  • Taking more than one advisory lock in a transaction can deadlock if another session takes the same locks in the opposite order. Use a consistent application order (for example parent before child, or a stable resource identity). Do not sort by lock_key.
  • lock() sets a transaction-local lock_timeout (default 1000 ms). PostgreSQL's deadlock detector only runs after deadlock_timeout (also 1 s by default). When lock_timeout expires first, a real deadlock is reported as SQLSTATE 55P03 and becomes PgAdvisoryLockTimeout or False, same as ordinary contention. A longer timeout_in_ms can instead surface 40P01, which this library does not treat as contention.
  • Each distinct advisory lock occupies a slot in PostgreSQL's shared lock table, sized from max_locks_per_transaction and shared with ordinary relation and row locks. Exhaustion is an out-of-shared-memory error, not 55P03, and is propagated unchanged.

Lock arguments and key generation

lock_args must be a non-empty list or tuple. A bare string, set, mapping, generator, or other iterable is rejected.

The arguments are copied to an immutable tuple during construction, so later changes to the original list do not affect the lock identity.

For a lock with one component, wrap the value in a list or one-element tuple:

PgAdvisoryLock(lock_args=['payment_processing'])
PgAdvisoryLock(lock_args=('payment_processing',))

Each component must be a str, int, or uuid.UUID. Any other type, including None, SQLAlchemy model instances, model classes, and Table objects, raises PgAdvisoryLockError at construction.

The restriction exists because a value relying on Python's default repr stringifies with a memory address that differs in every process. Hashing one would derive a key that never matches another process, so the lock would appear granted while excluding nobody.

Pass the table name explicitly:

locker = PgAdvisoryLock(lock_args=[UserModel.__table__.name, user.id])

__table__.name avoids duplicating the table name in application code. Note that renaming the table changes the derived key, whereas a string literal would not.

The normalized strings are serialized as a compact JSON array, preserving component boundaries even when values contain separators. The serialized value is hashed with SHA-256 and mapped to PostgreSQL's signed 64-bit advisory-lock key range.

Normalization is intentionally type-insensitive, so values with the same normalized string representation, such as 1 and '1', produce the same lock key.

Thread safety

  • An instance keeps no state after construction and can be shared across sessions and threads.
  • Each thread needs its own Session; SQLAlchemy sessions are not thread-safe.
  • A single acquisition belongs to the session and transaction that made it.

Development

Running tests

Install the project and its development dependencies:

poetry install

Start the local PostgreSQL test database and run the test suite:

docker compose up -d
poetry run pytest

Tests connect to the following database by default:

postgresql+psycopg://postgres:123@localhost:6543/testing

Override the database URL when using another PostgreSQL instance:

TEST_DATABASE_URL='postgresql+psycopg://user:password@host:5432/database' \
  poetry run pytest

Stop the local test database when it is no longer needed:

docker compose down

Checks and GitHub workflows

Run the same checks locally as the CI workflow:

poetry run ruff check .
poetry run ruff format --check .
poetry run mypy
poetry run pytest
poetry build

CI runs on pull requests targeting master and pushes to master. It runs linting, formatting, type checks, tests against PostgreSQL, and a package build.

Releasing to PyPI

  1. Update the version in pyproject.toml and add the release notes to CHANGES.md. Merge these changes into master and wait for CI to pass.

  2. Switch to the latest master and confirm the package version:

    git switch master
    git pull --ff-only origin master
    poetry version -s
    
  3. Create an annotated tag matching that version, then push the tag. For version 0.1.3:

    git tag -a v0.1.3 -m "Release v0.1.3"
    git push origin v0.1.3
    

Pushing a v* tag starts the Release workflow. It builds the distributions from the tagged commit and publishes them to PyPI using trusted publishing through the pypi GitHub environment. The release workflow does not run the CI checks, so tag the commit that passed CI. The package version comes from pyproject.toml; creating a tag does not update it.

VS Code tasks

Use Tasks: Run Task to run the tasks in .vscode/tasks.json. The release flow uses go-to-master, project-version, git-create-tag, and git-push-tag, in that order. Both tag tasks prompt for the release version without the v prefix; enter the same version shown by project-version for each task.

Tasks also cover dependency installation, formatting, type checks, tests, and the local PostgreSQL service. The docker-down task removes the database volume as well as stopping the service.

License

MIT. See LICENSE.

Metadata

Release files for pg-advisory-lock 0.1.3

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

Source distribution (sdist)

Source distribution for pg-advisory-lock 0.1.3
File Size Uploaded Provenance Actions
pg_advisory_lock-0.1.3.tar.gz 9.0 kB With provenance Details for pg_advisory_lock-0.1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pg-advisory-lock 0.1.3
File Interpreter ABI Platform Actions
pg_advisory_lock-0.1.3-py3-none-any.whl Python 3 none any Details for pg_advisory_lock-0.1.3-py3-none-any.whl

Total release size: 20.2 kB

Release files / pg_advisory_lock-0.1.3.tar.gz

Download URL pg_advisory_lock-0.1.3.tar.gz
Size 9.0 kB
Tags Source
SHA-256 checksum
How to use checksums
3123fa60e34ad1de6e766313a033befa32af3abba2d34d7ead69ebf1205c89c5
BLAKE2b-256 checksum
How to use checksums
b48614d9347ed7e273afacb6a76a0279441012e6a30e869563270229d362ebb9
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 12, 2026.

Transparency log

Release files / pg_advisory_lock-0.1.3-py3-none-any.whl

Download URL pg_advisory_lock-0.1.3-py3-none-any.whl
Size 11.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6463018e25fe05fbacf38a862f2f4180e53086eae73552c2484878af568eca37
BLAKE2b-256 checksum
How to use checksums
255eee43ed68bbcd4d813f1e5a59deee582b695dbd4718fbf4a5d0d292753dc3
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 12, 2026.

Transparency log

Security for pg-advisory-lock 0.1.3

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 pg_advisory_lock-0.1.3.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 12, 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