LCOV - differential code coverage report
Current view: top level - contrib/test_decoding - test_decoding.c (source / functions) Coverage Total Hit UBC GNC CBC
Current: 4b3c9325757dfd8f2312e9f1a8decf4431f344ae vs e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8 Lines: 85.5 % 413 353 60 2 351
Current Date: 2026-11-28 16:48:44 +0900 Functions: 96.4 % 28 27 1 2 25
Baseline: lcov-20261128-baseline Branches: 67.8 % 242 164 78 2 162
Baseline Date: 2026-11-28 16:48:48 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 100.0 % 4 4 2 2
(360..) days: 85.3 % 409 349 60 349
Function coverage date bins:
(360..) days: 96.4 % 28 27 1 2 25
Branch coverage date bins:
(30,360] days: 100.0 % 6 6 2 4
(360..) days: 66.9 % 236 158 78 158

 Age         Owner                    Branch data    TLA  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                 :                : 
  612 tgl@sss.pgh.pa.us          25                 :CBC         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
 4653 rhaas@postgresql.org      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;
 3157 peter_e@gmx.net           137                 :            399 :     cb->truncate_cb = pg_decode_truncate;
 4653 rhaas@postgresql.org      138                 :            399 :     cb->commit_cb = pg_decode_commit_txn;
 4231 andres@anarazel.de        139                 :            399 :     cb->filter_by_origin_cb = pg_decode_filter;
 4653 rhaas@postgresql.org      140                 :            399 :     cb->shutdown_cb = pg_decode_shutdown;
 3888 simon@2ndQuadrant.co      141                 :            399 :     cb->message_cb = pg_decode_message;
 2159 akapila@postgresql.o      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;
 2314                           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;
 2159                           150                 :            399 :     cb->stream_prepare_cb = pg_decode_stream_prepare;
 2314                           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;
 4653 rhaas@postgresql.org      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;
 2303 akapila@postgresql.o      165                 :            399 :     bool        enable_streaming = false;
                                166                 :                : 
  358 michael@paquier.xyz       167                 :            399 :     data = palloc0_object(TestDecodingData);
 4653 rhaas@postgresql.org      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;
 4471 andres@anarazel.de        173                 :            399 :     data->skip_empty_xacts = false;
 4231                           174                 :            399 :     data->only_local = false;
                                175                 :                : 
 4653 rhaas@postgresql.org      176                 :            399 :     ctx->output_plugin_private = data;
                                177                 :                : 
                                178                 :            399 :     opt->output_type = OUTPUT_PLUGIN_TEXTUAL_OUTPUT;
 3174 peter_e@gmx.net           179                 :            399 :     opt->receive_rewrites = false;
                                180                 :                : 
 4653 rhaas@postgresql.org      181   [ +  +  +  +  :            803 :     foreach(option, ctx->output_plugin_options)
                                              +  + ]
                                182                 :                :     {
                                183                 :            407 :         DefElem    *elem = lfirst(option);
                                184                 :                : 
                                185   [ +  -  -  + ]:            407 :         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)
 4653 rhaas@postgresql.org      191                 :UBC           0 :                 data->include_xids = true;
 4653 rhaas@postgresql.org      192         [ +  + ]:CBC         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)
 4653 rhaas@postgresql.org      201                 :UBC           0 :                 data->include_timestamp = true;
 4653 rhaas@postgresql.org      202         [ -  + ]:CBC           1 :             else if (!parse_bool(strVal(elem->arg), &data->include_timestamp))
 4653 rhaas@postgresql.org      203         [ #  # ]:UBC           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                 :                :         }
 4653 rhaas@postgresql.org      208         [ +  + ]:CBC         217 :         else if (strcmp(elem->defname, "force-binary") == 0)
                                209                 :                :         {
                                210                 :                :             bool        force_binary;
                                211                 :                : 
                                212         [ -  + ]:              8 :             if (elem->arg == NULL)
 4653 rhaas@postgresql.org      213                 :UBC           0 :                 continue;
 4653 rhaas@postgresql.org      214         [ -  + ]:CBC           8 :             else if (!parse_bool(strVal(elem->arg), &force_binary))
 4653 rhaas@postgresql.org      215         [ #  # ]:UBC           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                 :                : 
 4653 rhaas@postgresql.org      220         [ +  + ]:CBC           8 :             if (force_binary)
                                221                 :              2 :                 opt->output_type = OUTPUT_PLUGIN_BINARY_OUTPUT;
                                222                 :                :         }
 4471 andres@anarazel.de        223         [ +  + ]:            209 :         else if (strcmp(elem->defname, "skip-empty-xacts") == 0)
                                224                 :                :         {
                                225                 :                : 
                                226         [ -  + ]:            190 :             if (elem->arg == NULL)
 4471 andres@anarazel.de        227                 :UBC           0 :                 data->skip_empty_xacts = true;
 4471 andres@anarazel.de        228         [ -  + ]:CBC         190 :             else if (!parse_bool(strVal(elem->arg), &data->skip_empty_xacts))
 4471 andres@anarazel.de        229         [ #  # ]:UBC           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                 :                :         }
 4231 andres@anarazel.de        234         [ +  + ]:CBC          19 :         else if (strcmp(elem->defname, "only-local") == 0)
                                235                 :                :         {
                                236                 :                : 
                                237         [ -  + ]:              3 :             if (elem->arg == NULL)
 4231 andres@anarazel.de        238                 :UBC           0 :                 data->only_local = true;
 4231 andres@anarazel.de        239         [ -  + ]:CBC           3 :             else if (!parse_bool(strVal(elem->arg), &data->only_local))
 4231 andres@anarazel.de        240         [ #  # ]:UBC           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                 :                :         }
 3174 peter_e@gmx.net           245         [ +  + ]:CBC          16 :         else if (strcmp(elem->defname, "include-rewrites") == 0)
                                246                 :                :         {
                                247                 :                : 
                                248         [ -  + ]:              3 :             if (elem->arg == NULL)
 3174 peter_e@gmx.net           249                 :UBC           0 :                 continue;
 3174 peter_e@gmx.net           250         [ -  + ]:CBC           3 :             else if (!parse_bool(strVal(elem->arg), &opt->receive_rewrites))
 3174 peter_e@gmx.net           251         [ #  # ]:UBC           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                 :                :         }
 2303 akapila@postgresql.o      256         [ +  + ]:CBC          13 :         else if (strcmp(elem->defname, "stream-changes") == 0)
                                257                 :                :         {
                                258         [ -  + ]:             12 :             if (elem->arg == NULL)
 2303 akapila@postgresql.o      259                 :UBC           0 :                 continue;
 2303 akapila@postgresql.o      260         [ -  + ]:CBC          12 :             else if (!parse_bool(strVal(elem->arg), &enable_streaming))
 2303 akapila@postgresql.o      261         [ #  # ]:UBC           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                 :                :         {
 4653 rhaas@postgresql.org      268   [ +  -  +  - ]:CBC           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                 :                : 
 2303 akapila@postgresql.o      276                 :            396 :     ctx->streaming &= enable_streaming;
 4653 rhaas@postgresql.org      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                 :            467 : pg_decode_begin_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
                                292                 :                : {
                                293                 :            467 :     TestDecodingData *data = ctx->output_plugin_private;
                                294                 :                :     TestDecodingTxnData *txndata =
 1289 tgl@sss.pgh.pa.us         295                 :            467 :         MemoryContextAllocZero(ctx->context, sizeof(TestDecodingTxnData));
                                296                 :                : 
 2202 akapila@postgresql.o      297                 :            467 :     txndata->xact_wrote_changes = false;
                                298                 :            467 :     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                 :                :      */
 4471 andres@anarazel.de        304         [ +  + ]:            467 :     if (data->skip_empty_xacts)
                                305                 :            423 :         return;
                                306                 :                : 
                                307                 :             44 :     pg_output_begin(ctx, data, txn, true);
                                308                 :                : }
                                309                 :                : 
                                310                 :                : static void
                                311                 :            289 : pg_output_begin(LogicalDecodingContext *ctx, TestDecodingData *data, ReorderBufferTXN *txn, bool last_write)
                                312                 :                : {
                                313                 :            289 :     OutputPluginPrepareWrite(ctx, last_write);
 4653 rhaas@postgresql.org      314         [ +  + ]:            289 :     if (data->include_xids)
                                315                 :             42 :         appendStringInfo(ctx->out, "BEGIN %u", txn->xid);
                                316                 :                :     else
                                317                 :            247 :         appendStringInfoString(ctx->out, "BEGIN");
 4471 andres@anarazel.de        318                 :            289 :     OutputPluginWrite(ctx, last_write);
 4653 rhaas@postgresql.org      319                 :            289 : }
                                320                 :                : 
                                321                 :                : /* COMMIT callback */
                                322                 :                : static void
                                323                 :            465 : pg_decode_commit_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
                                324                 :                :                      XLogRecPtr commit_lsn)
                                325                 :                : {
                                326                 :            465 :     TestDecodingData *data = ctx->output_plugin_private;
 2202 akapila@postgresql.o      327                 :            465 :     TestDecodingTxnData *txndata = txn->output_plugin_private;
                                328                 :            465 :     bool        xact_wrote_changes = txndata->xact_wrote_changes;
                                329                 :                : 
                                330                 :            465 :     pfree(txndata);
                                331                 :            465 :     txn->output_plugin_private = NULL;
                                332                 :                : 
                                333   [ +  +  +  + ]:            465 :     if (data->skip_empty_xacts && !xact_wrote_changes)
 4471 andres@anarazel.de        334                 :            186 :         return;
                                335                 :                : 
 4653 rhaas@postgresql.org      336                 :            279 :     OutputPluginPrepareWrite(ctx, true);
                                337         [ +  + ]:            279 :     if (data->include_xids)
                                338                 :             39 :         appendStringInfo(ctx->out, "COMMIT %u", txn->xid);
                                339                 :                :     else
                                340                 :            240 :         appendStringInfoString(ctx->out, "COMMIT");
                                341                 :                : 
                                342         [ +  + ]:            279 :     if (data->include_timestamp)
                                343                 :              1 :         appendStringInfo(ctx->out, " (at %s)",
                                344                 :                :                          timestamptz_to_str(txn->commit_time));
                                345                 :                : 
                                346                 :            279 :     OutputPluginWrite(ctx, true);
                                347                 :                : }
                                348                 :                : 
                                349                 :                : /* BEGIN PREPARE callback */
                                350                 :                : static void
 2159 akapila@postgresql.o      351                 :              9 : pg_decode_begin_prepare_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
                                352                 :                : {
                                353                 :              9 :     TestDecodingData *data = ctx->output_plugin_private;
                                354                 :                :     TestDecodingTxnData *txndata =
 1289 tgl@sss.pgh.pa.us         355                 :              9 :         MemoryContextAllocZero(ctx->context, sizeof(TestDecodingTxnData));
                                356                 :                : 
 2159 akapila@postgresql.o      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)
 2159 akapila@postgresql.o      394                 :UBC           0 :         appendStringInfo(ctx->out, " (at %s)",
                                395                 :                :                          timestamptz_to_str(txn->prepare_time));
                                396                 :                : 
 2159 akapila@postgresql.o      397                 :CBC           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)
 2159 akapila@postgresql.o      416                 :UBC           0 :         appendStringInfo(ctx->out, " (at %s)",
                                417                 :                :                          timestamptz_to_str(txn->commit_time));
                                418                 :                : 
 2159 akapila@postgresql.o      419                 :CBC           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)
 2159 akapila@postgresql.o      437                 :UBC           0 :         appendStringInfo(ctx->out, ", txid %u", txn->xid);
                                438                 :                : 
 2159 akapila@postgresql.o      439         [ -  + ]:CBC           2 :     if (data->include_timestamp)
 2159 akapila@postgresql.o      440                 :UBC           0 :         appendStringInfo(ctx->out, " (at %s)",
                                441                 :                :                          timestamptz_to_str(txn->commit_time));
                                442                 :                : 
 2159 akapila@postgresql.o      443                 :CBC           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
 2069                           454                 :            170 : pg_decode_filter_prepare(LogicalDecodingContext *ctx, TransactionId xid,
                                455                 :                :                          const char *gid)
                                456                 :                : {
 2159                           457         [ +  + ]:            170 :     if (strstr(gid, "_nodecode") != NULL)
                                458                 :             14 :         return true;
                                459                 :                : 
                                460                 :            156 :     return false;
                                461                 :                : }
                                462                 :                : 
                                463                 :                : static bool
 4231 andres@anarazel.de        464                 :        1252340 : pg_decode_filter(LogicalDecodingContext *ctx,
                                465                 :                :                  ReplOriginId origin_id)
                                466                 :                : {
                                467                 :        1252340 :     TestDecodingData *data = ctx->output_plugin_private;
                                468                 :                : 
  304 msawada@postgresql.o      469   [ +  +  +  + ]:        1252340 :     if (data->only_local && origin_id != InvalidReplOriginId)
 4231 andres@anarazel.de        470                 :              9 :         return true;
                                471                 :        1252331 :     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
 4653 rhaas@postgresql.org      482                 :         175993 : print_literal(StringInfo s, Oid typid, char *outputstr)
                                483                 :                : {
                                484                 :                :     const char *valptr;
                                485                 :                : 
                                486   [ +  -  -  + ]:         175993 :     switch (typid)
                                487                 :                :     {
                                488                 :          60289 :         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                 :          60289 :             appendStringInfoString(s, outputstr);
                                497                 :          60289 :             break;
                                498                 :                : 
 4653 rhaas@postgresql.org      499                 :UBC           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                 :                : 
 4653 rhaas@postgresql.org      511                 :CBC      115704 :         default:
                                512                 :         115704 :             appendStringInfoChar(s, '\'');
                                513         [ +  + ]:        5856323 :             for (valptr = outputstr; *valptr; valptr++)
                                514                 :                :             {
                                515                 :        5740619 :                 char        ch = *valptr;
                                516                 :                : 
                                517         [ +  + ]:        5740619 :                 if (SQL_STR_DOUBLE(ch, false))
                                518                 :             64 :                     appendStringInfoChar(s, ch);
                                519                 :        5740619 :                 appendStringInfoChar(s, ch);
                                520                 :                :             }
                                521                 :         115704 :             appendStringInfoChar(s, '\'');
                                522                 :         115704 :             break;
                                523                 :                :     }
                                524                 :         175993 : }
                                525                 :                : 
                                526                 :                : /* print the tuple 'tuple' into the StringInfo s */
                                527                 :                : static void
                                528                 :         145594 : tuple_to_stringinfo(StringInfo s, TupleDesc tupdesc, HeapTuple tuple, bool skip_nulls)
                                529                 :                : {
                                530                 :                :     int         natt;
                                531                 :                : 
                                532                 :                :     /* print all columns individually */
                                533         [ +  + ]:         347351 :     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                 :                : 
 3387 andres@anarazel.de        542                 :         201759 :         attr = TupleDescAttr(tupdesc, natt);
                                543                 :                : 
                                544                 :                :         /*
                                545                 :                :          * don't print dropped columns, we can't be sure everything is
                                546                 :                :          * available for them
                                547                 :                :          */
 4653 rhaas@postgresql.org      548         [ +  + ]:         201759 :         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         [ -  + ]:         201687 :         if (attr->attnum < 0)
 4653 rhaas@postgresql.org      556                 :UBC           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                 :                :          */
  102 msawada@postgresql.o      565         [ +  + ]:GNC      201687 :         if (attr->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)
                                566                 :              1 :             continue;
                                567                 :                : 
 4653 rhaas@postgresql.org      568                 :CBC      201686 :         typid = attr->atttypid;
                                569                 :                : 
                                570                 :                :         /* get Datum from tuple */
 4172 andres@anarazel.de        571                 :         201686 :         origval = heap_getattr(tuple, natt + 1, tupdesc, &isnull);
                                572                 :                : 
 4653 rhaas@postgresql.org      573   [ +  +  +  + ]:         201686 :         if (isnull && skip_nulls)
                                574                 :           5061 :             continue;
                                575                 :                : 
                                576                 :                :         /* print attribute name */
                                577                 :         196625 :         appendStringInfoChar(s, ' ');
                                578                 :         196625 :         appendStringInfoString(s, quote_identifier(NameStr(attr->attname)));
                                579                 :                : 
                                580                 :                :         /* print attribute type */
                                581                 :         196625 :         appendStringInfoChar(s, '[');
                                582                 :         196625 :         appendStringInfoString(s, format_type_be(typid));
                                583                 :         196625 :         appendStringInfoChar(s, ']');
                                584                 :                : 
                                585                 :                :         /* query output function */
                                586                 :         196625 :         getTypeOutputInfo(typid,
                                587                 :                :                           &typoutput, &typisvarlena);
                                588                 :                : 
                                589                 :                :         /* print separator */
                                590                 :         196625 :         appendStringInfoChar(s, ':');
                                591                 :                : 
                                592                 :                :         /* print data */
                                593         [ +  + ]:         196625 :         if (isnull)
                                594                 :          20617 :             appendStringInfoString(s, "null");
  480 peter@eisentraut.org      595   [ +  +  +  + ]:         176008 :         else if (typisvarlena && VARATT_IS_EXTERNAL_ONDISK(DatumGetPointer(origval)))
 4653 rhaas@postgresql.org      596                 :             13 :             appendStringInfoString(s, "unchanged-toast-datum");
                                597         [ +  + ]:         175995 :         else if (!typisvarlena)
                                598                 :          60293 :             print_literal(s, typid,
                                599                 :                :                           OidOutputFunctionCall(typoutput, origval));
                                600                 :                :         else
                                601                 :                :         {
                                602                 :                :             Datum       val;    /* definitely detoasted Datum */
                                603                 :                : 
                                604                 :         115702 :             val = PointerGetDatum(PG_DETOAST_DATUM(origval));
                                605                 :         115702 :             print_literal(s, typid, OidOutputFunctionCall(typoutput, val));
                                606                 :                :         }
                                607                 :                :     }
                                608                 :         145592 : }
                                609                 :                : 
                                610                 :                : /*
                                611                 :                :  * callback for individual changed tuples
                                612                 :                :  */
                                613                 :                : static void
                                614                 :         150582 : 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                 :         150582 :     data = ctx->output_plugin_private;
 2202 akapila@postgresql.o      624                 :         150582 :     txndata = txn->output_plugin_private;
                                625                 :                : 
                                626                 :                :     /* output BEGIN if we haven't yet */
                                627   [ +  +  +  + ]:         150582 :     if (data->skip_empty_xacts && !txndata->xact_wrote_changes)
                                628                 :                :     {
 4471 andres@anarazel.de        629                 :            233 :         pg_output_begin(ctx, data, txn, false);
                                630                 :                :     }
 2202 akapila@postgresql.o      631                 :         150582 :     txndata->xact_wrote_changes = true;
                                632                 :                : 
 4653 rhaas@postgresql.org      633                 :         150582 :     class_form = RelationGetForm(relation);
                                634                 :         150582 :     tupdesc = RelationGetDescr(relation);
                                635                 :                : 
                                636                 :                :     /* Avoid leaking memory by using and resetting our own context */
                                637                 :         150582 :     old = MemoryContextSwitchTo(data->context);
                                638                 :                : 
                                639                 :         150582 :     OutputPluginPrepareWrite(ctx, true);
                                640                 :                : 
                                641                 :         150582 :     appendStringInfoString(ctx->out, "table ");
                                642                 :         150582 :     appendStringInfoString(ctx->out,
 2494 alvherre@alvh.no-ip.      643                 :         150582 :                            quote_qualified_identifier(get_namespace_name(get_rel_namespace(RelationGetRelid(relation))),
 3174 peter_e@gmx.net           644         [ +  + ]:         150582 :                                                       class_form->relrewrite ?
                                645                 :              1 :                                                       get_rel_name(class_form->relrewrite) :
                                646                 :                :                                                       NameStr(class_form->relname)));
 4219                           647                 :         150582 :     appendStringInfoChar(ctx->out, ':');
                                648                 :                : 
 4653 rhaas@postgresql.org      649   [ +  +  +  - ]:         150582 :     switch (change->action)
                                650                 :                :     {
                                651                 :         133011 :         case REORDER_BUFFER_CHANGE_INSERT:
                                652                 :         133011 :             appendStringInfoString(ctx->out, " INSERT:");
 4649 tgl@sss.pgh.pa.us         653         [ -  + ]:         133011 :             if (change->data.tp.newtuple == NULL)
 4653 rhaas@postgresql.org      654                 :UBC           0 :                 appendStringInfoString(ctx->out, " (no-tuple-data)");
                                655                 :                :             else
 4653 rhaas@postgresql.org      656                 :CBC      133011 :                 tuple_to_stringinfo(ctx->out, tupdesc,
                                657                 :                :                                     change->data.tp.newtuple,
                                658                 :                :                                     false);
                                659                 :         133009 :             break;
                                660                 :           7547 :         case REORDER_BUFFER_CHANGE_UPDATE:
                                661                 :           7547 :             appendStringInfoString(ctx->out, " UPDATE:");
 4649 tgl@sss.pgh.pa.us         662         [ +  + ]:           7547 :             if (change->data.tp.oldtuple != NULL)
                                663                 :                :             {
 4653 rhaas@postgresql.org      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                 :                : 
 4649 tgl@sss.pgh.pa.us         671         [ -  + ]:           7547 :             if (change->data.tp.newtuple == NULL)
 4653 rhaas@postgresql.org      672                 :UBC           0 :                 appendStringInfoString(ctx->out, " (no-tuple-data)");
                                673                 :                :             else
 4653 rhaas@postgresql.org      674                 :CBC        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 */
 4649 tgl@sss.pgh.pa.us         682         [ +  + ]:          10024 :             if (change->data.tp.oldtuple == NULL)
 4653 rhaas@postgresql.org      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;
 4649 tgl@sss.pgh.pa.us         690                 :UBC           0 :         default:
                                691                 :              0 :             Assert(false);
                                692                 :                :     }
                                693                 :                : 
 4653 rhaas@postgresql.org      694                 :CBC      150580 :     MemoryContextSwitchTo(old);
                                695                 :         150580 :     MemoryContextReset(data->context);
                                696                 :                : 
                                697                 :         150580 :     OutputPluginWrite(ctx, true);
                                698                 :         150580 : }
                                699                 :                : 
                                700                 :                : static void
 3157 peter_e@gmx.net           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;
 2202 akapila@postgresql.o      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                 :                :     {
 3157 peter_e@gmx.net           715                 :              7 :         pg_output_begin(ctx, data, txn, false);
                                716                 :                :     }
 2202 akapila@postgresql.o      717                 :              8 :     txndata->xact_wrote_changes = true;
                                718                 :                : 
                                719                 :                :     /* Avoid leaking memory by using and resetting our own context */
 3157 peter_e@gmx.net           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)
 2704 drowley@postgresql.o      742                 :              1 :             appendStringInfoString(ctx->out, " restart_seqs");
 3157 peter_e@gmx.net           743         [ +  - ]:              1 :         if (change->data.truncate.cascade)
 2704 drowley@postgresql.o      744                 :              1 :             appendStringInfoString(ctx->out, " cascade");
                                745                 :                :     }
                                746                 :                :     else
 3157 peter_e@gmx.net           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
 3888 simon@2ndQuadrant.co      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                 :                : {
 1236 akapila@postgresql.o      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                 :                : 
 3888 simon@2ndQuadrant.co      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
 2314 akapila@postgresql.o      781                 :             18 : pg_decode_stream_start(LogicalDecodingContext *ctx,
                                782                 :                :                        ReorderBufferTXN *txn)
                                783                 :                : {
                                784                 :             18 :     TestDecodingData *data = ctx->output_plugin_private;
 2202                           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;
 2269                           799         [ +  - ]:             18 :     if (data->skip_empty_xacts)
                                800                 :             18 :         return;
 2269 akapila@postgresql.o      801                 :UBC           0 :     pg_output_stream_start(ctx, data, txn, true);
                                802                 :                : }
                                803                 :                : 
                                804                 :                : static void
 2269 akapila@postgresql.o      805                 :CBC          11 : pg_output_stream_start(LogicalDecodingContext *ctx, TestDecodingData *data, ReorderBufferTXN *txn, bool last_write)
                                806                 :                : {
                                807                 :             11 :     OutputPluginPrepareWrite(ctx, last_write);
 2314                           808         [ -  + ]:             11 :     if (data->include_xids)
 2314 akapila@postgresql.o      809                 :UBC           0 :         appendStringInfo(ctx->out, "opening a streamed block for transaction TXN %u", txn->xid);
                                810                 :                :     else
 2235 drowley@postgresql.o      811                 :CBC          11 :         appendStringInfoString(ctx->out, "opening a streamed block for transaction");
 2269 akapila@postgresql.o      812                 :             11 :     OutputPluginWrite(ctx, last_write);
 2314                           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;
 2202                           820                 :             18 :     TestDecodingTxnData *txndata = txn->output_plugin_private;
                                821                 :                : 
                                822   [ +  -  +  + ]:             18 :     if (data->skip_empty_xacts && !txndata->stream_wrote_changes)
 2269                           823                 :              7 :         return;
                                824                 :                : 
 2314                           825                 :             11 :     OutputPluginPrepareWrite(ctx, true);
                                826         [ -  + ]:             11 :     if (data->include_xids)
 2314 akapila@postgresql.o      827                 :UBC           0 :         appendStringInfo(ctx->out, "closing a streamed block for transaction TXN %u", txn->xid);
                                828                 :                :     else
 2235 drowley@postgresql.o      829                 :CBC          11 :         appendStringInfoString(ctx->out, "closing a streamed block for transaction");
 2314 akapila@postgresql.o      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                 :                :      */
 1352                           845         [ +  - ]:              4 :     ReorderBufferTXN *toptxn = rbtxn_get_toptxn(txn);
 2202                           846                 :              4 :     TestDecodingTxnData *txndata = toptxn->output_plugin_private;
                                847                 :              4 :     bool        xact_wrote_changes = txndata->xact_wrote_changes;
                                848                 :                : 
 1352                           849         [ -  + ]:              4 :     if (rbtxn_is_toptxn(txn))
                                850                 :                :     {
 2202 akapila@postgresql.o      851         [ #  # ]:UBC           0 :         Assert(txn->output_plugin_private != NULL);
                                852                 :              0 :         pfree(txndata);
                                853                 :              0 :         txn->output_plugin_private = NULL;
                                854                 :                :     }
                                855                 :                : 
 2202 akapila@postgresql.o      856   [ +  -  -  + ]:CBC           4 :     if (data->skip_empty_xacts && !xact_wrote_changes)
 2269 akapila@postgresql.o      857                 :UBC           0 :         return;
                                858                 :                : 
 2314 akapila@postgresql.o      859                 :CBC           4 :     OutputPluginPrepareWrite(ctx, true);
                                860         [ -  + ]:              4 :     if (data->include_xids)
 2314 akapila@postgresql.o      861                 :UBC           0 :         appendStringInfo(ctx->out, "aborting streamed (sub)transaction TXN %u", txn->xid);
                                862                 :                :     else
 2235 drowley@postgresql.o      863                 :CBC           4 :         appendStringInfoString(ctx->out, "aborting streamed (sub)transaction");
 2314 akapila@postgresql.o      864                 :              4 :     OutputPluginWrite(ctx, true);
                                865                 :                : }
                                866                 :                : 
                                867                 :                : static void
 2159                           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)
 2159 akapila@postgresql.o      876                 :UBC           0 :         return;
                                877                 :                : 
 2159 akapila@postgresql.o      878                 :CBC           1 :     OutputPluginPrepareWrite(ctx, true);
                                879                 :                : 
                                880         [ -  + ]:              1 :     if (data->include_xids)
 2159 akapila@postgresql.o      881                 :UBC           0 :         appendStringInfo(ctx->out, "preparing streamed transaction TXN %s, txid %u",
                                882                 :              0 :                          quote_literal_cstr(txn->gid), txn->xid);
                                883                 :                :     else
 2159 akapila@postgresql.o      884                 :CBC           1 :         appendStringInfo(ctx->out, "preparing streamed transaction %s",
                                885                 :              1 :                          quote_literal_cstr(txn->gid));
                                886                 :                : 
                                887         [ -  + ]:              1 :     if (data->include_timestamp)
 2159 akapila@postgresql.o      888                 :UBC           0 :         appendStringInfo(ctx->out, " (at %s)",
                                889                 :                :                          timestamptz_to_str(txn->prepare_time));
                                890                 :                : 
 2159 akapila@postgresql.o      891                 :CBC           1 :     OutputPluginWrite(ctx, true);
                                892                 :                : }
                                893                 :                : 
                                894                 :                : static void
 2314                           895                 :              5 : pg_decode_stream_commit(LogicalDecodingContext *ctx,
                                896                 :                :                         ReorderBufferTXN *txn,
                                897                 :                :                         XLogRecPtr commit_lsn)
                                898                 :                : {
                                899                 :              5 :     TestDecodingData *data = ctx->output_plugin_private;
 2202                           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)
 2269 akapila@postgresql.o      907                 :UBC           0 :         return;
                                908                 :                : 
 2314 akapila@postgresql.o      909                 :CBC           5 :     OutputPluginPrepareWrite(ctx, true);
                                910                 :                : 
                                911         [ -  + ]:              5 :     if (data->include_xids)
 2314 akapila@postgresql.o      912                 :UBC           0 :         appendStringInfo(ctx->out, "committing streamed transaction TXN %u", txn->xid);
                                913                 :                :     else
 2235 drowley@postgresql.o      914                 :CBC           5 :         appendStringInfoString(ctx->out, "committing streamed transaction");
                                915                 :                : 
 2314 akapila@postgresql.o      916         [ -  + ]:              5 :     if (data->include_timestamp)
 2314 akapila@postgresql.o      917                 :UBC           0 :         appendStringInfo(ctx->out, " (at %s)",
                                918                 :                :                          timestamptz_to_str(txn->commit_time));
                                919                 :                : 
 2314 akapila@postgresql.o      920                 :CBC           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;
 2202                           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                 :                :     {
 2269                           940                 :              8 :         pg_output_stream_start(ctx, data, txn, false);
                                941                 :                :     }
 2202                           942                 :             65 :     txndata->xact_wrote_changes = txndata->stream_wrote_changes = true;
                                943                 :                : 
 2314                           944                 :             65 :     OutputPluginPrepareWrite(ctx, true);
                                945         [ -  + ]:             65 :     if (data->include_xids)
 2314 akapila@postgresql.o      946                 :UBC           0 :         appendStringInfo(ctx->out, "streaming change for TXN %u", txn->xid);
                                947                 :                :     else
 2235 drowley@postgresql.o      948                 :CBC          65 :         appendStringInfoString(ctx->out, "streaming change for transaction");
 2314 akapila@postgresql.o      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. */
 1125                           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                 :                : 
 2314                           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                 :                :     {
 2314 akapila@postgresql.o      984                 :UBC           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                 :                : 
 2314 akapila@postgresql.o      989                 :CBC           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
 2314 akapila@postgresql.o      997                 :UBC           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;
 2202                          1002                 :              0 :     TestDecodingTxnData *txndata = txn->output_plugin_private;
                               1003                 :                : 
                               1004   [ #  #  #  # ]:              0 :     if (data->skip_empty_xacts && !txndata->stream_wrote_changes)
                               1005                 :                :     {
 2269                          1006                 :              0 :         pg_output_stream_start(ctx, data, txn, false);
                               1007                 :                :     }
 2202                          1008                 :              0 :     txndata->xact_wrote_changes = txndata->stream_wrote_changes = true;
                               1009                 :                : 
 2314                          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
 2235 drowley@postgresql.o     1014                 :              0 :         appendStringInfoString(ctx->out, "streaming truncate for transaction");
 2314 akapila@postgresql.o     1015                 :              0 :     OutputPluginWrite(ctx, true);
                               1016                 :              0 : }
        

Generated by: LCOV version 2.0-1