Skip to main content

Utility function to run a PostgreSQL query with SQLAlchemy, terminating any queries that block it

Project description

pg-force-execute

Utility function to run a PostgreSQL query with SQLAlchemy, terminating any queries that continue to block it after a configurable delay.

Installation

pip install pg-force-execute

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://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:
    results = pg_force_execute(
        sa.text(query), # SQL query to execute
        conn,           # SQLAlchemy connection to run the query
        engine,         # SQLAlchemy engine that will create new connections to cancel blocking queries
        delay=datetime.timedelta(minutes=5),  # Amount of time to wait before cancelling queries
    )
    print(results.fetchall())

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.1.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

pg_force_execute-0.0.1-py3-none-any.whl (3.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pg_force_execute-0.0.1.tar.gz
Algorithm Hash digest
SHA256 11400ddee939ea10c8f3dc7944d00eabefc801c9d6c54f4883a207b3982741d4
MD5 4e767c1e0e0c5a865d685b64d8d75db5
BLAKE2b-256 e83a3e470f22f30fdf3db847fb1499ab9b3124c0554a8b15dc3c6c0bb0faa43f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pg_force_execute-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6c9c3d4f313ee9d5574bc5b1f5bb874f1d4f0ce4c531a544139115d67ddc11f4
MD5 9f51b76c5ecec5fee5841835c937b533
BLAKE2b-256 8afacfdd9b05d3dd9ecc846cf5ba0d7f73ba6004d0e5abb2b751f0353a63e936

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