Skip to content

perf: reference prior CTE data via unnest when evaluating later CTE - #2024

Open
jkaczman wants to merge 1 commit into
mainfrom
jk-subqueries-cte-unnest
Open

jkaczman wants to merge 1 commit into
mainfrom
jk-subqueries-cte-unnest

Conversation

@jkaczman

Copy link
Copy Markdown
Contributor

When we send CTE subselects to be evaluated separately from the top-level query, sometimes the subselects reference earlier executed CTEs (e.g., CTE b references CTE a). When we send CTE b, we need to include the values that CTE a previously returned; this is done via re-writing the CTE b subselect to include a WITH clause with CTE a's returned values, so that when we execute it separately from the top-level query, it has everything it needs to return the values it should. Right now, this is done with a VALUES clause with all the DataRows inlined as tuples. Example,

WITH a (id, status) AS (VALUES (1::bigint, 'VIP'::text), (2, 'regular')) SELECT x.id, y.id FROM a x JOIN a y ON x.status = y.status

This migrates those from VALUES to unnest (via Bind param array) for massive speedup (follow up to #2003, #2023).

@codecov

codecov Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 70.00000% with 9 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...end/client/query_engine/multi_step/subquery/mod.rs 70.00% 9 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

Copy link
Copy Markdown
Contributor

Metal bench (arm64) failed. 1 of 517 measures outside bounds.

suite throughput cpu/txn user cycles/txn user instr/txn kernel cycles/txn kernel instr/txn ctx/txn
pooler 19.29 (+0.2% ± 1.4%) 19,135 (+0.7% ± 0.7%) 31,993 (+0.0% ± 0.07%) 30,342 (+0.1% ± 2.5%) 51,168 (+0.2% ± 0.2%)
lb 19.81 (+0.1% ± 1.3%) 19,582 (+0.5% ± 1.3%) 32,882 (-0.1% ± 0.08%) 30,928 (-0.5% ± 2.2%) 52,132 (-0.5% ± 0.3%)
sharding-w2 31.45 (+1.2% ± 1.2%) 42,808 (+0.2% ± 1.0%) 97,424 (-0.0% ± 0.01%) 38,732 (+2.5% ± 1.6%) 53,622 (-0.0% ± 0.1%) 1.548 (-0.2% ± 0.03%)
sharding-w8 253,539 (+0.3% ± 1.3%) 28.15 (-0.1% ± 1.7%) 43,579 (-0.1% ± 1.7%) 52,930 (-0.0% ± 0.09%) 34,102 (-0.2% ± 3.0%) 42,349 (-0.0% ± 0.1%) 0.1742 (-0.5% ± 2.7%)
Counters per txn
suite frontend stall l1i refill backend stall
pooler 8,568 (+1.0% ± 1.8%) 2,766 (+0.6% ± 0.6%)
lb 8,721 (+0.8% ± 3.3%) 2,798 (+0.3% ± 1.6%)
sharding-w2 16,367 (-0.6% ± 2.1%) 3,925 (-0.1% ± 0.9%)
sharding-w8 18,126 (-0.4% ± 2.9%) 3,775 (-0.4% ± 1.8%)
benchmark measure result baseline change bound
full_simple::order_limit_offset instructions 197,811 195,147 +1.37% ± 0.00% +1%

Microbench: 503 of 504 measures within bounds.

Hot code per txn
path base head change
pooler 110.8 KB 109.8 KB -1.0 KB
sharding-w2 166.7 KB 167.3 KB +0.6 KB

pooler:

crate base head change
pgdog::frontend::client::query_engine 19.8 KB 19.5 KB -0.2 KB
tokio 18.7 KB 18.2 KB -0.4 KB
pgdog::backend::pool 17.6 KB 17.4 KB -0.2 KB
aws-lc (TLS crypto) 10.2 KB 10.3 KB +0.1 KB
rustls (TLS) 8.4 KB 8.3 KB -0.1 KB
pgdog::frontend (other) 7.6 KB 7.6 KB +0.0 KB
pgdog::backend::server 7.1 KB 7.2 KB +0.1 KB
other 6.4 KB 6.2 KB -0.2 KB
everything else 15.1 KB 15.1 KB +0.0 KB
function head change
pgdog::frontend::client::query_engine::query::<impl pgdog::frontend::client::query_engine: 5.4 KB +0.1 KB
pgdog::frontend::client::Client::run::{{closure}} 5.3 KB -0.1 KB
pgdog::frontend::client::query_engine::QueryEngine::handle::{{closure}} 3.9 KB -0.1 KB
tokio::runtime::task::raw::poll 3.1 KB -0.1 KB
pgdog::backend::server::Server::read::{{closure}} 2.9 KB +0.1 KB

sharding-w2:

crate base head change
pgdog::frontend::router 29.0 KB 28.9 KB -0.1 KB
pgdog::frontend::client::query_engine 20.8 KB 20.9 KB +0.1 KB
tokio 19.1 KB 19.0 KB -0.1 KB
pgdog::backend::pool 18.4 KB 18.5 KB +0.1 KB
aws-lc (TLS crypto) 13.1 KB 12.9 KB -0.1 KB
other 10.0 KB 9.9 KB -0.1 KB
pgdog::frontend (other) 8.7 KB 8.6 KB -0.1 KB
rustls (TLS) 8.1 KB 8.2 KB +0.2 KB
everything else 39.6 KB 40.3 KB +0.7 KB
function head change
pgdog::frontend::client::Client::run::{{closure}} 6.2 KB +0.1 KB
pgdog::frontend::client::query_engine::query::<impl pgdog::frontend::client::query_engine: 5.2 KB +0.2 KB
pgdog::frontend::client::query_engine::QueryEngine::handle::{{closure}} 4.1 KB +0.1 KB
tokio::runtime::task::raw::poll 3.4 KB +0.1 KB
pgdog::frontend::router::parser::query::select::<impl pgdog::frontend::router::parser::que 3.4 KB +0.0 KB
lb flamegraph vs e084496

lb

Open interactive for hover, zoom and search.

lb removed

Open interactive.

microbench flamegraph vs e084496

microbench

Open interactive for hover, zoom and search.

microbench removed

Open interactive.

pooler flamegraph vs e084496

pooler

Open interactive for hover, zoom and search.

pooler removed

Open interactive.

sharding-w2 flamegraph vs e084496

sharding-w2

Open interactive for hover, zoom and search.

sharding-w2 removed

Open interactive.

sharding-w8 flamegraph vs e084496

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 56c21c2 with e084496 on m8g.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 26.09 (-0.1% ± 0.3%) 18,938 (+0.0% ± 0.3%) 33,966 (+0.2% ± 0.3%) 33,767 (+0.1% ± 0.3%) 36,576 (+0.2% ± 0.4%)
lb 26.49 (-0.0% ± 0.3%) 19,482 (+0.1% ± 0.2%) 34,906 (+0.1% ± 0.2%) 33,762 (-0.3% ± 0.4%) 37,148 (-0.3% ± 0.7%)
sharding-w2 41.59 (-0.4% ± 0.3%) 46,635 (-0.7% ± 0.5%) 101,415 (-0.0% ± 0.01%) 38,024 (-0.2% ± 1.9%) 40,150 (+0.0% ± 0.08%) 1.534 (+0.2% ± 0.3%)
sharding-w8 208,650 (+0.4% ± 0.3%) 32.56 (-0.5% ± 0.4%) 45,227 (-0.7% ± 0.9%) 55,726 (-0.0% ± 0.01%) 31,162 (-0.3% ± 0.3%) 32,474 (-0.0% ± 0.08%) 0.1734 (+0.2% ± 1.0%)
Counters per txn
suite frontend stall l1i refill backend stall
pooler 3,492 (-0.7% ± 0.7%) 1,171 (-1.2% ± 0.6%) 37,861 (+0.8% ± 0.6%)
lb 3,630 (-0.1% ± 0.4%) 1,194 (-0.5% ± 0.8%) 38,861 (+0.6% ± 0.5%)
sharding-w2 5,796 (+1.3% ± 1.5%) 1,591 (+1.2% ± 1.1%) 112,470 (-2.2% ± 1.8%)
sharding-w8 3,103 (+1.8% ± 0.6%) 992 (+1.9% ± 0.4%) 171,559 (-1.4% ± 1.4%)

Microbench: all 504 measures within bounds.

Hot code per txn
path base head change
pooler 111.2 KB 111.7 KB +0.5 KB
sharding-w2 164.7 KB 163.4 KB -1.3 KB

pooler:

crate base head change
pgdog::frontend::client::query_engine 20.9 KB 21.2 KB +0.4 KB
pgdog::backend::pool 18.6 KB 18.4 KB -0.1 KB
tokio 18.3 KB 18.3 KB +0.0 KB
pgdog::frontend (other) 9.4 KB 9.4 KB +0.0 KB
aws-lc (TLS crypto) 9.1 KB 9.1 KB +0.0 KB
rustls (TLS) 7.6 KB 7.8 KB +0.1 KB
pgdog::backend::server 6.9 KB 7.0 KB +0.1 KB
other 5.9 KB 6.0 KB +0.1 KB
everything else 14.4 KB 14.4 KB +0.0 KB
function head change
pgdog::frontend::client::Client::run::{{closure}} 7.2 KB +0.1 KB
pgdog::frontend::client::query_engine::query::<impl pgdog::frontend::client::query_engine: 6.3 KB +0.1 KB
pgdog::frontend::client::query_engine::QueryEngine::handle::{{closure}} 4.9 KB +0.1 KB
tokio::runtime::task::raw::poll 3.7 KB +0.0 KB
pgdog::frontend::client::query_engine::query::<impl pgdog::frontend::client::query_engine: 3.2 KB +0.1 KB

sharding-w2:

crate base head change
pgdog::frontend::router 28.7 KB 28.2 KB -0.4 KB
pgdog::frontend::client::query_engine 22.6 KB 22.5 KB -0.1 KB
pgdog::backend::pool 18.9 KB 18.9 KB +0.0 KB
tokio 18.3 KB 18.5 KB +0.2 KB
pgdog::frontend (other) 10.7 KB 10.6 KB -0.1 KB
aws-lc (TLS crypto) 10.4 KB 10.4 KB +0.0 KB
other 10.0 KB 10.1 KB +0.1 KB
pgdog::backend::server 7.9 KB 7.9 KB +0.0 KB
everything else 37.2 KB 36.3 KB -0.9 KB
function head change
pgdog::frontend::client::Client::run::{{closure}} 8.1 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.6 KB +0.0 KB
pgdog::frontend::router::parser::query::select::<impl pgdog::frontend::router::parser::que 4.2 KB -0.2 KB
tokio::runtime::task::raw::poll 3.9 KB +0.1 KB
lb flamegraph vs e084496

lb

Open interactive for hover, zoom and search.

lb removed

Open interactive.

microbench flamegraph vs e084496

microbench

Open interactive for hover, zoom and search.

microbench removed

Open interactive.

pooler flamegraph vs e084496

pooler

Open interactive for hover, zoom and search.

pooler removed

Open interactive.

sharding-w2 flamegraph vs e084496

sharding-w2

Open interactive for hover, zoom and search.

sharding-w2 removed

Open interactive.

sharding-w8 flamegraph vs e084496

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 56c21c2 with e084496 on c7i.metal-48xl (on-demand).

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.

1 participant