Make group runner CI builds use distinct CTE optimzation [RUN ALL RSPEC] [RUN AS-IF-FOSS]
What does this MR do?
Related issue: gitlab-com/gl-infra/production#4024 (closed)
Make group runner CI builds use distinct CTE optimzation. This uses the work in !56509 (merged) to ensure the query planner chooses the nested join vs. the hashed join.
Before:
SELECT "ci_builds"."id" FROM "ci_builds" WHERE "ci_builds"."type" = 'Ci::Build' AND ("ci_builds"."status" IN ('pending')) AND "ci_builds"."runner_id" IS NULL AND "ci_builds"."project_id" IN (SELECT "projects"."id" FROM "projects" INNER JOIN "project_ci_cd_settings" ON "project_ci_cd_settings"."project_id" = "projects"."id" LEFT JOIN project_features ON projects.id = project_features.project_id WHERE "projects"."namespace_id" IN (WITH RECURSIVE "base_and_descendants" AS ((SELECT "namespaces".* FROM "namespaces" INNER JOIN "ci_runner_namespaces" ON "ci_runner_namespaces"."namespace_id" = "namespaces"."id" WHERE "namespaces"."type" = 'Group' AND "ci_runner_namespaces"."runner_id" = 3)
UNION
(SELECT "namespaces".* FROM "namespaces", "base_and_descendants" WHERE "namespaces"."type" = 'Group' AND "namespaces"."parent_id" = "base_and_descendants"."id")) SELECT "namespaces"."id" FROM "base_and_descendants" AS "namespaces") AND "project_ci_cd_settings"."group_runners_enabled" = TRUE AND ("project_features"."builds_access_level" > 0 OR "project_features"."builds_access_level" IS NULL) AND "projects"."pending_delete" = FALSE) AND (NOT EXISTS (SELECT 1 FROM "taggings" WHERE "taggings"."taggable_type" = 'CommitStatus' AND "taggings"."context" = 'tags' AND (taggable_id = ci_builds.id) AND 1=1)) ORDER BY id ASC /*application:test*/
After:
SELECT "ci_builds"."id" FROM "ci_builds" WHERE "ci_builds"."type" = 'Ci::Build' AND ("ci_builds"."status" IN ('pending')) AND "ci_builds"."runner_id" IS NULL AND "ci_builds"."project_id" IN (SELECT "projects"."id" FROM "projects" INNER JOIN "project_ci_cd_settings" ON "project_ci_cd_settings"."project_id" = "projects"."id" LEFT JOIN project_features ON projects.id = project_features.project_id WHERE "projects"."namespace_id" IN (SELECT "namespaces"."id" FROM (SELECT "namespaces".* FROM "namespaces" INNER JOIN (WITH RECURSIVE "base_and_descendants" AS ((SELECT 1 AS depth, ARRAY[namespaces.id] AS tree_path, false AS tree_cycle, "namespaces".* FROM "namespaces" INNER JOIN "ci_runner_namespaces" ON "ci_runner_namespaces"."namespace_id" = "namespaces"."id" WHERE "namespaces"."type" = 'Group' AND "ci_runner_namespaces"."runner_id" = 3)
UNION
(SELECT ("base_and_descendants"."depth" + 1), tree_path || "namespaces".id, "namespaces".id = ANY(tree_path), "namespaces".* FROM "namespaces", "base_and_descendants" WHERE "namespaces"."type" = 'Group' AND "namespaces"."parent_id" = "base_and_descendants"."id" AND "base_and_descendants"."tree_cycle" = FALSE)) SELECT DISTINCT "id", "depth" FROM "base_and_descendants" AS "namespaces") namespaces_join_table on namespaces_join_table.id = namespaces.id WHERE "namespaces"."type" = 'Group' ORDER BY "namespaces_join_table"."depth" ASC) AS "namespaces" WHERE "namespaces"."type" = 'Group') AND "project_ci_cd_settings"."group_runners_enabled" = TRUE AND ("project_features"."builds_access_level" > 0 OR "project_features"."builds_access_level" IS NULL) AND "projects"."pending_delete" = FALSE) AND (NOT EXISTS (SELECT 1 FROM "taggings" WHERE "taggings"."taggable_type" = 'CommitStatus' AND "taggings"."context" = 'tags' AND (taggable_id = ci_builds.id) AND 1=1)) ORDER BY id ASC /*application:test*/
Screenshots (strongly suggested)
Does this MR meet the acceptance criteria?
Conformity
-
📋 Does this MR need a changelog?-
I have included a changelog entry. -
I have not included a changelog entry because it's behind a feature flag .
-
-
Documentation (if required) -
Code review guidelines -
Merge request performance guidelines -
Style guides -
Database guides -
Separation of EE specific content
Availability and Testing
-
Review and add/update tests for this feature/bug. Consider all test levels. See the Test Planning Process. -
Tested in all supported browsers -
Informed Infrastructure department of a default or new setting change, if applicable per definition of done
Security
If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:
-
Label as security and @ mention @gitlab-com/gl-security/appsec
-
The MR includes necessary changes to maintain consistency between UI, API, email, or other methods -
Security reports checked/validated by a reviewer from the AppSec team
Edited by Grzegorz Bizon