-
Notifications
You must be signed in to change notification settings - Fork 222
Support for prepared statements #474
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
magec
reviewed
Jun 19, 2023
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a very nice feature. I had already outlined the implementation and in my mind was pretty much like this implementation. I wouldn't write it as clean as it is now though .
Thanks!
pascalporedda
added a commit
to pascalporedda/pgcat
that referenced
this pull request
Oct 17, 2023
Reflect the changes from postgresml#474 in the Readme
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Feature
Add support for prepared statements in both session and transaction mode. Prepared statements allow the server to cache the execution plan for queries and saves time during query execution. They were previously not supported because servers are re-used between clients in both session and transaction mode.
Clients in session mode would expect the prepared statements to be gone after they disconnect and clients in transaction mode would expect the prepared statements to be available on the server until they deallocate them, but that wasn't the case because the same servers are shared between multiple connected clients.
Implementation
Resembles closely pgbouncer/pgbouncer#695. If the prepared statement is already prepared, nothing happens, if it's not, we prepare it on the server before sending over
Bind
andExecute
. One notable difference is we rename all prepared statements to our own names and keep a mapping between the client name and the server name. This is done so clients that don't randomize prepared statement names don't get conflicts after disconnecting and reconnecting to the same pooler.Benchmarks
Using prepared statements in pgbench produces a ~30% performance improvement.
Discussion
We don't hash the prepared statements, so if two clients prepare the same query twice, so do we. I think that's a good thing because the query plan can change between those two invocations and we don't want stale plans if clients don't.
Bug fixes
Fix reporting of query counts for servers (
SHOW SERVERS
).