question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

prepared statement \"s0\" already exists

See original GitHub issue

Bug description

I’m connecting to Supabase with their connection pooling enabled and I’m seeing the following 500 errors after multiple refreshes of the same page, until eventually it forces me to have to restart my server.

Supabase Connection Pooling

Connection String (Pool Mode = Transaction): postgres://postgres:[YOUR-PASSWORD]@[host].supabase.co:6543/postgres

With Supabase’s connection pooling I still see the 500 error that is shown below.

Prisma PGBouncer

Connection String: "postgresql://postgres:[PASSWORD]@[HOST].supabase.co:5432/postgres?pgbouncer=true"

With Prisma’s PG Bouncer I am still getting the same error after multiple refreshes of the same page. This is very problematic because very quickly I am getting these errors. Even when testing locally.

Error:

"PrismaClientUnknownRequestError: Error occurred during query execution:
ConnectorError(ConnectorError { user_facing_error: None, kind: QueryError(Error { kind: Db, cause: Some(
DbError { severity: \"ERROR\", parsed_severity: Some(Error), code: SqlState(\"42P05\"), 
message: \"prepared statement \\"s0\\" already exists\", detail: None, hint: None, position: None, where_: None, schema: None, table: None, column: None, datatype: None, constraint: None, file: Some(\"prepare.c\"), line: Some(447), routine: Some(\"StorePreparedStatement\") 
}) }) })\n}"

How to reproduce

Expected behavior

Prisma queries should be executed normally when connected to a pgbouncer pool or to supabase’s connection pooling.

Prisma information

"@prisma/client": "^3.5.0",

Environment & setup

  • OS: Mac OS 12.0.1
  • Database: PostgreSQL 12
  • Node.js version: v16.3.0

Prisma Version

3.5.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

16reactions
dan-turnercommented, Oct 5, 2022

@woniesong92 I just encountered this same issue and stumbled upon this issue via googling the error

What I’ve found is, if you’ve ever accidentally connected to the without ?pgbouncer=true added to the connection string, you will keep getting this error (even after adding ?pgbouncer=true) until you restart the Supabase project.

TL;RD; Never connect to Supabase without ?pgbouncer=true. If you do by accident, restart the Supabase project.

7reactions
janpiocommented, Mar 2, 2022

This problem turned out to ultimately be caused by usage of a pooled connection string (port 6543 at Supabase) without supplying pgbouncer=true. When that was added, the error went away.

But the cause for this is a bit more complicated: @juanzgc has a Next.js app where he is using a .env.local file. When using the Prisma CLI, only the .env file is loaded, and not the .env.local. Hence Juan created a .env with a copy of the database. Then he later messed around with things, and thinking that Prisma would only use .env, only added pgbouncer=true to the connection string there. But his Next.js app on startup loads both .env.local and .env, which meant that Prisma in the end was not using the value from .env but .env.local instead - which was missing the pgbouncer=true. The weird thing now is that if you only have a .env.local file, Prisma still complains about an env var not being set when running queries inside the Next.js app, although that clearly loaded the env var when starting. This is what caused him to be in this weird position where it was easy to get this wrong.

Read more comments on GitHub >

github_iconTop Results From Across the Web

prepared statement \"s0\" already exists when connecting to ...
Connect with SSL config the above. Run any query. Expected behavior. Prisma queries should be executed normally when connected to a pgbouncer ...
Read more >
Davis on Twitter: "@TedSpare @supabase @prisma @nextjs ...
prepared statement \"s0\" already exists when connecting to pgbouncer with ssl · Issue #4752 ·... Bug description I'm connecting to postgres on digital...
Read more >
prepared statement already exists - Stack Overflow
The already existing prepared statement is created by a previous execution with the same db connection.
Read more >
Configure Prisma Client with PgBouncer
Prisma cleans up already present prepared statements in the connection by running DEALLOCATE ALL before preparing and executing Prisma Client queries.
Read more >
[Guide] Migrations & Connection Pool URLs (Supabase)
Error: undefined: Database error Error querying the database: db error: ERROR: prepared statement "s0" already exists.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found