Class: ActiveRecord::PostgreSQLAnalyzer::LogSubscriber

Inherits:
LogSubscriber
  • Object
show all
Defined in:
lib/active_record/postgresql_analyzer.rb

Constant Summary collapse

IGNORED_PAYLOADS =
%w(SCHEMA EXPLAIN CACHE)
EXPLAINED_SQLS =
/\A\s*(select|update|delete|insert)\b/i
SEQ_SCAN =
/.*Seq Scan.*/

Instance Method Summary collapse

Instance Method Details

#ignore_payload?(payload) ⇒ Boolean

Returns:



30
31
32
# File 'lib/active_record/postgresql_analyzer.rb', line 30

def ignore_payload?(payload)
  payload[:exception] || IGNORED_PAYLOADS.include?(payload[:name]) || payload[:sql] !~ EXPLAINED_SQLS
end

#seq_scan?(explain_result) ⇒ Boolean

Returns:



34
35
36
# File 'lib/active_record/postgresql_analyzer.rb', line 34

def seq_scan?(explain_result)
  explain_result =~ SEQ_SCAN
end

#sql(event) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/active_record/postgresql_analyzer.rb', line 13

def sql(event)
  payload = event.payload

  return if ignore_payload?(payload)

  # disable SeqScan when index exists
  #   SEE ALSO: http://www.postgresql.org/docs/9.4/static/indexes-examine.html
  ActiveRecord::Base.connection.execute("SET enable_seqscan TO off", "SCHEMA")

  explain_result = ActiveRecord::Base.connection.explain(payload[:sql], payload[:binds])
  if seq_scan?(explain_result)
    debug '------------ find Seq Scan query ------------'
    debug payload[:sql]
    debug explain_result
  end
end