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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//! # PostgreSQL Logical Replication Protocol Library
//!
//! A platform-agnostic library for parsing and streaming PostgreSQL logical replication
//! protocol messages. The protocol parser is reusable on its own, and the crate also
//! includes a libpq-based connection layer for replication streaming.
//!
//! ## Features
//!
//! - Full PostgreSQL logical replication protocol support (versions 1-4)
//! - Streaming transaction support (protocol v2+)
//! - Two-phase commit support (protocol v3+)
//! - Parallel streaming support (protocol v4+)
//! - Zero-copy buffer operations using `bytes` crate
//! - Thread-safe LSN tracking
//! - **Truly async, non-blocking I/O** - Tasks properly yield to the executor
//! - **`futures::Stream` trait implementation** - Works with all stream combinators
//! - **Graceful cancellation** - All operations support cancellation tokens
//! - Protocol parsing is portable; the connection module uses libpq
//!
//! ## Async Stream API
//!
//! The `EventStream` type implements both:
//! - A native `.next_event().await` API for simple usage without trait imports
//! - The [`futures_core::Stream`] trait for use with stream combinators
//!
//! ```ignore
//! use futures::StreamExt;
//!
//! let mut event_stream = stream.into_stream(cancel_token);
//!
//! // Use as a futures::Stream with combinators
//! while let Some(result) = event_stream.next().await {
//! let event = result?;
//! println!("Event: {:?}", event);
//! event_stream.update_applied_lsn(event.lsn.value());
//! }
//! ```
//!
//! ## Async I/O Performance
//!
//! The library implements proper async I/O patterns that allow tokio to efficiently
//! schedule tasks without blocking threads:
//!
//! - When waiting for data from PostgreSQL, the task is suspended and the thread
//! is released back to the executor to run other tasks
//! - Uses `AsyncFd` with proper edge-triggered readiness handling
//! - Zero-copy `Bytes` throughout the WAL data path
//! - Supports concurrent processing of multiple replication streams on a single thread
//! - Enables efficient resource utilization in high-concurrency scenarios
//!
//! ## Protocol Support
//!
//! This library implements the PostgreSQL logical replication protocol as documented at:
//! - <https://www.postgresql.org/docs/current/protocol-logical-replication.html>
//! - <https://www.postgresql.org/docs/current/protocol-logicalrep-message-formats.html>
//! - <https://www.postgresql.org/docs/current/protocol-replication.html>
//!
//! ## Quick Start
//!
//! ```ignore
//! use pg_walstream::{
//! LogicalReplicationStream, ReplicationStreamConfig, RetryConfig,
//! SharedLsnFeedback, CancellationToken,
//! };
//! use std::sync::Arc;
//! use std::time::Duration;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let config = ReplicationStreamConfig::new(
//! "my_slot".to_string(),
//! "my_publication".to_string(),
//! 2,
//! StreamingMode::On,
//! Duration::from_secs(10),
//! Duration::from_secs(30),
//! Duration::from_secs(60),
//! RetryConfig::default(),
//! );
//!
//! let mut stream = LogicalReplicationStream::new(
//! "postgresql://postgres:password@localhost:5432/mydb?replication=database",
//! config,
//! ).await?;
//!
//! // Optional: create the slot first to use the exported snapshot
//! // for an initial consistent table read (before start() destroys it).
//! // stream.ensure_replication_slot().await?;
//! // if let Some(snap) = stream.exported_snapshot_name() { /* read snapshot */ }
//!
//! stream.start(None).await?;
//!
//! let cancel_token = CancellationToken::new();
//! let mut event_stream = stream.into_stream(cancel_token.clone());
//!
//! // Option 1: Use as futures::Stream
//! // use futures::StreamExt;
//! // while let Some(result) = event_stream.next().await { ... }
//!
//! // Option 2: Use native API
//! loop {
//! match event_stream.next_event().await {
//! Ok(event) => {
//! println!("Received event: {:?}", event);
//! event_stream.update_applied_lsn(event.lsn.value());
//! }
//! Err(e) if matches!(e, pg_walstream::ReplicationError::Cancelled(_)) => {
//! println!("Cancelled, shutting down gracefully");
//! break;
//! }
//! Err(e) => {
//! eprintln!("Error: {}", e);
//! break;
//! }
//! }
//! }
//!
//! // Graceful shutdown
//! event_stream.shutdown().await?;
//!
//! Ok(())
//! }
//! ```
// Core modules
// Protocol implementation
// High-level stream management
// Re-export main types for convenience
pub use ;
pub use ;
pub use SharedLsnFeedback;
// Re-export column value types
pub use ;
// Re-export deserializer
pub use ;
// Re-export type aliases and utilities
pub use ;
// Re-export protocol types
pub use ;
// Re-export stream types
pub use ;
// Re-export tokio_util for CancellationToken
pub use CancellationToken;
// Re-export libpq-specific types
pub use ;
// Re-export retry types
pub use ;
// Re-export SQL builder utilities
pub use ;