Skip to content

refactor(cross-shard): sharding key lookup for aggregate rewrite - #2072

Open
murex971 wants to merge 2 commits into
mainfrom
murex-agg-sharding-key-lookup
Open

murex971 wants to merge 2 commits into
mainfrom
murex-agg-sharding-key-lookup

Conversation

@murex971

Copy link
Copy Markdown
Contributor

Summary

Groundwork for cross-shard DISTINCT aggregates. No behaviour change.

When DISTINCT is on the sharding key, or GROUP BY includes it, shards
never share a value or a group, so per-shard results can be added up.
The aggregate rewrite needs to know when that is the case.

  • Move the router's sharding key lookup to ShardedTables::sharding_key
    so the rewrite can reuse it. The router calls the same method.
  • Add ShardingKey, which answers whether an expression or the
    GROUP BY is the sharding key of the table a SELECT reads. It only
    answers for a single plain table and a bare column reference; joins,
    subqueries, CTEs and expressions over the key are treated as "no".

@murex971
murex971 added this pull request to stack #2073 November 19, 2026 13:21
@codecov

codecov Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.18182% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...parser/rewrite/statement/aggregate/sharding_key.rs 97.94% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Metal bench (arm64) passed. All 520 measures within bounds.

suite throughput cpu/txn user cycles/txn user instr/txn kernel cycles/txn kernel instr/txn ctx/txn
pooler 19.03 (+0.3% ± 1.2%) 18,068 (-0.0% ± 0.8%) 31,448 (-0.0% ± 0.04%) 30,642 (+0.6% ± 2.1%) 51,112 (+0.0% ± 0.4%)
lb 19.59 (+0.5% ± 1.6%) 18,653 (+0.2% ± 1.0%) 32,348 (-0.0% ± 0.05%) 31,300 (+1.1% ± 2.3%) 52,228 (+0.2% ± 0.5%)
sharding-w2 32.28 (-0.2% ± 1.7%) 43,386 (-0.2% ± 0.9%) 94,901 (+0.0% ± 0.02%) 40,460 (-0.2% ± 3.0%) 53,742 (+0.1% ± 0.1%) 1.547 (+0.1% ± 0.1%)
sharding-w8 243,532 (+0.5% ± 3.5%) 29.43 (-0.3% ± 3.3%) 44,253 (+0.1% ± 3.0%) 50,494 (-0.0% ± 0.09%) 37,021 (-0.8% ± 3.7%) 42,373 (-0.0% ± 0.09%) 0.166 (-1.0% ± 0.8%)
Counters per txn
suite frontend stall l1i refill backend stall
pooler 7,789 (+0.0% ± 2.1%) 2,677 (-0.1% ± 0.9%)
lb 8,142 (+0.7% ± 2.4%) 2,728 (+0.2% ± 1.1%)
sharding-w2 17,981 (-0.3% ± 1.8%) 4,167 (-0.2% ± 0.9%)
sharding-w8 20,411 (+1.0% ± 5.0%) 4,184 (+0.4% ± 2.5%)

Microbench: all 507 measures within bounds.

Hot code per txn
path base head change
pooler 115.5 KB 115.1 KB -0.4 KB
sharding-w2 168.2 KB 168.9 KB +0.7 KB

pooler:

crate base head change
pgdog::frontend::client::query_engine 20.4 KB 20.2 KB -0.1 KB
tokio 18.6 KB 18.6 KB +0.0 KB
pgdog::backend::pool 17.8 KB 17.7 KB -0.1 KB
aws-lc (TLS crypto) 10.8 KB 10.7 KB -0.1 KB
rustls (TLS) 8.4 KB 8.4 KB +0.1 KB
pgdog::frontend (other) 8.1 KB 7.9 KB -0.1 KB
pgdog::backend::server 7.9 KB 7.7 KB -0.2 KB
other 6.4 KB 6.4 KB +0.0 KB
everything else 17.2 KB 17.4 KB +0.1 KB
function head change
pgdog::frontend::client::Client::run::{{closure}} 5.6 KB -0.1 KB
pgdog::frontend::client::query_engine::query::<impl pgdog::frontend::client::query_engine: 5.4 KB +0.1 KB
pgdog::frontend::client::query_engine::QueryEngine::handle::{{closure}} 4.4 KB +0.1 KB
pgdog::frontend::client::query_engine::query::<impl pgdog::frontend::client::query_engine: 3.4 KB -0.1 KB
tokio::runtime::task::raw::poll 3.2 KB -0.1 KB

sharding-w2:

crate base head change
pgdog::frontend::router 29.1 KB 29.1 KB -0.1 KB
pgdog::frontend::client::query_engine 21.8 KB 21.6 KB -0.2 KB
tokio 18.4 KB 18.4 KB +0.0 KB
pgdog::backend::pool 18.1 KB 18.4 KB +0.2 KB
aws-lc (TLS crypto) 13.0 KB 13.2 KB +0.2 KB
other 10.1 KB 10.4 KB +0.2 KB
pgdog::frontend (other) 8.8 KB 8.9 KB +0.1 KB
pgdog::backend::server 8.0 KB 8.2 KB +0.2 KB
everything else 40.8 KB 40.8 KB -0.1 KB
function head change
pgdog::frontend::client::Client::run::{{closure}} 6.3 KB +0.1 KB
pgdog::frontend::client::query_engine::query::<impl pgdog::frontend::client::query_engine: 5.3 KB +0.0 KB
pgdog::frontend::client::query_engine::QueryEngine::handle::{{closure}} 4.1 KB -0.1 KB
tokio::runtime::task::raw::poll 3.4 KB +0.2 KB
pgdog::frontend::client::query_engine::query::<impl pgdog::frontend::client::query_engine: 3.3 KB +0.0 KB
lb flamegraph vs 65f51c7

lb

Open interactive for hover, zoom and search.

lb removed

Open interactive.

microbench flamegraph vs 65f51c7

microbench

Open interactive for hover, zoom and search.

microbench removed

Open interactive.

pooler flamegraph vs 65f51c7

pooler

Open interactive for hover, zoom and search.

pooler removed

Open interactive.

sharding-w2 flamegraph vs 65f51c7

sharding-w2

Open interactive for hover, zoom and search.

sharding-w2 removed

Open interactive.

sharding-w8 flamegraph vs 65f51c7

sharding-w8

Open interactive for hover, zoom and search.

sharding-w8 removed

Open interactive.

Logs

lb, microbench, pooler, sharding-w2, sharding-w8, verdict.json

Compared 7bbe99a with 65f51c7 on c8g.metal-24xl (on-demand).

@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Metal bench (x86) passed. All 520 measures within bounds.

suite throughput cpu/txn user cycles/txn user instr/txn kernel cycles/txn kernel instr/txn ctx/txn
pooler 25.77 (-0.0% ± 0.3%) 18,544 (+0.1% ± 0.2%) 32,799 (-0.0% ± 0.04%) 33,675 (-0.0% ± 0.3%) 36,514 (-0.2% ± 0.3%)
lb 26.18 (+0.1% ± 0.4%) 19,093 (-0.1% ± 0.09%) 33,779 (-0.0% ± 0.05%) 33,808 (+0.1% ± 0.2%) 37,316 (-0.1% ± 0.3%)
sharding-w2 42.4 (+0.2% ± 7.5%) 47,936 (+0.4% ± 15.0%) 98,725 (+0.0% ± 0.1%) 38,787 (-0.2% ± 1.9%) 40,061 (-0.0% ± 0.6%) 1.517 (-0.0% ± 3.2%)
sharding-w8 206,253 (+0.1% ± 1.2%) 32.97 (-0.2% ± 1.5%) 45,983 (-0.4% ± 2.3%) 53,048 (+0.0% ± 0.1%) 31,396 (+0.0% ± 1.0%) 32,506 (+0.0% ± 0.2%) 0.173 (+0.4% ± 2.2%)
Counters per txn
suite frontend stall l1i refill backend stall
pooler 3,117 (-0.1% ± 0.6%) 1,073 (-0.4% ± 0.8%) 39,920 (+0.1% ± 0.5%)
lb 3,280 (-0.5% ± 0.5%) 1,110 (-0.4% ± 0.6%) 40,667 (+0.2% ± 0.4%)
sharding-w2 8,234 (+23.4% ± 142.4%) 1,636 (+1.6% ± 17.6%) 107,641 (+0.8% ± 29.5%)
sharding-w8 3,355 (+0.4% ± 3.1%) 890 (+0.5% ± 1.0%) 178,158 (-0.7% ± 3.2%)

Microbench: all 507 measures within bounds.

Microbench latency: 3 benches changed more than 3%
benchmark latency baseline change instructions change
simple_cold::order_limit_offset 8,998 ns 8,668 ns +3.80% ± 0.72% +0.04% ± 0.00%
extended_evicting::having 18,084 ns 18,702 ns -3.30% ± 0.72% -0.00% ± 0.00%
extended_cold::having 18,012 ns 18,572 ns -3.01% ± 0.94% -0.00% ± 0.00%
Hot code per txn
path base head change
pooler 117.6 KB 115.6 KB -2.1 KB
sharding-w2 174.4 KB 168.3 KB -6.1 KB

pooler:

crate base head change
pgdog::frontend::client::query_engine 23.3 KB 22.6 KB -0.8 KB
pgdog::backend::pool 18.6 KB 18.4 KB -0.2 KB
tokio 18.5 KB 18.4 KB -0.1 KB
pgdog::frontend (other) 9.8 KB 9.8 KB +0.0 KB
aws-lc (TLS crypto) 8.6 KB 8.4 KB -0.2 KB
pgdog::backend::server 8.1 KB 7.9 KB -0.1 KB
rustls (TLS) 7.9 KB 7.9 KB -0.1 KB
other 6.2 KB 5.9 KB -0.2 KB
everything else 16.6 KB 16.1 KB -0.4 KB
function head change
pgdog::frontend::client::Client::run::{{closure}} 7.4 KB -0.1 KB
pgdog::frontend::client::query_engine::query::<impl pgdog::frontend::client::query_engine: 6.2 KB -0.2 KB
pgdog::frontend::client::query_engine::QueryEngine::handle::{{closure}} 5.0 KB -0.2 KB
pgdog::frontend::client::query_engine::query::<impl pgdog::frontend::client::query_engine: 3.9 KB -0.1 KB
tokio::runtime::task::raw::poll 3.6 KB +0.0 KB

sharding-w2:

crate base head change
pgdog::frontend::router 28.7 KB 28.4 KB -0.3 KB
pgdog::frontend::client::query_engine 24.9 KB 23.3 KB -1.6 KB
pgdog::backend::pool 19.6 KB 18.9 KB -0.8 KB
tokio 18.8 KB 18.7 KB -0.1 KB
pgdog::frontend (other) 11.1 KB 10.7 KB -0.4 KB
other 11.0 KB 10.2 KB -0.8 KB
aws-lc (TLS crypto) 10.2 KB 10.1 KB -0.1 KB
pgdog::backend::server 9.1 KB 8.8 KB -0.3 KB
everything else 41.2 KB 39.2 KB -1.9 KB
function head change
pgdog::frontend::client::Client::run::{{closure}} 8.2 KB -0.1 KB
pgdog::frontend::client::query_engine::query::<impl pgdog::frontend::client::query_engine: 6.2 KB -0.1 KB
pgdog::frontend::client::query_engine::QueryEngine::handle::{{closure}} 4.5 KB -0.4 KB
pgdog::frontend::router::parser::query::select::<impl pgdog::frontend::router::parser::que 4.2 KB -0.1 KB
tokio::runtime::task::raw::poll 4.0 KB +0.0 KB
lb flamegraph vs 65f51c7

lb

Open interactive for hover, zoom and search.

lb removed

Open interactive.

microbench flamegraph vs 65f51c7

microbench

Open interactive for hover, zoom and search.

microbench removed

Open interactive.

pooler flamegraph vs 65f51c7

pooler

Open interactive for hover, zoom and search.

pooler removed

Open interactive.

sharding-w2 flamegraph vs 65f51c7

sharding-w2

Open interactive for hover, zoom and search.

sharding-w2 removed

Open interactive.

sharding-w8 flamegraph vs 65f51c7

sharding-w8

Open interactive for hover, zoom and search.

sharding-w8 removed

Open interactive.

Logs

lb, microbench, pooler, sharding-w2, sharding-w8, verdict.json

Compared 7bbe99a with 65f51c7 on c7i.metal-48xl (on-demand).

Base automatically changed from fix-unsupported-distinct to main November 21, 2026 08:22
@murex971
murex971 force-pushed the murex-agg-sharding-key-lookup branch from 29d326e to bcf5140 Compare November 21, 2026 08:22

This branch has not been deployed

No deployments
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