LCOV - code coverage report
Current view: top level - src/backend/replication/logical - logical.c (source / functions) Hit Total Coverage
Test: PostgreSQL 19devel Lines: 697 749 93.1 %
Date: 2025-12-28 18:17:34 Functions: 40 41 97.6 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.16