Enable Zoekt namespaces when a GitLab.com subscription changes

What does this MR do and why?

On GitLab.com a namespace with a new paid or trial subscription waits up to 2 hours (about 1 hour on average) for Search::Zoekt::SaasRolloutEventWorker to create its Search::Zoekt::EnabledNamespace. Advanced search starts immediately, from GitlabSubscription#index_namespace. This MR starts Zoekt just as early, the same way, behind zoekt_enable_namespace_on_subscription_change (gitlab_com_derisk, off by default).

GitlabSubscription#enable_zoekt_namespace runs after commit, next to index_namespace, when a subscription is created or hosted_plan_name_uid, trial, trial_ends_on or end_date changes. Other updates, about 20,500 a day, skip it. On GitLab.com, for a root namespace with the flag on:

  1. It checks active_paid_or_trial? on the record. Its spec keeps it in agreement with GitlabSubscription.with_active_paid_or_trial_hosted_plan, the sweep's rule.
  2. It inserts the EnabledNamespace with insert_all(unique_by: :root_namespace_id), the same insert the sweep uses.
  3. When a row was created, it queues Search::Zoekt::RolloutWorker instead of waiting up to 10 minutes for its cron.

SaasRolloutEventWorker is unchanged and remains the safety net. Errors in the callback are tracked, not raised: after_commit callbacks run in reverse order, so raising would skip index_namespace, the seat callout reset and the after-commit queue.

Why not a shorter sweep period
  • Each sweep run takes p50 65 s and up to 456 s on GitLab.com, almost all database time, to find 150–430 new namespaces. A 1-minute period would scan all ~121k eligible namespaces back to back and still be minutes behind advanced search.
  • The callback costs one insert, only when a plan or term changes.
Checked before implementing
  • All application writes to gitlab_subscriptions go through model callbacks: Namespace#generate_subscription, the public and internal gitlab_subscription API endpoints, and admin nested attributes. No update_all, update_column, insert_all or upsert exists on the table outside two hosted_plan_name_uid backfill migrations.
  • Eligibility is only gained by a write. Time can only end it, which the daily remove_expired_subscriptions task already handles.
  • automatically_index_in_elasticsearch? is a different rule: it checks end_date for trials, where the Zoekt scope uses trial_ends_on. active_paid_or_trial? keeps the callback and the sweep in agreement.
  • The existing GitlabSubscriptions::RenewedEvent fires only on a new term of an existing subscription, not on create, plan upgrade or trial start, so it can't drive this.

References

Screenshots or screen recordings

Not applicable: backend only, no UI change.

Database

New query run by GitlabSubscription#enable_zoekt_namespace, after commit, only when a subscription is created or hosted_plan_name_uid, trial, trial_ends_on or end_date changes, on GitLab.com, for a root namespace with the flag on.

Enable the namespace

INSERT INTO "zoekt_enabled_namespaces" ("root_namespace_id","created_at","updated_at") VALUES (<namespace_id>, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP) ON CONFLICT ("root_namespace_id") DO NOTHING RETURNING "id"

New namespace: https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/75426/commands/180127 (1 row inserted, 82.2 ms cold, of which 56.6 ms is the namespaces foreign key check, buffers: 39 hit / 25 read)

Already enabled: https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/75426/commands/180129 (Conflict on unique_zoekt_enabled_namespaces_on_root_namespace_id, 0 rows, 1.1 ms, buffers: 26 hit / 2 read)

namespace&.root? also loads the subscription's namespace through belongs_to, by primary key (find_namespaces_by_id(<namespace_id>)). That is the existing association query, not a new shape.

How the plans were made
  • SQL: recorded from enable_zoekt_namespace in GDK.
  • Database Lab: snapshot from 2026-10-29 14:53 UTC.
  • New insert: a root namespace not yet enabled.
  • Conflict: a namespace already enabled.
  • Timing: all plans are first runs, so the timings are cold.

How to set up and validate locally

  1. Run bin/rspec ee/spec/models/gitlab_subscription_spec.rb -e enable_zoekt_namespace.

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

🤖 Generated with Claude Code

Edited by Ravi Kumar

Merge request reports

Loading
Loading