Module: PgHero::Methods::Connections

Included in:
Database
Defined in:
lib/pghero/methods/connections.rb

Instance Method Summary collapse

Instance Method Details

#connection_sourcesObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/pghero/methods/connections.rb', line 43

def connection_sources
  select_all <<~SQL
    SELECT
      datname AS database,
      usename AS user,
      application_name AS source,
      client_addr AS ip,
      COUNT(*) AS total_connections
    FROM
      pg_stat_activity
    GROUP BY
      1, 2, 3, 4
    ORDER BY
      5 DESC, 1, 2, 3, 4
  SQL
end

#connection_statesObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pghero/methods/connections.rb', line 27

def connection_states
  states = select_all <<~SQL
    SELECT
      state,
      COUNT(*) AS connections
    FROM
      pg_stat_activity
    GROUP BY
      1
    ORDER BY
      2 DESC, 1
  SQL

  states.to_h { |s| [s[:state], s[:connections]] }
end

#connectionsObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/pghero/methods/connections.rb', line 4

def connections
  select_all <<~SQL
    SELECT
      pg_stat_activity.pid,
      datname AS database,
      usename AS user,
      application_name AS source,
      client_addr AS ip,
      state,
      ssl
    FROM
      pg_stat_activity
    LEFT JOIN
      pg_stat_ssl ON pg_stat_activity.pid = pg_stat_ssl.pid
    ORDER BY
      pg_stat_activity.pid
  SQL
end

#total_connectionsObject



23
24
25
# File 'lib/pghero/methods/connections.rb', line 23

def total_connections
  select_one("SELECT COUNT(*) FROM pg_stat_activity")
end