pg_walstream 0.8.0

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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
//! Encoders for the protocol-v1 message set.

use super::wire::{write_cstring, write_tuple_data};
use crate::protocol::{message_types, ColumnInfo, TupleData};
use crate::types::{Oid, TimestampTz, XLogRecPtr, Xid};
use bytes::{BufMut, BytesMut};

/// Emit the in-stream `Int32` transaction id present on data messages inside a
/// streamed transaction (protocol v2+). `None` is the non-streaming framing.
#[inline]
fn write_xid_prefix(buf: &mut BytesMut, in_stream_xid: Option<Xid>) {
    if let Some(xid) = in_stream_xid {
        buf.put_u32(xid);
    }
}

pub(super) fn encode_begin(
    buf: &mut BytesMut,
    final_lsn: XLogRecPtr,
    timestamp: TimestampTz,
    xid: Xid,
) {
    buf.put_u8(message_types::BEGIN);
    buf.put_u64(final_lsn);
    buf.put_i64(timestamp);
    buf.put_u32(xid);
}

pub(super) fn encode_commit(
    buf: &mut BytesMut,
    flags: u8,
    commit_lsn: XLogRecPtr,
    end_lsn: XLogRecPtr,
    timestamp: TimestampTz,
) {
    buf.put_u8(message_types::COMMIT);
    buf.put_u8(flags);
    buf.put_u64(commit_lsn);
    buf.put_u64(end_lsn);
    buf.put_i64(timestamp);
}

pub(super) fn encode_relation(
    buf: &mut BytesMut,
    in_stream_xid: Option<Xid>,
    relation_id: Oid,
    namespace: &str,
    relation_name: &str,
    replica_identity: u8,
    columns: &[ColumnInfo],
) {
    buf.put_u8(message_types::RELATION);
    write_xid_prefix(buf, in_stream_xid);
    buf.put_u32(relation_id);
    write_cstring(buf, namespace);
    write_cstring(buf, relation_name);
    buf.put_u8(replica_identity);
    debug_assert!(
        columns.len() <= u16::MAX as usize,
        "relation column count exceeds int16"
    );
    buf.put_u16(columns.len() as u16);
    for col in columns {
        buf.put_u8(col.flags);
        write_cstring(buf, &col.name);
        buf.put_u32(col.type_id);
        buf.put_i32(col.type_modifier);
    }
}

pub(super) fn encode_insert(
    buf: &mut BytesMut,
    in_stream_xid: Option<Xid>,
    relation_id: Oid,
    tuple: &TupleData,
) {
    buf.put_u8(message_types::INSERT);
    write_xid_prefix(buf, in_stream_xid);
    buf.put_u32(relation_id);
    buf.put_u8(b'N');
    write_tuple_data(buf, tuple);
}

pub(super) fn encode_update(
    buf: &mut BytesMut,
    in_stream_xid: Option<Xid>,
    relation_id: Oid,
    old_tuple: Option<&TupleData>,
    new_tuple: &TupleData,
    key_type: Option<char>,
) {
    buf.put_u8(message_types::UPDATE);
    write_xid_prefix(buf, in_stream_xid);
    buf.put_u32(relation_id);
    // Old-tuple block only with a key/full image. Byte is 'K' or 'O'.
    if let Some(old) = old_tuple {
        let key = key_type.unwrap_or('K');
        debug_assert!(
            key == 'K' || key == 'O',
            "update key type must be 'K' or 'O'"
        );
        buf.put_u8(key as u8);
        write_tuple_data(buf, old);
    }
    buf.put_u8(b'N');
    write_tuple_data(buf, new_tuple);
}

pub(super) fn encode_delete(
    buf: &mut BytesMut,
    in_stream_xid: Option<Xid>,
    relation_id: Oid,
    old_tuple: &TupleData,
    key_type: char,
) {
    buf.put_u8(message_types::DELETE);
    write_xid_prefix(buf, in_stream_xid);
    buf.put_u32(relation_id);
    // Parser accepts any byte here, must fit one byte for the cast.
    debug_assert!(
        (key_type as u32) <= 0xFF,
        "delete key type must fit in a single byte"
    );
    buf.put_u8(key_type as u8);
    write_tuple_data(buf, old_tuple);
}

pub(super) fn encode_truncate(
    buf: &mut BytesMut,
    in_stream_xid: Option<Xid>,
    relation_ids: &[Oid],
    flags: u8,
) {
    buf.put_u8(message_types::TRUNCATE);
    write_xid_prefix(buf, in_stream_xid);
    debug_assert!(
        relation_ids.len() <= u32::MAX as usize,
        "truncate relation count exceeds int32"
    );
    buf.put_u32(relation_ids.len() as u32);
    buf.put_u8(flags);
    for &oid in relation_ids {
        buf.put_u32(oid);
    }
}

pub(super) fn encode_type(
    buf: &mut BytesMut,
    in_stream_xid: Option<Xid>,
    type_id: Oid,
    namespace: &str,
    type_name: &str,
) {
    buf.put_u8(message_types::TYPE);
    write_xid_prefix(buf, in_stream_xid);
    buf.put_u32(type_id);
    write_cstring(buf, namespace);
    write_cstring(buf, type_name);
}

pub(super) fn encode_origin(buf: &mut BytesMut, origin_lsn: XLogRecPtr, origin_name: &str) {
    buf.put_u8(message_types::ORIGIN);
    buf.put_u64(origin_lsn);
    write_cstring(buf, origin_name);
}

pub(super) fn encode_logical_message(
    buf: &mut BytesMut,
    in_stream_xid: Option<Xid>,
    flags: u8,
    lsn: XLogRecPtr,
    prefix: &str,
    content: &[u8],
) {
    buf.put_u8(message_types::MESSAGE);
    write_xid_prefix(buf, in_stream_xid);
    buf.put_u8(flags);
    buf.put_u64(lsn);
    write_cstring(buf, prefix);
    debug_assert!(
        content.len() <= u32::MAX as usize,
        "message content exceeds int32"
    );
    buf.put_u32(content.len() as u32);
    buf.put_slice(content);
}

#[cfg(test)]
mod tests {
    use crate::protocol::{ColumnData, ColumnInfo, LogicalReplicationMessage as M, TupleData};
    use bytes::{Bytes, BytesMut};
    use std::sync::Arc;

    fn encode(msg: &M, version: u8) -> Vec<u8> {
        let mut buf = BytesMut::new();
        crate::pgoutput_encode::encode_message(msg, version, &mut buf);
        buf.to_vec()
    }

    #[test]
    fn encode_begin_matches_spec_bytes() {
        let msg = M::Begin {
            final_lsn: 0x0000_0000_0100_0000,
            timestamp: 1_700_000_000_000_000,
            xid: 42,
        };
        let mut expected = vec![b'B'];
        expected.extend_from_slice(&0x0000_0000_0100_0000u64.to_be_bytes());
        expected.extend_from_slice(&1_700_000_000_000_000i64.to_be_bytes());
        expected.extend_from_slice(&42u32.to_be_bytes());
        assert_eq!(encode(&msg, 1), expected);
    }

    #[test]
    fn encode_commit_matches_spec_bytes() {
        let msg = M::Commit {
            flags: 0,
            commit_lsn: 0x0000_0000_0100_0000,
            end_lsn: 0x0000_0000_0100_0020,
            timestamp: 1_700_000_000_000_000,
        };
        let mut expected = vec![b'C', 0u8];
        expected.extend_from_slice(&0x0000_0000_0100_0000u64.to_be_bytes());
        expected.extend_from_slice(&0x0000_0000_0100_0020u64.to_be_bytes());
        expected.extend_from_slice(&1_700_000_000_000_000i64.to_be_bytes());
        assert_eq!(encode(&msg, 1), expected);
    }

    #[test]
    fn encode_origin_matches_spec_bytes() {
        let msg = M::Origin {
            origin_lsn: 0x0000_0000_1234_5678,
            origin_name: "pg_origin".to_string(),
        };
        let mut expected = vec![b'O'];
        expected.extend_from_slice(&0x0000_0000_1234_5678u64.to_be_bytes());
        expected.extend_from_slice(b"pg_origin\0");
        assert_eq!(encode(&msg, 1), expected);
    }

    #[test]
    fn encode_relation_matches_spec_bytes() {
        let msg = M::Relation {
            relation_id: 0x0001_0203,
            namespace: Arc::from("public"),
            relation_name: Arc::from("users"),
            replica_identity: b'd',
            columns: vec![
                ColumnInfo::new(1, "id".to_string(), 23, -1),
                ColumnInfo::new(0, "name".to_string(), 25, -1),
            ],
        };
        let mut expected = vec![b'R'];
        expected.extend_from_slice(&0x0001_0203u32.to_be_bytes());
        expected.extend_from_slice(b"public\0");
        expected.extend_from_slice(b"users\0");
        expected.push(b'd');
        expected.extend_from_slice(&2u16.to_be_bytes());
        expected.push(1);
        expected.extend_from_slice(b"id\0");
        expected.extend_from_slice(&23u32.to_be_bytes());
        expected.extend_from_slice(&(-1i32).to_be_bytes());
        expected.push(0);
        expected.extend_from_slice(b"name\0");
        expected.extend_from_slice(&25u32.to_be_bytes());
        expected.extend_from_slice(&(-1i32).to_be_bytes());
        assert_eq!(encode(&msg, 1), expected);
    }

    #[test]
    fn encode_type_matches_spec_bytes() {
        let msg = M::Type {
            type_id: 0x0000_4567,
            namespace: "public".to_string(),
            type_name: "mood".to_string(),
        };
        let mut expected = vec![b'Y'];
        expected.extend_from_slice(&0x0000_4567u32.to_be_bytes());
        expected.extend_from_slice(b"public\0");
        expected.extend_from_slice(b"mood\0");
        assert_eq!(encode(&msg, 1), expected);
    }

    #[test]
    fn encode_insert_matches_spec_bytes() {
        let tuple = TupleData::new(vec![ColumnData::text(b"42".to_vec()), ColumnData::null()]);
        let msg = M::Insert {
            relation_id: 0x0000_002A,
            tuple,
        };
        let mut expected = vec![b'I'];
        expected.extend_from_slice(&0x0000_002Au32.to_be_bytes());
        expected.push(b'N');
        expected.extend_from_slice(&2u16.to_be_bytes());
        expected.push(b't');
        expected.extend_from_slice(&2u32.to_be_bytes());
        expected.extend_from_slice(b"42");
        expected.push(b'n');
        assert_eq!(encode(&msg, 1), expected);
    }

    #[test]
    fn encode_update_default_identity_matches_spec_bytes() {
        // 'K' old-tuple block (REPLICA IDENTITY DEFAULT), with an unchanged-TOAST
        // column in the old image, then the 'N' new tuple.
        let old = TupleData::new(vec![
            ColumnData::text(b"1".to_vec()),
            ColumnData::unchanged(),
        ]);
        let new = TupleData::new(vec![
            ColumnData::text(b"1".to_vec()),
            ColumnData::text(b"x".to_vec()),
        ]);
        let msg = M::Update {
            relation_id: 7,
            old_tuple: Some(old),
            new_tuple: new,
            key_type: Some('K'),
        };
        let mut expected = vec![b'U'];
        expected.extend_from_slice(&7u32.to_be_bytes());
        expected.push(b'K');
        expected.extend_from_slice(&2u16.to_be_bytes());
        expected.push(b't');
        expected.extend_from_slice(&1u32.to_be_bytes());
        expected.extend_from_slice(b"1");
        expected.push(b'u');
        expected.push(b'N');
        expected.extend_from_slice(&2u16.to_be_bytes());
        expected.push(b't');
        expected.extend_from_slice(&1u32.to_be_bytes());
        expected.extend_from_slice(b"1");
        expected.push(b't');
        expected.extend_from_slice(&1u32.to_be_bytes());
        expected.extend_from_slice(b"x");
        assert_eq!(encode(&msg, 1), expected);
    }

    #[test]
    fn encode_update_full_identity_matches_spec_bytes() {
        let old = TupleData::new(vec![ColumnData::text(b"1".to_vec())]);
        let new = TupleData::new(vec![ColumnData::text(b"2".to_vec())]);
        let msg = M::Update {
            relation_id: 7,
            old_tuple: Some(old),
            new_tuple: new,
            key_type: Some('O'),
        };
        let mut expected = vec![b'U'];
        expected.extend_from_slice(&7u32.to_be_bytes());
        expected.push(b'O');
        expected.extend_from_slice(&1u16.to_be_bytes());
        expected.push(b't');
        expected.extend_from_slice(&1u32.to_be_bytes());
        expected.extend_from_slice(b"1");
        expected.push(b'N');
        expected.extend_from_slice(&1u16.to_be_bytes());
        expected.push(b't');
        expected.extend_from_slice(&1u32.to_be_bytes());
        expected.extend_from_slice(b"2");
        assert_eq!(encode(&msg, 1), expected);
    }

    #[test]
    fn encode_update_without_old_tuple_matches_spec_bytes() {
        let new = TupleData::new(vec![ColumnData::text(b"9".to_vec())]);
        let msg = M::Update {
            relation_id: 7,
            old_tuple: None,
            new_tuple: new,
            key_type: None,
        };
        let mut expected = vec![b'U'];
        expected.extend_from_slice(&7u32.to_be_bytes());
        expected.push(b'N');
        expected.extend_from_slice(&1u16.to_be_bytes());
        expected.push(b't');
        expected.extend_from_slice(&1u32.to_be_bytes());
        expected.extend_from_slice(b"9");
        assert_eq!(encode(&msg, 1), expected);
    }

    #[test]
    fn encode_delete_default_identity_matches_spec_bytes() {
        let old = TupleData::new(vec![ColumnData::text(b"1".to_vec())]);
        let msg = M::Delete {
            relation_id: 7,
            old_tuple: old,
            key_type: 'K',
        };
        let mut expected = vec![b'D'];
        expected.extend_from_slice(&7u32.to_be_bytes());
        expected.push(b'K');
        expected.extend_from_slice(&1u16.to_be_bytes());
        expected.push(b't');
        expected.extend_from_slice(&1u32.to_be_bytes());
        expected.extend_from_slice(b"1");
        assert_eq!(encode(&msg, 1), expected);
    }

    #[test]
    fn encode_delete_full_identity_matches_spec_bytes() {
        let old = TupleData::new(vec![ColumnData::binary(vec![0xDE, 0xAD])]);
        let msg = M::Delete {
            relation_id: 7,
            old_tuple: old,
            key_type: 'O',
        };
        let mut expected = vec![b'D'];
        expected.extend_from_slice(&7u32.to_be_bytes());
        expected.push(b'O');
        expected.extend_from_slice(&1u16.to_be_bytes());
        expected.push(b'b');
        expected.extend_from_slice(&2u32.to_be_bytes());
        expected.extend_from_slice(&[0xDE, 0xAD]);
        assert_eq!(encode(&msg, 1), expected);
    }

    #[test]
    fn encode_truncate_matches_spec_bytes() {
        let msg = M::Truncate {
            relation_ids: vec![10, 20],
            flags: 0b11, // CASCADE | RESTART IDENTITY
        };
        let mut expected = vec![b'T'];
        expected.extend_from_slice(&2u32.to_be_bytes());
        expected.push(0b11);
        expected.extend_from_slice(&10u32.to_be_bytes());
        expected.extend_from_slice(&20u32.to_be_bytes());
        assert_eq!(encode(&msg, 1), expected);
    }

    #[test]
    fn encode_message_msg_matches_spec_bytes() {
        let msg = M::Message {
            flags: 1,
            lsn: 0x0000_0000_0000_1000,
            prefix: "test".to_string(),
            content: Bytes::from_static(&[0xDE, 0xAD, 0xBE, 0xEF]),
        };
        let mut expected = vec![b'M', 1u8];
        expected.extend_from_slice(&0x0000_0000_0000_1000u64.to_be_bytes());
        expected.extend_from_slice(b"test\0");
        expected.extend_from_slice(&4u32.to_be_bytes());
        expected.extend_from_slice(&[0xDE, 0xAD, 0xBE, 0xEF]);
        assert_eq!(encode(&msg, 1), expected);
    }

    #[test]
    fn data_message_framing_is_version_independent() {
        // Data messages carry no leading xid prefix in this surface, so the bytes
        // are identical across protocol versions (the in-stream prefix is context,
        // not part of the message value).
        let msg = M::Insert {
            relation_id: 1,
            tuple: TupleData::new(vec![ColumnData::text(b"abc".to_vec())]),
        };
        assert_eq!(encode(&msg, 1), encode(&msg, 2));
        assert_eq!(encode(&msg, 1), encode(&msg, 4));
    }
}