Skip to content

Fix 500 error for /api/v4/groups?all_available=false&statistics=true API

Abdul Wadood requested to merge 378714-sum-by-partition into master

What does this MR do and why?

The GET /api/v4/groups?all_available=false&statistics=true API was throwing internal server error when logged in using an admin user because the SUM aggregate was used to calculate the sum and all the columns from the subquery did not appear in the GROUP BY clause which resulted in a semantically incorrect query.

Here we have fixed the issue by moving the calculation of the sums to a lateral join.

MR acceptance checklist

Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Query plans

Before

  1. https://gdk.test:3000/api/v4/groups?all_available=true&statistics=true

Plan: https://console.postgres.ai/gitlab/gitlab-production-main/sessions/28664/commands/89286

Raw Query
SELECT
    "namespaces".*,
    namespaces.*,
    COALESCE(SUM(ps.storage_size), 0) AS storage_size,
    COALESCE(SUM(ps.repository_size), 0) AS repository_size,
    COALESCE(SUM(ps.wiki_size), 0) AS wiki_size,
    COALESCE(SUM(ps.snippets_size), 0) AS snippets_size,
    COALESCE(SUM(ps.lfs_objects_size), 0) AS lfs_objects_size,
    COALESCE(SUM(ps.build_artifacts_size), 0) AS build_artifacts_size,
    COALESCE(SUM(ps.pipeline_artifacts_size), 0) AS pipeline_artifacts_size,
    COALESCE(SUM(ps.packages_size), 0) AS packages_size,
    COALESCE(SUM(ps.uploads_size), 0) AS uploads_size
FROM
    "namespaces"
    LEFT JOIN project_statistics ps ON ps.namespace_id = namespaces.id
WHERE
    "namespaces"."type" = 'Group'
GROUP BY
    "namespaces"."id"
ORDER BY
    "namespaces"."name" ASC,
    "namespaces"."id" ASC
LIMIT 20 OFFSET 0;
  1. https://gdk.test:3000/api/v4/groups?all_available=false&statistics=true

Plan: Generated query syntax is incorrect

Raw Query
SELECT
    "namespaces".*,
    namespaces.*,
    COALESCE(SUM(ps.storage_size), 0) AS storage_size,
    COALESCE(SUM(ps.repository_size), 0) AS repository_size,
    COALESCE(SUM(ps.wiki_size), 0) AS wiki_size,
    COALESCE(SUM(ps.snippets_size), 0) AS snippets_size,
    COALESCE(SUM(ps.lfs_objects_size), 0) AS lfs_objects_size,
    COALESCE(SUM(ps.build_artifacts_size), 0) AS build_artifacts_size,
    COALESCE(SUM(ps.pipeline_artifacts_size), 0) AS pipeline_artifacts_size,
    COALESCE(SUM(ps.packages_size), 0) AS packages_size,
    COALESCE(SUM(ps.uploads_size), 0) AS uploads_size
FROM (( WITH "descendants_base_cte" AS MATERIALIZED (
            SELECT
                "namespaces"."id",
                "namespaces"."traversal_ids"
            FROM
                "namespaces"
                INNER JOIN "members" ON "namespaces"."id" = "members"."source_id"
            WHERE
                "members"."type" = 'GroupMember'
                AND "members"."source_type" = 'Namespace'
                AND "namespaces"."type" = 'Group'
                AND "members"."user_id" = 75086
                AND "members"."requested_at" IS NULL
                AND (
                    access_level >= 10
)
),
            "superset" AS (
                SELECT
                    d1.traversal_ids
                FROM
                    descendants_base_cte d1
                WHERE
                    NOT EXISTS (
                        SELECT
                            1
                        FROM
                            descendants_base_cte d2
                        WHERE
                            d2.id = ANY (d1.traversal_ids)
                            AND d2.id <> d1.id))
                    SELECT DISTINCT
                        "namespaces"."id",
                        "namespaces"."name",
                        "namespaces"."path",
                        "namespaces"."owner_id",
                        "namespaces"."created_at",
                        "namespaces"."updated_at",
                        "namespaces"."type",
                        "namespaces"."description",
                        "namespaces"."avatar",
                        "namespaces"."membership_lock",
                        "namespaces"."share_with_group_lock",
                        "namespaces"."visibility_level",
                        "namespaces"."request_access_enabled",
                        "namespaces"."ldap_sync_status",
                        "namespaces"."ldap_sync_error",
                        "namespaces"."ldap_sync_last_update_at",
                        "namespaces"."ldap_sync_last_successful_update_at",
                        "namespaces"."ldap_sync_last_sync_at",
                        "namespaces"."description_html",
                        "namespaces"."lfs_enabled",
                        "namespaces"."parent_id",
                        "namespaces"."shared_runners_minutes_limit",
                        "namespaces"."repository_size_limit",
                        "namespaces"."require_two_factor_authentication",
                        "namespaces"."two_factor_grace_period",
                        "namespaces"."cached_markdown_version",
                        "namespaces"."project_creation_level",
                        "namespaces"."runners_token",
                        "namespaces"."file_template_project_id",
                        "namespaces"."saml_discovery_token",
                        "namespaces"."runners_token_encrypted",
                        "namespaces"."custom_project_templates_group_id",
                        "namespaces"."auto_devops_enabled",
                        "namespaces"."extra_shared_runners_minutes_limit",
                        "namespaces"."last_ci_minutes_notification_at",
                        "namespaces"."last_ci_minutes_usage_notification_level",
                        "namespaces"."subgroup_creation_level",
                        "namespaces"."max_pages_size",
                        "namespaces"."max_artifacts_size",
                        "namespaces"."mentions_disabled",
                        "namespaces"."default_branch_protection",
                        "namespaces"."max_personal_access_token_lifetime",
                        "namespaces"."push_rule_id",
                        "namespaces"."shared_runners_enabled",
                        "namespaces"."allow_descendants_override_disabled_shared_runners",
                        "namespaces"."traversal_ids",
                        "namespaces"."organization_id"
                    FROM
                        "superset",
                        "namespaces"
                    WHERE
                        "namespaces"."type" = 'Group'
                        AND next_traversal_ids_sibling ("superset"."traversal_ids") > "namespaces"."traversal_ids"
                        AND "superset"."traversal_ids" <= "namespaces"."traversal_ids")
                UNION ( WITH "base_ancestors_cte" AS MATERIALIZED (
                        SELECT
                            "namespaces"."traversal_ids"
                        FROM (
                            (
                                WITH "direct_groups" AS MATERIALIZED (
                                    SELECT
                                        "namespaces"."id",
                                        "namespaces"."name",
                                        "namespaces"."path",
                                        "namespaces"."owner_id",
                                        "namespaces"."created_at",
                                        "namespaces"."updated_at",
                                        "namespaces"."type",
                                        "namespaces"."description",
                                        "namespaces"."avatar",
                                        "namespaces"."membership_lock",
                                        "namespaces"."share_with_group_lock",
                                        "namespaces"."visibility_level",
                                        "namespaces"."request_access_enabled",
                                        "namespaces"."ldap_sync_status",
                                        "namespaces"."ldap_sync_error",
                                        "namespaces"."ldap_sync_last_update_at",
                                        "namespaces"."ldap_sync_last_successful_update_at",
                                        "namespaces"."ldap_sync_last_sync_at",
                                        "namespaces"."description_html",
                                        "namespaces"."lfs_enabled",
                                        "namespaces"."parent_id",
                                        "namespaces"."shared_runners_minutes_limit",
                                        "namespaces"."repository_size_limit",
                                        "namespaces"."require_two_factor_authentication",
                                        "namespaces"."two_factor_grace_period",
                                        "namespaces"."cached_markdown_version",
                                        "namespaces"."project_creation_level",
                                        "namespaces"."runners_token",
                                        "namespaces"."file_template_project_id",
                                        "namespaces"."saml_discovery_token",
                                        "namespaces"."runners_token_encrypted",
                                        "namespaces"."custom_project_templates_group_id",
                                        "namespaces"."auto_devops_enabled",
                                        "namespaces"."extra_shared_runners_minutes_limit",
                                        "namespaces"."last_ci_minutes_notification_at",
                                        "namespaces"."last_ci_minutes_usage_notification_level",
                                        "namespaces"."subgroup_creation_level",
                                        "namespaces"."max_pages_size",
                                        "namespaces"."max_artifacts_size",
                                        "namespaces"."mentions_disabled",
                                        "namespaces"."default_branch_protection",
                                        "namespaces"."max_personal_access_token_lifetime",
                                        "namespaces"."push_rule_id",
                                        "namespaces"."shared_runners_enabled",
                                        "namespaces"."allow_descendants_override_disabled_shared_runners",
                                        "namespaces"."traversal_ids",
                                        "namespaces"."organization_id"
                                    FROM
                                        "namespaces"
                                        INNER JOIN "members" ON "namespaces"."id" = "members"."source_id"
                                    WHERE
                                        "members"."type" = 'GroupMember'
                                        AND "members"."source_type" = 'Namespace'
                                        AND "namespaces"."type" = 'Group'
                                        AND "members"."user_id" = 75086
                                        AND "members"."requested_at" IS NULL
                                        AND (
                                            access_level >= 10
)
)
                                    SELECT
                                        "namespaces"."id",
                                        "namespaces"."name",
                                        "namespaces"."path",
                                        "namespaces"."owner_id",
                                        "namespaces"."created_at",
                                        "namespaces"."updated_at",
                                        "namespaces"."type",
                                        "namespaces"."description",
                                        "namespaces"."avatar",
                                        "namespaces"."membership_lock",
                                        "namespaces"."share_with_group_lock",
                                        "namespaces"."visibility_level",
                                        "namespaces"."request_access_enabled",
                                        "namespaces"."ldap_sync_status",
                                        "namespaces"."ldap_sync_error",
                                        "namespaces"."ldap_sync_last_update_at",
                                        "namespaces"."ldap_sync_last_successful_update_at",
                                        "namespaces"."ldap_sync_last_sync_at",
                                        "namespaces"."description_html",
                                        "namespaces"."lfs_enabled",
                                        "namespaces"."parent_id",
                                        "namespaces"."shared_runners_minutes_limit",
                                        "namespaces"."repository_size_limit",
                                        "namespaces"."require_two_factor_authentication",
                                        "namespaces"."two_factor_grace_period",
                                        "namespaces"."cached_markdown_version",
                                        "namespaces"."project_creation_level",
                                        "namespaces"."runners_token",
                                        "namespaces"."file_template_project_id",
                                        "namespaces"."saml_discovery_token",
                                        "namespaces"."runners_token_encrypted",
                                        "namespaces"."custom_project_templates_group_id",
                                        "namespaces"."auto_devops_enabled",
                                        "namespaces"."extra_shared_runners_minutes_limit",
                                        "namespaces"."last_ci_minutes_notification_at",
                                        "namespaces"."last_ci_minutes_usage_notification_level",
                                        "namespaces"."subgroup_creation_level",
                                        "namespaces"."max_pages_size",
                                        "namespaces"."max_artifacts_size",
                                        "namespaces"."mentions_disabled",
                                        "namespaces"."default_branch_protection",
                                        "namespaces"."max_personal_access_token_lifetime",
                                        "namespaces"."push_rule_id",
                                        "namespaces"."shared_runners_enabled",
                                        "namespaces"."allow_descendants_override_disabled_shared_runners",
                                        "namespaces"."traversal_ids",
                                        "namespaces"."organization_id"
                                    FROM (
                                        (
                                            SELECT
                                                "namespaces"."id",
                                                "namespaces"."name",
                                                "namespaces"."path",
                                                "namespaces"."owner_id",
                                                "namespaces"."created_at",
                                                "namespaces"."updated_at",
                                                "namespaces"."type",
                                                "namespaces"."description",
                                                "namespaces"."avatar",
                                                "namespaces"."membership_lock",
                                                "namespaces"."share_with_group_lock",
                                                "namespaces"."visibility_level",
                                                "namespaces"."request_access_enabled",
                                                "namespaces"."ldap_sync_status",
                                                "namespaces"."ldap_sync_error",
                                                "namespaces"."ldap_sync_last_update_at",
                                                "namespaces"."ldap_sync_last_successful_update_at",
                                                "namespaces"."ldap_sync_last_sync_at",
                                                "namespaces"."description_html",
                                                "namespaces"."lfs_enabled",
                                                "namespaces"."parent_id",
                                                "namespaces"."shared_runners_minutes_limit",
                                                "namespaces"."repository_size_limit",
                                                "namespaces"."require_two_factor_authentication",
                                                "namespaces"."two_factor_grace_period",
                                                "namespaces"."cached_markdown_version",
                                                "namespaces"."project_creation_level",
                                                "namespaces"."runners_token",
                                                "namespaces"."file_template_project_id",
                                                "namespaces"."saml_discovery_token",
                                                "namespaces"."runners_token_encrypted",
                                                "namespaces"."custom_project_templates_group_id",
                                                "namespaces"."auto_devops_enabled",
                                                "namespaces"."extra_shared_runners_minutes_limit",
                                                "namespaces"."last_ci_minutes_notification_at",
                                                "namespaces"."last_ci_minutes_usage_notification_level",
                                                "namespaces"."subgroup_creation_level",
                                                "namespaces"."max_pages_size",
                                                "namespaces"."max_artifacts_size",
                                                "namespaces"."mentions_disabled",
                                                "namespaces"."default_branch_protection",
                                                "namespaces"."max_personal_access_token_lifetime",
                                                "namespaces"."push_rule_id",
                                                "namespaces"."shared_runners_enabled",
                                                "namespaces"."allow_descendants_override_disabled_shared_runners",
                                                "namespaces"."traversal_ids",
                                                "namespaces"."organization_id"
                                            FROM
                                                "direct_groups" "namespaces"
                                            WHERE
                                                "namespaces"."type" = 'Group'
)
                                        UNION (
                                            SELECT
                                                "namespaces"."id",
                                                "namespaces"."name",
                                                "namespaces"."path",
                                                "namespaces"."owner_id",
                                                "namespaces"."created_at",
                                                "namespaces"."updated_at",
                                                "namespaces"."type",
                                                "namespaces"."description",
                                                "namespaces"."avatar",
                                                "namespaces"."membership_lock",
                                                "namespaces"."share_with_group_lock",
                                                "namespaces"."visibility_level",
                                                "namespaces"."request_access_enabled",
                                                "namespaces"."ldap_sync_status",
                                                "namespaces"."ldap_sync_error",
                                                "namespaces"."ldap_sync_last_update_at",
                                                "namespaces"."ldap_sync_last_successful_update_at",
                                                "namespaces"."ldap_sync_last_sync_at",
                                                "namespaces"."description_html",
                                                "namespaces"."lfs_enabled",
                                                "namespaces"."parent_id",
                                                "namespaces"."shared_runners_minutes_limit",
                                                "namespaces"."repository_size_limit",
                                                "namespaces"."require_two_factor_authentication",
                                                "namespaces"."two_factor_grace_period",
                                                "namespaces"."cached_markdown_version",
                                                "namespaces"."project_creation_level",
                                                "namespaces"."runners_token",
                                                "namespaces"."file_template_project_id",
                                                "namespaces"."saml_discovery_token",
                                                "namespaces"."runners_token_encrypted",
                                                "namespaces"."custom_project_templates_group_id",
                                                "namespaces"."auto_devops_enabled",
                                                "namespaces"."extra_shared_runners_minutes_limit",
                                                "namespaces"."last_ci_minutes_notification_at",
                                                "namespaces"."last_ci_minutes_usage_notification_level",
                                                "namespaces"."subgroup_creation_level",
                                                "namespaces"."max_pages_size",
                                                "namespaces"."max_artifacts_size",
                                                "namespaces"."mentions_disabled",
                                                "namespaces"."default_branch_protection",
                                                "namespaces"."max_personal_access_token_lifetime",
                                                "namespaces"."push_rule_id",
                                                "namespaces"."shared_runners_enabled",
                                                "namespaces"."allow_descendants_override_disabled_shared_runners",
                                                "namespaces"."traversal_ids",
                                                "namespaces"."organization_id"
                                            FROM
                                                "namespaces"
                                            WHERE
                                                "namespaces"."type" = 'Group'
                                                AND "namespaces"."id" IN (
                                                    SELECT
                                                        "projects"."namespace_id"
                                                    FROM
                                                        "projects"
                                                        INNER JOIN "project_authorizations" ON "projects"."id" = "project_authorizations"."project_id"
                                                    WHERE
                                                        "project_authorizations"."user_id" = 75086
)
)
                                            UNION (
                                                SELECT
                                                    "namespaces"."id",
                                                    "namespaces"."name",
                                                    "namespaces"."path",
                                                    "namespaces"."owner_id",
                                                    "namespaces"."created_at",
                                                    "namespaces"."updated_at",
                                                    "namespaces"."type",
                                                    "namespaces"."description",
                                                    "namespaces"."avatar",
                                                    "namespaces"."membership_lock",
                                                    "namespaces"."share_with_group_lock",
                                                    "namespaces"."visibility_level",
                                                    "namespaces"."request_access_enabled",
                                                    "namespaces"."ldap_sync_status",
                                                    "namespaces"."ldap_sync_error",
                                                    "namespaces"."ldap_sync_last_update_at",
                                                    "namespaces"."ldap_sync_last_successful_update_at",
                                                    "namespaces"."ldap_sync_last_sync_at",
                                                    "namespaces"."description_html",
                                                    "namespaces"."lfs_enabled",
                                                    "namespaces"."parent_id",
                                                    "namespaces"."shared_runners_minutes_limit",
                                                    "namespaces"."repository_size_limit",
                                                    "namespaces"."require_two_factor_authentication",
                                                    "namespaces"."two_factor_grace_period",
                                                    "namespaces"."cached_markdown_version",
                                                    "namespaces"."project_creation_level",
                                                    "namespaces"."runners_token",
                                                    "namespaces"."file_template_project_id",
                                                    "namespaces"."saml_discovery_token",
                                                    "namespaces"."runners_token_encrypted",
                                                    "namespaces"."custom_project_templates_group_id",
                                                    "namespaces"."auto_devops_enabled",
                                                    "namespaces"."extra_shared_runners_minutes_limit",
                                                    "namespaces"."last_ci_minutes_notification_at",
                                                    "namespaces"."last_ci_minutes_usage_notification_level",
                                                    "namespaces"."subgroup_creation_level",
                                                    "namespaces"."max_pages_size",
                                                    "namespaces"."max_artifacts_size",
                                                    "namespaces"."mentions_disabled",
                                                    "namespaces"."default_branch_protection",
                                                    "namespaces"."max_personal_access_token_lifetime",
                                                    "namespaces"."push_rule_id",
                                                    "namespaces"."shared_runners_enabled",
                                                    "namespaces"."allow_descendants_override_disabled_shared_runners",
                                                    "namespaces"."traversal_ids",
                                                    "namespaces"."organization_id"
                                                FROM
                                                    "namespaces"
                                                    INNER JOIN "group_group_links" ON "group_group_links"."shared_group_id" = "namespaces"."id"
                                                WHERE
                                                    "namespaces"."type" = 'Group'
                                                    AND "group_group_links"."shared_with_group_id" IN (
                                                        SELECT
                                                            "namespaces"."id"
                                                        FROM
                                                            "direct_groups" "namespaces"
                                                        WHERE
                                                            "namespaces"."type" = 'Group'
)
)
) namespaces
                                            WHERE
                                                "namespaces"."type" = 'Group'
)
) namespaces
                                    WHERE
                                        "namespaces"."type" = 'Group'
)
                                    SELECT
                                        "namespaces"."id",
                                        "namespaces"."name",
                                        "namespaces"."path",
                                        "namespaces"."owner_id",
                                        "namespaces"."created_at",
                                        "namespaces"."updated_at",
                                        "namespaces"."type",
                                        "namespaces"."description",
                                        "namespaces"."avatar",
                                        "namespaces"."membership_lock",
                                        "namespaces"."share_with_group_lock",
                                        "namespaces"."visibility_level",
                                        "namespaces"."request_access_enabled",
                                        "namespaces"."ldap_sync_status",
                                        "namespaces"."ldap_sync_error",
                                        "namespaces"."ldap_sync_last_update_at",
                                        "namespaces"."ldap_sync_last_successful_update_at",
                                        "namespaces"."ldap_sync_last_sync_at",
                                        "namespaces"."description_html",
                                        "namespaces"."lfs_enabled",
                                        "namespaces"."parent_id",
                                        "namespaces"."shared_runners_minutes_limit",
                                        "namespaces"."repository_size_limit",
                                        "namespaces"."require_two_factor_authentication",
                                        "namespaces"."two_factor_grace_period",
                                        "namespaces"."cached_markdown_version",
                                        "namespaces"."project_creation_level",
                                        "namespaces"."runners_token",
                                        "namespaces"."file_template_project_id",
                                        "namespaces"."saml_discovery_token",
                                        "namespaces"."runners_token_encrypted",
                                        "namespaces"."custom_project_templates_group_id",
                                        "namespaces"."auto_devops_enabled",
                                        "namespaces"."extra_shared_runners_minutes_limit",
                                        "namespaces"."last_ci_minutes_notification_at",
                                        "namespaces"."last_ci_minutes_usage_notification_level",
                                        "namespaces"."subgroup_creation_level",
                                        "namespaces"."max_pages_size",
                                        "namespaces"."max_artifacts_size",
                                        "namespaces"."mentions_disabled",
                                        "namespaces"."default_branch_protection",
                                        "namespaces"."max_personal_access_token_lifetime",
                                        "namespaces"."push_rule_id",
                                        "namespaces"."shared_runners_enabled",
                                        "namespaces"."allow_descendants_override_disabled_shared_runners",
                                        "namespaces"."traversal_ids",
                                        "namespaces"."organization_id"
                                    FROM
                                        "namespaces"
                                        INNER JOIN ( SELECT DISTINCT
                                                UNNEST("base_ancestors_cte"."traversal_ids")
                                            FROM
                                                base_ancestors_cte) AS ancestors (ancestor_id) ON namespaces.id = ancestors.ancestor_id
                                        WHERE
                                            "namespaces"."type" = 'Group')) namespaces
    LEFT JOIN project_statistics ps ON ps.namespace_id = namespaces.id
WHERE
    "namespaces"."type" = 'Group'
GROUP BY
    "namespaces"."id"
ORDER BY
    "namespaces"."name" ASC,
    "namespaces"."id" ASC
LIMIT 20 OFFSET 0;

After

  1. https://gdk.test:3000/api/v4/groups?all_available=true&statistics=true

Plan: https://console.postgres.ai/gitlab/gitlab-production-main/sessions/28664/commands/89364

Raw Query
EXPLAIN -- After
SELECT
    "namespaces".*,
    "statistics".*
FROM (
    SELECT
        "namespaces".*
    FROM
        "namespaces"
    WHERE
        "namespaces"."type" = 'Group'
    ORDER BY
        "namespaces"."name" ASC,
        "namespaces"."id" ASC) namespaces,
    LATERAL (
        SELECT
            COALESCE(SUM("project_statistics"."storage_size"), 0) AS storage_size,
            COALESCE(SUM("project_statistics"."repository_size"), 0) AS repository_size,
            COALESCE(SUM("project_statistics"."wiki_size"), 0) AS wiki_size,
            COALESCE(SUM("project_statistics"."snippets_size"), 0) AS snippets_size,
            COALESCE(SUM("project_statistics"."lfs_objects_size"), 0) AS lfs_objects_size,
            COALESCE(SUM("project_statistics"."build_artifacts_size"), 0) AS build_artifacts_size,
            COALESCE(SUM("project_statistics"."pipeline_artifacts_size"), 0) AS pipeline_artifacts_size,
            COALESCE(SUM("project_statistics"."packages_size"), 0) AS packages_size,
            COALESCE(SUM("project_statistics"."uploads_size"), 0) AS uploads_size
        FROM
            "project_statistics"
        WHERE
            "project_statistics"."namespace_id" = "namespaces"."id") statistics
WHERE
    "namespaces"."type" = 'Group'
ORDER BY
    "namespaces"."name" ASC,
    "namespaces"."id" ASC
LIMIT 20 OFFSET 0;
  1. https://gdk.test:3000/api/v4/groups?all_available=false&statistics=true

Plan: https://console.postgres.ai/gitlab/gitlab-production-main/sessions/28664/commands/89290

Raw Query
EXPLAIN -- After fix
SELECT
    "namespaces".*,
    "statistics".*
FROM (
    SELECT
        "namespaces".*
    FROM (( WITH "descendants_base_cte" AS MATERIALIZED (
                SELECT
                    "namespaces"."id",
                    "namespaces"."traversal_ids"
                FROM
                    "namespaces"
                    INNER JOIN "members" ON "namespaces"."id" = "members"."source_id"
                WHERE
                    "members"."type" = 'GroupMember'
                    AND "members"."source_type" = 'Namespace'
                    AND "namespaces"."type" = 'Group'
                    AND "members"."user_id" = 75086
                    AND "members"."requested_at" IS NULL
                    AND (
                        access_level >= 10
)
),
                "superset" AS (
                    SELECT
                        d1.traversal_ids
                    FROM
                        descendants_base_cte d1
                    WHERE
                        NOT EXISTS (
                            SELECT
                                1
                            FROM
                                descendants_base_cte d2
                            WHERE
                                d2.id = ANY (d1.traversal_ids)
                                AND d2.id <> d1.id))
                        SELECT DISTINCT
                            "namespaces"."id",
                            "namespaces"."name",
                            "namespaces"."path",
                            "namespaces"."owner_id",
                            "namespaces"."created_at",
                            "namespaces"."updated_at",
                            "namespaces"."type",
                            "namespaces"."description",
                            "namespaces"."avatar",
                            "namespaces"."membership_lock",
                            "namespaces"."share_with_group_lock",
                            "namespaces"."visibility_level",
                            "namespaces"."request_access_enabled",
                            "namespaces"."ldap_sync_status",
                            "namespaces"."ldap_sync_error",
                            "namespaces"."ldap_sync_last_update_at",
                            "namespaces"."ldap_sync_last_successful_update_at",
                            "namespaces"."ldap_sync_last_sync_at",
                            "namespaces"."description_html",
                            "namespaces"."lfs_enabled",
                            "namespaces"."parent_id",
                            "namespaces"."shared_runners_minutes_limit",
                            "namespaces"."repository_size_limit",
                            "namespaces"."require_two_factor_authentication",
                            "namespaces"."two_factor_grace_period",
                            "namespaces"."cached_markdown_version",
                            "namespaces"."project_creation_level",
                            "namespaces"."runners_token",
                            "namespaces"."file_template_project_id",
                            "namespaces"."saml_discovery_token",
                            "namespaces"."runners_token_encrypted",
                            "namespaces"."custom_project_templates_group_id",
                            "namespaces"."auto_devops_enabled",
                            "namespaces"."extra_shared_runners_minutes_limit",
                            "namespaces"."last_ci_minutes_notification_at",
                            "namespaces"."last_ci_minutes_usage_notification_level",
                            "namespaces"."subgroup_creation_level",
                            "namespaces"."max_pages_size",
                            "namespaces"."max_artifacts_size",
                            "namespaces"."mentions_disabled",
                            "namespaces"."default_branch_protection",
                            "namespaces"."max_personal_access_token_lifetime",
                            "namespaces"."push_rule_id",
                            "namespaces"."shared_runners_enabled",
                            "namespaces"."allow_descendants_override_disabled_shared_runners",
                            "namespaces"."traversal_ids",
                            "namespaces"."organization_id"
                        FROM
                            "superset",
                            "namespaces"
                        WHERE
                            "namespaces"."type" = 'Group'
                            AND next_traversal_ids_sibling ("superset"."traversal_ids") > "namespaces"."traversal_ids"
                            AND "superset"."traversal_ids" <= "namespaces"."traversal_ids")
                    UNION ( WITH "base_ancestors_cte" AS MATERIALIZED (
                            SELECT
                                "namespaces"."traversal_ids"
                            FROM (
                                (
                                    WITH "direct_groups" AS MATERIALIZED (
                                        SELECT
                                            "namespaces"."id",
                                            "namespaces"."name",
                                            "namespaces"."path",
                                            "namespaces"."owner_id",
                                            "namespaces"."created_at",
                                            "namespaces"."updated_at",
                                            "namespaces"."type",
                                            "namespaces"."description",
                                            "namespaces"."avatar",
                                            "namespaces"."membership_lock",
                                            "namespaces"."share_with_group_lock",
                                            "namespaces"."visibility_level",
                                            "namespaces"."request_access_enabled",
                                            "namespaces"."ldap_sync_status",
                                            "namespaces"."ldap_sync_error",
                                            "namespaces"."ldap_sync_last_update_at",
                                            "namespaces"."ldap_sync_last_successful_update_at",
                                            "namespaces"."ldap_sync_last_sync_at",
                                            "namespaces"."description_html",
                                            "namespaces"."lfs_enabled",
                                            "namespaces"."parent_id",
                                            "namespaces"."shared_runners_minutes_limit",
                                            "namespaces"."repository_size_limit",
                                            "namespaces"."require_two_factor_authentication",
                                            "namespaces"."two_factor_grace_period",
                                            "namespaces"."cached_markdown_version",
                                            "namespaces"."project_creation_level",
                                            "namespaces"."runners_token",
                                            "namespaces"."file_template_project_id",
                                            "namespaces"."saml_discovery_token",
                                            "namespaces"."runners_token_encrypted",
                                            "namespaces"."custom_project_templates_group_id",
                                            "namespaces"."auto_devops_enabled",
                                            "namespaces"."extra_shared_runners_minutes_limit",
                                            "namespaces"."last_ci_minutes_notification_at",
                                            "namespaces"."last_ci_minutes_usage_notification_level",
                                            "namespaces"."subgroup_creation_level",
                                            "namespaces"."max_pages_size",
                                            "namespaces"."max_artifacts_size",
                                            "namespaces"."mentions_disabled",
                                            "namespaces"."default_branch_protection",
                                            "namespaces"."max_personal_access_token_lifetime",
                                            "namespaces"."push_rule_id",
                                            "namespaces"."shared_runners_enabled",
                                            "namespaces"."allow_descendants_override_disabled_shared_runners",
                                            "namespaces"."traversal_ids",
                                            "namespaces"."organization_id"
                                        FROM
                                            "namespaces"
                                            INNER JOIN "members" ON "namespaces"."id" = "members"."source_id"
                                        WHERE
                                            "members"."type" = 'GroupMember'
                                            AND "members"."source_type" = 'Namespace'
                                            AND "namespaces"."type" = 'Group'
                                            AND "members"."user_id" = 75086
                                            AND "members"."requested_at" IS NULL
                                            AND (
                                                access_level >= 10
)
)
                                        SELECT
                                            "namespaces"."id",
                                            "namespaces"."name",
                                            "namespaces"."path",
                                            "namespaces"."owner_id",
                                            "namespaces"."created_at",
                                            "namespaces"."updated_at",
                                            "namespaces"."type",
                                            "namespaces"."description",
                                            "namespaces"."avatar",
                                            "namespaces"."membership_lock",
                                            "namespaces"."share_with_group_lock",
                                            "namespaces"."visibility_level",
                                            "namespaces"."request_access_enabled",
                                            "namespaces"."ldap_sync_status",
                                            "namespaces"."ldap_sync_error",
                                            "namespaces"."ldap_sync_last_update_at",
                                            "namespaces"."ldap_sync_last_successful_update_at",
                                            "namespaces"."ldap_sync_last_sync_at",
                                            "namespaces"."description_html",
                                            "namespaces"."lfs_enabled",
                                            "namespaces"."parent_id",
                                            "namespaces"."shared_runners_minutes_limit",
                                            "namespaces"."repository_size_limit",
                                            "namespaces"."require_two_factor_authentication",
                                            "namespaces"."two_factor_grace_period",
                                            "namespaces"."cached_markdown_version",
                                            "namespaces"."project_creation_level",
                                            "namespaces"."runners_token",
                                            "namespaces"."file_template_project_id",
                                            "namespaces"."saml_discovery_token",
                                            "namespaces"."runners_token_encrypted",
                                            "namespaces"."custom_project_templates_group_id",
                                            "namespaces"."auto_devops_enabled",
                                            "namespaces"."extra_shared_runners_minutes_limit",
                                            "namespaces"."last_ci_minutes_notification_at",
                                            "namespaces"."last_ci_minutes_usage_notification_level",
                                            "namespaces"."subgroup_creation_level",
                                            "namespaces"."max_pages_size",
                                            "namespaces"."max_artifacts_size",
                                            "namespaces"."mentions_disabled",
                                            "namespaces"."default_branch_protection",
                                            "namespaces"."max_personal_access_token_lifetime",
                                            "namespaces"."push_rule_id",
                                            "namespaces"."shared_runners_enabled",
                                            "namespaces"."allow_descendants_override_disabled_shared_runners",
                                            "namespaces"."traversal_ids",
                                            "namespaces"."organization_id"
                                        FROM (
                                            (
                                                SELECT
                                                    "namespaces"."id",
                                                    "namespaces"."name",
                                                    "namespaces"."path",
                                                    "namespaces"."owner_id",
                                                    "namespaces"."created_at",
                                                    "namespaces"."updated_at",
                                                    "namespaces"."type",
                                                    "namespaces"."description",
                                                    "namespaces"."avatar",
                                                    "namespaces"."membership_lock",
                                                    "namespaces"."share_with_group_lock",
                                                    "namespaces"."visibility_level",
                                                    "namespaces"."request_access_enabled",
                                                    "namespaces"."ldap_sync_status",
                                                    "namespaces"."ldap_sync_error",
                                                    "namespaces"."ldap_sync_last_update_at",
                                                    "namespaces"."ldap_sync_last_successful_update_at",
                                                    "namespaces"."ldap_sync_last_sync_at",
                                                    "namespaces"."description_html",
                                                    "namespaces"."lfs_enabled",
                                                    "namespaces"."parent_id",
                                                    "namespaces"."shared_runners_minutes_limit",
                                                    "namespaces"."repository_size_limit",
                                                    "namespaces"."require_two_factor_authentication",
                                                    "namespaces"."two_factor_grace_period",
                                                    "namespaces"."cached_markdown_version",
                                                    "namespaces"."project_creation_level",
                                                    "namespaces"."runners_token",
                                                    "namespaces"."file_template_project_id",
                                                    "namespaces"."saml_discovery_token",
                                                    "namespaces"."runners_token_encrypted",
                                                    "namespaces"."custom_project_templates_group_id",
                                                    "namespaces"."auto_devops_enabled",
                                                    "namespaces"."extra_shared_runners_minutes_limit",
                                                    "namespaces"."last_ci_minutes_notification_at",
                                                    "namespaces"."last_ci_minutes_usage_notification_level",
                                                    "namespaces"."subgroup_creation_level",
                                                    "namespaces"."max_pages_size",
                                                    "namespaces"."max_artifacts_size",
                                                    "namespaces"."mentions_disabled",
                                                    "namespaces"."default_branch_protection",
                                                    "namespaces"."max_personal_access_token_lifetime",
                                                    "namespaces"."push_rule_id",
                                                    "namespaces"."shared_runners_enabled",
                                                    "namespaces"."allow_descendants_override_disabled_shared_runners",
                                                    "namespaces"."traversal_ids",
                                                    "namespaces"."organization_id"
                                                FROM
                                                    "direct_groups" "namespaces"
                                                WHERE
                                                    "namespaces"."type" = 'Group'
)
                                            UNION (
                                                SELECT
                                                    "namespaces"."id",
                                                    "namespaces"."name",
                                                    "namespaces"."path",
                                                    "namespaces"."owner_id",
                                                    "namespaces"."created_at",
                                                    "namespaces"."updated_at",
                                                    "namespaces"."type",
                                                    "namespaces"."description",
                                                    "namespaces"."avatar",
                                                    "namespaces"."membership_lock",
                                                    "namespaces"."share_with_group_lock",
                                                    "namespaces"."visibility_level",
                                                    "namespaces"."request_access_enabled",
                                                    "namespaces"."ldap_sync_status",
                                                    "namespaces"."ldap_sync_error",
                                                    "namespaces"."ldap_sync_last_update_at",
                                                    "namespaces"."ldap_sync_last_successful_update_at",
                                                    "namespaces"."ldap_sync_last_sync_at",
                                                    "namespaces"."description_html",
                                                    "namespaces"."lfs_enabled",
                                                    "namespaces"."parent_id",
                                                    "namespaces"."shared_runners_minutes_limit",
                                                    "namespaces"."repository_size_limit",
                                                    "namespaces"."require_two_factor_authentication",
                                                    "namespaces"."two_factor_grace_period",
                                                    "namespaces"."cached_markdown_version",
                                                    "namespaces"."project_creation_level",
                                                    "namespaces"."runners_token",
                                                    "namespaces"."file_template_project_id",
                                                    "namespaces"."saml_discovery_token",
                                                    "namespaces"."runners_token_encrypted",
                                                    "namespaces"."custom_project_templates_group_id",
                                                    "namespaces"."auto_devops_enabled",
                                                    "namespaces"."extra_shared_runners_minutes_limit",
                                                    "namespaces"."last_ci_minutes_notification_at",
                                                    "namespaces"."last_ci_minutes_usage_notification_level",
                                                    "namespaces"."subgroup_creation_level",
                                                    "namespaces"."max_pages_size",
                                                    "namespaces"."max_artifacts_size",
                                                    "namespaces"."mentions_disabled",
                                                    "namespaces"."default_branch_protection",
                                                    "namespaces"."max_personal_access_token_lifetime",
                                                    "namespaces"."push_rule_id",
                                                    "namespaces"."shared_runners_enabled",
                                                    "namespaces"."allow_descendants_override_disabled_shared_runners",
                                                    "namespaces"."traversal_ids",
                                                    "namespaces"."organization_id"
                                                FROM
                                                    "namespaces"
                                                WHERE
                                                    "namespaces"."type" = 'Group'
                                                    AND "namespaces"."id" IN (
                                                        SELECT
                                                            "projects"."namespace_id"
                                                        FROM
                                                            "projects"
                                                            INNER JOIN "project_authorizations" ON "projects"."id" = "project_authorizations"."project_id"
                                                        WHERE
                                                            "project_authorizations"."user_id" = 75086
)
)
                                                UNION (
                                                    SELECT
                                                        "namespaces"."id",
                                                        "namespaces"."name",
                                                        "namespaces"."path",
                                                        "namespaces"."owner_id",
                                                        "namespaces"."created_at",
                                                        "namespaces"."updated_at",
                                                        "namespaces"."type",
                                                        "namespaces"."description",
                                                        "namespaces"."avatar",
                                                        "namespaces"."membership_lock",
                                                        "namespaces"."share_with_group_lock",
                                                        "namespaces"."visibility_level",
                                                        "namespaces"."request_access_enabled",
                                                        "namespaces"."ldap_sync_status",
                                                        "namespaces"."ldap_sync_error",
                                                        "namespaces"."ldap_sync_last_update_at",
                                                        "namespaces"."ldap_sync_last_successful_update_at",
                                                        "namespaces"."ldap_sync_last_sync_at",
                                                        "namespaces"."description_html",
                                                        "namespaces"."lfs_enabled",
                                                        "namespaces"."parent_id",
                                                        "namespaces"."shared_runners_minutes_limit",
                                                        "namespaces"."repository_size_limit",
                                                        "namespaces"."require_two_factor_authentication",
                                                        "namespaces"."two_factor_grace_period",
                                                        "namespaces"."cached_markdown_version",
                                                        "namespaces"."project_creation_level",
                                                        "namespaces"."runners_token",
                                                        "namespaces"."file_template_project_id",
                                                        "namespaces"."saml_discovery_token",
                                                        "namespaces"."runners_token_encrypted",
                                                        "namespaces"."custom_project_templates_group_id",
                                                        "namespaces"."auto_devops_enabled",
                                                        "namespaces"."extra_shared_runners_minutes_limit",
                                                        "namespaces"."last_ci_minutes_notification_at",
                                                        "namespaces"."last_ci_minutes_usage_notification_level",
                                                        "namespaces"."subgroup_creation_level",
                                                        "namespaces"."max_pages_size",
                                                        "namespaces"."max_artifacts_size",
                                                        "namespaces"."mentions_disabled",
                                                        "namespaces"."default_branch_protection",
                                                        "namespaces"."max_personal_access_token_lifetime",
                                                        "namespaces"."push_rule_id",
                                                        "namespaces"."shared_runners_enabled",
                                                        "namespaces"."allow_descendants_override_disabled_shared_runners",
                                                        "namespaces"."traversal_ids",
                                                        "namespaces"."organization_id"
                                                    FROM
                                                        "namespaces"
                                                        INNER JOIN "group_group_links" ON "group_group_links"."shared_group_id" = "namespaces"."id"
                                                    WHERE
                                                        "namespaces"."type" = 'Group'
                                                        AND "group_group_links"."shared_with_group_id" IN (
                                                            SELECT
                                                                "namespaces"."id"
                                                            FROM
                                                                "direct_groups" "namespaces"
                                                            WHERE
                                                                "namespaces"."type" = 'Group'
)
)
) namespaces
                                                WHERE
                                                    "namespaces"."type" = 'Group'
)
) namespaces
                                        WHERE
                                            "namespaces"."type" = 'Group'
)
                                        SELECT
                                            "namespaces"."id",
                                            "namespaces"."name",
                                            "namespaces"."path",
                                            "namespaces"."owner_id",
                                            "namespaces"."created_at",
                                            "namespaces"."updated_at",
                                            "namespaces"."type",
                                            "namespaces"."description",
                                            "namespaces"."avatar",
                                            "namespaces"."membership_lock",
                                            "namespaces"."share_with_group_lock",
                                            "namespaces"."visibility_level",
                                            "namespaces"."request_access_enabled",
                                            "namespaces"."ldap_sync_status",
                                            "namespaces"."ldap_sync_error",
                                            "namespaces"."ldap_sync_last_update_at",
                                            "namespaces"."ldap_sync_last_successful_update_at",
                                            "namespaces"."ldap_sync_last_sync_at",
                                            "namespaces"."description_html",
                                            "namespaces"."lfs_enabled",
                                            "namespaces"."parent_id",
                                            "namespaces"."shared_runners_minutes_limit",
                                            "namespaces"."repository_size_limit",
                                            "namespaces"."require_two_factor_authentication",
                                            "namespaces"."two_factor_grace_period",
                                            "namespaces"."cached_markdown_version",
                                            "namespaces"."project_creation_level",
                                            "namespaces"."runners_token",
                                            "namespaces"."file_template_project_id",
                                            "namespaces"."saml_discovery_token",
                                            "namespaces"."runners_token_encrypted",
                                            "namespaces"."custom_project_templates_group_id",
                                            "namespaces"."auto_devops_enabled",
                                            "namespaces"."extra_shared_runners_minutes_limit",
                                            "namespaces"."last_ci_minutes_notification_at",
                                            "namespaces"."last_ci_minutes_usage_notification_level",
                                            "namespaces"."subgroup_creation_level",
                                            "namespaces"."max_pages_size",
                                            "namespaces"."max_artifacts_size",
                                            "namespaces"."mentions_disabled",
                                            "namespaces"."default_branch_protection",
                                            "namespaces"."max_personal_access_token_lifetime",
                                            "namespaces"."push_rule_id",
                                            "namespaces"."shared_runners_enabled",
                                            "namespaces"."allow_descendants_override_disabled_shared_runners",
                                            "namespaces"."traversal_ids",
                                            "namespaces"."organization_id"
                                        FROM
                                            "namespaces"
                                            INNER JOIN ( SELECT DISTINCT
                                                    UNNEST("base_ancestors_cte"."traversal_ids")
                                                FROM
                                                    base_ancestors_cte) AS ancestors (ancestor_id) ON namespaces.id = ancestors.ancestor_id
                                            WHERE
                                                "namespaces"."type" = 'Group')) namespaces
                                    WHERE
                                        "namespaces"."type" = 'Group'
                                    ORDER BY
                                        "namespaces"."name" ASC,
                                        "namespaces"."id" ASC) namespaces,
                                    LATERAL (
                                        SELECT
                                            COALESCE(SUM("project_statistics"."storage_size"), 0) AS storage_size,
                                            COALESCE(SUM("project_statistics"."repository_size"), 0) AS repository_size,
                                            COALESCE(SUM("project_statistics"."wiki_size"), 0) AS wiki_size,
                                            COALESCE(SUM("project_statistics"."snippets_size"), 0) AS snippets_size,
                                            COALESCE(SUM("project_statistics"."lfs_objects_size"), 0) AS lfs_objects_size,
                                            COALESCE(SUM("project_statistics"."build_artifacts_size"), 0) AS build_artifacts_size,
                                            COALESCE(SUM("project_statistics"."pipeline_artifacts_size"), 0) AS pipeline_artifacts_size,
                                            COALESCE(SUM("project_statistics"."packages_size"), 0) AS packages_size,
                                            COALESCE(SUM("project_statistics"."uploads_size"), 0) AS uploads_size
                                        FROM
                                            "project_statistics"
                                        WHERE
                                            "project_statistics"."namespace_id" = "namespaces"."id") statistics
                                    WHERE
                                        "namespaces"."type" = 'Group'
                                    ORDER BY
                                        "namespaces"."name" ASC,
                                        "namespaces"."id" ASC
                                    LIMIT 20 OFFSET 0;

How to set up and validate locally

https://gdk.test:3000/api/v4/groups?all_available=false&statistics=true

  1. Login using an admin user.
  2. On the master go to the above URL and you'll get an internal server error message.
  3. On this branch go to the above URL and you'll get the groups with their statistics.

Related to #378714 (closed)

Edited by Abdul Wadood

Merge request reports

Loading