Midwess
Guides

Enable the feature:

cargo add pglite-rs --features replica,multiple-process

Start a replica:

use pglite::{PGlite, Replica, ReplicaConfig, SslMode};

let db = PGlite::open_multi_process("./replica", Default::default()).await?;

let replica = Replica::start(
    db.clone(),
    ReplicaConfig {
        host: "127.0.0.1".into(),
        port: 5432,
        user: "postgres".into(),
        password: "postgres".into(),
        database: "app".into(),
        publication: "app_pub".into(),
        slot_name: "app_slot".into(),
        sslmode: SslMode::Disable,
        ..Default::default()
    }
).await?;

Replica events

let mut rx = replica.subscribe();

while let Some(tx) = rx.recv().await {
    println!("xid={} lsn={}", tx.xid, tx.commit_lsn.to_pg_str());
    for change in &tx.changes {
        println!("{change:?}");
    }
}

CommittedTransaction includes xid, commit_lsn, end_lsn, commit_ts, and row changes.

Row changes

  • Insert { schema, table, row }
  • Update { schema, table, key, row }
  • Delete { schema, table, key }
  • Truncate { schema, table }

Control

Method Purpose
stop() Stop the replication worker.
watermark() Current applied LSN.
is_halted() Whether replication halted on an error.
halt_reason() Halt detail when available.
security_version() Version used by consumers that cache privilege/RLS metadata.
decommission(db, config) Remove replica state for a config.