fix: optimize customer usage attribution lookup#4684
Merged
Conversation
Contributor
|
Note Currently processing new changes in this PR. This may take a few minutes, please wait... ⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
turip
marked this pull request as ready for review
July 10, 2026 12:54
Contributor
|
Caution Failed to replace (edit) comment. This is likely due to insufficient permissions or the comment being deleted. Error details |
GAlexIHU
approved these changes
Jul 10, 2026
gergely-kurucz-konghq
approved these changes
Jul 10, 2026
gergely-kurucz-konghq
added a commit
that referenced
this pull request
Jul 17, 2026
GetCustomersByUsageAttribution had the same cross-table OR anti-pattern PR #4684 fixed for the single-key lookup, causing a Postgres seq scan on large namespaces. Split into two independently-indexable UNION ALL branches, same as the single-key fix, without lookup_priority (bulk still returns every match). Benchmark: 222ms -> 1ms (220x) at 100 keys / 100k customers.
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.
Summary
ORinGetCustomerByUsageAttributionwith two independently indexable lookup branches combined withUNION ALLWhy
On large customer namespaces, subject-key customer resolution regressed from sub-second behavior to roughly 2-4 seconds per lookup. The old predicate combines a customer-key condition with a correlated subject lookup using a cross-table
OR. PostgreSQL can respond by scanning the customer namespace and applying the two alternatives as a filter, even though each lookup is independently indexed.This lookup is used by entitlement and event-resolution paths, so the per-lookup delay can cascade into API timeouts and worker failures. The new query selects one candidate customer ID through indexed branches, then lets the existing Ent query load that customer normally.
Query plans
Representative local plans below use a synthetic namespace with 100,000 customers and one subject per customer.
Cross-table OR
The subject lookup uses its index, but the cross-table
ORstill leaves PostgreSQL scanning and filtering the customer namespace.UNION ALL candidate lookup
Both alternatives enter through their existing indexes and the outer customer fetch uses the selected primary key.
Benchmark
Synthetic local benchmark, 100,000 customers, median of three runs with ten timed iterations per case:
Correctness coverage
Validation
make testnix develop --impure .#ci -c make lint-go-fastgo test -tags=dynamic ./openmeter/customer/adapter -count=1Greptile Summary
This PR speeds up customer usage attribution lookup. The main changes are:
ORwith aUNION ALLcandidate lookup.Confidence Score: 5/5
This looks safe to merge after checking the helper dependency signature.
openmeter/testutils/pg_driver.go
Important Files Changed
testing.TBso it can be called from benchmarks.Prompt To Fix All With AI
Reviews (1): Last reviewed commit: "fix: optimize customer usage attribution..." | Re-trigger Greptile
Context used:
Summary by CodeRabbit
Bug Fixes
Performance
Tests