Skip to content

fix: match WHERE subquery by location - #2034

Merged
jkaczman merged 1 commit into
mainfrom
jk-where-location
Nov 14, 2026
Merged

jkaczman merged 1 commit into
mainfrom
jk-where-location

Conversation

@jkaczman

Copy link
Copy Markdown
Contributor

Previously, when we rewrote cross-shard subqueries in WHERE with the rows that those specific subqueries returned, we weren't checking which subquery we replaced, which caused all of the subqueries in WHERE to be replaced with these rows (even for other subqueries we weren't supposed to rewrite, causing incorrect response to be returned)

E.g., if we have a statement such as

SELECT * FROM users WHERE id IN (SELECT user_id FROM orders) AND state IN (SELECT state FROM states)

The subquery is executed and rewritten for the orders subquery, however, it shouldn't be for the states one, as orders is sharded and states isn't. However, previously, both subqueries would be replaced with the user ids from the orders query.

Now, the subquery is matched by location in the statement, so only applicable subqueries are rewritten in this scenario.

@codecov

codecov Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions

Copy link
Copy Markdown
Contributor

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

suite throughput cpu/txn user cycles/txn user instr/txn kernel cycles/txn kernel instr/txn ctx/txn
pooler 19.15 (+0.6% ± 1.5%) 18,404 (+0.4% ± 0.5%) 31,598 (+0.0% ± 0.04%) 30,648 (+0.9% ± 2.6%) 50,956 (-0.1% ± 0.4%)
lb 19.86 (+0.6% ± 1.1%) 18,990 (+0.3% ± 0.7%) 32,495 (-0.0% ± 0.05%) 31,727 (+0.8% ± 1.7%) 52,103 (-0.1% ± 0.4%)
sharding-w2 31.8 (-0.4% ± 2.6%) 42,951 (-0.5% ± 2.8%) 94,867 (-0.0% ± 0.01%) 39,762 (-0.1% ± 2.5%) 53,707 (-0.0% ± 0.09%) 1.547 (-0.1% ± 0.4%)
sharding-w8 245,618 (+0.3% ± 2.0%) 29.06 (-0.3% ± 2.1%) 44,000 (+0.3% ± 0.2%) 50,448 (-0.0% ± 0.02%) 36,249 (-0.9% ± 4.6%) 42,375 (+0.0% ± 0.2%) 0.1701 (-0.1% ± 3.8%)
Counters per txn
suite frontend stall l1i refill backend stall
pooler 8,078 (+1.2% ± 1.4%) 2,734 (+0.3% ± 0.5%)
lb 8,421 (+0.9% ± 1.9%) 2,785 (+0.3% ± 0.8%)
sharding-w2 17,265 (-1.1% ± 4.8%) 4,066 (-0.7% ± 2.7%)
sharding-w8 19,872 (+1.5% ± 2.5%) 4,094 (+1.2% ± 2.3%)

Microbench: all 504 measures within bounds.

Hot code per txn
path base head change
pooler 113.6 KB 114.0 KB +0.4 KB
sharding-w2 167.8 KB 170.8 KB +3.0 KB

pooler:

crate base head change
pgdog::frontend::client::query_engine 19.8 KB 20.0 KB +0.2 KB
tokio 18.6 KB 18.4 KB -0.2 KB
pgdog::backend::pool 17.6 KB 17.5 KB -0.1 KB
aws-lc (TLS crypto) 9.9 KB 10.1 KB +0.2 KB
rustls (TLS) 8.5 KB 8.4 KB -0.1 KB
pgdog::frontend (other) 7.8 KB 7.9 KB +0.2 KB
pgdog::backend::server 7.8 KB 7.8 KB +0.1 KB
other 6.1 KB 6.1 KB +0.0 KB
everything else 17.8 KB 17.8 KB +0.0 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.2 KB +0.0 KB
pgdog::frontend::client::query_engine::QueryEngine::handle::{{closure}} 4.3 KB +0.1 KB
pgdog::frontend::client::query_engine::query::<impl pgdog::frontend::client::query_engine: 3.2 KB +0.0 KB
tokio::runtime::task::raw::poll 3.1 KB -0.2 KB

sharding-w2:

crate base head change
pgdog::frontend::router 28.8 KB 29.5 KB +0.7 KB
pgdog::frontend::client::query_engine 21.0 KB 21.3 KB +0.3 KB
tokio 19.2 KB 19.4 KB +0.2 KB
pgdog::backend::pool 18.0 KB 18.8 KB +0.8 KB
aws-lc (TLS crypto) 12.6 KB 12.8 KB +0.1 KB
other 10.4 KB 10.1 KB -0.2 KB
pgdog::frontend (other) 8.9 KB 9.1 KB +0.1 KB
rustls (TLS) 8.1 KB 8.3 KB +0.2 KB
everything else 40.8 KB 41.5 KB +0.8 KB
function head change
pgdog::frontend::client::Client::run::{{closure}} 6.5 KB +0.0 KB
pgdog::frontend::client::query_engine::query::<impl pgdog::frontend::client::query_engine: 5.1 KB +0.1 KB
pgdog::frontend::client::query_engine::QueryEngine::handle::{{closure}} 4.2 KB +0.1 KB
tokio::runtime::task::raw::poll 3.6 KB +0.2 KB
pgdog::frontend::router::parser::query::select::<impl pgdog::frontend::router::parser::que 3.2 KB -0.1 KB
lb flamegraph vs 2131844

lb

Open interactive for hover, zoom and search.

lb removed

Open interactive.

microbench flamegraph vs 2131844

microbench

Open interactive for hover, zoom and search.

microbench removed

Open interactive.

pooler flamegraph vs 2131844

pooler

Open interactive for hover, zoom and search.

pooler removed

Open interactive.

sharding-w2 flamegraph vs 2131844

sharding-w2

Open interactive for hover, zoom and search.

sharding-w2 removed

Open interactive.

sharding-w8 flamegraph vs 2131844

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 64128b5 with 2131844 on c8g.metal-24xl (spot).

@github-actions

Copy link
Copy Markdown
Contributor

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

suite throughput cpu/txn user cycles/txn user instr/txn kernel cycles/txn kernel instr/txn ctx/txn
pooler 25.95 (-0.1% ± 0.3%) 19,016 (-0.1% ± 0.2%) 32,933 (-0.0% ± 0.07%) 33,408 (-0.2% ± 0.3%) 36,422 (-0.3% ± 0.5%)
lb 26.41 (-0.0% ± 0.3%) 19,577 (+0.0% ± 0.2%) 33,912 (+0.0% ± 0.06%) 33,613 (+0.0% ± 0.6%) 37,328 (+0.4% ± 0.5%)
sharding-w2 41.77 (+0.0% ± 1.7%) 46,755 (-0.3% ± 0.7%) 98,577 (+0.0% ± 0.08%) 38,198 (+0.2% ± 3.6%) 40,236 (+0.0% ± 0.1%) 1.53 (+0.1% ± 0.6%)
sharding-w8 207,205 (-0.0% ± 0.3%) 32.75 (+0.1% ± 0.10%) 45,721 (+0.2% ± 0.2%) 52,876 (-0.0% ± 0.02%) 31,121 (-0.0% ± 0.3%) 32,530 (+0.0% ± 0.09%) 0.1736 (-0.5% ± 0.7%)
Counters per txn
suite frontend stall l1i refill backend stall
pooler 3,932 (-0.2% ± 0.6%) 1,214 (+0.1% ± 0.7%) 36,788 (-0.1% ± 0.4%)
lb 4,104 (+0.1% ± 0.7%) 1,250 (+0.1% ± 0.8%) 37,595 (+0.1% ± 0.7%)
sharding-w2 6,325 (-0.5% ± 0.3%) 1,534 (+0.1% ± 0.5%) 113,866 (-0.3% ± 1.7%)
sharding-w8 3,234 (-0.0% ± 1.4%) 863 (+0.3% ± 1.8%) 177,902 (+0.3% ± 0.3%)

Microbench: all 504 measures within bounds.

Hot code per txn
path base head change
pooler 114.8 KB 114.4 KB -0.3 KB
sharding-w2 167.2 KB 168.0 KB +0.8 KB

pooler:

crate base head change
pgdog::frontend::client::query_engine 22.2 KB 22.2 KB +0.0 KB
tokio 18.2 KB 18.4 KB +0.2 KB
pgdog::backend::pool 17.9 KB 18.2 KB +0.2 KB
pgdog::frontend (other) 9.2 KB 9.2 KB +0.0 KB
aws-lc (TLS crypto) 9.0 KB 8.2 KB -0.8 KB
pgdog::backend::server 8.0 KB 7.9 KB -0.1 KB
rustls (TLS) 7.9 KB 7.9 KB +0.1 KB
other 5.7 KB 5.9 KB +0.2 KB
everything else 16.6 KB 16.5 KB -0.1 KB
function head change
pgdog::frontend::client::Client::run::{{closure}} 7.1 KB +0.0 KB
pgdog::frontend::client::query_engine::query::<impl pgdog::frontend::client::query_engine: 6.4 KB +0.0 KB
pgdog::frontend::client::query_engine::QueryEngine::handle::{{closure}} 4.9 KB -0.1 KB
pgdog::frontend::client::query_engine::query::<impl pgdog::frontend::client::query_engine: 3.8 KB +0.1 KB
tokio::runtime::task::raw::poll 3.6 KB -0.1 KB

sharding-w2:

crate base head change
pgdog::frontend::router 28.0 KB 28.1 KB +0.1 KB
pgdog::frontend::client::query_engine 23.0 KB 23.3 KB +0.3 KB
pgdog::backend::pool 19.1 KB 18.8 KB -0.4 KB
tokio 18.3 KB 18.8 KB +0.4 KB
pgdog::frontend (other) 10.8 KB 10.6 KB -0.2 KB
aws-lc (TLS crypto) 10.0 KB 10.3 KB +0.3 KB
other 10.2 KB 10.1 KB -0.1 KB
pgdog::backend::server 8.6 KB 8.5 KB -0.1 KB
everything else 39.2 KB 39.6 KB +0.4 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.5 KB +0.2 KB
pgdog::frontend::client::query_engine::QueryEngine::handle::{{closure}} 4.5 KB -0.1 KB
pgdog::frontend::router::parser::query::select::<impl pgdog::frontend::router::parser::que 4.2 KB +0.1 KB
tokio::runtime::task::raw::poll 3.9 KB +0.0 KB
lb flamegraph vs 2131844

lb

Open interactive for hover, zoom and search.

lb removed

Open interactive.

microbench flamegraph vs 2131844

microbench

Open interactive for hover, zoom and search.

microbench removed

Open interactive.

pooler flamegraph vs 2131844

pooler

Open interactive for hover, zoom and search.

pooler removed

Open interactive.

sharding-w2 flamegraph vs 2131844

sharding-w2

Open interactive for hover, zoom and search.

sharding-w2 removed

Open interactive.

sharding-w8 flamegraph vs 2131844

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 64128b5 with 2131844 on m7i.metal-48xl (spot).

@jkaczman
jkaczman merged commit 96c83f3 into main Nov 14, 2026
52 checks passed
@jkaczman
jkaczman deleted the jk-where-location branch November 14, 2026 03:51
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