Module: PG::Replication

Defined in:
lib/pg/replication.rb,
lib/pg/replication/buffer.rb,
lib/pg/replication/version.rb,
lib/pg/replication/protocol.rb,
lib/pg/replication/pg_output.rb

Defined Under Namespace

Modules: PGOutput, Protocol Classes: Buffer

Constant Summary collapse

VERSION =
"0.0.7"

Instance Method Summary collapse

Instance Method Details

#confirmed_slot_lsn(slot) ⇒ Object



118
119
120
121
122
123
124
125
126
# File 'lib/pg/replication.rb', line 118

def confirmed_slot_lsn(slot)
  lsn = query("    SELECT confirmed_flush_lsn FROM pg_replication_slots WHERE slot_name = '\#{slot}'\n  SQL\n  high, low = lsn.split(\"/\")\n  (high.to_i(16) << 32) + low.to_i(16)\nrescue StandardError\n  nil\nend\n").getvalue(0, 0)

#last_confirmed_lsnObject



108
109
110
# File 'lib/pg/replication.rb', line 108

def last_confirmed_lsn
  @last_confirmed_lsn
end

#standby_status_update(write_lsn:, flush_lsn: write_lsn, apply_lsn: write_lsn, timestamp: Time.now, reply: false) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/pg/replication.rb', line 87

def standby_status_update(
  write_lsn:,
  flush_lsn: write_lsn,
  apply_lsn: write_lsn,
  timestamp: Time.now,
  reply: false
)
  msg = [
    "r".bytes.first,
    write_lsn,
    flush_lsn,
    apply_lsn,
    (timestamp - Time.new(2_000, 1, 1, 0, 0, 0, 0)) * 10**6,
    reply ? 1 : 0,
  ].pack("CQ>Q>Q>Q>C")

  put_copy_data(msg)
  flush
  @last_confirmed_lsn = [@last_confirmed_lsn, write_lsn].compact.max
end

#start_pgoutput_replication_slot(slot, publication_names, **kwargs) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/pg/replication.rb', line 72

def start_pgoutput_replication_slot(slot, publication_names, **kwargs)
  publication_names = publication_names.join(",")

  start_replication_slot(slot, **kwargs.merge(proto_version: "1", publication_names:))
    .map do |msg|
      case msg
      in Protocol::XLogData(data:, lsn:)
        data = data.force_encoding(internal_encoding)
        msg.with(data: PGOutput.read_message(Buffer.from_string(data)))
      else
        msg
      end
    end
end

#start_replication_slot(slot, logical: true, auto_keep_alive: true, location: "0/0", **params) ⇒ Object



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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/pg/replication.rb', line 11

def start_replication_slot(slot, logical: true, auto_keep_alive: true, location: "0/0", **params)
  keep_alive_secs = wal_receiver_status_interval
  @last_confirmed_lsn = confirmed_slot_lsn(slot) || 0

  start_query = "START_REPLICATION SLOT #{slot} #{logical ? "LOGICAL" : "PHYSICAL"} #{location}"
  unless params.empty?
    start_query << "("
    start_query << params
      .map { |k, v| "#{quote_ident(k.to_s)} '#{escape_string(v.to_s)}'" }
      .join(", ")
    start_query << ")"
  end
  query(start_query)

  last_keep_alive = Time.now

  Enumerator
    .new do |y|
      loop do
        if auto_keep_alive && Time.now - last_keep_alive > keep_alive_secs
          standby_status_update(write_lsn: @last_confirmed_lsn)
          last_keep_alive = Time.now
        end

        consume_input
        next if is_busy

        case get_copy_data(async: true)
        in nil
          get_last_result
          break

        in false
          IO.select([socket_io], nil, nil, keep_alive_secs)
          next

        in data
          case (msg = Protocol.read_message(Buffer.new(StringIO.new(data))))
          in Protocol::XLogData(lsn:, data:) if auto_keep_alive
            y << msg
            standby_status_update(write_lsn: lsn) if lsn > 0
            last_keep_alive = Time.now

          in Protocol::PrimaryKeepalive(current_lsn:, server_time:, asap: true) if auto_keep_alive
            standby_status_update(write_lsn: current_lsn)
            last_keep_alive = Time.now
            y << msg

          in Protocol::PrimaryKeepalive(current_lsn:)
            y << msg
            @last_confirmed_lsn = [@last_confirmed_lsn, current_lsn].compact.max

          else
            y << msg
          end
        end
      end
    end
    .lazy
end

#wal_receiver_status_intervalObject



112
113
114
115
116
# File 'lib/pg/replication.rb', line 112

def wal_receiver_status_interval
  query("    SELECT setting FROM pg_catalog.pg_settings WHERE name = 'wal_receiver_status_interval'\n  SQL\nend\n").getvalue(0, 0)&.to_i || 10