LCOV - differential code coverage report
Current view: top level - src/backend/replication/logical - logical.c (source / functions) Coverage Total Hit UNC LBC UIC UBC GBC GIC GNC CBC EUB ECB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 93.4 % 731 683 1 12 34 1 16 373 26 268 31 398 5
Current Date: 2023-04-08 15:15:32 Functions: 97.4 % 39 38 1 37 1 1 38
Baseline: 15
Baseline Date: 2023-04-08 15:09:40
Legend: Lines: hit not hit

           TLA  Line data    Source code
       1                 : /*-------------------------------------------------------------------------
       2                 :  * logical.c
       3                 :  *     PostgreSQL logical decoding coordination
       4                 :  *
       5                 :  * Copyright (c) 2012-2023, 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 "fmgr.h"
      34                 : #include "miscadmin.h"
      35                 : #include "pgstat.h"
      36                 : #include "replication/decode.h"
      37                 : #include "replication/logical.h"
      38                 : #include "replication/origin.h"
      39                 : #include "replication/reorderbuffer.h"
      40                 : #include "replication/snapbuild.h"
      41                 : #include "storage/proc.h"
      42                 : #include "storage/procarray.h"
      43                 : #include "utils/builtins.h"
      44                 : #include "utils/memutils.h"
      45                 : 
      46                 : /* data for errcontext callback */
      47                 : typedef struct LogicalErrorCallbackState
      48                 : {
      49                 :     LogicalDecodingContext *ctx;
      50                 :     const char *callback_name;
      51                 :     XLogRecPtr  report_location;
      52                 : } LogicalErrorCallbackState;
      53                 : 
      54                 : /* wrappers around output plugin callbacks */
      55                 : static void output_plugin_error_callback(void *arg);
      56                 : static void startup_cb_wrapper(LogicalDecodingContext *ctx, OutputPluginOptions *opt,
      57                 :                                bool is_init);
      58                 : static void shutdown_cb_wrapper(LogicalDecodingContext *ctx);
      59                 : static void begin_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn);
      60                 : static void commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
      61                 :                               XLogRecPtr commit_lsn);
      62                 : static void begin_prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn);
      63                 : static void prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
      64                 :                                XLogRecPtr prepare_lsn);
      65                 : static void commit_prepared_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
      66                 :                                        XLogRecPtr commit_lsn);
      67                 : static void rollback_prepared_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
      68                 :                                          XLogRecPtr prepare_end_lsn, TimestampTz prepare_time);
      69                 : static void change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
      70                 :                               Relation relation, ReorderBufferChange *change);
      71                 : static void truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
      72                 :                                 int nrelations, Relation relations[], ReorderBufferChange *change);
      73                 : static void message_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
      74                 :                                XLogRecPtr message_lsn, bool transactional,
      75                 :                                const char *prefix, Size message_size, const char *message);
      76                 : 
      77                 : /* streaming callbacks */
      78                 : static void stream_start_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
      79                 :                                     XLogRecPtr first_lsn);
      80                 : static void stream_stop_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
      81                 :                                    XLogRecPtr last_lsn);
      82                 : static void stream_abort_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
      83                 :                                     XLogRecPtr abort_lsn);
      84                 : static void stream_prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
      85                 :                                       XLogRecPtr prepare_lsn);
      86                 : static void stream_commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
      87                 :                                      XLogRecPtr commit_lsn);
      88                 : static void stream_change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
      89                 :                                      Relation relation, ReorderBufferChange *change);
      90                 : static void stream_message_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
      91                 :                                       XLogRecPtr message_lsn, bool transactional,
      92                 :                                       const char *prefix, Size message_size, const char *message);
      93                 : static void stream_truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
      94                 :                                        int nrelations, Relation relations[], ReorderBufferChange *change);
      95                 : 
      96                 : /* callback to update txn's progress */
      97                 : static void update_progress_txn_cb_wrapper(ReorderBuffer *cache,
      98                 :                                            ReorderBufferTXN *txn,
      99                 :                                            XLogRecPtr lsn);
     100                 : 
     101                 : static void LoadOutputPlugin(OutputPluginCallbacks *callbacks, const char *plugin);
     102                 : 
     103                 : /*
     104                 :  * Make sure the current settings & environment are capable of doing logical
     105                 :  * decoding.
     106                 :  */
     107                 : void
     108 GIC        1182 : CheckLogicalDecodingRequirements(void)
     109                 : {
     110            1182 :     CheckSlotRequirements();
     111                 : 
     112                 :     /*
     113 ECB             :      * NB: Adding a new requirement likely means that RestoreSlotFromDisk()
     114                 :      * needs the same check.
     115                 :      */
     116                 : 
     117 GIC        1182 :     if (wal_level < WAL_LEVEL_LOGICAL)
     118 UIC           0 :         ereport(ERROR,
     119                 :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
     120                 :                  errmsg("logical decoding requires wal_level >= logical")));
     121                 : 
     122 CBC        1182 :     if (MyDatabaseId == InvalidOid)
     123 GBC           1 :         ereport(ERROR,
     124                 :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
     125                 :                  errmsg("logical decoding requires a database connection")));
     126                 : 
     127 GIC        1181 :     if (RecoveryInProgress())
     128                 :     {
     129                 :         /*
     130                 :          * This check may have race conditions, but whenever
     131                 :          * XLOG_PARAMETER_CHANGE indicates that wal_level has changed, we
     132                 :          * verify that there are no existing logical replication slots. And to
     133                 :          * avoid races around creating a new slot,
     134                 :          * CheckLogicalDecodingRequirements() is called once before creating
     135                 :          * the slot, and once when logical decoding is initially starting up.
     136                 :          */
     137 GNC          61 :         if (GetActiveWalLevelOnStandby() < WAL_LEVEL_LOGICAL)
     138               1 :             ereport(ERROR,
     139                 :                     (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
     140                 :                      errmsg("logical decoding on a standby requires wal_level to be at least logical on the primary")));
     141                 :     }
     142 GIC        1180 : }
     143                 : 
     144                 : /*
     145 ECB             :  * Helper function for CreateInitDecodingContext() and
     146                 :  * CreateDecodingContext() performing common tasks.
     147                 :  */
     148                 : static LogicalDecodingContext *
     149 GIC         822 : StartupDecodingContext(List *output_plugin_options,
     150                 :                        XLogRecPtr start_lsn,
     151                 :                        TransactionId xmin_horizon,
     152 ECB             :                        bool need_full_snapshot,
     153                 :                        bool fast_forward,
     154                 :                        XLogReaderRoutine *xl_routine,
     155                 :                        LogicalOutputPluginWriterPrepareWrite prepare_write,
     156                 :                        LogicalOutputPluginWriterWrite do_write,
     157                 :                        LogicalOutputPluginWriterUpdateProgress update_progress)
     158                 : {
     159                 :     ReplicationSlot *slot;
     160                 :     MemoryContext context,
     161                 :                 old_context;
     162                 :     LogicalDecodingContext *ctx;
     163                 : 
     164                 :     /* shorter lines... */
     165 GIC         822 :     slot = MyReplicationSlot;
     166                 : 
     167             822 :     context = AllocSetContextCreate(CurrentMemoryContext,
     168 ECB             :                                     "Logical decoding context",
     169                 :                                     ALLOCSET_DEFAULT_SIZES);
     170 CBC         822 :     old_context = MemoryContextSwitchTo(context);
     171 GIC         822 :     ctx = palloc0(sizeof(LogicalDecodingContext));
     172                 : 
     173 CBC         822 :     ctx->context = context;
     174 ECB             : 
     175                 :     /*
     176                 :      * (re-)load output plugins, so we detect a bad (removed) output plugin
     177                 :      * now.
     178                 :      */
     179 GIC         822 :     if (!fast_forward)
     180             819 :         LoadOutputPlugin(&ctx->callbacks, NameStr(slot->data.plugin));
     181                 : 
     182 ECB             :     /*
     183                 :      * Now that the slot's xmin has been set, we can announce ourselves as a
     184                 :      * logical decoding backend which doesn't need to be checked individually
     185                 :      * when computing the xmin horizon because the xmin is enforced via
     186                 :      * replication slots.
     187                 :      *
     188                 :      * We can only do so if we're outside of a transaction (i.e. the case when
     189                 :      * streaming changes via walsender), otherwise an already setup
     190                 :      * snapshot/xid would end up being ignored. That's not a particularly
     191                 :      * bothersome restriction since the SQL interface can't be used for
     192                 :      * streaming anyway.
     193                 :      */
     194 GIC         821 :     if (!IsTransactionOrTransactionBlock())
     195                 :     {
     196             382 :         LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
     197 CBC         382 :         MyProc->statusFlags |= PROC_IN_LOGICAL_DECODING;
     198 GIC         382 :         ProcGlobal->statusFlags[MyProc->pgxactoff] = MyProc->statusFlags;
     199 CBC         382 :         LWLockRelease(ProcArrayLock);
     200 ECB             :     }
     201                 : 
     202 CBC         821 :     ctx->slot = slot;
     203                 : 
     204 GIC         821 :     ctx->reader = XLogReaderAllocate(wal_segment_size, NULL, xl_routine, ctx);
     205 CBC         821 :     if (!ctx->reader)
     206 UIC           0 :         ereport(ERROR,
     207 ECB             :                 (errcode(ERRCODE_OUT_OF_MEMORY),
     208                 :                  errmsg("out of memory"),
     209 EUB             :                  errdetail("Failed while allocating a WAL reading processor.")));
     210                 : 
     211 GIC         821 :     ctx->reorder = ReorderBufferAllocate();
     212             821 :     ctx->snapshot_builder =
     213             821 :         AllocateSnapshotBuilder(ctx->reorder, xmin_horizon, start_lsn,
     214 ECB             :                                 need_full_snapshot, slot->data.two_phase_at);
     215                 : 
     216 CBC         821 :     ctx->reorder->private_data = ctx;
     217                 : 
     218                 :     /* wrap output plugin callbacks, so we can add error context information */
     219             821 :     ctx->reorder->begin = begin_cb_wrapper;
     220 GIC         821 :     ctx->reorder->apply_change = change_cb_wrapper;
     221             821 :     ctx->reorder->apply_truncate = truncate_cb_wrapper;
     222 CBC         821 :     ctx->reorder->commit = commit_cb_wrapper;
     223             821 :     ctx->reorder->message = message_cb_wrapper;
     224 ECB             : 
     225                 :     /*
     226                 :      * To support streaming, we require start/stop/abort/commit/change
     227                 :      * callbacks. The message and truncate callbacks are optional, similar to
     228                 :      * regular output plugins. We however enable streaming when at least one
     229                 :      * of the methods is enabled so that we can easily identify missing
     230                 :      * methods.
     231                 :      *
     232                 :      * We decide it here, but only check it later in the wrappers.
     233                 :      */
     234 GIC        1645 :     ctx->streaming = (ctx->callbacks.stream_start_cb != NULL) ||
     235               3 :         (ctx->callbacks.stream_stop_cb != NULL) ||
     236               3 :         (ctx->callbacks.stream_abort_cb != NULL) ||
     237 CBC           3 :         (ctx->callbacks.stream_commit_cb != NULL) ||
     238               3 :         (ctx->callbacks.stream_change_cb != NULL) ||
     239             827 :         (ctx->callbacks.stream_message_cb != NULL) ||
     240               3 :         (ctx->callbacks.stream_truncate_cb != NULL);
     241 ECB             : 
     242                 :     /*
     243                 :      * streaming callbacks
     244                 :      *
     245                 :      * stream_message and stream_truncate callbacks are optional, so we do not
     246                 :      * fail with ERROR when missing, but the wrappers simply do nothing. We
     247                 :      * must set the ReorderBuffer callbacks to something, otherwise the calls
     248                 :      * from there will crash (we don't want to move the checks there).
     249                 :      */
     250 GIC         821 :     ctx->reorder->stream_start = stream_start_cb_wrapper;
     251             821 :     ctx->reorder->stream_stop = stream_stop_cb_wrapper;
     252             821 :     ctx->reorder->stream_abort = stream_abort_cb_wrapper;
     253 CBC         821 :     ctx->reorder->stream_prepare = stream_prepare_cb_wrapper;
     254             821 :     ctx->reorder->stream_commit = stream_commit_cb_wrapper;
     255             821 :     ctx->reorder->stream_change = stream_change_cb_wrapper;
     256             821 :     ctx->reorder->stream_message = stream_message_cb_wrapper;
     257             821 :     ctx->reorder->stream_truncate = stream_truncate_cb_wrapper;
     258 ECB             : 
     259                 : 
     260                 :     /*
     261                 :      * To support two-phase logical decoding, we require
     262                 :      * begin_prepare/prepare/commit-prepare/abort-prepare callbacks. The
     263                 :      * filter_prepare callback is optional. We however enable two-phase
     264                 :      * logical decoding when at least one of the methods is enabled so that we
     265                 :      * can easily identify missing methods.
     266                 :      *
     267                 :      * We decide it here, but only check it later in the wrappers.
     268                 :      */
     269 GIC        1645 :     ctx->twophase = (ctx->callbacks.begin_prepare_cb != NULL) ||
     270               3 :         (ctx->callbacks.prepare_cb != NULL) ||
     271               3 :         (ctx->callbacks.commit_prepared_cb != NULL) ||
     272 CBC           3 :         (ctx->callbacks.rollback_prepared_cb != NULL) ||
     273             827 :         (ctx->callbacks.stream_prepare_cb != NULL) ||
     274               3 :         (ctx->callbacks.filter_prepare_cb != NULL);
     275 ECB             : 
     276                 :     /*
     277                 :      * Callback to support decoding at prepare time.
     278                 :      */
     279 GIC         821 :     ctx->reorder->begin_prepare = begin_prepare_cb_wrapper;
     280             821 :     ctx->reorder->prepare = prepare_cb_wrapper;
     281             821 :     ctx->reorder->commit_prepared = commit_prepared_cb_wrapper;
     282 CBC         821 :     ctx->reorder->rollback_prepared = rollback_prepared_cb_wrapper;
     283 ECB             : 
     284                 :     /*
     285                 :      * Callback to support updating progress during sending data of a
     286                 :      * transaction (and its subtransactions) to the output plugin.
     287                 :      */
     288 GNC         821 :     ctx->reorder->update_progress_txn = update_progress_txn_cb_wrapper;
     289                 : 
     290 CBC         821 :     ctx->out = makeStringInfo();
     291             821 :     ctx->prepare_write = prepare_write;
     292 GIC         821 :     ctx->write = do_write;
     293             821 :     ctx->update_progress = update_progress;
     294                 : 
     295             821 :     ctx->output_plugin_options = output_plugin_options;
     296                 : 
     297 CBC         821 :     ctx->fast_forward = fast_forward;
     298                 : 
     299             821 :     MemoryContextSwitchTo(old_context);
     300 ECB             : 
     301 CBC         821 :     return ctx;
     302 ECB             : }
     303                 : 
     304                 : /*
     305                 :  * Create a new decoding context, for a new logical slot.
     306                 :  *
     307                 :  * plugin -- contains the name of the output plugin
     308                 :  * output_plugin_options -- contains options passed to the output plugin
     309                 :  * need_full_snapshot -- if true, must obtain a snapshot able to read all
     310                 :  *      tables; if false, one that can read only catalogs is acceptable.
     311                 :  * restart_lsn -- if given as invalid, it's this routine's responsibility to
     312                 :  *      mark WAL as reserved by setting a convenient restart_lsn for the slot.
     313                 :  *      Otherwise, we set for decoding to start from the given LSN without
     314                 :  *      marking WAL reserved beforehand.  In that scenario, it's up to the
     315                 :  *      caller to guarantee that WAL remains available.
     316                 :  * xl_routine -- XLogReaderRoutine for underlying XLogReader
     317                 :  * prepare_write, do_write, update_progress --
     318                 :  *      callbacks that perform the use-case dependent, actual, work.
     319                 :  *
     320                 :  * Needs to be called while in a memory context that's at least as long lived
     321                 :  * as the decoding context because further memory contexts will be created
     322                 :  * inside it.
     323                 :  *
     324                 :  * Returns an initialized decoding context after calling the output plugin's
     325                 :  * startup function.
     326                 :  */
     327                 : LogicalDecodingContext *
     328 GIC         344 : CreateInitDecodingContext(const char *plugin,
     329                 :                           List *output_plugin_options,
     330                 :                           bool need_full_snapshot,
     331                 :                           XLogRecPtr restart_lsn,
     332                 :                           XLogReaderRoutine *xl_routine,
     333                 :                           LogicalOutputPluginWriterPrepareWrite prepare_write,
     334                 :                           LogicalOutputPluginWriterWrite do_write,
     335                 :                           LogicalOutputPluginWriterUpdateProgress update_progress)
     336                 : {
     337 CBC         344 :     TransactionId xmin_horizon = InvalidTransactionId;
     338                 :     ReplicationSlot *slot;
     339                 :     NameData    plugin_name;
     340                 :     LogicalDecodingContext *ctx;
     341                 :     MemoryContext old_context;
     342                 : 
     343                 :     /*
     344                 :      * On a standby, this check is also required while creating the
     345                 :      * slot. Check the comments in the function.
     346                 :      */
     347 GNC         344 :     CheckLogicalDecodingRequirements();
     348                 : 
     349                 :     /* shorter lines... */
     350 GIC         344 :     slot = MyReplicationSlot;
     351                 : 
     352 ECB             :     /* first some sanity checks that are unlikely to be violated */
     353 GIC         344 :     if (slot == NULL)
     354 UIC           0 :         elog(ERROR, "cannot perform logical decoding without an acquired slot");
     355                 : 
     356 GIC         344 :     if (plugin == NULL)
     357 UIC           0 :         elog(ERROR, "cannot initialize logical decoding without a specified plugin");
     358                 : 
     359                 :     /* Make sure the passed slot is suitable. These are user facing errors. */
     360 GIC         344 :     if (SlotIsPhysical(slot))
     361 UIC           0 :         ereport(ERROR,
     362 ECB             :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
     363                 :                  errmsg("cannot use physical replication slot for logical decoding")));
     364                 : 
     365 CBC         344 :     if (slot->data.database != MyDatabaseId)
     366 UIC           0 :         ereport(ERROR,
     367                 :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
     368 ECB             :                  errmsg("replication slot \"%s\" was not created in this database",
     369 EUB             :                         NameStr(slot->data.name))));
     370                 : 
     371 CBC         596 :     if (IsTransactionState() &&
     372 GBC         252 :         GetTopTransactionIdIfAny() != InvalidTransactionId)
     373 GIC           2 :         ereport(ERROR,
     374                 :                 (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
     375 ECB             :                  errmsg("cannot create logical replication slot in transaction that has performed writes")));
     376 EUB             : 
     377                 :     /*
     378                 :      * Register output plugin name with slot.  We need the mutex to avoid
     379                 :      * concurrent reading of a partially copied string.  But we don't want any
     380 ECB             :      * complicated code while holding a spinlock, so do namestrcpy() outside.
     381 EUB             :      */
     382 GIC         342 :     namestrcpy(&plugin_name, plugin);
     383             342 :     SpinLockAcquire(&slot->mutex);
     384             342 :     slot->data.plugin = plugin_name;
     385             342 :     SpinLockRelease(&slot->mutex);
     386 ECB             : 
     387 CBC         342 :     if (XLogRecPtrIsInvalid(restart_lsn))
     388             336 :         ReplicationSlotReserveWal();
     389                 :     else
     390                 :     {
     391 GIC           6 :         SpinLockAcquire(&slot->mutex);
     392               6 :         slot->data.restart_lsn = restart_lsn;
     393               6 :         SpinLockRelease(&slot->mutex);
     394                 :     }
     395                 : 
     396                 :     /* ----
     397 ECB             :      * This is a bit tricky: We need to determine a safe xmin horizon to start
     398                 :      * decoding from, to avoid starting from a running xacts record referring
     399                 :      * to xids whose rows have been vacuumed or pruned
     400                 :      * already. GetOldestSafeDecodingTransactionId() returns such a value, but
     401                 :      * without further interlock its return value might immediately be out of
     402                 :      * date.
     403                 :      *
     404                 :      * So we have to acquire the ProcArrayLock to prevent computation of new
     405                 :      * xmin horizons by other backends, get the safe decoding xid, and inform
     406                 :      * the slot machinery about the new limit. Once that's done the
     407                 :      * ProcArrayLock can be released as the slot machinery now is
     408                 :      * protecting against vacuum.
     409                 :      *
     410                 :      * Note that, temporarily, the data, not just the catalog, xmin has to be
     411                 :      * reserved if a data snapshot is to be exported.  Otherwise the initial
     412                 :      * data snapshot created here is not guaranteed to be valid. After that
     413                 :      * the data xmin doesn't need to be managed anymore and the global xmin
     414                 :      * should be recomputed. As we are fine with losing the pegged data xmin
     415                 :      * after crash - no chance a snapshot would get exported anymore - we can
     416                 :      * get away with just setting the slot's
     417                 :      * effective_xmin. ReplicationSlotRelease will reset it again.
     418                 :      *
     419                 :      * ----
     420                 :      */
     421 GIC         342 :     LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
     422                 : 
     423             342 :     xmin_horizon = GetOldestSafeDecodingTransactionId(!need_full_snapshot);
     424                 : 
     425             342 :     SpinLockAcquire(&slot->mutex);
     426             342 :     slot->effective_catalog_xmin = xmin_horizon;
     427             342 :     slot->data.catalog_xmin = xmin_horizon;
     428             342 :     if (need_full_snapshot)
     429             155 :         slot->effective_xmin = xmin_horizon;
     430             342 :     SpinLockRelease(&slot->mutex);
     431                 : 
     432             342 :     ReplicationSlotsComputeRequiredXmin(true);
     433                 : 
     434             342 :     LWLockRelease(ProcArrayLock);
     435                 : 
     436 CBC         342 :     ReplicationSlotMarkDirty();
     437 GIC         342 :     ReplicationSlotSave();
     438 ECB             : 
     439 GIC         342 :     ctx = StartupDecodingContext(NIL, restart_lsn, xmin_horizon,
     440 ECB             :                                  need_full_snapshot, false,
     441                 :                                  xl_routine, prepare_write, do_write,
     442                 :                                  update_progress);
     443                 : 
     444                 :     /* call output plugin initialization callback */
     445 CBC         341 :     old_context = MemoryContextSwitchTo(ctx->context);
     446 GIC         341 :     if (ctx->callbacks.startup_cb != NULL)
     447 CBC         341 :         startup_cb_wrapper(ctx, &ctx->options, true);
     448 GIC         341 :     MemoryContextSwitchTo(old_context);
     449 ECB             : 
     450                 :     /*
     451                 :      * We allow decoding of prepared transactions when the two_phase is
     452                 :      * enabled at the time of slot creation, or when the two_phase option is
     453                 :      * given at the streaming start, provided the plugin supports all the
     454                 :      * callbacks for two-phase.
     455                 :      */
     456 GIC         341 :     ctx->twophase &= slot->data.two_phase;
     457                 : 
     458             341 :     ctx->reorder->output_rewrites = ctx->options.receive_rewrites;
     459                 : 
     460 CBC         341 :     return ctx;
     461 ECB             : }
     462                 : 
     463                 : /*
     464                 :  * Create a new decoding context, for a logical slot that has previously been
     465                 :  * used already.
     466                 :  *
     467                 :  * start_lsn
     468                 :  *      The LSN at which to start decoding.  If InvalidXLogRecPtr, restart
     469                 :  *      from the slot's confirmed_flush; otherwise, start from the specified
     470                 :  *      location (but move it forwards to confirmed_flush if it's older than
     471                 :  *      that, see below).
     472                 :  *
     473                 :  * output_plugin_options
     474                 :  *      options passed to the output plugin.
     475                 :  *
     476                 :  * fast_forward
     477                 :  *      bypass the generation of logical changes.
     478                 :  *
     479                 :  * xl_routine
     480                 :  *      XLogReaderRoutine used by underlying xlogreader
     481                 :  *
     482                 :  * prepare_write, do_write, update_progress
     483                 :  *      callbacks that have to be filled to perform the use-case dependent,
     484                 :  *      actual work.
     485                 :  *
     486                 :  * Needs to be called while in a memory context that's at least as long lived
     487                 :  * as the decoding context because further memory contexts will be created
     488                 :  * inside it.
     489                 :  *
     490                 :  * Returns an initialized decoding context after calling the output plugin's
     491                 :  * startup function.
     492                 :  */
     493                 : LogicalDecodingContext *
     494 GIC         488 : CreateDecodingContext(XLogRecPtr start_lsn,
     495                 :                       List *output_plugin_options,
     496                 :                       bool fast_forward,
     497                 :                       XLogReaderRoutine *xl_routine,
     498                 :                       LogicalOutputPluginWriterPrepareWrite prepare_write,
     499                 :                       LogicalOutputPluginWriterWrite do_write,
     500                 :                       LogicalOutputPluginWriterUpdateProgress update_progress)
     501                 : {
     502                 :     LogicalDecodingContext *ctx;
     503                 :     ReplicationSlot *slot;
     504                 :     MemoryContext old_context;
     505                 : 
     506                 :     /* shorter lines... */
     507             488 :     slot = MyReplicationSlot;
     508                 : 
     509 ECB             :     /* first some sanity checks that are unlikely to be violated */
     510 GIC         488 :     if (slot == NULL)
     511 UIC           0 :         elog(ERROR, "cannot perform logical decoding without an acquired slot");
     512                 : 
     513                 :     /* make sure the passed slot is suitable, these are user facing errors */
     514 GIC         488 :     if (SlotIsPhysical(slot))
     515               1 :         ereport(ERROR,
     516                 :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
     517                 :                  errmsg("cannot use physical replication slot for logical decoding")));
     518                 : 
     519             487 :     if (slot->data.database != MyDatabaseId)
     520               2 :         ereport(ERROR,
     521                 :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
     522 ECB             :                  errmsg("replication slot \"%s\" was not created in this database",
     523                 :                         NameStr(slot->data.name))));
     524                 : 
     525                 :     /*
     526                 :      * Check if slot has been invalidated due to max_slot_wal_keep_size. Avoid
     527                 :      * "cannot get changes" wording in this errmsg because that'd be
     528                 :      * confusingly ambiguous about no changes being available when called from
     529                 :      * pg_logical_slot_get_changes_guts().
     530                 :      */
     531 GNC         485 :     if (MyReplicationSlot->data.invalidated == RS_INVAL_WAL_REMOVED)
     532 UNC           0 :         ereport(ERROR,
     533                 :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
     534                 :                  errmsg("can no longer get changes from replication slot \"%s\"",
     535                 :                         NameStr(MyReplicationSlot->data.name)),
     536                 :                  errdetail("This slot has been invalidated because it exceeded the maximum reserved size.")));
     537                 : 
     538 GNC         485 :     if (MyReplicationSlot->data.invalidated != RS_INVAL_NONE)
     539               5 :         ereport(ERROR,
     540                 :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
     541                 :                  errmsg("can no longer get changes from replication slot \"%s\"",
     542                 :                         NameStr(MyReplicationSlot->data.name)),
     543                 :                  errdetail("This slot has been invalidated because it was conflicting with recovery.")));
     544                 : 
     545             480 :     Assert(MyReplicationSlot->data.invalidated == RS_INVAL_NONE);
     546             480 :     Assert(MyReplicationSlot->data.restart_lsn != InvalidXLogRecPtr);
     547                 : 
     548 CBC         480 :     if (start_lsn == InvalidXLogRecPtr)
     549 EUB             :     {
     550                 :         /* continue from last position */
     551 GIC         279 :         start_lsn = slot->data.confirmed_flush;
     552 ECB             :     }
     553 CBC         201 :     else if (start_lsn < slot->data.confirmed_flush)
     554                 :     {
     555                 :         /*
     556                 :          * It might seem like we should error out in this case, but it's
     557 ECB             :          * pretty common for a client to acknowledge a LSN it doesn't have to
     558                 :          * do anything for, and thus didn't store persistently, because the
     559                 :          * xlog records didn't result in anything relevant for logical
     560                 :          * decoding. Clients have to be able to do that to support synchronous
     561                 :          * replication.
     562                 :          *
     563                 :          * Starting at a different LSN than requested might not catch certain
     564                 :          * kinds of client errors; so the client may wish to check that
     565                 :          * confirmed_flush_lsn matches its expectations.
     566                 :          */
     567 GIC          23 :         elog(LOG, "%X/%X has been already streamed, forwarding to %X/%X",
     568                 :              LSN_FORMAT_ARGS(start_lsn),
     569 ECB             :              LSN_FORMAT_ARGS(slot->data.confirmed_flush));
     570 EUB             : 
     571 GIC          23 :         start_lsn = slot->data.confirmed_flush;
     572                 :     }
     573                 : 
     574             480 :     ctx = StartupDecodingContext(output_plugin_options,
     575                 :                                  start_lsn, InvalidTransactionId, false,
     576 ECB             :                                  fast_forward, xl_routine, prepare_write,
     577                 :                                  do_write, update_progress);
     578                 : 
     579                 :     /* call output plugin initialization callback */
     580 GIC         480 :     old_context = MemoryContextSwitchTo(ctx->context);
     581             480 :     if (ctx->callbacks.startup_cb != NULL)
     582             477 :         startup_cb_wrapper(ctx, &ctx->options, false);
     583 CBC         477 :     MemoryContextSwitchTo(old_context);
     584 ECB             : 
     585                 :     /*
     586                 :      * We allow decoding of prepared transactions when the two_phase is
     587                 :      * enabled at the time of slot creation, or when the two_phase option is
     588                 :      * given at the streaming start, provided the plugin supports all the
     589                 :      * callbacks for two-phase.
     590                 :      */
     591 CBC         477 :     ctx->twophase &= (slot->data.two_phase || ctx->twophase_opt_given);
     592                 : 
     593                 :     /* Mark slot to allow two_phase decoding if not already marked */
     594 GIC         477 :     if (ctx->twophase && !slot->data.two_phase)
     595                 :     {
     596               4 :         SpinLockAcquire(&slot->mutex);
     597               4 :         slot->data.two_phase = true;
     598               4 :         slot->data.two_phase_at = start_lsn;
     599               4 :         SpinLockRelease(&slot->mutex);
     600               4 :         ReplicationSlotMarkDirty();
     601               4 :         ReplicationSlotSave();
     602               4 :         SnapBuildSetTwoPhaseAt(ctx->snapshot_builder, start_lsn);
     603                 :     }
     604                 : 
     605 CBC         477 :     ctx->reorder->output_rewrites = ctx->options.receive_rewrites;
     606                 : 
     607 GIC         477 :     ereport(LOG,
     608                 :             (errmsg("starting logical decoding for slot \"%s\"",
     609 ECB             :                     NameStr(slot->data.name)),
     610                 :              errdetail("Streaming transactions committing after %X/%X, reading WAL from %X/%X.",
     611                 :                        LSN_FORMAT_ARGS(slot->data.confirmed_flush),
     612                 :                        LSN_FORMAT_ARGS(slot->data.restart_lsn))));
     613                 : 
     614 GIC         477 :     return ctx;
     615                 : }
     616                 : 
     617                 : /*
     618 ECB             :  * Returns true if a consistent initial decoding snapshot has been built.
     619                 :  */
     620                 : bool
     621 CBC         415 : DecodingContextReady(LogicalDecodingContext *ctx)
     622                 : {
     623 GIC         415 :     return SnapBuildCurrentState(ctx->snapshot_builder) == SNAPBUILD_CONSISTENT;
     624                 : }
     625                 : 
     626                 : /*
     627                 :  * Read from the decoding slot, until it is ready to start extracting changes.
     628                 :  */
     629 ECB             : void
     630 GIC         335 : DecodingContextFindStartpoint(LogicalDecodingContext *ctx)
     631                 : {
     632 CBC         335 :     ReplicationSlot *slot = ctx->slot;
     633                 : 
     634 ECB             :     /* Initialize from where to start reading WAL. */
     635 CBC         335 :     XLogBeginRead(ctx->reader, slot->data.restart_lsn);
     636 ECB             : 
     637 CBC         335 :     elog(DEBUG1, "searching for logical decoding starting point, starting at %X/%X",
     638 ECB             :          LSN_FORMAT_ARGS(slot->data.restart_lsn));
     639                 : 
     640                 :     /* Wait for a consistent starting point */
     641                 :     for (;;)
     642 GIC          82 :     {
     643 ECB             :         XLogRecord *record;
     644 GIC         417 :         char       *err = NULL;
     645 ECB             : 
     646                 :         /* the read_page callback waits for new WAL */
     647 GIC         417 :         record = XLogReadRecord(ctx->reader, &err);
     648             417 :         if (err)
     649 UIC           0 :             elog(ERROR, "could not find logical decoding starting point: %s", err);
     650 GIC         417 :         if (!record)
     651 UIC           0 :             elog(ERROR, "could not find logical decoding starting point");
     652 ECB             : 
     653 GIC         417 :         LogicalDecodingProcessRecord(ctx, ctx->reader);
     654                 : 
     655                 :         /* only continue till we found a consistent spot */
     656             415 :         if (DecodingContextReady(ctx))
     657             333 :             break;
     658                 : 
     659 CBC          82 :         CHECK_FOR_INTERRUPTS();
     660                 :     }
     661 ECB             : 
     662 GIC         333 :     SpinLockAcquire(&slot->mutex);
     663             333 :     slot->data.confirmed_flush = ctx->reader->EndRecPtr;
     664             333 :     if (slot->data.two_phase)
     665               5 :         slot->data.two_phase_at = ctx->reader->EndRecPtr;
     666             333 :     SpinLockRelease(&slot->mutex);
     667             333 : }
     668 ECB             : 
     669                 : /*
     670                 :  * Free a previously allocated decoding context, invoking the shutdown
     671                 :  * callback if necessary.
     672                 :  */
     673                 : void
     674 GIC         679 : FreeDecodingContext(LogicalDecodingContext *ctx)
     675 ECB             : {
     676 GIC         679 :     if (ctx->callbacks.shutdown_cb != NULL)
     677             676 :         shutdown_cb_wrapper(ctx);
     678                 : 
     679             679 :     ReorderBufferFree(ctx->reorder);
     680 CBC         679 :     FreeSnapshotBuilder(ctx->snapshot_builder);
     681 GIC         679 :     XLogReaderFree(ctx->reader);
     682 CBC         679 :     MemoryContextDelete(ctx->context);
     683 GIC         679 : }
     684                 : 
     685 ECB             : /*
     686                 :  * Prepare a write using the context's output routine.
     687 EUB             :  */
     688 ECB             : void
     689 GBC      335457 : OutputPluginPrepareWrite(struct LogicalDecodingContext *ctx, bool last_write)
     690                 : {
     691 CBC      335457 :     if (!ctx->accept_writes)
     692 UIC           0 :         elog(ERROR, "writes are only accepted in commit, begin and change callbacks");
     693                 : 
     694 CBC      335457 :     ctx->prepare_write(ctx, ctx->write_location, ctx->write_xid, last_write);
     695          335457 :     ctx->prepared_write = true;
     696 GIC      335457 : }
     697 ECB             : 
     698                 : /*
     699                 :  * Perform a write using the context's output routine.
     700                 :  */
     701                 : void
     702 CBC      335457 : OutputPluginWrite(struct LogicalDecodingContext *ctx, bool last_write)
     703 ECB             : {
     704 CBC      335457 :     if (!ctx->prepared_write)
     705 LBC           0 :         elog(ERROR, "OutputPluginPrepareWrite needs to be called before OutputPluginWrite");
     706                 : 
     707 GIC      335457 :     ctx->write(ctx, ctx->write_location, ctx->write_xid, last_write);
     708          335451 :     ctx->prepared_write = false;
     709          335451 : }
     710                 : 
     711                 : /*
     712 ECB             :  * Update progress tracking (if supported).
     713                 :  */
     714                 : void
     715 CBC        3791 : OutputPluginUpdateProgress(struct LogicalDecodingContext *ctx,
     716                 :                            bool skipped_xact)
     717 ECB             : {
     718 CBC        3791 :     if (!ctx->update_progress)
     719            1576 :         return;
     720 ECB             : 
     721 CBC        2215 :     ctx->update_progress(ctx, ctx->write_location, ctx->write_xid,
     722                 :                          skipped_xact);
     723                 : }
     724                 : 
     725                 : /*
     726                 :  * Load the output plugin, lookup its output plugin init function, and check
     727 ECB             :  * that it provides the required callbacks.
     728                 :  */
     729                 : static void
     730 GBC         819 : LoadOutputPlugin(OutputPluginCallbacks *callbacks, const char *plugin)
     731                 : {
     732 ECB             :     LogicalOutputPluginInit plugin_init;
     733                 : 
     734 CBC         818 :     plugin_init = (LogicalOutputPluginInit)
     735 GIC         819 :         load_external_function(plugin, "_PG_output_plugin_init", false, NULL);
     736                 : 
     737             818 :     if (plugin_init == NULL)
     738 UIC           0 :         elog(ERROR, "output plugins have to declare the _PG_output_plugin_init symbol");
     739                 : 
     740 ECB             :     /* ask the output plugin to fill the callback struct */
     741 GIC         818 :     plugin_init(callbacks);
     742 ECB             : 
     743 GBC         818 :     if (callbacks->begin_cb == NULL)
     744 UIC           0 :         elog(ERROR, "output plugins have to register a begin callback");
     745 CBC         818 :     if (callbacks->change_cb == NULL)
     746 LBC           0 :         elog(ERROR, "output plugins have to register a change callback");
     747 CBC         818 :     if (callbacks->commit_cb == NULL)
     748 UIC           0 :         elog(ERROR, "output plugins have to register a commit callback");
     749 GIC         818 : }
     750                 : 
     751                 : static void
     752              13 : output_plugin_error_callback(void *arg)
     753 ECB             : {
     754 GIC          13 :     LogicalErrorCallbackState *state = (LogicalErrorCallbackState *) arg;
     755                 : 
     756 ECB             :     /* not all callbacks have an associated LSN  */
     757 CBC          13 :     if (state->report_location != InvalidXLogRecPtr)
     758 GIC          10 :         errcontext("slot \"%s\", output plugin \"%s\", in the %s callback, associated LSN %X/%X",
     759 CBC          10 :                    NameStr(state->ctx->slot->data.name),
     760 GIC          10 :                    NameStr(state->ctx->slot->data.plugin),
     761                 :                    state->callback_name,
     762              10 :                    LSN_FORMAT_ARGS(state->report_location));
     763                 :     else
     764               3 :         errcontext("slot \"%s\", output plugin \"%s\", in the %s callback",
     765               3 :                    NameStr(state->ctx->slot->data.name),
     766               3 :                    NameStr(state->ctx->slot->data.plugin),
     767                 :                    state->callback_name);
     768 CBC          13 : }
     769                 : 
     770                 : static void
     771 GIC         818 : startup_cb_wrapper(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
     772 ECB             : {
     773                 :     LogicalErrorCallbackState state;
     774                 :     ErrorContextCallback errcallback;
     775                 : 
     776 GBC         818 :     Assert(!ctx->fast_forward);
     777                 : 
     778                 :     /* Push callback + info on the error context stack */
     779 CBC         818 :     state.ctx = ctx;
     780 GIC         818 :     state.callback_name = "startup";
     781 CBC         818 :     state.report_location = InvalidXLogRecPtr;
     782 GBC         818 :     errcallback.callback = output_plugin_error_callback;
     783 CBC         818 :     errcallback.arg = (void *) &state;
     784 GBC         818 :     errcallback.previous = error_context_stack;
     785 CBC         818 :     error_context_stack = &errcallback;
     786 EUB             : 
     787 ECB             :     /* set output state */
     788 GIC         818 :     ctx->accept_writes = false;
     789             818 :     ctx->end_xact = false;
     790 ECB             : 
     791                 :     /* do the actual work: call callback */
     792 CBC         818 :     ctx->callbacks.startup_cb(ctx, opt, is_init);
     793                 : 
     794                 :     /* Pop the error context stack */
     795             815 :     error_context_stack = errcallback.previous;
     796             815 : }
     797 ECB             : 
     798                 : static void
     799 GIC         676 : shutdown_cb_wrapper(LogicalDecodingContext *ctx)
     800 ECB             : {
     801                 :     LogicalErrorCallbackState state;
     802                 :     ErrorContextCallback errcallback;
     803                 : 
     804 CBC         676 :     Assert(!ctx->fast_forward);
     805                 : 
     806 ECB             :     /* Push callback + info on the error context stack */
     807 GIC         676 :     state.ctx = ctx;
     808             676 :     state.callback_name = "shutdown";
     809 CBC         676 :     state.report_location = InvalidXLogRecPtr;
     810 GIC         676 :     errcallback.callback = output_plugin_error_callback;
     811             676 :     errcallback.arg = (void *) &state;
     812             676 :     errcallback.previous = error_context_stack;
     813             676 :     error_context_stack = &errcallback;
     814 ECB             : 
     815                 :     /* set output state */
     816 GIC         676 :     ctx->accept_writes = false;
     817 CBC         676 :     ctx->end_xact = false;
     818 ECB             : 
     819                 :     /* do the actual work: call callback */
     820 CBC         676 :     ctx->callbacks.shutdown_cb(ctx);
     821 ECB             : 
     822                 :     /* Pop the error context stack */
     823 CBC         676 :     error_context_stack = errcallback.previous;
     824 GIC         676 : }
     825                 : 
     826 ECB             : 
     827                 : /*
     828                 :  * Callbacks for ReorderBuffer which add in some more information and then call
     829                 :  * output_plugin.h plugins.
     830                 :  */
     831                 : static void
     832 GIC        1015 : begin_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
     833 ECB             : {
     834 CBC        1015 :     LogicalDecodingContext *ctx = cache->private_data;
     835                 :     LogicalErrorCallbackState state;
     836                 :     ErrorContextCallback errcallback;
     837 ECB             : 
     838 GIC        1015 :     Assert(!ctx->fast_forward);
     839                 : 
     840                 :     /* Push callback + info on the error context stack */
     841            1015 :     state.ctx = ctx;
     842 CBC        1015 :     state.callback_name = "begin";
     843 GIC        1015 :     state.report_location = txn->first_lsn;
     844            1015 :     errcallback.callback = output_plugin_error_callback;
     845 CBC        1015 :     errcallback.arg = (void *) &state;
     846            1015 :     errcallback.previous = error_context_stack;
     847            1015 :     error_context_stack = &errcallback;
     848 ECB             : 
     849                 :     /* set output state */
     850 CBC        1015 :     ctx->accept_writes = true;
     851            1015 :     ctx->write_xid = txn->xid;
     852 GIC        1015 :     ctx->write_location = txn->first_lsn;
     853            1015 :     ctx->end_xact = false;
     854 ECB             : 
     855                 :     /* do the actual work: call callback */
     856 GIC        1015 :     ctx->callbacks.begin_cb(ctx, txn);
     857                 : 
     858 ECB             :     /* Pop the error context stack */
     859 GIC        1015 :     error_context_stack = errcallback.previous;
     860            1015 : }
     861 ECB             : 
     862                 : static void
     863 GIC        1011 : commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
     864                 :                   XLogRecPtr commit_lsn)
     865                 : {
     866            1011 :     LogicalDecodingContext *ctx = cache->private_data;
     867                 :     LogicalErrorCallbackState state;
     868                 :     ErrorContextCallback errcallback;
     869                 : 
     870 CBC        1011 :     Assert(!ctx->fast_forward);
     871                 : 
     872 ECB             :     /* Push callback + info on the error context stack */
     873 GIC        1011 :     state.ctx = ctx;
     874            1011 :     state.callback_name = "commit";
     875            1011 :     state.report_location = txn->final_lsn; /* beginning of commit record */
     876 CBC        1011 :     errcallback.callback = output_plugin_error_callback;
     877 GIC        1011 :     errcallback.arg = (void *) &state;
     878            1011 :     errcallback.previous = error_context_stack;
     879 CBC        1011 :     error_context_stack = &errcallback;
     880 ECB             : 
     881                 :     /* set output state */
     882 CBC        1011 :     ctx->accept_writes = true;
     883            1011 :     ctx->write_xid = txn->xid;
     884            1011 :     ctx->write_location = txn->end_lsn; /* points to the end of the record */
     885            1011 :     ctx->end_xact = true;
     886                 : 
     887                 :     /* do the actual work: call callback */
     888            1011 :     ctx->callbacks.commit_cb(ctx, txn, commit_lsn);
     889 ECB             : 
     890                 :     /* Pop the error context stack */
     891 CBC        1006 :     error_context_stack = errcallback.previous;
     892 GIC        1006 : }
     893                 : 
     894 ECB             : /*
     895                 :  * The functionality of begin_prepare is quite similar to begin with the
     896                 :  * exception that this will have gid (global transaction id) information which
     897                 :  * can be used by plugin. Now, we thought about extending the existing begin
     898                 :  * but that would break the replication protocol and additionally this looks
     899                 :  * cleaner.
     900                 :  */
     901                 : static void
     902 GIC          25 : begin_prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
     903                 : {
     904 CBC          25 :     LogicalDecodingContext *ctx = cache->private_data;
     905                 :     LogicalErrorCallbackState state;
     906                 :     ErrorContextCallback errcallback;
     907                 : 
     908              25 :     Assert(!ctx->fast_forward);
     909                 : 
     910                 :     /* We're only supposed to call this when two-phase commits are supported */
     911              25 :     Assert(ctx->twophase);
     912 ECB             : 
     913                 :     /* Push callback + info on the error context stack */
     914 CBC          25 :     state.ctx = ctx;
     915              25 :     state.callback_name = "begin_prepare";
     916              25 :     state.report_location = txn->first_lsn;
     917              25 :     errcallback.callback = output_plugin_error_callback;
     918 GIC          25 :     errcallback.arg = (void *) &state;
     919              25 :     errcallback.previous = error_context_stack;
     920 CBC          25 :     error_context_stack = &errcallback;
     921 ECB             : 
     922                 :     /* set output state */
     923 CBC          25 :     ctx->accept_writes = true;
     924 GIC          25 :     ctx->write_xid = txn->xid;
     925              25 :     ctx->write_location = txn->first_lsn;
     926 CBC          25 :     ctx->end_xact = false;
     927                 : 
     928                 :     /*
     929 ECB             :      * If the plugin supports two-phase commits then begin prepare callback is
     930                 :      * mandatory
     931                 :      */
     932 GIC          25 :     if (ctx->callbacks.begin_prepare_cb == NULL)
     933 UIC           0 :         ereport(ERROR,
     934                 :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
     935                 :                  errmsg("logical replication at prepare time requires a %s callback",
     936                 :                         "begin_prepare_cb")));
     937                 : 
     938                 :     /* do the actual work: call callback */
     939 GIC          25 :     ctx->callbacks.begin_prepare_cb(ctx, txn);
     940 ECB             : 
     941                 :     /* Pop the error context stack */
     942 CBC          25 :     error_context_stack = errcallback.previous;
     943 GIC          25 : }
     944                 : 
     945                 : static void
     946 CBC          25 : prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
     947                 :                    XLogRecPtr prepare_lsn)
     948                 : {
     949              25 :     LogicalDecodingContext *ctx = cache->private_data;
     950                 :     LogicalErrorCallbackState state;
     951                 :     ErrorContextCallback errcallback;
     952 ECB             : 
     953 CBC          25 :     Assert(!ctx->fast_forward);
     954 ECB             : 
     955                 :     /* We're only supposed to call this when two-phase commits are supported */
     956 CBC          25 :     Assert(ctx->twophase);
     957 ECB             : 
     958                 :     /* Push callback + info on the error context stack */
     959 GIC          25 :     state.ctx = ctx;
     960              25 :     state.callback_name = "prepare";
     961 CBC          25 :     state.report_location = txn->final_lsn; /* beginning of prepare record */
     962              25 :     errcallback.callback = output_plugin_error_callback;
     963              25 :     errcallback.arg = (void *) &state;
     964              25 :     errcallback.previous = error_context_stack;
     965 GIC          25 :     error_context_stack = &errcallback;
     966                 : 
     967                 :     /* set output state */
     968              25 :     ctx->accept_writes = true;
     969              25 :     ctx->write_xid = txn->xid;
     970 CBC          25 :     ctx->write_location = txn->end_lsn; /* points to the end of the record */
     971 GBC          25 :     ctx->end_xact = true;
     972                 : 
     973                 :     /*
     974                 :      * If the plugin supports two-phase commits then prepare callback is
     975                 :      * mandatory
     976                 :      */
     977 CBC          25 :     if (ctx->callbacks.prepare_cb == NULL)
     978 UIC           0 :         ereport(ERROR,
     979                 :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
     980 ECB             :                  errmsg("logical replication at prepare time requires a %s callback",
     981                 :                         "prepare_cb")));
     982                 : 
     983                 :     /* do the actual work: call callback */
     984 CBC          25 :     ctx->callbacks.prepare_cb(ctx, txn, prepare_lsn);
     985                 : 
     986                 :     /* Pop the error context stack */
     987              25 :     error_context_stack = errcallback.previous;
     988 GIC          25 : }
     989                 : 
     990                 : static void
     991 CBC          29 : commit_prepared_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
     992                 :                            XLogRecPtr commit_lsn)
     993                 : {
     994              29 :     LogicalDecodingContext *ctx = cache->private_data;
     995                 :     LogicalErrorCallbackState state;
     996                 :     ErrorContextCallback errcallback;
     997 ECB             : 
     998 CBC          29 :     Assert(!ctx->fast_forward);
     999 ECB             : 
    1000                 :     /* We're only supposed to call this when two-phase commits are supported */
    1001 CBC          29 :     Assert(ctx->twophase);
    1002 ECB             : 
    1003                 :     /* Push callback + info on the error context stack */
    1004 GIC          29 :     state.ctx = ctx;
    1005              29 :     state.callback_name = "commit_prepared";
    1006 CBC          29 :     state.report_location = txn->final_lsn; /* beginning of commit record */
    1007              29 :     errcallback.callback = output_plugin_error_callback;
    1008              29 :     errcallback.arg = (void *) &state;
    1009              29 :     errcallback.previous = error_context_stack;
    1010 GIC          29 :     error_context_stack = &errcallback;
    1011                 : 
    1012                 :     /* set output state */
    1013              29 :     ctx->accept_writes = true;
    1014              29 :     ctx->write_xid = txn->xid;
    1015 CBC          29 :     ctx->write_location = txn->end_lsn; /* points to the end of the record */
    1016 GBC          29 :     ctx->end_xact = true;
    1017                 : 
    1018                 :     /*
    1019                 :      * If the plugin support two-phase commits then commit prepared callback
    1020                 :      * is mandatory
    1021                 :      */
    1022 CBC          29 :     if (ctx->callbacks.commit_prepared_cb == NULL)
    1023 UIC           0 :         ereport(ERROR,
    1024                 :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
    1025 ECB             :                  errmsg("logical replication at prepare time requires a %s callback",
    1026                 :                         "commit_prepared_cb")));
    1027                 : 
    1028                 :     /* do the actual work: call callback */
    1029 CBC          29 :     ctx->callbacks.commit_prepared_cb(ctx, txn, commit_lsn);
    1030                 : 
    1031                 :     /* Pop the error context stack */
    1032              29 :     error_context_stack = errcallback.previous;
    1033 GIC          29 : }
    1034                 : 
    1035                 : static void
    1036 CBC           9 : rollback_prepared_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
    1037                 :                              XLogRecPtr prepare_end_lsn,
    1038                 :                              TimestampTz prepare_time)
    1039 ECB             : {
    1040 GIC           9 :     LogicalDecodingContext *ctx = cache->private_data;
    1041                 :     LogicalErrorCallbackState state;
    1042 ECB             :     ErrorContextCallback errcallback;
    1043                 : 
    1044 CBC           9 :     Assert(!ctx->fast_forward);
    1045 ECB             : 
    1046                 :     /* We're only supposed to call this when two-phase commits are supported */
    1047 CBC           9 :     Assert(ctx->twophase);
    1048 ECB             : 
    1049                 :     /* Push callback + info on the error context stack */
    1050 GIC           9 :     state.ctx = ctx;
    1051 CBC           9 :     state.callback_name = "rollback_prepared";
    1052               9 :     state.report_location = txn->final_lsn; /* beginning of commit record */
    1053               9 :     errcallback.callback = output_plugin_error_callback;
    1054               9 :     errcallback.arg = (void *) &state;
    1055 GIC           9 :     errcallback.previous = error_context_stack;
    1056               9 :     error_context_stack = &errcallback;
    1057                 : 
    1058                 :     /* set output state */
    1059               9 :     ctx->accept_writes = true;
    1060 CBC           9 :     ctx->write_xid = txn->xid;
    1061 GBC           9 :     ctx->write_location = txn->end_lsn; /* points to the end of the record */
    1062 GIC           9 :     ctx->end_xact = true;
    1063                 : 
    1064                 :     /*
    1065                 :      * If the plugin support two-phase commits then rollback prepared callback
    1066                 :      * is mandatory
    1067 ECB             :      */
    1068 GIC           9 :     if (ctx->callbacks.rollback_prepared_cb == NULL)
    1069 UIC           0 :         ereport(ERROR,
    1070 ECB             :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
    1071                 :                  errmsg("logical replication at prepare time requires a %s callback",
    1072                 :                         "rollback_prepared_cb")));
    1073                 : 
    1074                 :     /* do the actual work: call callback */
    1075 GIC           9 :     ctx->callbacks.rollback_prepared_cb(ctx, txn, prepare_end_lsn,
    1076                 :                                         prepare_time);
    1077                 : 
    1078 ECB             :     /* Pop the error context stack */
    1079 GIC           9 :     error_context_stack = errcallback.previous;
    1080               9 : }
    1081                 : 
    1082 ECB             : static void
    1083 GIC      157703 : change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
    1084                 :                   Relation relation, ReorderBufferChange *change)
    1085 ECB             : {
    1086 GIC      157703 :     LogicalDecodingContext *ctx = cache->private_data;
    1087                 :     LogicalErrorCallbackState state;
    1088 ECB             :     ErrorContextCallback errcallback;
    1089                 : 
    1090 CBC      157703 :     Assert(!ctx->fast_forward);
    1091 ECB             : 
    1092                 :     /* Push callback + info on the error context stack */
    1093 CBC      157703 :     state.ctx = ctx;
    1094          157703 :     state.callback_name = "change";
    1095 GIC      157703 :     state.report_location = change->lsn;
    1096          157703 :     errcallback.callback = output_plugin_error_callback;
    1097 CBC      157703 :     errcallback.arg = (void *) &state;
    1098          157703 :     errcallback.previous = error_context_stack;
    1099          157703 :     error_context_stack = &errcallback;
    1100 ECB             : 
    1101                 :     /* set output state */
    1102 GIC      157703 :     ctx->accept_writes = true;
    1103          157703 :     ctx->write_xid = txn->xid;
    1104                 : 
    1105                 :     /*
    1106 ECB             :      * Report this change's lsn so replies from clients can give an up-to-date
    1107 EUB             :      * answer. This won't ever be enough (and shouldn't be!) to confirm
    1108                 :      * receipt of this transaction, but it might allow another transaction's
    1109                 :      * commit to be confirmed with one message.
    1110                 :      */
    1111 GIC      157703 :     ctx->write_location = change->lsn;
    1112                 : 
    1113 CBC      157703 :     ctx->end_xact = false;
    1114                 : 
    1115 GIC      157703 :     ctx->callbacks.change_cb(ctx, txn, relation, change);
    1116                 : 
    1117 ECB             :     /* Pop the error context stack */
    1118 CBC      157699 :     error_context_stack = errcallback.previous;
    1119 GIC      157699 : }
    1120                 : 
    1121 ECB             : static void
    1122 GIC          17 : truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
    1123                 :                     int nrelations, Relation relations[], ReorderBufferChange *change)
    1124 ECB             : {
    1125 GIC          17 :     LogicalDecodingContext *ctx = cache->private_data;
    1126                 :     LogicalErrorCallbackState state;
    1127                 :     ErrorContextCallback errcallback;
    1128 ECB             : 
    1129 GIC          17 :     Assert(!ctx->fast_forward);
    1130                 : 
    1131 CBC          17 :     if (!ctx->callbacks.truncate_cb)
    1132 LBC           0 :         return;
    1133 ECB             : 
    1134                 :     /* Push callback + info on the error context stack */
    1135 CBC          17 :     state.ctx = ctx;
    1136              17 :     state.callback_name = "truncate";
    1137              17 :     state.report_location = change->lsn;
    1138 GIC          17 :     errcallback.callback = output_plugin_error_callback;
    1139              17 :     errcallback.arg = (void *) &state;
    1140 CBC          17 :     errcallback.previous = error_context_stack;
    1141              17 :     error_context_stack = &errcallback;
    1142                 : 
    1143                 :     /* set output state */
    1144 GIC          17 :     ctx->accept_writes = true;
    1145              17 :     ctx->write_xid = txn->xid;
    1146                 : 
    1147                 :     /*
    1148                 :      * Report this change's lsn so replies from clients can give an up-to-date
    1149 ECB             :      * answer. This won't ever be enough (and shouldn't be!) to confirm
    1150                 :      * receipt of this transaction, but it might allow another transaction's
    1151                 :      * commit to be confirmed with one message.
    1152                 :      */
    1153 CBC          17 :     ctx->write_location = change->lsn;
    1154                 : 
    1155 GIC          17 :     ctx->end_xact = false;
    1156 ECB             : 
    1157 CBC          17 :     ctx->callbacks.truncate_cb(ctx, txn, nrelations, relations, change);
    1158                 : 
    1159                 :     /* Pop the error context stack */
    1160              17 :     error_context_stack = errcallback.previous;
    1161                 : }
    1162                 : 
    1163 ECB             : bool
    1164 GIC         117 : filter_prepare_cb_wrapper(LogicalDecodingContext *ctx, TransactionId xid,
    1165                 :                           const char *gid)
    1166                 : {
    1167 ECB             :     LogicalErrorCallbackState state;
    1168                 :     ErrorContextCallback errcallback;
    1169                 :     bool        ret;
    1170 EUB             : 
    1171 GIC         117 :     Assert(!ctx->fast_forward);
    1172                 : 
    1173 ECB             :     /* Push callback + info on the error context stack */
    1174 CBC         117 :     state.ctx = ctx;
    1175             117 :     state.callback_name = "filter_prepare";
    1176             117 :     state.report_location = InvalidXLogRecPtr;
    1177             117 :     errcallback.callback = output_plugin_error_callback;
    1178             117 :     errcallback.arg = (void *) &state;
    1179             117 :     errcallback.previous = error_context_stack;
    1180 GIC         117 :     error_context_stack = &errcallback;
    1181                 : 
    1182 ECB             :     /* set output state */
    1183 CBC         117 :     ctx->accept_writes = false;
    1184 GIC         117 :     ctx->end_xact = false;
    1185                 : 
    1186                 :     /* do the actual work: call callback */
    1187             117 :     ret = ctx->callbacks.filter_prepare_cb(ctx, xid, gid);
    1188                 : 
    1189                 :     /* Pop the error context stack */
    1190             117 :     error_context_stack = errcallback.previous;
    1191 ECB             : 
    1192 GIC         117 :     return ret;
    1193 ECB             : }
    1194                 : 
    1195                 : bool
    1196 GIC     1685576 : filter_by_origin_cb_wrapper(LogicalDecodingContext *ctx, RepOriginId origin_id)
    1197                 : {
    1198 ECB             :     LogicalErrorCallbackState state;
    1199                 :     ErrorContextCallback errcallback;
    1200                 :     bool        ret;
    1201                 : 
    1202 CBC     1685576 :     Assert(!ctx->fast_forward);
    1203                 : 
    1204                 :     /* Push callback + info on the error context stack */
    1205 GIC     1685576 :     state.ctx = ctx;
    1206         1685576 :     state.callback_name = "filter_by_origin";
    1207         1685576 :     state.report_location = InvalidXLogRecPtr;
    1208         1685576 :     errcallback.callback = output_plugin_error_callback;
    1209 CBC     1685576 :     errcallback.arg = (void *) &state;
    1210 GIC     1685576 :     errcallback.previous = error_context_stack;
    1211         1685576 :     error_context_stack = &errcallback;
    1212 ECB             : 
    1213                 :     /* set output state */
    1214 CBC     1685576 :     ctx->accept_writes = false;
    1215         1685576 :     ctx->end_xact = false;
    1216 ECB             : 
    1217                 :     /* do the actual work: call callback */
    1218 CBC     1685576 :     ret = ctx->callbacks.filter_by_origin_cb(ctx, origin_id);
    1219                 : 
    1220                 :     /* Pop the error context stack */
    1221         1685576 :     error_context_stack = errcallback.previous;
    1222 ECB             : 
    1223 GIC     1685576 :     return ret;
    1224                 : }
    1225 ECB             : 
    1226                 : static void
    1227 GIC          14 : message_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
    1228 ECB             :                    XLogRecPtr message_lsn, bool transactional,
    1229                 :                    const char *prefix, Size message_size, const char *message)
    1230                 : {
    1231 GIC          14 :     LogicalDecodingContext *ctx = cache->private_data;
    1232                 :     LogicalErrorCallbackState state;
    1233                 :     ErrorContextCallback errcallback;
    1234 ECB             : 
    1235 GIC          14 :     Assert(!ctx->fast_forward);
    1236                 : 
    1237              14 :     if (ctx->callbacks.message_cb == NULL)
    1238 UIC           0 :         return;
    1239                 : 
    1240 ECB             :     /* Push callback + info on the error context stack */
    1241 GIC          14 :     state.ctx = ctx;
    1242              14 :     state.callback_name = "message";
    1243 CBC          14 :     state.report_location = message_lsn;
    1244              14 :     errcallback.callback = output_plugin_error_callback;
    1245              14 :     errcallback.arg = (void *) &state;
    1246              14 :     errcallback.previous = error_context_stack;
    1247              14 :     error_context_stack = &errcallback;
    1248 ECB             : 
    1249                 :     /* set output state */
    1250 GIC          14 :     ctx->accept_writes = true;
    1251              14 :     ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
    1252 CBC          14 :     ctx->write_location = message_lsn;
    1253              14 :     ctx->end_xact = false;
    1254                 : 
    1255                 :     /* do the actual work: call callback */
    1256              14 :     ctx->callbacks.message_cb(ctx, txn, message_lsn, transactional, prefix,
    1257                 :                               message_size, message);
    1258                 : 
    1259 ECB             :     /* Pop the error context stack */
    1260 GIC          14 :     error_context_stack = errcallback.previous;
    1261 ECB             : }
    1262                 : 
    1263                 : static void
    1264 GIC         626 : stream_start_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
    1265 ECB             :                         XLogRecPtr first_lsn)
    1266                 : {
    1267 GIC         626 :     LogicalDecodingContext *ctx = cache->private_data;
    1268                 :     LogicalErrorCallbackState state;
    1269 ECB             :     ErrorContextCallback errcallback;
    1270                 : 
    1271 GIC         626 :     Assert(!ctx->fast_forward);
    1272                 : 
    1273 ECB             :     /* We're only supposed to call this when streaming is supported. */
    1274 GIC         626 :     Assert(ctx->streaming);
    1275 ECB             : 
    1276 EUB             :     /* Push callback + info on the error context stack */
    1277 GIC         626 :     state.ctx = ctx;
    1278             626 :     state.callback_name = "stream_start";
    1279 CBC         626 :     state.report_location = first_lsn;
    1280             626 :     errcallback.callback = output_plugin_error_callback;
    1281             626 :     errcallback.arg = (void *) &state;
    1282             626 :     errcallback.previous = error_context_stack;
    1283             626 :     error_context_stack = &errcallback;
    1284 ECB             : 
    1285                 :     /* set output state */
    1286 GIC         626 :     ctx->accept_writes = true;
    1287             626 :     ctx->write_xid = txn->xid;
    1288 ECB             : 
    1289                 :     /*
    1290                 :      * Report this message's lsn so replies from clients can give an
    1291                 :      * up-to-date answer. This won't ever be enough (and shouldn't be!) to
    1292                 :      * confirm receipt of this transaction, but it might allow another
    1293                 :      * transaction's commit to be confirmed with one message.
    1294                 :      */
    1295 GIC         626 :     ctx->write_location = first_lsn;
    1296                 : 
    1297             626 :     ctx->end_xact = false;
    1298 ECB             : 
    1299                 :     /* in streaming mode, stream_start_cb is required */
    1300 GIC         626 :     if (ctx->callbacks.stream_start_cb == NULL)
    1301 UIC           0 :         ereport(ERROR,
    1302 ECB             :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
    1303                 :                  errmsg("logical streaming requires a %s callback",
    1304                 :                         "stream_start_cb")));
    1305                 : 
    1306 GIC         626 :     ctx->callbacks.stream_start_cb(ctx, txn);
    1307                 : 
    1308                 :     /* Pop the error context stack */
    1309 CBC         626 :     error_context_stack = errcallback.previous;
    1310 GIC         626 : }
    1311                 : 
    1312 ECB             : static void
    1313 GIC         626 : stream_stop_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
    1314                 :                        XLogRecPtr last_lsn)
    1315 ECB             : {
    1316 CBC         626 :     LogicalDecodingContext *ctx = cache->private_data;
    1317 ECB             :     LogicalErrorCallbackState state;
    1318                 :     ErrorContextCallback errcallback;
    1319                 : 
    1320 CBC         626 :     Assert(!ctx->fast_forward);
    1321 ECB             : 
    1322                 :     /* We're only supposed to call this when streaming is supported. */
    1323 GIC         626 :     Assert(ctx->streaming);
    1324 ECB             : 
    1325                 :     /* Push callback + info on the error context stack */
    1326 GIC         626 :     state.ctx = ctx;
    1327             626 :     state.callback_name = "stream_stop";
    1328             626 :     state.report_location = last_lsn;
    1329             626 :     errcallback.callback = output_plugin_error_callback;
    1330             626 :     errcallback.arg = (void *) &state;
    1331             626 :     errcallback.previous = error_context_stack;
    1332             626 :     error_context_stack = &errcallback;
    1333 ECB             : 
    1334                 :     /* set output state */
    1335 CBC         626 :     ctx->accept_writes = true;
    1336 GIC         626 :     ctx->write_xid = txn->xid;
    1337                 : 
    1338 ECB             :     /*
    1339 EUB             :      * Report this message's lsn so replies from clients can give an
    1340                 :      * up-to-date answer. This won't ever be enough (and shouldn't be!) to
    1341                 :      * confirm receipt of this transaction, but it might allow another
    1342                 :      * transaction's commit to be confirmed with one message.
    1343                 :      */
    1344 CBC         626 :     ctx->write_location = last_lsn;
    1345                 : 
    1346 GIC         626 :     ctx->end_xact = false;
    1347 ECB             : 
    1348                 :     /* in streaming mode, stream_stop_cb is required */
    1349 GIC         626 :     if (ctx->callbacks.stream_stop_cb == NULL)
    1350 UIC           0 :         ereport(ERROR,
    1351 ECB             :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
    1352                 :                  errmsg("logical streaming requires a %s callback",
    1353                 :                         "stream_stop_cb")));
    1354                 : 
    1355 GIC         626 :     ctx->callbacks.stream_stop_cb(ctx, txn);
    1356                 : 
    1357                 :     /* Pop the error context stack */
    1358 CBC         626 :     error_context_stack = errcallback.previous;
    1359 GIC         626 : }
    1360                 : 
    1361 ECB             : static void
    1362 GIC          29 : stream_abort_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
    1363                 :                         XLogRecPtr abort_lsn)
    1364 ECB             : {
    1365 CBC          29 :     LogicalDecodingContext *ctx = cache->private_data;
    1366 ECB             :     LogicalErrorCallbackState state;
    1367                 :     ErrorContextCallback errcallback;
    1368                 : 
    1369 CBC          29 :     Assert(!ctx->fast_forward);
    1370 ECB             : 
    1371                 :     /* We're only supposed to call this when streaming is supported. */
    1372 GIC          29 :     Assert(ctx->streaming);
    1373 ECB             : 
    1374                 :     /* Push callback + info on the error context stack */
    1375 GIC          29 :     state.ctx = ctx;
    1376              29 :     state.callback_name = "stream_abort";
    1377              29 :     state.report_location = abort_lsn;
    1378              29 :     errcallback.callback = output_plugin_error_callback;
    1379              29 :     errcallback.arg = (void *) &state;
    1380              29 :     errcallback.previous = error_context_stack;
    1381              29 :     error_context_stack = &errcallback;
    1382 ECB             : 
    1383                 :     /* set output state */
    1384 CBC          29 :     ctx->accept_writes = true;
    1385 GIC          29 :     ctx->write_xid = txn->xid;
    1386              29 :     ctx->write_location = abort_lsn;
    1387 CBC          29 :     ctx->end_xact = true;
    1388 EUB             : 
    1389                 :     /* in streaming mode, stream_abort_cb is required */
    1390 GIC          29 :     if (ctx->callbacks.stream_abort_cb == NULL)
    1391 UIC           0 :         ereport(ERROR,
    1392                 :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
    1393 ECB             :                  errmsg("logical streaming requires a %s callback",
    1394                 :                         "stream_abort_cb")));
    1395                 : 
    1396 CBC          29 :     ctx->callbacks.stream_abort_cb(ctx, txn, abort_lsn);
    1397 ECB             : 
    1398                 :     /* Pop the error context stack */
    1399 GIC          29 :     error_context_stack = errcallback.previous;
    1400 CBC          29 : }
    1401                 : 
    1402                 : static void
    1403              12 : stream_prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
    1404                 :                           XLogRecPtr prepare_lsn)
    1405                 : {
    1406 GIC          12 :     LogicalDecodingContext *ctx = cache->private_data;
    1407 ECB             :     LogicalErrorCallbackState state;
    1408                 :     ErrorContextCallback errcallback;
    1409                 : 
    1410 CBC          12 :     Assert(!ctx->fast_forward);
    1411                 : 
    1412                 :     /*
    1413 ECB             :      * We're only supposed to call this when streaming and two-phase commits
    1414                 :      * are supported.
    1415                 :      */
    1416 CBC          12 :     Assert(ctx->streaming);
    1417              12 :     Assert(ctx->twophase);
    1418 ECB             : 
    1419                 :     /* Push callback + info on the error context stack */
    1420 GIC          12 :     state.ctx = ctx;
    1421              12 :     state.callback_name = "stream_prepare";
    1422 CBC          12 :     state.report_location = txn->final_lsn;
    1423              12 :     errcallback.callback = output_plugin_error_callback;
    1424              12 :     errcallback.arg = (void *) &state;
    1425              12 :     errcallback.previous = error_context_stack;
    1426 GIC          12 :     error_context_stack = &errcallback;
    1427                 : 
    1428 ECB             :     /* set output state */
    1429 GBC          12 :     ctx->accept_writes = true;
    1430 GIC          12 :     ctx->write_xid = txn->xid;
    1431              12 :     ctx->write_location = txn->end_lsn;
    1432              12 :     ctx->end_xact = true;
    1433                 : 
    1434 ECB             :     /* in streaming mode with two-phase commits, stream_prepare_cb is required */
    1435 GIC          12 :     if (ctx->callbacks.stream_prepare_cb == NULL)
    1436 UIC           0 :         ereport(ERROR,
    1437 ECB             :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
    1438                 :                  errmsg("logical streaming at prepare time requires a %s callback",
    1439                 :                         "stream_prepare_cb")));
    1440                 : 
    1441 CBC          12 :     ctx->callbacks.stream_prepare_cb(ctx, txn, prepare_lsn);
    1442                 : 
    1443                 :     /* Pop the error context stack */
    1444              12 :     error_context_stack = errcallback.previous;
    1445 GIC          12 : }
    1446                 : 
    1447                 : static void
    1448 CBC          49 : stream_commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
    1449                 :                          XLogRecPtr commit_lsn)
    1450                 : {
    1451 GIC          49 :     LogicalDecodingContext *ctx = cache->private_data;
    1452                 :     LogicalErrorCallbackState state;
    1453                 :     ErrorContextCallback errcallback;
    1454 ECB             : 
    1455 CBC          49 :     Assert(!ctx->fast_forward);
    1456                 : 
    1457                 :     /* We're only supposed to call this when streaming is supported. */
    1458              49 :     Assert(ctx->streaming);
    1459 ECB             : 
    1460                 :     /* Push callback + info on the error context stack */
    1461 CBC          49 :     state.ctx = ctx;
    1462              49 :     state.callback_name = "stream_commit";
    1463              49 :     state.report_location = txn->final_lsn;
    1464              49 :     errcallback.callback = output_plugin_error_callback;
    1465 GIC          49 :     errcallback.arg = (void *) &state;
    1466              49 :     errcallback.previous = error_context_stack;
    1467 CBC          49 :     error_context_stack = &errcallback;
    1468 ECB             : 
    1469                 :     /* set output state */
    1470 CBC          49 :     ctx->accept_writes = true;
    1471 GIC          49 :     ctx->write_xid = txn->xid;
    1472              49 :     ctx->write_location = txn->end_lsn;
    1473 CBC          49 :     ctx->end_xact = true;
    1474 EUB             : 
    1475                 :     /* in streaming mode, stream_commit_cb is required */
    1476 GIC          49 :     if (ctx->callbacks.stream_commit_cb == NULL)
    1477 UIC           0 :         ereport(ERROR,
    1478                 :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
    1479 ECB             :                  errmsg("logical streaming requires a %s callback",
    1480                 :                         "stream_commit_cb")));
    1481                 : 
    1482 CBC          49 :     ctx->callbacks.stream_commit_cb(ctx, txn, commit_lsn);
    1483 ECB             : 
    1484                 :     /* Pop the error context stack */
    1485 GIC          49 :     error_context_stack = errcallback.previous;
    1486 CBC          49 : }
    1487                 : 
    1488                 : static void
    1489          175984 : stream_change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
    1490                 :                          Relation relation, ReorderBufferChange *change)
    1491                 : {
    1492 GIC      175984 :     LogicalDecodingContext *ctx = cache->private_data;
    1493 ECB             :     LogicalErrorCallbackState state;
    1494                 :     ErrorContextCallback errcallback;
    1495                 : 
    1496 CBC      175984 :     Assert(!ctx->fast_forward);
    1497                 : 
    1498                 :     /* We're only supposed to call this when streaming is supported. */
    1499          175984 :     Assert(ctx->streaming);
    1500 ECB             : 
    1501                 :     /* Push callback + info on the error context stack */
    1502 CBC      175984 :     state.ctx = ctx;
    1503          175984 :     state.callback_name = "stream_change";
    1504          175984 :     state.report_location = change->lsn;
    1505          175984 :     errcallback.callback = output_plugin_error_callback;
    1506 GIC      175984 :     errcallback.arg = (void *) &state;
    1507          175984 :     errcallback.previous = error_context_stack;
    1508 CBC      175984 :     error_context_stack = &errcallback;
    1509 ECB             : 
    1510                 :     /* set output state */
    1511 CBC      175984 :     ctx->accept_writes = true;
    1512 GIC      175984 :     ctx->write_xid = txn->xid;
    1513                 : 
    1514 ECB             :     /*
    1515 EUB             :      * Report this change's lsn so replies from clients can give an up-to-date
    1516                 :      * answer. This won't ever be enough (and shouldn't be!) to confirm
    1517                 :      * receipt of this transaction, but it might allow another transaction's
    1518                 :      * commit to be confirmed with one message.
    1519                 :      */
    1520 CBC      175984 :     ctx->write_location = change->lsn;
    1521                 : 
    1522 GIC      175984 :     ctx->end_xact = false;
    1523 ECB             : 
    1524                 :     /* in streaming mode, stream_change_cb is required */
    1525 GIC      175984 :     if (ctx->callbacks.stream_change_cb == NULL)
    1526 UIC           0 :         ereport(ERROR,
    1527 ECB             :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
    1528                 :                  errmsg("logical streaming requires a %s callback",
    1529                 :                         "stream_change_cb")));
    1530                 : 
    1531 GIC      175984 :     ctx->callbacks.stream_change_cb(ctx, txn, relation, change);
    1532                 : 
    1533                 :     /* Pop the error context stack */
    1534 CBC      175984 :     error_context_stack = errcallback.previous;
    1535 GIC      175984 : }
    1536                 : 
    1537 ECB             : static void
    1538 GIC           3 : stream_message_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
    1539                 :                           XLogRecPtr message_lsn, bool transactional,
    1540 ECB             :                           const char *prefix, Size message_size, const char *message)
    1541                 : {
    1542 CBC           3 :     LogicalDecodingContext *ctx = cache->private_data;
    1543 ECB             :     LogicalErrorCallbackState state;
    1544                 :     ErrorContextCallback errcallback;
    1545                 : 
    1546 CBC           3 :     Assert(!ctx->fast_forward);
    1547                 : 
    1548                 :     /* We're only supposed to call this when streaming is supported. */
    1549               3 :     Assert(ctx->streaming);
    1550 ECB             : 
    1551                 :     /* this callback is optional */
    1552 GIC           3 :     if (ctx->callbacks.stream_message_cb == NULL)
    1553 UIC           0 :         return;
    1554                 : 
    1555                 :     /* Push callback + info on the error context stack */
    1556 GIC           3 :     state.ctx = ctx;
    1557               3 :     state.callback_name = "stream_message";
    1558 CBC           3 :     state.report_location = message_lsn;
    1559 GIC           3 :     errcallback.callback = output_plugin_error_callback;
    1560 CBC           3 :     errcallback.arg = (void *) &state;
    1561 GIC           3 :     errcallback.previous = error_context_stack;
    1562               3 :     error_context_stack = &errcallback;
    1563 ECB             : 
    1564 EUB             :     /* set output state */
    1565 GIC           3 :     ctx->accept_writes = true;
    1566               3 :     ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
    1567               3 :     ctx->write_location = message_lsn;
    1568               3 :     ctx->end_xact = false;
    1569 ECB             : 
    1570                 :     /* do the actual work: call callback */
    1571 GIC           3 :     ctx->callbacks.stream_message_cb(ctx, txn, message_lsn, transactional, prefix,
    1572 ECB             :                                      message_size, message);
    1573                 : 
    1574                 :     /* Pop the error context stack */
    1575 GIC           3 :     error_context_stack = errcallback.previous;
    1576 ECB             : }
    1577                 : 
    1578                 : static void
    1579 UIC           0 : stream_truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
    1580 ECB             :                            int nrelations, Relation relations[],
    1581                 :                            ReorderBufferChange *change)
    1582                 : {
    1583 UIC           0 :     LogicalDecodingContext *ctx = cache->private_data;
    1584 ECB             :     LogicalErrorCallbackState state;
    1585                 :     ErrorContextCallback errcallback;
    1586                 : 
    1587 LBC           0 :     Assert(!ctx->fast_forward);
    1588                 : 
    1589                 :     /* We're only supposed to call this when streaming is supported. */
    1590               0 :     Assert(ctx->streaming);
    1591 EUB             : 
    1592                 :     /* this callback is optional */
    1593 UIC           0 :     if (!ctx->callbacks.stream_truncate_cb)
    1594 LBC           0 :         return;
    1595 ECB             : 
    1596                 :     /* Push callback + info on the error context stack */
    1597 LBC           0 :     state.ctx = ctx;
    1598               0 :     state.callback_name = "stream_truncate";
    1599               0 :     state.report_location = change->lsn;
    1600               0 :     errcallback.callback = output_plugin_error_callback;
    1601 UIC           0 :     errcallback.arg = (void *) &state;
    1602               0 :     errcallback.previous = error_context_stack;
    1603 LBC           0 :     error_context_stack = &errcallback;
    1604 ECB             : 
    1605                 :     /* set output state */
    1606 LBC           0 :     ctx->accept_writes = true;
    1607 UIC           0 :     ctx->write_xid = txn->xid;
    1608                 : 
    1609 ECB             :     /*
    1610                 :      * Report this change's lsn so replies from clients can give an up-to-date
    1611                 :      * answer. This won't ever be enough (and shouldn't be!) to confirm
    1612                 :      * receipt of this transaction, but it might allow another transaction's
    1613                 :      * commit to be confirmed with one message.
    1614                 :      */
    1615 UIC           0 :     ctx->write_location = change->lsn;
    1616                 : 
    1617 UBC           0 :     ctx->end_xact = false;
    1618                 : 
    1619 UIC           0 :     ctx->callbacks.stream_truncate_cb(ctx, txn, nrelations, relations, change);
    1620                 : 
    1621 EUB             :     /* Pop the error context stack */
    1622 UIC           0 :     error_context_stack = errcallback.previous;
    1623                 : }
    1624                 : 
    1625                 : static void
    1626 GNC        3097 : update_progress_txn_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
    1627                 :                                XLogRecPtr lsn)
    1628                 : {
    1629            3097 :     LogicalDecodingContext *ctx = cache->private_data;
    1630                 :     LogicalErrorCallbackState state;
    1631                 :     ErrorContextCallback errcallback;
    1632                 : 
    1633            3097 :     Assert(!ctx->fast_forward);
    1634                 : 
    1635                 :     /* Push callback + info on the error context stack */
    1636            3097 :     state.ctx = ctx;
    1637            3097 :     state.callback_name = "update_progress_txn";
    1638            3097 :     state.report_location = lsn;
    1639            3097 :     errcallback.callback = output_plugin_error_callback;
    1640            3097 :     errcallback.arg = (void *) &state;
    1641            3097 :     errcallback.previous = error_context_stack;
    1642            3097 :     error_context_stack = &errcallback;
    1643                 : 
    1644                 :     /* set output state */
    1645            3097 :     ctx->accept_writes = false;
    1646            3097 :     ctx->write_xid = txn->xid;
    1647                 : 
    1648                 :     /*
    1649                 :      * Report this change's lsn so replies from clients can give an up-to-date
    1650                 :      * answer. This won't ever be enough (and shouldn't be!) to confirm
    1651                 :      * receipt of this transaction, but it might allow another transaction's
    1652                 :      * commit to be confirmed with one message.
    1653                 :      */
    1654            3097 :     ctx->write_location = lsn;
    1655                 : 
    1656            3097 :     ctx->end_xact = false;
    1657                 : 
    1658            3097 :     OutputPluginUpdateProgress(ctx, false);
    1659                 : 
    1660                 :     /* Pop the error context stack */
    1661            3097 :     error_context_stack = errcallback.previous;
    1662            3097 : }
    1663                 : 
    1664 EUB             : /*
    1665                 :  * Set the required catalog xmin horizon for historic snapshots in the current
    1666                 :  * replication slot.
    1667                 :  *
    1668                 :  * Note that in the most cases, we won't be able to immediately use the xmin
    1669                 :  * to increase the xmin horizon: we need to wait till the client has confirmed
    1670                 :  * receiving current_lsn with LogicalConfirmReceivedLocation().
    1671                 :  */
    1672                 : void
    1673 GIC         274 : LogicalIncreaseXminForSlot(XLogRecPtr current_lsn, TransactionId xmin)
    1674 EUB             : {
    1675 GBC         274 :     bool        updated_xmin = false;
    1676 EUB             :     ReplicationSlot *slot;
    1677 GBC         274 :     bool        got_new_xmin = false;
    1678 EUB             : 
    1679 GBC         274 :     slot = MyReplicationSlot;
    1680 EUB             : 
    1681 GIC         274 :     Assert(slot != NULL);
    1682                 : 
    1683 GBC         274 :     SpinLockAcquire(&slot->mutex);
    1684 EUB             : 
    1685                 :     /*
    1686                 :      * don't overwrite if we already have a newer xmin. This can happen if we
    1687                 :      * restart decoding in a slot.
    1688                 :      */
    1689 GIC         274 :     if (TransactionIdPrecedesOrEquals(xmin, slot->data.catalog_xmin))
    1690                 :     {
    1691                 :     }
    1692 EUB             : 
    1693                 :     /*
    1694                 :      * If the client has already confirmed up to this lsn, we directly can
    1695                 :      * mark this as accepted. This can happen if we restart decoding in a
    1696                 :      * slot.
    1697                 :      */
    1698 GIC          71 :     else if (current_lsn <= slot->data.confirmed_flush)
    1699 EUB             :     {
    1700 GIC          43 :         slot->candidate_catalog_xmin = xmin;
    1701              43 :         slot->candidate_xmin_lsn = current_lsn;
    1702                 : 
    1703 ECB             :         /* our candidate can directly be used */
    1704 GIC          43 :         updated_xmin = true;
    1705                 :     }
    1706 ECB             : 
    1707                 :     /*
    1708                 :      * Only increase if the previous values have been applied, otherwise we
    1709                 :      * might never end up updating if the receiver acks too slowly.
    1710                 :      */
    1711 GIC          28 :     else if (slot->candidate_xmin_lsn == InvalidXLogRecPtr)
    1712                 :     {
    1713 CBC          13 :         slot->candidate_catalog_xmin = xmin;
    1714              13 :         slot->candidate_xmin_lsn = current_lsn;
    1715 ECB             : 
    1716                 :         /*
    1717                 :          * Log new xmin at an appropriate log level after releasing the
    1718                 :          * spinlock.
    1719                 :          */
    1720 GIC          13 :         got_new_xmin = true;
    1721                 :     }
    1722 CBC         274 :     SpinLockRelease(&slot->mutex);
    1723 ECB             : 
    1724 GIC         274 :     if (got_new_xmin)
    1725              13 :         elog(DEBUG1, "got new catalog xmin %u at %X/%X", xmin,
    1726                 :              LSN_FORMAT_ARGS(current_lsn));
    1727                 : 
    1728                 :     /* candidate already valid with the current flush position, apply */
    1729             274 :     if (updated_xmin)
    1730              43 :         LogicalConfirmReceivedLocation(slot->data.confirmed_flush);
    1731 CBC         274 : }
    1732                 : 
    1733 ECB             : /*
    1734                 :  * Mark the minimal LSN (restart_lsn) we need to read to replay all
    1735                 :  * transactions that have not yet committed at current_lsn.
    1736                 :  *
    1737                 :  * Just like LogicalIncreaseXminForSlot this only takes effect when the
    1738                 :  * client has confirmed to have received current_lsn.
    1739                 :  */
    1740                 : void
    1741 GIC         234 : LogicalIncreaseRestartDecodingForSlot(XLogRecPtr current_lsn, XLogRecPtr restart_lsn)
    1742                 : {
    1743             234 :     bool        updated_lsn = false;
    1744                 :     ReplicationSlot *slot;
    1745                 : 
    1746             234 :     slot = MyReplicationSlot;
    1747                 : 
    1748             234 :     Assert(slot != NULL);
    1749             234 :     Assert(restart_lsn != InvalidXLogRecPtr);
    1750 CBC         234 :     Assert(current_lsn != InvalidXLogRecPtr);
    1751                 : 
    1752             234 :     SpinLockAcquire(&slot->mutex);
    1753                 : 
    1754 ECB             :     /* don't overwrite if have a newer restart lsn */
    1755 GIC         234 :     if (restart_lsn <= slot->data.restart_lsn)
    1756 ECB             :     {
    1757                 :     }
    1758                 : 
    1759                 :     /*
    1760                 :      * We might have already flushed far enough to directly accept this lsn,
    1761                 :      * in this case there is no need to check for existing candidate LSNs
    1762                 :      */
    1763 GIC         227 :     else if (current_lsn <= slot->data.confirmed_flush)
    1764                 :     {
    1765             187 :         slot->candidate_restart_valid = current_lsn;
    1766 CBC         187 :         slot->candidate_restart_lsn = restart_lsn;
    1767                 : 
    1768                 :         /* our candidate can directly be used */
    1769 GIC         187 :         updated_lsn = true;
    1770                 :     }
    1771                 : 
    1772                 :     /*
    1773                 :      * Only increase if the previous values have been applied, otherwise we
    1774                 :      * might never end up updating if the receiver acks too slowly. A missed
    1775 ECB             :      * value here will just cause some extra effort after reconnecting.
    1776                 :      */
    1777 CBC         234 :     if (slot->candidate_restart_valid == InvalidXLogRecPtr)
    1778 ECB             :     {
    1779 GIC          27 :         slot->candidate_restart_valid = current_lsn;
    1780              27 :         slot->candidate_restart_lsn = restart_lsn;
    1781 CBC          27 :         SpinLockRelease(&slot->mutex);
    1782                 : 
    1783 GIC          27 :         elog(DEBUG1, "got new restart lsn %X/%X at %X/%X",
    1784                 :              LSN_FORMAT_ARGS(restart_lsn),
    1785                 :              LSN_FORMAT_ARGS(current_lsn));
    1786                 :     }
    1787                 :     else
    1788 ECB             :     {
    1789                 :         XLogRecPtr  candidate_restart_lsn;
    1790                 :         XLogRecPtr  candidate_restart_valid;
    1791                 :         XLogRecPtr  confirmed_flush;
    1792                 : 
    1793 GIC         207 :         candidate_restart_lsn = slot->candidate_restart_lsn;
    1794             207 :         candidate_restart_valid = slot->candidate_restart_valid;
    1795             207 :         confirmed_flush = slot->data.confirmed_flush;
    1796             207 :         SpinLockRelease(&slot->mutex);
    1797 ECB             : 
    1798 GIC         207 :         elog(DEBUG1, "failed to increase restart lsn: proposed %X/%X, after %X/%X, current candidate %X/%X, current after %X/%X, flushed up to %X/%X",
    1799 ECB             :              LSN_FORMAT_ARGS(restart_lsn),
    1800                 :              LSN_FORMAT_ARGS(current_lsn),
    1801                 :              LSN_FORMAT_ARGS(candidate_restart_lsn),
    1802                 :              LSN_FORMAT_ARGS(candidate_restart_valid),
    1803                 :              LSN_FORMAT_ARGS(confirmed_flush));
    1804                 :     }
    1805                 : 
    1806                 :     /* candidates are already valid with the current flush position, apply */
    1807 CBC         234 :     if (updated_lsn)
    1808             187 :         LogicalConfirmReceivedLocation(slot->data.confirmed_flush);
    1809 GIC         234 : }
    1810                 : 
    1811                 : /*
    1812                 :  * Handle a consumer's confirmation having received all changes up to lsn.
    1813                 :  */
    1814                 : void
    1815           43657 : LogicalConfirmReceivedLocation(XLogRecPtr lsn)
    1816                 : {
    1817           43657 :     Assert(lsn != InvalidXLogRecPtr);
    1818 ECB             : 
    1819                 :     /* Do an unlocked check for candidate_lsn first. */
    1820 CBC       43657 :     if (MyReplicationSlot->candidate_xmin_lsn != InvalidXLogRecPtr ||
    1821 GIC       43604 :         MyReplicationSlot->candidate_restart_valid != InvalidXLogRecPtr)
    1822             256 :     {
    1823 CBC         256 :         bool        updated_xmin = false;
    1824 GIC         256 :         bool        updated_restart = false;
    1825 ECB             : 
    1826 CBC         256 :         SpinLockAcquire(&MyReplicationSlot->mutex);
    1827 ECB             : 
    1828 GIC         256 :         MyReplicationSlot->data.confirmed_flush = lsn;
    1829 ECB             : 
    1830                 :         /* if we're past the location required for bumping xmin, do so */
    1831 GIC         256 :         if (MyReplicationSlot->candidate_xmin_lsn != InvalidXLogRecPtr &&
    1832 CBC          53 :             MyReplicationSlot->candidate_xmin_lsn <= lsn)
    1833                 :         {
    1834                 :             /*
    1835                 :              * We have to write the changed xmin to disk *before* we change
    1836                 :              * the in-memory value, otherwise after a crash we wouldn't know
    1837                 :              * that some catalog tuples might have been removed already.
    1838                 :              *
    1839                 :              * Ensure that by first writing to ->xmin and only update
    1840 ECB             :              * ->effective_xmin once the new state is synced to disk. After a
    1841                 :              * crash ->effective_xmin is set to ->xmin.
    1842                 :              */
    1843 CBC          50 :             if (TransactionIdIsValid(MyReplicationSlot->candidate_catalog_xmin) &&
    1844 GIC          50 :                 MyReplicationSlot->data.catalog_xmin != MyReplicationSlot->candidate_catalog_xmin)
    1845                 :             {
    1846 CBC          50 :                 MyReplicationSlot->data.catalog_xmin = MyReplicationSlot->candidate_catalog_xmin;
    1847 GIC          50 :                 MyReplicationSlot->candidate_catalog_xmin = InvalidTransactionId;
    1848              50 :                 MyReplicationSlot->candidate_xmin_lsn = InvalidXLogRecPtr;
    1849              50 :                 updated_xmin = true;
    1850                 :             }
    1851                 :         }
    1852                 : 
    1853             256 :         if (MyReplicationSlot->candidate_restart_valid != InvalidXLogRecPtr &&
    1854 CBC         213 :             MyReplicationSlot->candidate_restart_valid <= lsn)
    1855                 :         {
    1856             210 :             Assert(MyReplicationSlot->candidate_restart_lsn != InvalidXLogRecPtr);
    1857 ECB             : 
    1858 CBC         210 :             MyReplicationSlot->data.restart_lsn = MyReplicationSlot->candidate_restart_lsn;
    1859 GIC         210 :             MyReplicationSlot->candidate_restart_lsn = InvalidXLogRecPtr;
    1860 CBC         210 :             MyReplicationSlot->candidate_restart_valid = InvalidXLogRecPtr;
    1861 GIC         210 :             updated_restart = true;
    1862                 :         }
    1863                 : 
    1864             256 :         SpinLockRelease(&MyReplicationSlot->mutex);
    1865                 : 
    1866                 :         /* first write new xmin to disk, so we know what's up after a crash */
    1867             256 :         if (updated_xmin || updated_restart)
    1868                 :         {
    1869             253 :             ReplicationSlotMarkDirty();
    1870 CBC         253 :             ReplicationSlotSave();
    1871             253 :             elog(DEBUG1, "updated xmin: %u restart: %u", updated_xmin, updated_restart);
    1872 ECB             :         }
    1873                 : 
    1874                 :         /*
    1875                 :          * Now the new xmin is safely on disk, we can let the global value
    1876                 :          * advance. We do not take ProcArrayLock or similar since we only
    1877                 :          * advance xmin here and there's not much harm done by a concurrent
    1878                 :          * computation missing that.
    1879                 :          */
    1880 GIC         256 :         if (updated_xmin)
    1881                 :         {
    1882              50 :             SpinLockAcquire(&MyReplicationSlot->mutex);
    1883              50 :             MyReplicationSlot->effective_catalog_xmin = MyReplicationSlot->data.catalog_xmin;
    1884 CBC          50 :             SpinLockRelease(&MyReplicationSlot->mutex);
    1885 ECB             : 
    1886 CBC          50 :             ReplicationSlotsComputeRequiredXmin(false);
    1887 GIC          50 :             ReplicationSlotsComputeRequiredLSN();
    1888                 :         }
    1889                 :     }
    1890                 :     else
    1891                 :     {
    1892 CBC       43401 :         SpinLockAcquire(&MyReplicationSlot->mutex);
    1893 GIC       43401 :         MyReplicationSlot->data.confirmed_flush = lsn;
    1894 CBC       43401 :         SpinLockRelease(&MyReplicationSlot->mutex);
    1895                 :     }
    1896 GIC       43657 : }
    1897 ECB             : 
    1898                 : /*
    1899                 :  * Clear logical streaming state during (sub)transaction abort.
    1900                 :  */
    1901                 : void
    1902 GIC       24599 : ResetLogicalStreamingState(void)
    1903 ECB             : {
    1904 GIC       24599 :     CheckXidAlive = InvalidTransactionId;
    1905 CBC       24599 :     bsysscan = false;
    1906 GIC       24599 : }
    1907                 : 
    1908 ECB             : /*
    1909                 :  * Report stats for a slot.
    1910                 :  */
    1911                 : void
    1912 GIC        5642 : UpdateDecodingStats(LogicalDecodingContext *ctx)
    1913                 : {
    1914            5642 :     ReorderBuffer *rb = ctx->reorder;
    1915                 :     PgStat_StatReplSlotEntry repSlotStat;
    1916                 : 
    1917                 :     /* Nothing to do if we don't have any replication stats to be sent. */
    1918            5642 :     if (rb->spillBytes <= 0 && rb->streamBytes <= 0 && rb->totalBytes <= 0)
    1919             233 :         return;
    1920 ECB             : 
    1921 CBC        5409 :     elog(DEBUG2, "UpdateDecodingStats: updating stats %p %lld %lld %lld %lld %lld %lld %lld %lld",
    1922                 :          rb,
    1923 ECB             :          (long long) rb->spillTxns,
    1924                 :          (long long) rb->spillCount,
    1925                 :          (long long) rb->spillBytes,
    1926                 :          (long long) rb->streamTxns,
    1927                 :          (long long) rb->streamCount,
    1928                 :          (long long) rb->streamBytes,
    1929                 :          (long long) rb->totalTxns,
    1930                 :          (long long) rb->totalBytes);
    1931                 : 
    1932 GIC        5409 :     repSlotStat.spill_txns = rb->spillTxns;
    1933 CBC        5409 :     repSlotStat.spill_count = rb->spillCount;
    1934 GIC        5409 :     repSlotStat.spill_bytes = rb->spillBytes;
    1935 CBC        5409 :     repSlotStat.stream_txns = rb->streamTxns;
    1936            5409 :     repSlotStat.stream_count = rb->streamCount;
    1937            5409 :     repSlotStat.stream_bytes = rb->streamBytes;
    1938            5409 :     repSlotStat.total_txns = rb->totalTxns;
    1939 GIC        5409 :     repSlotStat.total_bytes = rb->totalBytes;
    1940                 : 
    1941 CBC        5409 :     pgstat_report_replslot(ctx->slot, &repSlotStat);
    1942                 : 
    1943 GIC        5409 :     rb->spillTxns = 0;
    1944 CBC        5409 :     rb->spillCount = 0;
    1945 GIC        5409 :     rb->spillBytes = 0;
    1946 CBC        5409 :     rb->streamTxns = 0;
    1947            5409 :     rb->streamCount = 0;
    1948            5409 :     rb->streamBytes = 0;
    1949 GIC        5409 :     rb->totalTxns = 0;
    1950            5409 :     rb->totalBytes = 0;
    1951                 : }
        

Generated by: LCOV version v1.16-55-g56c0a2a