Skip to content

Make group runner CI builds use distinct CTE optimzation [RUN ALL RSPEC] [RUN AS-IF-FOSS]

Thong Kuah requested to merge use_distinct_in_register_job_object_hierarchy into master

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*/

/cc @ahegyi @stanhu

Screenshots (strongly suggested)

Does this MR meet the acceptance criteria?

Conformity

Availability and Testing

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

Merge request reports