FG
🗄️ DatabasesVercelproduction

Prisma "prepared statement already exists" error in serverless environments

Fresh4 months ago
Mar 14, 20260 views
Confidence Score67%
67%

Problem

Prisma throws 'prepared statement "s0" already exists' in serverless functions (Vercel, AWS Lambda, Netlify). Prisma uses named prepared statements for performance. In serverless, multiple function instances may share a PostgreSQL connection via a pooler. When a new invocation reuses a connection that already has a prepared statement with the same name from a previous invocation, PostgreSQL rejects it.

Error Output

PrismaClientKnownRequestError: 
Invalid `prisma.user.findMany()` invocation:

prepared statement "s0" already exists

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Moderate Confidence Fix
64% confidence67% success rate3 verificationsLast verified Mar 14, 2026

Add pgbouncer=true to DATABASE_URL and use Prisma singleton

Medium Risk

In serverless, multiple function instances share PostgreSQL connections via a pooler. Prisma uses named prepared statements. When a connection is reused by a different Prisma instance, the statement name collides.

64

Trust Score

3 verifications

67% success
  1. 1

    Append pgbouncer=true to DATABASE_URL

    This disables prepared statements, making Prisma safe for connection poolers:

    bash
    DATABASE_URL="postgresql://user:pass@host:5432/db?pgbouncer=true&connection_limit=1"
  2. 2

    Use a Prisma singleton to prevent multiple instances

    In lib/prisma.ts:

    typescript
    import { PrismaClient } from '@prisma/client'
    
    const globalForPrisma = globalThis as unknown as { prisma: PrismaClient | undefined }
    export const prisma = globalForPrisma.prisma ?? new PrismaClient()
    if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma

Validation

Run 100 concurrent requests hitting the database. No prepared statement errors in logs.

Verification Summary

Worked: 3
Partial: 1
Failed: 2
Last verified Mar 14, 2026

Sign in to verify this fix

Environment

Product
Prisma + PostgreSQL (Serverless)
Version
5.x
Environment
production

Submitted by

AC

Alex Chen

2450 rep

Tags

prismapostgresqlserverlessprepared-statementsconnection-pool