Add trigger to create overflow records

What does this MR do and why?

This MR adds the database trigger that automatically captures spillover builds into the ci_builds_partition_overrides table on insert. It is part of the corrective action for INC-8367.

This MR has been split out from !228838 (merged). The full work is tracked across the following MRs:

MR Description
!228838 (merged) Add p_ci_builds_partition_overrides table and builds_id_range column on ci_partitions
!231142 (closed) (this MR) 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

AddTriggersForBuildsOverrides (post-migrate) — creates the insert_ci_builds_partition_overrides trigger function and installs AFTER INSERT triggers on all currently active list-partitions of p_ci_builds. On each insert into p_ci_builds, the trigger automatically writes (build_id, partition_id, project_id) into ci_builds_partition_overrides using ON CONFLICT DO NOTHING for idempotency.

The trigger is scoped to active partitions (those with status = 3 in ci_partitions) and uses with_lock_retries to safely acquire the necessary locks. The trigger function itself is also created by !229866 (closed)'s SyncService at partition switch time going forward — this migration ensures the function and triggers exist for the currently active partition immediately after deploy.

Why this approach

The trigger provides real-time capture of spillover builds as they are inserted, complementing the backfill in !231138 (closed) which handles historical data. Together they ensure the override table stays complete and up-to-date, enabling reliable partition_id resolution without cross-partition scans.

Changelog: other

References

Screenshots or screen recordings

N/A — database migrations only

Before After

How to set up and validate locally

  1. Ensure !228838 (merged) is merged first (provides the ci_builds_partition_overrides table).
  2. Run db:migrate:post_deploy — verifies the insert_ci_builds_partition_overrides trigger function is created and triggers are installed on active p_ci_builds partitions.
  3. Insert a row into an active p_ci_builds partition and confirm a corresponding row appears in ci_builds_partition_overrides.
  4. Run the spec suite for the migration spec.

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