Sha256: 00a6d5dcc659307894756fa05fb39e80b51f83f5576c64b9e9327663584de1e9

Contents?: true

Size: 1.77 KB

Versions: 14

Compression:

Stored size: 1.77 KB

Contents

module PgHero
  module Methods
    module Connections
      def connections
        if server_version_num >= 90500
          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
        else
          select_all <<-SQL
            SELECT
              pid,
              datname AS database,
              usename AS user,
              application_name AS source,
              client_addr AS ip,
              state
            FROM
              pg_stat_activity
            ORDER BY
              pid
          SQL
        end
      end

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

      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

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

      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
    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
pghero-2.8.3 lib/pghero/methods/connections.rb
pghero-2.8.2 lib/pghero/methods/connections.rb
pghero-2.8.1 lib/pghero/methods/connections.rb
pghero-2.8.0 lib/pghero/methods/connections.rb
pghero-2.7.4 lib/pghero/methods/connections.rb
pghero_fork-2.7.3 lib/pghero/methods/connections.rb
pghero-2.7.3 lib/pghero/methods/connections.rb
pghero-2.7.2 lib/pghero/methods/connections.rb
pghero-2.7.1 lib/pghero/methods/connections.rb
pghero-2.7.0 lib/pghero/methods/connections.rb
pghero-2.6.0 lib/pghero/methods/connections.rb
pghero-2.5.1 lib/pghero/methods/connections.rb
pghero-2.5.0 lib/pghero/methods/connections.rb
pghero-2.4.2 lib/pghero/methods/connections.rb