Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * test_decoding.c
4 : : * example logical decoding output plugin
5 : : *
6 : : * Copyright (c) 2012-2026, PostgreSQL Global Development Group
7 : : *
8 : : * IDENTIFICATION
9 : : * contrib/test_decoding/test_decoding.c
10 : : *
11 : : *-------------------------------------------------------------------------
12 : : */
13 : : #include "postgres.h"
14 : :
15 : : #include "catalog/pg_type.h"
16 : :
17 : : #include "replication/logical.h"
18 : : #include "replication/origin.h"
19 : :
20 : : #include "utils/builtins.h"
21 : : #include "utils/lsyscache.h"
22 : : #include "utils/memutils.h"
23 : : #include "utils/rel.h"
24 : :
25 : 155 : PG_MODULE_MAGIC_EXT(
26 : : .name = "test_decoding",
27 : : .version = PG_VERSION
28 : : );
29 : :
30 : : typedef struct
31 : : {
32 : : MemoryContext context;
33 : : bool include_xids;
34 : : bool include_timestamp;
35 : : bool skip_empty_xacts;
36 : : bool only_local;
37 : : } TestDecodingData;
38 : :
39 : : /*
40 : : * Maintain the per-transaction level variables to track whether the
41 : : * transaction and or streams have written any changes. In streaming mode the
42 : : * transaction can be decoded in streams so along with maintaining whether the
43 : : * transaction has written any changes, we also need to track whether the
44 : : * current stream has written any changes. This is required so that if user
45 : : * has requested to skip the empty transactions we can skip the empty streams
46 : : * even though the transaction has written some changes.
47 : : */
48 : : typedef struct
49 : : {
50 : : bool xact_wrote_changes;
51 : : bool stream_wrote_changes;
52 : : } TestDecodingTxnData;
53 : :
54 : : static void pg_decode_startup(LogicalDecodingContext *ctx, OutputPluginOptions *opt,
55 : : bool is_init);
56 : : static void pg_decode_shutdown(LogicalDecodingContext *ctx);
57 : : static void pg_decode_begin_txn(LogicalDecodingContext *ctx,
58 : : ReorderBufferTXN *txn);
59 : : static void pg_output_begin(LogicalDecodingContext *ctx,
60 : : TestDecodingData *data,
61 : : ReorderBufferTXN *txn,
62 : : bool last_write);
63 : : static void pg_decode_commit_txn(LogicalDecodingContext *ctx,
64 : : ReorderBufferTXN *txn, XLogRecPtr commit_lsn);
65 : : static void pg_decode_change(LogicalDecodingContext *ctx,
66 : : ReorderBufferTXN *txn, Relation relation,
67 : : ReorderBufferChange *change);
68 : : static void pg_decode_truncate(LogicalDecodingContext *ctx,
69 : : ReorderBufferTXN *txn,
70 : : int nrelations, Relation relations[],
71 : : ReorderBufferChange *change);
72 : : static bool pg_decode_filter(LogicalDecodingContext *ctx,
73 : : ReplOriginId origin_id);
74 : : static void pg_decode_message(LogicalDecodingContext *ctx,
75 : : ReorderBufferTXN *txn, TransactionId xid,
76 : : XLogRecPtr lsn, bool transactional,
77 : : const char *prefix, Size sz, const char *message);
78 : : static bool pg_decode_filter_prepare(LogicalDecodingContext *ctx,
79 : : TransactionId xid,
80 : : const char *gid);
81 : : static void pg_decode_begin_prepare_txn(LogicalDecodingContext *ctx,
82 : : ReorderBufferTXN *txn);
83 : : static void pg_decode_prepare_txn(LogicalDecodingContext *ctx,
84 : : ReorderBufferTXN *txn,
85 : : XLogRecPtr prepare_lsn);
86 : : static void pg_decode_commit_prepared_txn(LogicalDecodingContext *ctx,
87 : : ReorderBufferTXN *txn,
88 : : XLogRecPtr commit_lsn);
89 : : static void pg_decode_rollback_prepared_txn(LogicalDecodingContext *ctx,
90 : : ReorderBufferTXN *txn,
91 : : XLogRecPtr prepare_end_lsn,
92 : : TimestampTz prepare_time);
93 : : static void pg_decode_stream_start(LogicalDecodingContext *ctx,
94 : : ReorderBufferTXN *txn);
95 : : static void pg_output_stream_start(LogicalDecodingContext *ctx,
96 : : TestDecodingData *data,
97 : : ReorderBufferTXN *txn,
98 : : bool last_write);
99 : : static void pg_decode_stream_stop(LogicalDecodingContext *ctx,
100 : : ReorderBufferTXN *txn);
101 : : static void pg_decode_stream_abort(LogicalDecodingContext *ctx,
102 : : ReorderBufferTXN *txn,
103 : : XLogRecPtr abort_lsn);
104 : : static void pg_decode_stream_prepare(LogicalDecodingContext *ctx,
105 : : ReorderBufferTXN *txn,
106 : : XLogRecPtr prepare_lsn);
107 : : static void pg_decode_stream_commit(LogicalDecodingContext *ctx,
108 : : ReorderBufferTXN *txn,
109 : : XLogRecPtr commit_lsn);
110 : : static void pg_decode_stream_change(LogicalDecodingContext *ctx,
111 : : ReorderBufferTXN *txn,
112 : : Relation relation,
113 : : ReorderBufferChange *change);
114 : : static void pg_decode_stream_message(LogicalDecodingContext *ctx,
115 : : ReorderBufferTXN *txn, TransactionId xid,
116 : : XLogRecPtr lsn, bool transactional,
117 : : const char *prefix, Size sz,
118 : : const char *message);
119 : : static void pg_decode_stream_truncate(LogicalDecodingContext *ctx,
120 : : ReorderBufferTXN *txn,
121 : : int nrelations, Relation relations[],
122 : : ReorderBufferChange *change);
123 : :
124 : : void
125 : 155 : _PG_init(void)
126 : : {
127 : : /* other plugins can perform things here */
128 : 155 : }
129 : :
130 : : /* specify output plugin callbacks */
131 : : void
132 : 399 : _PG_output_plugin_init(OutputPluginCallbacks *cb)
133 : : {
134 : 399 : cb->startup_cb = pg_decode_startup;
135 : 399 : cb->begin_cb = pg_decode_begin_txn;
136 : 399 : cb->change_cb = pg_decode_change;
137 : 399 : cb->truncate_cb = pg_decode_truncate;
138 : 399 : cb->commit_cb = pg_decode_commit_txn;
139 : 399 : cb->filter_by_origin_cb = pg_decode_filter;
140 : 399 : cb->shutdown_cb = pg_decode_shutdown;
141 : 399 : cb->message_cb = pg_decode_message;
142 : 399 : cb->filter_prepare_cb = pg_decode_filter_prepare;
143 : 399 : cb->begin_prepare_cb = pg_decode_begin_prepare_txn;
144 : 399 : cb->prepare_cb = pg_decode_prepare_txn;
145 : 399 : cb->commit_prepared_cb = pg_decode_commit_prepared_txn;
146 : 399 : cb->rollback_prepared_cb = pg_decode_rollback_prepared_txn;
147 : 399 : cb->stream_start_cb = pg_decode_stream_start;
148 : 399 : cb->stream_stop_cb = pg_decode_stream_stop;
149 : 399 : cb->stream_abort_cb = pg_decode_stream_abort;
150 : 399 : cb->stream_prepare_cb = pg_decode_stream_prepare;
151 : 399 : cb->stream_commit_cb = pg_decode_stream_commit;
152 : 399 : cb->stream_change_cb = pg_decode_stream_change;
153 : 399 : cb->stream_message_cb = pg_decode_stream_message;
154 : 399 : cb->stream_truncate_cb = pg_decode_stream_truncate;
155 : 399 : }
156 : :
157 : :
158 : : /* initialize this plugin */
159 : : static void
160 : 399 : pg_decode_startup(LogicalDecodingContext *ctx, OutputPluginOptions *opt,
161 : : bool is_init)
162 : : {
163 : : ListCell *option;
164 : : TestDecodingData *data;
165 : 399 : bool enable_streaming = false;
166 : :
167 : 399 : data = palloc0_object(TestDecodingData);
168 : 399 : data->context = AllocSetContextCreate(ctx->context,
169 : : "text conversion context",
170 : : ALLOCSET_DEFAULT_SIZES);
171 : 399 : data->include_xids = true;
172 : 399 : data->include_timestamp = false;
173 : 399 : data->skip_empty_xacts = false;
174 : 399 : data->only_local = false;
175 : :
176 : 399 : ctx->output_plugin_private = data;
177 : :
178 : 399 : opt->output_type = OUTPUT_PLUGIN_TEXTUAL_OUTPUT;
179 : 399 : opt->receive_rewrites = false;
180 : :
181 [ + + + + : 803 : foreach(option, ctx->output_plugin_options)
+ + ]
182 : : {
183 : 407 : DefElem *elem = lfirst(option);
184 : :
185 : : Assert(elem->arg == NULL || IsA(elem->arg, String));
186 : :
187 [ + + ]: 407 : if (strcmp(elem->defname, "include-xids") == 0)
188 : : {
189 : : /* if option does not provide a value, it means its value is true */
190 [ - + ]: 189 : if (elem->arg == NULL)
191 : 0 : data->include_xids = true;
192 [ + + ]: 189 : else if (!parse_bool(strVal(elem->arg), &data->include_xids))
193 [ + - ]: 2 : ereport(ERROR,
194 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
195 : : errmsg("could not parse value \"%s\" for parameter \"%s\"",
196 : : strVal(elem->arg), elem->defname)));
197 : : }
198 [ + + ]: 218 : else if (strcmp(elem->defname, "include-timestamp") == 0)
199 : : {
200 [ - + ]: 1 : if (elem->arg == NULL)
201 : 0 : data->include_timestamp = true;
202 [ - + ]: 1 : else if (!parse_bool(strVal(elem->arg), &data->include_timestamp))
203 [ # # ]: 0 : ereport(ERROR,
204 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
205 : : errmsg("could not parse value \"%s\" for parameter \"%s\"",
206 : : strVal(elem->arg), elem->defname)));
207 : : }
208 [ + + ]: 217 : else if (strcmp(elem->defname, "force-binary") == 0)
209 : : {
210 : : bool force_binary;
211 : :
212 [ - + ]: 8 : if (elem->arg == NULL)
213 : 0 : continue;
214 [ - + ]: 8 : else if (!parse_bool(strVal(elem->arg), &force_binary))
215 [ # # ]: 0 : ereport(ERROR,
216 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
217 : : errmsg("could not parse value \"%s\" for parameter \"%s\"",
218 : : strVal(elem->arg), elem->defname)));
219 : :
220 [ + + ]: 8 : if (force_binary)
221 : 2 : opt->output_type = OUTPUT_PLUGIN_BINARY_OUTPUT;
222 : : }
223 [ + + ]: 209 : else if (strcmp(elem->defname, "skip-empty-xacts") == 0)
224 : : {
225 : :
226 [ - + ]: 190 : if (elem->arg == NULL)
227 : 0 : data->skip_empty_xacts = true;
228 [ - + ]: 190 : else if (!parse_bool(strVal(elem->arg), &data->skip_empty_xacts))
229 [ # # ]: 0 : ereport(ERROR,
230 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
231 : : errmsg("could not parse value \"%s\" for parameter \"%s\"",
232 : : strVal(elem->arg), elem->defname)));
233 : : }
234 [ + + ]: 19 : else if (strcmp(elem->defname, "only-local") == 0)
235 : : {
236 : :
237 [ - + ]: 3 : if (elem->arg == NULL)
238 : 0 : data->only_local = true;
239 [ - + ]: 3 : else if (!parse_bool(strVal(elem->arg), &data->only_local))
240 [ # # ]: 0 : ereport(ERROR,
241 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
242 : : errmsg("could not parse value \"%s\" for parameter \"%s\"",
243 : : strVal(elem->arg), elem->defname)));
244 : : }
245 [ + + ]: 16 : else if (strcmp(elem->defname, "include-rewrites") == 0)
246 : : {
247 : :
248 [ - + ]: 3 : if (elem->arg == NULL)
249 : 0 : continue;
250 [ - + ]: 3 : else if (!parse_bool(strVal(elem->arg), &opt->receive_rewrites))
251 [ # # ]: 0 : ereport(ERROR,
252 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
253 : : errmsg("could not parse value \"%s\" for parameter \"%s\"",
254 : : strVal(elem->arg), elem->defname)));
255 : : }
256 [ + + ]: 13 : else if (strcmp(elem->defname, "stream-changes") == 0)
257 : : {
258 [ - + ]: 12 : if (elem->arg == NULL)
259 : 0 : continue;
260 [ - + ]: 12 : else if (!parse_bool(strVal(elem->arg), &enable_streaming))
261 [ # # ]: 0 : ereport(ERROR,
262 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
263 : : errmsg("could not parse value \"%s\" for parameter \"%s\"",
264 : : strVal(elem->arg), elem->defname)));
265 : : }
266 : : else
267 : : {
268 [ + - + - ]: 1 : ereport(ERROR,
269 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
270 : : errmsg("option \"%s\" = \"%s\" is unknown",
271 : : elem->defname,
272 : : elem->arg ? strVal(elem->arg) : "(null)")));
273 : : }
274 : : }
275 : :
276 : 396 : ctx->streaming &= enable_streaming;
277 : 396 : }
278 : :
279 : : /* cleanup this plugin's resources */
280 : : static void
281 : 383 : pg_decode_shutdown(LogicalDecodingContext *ctx)
282 : : {
283 : 383 : TestDecodingData *data = ctx->output_plugin_private;
284 : :
285 : : /* cleanup our own resources via memory context reset */
286 : 383 : MemoryContextDelete(data->context);
287 : 383 : }
288 : :
289 : : /* BEGIN callback */
290 : : static void
291 : 469 : pg_decode_begin_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
292 : : {
293 : 469 : TestDecodingData *data = ctx->output_plugin_private;
294 : : TestDecodingTxnData *txndata =
295 : 469 : MemoryContextAllocZero(ctx->context, sizeof(TestDecodingTxnData));
296 : :
297 : 469 : txndata->xact_wrote_changes = false;
298 : 469 : txn->output_plugin_private = txndata;
299 : :
300 : : /*
301 : : * If asked to skip empty transactions, we'll emit BEGIN at the point
302 : : * where the first operation is received for this transaction.
303 : : */
304 [ + + ]: 469 : if (data->skip_empty_xacts)
305 : 425 : return;
306 : :
307 : 44 : pg_output_begin(ctx, data, txn, true);
308 : : }
309 : :
310 : : static void
311 : 291 : pg_output_begin(LogicalDecodingContext *ctx, TestDecodingData *data, ReorderBufferTXN *txn, bool last_write)
312 : : {
313 : 291 : OutputPluginPrepareWrite(ctx, last_write);
314 [ + + ]: 291 : if (data->include_xids)
315 : 42 : appendStringInfo(ctx->out, "BEGIN %u", txn->xid);
316 : : else
317 : 249 : appendStringInfoString(ctx->out, "BEGIN");
318 : 291 : OutputPluginWrite(ctx, last_write);
319 : 291 : }
320 : :
321 : : /* COMMIT callback */
322 : : static void
323 : 467 : pg_decode_commit_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
324 : : XLogRecPtr commit_lsn)
325 : : {
326 : 467 : TestDecodingData *data = ctx->output_plugin_private;
327 : 467 : TestDecodingTxnData *txndata = txn->output_plugin_private;
328 : 467 : bool xact_wrote_changes = txndata->xact_wrote_changes;
329 : :
330 : 467 : pfree(txndata);
331 : 467 : txn->output_plugin_private = NULL;
332 : :
333 [ + + + + ]: 467 : if (data->skip_empty_xacts && !xact_wrote_changes)
334 : 186 : return;
335 : :
336 : 281 : OutputPluginPrepareWrite(ctx, true);
337 [ + + ]: 281 : if (data->include_xids)
338 : 39 : appendStringInfo(ctx->out, "COMMIT %u", txn->xid);
339 : : else
340 : 242 : appendStringInfoString(ctx->out, "COMMIT");
341 : :
342 [ + + ]: 281 : if (data->include_timestamp)
343 : 1 : appendStringInfo(ctx->out, " (at %s)",
344 : : timestamptz_to_str(txn->commit_time));
345 : :
346 : 281 : OutputPluginWrite(ctx, true);
347 : : }
348 : :
349 : : /* BEGIN PREPARE callback */
350 : : static void
351 : 9 : pg_decode_begin_prepare_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
352 : : {
353 : 9 : TestDecodingData *data = ctx->output_plugin_private;
354 : : TestDecodingTxnData *txndata =
355 : 9 : MemoryContextAllocZero(ctx->context, sizeof(TestDecodingTxnData));
356 : :
357 : 9 : txndata->xact_wrote_changes = false;
358 : 9 : txn->output_plugin_private = txndata;
359 : :
360 : : /*
361 : : * If asked to skip empty transactions, we'll emit BEGIN at the point
362 : : * where the first operation is received for this transaction.
363 : : */
364 [ + + ]: 9 : if (data->skip_empty_xacts)
365 : 8 : return;
366 : :
367 : 1 : pg_output_begin(ctx, data, txn, true);
368 : : }
369 : :
370 : : /* PREPARE callback */
371 : : static void
372 : 9 : pg_decode_prepare_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
373 : : XLogRecPtr prepare_lsn)
374 : : {
375 : 9 : TestDecodingData *data = ctx->output_plugin_private;
376 : 9 : TestDecodingTxnData *txndata = txn->output_plugin_private;
377 : :
378 : : /*
379 : : * If asked to skip empty transactions, we'll emit PREPARE at the point
380 : : * where the first operation is received for this transaction.
381 : : */
382 [ + + + + ]: 9 : if (data->skip_empty_xacts && !txndata->xact_wrote_changes)
383 : 1 : return;
384 : :
385 : 8 : OutputPluginPrepareWrite(ctx, true);
386 : :
387 : 8 : appendStringInfo(ctx->out, "PREPARE TRANSACTION %s",
388 : 8 : quote_literal_cstr(txn->gid));
389 : :
390 [ + + ]: 8 : if (data->include_xids)
391 : 1 : appendStringInfo(ctx->out, ", txid %u", txn->xid);
392 : :
393 [ - + ]: 8 : if (data->include_timestamp)
394 : 0 : appendStringInfo(ctx->out, " (at %s)",
395 : : timestamptz_to_str(txn->prepare_time));
396 : :
397 : 8 : OutputPluginWrite(ctx, true);
398 : : }
399 : :
400 : : /* COMMIT PREPARED callback */
401 : : static void
402 : 8 : pg_decode_commit_prepared_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
403 : : XLogRecPtr commit_lsn)
404 : : {
405 : 8 : TestDecodingData *data = ctx->output_plugin_private;
406 : :
407 : 8 : OutputPluginPrepareWrite(ctx, true);
408 : :
409 : 8 : appendStringInfo(ctx->out, "COMMIT PREPARED %s",
410 : 8 : quote_literal_cstr(txn->gid));
411 : :
412 [ + + ]: 8 : if (data->include_xids)
413 : 1 : appendStringInfo(ctx->out, ", txid %u", txn->xid);
414 : :
415 [ - + ]: 8 : if (data->include_timestamp)
416 : 0 : appendStringInfo(ctx->out, " (at %s)",
417 : : timestamptz_to_str(txn->commit_time));
418 : :
419 : 8 : OutputPluginWrite(ctx, true);
420 : 8 : }
421 : :
422 : : /* ROLLBACK PREPARED callback */
423 : : static void
424 : 2 : pg_decode_rollback_prepared_txn(LogicalDecodingContext *ctx,
425 : : ReorderBufferTXN *txn,
426 : : XLogRecPtr prepare_end_lsn,
427 : : TimestampTz prepare_time)
428 : : {
429 : 2 : TestDecodingData *data = ctx->output_plugin_private;
430 : :
431 : 2 : OutputPluginPrepareWrite(ctx, true);
432 : :
433 : 2 : appendStringInfo(ctx->out, "ROLLBACK PREPARED %s",
434 : 2 : quote_literal_cstr(txn->gid));
435 : :
436 [ - + ]: 2 : if (data->include_xids)
437 : 0 : appendStringInfo(ctx->out, ", txid %u", txn->xid);
438 : :
439 [ - + ]: 2 : if (data->include_timestamp)
440 : 0 : appendStringInfo(ctx->out, " (at %s)",
441 : : timestamptz_to_str(txn->commit_time));
442 : :
443 : 2 : OutputPluginWrite(ctx, true);
444 : 2 : }
445 : :
446 : : /*
447 : : * Filter out two-phase transactions.
448 : : *
449 : : * Each plugin can implement its own filtering logic. Here we demonstrate a
450 : : * simple logic by checking the GID. If the GID contains the "_nodecode"
451 : : * substring, then we filter it out.
452 : : */
453 : : static bool
454 : 170 : pg_decode_filter_prepare(LogicalDecodingContext *ctx, TransactionId xid,
455 : : const char *gid)
456 : : {
457 [ + + ]: 170 : if (strstr(gid, "_nodecode") != NULL)
458 : 14 : return true;
459 : :
460 : 156 : return false;
461 : : }
462 : :
463 : : static bool
464 : 1252447 : pg_decode_filter(LogicalDecodingContext *ctx,
465 : : ReplOriginId origin_id)
466 : : {
467 : 1252447 : TestDecodingData *data = ctx->output_plugin_private;
468 : :
469 [ + + + + ]: 1252447 : if (data->only_local && origin_id != InvalidReplOriginId)
470 : 9 : return true;
471 : 1252438 : return false;
472 : : }
473 : :
474 : : /*
475 : : * Print literal `outputstr' already represented as string of type `typid'
476 : : * into stringbuf `s'.
477 : : *
478 : : * Some builtin types aren't quoted, the rest is quoted. Escaping is done
479 : : * per standard SQL rules.
480 : : */
481 : : static void
482 : 176177 : print_literal(StringInfo s, Oid typid, char *outputstr)
483 : : {
484 : : const char *valptr;
485 : :
486 [ + - - + ]: 176177 : switch (typid)
487 : : {
488 : 60381 : case INT2OID:
489 : : case INT4OID:
490 : : case INT8OID:
491 : : case OIDOID:
492 : : case FLOAT4OID:
493 : : case FLOAT8OID:
494 : : case NUMERICOID:
495 : : /* NB: We don't care about Inf, NaN et al. */
496 : 60381 : appendStringInfoString(s, outputstr);
497 : 60381 : break;
498 : :
499 : 0 : case BITOID:
500 : : case VARBITOID:
501 : 0 : appendStringInfo(s, "B'%s'", outputstr);
502 : 0 : break;
503 : :
504 : 0 : case BOOLOID:
505 [ # # ]: 0 : if (strcmp(outputstr, "t") == 0)
506 : 0 : appendStringInfoString(s, "true");
507 : : else
508 : 0 : appendStringInfoString(s, "false");
509 : 0 : break;
510 : :
511 : 115796 : default:
512 : 115796 : appendStringInfoChar(s, '\'');
513 [ + + ]: 5856589 : for (valptr = outputstr; *valptr; valptr++)
514 : : {
515 : 5740793 : char ch = *valptr;
516 : :
517 [ + + ]: 5740793 : if (SQL_STR_DOUBLE(ch, false))
518 : 64 : appendStringInfoChar(s, ch);
519 : 5740793 : appendStringInfoChar(s, ch);
520 : : }
521 : 115796 : appendStringInfoChar(s, '\'');
522 : 115796 : break;
523 : : }
524 : 176177 : }
525 : :
526 : : /* print the tuple 'tuple' into the StringInfo s */
527 : : static void
528 : 145686 : tuple_to_stringinfo(StringInfo s, TupleDesc tupdesc, HeapTuple tuple, bool skip_nulls)
529 : : {
530 : : int natt;
531 : :
532 : : /* print all columns individually */
533 [ + + ]: 347627 : for (natt = 0; natt < tupdesc->natts; natt++)
534 : : {
535 : : Form_pg_attribute attr; /* the attribute itself */
536 : : Oid typid; /* type of current attribute */
537 : : Oid typoutput; /* output function */
538 : : bool typisvarlena;
539 : : Datum origval; /* possibly toasted Datum */
540 : : bool isnull; /* column is null? */
541 : :
542 : 201943 : attr = TupleDescAttr(tupdesc, natt);
543 : :
544 : : /*
545 : : * don't print dropped columns, we can't be sure everything is
546 : : * available for them
547 : : */
548 [ + + ]: 201943 : if (attr->attisdropped)
549 : 5134 : continue;
550 : :
551 : : /*
552 : : * Don't print system columns, oid will already have been printed if
553 : : * present.
554 : : */
555 [ - + ]: 201871 : if (attr->attnum < 0)
556 : 0 : continue;
557 : :
558 : : /*
559 : : * Virtual generated columns are always stored as null in the tuple,
560 : : * so don't print them at all. A printed null would not be
561 : : * distinguishable from a column that really contains a null. Stored
562 : : * generated columns are printed as usual since their values are
563 : : * actually on disk.
564 : : */
565 [ + + ]: 201871 : if (attr->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)
566 : 1 : continue;
567 : :
568 : 201870 : typid = attr->atttypid;
569 : :
570 : : /* get Datum from tuple */
571 : 201870 : origval = heap_getattr(tuple, natt + 1, tupdesc, &isnull);
572 : :
573 [ + + + + ]: 201870 : if (isnull && skip_nulls)
574 : 5061 : continue;
575 : :
576 : : /* print attribute name */
577 : 196809 : appendStringInfoChar(s, ' ');
578 : 196809 : appendStringInfoString(s, quote_identifier(NameStr(attr->attname)));
579 : :
580 : : /* print attribute type */
581 : 196809 : appendStringInfoChar(s, '[');
582 : 196809 : appendStringInfoString(s, format_type_be(typid));
583 : 196809 : appendStringInfoChar(s, ']');
584 : :
585 : : /* query output function */
586 : 196809 : getTypeOutputInfo(typid,
587 : : &typoutput, &typisvarlena);
588 : :
589 : : /* print separator */
590 : 196809 : appendStringInfoChar(s, ':');
591 : :
592 : : /* print data */
593 [ + + ]: 196809 : if (isnull)
594 : 20617 : appendStringInfoString(s, "null");
595 [ + + + + ]: 176192 : else if (typisvarlena && VARATT_IS_EXTERNAL_ONDISK(DatumGetPointer(origval)))
596 : 13 : appendStringInfoString(s, "unchanged-toast-datum");
597 [ + + ]: 176179 : else if (!typisvarlena)
598 : 60385 : print_literal(s, typid,
599 : : OidOutputFunctionCall(typoutput, origval));
600 : : else
601 : : {
602 : : Datum val; /* definitely detoasted Datum */
603 : :
604 : 115794 : val = PointerGetDatum(PG_DETOAST_DATUM(origval));
605 : 115794 : print_literal(s, typid, OidOutputFunctionCall(typoutput, val));
606 : : }
607 : : }
608 : 145684 : }
609 : :
610 : : /*
611 : : * callback for individual changed tuples
612 : : */
613 : : static void
614 : 150674 : pg_decode_change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
615 : : Relation relation, ReorderBufferChange *change)
616 : : {
617 : : TestDecodingData *data;
618 : : TestDecodingTxnData *txndata;
619 : : Form_pg_class class_form;
620 : : TupleDesc tupdesc;
621 : : MemoryContext old;
622 : :
623 : 150674 : data = ctx->output_plugin_private;
624 : 150674 : txndata = txn->output_plugin_private;
625 : :
626 : : /* output BEGIN if we haven't yet */
627 [ + + + + ]: 150674 : if (data->skip_empty_xacts && !txndata->xact_wrote_changes)
628 : : {
629 : 235 : pg_output_begin(ctx, data, txn, false);
630 : : }
631 : 150674 : txndata->xact_wrote_changes = true;
632 : :
633 : 150674 : class_form = RelationGetForm(relation);
634 : 150674 : tupdesc = RelationGetDescr(relation);
635 : :
636 : : /* Avoid leaking memory by using and resetting our own context */
637 : 150674 : old = MemoryContextSwitchTo(data->context);
638 : :
639 : 150674 : OutputPluginPrepareWrite(ctx, true);
640 : :
641 : 150674 : appendStringInfoString(ctx->out, "table ");
642 : 150674 : appendStringInfoString(ctx->out,
643 : 150674 : quote_qualified_identifier(get_namespace_name(get_rel_namespace(RelationGetRelid(relation))),
644 [ + + ]: 150674 : class_form->relrewrite ?
645 : 1 : get_rel_name(class_form->relrewrite) :
646 : : NameStr(class_form->relname)));
647 : 150674 : appendStringInfoChar(ctx->out, ':');
648 : :
649 [ + + + - ]: 150674 : switch (change->action)
650 : : {
651 : 133103 : case REORDER_BUFFER_CHANGE_INSERT:
652 : 133103 : appendStringInfoString(ctx->out, " INSERT:");
653 [ - + ]: 133103 : if (change->data.tp.newtuple == NULL)
654 : 0 : appendStringInfoString(ctx->out, " (no-tuple-data)");
655 : : else
656 : 133103 : tuple_to_stringinfo(ctx->out, tupdesc,
657 : : change->data.tp.newtuple,
658 : : false);
659 : 133101 : break;
660 : 7547 : case REORDER_BUFFER_CHANGE_UPDATE:
661 : 7547 : appendStringInfoString(ctx->out, " UPDATE:");
662 [ + + ]: 7547 : if (change->data.tp.oldtuple != NULL)
663 : : {
664 : 19 : appendStringInfoString(ctx->out, " old-key:");
665 : 19 : tuple_to_stringinfo(ctx->out, tupdesc,
666 : : change->data.tp.oldtuple,
667 : : true);
668 : 19 : appendStringInfoString(ctx->out, " new-tuple:");
669 : : }
670 : :
671 [ - + ]: 7547 : if (change->data.tp.newtuple == NULL)
672 : 0 : appendStringInfoString(ctx->out, " (no-tuple-data)");
673 : : else
674 : 7547 : tuple_to_stringinfo(ctx->out, tupdesc,
675 : : change->data.tp.newtuple,
676 : : false);
677 : 7547 : break;
678 : 10024 : case REORDER_BUFFER_CHANGE_DELETE:
679 : 10024 : appendStringInfoString(ctx->out, " DELETE:");
680 : :
681 : : /* if there was no PK, we only know that a delete happened */
682 [ + + ]: 10024 : if (change->data.tp.oldtuple == NULL)
683 : 5007 : appendStringInfoString(ctx->out, " (no-tuple-data)");
684 : : /* In DELETE, only the replica identity is present; display that */
685 : : else
686 : 5017 : tuple_to_stringinfo(ctx->out, tupdesc,
687 : : change->data.tp.oldtuple,
688 : : true);
689 : 10024 : break;
690 : 150672 : default:
691 : : Assert(false);
692 : : }
693 : :
694 : 150672 : MemoryContextSwitchTo(old);
695 : 150672 : MemoryContextReset(data->context);
696 : :
697 : 150672 : OutputPluginWrite(ctx, true);
698 : 150672 : }
699 : :
700 : : static void
701 : 8 : pg_decode_truncate(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
702 : : int nrelations, Relation relations[], ReorderBufferChange *change)
703 : : {
704 : : TestDecodingData *data;
705 : : TestDecodingTxnData *txndata;
706 : : MemoryContext old;
707 : : int i;
708 : :
709 : 8 : data = ctx->output_plugin_private;
710 : 8 : txndata = txn->output_plugin_private;
711 : :
712 : : /* output BEGIN if we haven't yet */
713 [ + + + - ]: 8 : if (data->skip_empty_xacts && !txndata->xact_wrote_changes)
714 : : {
715 : 7 : pg_output_begin(ctx, data, txn, false);
716 : : }
717 : 8 : txndata->xact_wrote_changes = true;
718 : :
719 : : /* Avoid leaking memory by using and resetting our own context */
720 : 8 : old = MemoryContextSwitchTo(data->context);
721 : :
722 : 8 : OutputPluginPrepareWrite(ctx, true);
723 : :
724 : 8 : appendStringInfoString(ctx->out, "table ");
725 : :
726 [ + + ]: 17 : for (i = 0; i < nrelations; i++)
727 : : {
728 [ + + ]: 9 : if (i > 0)
729 : 1 : appendStringInfoString(ctx->out, ", ");
730 : :
731 : 9 : appendStringInfoString(ctx->out,
732 : 9 : quote_qualified_identifier(get_namespace_name(relations[i]->rd_rel->relnamespace),
733 : 9 : NameStr(relations[i]->rd_rel->relname)));
734 : : }
735 : :
736 : 8 : appendStringInfoString(ctx->out, ": TRUNCATE:");
737 : :
738 [ + + ]: 8 : if (change->data.truncate.restart_seqs
739 [ - + ]: 7 : || change->data.truncate.cascade)
740 : : {
741 [ + - ]: 1 : if (change->data.truncate.restart_seqs)
742 : 1 : appendStringInfoString(ctx->out, " restart_seqs");
743 [ + - ]: 1 : if (change->data.truncate.cascade)
744 : 1 : appendStringInfoString(ctx->out, " cascade");
745 : : }
746 : : else
747 : 7 : appendStringInfoString(ctx->out, " (no-flags)");
748 : :
749 : 8 : MemoryContextSwitchTo(old);
750 : 8 : MemoryContextReset(data->context);
751 : :
752 : 8 : OutputPluginWrite(ctx, true);
753 : 8 : }
754 : :
755 : : static void
756 : 11 : pg_decode_message(LogicalDecodingContext *ctx,
757 : : ReorderBufferTXN *txn, TransactionId xid, XLogRecPtr lsn,
758 : : bool transactional, const char *prefix, Size sz,
759 : : const char *message)
760 : : {
761 : 11 : TestDecodingData *data = ctx->output_plugin_private;
762 : : TestDecodingTxnData *txndata;
763 : :
764 [ + + ]: 11 : txndata = transactional ? txn->output_plugin_private : NULL;
765 : :
766 : : /* output BEGIN if we haven't yet for transactional messages */
767 [ + + + - : 11 : if (transactional && data->skip_empty_xacts && !txndata->xact_wrote_changes)
+ + ]
768 : 4 : pg_output_begin(ctx, data, txn, false);
769 : :
770 [ + + ]: 11 : if (transactional)
771 : 6 : txndata->xact_wrote_changes = true;
772 : :
773 : 11 : OutputPluginPrepareWrite(ctx, true);
774 : 11 : appendStringInfo(ctx->out, "message: transactional: %d prefix: %s, sz: %zu content:",
775 : : transactional, prefix, sz);
776 : 11 : appendBinaryStringInfo(ctx->out, message, sz);
777 : 11 : OutputPluginWrite(ctx, true);
778 : 11 : }
779 : :
780 : : static void
781 : 18 : pg_decode_stream_start(LogicalDecodingContext *ctx,
782 : : ReorderBufferTXN *txn)
783 : : {
784 : 18 : TestDecodingData *data = ctx->output_plugin_private;
785 : 18 : TestDecodingTxnData *txndata = txn->output_plugin_private;
786 : :
787 : : /*
788 : : * Allocate the txn plugin data for the first stream in the transaction.
789 : : */
790 [ + + ]: 18 : if (txndata == NULL)
791 : : {
792 : : txndata =
793 : 8 : MemoryContextAllocZero(ctx->context, sizeof(TestDecodingTxnData));
794 : 8 : txndata->xact_wrote_changes = false;
795 : 8 : txn->output_plugin_private = txndata;
796 : : }
797 : :
798 : 18 : txndata->stream_wrote_changes = false;
799 [ + - ]: 18 : if (data->skip_empty_xacts)
800 : 18 : return;
801 : 0 : pg_output_stream_start(ctx, data, txn, true);
802 : : }
803 : :
804 : : static void
805 : 11 : pg_output_stream_start(LogicalDecodingContext *ctx, TestDecodingData *data, ReorderBufferTXN *txn, bool last_write)
806 : : {
807 : 11 : OutputPluginPrepareWrite(ctx, last_write);
808 [ - + ]: 11 : if (data->include_xids)
809 : 0 : appendStringInfo(ctx->out, "opening a streamed block for transaction TXN %u", txn->xid);
810 : : else
811 : 11 : appendStringInfoString(ctx->out, "opening a streamed block for transaction");
812 : 11 : OutputPluginWrite(ctx, last_write);
813 : 11 : }
814 : :
815 : : static void
816 : 18 : pg_decode_stream_stop(LogicalDecodingContext *ctx,
817 : : ReorderBufferTXN *txn)
818 : : {
819 : 18 : TestDecodingData *data = ctx->output_plugin_private;
820 : 18 : TestDecodingTxnData *txndata = txn->output_plugin_private;
821 : :
822 [ + - + + ]: 18 : if (data->skip_empty_xacts && !txndata->stream_wrote_changes)
823 : 7 : return;
824 : :
825 : 11 : OutputPluginPrepareWrite(ctx, true);
826 [ - + ]: 11 : if (data->include_xids)
827 : 0 : appendStringInfo(ctx->out, "closing a streamed block for transaction TXN %u", txn->xid);
828 : : else
829 : 11 : appendStringInfoString(ctx->out, "closing a streamed block for transaction");
830 : 11 : OutputPluginWrite(ctx, true);
831 : : }
832 : :
833 : : static void
834 : 4 : pg_decode_stream_abort(LogicalDecodingContext *ctx,
835 : : ReorderBufferTXN *txn,
836 : : XLogRecPtr abort_lsn)
837 : : {
838 : 4 : TestDecodingData *data = ctx->output_plugin_private;
839 : :
840 : : /*
841 : : * stream abort can be sent for an individual subtransaction but we
842 : : * maintain the output_plugin_private only under the toptxn so if this is
843 : : * not the toptxn then fetch the toptxn.
844 : : */
845 [ + - ]: 4 : ReorderBufferTXN *toptxn = rbtxn_get_toptxn(txn);
846 : 4 : TestDecodingTxnData *txndata = toptxn->output_plugin_private;
847 : 4 : bool xact_wrote_changes = txndata->xact_wrote_changes;
848 : :
849 [ - + ]: 4 : if (rbtxn_is_toptxn(txn))
850 : : {
851 : : Assert(txn->output_plugin_private != NULL);
852 : 0 : pfree(txndata);
853 : 0 : txn->output_plugin_private = NULL;
854 : : }
855 : :
856 [ + - - + ]: 4 : if (data->skip_empty_xacts && !xact_wrote_changes)
857 : 0 : return;
858 : :
859 : 4 : OutputPluginPrepareWrite(ctx, true);
860 [ - + ]: 4 : if (data->include_xids)
861 : 0 : appendStringInfo(ctx->out, "aborting streamed (sub)transaction TXN %u", txn->xid);
862 : : else
863 : 4 : appendStringInfoString(ctx->out, "aborting streamed (sub)transaction");
864 : 4 : OutputPluginWrite(ctx, true);
865 : : }
866 : :
867 : : static void
868 : 1 : pg_decode_stream_prepare(LogicalDecodingContext *ctx,
869 : : ReorderBufferTXN *txn,
870 : : XLogRecPtr prepare_lsn)
871 : : {
872 : 1 : TestDecodingData *data = ctx->output_plugin_private;
873 : 1 : TestDecodingTxnData *txndata = txn->output_plugin_private;
874 : :
875 [ + - - + ]: 1 : if (data->skip_empty_xacts && !txndata->xact_wrote_changes)
876 : 0 : return;
877 : :
878 : 1 : OutputPluginPrepareWrite(ctx, true);
879 : :
880 [ - + ]: 1 : if (data->include_xids)
881 : 0 : appendStringInfo(ctx->out, "preparing streamed transaction TXN %s, txid %u",
882 : 0 : quote_literal_cstr(txn->gid), txn->xid);
883 : : else
884 : 1 : appendStringInfo(ctx->out, "preparing streamed transaction %s",
885 : 1 : quote_literal_cstr(txn->gid));
886 : :
887 [ - + ]: 1 : if (data->include_timestamp)
888 : 0 : appendStringInfo(ctx->out, " (at %s)",
889 : : timestamptz_to_str(txn->prepare_time));
890 : :
891 : 1 : OutputPluginWrite(ctx, true);
892 : : }
893 : :
894 : : static void
895 : 5 : pg_decode_stream_commit(LogicalDecodingContext *ctx,
896 : : ReorderBufferTXN *txn,
897 : : XLogRecPtr commit_lsn)
898 : : {
899 : 5 : TestDecodingData *data = ctx->output_plugin_private;
900 : 5 : TestDecodingTxnData *txndata = txn->output_plugin_private;
901 : 5 : bool xact_wrote_changes = txndata->xact_wrote_changes;
902 : :
903 : 5 : pfree(txndata);
904 : 5 : txn->output_plugin_private = NULL;
905 : :
906 [ + - - + ]: 5 : if (data->skip_empty_xacts && !xact_wrote_changes)
907 : 0 : return;
908 : :
909 : 5 : OutputPluginPrepareWrite(ctx, true);
910 : :
911 [ - + ]: 5 : if (data->include_xids)
912 : 0 : appendStringInfo(ctx->out, "committing streamed transaction TXN %u", txn->xid);
913 : : else
914 : 5 : appendStringInfoString(ctx->out, "committing streamed transaction");
915 : :
916 [ - + ]: 5 : if (data->include_timestamp)
917 : 0 : appendStringInfo(ctx->out, " (at %s)",
918 : : timestamptz_to_str(txn->commit_time));
919 : :
920 : 5 : OutputPluginWrite(ctx, true);
921 : : }
922 : :
923 : : /*
924 : : * In streaming mode, we don't display the changes as the transaction can abort
925 : : * at a later point in time. We don't want users to see the changes until the
926 : : * transaction is committed.
927 : : */
928 : : static void
929 : 65 : pg_decode_stream_change(LogicalDecodingContext *ctx,
930 : : ReorderBufferTXN *txn,
931 : : Relation relation,
932 : : ReorderBufferChange *change)
933 : : {
934 : 65 : TestDecodingData *data = ctx->output_plugin_private;
935 : 65 : TestDecodingTxnData *txndata = txn->output_plugin_private;
936 : :
937 : : /* output stream start if we haven't yet */
938 [ + - + + ]: 65 : if (data->skip_empty_xacts && !txndata->stream_wrote_changes)
939 : : {
940 : 8 : pg_output_stream_start(ctx, data, txn, false);
941 : : }
942 : 65 : txndata->xact_wrote_changes = txndata->stream_wrote_changes = true;
943 : :
944 : 65 : OutputPluginPrepareWrite(ctx, true);
945 [ - + ]: 65 : if (data->include_xids)
946 : 0 : appendStringInfo(ctx->out, "streaming change for TXN %u", txn->xid);
947 : : else
948 : 65 : appendStringInfoString(ctx->out, "streaming change for transaction");
949 : 65 : OutputPluginWrite(ctx, true);
950 : 65 : }
951 : :
952 : : /*
953 : : * In streaming mode, we don't display the contents for transactional messages
954 : : * as the transaction can abort at a later point in time. We don't want users to
955 : : * see the message contents until the transaction is committed.
956 : : */
957 : : static void
958 : 3 : pg_decode_stream_message(LogicalDecodingContext *ctx,
959 : : ReorderBufferTXN *txn, TransactionId xid, XLogRecPtr lsn,
960 : : bool transactional, const char *prefix, Size sz, const char *message)
961 : : {
962 : : /* Output stream start if we haven't yet for transactional messages. */
963 [ + - ]: 3 : if (transactional)
964 : : {
965 : 3 : TestDecodingData *data = ctx->output_plugin_private;
966 : 3 : TestDecodingTxnData *txndata = txn->output_plugin_private;
967 : :
968 [ + - + - ]: 3 : if (data->skip_empty_xacts && !txndata->stream_wrote_changes)
969 : : {
970 : 3 : pg_output_stream_start(ctx, data, txn, false);
971 : : }
972 : 3 : txndata->xact_wrote_changes = txndata->stream_wrote_changes = true;
973 : : }
974 : :
975 : 3 : OutputPluginPrepareWrite(ctx, true);
976 : :
977 [ + - ]: 3 : if (transactional)
978 : : {
979 : 3 : appendStringInfo(ctx->out, "streaming message: transactional: %d prefix: %s, sz: %zu",
980 : : transactional, prefix, sz);
981 : : }
982 : : else
983 : : {
984 : 0 : appendStringInfo(ctx->out, "streaming message: transactional: %d prefix: %s, sz: %zu content:",
985 : : transactional, prefix, sz);
986 : 0 : appendBinaryStringInfo(ctx->out, message, sz);
987 : : }
988 : :
989 : 3 : OutputPluginWrite(ctx, true);
990 : 3 : }
991 : :
992 : : /*
993 : : * In streaming mode, we don't display the detailed information of Truncate.
994 : : * See pg_decode_stream_change.
995 : : */
996 : : static void
997 : 0 : pg_decode_stream_truncate(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
998 : : int nrelations, Relation relations[],
999 : : ReorderBufferChange *change)
1000 : : {
1001 : 0 : TestDecodingData *data = ctx->output_plugin_private;
1002 : 0 : TestDecodingTxnData *txndata = txn->output_plugin_private;
1003 : :
1004 [ # # # # ]: 0 : if (data->skip_empty_xacts && !txndata->stream_wrote_changes)
1005 : : {
1006 : 0 : pg_output_stream_start(ctx, data, txn, false);
1007 : : }
1008 : 0 : txndata->xact_wrote_changes = txndata->stream_wrote_changes = true;
1009 : :
1010 : 0 : OutputPluginPrepareWrite(ctx, true);
1011 [ # # ]: 0 : if (data->include_xids)
1012 : 0 : appendStringInfo(ctx->out, "streaming truncate for TXN %u", txn->xid);
1013 : : else
1014 : 0 : appendStringInfoString(ctx->out, "streaming truncate for transaction");
1015 : 0 : OutputPluginWrite(ctx, true);
1016 : 0 : }
|