Draft
Conversation
To use from a connection:
result_sets = pg.pipeline do |pipe|
pipe.query "SELECT 42"
pipe.query "SELECT * FROM posts LIMIT $1", limit
end
result_sets.scalar(Int32) # => 42
result_sets.read_all(Post) # => [Post(@id=1, ...), ...]
WARNING: This commit does not ensure that all result sets are fully
consumed.
jgaskins
commented
Jul 4, 2022
Comment on lines
+55
to
+61
| case frame = conn.expect_frame(PQ::Frame::ParseComplete | PQ::Frame::CommandComplete) | ||
| when PQ::Frame::ParseComplete | ||
| conn.expect_frame PQ::Frame::BindComplete | ||
| when PQ::Frame::CommandComplete | ||
| conn.expect_frame PQ::Frame::ReadyForQuery | ||
| conn.expect_frame PQ::Frame::ParseComplete | ||
| conn.expect_frame PQ::Frame::BindComplete |
Contributor
Author
There was a problem hiding this comment.
I don't know yet why I had to do this. Hoping someone with more understanding of the Postgres wire protocol can drop some hints.
Owner
|
So sorry @jgaskins I missed this being opened completely until just now! I think it'd be cool to get pipelining in for sure. I think your approach here of a different interface for it is probably the only sane way to do it. I could never come up with a safe way to auto-pipeline queries. No exec I think is fine. If we can't figure out the hang, we can maybe put a limit as to how many queries are in the pipe? Mechanism to ensure all result sets are consumed, I don’t have a strong opinion on this. |
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
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.
This is a rough sketch for query pipelining support. Pipelined queries in most DBs are implemented as:
Benchmark
Time spent on 100k
SELECT 42queries (lower is better):That's 55% less wall-clock time and 58% less CPU time with
localhostlatency. For databases where latency might be in the 1-2ms range, the difference will probably be orders of magnitude. In fact, it might've been a more dramatic difference but I had to split the pipelines into chunks — see "known issues" below.Usage
Via
crystal-lang/crystal-db:The API I had in mind with this is that
query_one,query_all, andquery_eachbecomequeryinside thepipelineblock andread_one,read_all, andread_eachwhen you're consuming results.Known issues/Remaining work to do
execsupport10_000.times { pipe.query "..." }PG::Connection?Example:
Closes #155