Add prepared statement caching control via persistent option #2808
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR Info
Motivation:
When working with multiple connections to a single database and executing various queries concurrently, there was a need to directly control the number of prepared statements for better resource management. This feature allows fine-grained control over prepared statement caching to optimize memory usage and connection pool efficiency.
Note: This implementation was initially prototyped with the help of an AI assistant, so some parts of the code may not yet fully align with SeaORM’s internal design or best practices. I’m very open to feedback and happy to revise the approach as needed.
In our project we currently have a concrete need for this kind of fine-grained control over prepared statement persistence. Even if this exact pull request is not accepted, I would be grateful if a similar mechanism or configuration option could be considered for inclusion in SeaORM.
New Features
Added
persistentoption to control prepared statement caching behaviorpersistent: Option<bool>field toStatementstructpersistent()method to query builders (Select,Update,Insert,Delete,Paginator)Added default persistent option at database connection level
default_persistentfield to SQLx pool connections (MySQL, Postgres, SQLite)with_default_persistent()method to set the default caching behaviorChanges
Statementstruct to includepersistent: Option<bool>fieldUsage Example