Commit 93a80b7f authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Fix issuable search optimization in PG12

Uses a materialized CTE so that our optimization fence actually works.

Recursive CTEs are materialized by default in PG12 but we're not using
this recursively here so we use Gitlab::SQL::CTE which handles adding of
the MATERIALIZED keyword when needed.

Changelog: fixed
MR: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/61880
parent 45cf5194
......@@ -334,8 +334,7 @@ class IssuableFinder
return items if items.is_a?(ActiveRecord::NullRelation)
if use_cte_for_search?
cte = Gitlab::SQL::RecursiveCTE.new(klass.table_name)
cte << items
cte = Gitlab::SQL::CTE.new(klass.table_name, items)
items = klass.with(cte.to_arel).from(klass.table_name)
end
......
---
title: Fix issuable search optimization in PG12
merge_request: 61880
author:
type: fixed
......@@ -1178,6 +1178,7 @@ RSpec.describe IssuesFinder do
it 'returns true' do
expect(finder.use_cte_for_search?).to be_truthy
expect(finder.execute.to_sql).to match(/^WITH "issues" AS #{Gitlab::Database::AsWithMaterialized.materialized_if_supported}/)
end
end
......@@ -1186,6 +1187,7 @@ RSpec.describe IssuesFinder do
it 'returns true' do
expect(finder.use_cte_for_search?).to be_truthy
expect(finder.execute.to_sql).to match(/^WITH "issues" AS #{Gitlab::Database::AsWithMaterialized.materialized_if_supported}/)
end
end
end
......
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