Skip to main content

Context manager to run PostgreSQL queries with SQLAlchemy, terminating any other clients that block them

Project description

pg-force-execute

Context manager to run PostgreSQL queries with SQLAlchemy, terminating any other clients that continue to block it after a configurable delay.

Using this to wrap queries is somewhat of a last resort, but is useful in certain Extract Transform Load (ETL) pipeline contexts. For example, if it is more important to replace one table with another than to allow running queries on the table to complete, then this can be used to run the relevant ALTER TABLE RENAME TO query.

Installation

pip install pg-force-execute

Example usage

import datetime
import sqlalchemy as sa
from pg_force_execute import pg_force_execute

# Run postgresql locally should allow the below to run
# docker run --rm -it -e POSTGRES_HOST_AUTH_METHOD=trust -p 5432:5432 postgres

engine = sa.create_engine('postgresql+psycopg://postgres@127.0.0.1:5432/')
query = 'SELECT 1'  # A more realistic example would be something that needs an exclusive lock on a table

with \
        engine.begin() as conn, \
        pg_force_execute(
            conn,           # SQLAlchemy connection to run the query
            delay=datetime.timedelta(minutes=5),  # Amount of time to wait before cancelling queries
        ):

    results = conn.execute(sa.text(query))
    print(results.fetchall())

API

The API a single context manager pg_force_execute.

pg_force_execute(conn, engine, delay=datetime.timedelta(minutes=5), check_interval=datetime.timedelta(seconds=1), termination_thread_timeout=datetime.timedelta(seconds=10), logger=logging.getLogger("pg_force_execute"))

  • conn - A SQLAlchemy connection that will be unblocked

  • delay (optional) - How long to wait before attempting to terminate backends blocking conn

  • check_interval (optional) - The interval between repeated attempted to terminate backends blocking conn

  • termination_thread_timeout (optional) - How long to wait for the termination to complete

  • logger (optional) The Python logger instance through which to log

Running tests locally

pip install -e ".[dev]"  # Only needed once
./start-services.sh      # Only needed once
pytest

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

pg_force_execute-0.0.7.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

pg_force_execute-0.0.7-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file pg_force_execute-0.0.7.tar.gz.

File metadata

  • Download URL: pg_force_execute-0.0.7.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for pg_force_execute-0.0.7.tar.gz
Algorithm Hash digest
SHA256 1d9f11c6ddcfe96939b6b248f3dbc9e4912c338df5676d5fe9f92ac87ddb881a
MD5 2e708ec9359e2feb51354fb3ef8be1f7
BLAKE2b-256 4e0955167f7c69f861c07645df367ed2c285206e7ab387c587a987ba054c6a8f

See more details on using hashes here.

File details

Details for the file pg_force_execute-0.0.7-py3-none-any.whl.

File metadata

File hashes

Hashes for pg_force_execute-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 ed8dfdb9d6c637f99fcbc68e7dc72a20bd37137fbf65831a6e687b7c32b5437c
MD5 b6e0105b114ad0d1e25ce260625c731b
BLAKE2b-256 5fdc71daeef91688aa6ce832e3bbbf92acfbe9c5c638d557f19ffa429fa3eb24

See more details on using hashes here.

Supported by

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