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 = "    SELECT\n      query,\n      total_plan_time + total_exec_time AS duration_ms,\n      calls\n    FROM\n      pg_stat_statements\n    INNER JOIN\n      pg_database ON pg_database.oid = pg_stat_statements.dbid\n    WHERE\n      datname = current_database()\n    ORDER BY\n      1\n  SQL\n  @connection.execute(sql)\nrescue PG::UndefinedTable => e\n  raise Error, e.message\nend\n"