Skip to content

Resolve "Linear Project#ancestors"

Alex Pooley requested to merge 337719-linear-project-ancestors into master

What does this MR do?

Project#ancestors (aliased to #ancestors_upto) uses ObjectHierarchy to make recursive calls. We can replace this method with the linear version from the Namespace model. In practice we've found the linear versions to be faster and less complicated for the database optimizer to work with.

SQL Changes

Click through to the postgres.ai links for actual IDs used.

Project#ancestors

New

SELECT "namespaces".* FROM "namespaces" WHERE "namespaces"."id" IN (22, 23, 24)
Time: 4.003 ms  
  - planning: 2.956 ms  
  - execution: 1.047 ms  
    - I/O read: 0.927 ms  
    - I/O write: 0.000 ms  
  
Shared buffers:  
  - hits: 6 (~48.00 KiB) from the buffer pool  
  - reads: 13 (~104.00 KiB) from the OS file cache, including disk I/O  
  - dirtied: 0  
  - writes: 0 

https://postgres.ai/console/gitlab/gitlab-production-tunnel-pg12/sessions/5825/commands/19745

Old

WITH RECURSIVE "base_and_ancestors" AS (
  (
    SELECT 
      "namespaces".* 
    FROM 
      "namespaces" 
    WHERE 
      "namespaces"."type" = 'Group' 
      AND "namespaces"."id" = 24
  ) 
  UNION 
    (
      SELECT 
        "namespaces".* 
      FROM 
        "namespaces", 
        "base_and_ancestors" 
      WHERE 
        "namespaces"."type" = 'Group' 
        AND "namespaces"."id" = "base_and_ancestors"."parent_id"
    )
) 
SELECT 
  "namespaces".* 
FROM 
  "base_and_ancestors" AS "namespaces"

Namespace#ancestors with hierarchy_order

The "SELECT namespaces.*" wrapper was added to normalize the SELECT. Otherwise you can get mismatched column count errors with unions. Union queries are common when working with namespace hierarchies.

SELECT 
  "namespaces".* 
FROM 
  (
    SELECT 
      "namespaces".*, 
      ABS(
        3 - array_length(traversal_ids, 1)
      ) as depth 
    FROM 
      "namespaces" 
    WHERE 
      "namespaces"."id" IN (27, 28) 
    ORDER BY 
      "depth" DESC
  ) namespaces
Time: 4.275 ms  
  - planning: 3.130 ms  
  - execution: 1.145 ms  
    - I/O read: 0.964 ms  
    - I/O write: 0.000 ms  
  
Shared buffers:  
  - hits: 9 (~72.00 KiB) from the buffer pool  
  - reads: 13 (~104.00 KiB) from the OS file cache, including disk I/O  
  - dirtied: 0  
  - writes: 0 

https://postgres.ai/console/gitlab/gitlab-production-tunnel-pg12/sessions/5836/commands/19748

How to setup and validate locally (strongly suggested)

Does this MR meet the acceptance criteria?

Conformity

Availability and Testing

Security

Does this MR contain changes to processing or storing of credentials or tokens, authorization and authentication methods or other items described in the security review guidelines? If not, then delete this Security section.

  • [-] 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

Related to #337719 (closed)

Edited by Mayra Cabrera

Merge request reports

Loading