pgsql-loupe
A debug panel for Python web apps on PostgreSQL. Open any page with ?lens=1 and see what it
did to the database: every statement, its parameters, how long it took, the line of your code
that ran it, EXPLAIN ANALYZE plans, the triggers that fired, and a warning when the same
query ran once per row.
Try it
pip install "pgsql-loupe[psycopg]" "psycopg[binary]" psycopg-pool fastapi uvicorn
Save this as app.py; the two marked lines are all pgsql-loupe needs:
import os
from fastapi import FastAPI
from psycopg_pool import ConnectionPool
from pgsql_loupe.asgi import ExplainOptions, LoupeConfig, install
from pgsql_loupe.collectors.psycopg import instrument
DSN = os.environ["DATABASE_URL"]
pool = ConnectionPool(DSN, configure=instrument, open=True) # 1. record statements
app = FastAPI()
install(app, LoupeConfig(explain=ExplainOptions(dsn=DSN))) # 2. add the panel
@app.get("/tables")
def tables() -> list[str]:
with pool.connection() as conn:
rows = conn.execute("SELECT tablename FROM pg_tables ORDER BY 1 LIMIT %s", (5,))
return [name for (name,) in rows]
PGSQL_LOUPE_ENABLED=1 DATABASE_URL=postgresql://user:password@localhost/db uvicorn app:app
Open http://127.0.0.1:8000/tables?lens=plan, then the panel at http://127.0.0.1:8000/__lens/.
It is off unless PGSQL_LOUPE_ENABLED=1, refuses to start when the environment says
production, answers only requests from this machine, and redacts parameters that look secret.
Plan capture runs on its own connection and never executes a write unless you ask
(how).
Drivers
| Driver | Setup |
|---|---|
| psycopg 3 | ConnectionPool(dsn, configure=instrument) or instrument(conn) |
| SQLAlchemy 2 (sync or async) | instrument_engine(engine) |
| asyncpg | asyncpg.create_pool(dsn, connection_class=TracedConnection) |
Any ASGI app works: install(app) for FastAPI and Starlette, LoupeMiddleware(app) otherwise.
In tests (pytest)
Installing the package adds a loupe fixture:
def test_artists_page(loupe, client):
with loupe.record() as r:
client.get("/artists/tracks")
r.assert_max_queries(2)
r.assert_no_nplus1()
r.assert_no_seq_scan("tracks") # needs --pgsql-loupe-dsn or PGSQL_LOUPE_TEST_DSN
A failing assertion lists each statement with its parameters, the line of your code that ran it, and its time.
Compared with other tools
Checked hands-on on 2026-11-18 (details and how to reproduce):
| pgsql-loupe | litestar-debug-toolbar | queryspy | PostgreSQL auto_explain | |
|---|---|---|---|---|
| Raw psycopg 3 / asyncpg | yes | no | no | yes (server side) |
| SQLAlchemy | yes | yes | yes | yes (server side) |
| Per request, correct under concurrency | yes | no | yes | no (server log) |
| Analysed plans with fired triggers | yes | no | no | yes |
| N+1 warnings | yes | yes | ORM select() only |
no |
| pytest assertions | yes | no | yes | no |
Overhead
Measured by bench/overhead.py; full output, machine and versions in
bench/results/2026-11-19-windows.md
(Windows 11 laptop, PostgreSQL 18.7 in Docker on the same machine).
| Added cost | |
|---|---|
| Statement on an instrumented connection, no trace active | 0.11 µs |
| Statement recorded into a trace | 9.74 µs |
GET /artists/tracks (6 statements) with ?lens=1, p50 |
+0.16 ms (11.86 ms without the lens) |
Documentation
Quick start per driver · Query plans · In tests · Configuration · Safety model · Recipes · Design decisions
Bugs and ideas
Please open an issue on GitHub: report a bug or suggest an improvement. Fixes are welcome as pull requests; see CONTRIBUTING.md. Security problems: report them privately through GitHub's advisory form.
Python 3.11 to 3.15, PostgreSQL 16 to 18. Licence: Apache-2.0.
Built with help from Claude Code.
Metadata
Release files for pgsql-loupe 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pgsql_loupe-0.1.0.tar.gz | 120.0 kB | With provenance | Details for pgsql_loupe-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pgsql_loupe-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for pgsql_loupe-0.1.0-py3-none-any.whl |
Total release size: 249.6 kB
Release files / pgsql_loupe-0.1.0.tar.gz
| Download URL | pgsql_loupe-0.1.0.tar.gz |
|---|---|
| Size | 120.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ffaba3e521843031deab28afdec0c4165a1f8cadc7d7d92812d1787d68d33de5
|
|
BLAKE2b-256 checksum How to use checksums |
206a857793833ecf52871ac33ce3f8fca184a161250b1dd1426df7ae2010e41f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 19, 2026.
Transparency logRelease files / pgsql_loupe-0.1.0-py3-none-any.whl
| Download URL | pgsql_loupe-0.1.0-py3-none-any.whl |
|---|---|
| Size | 129.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9797ef3a45483d8300567baf6cb0626699b7349f5548a3a1e5c4f50ee7fdbd6e
|
|
BLAKE2b-256 checksum How to use checksums |
e637ac7362ac59d8a2455a16346b2b828f9bf2b90fae8fea50bd264c0e3738f3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 19, 2026.
Transparency logSecurity for pgsql-loupe 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for pgsql_loupe-0.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 19, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.