Commit 07b60225 authored by Stan Hu's avatar Stan Hu

Add partial index on locked merge requests

In https://gitlab.com/gitlab-org/gitlab/-/issues/220771, the
`/api/v4/internal/allowed` endpoint would timeout after 60 seconds due
to this query in `MatchingMergeRequest#match?`.

This commit adds a partial index for locked merge requests to improve
the performance of this query.

Relates to https://gitlab.com/gitlab-org/gitlab/-/issues/220771
parent 994212b5
---
title: Add partial index on locked merge requets
merge_request: 34127
author:
type: performance
# frozen_string_literal: true
class AddPartialIndexOnLockedStateIdToMergeRequests < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
INDEX_NAME = "idx_merge_requests_on_target_project_id_and_locked_state"
LOCKED_STATE_ID = 4
disable_ddl_transaction!
def up
add_concurrent_index :merge_requests, :target_project_id, where: "(state_id = #{LOCKED_STATE_ID})", name: INDEX_NAME
end
def down
remove_concurrent_index_by_name :merge_requests, name: INDEX_NAME
end
end
......@@ -9119,6 +9119,8 @@ CREATE INDEX idx_merge_requests_on_state_id_and_merge_status ON public.merge_req
CREATE INDEX idx_merge_requests_on_target_project_id_and_iid_opened ON public.merge_requests USING btree (target_project_id, iid) WHERE (state_id = 1);
CREATE INDEX idx_merge_requests_on_target_project_id_and_locked_state ON public.merge_requests USING btree (target_project_id) WHERE (state_id = 4);
CREATE UNIQUE INDEX idx_metrics_users_starred_dashboard_on_user_project_dashboard ON public.metrics_users_starred_dashboards USING btree (user_id, project_id, dashboard_path);
CREATE INDEX idx_mr_cc_diff_files_on_mr_cc_id_and_sha ON public.merge_request_context_commit_diff_files USING btree (merge_request_context_commit_id, sha);
......@@ -13847,5 +13849,6 @@ COPY "schema_migrations" (version) FROM STDIN;
20200603073101
20200604143628
20200608075553
20200609002841
\.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment