prisma icon indicating copy to clipboard operation
prisma copied to clipboard

Postgres: too many bind variables in prepared statement on `findMany()`

Open joaomlneto opened this issue 2 years ago • 15 comments

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

  1. Create table A with a composite ID.
  2. Create table B that has an ID foreign key referencing A.
  3. Populate first table with at least 16384 items.
  4. 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

joaomlneto avatar Oct 27 '23 13:10 joaomlneto