Skip to content

fix: support cross-shard subqueries that only return constants (e.g., Rails .limit(N).count) - #2066

Merged
jkaczman merged 1 commit into
mainfrom
jk-rails-limit-count-const-return-fix
Nov 19, 2026
Merged

jkaczman merged 1 commit into
mainfrom
jk-rails-limit-count-const-return-fix

Conversation

@jkaczman

Copy link
Copy Markdown
Contributor

When we re-write a cross-shard subquery (#381) into a new SELECT statement to replace the subquery with:

However, there was a previous assumption of there always being a column returned; we didn't have a way to handle only constants. If no columns were returned, the subquery would be rewritten as unnest() with no args (which Postgres errors on, returning unnest() does not exist error)

Thus, this PR implements functionality to properly handle subqueries that only return constants.

Why is this important / common? When calling .limit(N).count with Rails, Rails sends a subquery that returns the constant 1 for each row that it matches (no columns in SELECT list) (Rails source). E.g., for FollowRequest.where(..).limit(40).count, Rails sends

SELECT COUNT(*) FROM (SELECT 1 AS one FROM follow_requests WHERE target_account_id = $1 LIMIT $2) subquery_for_count

Now, as a bit of a workaround, we send an array with a NULL for each row that the subquery returned. E.g., if the subquery returns 3 rows, we send {NULL,NULL,NULL}, so unnest(..) returns 3 rows, and, thus, COUNT(*) correctly returns 3.

SELECT count(*) FROM (SELECT 1 FROM unnest($1::pg_catalog._int4) u(c1)) subquery_for_count(one)

This is motivated by Mastodon (written in Ruby on Rails). When a user logs in, Mastodon counts the pending follow requests sent to the user with FollowRequest.where(..).limit(40).count (Mastodon source).

@jkaczman
jkaczman added this pull request to stack #2041 November 18, 2026 19:36
@jkaczman
jkaczman force-pushed the jk-rails-limit-count-const-return-fix branch from 7f5952e to 2e3b174 Compare November 18, 2026 20:06
@codecov

codecov Bot commented Nov 18, 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

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

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

suite throughput cpu/txn user cycles/txn user instr/txn kernel cycles/txn kernel instr/txn ctx/txn
pooler 19.06 (+1.0% ± 1.2%) 17,900 (+0.3% ± 0.5%) 31,453 (-0.0% ± 0.05%) 30,867 (+1.3% ± 2.2%) 51,015 (-0.3% ± 0.4%)
lb 19.54 (+0.4% ± 1.7%) 18,469 (-0.1% ± 0.9%) 32,358 (-0.0% ± 0.05%) 31,314 (+0.8% ± 2.8%) 52,155 (-0.1% ± 0.4%)
sharding-w2 32.7 (-0.0% ± 2.2%) 43,733 (-0.4% ± 0.4%) 94,979 (+0.0% ± 0.07%) 41,085 (+0.2% ± 5.6%) 53,656 (-0.0% ± 0.10%) 1.546 (-0.0% ± 0.4%)
sharding-w8 247,189 (-0.2% ± 2.0%) 28.78 (+0.1% ± 2.2%) 43,721 (+0.1% ± 1.6%) 50,527 (+0.0% ± 0.03%) 35,721 (+0.1% ± 3.1%) 42,394 (+0.0% ± 0.06%) 0.1733 (+0.2% ± 4.2%)
Counters per txn
suite frontend stall l1i refill backend stall
pooler 7,615 (+1.1% ± 1.6%) 2,659 (+0.3% ± 0.8%)
lb 7,965 (+0.1% ± 2.3%) 2,706 (-0.0% ± 0.9%)
sharding-w2 18,086 (-1.1% ± 0.9%) 4,154 (-0.3% ± 0.8%)
sharding-w8 18,839 (-1.5% ± 7.6%) 3,929 (-1.0% ± 4.2%)
benchmark measure result baseline change bound
full_named_cached::having_distinct_limit alloc_count 535 526 +1.75% ± 0.94% +0%

Microbench: 506 of 507 measures within bounds.

Hot code per txn
path base head change
pooler 114.6 KB 113.8 KB -0.8 KB
sharding-w2 169.7 KB 168.4 KB -1.2 KB

pooler:

crate base head change
pgdog::frontend::client::query_engine 20.4 KB 20.4 KB +0.0 KB
tokio 18.4 KB 18.5 KB +0.1 KB
pgdog::backend::pool 17.8 KB 17.5 KB -0.3 KB
aws-lc (TLS crypto) 10.2 KB 10.1 KB -0.1 KB
rustls (TLS) 8.4 KB 8.4 KB +0.0 KB
pgdog::frontend (other) 8.0 KB 7.8 KB -0.2 KB
pgdog::backend::server 7.8 KB 7.8 KB +0.0 KB
other 6.0 KB 5.9 KB -0.1 KB
everything else 17.6 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.3 KB -0.1 KB
pgdog::frontend::client::query_engine::QueryEngine::handle::{{closure}} 4.4 KB +0.2 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.0 KB

sharding-w2:

crate base head change
pgdog::frontend::router 29.4 KB 29.2 KB -0.2 KB
pgdog::frontend::client::query_engine 21.8 KB 21.6 KB -0.2 KB
tokio 19.0 KB 18.4 KB -0.6 KB
pgdog::backend::pool 18.2 KB 18.1 KB -0.1 KB
aws-lc (TLS crypto) 13.0 KB 13.2 KB +0.2 KB
other 10.3 KB 10.4 KB +0.1 KB
pgdog::frontend (other) 8.6 KB 8.8 KB +0.2 KB
pgdog::backend::server 8.2 KB 8.1 KB -0.1 KB
everything else 41.1 KB 40.6 KB -0.5 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.1 KB
pgdog::frontend::client::query_engine::QueryEngine::handle::{{closure}} 4.1 KB -0.1 KB
pgdog::frontend::router::parser::query::select::<impl pgdog::frontend::router::parser::que 3.8 KB -0.1 KB
pgdog::frontend::client::query_engine::query::<impl pgdog::frontend::client::query_engine: 3.2 KB +0.0 KB
lb flamegraph vs 6b0e24d

lb

Open interactive for hover, zoom and search.

lb removed

Open interactive.

microbench flamegraph vs 6b0e24d

microbench

Open interactive for hover, zoom and search.

microbench removed

Open interactive.

pooler flamegraph vs 6b0e24d

pooler

Open interactive for hover, zoom and search.

pooler removed

Open interactive.

sharding-w2 flamegraph vs 6b0e24d

sharding-w2

Open interactive for hover, zoom and search.

sharding-w2 removed

Open interactive.

sharding-w8 flamegraph vs 6b0e24d

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 521afa7 with 6b0e24d on m8g.metal-24xl (spot).

@jkaczman
jkaczman force-pushed the jk-rails-limit-count-const-return-fix branch from 2e3b174 to 6c0bda2 Compare November 18, 2026 21:24
@github-actions

github-actions Bot commented Nov 18, 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.87 (-0.1% ± 0.6%) 18,652 (+0.1% ± 0.1%) 32,813 (-0.0% ± 0.03%) 33,700 (-0.2% ± 0.9%) 36,499 (+0.1% ± 0.4%)
lb 26.29 (+0.1% ± 0.5%) 19,244 (+0.1% ± 0.1%) 33,785 (-0.0% ± 0.02%) 33,842 (+0.1% ± 0.9%) 37,259 (+0.3% ± 0.7%)
sharding-w2 42.09 (+0.2% ± 0.8%) 46,958 (+0.5% ± 1.2%) 98,726 (+0.0% ± 0.01%) 38,627 (+0.0% ± 2.3%) 40,172 (+0.0% ± 0.4%) 1.525 (+0.0% ± 0.3%)
sharding-w8 207,093 (+0.2% ± 0.8%) 32.9 (-0.0% ± 0.4%) 46,035 (-0.1% ± 0.7%) 53,053 (-0.0% ± 0.03%) 31,164 (+0.0% ± 0.7%) 32,487 (-0.0% ± 0.07%) 0.1715 (-0.3% ± 1.1%)
Counters per txn
suite frontend stall l1i refill backend stall
pooler 3,301 (+0.3% ± 0.7%) 1,102 (+0.1% ± 0.4%) 39,155 (+0.0% ± 0.3%)
lb 3,564 (-0.1% ± 0.7%) 1,153 (-0.3% ± 0.7%) 39,416 (+0.4% ± 0.6%)
sharding-w2 6,528 (+0.5% ± 0.9%) 1,588 (+0.3% ± 0.7%) 112,098 (+0.9% ± 2.2%)
sharding-w8 3,404 (-0.1% ± 1.7%) 865 (-0.1% ± 1.0%) 177,764 (-0.0% ± 0.7%)

Microbench: all 507 measures within bounds.

Microbench latency: 1 benches changed more than 3%
benchmark latency baseline change instructions change
simple_cold::aggregate 8,785 ns 9,061 ns -3.04% ± 0.38% +0.00% ± 0.00%
Hot code per txn
path base head change
pooler 115.4 KB 116.1 KB +0.7 KB
sharding-w2 169.2 KB 168.8 KB -0.5 KB

pooler:

crate base head change
pgdog::frontend::client::query_engine 22.8 KB 23.0 KB +0.2 KB
tokio 18.4 KB 18.6 KB +0.2 KB
pgdog::backend::pool 18.1 KB 18.1 KB -0.1 KB
pgdog::frontend (other) 9.8 KB 9.6 KB -0.1 KB
aws-lc (TLS crypto) 8.8 KB 8.9 KB +0.1 KB
pgdog::backend::server 7.9 KB 8.2 KB +0.3 KB
rustls (TLS) 7.9 KB 7.9 KB +0.0 KB
other 5.9 KB 5.8 KB -0.1 KB
everything else 15.8 KB 15.9 KB +0.2 KB
function head change
pgdog::frontend::client::Client::run::{{closure}} 7.3 KB -0.2 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}} 5.2 KB +0.1 KB
pgdog::frontend::client::query_engine::query::<impl pgdog::frontend::client::query_engine: 3.9 KB +0.0 KB
tokio::runtime::task::raw::poll 3.8 KB +0.1 KB

sharding-w2:

crate base head change
pgdog::frontend::router 28.4 KB 28.1 KB -0.4 KB
pgdog::frontend::client::query_engine 23.8 KB 23.8 KB +0.0 KB
pgdog::backend::pool 18.9 KB 18.3 KB -0.6 KB
tokio 18.8 KB 18.1 KB -0.7 KB
pgdog::frontend (other) 10.9 KB 11.1 KB +0.1 KB
aws-lc (TLS crypto) 10.2 KB 10.4 KB +0.2 KB
other 10.1 KB 10.3 KB +0.2 KB
pgdog::backend::server 8.4 KB 8.8 KB +0.4 KB
everything else 39.7 KB 39.8 KB +0.1 KB
function head change
pgdog::frontend::client::Client::run::{{closure}} 8.4 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.8 KB +0.0 KB
pgdog::frontend::router::parser::query::select::<impl pgdog::frontend::router::parser::que 4.6 KB -0.1 KB
pgdog::frontend::client::query_engine::query::<impl pgdog::frontend::client::query_engine: 3.9 KB +0.1 KB
lb flamegraph vs 6b0e24d

lb

Open interactive for hover, zoom and search.

lb removed

Open interactive.

microbench flamegraph vs 6b0e24d

microbench

Open interactive for hover, zoom and search.

microbench removed

Open interactive.

pooler flamegraph vs 6b0e24d

pooler

Open interactive for hover, zoom and search.

pooler removed

Open interactive.

sharding-w2 flamegraph vs 6b0e24d

sharding-w2

Open interactive for hover, zoom and search.

sharding-w2 removed

Open interactive.

sharding-w8 flamegraph vs 6b0e24d

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 521afa7 with 6b0e24d on m7i.metal-48xl (spot).

@jkaczman
jkaczman force-pushed the jk-rails-limit-count-const-return-fix branch 4 times, most recently from f104a75 to 118466b Compare November 18, 2026 22:50
Base automatically changed from jk-router-derived-table-aliases to main November 19, 2026 13:56
@jkaczman
jkaczman force-pushed the jk-rails-limit-count-const-return-fix branch from 118466b to 521afa7 Compare November 19, 2026 13:56
@jkaczman
jkaczman merged commit 61e9a90 into main Nov 19, 2026
46 of 47 checks passed
@jkaczman
jkaczman deleted the jk-rails-limit-count-const-return-fix branch November 19, 2026 18:26
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