Class: PgCanary::Rules::JsonbSearchWithoutGin

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

Overview

Searching a jsonb column in WHERE:

  • containment/existence operators (@>, <@, ?, ?|, ?&) need a GIN index on the column
  • key extraction (->> etc.) compared against a value needs a matching expression index

Constant Summary collapse

CONTAINMENT_OPS =
%w[@> <@ ? ?| ?&].freeze
EXTRACTION_OPS =
%w[-> ->> #> #>>].freeze

Constants included from IndexPredicates

IndexPredicates::TRGM_ACCESS_METHODS

Instance Method Summary collapse

Methods inherited from Base

all, check, default_enabled, enabled?, #initialize, option, options, rule_name

Constructor Details

This class inherits a constructor from PgCanary::Rules::Base

Instance Method Details

#checkObject



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

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

    scope.where_clause.walk_scope do |node|
      next unless node.is_a?(PgQuery::A_Expr) && node.kind == :AEXPR_OP

      operator = node.operator
      if CONTAINMENT_OPS.include?(operator)
        detections << inspect_containment(scope, node, operator)
      elsif EXTRACTION_OPS.include?(operator)
        detections << inspect_extraction(scope, node, operator)
      end
    end
  end
  detections.compact
end