Add UNIQUE index with sharding column to ci_runner_machines

What does this MR do and why?

This MR adds a new UNIQUE index on ci_runner_machines that includes the sharding column (organization_id) to support the Cells architecture.

Changes:

  • Adds a new unique partitioned index idx_ci_runner_machines_on_runner_id_type_system_xid_org_id on columns: runner_id, runner_type, system_xid, organization_id
  • Updates the RunnerManager.for_runner scope to include organization_id in the WHERE clause to leverage the new index
  • Creates partition-specific indexes for all three runner types (instance, group, project)

This index will replace the current index_ci_runner_machines_on_runner_id_and_type_and_system_xid index by including the sharding column, which is required for proper query routing in a Cells environment.

Changelog: added

References

Cells: Handle unique index gitlab_ci.group_type... (#562046 - closed)

Screenshots or screen recordings

N/A - Database migration

Before After
Index without organization_id Index with organization_id as first column

How to set up and validate locally

  1. Run the migration: bundle exec rails db:migrate

  2. Verify the new index exists:

    \d ci_runner_machines
  3. Check that queries using Ci::RunnerManager.for_runner use the new index in the query plan

    gitlabhq_test_ci=# explain SELECT "ci_runner_machines".*
      FROM "ci_runner_machines"
      WHERE "ci_runner_machines"."organization_id" = 1
        AND "ci_runner_machines"."runner_id" = 1434
        AND "ci_runner_machines"."runner_type" = 3
      LIMIT 11;
    ┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
                                                                                        QUERY PLAN                                                                                    
    ├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
     Limit  (cost=0.14..2.17 rows=1 width=342)                                                                                                                                        
       ->  Index Scan using idx_proj_ci_runner_machines_on_runner_id_type_system_xid_org_id on project_type_ci_runner_machines ci_runner_machines  (cost=0.14..2.17 rows=1 width=342) 
             Index Cond: ((runner_id = 1434) AND (runner_type = 3) AND (organization_id = 1))                                                                                         
    └──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
    (3 rows)

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.

Edited by Pedro Pombeiro

Merge request reports

Loading