prisma: Postgres: too many bind variables in prepared statement on `findMany()`
Bug description
findMany() fails with error:
PrismaClientKnownRequestError:
Invalid `prisma.a.findMany()` invocation:
Assertion violation on the database: `too many bind variables in prepared statement, expected maximum of 32767, received 32769`
at Cn.handleRequestError (/Users/joaomlneto/git/joaomlneto/prisma-bug/node_modules/@prisma/client/runtime/library.js:123:6817)
at Cn.handleAndLogRequestError (/Users/joaomlneto/git/joaomlneto/prisma-bug/node_modules/@prisma/client/runtime/library.js:123:6206)
at Cn.request (/Users/joaomlneto/git/joaomlneto/prisma-bug/node_modules/@prisma/client/runtime/library.js:123:5926)
at async l (/Users/joaomlneto/git/joaomlneto/prisma-bug/node_modules/@prisma/client/runtime/library.js:128:9968)
at async file:///Users/joaomlneto/git/joaomlneto/prisma-bug/index.ts:22:1 {
code: 'P2035',
clientVersion: '5.5.2',
meta: {
database_error: 'too many bind variables in prepared statement, expected maximum of 32767, received 32769'
}
}
How to reproduce
- Create table
Awith a composite ID. - Create table
Bthat has an ID foreign key referencingA. - Populate first table with at least
16384items. - Attempt to execute
findMany()on the first table, while also selecting related entries from the second table.
const isInitialized = (await prisma.a.count()) > 0;
if (!isInitialized) {
console.log('going to initialize')
await prisma.a.createMany({
data: [...Array(16384).keys()].map(i => ({
key1: i,
key2: i,
}))
})
console.log('initialized successfully!')
}
console.log('going to run buggy query')
await prisma.a.findMany({
select: {
key1: true,
key2: true,
children: {
select: {
bKey: true,
},
},
},
})
Expected behavior
Query executes successfully.
Prisma information
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model A {
key1 Int
key2 Int
children B[]
@@id([key1, key2])
}
model B {
a A @relation(fields: [key1, key2], references: [key1, key2])
key1 Int
key2 Int
bKey Int
@@id([key1, key2, bKey])
}
await prisma.a.findMany({
select: {
key1: true,
key2: true,
children: {
select: {
bKey: true,
},
},
},
})
Environment & setup
- OS: macOS
- Database: PostgreSQL, CockroachDB
- Node.js version:
v20.9.0
Prisma Version
prisma : 5.5.2
@prisma/client : 5.5.2
Current platform : darwin-arm64
Query Engine (Node-API) : libquery-engine aebc046ce8b88ebbcb45efe31cbe7d06fd6abc0a (at node_modules/@prisma/engines/libquery_engine-darwin-arm64.dylib.node)
Schema Engine : schema-engine-cli aebc046ce8b88ebbcb45efe31cbe7d06fd6abc0a (at node_modules/@prisma/engines/schema-engine-darwin-arm64)
Schema Wasm : @prisma/prisma-schema-wasm 5.5.1-1.aebc046ce8b88ebbcb45efe31cbe7d06fd6abc0a
Default Engines Hash : aebc046ce8b88ebbcb45efe31cbe7d06fd6abc0a
Studio : 0.494.0
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 11
- Comments: 18 (9 by maintainers)
Commits related to this issue
- web: workaround for prisma/prisma#21648 — committed to joaomlneto/jitaspace by joaomlneto 3 years ago
😆 It happens with both 😃 The example above is for PostgreSQL. However, I just changed the
providertocockroachdbin myschema.prismaand could reproduce it with CockroachDB as well.Since CockroachDB’s speaks primarily(?) PostgreSQL and this error is related to the query generated, combined with issues related to the PostgreSQL driver, it makes sense in my head. I think it’s mostly similar to https://github.com/prisma/prisma/issues/9326#issuecomment-1260546512, but maybe also https://github.com/prisma/prisma/issues/8832, and the number 32768 also appears on open issues https://github.com/prisma/prisma/issues/19901 https://github.com/prisma/prisma/issues/20760.
I have just hit this error myself, after adding relations to a large table.
@janpio You wrote:
I was able to work around this problem by batching my
findManycalls into batches of 30k with simpletakeandskiplogic.It’s been almost a year since this issue was opened, and it is trivial to reproduce. Can we expect a fix anytime soon? A userland “fix” is ~10 LOC, so it would be great to see this fixed properly.
Hey folks,
We have spotted this issue as potentially being solved by the new
relationJoinspreview feature, which usesJOINs to resolve relations in a single query.If you have the time, we’d love for you to try it out and let us know if it has indeed fixed your issue. You can learn more about it on our release notes here.
Thank you 🙏
This has solved the issue for me.
Can you please also open a new issue @elaskowski? Please share the schema and full query. This is just one error message, that can have many reasons and we need to know about them to be able to fix them. Thanks.
Simpler recreation, no FKs or compound ids req’d:
If I remove the two arguments (name and companyId) it chunks exactly as expected:
Looks related to other issues like you mentioned
This message mentions it was fixed in
4.4.0https://github.com/prisma/prisma/issues/8832#issuecomment-1260533913, related tests: https://github.com/prisma/prisma/blob/main/packages/client/tests/functional/issues/8832/tests.ts Obviously you have a more recent version here, so it’s something to be reproduced and investigated.Just noticed it with the newest Prisma version.
I’ve just migrated away from MySQL/Vitess and things break using Postgres/Cockroach.