fix(search): drop subquery wrapper that leaked LIMIT into COUNT#28
Merged
Conversation
After deploying the trigram autocomplete (PR #26) to prod, every result returned listingCount = limit instead of the true count: /api/search/locations?q=port&limit=1 → listingCount=1 ❌ /api/search/locations?q=port&limit=10 → listingCount=10 ❌ /api/search/locations?q=port&limit=20 → listingCount=20 ❌ Same prod data, only 1 grouped row, COUNT(*) varying with the outer LIMIT clause. The popular-locations path (no subquery wrapper) correctly returned 100 across the same data. Root cause: the subquery wrapper from PR #26 was added to keep similarity() outside the GROUP BY for clean ORDER BY ranking. But the planner under that shape pushed the outer LIMIT into the inner GROUP BY's COUNT — a known Postgres planner pathology with GIN-indexed predicates and outer LIMITs over aggregates. Fix: drop the subquery. Aggregate similarity with MAX(GREATEST(...)) in the same SELECT as COUNT. similarity(grouped_col, const) is functionally constant per group, so MAX is idempotent — same ranking semantics, no subquery, no planner bug. Verified locally with the same 19 tests; will verify prod after deploy reaches READY: curl '...?q=port&limit=1' → expect listingCount=100 curl '...?q=port&limit=10' → expect listingCount=100 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This was referenced May 10, 2026
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.
The bug
After #26 deployed, every autocomplete result returns
listingCount = limitinstead of the true count:The popular-locations path (single SELECT, no subquery wrapper) returns 100 correctly against the same data. So the data and the JOIN are fine — the bug is in the trigram subquery shape.
Root cause
#26 wrapped the aggregation in a subquery to keep
similarity(l.city, $q)out of the GROUP BY (so it could be ranked in the outer ORDER BY):Under that shape the planner pushes the outer
LIMITinto the innerGROUP BY'sCOUNT— a Postgres planner pathology that surfaces with GIN-indexed predicates plus an outer LIMIT over aggregates. Result:COUNT(*) ≡ LIMIT.Fix
Drop the subquery. Aggregate similarity with
MAX(GREATEST(...))in the same SELECT as COUNT:similarity(grouped_col, const)is functionally constant per group, soMAXis idempotent — same ranking semantics, no subquery, no planner bug.Validation
pnpm typecheck→ exit 0pnpm vitest run tests/actions/search-actions.test.ts→ 19 / 19 pass?q=port&limit=1→listingCount=100🤖 Generated with Claude Code