Skip to content

perf(features): Introduce materialized view for Features#subscriptions_count#5889

Open
vankiru wants to merge 5 commits into
mainfrom
fix/features-subscriptions-count
Open

perf(features): Introduce materialized view for Features#subscriptions_count#5889
vankiru wants to merge 5 commits into
mainfrom
fix/features-subscriptions-count

Conversation

@vankiru

@vankiru vankiru commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Context

The subscription count on the Features page is currently incorrect. Right now, it only counts subscriptions that get a feature through their plan. It doesn't include subscriptions that have the feature assigned directly on their Entitlements page, and it doesn't account for subscriptions where the feature has been removed.

Description

This PR adds a materialized view that precomputes and stores the subscription count for every non-deleted feature that has at least one associated subscription.

The count is calculated in four steps for each feature:

  1. Count subscriptions that get the feature through their plan. If a subscription has an overridden plan, it's counted towards the parent plan's features, since plan overrides can't have their own entitlements.

  2. Count subscriptions that have the feature assigned directly on their Entitlements page. This step also removes duplicates when a subscription gets the same feature both through its plan and via a direct assignment.

  3. Count subscriptions where the feature has been manually removed.

  4. Calculate the final count as:

    plan subscriptions + direct subscriptions - feature removals

Refreshing the Materialized View

On a database with around 2.5M subscriptions and 4M entitlements, refreshing the view takes about 3 seconds. On databases roughly 10× larger, it may take up to 30 seconds.

To balance performance with data freshness, the view is refreshed every 10 minutes. This helps avoid refresh-time spikes while keeping the counts reasonably up to date.

@vankiru vankiru marked this pull request as ready for review July 9, 2026 08:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant