DEV Community

Alex
Alex

Posted on

I built a tiny proxy that finds N+1 queries in any Postgres app

N+1 queries are the bug that never shows up in your slow query log. Each query is fast (0.2 ms, 0.3 ms), so auto_explain and log_min_duration_statement stay quiet. But your endpoint runs 50 of them in a loop, and a page that should take 20 ms takes 400.

The usual detectors are tied to one framework: Bullet for Rails, nplusone for Django, debug toolbars. Each runs inside your process and covers one ORM. I wanted something that works for Prisma, Drizzle, SQLAlchemy, pgx, raw SQL, anything.

So I built n1scan: a small local proxy that sits between your app and PostgreSQL, watches the wire protocol, and tells you when the same query shape keeps running with different values.

n1scan live screen

How you use it

export DATABASE_URL=postgres://me:secret@localhost:5432/app
n1scan run -- npm test
Enter fullscreen mode Exit fullscreen mode

run starts the proxy, rewrites $DATABASE_URL for the child process, and prints a report when the command exits. No code changes, no library to install in your app.

Real output from the Prisma example in the repo:

N+1 01:50:05 conn 1: same query 5+ times in 31.6ms
    SELECT COUNT(*) ... FROM "public"."posts" WHERE "public"."posts"."author_id" = $1 ...
    after: SELECT "public"."authors"."id", "public"."authors"."name" FROM "public"."authors" ...
    params: 1, 0 … 5, 0
    index: CREATE INDEX CONCURRENTLY ON public.posts (author_id);
    sequential scan on public.posts (~200000 rows) filtering on author_id, run once per repetition
Enter fullscreen mode Exit fullscreen mode

Two things I find useful there:

  • after: is the statement that ran just before the loop started, usually the "1" of the N+1. It is the quickest way to find the loop in your code.
  • n1scan runs a read-only EXPLAIN (never ANALYZE) and, if it sees a sequential scan on a big table, suggests the index. On that example, the index alone took the endpoint from ~370 ms to ~118 ms. The real fix (one query with include) is better, but the index is a one-liner.

How it detects them

  1. Every query is fingerprinted: literals and $n placeholders become ?, IN (...) lists collapse, comments disappear. WHERE id = 1 and WHERE id = 42 are the same shape.
  2. When one fingerprint runs 5 times on the same connection with no pause over 200 ms, and with more than one distinct set of values, it is an N+1.
  3. The statement before the burst is reported as the parent.

The proxy forwards every byte unchanged and decodes a copy. If decoding ever fails, it falls back to raw copying, so it should never be the reason a query fails. Overhead measured with pgbench -S was about 0.03 ms per query.

In CI

n1scan run --ci -- npm test
Enter fullscreen mode Exit fullscreen mode

Exit code 1 if your test suite introduces an N+1. Add --json for machine-readable output.

Tested with 13 clients

pgx, database/sql, lib/pq, node-postgres, postgres.js, Prisma 6 and 7, Drizzle, psycopg 3, SQLAlchemy 2, asyncpg, Django and psql. Each one runs an N+1 loop and a fixed version, straight to Postgres and through the proxy, with and without TLS. Results must match and the fixed version must not be flagged.

Try it

One Go binary for Linux, macOS and Windows: https://github.com/alex00010/n1scan

It is MIT licensed and brand new, so there are plenty of rough edges. I wrote down a list of good first issues for people who want to help, and I would love to hear which client or ORM it misses.

Top comments (0)