You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
perf(ledger): scope history and balance lookups by namespace - #5431
Add namespace predicates where ledger queries look up entries, subaccounts, and group transactions. These lookups use indexes whose leading column is namespace:
Transaction listing: filtered entries, subaccounts, and net FBO movements.
Transaction-group loading: the separate eager-load query for child transactions.
Balance reads: subaccount lookups shared by booked balances, source minima, and history boundary balances.
Unfiltered reads still avoid unnecessary entry/subaccount lookups. No schema, balance semantics, ordering, or pagination changes.
Notes for reviewer
Benchmarked on my machine with PostgreSQL 14.20, existing migrated indexes, 500,000 synthetic transactions and 1,000,000 entries. The queried namespace contains 250,000 transactions. Medians of seven warmed, alternating EXPLAIN ANALYZE runs:
Customer history
Query
Before
After
50 transactions
Funded candidates
7.68ms
0.69ms
50 transactions
Consumed candidates
6.32ms
1.22ms
5,000 transactions
Funded candidates
26.16ms
20.74ms
5,000 transactions
Consumed candidates
71.21ms
52.11ms
One selected group
Child transactions
22.07ms
0.07ms
50 transactions
Booked balance
2.49ms
0.55ms
50 transactions
History balances, 50 rows
4.20ms
0.96ms
50 transactions
Source minimum
4.23ms
1.08ms
5,000 transactions
Booked balance
16.69ms
15.87ms
5,000 transactions
History balances, 50 rows
41.36ms
34.49ms
5,000 transactions
Source minimum
38.76ms
36.87ms
Results matched between variants. Gains depend on selectivity: an exploratory fixture where one customer owned all 5,000 namespace transactions regressed the funded lookup from roughly 15ms to 20ms. These are local query measurements, not endpoint latency predictions. Benchmark artifacts are not committed.
Validation:
SQL and bound-argument assertions cover listing, group loading, and each balance query.
PostgreSQL-backed historical adapter, FBO, and customer-balance suites.
golangci-lint run ./openmeter/ledger/historical/adapter/...
Summary by CodeRabbit
Bug Fixes
Transaction listings now consistently respect the selected namespace when filtering entries, subaccounts, and credit movements.
Credit movement results remain correctly scoped for positive, negative, and unspecified filters.
Transaction details and balance-related queries now stay within the selected namespace.
Empty ledgers continue to return an empty result without an error.
Confidence Score: 5/5
The PR appears safe to merge; no actionable issues were found.
Summary
Scopes ledger history lookups by namespace so PostgreSQL can use existing indexes.
Filtered transaction listings scope their entry lookups by namespace.
Filtered balance queries scope subaccount lookups by namespace.
Transaction groups load their transactions within the group's namespace.
No actionable comments were generated in the recent review.
Walkthrough
Ledger retrieval and query predicates now apply namespace constraints. Adapter tests verify the generated SQL and bound arguments for transaction listing and balance queries.
Predicate builders receive the full listing input and apply namespace constraints to entry, subaccount, and credit-movement queries. Tests check generated SQL, bound arguments, and empty-ledger results.
Subaccount lookups in sum-entry and balance queries use namespace constraints. SQL expectations include the added namespace parameters and updated placeholder positions.
Namespace-scoped ledger reads have no identified merge-blocking issue.
Pre-merge checks: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
The title "perf(ledger): scope history and balance lookups by namespace" matches the main change. The raw summary shows namespace predicates added to entry, subaccount, and FBO movement lookups in the…
Linked Issues check
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
Check skipped because no linked issues were found for this pull request.
Full details: Title check
Explanation
The title "perf(ledger): scope history and balance lookups by namespace" matches the main change. The raw summary shows namespace predicates added to entry, subaccount, and FBO movement lookups in the ledger historical adapter, and the PR objectives describe the same goal of letting PostgreSQL use index leading columns. The title says "balance lookups" while the objectives also mention group lookups, but the title still covers the primary change.
Finishing Touches
Generate docstrings
Commit to this branch
Create a new PR
Generate unit tests (beta)
Commit to this branch
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Tip
🎯 Customize your risk assessments
Guide how CodeRabbit assesses merge risk and security architecture risk. Highlight sensitive changes and explain your security boundaries using Merge Risk Instructions and Security Risk Instructions in your review settings. Security architecture risk assessments require the Advanced plan or above.
✅ Automatic approvals, with your rules
Let CodeRabbit approve pull requests once required threads are resolved, pre-merge checks pass, and your risk limits are met. Enable Automatic approvals in review settings, with overrides for teams or file paths.
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.
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.
GAlexIHU
changed the title
perf(ledger): scope history entry lookups by namespace
perf(ledger): scope history and balance lookups by namespace
Nov 3, 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
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.
Overview
Add namespace predicates where ledger queries look up entries, subaccounts, and group transactions. These lookups use indexes whose leading column is namespace:
Unfiltered reads still avoid unnecessary entry/subaccount lookups. No schema, balance semantics, ordering, or pagination changes.
Notes for reviewer
Benchmarked on my machine with PostgreSQL 14.20, existing migrated indexes, 500,000 synthetic transactions and 1,000,000 entries. The queried namespace contains 250,000 transactions. Medians of seven warmed, alternating EXPLAIN ANALYZE runs:
Results matched between variants. Gains depend on selectivity: an exploratory fixture where one customer owned all 5,000 namespace transactions regressed the funded lookup from roughly 15ms to 20ms. These are local query measurements, not endpoint latency predictions. Benchmark artifacts are not committed.
Validation:
golangci-lint run ./openmeter/ledger/historical/adapter/...Summary by CodeRabbit


Confidence Score: 5/5
The PR appears safe to merge; no actionable issues were found.Summary
Scopes ledger history lookups by namespace so PostgreSQL can use existing indexes.
Reviews (2) · Last reviewed commit: "perf(ledger): scope group and balance lo..." · Reviewed by Greptile