PostgreSQL Source Code  git master
test_decoding.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * test_decoding.c
4  * example logical decoding output plugin
5  *
6  * Copyright (c) 2012-2021, 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 
26 
27 /* These must be available to dlsym() */
28 extern void _PG_init(void);
30 
31 typedef struct
32 {
37  bool only_local;
39 
40 /*
41  * Maintain the per-transaction level variables to track whether the
42  * transaction and or streams have written any changes. In streaming mode the
43  * transaction can be decoded in streams so along with maintaining whether the
44  * transaction has written any changes, we also need to track whether the
45  * current stream has written any changes. This is required so that if user
46  * has requested to skip the empty transactions we can skip the empty streams
47  * even though the transaction has written some changes.
48  */
49 typedef struct
50 {
54 
56  bool is_init);
59  ReorderBufferTXN *txn);
61  TestDecodingData *data,
62  ReorderBufferTXN *txn,
63  bool last_write);
65  ReorderBufferTXN *txn, XLogRecPtr commit_lsn);
67  ReorderBufferTXN *txn, Relation rel,
68  ReorderBufferChange *change);
70  ReorderBufferTXN *txn,
71  int nrelations, Relation relations[],
72  ReorderBufferChange *change);
74  RepOriginId origin_id);
76  ReorderBufferTXN *txn, XLogRecPtr message_lsn,
77  bool transactional, const char *prefix,
78  Size sz, const char *message);
80  TransactionId xid,
81  const char *gid);
83  ReorderBufferTXN *txn);
85  ReorderBufferTXN *txn,
86  XLogRecPtr prepare_lsn);
88  ReorderBufferTXN *txn,
89  XLogRecPtr commit_lsn);
91  ReorderBufferTXN *txn,
92  XLogRecPtr prepare_end_lsn,
93  TimestampTz prepare_time);
95  ReorderBufferTXN *txn);
97  TestDecodingData *data,
98  ReorderBufferTXN *txn,
99  bool last_write);
101  ReorderBufferTXN *txn);
103  ReorderBufferTXN *txn,
104  XLogRecPtr abort_lsn);
106  ReorderBufferTXN *txn,
107  XLogRecPtr prepare_lsn);
109  ReorderBufferTXN *txn,
110  XLogRecPtr commit_lsn);
112  ReorderBufferTXN *txn,
113  Relation relation,
114  ReorderBufferChange *change);
116  ReorderBufferTXN *txn, XLogRecPtr message_lsn,
117  bool transactional, const char *prefix,
118  Size sz, const char *message);
120  ReorderBufferTXN *txn,
121  int nrelations, Relation relations[],
122  ReorderBufferChange *change);
123 
124 void
125 _PG_init(void)
126 {
127  /* other plugins can perform things here */
128 }
129 
130 /* specify output plugin callbacks */
131 void
133 {
135 
157 }
158 
159 
160 /* initialize this plugin */
161 static void
163  bool is_init)
164 {
165  ListCell *option;
166  TestDecodingData *data;
167  bool enable_streaming = false;
168 
169  data = palloc0(sizeof(TestDecodingData));
171  "text conversion context",
173  data->include_xids = true;
174  data->include_timestamp = false;
175  data->skip_empty_xacts = false;
176  data->only_local = false;
177 
178  ctx->output_plugin_private = data;
179 
181  opt->receive_rewrites = false;
182 
183  foreach(option, ctx->output_plugin_options)
184  {
185  DefElem *elem = lfirst(option);
186 
187  Assert(elem->arg == NULL || IsA(elem->arg, String));
188 
189  if (strcmp(elem->defname, "include-xids") == 0)
190  {
191  /* if option does not provide a value, it means its value is true */
192  if (elem->arg == NULL)
193  data->include_xids = true;
194  else if (!parse_bool(strVal(elem->arg), &data->include_xids))
195  ereport(ERROR,
196  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
197  errmsg("could not parse value \"%s\" for parameter \"%s\"",
198  strVal(elem->arg), elem->defname)));
199  }
200  else if (strcmp(elem->defname, "include-timestamp") == 0)
201  {
202  if (elem->arg == NULL)
203  data->include_timestamp = true;
204  else if (!parse_bool(strVal(elem->arg), &data->include_timestamp))
205  ereport(ERROR,
206  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
207  errmsg("could not parse value \"%s\" for parameter \"%s\"",
208  strVal(elem->arg), elem->defname)));
209  }
210  else if (strcmp(elem->defname, "force-binary") == 0)
211  {
212  bool force_binary;
213 
214  if (elem->arg == NULL)
215  continue;
216  else if (!parse_bool(strVal(elem->arg), &force_binary))
217  ereport(ERROR,
218  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
219  errmsg("could not parse value \"%s\" for parameter \"%s\"",
220  strVal(elem->arg), elem->defname)));
221 
222  if (force_binary)
224  }
225  else if (strcmp(elem->defname, "skip-empty-xacts") == 0)
226  {
227 
228  if (elem->arg == NULL)
229  data->skip_empty_xacts = true;
230  else if (!parse_bool(strVal(elem->arg), &data->skip_empty_xacts))
231  ereport(ERROR,
232  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
233  errmsg("could not parse value \"%s\" for parameter \"%s\"",
234  strVal(elem->arg), elem->defname)));
235  }
236  else if (strcmp(elem->defname, "only-local") == 0)
237  {
238 
239  if (elem->arg == NULL)
240  data->only_local = true;
241  else if (!parse_bool(strVal(elem->arg), &data->only_local))
242  ereport(ERROR,
243  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
244  errmsg("could not parse value \"%s\" for parameter \"%s\"",
245  strVal(elem->arg), elem->defname)));
246  }
247  else if (strcmp(elem->defname, "include-rewrites") == 0)
248  {
249 
250  if (elem->arg == NULL)
251  continue;
252  else if (!parse_bool(strVal(elem->arg), &opt->receive_rewrites))
253  ereport(ERROR,
254  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
255  errmsg("could not parse value \"%s\" for parameter \"%s\"",
256  strVal(elem->arg), elem->defname)));
257  }
258  else if (strcmp(elem->defname, "stream-changes") == 0)
259  {
260  if (elem->arg == NULL)
261  continue;
262  else if (!parse_bool(strVal(elem->arg), &enable_streaming))
263  ereport(ERROR,
264  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
265  errmsg("could not parse value \"%s\" for parameter \"%s\"",
266  strVal(elem->arg), elem->defname)));
267  }
268  else
269  {
270  ereport(ERROR,
271  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
272  errmsg("option \"%s\" = \"%s\" is unknown",
273  elem->defname,
274  elem->arg ? strVal(elem->arg) : "(null)")));
275  }
276  }
277 
278  ctx->streaming &= enable_streaming;
279 }
280 
281 /* cleanup this plugin's resources */
282 static void
284 {
286 
287  /* cleanup our own resources via memory context reset */
289 }
290 
291 /* BEGIN callback */
292 static void
294 {
296  TestDecodingTxnData *txndata =
298 
299  txndata->xact_wrote_changes = false;
300  txn->output_plugin_private = txndata;
301 
302  if (data->skip_empty_xacts)
303  return;
304 
305  pg_output_begin(ctx, data, txn, true);
306 }
307 
308 static void
310 {
311  OutputPluginPrepareWrite(ctx, last_write);
312  if (data->include_xids)
313  appendStringInfo(ctx->out, "BEGIN %u", txn->xid);
314  else
315  appendStringInfoString(ctx->out, "BEGIN");
316  OutputPluginWrite(ctx, last_write);
317 }
318 
319 /* COMMIT callback */
320 static void
322  XLogRecPtr commit_lsn)
323 {
326  bool xact_wrote_changes = txndata->xact_wrote_changes;
327 
328  pfree(txndata);
329  txn->output_plugin_private = NULL;
330 
331  if (data->skip_empty_xacts && !xact_wrote_changes)
332  return;
333 
334  OutputPluginPrepareWrite(ctx, true);
335  if (data->include_xids)
336  appendStringInfo(ctx->out, "COMMIT %u", txn->xid);
337  else
338  appendStringInfoString(ctx->out, "COMMIT");
339 
340  if (data->include_timestamp)
341  appendStringInfo(ctx->out, " (at %s)",
343 
344  OutputPluginWrite(ctx, true);
345 }
346 
347 /* BEGIN PREPARE callback */
348 static void
350 {
352  TestDecodingTxnData *txndata =
354 
355  txndata->xact_wrote_changes = false;
356  txn->output_plugin_private = txndata;
357 
358  if (data->skip_empty_xacts)
359  return;
360 
361  pg_output_begin(ctx, data, txn, true);
362 }
363 
364 /* PREPARE callback */
365 static void
367  XLogRecPtr prepare_lsn)
368 {
371 
372  if (data->skip_empty_xacts && !txndata->xact_wrote_changes)
373  return;
374 
375  OutputPluginPrepareWrite(ctx, true);
376 
377  appendStringInfo(ctx->out, "PREPARE TRANSACTION %s",
378  quote_literal_cstr(txn->gid));
379 
380  if (data->include_xids)
381  appendStringInfo(ctx->out, ", txid %u", txn->xid);
382 
383  if (data->include_timestamp)
384  appendStringInfo(ctx->out, " (at %s)",
386 
387  OutputPluginWrite(ctx, true);
388 }
389 
390 /* COMMIT PREPARED callback */
391 static void
393  XLogRecPtr commit_lsn)
394 {
396 
397  OutputPluginPrepareWrite(ctx, true);
398 
399  appendStringInfo(ctx->out, "COMMIT PREPARED %s",
400  quote_literal_cstr(txn->gid));
401 
402  if (data->include_xids)
403  appendStringInfo(ctx->out, ", txid %u", txn->xid);
404 
405  if (data->include_timestamp)
406  appendStringInfo(ctx->out, " (at %s)",
408 
409  OutputPluginWrite(ctx, true);
410 }
411 
412 /* ROLLBACK PREPARED callback */
413 static void
415  ReorderBufferTXN *txn,
416  XLogRecPtr prepare_end_lsn,
417  TimestampTz prepare_time)
418 {
420 
421  OutputPluginPrepareWrite(ctx, true);
422 
423  appendStringInfo(ctx->out, "ROLLBACK PREPARED %s",
424  quote_literal_cstr(txn->gid));
425 
426  if (data->include_xids)
427  appendStringInfo(ctx->out, ", txid %u", txn->xid);
428 
429  if (data->include_timestamp)
430  appendStringInfo(ctx->out, " (at %s)",
432 
433  OutputPluginWrite(ctx, true);
434 }
435 
436 /*
437  * Filter out two-phase transactions.
438  *
439  * Each plugin can implement its own filtering logic. Here we demonstrate a
440  * simple logic by checking the GID. If the GID contains the "_nodecode"
441  * substring, then we filter it out.
442  */
443 static bool
445  const char *gid)
446 {
447  if (strstr(gid, "_nodecode") != NULL)
448  return true;
449 
450  return false;
451 }
452 
453 static bool
455  RepOriginId origin_id)
456 {
458 
459  if (data->only_local && origin_id != InvalidRepOriginId)
460  return true;
461  return false;
462 }
463 
464 /*
465  * Print literal `outputstr' already represented as string of type `typid'
466  * into stringbuf `s'.
467  *
468  * Some builtin types aren't quoted, the rest is quoted. Escaping is done as
469  * if standard_conforming_strings were enabled.
470  */
471 static void
472 print_literal(StringInfo s, Oid typid, char *outputstr)
473 {
474  const char *valptr;
475 
476  switch (typid)
477  {
478  case INT2OID:
479  case INT4OID:
480  case INT8OID:
481  case OIDOID:
482  case FLOAT4OID:
483  case FLOAT8OID:
484  case NUMERICOID:
485  /* NB: We don't care about Inf, NaN et al. */
486  appendStringInfoString(s, outputstr);
487  break;
488 
489  case BITOID:
490  case VARBITOID:
491  appendStringInfo(s, "B'%s'", outputstr);
492  break;
493 
494  case BOOLOID:
495  if (strcmp(outputstr, "t") == 0)
496  appendStringInfoString(s, "true");
497  else
498  appendStringInfoString(s, "false");
499  break;
500 
501  default:
502  appendStringInfoChar(s, '\'');
503  for (valptr = outputstr; *valptr; valptr++)
504  {
505  char ch = *valptr;
506 
507  if (SQL_STR_DOUBLE(ch, false))
508  appendStringInfoChar(s, ch);
509  appendStringInfoChar(s, ch);
510  }
511  appendStringInfoChar(s, '\'');
512  break;
513  }
514 }
515 
516 /* print the tuple 'tuple' into the StringInfo s */
517 static void
518 tuple_to_stringinfo(StringInfo s, TupleDesc tupdesc, HeapTuple tuple, bool skip_nulls)
519 {
520  int natt;
521 
522  /* print all columns individually */
523  for (natt = 0; natt < tupdesc->natts; natt++)
524  {
525  Form_pg_attribute attr; /* the attribute itself */
526  Oid typid; /* type of current attribute */
527  Oid typoutput; /* output function */
528  bool typisvarlena;
529  Datum origval; /* possibly toasted Datum */
530  bool isnull; /* column is null? */
531 
532  attr = TupleDescAttr(tupdesc, natt);
533 
534  /*
535  * don't print dropped columns, we can't be sure everything is
536  * available for them
537  */
538  if (attr->attisdropped)
539  continue;
540 
541  /*
542  * Don't print system columns, oid will already have been printed if
543  * present.
544  */
545  if (attr->attnum < 0)
546  continue;
547 
548  typid = attr->atttypid;
549 
550  /* get Datum from tuple */
551  origval = heap_getattr(tuple, natt + 1, tupdesc, &isnull);
552 
553  if (isnull && skip_nulls)
554  continue;
555 
556  /* print attribute name */
557  appendStringInfoChar(s, ' ');
558  appendStringInfoString(s, quote_identifier(NameStr(attr->attname)));
559 
560  /* print attribute type */
561  appendStringInfoChar(s, '[');
563  appendStringInfoChar(s, ']');
564 
565  /* query output function */
566  getTypeOutputInfo(typid,
567  &typoutput, &typisvarlena);
568 
569  /* print separator */
570  appendStringInfoChar(s, ':');
571 
572  /* print data */
573  if (isnull)
574  appendStringInfoString(s, "null");
575  else if (typisvarlena && VARATT_IS_EXTERNAL_ONDISK(origval))
576  appendStringInfoString(s, "unchanged-toast-datum");
577  else if (!typisvarlena)
578  print_literal(s, typid,
579  OidOutputFunctionCall(typoutput, origval));
580  else
581  {
582  Datum val; /* definitely detoasted Datum */
583 
584  val = PointerGetDatum(PG_DETOAST_DATUM(origval));
585  print_literal(s, typid, OidOutputFunctionCall(typoutput, val));
586  }
587  }
588 }
589 
590 /*
591  * callback for individual changed tuples
592  */
593 static void
595  Relation relation, ReorderBufferChange *change)
596 {
597  TestDecodingData *data;
598  TestDecodingTxnData *txndata;
599  Form_pg_class class_form;
600  TupleDesc tupdesc;
601  MemoryContext old;
602 
603  data = ctx->output_plugin_private;
604  txndata = txn->output_plugin_private;
605 
606  /* output BEGIN if we haven't yet */
607  if (data->skip_empty_xacts && !txndata->xact_wrote_changes)
608  {
609  pg_output_begin(ctx, data, txn, false);
610  }
611  txndata->xact_wrote_changes = true;
612 
613  class_form = RelationGetForm(relation);
614  tupdesc = RelationGetDescr(relation);
615 
616  /* Avoid leaking memory by using and resetting our own context */
617  old = MemoryContextSwitchTo(data->context);
618 
619  OutputPluginPrepareWrite(ctx, true);
620 
621  appendStringInfoString(ctx->out, "table ");
624  class_form->relrewrite ?
625  get_rel_name(class_form->relrewrite) :
626  NameStr(class_form->relname)));
627  appendStringInfoChar(ctx->out, ':');
628 
629  switch (change->action)
630  {
632  appendStringInfoString(ctx->out, " INSERT:");
633  if (change->data.tp.newtuple == NULL)
634  appendStringInfoString(ctx->out, " (no-tuple-data)");
635  else
636  tuple_to_stringinfo(ctx->out, tupdesc,
637  &change->data.tp.newtuple->tuple,
638  false);
639  break;
641  appendStringInfoString(ctx->out, " UPDATE:");
642  if (change->data.tp.oldtuple != NULL)
643  {
644  appendStringInfoString(ctx->out, " old-key:");
645  tuple_to_stringinfo(ctx->out, tupdesc,
646  &change->data.tp.oldtuple->tuple,
647  true);
648  appendStringInfoString(ctx->out, " new-tuple:");
649  }
650 
651  if (change->data.tp.newtuple == NULL)
652  appendStringInfoString(ctx->out, " (no-tuple-data)");
653  else
654  tuple_to_stringinfo(ctx->out, tupdesc,
655  &change->data.tp.newtuple->tuple,
656  false);
657  break;
659  appendStringInfoString(ctx->out, " DELETE:");
660 
661  /* if there was no PK, we only know that a delete happened */
662  if (change->data.tp.oldtuple == NULL)
663  appendStringInfoString(ctx->out, " (no-tuple-data)");
664  /* In DELETE, only the replica identity is present; display that */
665  else
666  tuple_to_stringinfo(ctx->out, tupdesc,
667  &change->data.tp.oldtuple->tuple,
668  true);
669  break;
670  default:
671  Assert(false);
672  }
673 
676 
677  OutputPluginWrite(ctx, true);
678 }
679 
680 static void
682  int nrelations, Relation relations[], ReorderBufferChange *change)
683 {
684  TestDecodingData *data;
685  TestDecodingTxnData *txndata;
686  MemoryContext old;
687  int i;
688 
689  data = ctx->output_plugin_private;
690  txndata = txn->output_plugin_private;
691 
692  /* output BEGIN if we haven't yet */
693  if (data->skip_empty_xacts && !txndata->xact_wrote_changes)
694  {
695  pg_output_begin(ctx, data, txn, false);
696  }
697  txndata->xact_wrote_changes = true;
698 
699  /* Avoid leaking memory by using and resetting our own context */
700  old = MemoryContextSwitchTo(data->context);
701 
702  OutputPluginPrepareWrite(ctx, true);
703 
704  appendStringInfoString(ctx->out, "table ");
705 
706  for (i = 0; i < nrelations; i++)
707  {
708  if (i > 0)
709  appendStringInfoString(ctx->out, ", ");
710 
712  quote_qualified_identifier(get_namespace_name(relations[i]->rd_rel->relnamespace),
713  NameStr(relations[i]->rd_rel->relname)));
714  }
715 
716  appendStringInfoString(ctx->out, ": TRUNCATE:");
717 
718  if (change->data.truncate.restart_seqs
719  || change->data.truncate.cascade)
720  {
721  if (change->data.truncate.restart_seqs)
722  appendStringInfoString(ctx->out, " restart_seqs");
723  if (change->data.truncate.cascade)
724  appendStringInfoString(ctx->out, " cascade");
725  }
726  else
727  appendStringInfoString(ctx->out, " (no-flags)");
728 
731 
732  OutputPluginWrite(ctx, true);
733 }
734 
735 static void
737  ReorderBufferTXN *txn, XLogRecPtr lsn, bool transactional,
738  const char *prefix, Size sz, const char *message)
739 {
740  OutputPluginPrepareWrite(ctx, true);
741  appendStringInfo(ctx->out, "message: transactional: %d prefix: %s, sz: %zu content:",
742  transactional, prefix, sz);
743  appendBinaryStringInfo(ctx->out, message, sz);
744  OutputPluginWrite(ctx, true);
745 }
746 
747 static void
749  ReorderBufferTXN *txn)
750 {
753 
754  /*
755  * Allocate the txn plugin data for the first stream in the transaction.
756  */
757  if (txndata == NULL)
758  {
759  txndata =
761  txndata->xact_wrote_changes = false;
762  txn->output_plugin_private = txndata;
763  }
764 
765  txndata->stream_wrote_changes = false;
766  if (data->skip_empty_xacts)
767  return;
768  pg_output_stream_start(ctx, data, txn, true);
769 }
770 
771 static void
773 {
774  OutputPluginPrepareWrite(ctx, last_write);
775  if (data->include_xids)
776  appendStringInfo(ctx->out, "opening a streamed block for transaction TXN %u", txn->xid);
777  else
778  appendStringInfoString(ctx->out, "opening a streamed block for transaction");
779  OutputPluginWrite(ctx, last_write);
780 }
781 
782 static void
784  ReorderBufferTXN *txn)
785 {
788 
789  if (data->skip_empty_xacts && !txndata->stream_wrote_changes)
790  return;
791 
792  OutputPluginPrepareWrite(ctx, true);
793  if (data->include_xids)
794  appendStringInfo(ctx->out, "closing a streamed block for transaction TXN %u", txn->xid);
795  else
796  appendStringInfoString(ctx->out, "closing a streamed block for transaction");
797  OutputPluginWrite(ctx, true);
798 }
799 
800 static void
802  ReorderBufferTXN *txn,
803  XLogRecPtr abort_lsn)
804 {
806 
807  /*
808  * stream abort can be sent for an individual subtransaction but we
809  * maintain the output_plugin_private only under the toptxn so if this is
810  * not the toptxn then fetch the toptxn.
811  */
812  ReorderBufferTXN *toptxn = txn->toptxn ? txn->toptxn : txn;
813  TestDecodingTxnData *txndata = toptxn->output_plugin_private;
814  bool xact_wrote_changes = txndata->xact_wrote_changes;
815 
816  if (txn->toptxn == NULL)
817  {
818  Assert(txn->output_plugin_private != NULL);
819  pfree(txndata);
820  txn->output_plugin_private = NULL;
821  }
822 
823  if (data->skip_empty_xacts && !xact_wrote_changes)
824  return;
825 
826  OutputPluginPrepareWrite(ctx, true);
827  if (data->include_xids)
828  appendStringInfo(ctx->out, "aborting streamed (sub)transaction TXN %u", txn->xid);
829  else
830  appendStringInfoString(ctx->out, "aborting streamed (sub)transaction");
831  OutputPluginWrite(ctx, true);
832 }
833 
834 static void
836  ReorderBufferTXN *txn,
837  XLogRecPtr prepare_lsn)
838 {
841 
842  if (data->skip_empty_xacts && !txndata->xact_wrote_changes)
843  return;
844 
845  OutputPluginPrepareWrite(ctx, true);
846 
847  if (data->include_xids)
848  appendStringInfo(ctx->out, "preparing streamed transaction TXN %s, txid %u",
849  quote_literal_cstr(txn->gid), txn->xid);
850  else
851  appendStringInfo(ctx->out, "preparing streamed transaction %s",
852  quote_literal_cstr(txn->gid));
853 
854  if (data->include_timestamp)
855  appendStringInfo(ctx->out, " (at %s)",
857 
858  OutputPluginWrite(ctx, true);
859 }
860 
861 static void
863  ReorderBufferTXN *txn,
864  XLogRecPtr commit_lsn)
865 {
868  bool xact_wrote_changes = txndata->xact_wrote_changes;
869 
870  pfree(txndata);
871  txn->output_plugin_private = NULL;
872 
873  if (data->skip_empty_xacts && !xact_wrote_changes)
874  return;
875 
876  OutputPluginPrepareWrite(ctx, true);
877 
878  if (data->include_xids)
879  appendStringInfo(ctx->out, "committing streamed transaction TXN %u", txn->xid);
880  else
881  appendStringInfoString(ctx->out, "committing streamed transaction");
882 
883  if (data->include_timestamp)
884  appendStringInfo(ctx->out, " (at %s)",
886 
887  OutputPluginWrite(ctx, true);
888 }
889 
890 /*
891  * In streaming mode, we don't display the changes as the transaction can abort
892  * at a later point in time. We don't want users to see the changes until the
893  * transaction is committed.
894  */
895 static void
897  ReorderBufferTXN *txn,
898  Relation relation,
899  ReorderBufferChange *change)
900 {
903 
904  /* output stream start if we haven't yet */
905  if (data->skip_empty_xacts && !txndata->stream_wrote_changes)
906  {
907  pg_output_stream_start(ctx, data, txn, false);
908  }
909  txndata->xact_wrote_changes = txndata->stream_wrote_changes = true;
910 
911  OutputPluginPrepareWrite(ctx, true);
912  if (data->include_xids)
913  appendStringInfo(ctx->out, "streaming change for TXN %u", txn->xid);
914  else
915  appendStringInfoString(ctx->out, "streaming change for transaction");
916  OutputPluginWrite(ctx, true);
917 }
918 
919 /*
920  * In streaming mode, we don't display the contents for transactional messages
921  * as the transaction can abort at a later point in time. We don't want users to
922  * see the message contents until the transaction is committed.
923  */
924 static void
926  ReorderBufferTXN *txn, XLogRecPtr lsn, bool transactional,
927  const char *prefix, Size sz, const char *message)
928 {
929  OutputPluginPrepareWrite(ctx, true);
930 
931  if (transactional)
932  {
933  appendStringInfo(ctx->out, "streaming message: transactional: %d prefix: %s, sz: %zu",
934  transactional, prefix, sz);
935  }
936  else
937  {
938  appendStringInfo(ctx->out, "streaming message: transactional: %d prefix: %s, sz: %zu content:",
939  transactional, prefix, sz);
940  appendBinaryStringInfo(ctx->out, message, sz);
941  }
942 
943  OutputPluginWrite(ctx, true);
944 }
945 
946 /*
947  * In streaming mode, we don't display the detailed information of Truncate.
948  * See pg_decode_stream_change.
949  */
950 static void
952  int nrelations, Relation relations[],
953  ReorderBufferChange *change)
954 {
957 
958  if (data->skip_empty_xacts && !txndata->stream_wrote_changes)
959  {
960  pg_output_stream_start(ctx, data, txn, false);
961  }
962  txndata->xact_wrote_changes = txndata->stream_wrote_changes = true;
963 
964  OutputPluginPrepareWrite(ctx, true);
965  if (data->include_xids)
966  appendStringInfo(ctx->out, "streaming truncate for TXN %u", txn->xid);
967  else
968  appendStringInfoString(ctx->out, "streaming truncate for transaction");
969  OutputPluginWrite(ctx, true);
970 }
static void tuple_to_stringinfo(StringInfo s, TupleDesc tupdesc, HeapTuple tuple, bool skip_nulls)
LogicalDecodeTruncateCB truncate_cb
static void pg_decode_shutdown(LogicalDecodingContext *ctx)
TimestampTz commit_time
#define VARATT_IS_EXTERNAL_ONDISK(PTR)
Definition: postgres.h:327
static void pg_decode_stream_abort(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr abort_lsn)
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
#define AllocSetContextCreate
Definition: memutils.h:173
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2854
char * quote_literal_cstr(const char *rawstr)
Definition: quote.c:102
static bool pg_decode_filter(LogicalDecodingContext *ctx, RepOriginId origin_id)
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:11323
uint32 TransactionId
Definition: c.h:587
LogicalDecodeStreamPrepareCB stream_prepare_cb
#define RelationGetDescr(relation)
Definition: rel.h:495
int64 TimestampTz
Definition: timestamp.h:39
static void pg_decode_begin_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
#define PointerGetDatum(X)
Definition: postgres.h:600
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
static void pg_decode_begin_prepare_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
#define RelationGetForm(relation)
Definition: rel.h:463
PG_MODULE_MAGIC
Definition: test_decoding.c:25
LogicalDecodeMessageCB message_cb
Oid get_rel_namespace(Oid relid)
Definition: lsyscache.c:1923
LogicalDecodeStreamMessageCB stream_message_cb
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
uint16 RepOriginId
Definition: xlogdefs.h:65
#define strVal(v)
Definition: value.h:54
static void pg_decode_stream_commit(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
int errcode(int sqlerrcode)
Definition: elog.c:698
void * output_plugin_private
Definition: logical.h:78
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
MemoryContext context
Definition: logical.h:37
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:143
LogicalDecodeStreamAbortCB stream_abort_cb
List * output_plugin_options
Definition: logical.h:61
bool parse_bool(const char *value, bool *result)
Definition: bool.c:30
unsigned int Oid
Definition: postgres_ext.h:31
static void pg_decode_rollback_prepared_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr prepare_end_lsn, TimestampTz prepare_time)
enum ReorderBufferChangeType action
Definition: reorderbuffer.h:84
static void pg_decode_stream_start(LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
static void pg_decode_stream_truncate(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
LogicalDecodeBeginPrepareCB begin_prepare_cb
static void pg_decode_commit_prepared_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
OutputPluginOutputType output_type
Definition: output_plugin.h:28
LogicalDecodePrepareCB prepare_cb
void pfree(void *pointer)
Definition: mcxt.c:1169
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
static void print_literal(StringInfo s, Oid typid, char *outputstr)
#define ERROR
Definition: elog.h:46
static bool pg_decode_filter_prepare(LogicalDecodingContext *ctx, TransactionId xid, const char *gid)
LogicalDecodeCommitCB commit_cb
static void pg_decode_stream_stop(LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
void(* LogicalOutputPluginInit)(struct OutputPluginCallbacks *cb)
Definition: output_plugin.h:36
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:195
static void pg_decode_stream_message(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr message_lsn, bool transactional, const char *prefix, Size sz, const char *message)
static void pg_decode_message(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr message_lsn, bool transactional, const char *prefix, Size sz, const char *message)
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3316
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:203
LogicalDecodeRollbackPreparedCB rollback_prepared_cb
struct ReorderBufferTXN * toptxn
LogicalDecodeCommitPreparedCB commit_prepared_cb
static void pg_decode_truncate(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
union ReorderBufferChange::@97 data
Node * arg
Definition: parsenodes.h:747
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
char * quote_qualified_identifier(const char *qualifier, const char *ident)
Definition: ruleutils.c:11407
MemoryContext context
Definition: test_decoding.c:33
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:761
static void pg_decode_commit_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
void * palloc0(Size size)
Definition: mcxt.c:1093
LogicalDecodeChangeCB change_cb
LogicalDecodeFilterPrepareCB filter_prepare_cb
uintptr_t Datum
Definition: postgres.h:411
LogicalDecodeStreamTruncateCB stream_truncate_cb
TransactionId xid
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:906
static void pg_decode_startup(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
#define ereport(elevel,...)
Definition: elog.h:157
static void pg_decode_change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, Relation rel, ReorderBufferChange *change)
void OutputPluginPrepareWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:639
static void pg_decode_stream_prepare(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
size_t Size
Definition: c.h:540
static void pg_output_begin(LogicalDecodingContext *ctx, TestDecodingData *data, ReorderBufferTXN *txn, bool last_write)
LogicalDecodeShutdownCB shutdown_cb
LogicalDecodeStreamCommitCB stream_commit_cb
void _PG_init(void)
LogicalDecodeStartupCB startup_cb
#define InvalidRepOriginId
Definition: origin.h:33
struct ReorderBufferChange::@97::@98 tp
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
static void pg_decode_stream_change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
LogicalDecodeStreamStartCB stream_start_cb
struct ReorderBufferChange::@97::@99 truncate
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1653
int errmsg(const char *fmt,...)
Definition: elog.c:909
void OutputPluginWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:652
static void pg_decode_prepare_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
StringInfo out
Definition: logical.h:73
int i
#define NameStr(name)
Definition: c.h:681
#define SQL_STR_DOUBLE(ch, escape_backslash)
Definition: c.h:1164
LogicalDecodeBeginCB begin_cb
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:240
char * defname
Definition: parsenodes.h:746
LogicalDecodeStreamStopCB stream_stop_cb
LogicalDecodeFilterByOriginCB filter_by_origin_cb
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1899
LogicalDecodeStreamChangeCB stream_change_cb
void * output_plugin_private
#define RelationGetRelid(relation)
Definition: rel.h:469
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:227
long val
Definition: informix.c:664
void _PG_output_plugin_init(OutputPluginCallbacks *cb)
const char * timestamptz_to_str(TimestampTz t)
Definition: timestamp.c:1774
static void pg_output_stream_start(LogicalDecodingContext *ctx, TestDecodingData *data, ReorderBufferTXN *txn, bool last_write)
#define AssertVariableIsOfType(varname, typename)
Definition: c.h:963