prisma: Prepared statement caching is data dependent on numeric input parameters (`incorrect binary data format in bind parameter x`)

Bug description

When I run a raw query with a numeric parameter, the sequence of values for that parameter can cause error 22PO3 - incorrect binary data format.

Related issue: https://github.com/prisma/prisma/issues/16611

How to reproduce

Test code:

await prisma.$queryRaw(
    Prisma.sql`select * from version() where LENGTH("version") > ${1}`,
  );

  await prisma.$queryRaw(
    Prisma.sql`select * from version() where LENGTH("version") > ${1.1}`,
  );

Success cases:

  • Run query with decimal value (1.1) first, then query with integer value.

Failure cases:

  • Run query with integer value (1) first, then query with decimal value. Throws code 22PO3 - incorrect binary data format.

Workarounds:

  • Disabling Prisma’s prepared statement cache (?statement_cache_size=0) prevents the issue from occurring.
  • Manually serializing the input value to a string and then casting to a Postgres decimal type separately prevents the issue from occurring ( ${n.toFixed(2)}::decimal )

Expected behavior

Prisma prepared statement caching should not prevent varying numeric types from being passed in as parameters.

Prisma information

See test code above.

Environment & setup

  • OS: macOS
  • Database: PostgreSQL
  • Node.js version: v18.18.2

Prisma Version

5.7.1

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 9
  • Comments: 15 (5 by maintainers)

Commits related to this issue

Most upvoted comments

@janpio @jkomyno Any updates on this? My team has been using the (undocumented) workaround of setting statement_cache_size=0 but it seems like this has been causing production issues for a lot of folks – any timeline for a fix or anything we can do to help?

Thanks everyone! The issue should be fixed and the fix will be shipped as a part of 5.15.0 release next week. As my co-worker mentioned, you can try out the fix now by using 5.15.0-dev.47 snapshot. We do not recommend using snapshot versions in production, but it’s enough to verify the fix. Feel free to comment if that version does not fix the problem for you.

@janpio looks like my @prisma/client hadn’t updated so thank you for double checking. It worked!

Thank you for reporting the issue @lbebels!

If you are not using driver adapters for Postgres, can you please try this version and tell us if it fixes the issue? Note that this is a dev version and it is not suitable for production use.

npm i prisma@5.15.0-dev.47
npm i @prisma/client@5.15.0-dev.47

@janpio @jkomyno Any updates here? This seems to be a pretty severe issue; it was causing major production outages for us in unpredictable ways (because it’s dependent on the order in which you issue similar-looking queries), and the only available workaround is the undocumented statement_cache_size=0. Are there any updates or a timeline for a fix?

@jkomyno I am able to reproduce a similar issue by switching between arrays of null values and arrays of text values: https://github.com/benweissmann/prisma-22482-repro – that produces the error Raw query failed. Code: 08P01. Message: ERROR: insufficient data left in message.

As you’ll see in that reproduction, the issue is related to the prepared statement cache: running just one query per client instance does not trigger the bug, and disabling the prepared statement cache works around the bug.