Skip to content

perf(customer): index namespace-scoped list ordering - #5505

Merged
turip merged 1 commit into
mainfrom
perf/customer-namespace-order-indexes
Nov 13, 2026
Merged

turip merged 1 commit into
mainfrom
perf/customer-namespace-order-indexes

Conversation

@tothandras

@tothandras tothandras commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

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:

  • 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 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.

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

Reviews (1) · Last reviewed commit: "perf(customer): index namespace-scoped l..." · 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

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.

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

Change: Refactor

Suggested reviewers: turip, gergely-kurucz-konghq

Merge Risk: Minimal · up to 80e01

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: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 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 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 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.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between d538903 and 80e0145.

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: 9

  • openmeter/ent/schema/customer.go
  • tools/migrate/migrations/20261113012708_customer_namespace_order_indexes_add_created_at.down.sql
  • tools/migrate/migrations/20261113012708_customer_namespace_order_indexes_add_created_at.up.sql
  • tools/migrate/migrations/20261113012709_customer_namespace_order_indexes_add_name.down.sql
  • tools/migrate/migrations/20261113012709_customer_namespace_order_indexes_add_name.up.sql
  • tools/migrate/migrations/20261113012710_customer_namespace_order_indexes_drop_global_created_at.down.sql
  • tools/migrate/migrations/20261113012710_customer_namespace_order_indexes_drop_global_created_at.up.sql
  • tools/migrate/migrations/20261113012711_customer_namespace_order_indexes_drop_global_name.down.sql
  • tools/migrate/migrations/20261113012711_customer_namespace_order_indexes_drop_global_name.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 the release-note/bug-fix Release note: Bug Fixes label Nov 13, 2026
@turip
turip merged commit a502aaa into main Nov 13, 2026
34 of 35 checks passed
@turip
turip deleted the perf/customer-namespace-order-indexes branch November 13, 2026 06:19
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