Class: Dexter::PgStatStatementsSource

Inherits:
Object
  • Object
show all
Defined in:
lib/dexter/sources/pg_stat_statements_source.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ PgStatStatementsSource

Returns a new instance of PgStatStatementsSource.



3
4
5
# File 'lib/dexter/sources/pg_stat_statements_source.rb', line 3

def initialize(connection)
  @connection = connection
end

Instance Method Details

#perform(collector) ⇒ Object



7
8
9
10
11
# File 'lib/dexter/sources/pg_stat_statements_source.rb', line 7

def perform(collector)
  stat_statements.each do |row|
    collector.add(row["query"], row["duration_ms"].to_f, row["calls"].to_i)
  end
end

#stat_statementsObject

could group, sum, and filter min_time/min_calls in SQL, but keep simple for now



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/dexter/sources/pg_stat_statements_source.rb', line 14

def stat_statements
  sql = <<~SQL
    SELECT
      query,
      total_plan_time + total_exec_time AS duration_ms,
      calls
    FROM
      pg_stat_statements
    INNER JOIN
      pg_database ON pg_database.oid = pg_stat_statements.dbid
    WHERE
      datname = current_database()
    ORDER BY
      1
  SQL
  @connection.execute(sql)
rescue PG::UndefinedTable => e
  raise Error, e.message
end