<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Alex</title>
    <description>The latest articles on DEV Community by Alex (@alex3154).</description>
    <link>https://dev.to/alex3154</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4194138%2F9e79a052-e92c-4f11-878d-488bc6a63f72.png</url>
      <title>DEV Community: Alex</title>
      <link>https://dev.to/alex3154</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alex3154"/>
    <language>en</language>
    <item>
      <title>I built a tiny proxy that finds N+1 queries in any Postgres app</title>
      <dc:creator>Alex</dc:creator>
      <pubDate>Sun, 29 Nov 2026 10:38:19 +0000</pubDate>
      <link>https://dev.to/alex3154/i-built-a-tiny-proxy-that-finds-n1-queries-in-any-postgres-app-56bl</link>
      <guid>https://dev.to/alex3154/i-built-a-tiny-proxy-that-finds-n1-queries-in-any-postgres-app-56bl</guid>
      <description>&lt;p&gt;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 &lt;code&gt;auto_explain&lt;/code&gt; and &lt;code&gt;log_min_duration_statement&lt;/code&gt; stay quiet. But your endpoint runs 50 of them in a loop, and a page that should take 20 ms takes 400.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;So I built &lt;strong&gt;n1scan&lt;/strong&gt;: 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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Foumkdqn00f6tu5wuht1a.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Foumkdqn00f6tu5wuht1a.gif" alt="n1scan live screen" width="600" height="462"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How you use it
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;DATABASE_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;postgres://me:secret@localhost:5432/app
n1scan run -- npm &lt;span class="nb"&gt;test&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Real output from the Prisma example in the repo:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two things I find useful there:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;after:&lt;/code&gt; 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.&lt;/li&gt;
&lt;li&gt;n1scan runs a read-only &lt;code&gt;EXPLAIN&lt;/code&gt; (never &lt;code&gt;ANALYZE&lt;/code&gt;) 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 &lt;code&gt;include&lt;/code&gt;) is better, but the index is a one-liner.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How it detects them
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Every query is fingerprinted: literals and &lt;code&gt;$n&lt;/code&gt; placeholders become &lt;code&gt;?&lt;/code&gt;, &lt;code&gt;IN (...)&lt;/code&gt; lists collapse, comments disappear. &lt;code&gt;WHERE id = 1&lt;/code&gt; and &lt;code&gt;WHERE id = 42&lt;/code&gt; are the same shape.&lt;/li&gt;
&lt;li&gt;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.&lt;/li&gt;
&lt;li&gt;The statement before the burst is reported as the parent.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;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 &lt;code&gt;pgbench -S&lt;/code&gt; was about 0.03 ms per query.&lt;/p&gt;

&lt;h2&gt;
  
  
  In CI
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;n1scan run --ci -- npm &lt;span class="nb"&gt;test&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Exit code 1 if your test suite introduces an N+1. Add &lt;code&gt;--json&lt;/code&gt; for machine-readable output.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tested with 13 clients
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;

&lt;p&gt;One Go binary for Linux, macOS and Windows: &lt;a href="https://github.com/alex00010/n1scan" rel="noopener noreferrer"&gt;https://github.com/alex00010/n1scan&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>go</category>
      <category>webdev</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Referral codes are the future, so I built a web to share them and to compare which ones are actually worth it</title>
      <dc:creator>Alex</dc:creator>
      <pubDate>Mon, 19 Oct 2026 12:42:25 +0000</pubDate>
      <link>https://dev.to/alex3154/referral-codes-are-the-future-so-i-built-a-web-to-share-them-and-to-compare-which-ones-are-554p</link>
      <guid>https://dev.to/alex3154/referral-codes-are-the-future-so-i-built-a-web-to-share-them-and-to-compare-which-ones-are-554p</guid>
      <description>&lt;p&gt;A few months ago I started noticing how many apps and banks offer money if you sign up with a friend's code. What annoyed me was the fine print. "Get €100" usually turns out to mean payroll, a minimum deposit, or staying a year.&lt;/p&gt;

&lt;p&gt;So I built &lt;a href="https://yourreferralcodes.com/" rel="noopener noreferrer"&gt;yourreferralcodes.com&lt;/a&gt;. You can look up and share codes, and each one shows the reward and the conditions in plain words.&lt;/p&gt;

&lt;p&gt;The part I like most is the comparator. Every program gets an effort level from 1 to 3.&lt;/p&gt;

&lt;p&gt;The score is simply the reward divided by a weight (1, 2 or 8 for each level). I made level 3 weigh a lot on purpose. Example with made-up numbers: €50 at level 1 scores 50, €100 at level 2 scores 50, and €200 at level 3 scores 25. The biggest headline is the worst deal.&lt;/p&gt;

&lt;p&gt;Two things I had to decide:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Only cash rewards for the invited friend enter the ranking. Vouchers, tokens and "up to X" offers distort everything.&lt;/li&gt;
&lt;li&gt;The effort levels are my own reading of each program's public conditions, so treat them as a guide, not a verdict. Programs change, so I keep them in one small file that's easy to edit.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Built with Next.js, Supabase and Vercel. If you think I scored a program wrong, tell me in the comments and I'll fix it. I'd apreciate some feedback. Thank you!&lt;/p&gt;

</description>
      <category>webdev</category>
    </item>
  </channel>
</rss>
