Skip to content

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 18 commits into from
Jun 16, 2023
Merged

Support for prepared statements #474

merged 18 commits into from
Jun 16, 2023

Conversation

levkk
Copy link
Contributor

@levkk levkk commented Jun 15, 2023

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 and Execute. 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).

@levkk levkk changed the title Prepared statements Prepared statements - Part I Jun 15, 2023
@levkk levkk changed the title Prepared statements - Part I Prepared statements, Part I: session mode Jun 15, 2023
@levkk levkk changed the title Prepared statements, Part I: session mode Support for prepared statements Jun 15, 2023
@levkk levkk marked this pull request as ready for review June 15, 2023 22:35
@levkk levkk requested review from magec and drdrsh June 15, 2023 22:44
@levkk levkk merged commit c7d6273 into main Jun 16, 2023
@levkk levkk deleted the levkk-prep-statements branch June 16, 2023 19:57
Copy link
Collaborator

@magec magec left a 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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants