LCOV - differential code coverage report
Current view: top level - src/backend/replication/logical - logical.c (source / functions) Coverage Total Hit UBC GNC CBC DCB
Current: 4b3c9325757dfd8f2312e9f1a8decf4431f344ae vs e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8 Lines: 93.2 % 833 776 57 2 774 2
Current Date: 2026-11-28 16:48:44 +0900 Functions: 97.6 % 42 41 1 2 39
Baseline: lcov-20261128-baseline Branches: 60.4 % 444 268 176 268
Baseline Date: 2026-11-28 16:48:48 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(1,7] days: 100.0 % 2 2 2
(7,30] days: 83.3 % 12 10 2 10
(30,360] days: 97.7 % 43 42 1 42
(360..) days: 93.0 % 776 722 54 722
Function coverage date bins:
(7,30] days: 100.0 % 1 1 1
(30,360] days: 100.0 % 3 3 3
(360..) days: 97.4 % 38 37 1 2 35
Branch coverage date bins:
(7,30] days: 50.0 % 6 3 3 3
(30,360] days: 77.1 % 48 37 11 37
(360..) days: 58.5 % 390 228 162 228

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  * logical.c
                                  3                 :                :  *     PostgreSQL logical decoding coordination
                                  4                 :                :  *
                                  5                 :                :  * Copyright (c) 2012-2026, PostgreSQL Global Development Group
                                  6                 :                :  *
                                  7                 :                :  * IDENTIFICATION
                                  8                 :                :  *    src/backend/replication/logical/logical.c
                                  9                 :                :  *
                                 10                 :                :  * NOTES
                                 11                 :                :  *    This file coordinates interaction between the various modules that
                                 12                 :                :  *    together provide logical decoding, primarily by providing so
                                 13                 :                :  *    called LogicalDecodingContexts. The goal is to encapsulate most of the
                                 14                 :                :  *    internal complexity for consumers of logical decoding, so they can
                                 15                 :                :  *    create and consume a changestream with a low amount of code. Builtin
                                 16                 :                :  *    consumers are the walsender and SQL SRF interface, but it's possible to
                                 17                 :                :  *    add further ones without changing core code, e.g. to consume changes in
                                 18                 :                :  *    a bgworker.
                                 19                 :                :  *
                                 20                 :                :  *    The idea is that a consumer provides three callbacks, one to read WAL,
                                 21                 :                :  *    one to prepare a data write, and a final one for actually writing since
                                 22                 :                :  *    their implementation depends on the type of consumer.  Check
                                 23                 :                :  *    logicalfuncs.c for an example implementation of a fairly simple consumer
                                 24                 :                :  *    and an implementation of a WAL reading callback that's suitable for
                                 25                 :                :  *    simple consumers.
                                 26                 :                :  *-------------------------------------------------------------------------
                                 27                 :                :  */
                                 28                 :                : 
                                 29                 :                : #include "postgres.h"
                                 30                 :                : 
                                 31                 :                : #include "access/xact.h"
                                 32                 :                : #include "access/xlog_internal.h"
                                 33                 :                : #include "access/xlogutils.h"
                                 34                 :                : #include "commands/repack.h"
                                 35                 :                : #include "fmgr.h"
                                 36                 :                : #include "miscadmin.h"
                                 37                 :                : #include "pgstat.h"
                                 38                 :                : #include "replication/decode.h"
                                 39                 :                : #include "replication/logical.h"
                                 40                 :                : #include "replication/reorderbuffer.h"
                                 41                 :                : #include "replication/slotsync.h"
                                 42                 :                : #include "replication/snapbuild.h"
                                 43                 :                : #include "storage/proc.h"
                                 44                 :                : #include "storage/procarray.h"
                                 45                 :                : #include "utils/builtins.h"
                                 46                 :                : #include "utils/guc.h"
                                 47                 :                : #include "utils/guc_hooks.h"
                                 48                 :                : #include "utils/injection_point.h"
                                 49                 :                : #include "utils/inval.h"
                                 50                 :                : #include "utils/memutils.h"
                                 51                 :                : #include "utils/varlena.h"
                                 52                 :                : 
                                 53                 :                : /* data for errcontext callback */
                                 54                 :                : typedef struct LogicalErrorCallbackState
                                 55                 :                : {
                                 56                 :                :     LogicalDecodingContext *ctx;
                                 57                 :                :     const char *callback_name;
                                 58                 :                :     XLogRecPtr  report_location;
                                 59                 :                : } LogicalErrorCallbackState;
                                 60                 :                : 
                                 61                 :                : /* GUC variables */
                                 62                 :                : char       *output_plugin_libraries_string;
                                 63                 :                : 
                                 64                 :                : /* wrappers around output plugin callbacks */
                                 65                 :                : static void output_plugin_error_callback(void *arg);
                                 66                 :                : static void startup_cb_wrapper(LogicalDecodingContext *ctx, OutputPluginOptions *opt,
                                 67                 :                :                                bool is_init);
                                 68                 :                : static void shutdown_cb_wrapper(LogicalDecodingContext *ctx);
                                 69                 :                : static void begin_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn);
                                 70                 :                : static void commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
                                 71                 :                :                               XLogRecPtr commit_lsn);
                                 72                 :                : static void begin_prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn);
                                 73                 :                : static void prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
                                 74                 :                :                                XLogRecPtr prepare_lsn);
                                 75                 :                : static void commit_prepared_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
                                 76                 :                :                                        XLogRecPtr commit_lsn);
                                 77                 :                : static void rollback_prepared_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
                                 78                 :                :                                          XLogRecPtr prepare_end_lsn, TimestampTz prepare_time);
                                 79                 :                : static void change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
                                 80                 :                :                               Relation relation, ReorderBufferChange *change);
                                 81                 :                : static void truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
                                 82                 :                :                                 int nrelations, Relation relations[], ReorderBufferChange *change);
                                 83                 :                : static void message_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
                                 84                 :                :                                TransactionId xid, XLogRecPtr message_lsn, bool transactional,
                                 85                 :                :                                const char *prefix, Size message_size, const char *message);
                                 86                 :                : 
                                 87                 :                : /* streaming callbacks */
                                 88                 :                : static void stream_start_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
                                 89                 :                :                                     XLogRecPtr first_lsn);
                                 90                 :                : static void stream_stop_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
                                 91                 :                :                                    XLogRecPtr last_lsn);
                                 92                 :                : static void stream_abort_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
                                 93                 :                :                                     XLogRecPtr abort_lsn);
                                 94                 :                : static void stream_prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
                                 95                 :                :                                       XLogRecPtr prepare_lsn);
                                 96                 :                : static void stream_commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
                                 97                 :                :                                      XLogRecPtr commit_lsn);
                                 98                 :                : static void stream_change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
                                 99                 :                :                                      Relation relation, ReorderBufferChange *change);
                                100                 :                : static void stream_message_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
                                101                 :                :                                       TransactionId xid, XLogRecPtr message_lsn, bool transactional,
                                102                 :                :                                       const char *prefix, Size message_size, const char *message);
                                103                 :                : static void stream_truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
                                104                 :                :                                        int nrelations, Relation relations[], ReorderBufferChange *change);
                                105                 :                : 
                                106                 :                : /* callback to update txn's progress */
                                107                 :                : static void update_progress_txn_cb_wrapper(ReorderBuffer *cache,
                                108                 :                :                                            ReorderBufferTXN *txn,
                                109                 :                :                                            XLogRecPtr lsn);
                                110                 :                : 
                                111                 :                : static void LoadOutputPlugin(OutputPluginCallbacks *callbacks, const char *plugin);
                                112                 :                : 
                                113                 :                : /* check_hook: validate new output_plugin_libraries value */
                                114                 :                : bool
   23 jchampion@postgresql      115                 :CBC        1382 : check_output_plugin_libraries(char **newval, void **extra, GucSource source)
                                116                 :                : {
                                117                 :                :     char       *copy;
                                118                 :                :     List       *components;
                                119                 :           1382 :     bool        ok = true;
                                120                 :                : 
                                121                 :           1382 :     copy = guc_strdup(LOG, *newval);
                                122         [ -  + ]:           1382 :     if (!copy)
   23 jchampion@postgresql      123                 :UBC           0 :         return false;
                                124                 :                : 
                                125                 :                :     /*
                                126                 :                :      * XXX SplitGUCList won't respect guc_malloc requirements, but this is
                                127                 :                :      * consistent with other check_hook implementations...
                                128                 :                :      */
   23 jchampion@postgresql      129         [ +  + ]:CBC        1382 :     if (!SplitGUCList(copy, ',', &components))
                                130                 :                :     {
                                131                 :              4 :         GUC_check_errdetail("List syntax is invalid.");
                                132                 :              4 :         ok = false;
                                133                 :                :     }
                                134                 :                : 
                                135                 :                :     /*
                                136                 :                :      * Like with related GUC_LIST_QUOTE variables, we check only syntax here
                                137                 :                :      * and not the existence of the plugins themselves.
                                138                 :                :      */
                                139                 :                : 
                                140                 :           1382 :     list_free(components);
                                141                 :           1382 :     guc_free(copy);
                                142                 :                : 
                                143                 :           1382 :     return ok;
                                144                 :                : }
                                145                 :                : 
                                146                 :                : /*
                                147                 :                :  * Make sure the current settings & environment are capable of doing logical
                                148                 :                :  * decoding.
                                149                 :                :  */
                                150                 :                : void
  235 alvherre@kurilemu.de      151                 :           1896 : CheckLogicalDecodingRequirements(bool repack)
                                152                 :                : {
                                153                 :           1896 :     CheckSlotRequirements(repack);
                                154                 :                : 
                                155                 :                :     /*
                                156                 :                :      * NB: Adding a new requirement likely means that RestoreSlotFromDisk()
                                157                 :                :      * needs the same check.
                                158                 :                :      */
                                159                 :                : 
 4653 rhaas@postgresql.org      160         [ +  + ]:           1896 :     if (MyDatabaseId == InvalidOid)
                                161         [ +  - ]:              1 :         ereport(ERROR,
                                162                 :                :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                                163                 :                :                  errmsg("logical decoding requires a database connection")));
                                164                 :                : 
                                165                 :                :     /* CheckSlotRequirements() has already checked if wal_level >= 'replica' */
  340 msawada@postgresql.o      166         [ -  + ]:           1895 :     Assert(wal_level >= WAL_LEVEL_REPLICA);
                                167                 :                : 
                                168                 :                :     /* Check if logical decoding is available on standby */
                                169   [ +  +  +  + ]:           1895 :     if (RecoveryInProgress() && !IsLogicalDecodingEnabled())
                                170         [ +  - ]:              2 :         ereport(ERROR,
                                171                 :                :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                                172                 :                :                  errmsg("logical decoding on standby requires \"effective_wal_level\" >= \"logical\" on the primary"),
                                173                 :                :                  errhint("Set \"wal_level\" >= \"logical\" or create at least one logical slot when \"wal_level\" = \"replica\".")));
 4653 rhaas@postgresql.org      174                 :           1893 : }
                                175                 :                : 
                                176                 :                : /*
                                177                 :                :  * Helper function for CreateInitDecodingContext() and
                                178                 :                :  * CreateDecodingContext() performing common tasks.
                                179                 :                :  */
                                180                 :                : static LogicalDecodingContext *
                                181                 :           1337 : StartupDecodingContext(List *output_plugin_options,
                                182                 :                :                        XLogRecPtr start_lsn,
                                183                 :                :                        TransactionId xmin_horizon,
                                184                 :                :                        bool need_full_snapshot,
                                185                 :                :                        bool fast_forward,
                                186                 :                :                        bool in_create,
                                187                 :                :                        bool for_repack,
                                188                 :                :                        XLogReaderRoutine *xl_routine,
                                189                 :                :                        LogicalOutputPluginWriterPrepareWrite prepare_write,
                                190                 :                :                        LogicalOutputPluginWriterWrite do_write,
                                191                 :                :                        LogicalOutputPluginWriterUpdateProgress update_progress)
                                192                 :                : {
                                193                 :                :     ReplicationSlot *slot;
                                194                 :                :     MemoryContext context,
                                195                 :                :                 old_context;
                                196                 :                :     LogicalDecodingContext *ctx;
                                197                 :                : 
                                198                 :                :     /* shorter lines... */
                                199                 :           1337 :     slot = MyReplicationSlot;
                                200                 :                : 
                                201                 :           1337 :     context = AllocSetContextCreate(CurrentMemoryContext,
                                202                 :                :                                     "Logical decoding context",
                                203                 :                :                                     ALLOCSET_DEFAULT_SIZES);
                                204                 :           1337 :     old_context = MemoryContextSwitchTo(context);
  353 michael@paquier.xyz       205                 :           1337 :     ctx = palloc0_object(LogicalDecodingContext);
                                206                 :                : 
 4653 rhaas@postgresql.org      207                 :           1337 :     ctx->context = context;
                                208                 :                : 
                                209                 :                :     /*
                                210                 :                :      * (re-)load output plugins, so we detect a bad (removed) output plugin
                                211                 :                :      * now.
                                212                 :                :      */
 3237 simon@2ndQuadrant.co      213         [ +  + ]:           1337 :     if (!fast_forward)
                                214                 :                :     {
                                215                 :                :         /*
                                216                 :                :          * Before loading this library, make sure it's been blessed for
                                217                 :                :          * logical decoding.
                                218                 :                :          */
  110 jchampion@postgresql      219                 :           1309 :         const char *plugin = NameStr(slot->data.plugin);
                                220                 :           1309 :         bool        plugin_allowed = false;
                                221                 :                : 
                                222         [ +  + ]:           1309 :         if (strcmp(plugin, "pgrepack") == 0)
                                223                 :                :         {
                                224                 :                :             /*
                                225                 :                :              * REPACK is a special case -- print a more helpful error message
                                226                 :                :              * here if we're not being called in the correct context.
                                227                 :                :              */
                                228   [ +  +  -  + ]:             14 :             if (!for_repack || !AmRepackWorker())
                                229         [ +  - ]:              1 :                 ereport(ERROR,
                                230                 :                :                         errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                231                 :                :                         errmsg("unsupported use of logical decoding plugin \"%s\"",
                                232                 :                :                                "pgrepack"),
                                233                 :                :                         errdetail("This plugin can only be used by %s.",
                                234                 :                :                                   "REPACK (CONCURRENTLY)"));
                                235                 :                : 
                                236                 :             13 :             plugin_allowed = true;
                                237                 :                :         }
                                238         [ +  - ]:           1295 :         else if (output_plugin_libraries_string &&
                                239         [ +  - ]:           1295 :                  output_plugin_libraries_string[0])
                                240                 :                :         {
                                241                 :                :             /* Check this plugin against output_plugin_libraries. */
                                242                 :                :             char       *rawstring;
                                243                 :           1295 :             List       *elemlist = NIL;
                                244                 :                : 
                                245                 :                :             /* Need a modifiable copy */
                                246                 :           1295 :             rawstring = pstrdup(output_plugin_libraries_string);
                                247                 :                : 
                                248                 :                :             /* The check_hook should have already handled syntax errors. */
                                249         [ -  + ]:           1295 :             if (!SplitGUCList(rawstring, ',', &elemlist))
   23 jchampion@postgresql      250         [ #  # ]:UBC           0 :                 elog(ERROR, "internal error: invalid output_plugin_libraries syntax after check_hook");
                                251                 :                : 
  110 jchampion@postgresql      252   [ +  -  +  +  :CBC        2994 :             foreach_ptr(char, allowed, elemlist)
                                              +  + ]
                                253                 :                :             {
                                254         [ +  + ]:           1696 :                 if (strcmp(allowed, plugin) == 0)
                                255                 :                :                 {
                                256                 :           1292 :                     plugin_allowed = true;
                                257                 :           1292 :                     break;
                                258                 :                :                 }
                                259                 :                :             }
                                260                 :                : 
                                261                 :           1295 :             list_free(elemlist);
                                262                 :           1295 :             pfree(rawstring);
                                263                 :                :         }
                                264                 :                : 
                                265         [ +  + ]:           1308 :         if (!plugin_allowed)
                                266                 :                :         {
                                267                 :                :             /*
                                268                 :                :              * Use the same error message as check_restricted_library_name(),
                                269                 :                :              * but provide additional context for the DBA in the logs. (The
                                270                 :                :              * HINT will be sent to the client, but that's not a secret.)
                                271                 :                :              */
                                272         [ +  - ]:              3 :             ereport(ERROR,
                                273                 :                :                     errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
                                274                 :                :                     errmsg("library \"%s\" may not be used as an output plugin",
                                275                 :                :                            plugin),
                                276                 :                :             /*- translator: The second %s is the value of the output_plugin_libraries GUC, which may contain whitespace, commas, and double-quotes (") */
                                277                 :                :                     errdetail_log("The configuration parameter \"%s\" (currently '%s') does not name this library as a trusted output plugin.",
                                278                 :                :                                   "output_plugin_libraries",
                                279                 :                :                                   output_plugin_libraries_string),
                                280                 :                :                     errhint("If it is safe for all REPLICATION users to use this library as an output plugin, add it to \"%s\" and reload the server configuration.",
                                281                 :                :                             "output_plugin_libraries"));
                                282                 :                :         }
                                283                 :                : 
                                284                 :           1305 :         LoadOutputPlugin(&ctx->callbacks, plugin);
                                285                 :                :     }
                                286                 :                : 
                                287                 :                :     /*
                                288                 :                :      * Now that the slot's xmin has been set, we can announce ourselves as a
                                289                 :                :      * logical decoding backend which doesn't need to be checked individually
                                290                 :                :      * when computing the xmin horizon because the xmin is enforced via
                                291                 :                :      * replication slots.
                                292                 :                :      *
                                293                 :                :      * We can only do so if we're outside of a transaction (i.e. the case when
                                294                 :                :      * streaming changes via walsender), otherwise an already setup
                                295                 :                :      * snapshot/xid would end up being ignored. That's not a particularly
                                296                 :                :      * bothersome restriction since the SQL interface can't be used for
                                297                 :                :      * streaming anyway.
                                298                 :                :      */
 4379 andres@anarazel.de        299         [ +  + ]:           1332 :     if (!IsTransactionOrTransactionBlock())
                                300                 :                :     {
 2193 alvherre@alvh.no-ip.      301                 :            675 :         LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
 2203                           302                 :            675 :         MyProc->statusFlags |= PROC_IN_LOGICAL_DECODING;
                                303                 :            675 :         ProcGlobal->statusFlags[MyProc->pgxactoff] = MyProc->statusFlags;
 4379 andres@anarazel.de        304                 :            675 :         LWLockRelease(ProcArrayLock);
                                305                 :                :     }
                                306                 :                : 
 4653 rhaas@postgresql.org      307                 :           1332 :     ctx->slot = slot;
                                308                 :                : 
 2028 tmunro@postgresql.or      309                 :           1332 :     ctx->reader = XLogReaderAllocate(wal_segment_size, NULL, xl_routine, ctx);
 4257 fujii@postgresql.org      310         [ -  + ]:           1332 :     if (!ctx->reader)
 4257 fujii@postgresql.org      311         [ #  # ]:UBC           0 :         ereport(ERROR,
                                312                 :                :                 (errcode(ERRCODE_OUT_OF_MEMORY),
                                313                 :                :                  errmsg("out of memory"),
                                314                 :                :                  errdetail("Failed while allocating a WAL reading processor.")));
                                315                 :                : 
 4653 rhaas@postgresql.org      316                 :CBC        1332 :     ctx->reorder = ReorderBufferAllocate();
                                317                 :           1332 :     ctx->snapshot_builder =
 3502 andres@anarazel.de        318                 :           1332 :         AllocateSnapshotBuilder(ctx->reorder, xmin_horizon, start_lsn,
                                319                 :                :                                 need_full_snapshot, in_create, slot->data.two_phase_at);
                                320                 :                : 
 4653 rhaas@postgresql.org      321                 :           1332 :     ctx->reorder->private_data = ctx;
                                322                 :                : 
                                323                 :                :     /* wrap output plugin callbacks, so we can add error context information */
                                324                 :           1332 :     ctx->reorder->begin = begin_cb_wrapper;
                                325                 :           1332 :     ctx->reorder->apply_change = change_cb_wrapper;
 3157 peter_e@gmx.net           326                 :           1332 :     ctx->reorder->apply_truncate = truncate_cb_wrapper;
 4653 rhaas@postgresql.org      327                 :           1332 :     ctx->reorder->commit = commit_cb_wrapper;
 3888 simon@2ndQuadrant.co      328                 :           1332 :     ctx->reorder->message = message_cb_wrapper;
                                329                 :                : 
                                330                 :                :     /*
                                331                 :                :      * To support streaming, we require start/stop/abort/commit/change
                                332                 :                :      * callbacks. The message and truncate callbacks are optional, similar to
                                333                 :                :      * regular output plugins. We however enable streaming when at least one
                                334                 :                :      * of the methods is enabled so that we can easily identify missing
                                335                 :                :      * methods.
                                336                 :                :      *
                                337                 :                :      * We decide it here, but only check it later in the wrappers.
                                338                 :                :      */
 2314 akapila@postgresql.o      339                 :           2705 :     ctx->streaming = (ctx->callbacks.stream_start_cb != NULL) ||
                                340         [ +  - ]:             41 :         (ctx->callbacks.stream_stop_cb != NULL) ||
                                341         [ +  - ]:             41 :         (ctx->callbacks.stream_abort_cb != NULL) ||
                                342         [ +  - ]:             41 :         (ctx->callbacks.stream_commit_cb != NULL) ||
                                343         [ +  - ]:             41 :         (ctx->callbacks.stream_change_cb != NULL) ||
                                344   [ +  +  +  - ]:           1414 :         (ctx->callbacks.stream_message_cb != NULL) ||
                                345         [ -  + ]:             41 :         (ctx->callbacks.stream_truncate_cb != NULL);
                                346                 :                : 
                                347                 :                :     /*
                                348                 :                :      * streaming callbacks
                                349                 :                :      *
                                350                 :                :      * stream_message and stream_truncate callbacks are optional, so we do not
                                351                 :                :      * fail with ERROR when missing, but the wrappers simply do nothing. We
                                352                 :                :      * must set the ReorderBuffer callbacks to something, otherwise the calls
                                353                 :                :      * from there will crash (we don't want to move the checks there).
                                354                 :                :      */
                                355                 :           1332 :     ctx->reorder->stream_start = stream_start_cb_wrapper;
                                356                 :           1332 :     ctx->reorder->stream_stop = stream_stop_cb_wrapper;
                                357                 :           1332 :     ctx->reorder->stream_abort = stream_abort_cb_wrapper;
 2159                           358                 :           1332 :     ctx->reorder->stream_prepare = stream_prepare_cb_wrapper;
 2314                           359                 :           1332 :     ctx->reorder->stream_commit = stream_commit_cb_wrapper;
                                360                 :           1332 :     ctx->reorder->stream_change = stream_change_cb_wrapper;
                                361                 :           1332 :     ctx->reorder->stream_message = stream_message_cb_wrapper;
                                362                 :           1332 :     ctx->reorder->stream_truncate = stream_truncate_cb_wrapper;
                                363                 :                : 
                                364                 :                : 
                                365                 :                :     /*
                                366                 :                :      * To support two-phase logical decoding, we require
                                367                 :                :      * begin_prepare/prepare/commit-prepare/abort-prepare callbacks. The
                                368                 :                :      * filter_prepare callback is optional. We however enable two-phase
                                369                 :                :      * logical decoding when at least one of the methods is enabled so that we
                                370                 :                :      * can easily identify missing methods.
                                371                 :                :      *
                                372                 :                :      * We decide it here, but only check it later in the wrappers.
                                373                 :                :      */
 2159                           374                 :           2705 :     ctx->twophase = (ctx->callbacks.begin_prepare_cb != NULL) ||
                                375         [ +  - ]:             41 :         (ctx->callbacks.prepare_cb != NULL) ||
                                376         [ +  - ]:             41 :         (ctx->callbacks.commit_prepared_cb != NULL) ||
                                377         [ +  - ]:             41 :         (ctx->callbacks.rollback_prepared_cb != NULL) ||
                                378   [ +  +  +  - ]:           1414 :         (ctx->callbacks.stream_prepare_cb != NULL) ||
                                379         [ -  + ]:             41 :         (ctx->callbacks.filter_prepare_cb != NULL);
                                380                 :                : 
                                381                 :                :     /*
                                382                 :                :      * Callback to support decoding at prepare time.
                                383                 :                :      */
                                384                 :           1332 :     ctx->reorder->begin_prepare = begin_prepare_cb_wrapper;
                                385                 :           1332 :     ctx->reorder->prepare = prepare_cb_wrapper;
                                386                 :           1332 :     ctx->reorder->commit_prepared = commit_prepared_cb_wrapper;
                                387                 :           1332 :     ctx->reorder->rollback_prepared = rollback_prepared_cb_wrapper;
                                388                 :                : 
                                389                 :                :     /*
                                390                 :                :      * Callback to support updating progress during sending data of a
                                391                 :                :      * transaction (and its subtransactions) to the output plugin.
                                392                 :                :      */
 1389                           393                 :           1332 :     ctx->reorder->update_progress_txn = update_progress_txn_cb_wrapper;
                                394                 :                : 
 4653 rhaas@postgresql.org      395                 :           1332 :     ctx->out = makeStringInfo();
                                396                 :           1332 :     ctx->prepare_write = prepare_write;
                                397                 :           1332 :     ctx->write = do_write;
 3487 simon@2ndQuadrant.co      398                 :           1332 :     ctx->update_progress = update_progress;
                                399                 :                : 
 4653 rhaas@postgresql.org      400                 :           1332 :     ctx->output_plugin_options = output_plugin_options;
                                401                 :                : 
 3237 simon@2ndQuadrant.co      402                 :           1332 :     ctx->fast_forward = fast_forward;
                                403                 :                : 
 4653 rhaas@postgresql.org      404                 :           1332 :     MemoryContextSwitchTo(old_context);
                                405                 :                : 
                                406                 :           1332 :     return ctx;
                                407                 :                : }
                                408                 :                : 
                                409                 :                : /*
                                410                 :                :  * Create a new decoding context, for a new logical slot.
                                411                 :                :  *
                                412                 :                :  * plugin -- contains the name of the output plugin
                                413                 :                :  * output_plugin_options -- contains options passed to the output plugin
                                414                 :                :  * need_full_snapshot -- if true, must obtain a snapshot able to read all
                                415                 :                :  *      tables; if false, one that can read only catalogs is acceptable.
                                416                 :                :  * for_repack -- if true, we're going to be decoding for REPACK.
                                417                 :                :  * restart_lsn -- if given as invalid, it's this routine's responsibility to
                                418                 :                :  *      mark WAL as reserved by setting a convenient restart_lsn for the slot.
                                419                 :                :  *      Otherwise, we set for decoding to start from the given LSN without
                                420                 :                :  *      marking WAL reserved beforehand.  In that scenario, it's up to the
                                421                 :                :  *      caller to guarantee that WAL remains available.
                                422                 :                :  * xl_routine -- XLogReaderRoutine for underlying XLogReader
                                423                 :                :  * prepare_write, do_write, update_progress --
                                424                 :                :  *      callbacks that perform the use-case dependent, actual, work.
                                425                 :                :  *
                                426                 :                :  * Needs to be called while in a memory context that's at least as long lived
                                427                 :                :  * as the decoding context because further memory contexts will be created
                                428                 :                :  * inside it.
                                429                 :                :  *
                                430                 :                :  * Returns an initialized decoding context after calling the output plugin's
                                431                 :                :  * startup function.
                                432                 :                :  */
                                433                 :                : LogicalDecodingContext *
 2303 peter@eisentraut.org      434                 :            560 : CreateInitDecodingContext(const char *plugin,
                                435                 :                :                           List *output_plugin_options,
                                436                 :                :                           bool need_full_snapshot,
                                437                 :                :                           bool for_repack,
                                438                 :                :                           XLogRecPtr restart_lsn,
                                439                 :                :                           XLogReaderRoutine *xl_routine,
                                440                 :                :                           LogicalOutputPluginWriterPrepareWrite prepare_write,
                                441                 :                :                           LogicalOutputPluginWriterWrite do_write,
                                442                 :                :                           LogicalOutputPluginWriterUpdateProgress update_progress)
                                443                 :                : {
 4589 bruce@momjian.us          444                 :            560 :     TransactionId xmin_horizon = InvalidTransactionId;
                                445                 :                :     ReplicationSlot *slot;
                                446                 :                :     NameData    plugin_name;
                                447                 :                :     LogicalDecodingContext *ctx;
                                448                 :                :     MemoryContext old_context;
                                449                 :                : 
                                450                 :                :     /*
                                451                 :                :      * On a standby, this check is also required while creating the slot.
                                452                 :                :      * Check the comments in the function.
                                453                 :                :      */
  235 alvherre@kurilemu.de      454                 :            560 :     CheckLogicalDecodingRequirements(for_repack);
                                455                 :                : 
                                456                 :                :     /* shorter lines... */
 4653 rhaas@postgresql.org      457                 :            560 :     slot = MyReplicationSlot;
                                458                 :                : 
                                459                 :                :     /* first some sanity checks that are unlikely to be violated */
                                460         [ -  + ]:            560 :     if (slot == NULL)
 4210 heikki.linnakangas@i      461         [ #  # ]:UBC           0 :         elog(ERROR, "cannot perform logical decoding without an acquired slot");
                                462                 :                : 
 4653 rhaas@postgresql.org      463         [ -  + ]:CBC         560 :     if (plugin == NULL)
 4653 rhaas@postgresql.org      464         [ #  # ]:UBC           0 :         elog(ERROR, "cannot initialize logical decoding without a specified plugin");
                                465                 :                : 
                                466                 :                :     /* Make sure the passed slot is suitable. These are user facing errors. */
 4127 andres@anarazel.de        467         [ -  + ]:CBC         560 :     if (SlotIsPhysical(slot))
 4653 rhaas@postgresql.org      468         [ #  # ]:UBC           0 :         ereport(ERROR,
                                469                 :                :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                                470                 :                :                  errmsg("cannot use physical replication slot for logical decoding")));
                                471                 :                : 
 4653 rhaas@postgresql.org      472         [ -  + ]:CBC         560 :     if (slot->data.database != MyDatabaseId)
 4653 rhaas@postgresql.org      473         [ #  # ]:UBC           0 :         ereport(ERROR,
                                474                 :                :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                                475                 :                :                  errmsg("replication slot \"%s\" was not created in this database",
                                476                 :                :                         NameStr(slot->data.name))));
                                477                 :                : 
 4653 rhaas@postgresql.org      478   [ +  +  +  + ]:CBC         958 :     if (IsTransactionState() &&
                                479                 :            398 :         GetTopTransactionIdIfAny() != InvalidTransactionId)
                                480         [ +  - ]:              2 :         ereport(ERROR,
                                481                 :                :                 (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
                                482                 :                :                  errmsg("cannot create logical replication slot in transaction that has performed writes")));
                                483                 :                : 
                                484                 :                :     /*
                                485                 :                :      * Register output plugin name with slot.  We need the mutex to avoid
                                486                 :                :      * concurrent reading of a partially copied string.  But we don't want any
                                487                 :                :      * complicated code while holding a spinlock, so do namestrcpy() outside.
                                488                 :                :      */
 2301 peter@eisentraut.org      489                 :            558 :     namestrcpy(&plugin_name, plugin);
 4653 rhaas@postgresql.org      490                 :            558 :     SpinLockAcquire(&slot->mutex);
 2301 peter@eisentraut.org      491                 :            558 :     slot->data.plugin = plugin_name;
 4653 rhaas@postgresql.org      492                 :            558 :     SpinLockRelease(&slot->mutex);
                                493                 :                : 
  387 alvherre@kurilemu.de      494         [ +  + ]:            558 :     if (!XLogRecPtrIsValid(restart_lsn))
 2794 alvherre@alvh.no-ip.      495                 :            551 :         ReplicationSlotReserveWal();
                                496                 :                :     else
                                497                 :                :     {
                                498                 :              7 :         SpinLockAcquire(&slot->mutex);
                                499                 :              7 :         slot->data.restart_lsn = restart_lsn;
                                500                 :              7 :         SpinLockRelease(&slot->mutex);
                                501                 :                :     }
                                502                 :                : 
                                503                 :                :     /* ----
                                504                 :                :      * This is a bit tricky: We need to determine a safe xmin horizon to start
                                505                 :                :      * decoding from, to avoid starting from a running xacts record referring
                                506                 :                :      * to xids whose rows have been vacuumed or pruned
                                507                 :                :      * already. GetOldestSafeDecodingTransactionId() returns such a value, but
                                508                 :                :      * without further interlock its return value might immediately be out of
                                509                 :                :      * date.
                                510                 :                :      *
                                511                 :                :      * So we have to acquire both the ReplicationSlotControlLock and the
                                512                 :                :      * ProcArrayLock to prevent concurrent computation and update of new xmin
                                513                 :                :      * horizons by other backends, get the safe decoding xid, and inform the
                                514                 :                :      * slot machinery about the new limit. Once that's done both locks can be
                                515                 :                :      * released as the slot machinery now is protecting against vacuum.
                                516                 :                :      *
                                517                 :                :      * Note that, temporarily, the data, not just the catalog, xmin has to be
                                518                 :                :      * reserved if a data snapshot is to be exported.  Otherwise the initial
                                519                 :                :      * data snapshot created here is not guaranteed to be valid. After that
                                520                 :                :      * the data xmin doesn't need to be managed anymore and the global xmin
                                521                 :                :      * should be recomputed. As we are fine with losing the pegged data xmin
                                522                 :                :      * after crash - no chance a snapshot would get exported anymore - we can
                                523                 :                :      * get away with just setting the slot's
                                524                 :                :      * effective_xmin. ReplicationSlotRelease will reset it again.
                                525                 :                :      *
                                526                 :                :      * ----
                                527                 :                :      */
  333 msawada@postgresql.o      528                 :            558 :     LWLockAcquire(ReplicationSlotControlLock, LW_EXCLUSIVE);
 4653 rhaas@postgresql.org      529                 :            558 :     LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
                                530                 :                : 
 3402 andres@anarazel.de        531                 :            558 :     xmin_horizon = GetOldestSafeDecodingTransactionId(!need_full_snapshot);
                                532                 :                : 
 3102 michael@paquier.xyz       533                 :            558 :     SpinLockAcquire(&slot->mutex);
 3506 andres@anarazel.de        534                 :            558 :     slot->effective_catalog_xmin = xmin_horizon;
                                535                 :            558 :     slot->data.catalog_xmin = xmin_horizon;
                                536         [ +  + ]:            558 :     if (need_full_snapshot)
                                537                 :            237 :         slot->effective_xmin = xmin_horizon;
 3102 michael@paquier.xyz       538                 :            558 :     SpinLockRelease(&slot->mutex);
                                539                 :                : 
 4653 rhaas@postgresql.org      540                 :            558 :     ReplicationSlotsComputeRequiredXmin(true);
                                541                 :                : 
                                542                 :            558 :     LWLockRelease(ProcArrayLock);
  333 msawada@postgresql.o      543                 :            558 :     LWLockRelease(ReplicationSlotControlLock);
                                544                 :                : 
 4653 rhaas@postgresql.org      545                 :            558 :     ReplicationSlotMarkDirty();
                                546                 :            558 :     ReplicationSlotSave();
                                547                 :                : 
 2794 alvherre@alvh.no-ip.      548                 :            558 :     ctx = StartupDecodingContext(NIL, restart_lsn, xmin_horizon,
                                549                 :                :                                  need_full_snapshot, false, true, for_repack,
                                550                 :                :                                  xl_routine, prepare_write, do_write,
                                551                 :                :                                  update_progress);
                                552                 :                : 
                                553                 :                :     /* call output plugin initialization callback */
 4653 rhaas@postgresql.org      554                 :            553 :     old_context = MemoryContextSwitchTo(ctx->context);
                                555         [ +  - ]:            553 :     if (ctx->callbacks.startup_cb != NULL)
                                556                 :            553 :         startup_cb_wrapper(ctx, &ctx->options, true);
                                557                 :            553 :     MemoryContextSwitchTo(old_context);
                                558                 :                : 
                                559                 :                :     /*
                                560                 :                :      * We allow decoding of prepared transactions when the two_phase is
                                561                 :                :      * enabled at the time of slot creation, or when the two_phase option is
                                562                 :                :      * given at the streaming start, provided the plugin supports all the
                                563                 :                :      * callbacks for two-phase.
                                564                 :                :      */
 1963 akapila@postgresql.o      565                 :            553 :     ctx->twophase &= slot->data.two_phase;
                                566                 :                : 
 3174 peter_e@gmx.net           567                 :            553 :     ctx->reorder->output_rewrites = ctx->options.receive_rewrites;
                                568                 :                : 
 4653 rhaas@postgresql.org      569                 :            553 :     return ctx;
                                570                 :                : }
                                571                 :                : 
                                572                 :                : /*
                                573                 :                :  * Create a new decoding context, for a logical slot that has previously been
                                574                 :                :  * used already.
                                575                 :                :  *
                                576                 :                :  * start_lsn
                                577                 :                :  *      The LSN at which to start decoding.  If InvalidXLogRecPtr, restart
                                578                 :                :  *      from the slot's confirmed_flush; otherwise, start from the specified
                                579                 :                :  *      location (but move it forwards to confirmed_flush if it's older than
                                580                 :                :  *      that, see below).
                                581                 :                :  *
                                582                 :                :  * output_plugin_options
                                583                 :                :  *      options passed to the output plugin.
                                584                 :                :  *
                                585                 :                :  * fast_forward
                                586                 :                :  *      bypass the generation of logical changes.
                                587                 :                :  *
                                588                 :                :  * xl_routine
                                589                 :                :  *      XLogReaderRoutine used by underlying xlogreader
                                590                 :                :  *
                                591                 :                :  * prepare_write, do_write, update_progress
                                592                 :                :  *      callbacks that have to be filled to perform the use-case dependent,
                                593                 :                :  *      actual work.
                                594                 :                :  *
                                595                 :                :  * Needs to be called while in a memory context that's at least as long lived
                                596                 :                :  * as the decoding context because further memory contexts will be created
                                597                 :                :  * inside it.
                                598                 :                :  *
                                599                 :                :  * Returns an initialized decoding context after calling the output plugin's
                                600                 :                :  * startup function.
                                601                 :                :  */
                                602                 :                : LogicalDecodingContext *
                                603                 :            784 : CreateDecodingContext(XLogRecPtr start_lsn,
                                604                 :                :                       List *output_plugin_options,
                                605                 :                :                       bool fast_forward,
                                606                 :                :                       XLogReaderRoutine *xl_routine,
                                607                 :                :                       LogicalOutputPluginWriterPrepareWrite prepare_write,
                                608                 :                :                       LogicalOutputPluginWriterWrite do_write,
                                609                 :                :                       LogicalOutputPluginWriterUpdateProgress update_progress)
                                610                 :                : {
                                611                 :                :     LogicalDecodingContext *ctx;
                                612                 :                :     ReplicationSlot *slot;
                                613                 :                :     MemoryContext old_context;
                                614                 :                : 
                                615                 :                :     /* shorter lines... */
                                616                 :            784 :     slot = MyReplicationSlot;
                                617                 :                : 
                                618                 :                :     /* first some sanity checks that are unlikely to be violated */
                                619         [ -  + ]:            784 :     if (slot == NULL)
 4210 heikki.linnakangas@i      620         [ #  # ]:UBC           0 :         elog(ERROR, "cannot perform logical decoding without an acquired slot");
                                621                 :                : 
                                622                 :                :     /* make sure the passed slot is suitable, these are user facing errors */
 4127 andres@anarazel.de        623         [ +  + ]:CBC         784 :     if (SlotIsPhysical(slot))
 4653 rhaas@postgresql.org      624         [ +  - ]:              1 :         ereport(ERROR,
                                625                 :                :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                                626                 :                :                  errmsg("cannot use physical replication slot for logical decoding")));
                                627                 :                : 
                                628                 :                :     /*
                                629                 :                :      * We need to access the system tables during decoding to build the
                                630                 :                :      * logical changes unless we are in fast_forward mode where no changes are
                                631                 :                :      * generated.
                                632                 :                :      */
  969 akapila@postgresql.o      633   [ +  +  +  + ]:            783 :     if (slot->data.database != MyDatabaseId && !fast_forward)
 4653 rhaas@postgresql.org      634         [ +  - ]:              3 :         ereport(ERROR,
                                635                 :                :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                                636                 :                :                  errmsg("replication slot \"%s\" was not created in this database",
                                637                 :                :                         NameStr(slot->data.name))));
                                638                 :                : 
                                639                 :                :     /*
                                640                 :                :      * The slots being synced from the primary can't be used for decoding as
                                641                 :                :      * they are used after failover. However, we do allow advancing the LSNs
                                642                 :                :      * during the synchronization of slots. See update_local_synced_slot.
                                643                 :                :      */
  969 akapila@postgresql.o      644   [ +  +  +  +  :            780 :     if (RecoveryInProgress() && slot->data.synced && !IsSyncingReplicationSlots())
                                              +  + ]
 1018                           645         [ +  - ]:              1 :         ereport(ERROR,
                                646                 :                :                 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                                647                 :                :                 errmsg("cannot use replication slot \"%s\" for logical decoding",
                                648                 :                :                        NameStr(slot->data.name)),
                                649                 :                :                 errdetail("This replication slot is being synchronized from the primary server."),
                                650                 :                :                 errhint("Specify another replication slot."));
                                651                 :                : 
                                652                 :                :     /* slot must be valid to allow decoding */
  666                           653         [ -  + ]:            779 :     Assert(slot->data.invalidated == RS_INVAL_NONE);
  387 alvherre@kurilemu.de      654         [ -  + ]:            779 :     Assert(XLogRecPtrIsValid(slot->data.restart_lsn));
                                655                 :                : 
                                656         [ +  + ]:            779 :     if (!XLogRecPtrIsValid(start_lsn))
                                657                 :                :     {
                                658                 :                :         /* continue from last position */
 4653 rhaas@postgresql.org      659                 :            430 :         start_lsn = slot->data.confirmed_flush;
                                660                 :                :     }
                                661         [ +  + ]:            349 :     else if (start_lsn < slot->data.confirmed_flush)
                                662                 :                :     {
                                663                 :                :         /*
                                664                 :                :          * It might seem like we should error out in this case, but it's
                                665                 :                :          * pretty common for a client to acknowledge a LSN it doesn't have to
                                666                 :                :          * do anything for, and thus didn't store persistently, because the
                                667                 :                :          * xlog records didn't result in anything relevant for logical
                                668                 :                :          * decoding. Clients have to be able to do that to support synchronous
                                669                 :                :          * replication.
                                670                 :                :          *
                                671                 :                :          * Starting at a different LSN than requested might not catch certain
                                672                 :                :          * kinds of client errors; so the client may wish to check that
                                673                 :                :          * confirmed_flush_lsn matches its expectations.
                                674                 :                :          */
  509 alvherre@kurilemu.de      675         [ +  - ]:             34 :         elog(LOG, "%X/%08X has been already streamed, forwarding to %X/%08X",
                                676                 :                :              LSN_FORMAT_ARGS(start_lsn),
                                677                 :                :              LSN_FORMAT_ARGS(slot->data.confirmed_flush));
                                678                 :                : 
 4133 andres@anarazel.de        679                 :             34 :         start_lsn = slot->data.confirmed_flush;
                                680                 :                :     }
                                681                 :                : 
 4653 rhaas@postgresql.org      682                 :            779 :     ctx = StartupDecodingContext(output_plugin_options,
                                683                 :                :                                  start_lsn, InvalidTransactionId, false,
                                684                 :                :                                  fast_forward, false, false,
                                685                 :                :                                  xl_routine, prepare_write,
                                686                 :                :                                  do_write, update_progress);
                                687                 :                : 
                                688                 :                :     /* call output plugin initialization callback */
                                689                 :            779 :     old_context = MemoryContextSwitchTo(ctx->context);
                                690         [ +  + ]:            779 :     if (ctx->callbacks.startup_cb != NULL)
 4625                           691                 :            751 :         startup_cb_wrapper(ctx, &ctx->options, false);
 4653                           692                 :            773 :     MemoryContextSwitchTo(old_context);
                                693                 :                : 
                                694                 :                :     /*
                                695                 :                :      * We allow decoding of prepared transactions when the two_phase is
                                696                 :                :      * enabled at the time of slot creation, or when the two_phase option is
                                697                 :                :      * given at the streaming start, provided the plugin supports all the
                                698                 :                :      * callbacks for two-phase.
                                699                 :                :      */
 1963 akapila@postgresql.o      700   [ +  +  +  + ]:            773 :     ctx->twophase &= (slot->data.two_phase || ctx->twophase_opt_given);
                                701                 :                : 
                                702                 :                :     /* Mark slot to allow two_phase decoding if not already marked */
                                703   [ +  +  +  + ]:            773 :     if (ctx->twophase && !slot->data.two_phase)
                                704                 :                :     {
 1416 michael@paquier.xyz       705                 :              9 :         SpinLockAcquire(&slot->mutex);
 1963 akapila@postgresql.o      706                 :              9 :         slot->data.two_phase = true;
                                707                 :              9 :         slot->data.two_phase_at = start_lsn;
 1416 michael@paquier.xyz       708                 :              9 :         SpinLockRelease(&slot->mutex);
 1963 akapila@postgresql.o      709                 :              9 :         ReplicationSlotMarkDirty();
                                710                 :              9 :         ReplicationSlotSave();
                                711                 :              9 :         SnapBuildSetTwoPhaseAt(ctx->snapshot_builder, start_lsn);
                                712                 :                :     }
                                713                 :                : 
 3174 peter_e@gmx.net           714                 :            773 :     ctx->reorder->output_rewrites = ctx->options.receive_rewrites;
                                715                 :                : 
  232 fujii@postgresql.org      716   [ +  +  +  + ]:            773 :     ereport(LogicalDecodingLogLevel(),
                                717                 :                :             (errmsg("starting logical decoding for slot \"%s\"",
                                718                 :                :                     NameStr(slot->data.name)),
                                719                 :                :              errdetail("Streaming transactions committing after %X/%08X, reading WAL from %X/%08X.",
                                720                 :                :                        LSN_FORMAT_ARGS(slot->data.confirmed_flush),
                                721                 :                :                        LSN_FORMAT_ARGS(slot->data.restart_lsn))));
                                722                 :                : 
 4653 rhaas@postgresql.org      723                 :            773 :     return ctx;
                                724                 :                : }
                                725                 :                : 
                                726                 :                : /*
                                727                 :                :  * Returns true if a consistent initial decoding snapshot has been built.
                                728                 :                :  */
                                729                 :                : bool
                                730                 :           4924 : DecodingContextReady(LogicalDecodingContext *ctx)
                                731                 :                : {
                                732                 :           4924 :     return SnapBuildCurrentState(ctx->snapshot_builder) == SNAPBUILD_CONSISTENT;
                                733                 :                : }
                                734                 :                : 
                                735                 :                : /*
                                736                 :                :  * Read from the decoding slot, until it is ready to start extracting changes.
                                737                 :                :  */
                                738                 :                : void
                                739                 :            546 : DecodingContextFindStartpoint(LogicalDecodingContext *ctx)
                                740                 :                : {
 3102 michael@paquier.xyz       741                 :            546 :     ReplicationSlot *slot = ctx->slot;
                                742                 :                : 
                                743                 :                :     /* Initialize from where to start reading WAL. */
 2498 heikki.linnakangas@i      744                 :            546 :     XLogBeginRead(ctx->reader, slot->data.restart_lsn);
                                745                 :                : 
  509 alvherre@kurilemu.de      746         [ +  + ]:            546 :     elog(DEBUG1, "searching for logical decoding starting point, starting at %X/%08X",
                                747                 :                :          LSN_FORMAT_ARGS(slot->data.restart_lsn));
                                748                 :                : 
                                749                 :                :     /* Wait for a consistent starting point */
                                750                 :                :     for (;;)
 4653 rhaas@postgresql.org      751                 :           4368 :     {
                                752                 :                :         XLogRecord *record;
                                753                 :           4914 :         char       *err = NULL;
                                754                 :                : 
                                755                 :                :         /* the read_page callback waits for new WAL */
 2028 tmunro@postgresql.or      756                 :           4914 :         record = XLogReadRecord(ctx->reader, &err);
 4653 rhaas@postgresql.org      757         [ -  + ]:           4914 :         if (err)
 1844 michael@paquier.xyz       758         [ #  # ]:UBC           0 :             elog(ERROR, "could not find logical decoding starting point: %s", err);
 4391 heikki.linnakangas@i      759         [ -  + ]:CBC        4914 :         if (!record)
 1844 michael@paquier.xyz       760         [ #  # ]:UBC           0 :             elog(ERROR, "could not find logical decoding starting point");
                                761                 :                : 
 4391 heikki.linnakangas@i      762                 :CBC        4914 :         LogicalDecodingProcessRecord(ctx, ctx->reader);
                                763                 :                : 
                                764                 :                :         /* only continue till we found a consistent spot */
 4653 rhaas@postgresql.org      765         [ +  + ]:           4912 :         if (DecodingContextReady(ctx))
                                766                 :            544 :             break;
                                767                 :                : 
 4535 andres@anarazel.de        768         [ -  + ]:           4368 :         CHECK_FOR_INTERRUPTS();
                                769                 :                :     }
                                770                 :                : 
 3102 michael@paquier.xyz       771                 :            544 :     SpinLockAcquire(&slot->mutex);
                                772                 :            544 :     slot->data.confirmed_flush = ctx->reader->EndRecPtr;
 1963 akapila@postgresql.o      773         [ +  + ]:            544 :     if (slot->data.two_phase)
                                774                 :              9 :         slot->data.two_phase_at = ctx->reader->EndRecPtr;
 3102 michael@paquier.xyz       775                 :            544 :     SpinLockRelease(&slot->mutex);
 4653 rhaas@postgresql.org      776                 :            544 : }
                                777                 :                : 
                                778                 :                : /*
                                779                 :                :  * Free a previously allocated decoding context, invoking the shutdown
                                780                 :                :  * callback if necessary.
                                781                 :                :  */
                                782                 :                : void
                                783                 :           1027 : FreeDecodingContext(LogicalDecodingContext *ctx)
                                784                 :                : {
                                785         [ +  + ]:           1027 :     if (ctx->callbacks.shutdown_cb != NULL)
                                786                 :            999 :         shutdown_cb_wrapper(ctx);
                                787                 :                : 
                                788                 :           1027 :     ReorderBufferFree(ctx->reorder);
                                789                 :           1027 :     FreeSnapshotBuilder(ctx->snapshot_builder);
                                790                 :           1027 :     XLogReaderFree(ctx->reader);
                                791                 :           1027 :     MemoryContextDelete(ctx->context);
                                792                 :           1027 : }
                                793                 :                : 
                                794                 :                : /*
                                795                 :                :  * Prepare a write using the context's output routine.
                                796                 :                :  */
                                797                 :                : void
                                798                 :         357117 : OutputPluginPrepareWrite(struct LogicalDecodingContext *ctx, bool last_write)
                                799                 :                : {
                                800         [ -  + ]:         357117 :     if (!ctx->accept_writes)
 4653 rhaas@postgresql.org      801         [ #  # ]:UBC           0 :         elog(ERROR, "writes are only accepted in commit, begin and change callbacks");
                                802                 :                : 
 4653 rhaas@postgresql.org      803                 :CBC      357117 :     ctx->prepare_write(ctx, ctx->write_location, ctx->write_xid, last_write);
                                804                 :         357117 :     ctx->prepared_write = true;
                                805                 :         357117 : }
                                806                 :                : 
                                807                 :                : /*
                                808                 :                :  * Perform a write using the context's output routine.
                                809                 :                :  */
                                810                 :                : void
                                811                 :         357115 : OutputPluginWrite(struct LogicalDecodingContext *ctx, bool last_write)
                                812                 :                : {
                                813         [ -  + ]:         357115 :     if (!ctx->prepared_write)
 4653 rhaas@postgresql.org      814         [ #  # ]:UBC           0 :         elog(ERROR, "OutputPluginPrepareWrite needs to be called before OutputPluginWrite");
                                815                 :                : 
 4653 rhaas@postgresql.org      816                 :CBC      357115 :     ctx->write(ctx, ctx->write_location, ctx->write_xid, last_write);
                                817                 :         357102 :     ctx->prepared_write = false;
                                818                 :         357102 : }
                                819                 :                : 
                                820                 :                : /*
                                821                 :                :  * Update progress tracking (if supported).
                                822                 :                :  */
                                823                 :                : void
 1704 akapila@postgresql.o      824                 :           5496 : OutputPluginUpdateProgress(struct LogicalDecodingContext *ctx,
                                825                 :                :                            bool skipped_xact)
                                826                 :                : {
 3487 simon@2ndQuadrant.co      827         [ +  + ]:           5496 :     if (!ctx->update_progress)
                                828                 :           1600 :         return;
                                829                 :                : 
 1704 akapila@postgresql.o      830                 :           3896 :     ctx->update_progress(ctx, ctx->write_location, ctx->write_xid,
                                831                 :                :                          skipped_xact);
                                832                 :                : }
                                833                 :                : 
                                834                 :                : /*
                                835                 :                :  * Load the output plugin, lookup its output plugin init function, and check
                                836                 :                :  * that it provides the required callbacks. The caller must have checked that
                                837                 :                :  * the current user has the necessary privileges to load the given plugin;
                                838                 :                :  * standard LOAD restrictions are not applied here.
                                839                 :                :  */
                                840                 :                : static void
 2303 peter@eisentraut.org      841                 :           1305 : LoadOutputPlugin(OutputPluginCallbacks *callbacks, const char *plugin)
                                842                 :                : {
                                843                 :                :     LogicalOutputPluginInit plugin_init;
                                844                 :                : 
 4653 rhaas@postgresql.org      845                 :           1304 :     plugin_init = (LogicalOutputPluginInit)
                                846                 :           1305 :         load_external_function(plugin, "_PG_output_plugin_init", false, NULL);
                                847                 :                : 
                                848         [ -  + ]:           1304 :     if (plugin_init == NULL)
 4653 rhaas@postgresql.org      849         [ #  # ]:UBC           0 :         elog(ERROR, "output plugins have to declare the _PG_output_plugin_init symbol");
                                850                 :                : 
                                851                 :                :     /* ask the output plugin to fill the callback struct */
 4653 rhaas@postgresql.org      852                 :CBC        1304 :     plugin_init(callbacks);
                                853                 :                : 
                                854         [ -  + ]:           1304 :     if (callbacks->begin_cb == NULL)
 4653 rhaas@postgresql.org      855         [ #  # ]:UBC           0 :         elog(ERROR, "output plugins have to register a begin callback");
 4653 rhaas@postgresql.org      856         [ -  + ]:CBC        1304 :     if (callbacks->change_cb == NULL)
 4653 rhaas@postgresql.org      857         [ #  # ]:UBC           0 :         elog(ERROR, "output plugins have to register a change callback");
 4653 rhaas@postgresql.org      858         [ -  + ]:CBC        1304 :     if (callbacks->commit_cb == NULL)
 4653 rhaas@postgresql.org      859         [ #  # ]:UBC           0 :         elog(ERROR, "output plugins have to register a commit callback");
 4653 rhaas@postgresql.org      860                 :CBC        1304 : }
                                861                 :                : 
                                862                 :                : static void
                                863                 :           1042 : output_plugin_error_callback(void *arg)
                                864                 :                : {
                                865                 :           1042 :     LogicalErrorCallbackState *state = (LogicalErrorCallbackState *) arg;
                                866                 :                : 
                                867                 :                :     /* not all callbacks have an associated LSN  */
  387 alvherre@kurilemu.de      868         [ +  + ]:           1042 :     if (XLogRecPtrIsValid(state->report_location))
  509                           869                 :           1036 :         errcontext("slot \"%s\", output plugin \"%s\", in the %s callback, associated LSN %X/%08X",
 4653 rhaas@postgresql.org      870                 :           1036 :                    NameStr(state->ctx->slot->data.name),
                                871                 :           1036 :                    NameStr(state->ctx->slot->data.plugin),
                                872                 :                :                    state->callback_name,
 2104 peter@eisentraut.org      873                 :           1036 :                    LSN_FORMAT_ARGS(state->report_location));
                                874                 :                :     else
 4653 rhaas@postgresql.org      875                 :              6 :         errcontext("slot \"%s\", output plugin \"%s\", in the %s callback",
                                876                 :              6 :                    NameStr(state->ctx->slot->data.name),
                                877                 :              6 :                    NameStr(state->ctx->slot->data.plugin),
                                878                 :                :                    state->callback_name);
                                879                 :           1042 : }
                                880                 :                : 
                                881                 :                : static void
                                882                 :           1304 : startup_cb_wrapper(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
                                883                 :                : {
                                884                 :                :     LogicalErrorCallbackState state;
                                885                 :                :     ErrorContextCallback errcallback;
                                886                 :                : 
 3237 simon@2ndQuadrant.co      887         [ -  + ]:           1304 :     Assert(!ctx->fast_forward);
                                888                 :                : 
                                889                 :                :     /* Push callback + info on the error context stack */
 4653 rhaas@postgresql.org      890                 :           1304 :     state.ctx = ctx;
                                891                 :           1304 :     state.callback_name = "startup";
                                892                 :           1304 :     state.report_location = InvalidXLogRecPtr;
                                893                 :           1304 :     errcallback.callback = output_plugin_error_callback;
  730 peter@eisentraut.org      894                 :           1304 :     errcallback.arg = &state;
 4653 rhaas@postgresql.org      895                 :           1304 :     errcallback.previous = error_context_stack;
                                896                 :           1304 :     error_context_stack = &errcallback;
                                897                 :                : 
                                898                 :                :     /* set output state */
                                899                 :           1304 :     ctx->accept_writes = false;
 1662 akapila@postgresql.o      900                 :           1304 :     ctx->end_xact = false;
                                901                 :                : 
                                902                 :                :     /* do the actual work: call callback */
 4653 rhaas@postgresql.org      903                 :           1304 :     ctx->callbacks.startup_cb(ctx, opt, is_init);
                                904                 :                : 
                                905                 :                :     /* Pop the error context stack */
                                906                 :           1298 :     error_context_stack = errcallback.previous;
                                907                 :           1298 : }
                                908                 :                : 
                                909                 :                : static void
                                910                 :            999 : shutdown_cb_wrapper(LogicalDecodingContext *ctx)
                                911                 :                : {
                                912                 :                :     LogicalErrorCallbackState state;
                                913                 :                :     ErrorContextCallback errcallback;
                                914                 :                : 
 3237 simon@2ndQuadrant.co      915         [ -  + ]:            999 :     Assert(!ctx->fast_forward);
                                916                 :                : 
                                917                 :                :     /* Push callback + info on the error context stack */
 4653 rhaas@postgresql.org      918                 :            999 :     state.ctx = ctx;
                                919                 :            999 :     state.callback_name = "shutdown";
                                920                 :            999 :     state.report_location = InvalidXLogRecPtr;
                                921                 :            999 :     errcallback.callback = output_plugin_error_callback;
  730 peter@eisentraut.org      922                 :            999 :     errcallback.arg = &state;
 4653 rhaas@postgresql.org      923                 :            999 :     errcallback.previous = error_context_stack;
                                924                 :            999 :     error_context_stack = &errcallback;
                                925                 :                : 
                                926                 :                :     /* set output state */
                                927                 :            999 :     ctx->accept_writes = false;
 1662 akapila@postgresql.o      928                 :            999 :     ctx->end_xact = false;
                                929                 :                : 
                                930                 :                :     /* do the actual work: call callback */
 4653 rhaas@postgresql.org      931                 :            999 :     ctx->callbacks.shutdown_cb(ctx);
                                932                 :                : 
                                933                 :                :     /* Pop the error context stack */
                                934                 :            999 :     error_context_stack = errcallback.previous;
                                935                 :            999 : }
                                936                 :                : 
                                937                 :                : 
                                938                 :                : /*
                                939                 :                :  * Callbacks for ReorderBuffer which add in some more information and then call
                                940                 :                :  * output_plugin.h plugins.
                                941                 :                :  */
                                942                 :                : static void
                                943                 :           2536 : begin_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
                                944                 :                : {
                                945                 :           2536 :     LogicalDecodingContext *ctx = cache->private_data;
                                946                 :                :     LogicalErrorCallbackState state;
                                947                 :                :     ErrorContextCallback errcallback;
                                948                 :                : 
 3237 simon@2ndQuadrant.co      949         [ -  + ]:           2536 :     Assert(!ctx->fast_forward);
                                950                 :                : 
                                951                 :                :     /* Push callback + info on the error context stack */
 4653 rhaas@postgresql.org      952                 :           2536 :     state.ctx = ctx;
                                953                 :           2536 :     state.callback_name = "begin";
                                954                 :           2536 :     state.report_location = txn->first_lsn;
                                955                 :           2536 :     errcallback.callback = output_plugin_error_callback;
  730 peter@eisentraut.org      956                 :           2536 :     errcallback.arg = &state;
 4653 rhaas@postgresql.org      957                 :           2536 :     errcallback.previous = error_context_stack;
                                958                 :           2536 :     error_context_stack = &errcallback;
                                959                 :                : 
                                960                 :                :     /* set output state */
                                961                 :           2536 :     ctx->accept_writes = true;
                                962                 :           2536 :     ctx->write_xid = txn->xid;
                                963                 :           2536 :     ctx->write_location = txn->first_lsn;
 1662 akapila@postgresql.o      964                 :           2536 :     ctx->end_xact = false;
                                965                 :                : 
                                966                 :                :     /* do the actual work: call callback */
 4653 rhaas@postgresql.org      967                 :           2536 :     ctx->callbacks.begin_cb(ctx, txn);
                                968                 :                : 
                                969                 :                :     /* Pop the error context stack */
                                970                 :           2536 :     error_context_stack = errcallback.previous;
                                971                 :           2536 : }
                                972                 :                : 
                                973                 :                : static void
                                974                 :           2529 : commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
                                975                 :                :                   XLogRecPtr commit_lsn)
                                976                 :                : {
                                977                 :           2529 :     LogicalDecodingContext *ctx = cache->private_data;
                                978                 :                :     LogicalErrorCallbackState state;
                                979                 :                :     ErrorContextCallback errcallback;
                                980                 :                : 
 3237 simon@2ndQuadrant.co      981         [ -  + ]:           2529 :     Assert(!ctx->fast_forward);
                                982                 :                : 
                                983                 :                :     /* Push callback + info on the error context stack */
 4653 rhaas@postgresql.org      984                 :           2529 :     state.ctx = ctx;
                                985                 :           2529 :     state.callback_name = "commit";
 3447 tgl@sss.pgh.pa.us         986                 :           2529 :     state.report_location = txn->final_lsn; /* beginning of commit record */
 4653 rhaas@postgresql.org      987                 :           2529 :     errcallback.callback = output_plugin_error_callback;
  730 peter@eisentraut.org      988                 :           2529 :     errcallback.arg = &state;
 4653 rhaas@postgresql.org      989                 :           2529 :     errcallback.previous = error_context_stack;
                                990                 :           2529 :     error_context_stack = &errcallback;
                                991                 :                : 
                                992                 :                :     /* set output state */
                                993                 :           2529 :     ctx->accept_writes = true;
                                994                 :           2529 :     ctx->write_xid = txn->xid;
                                995                 :           2529 :     ctx->write_location = txn->end_lsn; /* points to the end of the record */
 1662 akapila@postgresql.o      996                 :           2529 :     ctx->end_xact = true;
                                997                 :                : 
                                998                 :                :     /* do the actual work: call callback */
 4653 rhaas@postgresql.org      999                 :           2529 :     ctx->callbacks.commit_cb(ctx, txn, commit_lsn);
                               1000                 :                : 
                               1001                 :                :     /* Pop the error context stack */
                               1002                 :           2520 :     error_context_stack = errcallback.previous;
                               1003                 :           2520 : }
                               1004                 :                : 
                               1005                 :                : /*
                               1006                 :                :  * The functionality of begin_prepare is quite similar to begin with the
                               1007                 :                :  * exception that this will have gid (global transaction id) information which
                               1008                 :                :  * can be used by plugin. Now, we thought about extending the existing begin
                               1009                 :                :  * but that would break the replication protocol and additionally this looks
                               1010                 :                :  * cleaner.
                               1011                 :                :  */
                               1012                 :                : static void
 2159 akapila@postgresql.o     1013                 :             30 : begin_prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
                               1014                 :                : {
                               1015                 :             30 :     LogicalDecodingContext *ctx = cache->private_data;
                               1016                 :                :     LogicalErrorCallbackState state;
                               1017                 :                :     ErrorContextCallback errcallback;
                               1018                 :                : 
                               1019         [ -  + ]:             30 :     Assert(!ctx->fast_forward);
                               1020                 :                : 
                               1021                 :                :     /* We're only supposed to call this when two-phase commits are supported */
                               1022         [ -  + ]:             30 :     Assert(ctx->twophase);
                               1023                 :                : 
                               1024                 :                :     /* Push callback + info on the error context stack */
                               1025                 :             30 :     state.ctx = ctx;
                               1026                 :             30 :     state.callback_name = "begin_prepare";
                               1027                 :             30 :     state.report_location = txn->first_lsn;
                               1028                 :             30 :     errcallback.callback = output_plugin_error_callback;
  730 peter@eisentraut.org     1029                 :             30 :     errcallback.arg = &state;
 2159 akapila@postgresql.o     1030                 :             30 :     errcallback.previous = error_context_stack;
                               1031                 :             30 :     error_context_stack = &errcallback;
                               1032                 :                : 
                               1033                 :                :     /* set output state */
                               1034                 :             30 :     ctx->accept_writes = true;
                               1035                 :             30 :     ctx->write_xid = txn->xid;
                               1036                 :             30 :     ctx->write_location = txn->first_lsn;
 1662                          1037                 :             30 :     ctx->end_xact = false;
                               1038                 :                : 
                               1039                 :                :     /*
                               1040                 :                :      * If the plugin supports two-phase commits then begin prepare callback is
                               1041                 :                :      * mandatory
                               1042                 :                :      */
 2159                          1043         [ -  + ]:             30 :     if (ctx->callbacks.begin_prepare_cb == NULL)
 2159 akapila@postgresql.o     1044         [ #  # ]:UBC           0 :         ereport(ERROR,
                               1045                 :                :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                               1046                 :                :                  errmsg("logical replication at prepare time requires a %s callback",
                               1047                 :                :                         "begin_prepare_cb")));
                               1048                 :                : 
                               1049                 :                :     /* do the actual work: call callback */
 2159 akapila@postgresql.o     1050                 :CBC          30 :     ctx->callbacks.begin_prepare_cb(ctx, txn);
                               1051                 :                : 
                               1052                 :                :     /* Pop the error context stack */
                               1053                 :             30 :     error_context_stack = errcallback.previous;
                               1054                 :             30 : }
                               1055                 :                : 
                               1056                 :                : static void
                               1057                 :             30 : prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
                               1058                 :                :                    XLogRecPtr prepare_lsn)
                               1059                 :                : {
                               1060                 :             30 :     LogicalDecodingContext *ctx = cache->private_data;
                               1061                 :                :     LogicalErrorCallbackState state;
                               1062                 :                :     ErrorContextCallback errcallback;
                               1063                 :                : 
                               1064         [ -  + ]:             30 :     Assert(!ctx->fast_forward);
                               1065                 :                : 
                               1066                 :                :     /* We're only supposed to call this when two-phase commits are supported */
                               1067         [ -  + ]:             30 :     Assert(ctx->twophase);
                               1068                 :                : 
                               1069                 :                :     /* Push callback + info on the error context stack */
                               1070                 :             30 :     state.ctx = ctx;
                               1071                 :             30 :     state.callback_name = "prepare";
                               1072                 :             30 :     state.report_location = txn->final_lsn; /* beginning of prepare record */
                               1073                 :             30 :     errcallback.callback = output_plugin_error_callback;
  730 peter@eisentraut.org     1074                 :             30 :     errcallback.arg = &state;
 2159 akapila@postgresql.o     1075                 :             30 :     errcallback.previous = error_context_stack;
                               1076                 :             30 :     error_context_stack = &errcallback;
                               1077                 :                : 
                               1078                 :                :     /* set output state */
                               1079                 :             30 :     ctx->accept_writes = true;
                               1080                 :             30 :     ctx->write_xid = txn->xid;
                               1081                 :             30 :     ctx->write_location = txn->end_lsn; /* points to the end of the record */
 1662                          1082                 :             30 :     ctx->end_xact = true;
                               1083                 :                : 
                               1084                 :                :     /*
                               1085                 :                :      * If the plugin supports two-phase commits then prepare callback is
                               1086                 :                :      * mandatory
                               1087                 :                :      */
 2159                          1088         [ -  + ]:             30 :     if (ctx->callbacks.prepare_cb == NULL)
 2159 akapila@postgresql.o     1089         [ #  # ]:UBC           0 :         ereport(ERROR,
                               1090                 :                :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                               1091                 :                :                  errmsg("logical replication at prepare time requires a %s callback",
                               1092                 :                :                         "prepare_cb")));
                               1093                 :                : 
                               1094                 :                :     /* do the actual work: call callback */
 2159 akapila@postgresql.o     1095                 :CBC          30 :     ctx->callbacks.prepare_cb(ctx, txn, prepare_lsn);
                               1096                 :                : 
                               1097                 :                :     /* Pop the error context stack */
                               1098                 :             30 :     error_context_stack = errcallback.previous;
                               1099                 :             30 : }
                               1100                 :                : 
                               1101                 :                : static void
                               1102                 :             36 : commit_prepared_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
                               1103                 :                :                            XLogRecPtr commit_lsn)
                               1104                 :                : {
                               1105                 :             36 :     LogicalDecodingContext *ctx = cache->private_data;
                               1106                 :                :     LogicalErrorCallbackState state;
                               1107                 :                :     ErrorContextCallback errcallback;
                               1108                 :                : 
                               1109         [ -  + ]:             36 :     Assert(!ctx->fast_forward);
                               1110                 :                : 
                               1111                 :                :     /* We're only supposed to call this when two-phase commits are supported */
                               1112         [ -  + ]:             36 :     Assert(ctx->twophase);
                               1113                 :                : 
                               1114                 :                :     /* Push callback + info on the error context stack */
                               1115                 :             36 :     state.ctx = ctx;
                               1116                 :             36 :     state.callback_name = "commit_prepared";
                               1117                 :             36 :     state.report_location = txn->final_lsn; /* beginning of commit record */
                               1118                 :             36 :     errcallback.callback = output_plugin_error_callback;
  730 peter@eisentraut.org     1119                 :             36 :     errcallback.arg = &state;
 2159 akapila@postgresql.o     1120                 :             36 :     errcallback.previous = error_context_stack;
                               1121                 :             36 :     error_context_stack = &errcallback;
                               1122                 :                : 
                               1123                 :                :     /* set output state */
                               1124                 :             36 :     ctx->accept_writes = true;
                               1125                 :             36 :     ctx->write_xid = txn->xid;
                               1126                 :             36 :     ctx->write_location = txn->end_lsn; /* points to the end of the record */
 1662                          1127                 :             36 :     ctx->end_xact = true;
                               1128                 :                : 
                               1129                 :                :     /*
                               1130                 :                :      * If the plugin support two-phase commits then commit prepared callback
                               1131                 :                :      * is mandatory
                               1132                 :                :      */
 2159                          1133         [ -  + ]:             36 :     if (ctx->callbacks.commit_prepared_cb == NULL)
 2159 akapila@postgresql.o     1134         [ #  # ]:UBC           0 :         ereport(ERROR,
                               1135                 :                :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                               1136                 :                :                  errmsg("logical replication at prepare time requires a %s callback",
                               1137                 :                :                         "commit_prepared_cb")));
                               1138                 :                : 
                               1139                 :                :     /* do the actual work: call callback */
 2159 akapila@postgresql.o     1140                 :CBC          36 :     ctx->callbacks.commit_prepared_cb(ctx, txn, commit_lsn);
                               1141                 :                : 
                               1142                 :                :     /* Pop the error context stack */
                               1143                 :             36 :     error_context_stack = errcallback.previous;
                               1144                 :             36 : }
                               1145                 :                : 
                               1146                 :                : static void
                               1147                 :             10 : rollback_prepared_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
                               1148                 :                :                              XLogRecPtr prepare_end_lsn,
                               1149                 :                :                              TimestampTz prepare_time)
                               1150                 :                : {
                               1151                 :             10 :     LogicalDecodingContext *ctx = cache->private_data;
                               1152                 :                :     LogicalErrorCallbackState state;
                               1153                 :                :     ErrorContextCallback errcallback;
                               1154                 :                : 
                               1155         [ -  + ]:             10 :     Assert(!ctx->fast_forward);
                               1156                 :                : 
                               1157                 :                :     /* We're only supposed to call this when two-phase commits are supported */
                               1158         [ -  + ]:             10 :     Assert(ctx->twophase);
                               1159                 :                : 
                               1160                 :                :     /* Push callback + info on the error context stack */
                               1161                 :             10 :     state.ctx = ctx;
                               1162                 :             10 :     state.callback_name = "rollback_prepared";
                               1163                 :             10 :     state.report_location = txn->final_lsn; /* beginning of commit record */
                               1164                 :             10 :     errcallback.callback = output_plugin_error_callback;
  730 peter@eisentraut.org     1165                 :             10 :     errcallback.arg = &state;
 2159 akapila@postgresql.o     1166                 :             10 :     errcallback.previous = error_context_stack;
                               1167                 :             10 :     error_context_stack = &errcallback;
                               1168                 :                : 
                               1169                 :                :     /* set output state */
                               1170                 :             10 :     ctx->accept_writes = true;
                               1171                 :             10 :     ctx->write_xid = txn->xid;
                               1172                 :             10 :     ctx->write_location = txn->end_lsn; /* points to the end of the record */
 1662                          1173                 :             10 :     ctx->end_xact = true;
                               1174                 :                : 
                               1175                 :                :     /*
                               1176                 :                :      * If the plugin support two-phase commits then rollback prepared callback
                               1177                 :                :      * is mandatory
                               1178                 :                :      */
 2159                          1179         [ -  + ]:             10 :     if (ctx->callbacks.rollback_prepared_cb == NULL)
 2159 akapila@postgresql.o     1180         [ #  # ]:UBC           0 :         ereport(ERROR,
                               1181                 :                :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                               1182                 :                :                  errmsg("logical replication at prepare time requires a %s callback",
                               1183                 :                :                         "rollback_prepared_cb")));
                               1184                 :                : 
                               1185                 :                :     /* do the actual work: call callback */
 2159 akapila@postgresql.o     1186                 :CBC          10 :     ctx->callbacks.rollback_prepared_cb(ctx, txn, prepare_end_lsn,
                               1187                 :                :                                         prepare_time);
                               1188                 :                : 
                               1189                 :                :     /* Pop the error context stack */
                               1190                 :             10 :     error_context_stack = errcallback.previous;
                               1191                 :             10 : }
                               1192                 :                : 
                               1193                 :                : static void
 4653 rhaas@postgresql.org     1194                 :         179486 : change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
                               1195                 :                :                   Relation relation, ReorderBufferChange *change)
                               1196                 :                : {
                               1197                 :         179486 :     LogicalDecodingContext *ctx = cache->private_data;
                               1198                 :                :     LogicalErrorCallbackState state;
                               1199                 :                :     ErrorContextCallback errcallback;
                               1200                 :                : 
 3237 simon@2ndQuadrant.co     1201         [ -  + ]:         179486 :     Assert(!ctx->fast_forward);
                               1202                 :                : 
                               1203                 :                :     /* Push callback + info on the error context stack */
 4653 rhaas@postgresql.org     1204                 :         179486 :     state.ctx = ctx;
                               1205                 :         179486 :     state.callback_name = "change";
                               1206                 :         179486 :     state.report_location = change->lsn;
                               1207                 :         179486 :     errcallback.callback = output_plugin_error_callback;
  730 peter@eisentraut.org     1208                 :         179486 :     errcallback.arg = &state;
 4653 rhaas@postgresql.org     1209                 :         179486 :     errcallback.previous = error_context_stack;
                               1210                 :         179486 :     error_context_stack = &errcallback;
                               1211                 :                : 
                               1212                 :                :     /* set output state */
                               1213                 :         179486 :     ctx->accept_writes = true;
                               1214                 :         179486 :     ctx->write_xid = txn->xid;
                               1215                 :                : 
                               1216                 :                :     /*
                               1217                 :                :      * Report this change's lsn so replies from clients can give an up-to-date
                               1218                 :                :      * answer. This won't ever be enough (and shouldn't be!) to confirm
                               1219                 :                :      * receipt of this transaction, but it might allow another transaction's
                               1220                 :                :      * commit to be confirmed with one message.
                               1221                 :                :      */
                               1222                 :         179486 :     ctx->write_location = change->lsn;
                               1223                 :                : 
 1662 akapila@postgresql.o     1224                 :         179486 :     ctx->end_xact = false;
                               1225                 :                : 
 4653 rhaas@postgresql.org     1226                 :         179486 :     ctx->callbacks.change_cb(ctx, txn, relation, change);
                               1227                 :                : 
                               1228                 :                :     /* Pop the error context stack */
                               1229                 :         179479 :     error_context_stack = errcallback.previous;
                               1230                 :         179479 : }
                               1231                 :                : 
                               1232                 :                : static void
 3157 peter_e@gmx.net          1233                 :             26 : truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
                               1234                 :                :                     int nrelations, Relation relations[], ReorderBufferChange *change)
                               1235                 :                : {
                               1236                 :             26 :     LogicalDecodingContext *ctx = cache->private_data;
                               1237                 :                :     LogicalErrorCallbackState state;
                               1238                 :                :     ErrorContextCallback errcallback;
                               1239                 :                : 
                               1240         [ -  + ]:             26 :     Assert(!ctx->fast_forward);
                               1241                 :                : 
                               1242         [ -  + ]:             26 :     if (!ctx->callbacks.truncate_cb)
 3157 peter_e@gmx.net          1243                 :UBC           0 :         return;
                               1244                 :                : 
                               1245                 :                :     /* Push callback + info on the error context stack */
 3157 peter_e@gmx.net          1246                 :CBC          26 :     state.ctx = ctx;
                               1247                 :             26 :     state.callback_name = "truncate";
                               1248                 :             26 :     state.report_location = change->lsn;
                               1249                 :             26 :     errcallback.callback = output_plugin_error_callback;
  730 peter@eisentraut.org     1250                 :             26 :     errcallback.arg = &state;
 3157 peter_e@gmx.net          1251                 :             26 :     errcallback.previous = error_context_stack;
                               1252                 :             26 :     error_context_stack = &errcallback;
                               1253                 :                : 
                               1254                 :                :     /* set output state */
                               1255                 :             26 :     ctx->accept_writes = true;
                               1256                 :             26 :     ctx->write_xid = txn->xid;
                               1257                 :                : 
                               1258                 :                :     /*
                               1259                 :                :      * Report this change's lsn so replies from clients can give an up-to-date
                               1260                 :                :      * answer. This won't ever be enough (and shouldn't be!) to confirm
                               1261                 :                :      * receipt of this transaction, but it might allow another transaction's
                               1262                 :                :      * commit to be confirmed with one message.
                               1263                 :                :      */
                               1264                 :             26 :     ctx->write_location = change->lsn;
                               1265                 :                : 
 1662 akapila@postgresql.o     1266                 :             26 :     ctx->end_xact = false;
                               1267                 :                : 
 3157 peter_e@gmx.net          1268                 :             26 :     ctx->callbacks.truncate_cb(ctx, txn, nrelations, relations, change);
                               1269                 :                : 
                               1270                 :                :     /* Pop the error context stack */
                               1271                 :             26 :     error_context_stack = errcallback.previous;
                               1272                 :                : }
                               1273                 :                : 
                               1274                 :                : bool
 2069 akapila@postgresql.o     1275                 :            170 : filter_prepare_cb_wrapper(LogicalDecodingContext *ctx, TransactionId xid,
                               1276                 :                :                           const char *gid)
                               1277                 :                : {
                               1278                 :                :     LogicalErrorCallbackState state;
                               1279                 :                :     ErrorContextCallback errcallback;
                               1280                 :                :     bool        ret;
                               1281                 :                : 
 2159                          1282         [ -  + ]:            170 :     Assert(!ctx->fast_forward);
                               1283                 :                : 
                               1284                 :                :     /* Push callback + info on the error context stack */
                               1285                 :            170 :     state.ctx = ctx;
                               1286                 :            170 :     state.callback_name = "filter_prepare";
                               1287                 :            170 :     state.report_location = InvalidXLogRecPtr;
                               1288                 :            170 :     errcallback.callback = output_plugin_error_callback;
  730 peter@eisentraut.org     1289                 :            170 :     errcallback.arg = &state;
 2159 akapila@postgresql.o     1290                 :            170 :     errcallback.previous = error_context_stack;
                               1291                 :            170 :     error_context_stack = &errcallback;
                               1292                 :                : 
                               1293                 :                :     /* set output state */
                               1294                 :            170 :     ctx->accept_writes = false;
 1662                          1295                 :            170 :     ctx->end_xact = false;
                               1296                 :                : 
                               1297                 :                :     /* do the actual work: call callback */
 2069                          1298                 :            170 :     ret = ctx->callbacks.filter_prepare_cb(ctx, xid, gid);
                               1299                 :                : 
                               1300                 :                :     /* Pop the error context stack */
 2159                          1301                 :            170 :     error_context_stack = errcallback.previous;
                               1302                 :                : 
                               1303                 :            170 :     return ret;
                               1304                 :                : }
                               1305                 :                : 
                               1306                 :                : bool
  304 msawada@postgresql.o     1307                 :        1596993 : filter_by_origin_cb_wrapper(LogicalDecodingContext *ctx, ReplOriginId origin_id)
                               1308                 :                : {
                               1309                 :                :     LogicalErrorCallbackState state;
                               1310                 :                :     ErrorContextCallback errcallback;
                               1311                 :                :     bool        ret;
                               1312                 :                : 
 3237 simon@2ndQuadrant.co     1313         [ -  + ]:        1596993 :     Assert(!ctx->fast_forward);
                               1314                 :                : 
                               1315                 :                :     /* Push callback + info on the error context stack */
 4231 andres@anarazel.de       1316                 :        1596993 :     state.ctx = ctx;
 3998 rhaas@postgresql.org     1317                 :        1596993 :     state.callback_name = "filter_by_origin";
 4231 andres@anarazel.de       1318                 :        1596993 :     state.report_location = InvalidXLogRecPtr;
                               1319                 :        1596993 :     errcallback.callback = output_plugin_error_callback;
  730 peter@eisentraut.org     1320                 :        1596993 :     errcallback.arg = &state;
 4231 andres@anarazel.de       1321                 :        1596993 :     errcallback.previous = error_context_stack;
                               1322                 :        1596993 :     error_context_stack = &errcallback;
                               1323                 :                : 
                               1324                 :                :     /* set output state */
                               1325                 :        1596993 :     ctx->accept_writes = false;
 1662 akapila@postgresql.o     1326                 :        1596993 :     ctx->end_xact = false;
                               1327                 :                : 
                               1328                 :                :     /* do the actual work: call callback */
 4231 andres@anarazel.de       1329                 :        1596993 :     ret = ctx->callbacks.filter_by_origin_cb(ctx, origin_id);
                               1330                 :                : 
                               1331                 :                :     /* Pop the error context stack */
                               1332                 :        1596993 :     error_context_stack = errcallback.previous;
                               1333                 :                : 
                               1334                 :        1596993 :     return ret;
                               1335                 :                : }
                               1336                 :                : 
                               1337                 :                : static void
 3888 simon@2ndQuadrant.co     1338                 :             18 : message_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
                               1339                 :                :                    TransactionId xid, XLogRecPtr message_lsn, bool transactional,
                               1340                 :                :                    const char *prefix, Size message_size, const char *message)
                               1341                 :                : {
                               1342                 :             18 :     LogicalDecodingContext *ctx = cache->private_data;
                               1343                 :                :     LogicalErrorCallbackState state;
                               1344                 :                :     ErrorContextCallback errcallback;
                               1345                 :                : 
 3237                          1346         [ -  + ]:             18 :     Assert(!ctx->fast_forward);
                               1347                 :                : 
 3888                          1348         [ -  + ]:             18 :     if (ctx->callbacks.message_cb == NULL)
 3888 simon@2ndQuadrant.co     1349                 :UBC           0 :         return;
                               1350                 :                : 
                               1351                 :                :     /* Push callback + info on the error context stack */
 3888 simon@2ndQuadrant.co     1352                 :CBC          18 :     state.ctx = ctx;
                               1353                 :             18 :     state.callback_name = "message";
                               1354                 :             18 :     state.report_location = message_lsn;
                               1355                 :             18 :     errcallback.callback = output_plugin_error_callback;
  730 peter@eisentraut.org     1356                 :             18 :     errcallback.arg = &state;
 3888 simon@2ndQuadrant.co     1357                 :             18 :     errcallback.previous = error_context_stack;
                               1358                 :             18 :     error_context_stack = &errcallback;
                               1359                 :                : 
                               1360                 :                :     /* set output state */
                               1361                 :             18 :     ctx->accept_writes = true;
                               1362         [ +  + ]:             18 :     ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
                               1363                 :             18 :     ctx->write_location = message_lsn;
 1662 akapila@postgresql.o     1364                 :             18 :     ctx->end_xact = false;
                               1365                 :                : 
                               1366                 :                :     /* do the actual work: call callback */
    5 msawada@postgresql.o     1367                 :GNC          18 :     ctx->callbacks.message_cb(ctx, txn, xid, message_lsn, transactional, prefix,
                               1368                 :                :                               message_size, message);
                               1369                 :                : 
                               1370                 :                :     /* Pop the error context stack */
 3888 simon@2ndQuadrant.co     1371                 :CBC          18 :     error_context_stack = errcallback.previous;
                               1372                 :                : }
                               1373                 :                : 
                               1374                 :                : static void
 2314 akapila@postgresql.o     1375                 :            696 : stream_start_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
                               1376                 :                :                         XLogRecPtr first_lsn)
                               1377                 :                : {
                               1378                 :            696 :     LogicalDecodingContext *ctx = cache->private_data;
                               1379                 :                :     LogicalErrorCallbackState state;
                               1380                 :                :     ErrorContextCallback errcallback;
                               1381                 :                : 
                               1382         [ -  + ]:            696 :     Assert(!ctx->fast_forward);
                               1383                 :                : 
                               1384                 :                :     /* We're only supposed to call this when streaming is supported. */
                               1385         [ -  + ]:            696 :     Assert(ctx->streaming);
                               1386                 :                : 
                               1387                 :                :     /* Push callback + info on the error context stack */
                               1388                 :            696 :     state.ctx = ctx;
                               1389                 :            696 :     state.callback_name = "stream_start";
                               1390                 :            696 :     state.report_location = first_lsn;
                               1391                 :            696 :     errcallback.callback = output_plugin_error_callback;
  730 peter@eisentraut.org     1392                 :            696 :     errcallback.arg = &state;
 2314 akapila@postgresql.o     1393                 :            696 :     errcallback.previous = error_context_stack;
                               1394                 :            696 :     error_context_stack = &errcallback;
                               1395                 :                : 
                               1396                 :                :     /* set output state */
                               1397                 :            696 :     ctx->accept_writes = true;
                               1398                 :            696 :     ctx->write_xid = txn->xid;
                               1399                 :                : 
                               1400                 :                :     /*
                               1401                 :                :      * Report this message's lsn so replies from clients can give an
                               1402                 :                :      * up-to-date answer. This won't ever be enough (and shouldn't be!) to
                               1403                 :                :      * confirm receipt of this transaction, but it might allow another
                               1404                 :                :      * transaction's commit to be confirmed with one message.
                               1405                 :                :      */
                               1406                 :            696 :     ctx->write_location = first_lsn;
                               1407                 :                : 
 1662                          1408                 :            696 :     ctx->end_xact = false;
                               1409                 :                : 
                               1410                 :                :     /* in streaming mode, stream_start_cb is required */
 2314                          1411         [ -  + ]:            696 :     if (ctx->callbacks.stream_start_cb == NULL)
 2314 akapila@postgresql.o     1412         [ #  # ]:UBC           0 :         ereport(ERROR,
                               1413                 :                :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                               1414                 :                :                  errmsg("logical streaming requires a %s callback",
                               1415                 :                :                         "stream_start_cb")));
                               1416                 :                : 
 2314 akapila@postgresql.o     1417                 :CBC         696 :     ctx->callbacks.stream_start_cb(ctx, txn);
                               1418                 :                : 
                               1419                 :                :     /* Pop the error context stack */
                               1420                 :            696 :     error_context_stack = errcallback.previous;
                               1421                 :            696 : }
                               1422                 :                : 
                               1423                 :                : static void
                               1424                 :            695 : stream_stop_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
                               1425                 :                :                        XLogRecPtr last_lsn)
                               1426                 :                : {
                               1427                 :            695 :     LogicalDecodingContext *ctx = cache->private_data;
                               1428                 :                :     LogicalErrorCallbackState state;
                               1429                 :                :     ErrorContextCallback errcallback;
                               1430                 :                : 
                               1431         [ -  + ]:            695 :     Assert(!ctx->fast_forward);
                               1432                 :                : 
                               1433                 :                :     /* We're only supposed to call this when streaming is supported. */
                               1434         [ -  + ]:            695 :     Assert(ctx->streaming);
                               1435                 :                : 
                               1436                 :                :     /* Push callback + info on the error context stack */
                               1437                 :            695 :     state.ctx = ctx;
                               1438                 :            695 :     state.callback_name = "stream_stop";
                               1439                 :            695 :     state.report_location = last_lsn;
                               1440                 :            695 :     errcallback.callback = output_plugin_error_callback;
  730 peter@eisentraut.org     1441                 :            695 :     errcallback.arg = &state;
 2314 akapila@postgresql.o     1442                 :            695 :     errcallback.previous = error_context_stack;
                               1443                 :            695 :     error_context_stack = &errcallback;
                               1444                 :                : 
                               1445                 :                :     /* set output state */
                               1446                 :            695 :     ctx->accept_writes = true;
                               1447                 :            695 :     ctx->write_xid = txn->xid;
                               1448                 :                : 
                               1449                 :                :     /*
                               1450                 :                :      * Report this message's lsn so replies from clients can give an
                               1451                 :                :      * up-to-date answer. This won't ever be enough (and shouldn't be!) to
                               1452                 :                :      * confirm receipt of this transaction, but it might allow another
                               1453                 :                :      * transaction's commit to be confirmed with one message.
                               1454                 :                :      */
                               1455                 :            695 :     ctx->write_location = last_lsn;
                               1456                 :                : 
 1662                          1457                 :            695 :     ctx->end_xact = false;
                               1458                 :                : 
                               1459                 :                :     /* in streaming mode, stream_stop_cb is required */
 2314                          1460         [ -  + ]:            695 :     if (ctx->callbacks.stream_stop_cb == NULL)
 2314 akapila@postgresql.o     1461         [ #  # ]:UBC           0 :         ereport(ERROR,
                               1462                 :                :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                               1463                 :                :                  errmsg("logical streaming requires a %s callback",
                               1464                 :                :                         "stream_stop_cb")));
                               1465                 :                : 
 2314 akapila@postgresql.o     1466                 :CBC         695 :     ctx->callbacks.stream_stop_cb(ctx, txn);
                               1467                 :                : 
                               1468                 :                :     /* Pop the error context stack */
                               1469                 :            695 :     error_context_stack = errcallback.previous;
                               1470                 :            695 : }
                               1471                 :                : 
                               1472                 :                : static void
                               1473                 :             31 : stream_abort_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
                               1474                 :                :                         XLogRecPtr abort_lsn)
                               1475                 :                : {
                               1476                 :             31 :     LogicalDecodingContext *ctx = cache->private_data;
                               1477                 :                :     LogicalErrorCallbackState state;
                               1478                 :                :     ErrorContextCallback errcallback;
                               1479                 :                : 
                               1480         [ -  + ]:             31 :     Assert(!ctx->fast_forward);
                               1481                 :                : 
                               1482                 :                :     /* We're only supposed to call this when streaming is supported. */
                               1483         [ -  + ]:             31 :     Assert(ctx->streaming);
                               1484                 :                : 
                               1485                 :                :     /* Push callback + info on the error context stack */
                               1486                 :             31 :     state.ctx = ctx;
                               1487                 :             31 :     state.callback_name = "stream_abort";
                               1488                 :             31 :     state.report_location = abort_lsn;
                               1489                 :             31 :     errcallback.callback = output_plugin_error_callback;
  730 peter@eisentraut.org     1490                 :             31 :     errcallback.arg = &state;
 2314 akapila@postgresql.o     1491                 :             31 :     errcallback.previous = error_context_stack;
                               1492                 :             31 :     error_context_stack = &errcallback;
                               1493                 :                : 
                               1494                 :                :     /* set output state */
                               1495                 :             31 :     ctx->accept_writes = true;
                               1496                 :             31 :     ctx->write_xid = txn->xid;
                               1497                 :             31 :     ctx->write_location = abort_lsn;
 1662                          1498                 :             31 :     ctx->end_xact = true;
                               1499                 :                : 
                               1500                 :                :     /* in streaming mode, stream_abort_cb is required */
 2314                          1501         [ -  + ]:             31 :     if (ctx->callbacks.stream_abort_cb == NULL)
 2314 akapila@postgresql.o     1502         [ #  # ]:UBC           0 :         ereport(ERROR,
                               1503                 :                :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                               1504                 :                :                  errmsg("logical streaming requires a %s callback",
                               1505                 :                :                         "stream_abort_cb")));
                               1506                 :                : 
 2314 akapila@postgresql.o     1507                 :CBC          31 :     ctx->callbacks.stream_abort_cb(ctx, txn, abort_lsn);
                               1508                 :                : 
                               1509                 :                :     /* Pop the error context stack */
                               1510                 :             31 :     error_context_stack = errcallback.previous;
                               1511                 :             31 : }
                               1512                 :                : 
                               1513                 :                : static void
 2159                          1514                 :             17 : stream_prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
                               1515                 :                :                           XLogRecPtr prepare_lsn)
                               1516                 :                : {
                               1517                 :             17 :     LogicalDecodingContext *ctx = cache->private_data;
                               1518                 :                :     LogicalErrorCallbackState state;
                               1519                 :                :     ErrorContextCallback errcallback;
                               1520                 :                : 
                               1521         [ -  + ]:             17 :     Assert(!ctx->fast_forward);
                               1522                 :                : 
                               1523                 :                :     /*
                               1524                 :                :      * We're only supposed to call this when streaming and two-phase commits
                               1525                 :                :      * are supported.
                               1526                 :                :      */
                               1527         [ -  + ]:             17 :     Assert(ctx->streaming);
                               1528         [ -  + ]:             17 :     Assert(ctx->twophase);
                               1529                 :                : 
                               1530                 :                :     /* Push callback + info on the error context stack */
                               1531                 :             17 :     state.ctx = ctx;
                               1532                 :             17 :     state.callback_name = "stream_prepare";
                               1533                 :             17 :     state.report_location = txn->final_lsn;
                               1534                 :             17 :     errcallback.callback = output_plugin_error_callback;
  730 peter@eisentraut.org     1535                 :             17 :     errcallback.arg = &state;
 2159 akapila@postgresql.o     1536                 :             17 :     errcallback.previous = error_context_stack;
                               1537                 :             17 :     error_context_stack = &errcallback;
                               1538                 :                : 
                               1539                 :                :     /* set output state */
                               1540                 :             17 :     ctx->accept_writes = true;
                               1541                 :             17 :     ctx->write_xid = txn->xid;
                               1542                 :             17 :     ctx->write_location = txn->end_lsn;
 1662                          1543                 :             17 :     ctx->end_xact = true;
                               1544                 :                : 
                               1545                 :                :     /* in streaming mode with two-phase commits, stream_prepare_cb is required */
 2159                          1546         [ -  + ]:             17 :     if (ctx->callbacks.stream_prepare_cb == NULL)
 2159 akapila@postgresql.o     1547         [ #  # ]:UBC           0 :         ereport(ERROR,
                               1548                 :                :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                               1549                 :                :                  errmsg("logical streaming at prepare time requires a %s callback",
                               1550                 :                :                         "stream_prepare_cb")));
                               1551                 :                : 
 2159 akapila@postgresql.o     1552                 :CBC          17 :     ctx->callbacks.stream_prepare_cb(ctx, txn, prepare_lsn);
                               1553                 :                : 
                               1554                 :                :     /* Pop the error context stack */
                               1555                 :             17 :     error_context_stack = errcallback.previous;
                               1556                 :             17 : }
                               1557                 :                : 
                               1558                 :                : static void
 2314                          1559                 :             51 : stream_commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
                               1560                 :                :                          XLogRecPtr commit_lsn)
                               1561                 :                : {
                               1562                 :             51 :     LogicalDecodingContext *ctx = cache->private_data;
                               1563                 :                :     LogicalErrorCallbackState state;
                               1564                 :                :     ErrorContextCallback errcallback;
                               1565                 :                : 
                               1566         [ -  + ]:             51 :     Assert(!ctx->fast_forward);
                               1567                 :                : 
                               1568                 :                :     /* We're only supposed to call this when streaming is supported. */
                               1569         [ -  + ]:             51 :     Assert(ctx->streaming);
                               1570                 :                : 
                               1571                 :                :     /* Push callback + info on the error context stack */
                               1572                 :             51 :     state.ctx = ctx;
                               1573                 :             51 :     state.callback_name = "stream_commit";
                               1574                 :             51 :     state.report_location = txn->final_lsn;
                               1575                 :             51 :     errcallback.callback = output_plugin_error_callback;
  730 peter@eisentraut.org     1576                 :             51 :     errcallback.arg = &state;
 2314 akapila@postgresql.o     1577                 :             51 :     errcallback.previous = error_context_stack;
                               1578                 :             51 :     error_context_stack = &errcallback;
                               1579                 :                : 
                               1580                 :                :     /* set output state */
                               1581                 :             51 :     ctx->accept_writes = true;
                               1582                 :             51 :     ctx->write_xid = txn->xid;
                               1583                 :             51 :     ctx->write_location = txn->end_lsn;
 1662                          1584                 :             51 :     ctx->end_xact = true;
                               1585                 :                : 
                               1586                 :                :     /* in streaming mode, stream_commit_cb is required */
 2314                          1587         [ -  + ]:             51 :     if (ctx->callbacks.stream_commit_cb == NULL)
 2314 akapila@postgresql.o     1588         [ #  # ]:UBC           0 :         ereport(ERROR,
                               1589                 :                :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                               1590                 :                :                  errmsg("logical streaming requires a %s callback",
                               1591                 :                :                         "stream_commit_cb")));
                               1592                 :                : 
 2314 akapila@postgresql.o     1593                 :CBC          51 :     ctx->callbacks.stream_commit_cb(ctx, txn, commit_lsn);
                               1594                 :                : 
                               1595                 :                :     /* Pop the error context stack */
                               1596                 :             51 :     error_context_stack = errcallback.previous;
                               1597                 :             51 : }
                               1598                 :                : 
                               1599                 :                : static void
                               1600                 :         175914 : stream_change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
                               1601                 :                :                          Relation relation, ReorderBufferChange *change)
                               1602                 :                : {
                               1603                 :         175914 :     LogicalDecodingContext *ctx = cache->private_data;
                               1604                 :                :     LogicalErrorCallbackState state;
                               1605                 :                :     ErrorContextCallback errcallback;
                               1606                 :                : 
                               1607         [ -  + ]:         175914 :     Assert(!ctx->fast_forward);
                               1608                 :                : 
                               1609                 :                :     /* We're only supposed to call this when streaming is supported. */
                               1610         [ -  + ]:         175914 :     Assert(ctx->streaming);
                               1611                 :                : 
                               1612                 :                :     /* Push callback + info on the error context stack */
                               1613                 :         175914 :     state.ctx = ctx;
                               1614                 :         175914 :     state.callback_name = "stream_change";
                               1615                 :         175914 :     state.report_location = change->lsn;
                               1616                 :         175914 :     errcallback.callback = output_plugin_error_callback;
  730 peter@eisentraut.org     1617                 :         175914 :     errcallback.arg = &state;
 2314 akapila@postgresql.o     1618                 :         175914 :     errcallback.previous = error_context_stack;
                               1619                 :         175914 :     error_context_stack = &errcallback;
                               1620                 :                : 
                               1621                 :                :     /* set output state */
                               1622                 :         175914 :     ctx->accept_writes = true;
                               1623                 :         175914 :     ctx->write_xid = txn->xid;
                               1624                 :                : 
                               1625                 :                :     /*
                               1626                 :                :      * Report this change's lsn so replies from clients can give an up-to-date
                               1627                 :                :      * answer. This won't ever be enough (and shouldn't be!) to confirm
                               1628                 :                :      * receipt of this transaction, but it might allow another transaction's
                               1629                 :                :      * commit to be confirmed with one message.
                               1630                 :                :      */
                               1631                 :         175914 :     ctx->write_location = change->lsn;
                               1632                 :                : 
 1662                          1633                 :         175914 :     ctx->end_xact = false;
                               1634                 :                : 
                               1635                 :                :     /* in streaming mode, stream_change_cb is required */
 2314                          1636         [ -  + ]:         175914 :     if (ctx->callbacks.stream_change_cb == NULL)
 2314 akapila@postgresql.o     1637         [ #  # ]:UBC           0 :         ereport(ERROR,
                               1638                 :                :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                               1639                 :                :                  errmsg("logical streaming requires a %s callback",
                               1640                 :                :                         "stream_change_cb")));
                               1641                 :                : 
 2314 akapila@postgresql.o     1642                 :CBC      175914 :     ctx->callbacks.stream_change_cb(ctx, txn, relation, change);
                               1643                 :                : 
                               1644                 :                :     /* Pop the error context stack */
                               1645                 :         175913 :     error_context_stack = errcallback.previous;
                               1646                 :         175913 : }
                               1647                 :                : 
                               1648                 :                : static void
                               1649                 :              4 : stream_message_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
                               1650                 :                :                           TransactionId xid, XLogRecPtr message_lsn, bool transactional,
                               1651                 :                :                           const char *prefix, Size message_size, const char *message)
                               1652                 :                : {
                               1653                 :              4 :     LogicalDecodingContext *ctx = cache->private_data;
                               1654                 :                :     LogicalErrorCallbackState state;
                               1655                 :                :     ErrorContextCallback errcallback;
                               1656                 :                : 
                               1657         [ -  + ]:              4 :     Assert(!ctx->fast_forward);
                               1658                 :                : 
                               1659                 :                :     /* We're only supposed to call this when streaming is supported. */
                               1660         [ -  + ]:              4 :     Assert(ctx->streaming);
                               1661                 :                : 
                               1662                 :                :     /* this callback is optional */
                               1663         [ -  + ]:              4 :     if (ctx->callbacks.stream_message_cb == NULL)
 2314 akapila@postgresql.o     1664                 :UBC           0 :         return;
                               1665                 :                : 
                               1666                 :                :     /* Push callback + info on the error context stack */
 2314 akapila@postgresql.o     1667                 :CBC           4 :     state.ctx = ctx;
                               1668                 :              4 :     state.callback_name = "stream_message";
                               1669                 :              4 :     state.report_location = message_lsn;
                               1670                 :              4 :     errcallback.callback = output_plugin_error_callback;
  730 peter@eisentraut.org     1671                 :              4 :     errcallback.arg = &state;
 2314 akapila@postgresql.o     1672                 :              4 :     errcallback.previous = error_context_stack;
                               1673                 :              4 :     error_context_stack = &errcallback;
                               1674                 :                : 
                               1675                 :                :     /* set output state */
                               1676                 :              4 :     ctx->accept_writes = true;
                               1677         [ +  - ]:              4 :     ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
                               1678                 :              4 :     ctx->write_location = message_lsn;
 1662                          1679                 :              4 :     ctx->end_xact = false;
                               1680                 :                : 
                               1681                 :                :     /* do the actual work: call callback */
    5 msawada@postgresql.o     1682                 :GNC           4 :     ctx->callbacks.stream_message_cb(ctx, txn, xid, message_lsn, transactional, prefix,
                               1683                 :                :                                      message_size, message);
                               1684                 :                : 
                               1685                 :                :     /* Pop the error context stack */
 2314 akapila@postgresql.o     1686                 :CBC           4 :     error_context_stack = errcallback.previous;
                               1687                 :                : }
                               1688                 :                : 
                               1689                 :                : static void
 2314 akapila@postgresql.o     1690                 :UBC           0 : stream_truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
                               1691                 :                :                            int nrelations, Relation relations[],
                               1692                 :                :                            ReorderBufferChange *change)
                               1693                 :                : {
                               1694                 :              0 :     LogicalDecodingContext *ctx = cache->private_data;
                               1695                 :                :     LogicalErrorCallbackState state;
                               1696                 :                :     ErrorContextCallback errcallback;
                               1697                 :                : 
                               1698         [ #  # ]:              0 :     Assert(!ctx->fast_forward);
                               1699                 :                : 
                               1700                 :                :     /* We're only supposed to call this when streaming is supported. */
                               1701         [ #  # ]:              0 :     Assert(ctx->streaming);
                               1702                 :                : 
                               1703                 :                :     /* this callback is optional */
                               1704         [ #  # ]:              0 :     if (!ctx->callbacks.stream_truncate_cb)
                               1705                 :              0 :         return;
                               1706                 :                : 
                               1707                 :                :     /* Push callback + info on the error context stack */
                               1708                 :              0 :     state.ctx = ctx;
                               1709                 :              0 :     state.callback_name = "stream_truncate";
                               1710                 :              0 :     state.report_location = change->lsn;
                               1711                 :              0 :     errcallback.callback = output_plugin_error_callback;
  730 peter@eisentraut.org     1712                 :              0 :     errcallback.arg = &state;
 2314 akapila@postgresql.o     1713                 :              0 :     errcallback.previous = error_context_stack;
                               1714                 :              0 :     error_context_stack = &errcallback;
                               1715                 :                : 
                               1716                 :                :     /* set output state */
                               1717                 :              0 :     ctx->accept_writes = true;
                               1718                 :              0 :     ctx->write_xid = txn->xid;
                               1719                 :                : 
                               1720                 :                :     /*
                               1721                 :                :      * Report this change's lsn so replies from clients can give an up-to-date
                               1722                 :                :      * answer. This won't ever be enough (and shouldn't be!) to confirm
                               1723                 :                :      * receipt of this transaction, but it might allow another transaction's
                               1724                 :                :      * commit to be confirmed with one message.
                               1725                 :                :      */
                               1726                 :              0 :     ctx->write_location = change->lsn;
                               1727                 :                : 
 1662                          1728                 :              0 :     ctx->end_xact = false;
                               1729                 :                : 
 2314                          1730                 :              0 :     ctx->callbacks.stream_truncate_cb(ctx, txn, nrelations, relations, change);
                               1731                 :                : 
                               1732                 :                :     /* Pop the error context stack */
                               1733                 :              0 :     error_context_stack = errcallback.previous;
                               1734                 :                : }
                               1735                 :                : 
                               1736                 :                : static void
 1389 akapila@postgresql.o     1737                 :CBC        3337 : update_progress_txn_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
                               1738                 :                :                                XLogRecPtr lsn)
                               1739                 :                : {
                               1740                 :           3337 :     LogicalDecodingContext *ctx = cache->private_data;
                               1741                 :                :     LogicalErrorCallbackState state;
                               1742                 :                :     ErrorContextCallback errcallback;
                               1743                 :                : 
                               1744         [ -  + ]:           3337 :     Assert(!ctx->fast_forward);
                               1745                 :                : 
                               1746                 :                :     /* Push callback + info on the error context stack */
                               1747                 :           3337 :     state.ctx = ctx;
                               1748                 :           3337 :     state.callback_name = "update_progress_txn";
                               1749                 :           3337 :     state.report_location = lsn;
                               1750                 :           3337 :     errcallback.callback = output_plugin_error_callback;
  730 peter@eisentraut.org     1751                 :           3337 :     errcallback.arg = &state;
 1389 akapila@postgresql.o     1752                 :           3337 :     errcallback.previous = error_context_stack;
                               1753                 :           3337 :     error_context_stack = &errcallback;
                               1754                 :                : 
                               1755                 :                :     /* set output state */
                               1756                 :           3337 :     ctx->accept_writes = false;
                               1757                 :           3337 :     ctx->write_xid = txn->xid;
                               1758                 :                : 
                               1759                 :                :     /*
                               1760                 :                :      * Report this change's lsn so replies from clients can give an up-to-date
                               1761                 :                :      * answer. This won't ever be enough (and shouldn't be!) to confirm
                               1762                 :                :      * receipt of this transaction, but it might allow another transaction's
                               1763                 :                :      * commit to be confirmed with one message.
                               1764                 :                :      */
                               1765                 :           3337 :     ctx->write_location = lsn;
                               1766                 :                : 
                               1767                 :           3337 :     ctx->end_xact = false;
                               1768                 :                : 
                               1769                 :           3337 :     OutputPluginUpdateProgress(ctx, false);
                               1770                 :                : 
                               1771                 :                :     /* Pop the error context stack */
                               1772                 :           3337 :     error_context_stack = errcallback.previous;
                               1773                 :           3337 : }
                               1774                 :                : 
                               1775                 :                : /*
                               1776                 :                :  * Set the required catalog xmin horizon for historic snapshots in the current
                               1777                 :                :  * replication slot.
                               1778                 :                :  *
                               1779                 :                :  * Note that in the most cases, we won't be able to immediately use the xmin
                               1780                 :                :  * to increase the xmin horizon: we need to wait till the client has confirmed
                               1781                 :                :  * receiving current_lsn with LogicalConfirmReceivedLocation().
                               1782                 :                :  */
                               1783                 :                : void
 4653 rhaas@postgresql.org     1784                 :            695 : LogicalIncreaseXminForSlot(XLogRecPtr current_lsn, TransactionId xmin)
                               1785                 :                : {
 4589 bruce@momjian.us         1786                 :            695 :     bool        updated_xmin = false;
                               1787                 :                :     ReplicationSlot *slot;
 1908 akapila@postgresql.o     1788                 :            695 :     bool        got_new_xmin = false;
                               1789                 :                : 
 4653 rhaas@postgresql.org     1790                 :            695 :     slot = MyReplicationSlot;
                               1791                 :                : 
                               1792         [ -  + ]:            695 :     Assert(slot != NULL);
                               1793                 :                : 
                               1794                 :            695 :     SpinLockAcquire(&slot->mutex);
                               1795                 :                : 
                               1796                 :                :     /*
                               1797                 :                :      * don't overwrite if we already have a newer xmin. This can happen if we
                               1798                 :                :      * restart decoding in a slot.
                               1799                 :                :      */
                               1800         [ +  + ]:            695 :     if (TransactionIdPrecedesOrEquals(xmin, slot->data.catalog_xmin))
                               1801                 :                :     {
                               1802                 :                :     }
                               1803                 :                : 
                               1804                 :                :     /*
                               1805                 :                :      * If the client has already confirmed up to this lsn, we directly can
                               1806                 :                :      * mark this as accepted. This can happen if we restart decoding in a
                               1807                 :                :      * slot.
                               1808                 :                :      */
                               1809         [ +  + ]:            263 :     else if (current_lsn <= slot->data.confirmed_flush)
                               1810                 :                :     {
                               1811                 :             84 :         slot->candidate_catalog_xmin = xmin;
                               1812                 :             84 :         slot->candidate_xmin_lsn = current_lsn;
                               1813                 :                : 
                               1814                 :                :         /* our candidate can directly be used */
                               1815                 :             84 :         updated_xmin = true;
                               1816                 :                :     }
                               1817                 :                : 
                               1818                 :                :     /*
                               1819                 :                :      * Only increase if the previous values have been applied, otherwise we
                               1820                 :                :      * might never end up updating if the receiver acks too slowly.
                               1821                 :                :      */
  387 alvherre@kurilemu.de     1822         [ +  + ]:            179 :     else if (!XLogRecPtrIsValid(slot->candidate_xmin_lsn))
                               1823                 :                :     {
 4653 rhaas@postgresql.org     1824                 :             32 :         slot->candidate_catalog_xmin = xmin;
                               1825                 :             32 :         slot->candidate_xmin_lsn = current_lsn;
                               1826                 :                : 
                               1827                 :                :         /*
                               1828                 :                :          * Log new xmin at an appropriate log level after releasing the
                               1829                 :                :          * spinlock.
                               1830                 :                :          */
 1908 akapila@postgresql.o     1831                 :             32 :         got_new_xmin = true;
                               1832                 :                :     }
 4653 rhaas@postgresql.org     1833                 :            695 :     SpinLockRelease(&slot->mutex);
                               1834                 :                : 
 1908 akapila@postgresql.o     1835         [ +  + ]:            695 :     if (got_new_xmin)
  509 alvherre@kurilemu.de     1836         [ +  + ]:             32 :         elog(DEBUG1, "got new catalog xmin %u at %X/%08X", xmin,
                               1837                 :                :              LSN_FORMAT_ARGS(current_lsn));
                               1838                 :                : 
                               1839                 :                :     /* candidate already valid with the current flush position, apply */
 4653 rhaas@postgresql.org     1840         [ +  + ]:            695 :     if (updated_xmin)
                               1841                 :             84 :         LogicalConfirmReceivedLocation(slot->data.confirmed_flush);
                               1842                 :            695 : }
                               1843                 :                : 
                               1844                 :                : /*
                               1845                 :                :  * Mark the minimal LSN (restart_lsn) we need to read to replay all
                               1846                 :                :  * transactions that have not yet committed at current_lsn.
                               1847                 :                :  *
                               1848                 :                :  * Just like LogicalIncreaseXminForSlot this only takes effect when the
                               1849                 :                :  * client has confirmed to have received current_lsn.
                               1850                 :                :  */
                               1851                 :                : void
                               1852                 :            619 : LogicalIncreaseRestartDecodingForSlot(XLogRecPtr current_lsn, XLogRecPtr restart_lsn)
                               1853                 :                : {
 4589 bruce@momjian.us         1854                 :            619 :     bool        updated_lsn = false;
                               1855                 :                :     ReplicationSlot *slot;
                               1856                 :                : 
 4653 rhaas@postgresql.org     1857                 :            619 :     slot = MyReplicationSlot;
                               1858                 :                : 
                               1859         [ -  + ]:            619 :     Assert(slot != NULL);
  387 alvherre@kurilemu.de     1860         [ -  + ]:            619 :     Assert(XLogRecPtrIsValid(restart_lsn));
                               1861         [ -  + ]:            619 :     Assert(XLogRecPtrIsValid(current_lsn));
                               1862                 :                : 
 4653 rhaas@postgresql.org     1863                 :            619 :     SpinLockAcquire(&slot->mutex);
                               1864                 :                : 
                               1865                 :                :     /* don't overwrite if have a newer restart lsn */
                               1866         [ +  + ]:            619 :     if (restart_lsn <= slot->data.restart_lsn)
                               1867                 :                :     {
  743 msawada@postgresql.o     1868                 :             13 :         SpinLockRelease(&slot->mutex);
                               1869                 :                :     }
                               1870                 :                : 
                               1871                 :                :     /*
                               1872                 :                :      * We might have already flushed far enough to directly accept this lsn,
                               1873                 :                :      * in this case there is no need to check for existing candidate LSNs
                               1874                 :                :      */
 4653 rhaas@postgresql.org     1875         [ +  + ]:            606 :     else if (current_lsn <= slot->data.confirmed_flush)
                               1876                 :                :     {
                               1877                 :            361 :         slot->candidate_restart_valid = current_lsn;
                               1878                 :            361 :         slot->candidate_restart_lsn = restart_lsn;
  743 msawada@postgresql.o     1879                 :            361 :         SpinLockRelease(&slot->mutex);
                               1880                 :                : 
                               1881                 :                :         /* our candidate can directly be used */
 4653 rhaas@postgresql.org     1882                 :            361 :         updated_lsn = true;
                               1883                 :                :     }
                               1884                 :                : 
                               1885                 :                :     /*
                               1886                 :                :      * Only increase if the previous values have been applied, otherwise we
                               1887                 :                :      * might never end up updating if the receiver acks too slowly. A missed
                               1888                 :                :      * value here will just cause some extra effort after reconnecting.
                               1889                 :                :      */
  387 alvherre@kurilemu.de     1890         [ +  + ]:            245 :     else if (!XLogRecPtrIsValid(slot->candidate_restart_valid))
                               1891                 :                :     {
 4653 rhaas@postgresql.org     1892                 :             47 :         slot->candidate_restart_valid = current_lsn;
                               1893                 :             47 :         slot->candidate_restart_lsn = restart_lsn;
 2368 michael@paquier.xyz      1894                 :             47 :         SpinLockRelease(&slot->mutex);
                               1895                 :                : 
  509 alvherre@kurilemu.de     1896         [ +  + ]:             47 :         elog(DEBUG1, "got new restart lsn %X/%08X at %X/%08X",
                               1897                 :                :              LSN_FORMAT_ARGS(restart_lsn),
                               1898                 :                :              LSN_FORMAT_ARGS(current_lsn));
                               1899                 :                :     }
                               1900                 :                :     else
                               1901                 :                :     {
                               1902                 :                :         XLogRecPtr  candidate_restart_lsn;
                               1903                 :                :         XLogRecPtr  candidate_restart_valid;
                               1904                 :                :         XLogRecPtr  confirmed_flush;
                               1905                 :                : 
 2368 michael@paquier.xyz      1906                 :            198 :         candidate_restart_lsn = slot->candidate_restart_lsn;
                               1907                 :            198 :         candidate_restart_valid = slot->candidate_restart_valid;
                               1908                 :            198 :         confirmed_flush = slot->data.confirmed_flush;
                               1909                 :            198 :         SpinLockRelease(&slot->mutex);
                               1910                 :                : 
  509 alvherre@kurilemu.de     1911         [ +  + ]:            198 :         elog(DEBUG1, "failed to increase restart lsn: proposed %X/%08X, after %X/%08X, current candidate %X/%08X, current after %X/%08X, flushed up to %X/%08X",
                               1912                 :                :              LSN_FORMAT_ARGS(restart_lsn),
                               1913                 :                :              LSN_FORMAT_ARGS(current_lsn),
                               1914                 :                :              LSN_FORMAT_ARGS(candidate_restart_lsn),
                               1915                 :                :              LSN_FORMAT_ARGS(candidate_restart_valid),
                               1916                 :                :              LSN_FORMAT_ARGS(confirmed_flush));
                               1917                 :                :     }
                               1918                 :                : 
                               1919                 :                :     /* candidates are already valid with the current flush position, apply */
 4653 rhaas@postgresql.org     1920         [ +  + ]:            619 :     if (updated_lsn)
                               1921                 :            361 :         LogicalConfirmReceivedLocation(slot->data.confirmed_flush);
                               1922                 :            619 : }
                               1923                 :                : 
                               1924                 :                : /*
                               1925                 :                :  * Handle a consumer's confirmation having received all changes up to lsn.
                               1926                 :                :  */
                               1927                 :                : void
                               1928                 :          23837 : LogicalConfirmReceivedLocation(XLogRecPtr lsn)
                               1929                 :                : {
  387 alvherre@kurilemu.de     1930         [ -  + ]:          23837 :     Assert(XLogRecPtrIsValid(lsn));
                               1931                 :                : 
                               1932                 :                :     /* Do an unlocked check for candidate_lsn first. */
                               1933         [ +  + ]:          23837 :     if (XLogRecPtrIsValid(MyReplicationSlot->candidate_xmin_lsn) ||
                               1934         [ +  + ]:          23679 :         XLogRecPtrIsValid(MyReplicationSlot->candidate_restart_valid))
 4653 rhaas@postgresql.org     1935                 :            537 :     {
                               1936                 :            537 :         bool        updated_xmin = false;
                               1937                 :            537 :         bool        updated_restart = false;
                               1938                 :                :         XLogRecPtr  restart_lsn pg_attribute_unused();
                               1939                 :                : 
 4071                          1940                 :            537 :         SpinLockAcquire(&MyReplicationSlot->mutex);
                               1941                 :                : 
                               1942                 :                :         /* remember the old restart lsn */
  532 akorotkov@postgresql     1943                 :            537 :         restart_lsn = MyReplicationSlot->data.restart_lsn;
                               1944                 :                : 
                               1945                 :                :         /*
                               1946                 :                :          * Prevent moving the confirmed_flush backwards, as this could lead to
                               1947                 :                :          * data duplication issues caused by replicating already replicated
                               1948                 :                :          * changes.
                               1949                 :                :          *
                               1950                 :                :          * This can happen when a client acknowledges an LSN it doesn't have
                               1951                 :                :          * to do anything for, and thus didn't store persistently. After a
                               1952                 :                :          * restart, the client can send the prior LSN that it stored
                               1953                 :                :          * persistently as an acknowledgement, but we need to ignore such an
                               1954                 :                :          * LSN. See similar case handling in CreateDecodingContext.
                               1955                 :                :          */
  558 akapila@postgresql.o     1956         [ +  + ]:            537 :         if (lsn > MyReplicationSlot->data.confirmed_flush)
                               1957                 :             39 :             MyReplicationSlot->data.confirmed_flush = lsn;
                               1958                 :                : 
                               1959                 :                :         /* if we're past the location required for bumping xmin, do so */
  387 alvherre@kurilemu.de     1960         [ +  + ]:            537 :         if (XLogRecPtrIsValid(MyReplicationSlot->candidate_xmin_lsn) &&
 4071 rhaas@postgresql.org     1961         [ +  + ]:            158 :             MyReplicationSlot->candidate_xmin_lsn <= lsn)
                               1962                 :                :         {
                               1963                 :                :             /*
                               1964                 :                :              * We have to write the changed xmin to disk *before* we change
                               1965                 :                :              * the in-memory value, otherwise after a crash we wouldn't know
                               1966                 :                :              * that some catalog tuples might have been removed already.
                               1967                 :                :              *
                               1968                 :                :              * Ensure that by first writing to ->xmin and only update
                               1969                 :                :              * ->effective_xmin once the new state is synced to disk. After a
                               1970                 :                :              * crash ->effective_xmin is set to ->xmin.
                               1971                 :                :              */
                               1972         [ +  - ]:            106 :             if (TransactionIdIsValid(MyReplicationSlot->candidate_catalog_xmin) &&
                               1973         [ +  - ]:            106 :                 MyReplicationSlot->data.catalog_xmin != MyReplicationSlot->candidate_catalog_xmin)
                               1974                 :                :             {
                               1975                 :            106 :                 MyReplicationSlot->data.catalog_xmin = MyReplicationSlot->candidate_catalog_xmin;
                               1976                 :            106 :                 MyReplicationSlot->candidate_catalog_xmin = InvalidTransactionId;
                               1977                 :            106 :                 MyReplicationSlot->candidate_xmin_lsn = InvalidXLogRecPtr;
 4653                          1978                 :            106 :                 updated_xmin = true;
                               1979                 :                :             }
                               1980                 :                :         }
                               1981                 :                : 
  387 alvherre@kurilemu.de     1982         [ +  + ]:            537 :         if (XLogRecPtrIsValid(MyReplicationSlot->candidate_restart_valid) &&
 4071 rhaas@postgresql.org     1983         [ +  + ]:            451 :             MyReplicationSlot->candidate_restart_valid <= lsn)
                               1984                 :                :         {
  387 alvherre@kurilemu.de     1985         [ -  + ]:            399 :             Assert(XLogRecPtrIsValid(MyReplicationSlot->candidate_restart_lsn));
                               1986                 :                : 
 4071 rhaas@postgresql.org     1987                 :            399 :             MyReplicationSlot->data.restart_lsn = MyReplicationSlot->candidate_restart_lsn;
                               1988                 :            399 :             MyReplicationSlot->candidate_restart_lsn = InvalidXLogRecPtr;
                               1989                 :            399 :             MyReplicationSlot->candidate_restart_valid = InvalidXLogRecPtr;
 4653                          1990                 :            399 :             updated_restart = true;
                               1991                 :                :         }
                               1992                 :                : 
 4071                          1993                 :            537 :         SpinLockRelease(&MyReplicationSlot->mutex);
                               1994                 :                : 
                               1995                 :                :         /* first write new xmin to disk, so we know what's up after a crash */
 4653                          1996   [ +  +  +  + ]:            537 :         if (updated_xmin || updated_restart)
                               1997                 :                :         {
                               1998                 :                : #ifdef USE_INJECTION_POINTS
                               1999                 :                :             XLogSegNo   seg1,
                               2000                 :                :                         seg2;
                               2001                 :                : 
  532 akorotkov@postgresql     2002                 :            485 :             XLByteToSeg(restart_lsn, seg1, wal_segment_size);
                               2003                 :            485 :             XLByteToSeg(MyReplicationSlot->data.restart_lsn, seg2, wal_segment_size);
                               2004                 :                : 
                               2005                 :                :             /* trigger injection point, but only if segment changes */
                               2006         [ +  + ]:            485 :             if (seg1 != seg2)
                               2007                 :             17 :                 INJECTION_POINT("logical-replication-slot-advance-segment", NULL);
                               2008                 :                : #endif
                               2009                 :                : 
 4653 rhaas@postgresql.org     2010                 :            485 :             ReplicationSlotMarkDirty();
                               2011                 :            485 :             ReplicationSlotSave();
                               2012         [ +  + ]:            485 :             elog(DEBUG1, "updated xmin: %u restart: %u", updated_xmin, updated_restart);
                               2013                 :                :         }
                               2014                 :                : 
                               2015                 :                :         /*
                               2016                 :                :          * Now the new xmin is safely on disk, we can let the global value
                               2017                 :                :          * advance. We do not take ProcArrayLock or similar since we only
                               2018                 :                :          * advance xmin here and there's not much harm done by a concurrent
                               2019                 :                :          * computation missing that.
                               2020                 :                :          */
                               2021         [ +  + ]:            537 :         if (updated_xmin)
                               2022                 :                :         {
 4071                          2023                 :            106 :             SpinLockAcquire(&MyReplicationSlot->mutex);
                               2024                 :            106 :             MyReplicationSlot->effective_catalog_xmin = MyReplicationSlot->data.catalog_xmin;
                               2025                 :            106 :             SpinLockRelease(&MyReplicationSlot->mutex);
                               2026                 :                : 
 4653                          2027                 :            106 :             ReplicationSlotsComputeRequiredXmin(false);
                               2028                 :                :         }
                               2029                 :                : 
                               2030                 :                :         /*
                               2031                 :                :          * Now that the new restart_lsn is safely on disk, recompute the
                               2032                 :                :          * global WAL retention requirement.
                               2033                 :                :          */
  183 alvherre@kurilemu.de     2034         [ +  + ]:            537 :         if (updated_restart)
                               2035                 :            399 :             ReplicationSlotsComputeRequiredLSN();
                               2036                 :                :     }
                               2037                 :                :     else
                               2038                 :                :     {
 4071 rhaas@postgresql.org     2039                 :          23300 :         SpinLockAcquire(&MyReplicationSlot->mutex);
                               2040                 :                : 
                               2041                 :                :         /*
                               2042                 :                :          * Prevent moving the confirmed_flush backwards. See comments above
                               2043                 :                :          * for the details.
                               2044                 :                :          */
  558 akapila@postgresql.o     2045         [ +  + ]:          23300 :         if (lsn > MyReplicationSlot->data.confirmed_flush)
                               2046                 :          22348 :             MyReplicationSlot->data.confirmed_flush = lsn;
                               2047                 :                : 
 4071 rhaas@postgresql.org     2048                 :          23300 :         SpinLockRelease(&MyReplicationSlot->mutex);
                               2049                 :                :     }
 4653                          2050                 :          23837 : }
                               2051                 :                : 
                               2052                 :                : /*
                               2053                 :                :  * Clear logical streaming state during (sub)transaction abort.
                               2054                 :                :  */
                               2055                 :                : void
 2303 akapila@postgresql.o     2056                 :          42959 : ResetLogicalStreamingState(void)
                               2057                 :                : {
                               2058                 :          42959 :     CheckXidAlive = InvalidTransactionId;
                               2059                 :          42959 :     bsysscan = false;
                               2060                 :          42959 : }
                               2061                 :                : 
                               2062                 :                : /*
                               2063                 :                :  * Report stats for a slot.
                               2064                 :                :  */
                               2065                 :                : void
 2242                          2066                 :           7844 : UpdateDecodingStats(LogicalDecodingContext *ctx)
                               2067                 :                : {
                               2068                 :           7844 :     ReorderBuffer *rb = ctx->reorder;
                               2069                 :                :     PgStat_StatReplSlotEntry repSlotStat;
                               2070                 :                : 
                               2071                 :                :     /* Nothing to do if we don't have any replication stats to be sent. */
  416 msawada@postgresql.o     2072   [ +  +  +  +  :           7844 :     if (rb->spillBytes <= 0 && rb->streamBytes <= 0 && rb->totalBytes <= 0 &&
                                              +  + ]
                               2073         [ +  + ]:            513 :         rb->memExceededCount <= 0)
 2242 akapila@postgresql.o     2074                 :            501 :         return;
                               2075                 :                : 
  416 msawada@postgresql.o     2076         [ +  + ]:           7343 :     elog(DEBUG2, "UpdateDecodingStats: updating stats %p %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,
                               2077                 :                :          rb,
                               2078                 :                :          rb->spillTxns,
                               2079                 :                :          rb->spillCount,
                               2080                 :                :          rb->spillBytes,
                               2081                 :                :          rb->streamTxns,
                               2082                 :                :          rb->streamCount,
                               2083                 :                :          rb->streamBytes,
                               2084                 :                :          rb->memExceededCount,
                               2085                 :                :          rb->totalTxns,
                               2086                 :                :          rb->totalBytes);
                               2087                 :                : 
 2054 akapila@postgresql.o     2088                 :           7343 :     repSlotStat.spill_txns = rb->spillTxns;
                               2089                 :           7343 :     repSlotStat.spill_count = rb->spillCount;
                               2090                 :           7343 :     repSlotStat.spill_bytes = rb->spillBytes;
                               2091                 :           7343 :     repSlotStat.stream_txns = rb->streamTxns;
                               2092                 :           7343 :     repSlotStat.stream_count = rb->streamCount;
                               2093                 :           7343 :     repSlotStat.stream_bytes = rb->streamBytes;
  416 msawada@postgresql.o     2094                 :           7343 :     repSlotStat.mem_exceeded_count = rb->memExceededCount;
 2052 akapila@postgresql.o     2095                 :           7343 :     repSlotStat.total_txns = rb->totalTxns;
                               2096                 :           7343 :     repSlotStat.total_bytes = rb->totalBytes;
                               2097                 :                : 
 1697 andres@anarazel.de       2098                 :           7343 :     pgstat_report_replslot(ctx->slot, &repSlotStat);
                               2099                 :                : 
 2242 akapila@postgresql.o     2100                 :           7343 :     rb->spillTxns = 0;
                               2101                 :           7343 :     rb->spillCount = 0;
                               2102                 :           7343 :     rb->spillBytes = 0;
 2221                          2103                 :           7343 :     rb->streamTxns = 0;
                               2104                 :           7343 :     rb->streamCount = 0;
                               2105                 :           7343 :     rb->streamBytes = 0;
  416 msawada@postgresql.o     2106                 :           7343 :     rb->memExceededCount = 0;
 2052 akapila@postgresql.o     2107                 :           7343 :     rb->totalTxns = 0;
                               2108                 :           7343 :     rb->totalBytes = 0;
                               2109                 :                : }
                               2110                 :                : 
                               2111                 :                : /*
                               2112                 :                :  * Read up to the end of WAL starting from the decoding slot's restart_lsn
                               2113                 :                :  * to end_of_wal in order to check if any meaningful/decodable WAL records
                               2114                 :                :  * are encountered. scan_cutoff_lsn is the LSN, where we can terminate the
                               2115                 :                :  * WAL scan early if we find a decodable WAL record after this LSN.
                               2116                 :                :  *
                               2117                 :                :  * Returns the last LSN decodable WAL record's LSN if found, otherwise
                               2118                 :                :  * returns InvalidXLogRecPtr.
                               2119                 :                :  */
                               2120                 :                : XLogRecPtr
  297 msawada@postgresql.o     2121                 :              4 : LogicalReplicationSlotCheckPendingWal(XLogRecPtr end_of_wal,
                               2122                 :                :                                       XLogRecPtr scan_cutoff_lsn)
                               2123                 :                : {
                               2124                 :              4 :     XLogRecPtr  last_pending_wal = InvalidXLogRecPtr;
                               2125                 :                : 
 1129 akapila@postgresql.o     2126         [ -  + ]:              4 :     Assert(MyReplicationSlot);
  297 msawada@postgresql.o     2127         [ -  + ]:              4 :     Assert(end_of_wal >= scan_cutoff_lsn);
                               2128                 :                : 
 1129 akapila@postgresql.o     2129         [ +  - ]:              4 :     PG_TRY();
                               2130                 :                :     {
                               2131                 :                :         LogicalDecodingContext *ctx;
                               2132                 :                : 
                               2133                 :                :         /*
                               2134                 :                :          * Create our decoding context in fast_forward mode, passing start_lsn
                               2135                 :                :          * as InvalidXLogRecPtr, so that we start processing from the slot's
                               2136                 :                :          * confirmed_flush.
                               2137                 :                :          */
                               2138                 :              8 :         ctx = CreateDecodingContext(InvalidXLogRecPtr,
                               2139                 :                :                                     NIL,
                               2140                 :                :                                     true,   /* fast_forward */
                               2141                 :              4 :                                     XL_ROUTINE(.page_read = read_local_xlog_page,
                               2142                 :                :                                                .segment_open = wal_segment_open,
                               2143                 :                :                                                .segment_close = wal_segment_close),
                               2144                 :                :                                     NULL, NULL, NULL);
                               2145                 :                : 
                               2146                 :                :         /*
                               2147                 :                :          * Start reading at the slot's restart_lsn, which we know points to a
                               2148                 :                :          * valid record.
                               2149                 :                :          */
                               2150                 :              4 :         XLogBeginRead(ctx->reader, MyReplicationSlot->data.restart_lsn);
                               2151                 :                : 
                               2152                 :                :         /* Invalidate non-timetravel entries */
                               2153                 :              4 :         InvalidateSystemCaches();
                               2154                 :                : 
  297 msawada@postgresql.o     2155         [ +  + ]:             88 :         while (ctx->reader->EndRecPtr < end_of_wal)
                               2156                 :                :         {
                               2157                 :                :             XLogRecord *record;
 1129 akapila@postgresql.o     2158                 :             84 :             char       *errm = NULL;
                               2159                 :                : 
                               2160                 :             84 :             record = XLogReadRecord(ctx->reader, &errm);
                               2161                 :                : 
                               2162         [ -  + ]:             84 :             if (errm)
 1129 akapila@postgresql.o     2163         [ #  # ]:UBC           0 :                 elog(ERROR, "could not find record for logical decoding: %s", errm);
                               2164                 :                : 
 1129 akapila@postgresql.o     2165         [ +  - ]:CBC          84 :             if (record != NULL)
                               2166                 :             84 :                 LogicalDecodingProcessRecord(ctx, ctx->reader);
                               2167                 :                : 
  297 msawada@postgresql.o     2168         [ +  + ]:             84 :             if (ctx->processing_required)
                               2169                 :                :             {
                               2170                 :              2 :                 last_pending_wal = ctx->reader->ReadRecPtr;
                               2171                 :                : 
                               2172                 :                :                 /*
                               2173                 :                :                  * If we find a decodable WAL after the scan_cutoff_lsn point,
                               2174                 :                :                  * we can terminate the scan early.
                               2175                 :                :                  */
                               2176         [ -  + ]:              2 :                 if (last_pending_wal >= scan_cutoff_lsn)
  297 msawada@postgresql.o     2177                 :UBC           0 :                     break;
                               2178                 :                : 
                               2179                 :                :                 /* Reset the flag and continue checking */
  297 msawada@postgresql.o     2180                 :CBC           2 :                 ctx->processing_required = false;
                               2181                 :                :             }
                               2182                 :                : 
 1129 akapila@postgresql.o     2183         [ -  + ]:             84 :             CHECK_FOR_INTERRUPTS();
                               2184                 :                :         }
                               2185                 :                : 
                               2186                 :                :         /* Clean up */
                               2187                 :              4 :         FreeDecodingContext(ctx);
                               2188                 :              4 :         InvalidateSystemCaches();
                               2189                 :                :     }
 1129 akapila@postgresql.o     2190                 :UBC           0 :     PG_CATCH();
                               2191                 :                :     {
                               2192                 :                :         /* clear all timetravel entries */
                               2193                 :              0 :         InvalidateSystemCaches();
                               2194                 :                : 
                               2195                 :              0 :         PG_RE_THROW();
                               2196                 :                :     }
 1129 akapila@postgresql.o     2197         [ -  + ]:CBC           4 :     PG_END_TRY();
                               2198                 :                : 
  297 msawada@postgresql.o     2199                 :              4 :     return last_pending_wal;
                               2200                 :                : }
                               2201                 :                : 
                               2202                 :                : /*
                               2203                 :                :  * Helper function for advancing our logical replication slot forward.
                               2204                 :                :  *
                               2205                 :                :  * The slot's restart_lsn is used as start point for reading records, while
                               2206                 :                :  * confirmed_flush is used as base point for the decoding context.
                               2207                 :                :  *
                               2208                 :                :  * We cannot just do LogicalConfirmReceivedLocation to update confirmed_flush,
                               2209                 :                :  * because we need to digest WAL to advance restart_lsn allowing to recycle
                               2210                 :                :  * WAL and removal of old catalog tuples.  As decoding is done in fast_forward
                               2211                 :                :  * mode, no changes are generated anyway.
                               2212                 :                :  *
                               2213                 :                :  * *found_consistent_snapshot will be true if the initial decoding snapshot has
                               2214                 :                :  * been built; Otherwise, it will be false.
                               2215                 :                :  */
                               2216                 :                : XLogRecPtr
  969 akapila@postgresql.o     2217                 :             24 : LogicalSlotAdvanceAndCheckSnapState(XLogRecPtr moveto,
                               2218                 :                :                                     bool *found_consistent_snapshot)
                               2219                 :                : {
                               2220                 :                :     LogicalDecodingContext *ctx;
                               2221                 :                :     XLogRecPtr  retlsn;
                               2222                 :                : 
  387 alvherre@kurilemu.de     2223         [ -  + ]:             24 :     Assert(XLogRecPtrIsValid(moveto));
                               2224                 :                : 
  969 akapila@postgresql.o     2225         [ +  + ]:             24 :     if (found_consistent_snapshot)
                               2226                 :             12 :         *found_consistent_snapshot = false;
                               2227                 :                : 
                               2228         [ +  - ]:             24 :     PG_TRY();
                               2229                 :                :     {
                               2230                 :                :         /*
                               2231                 :                :          * Create our decoding context in fast_forward mode, passing start_lsn
                               2232                 :                :          * as InvalidXLogRecPtr, so that we start processing from my slot's
                               2233                 :                :          * confirmed_flush.
                               2234                 :                :          */
                               2235                 :             48 :         ctx = CreateDecodingContext(InvalidXLogRecPtr,
                               2236                 :                :                                     NIL,
                               2237                 :                :                                     true,   /* fast_forward */
                               2238                 :             24 :                                     XL_ROUTINE(.page_read = read_local_xlog_page,
                               2239                 :                :                                                .segment_open = wal_segment_open,
                               2240                 :                :                                                .segment_close = wal_segment_close),
                               2241                 :                :                                     NULL, NULL, NULL);
                               2242                 :                : 
                               2243                 :                :         /*
                               2244                 :                :          * Wait for specified streaming replication standby servers (if any)
                               2245                 :                :          * to confirm receipt of WAL up to moveto lsn.
                               2246                 :                :          */
                               2247                 :             24 :         WaitForStandbyConfirmation(moveto);
                               2248                 :                : 
                               2249                 :                :         /*
                               2250                 :                :          * Start reading at the slot's restart_lsn, which we know to point to
                               2251                 :                :          * a valid record.
                               2252                 :                :          */
                               2253                 :             24 :         XLogBeginRead(ctx->reader, MyReplicationSlot->data.restart_lsn);
                               2254                 :                : 
                               2255                 :                :         /* invalidate non-timetravel entries */
                               2256                 :             24 :         InvalidateSystemCaches();
                               2257                 :                : 
                               2258                 :                :         /* Decode records until we reach the requested target */
                               2259         [ +  + ]:           3076 :         while (ctx->reader->EndRecPtr < moveto)
                               2260                 :                :         {
                               2261                 :           3052 :             char       *errm = NULL;
                               2262                 :                :             XLogRecord *record;
                               2263                 :                : 
                               2264                 :                :             /*
                               2265                 :                :              * Read records.  No changes are generated in fast_forward mode,
                               2266                 :                :              * but snapbuilder/slot statuses are updated properly.
                               2267                 :                :              */
                               2268                 :           3052 :             record = XLogReadRecord(ctx->reader, &errm);
                               2269         [ -  + ]:           3052 :             if (errm)
  969 akapila@postgresql.o     2270         [ #  # ]:UBC           0 :                 elog(ERROR, "could not find record while advancing replication slot: %s",
                               2271                 :                :                      errm);
                               2272                 :                : 
                               2273                 :                :             /*
                               2274                 :                :              * Process the record.  Storage-level changes are ignored in
                               2275                 :                :              * fast_forward mode, but other modules (such as snapbuilder)
                               2276                 :                :              * might still have critical updates to do.
                               2277                 :                :              */
  969 akapila@postgresql.o     2278         [ +  - ]:CBC        3052 :             if (record)
                               2279                 :           3052 :                 LogicalDecodingProcessRecord(ctx, ctx->reader);
                               2280                 :                : 
                               2281         [ -  + ]:           3052 :             CHECK_FOR_INTERRUPTS();
                               2282                 :                :         }
                               2283                 :                : 
                               2284   [ +  +  +  - ]:             24 :         if (found_consistent_snapshot && DecodingContextReady(ctx))
                               2285                 :             12 :             *found_consistent_snapshot = true;
                               2286                 :                : 
  387 alvherre@kurilemu.de     2287         [ +  - ]:             24 :         if (XLogRecPtrIsValid(ctx->reader->EndRecPtr))
                               2288                 :                :         {
  969 akapila@postgresql.o     2289                 :             24 :             LogicalConfirmReceivedLocation(moveto);
                               2290                 :                : 
                               2291                 :                :             /*
                               2292                 :                :              * If only the confirmed_flush LSN has changed the slot won't get
                               2293                 :                :              * marked as dirty by the above. Callers on the walsender
                               2294                 :                :              * interface are expected to keep track of their own progress and
                               2295                 :                :              * don't need it written out. But SQL-interface users cannot
                               2296                 :                :              * specify their own start positions and it's harder for them to
                               2297                 :                :              * keep track of their progress, so we should make more of an
                               2298                 :                :              * effort to save it for them.
                               2299                 :                :              *
                               2300                 :                :              * Dirty the slot so it is written out at the next checkpoint. The
                               2301                 :                :              * LSN position advanced to may still be lost on a crash but this
                               2302                 :                :              * makes the data consistent after a clean shutdown.
                               2303                 :                :              */
                               2304                 :             24 :             ReplicationSlotMarkDirty();
                               2305                 :                :         }
                               2306                 :                : 
                               2307                 :             24 :         retlsn = MyReplicationSlot->data.confirmed_flush;
                               2308                 :                : 
                               2309                 :                :         /* free context, call shutdown callback */
                               2310                 :             24 :         FreeDecodingContext(ctx);
                               2311                 :                : 
                               2312                 :             24 :         InvalidateSystemCaches();
                               2313                 :                :     }
  969 akapila@postgresql.o     2314                 :UBC           0 :     PG_CATCH();
                               2315                 :                :     {
                               2316                 :                :         /* clear all timetravel entries */
                               2317                 :              0 :         InvalidateSystemCaches();
                               2318                 :                : 
                               2319                 :              0 :         PG_RE_THROW();
                               2320                 :                :     }
  969 akapila@postgresql.o     2321         [ -  + ]:CBC          24 :     PG_END_TRY();
                               2322                 :                : 
                               2323                 :             24 :     return retlsn;
                               2324                 :                : }
        

Generated by: LCOV version 2.0-1