Repository navigation
Labels
P4: nice to haveFor issues that are not bugs.dependencyFor issues and PRs. Things that are related to one or more dependencies.existing workaroundFor issues. There is a known workaround for this issue.status: understoodFor issues. Applied when the issue is understood / reproducible.
Description
When my server runs for a period of time, this prompt appears.
"can't create more than max_prepared_stmt_count statements".I checked the status of MySQL and got the following results:
mysql> SHOW GLOBAL STATUS LIKE 'com_stmt%';
+-------------------------+---------+
| Variable_name | Value |
+-------------------------+---------+
| Com_stmt_execute | 6512872 |
| Com_stmt_close | 0 |
| Com_stmt_fetch | 0 |
| Com_stmt_prepare | 18313 |
| Com_stmt_reset | 0 |
| Com_stmt_send_long_data | 0 |
| Com_stmt_reprepare | 0 |
+-------------------------+---------+
mysql> show variables like "%prepared%";
+------------------------------------------------------+-------+
| Variable_name | Value |
+------------------------------------------------------+-------+
| max_prepared_stmt_count | 16382 |
| performance_schema_max_prepared_statements_instances | -1 |
+------------------------------------------------------+-------+My friend suggested that I can modify mysql's 'max_prepared_stmt_count' to a higher value.I checked the sequelize code and found that the connection pool was just 'closed' connection, does not close PreparedStatement.Is there any API can solve this or only can be modifying 'max_prepared_stmt_count'.very thanks.
Environment
Dialect:
- mysql
Sequelize version: 5.3.5
Database version: 8.0.15
Node Version: v10.3.0
OS: Centos 7.5
TypeScript version: 3.4.3
Reacted by Tzelon Machluf, Marcello Bellomi, Florian Cucu, eggerand13, Mygod, Simon Fakir, Marcin Surma, Idan Davidi, Jon Snow, thaitq3 and 3 more
Metadata
Metadata
Assignees
Labels
P4: nice to haveFor issues that are not bugs.dependencyFor issues and PRs. Things that are related to one or more dependencies.existing workaroundFor issues. There is a known workaround for this issue.status: understoodFor issues. Applied when the issue is understood / reproducible.
Activity
Josephu commentedon Jul 15, 2019
I have also found this issue, and the root cause seem to be that sequelize does not parameterize variables when you use
Op.x. As an example, below is a query that had this issue:And by using Sequelize debug mode, I see:
So after I change to:
What I see from debug log is:
Interesting you'll notice that even after the change,
max_adultsstill has a static value inside, so it seems that arrays are also not parameterized properly.Environment
mysql 5.7
Sequelize 5.9.4
node v8.12.0
bsapot commentedon Aug 20, 2019
We ran into the original issue mentioned by @play2010dk as well and solved it. The number of prepared statements is controlled by the mysql2 library. The mysql2 lib uses a lru cache to cache the prepared statements. The default number of cached statements is 16,000 per connection.
See the documentation on prepared statements.
If you have a connection pool with more than 1 connection you may create:
number of connections * 16,000 = prepared statements.To solve this you can set maxPreparedStatements to a a much lower value in the sequelize connection config.
Here is an example, it is set in the dialect options.
Side note:
There may be a bug in the lru cache mysql is using or a bug in how lru cache is used by the mysql2 lib. We only have about 100 parameterized querys (update,inserts) so we should never have more than 100 per connection. But we are seeing 1000s in production and we can replicate it in our development environment.
Environment
mysql 5.7
Sequelize 5.15.0
node v10.16.0
papb commentedon Aug 21, 2019
@bsapot Thanks a lot for sharing this solution.
As I understand, you managed to workaround the issue successfully with the above, right?
Side note: although completely unrelated, thanks for accidentaly bringing issue #8019 to my attention from your code snippet above!
bsapot commentedon Aug 21, 2019
Yes, this seems to have fixed the issue the first person was having which is the problem we had. We deployed it to production earlier today and it is working well.
Horyougen commentedon Aug 21, 2019
Thank you very much.Before that, I had to set 'max_prepared_stmt_count' to a very big value and restart my MySQL service periodically.
papb commentedon Aug 21, 2019
Since the above is technically a workaround, I will leave this open for now
16 remaining items
ephys commentedon Mar 23, 2023
It was released in Sequelize 7 alpha 20
senthilbalajir commentedon Mar 23, 2023
Thanks @ephys, as the issue version 6, any chances of merging the fix to version 6 package?
ephys commentedon Mar 23, 2023
I wish it were possible but this change required a complete redesign of how we generate WHERE which took a year of work and many breaking changes which we cannot backport :(
senthilbalajir commentedon Mar 23, 2023
th1nkful commentedon Mar 31, 2023
@senthilbalajir hope this can help you(and others, like me) fix the issue and how you can avoid it on v6 (I just spent a few days migrated my project to v7, but needed to get a v6 deployment stable):
max_prepared_stmt_countis for the server, it is for ALL connectionsmaxPreparedStatementsmaxPreparedStatementslower tells mysql2 to reset the prepared statements it has cached when it hits that valueHopefully this helps you, this was crashing our project in production a fair bit when load got high, requiring a MySQL server restart
This issue over on the mysql2 project helped me wrap my head around all the variables to fully understand why it was happening:
sidorares/node-mysql2#702 (comment)
ephys commentedon Apr 1, 2023
Then I think we could fix the problem by setting
maxPreparedStatements's default value to to(max_prepared_stmt_count ?? 16382) / poolMaxSizeWe'd still be generating too many different queries in v6, but at least it would not crash
senthilbalajir commentedon Apr 3, 2023
@th1nkful, worked for me the setting up max prepared statements, but slow in a bit.
@ephys, thanks for re-opening the issue. The calculation will work for single instance but will it work in multi instance scenarios?
ephys commentedon Apr 3, 2023
You'd need to divide
max_prepared_stmt_countby how many instances you expect to have running at the same time, but otherwise yesfang-hd commentedon Nov 13, 2024
I'm using 5.7, has the latest version fixed this issue?
igor-gregori commentedon Dec 16, 2024
Hello everyone.
Has this issue been fixed in any version of Sequelize 6 @ephys ?
Or is it still recommended to set maxPreparedStatements?
I'm doing some tests, and apparently updating with
sequelize.querysolves my problem easily.dialectOptionsにmaxPreparedStatementsを追加
dialectOptionsにmaxPreparedStatementsを追加 (#23)