Packages

Framework-agnostic Elixir CDC consumer for Postgres logical replication (pgoutput) with zero-loss delivery to a pluggable sink — exactly-once for transactional sinks, at-least-once (duplicate-bounded) for non-transactional sinks.

Current section

Files

Jump to
replicant README.md
Raw

README.md

<!--
SPDX-FileCopyrightText: © 2026 BaseLabs Holdings LLC

SPDX-License-Identifier: MIT
-->

[![CI](https://github.com/baselabs/replicant/actions/workflows/ci.yml/badge.svg)](https://github.com/baselabs/replicant/actions/workflows/ci.yml)
[![Hex version](https://img.shields.io/hexpm/v/replicant.svg)](https://hex.pm/packages/replicant)
[![HexDocs](https://img.shields.io/badge/docs-HexDocs-blue)](https://hexdocs.pm/replicant)
[![REUSE](https://img.shields.io/badge/REUSE-lint-blue)](https://github.com/baselabs/replicant/blob/main/.check.exs)
[![MIT](https://img.shields.io/hexpm/l/replicant.svg)](LICENSE)

# Replicant

Replicant is an Elixir library for PostgreSQL change data capture. Use it to
stream committed transactions from a logical replication slot into a sink
you write, starting with an optional snapshot of existing rows.

- Acknowledges delivered changes only after the durable checkpoint advances.
- Supports effect-once writes when the sink commits its changes and checkpoint atomically.
- Applies the publication's column lists and row filters during snapshots and streaming.
- Keeps row values out of its errors and telemetry and stops on unaccepted destructive schema changes.

## Install

Add `{:replicant, "~> 3.0"}` to your Mix dependencies.

## Record committed changes

In a demo database with `wal_level = logical`, run this SQL as the table owner.
For the pipeline, use a role with replication access, read access to the source
table, and read and insert access to the ledger.

```sql
CREATE TABLE orders (id integer PRIMARY KEY, note text);
CREATE PUBLICATION orders_pub FOR TABLE orders;
CREATE TABLE cdc_ledger (commit_lsn numeric(20, 0) PRIMARY KEY, payload bytea NOT NULL);
```

Set `PGHOST`, `PGPORT`, `PGDATABASE`, `PGUSER` and `PGPASSWORD` for the connection.
In your application's IEx session, use a ledger row to commit the change and its
checkpoint in the same write. Use one ledger per source and slot.

```elixir
defmodule OrdersLedger do
  @behaviour Replicant.Sink

  @impl true
  def checkpoint do
    case Postgrex.query!(__MODULE__, "SELECT max(commit_lsn) FROM cdc_ledger", []).rows do
      [[nil]] -> {:ok, nil}
      [[lsn]] -> {:ok, Decimal.to_integer(lsn)}
    end
  end

  @impl true
  def handle_transaction(%Replicant.Transaction{commit_lsn: lsn, changes: changes}) do
    Postgrex.query!(
      __MODULE__,
      """
      INSERT INTO cdc_ledger (commit_lsn, payload)
      SELECT $1, $2
      WHERE NOT EXISTS (SELECT 1 FROM cdc_ledger WHERE commit_lsn >= $1)
      ON CONFLICT (commit_lsn) DO NOTHING
      """,
      [Decimal.new(lsn), :erlang.term_to_binary(changes)]
    )

    {:ok, lsn}
  end
end

connection = [
  hostname: System.fetch_env!("PGHOST"),
  port: String.to_integer(System.get_env("PGPORT", "5432")),
  database: System.fetch_env!("PGDATABASE"),
  username: System.fetch_env!("PGUSER"),
  password: System.fetch_env!("PGPASSWORD")
]

{:ok, _} = Postgrex.start_link(connection ++ [name: OrdersLedger])

{:ok, _} =
  Replicant.start_link(
    connection: connection,
    slot_name: "replicant_orders",
    publication: "orders_pub",
    sink: OrdersLedger,
    go_forward_only: true
  )
```

Once the slot is active, insert an order and check the ledger.

```sql
INSERT INTO orders VALUES (1, 'ready');
SELECT commit_lsn FROM cdc_ledger;
```

## Documentation

- **Tutorials.** [Getting started in Livebook](documentation/tutorials/getting_started.livemd)
  ([run the notebook](https://livebook.dev/run?url=https%3A%2F%2Fraw.githubusercontent.com%2Fbaselabs%2Freplicant%2Fmain%2Fdocumentation%2Ftutorials%2Fgetting_started.livemd)).
- **Topics.** [Delivery guarantees](documentation/topics/delivery/invariants.md),
  [snapshot coverage](documentation/topics/delivery/snapshot-coverage.md), and
  [source decoders](documentation/topics/about/decoders.md).
- **How-to.** [Run a reference pipeline](documentation/how-to/reference-deployment.md),
  [recover a stalled or halted pipeline](documentation/how-to/recover.md), and
  [upgrade a source](documentation/how-to/upgrade.md).
- **Reference.** [Modules and callbacks](https://hexdocs.pm/replicant/api-reference.html).

See [Compatibility](documentation/topics/about/compatibility.md) for supported versions and dependency floors.

## License

[MIT](LICENSE). [NOTICE](NOTICE) records the attribution for vendored code.