N
Neon2y ago
national-gold

NeonDbError: prepared statement "s4908" already exists

Getting the following error: NeonDbError: prepared statement "s4908" already exists And now also: NeonDbError: prepared statement "s5046" does not exist Not sure what could cause this, as I am not using prepared statements. If it matters, I'm using Drizzle, and this is how I instantiate the db
import { Pool, neon } from '@neondatabase/serverless'
import { drizzle } from 'drizzle-orm/neon-http'
import { drizzle as drizzlePool } from 'drizzle-orm/neon-serverless'
import * as schema from './schema'
export * from 'drizzle-orm'
export type { PgEnum } from 'drizzle-orm/pg-core'
export * from './schema/index'
export { db, poolDb }

if (!process.env.DRIZZLE_DATABASE_URL) {
throw new Error('Missing DRIZZLE_DATABASE_URL')
}
const sql = neon(process.env.DRIZZLE_DATABASE_URL!)

sql`CREATE EXTENSION IF NOT EXISTS vector`

const db = drizzle(sql, { schema })

const pool = new Pool({
connectionString: process.env.DRIZZLE_DATABASE_URL,
})
const poolDb = drizzlePool(pool, { schema })
import { Pool, neon } from '@neondatabase/serverless'
import { drizzle } from 'drizzle-orm/neon-http'
import { drizzle as drizzlePool } from 'drizzle-orm/neon-serverless'
import * as schema from './schema'
export * from 'drizzle-orm'
export type { PgEnum } from 'drizzle-orm/pg-core'
export * from './schema/index'
export { db, poolDb }

if (!process.env.DRIZZLE_DATABASE_URL) {
throw new Error('Missing DRIZZLE_DATABASE_URL')
}
const sql = neon(process.env.DRIZZLE_DATABASE_URL!)

sql`CREATE EXTENSION IF NOT EXISTS vector`

const db = drizzle(sql, { schema })

const pool = new Pool({
connectionString: process.env.DRIZZLE_DATABASE_URL,
})
const poolDb = drizzlePool(pool, { schema })
7 Replies
national-gold
national-goldOP2y ago
I have pgbouncer=true on my connection string
eastern-cyan
eastern-cyan2y ago
I have the same issue using Prisma Accelerate within Cloudflare Workers, for the past ~5 days
Invalid `prisma.content.findFirst()` invocation:


Error occurred during query execution:
ConnectorError(ConnectorError { user_facing_error: None, kind: QueryError(PostgresError { code: "26000", message: "prepared statement \"s366\" does not exist", severity: "ERROR", detail: None, column: None, hint: None }), transient: false })
Invalid `prisma.content.findFirst()` invocation:


Error occurred during query execution:
ConnectorError(ConnectorError { user_facing_error: None, kind: QueryError(PostgresError { code: "26000", message: "prepared statement \"s366\" does not exist", severity: "ERROR", detail: None, column: None, hint: None }), transient: false })
import { PrismaClient } from '@prisma/client/edge';
import { withAccelerate } from '@prisma/extension-accelerate';

const createPrismaClient = ({ databaseUrl }: { databaseUrl: string }) =>
new PrismaClient({
datasourceUrl: databaseUrl,
}).$extends(withAccelerate());

export type PrismaClientType = ReturnType<typeof createPrismaClient>;

export default createPrismaClient;
import { PrismaClient } from '@prisma/client/edge';
import { withAccelerate } from '@prisma/extension-accelerate';

const createPrismaClient = ({ databaseUrl }: { databaseUrl: string }) =>
new PrismaClient({
datasourceUrl: databaseUrl,
}).$extends(withAccelerate());

export type PrismaClientType = ReturnType<typeof createPrismaClient>;

export default createPrismaClient;
I am using the "-pooler" suffix connection string with: pgbouncer=true&sslmode=require&connect_timeout=15&pool_timeout=15 My connection string has the following parameters:
pgbouncer=true
sslmode=require
connect_timeout=15
pool_timeout=15
pgbouncer=true
sslmode=require
connect_timeout=15
pool_timeout=15
eastern-cyan
eastern-cyan2y ago
I found this comment on the neondatabase GitHub repository https://github.com/neondatabase/neon/issues/4596#issuecomment-1873785419
Hey. we released a relevant pgbouncer version but were forced to roll it back because of Prisma support. The fix was placed into pgbouncer (pgbouncer/pgbouncer#972), and we already have this version on production, but with disabled prepared statements – the idea to roll it out gradually in January'24.
GitHub
Epic: Support Prepared Statements in PgBouncer · Issue #4596 · neon...
Motivation There were a bunch of requests from users about it. DoD As a user, I can create a prepared statement using the pooled connection. As a user, I can execute the prepared statement with dif...
stormy-gold
stormy-gold2y ago
I'm having the same issue with Drizzle + Neon. I'm also using the pooler url and have the pgbouncer=true flag set. When I refresh the page, the request goes through. This issue is making the app unusable. Is there a suggested workaround? Like the code above, I'm also making a few DB calls inside a await Promise.all([...]). Interestingly, refactoring promise.all to N separate awaits appears to remediate the issue, but this has an impact on performance. I'm still very interested in a work-around that still allows me to make multiple async calls at once. The issue also seems to happen when I'm reloading the Astro dev sever. Perhaps that's related too.
fascinating-indigo
fascinating-indigo2y ago
@san4d can you share a small reproduction? If I understand correctly you're issuing queries (not using prepared statements) through the pooled URL, and are receiving an error?
stormy-gold
stormy-gold2y ago
I was not intentionally using prepared statements, but something about my setup seems to be using them. I followed the adjustments in this issue to resolve my issue: https://discord.com/channels/1176467419317940276/1176467419938701375/1201569843258855585 My queries were selects with some joins. Here are the three concurrent queries, though I'm not sure which caused the error.
Query: select "profiles"."id", "profiles"."created_at", "profiles"."modified_at", "profiles"."user_id", "profiles"."first_name", "profiles"."last_name", "profiles"."active_workspace_id", "workspaces"."id", "workspaces"."created_at", "workspaces"."modified_at", "workspaces"."name", "workspaces"."owner_user_id", "workspaces"."workspace_state" from "sbl_profile"."profiles" inner join "sbl_workspace"."workspaces" on "profiles"."active_workspace_id" = "workspaces"."id" where "profiles"."user_id" = $1 -- params: ["a-uuid"]

Query: select "type", "subtype", "mask", "short_name", coalesce(account.available_balance, account.current_balance), "limit", case when account.limit is not null THEN round(account.current_balance/account.limit*100) end, "institution_connection_id" from "sbl_ledger"."account" where "account"."workspace_id" = $1 -- params: ["a-uuid"]

Query: select "institution"."name", "institution"."primary_color_hex", "institution"."url", "institution"."logo_base_64", "institution_connection"."id", "institution_connection"."status" from "sbl_ledger"."institution_connection" inner join "sbl_ledger"."institution" on "institution_connection"."institution_id" = "institution"."id" where "institution_connection"."workspace_id" = $1 -- params: ["a-uuid"]

Query: select "profiles"."id", "profiles"."created_at", "profiles"."modified_at", "profiles"."user_id", "profiles"."first_name", "profiles"."last_name", "profiles"."active_workspace_id", "workspaces"."id", "workspaces"."created_at", "workspaces"."modified_at", "workspaces"."name", "workspaces"."owner_user_id", "workspaces"."workspace_state" from "sbl_profile"."profiles" inner join "sbl_workspace"."workspaces" on "profiles"."active_workspace_id" = "workspaces"."id" where "profiles"."user_id" = $1 -- params: ["a-uuid"]

Query: select "type", "subtype", "mask", "short_name", coalesce(account.available_balance, account.current_balance), "limit", case when account.limit is not null THEN round(account.current_balance/account.limit*100) end, "institution_connection_id" from "sbl_ledger"."account" where "account"."workspace_id" = $1 -- params: ["a-uuid"]

Query: select "institution"."name", "institution"."primary_color_hex", "institution"."url", "institution"."logo_base_64", "institution_connection"."id", "institution_connection"."status" from "sbl_ledger"."institution_connection" inner join "sbl_ledger"."institution" on "institution_connection"."institution_id" = "institution"."id" where "institution_connection"."workspace_id" = $1 -- params: ["a-uuid"]

fascinating-indigo
fascinating-indigo2y ago
Ah, OK. Good to know you have a workaround for now. It sounds like the ongoing issue we've seen with pgbouncer, and some drivers using them during initialisation. We hope to roll out a fix for that rolled out soon.

Did you find this page helpful?