Skip to content

perf(ledger): scope history and balance lookups by namespace - #5431

Merged
GAlexIHU merged 2 commits into
mainfrom
codex/history-query-namespace
Nov 3, 2026
Merged

GAlexIHU merged 2 commits into
mainfrom
codex/history-query-namespace

Conversation

@GAlexIHU

@GAlexIHU GAlexIHU commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Overview

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.

RetriggerView in GreptileConfidence 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.

Reviews (2) · Last reviewed commit: "perf(ledger): scope group and balance lo..." · Reviewed by Greptile

@GAlexIHU GAlexIHU added the release-note/bug-fix Release note: Bug Fixes label Nov 3, 2026
@GAlexIHU
GAlexIHU requested a review from a team as a code owner November 3, 2026 15:35
@coderabbitai

coderabbitai Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

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.

Changes: Ledger namespace scope

Layer / File(s) Summary
Scope transaction-group retrieval
openmeter/ledger/historical/adapter/ledger.go, openmeter/ledger/historical/adapter/ledger_test.go
Transaction-group lookup filters by namespace and group ID. Eager-loaded transactions also use the namespace filter.
Scope transaction-list predicates
openmeter/ledger/historical/adapter/ledger.go, openmeter/ledger/historical/adapter/listtransactions_query_test.go
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.
Scope subaccount query predicates
openmeter/ledger/historical/adapter/sumentries_query.go, openmeter/ledger/historical/adapter/ledger_test.go, openmeter/ledger/historical/adapter/*query_test.go
Subaccount lookups in sum-entry and balance queries use namespace constraints. SQL expectations include the added namespace parameters and updated placeholder positions.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Refactor

Possibly related PRs

Suggested labels: kind/refactor

Suggested reviewers: mark-vass-konghq, turip, `liormechlovich-kong

Merge Risk: Minimal · up to d16fd

Namespace-scoped ledger reads have no identified merge-blocking issue.

Pre-merge checks: Passed 4
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 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 Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed 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.

Share

CodeRabbit Verify

  • Run CodeRabbit Verify to test the changes in this pull request.
Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 4f1146e and d16fd26.

Files selected for processing: 6
  • openmeter/ledger/historical/adapter/balancebuckets_query_test.go
  • openmeter/ledger/historical/adapter/balancesatboundaries_query_test.go
  • openmeter/ledger/historical/adapter/ledger.go
  • openmeter/ledger/historical/adapter/ledger_test.go
  • openmeter/ledger/historical/adapter/minimumbalancebuckets_query_test.go
  • openmeter/ledger/historical/adapter/sumentries_query.go

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 GAlexIHU changed the title perf(ledger): scope history entry lookups by namespace perf(ledger): scope history and balance lookups by namespace Nov 3, 2026
@GAlexIHU
GAlexIHU merged commit 607f3d8 into main Nov 3, 2026
31 checks passed
@GAlexIHU
GAlexIHU deleted the codex/history-query-namespace branch November 3, 2026 16:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note/bug-fix Release note: Bug Fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants