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(customer): index namespace-scoped list ordering - #5505
Customer listing filters by namespace and orders by name or created_at, but those columns only had global single-column indexes. Postgres can walk the global customer_name index in sort order and filter rows by namespace. In a large table this reads most of the index before it finds a page for a namespace with a few thousand customers.
This replaces the global indexes with namespace-scoped ones:
On a table of about 400k customers across many namespaces, EXPLAIN ANALYZE of the list query (WHERE namespace = $1 AND (deleted_at IS NULL OR deleted_at >= $2) ORDER BY name LIMIT 20) showed:
Namespace size
Plan
Time
A few hundred customers
customer_namespace scan + sort
<1 ms
A few thousand customers
Index Scan using customer_name, filter on namespace
~12 s
Hundreds of thousands of customers
Index Scan using customer_name
<3 ms
Ordering by created_at has the same shape (tens to hundreds of ms). Ordering by id is already served by (namespace, id).
The only callers that order by these columns are the namespace-scoped list queries in openmeter/customer/adapter, so nothing depends on the global indexes.
Migration
The migrations create the composite indexes first, then drop the global ones. Each statement uses CONCURRENTLY and runs in its own migration file, following the existing convention.
Testing
make migrate-check passes.
Summary by CodeRabbit
Performance
Customer name and creation-date lookups are now indexed within their namespace.
Global name and creation-date indexes have been removed; namespace-specific indexes support these queries instead.
Confidence Score: 5/5
The PR appears safe to merge; no actionable defects were found.
Summary
Replaces global customer ordering indexes with (namespace, name) and (namespace, created_at) indexes.
Customer lists find name and creation-time results within their namespace.
No actionable issues were found. This review did not run migration checks or database benchmarks.
No actionable comments were generated in the recent review.
Walkthrough
Customer indexes now pair namespace with name and created_at. Migrations add the composite indexes and remove the prior global indexes. The migration rollback files recreate the global indexes or drop the composite indexes concurrently.
Changes: Customer index scoping
Layer / File(s)
Summary
Schema and index migrations openmeter/ent/schema/customer.go, tools/migrate/migrations/20261113012708_*, tools/migrate/migrations/20261113012709_*, tools/migrate/migrations/20261113012710_*, tools/migrate/migrations/20261113012711_*
The customer schema uses namespace, name and namespace, created_at indexes. Migrations add these composite indexes and drop the global indexes. The down migrations provide concurrent rollback operations.
This change swaps the global customer name and created_at indexes for namespace-scoped composite indexes, which should speed up namespace-filtered list queries. The migrations create the new indexes before dropping the old ones and can be rolled back. No concrete merge-blocking risk was found. The missing release-note label still needs to be added before merge.
Pre-merge checks: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
The title names the main change: composite indexes that scope customer list ordering to a namespace. The raw summary and PR objectives confirm that the global name and created_at indexes are replaced …
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 names the main change: composite indexes that scope customer list ordering to a namespace. The raw summary and PR objectives confirm that the global name and created_at indexes are replaced with (namespace, name) and (namespace, created_at). The perf() prefix matches the stated goal of faster namespace-scoped list queries. The title is specific, concise, and free of file lists or vague terms.
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.
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
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.
Summary
Customer listing filters by namespace and orders by
nameorcreated_at, but those columns only had global single-column indexes. Postgres can walk the globalcustomer_nameindex in sort order and filter rows by namespace. In a large table this reads most of the index before it finds a page for a namespace with a few thousand customers.This replaces the global indexes with namespace-scoped ones:
customer_name (name)→customer_namespace_name (namespace, name)customer_created_at (created_at)→customer_namespace_created_at (namespace, created_at)Why
On a table of about 400k customers across many namespaces,
EXPLAIN ANALYZEof the list query (WHERE namespace = $1 AND (deleted_at IS NULL OR deleted_at >= $2) ORDER BY name LIMIT 20) showed:customer_namespacescan + sortIndex Scan using customer_name, filter on namespaceIndex Scan using customer_nameOrdering by
created_athas the same shape (tens to hundreds of ms). Ordering byidis already served by(namespace, id).The only callers that order by these columns are the namespace-scoped list queries in
openmeter/customer/adapter, so nothing depends on the global indexes.Migration
The migrations create the composite indexes first, then drop the global ones. Each statement uses
CONCURRENTLYand runs in its own migration file, following the existing convention.Testing
make migrate-checkpasses.Summary by CodeRabbit


Confidence Score: 5/5
The PR appears safe to merge; no actionable defects were found.Summary
Replaces global customer ordering indexes with
(namespace, name)and(namespace, created_at)indexes.No actionable issues were found. This review did not run migration checks or database benchmarks.
Reviews (1) · Last reviewed commit: "perf(customer): index namespace-scoped l..." · Reviewed by Greptile