Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * proto.c
4 : : * logical replication protocol functions
5 : : *
6 : : * Copyright (c) 2015-2025, PostgreSQL Global Development Group
7 : : *
8 : : * IDENTIFICATION
9 : : * src/backend/replication/logical/proto.c
10 : : *
11 : : *-------------------------------------------------------------------------
12 : : */
13 : : #include "postgres.h"
14 : :
15 : : #include "access/sysattr.h"
16 : : #include "catalog/pg_namespace.h"
17 : : #include "catalog/pg_type.h"
18 : : #include "libpq/pqformat.h"
19 : : #include "replication/logicalproto.h"
20 : : #include "utils/lsyscache.h"
21 : : #include "utils/syscache.h"
22 : :
23 : : /*
24 : : * Protocol message flags.
25 : : */
26 : : #define LOGICALREP_IS_REPLICA_IDENTITY 1
27 : :
28 : : #define MESSAGE_TRANSACTIONAL (1<<0)
29 : : #define TRUNCATE_CASCADE (1<<0)
30 : : #define TRUNCATE_RESTART_SEQS (1<<1)
31 : :
32 : : static void logicalrep_write_attrs(StringInfo out, Relation rel,
33 : : Bitmapset *columns,
34 : : PublishGencolsType include_gencols_type);
35 : : static void logicalrep_write_tuple(StringInfo out, Relation rel,
36 : : TupleTableSlot *slot,
37 : : bool binary, Bitmapset *columns,
38 : : PublishGencolsType include_gencols_type);
39 : : static void logicalrep_read_attrs(StringInfo in, LogicalRepRelation *rel);
40 : : static void logicalrep_read_tuple(StringInfo in, LogicalRepTupleData *tuple);
41 : :
42 : : static void logicalrep_write_namespace(StringInfo out, Oid nspid);
43 : : static const char *logicalrep_read_namespace(StringInfo in);
44 : :
45 : : /*
46 : : * Write BEGIN to the output stream.
47 : : */
48 : : void
3152 peter_e@gmx.net 49 :CBC 450 : logicalrep_write_begin(StringInfo out, ReorderBufferTXN *txn)
50 : : {
1769 akapila@postgresql.o 51 : 450 : pq_sendbyte(out, LOGICAL_REP_MSG_BEGIN);
52 : :
53 : : /* fixed fields */
3152 peter_e@gmx.net 54 : 450 : pq_sendint64(out, txn->final_lsn);
1515 akapila@postgresql.o 55 : 450 : pq_sendint64(out, txn->xact_time.commit_time);
2887 andres@anarazel.de 56 : 450 : pq_sendint32(out, txn->xid);
3152 peter_e@gmx.net 57 : 450 : }
58 : :
59 : : /*
60 : : * Read transaction BEGIN from the stream.
61 : : */
62 : : void
63 : 481 : logicalrep_read_begin(StringInfo in, LogicalRepBeginData *begin_data)
64 : : {
65 : : /* read fields */
66 : 481 : begin_data->final_lsn = pq_getmsgint64(in);
67 [ - + ]: 481 : if (begin_data->final_lsn == InvalidXLogRecPtr)
3152 peter_e@gmx.net 68 [ # # ]:UBC 0 : elog(ERROR, "final_lsn not set in begin message");
3152 peter_e@gmx.net 69 :CBC 481 : begin_data->committime = pq_getmsgint64(in);
70 : 481 : begin_data->xid = pq_getmsgint(in, 4);
71 : 481 : }
72 : :
73 : :
74 : : /*
75 : : * Write COMMIT to the output stream.
76 : : */
77 : : void
78 : 448 : logicalrep_write_commit(StringInfo out, ReorderBufferTXN *txn,
79 : : XLogRecPtr commit_lsn)
80 : : {
3034 bruce@momjian.us 81 : 448 : uint8 flags = 0;
82 : :
1769 akapila@postgresql.o 83 : 448 : pq_sendbyte(out, LOGICAL_REP_MSG_COMMIT);
84 : :
85 : : /* send the flags field (unused for now) */
3152 peter_e@gmx.net 86 : 448 : pq_sendbyte(out, flags);
87 : :
88 : : /* send fields */
89 : 448 : pq_sendint64(out, commit_lsn);
90 : 448 : pq_sendint64(out, txn->end_lsn);
1515 akapila@postgresql.o 91 : 448 : pq_sendint64(out, txn->xact_time.commit_time);
3152 peter_e@gmx.net 92 : 448 : }
93 : :
94 : : /*
95 : : * Read transaction COMMIT from the stream.
96 : : */
97 : : void
98 : 432 : logicalrep_read_commit(StringInfo in, LogicalRepCommitData *commit_data)
99 : : {
100 : : /* read flags (unused for now) */
3034 bruce@momjian.us 101 : 432 : uint8 flags = pq_getmsgbyte(in);
102 : :
3152 peter_e@gmx.net 103 [ - + ]: 432 : if (flags != 0)
3046 tgl@sss.pgh.pa.us 104 [ # # ]:UBC 0 : elog(ERROR, "unrecognized flags %u in commit message", flags);
105 : :
106 : : /* read fields */
3152 peter_e@gmx.net 107 :CBC 432 : commit_data->commit_lsn = pq_getmsgint64(in);
108 : 432 : commit_data->end_lsn = pq_getmsgint64(in);
109 : 432 : commit_data->committime = pq_getmsgint64(in);
110 : 432 : }
111 : :
112 : : /*
113 : : * Write BEGIN PREPARE to the output stream.
114 : : */
115 : : void
1515 akapila@postgresql.o 116 : 18 : logicalrep_write_begin_prepare(StringInfo out, ReorderBufferTXN *txn)
117 : : {
118 : 18 : pq_sendbyte(out, LOGICAL_REP_MSG_BEGIN_PREPARE);
119 : :
120 : : /* fixed fields */
121 : 18 : pq_sendint64(out, txn->final_lsn);
122 : 18 : pq_sendint64(out, txn->end_lsn);
123 : 18 : pq_sendint64(out, txn->xact_time.prepare_time);
124 : 18 : pq_sendint32(out, txn->xid);
125 : :
126 : : /* send gid */
127 : 18 : pq_sendstring(out, txn->gid);
128 : 18 : }
129 : :
130 : : /*
131 : : * Read transaction BEGIN PREPARE from the stream.
132 : : */
133 : : void
134 : 16 : logicalrep_read_begin_prepare(StringInfo in, LogicalRepPreparedTxnData *begin_data)
135 : : {
136 : : /* read fields */
137 : 16 : begin_data->prepare_lsn = pq_getmsgint64(in);
138 [ - + ]: 16 : if (begin_data->prepare_lsn == InvalidXLogRecPtr)
1515 akapila@postgresql.o 139 [ # # ]:UBC 0 : elog(ERROR, "prepare_lsn not set in begin prepare message");
1515 akapila@postgresql.o 140 :CBC 16 : begin_data->end_lsn = pq_getmsgint64(in);
141 [ - + ]: 16 : if (begin_data->end_lsn == InvalidXLogRecPtr)
1515 akapila@postgresql.o 142 [ # # ]:UBC 0 : elog(ERROR, "end_lsn not set in begin prepare message");
1515 akapila@postgresql.o 143 :CBC 16 : begin_data->prepare_time = pq_getmsgint64(in);
144 : 16 : begin_data->xid = pq_getmsgint(in, 4);
145 : :
146 : : /* read gid (copy it into a pre-allocated buffer) */
1509 147 : 16 : strlcpy(begin_data->gid, pq_getmsgstring(in), sizeof(begin_data->gid));
1515 148 : 16 : }
149 : :
150 : : /*
151 : : * The core functionality for logicalrep_write_prepare and
152 : : * logicalrep_write_stream_prepare.
153 : : */
154 : : static void
1500 155 : 32 : logicalrep_write_prepare_common(StringInfo out, LogicalRepMsgType type,
156 : : ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
157 : : {
1515 158 : 32 : uint8 flags = 0;
159 : :
1500 160 : 32 : pq_sendbyte(out, type);
161 : :
162 : : /*
163 : : * This should only ever happen for two-phase commit transactions, in
164 : : * which case we expect to have a valid GID.
165 : : */
1515 166 [ - + ]: 32 : Assert(txn->gid != NULL);
206 msawada@postgresql.o 167 [ - + ]: 32 : Assert(rbtxn_is_prepared(txn));
1500 akapila@postgresql.o 168 [ - + ]: 32 : Assert(TransactionIdIsValid(txn->xid));
169 : :
170 : : /* send the flags field */
1515 171 : 32 : pq_sendbyte(out, flags);
172 : :
173 : : /* send fields */
174 : 32 : pq_sendint64(out, prepare_lsn);
175 : 32 : pq_sendint64(out, txn->end_lsn);
176 : 32 : pq_sendint64(out, txn->xact_time.prepare_time);
177 : 32 : pq_sendint32(out, txn->xid);
178 : :
179 : : /* send gid */
180 : 32 : pq_sendstring(out, txn->gid);
181 : 32 : }
182 : :
183 : : /*
184 : : * Write PREPARE to the output stream.
185 : : */
186 : : void
1500 187 : 18 : logicalrep_write_prepare(StringInfo out, ReorderBufferTXN *txn,
188 : : XLogRecPtr prepare_lsn)
189 : : {
190 : 18 : logicalrep_write_prepare_common(out, LOGICAL_REP_MSG_PREPARE,
191 : : txn, prepare_lsn);
192 : 18 : }
193 : :
194 : : /*
195 : : * The core functionality for logicalrep_read_prepare and
196 : : * logicalrep_read_stream_prepare.
197 : : */
198 : : static void
199 : 26 : logicalrep_read_prepare_common(StringInfo in, char *msgtype,
200 : : LogicalRepPreparedTxnData *prepare_data)
201 : : {
202 : : /* read flags */
1515 203 : 26 : uint8 flags = pq_getmsgbyte(in);
204 : :
205 [ - + ]: 26 : if (flags != 0)
1500 akapila@postgresql.o 206 [ # # ]:UBC 0 : elog(ERROR, "unrecognized flags %u in %s message", flags, msgtype);
207 : :
208 : : /* read fields */
1515 akapila@postgresql.o 209 :CBC 26 : prepare_data->prepare_lsn = pq_getmsgint64(in);
210 [ - + ]: 26 : if (prepare_data->prepare_lsn == InvalidXLogRecPtr)
1500 akapila@postgresql.o 211 [ # # ]:UBC 0 : elog(ERROR, "prepare_lsn is not set in %s message", msgtype);
1515 akapila@postgresql.o 212 :CBC 26 : prepare_data->end_lsn = pq_getmsgint64(in);
213 [ - + ]: 26 : if (prepare_data->end_lsn == InvalidXLogRecPtr)
1500 akapila@postgresql.o 214 [ # # ]:UBC 0 : elog(ERROR, "end_lsn is not set in %s message", msgtype);
1515 akapila@postgresql.o 215 :CBC 26 : prepare_data->prepare_time = pq_getmsgint64(in);
216 : 26 : prepare_data->xid = pq_getmsgint(in, 4);
1494 217 [ - + ]: 26 : if (prepare_data->xid == InvalidTransactionId)
1494 akapila@postgresql.o 218 [ # # ]:UBC 0 : elog(ERROR, "invalid two-phase transaction ID in %s message", msgtype);
219 : :
220 : : /* read gid (copy it into a pre-allocated buffer) */
1509 akapila@postgresql.o 221 :CBC 26 : strlcpy(prepare_data->gid, pq_getmsgstring(in), sizeof(prepare_data->gid));
1515 222 : 26 : }
223 : :
224 : : /*
225 : : * Read transaction PREPARE from the stream.
226 : : */
227 : : void
1500 228 : 15 : logicalrep_read_prepare(StringInfo in, LogicalRepPreparedTxnData *prepare_data)
229 : : {
230 : 15 : logicalrep_read_prepare_common(in, "prepare", prepare_data);
231 : 15 : }
232 : :
233 : : /*
234 : : * Write COMMIT PREPARED to the output stream.
235 : : */
236 : : void
1515 237 : 23 : logicalrep_write_commit_prepared(StringInfo out, ReorderBufferTXN *txn,
238 : : XLogRecPtr commit_lsn)
239 : : {
240 : 23 : uint8 flags = 0;
241 : :
242 : 23 : pq_sendbyte(out, LOGICAL_REP_MSG_COMMIT_PREPARED);
243 : :
244 : : /*
245 : : * This should only ever happen for two-phase commit transactions, in
246 : : * which case we expect to have a valid GID.
247 : : */
248 [ - + ]: 23 : Assert(txn->gid != NULL);
249 : :
250 : : /* send the flags field */
251 : 23 : pq_sendbyte(out, flags);
252 : :
253 : : /* send fields */
254 : 23 : pq_sendint64(out, commit_lsn);
255 : 23 : pq_sendint64(out, txn->end_lsn);
256 : 23 : pq_sendint64(out, txn->xact_time.commit_time);
257 : 23 : pq_sendint32(out, txn->xid);
258 : :
259 : : /* send gid */
260 : 23 : pq_sendstring(out, txn->gid);
261 : 23 : }
262 : :
263 : : /*
264 : : * Read transaction COMMIT PREPARED from the stream.
265 : : */
266 : : void
267 : 20 : logicalrep_read_commit_prepared(StringInfo in, LogicalRepCommitPreparedTxnData *prepare_data)
268 : : {
269 : : /* read flags */
270 : 20 : uint8 flags = pq_getmsgbyte(in);
271 : :
272 [ - + ]: 20 : if (flags != 0)
1515 akapila@postgresql.o 273 [ # # ]:UBC 0 : elog(ERROR, "unrecognized flags %u in commit prepared message", flags);
274 : :
275 : : /* read fields */
1515 akapila@postgresql.o 276 :CBC 20 : prepare_data->commit_lsn = pq_getmsgint64(in);
277 [ - + ]: 20 : if (prepare_data->commit_lsn == InvalidXLogRecPtr)
1515 akapila@postgresql.o 278 [ # # ]:UBC 0 : elog(ERROR, "commit_lsn is not set in commit prepared message");
1515 akapila@postgresql.o 279 :CBC 20 : prepare_data->end_lsn = pq_getmsgint64(in);
280 [ - + ]: 20 : if (prepare_data->end_lsn == InvalidXLogRecPtr)
1515 akapila@postgresql.o 281 [ # # ]:UBC 0 : elog(ERROR, "end_lsn is not set in commit prepared message");
1515 akapila@postgresql.o 282 :CBC 20 : prepare_data->commit_time = pq_getmsgint64(in);
283 : 20 : prepare_data->xid = pq_getmsgint(in, 4);
284 : :
285 : : /* read gid (copy it into a pre-allocated buffer) */
1509 286 : 20 : strlcpy(prepare_data->gid, pq_getmsgstring(in), sizeof(prepare_data->gid));
1515 287 : 20 : }
288 : :
289 : : /*
290 : : * Write ROLLBACK PREPARED to the output stream.
291 : : */
292 : : void
293 : 8 : logicalrep_write_rollback_prepared(StringInfo out, ReorderBufferTXN *txn,
294 : : XLogRecPtr prepare_end_lsn,
295 : : TimestampTz prepare_time)
296 : : {
297 : 8 : uint8 flags = 0;
298 : :
299 : 8 : pq_sendbyte(out, LOGICAL_REP_MSG_ROLLBACK_PREPARED);
300 : :
301 : : /*
302 : : * This should only ever happen for two-phase commit transactions, in
303 : : * which case we expect to have a valid GID.
304 : : */
305 [ - + ]: 8 : Assert(txn->gid != NULL);
306 : :
307 : : /* send the flags field */
308 : 8 : pq_sendbyte(out, flags);
309 : :
310 : : /* send fields */
311 : 8 : pq_sendint64(out, prepare_end_lsn);
312 : 8 : pq_sendint64(out, txn->end_lsn);
313 : 8 : pq_sendint64(out, prepare_time);
314 : 8 : pq_sendint64(out, txn->xact_time.commit_time);
315 : 8 : pq_sendint32(out, txn->xid);
316 : :
317 : : /* send gid */
318 : 8 : pq_sendstring(out, txn->gid);
319 : 8 : }
320 : :
321 : : /*
322 : : * Read transaction ROLLBACK PREPARED from the stream.
323 : : */
324 : : void
325 : 5 : logicalrep_read_rollback_prepared(StringInfo in,
326 : : LogicalRepRollbackPreparedTxnData *rollback_data)
327 : : {
328 : : /* read flags */
329 : 5 : uint8 flags = pq_getmsgbyte(in);
330 : :
331 [ - + ]: 5 : if (flags != 0)
1515 akapila@postgresql.o 332 [ # # ]:UBC 0 : elog(ERROR, "unrecognized flags %u in rollback prepared message", flags);
333 : :
334 : : /* read fields */
1515 akapila@postgresql.o 335 :CBC 5 : rollback_data->prepare_end_lsn = pq_getmsgint64(in);
336 [ - + ]: 5 : if (rollback_data->prepare_end_lsn == InvalidXLogRecPtr)
1515 akapila@postgresql.o 337 [ # # ]:UBC 0 : elog(ERROR, "prepare_end_lsn is not set in rollback prepared message");
1515 akapila@postgresql.o 338 :CBC 5 : rollback_data->rollback_end_lsn = pq_getmsgint64(in);
339 [ - + ]: 5 : if (rollback_data->rollback_end_lsn == InvalidXLogRecPtr)
1515 akapila@postgresql.o 340 [ # # ]:UBC 0 : elog(ERROR, "rollback_end_lsn is not set in rollback prepared message");
1515 akapila@postgresql.o 341 :CBC 5 : rollback_data->prepare_time = pq_getmsgint64(in);
342 : 5 : rollback_data->rollback_time = pq_getmsgint64(in);
343 : 5 : rollback_data->xid = pq_getmsgint(in, 4);
344 : :
345 : : /* read gid (copy it into a pre-allocated buffer) */
1509 346 : 5 : strlcpy(rollback_data->gid, pq_getmsgstring(in), sizeof(rollback_data->gid));
1515 347 : 5 : }
348 : :
349 : : /*
350 : : * Write STREAM PREPARE to the output stream.
351 : : */
352 : : void
1494 353 : 14 : logicalrep_write_stream_prepare(StringInfo out,
354 : : ReorderBufferTXN *txn,
355 : : XLogRecPtr prepare_lsn)
356 : : {
357 : 14 : logicalrep_write_prepare_common(out, LOGICAL_REP_MSG_STREAM_PREPARE,
358 : : txn, prepare_lsn);
359 : 14 : }
360 : :
361 : : /*
362 : : * Read STREAM PREPARE from the stream.
363 : : */
364 : : void
365 : 11 : logicalrep_read_stream_prepare(StringInfo in, LogicalRepPreparedTxnData *prepare_data)
366 : : {
367 : 11 : logicalrep_read_prepare_common(in, "stream prepare", prepare_data);
368 : 11 : }
369 : :
370 : : /*
371 : : * Write ORIGIN to the output stream.
372 : : */
373 : : void
3152 peter_e@gmx.net 374 : 8 : logicalrep_write_origin(StringInfo out, const char *origin,
375 : : XLogRecPtr origin_lsn)
376 : : {
1769 akapila@postgresql.o 377 : 8 : pq_sendbyte(out, LOGICAL_REP_MSG_ORIGIN);
378 : :
379 : : /* fixed fields */
3152 peter_e@gmx.net 380 : 8 : pq_sendint64(out, origin_lsn);
381 : :
382 : : /* origin string */
383 : 8 : pq_sendstring(out, origin);
384 : 8 : }
385 : :
386 : : /*
387 : : * Read ORIGIN from the output stream.
388 : : */
389 : : char *
3152 peter_e@gmx.net 390 :UBC 0 : logicalrep_read_origin(StringInfo in, XLogRecPtr *origin_lsn)
391 : : {
392 : : /* fixed fields */
393 : 0 : *origin_lsn = pq_getmsgint64(in);
394 : :
395 : : /* return origin */
396 : 0 : return pstrdup(pq_getmsgstring(in));
397 : : }
398 : :
399 : : /*
400 : : * Write INSERT to the output stream.
401 : : */
402 : : void
1829 akapila@postgresql.o 403 :CBC 105914 : logicalrep_write_insert(StringInfo out, TransactionId xid, Relation rel,
404 : : TupleTableSlot *newslot, bool binary,
405 : : Bitmapset *columns,
406 : : PublishGencolsType include_gencols_type)
407 : : {
1769 408 : 105914 : pq_sendbyte(out, LOGICAL_REP_MSG_INSERT);
409 : :
410 : : /* transaction ID (if not valid, we're not streaming) */
1829 411 [ + + ]: 105914 : if (TransactionIdIsValid(xid))
412 : 100084 : pq_sendint32(out, xid);
413 : :
414 : : /* use Oid as relation identifier */
2887 andres@anarazel.de 415 : 105914 : pq_sendint32(out, RelationGetRelid(rel));
416 : :
3152 peter_e@gmx.net 417 : 105914 : pq_sendbyte(out, 'N'); /* new tuple follows */
226 akapila@postgresql.o 418 : 105914 : logicalrep_write_tuple(out, rel, newslot, binary, columns,
419 : : include_gencols_type);
3152 peter_e@gmx.net 420 : 105914 : }
421 : :
422 : : /*
423 : : * Read INSERT from stream.
424 : : *
425 : : * Fills the new tuple.
426 : : */
427 : : LogicalRepRelId
428 : 75888 : logicalrep_read_insert(StringInfo in, LogicalRepTupleData *newtup)
429 : : {
430 : : char action;
431 : : LogicalRepRelId relid;
432 : :
433 : : /* read the relation id */
434 : 75888 : relid = pq_getmsgint(in, 4);
435 : :
436 : 75888 : action = pq_getmsgbyte(in);
437 [ - + ]: 75888 : if (action != 'N')
3152 peter_e@gmx.net 438 [ # # ]:UBC 0 : elog(ERROR, "expected new tuple but got %d",
439 : : action);
440 : :
3152 peter_e@gmx.net 441 :CBC 75888 : logicalrep_read_tuple(in, newtup);
442 : :
443 : 75888 : return relid;
444 : : }
445 : :
446 : : /*
447 : : * Write UPDATE to the output stream.
448 : : */
449 : : void
1829 akapila@postgresql.o 450 : 34447 : logicalrep_write_update(StringInfo out, TransactionId xid, Relation rel,
451 : : TupleTableSlot *oldslot, TupleTableSlot *newslot,
452 : : bool binary, Bitmapset *columns,
453 : : PublishGencolsType include_gencols_type)
454 : : {
1769 455 : 34447 : pq_sendbyte(out, LOGICAL_REP_MSG_UPDATE);
456 : :
3152 peter_e@gmx.net 457 [ + + + + : 34447 : Assert(rel->rd_rel->relreplident == REPLICA_IDENTITY_DEFAULT ||
- + ]
458 : : rel->rd_rel->relreplident == REPLICA_IDENTITY_FULL ||
459 : : rel->rd_rel->relreplident == REPLICA_IDENTITY_INDEX);
460 : :
461 : : /* transaction ID (if not valid, we're not streaming) */
1829 akapila@postgresql.o 462 [ + + ]: 34447 : if (TransactionIdIsValid(xid))
463 : 34232 : pq_sendint32(out, xid);
464 : :
465 : : /* use Oid as relation identifier */
2887 andres@anarazel.de 466 : 34447 : pq_sendint32(out, RelationGetRelid(rel));
467 : :
1292 akapila@postgresql.o 468 [ + + ]: 34447 : if (oldslot != NULL)
469 : : {
3152 peter_e@gmx.net 470 [ + + ]: 134 : if (rel->rd_rel->relreplident == REPLICA_IDENTITY_FULL)
2999 tgl@sss.pgh.pa.us 471 : 63 : pq_sendbyte(out, 'O'); /* old tuple follows */
472 : : else
473 : 71 : pq_sendbyte(out, 'K'); /* old key follows */
303 akapila@postgresql.o 474 : 134 : logicalrep_write_tuple(out, rel, oldslot, binary, columns,
475 : : include_gencols_type);
476 : : }
477 : :
3152 peter_e@gmx.net 478 : 34447 : pq_sendbyte(out, 'N'); /* new tuple follows */
226 akapila@postgresql.o 479 : 34447 : logicalrep_write_tuple(out, rel, newslot, binary, columns,
480 : : include_gencols_type);
3152 peter_e@gmx.net 481 : 34447 : }
482 : :
483 : : /*
484 : : * Read UPDATE from stream.
485 : : */
486 : : LogicalRepRelId
487 : 31941 : logicalrep_read_update(StringInfo in, bool *has_oldtuple,
488 : : LogicalRepTupleData *oldtup,
489 : : LogicalRepTupleData *newtup)
490 : : {
491 : : char action;
492 : : LogicalRepRelId relid;
493 : :
494 : : /* read the relation id */
495 : 31941 : relid = pq_getmsgint(in, 4);
496 : :
497 : : /* read and verify action */
498 : 31941 : action = pq_getmsgbyte(in);
499 [ + + + + : 31941 : if (action != 'K' && action != 'O' && action != 'N')
- + ]
3152 peter_e@gmx.net 500 [ # # ]:UBC 0 : elog(ERROR, "expected action 'N', 'O' or 'K', got %c",
501 : : action);
502 : :
503 : : /* check for old tuple */
3152 peter_e@gmx.net 504 [ + + + + ]:CBC 31941 : if (action == 'K' || action == 'O')
505 : : {
506 : 136 : logicalrep_read_tuple(in, oldtup);
507 : 136 : *has_oldtuple = true;
508 : :
509 : 136 : action = pq_getmsgbyte(in);
510 : : }
511 : : else
512 : 31805 : *has_oldtuple = false;
513 : :
514 : : /* check for new tuple */
515 [ - + ]: 31941 : if (action != 'N')
3152 peter_e@gmx.net 516 [ # # ]:UBC 0 : elog(ERROR, "expected action 'N', got %c",
517 : : action);
518 : :
3152 peter_e@gmx.net 519 :CBC 31941 : logicalrep_read_tuple(in, newtup);
520 : :
521 : 31941 : return relid;
522 : : }
523 : :
524 : : /*
525 : : * Write DELETE to the output stream.
526 : : */
527 : : void
1829 akapila@postgresql.o 528 : 41885 : logicalrep_write_delete(StringInfo out, TransactionId xid, Relation rel,
529 : : TupleTableSlot *oldslot, bool binary,
530 : : Bitmapset *columns,
531 : : PublishGencolsType include_gencols_type)
532 : : {
3152 peter_e@gmx.net 533 [ + + + + : 41885 : Assert(rel->rd_rel->relreplident == REPLICA_IDENTITY_DEFAULT ||
- + ]
534 : : rel->rd_rel->relreplident == REPLICA_IDENTITY_FULL ||
535 : : rel->rd_rel->relreplident == REPLICA_IDENTITY_INDEX);
536 : :
1769 akapila@postgresql.o 537 : 41885 : pq_sendbyte(out, LOGICAL_REP_MSG_DELETE);
538 : :
539 : : /* transaction ID (if not valid, we're not streaming) */
1829 540 [ + + ]: 41885 : if (TransactionIdIsValid(xid))
541 : 41625 : pq_sendint32(out, xid);
542 : :
543 : : /* use Oid as relation identifier */
2887 andres@anarazel.de 544 : 41885 : pq_sendint32(out, RelationGetRelid(rel));
545 : :
3152 peter_e@gmx.net 546 [ + + ]: 41885 : if (rel->rd_rel->relreplident == REPLICA_IDENTITY_FULL)
547 : 128 : pq_sendbyte(out, 'O'); /* old tuple follows */
548 : : else
549 : 41757 : pq_sendbyte(out, 'K'); /* old key follows */
550 : :
226 akapila@postgresql.o 551 : 41885 : logicalrep_write_tuple(out, rel, oldslot, binary, columns,
552 : : include_gencols_type);
3152 peter_e@gmx.net 553 : 41885 : }
554 : :
555 : : /*
556 : : * Read DELETE from stream.
557 : : *
558 : : * Fills the old tuple.
559 : : */
560 : : LogicalRepRelId
561 : 40320 : logicalrep_read_delete(StringInfo in, LogicalRepTupleData *oldtup)
562 : : {
563 : : char action;
564 : : LogicalRepRelId relid;
565 : :
566 : : /* read the relation id */
567 : 40320 : relid = pq_getmsgint(in, 4);
568 : :
569 : : /* read and verify action */
570 : 40320 : action = pq_getmsgbyte(in);
571 [ + + - + ]: 40320 : if (action != 'K' && action != 'O')
3152 peter_e@gmx.net 572 [ # # ]:UBC 0 : elog(ERROR, "expected action 'O' or 'K', got %c", action);
573 : :
3152 peter_e@gmx.net 574 :CBC 40320 : logicalrep_read_tuple(in, oldtup);
575 : :
576 : 40320 : return relid;
577 : : }
578 : :
579 : : /*
580 : : * Write TRUNCATE to the output stream.
581 : : */
582 : : void
2709 583 : 10 : logicalrep_write_truncate(StringInfo out,
584 : : TransactionId xid,
585 : : int nrelids,
586 : : Oid relids[],
587 : : bool cascade, bool restart_seqs)
588 : : {
589 : : int i;
2690 tgl@sss.pgh.pa.us 590 : 10 : uint8 flags = 0;
591 : :
1769 akapila@postgresql.o 592 : 10 : pq_sendbyte(out, LOGICAL_REP_MSG_TRUNCATE);
593 : :
594 : : /* transaction ID (if not valid, we're not streaming) */
1829 595 [ - + ]: 10 : if (TransactionIdIsValid(xid))
1829 akapila@postgresql.o 596 :UBC 0 : pq_sendint32(out, xid);
597 : :
2709 peter_e@gmx.net 598 :CBC 10 : pq_sendint32(out, nrelids);
599 : :
600 : : /* encode and send truncate flags */
601 [ - + ]: 10 : if (cascade)
2709 peter_e@gmx.net 602 :UBC 0 : flags |= TRUNCATE_CASCADE;
2709 peter_e@gmx.net 603 [ - + ]:CBC 10 : if (restart_seqs)
2709 peter_e@gmx.net 604 :UBC 0 : flags |= TRUNCATE_RESTART_SEQS;
2709 peter_e@gmx.net 605 :CBC 10 : pq_sendint8(out, flags);
606 : :
607 [ + + ]: 26 : for (i = 0; i < nrelids; i++)
608 : 16 : pq_sendint32(out, relids[i]);
609 : 10 : }
610 : :
611 : : /*
612 : : * Read TRUNCATE from stream.
613 : : */
614 : : List *
615 : 19 : logicalrep_read_truncate(StringInfo in,
616 : : bool *cascade, bool *restart_seqs)
617 : : {
618 : : int i;
619 : : int nrelids;
620 : 19 : List *relids = NIL;
621 : : uint8 flags;
622 : :
623 : 19 : nrelids = pq_getmsgint(in, 4);
624 : :
625 : : /* read and decode truncate flags */
626 : 19 : flags = pq_getmsgint(in, 1);
627 : 19 : *cascade = (flags & TRUNCATE_CASCADE) > 0;
628 : 19 : *restart_seqs = (flags & TRUNCATE_RESTART_SEQS) > 0;
629 : :
630 [ + + ]: 47 : for (i = 0; i < nrelids; i++)
631 : 28 : relids = lappend_oid(relids, pq_getmsgint(in, 4));
632 : :
633 : 19 : return relids;
634 : : }
635 : :
636 : : /*
637 : : * Write MESSAGE to stream
638 : : */
639 : : void
1614 akapila@postgresql.o 640 : 5 : logicalrep_write_message(StringInfo out, TransactionId xid, XLogRecPtr lsn,
641 : : bool transactional, const char *prefix, Size sz,
642 : : const char *message)
643 : : {
644 : 5 : uint8 flags = 0;
645 : :
646 : 5 : pq_sendbyte(out, LOGICAL_REP_MSG_MESSAGE);
647 : :
648 : : /* encode and send message flags */
649 [ + + ]: 5 : if (transactional)
650 : 2 : flags |= MESSAGE_TRANSACTIONAL;
651 : :
652 : : /* transaction ID (if not valid, we're not streaming) */
653 [ - + ]: 5 : if (TransactionIdIsValid(xid))
1614 akapila@postgresql.o 654 :UBC 0 : pq_sendint32(out, xid);
655 : :
1614 akapila@postgresql.o 656 :CBC 5 : pq_sendint8(out, flags);
657 : 5 : pq_sendint64(out, lsn);
658 : 5 : pq_sendstring(out, prefix);
659 : 5 : pq_sendint32(out, sz);
660 : 5 : pq_sendbytes(out, message, sz);
661 : 5 : }
662 : :
663 : : /*
664 : : * Write relation description to the output stream.
665 : : */
666 : : void
1260 tomas.vondra@postgre 667 : 373 : logicalrep_write_rel(StringInfo out, TransactionId xid, Relation rel,
668 : : Bitmapset *columns,
669 : : PublishGencolsType include_gencols_type)
670 : : {
671 : : char *relname;
672 : :
1769 akapila@postgresql.o 673 : 373 : pq_sendbyte(out, LOGICAL_REP_MSG_RELATION);
674 : :
675 : : /* transaction ID (if not valid, we're not streaming) */
1829 676 [ + + ]: 373 : if (TransactionIdIsValid(xid))
677 : 72 : pq_sendint32(out, xid);
678 : :
679 : : /* use Oid as relation identifier */
2887 andres@anarazel.de 680 : 373 : pq_sendint32(out, RelationGetRelid(rel));
681 : :
682 : : /* send qualified relation name */
3152 peter_e@gmx.net 683 : 373 : logicalrep_write_namespace(out, RelationGetNamespace(rel));
684 : 373 : relname = RelationGetRelationName(rel);
685 : 373 : pq_sendstring(out, relname);
686 : :
687 : : /* send replica identity */
688 : 373 : pq_sendbyte(out, rel->rd_rel->relreplident);
689 : :
690 : : /* send the attribute info */
226 akapila@postgresql.o 691 : 373 : logicalrep_write_attrs(out, rel, columns, include_gencols_type);
3152 peter_e@gmx.net 692 : 373 : }
693 : :
694 : : /*
695 : : * Read the relation info from stream and return as LogicalRepRelation.
696 : : */
697 : : LogicalRepRelation *
698 : 426 : logicalrep_read_rel(StringInfo in)
699 : : {
3034 bruce@momjian.us 700 : 426 : LogicalRepRelation *rel = palloc(sizeof(LogicalRepRelation));
701 : :
3152 peter_e@gmx.net 702 : 426 : rel->remoteid = pq_getmsgint(in, 4);
703 : :
704 : : /* Read relation name from stream */
705 : 426 : rel->nspname = pstrdup(logicalrep_read_namespace(in));
706 : 426 : rel->relname = pstrdup(pq_getmsgstring(in));
707 : :
708 : : /* Read the replica identity. */
709 : 426 : rel->replident = pq_getmsgbyte(in);
710 : :
711 : : /* Get attribute description */
712 : 426 : logicalrep_read_attrs(in, rel);
713 : :
714 : 426 : return rel;
715 : : }
716 : :
717 : : /*
718 : : * Write type info to the output stream.
719 : : *
720 : : * This function will always write base type info.
721 : : */
722 : : void
1829 akapila@postgresql.o 723 : 18 : logicalrep_write_typ(StringInfo out, TransactionId xid, Oid typoid)
724 : : {
3152 peter_e@gmx.net 725 : 18 : Oid basetypoid = getBaseType(typoid);
726 : : HeapTuple tup;
727 : : Form_pg_type typtup;
728 : :
1769 akapila@postgresql.o 729 : 18 : pq_sendbyte(out, LOGICAL_REP_MSG_TYPE);
730 : :
731 : : /* transaction ID (if not valid, we're not streaming) */
1829 732 [ - + ]: 18 : if (TransactionIdIsValid(xid))
1829 akapila@postgresql.o 733 :UBC 0 : pq_sendint32(out, xid);
734 : :
3152 peter_e@gmx.net 735 :CBC 18 : tup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(basetypoid));
736 [ - + ]: 18 : if (!HeapTupleIsValid(tup))
3152 peter_e@gmx.net 737 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for type %u", basetypoid);
3152 peter_e@gmx.net 738 :CBC 18 : typtup = (Form_pg_type) GETSTRUCT(tup);
739 : :
740 : : /* use Oid as type identifier */
2887 andres@anarazel.de 741 : 18 : pq_sendint32(out, typoid);
742 : :
743 : : /* send qualified type name */
3152 peter_e@gmx.net 744 : 18 : logicalrep_write_namespace(out, typtup->typnamespace);
745 : 18 : pq_sendstring(out, NameStr(typtup->typname));
746 : :
747 : 18 : ReleaseSysCache(tup);
748 : 18 : }
749 : :
750 : : /*
751 : : * Read type info from the output stream.
752 : : */
753 : : void
754 : 18 : logicalrep_read_typ(StringInfo in, LogicalRepTyp *ltyp)
755 : : {
756 : 18 : ltyp->remoteid = pq_getmsgint(in, 4);
757 : :
758 : : /* Read type name from stream */
759 : 18 : ltyp->nspname = pstrdup(logicalrep_read_namespace(in));
760 : 18 : ltyp->typname = pstrdup(pq_getmsgstring(in));
761 : 18 : }
762 : :
763 : : /*
764 : : * Write a tuple to the outputstream, in the most efficient format possible.
765 : : */
766 : : static void
1292 akapila@postgresql.o 767 : 182380 : logicalrep_write_tuple(StringInfo out, Relation rel, TupleTableSlot *slot,
768 : : bool binary, Bitmapset *columns,
769 : : PublishGencolsType include_gencols_type)
770 : : {
771 : : TupleDesc desc;
772 : : Datum *values;
773 : : bool *isnull;
774 : : int i;
3152 peter_e@gmx.net 775 : 182380 : uint16 nliveatts = 0;
776 : :
777 : 182380 : desc = RelationGetDescr(rel);
778 : :
779 [ + + ]: 548656 : for (i = 0; i < desc->natts; i++)
780 : : {
1260 tomas.vondra@postgre 781 : 366276 : Form_pg_attribute att = TupleDescAttr(desc, i);
782 : :
226 akapila@postgresql.o 783 [ + + ]: 366276 : if (!logicalrep_should_publish_column(att, columns,
784 : : include_gencols_type))
3152 peter_e@gmx.net 785 : 141 : continue;
786 : :
787 : 366135 : nliveatts++;
788 : : }
2887 andres@anarazel.de 789 : 182380 : pq_sendint16(out, nliveatts);
790 : :
1292 akapila@postgresql.o 791 : 182380 : slot_getallattrs(slot);
792 : 182380 : values = slot->tts_values;
793 : 182380 : isnull = slot->tts_isnull;
794 : :
795 : : /* Write the values */
3152 peter_e@gmx.net 796 [ + + ]: 548656 : for (i = 0; i < desc->natts; i++)
797 : : {
798 : : HeapTuple typtup;
799 : : Form_pg_type typclass;
2939 andres@anarazel.de 800 : 366276 : Form_pg_attribute att = TupleDescAttr(desc, i);
801 : :
226 akapila@postgresql.o 802 [ + + ]: 366276 : if (!logicalrep_should_publish_column(att, columns,
803 : : include_gencols_type))
1260 tomas.vondra@postgre 804 : 141 : continue;
805 : :
3152 peter_e@gmx.net 806 [ + + ]: 366135 : if (isnull[i])
807 : : {
1876 tgl@sss.pgh.pa.us 808 : 51927 : pq_sendbyte(out, LOGICALREP_COLUMN_NULL);
3152 peter_e@gmx.net 809 : 51927 : continue;
810 : : }
811 : :
32 peter@eisentraut.org 812 [ + + + + ]:GNC 314208 : if (att->attlen == -1 && VARATT_IS_EXTERNAL_ONDISK(DatumGetPointer(values[i])))
813 : : {
814 : : /*
815 : : * Unchanged toasted datum. (Note that we don't promise to detect
816 : : * unchanged data in general; this is just a cheap check to avoid
817 : : * sending large values unnecessarily.)
818 : : */
1876 tgl@sss.pgh.pa.us 819 :CBC 3 : pq_sendbyte(out, LOGICALREP_COLUMN_UNCHANGED);
3152 peter_e@gmx.net 820 : 3 : continue;
821 : : }
822 : :
823 : 314205 : typtup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(att->atttypid));
824 [ - + ]: 314205 : if (!HeapTupleIsValid(typtup))
3152 peter_e@gmx.net 825 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for type %u", att->atttypid);
3152 peter_e@gmx.net 826 :CBC 314205 : typclass = (Form_pg_type) GETSTRUCT(typtup);
827 : :
828 : : /*
829 : : * Send in binary if requested and type has suitable send function.
830 : : */
1873 tgl@sss.pgh.pa.us 831 [ + + + + ]: 314205 : if (binary && OidIsValid(typclass->typsend))
1876 832 : 115045 : {
833 : : bytea *outputbytes;
834 : : int len;
835 : :
836 : 115045 : pq_sendbyte(out, LOGICALREP_COLUMN_BINARY);
837 : 115045 : outputbytes = OidSendFunctionCall(typclass->typsend, values[i]);
838 : 115045 : len = VARSIZE(outputbytes) - VARHDRSZ;
839 : 115045 : pq_sendint(out, len, 4); /* length */
840 : 115045 : pq_sendbytes(out, VARDATA(outputbytes), len); /* data */
841 : 115045 : pfree(outputbytes);
842 : : }
843 : : else
844 : : {
845 : : char *outputstr;
846 : :
847 : 199160 : pq_sendbyte(out, LOGICALREP_COLUMN_TEXT);
848 : 199160 : outputstr = OidOutputFunctionCall(typclass->typoutput, values[i]);
551 heikki.linnakangas@i 849 : 199160 : pq_sendcountedtext(out, outputstr, strlen(outputstr));
1876 tgl@sss.pgh.pa.us 850 : 199160 : pfree(outputstr);
851 : : }
852 : :
3152 peter_e@gmx.net 853 : 314205 : ReleaseSysCache(typtup);
854 : : }
855 : 182380 : }
856 : :
857 : : /*
858 : : * Read tuple in logical replication format from stream.
859 : : */
860 : : static void
861 : 148285 : logicalrep_read_tuple(StringInfo in, LogicalRepTupleData *tuple)
862 : : {
863 : : int i;
864 : : int natts;
865 : :
866 : : /* Get number of attributes */
867 : 148285 : natts = pq_getmsgint(in, 2);
868 : :
869 : : /* Allocate space for per-column values; zero out unused StringInfoDatas */
1876 tgl@sss.pgh.pa.us 870 : 148285 : tuple->colvalues = (StringInfoData *) palloc0(natts * sizeof(StringInfoData));
871 : 148285 : tuple->colstatus = (char *) palloc(natts * sizeof(char));
1874 872 : 148285 : tuple->ncols = natts;
873 : :
874 : : /* Read the data */
3152 peter_e@gmx.net 875 [ + + ]: 451248 : for (i = 0; i < natts; i++)
876 : : {
877 : : char *buff;
878 : : char kind;
879 : : int len;
1876 tgl@sss.pgh.pa.us 880 : 302963 : StringInfo value = &tuple->colvalues[i];
881 : :
3152 peter_e@gmx.net 882 : 302963 : kind = pq_getmsgbyte(in);
1876 tgl@sss.pgh.pa.us 883 : 302963 : tuple->colstatus[i] = kind;
884 : :
3152 peter_e@gmx.net 885 [ + + + - ]: 302963 : switch (kind)
886 : : {
1876 tgl@sss.pgh.pa.us 887 : 50373 : case LOGICALREP_COLUMN_NULL:
888 : : /* nothing more to do */
3152 peter_e@gmx.net 889 : 50373 : break;
1876 tgl@sss.pgh.pa.us 890 : 3 : case LOGICALREP_COLUMN_UNCHANGED:
891 : : /* we don't receive the value of an unchanged column */
3152 peter_e@gmx.net 892 : 3 : break;
1876 tgl@sss.pgh.pa.us 893 : 252587 : case LOGICALREP_COLUMN_TEXT:
894 : : case LOGICALREP_COLUMN_BINARY:
895 : 252587 : len = pq_getmsgint(in, 4); /* read length */
896 : :
897 : : /* and data */
681 drowley@postgresql.o 898 : 252587 : buff = palloc(len + 1);
899 : 252587 : pq_copymsgbytes(in, buff, len);
900 : :
901 : : /*
902 : : * NUL termination is required for LOGICALREP_COLUMN_TEXT mode
903 : : * as input functions require that. For
904 : : * LOGICALREP_COLUMN_BINARY it's not technically required, but
905 : : * it's harmless.
906 : : */
907 : 252587 : buff[len] = '\0';
908 : :
909 : 252587 : initStringInfoFromString(value, buff, len);
3152 peter_e@gmx.net 910 : 252587 : break;
3152 peter_e@gmx.net 911 :UBC 0 : default:
3046 tgl@sss.pgh.pa.us 912 [ # # ]: 0 : elog(ERROR, "unrecognized data representation type '%c'", kind);
913 : : }
914 : : }
3152 peter_e@gmx.net 915 :CBC 148285 : }
916 : :
917 : : /*
918 : : * Write relation attribute metadata to the stream.
919 : : */
920 : : static void
303 akapila@postgresql.o 921 : 373 : logicalrep_write_attrs(StringInfo out, Relation rel, Bitmapset *columns,
922 : : PublishGencolsType include_gencols_type)
923 : : {
924 : : TupleDesc desc;
925 : : int i;
3152 peter_e@gmx.net 926 : 373 : uint16 nliveatts = 0;
927 : 373 : Bitmapset *idattrs = NULL;
928 : : bool replidentfull;
929 : :
930 : 373 : desc = RelationGetDescr(rel);
931 : :
932 : : /* send number of live attributes */
933 [ + + ]: 1155 : for (i = 0; i < desc->natts; i++)
934 : : {
1260 tomas.vondra@postgre 935 : 782 : Form_pg_attribute att = TupleDescAttr(desc, i);
936 : :
226 akapila@postgresql.o 937 [ + + ]: 782 : if (!logicalrep_should_publish_column(att, columns,
938 : : include_gencols_type))
1260 tomas.vondra@postgre 939 : 71 : continue;
940 : :
3152 peter_e@gmx.net 941 : 711 : nliveatts++;
942 : : }
2887 andres@anarazel.de 943 : 373 : pq_sendint16(out, nliveatts);
944 : :
945 : : /* fetch bitmap of REPLICATION IDENTITY attributes */
3152 peter_e@gmx.net 946 : 373 : replidentfull = (rel->rd_rel->relreplident == REPLICA_IDENTITY_FULL);
947 [ + + ]: 373 : if (!replidentfull)
1593 akapila@postgresql.o 948 : 317 : idattrs = RelationGetIdentityKeyBitmap(rel);
949 : :
950 : : /* send the attributes */
3152 peter_e@gmx.net 951 [ + + ]: 1155 : for (i = 0; i < desc->natts; i++)
952 : : {
2939 andres@anarazel.de 953 : 782 : Form_pg_attribute att = TupleDescAttr(desc, i);
3034 bruce@momjian.us 954 : 782 : uint8 flags = 0;
955 : :
226 akapila@postgresql.o 956 [ + + ]: 782 : if (!logicalrep_should_publish_column(att, columns,
957 : : include_gencols_type))
1260 tomas.vondra@postgre 958 : 71 : continue;
959 : :
960 : : /* REPLICA IDENTITY FULL means all columns are sent as part of key. */
3152 peter_e@gmx.net 961 [ + + + + ]: 1339 : if (replidentfull ||
962 : 628 : bms_is_member(att->attnum - FirstLowInvalidHeapAttributeNumber,
963 : : idattrs))
964 : 337 : flags |= LOGICALREP_IS_REPLICA_IDENTITY;
965 : :
966 : 711 : pq_sendbyte(out, flags);
967 : :
968 : : /* attribute name */
969 : 711 : pq_sendstring(out, NameStr(att->attname));
970 : :
971 : : /* attribute type id */
2887 andres@anarazel.de 972 : 711 : pq_sendint32(out, (int) att->atttypid);
973 : :
974 : : /* attribute mode */
975 : 711 : pq_sendint32(out, att->atttypmod);
976 : : }
977 : :
3152 peter_e@gmx.net 978 : 373 : bms_free(idattrs);
979 : 373 : }
980 : :
981 : : /*
982 : : * Read relation attribute metadata from the stream.
983 : : */
984 : : static void
985 : 426 : logicalrep_read_attrs(StringInfo in, LogicalRepRelation *rel)
986 : : {
987 : : int i;
988 : : int natts;
989 : : char **attnames;
990 : : Oid *atttyps;
991 : 426 : Bitmapset *attkeys = NULL;
992 : :
993 : 426 : natts = pq_getmsgint(in, 2);
994 : 426 : attnames = palloc(natts * sizeof(char *));
995 : 426 : atttyps = palloc(natts * sizeof(Oid));
996 : :
997 : : /* read the attributes */
998 [ + + ]: 1225 : for (i = 0; i < natts; i++)
999 : : {
1000 : : uint8 flags;
1001 : :
1002 : : /* Check for replica identity column */
1003 : 799 : flags = pq_getmsgbyte(in);
1004 [ + + ]: 799 : if (flags & LOGICALREP_IS_REPLICA_IDENTITY)
1005 : 377 : attkeys = bms_add_member(attkeys, i);
1006 : :
1007 : : /* attribute name */
1008 : 799 : attnames[i] = pstrdup(pq_getmsgstring(in));
1009 : :
1010 : : /* attribute type id */
1011 : 799 : atttyps[i] = (Oid) pq_getmsgint(in, 4);
1012 : :
1013 : : /* we ignore attribute mode for now */
1014 : 799 : (void) pq_getmsgint(in, 4);
1015 : : }
1016 : :
1017 : 426 : rel->attnames = attnames;
1018 : 426 : rel->atttyps = atttyps;
1019 : 426 : rel->attkeys = attkeys;
1020 : 426 : rel->natts = natts;
1021 : 426 : }
1022 : :
1023 : : /*
1024 : : * Write the namespace name or empty string for pg_catalog (to save space).
1025 : : */
1026 : : static void
1027 : 391 : logicalrep_write_namespace(StringInfo out, Oid nspid)
1028 : : {
1029 [ + + ]: 391 : if (nspid == PG_CATALOG_NAMESPACE)
1030 : 1 : pq_sendbyte(out, '\0');
1031 : : else
1032 : : {
3034 bruce@momjian.us 1033 : 390 : char *nspname = get_namespace_name(nspid);
1034 : :
3152 peter_e@gmx.net 1035 [ - + ]: 390 : if (nspname == NULL)
3152 peter_e@gmx.net 1036 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for namespace %u",
1037 : : nspid);
1038 : :
3152 peter_e@gmx.net 1039 :CBC 390 : pq_sendstring(out, nspname);
1040 : : }
1041 : 391 : }
1042 : :
1043 : : /*
1044 : : * Read the namespace name while treating empty string as pg_catalog.
1045 : : */
1046 : : static const char *
1047 : 444 : logicalrep_read_namespace(StringInfo in)
1048 : : {
1049 : 444 : const char *nspname = pq_getmsgstring(in);
1050 : :
1051 [ + + ]: 444 : if (nspname[0] == '\0')
1052 : 1 : nspname = "pg_catalog";
1053 : :
1054 : 444 : return nspname;
1055 : : }
1056 : :
1057 : : /*
1058 : : * Write the information for the start stream message to the output stream.
1059 : : */
1060 : : void
1829 akapila@postgresql.o 1061 : 647 : logicalrep_write_stream_start(StringInfo out,
1062 : : TransactionId xid, bool first_segment)
1063 : : {
1769 1064 : 647 : pq_sendbyte(out, LOGICAL_REP_MSG_STREAM_START);
1065 : :
1829 1066 [ - + ]: 647 : Assert(TransactionIdIsValid(xid));
1067 : :
1068 : : /* transaction ID (we're starting to stream, so must be valid) */
1069 : 647 : pq_sendint32(out, xid);
1070 : :
1071 : : /* 1 if this is the first streaming segment for this xid */
1072 : 647 : pq_sendbyte(out, first_segment ? 1 : 0);
1073 : 647 : }
1074 : :
1075 : : /*
1076 : : * Read the information about the start stream message from output stream.
1077 : : */
1078 : : TransactionId
1079 : 857 : logicalrep_read_stream_start(StringInfo in, bool *first_segment)
1080 : : {
1081 : : TransactionId xid;
1082 : :
1083 [ - + ]: 857 : Assert(first_segment);
1084 : :
1085 : 857 : xid = pq_getmsgint(in, 4);
1086 : 857 : *first_segment = (pq_getmsgbyte(in) == 1);
1087 : :
1088 : 857 : return xid;
1089 : : }
1090 : :
1091 : : /*
1092 : : * Write the stop stream message to the output stream.
1093 : : */
1094 : : void
1095 : 647 : logicalrep_write_stream_stop(StringInfo out)
1096 : : {
1479 1097 : 647 : pq_sendbyte(out, LOGICAL_REP_MSG_STREAM_STOP);
1829 1098 : 647 : }
1099 : :
1100 : : /*
1101 : : * Write STREAM COMMIT to the output stream.
1102 : : */
1103 : : void
1104 : 46 : logicalrep_write_stream_commit(StringInfo out, ReorderBufferTXN *txn,
1105 : : XLogRecPtr commit_lsn)
1106 : : {
1107 : 46 : uint8 flags = 0;
1108 : :
1769 1109 : 46 : pq_sendbyte(out, LOGICAL_REP_MSG_STREAM_COMMIT);
1110 : :
1829 1111 [ - + ]: 46 : Assert(TransactionIdIsValid(txn->xid));
1112 : :
1113 : : /* transaction ID */
1114 : 46 : pq_sendint32(out, txn->xid);
1115 : :
1116 : : /* send the flags field (unused for now) */
1117 : 46 : pq_sendbyte(out, flags);
1118 : :
1119 : : /* send fields */
1120 : 46 : pq_sendint64(out, commit_lsn);
1121 : 46 : pq_sendint64(out, txn->end_lsn);
1515 1122 : 46 : pq_sendint64(out, txn->xact_time.commit_time);
1829 1123 : 46 : }
1124 : :
1125 : : /*
1126 : : * Read STREAM COMMIT from the output stream.
1127 : : */
1128 : : TransactionId
1129 : 61 : logicalrep_read_stream_commit(StringInfo in, LogicalRepCommitData *commit_data)
1130 : : {
1131 : : TransactionId xid;
1132 : : uint8 flags;
1133 : :
1134 : 61 : xid = pq_getmsgint(in, 4);
1135 : :
1136 : : /* read flags (unused for now) */
1137 : 61 : flags = pq_getmsgbyte(in);
1138 : :
1139 [ - + ]: 61 : if (flags != 0)
1829 akapila@postgresql.o 1140 [ # # ]:UBC 0 : elog(ERROR, "unrecognized flags %u in commit message", flags);
1141 : :
1142 : : /* read fields */
1829 akapila@postgresql.o 1143 :CBC 61 : commit_data->commit_lsn = pq_getmsgint64(in);
1144 : 61 : commit_data->end_lsn = pq_getmsgint64(in);
1145 : 61 : commit_data->committime = pq_getmsgint64(in);
1146 : :
1147 : 61 : return xid;
1148 : : }
1149 : :
1150 : : /*
1151 : : * Write STREAM ABORT to the output stream. Note that xid and subxid will be
1152 : : * same for the top-level transaction abort.
1153 : : *
1154 : : * If write_abort_info is true, send the abort_lsn and abort_time fields,
1155 : : * otherwise don't.
1156 : : */
1157 : : void
1158 : 26 : logicalrep_write_stream_abort(StringInfo out, TransactionId xid,
1159 : : TransactionId subxid, XLogRecPtr abort_lsn,
1160 : : TimestampTz abort_time, bool write_abort_info)
1161 : : {
1769 1162 : 26 : pq_sendbyte(out, LOGICAL_REP_MSG_STREAM_ABORT);
1163 : :
1829 1164 [ + - - + ]: 26 : Assert(TransactionIdIsValid(xid) && TransactionIdIsValid(subxid));
1165 : :
1166 : : /* transaction ID */
1167 : 26 : pq_sendint32(out, xid);
1168 : 26 : pq_sendint32(out, subxid);
1169 : :
971 1170 [ + + ]: 26 : if (write_abort_info)
1171 : : {
1172 : 12 : pq_sendint64(out, abort_lsn);
1173 : 12 : pq_sendint64(out, abort_time);
1174 : : }
1829 1175 : 26 : }
1176 : :
1177 : : /*
1178 : : * Read STREAM ABORT from the output stream.
1179 : : *
1180 : : * If read_abort_info is true, read the abort_lsn and abort_time fields,
1181 : : * otherwise don't.
1182 : : */
1183 : : void
971 1184 : 38 : logicalrep_read_stream_abort(StringInfo in,
1185 : : LogicalRepStreamAbortData *abort_data,
1186 : : bool read_abort_info)
1187 : : {
1188 [ - + ]: 38 : Assert(abort_data);
1189 : :
1190 : 38 : abort_data->xid = pq_getmsgint(in, 4);
1191 : 38 : abort_data->subxid = pq_getmsgint(in, 4);
1192 : :
1193 [ + + ]: 38 : if (read_abort_info)
1194 : : {
1195 : 24 : abort_data->abort_lsn = pq_getmsgint64(in);
1196 : 24 : abort_data->abort_time = pq_getmsgint64(in);
1197 : : }
1198 : : else
1199 : : {
1200 : 14 : abort_data->abort_lsn = InvalidXLogRecPtr;
1201 : 14 : abort_data->abort_time = 0;
1202 : : }
1829 1203 : 38 : }
1204 : :
1205 : : /*
1206 : : * Get string representing LogicalRepMsgType.
1207 : : */
1208 : : const char *
1471 1209 : 392 : logicalrep_message_type(LogicalRepMsgType action)
1210 : : {
1211 : : static char err_unknown[20];
1212 : :
1213 [ + + - + : 392 : switch (action)
+ + - + -
- + + - -
+ + + + +
- ]
1214 : : {
1215 : 1 : case LOGICAL_REP_MSG_BEGIN:
1216 : 1 : return "BEGIN";
1217 : 1 : case LOGICAL_REP_MSG_COMMIT:
1218 : 1 : return "COMMIT";
1471 akapila@postgresql.o 1219 :UBC 0 : case LOGICAL_REP_MSG_ORIGIN:
1220 : 0 : return "ORIGIN";
1471 akapila@postgresql.o 1221 :CBC 57 : case LOGICAL_REP_MSG_INSERT:
1222 : 57 : return "INSERT";
1223 : 23 : case LOGICAL_REP_MSG_UPDATE:
1224 : 23 : return "UPDATE";
1225 : 13 : case LOGICAL_REP_MSG_DELETE:
1226 : 13 : return "DELETE";
1471 akapila@postgresql.o 1227 :UBC 0 : case LOGICAL_REP_MSG_TRUNCATE:
1228 : 0 : return "TRUNCATE";
1471 akapila@postgresql.o 1229 :CBC 2 : case LOGICAL_REP_MSG_RELATION:
1230 : 2 : return "RELATION";
1471 akapila@postgresql.o 1231 :UBC 0 : case LOGICAL_REP_MSG_TYPE:
1232 : 0 : return "TYPE";
1233 : 0 : case LOGICAL_REP_MSG_MESSAGE:
1234 : 0 : return "MESSAGE";
1471 akapila@postgresql.o 1235 :CBC 1 : case LOGICAL_REP_MSG_BEGIN_PREPARE:
1236 : 1 : return "BEGIN PREPARE";
1237 : 2 : case LOGICAL_REP_MSG_PREPARE:
1238 : 2 : return "PREPARE";
1471 akapila@postgresql.o 1239 :UBC 0 : case LOGICAL_REP_MSG_COMMIT_PREPARED:
1240 : 0 : return "COMMIT PREPARED";
1241 : 0 : case LOGICAL_REP_MSG_ROLLBACK_PREPARED:
1242 : 0 : return "ROLLBACK PREPARED";
1471 akapila@postgresql.o 1243 :CBC 13 : case LOGICAL_REP_MSG_STREAM_START:
1244 : 13 : return "STREAM START";
1245 : 238 : case LOGICAL_REP_MSG_STREAM_STOP:
1246 : 238 : return "STREAM STOP";
1247 : 20 : case LOGICAL_REP_MSG_STREAM_COMMIT:
1248 : 20 : return "STREAM COMMIT";
1249 : 19 : case LOGICAL_REP_MSG_STREAM_ABORT:
1250 : 19 : return "STREAM ABORT";
1251 : 2 : case LOGICAL_REP_MSG_STREAM_PREPARE:
1252 : 2 : return "STREAM PREPARE";
1253 : : }
1254 : :
1255 : : /*
1256 : : * This message provides context in the error raised when applying a
1257 : : * logical message. So we can't throw an error here. Return an unknown
1258 : : * indicator value so that the original error is still reported.
1259 : : */
774 akapila@postgresql.o 1260 :UBC 0 : snprintf(err_unknown, sizeof(err_unknown), "??? (%d)", action);
1261 : :
1262 : 0 : return err_unknown;
1263 : : }
1264 : :
1265 : : /*
1266 : : * Check if the column 'att' of a table should be published.
1267 : : *
1268 : : * 'columns' represents the publication column list (if any) for that table.
1269 : : *
1270 : : * 'include_gencols_type' value indicates whether generated columns should be
1271 : : * published when there is no column list. Typically, this will have the same
1272 : : * value as the 'publish_generated_columns' publication parameter.
1273 : : *
1274 : : * Note that generated columns can be published only when present in a
1275 : : * publication column list, or when include_gencols_type is
1276 : : * PUBLISH_GENCOLS_STORED.
1277 : : */
1278 : : bool
303 akapila@postgresql.o 1279 :CBC 734898 : logicalrep_should_publish_column(Form_pg_attribute att, Bitmapset *columns,
1280 : : PublishGencolsType include_gencols_type)
1281 : : {
311 1282 [ + + ]: 734898 : if (att->attisdropped)
1283 : 51 : return false;
1284 : :
1285 : : /* If a column list is provided, publish only the cols in that list. */
303 1286 [ + + ]: 734847 : if (columns)
1287 : 949 : return bms_is_member(att->attnum, columns);
1288 : :
1289 : : /* All non-generated columns are always published. */
226 1290 [ + + ]: 733898 : if (!att->attgenerated)
1291 : 733841 : return true;
1292 : :
1293 : : /*
1294 : : * Stored generated columns are only published when the user sets
1295 : : * publish_generated_columns as stored.
1296 : : */
1297 [ + + ]: 57 : if (att->attgenerated == ATTRIBUTE_GENERATED_STORED)
1298 : 33 : return include_gencols_type == PUBLISH_GENCOLS_STORED;
1299 : :
1300 : 24 : return false;
1301 : : }
|