Module: PgHero::Methods::Connections

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

Instance Method Summary collapse

Instance Method Details

#connection_sourcesObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/pghero/methods/connections.rb', line 59

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

#connection_statesObject



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

def connection_states
  states = select_all "    SELECT\n      state,\n      COUNT(*) AS connections\n    FROM\n      pg_stat_activity\n    GROUP BY\n      1\n    ORDER BY\n      2 DESC, 1\n  SQL\n\n  states.to_h { |s| [s[:state], s[:connections]] }\nend\n"

#connectionsObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/pghero/methods/connections.rb', line 4

def connections
  if server_version_num >= 90500
    select_all "      SELECT\n        pg_stat_activity.pid,\n        datname AS database,\n        usename AS user,\n        application_name AS source,\n        client_addr AS ip,\n        state,\n        ssl\n      FROM\n        pg_stat_activity\n      LEFT JOIN\n        pg_stat_ssl ON pg_stat_activity.pid = pg_stat_ssl.pid\n      ORDER BY\n        pg_stat_activity.pid\n    SQL\n  else\n    select_all <<~SQL\n      SELECT\n        pid,\n        datname AS database,\n        usename AS user,\n        application_name AS source,\n        client_addr AS ip,\n        state\n      FROM\n        pg_stat_activity\n      ORDER BY\n        pid\n    SQL\n  end\nend\n"

#total_connectionsObject



39
40
41
# File 'lib/pghero/methods/connections.rb', line 39

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