diff --git a/app/models/ci/build_metadata.rb b/app/models/ci/build_metadata.rb
index 3bdf2f90acbeaf47ff7f7c9ca037b6c5cd7cca76..98d829e92539cf79e1f24f2590a1689a14d78b15 100644
--- a/app/models/ci/build_metadata.rb
+++ b/app/models/ci/build_metadata.rb
@@ -11,6 +11,7 @@ class BuildMetadata < Ci::ApplicationRecord
     include Gitlab::Utils::StrongMemoize
 
     self.table_name = 'ci_builds_metadata'
+    self.primary_key = 'id'
 
     belongs_to :build, class_name: 'CommitStatus'
     belongs_to :project
diff --git a/db/post_migrate/20220920081631_prepare_ci_builds_metadata_for_partitioning_primary_key.rb b/db/post_migrate/20220920081631_prepare_ci_builds_metadata_for_partitioning_primary_key.rb
new file mode 100644
index 0000000000000000000000000000000000000000..907271786242df960720d427b3acc5dddec49113
--- /dev/null
+++ b/db/post_migrate/20220920081631_prepare_ci_builds_metadata_for_partitioning_primary_key.rb
@@ -0,0 +1,36 @@
+# frozen_string_literal: true
+
+class PrepareCiBuildsMetadataForPartitioningPrimaryKey < Gitlab::Database::Migration[2.0]
+  disable_ddl_transaction!
+
+  TABLE_NAME = 'ci_builds_metadata'
+  PRIMARY_KEY = 'ci_builds_metadata_pkey'
+  NEW_INDEX_NAME = 'index_ci_builds_metadata_on_id_partition_id_unique'
+  OLD_INDEX_NAME = 'index_ci_builds_metadata_on_id_unique'
+
+  def up
+    with_lock_retries(raise_on_exhaustion: true) do
+      execute("ALTER TABLE #{TABLE_NAME} DROP CONSTRAINT #{PRIMARY_KEY} CASCADE")
+
+      rename_index(TABLE_NAME, NEW_INDEX_NAME, PRIMARY_KEY)
+
+      execute("ALTER TABLE #{TABLE_NAME} ADD CONSTRAINT #{PRIMARY_KEY} " \
+        "PRIMARY KEY USING INDEX #{PRIMARY_KEY}")
+    end
+  end
+
+  # rolling back this migration is time consuming with the creation of these two indexes
+  def down
+    add_concurrent_index(TABLE_NAME, :id, unique: true, name: OLD_INDEX_NAME)
+    add_concurrent_index(TABLE_NAME, [:id, :partition_id], unique: true, name: NEW_INDEX_NAME)
+
+    with_lock_retries(raise_on_exhaustion: true) do
+      execute("ALTER TABLE #{TABLE_NAME} DROP CONSTRAINT #{PRIMARY_KEY} CASCADE")
+
+      rename_index(TABLE_NAME, OLD_INDEX_NAME, PRIMARY_KEY)
+
+      execute("ALTER TABLE #{TABLE_NAME} ADD CONSTRAINT #{PRIMARY_KEY} " \
+        "PRIMARY KEY USING INDEX #{PRIMARY_KEY}")
+    end
+  end
+end
diff --git a/db/schema_migrations/20220920081631 b/db/schema_migrations/20220920081631
new file mode 100644
index 0000000000000000000000000000000000000000..071ef93fc7a2aadd69f3a1722cbebccaff20fa7b
--- /dev/null
+++ b/db/schema_migrations/20220920081631
@@ -0,0 +1 @@
+081480492cbe6e631f0357b181a883a2bc7f34566f23f119c0ba4df59ee363d6
\ No newline at end of file
diff --git a/db/structure.sql b/db/structure.sql
index 0f6ff043d6e6da927305214e2e1854c58c5126d8..228d364bd696223ee1a41d5e0d7c72879c256489 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -25045,7 +25045,7 @@ ALTER TABLE ONLY ci_build_trace_metadata
     ADD CONSTRAINT ci_build_trace_metadata_pkey PRIMARY KEY (build_id);
 
 ALTER TABLE ONLY ci_builds_metadata
-    ADD CONSTRAINT ci_builds_metadata_pkey PRIMARY KEY (id);
+    ADD CONSTRAINT ci_builds_metadata_pkey PRIMARY KEY (id, partition_id);
 
 ALTER TABLE ONLY ci_builds
     ADD CONSTRAINT ci_builds_pkey PRIMARY KEY (id);