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())

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

Uploaded Source

Built Distribution

pg_force_execute-0.0.2-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pg_force_execute-0.0.2.tar.gz
  • Upload date:
  • Size: 3.7 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.2.tar.gz
Algorithm Hash digest
SHA256 cd997012934382ec42618b35c809846a49f755739e8b952dae9905b34105e2da
MD5 e1a05dc8f047c7a59c88ea8cb7393774
BLAKE2b-256 301dbd3b4a658733647d9b0a128b73a0f6111b898550d16faab7b3ffd3e336ee

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pg_force_execute-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f38b0b6863e5f5b02248c9e86036ce4c7dfeb1611a5f28525b6daf2b185d60fc
MD5 01bb8f3353d4bd58c9afee5d6f178f8e
BLAKE2b-256 fafa02dc4bf5bb6d21589f4f6a7d0b755d2f805269cbca89f95e7a2a595b320a

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