PostgreSQL Source Code  git master
logical.c File Reference
#include "postgres.h"
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "fmgr.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/decode.h"
#include "replication/logical.h"
#include "replication/origin.h"
#include "replication/reorderbuffer.h"
#include "replication/snapbuild.h"
#include "storage/proc.h"
#include "storage/procarray.h"
#include "utils/builtins.h"
#include "utils/memutils.h"
Include dependency graph for logical.c:

Go to the source code of this file.

Data Structures

struct  LogicalErrorCallbackState
 

Typedefs

typedef struct LogicalErrorCallbackState LogicalErrorCallbackState
 

Functions

static void output_plugin_error_callback (void *arg)
 
static void startup_cb_wrapper (LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
 
static void shutdown_cb_wrapper (LogicalDecodingContext *ctx)
 
static void begin_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn)
 
static void commit_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
 
static void begin_prepare_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn)
 
static void prepare_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
 
static void commit_prepared_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
 
static void rollback_prepared_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_end_lsn, TimestampTz prepare_time)
 
static void change_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
 
static void truncate_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
 
static void message_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr message_lsn, bool transactional, const char *prefix, Size message_size, const char *message)
 
static void stream_start_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr first_lsn)
 
static void stream_stop_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr last_lsn)
 
static void stream_abort_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr abort_lsn)
 
static void stream_prepare_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
 
static void stream_commit_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
 
static void stream_change_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
 
static void stream_message_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr message_lsn, bool transactional, const char *prefix, Size message_size, const char *message)
 
static void stream_truncate_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
 
static void update_progress_txn_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr lsn)
 
static void LoadOutputPlugin (OutputPluginCallbacks *callbacks, const char *plugin)
 
void CheckLogicalDecodingRequirements (void)
 
static LogicalDecodingContextStartupDecodingContext (List *output_plugin_options, XLogRecPtr start_lsn, TransactionId xmin_horizon, bool need_full_snapshot, bool fast_forward, XLogReaderRoutine *xl_routine, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
 
LogicalDecodingContextCreateInitDecodingContext (const char *plugin, List *output_plugin_options, bool need_full_snapshot, XLogRecPtr restart_lsn, XLogReaderRoutine *xl_routine, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
 
LogicalDecodingContextCreateDecodingContext (XLogRecPtr start_lsn, List *output_plugin_options, bool fast_forward, XLogReaderRoutine *xl_routine, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
 
bool DecodingContextReady (LogicalDecodingContext *ctx)
 
void DecodingContextFindStartpoint (LogicalDecodingContext *ctx)
 
void FreeDecodingContext (LogicalDecodingContext *ctx)
 
void OutputPluginPrepareWrite (struct LogicalDecodingContext *ctx, bool last_write)
 
void OutputPluginWrite (struct LogicalDecodingContext *ctx, bool last_write)
 
void OutputPluginUpdateProgress (struct LogicalDecodingContext *ctx, bool skipped_xact)
 
bool filter_prepare_cb_wrapper (LogicalDecodingContext *ctx, TransactionId xid, const char *gid)
 
bool filter_by_origin_cb_wrapper (LogicalDecodingContext *ctx, RepOriginId origin_id)
 
void LogicalIncreaseXminForSlot (XLogRecPtr current_lsn, TransactionId xmin)
 
void LogicalIncreaseRestartDecodingForSlot (XLogRecPtr current_lsn, XLogRecPtr restart_lsn)
 
void LogicalConfirmReceivedLocation (XLogRecPtr lsn)
 
void ResetLogicalStreamingState (void)
 
void UpdateDecodingStats (LogicalDecodingContext *ctx)
 

Typedef Documentation

◆ LogicalErrorCallbackState

Function Documentation

◆ begin_cb_wrapper()

static void begin_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn 
)
static

Definition at line 832 of file logical.c.

833 {
834  LogicalDecodingContext *ctx = cache->private_data;
836  ErrorContextCallback errcallback;
837 
838  Assert(!ctx->fast_forward);
839 
840  /* Push callback + info on the error context stack */
841  state.ctx = ctx;
842  state.callback_name = "begin";
843  state.report_location = txn->first_lsn;
845  errcallback.arg = (void *) &state;
846  errcallback.previous = error_context_stack;
847  error_context_stack = &errcallback;
848 
849  /* set output state */
850  ctx->accept_writes = true;
851  ctx->write_xid = txn->xid;
852  ctx->write_location = txn->first_lsn;
853  ctx->end_xact = false;
854 
855  /* do the actual work: call callback */
856  ctx->callbacks.begin_cb(ctx, txn);
857 
858  /* Pop the error context stack */
859  error_context_stack = errcallback.previous;
860 }
ErrorContextCallback * error_context_stack
Definition: elog.c:95
Assert(fmt[strlen(fmt) - 1] !='\n')
static void output_plugin_error_callback(void *arg)
Definition: logical.c:752
struct ErrorContextCallback * previous
Definition: elog.h:295
void(* callback)(void *arg)
Definition: elog.h:296
XLogRecPtr write_location
Definition: logical.h:108
OutputPluginCallbacks callbacks
Definition: logical.h:53
TransactionId write_xid
Definition: logical.h:109
LogicalDecodeBeginCB begin_cb
XLogRecPtr first_lsn
TransactionId xid
void * private_data
Definition: regguts.h:323

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert(), OutputPluginCallbacks::begin_cb, ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, error_context_stack, LogicalDecodingContext::fast_forward, ReorderBufferTXN::first_lsn, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ begin_prepare_cb_wrapper()

static void begin_prepare_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn 
)
static

Definition at line 902 of file logical.c.

903 {
904  LogicalDecodingContext *ctx = cache->private_data;
906  ErrorContextCallback errcallback;
907 
908  Assert(!ctx->fast_forward);
909 
910  /* We're only supposed to call this when two-phase commits are supported */
911  Assert(ctx->twophase);
912 
913  /* Push callback + info on the error context stack */
914  state.ctx = ctx;
915  state.callback_name = "begin_prepare";
916  state.report_location = txn->first_lsn;
918  errcallback.arg = (void *) &state;
919  errcallback.previous = error_context_stack;
920  error_context_stack = &errcallback;
921 
922  /* set output state */
923  ctx->accept_writes = true;
924  ctx->write_xid = txn->xid;
925  ctx->write_location = txn->first_lsn;
926  ctx->end_xact = false;
927 
928  /*
929  * If the plugin supports two-phase commits then begin prepare callback is
930  * mandatory
931  */
932  if (ctx->callbacks.begin_prepare_cb == NULL)
933  ereport(ERROR,
934  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
935  errmsg("logical replication at prepare time requires a %s callback",
936  "begin_prepare_cb")));
937 
938  /* do the actual work: call callback */
939  ctx->callbacks.begin_prepare_cb(ctx, txn);
940 
941  /* Pop the error context stack */
942  error_context_stack = errcallback.previous;
943 }
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
LogicalDecodeBeginPrepareCB begin_prepare_cb

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert(), OutputPluginCallbacks::begin_prepare_cb, ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, ereport, errcode(), errmsg(), ERROR, error_context_stack, LogicalDecodingContext::fast_forward, ReorderBufferTXN::first_lsn, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, LogicalDecodingContext::twophase, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ change_cb_wrapper()

static void change_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn,
Relation  relation,
ReorderBufferChange change 
)
static

Definition at line 1083 of file logical.c.

1085 {
1086  LogicalDecodingContext *ctx = cache->private_data;
1088  ErrorContextCallback errcallback;
1089 
1090  Assert(!ctx->fast_forward);
1091 
1092  /* Push callback + info on the error context stack */
1093  state.ctx = ctx;
1094  state.callback_name = "change";
1095  state.report_location = change->lsn;
1096  errcallback.callback = output_plugin_error_callback;
1097  errcallback.arg = (void *) &state;
1098  errcallback.previous = error_context_stack;
1099  error_context_stack = &errcallback;
1100 
1101  /* set output state */
1102  ctx->accept_writes = true;
1103  ctx->write_xid = txn->xid;
1104 
1105  /*
1106  * Report this change's lsn so replies from clients can give an up-to-date
1107  * answer. This won't ever be enough (and shouldn't be!) to confirm
1108  * receipt of this transaction, but it might allow another transaction's
1109  * commit to be confirmed with one message.
1110  */
1111  ctx->write_location = change->lsn;
1112 
1113  ctx->end_xact = false;
1114 
1115  ctx->callbacks.change_cb(ctx, txn, relation, change);
1116 
1117  /* Pop the error context stack */
1118  error_context_stack = errcallback.previous;
1119 }
LogicalDecodeChangeCB change_cb

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert(), ErrorContextCallback::callback, LogicalDecodingContext::callbacks, OutputPluginCallbacks::change_cb, LogicalDecodingContext::end_xact, error_context_stack, LogicalDecodingContext::fast_forward, ReorderBufferChange::lsn, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ CheckLogicalDecodingRequirements()

void CheckLogicalDecodingRequirements ( void  )

Definition at line 108 of file logical.c.

109 {
111 
112  /*
113  * NB: Adding a new requirement likely means that RestoreSlotFromDisk()
114  * needs the same check.
115  */
116 
118  ereport(ERROR,
119  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
120  errmsg("logical decoding requires wal_level >= logical")));
121 
122  if (MyDatabaseId == InvalidOid)
123  ereport(ERROR,
124  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
125  errmsg("logical decoding requires a database connection")));
126 
127  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  */
138  ereport(ERROR,
139  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
140  errmsg("logical decoding on standby requires wal_level >= logical on the primary")));
141  }
142 }
Oid MyDatabaseId
Definition: globals.c:89
#define InvalidOid
Definition: postgres_ext.h:36
void CheckSlotRequirements(void)
Definition: slot.c:1136
bool RecoveryInProgress(void)
Definition: xlog.c:5948
int wal_level
Definition: xlog.c:134
WalLevel GetActiveWalLevelOnStandby(void)
Definition: xlog.c:4501
@ WAL_LEVEL_LOGICAL
Definition: xlog.h:71

References CheckSlotRequirements(), ereport, errcode(), errmsg(), ERROR, GetActiveWalLevelOnStandby(), InvalidOid, MyDatabaseId, RecoveryInProgress(), wal_level, and WAL_LEVEL_LOGICAL.

Referenced by copy_replication_slot(), CreateInitDecodingContext(), CreateReplicationSlot(), pg_create_logical_replication_slot(), pg_logical_slot_get_changes_guts(), and StartLogicalReplication().

◆ commit_cb_wrapper()

static void commit_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn,
XLogRecPtr  commit_lsn 
)
static

Definition at line 863 of file logical.c.

865 {
866  LogicalDecodingContext *ctx = cache->private_data;
868  ErrorContextCallback errcallback;
869 
870  Assert(!ctx->fast_forward);
871 
872  /* Push callback + info on the error context stack */
873  state.ctx = ctx;
874  state.callback_name = "commit";
875  state.report_location = txn->final_lsn; /* beginning of commit record */
877  errcallback.arg = (void *) &state;
878  errcallback.previous = error_context_stack;
879  error_context_stack = &errcallback;
880 
881  /* set output state */
882  ctx->accept_writes = true;
883  ctx->write_xid = txn->xid;
884  ctx->write_location = txn->end_lsn; /* points to the end of the record */
885  ctx->end_xact = true;
886 
887  /* do the actual work: call callback */
888  ctx->callbacks.commit_cb(ctx, txn, commit_lsn);
889 
890  /* Pop the error context stack */
891  error_context_stack = errcallback.previous;
892 }
LogicalDecodeCommitCB commit_cb
XLogRecPtr final_lsn
XLogRecPtr end_lsn

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert(), ErrorContextCallback::callback, LogicalDecodingContext::callbacks, OutputPluginCallbacks::commit_cb, ReorderBufferTXN::end_lsn, LogicalDecodingContext::end_xact, error_context_stack, LogicalDecodingContext::fast_forward, ReorderBufferTXN::final_lsn, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ commit_prepared_cb_wrapper()

static void commit_prepared_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn,
XLogRecPtr  commit_lsn 
)
static

Definition at line 991 of file logical.c.

993 {
994  LogicalDecodingContext *ctx = cache->private_data;
996  ErrorContextCallback errcallback;
997 
998  Assert(!ctx->fast_forward);
999 
1000  /* We're only supposed to call this when two-phase commits are supported */
1001  Assert(ctx->twophase);
1002 
1003  /* Push callback + info on the error context stack */
1004  state.ctx = ctx;
1005  state.callback_name = "commit_prepared";
1006  state.report_location = txn->final_lsn; /* beginning of commit record */
1007  errcallback.callback = output_plugin_error_callback;
1008  errcallback.arg = (void *) &state;
1009  errcallback.previous = error_context_stack;
1010  error_context_stack = &errcallback;
1011 
1012  /* set output state */
1013  ctx->accept_writes = true;
1014  ctx->write_xid = txn->xid;
1015  ctx->write_location = txn->end_lsn; /* points to the end of the record */
1016  ctx->end_xact = true;
1017 
1018  /*
1019  * If the plugin support two-phase commits then commit prepared callback
1020  * is mandatory
1021  */
1022  if (ctx->callbacks.commit_prepared_cb == NULL)
1023  ereport(ERROR,
1024  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1025  errmsg("logical replication at prepare time requires a %s callback",
1026  "commit_prepared_cb")));
1027 
1028  /* do the actual work: call callback */
1029  ctx->callbacks.commit_prepared_cb(ctx, txn, commit_lsn);
1030 
1031  /* Pop the error context stack */
1032  error_context_stack = errcallback.previous;
1033 }
LogicalDecodeCommitPreparedCB commit_prepared_cb

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert(), ErrorContextCallback::callback, LogicalDecodingContext::callbacks, OutputPluginCallbacks::commit_prepared_cb, ReorderBufferTXN::end_lsn, LogicalDecodingContext::end_xact, ereport, errcode(), errmsg(), ERROR, error_context_stack, LogicalDecodingContext::fast_forward, ReorderBufferTXN::final_lsn, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, LogicalDecodingContext::twophase, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ CreateDecodingContext()

LogicalDecodingContext* CreateDecodingContext ( XLogRecPtr  start_lsn,
List output_plugin_options,
bool  fast_forward,
XLogReaderRoutine xl_routine,
LogicalOutputPluginWriterPrepareWrite  prepare_write,
LogicalOutputPluginWriterWrite  do_write,
LogicalOutputPluginWriterUpdateProgress  update_progress 
)

Definition at line 494 of file logical.c.

501 {
503  ReplicationSlot *slot;
504  MemoryContext old_context;
505 
506  /* shorter lines... */
507  slot = MyReplicationSlot;
508 
509  /* first some sanity checks that are unlikely to be violated */
510  if (slot == NULL)
511  elog(ERROR, "cannot perform logical decoding without an acquired slot");
512 
513  /* make sure the passed slot is suitable, these are user facing errors */
514  if (SlotIsPhysical(slot))
515  ereport(ERROR,
516  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
517  errmsg("cannot use physical replication slot for logical decoding")));
518 
519  if (slot->data.database != MyDatabaseId)
520  ereport(ERROR,
521  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
522  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  */
532  ereport(ERROR,
533  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
534  errmsg("can no longer get changes from replication slot \"%s\"",
536  errdetail("This slot has been invalidated because it exceeded the maximum reserved size.")));
537 
539  ereport(ERROR,
540  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
541  errmsg("can no longer get changes from replication slot \"%s\"",
543  errdetail("This slot has been invalidated because it was conflicting with recovery.")));
544 
547 
548  if (start_lsn == InvalidXLogRecPtr)
549  {
550  /* continue from last position */
551  start_lsn = slot->data.confirmed_flush;
552  }
553  else if (start_lsn < slot->data.confirmed_flush)
554  {
555  /*
556  * It might seem like we should error out in this case, but it's
557  * pretty common for a client to acknowledge a LSN it doesn't have to
558  * do anything for, and thus didn't store persistently, because the
559  * xlog records didn't result in anything relevant for logical
560  * decoding. Clients have to be able to do that to support synchronous
561  * replication.
562  *
563  * Starting at a different LSN than requested might not catch certain
564  * kinds of client errors; so the client may wish to check that
565  * confirmed_flush_lsn matches its expectations.
566  */
567  elog(LOG, "%X/%X has been already streamed, forwarding to %X/%X",
568  LSN_FORMAT_ARGS(start_lsn),
570 
571  start_lsn = slot->data.confirmed_flush;
572  }
573 
574  ctx = StartupDecodingContext(output_plugin_options,
575  start_lsn, InvalidTransactionId, false,
576  fast_forward, xl_routine, prepare_write,
577  do_write, update_progress);
578 
579  /* call output plugin initialization callback */
580  old_context = MemoryContextSwitchTo(ctx->context);
581  if (ctx->callbacks.startup_cb != NULL)
582  startup_cb_wrapper(ctx, &ctx->options, false);
583  MemoryContextSwitchTo(old_context);
584 
585  /*
586  * We allow decoding of prepared transactions when the two_phase is
587  * enabled at the time of slot creation, or when the two_phase option is
588  * given at the streaming start, provided the plugin supports all the
589  * callbacks for two-phase.
590  */
591  ctx->twophase &= (slot->data.two_phase || ctx->twophase_opt_given);
592 
593  /* Mark slot to allow two_phase decoding if not already marked */
594  if (ctx->twophase && !slot->data.two_phase)
595  {
596  SpinLockAcquire(&slot->mutex);
597  slot->data.two_phase = true;
598  slot->data.two_phase_at = start_lsn;
599  SpinLockRelease(&slot->mutex);
602  SnapBuildSetTwoPhaseAt(ctx->snapshot_builder, start_lsn);
603  }
604 
606 
607  ereport(LOG,
608  (errmsg("starting logical decoding for slot \"%s\"",
609  NameStr(slot->data.name)),
610  errdetail("Streaming transactions committing after %X/%X, reading WAL from %X/%X.",
612  LSN_FORMAT_ARGS(slot->data.restart_lsn))));
613 
614  return ctx;
615 }
#define NameStr(name)
Definition: c.h:735
int errdetail(const char *fmt,...)
Definition: elog.c:1202
#define LOG
Definition: elog.h:31
static LogicalDecodingContext * StartupDecodingContext(List *output_plugin_options, XLogRecPtr start_lsn, TransactionId xmin_horizon, bool need_full_snapshot, bool fast_forward, XLogReaderRoutine *xl_routine, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
Definition: logical.c:149
static void startup_cb_wrapper(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
Definition: logical.c:771
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:138
const void * data
void ReplicationSlotMarkDirty(void)
Definition: slot.c:798
ReplicationSlot * MyReplicationSlot
Definition: slot.c:99
void ReplicationSlotSave(void)
Definition: slot.c:780
#define SlotIsPhysical(slot)
Definition: slot.h:190
@ RS_INVAL_WAL_REMOVED
Definition: slot.h:48
@ RS_INVAL_NONE
Definition: slot.h:46
void SnapBuildSetTwoPhaseAt(SnapBuild *builder, XLogRecPtr ptr)
Definition: snapbuild.c:425
#define SpinLockRelease(lock)
Definition: spin.h:64
#define SpinLockAcquire(lock)
Definition: spin.h:62
OutputPluginOptions options
Definition: logical.h:54
MemoryContext context
Definition: logical.h:36
struct SnapBuild * snapshot_builder
Definition: logical.h:44
struct ReorderBuffer * reorder
Definition: logical.h:43
LogicalDecodeStartupCB startup_cb
XLogRecPtr restart_lsn
Definition: slot.h:88
XLogRecPtr confirmed_flush
Definition: slot.h:99
ReplicationSlotInvalidationCause invalidated
Definition: slot.h:91
slock_t mutex
Definition: slot.h:135
ReplicationSlotPersistentData data
Definition: slot.h:162
#define InvalidTransactionId
Definition: transam.h:31
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28

References Assert(), LogicalDecodingContext::callbacks, ReplicationSlotPersistentData::confirmed_flush, LogicalDecodingContext::context, ReplicationSlot::data, data, ReplicationSlotPersistentData::database, elog(), ereport, errcode(), errdetail(), errmsg(), ERROR, ReplicationSlotPersistentData::invalidated, InvalidTransactionId, InvalidXLogRecPtr, LOG, LSN_FORMAT_ARGS, MemoryContextSwitchTo(), ReplicationSlot::mutex, MyDatabaseId, MyReplicationSlot, ReplicationSlotPersistentData::name, NameStr, LogicalDecodingContext::options, ReorderBuffer::output_rewrites, OutputPluginOptions::receive_rewrites, LogicalDecodingContext::reorder, ReplicationSlotMarkDirty(), ReplicationSlotSave(), ReplicationSlotPersistentData::restart_lsn, RS_INVAL_NONE, RS_INVAL_WAL_REMOVED, SlotIsPhysical, SnapBuildSetTwoPhaseAt(), LogicalDecodingContext::snapshot_builder, SpinLockAcquire, SpinLockRelease, OutputPluginCallbacks::startup_cb, startup_cb_wrapper(), StartupDecodingContext(), ReplicationSlotPersistentData::two_phase, ReplicationSlotPersistentData::two_phase_at, LogicalDecodingContext::twophase, and LogicalDecodingContext::twophase_opt_given.

Referenced by pg_logical_replication_slot_advance(), pg_logical_slot_get_changes_guts(), and StartLogicalReplication().

◆ CreateInitDecodingContext()

LogicalDecodingContext* CreateInitDecodingContext ( const char *  plugin,
List output_plugin_options,
bool  need_full_snapshot,
XLogRecPtr  restart_lsn,
XLogReaderRoutine xl_routine,
LogicalOutputPluginWriterPrepareWrite  prepare_write,
LogicalOutputPluginWriterWrite  do_write,
LogicalOutputPluginWriterUpdateProgress  update_progress 
)

Definition at line 328 of file logical.c.

336 {
337  TransactionId xmin_horizon = InvalidTransactionId;
338  ReplicationSlot *slot;
339  NameData plugin_name;
341  MemoryContext old_context;
342 
343  /*
344  * On a standby, this check is also required while creating the slot.
345  * Check the comments in the function.
346  */
348 
349  /* shorter lines... */
350  slot = MyReplicationSlot;
351 
352  /* first some sanity checks that are unlikely to be violated */
353  if (slot == NULL)
354  elog(ERROR, "cannot perform logical decoding without an acquired slot");
355 
356  if (plugin == NULL)
357  elog(ERROR, "cannot initialize logical decoding without a specified plugin");
358 
359  /* Make sure the passed slot is suitable. These are user facing errors. */
360  if (SlotIsPhysical(slot))
361  ereport(ERROR,
362  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
363  errmsg("cannot use physical replication slot for logical decoding")));
364 
365  if (slot->data.database != MyDatabaseId)
366  ereport(ERROR,
367  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
368  errmsg("replication slot \"%s\" was not created in this database",
369  NameStr(slot->data.name))));
370 
371  if (IsTransactionState() &&
373  ereport(ERROR,
374  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
375  errmsg("cannot create logical replication slot in transaction that has performed writes")));
376 
377  /*
378  * Register output plugin name with slot. We need the mutex to avoid
379  * concurrent reading of a partially copied string. But we don't want any
380  * complicated code while holding a spinlock, so do namestrcpy() outside.
381  */
382  namestrcpy(&plugin_name, plugin);
383  SpinLockAcquire(&slot->mutex);
384  slot->data.plugin = plugin_name;
385  SpinLockRelease(&slot->mutex);
386 
387  if (XLogRecPtrIsInvalid(restart_lsn))
389  else
390  {
391  SpinLockAcquire(&slot->mutex);
392  slot->data.restart_lsn = restart_lsn;
393  SpinLockRelease(&slot->mutex);
394  }
395 
396  /* ----
397  * This is a bit tricky: We need to determine a safe xmin horizon to start
398  * decoding from, to avoid starting from a running xacts record referring
399  * to xids whose rows have been vacuumed or pruned
400  * already. GetOldestSafeDecodingTransactionId() returns such a value, but
401  * without further interlock its return value might immediately be out of
402  * date.
403  *
404  * So we have to acquire the ProcArrayLock to prevent computation of new
405  * xmin horizons by other backends, get the safe decoding xid, and inform
406  * the slot machinery about the new limit. Once that's done the
407  * ProcArrayLock can be released as the slot machinery now is
408  * protecting against vacuum.
409  *
410  * Note that, temporarily, the data, not just the catalog, xmin has to be
411  * reserved if a data snapshot is to be exported. Otherwise the initial
412  * data snapshot created here is not guaranteed to be valid. After that
413  * the data xmin doesn't need to be managed anymore and the global xmin
414  * should be recomputed. As we are fine with losing the pegged data xmin
415  * after crash - no chance a snapshot would get exported anymore - we can
416  * get away with just setting the slot's
417  * effective_xmin. ReplicationSlotRelease will reset it again.
418  *
419  * ----
420  */
421  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
422 
423  xmin_horizon = GetOldestSafeDecodingTransactionId(!need_full_snapshot);
424 
425  SpinLockAcquire(&slot->mutex);
426  slot->effective_catalog_xmin = xmin_horizon;
427  slot->data.catalog_xmin = xmin_horizon;
428  if (need_full_snapshot)
429  slot->effective_xmin = xmin_horizon;
430  SpinLockRelease(&slot->mutex);
431 
433 
434  LWLockRelease(ProcArrayLock);
435 
438 
439  ctx = StartupDecodingContext(NIL, restart_lsn, xmin_horizon,
440  need_full_snapshot, false,
441  xl_routine, prepare_write, do_write,
442  update_progress);
443 
444  /* call output plugin initialization callback */
445  old_context = MemoryContextSwitchTo(ctx->context);
446  if (ctx->callbacks.startup_cb != NULL)
447  startup_cb_wrapper(ctx, &ctx->options, true);
448  MemoryContextSwitchTo(old_context);
449 
450  /*
451  * We allow decoding of prepared transactions when the two_phase is
452  * enabled at the time of slot creation, or when the two_phase option is
453  * given at the streaming start, provided the plugin supports all the
454  * callbacks for two-phase.
455  */
456  ctx->twophase &= slot->data.two_phase;
457 
459 
460  return ctx;
461 }
uint32 TransactionId
Definition: c.h:641
void CheckLogicalDecodingRequirements(void)
Definition: logical.c:108
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1195
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1808
@ LW_EXCLUSIVE
Definition: lwlock.h:116
void namestrcpy(Name name, const char *str)
Definition: name.c:233
#define NIL
Definition: pg_list.h:68
static const char * plugin
TransactionId GetOldestSafeDecodingTransactionId(bool catalogOnly)
Definition: procarray.c:2910
void ReplicationSlotReserveWal(void)
Definition: slot.c:1175
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:837
TransactionId catalog_xmin
Definition: slot.h:85
TransactionId effective_catalog_xmin
Definition: slot.h:159
TransactionId effective_xmin
Definition: slot.h:158
Definition: c.h:730
bool IsTransactionState(void)
Definition: xact.c:378
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:432
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29

References LogicalDecodingContext::callbacks, ReplicationSlotPersistentData::catalog_xmin, CheckLogicalDecodingRequirements(), LogicalDecodingContext::context, ReplicationSlot::data, ReplicationSlotPersistentData::database, ReplicationSlot::effective_catalog_xmin, ReplicationSlot::effective_xmin, elog(), ereport, errcode(), errmsg(), ERROR, GetOldestSafeDecodingTransactionId(), GetTopTransactionIdIfAny(), InvalidTransactionId, IsTransactionState(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MemoryContextSwitchTo(), ReplicationSlot::mutex, MyDatabaseId, MyReplicationSlot, ReplicationSlotPersistentData::name, NameStr, namestrcpy(), NIL, LogicalDecodingContext::options, ReorderBuffer::output_rewrites, plugin, ReplicationSlotPersistentData::plugin, OutputPluginOptions::receive_rewrites, LogicalDecodingContext::reorder, ReplicationSlotMarkDirty(), ReplicationSlotReserveWal(), ReplicationSlotSave(), ReplicationSlotsComputeRequiredXmin(), ReplicationSlotPersistentData::restart_lsn, SlotIsPhysical, SpinLockAcquire, SpinLockRelease, OutputPluginCallbacks::startup_cb, startup_cb_wrapper(), StartupDecodingContext(), ReplicationSlotPersistentData::two_phase, LogicalDecodingContext::twophase, and XLogRecPtrIsInvalid.

Referenced by create_logical_replication_slot(), and CreateReplicationSlot().

◆ DecodingContextFindStartpoint()

void DecodingContextFindStartpoint ( LogicalDecodingContext ctx)

Definition at line 630 of file logical.c.

631 {
632  ReplicationSlot *slot = ctx->slot;
633 
634  /* Initialize from where to start reading WAL. */
635  XLogBeginRead(ctx->reader, slot->data.restart_lsn);
636 
637  elog(DEBUG1, "searching for logical decoding starting point, starting at %X/%X",
639 
640  /* Wait for a consistent starting point */
641  for (;;)
642  {
643  XLogRecord *record;
644  char *err = NULL;
645 
646  /* the read_page callback waits for new WAL */
647  record = XLogReadRecord(ctx->reader, &err);
648  if (err)
649  elog(ERROR, "could not find logical decoding starting point: %s", err);
650  if (!record)
651  elog(ERROR, "could not find logical decoding starting point");
652 
654 
655  /* only continue till we found a consistent spot */
656  if (DecodingContextReady(ctx))
657  break;
658 
660  }
661 
662  SpinLockAcquire(&slot->mutex);
663  slot->data.confirmed_flush = ctx->reader->EndRecPtr;
664  if (slot->data.two_phase)
665  slot->data.two_phase_at = ctx->reader->EndRecPtr;
666  SpinLockRelease(&slot->mutex);
667 }
void LogicalDecodingProcessRecord(LogicalDecodingContext *ctx, XLogReaderState *record)
Definition: decode.c:91
#define DEBUG1
Definition: elog.h:30
void err(int eval, const char *fmt,...)
Definition: err.c:43
bool DecodingContextReady(LogicalDecodingContext *ctx)
Definition: logical.c:621
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
XLogReaderState * reader
Definition: logical.h:42
ReplicationSlot * slot
Definition: logical.h:39
XLogRecPtr EndRecPtr
Definition: xlogreader.h:207
XLogRecord * XLogReadRecord(XLogReaderState *state, char **errormsg)
Definition: xlogreader.c:406
void XLogBeginRead(XLogReaderState *state, XLogRecPtr RecPtr)
Definition: xlogreader.c:248

References CHECK_FOR_INTERRUPTS, ReplicationSlotPersistentData::confirmed_flush, ReplicationSlot::data, DEBUG1, DecodingContextReady(), elog(), XLogReaderState::EndRecPtr, err(), ERROR, LogicalDecodingProcessRecord(), LSN_FORMAT_ARGS, ReplicationSlot::mutex, LogicalDecodingContext::reader, ReplicationSlotPersistentData::restart_lsn, LogicalDecodingContext::slot, SpinLockAcquire, SpinLockRelease, ReplicationSlotPersistentData::two_phase, ReplicationSlotPersistentData::two_phase_at, XLogBeginRead(), and XLogReadRecord().

Referenced by create_logical_replication_slot(), and CreateReplicationSlot().

◆ DecodingContextReady()

bool DecodingContextReady ( LogicalDecodingContext ctx)

Definition at line 621 of file logical.c.

622 {
624 }
SnapBuildState SnapBuildCurrentState(SnapBuild *builder)
Definition: snapbuild.c:407
@ SNAPBUILD_CONSISTENT
Definition: snapbuild.h:46

References SNAPBUILD_CONSISTENT, SnapBuildCurrentState(), and LogicalDecodingContext::snapshot_builder.

Referenced by DecodingContextFindStartpoint().

◆ filter_by_origin_cb_wrapper()

bool filter_by_origin_cb_wrapper ( LogicalDecodingContext ctx,
RepOriginId  origin_id 
)

Definition at line 1196 of file logical.c.

1197 {
1199  ErrorContextCallback errcallback;
1200  bool ret;
1201 
1202  Assert(!ctx->fast_forward);
1203 
1204  /* Push callback + info on the error context stack */
1205  state.ctx = ctx;
1206  state.callback_name = "filter_by_origin";
1207  state.report_location = InvalidXLogRecPtr;
1208  errcallback.callback = output_plugin_error_callback;
1209  errcallback.arg = (void *) &state;
1210  errcallback.previous = error_context_stack;
1211  error_context_stack = &errcallback;
1212 
1213  /* set output state */
1214  ctx->accept_writes = false;
1215  ctx->end_xact = false;
1216 
1217  /* do the actual work: call callback */
1218  ret = ctx->callbacks.filter_by_origin_cb(ctx, origin_id);
1219 
1220  /* Pop the error context stack */
1221  error_context_stack = errcallback.previous;
1222 
1223  return ret;
1224 }
LogicalDecodeFilterByOriginCB filter_by_origin_cb

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert(), ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, error_context_stack, LogicalDecodingContext::fast_forward, OutputPluginCallbacks::filter_by_origin_cb, InvalidXLogRecPtr, output_plugin_error_callback(), and ErrorContextCallback::previous.

Referenced by FilterByOrigin().

◆ filter_prepare_cb_wrapper()

bool filter_prepare_cb_wrapper ( LogicalDecodingContext ctx,
TransactionId  xid,
const char *  gid 
)

Definition at line 1164 of file logical.c.

1166 {
1168  ErrorContextCallback errcallback;
1169  bool ret;
1170 
1171  Assert(!ctx->fast_forward);
1172 
1173  /* Push callback + info on the error context stack */
1174  state.ctx = ctx;
1175  state.callback_name = "filter_prepare";
1176  state.report_location = InvalidXLogRecPtr;
1177  errcallback.callback = output_plugin_error_callback;
1178  errcallback.arg = (void *) &state;
1179  errcallback.previous = error_context_stack;
1180  error_context_stack = &errcallback;
1181 
1182  /* set output state */
1183  ctx->accept_writes = false;
1184  ctx->end_xact = false;
1185 
1186  /* do the actual work: call callback */
1187  ret = ctx->callbacks.filter_prepare_cb(ctx, xid, gid);
1188 
1189  /* Pop the error context stack */
1190  error_context_stack = errcallback.previous;
1191 
1192  return ret;
1193 }
LogicalDecodeFilterPrepareCB filter_prepare_cb

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert(), ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, error_context_stack, LogicalDecodingContext::fast_forward, OutputPluginCallbacks::filter_prepare_cb, InvalidXLogRecPtr, output_plugin_error_callback(), and ErrorContextCallback::previous.

Referenced by FilterPrepare().

◆ FreeDecodingContext()

void FreeDecodingContext ( LogicalDecodingContext ctx)

Definition at line 674 of file logical.c.

675 {
676  if (ctx->callbacks.shutdown_cb != NULL)
677  shutdown_cb_wrapper(ctx);
678 
681  XLogReaderFree(ctx->reader);
683 }
static void shutdown_cb_wrapper(LogicalDecodingContext *ctx)
Definition: logical.c:799
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:403
void ReorderBufferFree(ReorderBuffer *rb)
void FreeSnapshotBuilder(SnapBuild *builder)
Definition: snapbuild.c:363
LogicalDecodeShutdownCB shutdown_cb
void XLogReaderFree(XLogReaderState *state)
Definition: xlogreader.c:170

References LogicalDecodingContext::callbacks, LogicalDecodingContext::context, FreeSnapshotBuilder(), MemoryContextDelete(), LogicalDecodingContext::reader, LogicalDecodingContext::reorder, ReorderBufferFree(), OutputPluginCallbacks::shutdown_cb, shutdown_cb_wrapper(), LogicalDecodingContext::snapshot_builder, and XLogReaderFree().

Referenced by create_logical_replication_slot(), CreateReplicationSlot(), pg_logical_replication_slot_advance(), pg_logical_slot_get_changes_guts(), and StartLogicalReplication().

◆ LoadOutputPlugin()

static void LoadOutputPlugin ( OutputPluginCallbacks callbacks,
const char *  plugin 
)
static

Definition at line 730 of file logical.c.

731 {
732  LogicalOutputPluginInit plugin_init;
733 
734  plugin_init = (LogicalOutputPluginInit)
735  load_external_function(plugin, "_PG_output_plugin_init", false, NULL);
736 
737  if (plugin_init == NULL)
738  elog(ERROR, "output plugins have to declare the _PG_output_plugin_init symbol");
739 
740  /* ask the output plugin to fill the callback struct */
741  plugin_init(callbacks);
742 
743  if (callbacks->begin_cb == NULL)
744  elog(ERROR, "output plugins have to register a begin callback");
745  if (callbacks->change_cb == NULL)
746  elog(ERROR, "output plugins have to register a change callback");
747  if (callbacks->commit_cb == NULL)
748  elog(ERROR, "output plugins have to register a commit callback");
749 }
void * load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
Definition: dfmgr.c:105
void(* LogicalOutputPluginInit)(struct OutputPluginCallbacks *cb)
Definition: output_plugin.h:36

References OutputPluginCallbacks::begin_cb, OutputPluginCallbacks::change_cb, OutputPluginCallbacks::commit_cb, elog(), ERROR, load_external_function(), and plugin.

Referenced by StartupDecodingContext().

◆ LogicalConfirmReceivedLocation()

void LogicalConfirmReceivedLocation ( XLogRecPtr  lsn)

Definition at line 1815 of file logical.c.

1816 {
1817  Assert(lsn != InvalidXLogRecPtr);
1818 
1819  /* Do an unlocked check for candidate_lsn first. */
1822  {
1823  bool updated_xmin = false;
1824  bool updated_restart = false;
1825 
1827 
1829 
1830  /* if we're past the location required for bumping xmin, do so */
1833  {
1834  /*
1835  * We have to write the changed xmin to disk *before* we change
1836  * the in-memory value, otherwise after a crash we wouldn't know
1837  * that some catalog tuples might have been removed already.
1838  *
1839  * Ensure that by first writing to ->xmin and only update
1840  * ->effective_xmin once the new state is synced to disk. After a
1841  * crash ->effective_xmin is set to ->xmin.
1842  */
1845  {
1849  updated_xmin = true;
1850  }
1851  }
1852 
1855  {
1857 
1861  updated_restart = true;
1862  }
1863 
1865 
1866  /* first write new xmin to disk, so we know what's up after a crash */
1867  if (updated_xmin || updated_restart)
1868  {
1871  elog(DEBUG1, "updated xmin: %u restart: %u", updated_xmin, updated_restart);
1872  }
1873 
1874  /*
1875  * Now the new xmin is safely on disk, we can let the global value
1876  * advance. We do not take ProcArrayLock or similar since we only
1877  * advance xmin here and there's not much harm done by a concurrent
1878  * computation missing that.
1879  */
1880  if (updated_xmin)
1881  {
1885 
1888  }
1889  }
1890  else
1891  {
1895  }
1896 }
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:893
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:178
XLogRecPtr candidate_restart_valid
Definition: slot.h:179
XLogRecPtr candidate_restart_lsn
Definition: slot.h:180
TransactionId candidate_catalog_xmin
Definition: slot.h:177
#define TransactionIdIsValid(xid)
Definition: transam.h:41

References Assert(), ReplicationSlot::candidate_catalog_xmin, ReplicationSlot::candidate_restart_lsn, ReplicationSlot::candidate_restart_valid, ReplicationSlot::candidate_xmin_lsn, ReplicationSlotPersistentData::catalog_xmin, ReplicationSlotPersistentData::confirmed_flush, ReplicationSlot::data, DEBUG1, ReplicationSlot::effective_catalog_xmin, elog(), InvalidTransactionId, InvalidXLogRecPtr, ReplicationSlot::mutex, MyReplicationSlot, ReplicationSlotMarkDirty(), ReplicationSlotSave(), ReplicationSlotsComputeRequiredLSN(), ReplicationSlotsComputeRequiredXmin(), ReplicationSlotPersistentData::restart_lsn, SpinLockAcquire, SpinLockRelease, and TransactionIdIsValid.

Referenced by LogicalIncreaseRestartDecodingForSlot(), LogicalIncreaseXminForSlot(), pg_logical_replication_slot_advance(), pg_logical_slot_get_changes_guts(), and ProcessStandbyReplyMessage().

◆ LogicalIncreaseRestartDecodingForSlot()

void LogicalIncreaseRestartDecodingForSlot ( XLogRecPtr  current_lsn,
XLogRecPtr  restart_lsn 
)

Definition at line 1741 of file logical.c.

1742 {
1743  bool updated_lsn = false;
1744  ReplicationSlot *slot;
1745 
1746  slot = MyReplicationSlot;
1747 
1748  Assert(slot != NULL);
1749  Assert(restart_lsn != InvalidXLogRecPtr);
1750  Assert(current_lsn != InvalidXLogRecPtr);
1751 
1752  SpinLockAcquire(&slot->mutex);
1753 
1754  /* don't overwrite if have a newer restart lsn */
1755  if (restart_lsn <= slot->data.restart_lsn)
1756  {
1757  }
1758 
1759  /*
1760  * We might have already flushed far enough to directly accept this lsn,
1761  * in this case there is no need to check for existing candidate LSNs
1762  */
1763  else if (current_lsn <= slot->data.confirmed_flush)
1764  {
1765  slot->candidate_restart_valid = current_lsn;
1766  slot->candidate_restart_lsn = restart_lsn;
1767 
1768  /* our candidate can directly be used */
1769  updated_lsn = true;
1770  }
1771 
1772  /*
1773  * Only increase if the previous values have been applied, otherwise we
1774  * might never end up updating if the receiver acks too slowly. A missed
1775  * value here will just cause some extra effort after reconnecting.
1776  */
1778  {
1779  slot->candidate_restart_valid = current_lsn;
1780  slot->candidate_restart_lsn = restart_lsn;
1781  SpinLockRelease(&slot->mutex);
1782 
1783  elog(DEBUG1, "got new restart lsn %X/%X at %X/%X",
1784  LSN_FORMAT_ARGS(restart_lsn),
1785  LSN_FORMAT_ARGS(current_lsn));
1786  }
1787  else
1788  {
1789  XLogRecPtr candidate_restart_lsn;
1790  XLogRecPtr candidate_restart_valid;
1791  XLogRecPtr confirmed_flush;
1792 
1793  candidate_restart_lsn = slot->candidate_restart_lsn;
1794  candidate_restart_valid = slot->candidate_restart_valid;
1795  confirmed_flush = slot->data.confirmed_flush;
1796  SpinLockRelease(&slot->mutex);
1797 
1798  elog(DEBUG1, "failed to increase restart lsn: proposed %X/%X, after %X/%X, current candidate %X/%X, current after %X/%X, flushed up to %X/%X",
1799  LSN_FORMAT_ARGS(restart_lsn),
1800  LSN_FORMAT_ARGS(current_lsn),
1801  LSN_FORMAT_ARGS(candidate_restart_lsn),
1802  LSN_FORMAT_ARGS(candidate_restart_valid),
1803  LSN_FORMAT_ARGS(confirmed_flush));
1804  }
1805 
1806  /* candidates are already valid with the current flush position, apply */
1807  if (updated_lsn)
1809 }
void LogicalConfirmReceivedLocation(XLogRecPtr lsn)
Definition: logical.c:1815
uint64 XLogRecPtr
Definition: xlogdefs.h:21

References Assert(), ReplicationSlot::candidate_restart_lsn, ReplicationSlot::candidate_restart_valid, ReplicationSlotPersistentData::confirmed_flush, ReplicationSlot::data, data, DEBUG1, elog(), InvalidXLogRecPtr, LogicalConfirmReceivedLocation(), LSN_FORMAT_ARGS, ReplicationSlot::mutex, MyReplicationSlot, SpinLockAcquire, and SpinLockRelease.

Referenced by SnapBuildProcessRunningXacts().

◆ LogicalIncreaseXminForSlot()

void LogicalIncreaseXminForSlot ( XLogRecPtr  current_lsn,
TransactionId  xmin 
)

Definition at line 1673 of file logical.c.

1674 {
1675  bool updated_xmin = false;
1676  ReplicationSlot *slot;
1677  bool got_new_xmin = false;
1678 
1679  slot = MyReplicationSlot;
1680 
1681  Assert(slot != NULL);
1682 
1683  SpinLockAcquire(&slot->mutex);
1684 
1685  /*
1686  * don't overwrite if we already have a newer xmin. This can happen if we
1687  * restart decoding in a slot.
1688  */
1690  {
1691  }
1692 
1693  /*
1694  * If the client has already confirmed up to this lsn, we directly can
1695  * mark this as accepted. This can happen if we restart decoding in a
1696  * slot.
1697  */
1698  else if (current_lsn <= slot->data.confirmed_flush)
1699  {
1700  slot->candidate_catalog_xmin = xmin;
1701  slot->candidate_xmin_lsn = current_lsn;
1702 
1703  /* our candidate can directly be used */
1704  updated_xmin = true;
1705  }
1706 
1707  /*
1708  * Only increase if the previous values have been applied, otherwise we
1709  * might never end up updating if the receiver acks too slowly.
1710  */
1711  else if (slot->candidate_xmin_lsn == InvalidXLogRecPtr)
1712  {
1713  slot->candidate_catalog_xmin = xmin;
1714  slot->candidate_xmin_lsn = current_lsn;
1715 
1716  /*
1717  * Log new xmin at an appropriate log level after releasing the
1718  * spinlock.
1719  */
1720  got_new_xmin = true;
1721  }
1722  SpinLockRelease(&slot->mutex);
1723 
1724  if (got_new_xmin)
1725  elog(DEBUG1, "got new catalog xmin %u at %X/%X", xmin,
1726  LSN_FORMAT_ARGS(current_lsn));
1727 
1728  /* candidate already valid with the current flush position, apply */
1729  if (updated_xmin)
1731 }
bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:299

References Assert(), ReplicationSlot::candidate_catalog_xmin, ReplicationSlot::candidate_xmin_lsn, ReplicationSlotPersistentData::catalog_xmin, ReplicationSlotPersistentData::confirmed_flush, ReplicationSlot::data, data, DEBUG1, elog(), InvalidXLogRecPtr, LogicalConfirmReceivedLocation(), LSN_FORMAT_ARGS, ReplicationSlot::mutex, MyReplicationSlot, SpinLockAcquire, SpinLockRelease, and TransactionIdPrecedesOrEquals().

Referenced by SnapBuildProcessRunningXacts().

◆ message_cb_wrapper()

static void message_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn,
XLogRecPtr  message_lsn,
bool  transactional,
const char *  prefix,
Size  message_size,
const char *  message 
)
static

Definition at line 1227 of file logical.c.

1230 {
1231  LogicalDecodingContext *ctx = cache->private_data;
1233  ErrorContextCallback errcallback;
1234 
1235  Assert(!ctx->fast_forward);
1236 
1237  if (ctx->callbacks.message_cb == NULL)
1238  return;
1239 
1240  /* Push callback + info on the error context stack */
1241  state.ctx = ctx;
1242  state.callback_name = "message";
1243  state.report_location = message_lsn;
1244  errcallback.callback = output_plugin_error_callback;
1245  errcallback.arg = (void *) &state;
1246  errcallback.previous = error_context_stack;
1247  error_context_stack = &errcallback;
1248 
1249  /* set output state */
1250  ctx->accept_writes = true;
1251  ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
1252  ctx->write_location = message_lsn;
1253  ctx->end_xact = false;
1254 
1255  /* do the actual work: call callback */
1256  ctx->callbacks.message_cb(ctx, txn, message_lsn, transactional, prefix,
1257  message_size, message);
1258 
1259  /* Pop the error context stack */
1260  error_context_stack = errcallback.previous;
1261 }
LogicalDecodeMessageCB message_cb

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert(), ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, error_context_stack, LogicalDecodingContext::fast_forward, InvalidTransactionId, OutputPluginCallbacks::message_cb, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ output_plugin_error_callback()

static void output_plugin_error_callback ( void *  arg)
static

Definition at line 752 of file logical.c.

753 {
755 
756  /* not all callbacks have an associated LSN */
757  if (state->report_location != InvalidXLogRecPtr)
758  errcontext("slot \"%s\", output plugin \"%s\", in the %s callback, associated LSN %X/%X",
759  NameStr(state->ctx->slot->data.name),
760  NameStr(state->ctx->slot->data.plugin),
761  state->callback_name,
762  LSN_FORMAT_ARGS(state->report_location));
763  else
764  errcontext("slot \"%s\", output plugin \"%s\", in the %s callback",
765  NameStr(state->ctx->slot->data.name),
766  NameStr(state->ctx->slot->data.plugin),
767  state->callback_name);
768 }
#define errcontext
Definition: elog.h:196
void * arg

References arg, errcontext, InvalidXLogRecPtr, LSN_FORMAT_ARGS, and NameStr.

Referenced by begin_cb_wrapper(), begin_prepare_cb_wrapper(), change_cb_wrapper(), commit_cb_wrapper(), commit_prepared_cb_wrapper(), filter_by_origin_cb_wrapper(), filter_prepare_cb_wrapper(), message_cb_wrapper(), prepare_cb_wrapper(), rollback_prepared_cb_wrapper(), shutdown_cb_wrapper(), startup_cb_wrapper(), stream_abort_cb_wrapper(), stream_change_cb_wrapper(), stream_commit_cb_wrapper(), stream_message_cb_wrapper(), stream_prepare_cb_wrapper(), stream_start_cb_wrapper(), stream_stop_cb_wrapper(), stream_truncate_cb_wrapper(), truncate_cb_wrapper(), and update_progress_txn_cb_wrapper().

◆ OutputPluginPrepareWrite()

void OutputPluginPrepareWrite ( struct LogicalDecodingContext ctx,
bool  last_write 
)

Definition at line 689 of file logical.c.

690 {
691  if (!ctx->accept_writes)
692  elog(ERROR, "writes are only accepted in commit, begin and change callbacks");
693 
694  ctx->prepare_write(ctx, ctx->write_location, ctx->write_xid, last_write);
695  ctx->prepared_write = true;
696 }
LogicalOutputPluginWriterPrepareWrite prepare_write
Definition: logical.h:64

References LogicalDecodingContext::accept_writes, elog(), ERROR, LogicalDecodingContext::prepare_write, LogicalDecodingContext::prepared_write, LogicalDecodingContext::write_location, and LogicalDecodingContext::write_xid.

Referenced by pg_decode_change(), pg_decode_commit_prepared_txn(), pg_decode_commit_txn(), pg_decode_message(), pg_decode_prepare_txn(), pg_decode_rollback_prepared_txn(), pg_decode_stream_abort(), pg_decode_stream_change(), pg_decode_stream_commit(), pg_decode_stream_message(), pg_decode_stream_prepare(), pg_decode_stream_stop(), pg_decode_stream_truncate(), pg_decode_truncate(), pg_output_begin(), pg_output_stream_start(), pgoutput_begin_prepare_txn(), pgoutput_change(), pgoutput_commit_prepared_txn(), pgoutput_commit_txn(), pgoutput_message(), pgoutput_prepare_txn(), pgoutput_rollback_prepared_txn(), pgoutput_send_begin(), pgoutput_stream_abort(), pgoutput_stream_commit(), pgoutput_stream_prepare_txn(), pgoutput_stream_start(), pgoutput_stream_stop(), pgoutput_truncate(), send_relation_and_attrs(), and send_repl_origin().

◆ OutputPluginUpdateProgress()

void OutputPluginUpdateProgress ( struct LogicalDecodingContext ctx,
bool  skipped_xact 
)

◆ OutputPluginWrite()

◆ prepare_cb_wrapper()

static void prepare_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn,
XLogRecPtr  prepare_lsn 
)
static

Definition at line 946 of file logical.c.

948 {
949  LogicalDecodingContext *ctx = cache->private_data;
951  ErrorContextCallback errcallback;
952 
953  Assert(!ctx->fast_forward);
954 
955  /* We're only supposed to call this when two-phase commits are supported */
956  Assert(ctx->twophase);
957 
958  /* Push callback + info on the error context stack */
959  state.ctx = ctx;
960  state.callback_name = "prepare";
961  state.report_location = txn->final_lsn; /* beginning of prepare record */
963  errcallback.arg = (void *) &state;
964  errcallback.previous = error_context_stack;
965  error_context_stack = &errcallback;
966 
967  /* set output state */
968  ctx->accept_writes = true;
969  ctx->write_xid = txn->xid;
970  ctx->write_location = txn->end_lsn; /* points to the end of the record */
971  ctx->end_xact = true;
972 
973  /*
974  * If the plugin supports two-phase commits then prepare callback is
975  * mandatory
976  */
977  if (ctx->callbacks.prepare_cb == NULL)
978  ereport(ERROR,
979  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
980  errmsg("logical replication at prepare time requires a %s callback",
981  "prepare_cb")));
982 
983  /* do the actual work: call callback */
984  ctx->callbacks.prepare_cb(ctx, txn, prepare_lsn);
985 
986  /* Pop the error context stack */
987  error_context_stack = errcallback.previous;
988 }
LogicalDecodePrepareCB prepare_cb

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert(), ErrorContextCallback::callback, LogicalDecodingContext::callbacks, ReorderBufferTXN::end_lsn, LogicalDecodingContext::end_xact, ereport, errcode(), errmsg(), ERROR, error_context_stack, LogicalDecodingContext::fast_forward, ReorderBufferTXN::final_lsn, output_plugin_error_callback(), OutputPluginCallbacks::prepare_cb, ErrorContextCallback::previous, ReorderBuffer::private_data, LogicalDecodingContext::twophase, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ ResetLogicalStreamingState()

void ResetLogicalStreamingState ( void  )

Definition at line 1902 of file logical.c.

1903 {
1905  bsysscan = false;
1906 }
bool bsysscan
Definition: xact.c:100
TransactionId CheckXidAlive
Definition: xact.c:99

References bsysscan, CheckXidAlive, and InvalidTransactionId.

Referenced by AbortSubTransaction(), and AbortTransaction().

◆ rollback_prepared_cb_wrapper()

static void rollback_prepared_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn,
XLogRecPtr  prepare_end_lsn,
TimestampTz  prepare_time 
)
static

Definition at line 1036 of file logical.c.

1039 {
1040  LogicalDecodingContext *ctx = cache->private_data;
1042  ErrorContextCallback errcallback;
1043 
1044  Assert(!ctx->fast_forward);
1045 
1046  /* We're only supposed to call this when two-phase commits are supported */
1047  Assert(ctx->twophase);
1048 
1049  /* Push callback + info on the error context stack */
1050  state.ctx = ctx;
1051  state.callback_name = "rollback_prepared";
1052  state.report_location = txn->final_lsn; /* beginning of commit record */
1053  errcallback.callback = output_plugin_error_callback;
1054  errcallback.arg = (void *) &state;
1055  errcallback.previous = error_context_stack;
1056  error_context_stack = &errcallback;
1057 
1058  /* set output state */
1059  ctx->accept_writes = true;
1060  ctx->write_xid = txn->xid;
1061  ctx->write_location = txn->end_lsn; /* points to the end of the record */
1062  ctx->end_xact = true;
1063 
1064  /*
1065  * If the plugin support two-phase commits then rollback prepared callback
1066  * is mandatory
1067  */
1068  if (ctx->callbacks.rollback_prepared_cb == NULL)
1069  ereport(ERROR,
1070  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1071  errmsg("logical replication at prepare time requires a %s callback",
1072  "rollback_prepared_cb")));
1073 
1074  /* do the actual work: call callback */
1075  ctx->callbacks.rollback_prepared_cb(ctx, txn, prepare_end_lsn,
1076  prepare_time);
1077 
1078  /* Pop the error context stack */
1079  error_context_stack = errcallback.previous;
1080 }
LogicalDecodeRollbackPreparedCB rollback_prepared_cb

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert(), ErrorContextCallback::callback, LogicalDecodingContext::callbacks, ReorderBufferTXN::end_lsn, LogicalDecodingContext::end_xact, ereport, errcode(), errmsg(), ERROR, error_context_stack, LogicalDecodingContext::fast_forward, ReorderBufferTXN::final_lsn, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, OutputPluginCallbacks::rollback_prepared_cb, LogicalDecodingContext::twophase, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ shutdown_cb_wrapper()

static void shutdown_cb_wrapper ( LogicalDecodingContext ctx)
static

Definition at line 799 of file logical.c.

800 {
802  ErrorContextCallback errcallback;
803 
804  Assert(!ctx->fast_forward);
805 
806  /* Push callback + info on the error context stack */
807  state.ctx = ctx;
808  state.callback_name = "shutdown";
809  state.report_location = InvalidXLogRecPtr;
811  errcallback.arg = (void *) &state;
812  errcallback.previous = error_context_stack;
813  error_context_stack = &errcallback;
814 
815  /* set output state */
816  ctx->accept_writes = false;
817  ctx->end_xact = false;
818 
819  /* do the actual work: call callback */
820  ctx->callbacks.shutdown_cb(ctx);
821 
822  /* Pop the error context stack */
823  error_context_stack = errcallback.previous;
824 }

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert(), ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, error_context_stack, LogicalDecodingContext::fast_forward, InvalidXLogRecPtr, output_plugin_error_callback(), ErrorContextCallback::previous, and OutputPluginCallbacks::shutdown_cb.

Referenced by FreeDecodingContext().

◆ startup_cb_wrapper()

static void startup_cb_wrapper ( LogicalDecodingContext ctx,
OutputPluginOptions opt,
bool  is_init 
)
static

Definition at line 771 of file logical.c.

772 {
774  ErrorContextCallback errcallback;
775 
776  Assert(!ctx->fast_forward);
777 
778  /* Push callback + info on the error context stack */
779  state.ctx = ctx;
780  state.callback_name = "startup";
781  state.report_location = InvalidXLogRecPtr;
783  errcallback.arg = (void *) &state;
784  errcallback.previous = error_context_stack;
785  error_context_stack = &errcallback;
786 
787  /* set output state */
788  ctx->accept_writes = false;
789  ctx->end_xact = false;
790 
791  /* do the actual work: call callback */
792  ctx->callbacks.startup_cb(ctx, opt, is_init);
793 
794  /* Pop the error context stack */
795  error_context_stack = errcallback.previous;
796 }

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert(), ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, error_context_stack, LogicalDecodingContext::fast_forward, InvalidXLogRecPtr, output_plugin_error_callback(), ErrorContextCallback::previous, and OutputPluginCallbacks::startup_cb.

Referenced by CreateDecodingContext(), and CreateInitDecodingContext().

◆ StartupDecodingContext()

static LogicalDecodingContext* StartupDecodingContext ( List output_plugin_options,
XLogRecPtr  start_lsn,
TransactionId  xmin_horizon,
bool  need_full_snapshot,
bool  fast_forward,
XLogReaderRoutine xl_routine,
LogicalOutputPluginWriterPrepareWrite  prepare_write,
LogicalOutputPluginWriterWrite  do_write,
LogicalOutputPluginWriterUpdateProgress  update_progress 
)
static

Definition at line 149 of file logical.c.

158 {
159  ReplicationSlot *slot;
160  MemoryContext context,
161  old_context;
163 
164  /* shorter lines... */
165  slot = MyReplicationSlot;
166 
168  "Logical decoding context",
170  old_context = MemoryContextSwitchTo(context);
171  ctx = palloc0(sizeof(LogicalDecodingContext));
172 
173  ctx->context = context;
174 
175  /*
176  * (re-)load output plugins, so we detect a bad (removed) output plugin
177  * now.
178  */
179  if (!fast_forward)
181 
182  /*
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  */
195  {
196  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
199  LWLockRelease(ProcArrayLock);
200  }
201 
202  ctx->slot = slot;
203 
204  ctx->reader = XLogReaderAllocate(wal_segment_size, NULL, xl_routine, ctx);
205  if (!ctx->reader)
206  ereport(ERROR,
207  (errcode(ERRCODE_OUT_OF_MEMORY),
208  errmsg("out of memory"),
209  errdetail("Failed while allocating a WAL reading processor.")));
210 
212  ctx->snapshot_builder =
213  AllocateSnapshotBuilder(ctx->reorder, xmin_horizon, start_lsn,
214  need_full_snapshot, slot->data.two_phase_at);
215 
216  ctx->reorder->private_data = ctx;
217 
218  /* wrap output plugin callbacks, so we can add error context information */
224 
225  /*
226  * To support streaming, we require start/stop/abort/commit/change
227  * callbacks. The message and truncate callbacks are optional, similar to
228  * regular output plugins. We however enable streaming when at least one
229  * of the methods is enabled so that we can easily identify missing
230  * methods.
231  *
232  * We decide it here, but only check it later in the wrappers.
233  */
234  ctx->streaming = (ctx->callbacks.stream_start_cb != NULL) ||
235  (ctx->callbacks.stream_stop_cb != NULL) ||
236  (ctx->callbacks.stream_abort_cb != NULL) ||
237  (ctx->callbacks.stream_commit_cb != NULL) ||
238  (ctx->callbacks.stream_change_cb != NULL) ||
239  (ctx->callbacks.stream_message_cb != NULL) ||
240  (ctx->callbacks.stream_truncate_cb != NULL);
241 
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  */
258 
259 
260  /*
261  * To support two-phase logical decoding, we require
262  * begin_prepare/prepare/commit-prepare/abort-prepare callbacks. The
263  * filter_prepare callback is optional. We however enable two-phase
264  * logical decoding when at least one of the methods is enabled so that we
265  * can easily identify missing methods.
266  *
267  * We decide it here, but only check it later in the wrappers.
268  */
269  ctx->twophase = (ctx->callbacks.begin_prepare_cb != NULL) ||
270  (ctx->callbacks.prepare_cb != NULL) ||
271  (ctx->callbacks.commit_prepared_cb != NULL) ||
272  (ctx->callbacks.rollback_prepared_cb != NULL) ||
273  (ctx->callbacks.stream_prepare_cb != NULL) ||
274  (ctx->callbacks.filter_prepare_cb != NULL);
275 
276  /*
277  * Callback to support decoding at prepare time.
278  */
283 
284  /*
285  * Callback to support updating progress during sending data of a
286  * transaction (and its subtransactions) to the output plugin.
287  */
289 
290  ctx->out = makeStringInfo();
291  ctx->prepare_write = prepare_write;
292  ctx->write = do_write;
293  ctx->update_progress = update_progress;
294 
295  ctx->output_plugin_options = output_plugin_options;
296 
297  ctx->fast_forward = fast_forward;
298 
299  MemoryContextSwitchTo(old_context);
300 
301  return ctx;
302 }
static void change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
Definition: logical.c:1083
static void commit_prepared_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:991
static void update_progress_txn_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr lsn)
Definition: logical.c:1626
static void stream_start_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr first_lsn)
Definition: logical.c:1264
static void commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:863
static void begin_prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
Definition: logical.c:902
static void stream_prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
Definition: logical.c:1403
static void prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
Definition: logical.c:946
static void stream_truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
Definition: logical.c:1579
static void truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
Definition: logical.c:1122
static void begin_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
Definition: logical.c:832
static void rollback_prepared_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_end_lsn, TimestampTz prepare_time)
Definition: logical.c:1036
static void stream_change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
Definition: logical.c:1489
static void stream_abort_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr abort_lsn)
Definition: logical.c:1362
static void stream_message_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr message_lsn, bool transactional, const char *prefix, Size message_size, const char *message)
Definition: logical.c:1538
static void stream_commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:1448
static void LoadOutputPlugin(OutputPluginCallbacks *callbacks, const char *plugin)
Definition: logical.c:730
static void stream_stop_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr last_lsn)
Definition: logical.c:1313
static void message_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr message_lsn, bool transactional, const char *prefix, Size message_size, const char *message)
Definition: logical.c:1227
void * palloc0(Size size)
Definition: mcxt.c:1257
MemoryContext CurrentMemoryContext
Definition: mcxt.c:135
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:153
#define PROC_IN_LOGICAL_DECODING
Definition: proc.h:60
ReorderBuffer * ReorderBufferAllocate(void)
SnapBuild * AllocateSnapshotBuilder(ReorderBuffer *reorder, TransactionId xmin_horizon, XLogRecPtr start_lsn, bool need_full_snapshot, XLogRecPtr two_phase_at)
Definition: snapbuild.c:317
PGPROC * MyProc
Definition: proc.c:66
PROC_HDR * ProcGlobal
Definition: proc.c:78
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
StringInfo out
Definition: logical.h:71
List * output_plugin_options
Definition: logical.h:59
LogicalDecodeStreamChangeCB stream_change_cb
LogicalDecodeStreamTruncateCB stream_truncate_cb
LogicalDecodeStreamMessageCB stream_message_cb
LogicalDecodeStreamStopCB stream_stop_cb
LogicalDecodeStreamCommitCB stream_commit_cb
LogicalDecodeStreamPrepareCB stream_prepare_cb
LogicalDecodeStreamStartCB stream_start_cb
LogicalDecodeStreamAbortCB stream_abort_cb
uint8 statusFlags
Definition: proc.h:233
int pgxactoff
Definition: proc.h:188
uint8 * statusFlags
Definition: proc.h:377
ReorderBufferStreamMessageCB stream_message
ReorderBufferStreamChangeCB stream_change
ReorderBufferBeginCB begin_prepare
ReorderBufferStreamTruncateCB stream_truncate
ReorderBufferCommitPreparedCB commit_prepared
ReorderBufferUpdateProgressTxnCB update_progress_txn
ReorderBufferMessageCB message
ReorderBufferRollbackPreparedCB rollback_prepared
ReorderBufferPrepareCB prepare
ReorderBufferStreamStopCB stream_stop
ReorderBufferApplyChangeCB apply_change
ReorderBufferStreamPrepareCB stream_prepare
ReorderBufferStreamAbortCB stream_abort
ReorderBufferCommitCB commit
ReorderBufferStreamStartCB stream_start
ReorderBufferStreamCommitCB stream_commit
ReorderBufferApplyTruncateCB apply_truncate
ReorderBufferBeginCB begin
bool IsTransactionOrTransactionBlock(void)
Definition: xact.c:4834
int wal_segment_size
Definition: xlog.c:146
XLogReaderState * XLogReaderAllocate(int wal_segment_size, const char *waldir, XLogReaderRoutine *routine, void *private_data)
Definition: xlogreader.c:108

References AllocateSnapshotBuilder(), ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, ReorderBuffer::apply_change, ReorderBuffer::apply_truncate, ReorderBuffer::begin, begin_cb_wrapper(), ReorderBuffer::begin_prepare, OutputPluginCallbacks::begin_prepare_cb, begin_prepare_cb_wrapper(), LogicalDecodingContext::callbacks, change_cb_wrapper(), ReorderBuffer::commit, commit_cb_wrapper(), ReorderBuffer::commit_prepared, OutputPluginCallbacks::commit_prepared_cb, commit_prepared_cb_wrapper(), LogicalDecodingContext::context, CurrentMemoryContext, ReplicationSlot::data, ereport, errcode(), errdetail(), errmsg(), ERROR, LogicalDecodingContext::fast_forward, OutputPluginCallbacks::filter_prepare_cb, IsTransactionOrTransactionBlock(), LoadOutputPlugin(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), makeStringInfo(), MemoryContextSwitchTo(), ReorderBuffer::message, message_cb_wrapper(), MyProc, MyReplicationSlot, NameStr, LogicalDecodingContext::out, LogicalDecodingContext::output_plugin_options, palloc0(), PGPROC::pgxactoff, ReplicationSlotPersistentData::plugin, ReorderBuffer::prepare, OutputPluginCallbacks::prepare_cb, prepare_cb_wrapper(), LogicalDecodingContext::prepare_write, ReorderBuffer::private_data, PROC_IN_LOGICAL_DECODING, ProcGlobal, LogicalDecodingContext::reader, LogicalDecodingContext::reorder, ReorderBufferAllocate(), ReorderBuffer::rollback_prepared, OutputPluginCallbacks::rollback_prepared_cb, rollback_prepared_cb_wrapper(), LogicalDecodingContext::slot, LogicalDecodingContext::snapshot_builder, PGPROC::statusFlags, PROC_HDR::statusFlags, ReorderBuffer::stream_abort, OutputPluginCallbacks::stream_abort_cb, stream_abort_cb_wrapper(), ReorderBuffer::stream_change, OutputPluginCallbacks::stream_change_cb, stream_change_cb_wrapper(), ReorderBuffer::stream_commit, OutputPluginCallbacks::stream_commit_cb, stream_commit_cb_wrapper(), ReorderBuffer::stream_message, OutputPluginCallbacks::stream_message_cb, stream_message_cb_wrapper(), ReorderBuffer::stream_prepare, OutputPluginCallbacks::stream_prepare_cb, stream_prepare_cb_wrapper(), ReorderBuffer::stream_start, OutputPluginCallbacks::stream_start_cb, stream_start_cb_wrapper(), ReorderBuffer::stream_stop, OutputPluginCallbacks::stream_stop_cb, stream_stop_cb_wrapper(), ReorderBuffer::stream_truncate, OutputPluginCallbacks::stream_truncate_cb, stream_truncate_cb_wrapper(), LogicalDecodingContext::streaming, truncate_cb_wrapper(), ReplicationSlotPersistentData::two_phase_at, LogicalDecodingContext::twophase, LogicalDecodingContext::update_progress, ReorderBuffer::update_progress_txn, update_progress_txn_cb_wrapper(), wal_segment_size, LogicalDecodingContext::write, and XLogReaderAllocate().

Referenced by CreateDecodingContext(), and CreateInitDecodingContext().

◆ stream_abort_cb_wrapper()

static void stream_abort_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn,
XLogRecPtr  abort_lsn 
)
static

Definition at line 1362 of file logical.c.

1364 {
1365  LogicalDecodingContext *ctx = cache->private_data;
1367  ErrorContextCallback errcallback;
1368 
1369  Assert(!ctx->fast_forward);
1370 
1371  /* We're only supposed to call this when streaming is supported. */
1372  Assert(ctx->streaming);
1373 
1374  /* Push callback + info on the error context stack */
1375  state.ctx = ctx;
1376  state.callback_name = "stream_abort";
1377  state.report_location = abort_lsn;
1378  errcallback.callback = output_plugin_error_callback;
1379  errcallback.arg = (void *) &state;
1380  errcallback.previous = error_context_stack;
1381  error_context_stack = &errcallback;
1382 
1383  /* set output state */
1384  ctx->accept_writes = true;
1385  ctx->write_xid = txn->xid;
1386  ctx->write_location = abort_lsn;
1387  ctx->end_xact = true;
1388 
1389  /* in streaming mode, stream_abort_cb is required */
1390  if (ctx->callbacks.stream_abort_cb == NULL)
1391  ereport(ERROR,
1392  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1393  errmsg("logical streaming requires a %s callback",
1394  "stream_abort_cb")));
1395 
1396  ctx->callbacks.stream_abort_cb(ctx, txn, abort_lsn);
1397 
1398  /* Pop the error context stack */
1399  error_context_stack = errcallback.previous;
1400 }

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert(), ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, ereport, errcode(), errmsg(), ERROR, error_context_stack, LogicalDecodingContext::fast_forward, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, OutputPluginCallbacks::stream_abort_cb, LogicalDecodingContext::streaming, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ stream_change_cb_wrapper()

static void stream_change_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn,
Relation  relation,
ReorderBufferChange change 
)
static

Definition at line 1489 of file logical.c.

1491 {
1492  LogicalDecodingContext *ctx = cache->private_data;
1494  ErrorContextCallback errcallback;
1495 
1496  Assert(!ctx->fast_forward);
1497 
1498  /* We're only supposed to call this when streaming is supported. */
1499  Assert(ctx->streaming);
1500 
1501  /* Push callback + info on the error context stack */
1502  state.ctx = ctx;
1503  state.callback_name = "stream_change";
1504  state.report_location = change->lsn;
1505  errcallback.callback = output_plugin_error_callback;
1506  errcallback.arg = (void *) &state;
1507  errcallback.previous = error_context_stack;
1508  error_context_stack = &errcallback;
1509 
1510  /* set output state */
1511  ctx->accept_writes = true;
1512  ctx->write_xid = txn->xid;
1513 
1514  /*
1515  * Report this change's lsn so replies from clients can give an up-to-date
1516  * answer. This won't ever be enough (and shouldn't be!) to confirm
1517  * receipt of this transaction, but it might allow another transaction's
1518  * commit to be confirmed with one message.
1519  */
1520  ctx->write_location = change->lsn;
1521 
1522  ctx->end_xact = false;
1523 
1524  /* in streaming mode, stream_change_cb is required */
1525  if (ctx->callbacks.stream_change_cb == NULL)
1526  ereport(ERROR,
1527  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1528  errmsg("logical streaming requires a %s callback",
1529  "stream_change_cb")));
1530 
1531  ctx->callbacks.stream_change_cb(ctx, txn, relation, change);
1532 
1533  /* Pop the error context stack */
1534  error_context_stack = errcallback.previous;
1535 }

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert(), ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, ereport, errcode(), errmsg(), ERROR, error_context_stack, LogicalDecodingContext::fast_forward, ReorderBufferChange::lsn, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, OutputPluginCallbacks::stream_change_cb, LogicalDecodingContext::streaming, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ stream_commit_cb_wrapper()

static void stream_commit_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn,
XLogRecPtr  commit_lsn 
)
static

Definition at line 1448 of file logical.c.

1450 {
1451  LogicalDecodingContext *ctx = cache->private_data;
1453  ErrorContextCallback errcallback;
1454 
1455  Assert(!ctx->fast_forward);
1456 
1457  /* We're only supposed to call this when streaming is supported. */
1458  Assert(ctx->streaming);
1459 
1460  /* Push callback + info on the error context stack */
1461  state.ctx = ctx;
1462  state.callback_name = "stream_commit";
1463  state.report_location = txn->final_lsn;
1464  errcallback.callback = output_plugin_error_callback;
1465  errcallback.arg = (void *) &state;
1466  errcallback.previous = error_context_stack;
1467  error_context_stack = &errcallback;
1468 
1469  /* set output state */
1470  ctx->accept_writes = true;
1471  ctx->write_xid = txn->xid;
1472  ctx->write_location = txn->end_lsn;
1473  ctx->end_xact = true;
1474 
1475  /* in streaming mode, stream_commit_cb is required */
1476  if (ctx->callbacks.stream_commit_cb == NULL)
1477  ereport(ERROR,
1478  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1479  errmsg("logical streaming requires a %s callback",
1480  "stream_commit_cb")));
1481 
1482  ctx->callbacks.stream_commit_cb(ctx, txn, commit_lsn);
1483 
1484  /* Pop the error context stack */
1485  error_context_stack = errcallback.previous;
1486 }

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert(), ErrorContextCallback::callback, LogicalDecodingContext::callbacks, ReorderBufferTXN::end_lsn, LogicalDecodingContext::end_xact, ereport, errcode(), errmsg(), ERROR, error_context_stack, LogicalDecodingContext::fast_forward, ReorderBufferTXN::final_lsn, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, OutputPluginCallbacks::stream_commit_cb, LogicalDecodingContext::streaming, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ stream_message_cb_wrapper()

static void stream_message_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn,
XLogRecPtr  message_lsn,
bool  transactional,
const char *  prefix,
Size  message_size,
const char *  message 
)
static

Definition at line 1538 of file logical.c.

1541 {
1542  LogicalDecodingContext *ctx = cache->private_data;
1544  ErrorContextCallback errcallback;
1545 
1546  Assert(!ctx->fast_forward);
1547 
1548  /* We're only supposed to call this when streaming is supported. */
1549  Assert(ctx->streaming);
1550 
1551  /* this callback is optional */
1552  if (ctx->callbacks.stream_message_cb == NULL)
1553  return;
1554 
1555  /* Push callback + info on the error context stack */
1556  state.ctx = ctx;
1557  state.callback_name = "stream_message";
1558  state.report_location = message_lsn;
1559  errcallback.callback = output_plugin_error_callback;
1560  errcallback.arg = (void *) &state;
1561  errcallback.previous = error_context_stack;
1562  error_context_stack = &errcallback;
1563 
1564  /* set output state */
1565  ctx->accept_writes = true;
1566  ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
1567  ctx->write_location = message_lsn;
1568  ctx->end_xact = false;
1569 
1570  /* do the actual work: call callback */
1571  ctx->callbacks.stream_message_cb(ctx, txn, message_lsn, transactional, prefix,
1572  message_size, message);
1573 
1574  /* Pop the error context stack */
1575  error_context_stack = errcallback.previous;
1576 }

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert(), ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, error_context_stack, LogicalDecodingContext::fast_forward, InvalidTransactionId, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, OutputPluginCallbacks::stream_message_cb, LogicalDecodingContext::streaming, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ stream_prepare_cb_wrapper()

static void stream_prepare_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn,
XLogRecPtr  prepare_lsn 
)
static

Definition at line 1403 of file logical.c.

1405 {
1406  LogicalDecodingContext *ctx = cache->private_data;
1408  ErrorContextCallback errcallback;
1409 
1410  Assert(!ctx->fast_forward);
1411 
1412  /*
1413  * We're only supposed to call this when streaming and two-phase commits
1414  * are supported.
1415  */
1416  Assert(ctx->streaming);
1417  Assert(ctx->twophase);
1418 
1419  /* Push callback + info on the error context stack */
1420  state.ctx = ctx;
1421  state.callback_name = "stream_prepare";
1422  state.report_location = txn->final_lsn;
1423  errcallback.callback = output_plugin_error_callback;
1424  errcallback.arg = (void *) &state;
1425  errcallback.previous = error_context_stack;
1426  error_context_stack = &errcallback;
1427 
1428  /* set output state */
1429  ctx->accept_writes = true;
1430  ctx->write_xid = txn->xid;
1431  ctx->write_location = txn->end_lsn;
1432  ctx->end_xact = true;
1433 
1434  /* in streaming mode with two-phase commits, stream_prepare_cb is required */
1435  if (ctx->callbacks.stream_prepare_cb == NULL)
1436  ereport(ERROR,
1437  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1438  errmsg("logical streaming at prepare time requires a %s callback",
1439  "stream_prepare_cb")));
1440 
1441  ctx->callbacks.stream_prepare_cb(ctx, txn, prepare_lsn);
1442 
1443  /* Pop the error context stack */
1444  error_context_stack = errcallback.previous;
1445 }

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert(), ErrorContextCallback::callback, LogicalDecodingContext::callbacks, ReorderBufferTXN::end_lsn, LogicalDecodingContext::end_xact, ereport, errcode(), errmsg(), ERROR, error_context_stack, LogicalDecodingContext::fast_forward, ReorderBufferTXN::final_lsn, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, OutputPluginCallbacks::stream_prepare_cb, LogicalDecodingContext::streaming, LogicalDecodingContext::twophase, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ stream_start_cb_wrapper()

static void stream_start_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn,
XLogRecPtr  first_lsn 
)
static

Definition at line 1264 of file logical.c.

1266 {
1267  LogicalDecodingContext *ctx = cache->private_data;
1269  ErrorContextCallback errcallback;
1270 
1271  Assert(!ctx->fast_forward);
1272 
1273  /* We're only supposed to call this when streaming is supported. */
1274  Assert(ctx->streaming);
1275 
1276  /* Push callback + info on the error context stack */
1277  state.ctx = ctx;
1278  state.callback_name = "stream_start";
1279  state.report_location = first_lsn;
1280  errcallback.callback = output_plugin_error_callback;
1281  errcallback.arg = (void *) &state;
1282  errcallback.previous = error_context_stack;
1283  error_context_stack = &errcallback;
1284 
1285  /* set output state */
1286  ctx->accept_writes = true;
1287  ctx->write_xid = txn->xid;
1288 
1289  /*
1290  * Report this message's lsn so replies from clients can give an
1291  * up-to-date answer. This won't ever be enough (and shouldn't be!) to
1292  * confirm receipt of this transaction, but it might allow another
1293  * transaction's commit to be confirmed with one message.
1294  */
1295  ctx->write_location = first_lsn;
1296 
1297  ctx->end_xact = false;
1298 
1299  /* in streaming mode, stream_start_cb is required */
1300  if (ctx->callbacks.stream_start_cb == NULL)
1301  ereport(ERROR,
1302  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1303  errmsg("logical streaming requires a %s callback",
1304  "stream_start_cb")));
1305 
1306  ctx->callbacks.stream_start_cb(ctx, txn);
1307 
1308  /* Pop the error context stack */
1309  error_context_stack = errcallback.previous;
1310 }

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert(), ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, ereport, errcode(), errmsg(), ERROR, error_context_stack, LogicalDecodingContext::fast_forward, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, OutputPluginCallbacks::stream_start_cb, LogicalDecodingContext::streaming, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ stream_stop_cb_wrapper()

static void stream_stop_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn,
XLogRecPtr  last_lsn 
)
static

Definition at line 1313 of file logical.c.

1315 {
1316  LogicalDecodingContext *ctx = cache->private_data;
1318  ErrorContextCallback errcallback;
1319 
1320  Assert(!ctx->fast_forward);
1321 
1322  /* We're only supposed to call this when streaming is supported. */
1323  Assert(ctx->streaming);
1324 
1325  /* Push callback + info on the error context stack */
1326  state.ctx = ctx;
1327  state.callback_name = "stream_stop";
1328  state.report_location = last_lsn;
1329  errcallback.callback = output_plugin_error_callback;
1330  errcallback.arg = (void *) &state;
1331  errcallback.previous = error_context_stack;
1332  error_context_stack = &errcallback;
1333 
1334  /* set output state */
1335  ctx->accept_writes = true;
1336  ctx->write_xid = txn->xid;
1337 
1338  /*
1339  * Report this message's lsn so replies from clients can give an
1340  * up-to-date answer. This won't ever be enough (and shouldn't be!) to
1341  * confirm receipt of this transaction, but it might allow another
1342  * transaction's commit to be confirmed with one message.
1343  */
1344  ctx->write_location = last_lsn;
1345 
1346  ctx->end_xact = false;
1347 
1348  /* in streaming mode, stream_stop_cb is required */
1349  if (ctx->callbacks.stream_stop_cb == NULL)
1350  ereport(ERROR,
1351  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1352  errmsg("logical streaming requires a %s callback",
1353  "stream_stop_cb")));
1354 
1355  ctx->callbacks.stream_stop_cb(ctx, txn);
1356 
1357  /* Pop the error context stack */
1358  error_context_stack = errcallback.previous;
1359 }

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert(), ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, ereport, errcode(), errmsg(), ERROR, error_context_stack, LogicalDecodingContext::fast_forward, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, OutputPluginCallbacks::stream_stop_cb, LogicalDecodingContext::streaming, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ stream_truncate_cb_wrapper()

static void stream_truncate_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn,
int  nrelations,
Relation  relations[],
ReorderBufferChange change 
)
static

Definition at line 1579 of file logical.c.

1582 {
1583  LogicalDecodingContext *ctx = cache->private_data;
1585  ErrorContextCallback errcallback;
1586 
1587  Assert(!ctx->fast_forward);
1588 
1589  /* We're only supposed to call this when streaming is supported. */
1590  Assert(ctx->streaming);
1591 
1592  /* this callback is optional */
1593  if (!ctx->callbacks.stream_truncate_cb)
1594  return;
1595 
1596  /* Push callback + info on the error context stack */
1597  state.ctx = ctx;
1598  state.callback_name = "stream_truncate";
1599  state.report_location = change->lsn;
1600  errcallback.callback = output_plugin_error_callback;
1601  errcallback.arg = (void *) &state;
1602  errcallback.previous = error_context_stack;
1603  error_context_stack = &errcallback;
1604 
1605  /* set output state */
1606  ctx->accept_writes = true;
1607  ctx->write_xid = txn->xid;
1608 
1609  /*
1610  * Report this change's lsn so replies from clients can give an up-to-date
1611  * answer. This won't ever be enough (and shouldn't be!) to confirm
1612  * receipt of this transaction, but it might allow another transaction's
1613  * commit to be confirmed with one message.
1614  */
1615  ctx->write_location = change->lsn;
1616 
1617  ctx->end_xact = false;
1618 
1619  ctx->callbacks.stream_truncate_cb(ctx, txn, nrelations, relations, change);
1620 
1621  /* Pop the error context stack */
1622  error_context_stack = errcallback.previous;
1623 }

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert(), ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, error_context_stack, LogicalDecodingContext::fast_forward, ReorderBufferChange::lsn, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, OutputPluginCallbacks::stream_truncate_cb, LogicalDecodingContext::streaming, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ truncate_cb_wrapper()

static void truncate_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn,
int  nrelations,
Relation  relations[],
ReorderBufferChange change 
)
static

Definition at line 1122 of file logical.c.

1124 {
1125  LogicalDecodingContext *ctx = cache->private_data;
1127  ErrorContextCallback errcallback;
1128 
1129  Assert(!ctx->fast_forward);
1130 
1131  if (!ctx->callbacks.truncate_cb)
1132  return;
1133 
1134  /* Push callback + info on the error context stack */
1135  state.ctx = ctx;
1136  state.callback_name = "truncate";
1137  state.report_location = change->lsn;
1138  errcallback.callback = output_plugin_error_callback;
1139  errcallback.arg = (void *) &state;
1140  errcallback.previous = error_context_stack;
1141  error_context_stack = &errcallback;
1142 
1143  /* set output state */
1144  ctx->accept_writes = true;
1145  ctx->write_xid = txn->xid;
1146 
1147  /*
1148  * Report this change's lsn so replies from clients can give an up-to-date
1149  * answer. This won't ever be enough (and shouldn't be!) to confirm
1150  * receipt of this transaction, but it might allow another transaction's
1151  * commit to be confirmed with one message.
1152  */
1153  ctx->write_location = change->lsn;
1154 
1155  ctx->end_xact = false;
1156 
1157  ctx->callbacks.truncate_cb(ctx, txn, nrelations, relations, change);
1158 
1159  /* Pop the error context stack */
1160  error_context_stack = errcallback.previous;
1161 }
LogicalDecodeTruncateCB truncate_cb

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert(), ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, error_context_stack, LogicalDecodingContext::fast_forward, ReorderBufferChange::lsn, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, OutputPluginCallbacks::truncate_cb, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ update_progress_txn_cb_wrapper()

static void update_progress_txn_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn,
XLogRecPtr  lsn 
)
static

Definition at line 1626 of file logical.c.

1628 {
1629  LogicalDecodingContext *ctx = cache->private_data;
1631  ErrorContextCallback errcallback;
1632 
1633  Assert(!ctx->fast_forward);
1634 
1635  /* Push callback + info on the error context stack */
1636  state.ctx = ctx;
1637  state.callback_name = "update_progress_txn";
1638  state.report_location = lsn;
1639  errcallback.callback = output_plugin_error_callback;
1640  errcallback.arg = (void *) &state;
1641  errcallback.previous = error_context_stack;
1642  error_context_stack = &errcallback;
1643 
1644  /* set output state */
1645  ctx->accept_writes = false;
1646  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  ctx->write_location = lsn;
1655 
1656  ctx->end_xact = false;
1657 
1658  OutputPluginUpdateProgress(ctx, false);
1659 
1660  /* Pop the error context stack */
1661  error_context_stack = errcallback.previous;
1662 }
void OutputPluginUpdateProgress(struct LogicalDecodingContext *ctx, bool skipped_xact)
Definition: logical.c:715

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert(), ErrorContextCallback::callback, LogicalDecodingContext::end_xact, error_context_stack, LogicalDecodingContext::fast_forward, output_plugin_error_callback(), OutputPluginUpdateProgress(), ErrorContextCallback::previous, ReorderBuffer::private_data, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ UpdateDecodingStats()

void UpdateDecodingStats ( LogicalDecodingContext ctx)

Definition at line 1912 of file logical.c.

1913 {
1914  ReorderBuffer *rb = ctx->reorder;
1915  PgStat_StatReplSlotEntry repSlotStat;
1916 
1917  /* Nothing to do if we don't have any replication stats to be sent. */
1918  if (rb->spillBytes <= 0 && rb->streamBytes <= 0 && rb->totalBytes <= 0)
1919  return;
1920 
1921  elog(DEBUG2, "UpdateDecodingStats: updating stats %p %lld %lld %lld %lld %lld %lld %lld %lld",
1922  rb,
1923  (long long) rb->spillTxns,
1924  (long long) rb->spillCount,
1925  (long long) rb->spillBytes,
1926  (long long) rb->streamTxns,
1927  (long long) rb->streamCount,
1928  (long long) rb->streamBytes,
1929  (long long) rb->totalTxns,
1930  (long long) rb->totalBytes);
1931 
1932  repSlotStat.spill_txns = rb->spillTxns;
1933  repSlotStat.spill_count = rb->spillCount;
1934  repSlotStat.spill_bytes = rb->spillBytes;
1935  repSlotStat.stream_txns = rb->streamTxns;
1936  repSlotStat.stream_count = rb->streamCount;
1937  repSlotStat.stream_bytes = rb->streamBytes;
1938  repSlotStat.total_txns = rb->totalTxns;
1939  repSlotStat.total_bytes = rb->totalBytes;
1940 
1941  pgstat_report_replslot(ctx->slot, &repSlotStat);
1942 
1943  rb->spillTxns = 0;
1944  rb->spillCount = 0;
1945  rb->spillBytes = 0;
1946  rb->streamTxns = 0;
1947  rb->streamCount = 0;
1948  rb->streamBytes = 0;
1949  rb->totalTxns = 0;
1950  rb->totalBytes = 0;
1951 }
#define DEBUG2
Definition: elog.h:29
void pgstat_report_replslot(ReplicationSlot *slot, const PgStat_StatReplSlotEntry *repSlotStat)
PgStat_Counter stream_count
Definition: pgstat.h:371
PgStat_Counter total_txns
Definition: pgstat.h:373
PgStat_Counter total_bytes
Definition: pgstat.h:374
PgStat_Counter spill_txns
Definition: pgstat.h:367
PgStat_Counter stream_txns
Definition: pgstat.h:370
PgStat_Counter spill_count
Definition: pgstat.h:368
PgStat_Counter stream_bytes
Definition: pgstat.h:372
PgStat_Counter spill_bytes
Definition: pgstat.h:369

References DEBUG2, elog(), pgstat_report_replslot(), LogicalDecodingContext::reorder, LogicalDecodingContext::slot, PgStat_StatReplSlotEntry::spill_bytes, PgStat_StatReplSlotEntry::spill_count, PgStat_StatReplSlotEntry::spill_txns, ReorderBuffer::spillBytes, ReorderBuffer::spillCount, ReorderBuffer::spillTxns, PgStat_StatReplSlotEntry::stream_bytes, PgStat_StatReplSlotEntry::stream_count, PgStat_StatReplSlotEntry::stream_txns, ReorderBuffer::streamBytes, ReorderBuffer::streamCount, ReorderBuffer::streamTxns, PgStat_StatReplSlotEntry::total_bytes, PgStat_StatReplSlotEntry::total_txns, ReorderBuffer::totalBytes, and ReorderBuffer::totalTxns.

Referenced by DecodeAbort(), DecodeCommit(), DecodePrepare(), ReorderBufferSerializeTXN(), and ReorderBufferStreamTXN().