Skip to main content

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

pg-force-execute can be installed from PyPI using pip. psycopg2 or psycopg (Psycopg 3) must also be explicitly installed.

pip install pg-force-execute psycopg

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, delay=datetime.timedelta(minutes=5), check_interval=datetime.timedelta(seconds=1), cleanup_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 attempts to terminate backends blocking conn

  • cleanup_timeout (optional) - How long to wait for resources to be cleaned up before allowing exit of the context manager

    For usual operation this parameter shouldn't need to be changed.

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

Compatibility

  • Python >= 3.7.1 (tested on 3.7.1, 3.8.0, 3.9.0, 3.10.0, and 3.11.0)
  • psycopg2 >= 2.9.2 or Psycopg 3 >= 3.1.4
  • SQLAlchemy >= 1.4.24 (tested on 1.4.24 and 2.0.0)
  • PostgreSQL >= 9.6 (tested on 9.6, 10.0, 11.0, 12.0, 13.0, 14.0, and 15.0)

Note that SQLAlchemy < 2 does not support Psycopg 3.

Running tests locally

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

Metadata

Release files for pg-force-execute 0.0.11

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-force-execute 0.0.11
File Size Uploaded Provenance
pg_force_execute-0.0.11.tar.gz 4.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pg-force-execute 0.0.11
File Interpreter ABI Platform
pg_force_execute-0.0.11-py3-none-any.whl Python 3 none any Details

Total release size: 9.1 kB

Release files / pg_force_execute-0.0.11.tar.gz

Download URL pg_force_execute-0.0.11.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
0d56f49971cd954bc775c793a9850aea594e88195c57b2296c5898679328a59d
BLAKE2b-256 checksum
How to use checksums
91dc6669e519f5146a0751f0089f035f4f47790132daa0851e8c0c7d00fdb857
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.1 CPython/3.11.3

Release files / pg_force_execute-0.0.11-py3-none-any.whl

Download URL pg_force_execute-0.0.11-py3-none-any.whl
Size 4.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
250587c0f4c51a2997454442a0f39c2ab4113dc70ebae2015f1556f080595e4a
BLAKE2b-256 checksum
How to use checksums
3cc3a21d017f41c4d7603c0aa895ad781ea24fa7c9cc412056aee119eb326883
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.1 CPython/3.11.3

Security for pg-force-execute 0.0.11

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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