Class: PgCanary::Rules::ArraySearchWithoutGin

Inherits:
Base
  • Object
show all
Includes:
IndexPredicates
Defined in:
lib/pg_canary/rules/definitions/array_search_without_gin.rb

Overview

Searching an array column (@>, <@, &&, or value = ANY(column)) without a GIN index scans every row.

Constant Summary collapse

ARRAY_OPS =
%w[@> <@ &&].freeze

Constants included from IndexPredicates

IndexPredicates::TRGM_ACCESS_METHODS

Constants included from PgQuerySupport

PgQuerySupport::COMPARISON_OPS

Instance Method Summary collapse

Methods inherited from Base

all, #enabled?, options, rule_name

Instance Method Details

#check(query) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pg_canary/rules/definitions/array_search_without_gin.rb', line 16

def check(query)
  detections = []
  query.each_scope do |scope|
    next unless scope.where_clause

    walk_within_scope(scope.where_clause) do |node|
      next unless node.is_a?(PgQuery::A_Expr)

      case node.kind
      when :AEXPR_OP
        operator = operator_name(node)
        next unless ARRAY_OPS.include?(operator)

        detections << inspect_sides(query, scope, [node.lexpr, node.rexpr], operator)
      when :AEXPR_OP_ANY
        detections << inspect_sides(query, scope, [node.rexpr], "= ANY(column)")
      end
    end
  end
  detections.compact
end

#default_enabledObject



10
11
12
# File 'lib/pg_canary/rules/definitions/array_search_without_gin.rb', line 10

def default_enabled
  true
end