diff --git a/lib/db/client.ts b/lib/db/client.ts index 17056a24f..86a40e231 100644 --- a/lib/db/client.ts +++ b/lib/db/client.ts @@ -11,8 +11,19 @@ function createPrismaClient() { throw new Error("DATABASE_URL environment variable is required"); } + // Without an explicit cap, pg.Pool opens up to 10 connections per process and + // the adapter was only given the connection string. On serverless every warm + // instance builds its own pool, so two of them already exceeded the 15 + // connections of Supabase's free session pooler — surfacing as Prisma's + // "Can't reach database server", which reads like an outage rather than an + // exhausted pool. The cap is per-environment: small on Vercel, a bit higher + // on the worker, which is a single long-lived process. return new PrismaClient({ - adapter: new PrismaPg(databaseUrl), + adapter: new PrismaPg({ + connectionString: databaseUrl, + max: Number(process.env.DB_POOL_MAX ?? 3), + idleTimeoutMillis: 10_000, + }), }); }