Skip to content

Linear version of Project#ancestors

Alex Pooley requested to merge apooley/project-ancestors-v2 into master

What does this MR do?

The code in this MR was previously merged but then reverted due to a production incident.

  1. This MR re-introduces the commit from the original MR
  2. Extends with a second commit to correct errors, namely only considers Groups as ancestors and not Namespaces as in the case of personal projects.
  3. Guard the work behind a feature flag

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(
        4 - Array_length(traversal_ids, 1)
      ) AS depth 
    FROM 
      "namespaces" 
    WHERE 
      "namespaces"."id" IN (1,2,3)
  ) namespaces 
ORDER BY 
  "depth" DESC
Time: 6.245 ms  
  - planning: 3.872 ms  
  - execution: 2.373 ms  
    - I/O read: 2.108 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/5974/commands/20153

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 Alex Pooley

Merge request reports

Loading