Convert ci_builds.id to bigint - Step 1: Add new columns and sync data
The ci_builds
table is one of the largest tables in GitLab's database that still uses an integer (int4
) Primary Key. It has a high Primary Key Overflow risk as can be seen on its tracking issue.
The first step to address the problem is to create a new column of type bigint
, load all the data by using background jobs from the current id
column to it and keep them in sync with a trigger.
In addition, the ci_builds
table references (has a Foreign Key towards) the ci_stages
table, which is also one of the tables with a Primary Key Overflow risk and we'll have to convert its id
column to bigint
at some point, so it would be optimal to do so in one pass while converting its Primary Key.
The overall process for the ci_builds
table will be as follows:
- Create new column
ci_builds.new_id
for the Primary Key, withbigint NOT NULL DEFAULT 0
- Create new columns
ci_builds.new_stage_id
for the Foreign Key that referencesci_stages
, withbigint NOT NULL DEFAULT 0
- Install sync triggers for both columns to keep them updated while new records are inserted or existing ones are updated or deleted.
- Start background jobs that will batch through the whole table and copy the
id
andstage_id
values to the new columns.
We'll follow with a cleanup migration in the next milestone than the one that the aforementioned migrations are deployed, which will add the necessary indexes, swap the PK (and its sequence) and the FKs and finally drop the old columns.
Finally, the ci_builds
table is at the center of the CI tables design, with 17 tables referencing it:
- TABLE
ci_build_trace_sections
- FOREIGN KEYbuild_id
- TABLE
ci_builds_metadata
- FOREIGN KEYbuild_id
- (Covered in Address the PK Overflow risk for ci_builds_metadata) - TABLE
ci_build_trace_chunks
- FOREIGN KEYbuild_id
- TABLE
ci_build_needs
- FOREIGN KEYbuild_id
!59467 (merged) - TABLE
ci_builds_runner_session
- FOREIGN KEYbuild_id
- TABLE
ci_job_artifacts
- FOREIGN KEYjob_id
- (Covered in Address the PK Overflow risk for ci_job_artifacts) - TABLE
ci_sources_pipelines
- FOREIGN KEYsource_job_id
-
TABLE(already bigint)ci_job_variables
- FOREIGN KEYjob_id
-
TABLE(already bigint)ci_build_pending_states
- FOREIGN KEYbuild_id
-
TABLE(already bigint)ci_build_report_results
- FOREIGN KEYbuild_id
-
TABLE(already bigint)ci_resources
- FOREIGN KEYbuild_id
-
TABLE(already bigint)ci_test_case_failures
- FOREIGN KEYbuild_id
-
TABLE(already bigint)ci_unit_test_failures
- FOREIGN KEYbuild_id
-
TABLE(already bigint)pages_deployments
- FOREIGN KEYci_build_id
-
TABLE(already bigint)requirements_management_test_reports
- FOREIGN KEYbuild_id
-
TABLE(already bigint)security_scans
- FOREIGN KEYbuild_id
-
TABLE(already bigint)terraform_state_versions
- FOREIGN KEYci_build_id
So, we have to also follow the same approach for the 15 additional tables that reference it and are not covered by other tables' Primary Key conversion migrations.
Related Tracking Issue: #215017 (closed)