Add partial index on locked merge requests
In #220771 (closed), the /api/v4/internal/allowed
endpoint would timeout after 60 seconds due to this query in MatchingMergeRequest#match?
(https://gitlab.com/gitlab-org/gitlab/blob/e166e5747c6e8130f1227b427113f1231d9f46f1/lib/gitlab/checks/matching_merge_request.rb#L14-17):
SELECT "merge_requests".* FROM "merge_requests" WHERE "merge_requests"."target_project_id" = 7764 AND ("merge_requests"."state_id" IN (4)) AND "merge_requests"."in_progress_merge_commit_sha" = 'db21cf25663ce1ee2a548ce49db07e8882ed86ff' AND "merge_requests"."target_branch" = 'master' LIMIT 11;
Before
Limit (cost=0.56..69692.37 rows=1 width=726) (actual time=69976.787..69976.787 rows=0 loops=1)
Buffers: shared hit=2616 read=51601 dirtied=2358
I/O Timings: read=69297.775
-> Index Scan using index_merge_requests_on_target_project_id_and_iid on public.merge_requests (cost=0.56..69692.37 rows=1 width=726) (actual time=69976.784..69976.784 rows=0 loops=1)
Index Cond: (merge_requests.target_project_id = 7764)
Filter: ((merge_requests.state_id = 4) AND ((merge_requests.in_progress_merge_commit_sha)::text = 'db21cf25663ce1ee2a548ce49db07e8882ed86ff'::text) AND ((merge_requests.target_branch)::text = 'master'::text))
Rows Removed by Filter: 52239
Buffers: shared hit=2616 read=51601 dirtied=2358
I/O Timings: read=69297.775
Time: 1.166 min
- planning: 0.343 ms
- execution: 1.166 min
- I/O read: 1.155 min
- I/O write: 0.000 ms
Shared buffers:
- hits: 2616 (~20.40 MiB) from the buffer pool
- reads: 51601 (~403.10 MiB) from the OS file cache, including disk I/O
- dirtied: 2358 (~18.40 MiB)
- writes: 0
After
Limit (cost=0.28..6.73 rows=1 width=726) (actual time=0.037..0.037 rows=0 loops=1)
Buffers: shared hit=5
-> Index Scan using merge_request_test_idx on public.merge_requests (cost=0.28..6.73 rows=1 width=726) (actual time=0.035..0.035 rows=0 loops=1)
Index Cond: (merge_requests.target_project_id = 7764)
Filter: (((merge_requests.in_progress_merge_commit_sha)::text = 'db21cf25663ce1ee2a548ce49db07e8882ed86ff'::text) AND ((merge_requests.target_branch)::text = 'master'::text))
Rows Removed by Filter: 3
Buffers: shared hit=5
Time: 0.528 ms
- planning: 0.431 ms
- execution: 0.097 ms
- I/O read: 0.000 ms
- I/O write: 0.000 ms
Shared buffers:
- hits: 5 (~40.00 KiB) from the buffer pool
- reads: 0 from the OS file cache, including disk I/O
- dirtied: 0
- writes: 0
Migration timings
Duration: 12.505 min
Edited by Stan Hu