From df284b3194bbd0d3513d7eaf6586fea93f9169b8 Mon Sep 17 00:00:00 2001
From: Adam Hegyi <ahegyi@gitlab.com>
Date: Thu, 18 Jul 2024 07:35:43 +0000
Subject: [PATCH] Merge branch
 '471727-jobartifactstate-checksummed_count-query-timing-out' into 'master'

Resolve "JobArtifactState.checksummed_count query timing out"

See merge request https://gitlab.com/gitlab-org/gitlab/-/merge_requests/158901

Merged-by: Adam Hegyi <ahegyi@gitlab.com>
Approved-by: Josianne Hyson <jhyson@gitlab.com>
Approved-by: Terri Chu <tchu@gitlab.com>
Approved-by: Adam Hegyi <ahegyi@gitlab.com>
Reviewed-by: Josianne Hyson <jhyson@gitlab.com>
Reviewed-by: Terri Chu <tchu@gitlab.com>
Co-authored-by: pkanellidis <pkanellidis@gitlab.com>
---
 ...n_job_artifact_state_verification_state.rb | 22 +++++++++++++++++++
 db/schema_migrations/20240714093324           |  1 +
 db/structure.sql                              |  2 ++
 .../helpers/database/duplicate_indexes.yml    |  3 +++
 4 files changed, 28 insertions(+)
 create mode 100644 db/post_migrate/20240714093324_add_index_on_job_artifact_state_verification_state.rb
 create mode 100644 db/schema_migrations/20240714093324

diff --git a/db/post_migrate/20240714093324_add_index_on_job_artifact_state_verification_state.rb b/db/post_migrate/20240714093324_add_index_on_job_artifact_state_verification_state.rb
new file mode 100644
index 0000000000000000..46570aecc9e9cc10
--- /dev/null
+++ b/db/post_migrate/20240714093324_add_index_on_job_artifact_state_verification_state.rb
@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+class AddIndexOnJobArtifactStateVerificationState < Gitlab::Database::Migration[2.2]
+  milestone '17.3'
+  disable_ddl_transaction!
+
+  TABLE_NAME = :ci_job_artifact_states
+  INDEX = 'index_on_job_artifact_id_partition_id_verification_state'
+  COLUMNS = [:verification_state, :job_artifact_id, :partition_id]
+
+  def up
+    add_concurrent_index(
+      TABLE_NAME,
+      COLUMNS,
+      name: INDEX
+    )
+  end
+
+  def down
+    remove_concurrent_index_by_name(TABLE_NAME, name: INDEX)
+  end
+end
diff --git a/db/schema_migrations/20240714093324 b/db/schema_migrations/20240714093324
new file mode 100644
index 0000000000000000..bdf0e9106c0fdd15
--- /dev/null
+++ b/db/schema_migrations/20240714093324
@@ -0,0 +1 @@
+4479d162a8955543b2573e89b4653607dea79f089ed611ee6b0e0f500d3c3874
\ No newline at end of file
diff --git a/db/structure.sql b/db/structure.sql
index eb01b75edd83f643..f11828f5fb963d74 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -26478,6 +26478,8 @@ CREATE UNIQUE INDEX index_on_instance_statistics_recorded_at_and_identifier ON a
 
 CREATE INDEX index_on_issue_assignment_events_issue_id_action_created_at_id ON issue_assignment_events USING btree (issue_id, action, created_at, id);
 
+CREATE INDEX index_on_job_artifact_id_partition_id_verification_state ON ci_job_artifact_states USING btree (verification_state, job_artifact_id, partition_id);
+
 CREATE INDEX index_on_label_links_all_columns ON label_links USING btree (target_id, label_id, target_type);
 
 CREATE INDEX index_on_merge_request_diffs_head_commit_sha ON merge_request_diffs USING btree (head_commit_sha);
diff --git a/spec/support/helpers/database/duplicate_indexes.yml b/spec/support/helpers/database/duplicate_indexes.yml
index 9475aa2897894211..7917f5fb72a54301 100644
--- a/spec/support/helpers/database/duplicate_indexes.yml
+++ b/spec/support/helpers/database/duplicate_indexes.yml
@@ -20,6 +20,9 @@ ci_stages:
     - index_ci_stages_on_pipeline_id
   index_ci_stages_on_pipeline_id_name_partition_id_unique:
     - index_ci_stages_on_pipeline_id
+ci_job_artifact_states:
+  index_on_job_artifact_id_partition_id_verification_state:
+    - index_job_artifact_states_on_verification_state
 dast_site_tokens:
   index_dast_site_token_on_project_id_and_url:
     - index_dast_site_tokens_on_project_id
-- 
GitLab