diff --git a/db/post_migrate/20240129005943_sync_index_for_p_ci_builds_part1.rb b/db/post_migrate/20240129005943_sync_index_for_p_ci_builds_part1.rb
new file mode 100644
index 0000000000000000000000000000000000000000..e56418ab7a1dbd59290ab13564b18b3ba0cc7229
--- /dev/null
+++ b/db/post_migrate/20240129005943_sync_index_for_p_ci_builds_part1.rb
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+
+class SyncIndexForPCiBuildsPart1 < Gitlab::Database::Migration[2.2]
+  include Gitlab::Database::PartitioningMigrationHelpers
+
+  milestone '16.9'
+
+  TABLE_NAME = :p_ci_builds
+  INDEXES = [
+    ['p_ci_builds_auto_canceled_by_id_bigint_idx', [:auto_canceled_by_id_convert_to_bigint],
+      { where: "auto_canceled_by_id_convert_to_bigint IS NOT NULL" }],
+    ['p_ci_builds_commit_id_bigint_status_type_idx', [:commit_id_convert_to_bigint, :status, :type], {}],
+    ['p_ci_builds_commit_id_bigint_type_name_ref_idx', [:commit_id_convert_to_bigint, :type, :name, :ref], {}]
+  ]
+
+  disable_ddl_transaction!
+
+  def up
+    INDEXES.each do |index_name, columns, options|
+      add_concurrent_partitioned_index(TABLE_NAME, columns, name: index_name, **options)
+    end
+  end
+
+  def down
+    INDEXES.each do |index_name, _columns, _options|
+      remove_concurrent_partitioned_index_by_name(TABLE_NAME, index_name)
+    end
+  end
+end
diff --git a/db/post_migrate/20240129032459_add_fk_for_auto_canceled_by_id_bigint_between_p_ci_builds_and_ci_pipelines.rb b/db/post_migrate/20240129032459_add_fk_for_auto_canceled_by_id_bigint_between_p_ci_builds_and_ci_pipelines.rb
new file mode 100644
index 0000000000000000000000000000000000000000..3ba7c5c4e8a8cc93b43d50ca4c09e7c838cc0a9c
--- /dev/null
+++ b/db/post_migrate/20240129032459_add_fk_for_auto_canceled_by_id_bigint_between_p_ci_builds_and_ci_pipelines.rb
@@ -0,0 +1,56 @@
+# frozen_string_literal: true
+
+class AddFkForAutoCanceledByIdBigintBetweenPCiBuildsAndCiPipelines < Gitlab::Database::Migration[2.2]
+  include Gitlab::Database::PartitioningMigrationHelpers
+
+  milestone '16.9'
+  disable_ddl_transaction!
+
+  SOURCE_TABLE_NAME = :p_ci_builds
+  TARGET_TABLE_NAME = :ci_pipelines
+  COLUMN = :auto_canceled_by_id_convert_to_bigint
+  TARGET_COLUMN = :id
+  FK_NAME = :fk_dd3c83bdee
+
+  def up
+    add_concurrent_partitioned_foreign_key(
+      SOURCE_TABLE_NAME, TARGET_TABLE_NAME,
+      column: [COLUMN],
+      target_column: [TARGET_COLUMN],
+      validate: false,
+      reverse_lock_order: true,
+      on_delete: :nullify,
+      name: FK_NAME
+    )
+
+    prepare_partitioned_async_foreign_key_validation(
+      SOURCE_TABLE_NAME, [COLUMN],
+      name: FK_NAME
+    )
+  end
+
+  def down
+    unprepare_partitioned_async_foreign_key_validation(
+      SOURCE_TABLE_NAME, [COLUMN],
+      name: FK_NAME
+    )
+
+    Gitlab::Database::PostgresPartitionedTable.each_partition(SOURCE_TABLE_NAME) do |partition|
+      with_lock_retries do
+        remove_foreign_key_if_exists(
+          partition.identifier, TARGET_TABLE_NAME,
+          name: FK_NAME,
+          reverse_lock_order: true
+        )
+      end
+    end
+
+    with_lock_retries do
+      remove_foreign_key_if_exists(
+        SOURCE_TABLE_NAME, TARGET_TABLE_NAME,
+        name: FK_NAME,
+        reverse_lock_order: true
+      )
+    end
+  end
+end
diff --git a/db/schema_migrations/20240129005943 b/db/schema_migrations/20240129005943
new file mode 100644
index 0000000000000000000000000000000000000000..537a03af366d454c3b0d81b376391515ac64caf8
--- /dev/null
+++ b/db/schema_migrations/20240129005943
@@ -0,0 +1 @@
+81c0af94a19346f9f01c4191f37a579e4825a0d06efb42c714e6b835a114544a
\ No newline at end of file
diff --git a/db/schema_migrations/20240129032459 b/db/schema_migrations/20240129032459
new file mode 100644
index 0000000000000000000000000000000000000000..4f242323f6b9be1d6ce8fbb3491ce2833e36463c
--- /dev/null
+++ b/db/schema_migrations/20240129032459
@@ -0,0 +1 @@
+99c24a58185c81b7b4d2c0fb0b0fdfa3505035a9de5474f71b9a482c8c93f537
\ No newline at end of file
diff --git a/db/structure.sql b/db/structure.sql
index 2a08c3dc4e579dca9e0fcba4e30f69fbdcec61db..a0db942fdf8b49f4e53c3bcf34b0fd33600dc59e 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -32509,6 +32509,10 @@ CREATE INDEX idx_vulnerability_reads_project_id_scanner_id_vulnerability_id ON v
 
 CREATE UNIQUE INDEX idx_work_item_types_on_namespace_id_and_name_null_namespace ON work_item_types USING btree (btrim(lower(name)), ((namespace_id IS NULL))) WHERE (namespace_id IS NULL);
 
+CREATE INDEX p_ci_builds_commit_id_bigint_status_type_idx ON ONLY p_ci_builds USING btree (commit_id_convert_to_bigint, status, type);
+
+CREATE INDEX index_8c07a79c70 ON ci_builds USING btree (commit_id_convert_to_bigint, status, type);
+
 CREATE INDEX index_abuse_events_on_abuse_report_id ON abuse_events USING btree (abuse_report_id);
 
 CREATE INDEX index_abuse_events_on_category_and_source ON abuse_events USING btree (category, source);
@@ -33715,6 +33719,10 @@ CREATE UNIQUE INDEX index_external_audit_event_destinations_on_namespace_id ON a
 
 CREATE UNIQUE INDEX index_external_pull_requests_on_project_and_branches ON external_pull_requests USING btree (project_id, source_branch, target_branch);
 
+CREATE INDEX p_ci_builds_commit_id_bigint_type_name_ref_idx ON ONLY p_ci_builds USING btree (commit_id_convert_to_bigint, type, name, ref);
+
+CREATE INDEX index_feafb4d370 ON ci_builds USING btree (commit_id_convert_to_bigint, type, name, ref);
+
 CREATE UNIQUE INDEX index_feature_flag_scopes_on_flag_id_and_environment_scope ON operations_feature_flag_scopes USING btree (feature_flag_id, environment_scope);
 
 CREATE UNIQUE INDEX index_feature_flags_clients_on_project_id_and_token_encrypted ON operations_feature_flags_clients USING btree (project_id, token_encrypted);
@@ -33723,6 +33731,10 @@ CREATE UNIQUE INDEX index_feature_gates_on_feature_key_and_key_and_value ON feat
 
 CREATE UNIQUE INDEX index_features_on_key ON features USING btree (key);
 
+CREATE INDEX p_ci_builds_auto_canceled_by_id_bigint_idx ON ONLY p_ci_builds USING btree (auto_canceled_by_id_convert_to_bigint) WHERE (auto_canceled_by_id_convert_to_bigint IS NOT NULL);
+
+CREATE INDEX index_ffe1233676 ON ci_builds USING btree (auto_canceled_by_id_convert_to_bigint) WHERE (auto_canceled_by_id_convert_to_bigint IS NOT NULL);
+
 CREATE INDEX index_for_security_scans_scan_type ON security_scans USING btree (scan_type, project_id, pipeline_id) WHERE (status = 1);
 
 CREATE INDEX index_for_status_per_branch_per_project ON merge_trains USING btree (target_project_id, target_branch, status);
@@ -38005,6 +38017,8 @@ ALTER INDEX p_ci_builds_pkey ATTACH PARTITION ci_builds_pkey;
 
 ALTER INDEX p_ci_pipeline_variables_pkey ATTACH PARTITION ci_pipeline_variables_pkey;
 
+ALTER INDEX p_ci_builds_commit_id_bigint_status_type_idx ATTACH PARTITION index_8c07a79c70;
+
 ALTER INDEX p_ci_builds_metadata_build_id_idx ATTACH PARTITION index_ci_builds_metadata_on_build_id_and_has_exposed_artifacts;
 
 ALTER INDEX p_ci_builds_metadata_build_id_id_idx ATTACH PARTITION index_ci_builds_metadata_on_build_id_and_id_and_interruptible;
@@ -38049,6 +38063,10 @@ ALTER INDEX p_ci_builds_project_id_status_idx ATTACH PARTITION index_ci_builds_p
 
 ALTER INDEX p_ci_builds_runner_id_idx ATTACH PARTITION index_ci_builds_runner_id_running;
 
+ALTER INDEX p_ci_builds_commit_id_bigint_type_name_ref_idx ATTACH PARTITION index_feafb4d370;
+
+ALTER INDEX p_ci_builds_auto_canceled_by_id_bigint_idx ATTACH PARTITION index_ffe1233676;
+
 ALTER INDEX p_ci_builds_user_id_name_idx ATTACH PARTITION index_partial_ci_builds_on_user_id_name_parser_features;
 
 ALTER INDEX p_ci_pipeline_variables_pipeline_id_key_partition_id_idx ATTACH PARTITION index_pipeline_variables_on_pipeline_id_key_partition_id_unique;
@@ -39167,6 +39185,9 @@ ALTER TABLE ONLY workspaces
 ALTER TABLE ONLY epics
     ADD CONSTRAINT fk_dccd3f98fc FOREIGN KEY (assignee_id) REFERENCES users(id) ON DELETE SET NULL;
 
+ALTER TABLE ONLY ci_builds
+    ADD CONSTRAINT fk_dd3c83bdee FOREIGN KEY (auto_canceled_by_id_convert_to_bigint) REFERENCES ci_pipelines(id) ON DELETE SET NULL NOT VALID;
+
 ALTER TABLE ONLY protected_branches
     ADD CONSTRAINT fk_de9216e774 FOREIGN KEY (namespace_id) REFERENCES namespaces(id) ON DELETE CASCADE;