Mendlark / Guides

Supabase too many connections: how to fix it

Your app works, then under a bit of traffic the database starts refusing requests with errors like sorry, too many clients already, remaining connection slots are reserved or Max client connections reached. Sometimes the whole app goes down for a few minutes and comes back. This is almost always about how your code opens database connections, especially from serverless or edge functions, not about the size of your data.

Stuck on this? Get it fixed for €49.

Fixed in 48 hours or you don't pay. Launch deal for the first 5 customers (normally €79). You get the tested fix as a pull request, so nothing changes until you merge it.

Get it fixed for €49

Why it happens

Each Supabase compute size has a fixed limit. The free Nano and the Micro size allow 60 direct Postgres connections and 200 pooler clients. Small allows 90 and 400, Medium 120 and 600. Supabase's own services (Auth, Storage, the REST API and health checks) use some of those direct connections too.

Serverless platforms like Vercel and Netlify run many copies of your function at once. If each copy opens its own connection pool, and libraries such as Postgres.js default to 10 connections per pool, a few dozen warm instances can use up every slot.

1. Use the transaction pooler on port 6543

For serverless and edge code, don't connect directly to the database. In the Supabase dashboard, click Connect and copy the Transaction pooler string. It looks like this:

postgresql://postgres.[PROJECT-REF]:[PASSWORD]@[POOLER-HOST]:6543/postgres

Port 6543 is transaction mode: a real database connection is only borrowed for the length of each transaction, so many short-lived functions can share a small number of connections. Port 5432 on the pooler host is session mode, which holds a connection for as long as the client stays connected. Keep session mode or the direct connection for long-running servers, migrations and tools that need session features.

2. Turn off prepared statements

Transaction mode doesn't support prepared statements, so switch them off in your driver or you'll get confusing errors:

  • Postgres.js or Drizzle: prepare: false
  • Prisma: add ?pgbouncer=true to the connection string
  • asyncpg: statement_cache_size=0

Session features like LISTEN/NOTIFY, SET outside a transaction and temporary tables also don't carry over between transactions in this mode.

3. Keep the pool tiny in each function

In serverless, set the client-side pool to 1 connection and create the client once, outside the request handler, so a warm instance reuses it:

import postgres from "postgres";
const sql = postgres(process.env.DATABASE_URL, { max: 1, prepare: false });

export default async function handler(req, res) {
  const rows = await sql`select id, name from items limit 20`;
  res.json(rows);
}

With Prisma, add connection_limit=1 to the connection string. Only raise these numbers if you can see requests queuing on a single instance.

4. Use supabase-js where you can

Calls made with supabase-js (supabase.from('items').select()) go over HTTP through Supabase's API, which already manages its own connections. If a function only does simple reads and writes, it may not need a raw Postgres connection at all.

5. Find what's holding connections

Run this in the SQL editor to see who is connected and what they're doing:

select usename, application_name, state, count(*)
from pg_stat_activity
group by usename, application_name, state
order by count(*) desc;

Lots of idle connections from one app usually means a pool that's too big or a client created on every request. Rows stuck in idle in transaction point at code that starts a transaction and never commits.

6. Monitor it, then decide on size

In the dashboard, the database Reports show connection counts, CPU and memory over time, and Query Performance shows slow queries. Slow queries hold connections longer, so fixing one bad query can do more than a bigger plan. If you're well-behaved and still near the limit, a larger compute size raises both limits. You can also adjust the pooler's Pool Size in Database settings, but keep total connections below your compute's maximum to leave room for Supabase's own services.

Quick checklist

  • Serverless and edge code uses the transaction pooler on port 6543
  • Prepared statements off in the driver
  • Client created once per instance, with a pool of 1
  • No idle in transaction sessions piling up
  • Slow queries checked in Query Performance
  • Connection count watched in database Reports

Want a second pair of eyes?

Mendlark fixes AI-built apps that break at go-live. Start with a free scan and we'll tell you what's wrong and what a fix costs, or get a fix for €49 (launch price, normally €79) and we'll fix it, test it and send it as a pull request. Related: Supabase view bypasses RLS.

Not sure what else is wrong? Paste your app link into the free instant check and get a plain-English list of problems in seconds.