Skip to content

refactor: apply route-dependent rewrites after routing - #1545

Open
murex971 wants to merge 14 commits into
pgdogdev:mainfrom
murex971:refactor-parse-route
Open

murex971 wants to merge 14 commits into
pgdogdev:mainfrom
murex971:refactor-parse-route

Conversation

@murex971

@murex971 murex971 commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

Aggregate helpers, ORDER BY helpers, and LIMIT/OFFSET pushdown now run only after the route is known, so direct shard execution keeps original SQL and does not leak helper columns.

closes #1425 closes #1135

@codecov

codecov Bot commented Sep 14, 2026

Copy link
Copy Markdown

@murex971
murex971 marked this pull request as ready for review September 14, 2026 20:01
@levkk

levkk commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator

👀

describe.anonymize();
}
if let Some(describe) = describe {
return Ok(HandleResult::PrependProtocolMessageRewrite {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Q: why do we need this? I'm not too familiar with this piece of code, would be good to have a small example explaining.

@levkk levkk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This looks good to me I think. It would be good to get feedback from @jkaczman after we merge #1541 (both touch the rewriter heavily). Should be easy to resolve conflicts, but just want to double check one doesn't break the other and vice versa.

}))
}

fn internal_describe(&mut self, name: &str) -> Option<ProtocolMessage> {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Oh I see. The cross-shard Describe has different columns that direct-to-shard, so we need to rewrite it. I'm remembering now.

Comment thread pgdog/src/frontend/prepared_statements/global_cache.rs Outdated
Comment thread pgdog/src/frontend/prepared_statements/global_cache.rs Outdated
@murex971
murex971 requested a review from jkaczman September 16, 2026 13:33

@jkaczman jkaczman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

It would be great if you could document certain decisions e.g. why you chose to move from an explicit A_Const node with the limit + offset to an A_Expr executing on Postgres. I think I understand the tradeoffs, but it would be great to see the author's POV when reading the code.

I would also be curious to know if the benchmark from before vs after changed at all.

@murex971

Copy link
Copy Markdown
Contributor Author

Benchmarked release builds of main and this PR using pgbench prepared protocol against the same three shards. Each case used a 3s warmup followed by three 15s runs at 10 and 50 clients.

Results were effectively neutral: direct and cross-shard AVG ranged from −0.6% to +1.0%; comparable ORDER BY was ~1% slower (expected from post-route bookkeeping). CPU was unchanged (+0.13%), with ~476 KiB higher peak RSS.

@murex971
murex971 requested review from jkaczman and levkk September 17, 2026 18:36
Comment thread pgdog/src/frontend/router/parser/query/select.rs Outdated
Comment thread pgdog/src/frontend/router/parser/rewrite/statement/aggregate/mod.rs
Comment thread pgdog/src/frontend/router/parser/rewrite/statement/order_by.rs
Comment thread pgdog/src/frontend/router/parser/rewrite/statement/projection.rs Outdated
Comment thread pgdog/src/frontend/router/parser/rewrite/statement/projection.rs Outdated
Comment thread pgdog/src/frontend/router/parser/rewrite/statement/order_by.rs
Comment thread pgdog/src/frontend/router/parser/route.rs Outdated
Comment thread pgdog/src/frontend/prepared_statements/global_cache.rs
continue;
}

let projected_column = select.target_list().len() + helpers.len();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we shouldn't rely on this column index; it gives us the parse tree entries, but not the actual output columns (considering when * is used). I wonder if it would be better to use what you do in aggregate.rs again (decoder.row_description().field_index(&helper.alias) else {). Right now, I think there are edge cases where this can accidentally delete user columns / sort on the wrong column.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes, I thought this for the above case handling

Comment thread pgdog/src/frontend/router/parser/rewrite/statement/order_by.rs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants