View Source Postgrex.PgOutput (postgrex_pgoutput v0.2.0)

Postgrex.Pgoutput

CI Hex.pm Version

Encode / decode Postgres replication messages.

usage

Usage

See examples/cdc for a full example of using Postgrex.Replication to implement CDC (Change Data Capture).

installation

Installation

This package can be installed by adding postgrex_pgoutput to your list of dependencies in mix.exs:

def deps do
  [
    {:postgrex_pgoutput, "~> 0.1.0"}
  ]
end

Link to this section Summary

Functions

Decodes a binary replication message into

Decodes a string value into an elixir term based on type.

Encodes status messages to send to Postgres.

Link to this section Functions

@spec decode(binary()) :: term()

Decodes a binary replication message into

examples

Examples

  • Primary keep alive

    import Postgrex.PgOutput.Messages
    
    msg = <<107, 0, 0, 0, 0, 1, 130, 140, 128, 0, 2, 141, 89, 193, 229, 5, 73, 1>>
    msg_primary_keep_alive(reply: 1) = pka = Postgrex.PgOutput.decode(msg)
  • Xlog data

    import Postgrex.PgOutput.Messages
    
    msg = <<119, 0, 0, 0, 0, 1, 94, 109, 184, 0, 0, 0, 0, 1, 94, 109, 184, 0, 2, 141, 89, 243, 12,
      106, 120, 66, 0, 0, 0, 0, 40, 218, 86, 184, 0, 2, 116, 137, 36, 88, 241, 171, 0, 6, 166,
      173>>
    
    msg_xlog_data(data: msg_begin() = begin) = Postgrex.PgOutput.decode(msg)
Link to this function

decode_value(value, type)

View Source
@spec decode_value(binary(), atom()) :: term()

Decodes a string value into an elixir term based on type.

Values in replication messages are always encoded using the text protocol.

examples

Examples

iex> 1 = Postgrex.PgOutput.decode_value("1", "int4")
iex> "string" = Postgrex.PgOutput.decode_value("string", "varchar")

# jsonb & jsonb array requires that :jason is added to deps
iex> %{"a" => "b"} = Postgrex.PgOutput.decode_value(~s({"a": "b"}), "jsonb")
iex> [%{"a" => "b"}] = Postgrex.PgOutput.decode_value("{"{\"a\": \"b\"}"}", "_jsonb")
@spec encode(term()) :: binary()

Encodes status messages to send to Postgres.

examples

Examples

import Postgrex.PgOutput.Messages
<<lsn::64>> = Postgrex.PgOutput.Lsn.encode({0, 1})

msg =
  msg_standby_status_update(
    wal_recv: lsn + 1,
    wal_flush: lsn + 1,
    wal_apply: lsn + 1,
    system_clock: now(),
    reply: 0
  )

bin_msg = Postgrex.PgOutput.encode(msg)