Class: PgCanary::Rules::SelectStarWithHeavyColumns

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

Overview

Tier 2 (opt-in): SELECT * (ActiveRecord's default) on a table that has heavy columns (bytea / text / jsonb) transfers those payloads on every query. Whether that matters depends on the data, hence opt-in. Heavy types: config.rules.select_star_with_heavy_columns.heavy_types.

Constant Summary

Constants included from PgQuerySupport

PgQuerySupport::COMPARISON_OPS

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

all, #enabled?, rule_name

Class Method Details

.optionsObject



14
15
16
# File 'lib/pg_canary/rules/definitions/select_star_with_heavy_columns.rb', line 14

def self.options
  { heavy_types: %w[bytea jsonb text].freeze }
end

Instance Method Details

#check(query) ⇒ Object



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/select_star_with_heavy_columns.rb', line 18

def check(query)
  heavy_types = rule_config(query).heavy_types
  detections = []
  query.each_scope do |scope|
    star_tables(scope).each do |table|
      next unless applicable_table?(query, table)

      heavy = query.column_types(table).select { |_, type| heavy_types.include?(type) }.keys
      next if heavy.empty?

      detections << detection(
        query,
        table: table,
        columns: heavy,
        message: "SELECT * on #{table} transfers its heavy columns (#{heavy.join(", ")}) " \
                 "on every query.",
        suggestion: "Select only the columns you need (.select(:id, ...)), or move large " \
                    "payloads to a separate table loaded on demand."
      )
    end
  end
  detections
end

#default_enabledObject



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

def default_enabled
  false
end