Thomas Osayi

November 23, 2026 · 9 min read · Thomas Osayi

Too many connections in Postgres on serverless: why it happens and how to fix it

Why Vercel functions exhaust Postgres connections with Prisma, Supabase or Neon, what "too many clients already" means, and how poolers and pool sizes fix it.

An app that worked perfectly with ten test users can fall over the first time real traffic arrives, and the error is not about slow code at all. Requests start failing with "sorry, too many clients already" or "remaining connection slots are reserved", the database dashboard shows a wall of connections, and then, a few minutes later, everything recovers as if nothing happened.

That pattern is the classic serverless connection problem. My day job at The Hoop Gang is Next.js, TypeScript, Prisma and Postgres, so how connections are opened and shared is something I think about whenever I touch database code. This post explains why serverless functions exhaust Postgres connections, what the errors mean, and the specific settings that fix it on Supabase, Neon and Prisma, verified against their docs at the time of writing.

Why do serverless functions run out of Postgres connections?

Because each serverless instance opens its own connections, and under load the platform runs many instances at once, so the total quickly passes the database's limit.

A traditional server is one long running process with one connection pool, say ten connections, shared by every request. On Vercel, your API routes and server code run as functions. When traffic rises, the platform starts more instances to handle it. Each instance loads your code, creates its own database client and opens its own pool. Ten instances with a pool of ten each is a hundred connections, and Postgres starts refusing new ones.

Several things make it worse:

  • A database client created inside the request handler, so every invocation opens a fresh connection.
  • Default pool sizes meant for long running servers. The Prisma docs list a default pool size of 10 for the pg driver adapter.
  • Connections that stay open while an instance sits idle, holding slots nobody is using.
  • Other clients sharing the same limit: the database dashboard, migrations, background jobs, analytics tools.
  • In development, hot reloading creating a new client on every file save.

The fix is not a bigger database first. It is putting a pooler between your functions and Postgres, and keeping each instance's own pool small.

What do the error messages mean?

They all mean Postgres has run out of connection slots for your user, at slightly different points.

Postgres has a setting called max_connections, which the PostgreSQL documentation says is typically 100 by default. A few of those slots are held back: superuser_reserved_connections defaults to 3, and newer versions add reserved_connections for roles with special privileges. The errors you will see:

  • "FATAL: sorry, too many clients already" means every slot is in use.
  • "FATAL: remaining connection slots are reserved for non-replication superuser connections" is the older wording for the case where only reserved slots are left, so a normal app user is refused.
  • "remaining connection slots are reserved for roles with the SUPERUSER attribute" and "remaining connection slots are reserved for roles with privileges of pg_use_reserved_connections" are the same situation in newer Postgres versions.

Your ORM wraps these in its own error, so look for the underlying Postgres text in the function logs. If you see timeouts acquiring a connection instead, your app's local pool is the bottleneck rather than the database, which points to pool size and slow queries holding connections too long.

How many connections does your database actually allow?

Fewer than most people expect, especially on small hosted plans, so check the real number before tuning anything.

At the time of writing, Supabase lists 60 direct connections and 200 pooler clients for its Nano and Micro compute sizes, 90 and 400 for Small, and 120 and 600 for Medium. Neon sizes max_connections by compute: 112 at 0.25 compute units, 450 at 1, rising with larger computes. A self-hosted Postgres uses whatever max_connections is set to, often the default of about 100.

You can check from any SQL client:

  • show max_connections; returns the configured limit.
  • select count(*) from pg_stat_activity; returns how many connections exist right now.
  • Grouping pg_stat_activity by application_name, usename or state shows who is holding them and how many are idle.

If most connections are idle and belong to your app, you have a pooling problem. If a migration tool or a reporting dashboard is holding a large share, that is worth fixing too. Note the numbers during a traffic spike, not at 3am, because the problem only exists under load.

How do you fix it on Supabase?

Point your app's runtime connection at the Supavisor transaction pooler on port 6543, and keep port 5432 for migrations.

Supabase offers three ways to connect. The direct connection uses port 5432 on the db.your-project-ref.supabase.co host. The shared pooler, Supavisor, runs session mode on port 5432 and transaction mode on port 6543 on a pooler.supabase.com host. Supabase's docs describe transaction mode as the one for serverless and edge functions, which open many short lived connections. Copy the exact strings from the Connect dialog in the dashboard rather than building them by hand.

Two details catch people out:

  • The shared pooler does not support prepared statements in transaction mode. For Prisma, Supabase's guide says to add pgbouncer=true to the transaction pooler string, which turns prepared statements off.
  • Direct connections use IPv6 on Free and Pro plans unless you buy the IPv4 add-on, while the shared pooler is IPv4. Some hosts cannot reach IPv6, which is another reason serverless apps should use the pooler.

For Prisma, Supabase's setup uses DATABASE_URL for the transaction pooler on 6543 and DIRECT_URL for a session or direct connection used by migrations. If you change your schema in Supabase, "How to change your Supabase database safely with migrations" covers the migration side.

How do you fix it on Neon?

Use the pooled connection string, which has -pooler in the hostname, for your app, and the direct string for migrations and admin work.

In the Neon Console, click Connect and turn on the Connection pooling toggle. The hostname changes from something like ep-name-123456 to ep-name-123456-pooler, and connections go through PgBouncer. Neon's docs say PgBouncer accepts up to 10,000 client connections, which is far more than a busy serverless app needs, while it keeps the real Postgres connections within the compute's limit.

Neon runs PgBouncer in transaction mode, so a connection is returned to the pool after each transaction. That means some session features do not work on pooled connections:

  • SET and RESET for session settings, which could leak to other clients.
  • LISTEN and NOTIFY.
  • SQL level PREPARE and DEALLOCATE, though protocol level prepared statements through your driver are supported.
  • Session level advisory locks and some temporary table behaviour.

Neon recommends pooled connections for serverless functions and web apps, and direct connections for schema migrations, pg_dump and pg_restore, and long running admin or analytics sessions.

How should Prisma be configured for serverless?

Create one Prisma client per instance outside the request handler, keep its pool small, and connect it to a pooled URL.

The Prisma docs give clear serverless guidance: instantiate PrismaClient at module scope so warm invocations reuse it, configure a small pool, do not call disconnect after every invocation, and keep the number of concurrent function instances below what the database can serve. In Next.js development, store the client on globalThis when NODE_ENV is not production, so hot reloading does not create a new client on every save.

How you set the pool size depends on your Prisma version:

  • In Prisma ORM 6 and earlier, pool size is a connection_limit parameter on the connection URL, and serverless setups often set it very low.
  • In Prisma ORM 7, the docs say pool settings live on the driver adapter, with no connection URL parameters for them. With @prisma/adapter-pg, that is the max option, whose default is 10, plus timeouts such as connectionTimeoutMillis.

For migrations, Prisma's CLI reads the URL from prisma.config.ts, which in a pooled setup should point at DIRECT_URL.

TheHoopGang creator portal homepage. The Hoop Gang's stack is Next.js, TypeScript, Prisma and Postgres, the same combination this post is about.
TheHoopGang creator portal homepage. The Hoop Gang's stack is Next.js, TypeScript, Prisma and Postgres, the same combination this post is about.

What about Vercel Fluid compute?

Fluid compute lets one function instance handle several requests, which helps, but you still need to close idle connections and use a pooler.

Vercel's guide recommends creating your pool in global scope so concurrent requests on the same warm instance share it, releasing each client after use, and keeping idle timeouts short, around 5 seconds. It also provides attachDatabasePool from the @vercel/functions package, which closes idle connections before an instance suspends so they do not linger and hold slots.

A worked example

Here is an illustration with a made-up business. A meal prep company runs its ordering app on Vercel with Prisma and a small Supabase project. On a normal day it is fine. On the Sunday a promotion email goes out, orders fail for ten minutes with "remaining connection slots are reserved".

Looking at the setup, DATABASE_URL points at the direct connection on port 5432, the Prisma client is created inside a helper that every API route calls fresh, and the pool size is the default. During the spike, dozens of instances each open several connections, and the 60 direct connections on the small compute run out.

The fix is three changes. DATABASE_URL moves to the transaction pooler on port 6543 with pgbouncer=true, and a DIRECT_URL on 5432 is added for migrations. The Prisma client moves to a single module scope instance with the globalThis pattern for development. The adapter's max pool size is set low, so one instance cannot hog the pooler. The next promotion goes out without errors, and the database dashboard shows far fewer connections at the peak.

When should you get help?

Get help if errors continue after moving to a pooler, if you see prepared statement errors you cannot explain, or if migrations start failing after the change.

These problems are usually configuration rather than code, which makes them a reasonable fit for my Quick Fix, from $60 in about 2 days. If connection errors are part of a wider set of launch problems, Fix Plus Features starts at $200 for up to three fixes and one small feature, and Finish and Launch starts at $600 in about 10 days for apps whose core features already work. For apps that are slow rather than failing, "Why is my Lovable app slow? How to find and fix it" covers the other half of database performance, and "What does it cost to run an AI-built app per month? A real budget" helps decide whether a bigger database tier is worth it.

The bottom line

Too many connections on serverless Postgres is a multiplication problem: many function instances, each with its own pool, against a fixed limit. Read the actual Postgres error, check max_connections and pg_stat_activity under load, then route your app through a pooler: Supabase transaction mode on port 6543 or a Neon -pooler hostname. Keep one small Prisma client per instance, set the pool size on the driver adapter in Prisma 7 or with connection_limit in older versions, and use direct connections only for migrations and admin work. Most apps never need a bigger database after that. They needed fewer, better shared connections.

Questions, answered in one line

What does sorry, too many clients already mean in Postgres?

It means every connection slot allowed by max_connections is in use, so Postgres refused the new connection.

Which Supabase port should a Vercel app use?

Use the transaction pooler on port 6543 for serverless runtime traffic, and the session pooler or direct connection on port 5432 for migrations.

How do I get a pooled connection string on Neon?

In the Neon Console, click Connect and turn on Connection pooling, which gives a hostname ending in -pooler that routes through PgBouncer.

Should I call disconnect after every Prisma query in a serverless function?

No. Create the client once outside the handler and reuse it, because reconnecting on every invocation is slow and does not fix the underlying limit.

Can Thomas Osayi fix connection errors in my app?

Yes, Thomas Osayi works with Next.js, Prisma and Postgres, and a connection limit problem usually fits the Quick Fix package from $60 in about 2 days.

Sources

About the author

Thomas Osayi is a full-stack developer in Los Angeles studying computer science at Loyola Marymount University. He founded and built SQWAD, a SaaS platform Shopify brands use to run creator programs, and builds custom Next.js websites, SaaS MVPs and fixes for AI-built apps for businesses and founders.

See his work · Services and prices

Related articles

Fix, finish and launch your AI-built app

Built your app with Lovable, Bolt, v0, Replit or Cursor and got stuck? I fix the bugs, connect auth and payments, clean up the code and deploy it to production.

See the service