Skip to main content

Module client

Module client 

Source
Expand description

PostgreSQL logical replication client.

This module provides the main interface for consuming logical replication events from PostgreSQL.

§Overview

The client establishes a replication connection to PostgreSQL and streams change events (inserts, updates, deletes) from the configured publication.

§Architecture

┌─────────────────┐     channel      ┌─────────────────┐
│                 │◄────────────────│                 │
│  Your App       │  ReplicationEvent│  Worker Task    │
│                 │─────────────────►│                 │
│                 │   applied_lsn    │                 │
└─────────────────┘                  └────────┬────────┘
                                              │
                                              │ TCP/TLS
                                              ▼
                                     ┌─────────────────┐
                                     │   PostgreSQL    │
                                     │  (pgoutput)     │
                                     └─────────────────┘

§Example

use pgwire_replication::client::ReplicationClient;
use pgwire_replication::config::ReplicationConfig;
use pgwire_replication::ReplicationEvent;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = ReplicationConfig::new(
        "localhost",
        "replicator",
        "password",
        "mydb",
        "my_slot",
        "my_publication",
    );

    let mut client = ReplicationClient::connect(config).await?;

    while let Some(event) = client.recv().await? {
        match event {
            ReplicationEvent::XLogData { data, wal_end, .. } => {
                // Parse and process pgoutput data
                println!("Received {} bytes at {}", data.len(), wal_end);

                // Report progress to allow WAL cleanup
                client.update_applied_lsn(wal_end);
            }
            ReplicationEvent::KeepAlive { wal_end, .. } => {
                println!("Server heartbeat at {}", wal_end);
            }
            ReplicationEvent::StoppedAt { reached } => {
                println!("Reached stop LSN {}", reached);
                break;
            }
            _ => {}
        }
    }

    Ok(())
}

Structs§

FeedbackObserver
Called after each successful standby status update that advanced the position reported to the server; never for one that repeated it. Runs on the replication worker, so it must be cheap and must not block.
FeedbackSent
A standby status update that told the server about a newer applied position, reported to a FeedbackObserver after the write succeeded.
IdentifySystem
The IDENTIFY_SYSTEM answer of a replication session.
ReplicationClient
PostgreSQL logical replication client.
ReplicationMetrics
Pull-based metrics for a replication stream.
TimelineHistory
The TIMELINE_HISTORY answer: the history file of a timeline.

Enums§

ReplicationEvent
Events emitted by the replication worker.

Type Aliases§

QueryRow
One row of a simple-query result: each column’s text-format value, None for SQL NULL.
ReplicationEventReceiver
Channel receiver type for replication events.