feat: multi-shard LIMIT+OFFSET in-flight re-write in PREPARE+EXECUTE - #1504
Merged
Conversation
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
jkaczman
force-pushed
the
jk-prepare-execute-offset-limit
branch
from
September 7, 2026 00:01
cd95d6c to
0e03efa
Compare
levkk
reviewed
Sep 8, 2026
levkk
reviewed
Sep 10, 2026
| // Count the `ParamRef` nodes in the query, so that we know what number to start at | ||
| // if we need to add some more. | ||
| let mut param_refs_count: usize = 0; | ||
| pg_raw_parse::walk::walk(stmt_query.into(), |node| { |
Collaborator
There was a problem hiding this comment.
We do this already here:
and herepgdog/pgdog/src/frontend/router/parser/rewrite/statement/mod.rs
Lines 125 to 131 in af2edf5
Not a big deal, but at some point in the near/far future, we should consolidate all of our walks into one. It's cheap but it's not free.
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.
Implements functionality that handles multi-shard queries that use
LIMIT+OFFSETinPREPARE+EXECUTEto re-write theLIMITandOFFSETvalues (LIMIT = LIMIT + OFFSET,OFFSET = 0) and update theRouteto expect them later to handle the appropriate multi-shard response for the client.Specifically,
PreparedStatementwith aSELECTquery that has aLIMIT+OFFSET, it re-writes anyA_Constnodes (constant numbers) to beParamRefnodes. This allows us the ability to always be able to modify the limit/offset parameters later on, in a helper method stemming off ofOffsetPlan::apply_after_parser. This later-on modification (during execution) only happens if it's multi-shard.ExecuteStatement, if the Client previously usedA_Constnodes, which means we had to store them in cache, we append the relevant cached numbers to the parameters list for theExecuteStatement(as the Client does not know we internally re-wrote thePreparedStatementin our cache). If they previously usedParamRefnodes, the (cached, cloned)OffsetPlanis updated to the newly passed in values to complete it, so that we can use it in theOffsetPlan::apply_after_parserhelper method.Option<OffsetPlan>to prepared statement. Did not modifyCacheKey::Simple; I modified it to use the original query (before rewrite) to store in the cache, and store the rewritten query (if any) in the cache. This allows us to later (1) which parameter is offset / which is limit, as well as (2) if the client pre-specifiedA_Constnodes, we must save them in our cache, as they will not be given to us in theExecuteStatement.Added some integration tests to test various cases: both limit/offset being
ParamRefs, one being aParamRef, both beingA_Const, switching up the ordering (LIMIT $2 OFFSET $1), adding in an unrelatedParamRef(WHERE id < $2)... used ORDER BY for determinism in ensuring that the response we got is accurate.Integration test and unit test also checks to make sure that two Queries that are the same due to a re-write won't conflict in
GlobalCache.Fixes #1383.