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
Release history Release notifications | RSS feed
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.3.tar.gz
(3.8 kB
view hashes)
Built Distribution
Close
Hashes for pg_force_execute-0.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 724c45bb92a349b153c5e48141fced74a0a503d9f5fefb568b7089e3ec63cd3c |
|
MD5 | 0659671e9edacba4e265b7a6d8149c48 |
|
BLAKE2b-256 | 8eb4f77d68556344eedb8317c194e7e94eca9409889ba10b684c2902c66b8011 |