Add override table and range column for p_ci_builds
What does this MR do and why?
This MR implements the database foundation of a two-layer strategy to resolve partition_id from a build_id without performing expensive cross-partition scans on p_ci_builds. It is part of the corrective action for INC-8367, where high-frequency Sidekiq workers querying CI partitioned tables without a partition_id caused LockManager LWLock saturation on CI replicas.
This MR has been split into smaller ones. The work is tracked across the following MRs:
| MR | Description |
|---|---|
| !228838 (merged) (this MR) | Add ci_builds_partition_overrides table and builds_id_range column on ci_partitions |
| !231142 (closed) | Add trigger to create overflow records |
| !229866 (closed) | Add switching logic to capture partition range intervals |
| !231138 (closed) | Add backfill migrations for builds partition tracking |
What is added
ci_builds_partition_overrides table — a new hash-partitioned table (3 partitions) that stores (build_id, partition_id, project_id) for every CI build that was inserted into a partition other than the one active at insertion time (i.e. "spillover" records). It is keyed by build_id for fast single-row lookups and carries a cascading foreign key to p_ci_builds so it stays consistent automatically when rows are moved between partitions. Based on observed spillover rates (0.01–0.6%), this table is expected to hold ~50 million rows.
builds_id_range column on ci_partitions — an int8range column that records the range of build IDs that were generated while each partition was the active write target. This is the fallback layer: when no override row exists (the common case for the vast majority of builds), the correct partition_id can be derived from a sequential scan of this ~12-row table in under a millisecond. A GiST exclusion constraint prevents overlapping ranges across partitions.
Migrations included
CreateCiBuildsPartitionOverrides— creates the hash-partitioned override table with 3 static partitions and an index onproject_id.AddBuildsIdRangeToCiPartitions— adds thebuilds_id_range int8rangecolumn and the no-overlap GiST exclusion constraint toci_partitions.AddFkToCiBuildsPartitionOverrides(post-migrate) — adds the cascading partitioned foreign key fromci_builds_partition_overridestop_ci_builds.
Why this approach
Changing all Sidekiq worker signatures to accept a partition_id is not feasible short-term — it requires coordinated multi-release changes across every affected worker. This two-layer lookup (override table + ID range index) resolves partition_id transparently at query time with no worker signature changes, no Redis dependency on the hot path, and full durability via the FK cascade.
Changelog: other
References
- Related issue: #593701 (closed) — CI partition pruning: cache record_id → partition_id to fix unpruned single id queries
- Incident review: https://gitlab.com/gitlab-com/gl-infra/production/-/work_items/21534#note_3158996366
- CI partitioning design doc: https://handbook.gitlab.com/handbook/engineering/architecture/design-documents/ci_data_decay/pipeline_partitioning/
Screenshots or screen recordings
N/A — database migrations only
| Before | After |
|---|---|
How to set up and validate locally
- Run
db:migrate— verifiesci_builds_partition_overridesis created with 3 hash partitions andci_partitions.builds_id_rangeis added. - Run
db:migrate:post_deploy— verifies the FK is added. - Inspect
ci_partitionsrows to confirmbuilds_id_rangecolumn exists. - Run the spec suite for the migration specs.
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.
Related to #593701 (closed)