Class: Pgoutput::Client::Feedback

Inherits:
FeedbackData show all
Defined in:
lib/pgoutput/client/feedback.rb

Overview

Standby status feedback message builder.

Logical replication clients periodically send standby status updates to tell PostgreSQL which WAL location has been received, flushed, and applied. ‘Feedback` models that update and can serialize itself into the CopyData payload expected by the replication protocol.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#applied_lsnInteger (readonly)

latest WAL location applied by the client

Returns:

  • (Integer)

    the current value of applied_lsn



19
20
21
# File 'lib/pgoutput/client/feedback.rb', line 19

def applied_lsn
  @applied_lsn
end

#client_clockInteger (readonly)

PostgreSQL timestamp in microseconds since 2000-01-01 UTC

Returns:

  • (Integer)

    the current value of client_clock



19
20
21
# File 'lib/pgoutput/client/feedback.rb', line 19

def client_clock
  @client_clock
end

#flushed_lsnInteger (readonly)

latest WAL location flushed by the client

Returns:

  • (Integer)

    the current value of flushed_lsn



19
20
21
# File 'lib/pgoutput/client/feedback.rb', line 19

def flushed_lsn
  @flushed_lsn
end

#received_lsnInteger (readonly)

latest WAL location received by the client

Returns:

  • (Integer)

    the current value of received_lsn



19
20
21
# File 'lib/pgoutput/client/feedback.rb', line 19

def received_lsn
  @received_lsn
end

#reply_requestedBoolean (readonly)

whether this feedback is responding to an immediate-reply request

Returns:

  • (Boolean)

    the current value of reply_requested



19
20
21
# File 'lib/pgoutput/client/feedback.rb', line 19

def reply_requested
  @reply_requested
end

Class Method Details

.current_pg_timeInteger

Current PostgreSQL protocol timestamp.

PostgreSQL timestamps in replication messages are expressed as microseconds since 2000-01-01 00:00:00 UTC, not Unix epoch microseconds.

Returns:

  • (Integer)

    microseconds since 2000-01-01 UTC



57
58
59
# File 'lib/pgoutput/client/feedback.rb', line 57

def self.current_pg_time
  ((Time.now.utc - Time.utc(2000, 1, 1)) * 1_000_000).to_i
end

.now(received_lsn:, flushed_lsn: received_lsn, applied_lsn: flushed_lsn, reply_requested: false) ⇒ Feedback

Build feedback using the current wall-clock time.

By default, flushed and applied LSNs follow the received LSN. Callers can pass lower values if they need to distinguish receipt from durable flush or application progress.

Parameters:

  • received_lsn (Integer)

    latest WAL location received by the client

  • flushed_lsn (Integer) (defaults to: received_lsn)

    latest WAL location flushed by the client

  • applied_lsn (Integer) (defaults to: flushed_lsn)

    latest WAL location applied by the client

  • reply_requested (Boolean) (defaults to: false)

    whether this is an immediate reply

Returns:

  • (Feedback)

    immutable feedback value



31
32
33
# File 'lib/pgoutput/client/feedback.rb', line 31

def self.now(received_lsn:, flushed_lsn: received_lsn, applied_lsn: flushed_lsn, reply_requested: false)
  new(received_lsn, flushed_lsn, applied_lsn, current_pg_time, reply_requested)
end

Instance Method Details

#to_copy_dataString

Build a protocol CopyData payload for standby status update.

The payload begins with the standby status update tag ‘r`, followed by three unsigned 64-bit LSN fields, the PostgreSQL timestamp, and a one-byte reply-requested flag.

Returns:

  • (String)

    frozen binary CopyData payload



42
43
44
45
46
47
48
# File 'lib/pgoutput/client/feedback.rb', line 42

def to_copy_data
  (
    "r".b +
    [received_lsn, flushed_lsn, applied_lsn, client_clock].pack("Q>Q>Q>Q>") +
    [reply_requested ? 1 : 0].pack("C")
  ).freeze
end