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.

Raw query failed when trying to insert batch of data

See original GitHub issue

Bug description

I have a rawInsert function which is in charge of executing a raw query to insert multiple rows in a table. Function header looks like this:

export default async <T extends Record<string, any>>( table: string, fields: DistributiveKeyof<T>[], values: T[], opts?: { onConflict?: { target: (keyof T)[]; action: 'update' } }, ): Promise<number>

When I try to load test my API using k6 this function throws an error. This error happens when my batch size gets more than ~5000 rows. Error:

Raw query failed. Code: `N/A`. Message: `N/A`

How to reproduce

  1. generate a query like below and execute it using executeRaw. make sure you insert more than 5000 rows in this query. INSERT INTO table_name (col1, col2, col3, col4, col5, col6) VALUES ($1, $2, $3, $4, $5, $6), ($7, $8, $9, $10, $11, $12), ...
  2. run a load test over this query using k6. iterating through this also reproduces this error.
  3. run the exact same query using pg driver and everything is working just fine.

Expected behavior

To have a clear error message and insert all of my batch data rows into table.

Prisma information

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["selectRelationCount"]
}

datasource db {
  provider = "postgresql"
}

Environment & setup

  • OS: Mac OS
  • Database: PostgreSQL
  • Node.js version :v14.17.6

Prisma Version

prisma                  : 3.0.2
@prisma/client          : Not found
Current platform        : darwin
Query Engine (Node-API) : libquery-engine 2452cc6313d52b8b9a96999ac0e974d0aedf88db (at ../../.npm/_npx/52649/lib/node_modules/prisma/node_modules/@prisma/engines/libquery_engine-darwin.dylib.node)
Migration Engine        : migration-engine-cli 2452cc6313d52b8b9a96999ac0e974d0aedf88db (at ../../.npm/_npx/52649/lib/node_modules/prisma/node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine    : introspection-core 2452cc6313d52b8b9a96999ac0e974d0aedf88db (at ../../.npm/_npx/52649/lib/node_modules/prisma/node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary           : prisma-fmt 2452cc6313d52b8b9a96999ac0e974d0aedf88db (at ../../.npm/_npx/52649/lib/node_modules/prisma/node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash    : 2452cc6313d52b8b9a96999ac0e974d0aedf88db
Studio                  : 0.423.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
millspcommented, Sep 8, 2022

Internal conversation https://prisma-company.slack.com/archives/C040N0UADF0/p1662679627797069 Reproduction https://github.com/prisma/prisma/pull/15246 Todo Update snapshots once fix landed in the engines

0reactions
jkomynocommented, Sep 28, 2022

Hi, we discovered this issue was due to a validation check in the Postgres driver we use that we didn’t expect. Specifically, even though prepared statements in Postgres support up to 65535 parameters (also known as bind variables), the driver we currently use supports half that parameters (one can create prepared SQL statements in Prisma via e.g. $executeRaw and $queryRawUnsafe). This caused us to fall back to the generic error message:

Raw query failed. Code: N/A. Message: N/A

With the latest release, prisma@4.4.0, we now show a clearer error message (with error code P2035) when the Prisma client prepares more than 32767 bind variables:

Assertion violation on the database: too many bind variables in prepared statement, expected maximum of 32767, received ...

Thus, we now consider this issue solved.

Example

Given this schema:

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgres"
  url      = env("DATABASE_URL")
}

model Tag {
  id Int @id
  @@map("tag")
}

Here’s an example of a query that fails with error code P2035 due to hitting the maximum amount of bind variables:

const n = 32768

// [1, 2, ..., 32768]
const ids = Array.from({ length }, (_, i) => i + 1)

const data = ids.map((id) => ({ id }))
await prisma.tag.createMany({
  data,
})

// ['$1', '$2', ..., '$32768']
const idsParams = ids.map((paramIdx) => `\$${paramIdx}`)

const tags = await prisma.$queryRawUnsafe<unknown[]>(
  `
  SELECT *
  FROM tag
  WHERE "id" IN (${idsParams.join(', ')})
`,
  ...ids,
)

expect(tags.length).toBe(n)

The same query with n <= 32767 succeeds.

Read more comments on GitHub >

github_iconTop Results From Across the Web

MySQL batch insert fails, no error - Stack Overflow
I'm trying to insert thousands of records of user-data into a different format in the same database, later to be exported to a...
Read more >
Working With Line Numbers and Errors Using Bulk Insert
In this blog post, we look at these techniques using T-SQL's native bulk insert (Line Numbers and Errors Using Bulk Insert).
Read more >
Database Engine events and errors - SQL Server
Consult this MSSQL error code list to find explanations for error messages for SQL Server database engine events.
Read more >
How to INSERT If Row Does Not Exist (UPSERT) in MySQL
This means that an INSERT IGNORE statement which contains a duplicate value in a UNIQUE index or PRIMARY KEY field does not produce...
Read more >
INSERT INTO - Amazon Athena - AWS Documentation
When running an INSERT query on a table with underlying data that is encrypted in ... Attempting to do so may result in...
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