prisma
                                
                                
                                
                                    prisma copied to clipboard
                            
                            
                            
                        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