Skip to content

perf(billing): index invoice line charge lookups - #5506

Merged
turip merged 1 commit into
mainfrom
perf/billing-line-charge-index
Nov 13, 2026
Merged

turip merged 1 commit into
mainfrom
perf/billing-line-charge-index

Conversation

@tothandras

@tothandras tothandras commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds a (namespace, charge_id) index on billing_invoice_lines.

Why

Subscription reference updates on invoice lines filter by namespace and charge_id:

UPDATE billing_invoice_lines SET updated_at = $1, subscription_id = $2, subscription_phase_id = $3, subscription_item_id = $4
WHERE namespace = $5 AND charge_id = $6
  AND EXISTS (SELECT 1 FROM billing_invoices WHERE id = billing_invoice_lines.invoice_id AND status = $7)

No index includes charge_id, so on a table of about 300k lines the plan is a parallel sequential scan of billing_invoice_lines that takes about 60 ms per call. These updates run once per charge during sync, so in large namespaces they add up to a large share of database time.

The index is full rather than partial. The reference updates do not filter on deleted_at, and the subscription line lookups also match on charge_id IS NULL.

Testing

  • make migrate-check passes.
  • go test ./openmeter/billing/adapter/... passes.

Summary by CodeRabbit

  • Performance
    • Added an index to support faster lookups of billing invoice lines by namespace and charge.

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; no actionable issues were found.

Summary

Adds a full, non-unique index on billing_invoice_lines using namespace and charge_id to speed up charge lookups.

  • Invoice line updates find charge-linked rows through a namespace index.

Reviews (2) · Last reviewed commit: "perf(billing): index invoice line charge..." · Reviewed by Greptile

@tothandras
tothandras requested a review from a team as a code owner November 13, 2026 01:49
@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The billing invoice line schema adds a composite index on namespace and charge_id. A migration creates the index concurrently, and its down migration drops it concurrently.

Changes: Invoice line charge lookup index

Layer / File(s) Summary
Define and migrate the composite index
openmeter/ent/schema/billing.go, tools/migrate/migrations/20261113062618_billing_invoice_line_charge_lookup.up.sql, tools/migrate/migrations/20261113062618_billing_invoice_line_charge_lookup.down.sql
The schema declares an index on namespace and charge_id. The up migration creates it concurrently, and the down migration drops it concurrently.

Estimated code review effort: 2 (Simple) | ~5 minutes

Change: Refactor

Suggested reviewers: turip, borosr

Merge Risk: Minimal · up to c44a3

This adds an index to speed up invoice line charge lookups, with no behavior change. No actionable merge risk remains.

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(billing): index invoice line charge lookups" matches the main change. The PR adds a non-unique index on (namespace, charge_id) for billing_invoice_lines, which speeds up charge lookups…
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(billing): index invoice line charge lookups" matches the main change. The PR adds a non-unique index on (namespace, charge_id) for billing_invoice_lines, which speeds up charge lookups. The summary, migration files, and stated objectives all describe this change. The title is short, specific, and follows a conventional commit style.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between c4929bd and c44a368.

Files ignored due to path filters: 2

  • openmeter/ent/db/migrate/schema.go is excluded by !**/ent/db/**
  • tools/migrate/migrations/atlas.sum is excluded by !**/*.sum, !**/*.sum
Files selected for processing: 2

  • tools/migrate/migrations/20261113062618_billing_invoice_line_charge_lookup.down.sql
  • tools/migrate/migrations/20261113062618_billing_invoice_line_charge_lookup.up.sql

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@turip turip added release-note/bug-fix Release note: Bug Fixes area/billing labels Nov 13, 2026
@turip
turip force-pushed the perf/billing-line-charge-index branch from c4929bd to c44a368 Compare November 13, 2026 06:29

@turip turip left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the schema, generated index definition, concurrent up/down migrations, and charge lookup queries. The regenerated migration follows the existing migration sequence. Migration validation, targeted billing adapter tests, scoped fast lint, and CI passed. No actionable findings remain.

@turip
turip merged commit cf7289d into main Nov 13, 2026
32 checks passed
@turip
turip deleted the perf/billing-line-charge-index branch November 13, 2026 06:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants