Class: PgCanary::Rules::HugeInList

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

Overview

IN (...) / = ANY(...) with a huge number of values: the statement itself becomes expensive to parse/plan and usually signals a missing JOIN. Counts literal list items and runtime array binds, which static SQL linters cannot see. Threshold: config.rules.huge_in_list.threshold (default 500).

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



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

def check
  threshold = rule_config.threshold
  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)

      count = value_count(node)
      next unless count && count > threshold

      table, column = resolve_lexpr(scope, node)
      detections << detection(
        table: table,
        columns: column,
        message: "IN / ANY list with #{count} values (threshold: #{threshold}). Huge value lists " \
                 "are expensive to parse and plan, and usually mean a JOIN is missing.",
        suggestion: "Rewrite as a JOIN against the source of the values (subquery or VALUES list) " \
                    "instead of materializing the ids in the query."
      )
    end
  end
  detections
end