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 on project_id.
  • AddBuildsIdRangeToCiPartitions — adds the builds_id_range int8range column and the no-overlap GiST exclusion constraint to ci_partitions.
  • AddFkToCiBuildsPartitionOverrides (post-migrate) — adds the cascading partitioned foreign key from ci_builds_partition_overrides to p_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

Screenshots or screen recordings

N/A — database migrations only

Before After

How to set up and validate locally

  1. Run db:migrate — verifies ci_builds_partition_overrides is created with 3 hash partitions and ci_partitions.builds_id_range is added.
  2. Run db:migrate:post_deploy — verifies the FK is added.
  3. Inspect ci_partitions rows to confirm builds_id_range column exists.
  4. 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)

Edited by Marius Bobin

Merge request reports

Loading
Loading