Class: Gitlab::Database::AsWithMaterialized

Inherits:
Arel::Nodes::As
  • Object
show all
Extended by:
Utils::StrongMemoize
Defined in:
lib/gitlab/database/as_with_materialized.rb

Overview

This class is a special Arel node which allows optionally define the `MATERIALIZED` keyword for CTE and Recursive CTE queries.

Constant Summary collapse

MATERIALIZED =
'MATERIALIZED '

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::StrongMemoize

clear_memoization, strong_memoize, strong_memoized?

Constructor Details

#initialize(left, right, materialized: true) ⇒ AsWithMaterialized

Returns a new instance of AsWithMaterialized.


11
12
13
14
15
16
17
# File 'lib/gitlab/database/as_with_materialized.rb', line 11

def initialize(left, right, materialized: true)
  if materialized && self.class.materialized_supported?
    right.prepend(MATERIALIZED)
  end

  super(left, right)
end

Class Method Details

.materialized_if_supportedObject

Note: to be deleted after the minimum PG version is set to 12.0 Update the documentation together when deleting the method docs.gitlab.com/ee/development/merge_request_performance_guidelines.html#use-ctes-wisely


29
30
31
# File 'lib/gitlab/database/as_with_materialized.rb', line 29

def self.materialized_if_supported
  materialized_supported? ? 'MATERIALIZED' : ''
end

.materialized_supported?Boolean

Note: to be deleted after the minimum PG version is set to 12.0

Returns:

  • (Boolean)

20
21
22
23
24
# File 'lib/gitlab/database/as_with_materialized.rb', line 20

def self.materialized_supported?
  strong_memoize(:materialized_supported) do
    ApplicationRecord.database.version.match?(/^1[2-9]\./) # version 12.x and above
  end
end