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
EXPLAINasynchronously. Suggest the index. A drop-in FastAPI + SQLAlchemy middleware.
What it does
- Fingerprints every query via
sqlglot—WHERE id=1andWHERE id=2collapse into one row, so you see the pattern that's slow. - Runs
EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON)asynchronously for queries that cross the p95 threshold, off the request path. - 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. - 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:
Abdul-Muizz1310/slowquery-demo-backend— feathers-generated FastAPI service with a seeded 1M-row datasetAbdul-Muizz1310/slowquery-dashboard-frontend— Next.js dashboard
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
installhelper is defined bydocs/specs/05-middleware.mdand lands in S3. Seedocs/ARCHITECTURE.mdfor the component map anddocs/DEMO.mdfor 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
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b6cb0ef06ded862c544d0ce4aec9ee86c71df8010654cacd6f9fa456c86c3c60
|
|
| MD5 |
32118a354e9f4d7693d57c666a2db1ed
|
|
| BLAKE2b-256 |
97dd1cc5547f383cc68b90a76763b93ba1734ef7432911eeacffae21a2a0a678
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b57fe0646a794dcc4894b98d587c6861d3b9c3ed3510c4ac304914317b643283
|
|
| MD5 |
cc3dcb735e576fc8019772e4791c8de7
|
|
| BLAKE2b-256 |
99369f0792e2d504d3688fdae86a4ada3e9ebd73d9737a0901daf94dedae2563
|