Connection Pool Timeout with Prisma and Vercel Postgres

I’m building a NextJS app with prisma and the Vercel Postgres database. Like recommended in the docs, I added this datasource to my schema.prisma:

datasource db {
  provider = "postgresql"
  url = env("POSTGRES_PRISMA_URL")
  directUrl = env("POSTGRES_URL_NON_POOLING")
}

Generally this seems to work fine but randomly I sometimes get the error “Timed out fetching a new connection from the connection pool. More info: Connection pool | Prisma Documentation (Current connection pool timeout: 10, connection limit: 5)” from Prisma.
When using another database provider I don’t see those problems and there’s next to no load on that website so it shouldn’t reach any connection limits. I tried customizing the connection string to change timeouts or limits but that doesn’t seem to work.

What’s the issue with the connection here? Do I have some wrong settings?

Hey @vantezzen, thanks or reporting this issue. I was able to reproduce the problem. In my testing it occurs when Vercel Postgres goes from idle to active state. By default Vercel Postgres scales to zero after 5 minutes of inactivity. When it gets woken up is when the timeout occurs.

However, this only happens with Prisma so there is some kind of issue there. I reported the problem upstream so we can find a long-term solution.

In the meantime you can try these workarounds

Option A:

If you don’t have much load anyway you can try using the non-pooled direct connection

datasource db {
  provider = "postgresql"
  url = env("POSTGRES_URL_NON_POOLING")
  directUrl = env("POSTGRES_URL_NON_POOLING")
}

That seems to fix the issue. Prisma has some recommendations regarding connection limits that you can try as well.

Option B:

You can configure the time Vercel Postgres waits before scaling to zero. You can increase the suspend delay or disable it entirely. Keep in mind that this means the database is longer active and generates more Compute Hours.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.