Skip to main content

Lock mechanism implemented with PostgreSQL advisory locks.

Project description

postgresql-lock

Lock mechanism implemented with PostgreSQL advisory locks.

Easily implement distributed database locking.

Install

pip install postgresql-lock

Supported database interfaces

  • asyncpg
    • asynchronous
  • psycopg2
    • synchronous
  • psycopg3
    • asynchronous
    • synchronous
  • sqlalchemy (supports version 1 & 2; can use any underlying database interface)
    • asynchronous
    • synchronous

Why would I use this?

  • PostgreSQL table locks aren't sufficient for your use-case
  • PostgreSQL row locks don't work on INSERT
  • You want to prevent race conditions between INSERT and UPDATE on the same primary key
  • None of the aforementioned details fit your use-case, but you have PostgreSQL installed and need to prevent race conditions in a distributed system

Default operation

By default postgresql-lock will use session lock scope in blocking mode with rollback_on_error enabled. The session lock scope means only a single database connection can acquire the lock at a time.

Usage

All work revolves around the Lock class.

The easiest way to use Lock is with with or async with statements. The lock will be released automatically. If rollback_on_error is enabled (default), rollbacks are automatically handled prior to release.

Using with and async with implies blocking mode.

from postgresql_lock import Lock

# setup connection
conn = ...

# create and use lock
with Lock(conn, "shared-identifier"):
    print("Acquired lock!")

    # do something here

Now compare the above example to the equivalent try/finally example below:

from postgresql_lock import Lock

# setup connection
conn = ...

# create lock
lock = Lock(conn, "shared-identifier")

try:
    # acquire lock
    lock.acquire()

    print("Acquired lock!")

    try:
        # do something here
        pass

    except Exception as exc:
        # handle_error() will rollback the transaction by default
        lock.handle_error(exc)

        raise exc
finally:
    # release lock (this is safe to run even if the lock has not been acquired)
    lock.release()

Asynchronous usage (without async with)

from postgresql_lock import Lock

# setup connection
conn = ...

# create lock
lock = Lock(conn, "shared-identifier")

try:
    # acquire lock
    await lock.acquire_async()

    print("Acquired lock!")

    try:
        # do something here
        pass

    except Exception as exc:
        # handle_error_async() will rollback the transaction by default
        await lock.handle_error_async(exc)

        raise exc
finally:
    # release lock (this is safe to run even if the lock has not been acquired)
    await lock.release_async()

Non-blocking mode (supports async as well)

from postgresql_lock import Lock

# setup connection
conn = ...

# create lock
lock = Lock(conn, "shared-identifier")

# acquire lock
if lock.acquire(block=False):
    # do something here
    pass

else:
    # could not acquire lock
    pass

# release lock (this is safe to run even if the lock has not been acquired)
lock.release()

Specify the database interface manually

from postgresql_lock import Lock

# setup connection
conn = ...

# create and use lock
lock = Lock(conn, "shared-identifier", interface="asyncpg")

# do things with the lock

Handle rollbacks manually

from postgresql_lock import Lock

# setup connection
conn = ...

# create and use lock
lock = Lock(conn, "shared-identifier", rollback_on_error=False)

# do things with the lock

Changelog

  • 0.1.9
    • Fix: release_async() bug for sqlalchemy connections
  • 0.1.8
    • Add logger() function
    • Use "postgresql_lock" logger name
  • 0.1.7
    • Add logging statements
  • 0.1.6
    • Use int.from_bytes() to convert lock key into integer
    • Fix: psycopg3 close() not being awaited bug
  • 0.1.5
    • Rename package from postgres-lock to postgresql-lock
  • 0.1.4
    • Add py.typed for mypy
  • 0.1.3
    • Key can be any object
  • 0.1.2
    • Add Lock.rollback_on_error (default true)
    • Add Lock.handle_error() & Lock.handle_error_async()
  • 0.1.1
    • Key can be str or int

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

postgresql_lock-0.1.9.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

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

postgresql_lock-0.1.9-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file postgresql_lock-0.1.9.tar.gz.

File metadata

  • Download URL: postgresql_lock-0.1.9.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.12.3 Linux/6.8.0-49-generic

File hashes

Hashes for postgresql_lock-0.1.9.tar.gz
Algorithm Hash digest
SHA256 13e8c84eb9f357ca98742c77b97c1d0c7752d6f9870ba053fd91d000165ec92e
MD5 de7081a421e4f906c8d78f8637c1e3ef
BLAKE2b-256 681e7743eaf408f2a346d27cfdb6b75e9c7bd8fd1dc3e21a7d548c550008e964

See more details on using hashes here.

File details

Details for the file postgresql_lock-0.1.9-py3-none-any.whl.

File metadata

  • Download URL: postgresql_lock-0.1.9-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.12.3 Linux/6.8.0-49-generic

File hashes

Hashes for postgresql_lock-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 33085ded7794b4a51d8ebee1d6b825105d8973ebc5a90bba496effb795b4dfee
MD5 301c3b14d33ca1f059a190e019983846
BLAKE2b-256 330512bff39d95a26deb0b3a2283e3de3122d50e7020ef671c4036e281f5d282

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