Skip to content

perf(pg-pool): optimize client retrieval from pool by prioritizing prepared client #3398

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

Closed
wants to merge 17 commits into from

Conversation

cesco69
Copy link
Contributor

@cesco69 cesco69 commented Mar 4, 2025

see #3397

Instead of retrieving the first available client from the pool, check if there is a free client that already has cached prepared statements. This could improve performance by reducing the need to re-prepare statements on different clients.

@cesco69 cesco69 changed the title perf(pg-pool): optimize client retrieval from pool by prioritizing prepared client [POC] perf(pg-pool): optimize client retrieval from pool by prioritizing prepared client Mar 4, 2025
@cesco69 cesco69 changed the title [POC] perf(pg-pool): optimize client retrieval from pool by prioritizing prepared client perf(pg-pool): optimize client retrieval from pool by prioritizing prepared client Mar 4, 2025
Copy link
Collaborator

@charmander charmander left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs tests.

@cesco69

This comment was marked as resolved.

@cesco69 cesco69 requested a review from charmander March 5, 2025 09:27
@brianc
Copy link
Owner

brianc commented Mar 10, 2025

I'm not convinced this is a fair test. In fact, I should check that the query was executed with the same client, but how can I tell if it's the same client?

You can call pg_backend_pid() in a query - that should return the connection ID which will certainly be the same across queries on the same client.

@cesco69
Copy link
Contributor Author

cesco69 commented Mar 11, 2025

I'm not convinced this is a fair test. In fact, I should check that the query was executed with the same client, but how can I tell if it's the same client?

You can call pg_backend_pid() in a query - that should return the connection ID which will certainly be the same across queries on the same client.

Thanks! done with 20e7fa2

cesco69 added a commit to cesco69/DefinitelyTyped that referenced this pull request Mar 11, 2025
Merge this PR only if this PR brianc/node-postgres#3398 is merged and released
@brianc
Copy link
Owner

brianc commented Mar 15, 2025

While I appreciate the thought here....I'm thinking this might be a bit over-optimization and prioritizing a somewhat lesser used use-case over the general purpose. For example, applications that don't use named queries will incur an additional for-loop over the connection pool every time issuing a query via pool.query. It also complicates the pool's api surface slightly by including the name of the query more prominently....which could potentially impact future enhancments like "auto-prepare named statements" where we use md5s of query texts to manage the preparation or something fancier. Finally, and IMO most importantly: in any application under sufficient load, in which this feature would expect to potentially cause performance improvements, your pool will likely be saturated and since every client will be "hot," eventually every client will end up w/ the named queries stored & cached anyway....making this code not really needed. I think the largest measurable performance impact above and beyond what using named prepared statements already makes in applications underload is likely in micro-benchmarks....which isn't worth complicating the API surface for.

@cesco69
Copy link
Contributor Author

cesco69 commented Mar 17, 2025

Hi, thanks for the review!

For example, applications that don't use named queries will incur an additional for-loop over the connection pool every time issuing a query via pool.query

No, query without "name" don't are affected by my PR

It also complicates the pool's api surface slightly by including the name of the query more prominently

Absolutly true! I don't found a way for retrive the query "name" from the pool class. If there are a way for retrive the query "name" into _pulseQueue method of Pool class, I can rewrite my PR in more simple way!

@cesco69 cesco69 closed this Mar 17, 2025
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.

3 participants