pg_walstream 0.6.3

PostgreSQL logical replication protocol library - parse and handle PostgreSQL WAL streaming messages
Documentation
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//! Integration tests for the EXPORT_SNAPSHOT workflow.
//!
//! These tests verify the PostgreSQL snapshot lifecycle when using
//! `ensure_replication_slot()` with `EXPORT_SNAPSHOT`. They require a live
//! PostgreSQL instance configured for logical replication.
//!
//! ## Prerequisites
//!
//! - PostgreSQL 14+ with `wal_level = logical`
//! - A user with replication privileges
//! - Environment variable `DATABASE_URL` pointing to a replication-enabled connection
//!   (e.g. `postgresql://postgres:postgres@localhost:5432/test_walstream?replication=database`)
//! - Environment variable `DATABASE_URL_REGULAR` pointing to a regular (non-replication)
//!   connection to the same database (e.g. `postgresql://postgres:postgres@localhost:5432/test_walstream`)
//!
//! ## Running Locally
//!
//! ```bash
//! export DATABASE_URL="postgresql://postgres:postgres@localhost:5432/test_walstream?replication=database"
//! export DATABASE_URL_REGULAR="postgresql://postgres:postgres@localhost:5432/test_walstream"
//! cargo test --test snapshot_export -- --ignored
//! ```

use pg_walstream::{
    CancellationToken, LogicalReplicationStream, PgReplicationConnection, ReplicationSlotOptions,
    ReplicationStreamConfig, RetryConfig, StreamingMode,
};
use std::time::Duration;

/// Return the replication connection string from environment, or skip.
fn replication_conn_string() -> String {
    std::env::var("DATABASE_URL").unwrap_or_else(|_| {
        "postgresql://postgres:postgres@localhost:5432/test_walstream?replication=database"
            .to_string()
    })
}

/// Return the regular (non-replication) connection string from environment, or derive it.
fn regular_conn_string() -> String {
    std::env::var("DATABASE_URL_REGULAR").unwrap_or_else(|_| {
        // Strip `replication=database` from the replication connection string
        let repl = replication_conn_string();
        repl.replace("?replication=database", "")
            .replace("&replication=database", "")
    })
}

/// Create a helper config with a unique slot name to avoid collisions between tests.
fn test_config(slot_name: &str) -> ReplicationStreamConfig {
    ReplicationStreamConfig::new(
        slot_name.to_string(),
        "test_pub".to_string(),
        2,
        StreamingMode::On,
        Duration::from_secs(10),
        Duration::from_secs(30),
        Duration::from_secs(60),
        RetryConfig::default(),
    )
    .with_slot_options(ReplicationSlotOptions {
        temporary: true,
        snapshot: Some("export".to_string()),
        ..Default::default()
    })
}

/// Helper: set up the test schema (idempotent).
fn setup_schema(regular_conn: &mut PgReplicationConnection) {
    // Create table if not exists
    let _ = regular_conn.exec(
        "CREATE TABLE IF NOT EXISTS snapshot_test (id SERIAL PRIMARY KEY, name TEXT NOT NULL)",
    );

    // Clear and seed data
    let _ = regular_conn.exec("TRUNCATE snapshot_test RESTART IDENTITY");
    let _ = regular_conn
        .exec("INSERT INTO snapshot_test (name) VALUES ('alice'), ('bob'), ('charlie')");

    // Create publication if not exists (ignore errors if it already exists)
    let _ = regular_conn.exec("DROP PUBLICATION IF EXISTS test_pub");
    let _ = regular_conn.exec("CREATE PUBLICATION test_pub FOR TABLE snapshot_test");
}

/// Helper: clean up a replication slot (best-effort).
fn drop_slot(slot_name: &str) {
    if let Ok(mut conn) = PgReplicationConnection::connect(&replication_conn_string()) {
        let _ = conn.exec(&format!(
            "SELECT pg_drop_replication_slot('{}') WHERE EXISTS (SELECT 1 FROM pg_replication_slots WHERE slot_name = '{}')",
            slot_name, slot_name
        ));
    }
}

#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn test_ensure_slot_returns_snapshot_name() {
    let slot = "it_snap_name";
    drop_slot(slot);

    let mut regular =
        PgReplicationConnection::connect(&regular_conn_string()).expect("regular connection");
    setup_schema(&mut regular);

    let config = test_config(slot);
    let mut stream = LogicalReplicationStream::new(&replication_conn_string(), config)
        .await
        .expect("replication stream");

    stream
        .ensure_replication_slot()
        .await
        .expect("ensure_replication_slot");

    let snap = stream.exported_snapshot_name();
    assert!(
        snap.is_some(),
        "exported_snapshot_name() must be Some after EXPORT_SNAPSHOT"
    );
    let snap_name = snap.unwrap();
    assert!(!snap_name.is_empty(), "snapshot name must not be empty");
    println!("Exported snapshot: {snap_name}");
}

/// Verify the full snapshot workflow:
///   1. `ensure_replication_slot()` → snapshot name
///   2. Use the snapshot on a **separate regular connection** to read consistent data
///   3. `start()` proceeds without re-creating the slot
#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn test_snapshot_readable_before_start() {
    let slot = "it_snap_read";
    drop_slot(slot);

    let mut regular =
        PgReplicationConnection::connect(&regular_conn_string()).expect("regular connection");
    setup_schema(&mut regular);

    let config = test_config(slot);
    let mut stream = LogicalReplicationStream::new(&replication_conn_string(), config)
        .await
        .expect("replication stream");

    // Step 1: create slot, get snapshot
    stream
        .ensure_replication_slot()
        .await
        .expect("ensure_replication_slot");

    let snap_name = stream
        .exported_snapshot_name()
        .expect("snapshot must exist");
    println!("Snapshot: {snap_name}");

    // Step 2: open a separate regular connection, import the snapshot, read data
    let mut reader = PgReplicationConnection::connect(&regular_conn_string())
        .expect("snapshot reader connection");

    reader
        .exec("BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ")
        .expect("BEGIN");

    reader
        .exec(&format!("SET TRANSACTION SNAPSHOT '{snap_name}'"))
        .expect("SET TRANSACTION SNAPSHOT must succeed before START_REPLICATION");

    let result = reader
        .exec("SELECT id, name FROM snapshot_test ORDER BY id")
        .expect("SELECT");

    let row_count = result.ntuples();
    assert_eq!(row_count, 3, "expected 3 seeded rows, got {row_count}");

    // Verify actual data
    assert_eq!(result.get_value(0, 1).as_deref(), Some("alice"));
    assert_eq!(result.get_value(1, 1).as_deref(), Some("bob"));
    assert_eq!(result.get_value(2, 1).as_deref(), Some("charlie"));

    reader.exec("COMMIT").expect("COMMIT");

    // Step 3: start streaming — this should NOT re-create the slot
    stream.start(None).await.expect("start");

    println!("Stream started successfully after snapshot read — workflow complete");
}

/// Verify that the snapshot becomes **invalid** after `start()` issues
/// `START_REPLICATION`.
#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn test_snapshot_invalid_after_start() {
    let slot = "it_snap_invalid";
    drop_slot(slot);

    let mut regular =
        PgReplicationConnection::connect(&regular_conn_string()).expect("regular connection");
    setup_schema(&mut regular);

    let config = test_config(slot);
    let mut stream = LogicalReplicationStream::new(&replication_conn_string(), config)
        .await
        .expect("replication stream");

    stream
        .ensure_replication_slot()
        .await
        .expect("ensure_replication_slot");

    let snap_name = stream
        .exported_snapshot_name()
        .expect("snapshot must exist")
        .to_string();

    // Start replication — this destroys the snapshot
    stream.start(None).await.expect("start");

    // Now try to use the snapshot on a separate connection — it MUST fail
    let mut reader = PgReplicationConnection::connect(&regular_conn_string())
        .expect("snapshot reader connection");

    reader
        .exec("BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ")
        .expect("BEGIN");

    let result = reader.exec(&format!("SET TRANSACTION SNAPSHOT '{snap_name}'"));
    assert!(
        result.is_err(),
        "SET TRANSACTION SNAPSHOT must fail after START_REPLICATION, \
         but it succeeded. The snapshot should have been destroyed."
    );

    let _ = reader.exec("ROLLBACK");
    println!("Confirmed: snapshot '{snap_name}' is invalid after start()");
}

/// Verify that `ensure_replication_slot()` is idempotent — calling it twice
/// is a no-op the second time.
#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn test_ensure_slot_idempotent() {
    let slot = "it_snap_idempotent";
    drop_slot(slot);

    let mut regular =
        PgReplicationConnection::connect(&regular_conn_string()).expect("regular connection");
    setup_schema(&mut regular);

    let config = test_config(slot);
    let mut stream = LogicalReplicationStream::new(&replication_conn_string(), config)
        .await
        .expect("replication stream");

    // First call — creates the slot
    stream
        .ensure_replication_slot()
        .await
        .expect("first ensure_replication_slot");

    let snap1 = stream
        .exported_snapshot_name()
        .expect("snapshot from first call")
        .to_string();

    // Second call — should be a no-op (slot_created is true)
    stream
        .ensure_replication_slot()
        .await
        .expect("second ensure_replication_slot");

    let snap2 = stream
        .exported_snapshot_name()
        .expect("snapshot after second call");

    assert_eq!(
        snap1, snap2,
        "snapshot name must remain the same after idempotent call"
    );
}

/// Verify that `NOEXPORT_SNAPSHOT` results in `exported_snapshot_name()` returning `None`.
#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn test_noexport_snapshot_returns_none() {
    let slot = "it_snap_noexport";
    drop_slot(slot);

    let mut regular =
        PgReplicationConnection::connect(&regular_conn_string()).expect("regular connection");
    setup_schema(&mut regular);

    let config = ReplicationStreamConfig::new(
        slot.to_string(),
        "test_pub".to_string(),
        2,
        StreamingMode::On,
        Duration::from_secs(10),
        Duration::from_secs(30),
        Duration::from_secs(60),
        RetryConfig::default(),
    )
    .with_slot_options(ReplicationSlotOptions {
        temporary: true,
        snapshot: Some("nothing".to_string()), // NOEXPORT_SNAPSHOT
        ..Default::default()
    });

    let mut stream = LogicalReplicationStream::new(&replication_conn_string(), config)
        .await
        .expect("replication stream");

    stream
        .ensure_replication_slot()
        .await
        .expect("ensure_replication_slot");

    // NOEXPORT_SNAPSHOT should produce either None or an empty string
    let snap = stream.exported_snapshot_name();
    assert!(
        snap.is_none() || snap.is_some_and(str::is_empty),
        "No snapshot should be exported with NOEXPORT_SNAPSHOT, got: {snap:?}"
    );
}

/// End-to-end: snapshot read + stream a few events (insert during the test),
/// confirming no data gap between snapshot and stream.
#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn test_snapshot_and_stream_consistency() {
    let slot = "it_snap_consistency";
    drop_slot(slot);

    let mut regular =
        PgReplicationConnection::connect(&regular_conn_string()).expect("regular connection");
    setup_schema(&mut regular);

    let config = test_config(slot);
    let mut stream = LogicalReplicationStream::new(&replication_conn_string(), config)
        .await
        .expect("replication stream");

    // Step 1: create slot
    stream
        .ensure_replication_slot()
        .await
        .expect("ensure_replication_slot");

    let snap_name = stream
        .exported_snapshot_name()
        .expect("snapshot must exist");

    // Step 2: read initial state through the snapshot
    let mut reader = PgReplicationConnection::connect(&regular_conn_string())
        .expect("snapshot reader connection");

    reader
        .exec("BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ")
        .expect("BEGIN");
    reader
        .exec(&format!("SET TRANSACTION SNAPSHOT '{snap_name}'"))
        .expect("SET TRANSACTION SNAPSHOT");

    let snapshot_rows = reader
        .exec("SELECT count(*) FROM snapshot_test")
        .expect("SELECT count");
    let count_str = snapshot_rows.get_value(0, 0).unwrap_or_default();
    let initial_count: i32 = count_str.parse().unwrap_or(0);
    assert_eq!(initial_count, 3, "expected 3 rows in snapshot");

    reader.exec("COMMIT").expect("COMMIT");

    // Insert new data AFTER the snapshot was taken but BEFORE start()
    regular
        .exec("INSERT INTO snapshot_test (name) VALUES ('dave')")
        .expect("INSERT dave");

    // Step 3: start streaming
    stream.start(None).await.expect("start");

    let cancel_token = CancellationToken::new();
    let cancel_clone = cancel_token.clone();

    // Auto-cancel after 5 seconds to avoid hanging
    tokio::spawn(async move {
        tokio::time::sleep(Duration::from_secs(5)).await;
        cancel_clone.cancel();
    });

    // Trigger another insert to make sure we get at least one event
    // (dave's insert happened before START_REPLICATION, so it will
    // arrive as a streamed event)
    regular
        .exec("INSERT INTO snapshot_test (name) VALUES ('eve')")
        .expect("INSERT eve");

    let mut event_count = 0u32;
    loop {
        match stream.next_event(&cancel_token).await {
            Ok(event) => {
                event_count += 1;
                println!("Event #{event_count}: {:?}", event.event_type);
                stream
                    .shared_lsn_feedback
                    .update_applied_lsn(event.lsn.value());
                // We just need to confirm we can receive events — break early
                if event_count >= 1 {
                    break;
                }
            }
            Err(pg_walstream::ReplicationError::Cancelled(_)) => {
                println!("Stream cancelled after {event_count} events");
                break;
            }
            Err(e) => {
                panic!("Unexpected stream error: {e}");
            }
        }
    }

    assert!(
        event_count >= 1,
        "Expected at least 1 streamed event, got {event_count}"
    );
    println!("Snapshot + stream consistency test passed: {initial_count} initial rows, {event_count} streamed events");
}