1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//! 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
//!
//! ```text
//! ┌─────────────────┐ channel ┌─────────────────┐
//! │ │◄────────────────│ │
//! │ Your App │ ReplicationEvent│ Worker Task │
//! │ │─────────────────►│ │
//! │ │ applied_lsn │ │
//! └─────────────────┘ └────────┬────────┘
//! │
//! │ TCP/TLS
//! ▼
//! ┌─────────────────┐
//! │ PostgreSQL │
//! │ (pgoutput) │
//! └─────────────────┘
//! ```
//!
//! # Example
//!
//! ```no_run
//! 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(())
//! }
//! ```
pub use ReplicationClient;
pub use ;