Skip to main content

Drop-in FastAPI + SQLAlchemy middleware that fingerprints slow Postgres queries, runs EXPLAIN asynchronously, and suggests indexes via a rules engine with LLM fallback.

Project description

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 sqlglotWHERE 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.

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

slowquery_detective-0.1.0.tar.gz (20.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

slowquery_detective-0.1.0-py3-none-any.whl (29.5 kB view details)

Uploaded Python 3

File details

Details for the file slowquery_detective-0.1.0.tar.gz.

File metadata

  • Download URL: slowquery_detective-0.1.0.tar.gz
  • Upload date:
  • Size: 20.5 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for slowquery_detective-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b6cb0ef06ded862c544d0ce4aec9ee86c71df8010654cacd6f9fa456c86c3c60
MD5 32118a354e9f4d7693d57c666a2db1ed
BLAKE2b-256 97dd1cc5547f383cc68b90a76763b93ba1734ef7432911eeacffae21a2a0a678

See more details on using hashes here.

File details

Details for the file slowquery_detective-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: slowquery_detective-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 29.5 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for slowquery_detective-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b57fe0646a794dcc4894b98d587c6861d3b9c3ed3510c4ac304914317b643283
MD5 cc3dcb735e576fc8019772e4791c8de7
BLAKE2b-256 99369f0792e2d504d3688fdae86a4ada3e9ebd73d9737a0901daf94dedae2563

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page