postgres_replication_types/lib.rs
1use chrono::{DateTime, Utc};
2
3/// A logical replication message.
4pub enum ReplicationMessage<'a> {
5 Begin(BeginMessage),
6 Generic(GenericMessage<'a>),
7 Commit(CommitMessage),
8 Origin(OriginMessage),
9 Relation(RelationMessage),
10 Type(TypeMessage),
11 Insert(InsertMessage<'a>),
12 Update(UpdateMessage<'a>),
13 Delete(DeleteMessage<'a>),
14 Truncate(TruncateMessage),
15 StreamStart(StreamStartMessage),
16 StreamStop(StreamStopMessage),
17 StreamCommit(StreamCommitMessage),
18 StreamAbort(StreamAbortMessage),
19 BeginPrepare(BeginPrepareMessage),
20 Prepare(PrepareMessage),
21 CommitPrepared(CommitPreparedMessage),
22 RollbackPrepared(RollbackPreparedMessage),
23 StreamPrepare(StreamPrepareMessage),
24}
25
26pub struct BeginMessage {
27 /// The final LSN of the transaction.
28 pub final_lsn: i64,
29 /// Commit timestamp of the transaction.
30 pub timestamp: DateTime<Utc>,
31 /// Xid of the transaction.
32 pub transaction_id: i32,
33}
34
35pub struct GenericMessage<'a> {
36 /// Xid of the transaction (only present for streamed transactions).
37 ///
38 /// NOTE: This field is available since protocol version 2.
39 pub transaction_id: Option<i32>,
40 /// If the logical decoding message is transactional?
41 pub is_transactional: bool,
42 /// The LSN of the logical decoding message.
43 pub lsn: i64,
44 /// The prefix of the logical decoding message.
45 pub prefix: String,
46 /// Length of the content.
47 pub length: i32,
48 /// The content of the logical decoding message.
49 pub content: &'a [u8],
50}
51
52pub struct CommitMessage {
53 /// The LSN of the commit.
54 pub lsn: i64,
55 /// The final LSN of the transaction.
56 pub final_lsn: i64,
57 /// Commit timestamp of the transaction.
58 pub timestamp: DateTime<Utc>,
59}
60
61pub struct OriginMessage {
62 /// The LSN of the commit on the origin server.
63 pub lsn: i64,
64 /// Name of the origin.
65 ///
66 /// NOTE: There can be multiple Origin messages inside a single transaction.
67 pub name: String,
68}
69
70pub struct RelationMessage {
71 /// Xid of the transaction (only present for streamed transactions).
72 ///
73 /// NOTE: This field is available since protocol version 2.
74 pub transaction_id: Option<i32>,
75 /// OID of the relation.
76 pub oid: i32,
77 /// Namespace (`None` for `pg_catalog`).
78 pub namespace: Option<String>,
79 /// Relation name.
80 pub name: String,
81 /// Replica identity setting for the relation (same as `relreplident` in `pg_class`).
82 pub replica_identity: i8,
83 /// Columns.
84 pub columns: Vec<RelationMessageColumn>,
85}
86
87pub struct RelationMessageColumn {
88 /// Is part of the key?
89 pub is_part_of_the_key: bool,
90 /// Name of the column.
91 pub name: String,
92 /// OID of the column's data type.
93 pub oid: i32,
94 /// Type modifier of the column (`atttypmod`).
95 pub type_modifier: i32,
96}
97
98pub struct TypeMessage {
99 /// Xid of the transaction (only present for streamed transactions).
100 ///
101 /// NOTE: This field is available since protocol version 2.
102 pub transaction_id: Option<i32>,
103 /// OID of the relation.
104 pub oid: i32,
105 /// Namespace (`None` for `pg_catalog`).
106 pub namespace: Option<String>,
107 /// Name of the data type.
108 pub name: String,
109}
110
111pub struct InsertMessage<'a> {
112 /// Xid of the transaction (only present for streamed transactions).
113 ///
114 /// NOTE: This field is available since protocol version 2.
115 pub transaction_id: Option<i32>,
116 /// OID of the relation.
117 pub oid: i32,
118 /// [`TupleData`] message part representing the contents of new tuple.
119 pub data: TupleData<'a>,
120}
121
122pub struct UpdateMessage<'a> {
123 /// Xid of the transaction (only present for streamed transactions).
124 ///
125 /// NOTE: This field is available since protocol version 2.
126 pub transaction_id: Option<i32>,
127 /// OID of the relation corresponding to the ID in the relation message.
128 pub oid: i32,
129 /// This field is optional and is only present if the update changed data in any of the column(s) that are part of the REPLICA IDENTITY index.
130 pub key: Option<TupleData<'a>>,
131 /// This field is optional and is only present if table in which the update happened has REPLICA IDENTITY set to FULL.
132 pub old: Option<TupleData<'a>>,
133 /// TupleData message part representing the contents of a new tuple.
134 pub new: TupleData<'a>,
135}
136
137pub struct DeleteMessage<'a> {
138 /// Xid of the transaction (only present for streamed transactions).
139 ///
140 /// NOTE: This field is available since protocol version 2.
141 pub transaction_id: Option<i32>,
142 /// OID of the relation corresponding to the ID in the relation message.
143 pub oid: i32,
144 /// This field is optional and is only present if the update changed data in any of the column(s) that are part of the REPLICA IDENTITY index.
145 pub key: Option<TupleData<'a>>,
146 /// This field is optional and is only present if table in which the update happened has REPLICA IDENTITY set to FULL.
147 pub old: Option<TupleData<'a>>,
148}
149
150pub struct TruncateMessage {
151 /// Xid of the transaction (only present for streamed transactions).
152 ///
153 /// NOTE: This field is available since protocol version 2.
154 pub transaction_id: Option<i32>,
155 /// Number of relations
156 pub relations_count: i32,
157 /// Is `CASCADE`?
158 pub is_cascade: bool,
159 /// Is `RESTART IDENTITY`?
160 pub is_restart_identity: bool,
161 /// OID of the relation corresponding to the ID in the relation message.
162 pub oid: i32,
163}
164
165pub struct StreamStartMessage {
166 /// Xid of the transaction (only present for streamed transactions).
167 ///
168 /// NOTE: This field is available since protocol version 2.
169 pub transaction_id: Option<i32>,
170 /// Is it a first stream segment?
171 pub is_first_segment: bool,
172}
173
174pub struct StreamStopMessage {}
175
176pub struct StreamCommitMessage {
177 /// Xid of the transaction.
178 pub transaction_id: i32,
179 /// The LSN of the commit.
180 pub lsn: i64,
181 /// The end LSN of the transaction.
182 pub final_lsn: i64,
183 /// Commit timestamp of the transaction.
184 pub timestamp: DateTime<Utc>,
185}
186
187pub struct StreamAbortMessage {
188 /// Xid of the transaction.
189 pub transaction_id: i32,
190 /// Xid of the subtransaction (will be same as xid of the transaction for top-level transactions).
191 pub subtransaction_id: i32,
192}
193
194pub struct BeginPrepareMessage {
195 /// The LSN of the prepare.
196 pub lsn: i64,
197 /// The end LSN of the prepared transaction.
198 pub final_lsn: i64,
199 /// Prepare timestamp of the transaction.
200 pub timestamp: DateTime<Utc>,
201 /// Xid of the transaction.
202 pub transaction_id: i32,
203 /// The user defined GID of the prepared transaction.
204 pub gid: String,
205}
206
207pub struct PrepareMessage {
208 /// The LSN of the prepare.
209 pub lsn: i64,
210 /// The end LSN of the prepared transaction.
211 pub final_lsn: i64,
212 /// Prepare timestamp of the transaction.
213 pub timestamp: DateTime<Utc>,
214 /// Xid of the transaction.
215 pub transaction_id: i32,
216 /// The user defined GID of the prepared transaction.
217 pub gid: String,
218}
219
220pub struct CommitPreparedMessage {
221 /// The LSN of the commit.
222 pub lsn: i64,
223 /// The end LSN of the prepared transaction.
224 pub final_lsn: i64,
225 /// Commit timestamp of the transaction.
226 pub timestamp: DateTime<Utc>,
227 /// Xid of the transaction.
228 pub transaction_id: i32,
229 /// The user defined GID of the prepared transaction.
230 pub gid: String,
231}
232
233pub struct RollbackPreparedMessage {
234 /// The LSN of the rollback.
235 pub lsn: i64,
236 /// The end LSN of the rollback or the prepared transaction.
237 pub final_lsn: i64,
238 /// Prepare timestamp of the transaction.
239 pub prepare_timestamp: DateTime<Utc>,
240 /// Rollback timestamp of the transaction.
241 pub timestamp: DateTime<Utc>,
242 /// Xid of the transaction.
243 pub transaction_id: i32,
244 /// The user defined GID of the prepared transaction.
245 pub gid: String,
246}
247
248pub struct StreamPrepareMessage {
249 /// The LSN of the prepare.
250 pub lsn: i64,
251 /// The end LSN of the prepared transaction.
252 pub final_lsn: i64,
253 /// Prepare timestamp of the transaction.
254 pub timestamp: DateTime<Utc>,
255 /// Xid of the transaction.
256 pub transaction_id: i32,
257 /// The user defined GID of the prepared transaction.
258 pub gid: String,
259}
260
261pub struct TupleData<'a> {
262 /// Columns.
263 pub columns: Vec<TupleDataColumn<'a>>,
264}
265
266pub struct TupleDataColumn<'a> {
267 /// Identifies the data as NULL value.
268 pub is_null: bool,
269 /// Identifies unchanged TOASTed value (the actual value is not sent).
270 /// TODO: decide correct naming here after research
271 pub is_unchanged: bool,
272 /// Identifies the data as text formatted value.
273 pub is_text: bool,
274 /// Identifies the data as binary formatted value.
275 pub is_binary: bool,
276 /// The value of the column in bytes. Only present if `is_binary` is `true`.
277 pub binary_value: Option<&'a [u8]>,
278 /// The value of the column as [`String`]. Only present if `is_text` is `true`,
279 pub text_value: Option<String>,
280}