Sha256: d4394daa1e8e24eef860cbce73c75d2f105e1da73dfd5091b7cf9a63f30609b6

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 KB

Contents

module PgHero
  module Methods
    module Replication
      def replica?
        unless defined?(@replica)
          @replica = select_one("SELECT pg_is_in_recovery()")
        end
        @replica
      end

      # http://www.postgresql.org/message-id/CADKbJJWz9M0swPT3oqe8f9+tfD4-F54uE6Xtkh4nERpVsQnjnw@mail.gmail.com
      def replication_lag
        with_feature_support do
          lag_condition =
            if server_version_num >= 100000
              "pg_last_wal_receive_lsn() = pg_last_wal_replay_lsn()"
            else
              "pg_last_xlog_receive_location() = pg_last_xlog_replay_location()"
            end

          select_one <<-SQL
            SELECT
              CASE
                WHEN NOT pg_is_in_recovery() OR #{lag_condition} THEN 0
                ELSE EXTRACT (EPOCH FROM NOW() - pg_last_xact_replay_timestamp())
              END
            AS replication_lag
          SQL
        end
      end

      def replication_slots
        if server_version_num >= 90400
          with_feature_support([]) do
            select_all <<-SQL
              SELECT
                slot_name,
                database,
                active
              FROM pg_replication_slots
            SQL
          end
        else
          []
        end
      end

      def replicating?
        with_feature_support(false) do
          select_all("SELECT state FROM pg_stat_replication").any?
        end
      end

      private

      def with_feature_support(default = nil)
        begin
          yield
        rescue ActiveRecord::StatementInvalid => e
          raise unless e.message.start_with?("PG::FeatureNotSupported:")
          default
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pghero-2.1.1 lib/pghero/methods/replication.rb
pghero-2.1.0 lib/pghero/methods/replication.rb