From c29adfe7b51f4b6324b72a5abe678fcb8a459a03 Mon Sep 17 00:00:00 2001
From: drew cimino <dcimino@gitlab.com>
Date: Thu, 24 Mar 2022 17:37:54 -0400
Subject: [PATCH] Schedule async index build for ci_job_artifacts

Our purpose-built index for removal of expired records from
ci_job_artifacts does not exclude artifacts where expire_at is null.
This means job traces, which get unlocked but have no expiration, are
polluting an index that would otherwise be a clean ready-for-removal
queue and much smaller.

Changelog: performance
---
 ...or_ci_job_artifacts_unlocked_with_expire_at.rb | 15 +++++++++++++++
 db/schema_migrations/20220325000000               |  1 +
 2 files changed, 16 insertions(+)
 create mode 100644 db/post_migrate/20220325000000_prepare_index_for_ci_job_artifacts_unlocked_with_expire_at.rb
 create mode 100644 db/schema_migrations/20220325000000

diff --git a/db/post_migrate/20220325000000_prepare_index_for_ci_job_artifacts_unlocked_with_expire_at.rb b/db/post_migrate/20220325000000_prepare_index_for_ci_job_artifacts_unlocked_with_expire_at.rb
new file mode 100644
index 0000000000000..fa43f3b7f5997
--- /dev/null
+++ b/db/post_migrate/20220325000000_prepare_index_for_ci_job_artifacts_unlocked_with_expire_at.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+class PrepareIndexForCiJobArtifactsUnlockedWithExpireAt < Gitlab::Database::Migration[1.0]
+  TABLE_NAME = 'ci_job_artifacts'
+  INDEX_NAME = 'index_ci_job_artifacts_on_expire_at_for_removal'
+  CONDITIONS = 'locked = 0 AND expire_at IS NOT NULL'
+
+  def up
+    prepare_async_index TABLE_NAME, [:expire_at], where: CONDITIONS, name: INDEX_NAME
+  end
+
+  def down
+    unprepare_async_index_by_name TABLE_NAME, INDEX_NAME
+  end
+end
diff --git a/db/schema_migrations/20220325000000 b/db/schema_migrations/20220325000000
new file mode 100644
index 0000000000000..97f93ef57c75b
--- /dev/null
+++ b/db/schema_migrations/20220325000000
@@ -0,0 +1 @@
+fd738cf8a5642f96dc67a294dcf913d797b580a19f35d5ef76573c63bf2dd920
\ No newline at end of file
-- 
GitLab