Repository navigation
fix: support cross-shard subqueries that only return constants (e.g., Rails .limit(N).count) - #2066
Merged
Merged
Conversation
jkaczman
force-pushed
the
jk-rails-limit-count-const-return-fix
branch
from
November 18, 2026 20:06
7f5952e to
2e3b174
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Contributor
|
Metal bench (arm64) failed. 1 of 520 measures outside bounds.
Counters per txn
Microbench: 506 of 507 measures within bounds. Hot code per txn
pooler:
sharding-w2:
Logslb, microbench, pooler, sharding-w2, sharding-w8, verdict.json |
jkaczman
force-pushed
the
jk-rails-limit-count-const-return-fix
branch
from
November 18, 2026 21:24
2e3b174 to
6c0bda2
Compare
Contributor
|
Metal bench (x86) passed. All 520 measures within bounds.
Counters per txn
Microbench: all 507 measures within bounds. Microbench latency: 1 benches changed more than 3%
Hot code per txn
pooler:
sharding-w2:
Logslb, microbench, pooler, sharding-w2, sharding-w8, verdict.json |
jkaczman
force-pushed
the
jk-rails-limit-count-const-return-fix
branch
4 times, most recently
from
November 18, 2026 22:50
f104a75 to
118466b
Compare
… Rails .limit(N).count)
jkaczman
force-pushed
the
jk-rails-limit-count-const-return-fix
branch
from
November 19, 2026 13:56
118466b to
521afa7
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When we re-write a cross-shard subquery (#381) into a new
SELECTstatement to replace the subquery with:unnest(..)arrays (perf: migrate to unnest (from VALUES) for CTEs, INSERT .. SELECT FROM, SELECT/UPDATE FROM #2003)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, returningunnest()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).countwith Rails, Rails sends a subquery that returns the constant1for each row that it matches (no columns in SELECT list) (Rails source). E.g., forFollowRequest.where(..).limit(40).count, Rails sendsNow, 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}, sounnest(..)returns 3 rows, and, thus, COUNT(*) correctly returns 3.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).