Module: PgHero::Methods::Replication

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

Instance Method Summary collapse

Instance Method Details

#replica?Boolean

Returns:

  • (Boolean)


4
5
6
7
8
9
# File 'lib/pghero/methods/replication.rb', line 4

def replica?
  unless defined?(@replica)
    @replica = select_one("SELECT pg_is_in_recovery()")
  end
  @replica
end

#replicating?Boolean

Returns:

  • (Boolean)


48
49
50
51
52
# File 'lib/pghero/methods/replication.rb', line 48

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

#replication_lagObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pghero/methods/replication.rb', line 12

def replication_lag
  with_feature_support(:replication_lag) 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

#replication_slotsObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/pghero/methods/replication.rb', line 32

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