Repository navigation
Description
Bug description
Hey guys, I hope you all are doing well!
I'm running prisma @ 2.14 on MySQL, and after updating to and trying multiple versions (2.18, 2.20.1, 2.21.1), I'm running into issues where my Prepared_stmt_count global MySQL variable is increased by a huge factor compared to 2.14, which leads to me hitting the MAX_PREPARED_STATEMENTS limit. It seems like there is a difference in behavior of these prepared statements from 2.14 and onwards.
Just to reiterate, this is not a problem on 2.14, and was I wondering if there was anything I could do to get the old behavior back on a newer version.
Thank you!
How to reproduce
I'm running this on a production kube deployment, so I'm unsure what specific queries may be triggering this, but I do know that 2.14 and previous never had this behavior
- Be on similar env ?
- Use normally (create/update/delete) (many)
Expected behavior
Expect the Prepared_stmt_count to stay low, this is the current behavior on 2.14.
Actual behavior
Prepared statement count balloons, and stays that way for a long enough time to where my set MAX_PREPARED_STMT_COUNT gets triggered and hangs the db. Note that the actual value in prod gets to 64000+.
Prisma information
Environment & setup
- OS: Mac / Ubuntu
- Database: MySQL 8.0.21
- Node.js version: 12.16.1
- Prisma version: Tested on 2.17, 2.18, 2.20.x, 2.21.x
prisma : 2.17.0
@prisma/client : 2.17.0
Current platform : darwin
Query Engine : query-engine 3c463ebd78b1d21d8fdacdd27899e280cf686223 (at node_modules/@prisma/engines/query-engine-darwin)
Migration Engine : migration-engine-cli 3c463ebd78b1d21d8fdacdd27899e280cf686223 (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core 3c463ebd78b1d21d8fdacdd27899e280cf686223 (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary : prisma-fmt 3c463ebd78b1d21d8fdacdd27899e280cf686223 (at node_modules/@prisma/engines/prisma-fmt-darwin)
Studio : 0.353.0


Activity
janpio commentedon Apr 30, 2021
Interesting, I was not aware there is a limit on the # of prepared statements. After 2.14 we probably changed the usage and behavior of queries. We most probably did not consider this limitation yet, or did not expect it to be relevant.
Is this limit per connection or per server (restart)?
What does the error/crash look like?
rasberrylasagna commentedon Apr 30, 2021
Hey there!
I actually had no idea it was a thing either until I ran into this.
It's a per server limit, that can be potentially fixed by raising the limit (a global var in MySQL), but I didn't know if this was a symptom of something bigger and didn't want to band-aid something that may continue getting (much) bigger.
So the "normal" behavior (as of 2.14) was that the prepared statement count rises gradually through use, and quickly resets itself back down to <100 values.
Unfortunately I don't have a great error, but this is a snippet of it.
Error behavior:
This would lead to huge drops in coverage as no queries can go through, effectively halting all traffic.
Thanks for looking into this! :)
janpio commentedon Apr 30, 2021
Ok, it is a per server limit but I think the prepared statements are held by connection - so "rotating" the connections could probably help. You could do so by getting a new
PrismaClientinstance - depending on your application that might be as simple as restarting, or maybe writing some logic that every x hours a newprismais created and used (and the old callsprisma.$disconnect()first).There is nothing really in the 2.15 release notes that pop out to me: https://github.com/prisma/prisma/releases/tag/2.15.0
Can you tell us a bit more about the queries your application is running?
How many connections to the database are open at any time?
How many app servers? Prisma connection_limit (/pool size)? Serverless?
rasberrylasagna commentedon Apr 30, 2021
That's fair. I'm happy to adapt and implement workarounds if the changes here are actually intended / a necessary side effect.
And understood. Before continuing, I'm actually just going to verify and make sure that 2.15 breaks it or if it's a later version to potentially make troubleshooting easier, and report back with findings.
I might have went straight to 2.17 to test a preview feature with groupBy/filter on relationals.
I'll test with 2.15 all the way up to my first confirmed version where I come across this issue.
Thanks again for the receptive help.
janpio commentedon Apr 30, 2021
These workarounds are of course only meant as temporary fixes to allow you to upgrade your Prisma version - we will obviously still try to understand and fix this as soon as possible. Right now we are a bit lost though as first manual reproductions did not show an increasing prepared statement count.
rasberrylasagna commentedon May 6, 2021
Ahh understood. Much appreciated.
The results from my findings is that 2.15 is indeed where the issue starts.
It is a GQL based backend that handles all of the traffic for a production deployment, so it runs about 6 pods that use prisma.
I don't think we do anything special with our prisma deployment, we strictly just use the client feature (no migrate etc) and we use all the basic CRUD functionality, as well as groupBy/aggregate queries. We have used default config for connection limit/pool size (didn't edit it). We also utilize the basic dataloader as implemented here: https://github.com/graphql/dataloader for most of our nested gql resolvers.
Regarding to the things that connect to the DB, there are other things that connect to the database as well, so that bumps the total number up to 10, with between 40-55 simultaneous connections. There are no serverless access to the db.
Regarding reproducibility, for us it doesn't seem to take long to detect the discrepancy from one version to another, but I don't know if this would extend to other deployments.
Thank you!
Simskii commentedon May 12, 2021
We migrated from Prisma 1 to prisma 2.20 yesterday and came across this issue aswell.
We had our app running for about 20 minutes until we hit the prepared statement limit.
However if we downgrade to 2.14 it works like expected and the prepared statements keeps reseting and we do not hit the limit.
olif commentedon May 12, 2021
We have the same issue, the
Prepared_stmt_countgets exhausted and reaches themax_prepared_stmt_countlimit after a while. Theprepared_stmt_countis monotonically increasing which means that this limit will eventually be reached regardless of load and/or number of connections.From what I can tell, it seems like the connection-pooling did not work in
prisma 2.14which meant that new connections where spawned and old ones closed. When the old connections where closed, the prepared statements belonging to that connection/session where subsequently dropped by MySql.61 remaining items
janpio commentedon Jul 11, 2021
2.25.0 added a configuration to change this:
We might change the default value via #7727
thomaschaaf commentedon Aug 23, 2021
I actually believe this is actually caused by #8870
ssukienn commentedon Aug 26, 2021
We hit this for the first time during load testing in our architecture. Prisma version 2.29.1.
Setup is something like 4 services * 2 pods * 3 connections in pool.
We will probably increase max value but do you recommend approaching it differently maybe?
EDIT: Sorry, one service was 2.24.1. Might be it, will confirm.
EDIt2: Ok two services were 2.24.1 but another one with 2.29.1 also faced the same problem. Can it be because of bug from the former two even when these are different connections? The max value is server-based so I assume so?
pimeys commentedon Aug 27, 2021
Mught be this issue... #8870
homerjam commentedon Jan 5, 2022
I just hit this issue whilst seeding using
3.7.0, unfortunately, it seems to be inconsistent. Is the suggested fix as below (statement_cache_size=0being pertinent)?DATABASE_URL='mysql://root@localhost:3306/mydb?pool_timeout=0&statement_cache_size=0'cclaflin89 commentedon Feb 16, 2022
I'm also having the same max_prepared_stmt_count issue after setting
pool_timeout=0on prisma 3.7.0. Let me setstatement_cache_size=0and see how it works.janpio commentedon Feb 16, 2022
pool_timeout=0is a way to make sure all the underlying errors have a chance to bubble up and not be swallowed by the timeout.statement_cache_size=0also is the big hammer, completely disabling the statement cache. You should do some math instead to see what value you can set here (per container/pod/server) that will still fit into your overallprepared_stmt_countlimit of your MySQL installation.chanlito commentedon Apr 5, 2022
We're currently experiencing this issue. Is there a recommended approach to handle this?
PanMan commentedon Apr 5, 2022
We seem to be also running into this: today and yesterday our db connection "crashed" at the busiest time with the
Can’t create more than max_prepared_stmt_count statementserror. Is there anything we can do besides lower thestatement_cache_sizeto 500?janpio commentedon Apr 5, 2022
That should be enough (if the # of applications/servers using Prisma Client, multiplicated by the configured cache size is lower than the limit). If you are still getting the error, you probably have more applications created prepared statements or something else is weird.
ygoyal25 commentedon May 4, 2023
Hi,
We are still facing this issue for prisma version 4.10.1 the prepared_statement_count blows up on production. We have tried setting statement_cache_size value as well but it doesn't seem to work.
janpio commentedon May 17, 2023
Please open a new issue and provide all the information the issue template asks for, so we can hopefully reproduce, debug and fix. Thanks.