Skip to main content

slowquery-detective

Catch slow Postgres queries live. Fingerprint the pattern. Run EXPLAIN asynchronously. Suggest the index. A drop-in FastAPI + SQLAlchemy middleware.

ci python sqlalchemy fastapi mypy license


What it does

  1. Fingerprints every query via sqlglot — WHERE id=1 and WHERE id=2 collapse into one row, so you see the pattern that's slow.
  2. Runs EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) asynchronously for queries that cross the p95 threshold, off the request path.
  3. Suggests fixes via a deterministic rules engine (seq-scan on large tables, missing FK indexes, sort without index, function in WHERE, SELECT *, N+1). When no rule matches, an OpenRouter-backed LLM explains the plan in plain English.
  4. Exposes a tiny dashboard API (optional router) so a frontend can render live p95 timelines and one-click index suggestions.

This repository is the PyPI package only. The demo service and dashboard live in:

3-line integration

from slowquery_detective import install

install(app, engine)

Optional: mount the dashboard API on the same app.

from slowquery_detective import install, dashboard_router

install(app, engine)
app.include_router(dashboard_router, prefix="/_slowquery")

Status: this repo is at Phase 4a S1 — Bootstrap. The install helper is defined by docs/specs/05-middleware.md and lands in S3. See docs/ARCHITECTURE.md for the component map and docs/DEMO.md for the reproduction script.

Install

pip install slowquery-detective[fastapi]
# or, to enable the LLM fallback:
pip install slowquery-detective[fastapi,llm]

Development

git clone https://github.com/Abdul-Muizz1310/slowquery-detective
cd slowquery-detective
uv sync --all-extras
uv run pytest
uv run ruff check .
uv run mypy src/

License

MIT — see LICENSE.

Metadata

Release files for slowquery-detective 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for slowquery-detective 0.1.0
File Size Uploaded
slowquery_detective-0.1.0.tar.gz 20.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for slowquery-detective 0.1.0
File Interpreter ABI Platform
slowquery_detective-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 50.0 kB

Release files / slowquery_detective-0.1.0.tar.gz

Download URL slowquery_detective-0.1.0.tar.gz
Size 20.5 kB
Tags Source
SHA-256 checksum
How to use checksums
b6cb0ef06ded862c544d0ce4aec9ee86c71df8010654cacd6f9fa456c86c3c60
BLAKE2b-256 checksum
How to use checksums
97dd1cc5547f383cc68b90a76763b93ba1734ef7432911eeacffae21a2a0a678
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / slowquery_detective-0.1.0-py3-none-any.whl

Download URL slowquery_detective-0.1.0-py3-none-any.whl
Size 29.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b57fe0646a794dcc4894b98d587c6861d3b9c3ed3510c4ac304914317b643283
BLAKE2b-256 checksum
How to use checksums
99369f0792e2d504d3688fdae86a4ada3e9ebd73d9737a0901daf94dedae2563
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

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