Use PipelinePreloader for ScheduledPipelineExecutionPolicyTestRun

What does this MR do and why?

Converts the with_associations scope on Security::ScheduledPipelineExecutionPolicyTestRun to use Ci::Preloaders::PipelinePreloader for partition-aware pipeline preloading, instead of relying on ActiveRecord's default preload(:pipeline).

Why

Ci::Pipeline is stored in a partitioned table (p_ci_pipelines). When ActiveRecord's default preloader loads a belongs_to :pipeline association in bulk, it issues a single SELECT ... WHERE id IN (...) query without a partition_id filter. This forces PostgreSQL to scan all partitions, which is inefficient and can cause N+1-style cross-partition queries as the number of partitions grows.

Ci::Preloaders::PipelinePreloader solves this by using Gitlab::Ci::Pipeline::BulkByIdLookup, which resolves the correct partition_id for each pipeline ID (via the cached Ci::Partition ranges) and issues partition-scoped queries. This avoids full partition scans and keeps pipeline lookups efficient as the CI sliding-list partitioning scheme adds new partitions over time.

What

  • Extracts a new preload_pipeline scope on ScheduledPipelineExecutionPolicyTestRun that chains with_partition_aware_preload.preload(:pipeline), delegating to the partition-aware preloader.
  • Refactors with_associations to call preload_pipeline first, then chains the remaining associations (project: :route, security_policy: :security_policy_management_project) and the order(id: :desc) clause.
  • Adds a spec asserting that with_associations preloads the security_policy association (a previously untested association in the scope).

Related: #601100

References

  • Issue: #601100
  • Ci::Preloaders::PipelinePreloader: app/models/ci/preloaders/pipeline_preloader.rb

Screenshots or screen recordings

Not applicable — backend-only change with no UI impact.

Before After

How to set up and validate locally

  1. Create a project with a security policy that has scheduled pipeline execution policy test runs.
  2. Query Security::ScheduledPipelineExecutionPolicyTestRun.with_associations and verify no N+1 queries are issued against p_ci_pipelines across partitions.
  3. Run the model spec: bundle exec rspec ee/spec/models/security/scheduled_pipeline_execution_policy_test_run_spec.rb

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 Marius Bobin

Merge request reports

Loading