LCOV - differential code coverage report
Current view: top level - src/backend/replication/logical - logical.c (source / functions) Coverage Total Hit UNC LBC UIC UBC GBC GIC GNC CBC EUB ECB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 93.4 % 731 683 1 12 34 1 16 373 26 268 31 398 5
Current Date: 2023-04-08 17:13:01 Functions: 97.4 % 39 38 1 37 1 1 38
Baseline: 15 Line coverage date bins:
Baseline Date: 2023-04-08 15:09:40 [..60] days: 96.3 % 27 26 1 26
Legend: Lines: hit not hit (60,120] days: 100.0 % 2 2 2
(240..) days: 93.3 % 702 655 12 34 1 16 371 268 31 362
Function coverage date bins:
[..60] days: 100.0 % 1 1 1
(240..) days: 49.3 % 75 37 1 37 1 36

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

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