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
//! Typed payloads carried inside walsender `CopyData` messages.
use std::io;
use bytes::{Buf, BufMut, Bytes, BytesMut};
#[derive(Clone, Debug, Eq, PartialEq)]
/// A typed payload sent by a walsender inside backend `CopyData`.
pub enum BackendReplication {
/// A range of write-ahead log data.
XLogData {
/// WAL position of the first byte in `data`.
wal_start: u64,
/// Current end of WAL on the server.
wal_end: u64,
/// Server clock as microseconds since 2000-01-01 UTC.
server_time: i64,
/// WAL bytes.
data: Bytes,
},
/// A primary keepalive message.
PrimaryKeepalive {
/// Current end of WAL on the server.
wal_end: u64,
/// Server clock as microseconds since 2000-01-01 UTC.
server_time: i64,
/// Whether the server requests an immediate status reply.
reply_requested: bool,
},
/// An extension payload whose tag is not recognised by this crate.
Unknown {
/// Replication sub-message tag.
tag: u8,
/// Bytes following the tag.
body: Bytes,
},
}
#[derive(Clone, Debug, Eq, PartialEq)]
/// A typed payload sent by a standby inside frontend `CopyData`.
pub enum FrontendReplication {
/// The standby's WAL receipt, flush, and replay positions.
StandbyStatus {
/// Last WAL position written locally.
written: u64,
/// Last WAL position flushed durably.
flushed: u64,
/// Last WAL position applied during replay.
applied: u64,
/// Client clock as microseconds since 2000-01-01 UTC.
client_time: i64,
/// Whether the standby requests an immediate keepalive reply.
reply_requested: bool,
},
/// Transaction horizons used to prevent premature vacuuming on the primary.
HotStandbyFeedback {
/// Client clock as microseconds since 2000-01-01 UTC.
client_time: i64,
/// Oldest transaction identifier still needed by the standby.
xmin: u32,
/// Epoch disambiguating wraparound of `xmin`.
xmin_epoch: u32,
/// Oldest catalog transaction identifier still needed by the standby.
catalog_xmin: u32,
/// Epoch disambiguating wraparound of `catalog_xmin`.
catalog_xmin_epoch: u32,
},
/// An extension payload whose tag is not recognised by this crate.
Unknown {
/// Replication sub-message tag.
tag: u8,
/// Bytes following the tag.
body: Bytes,
},
}
impl BackendReplication {
/// Decodes a backend walsender payload while preserving extension messages.
///
/// # Errors
///
/// Returns an error for a truncated known message or an invalid Boolean byte.
pub fn decode(mut payload: Bytes) -> io::Result<Self> {
let tag = take_tag(&mut payload)?;
match tag {
b'w' => {
require(&payload, 24, "truncated XLogData")?;
Ok(Self::XLogData {
wal_start: payload.get_u64(),
wal_end: payload.get_u64(),
server_time: payload.get_i64(),
data: payload,
})
}
b'k' => {
require_exact(&payload, 17, "invalid primary keepalive length")?;
let wal_end = payload.get_u64();
let server_time = payload.get_i64();
let reply_requested = take_bool(payload.get_u8())?;
Ok(Self::PrimaryKeepalive {
wal_end,
server_time,
reply_requested,
})
}
tag => Ok(Self::Unknown { tag, body: payload }),
}
}
#[must_use]
/// Encodes this value as a backend replication sub-message.
pub fn encode(&self) -> Bytes {
let mut output = BytesMut::new();
match self {
Self::XLogData {
wal_start,
wal_end,
server_time,
data,
} => {
output.put_u8(b'w');
output.put_u64(*wal_start);
output.put_u64(*wal_end);
output.put_i64(*server_time);
output.extend_from_slice(data);
}
Self::PrimaryKeepalive {
wal_end,
server_time,
reply_requested,
} => {
output.put_u8(b'k');
output.put_u64(*wal_end);
output.put_i64(*server_time);
output.put_u8(u8::from(*reply_requested));
}
Self::Unknown { tag, body } => {
output.put_u8(*tag);
output.extend_from_slice(body);
}
}
output.freeze()
}
}
impl FrontendReplication {
/// Decodes a standby payload while preserving extension messages.
///
/// # Errors
///
/// Returns an error for a truncated known message or an invalid Boolean byte.
pub fn decode(mut payload: Bytes) -> io::Result<Self> {
let tag = take_tag(&mut payload)?;
match tag {
b'r' => {
require_exact(&payload, 33, "invalid standby status length")?;
let written = payload.get_u64();
let flushed = payload.get_u64();
let applied = payload.get_u64();
let client_time = payload.get_i64();
let reply_requested = take_bool(payload.get_u8())?;
Ok(Self::StandbyStatus {
written,
flushed,
applied,
client_time,
reply_requested,
})
}
b'h' => {
require_exact(&payload, 24, "invalid hot standby feedback length")?;
Ok(Self::HotStandbyFeedback {
client_time: payload.get_i64(),
xmin: payload.get_u32(),
xmin_epoch: payload.get_u32(),
catalog_xmin: payload.get_u32(),
catalog_xmin_epoch: payload.get_u32(),
})
}
tag => Ok(Self::Unknown { tag, body: payload }),
}
}
#[must_use]
/// Encodes this value as a frontend replication sub-message.
pub fn encode(&self) -> Bytes {
let mut output = BytesMut::new();
match self {
Self::StandbyStatus {
written,
flushed,
applied,
client_time,
reply_requested,
} => {
output.put_u8(b'r');
output.put_u64(*written);
output.put_u64(*flushed);
output.put_u64(*applied);
output.put_i64(*client_time);
output.put_u8(u8::from(*reply_requested));
}
Self::HotStandbyFeedback {
client_time,
xmin,
xmin_epoch,
catalog_xmin,
catalog_xmin_epoch,
} => {
output.put_u8(b'h');
output.put_i64(*client_time);
output.put_u32(*xmin);
output.put_u32(*xmin_epoch);
output.put_u32(*catalog_xmin);
output.put_u32(*catalog_xmin_epoch);
}
Self::Unknown { tag, body } => {
output.put_u8(*tag);
output.extend_from_slice(body);
}
}
output.freeze()
}
}
fn take_tag(payload: &mut Bytes) -> io::Result<u8> {
if payload.is_empty() {
Err(invalid("empty replication payload"))
} else {
Ok(payload.get_u8())
}
}
fn take_bool(value: u8) -> io::Result<bool> {
match value {
0 => Ok(false),
1 => Ok(true),
_ => Err(invalid("invalid replication Boolean")),
}
}
fn require(payload: &Bytes, minimum: usize, message: &'static str) -> io::Result<()> {
if payload.len() < minimum {
Err(invalid(message))
} else {
Ok(())
}
}
fn require_exact(payload: &Bytes, length: usize, message: &'static str) -> io::Result<()> {
if payload.len() == length {
Ok(())
} else {
Err(invalid(message))
}
}
fn invalid(message: &'static str) -> io::Error {
io::Error::new(io::ErrorKind::InvalidData, message)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn physical_replication_messages_round_trip() {
let backend = [
BackendReplication::XLogData {
wal_start: 10,
wal_end: 20,
server_time: -30,
data: Bytes::from_static(b"wal"),
},
BackendReplication::PrimaryKeepalive {
wal_end: 40,
server_time: 50,
reply_requested: true,
},
BackendReplication::Unknown {
tag: b'z',
body: Bytes::from_static(b"extension"),
},
];
for message in backend {
assert_eq!(
BackendReplication::decode(message.encode()).unwrap(),
message
);
}
let frontend = [
FrontendReplication::StandbyStatus {
written: 10,
flushed: 20,
applied: 30,
client_time: 40,
reply_requested: false,
},
FrontendReplication::HotStandbyFeedback {
client_time: 50,
xmin: 60,
xmin_epoch: 70,
catalog_xmin: 80,
catalog_xmin_epoch: 90,
},
FrontendReplication::Unknown {
tag: b'z',
body: Bytes::from_static(b"extension"),
},
];
for message in frontend {
assert_eq!(
FrontendReplication::decode(message.encode()).unwrap(),
message
);
}
}
#[test]
fn known_messages_reject_invalid_shapes() {
assert!(BackendReplication::decode(Bytes::from_static(b"kshort")).is_err());
let mut invalid_bool = BytesMut::new();
invalid_bool.put_u8(b'k');
invalid_bool.extend_from_slice(&[0; 16]);
invalid_bool.put_u8(2);
assert!(BackendReplication::decode(invalid_bool.freeze()).is_err());
}
}