PostgreSQL Source Code  git master
logical.c File Reference
#include "postgres.h"
#include "access/xact.h"
#include "access/xlogutils.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/inval.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)
 
bool LogicalReplicationSlotHasPendingWal (XLogRecPtr end_of_wal)
 

Typedef Documentation

◆ LogicalErrorCallbackState

Function Documentation

◆ begin_cb_wrapper()

static void begin_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn 
)
static

Definition at line 846 of file logical.c.

847 {
848  LogicalDecodingContext *ctx = cache->private_data;
850  ErrorContextCallback errcallback;
851 
852  Assert(!ctx->fast_forward);
853 
854  /* Push callback + info on the error context stack */
855  state.ctx = ctx;
856  state.callback_name = "begin";
857  state.report_location = txn->first_lsn;
859  errcallback.arg = (void *) &state;
860  errcallback.previous = error_context_stack;
861  error_context_stack = &errcallback;
862 
863  /* set output state */
864  ctx->accept_writes = true;
865  ctx->write_xid = txn->xid;
866  ctx->write_location = txn->first_lsn;
867  ctx->end_xact = false;
868 
869  /* do the actual work: call callback */
870  ctx->callbacks.begin_cb(ctx, txn);
871 
872  /* Pop the error context stack */
873  error_context_stack = errcallback.previous;
874 }
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:766
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 916 of file logical.c.

917 {
918  LogicalDecodingContext *ctx = cache->private_data;
920  ErrorContextCallback errcallback;
921 
922  Assert(!ctx->fast_forward);
923 
924  /* We're only supposed to call this when two-phase commits are supported */
925  Assert(ctx->twophase);
926 
927  /* Push callback + info on the error context stack */
928  state.ctx = ctx;
929  state.callback_name = "begin_prepare";
930  state.report_location = txn->first_lsn;
932  errcallback.arg = (void *) &state;
933  errcallback.previous = error_context_stack;
934  error_context_stack = &errcallback;
935 
936  /* set output state */
937  ctx->accept_writes = true;
938  ctx->write_xid = txn->xid;
939  ctx->write_location = txn->first_lsn;
940  ctx->end_xact = false;
941 
942  /*
943  * If the plugin supports two-phase commits then begin prepare callback is
944  * mandatory
945  */
946  if (ctx->callbacks.begin_prepare_cb == NULL)
947  ereport(ERROR,
948  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
949  errmsg("logical replication at prepare time requires a %s callback",
950  "begin_prepare_cb")));
951 
952  /* do the actual work: call callback */
953  ctx->callbacks.begin_prepare_cb(ctx, txn);
954 
955  /* Pop the error context stack */
956  error_context_stack = errcallback.previous;
957 }
int errcode(int sqlerrcode)
Definition: elog.c:860
int errmsg(const char *fmt,...)
Definition: elog.c:1073
#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 1097 of file logical.c.

1099 {
1100  LogicalDecodingContext *ctx = cache->private_data;
1102  ErrorContextCallback errcallback;
1103 
1104  Assert(!ctx->fast_forward);
1105 
1106  /* Push callback + info on the error context stack */
1107  state.ctx = ctx;
1108  state.callback_name = "change";
1109  state.report_location = change->lsn;
1110  errcallback.callback = output_plugin_error_callback;
1111  errcallback.arg = (void *) &state;
1112  errcallback.previous = error_context_stack;
1113  error_context_stack = &errcallback;
1114 
1115  /* set output state */
1116  ctx->accept_writes = true;
1117  ctx->write_xid = txn->xid;
1118 
1119  /*
1120  * Report this change's lsn so replies from clients can give an up-to-date
1121  * answer. This won't ever be enough (and shouldn't be!) to confirm
1122  * receipt of this transaction, but it might allow another transaction's
1123  * commit to be confirmed with one message.
1124  */
1125  ctx->write_location = change->lsn;
1126 
1127  ctx->end_xact = false;
1128 
1129  ctx->callbacks.change_cb(ctx, txn, relation, change);
1130 
1131  /* Pop the error context stack */
1132  error_context_stack = errcallback.previous;
1133 }
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 110 of file logical.c.

111 {
113 
114  /*
115  * NB: Adding a new requirement likely means that RestoreSlotFromDisk()
116  * needs the same check.
117  */
118 
120  ereport(ERROR,
121  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
122  errmsg("logical decoding requires wal_level >= logical")));
123 
124  if (MyDatabaseId == InvalidOid)
125  ereport(ERROR,
126  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
127  errmsg("logical decoding requires a database connection")));
128 
129  if (RecoveryInProgress())
130  {
131  /*
132  * This check may have race conditions, but whenever
133  * XLOG_PARAMETER_CHANGE indicates that wal_level has changed, we
134  * verify that there are no existing logical replication slots. And to
135  * avoid races around creating a new slot,
136  * CheckLogicalDecodingRequirements() is called once before creating
137  * the slot, and once when logical decoding is initially starting up.
138  */
140  ereport(ERROR,
141  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
142  errmsg("logical decoding on standby requires wal_level >= logical on the primary")));
143  }
144 }
Oid MyDatabaseId
Definition: globals.c:90
#define InvalidOid
Definition: postgres_ext.h:36
void CheckSlotRequirements(void)
Definition: slot.c:1298
bool RecoveryInProgress(void)
Definition: xlog.c:6206
int wal_level
Definition: xlog.c:136
WalLevel GetActiveWalLevelOnStandby(void)
Definition: xlog.c:4754
@ WAL_LEVEL_LOGICAL
Definition: xlog.h:74

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 877 of file logical.c.

879 {
880  LogicalDecodingContext *ctx = cache->private_data;
882  ErrorContextCallback errcallback;
883 
884  Assert(!ctx->fast_forward);
885 
886  /* Push callback + info on the error context stack */
887  state.ctx = ctx;
888  state.callback_name = "commit";
889  state.report_location = txn->final_lsn; /* beginning of commit record */
891  errcallback.arg = (void *) &state;
892  errcallback.previous = error_context_stack;
893  error_context_stack = &errcallback;
894 
895  /* set output state */
896  ctx->accept_writes = true;
897  ctx->write_xid = txn->xid;
898  ctx->write_location = txn->end_lsn; /* points to the end of the record */
899  ctx->end_xact = true;
900 
901  /* do the actual work: call callback */
902  ctx->callbacks.commit_cb(ctx, txn, commit_lsn);
903 
904  /* Pop the error context stack */
905  error_context_stack = errcallback.previous;
906 }
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 1005 of file logical.c.

1007 {
1008  LogicalDecodingContext *ctx = cache->private_data;
1010  ErrorContextCallback errcallback;
1011 
1012  Assert(!ctx->fast_forward);
1013 
1014  /* We're only supposed to call this when two-phase commits are supported */
1015  Assert(ctx->twophase);
1016 
1017  /* Push callback + info on the error context stack */
1018  state.ctx = ctx;
1019  state.callback_name = "commit_prepared";
1020  state.report_location = txn->final_lsn; /* beginning of commit record */
1021  errcallback.callback = output_plugin_error_callback;
1022  errcallback.arg = (void *) &state;
1023  errcallback.previous = error_context_stack;
1024  error_context_stack = &errcallback;
1025 
1026  /* set output state */
1027  ctx->accept_writes = true;
1028  ctx->write_xid = txn->xid;
1029  ctx->write_location = txn->end_lsn; /* points to the end of the record */
1030  ctx->end_xact = true;
1031 
1032  /*
1033  * If the plugin support two-phase commits then commit prepared callback
1034  * is mandatory
1035  */
1036  if (ctx->callbacks.commit_prepared_cb == NULL)
1037  ereport(ERROR,
1038  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1039  errmsg("logical replication at prepare time requires a %s callback",
1040  "commit_prepared_cb")));
1041 
1042  /* do the actual work: call callback */
1043  ctx->callbacks.commit_prepared_cb(ctx, txn, commit_lsn);
1044 
1045  /* Pop the error context stack */
1046  error_context_stack = errcallback.previous;
1047 }
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 496 of file logical.c.

503 {
505  ReplicationSlot *slot;
506  MemoryContext old_context;
507 
508  /* shorter lines... */
509  slot = MyReplicationSlot;
510 
511  /* first some sanity checks that are unlikely to be violated */
512  if (slot == NULL)
513  elog(ERROR, "cannot perform logical decoding without an acquired slot");
514 
515  /* make sure the passed slot is suitable, these are user facing errors */
516  if (SlotIsPhysical(slot))
517  ereport(ERROR,
518  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
519  errmsg("cannot use physical replication slot for logical decoding")));
520 
521  if (slot->data.database != MyDatabaseId)
522  ereport(ERROR,
523  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
524  errmsg("replication slot \"%s\" was not created in this database",
525  NameStr(slot->data.name))));
526 
527  /*
528  * Do not allow consumption of a "synchronized" slot until the standby
529  * gets promoted.
530  */
531  if (RecoveryInProgress() && slot->data.synced)
532  ereport(ERROR,
533  errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
534  errmsg("cannot use replication slot \"%s\" for logical decoding",
535  NameStr(slot->data.name)),
536  errdetail("This slot is being synchronized from the primary server."),
537  errhint("Specify another replication slot."));
538 
539  /*
540  * Check if slot has been invalidated due to max_slot_wal_keep_size. Avoid
541  * "cannot get changes" wording in this errmsg because that'd be
542  * confusingly ambiguous about no changes being available when called from
543  * pg_logical_slot_get_changes_guts().
544  */
546  ereport(ERROR,
547  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
548  errmsg("can no longer get changes from replication slot \"%s\"",
550  errdetail("This slot has been invalidated because it exceeded the maximum reserved size.")));
551 
553  ereport(ERROR,
554  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
555  errmsg("can no longer get changes from replication slot \"%s\"",
557  errdetail("This slot has been invalidated because it was conflicting with recovery.")));
558 
561 
562  if (start_lsn == InvalidXLogRecPtr)
563  {
564  /* continue from last position */
565  start_lsn = slot->data.confirmed_flush;
566  }
567  else if (start_lsn < slot->data.confirmed_flush)
568  {
569  /*
570  * It might seem like we should error out in this case, but it's
571  * pretty common for a client to acknowledge a LSN it doesn't have to
572  * do anything for, and thus didn't store persistently, because the
573  * xlog records didn't result in anything relevant for logical
574  * decoding. Clients have to be able to do that to support synchronous
575  * replication.
576  *
577  * Starting at a different LSN than requested might not catch certain
578  * kinds of client errors; so the client may wish to check that
579  * confirmed_flush_lsn matches its expectations.
580  */
581  elog(LOG, "%X/%X has been already streamed, forwarding to %X/%X",
582  LSN_FORMAT_ARGS(start_lsn),
584 
585  start_lsn = slot->data.confirmed_flush;
586  }
587 
588  ctx = StartupDecodingContext(output_plugin_options,
589  start_lsn, InvalidTransactionId, false,
590  fast_forward, xl_routine, prepare_write,
591  do_write, update_progress);
592 
593  /* call output plugin initialization callback */
594  old_context = MemoryContextSwitchTo(ctx->context);
595  if (ctx->callbacks.startup_cb != NULL)
596  startup_cb_wrapper(ctx, &ctx->options, false);
597  MemoryContextSwitchTo(old_context);
598 
599  /*
600  * We allow decoding of prepared transactions when the two_phase is
601  * enabled at the time of slot creation, or when the two_phase option is
602  * given at the streaming start, provided the plugin supports all the
603  * callbacks for two-phase.
604  */
605  ctx->twophase &= (slot->data.two_phase || ctx->twophase_opt_given);
606 
607  /* Mark slot to allow two_phase decoding if not already marked */
608  if (ctx->twophase && !slot->data.two_phase)
609  {
610  SpinLockAcquire(&slot->mutex);
611  slot->data.two_phase = true;
612  slot->data.two_phase_at = start_lsn;
613  SpinLockRelease(&slot->mutex);
616  SnapBuildSetTwoPhaseAt(ctx->snapshot_builder, start_lsn);
617  }
618 
620 
621  ereport(LOG,
622  (errmsg("starting logical decoding for slot \"%s\"",
623  NameStr(slot->data.name)),
624  errdetail("Streaming transactions committing after %X/%X, reading WAL from %X/%X.",
626  LSN_FORMAT_ARGS(slot->data.restart_lsn))));
627 
628  return ctx;
629 }
#define NameStr(name)
Definition: c.h:733
int errdetail(const char *fmt,...)
Definition: elog.c:1206
int errhint(const char *fmt,...)
Definition: elog.c:1320
#define LOG
Definition: elog.h:31
#define elog(elevel,...)
Definition: elog.h:224
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:151
static void startup_cb_wrapper(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
Definition: logical.c:785
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
const void * data
void ReplicationSlotMarkDirty(void)
Definition: slot.c:946
ReplicationSlot * MyReplicationSlot
Definition: slot.c:116
void ReplicationSlotSave(void)
Definition: slot.c:928
#define SlotIsPhysical(slot)
Definition: slot.h:206
@ RS_INVAL_WAL_REMOVED
Definition: slot.h:51
@ RS_INVAL_NONE
Definition: slot.h:49
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:93
XLogRecPtr confirmed_flush
Definition: slot.h:104
ReplicationSlotInvalidationCause invalidated
Definition: slot.h:96
slock_t mutex
Definition: slot.h:151
ReplicationSlotPersistentData data
Definition: slot.h:178
#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(), errhint(), 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, RecoveryInProgress(), 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::synced, ReplicationSlotPersistentData::two_phase, ReplicationSlotPersistentData::two_phase_at, LogicalDecodingContext::twophase, and LogicalDecodingContext::twophase_opt_given.

Referenced by LogicalReplicationSlotHasPendingWal(), 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 330 of file logical.c.

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

645 {
646  ReplicationSlot *slot = ctx->slot;
647 
648  /* Initialize from where to start reading WAL. */
649  XLogBeginRead(ctx->reader, slot->data.restart_lsn);
650 
651  elog(DEBUG1, "searching for logical decoding starting point, starting at %X/%X",
653 
654  /* Wait for a consistent starting point */
655  for (;;)
656  {
657  XLogRecord *record;
658  char *err = NULL;
659 
660  /* the read_page callback waits for new WAL */
661  record = XLogReadRecord(ctx->reader, &err);
662  if (err)
663  elog(ERROR, "could not find logical decoding starting point: %s", err);
664  if (!record)
665  elog(ERROR, "could not find logical decoding starting point");
666 
668 
669  /* only continue till we found a consistent spot */
670  if (DecodingContextReady(ctx))
671  break;
672 
674  }
675 
676  SpinLockAcquire(&slot->mutex);
677  slot->data.confirmed_flush = ctx->reader->EndRecPtr;
678  if (slot->data.two_phase)
679  slot->data.two_phase_at = ctx->reader->EndRecPtr;
680  SpinLockRelease(&slot->mutex);
681 }
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:635
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
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:391
void XLogBeginRead(XLogReaderState *state, XLogRecPtr RecPtr)
Definition: xlogreader.c:233

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 635 of file logical.c.

636 {
638 }
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 1210 of file logical.c.

1211 {
1213  ErrorContextCallback errcallback;
1214  bool ret;
1215 
1216  Assert(!ctx->fast_forward);
1217 
1218  /* Push callback + info on the error context stack */
1219  state.ctx = ctx;
1220  state.callback_name = "filter_by_origin";
1221  state.report_location = InvalidXLogRecPtr;
1222  errcallback.callback = output_plugin_error_callback;
1223  errcallback.arg = (void *) &state;
1224  errcallback.previous = error_context_stack;
1225  error_context_stack = &errcallback;
1226 
1227  /* set output state */
1228  ctx->accept_writes = false;
1229  ctx->end_xact = false;
1230 
1231  /* do the actual work: call callback */
1232  ret = ctx->callbacks.filter_by_origin_cb(ctx, origin_id);
1233 
1234  /* Pop the error context stack */
1235  error_context_stack = errcallback.previous;
1236 
1237  return ret;
1238 }
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 1178 of file logical.c.

1180 {
1182  ErrorContextCallback errcallback;
1183  bool ret;
1184 
1185  Assert(!ctx->fast_forward);
1186 
1187  /* Push callback + info on the error context stack */
1188  state.ctx = ctx;
1189  state.callback_name = "filter_prepare";
1190  state.report_location = InvalidXLogRecPtr;
1191  errcallback.callback = output_plugin_error_callback;
1192  errcallback.arg = (void *) &state;
1193  errcallback.previous = error_context_stack;
1194  error_context_stack = &errcallback;
1195 
1196  /* set output state */
1197  ctx->accept_writes = false;
1198  ctx->end_xact = false;
1199 
1200  /* do the actual work: call callback */
1201  ret = ctx->callbacks.filter_prepare_cb(ctx, xid, gid);
1202 
1203  /* Pop the error context stack */
1204  error_context_stack = errcallback.previous;
1205 
1206  return ret;
1207 }
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)

◆ LoadOutputPlugin()

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

Definition at line 744 of file logical.c.

745 {
746  LogicalOutputPluginInit plugin_init;
747 
748  plugin_init = (LogicalOutputPluginInit)
749  load_external_function(plugin, "_PG_output_plugin_init", false, NULL);
750 
751  if (plugin_init == NULL)
752  elog(ERROR, "output plugins have to declare the _PG_output_plugin_init symbol");
753 
754  /* ask the output plugin to fill the callback struct */
755  plugin_init(callbacks);
756 
757  if (callbacks->begin_cb == NULL)
758  elog(ERROR, "output plugins have to register a begin callback");
759  if (callbacks->change_cb == NULL)
760  elog(ERROR, "output plugins have to register a change callback");
761  if (callbacks->commit_cb == NULL)
762  elog(ERROR, "output plugins have to register a commit callback");
763 }
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 1829 of file logical.c.

1830 {
1831  Assert(lsn != InvalidXLogRecPtr);
1832 
1833  /* Do an unlocked check for candidate_lsn first. */
1836  {
1837  bool updated_xmin = false;
1838  bool updated_restart = false;
1839 
1841 
1843 
1844  /* if we're past the location required for bumping xmin, do so */
1847  {
1848  /*
1849  * We have to write the changed xmin to disk *before* we change
1850  * the in-memory value, otherwise after a crash we wouldn't know
1851  * that some catalog tuples might have been removed already.
1852  *
1853  * Ensure that by first writing to ->xmin and only update
1854  * ->effective_xmin once the new state is synced to disk. After a
1855  * crash ->effective_xmin is set to ->xmin.
1856  */
1859  {
1863  updated_xmin = true;
1864  }
1865  }
1866 
1869  {
1871 
1875  updated_restart = true;
1876  }
1877 
1879 
1880  /* first write new xmin to disk, so we know what's up after a crash */
1881  if (updated_xmin || updated_restart)
1882  {
1885  elog(DEBUG1, "updated xmin: %u restart: %u", updated_xmin, updated_restart);
1886  }
1887 
1888  /*
1889  * Now the new xmin is safely on disk, we can let the global value
1890  * advance. We do not take ProcArrayLock or similar since we only
1891  * advance xmin here and there's not much harm done by a concurrent
1892  * computation missing that.
1893  */
1894  if (updated_xmin)
1895  {
1899 
1902  }
1903  }
1904  else
1905  {
1909  }
1910 }
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:1041
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:194
XLogRecPtr candidate_restart_valid
Definition: slot.h:195
XLogRecPtr candidate_restart_lsn
Definition: slot.h:196
TransactionId candidate_catalog_xmin
Definition: slot.h:193
#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 1755 of file logical.c.

1756 {
1757  bool updated_lsn = false;
1758  ReplicationSlot *slot;
1759 
1760  slot = MyReplicationSlot;
1761 
1762  Assert(slot != NULL);
1763  Assert(restart_lsn != InvalidXLogRecPtr);
1764  Assert(current_lsn != InvalidXLogRecPtr);
1765 
1766  SpinLockAcquire(&slot->mutex);
1767 
1768  /* don't overwrite if have a newer restart lsn */
1769  if (restart_lsn <= slot->data.restart_lsn)
1770  {
1771  }
1772 
1773  /*
1774  * We might have already flushed far enough to directly accept this lsn,
1775  * in this case there is no need to check for existing candidate LSNs
1776  */
1777  else if (current_lsn <= slot->data.confirmed_flush)
1778  {
1779  slot->candidate_restart_valid = current_lsn;
1780  slot->candidate_restart_lsn = restart_lsn;
1781 
1782  /* our candidate can directly be used */
1783  updated_lsn = true;
1784  }
1785 
1786  /*
1787  * Only increase if the previous values have been applied, otherwise we
1788  * might never end up updating if the receiver acks too slowly. A missed
1789  * value here will just cause some extra effort after reconnecting.
1790  */
1792  {
1793  slot->candidate_restart_valid = current_lsn;
1794  slot->candidate_restart_lsn = restart_lsn;
1795  SpinLockRelease(&slot->mutex);
1796 
1797  elog(DEBUG1, "got new restart lsn %X/%X at %X/%X",
1798  LSN_FORMAT_ARGS(restart_lsn),
1799  LSN_FORMAT_ARGS(current_lsn));
1800  }
1801  else
1802  {
1803  XLogRecPtr candidate_restart_lsn;
1804  XLogRecPtr candidate_restart_valid;
1805  XLogRecPtr confirmed_flush;
1806 
1807  candidate_restart_lsn = slot->candidate_restart_lsn;
1808  candidate_restart_valid = slot->candidate_restart_valid;
1809  confirmed_flush = slot->data.confirmed_flush;
1810  SpinLockRelease(&slot->mutex);
1811 
1812  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",
1813  LSN_FORMAT_ARGS(restart_lsn),
1814  LSN_FORMAT_ARGS(current_lsn),
1815  LSN_FORMAT_ARGS(candidate_restart_lsn),
1816  LSN_FORMAT_ARGS(candidate_restart_valid),
1817  LSN_FORMAT_ARGS(confirmed_flush));
1818  }
1819 
1820  /* candidates are already valid with the current flush position, apply */
1821  if (updated_lsn)
1823 }
void LogicalConfirmReceivedLocation(XLogRecPtr lsn)
Definition: logical.c:1829
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 1687 of file logical.c.

1688 {
1689  bool updated_xmin = false;
1690  ReplicationSlot *slot;
1691  bool got_new_xmin = false;
1692 
1693  slot = MyReplicationSlot;
1694 
1695  Assert(slot != NULL);
1696 
1697  SpinLockAcquire(&slot->mutex);
1698 
1699  /*
1700  * don't overwrite if we already have a newer xmin. This can happen if we
1701  * restart decoding in a slot.
1702  */
1704  {
1705  }
1706 
1707  /*
1708  * If the client has already confirmed up to this lsn, we directly can
1709  * mark this as accepted. This can happen if we restart decoding in a
1710  * slot.
1711  */
1712  else if (current_lsn <= slot->data.confirmed_flush)
1713  {
1714  slot->candidate_catalog_xmin = xmin;
1715  slot->candidate_xmin_lsn = current_lsn;
1716 
1717  /* our candidate can directly be used */
1718  updated_xmin = true;
1719  }
1720 
1721  /*
1722  * Only increase if the previous values have been applied, otherwise we
1723  * might never end up updating if the receiver acks too slowly.
1724  */
1725  else if (slot->candidate_xmin_lsn == InvalidXLogRecPtr)
1726  {
1727  slot->candidate_catalog_xmin = xmin;
1728  slot->candidate_xmin_lsn = current_lsn;
1729 
1730  /*
1731  * Log new xmin at an appropriate log level after releasing the
1732  * spinlock.
1733  */
1734  got_new_xmin = true;
1735  }
1736  SpinLockRelease(&slot->mutex);
1737 
1738  if (got_new_xmin)
1739  elog(DEBUG1, "got new catalog xmin %u at %X/%X", xmin,
1740  LSN_FORMAT_ARGS(current_lsn));
1741 
1742  /* candidate already valid with the current flush position, apply */
1743  if (updated_xmin)
1745 }
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().

◆ LogicalReplicationSlotHasPendingWal()

bool LogicalReplicationSlotHasPendingWal ( XLogRecPtr  end_of_wal)

Definition at line 1973 of file logical.c.

1974 {
1975  bool has_pending_wal = false;
1976 
1978 
1979  PG_TRY();
1980  {
1982 
1983  /*
1984  * Create our decoding context in fast_forward mode, passing start_lsn
1985  * as InvalidXLogRecPtr, so that we start processing from the slot's
1986  * confirmed_flush.
1987  */
1989  NIL,
1990  true, /* fast_forward */
1991  XL_ROUTINE(.page_read = read_local_xlog_page,
1992  .segment_open = wal_segment_open,
1993  .segment_close = wal_segment_close),
1994  NULL, NULL, NULL);
1995 
1996  /*
1997  * Start reading at the slot's restart_lsn, which we know points to a
1998  * valid record.
1999  */
2001 
2002  /* Invalidate non-timetravel entries */
2004 
2005  /* Loop until the end of WAL or some changes are processed */
2006  while (!has_pending_wal && ctx->reader->EndRecPtr < end_of_wal)
2007  {
2008  XLogRecord *record;
2009  char *errm = NULL;
2010 
2011  record = XLogReadRecord(ctx->reader, &errm);
2012 
2013  if (errm)
2014  elog(ERROR, "could not find record for logical decoding: %s", errm);
2015 
2016  if (record != NULL)
2018 
2019  has_pending_wal = ctx->processing_required;
2020 
2022  }
2023 
2024  /* Clean up */
2025  FreeDecodingContext(ctx);
2027  }
2028  PG_CATCH();
2029  {
2030  /* clear all timetravel entries */
2032 
2033  PG_RE_THROW();
2034  }
2035  PG_END_TRY();
2036 
2037  return has_pending_wal;
2038 }
#define PG_RE_THROW()
Definition: elog.h:411
#define PG_TRY(...)
Definition: elog.h:370
#define PG_END_TRY(...)
Definition: elog.h:395
#define PG_CATCH(...)
Definition: elog.h:380
void InvalidateSystemCaches(void)
Definition: inval.c:793
void FreeDecodingContext(LogicalDecodingContext *ctx)
Definition: logical.c:688
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: logical.c:496
#define XL_ROUTINE(...)
Definition: xlogreader.h:117
void wal_segment_close(XLogReaderState *state)
Definition: xlogutils.c:845
void wal_segment_open(XLogReaderState *state, XLogSegNo nextSegNo, TimeLineID *tli_p)
Definition: xlogutils.c:820
int read_local_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetRecPtr, char *cur_page)
Definition: xlogutils.c:864

References Assert(), CHECK_FOR_INTERRUPTS, CreateDecodingContext(), ReplicationSlot::data, elog, XLogReaderState::EndRecPtr, ERROR, FreeDecodingContext(), InvalidateSystemCaches(), InvalidXLogRecPtr, LogicalDecodingProcessRecord(), MyReplicationSlot, NIL, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, LogicalDecodingContext::processing_required, read_local_xlog_page(), LogicalDecodingContext::reader, ReplicationSlotPersistentData::restart_lsn, wal_segment_close(), wal_segment_open(), XL_ROUTINE, XLogBeginRead(), and XLogReadRecord().

Referenced by binary_upgrade_logical_slot_has_caught_up().

◆ 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 1241 of file logical.c.

1244 {
1245  LogicalDecodingContext *ctx = cache->private_data;
1247  ErrorContextCallback errcallback;
1248 
1249  Assert(!ctx->fast_forward);
1250 
1251  if (ctx->callbacks.message_cb == NULL)
1252  return;
1253 
1254  /* Push callback + info on the error context stack */
1255  state.ctx = ctx;
1256  state.callback_name = "message";
1257  state.report_location = message_lsn;
1258  errcallback.callback = output_plugin_error_callback;
1259  errcallback.arg = (void *) &state;
1260  errcallback.previous = error_context_stack;
1261  error_context_stack = &errcallback;
1262 
1263  /* set output state */
1264  ctx->accept_writes = true;
1265  ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
1266  ctx->write_location = message_lsn;
1267  ctx->end_xact = false;
1268 
1269  /* do the actual work: call callback */
1270  ctx->callbacks.message_cb(ctx, txn, message_lsn, transactional, prefix,
1271  message_size, message);
1272 
1273  /* Pop the error context stack */
1274  error_context_stack = errcallback.previous;
1275 }
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 766 of file logical.c.

767 {
769 
770  /* not all callbacks have an associated LSN */
771  if (state->report_location != InvalidXLogRecPtr)
772  errcontext("slot \"%s\", output plugin \"%s\", in the %s callback, associated LSN %X/%X",
773  NameStr(state->ctx->slot->data.name),
774  NameStr(state->ctx->slot->data.plugin),
775  state->callback_name,
776  LSN_FORMAT_ARGS(state->report_location));
777  else
778  errcontext("slot \"%s\", output plugin \"%s\", in the %s callback",
779  NameStr(state->ctx->slot->data.name),
780  NameStr(state->ctx->slot->data.plugin),
781  state->callback_name);
782 }
#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 703 of file logical.c.

704 {
705  if (!ctx->accept_writes)
706  elog(ERROR, "writes are only accepted in commit, begin and change callbacks");
707 
708  ctx->prepare_write(ctx, ctx->write_location, ctx->write_xid, last_write);
709  ctx->prepared_write = true;
710 }
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 960 of file logical.c.

962 {
963  LogicalDecodingContext *ctx = cache->private_data;
965  ErrorContextCallback errcallback;
966 
967  Assert(!ctx->fast_forward);
968 
969  /* We're only supposed to call this when two-phase commits are supported */
970  Assert(ctx->twophase);
971 
972  /* Push callback + info on the error context stack */
973  state.ctx = ctx;
974  state.callback_name = "prepare";
975  state.report_location = txn->final_lsn; /* beginning of prepare record */
977  errcallback.arg = (void *) &state;
978  errcallback.previous = error_context_stack;
979  error_context_stack = &errcallback;
980 
981  /* set output state */
982  ctx->accept_writes = true;
983  ctx->write_xid = txn->xid;
984  ctx->write_location = txn->end_lsn; /* points to the end of the record */
985  ctx->end_xact = true;
986 
987  /*
988  * If the plugin supports two-phase commits then prepare callback is
989  * mandatory
990  */
991  if (ctx->callbacks.prepare_cb == NULL)
992  ereport(ERROR,
993  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
994  errmsg("logical replication at prepare time requires a %s callback",
995  "prepare_cb")));
996 
997  /* do the actual work: call callback */
998  ctx->callbacks.prepare_cb(ctx, txn, prepare_lsn);
999 
1000  /* Pop the error context stack */
1001  error_context_stack = errcallback.previous;
1002 }
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 1916 of file logical.c.

1917 {
1919  bsysscan = false;
1920 }
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 1050 of file logical.c.

1053 {
1054  LogicalDecodingContext *ctx = cache->private_data;
1056  ErrorContextCallback errcallback;
1057 
1058  Assert(!ctx->fast_forward);
1059 
1060  /* We're only supposed to call this when two-phase commits are supported */
1061  Assert(ctx->twophase);
1062 
1063  /* Push callback + info on the error context stack */
1064  state.ctx = ctx;
1065  state.callback_name = "rollback_prepared";
1066  state.report_location = txn->final_lsn; /* beginning of commit record */
1067  errcallback.callback = output_plugin_error_callback;
1068  errcallback.arg = (void *) &state;
1069  errcallback.previous = error_context_stack;
1070  error_context_stack = &errcallback;
1071 
1072  /* set output state */
1073  ctx->accept_writes = true;
1074  ctx->write_xid = txn->xid;
1075  ctx->write_location = txn->end_lsn; /* points to the end of the record */
1076  ctx->end_xact = true;
1077 
1078  /*
1079  * If the plugin support two-phase commits then rollback prepared callback
1080  * is mandatory
1081  */
1082  if (ctx->callbacks.rollback_prepared_cb == NULL)
1083  ereport(ERROR,
1084  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1085  errmsg("logical replication at prepare time requires a %s callback",
1086  "rollback_prepared_cb")));
1087 
1088  /* do the actual work: call callback */
1089  ctx->callbacks.rollback_prepared_cb(ctx, txn, prepare_end_lsn,
1090  prepare_time);
1091 
1092  /* Pop the error context stack */
1093  error_context_stack = errcallback.previous;
1094 }
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 813 of file logical.c.

814 {
816  ErrorContextCallback errcallback;
817 
818  Assert(!ctx->fast_forward);
819 
820  /* Push callback + info on the error context stack */
821  state.ctx = ctx;
822  state.callback_name = "shutdown";
823  state.report_location = InvalidXLogRecPtr;
825  errcallback.arg = (void *) &state;
826  errcallback.previous = error_context_stack;
827  error_context_stack = &errcallback;
828 
829  /* set output state */
830  ctx->accept_writes = false;
831  ctx->end_xact = false;
832 
833  /* do the actual work: call callback */
834  ctx->callbacks.shutdown_cb(ctx);
835 
836  /* Pop the error context stack */
837  error_context_stack = errcallback.previous;
838 }

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 785 of file logical.c.

786 {
788  ErrorContextCallback errcallback;
789 
790  Assert(!ctx->fast_forward);
791 
792  /* Push callback + info on the error context stack */
793  state.ctx = ctx;
794  state.callback_name = "startup";
795  state.report_location = InvalidXLogRecPtr;
797  errcallback.arg = (void *) &state;
798  errcallback.previous = error_context_stack;
799  error_context_stack = &errcallback;
800 
801  /* set output state */
802  ctx->accept_writes = false;
803  ctx->end_xact = false;
804 
805  /* do the actual work: call callback */
806  ctx->callbacks.startup_cb(ctx, opt, is_init);
807 
808  /* Pop the error context stack */
809  error_context_stack = errcallback.previous;
810 }

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 151 of file logical.c.

160 {
161  ReplicationSlot *slot;
162  MemoryContext context,
163  old_context;
165 
166  /* shorter lines... */
167  slot = MyReplicationSlot;
168 
170  "Logical decoding context",
172  old_context = MemoryContextSwitchTo(context);
173  ctx = palloc0(sizeof(LogicalDecodingContext));
174 
175  ctx->context = context;
176 
177  /*
178  * (re-)load output plugins, so we detect a bad (removed) output plugin
179  * now.
180  */
181  if (!fast_forward)
183 
184  /*
185  * Now that the slot's xmin has been set, we can announce ourselves as a
186  * logical decoding backend which doesn't need to be checked individually
187  * when computing the xmin horizon because the xmin is enforced via
188  * replication slots.
189  *
190  * We can only do so if we're outside of a transaction (i.e. the case when
191  * streaming changes via walsender), otherwise an already setup
192  * snapshot/xid would end up being ignored. That's not a particularly
193  * bothersome restriction since the SQL interface can't be used for
194  * streaming anyway.
195  */
197  {
198  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
201  LWLockRelease(ProcArrayLock);
202  }
203 
204  ctx->slot = slot;
205 
206  ctx->reader = XLogReaderAllocate(wal_segment_size, NULL, xl_routine, ctx);
207  if (!ctx->reader)
208  ereport(ERROR,
209  (errcode(ERRCODE_OUT_OF_MEMORY),
210  errmsg("out of memory"),
211  errdetail("Failed while allocating a WAL reading processor.")));
212 
214  ctx->snapshot_builder =
215  AllocateSnapshotBuilder(ctx->reorder, xmin_horizon, start_lsn,
216  need_full_snapshot, slot->data.two_phase_at);
217 
218  ctx->reorder->private_data = ctx;
219 
220  /* wrap output plugin callbacks, so we can add error context information */
226 
227  /*
228  * To support streaming, we require start/stop/abort/commit/change
229  * callbacks. The message and truncate callbacks are optional, similar to
230  * regular output plugins. We however enable streaming when at least one
231  * of the methods is enabled so that we can easily identify missing
232  * methods.
233  *
234  * We decide it here, but only check it later in the wrappers.
235  */
236  ctx->streaming = (ctx->callbacks.stream_start_cb != NULL) ||
237  (ctx->callbacks.stream_stop_cb != NULL) ||
238  (ctx->callbacks.stream_abort_cb != NULL) ||
239  (ctx->callbacks.stream_commit_cb != NULL) ||
240  (ctx->callbacks.stream_change_cb != NULL) ||
241  (ctx->callbacks.stream_message_cb != NULL) ||
242  (ctx->callbacks.stream_truncate_cb != NULL);
243 
244  /*
245  * streaming callbacks
246  *
247  * stream_message and stream_truncate callbacks are optional, so we do not
248  * fail with ERROR when missing, but the wrappers simply do nothing. We
249  * must set the ReorderBuffer callbacks to something, otherwise the calls
250  * from there will crash (we don't want to move the checks there).
251  */
260 
261 
262  /*
263  * To support two-phase logical decoding, we require
264  * begin_prepare/prepare/commit-prepare/abort-prepare callbacks. The
265  * filter_prepare callback is optional. We however enable two-phase
266  * logical decoding when at least one of the methods is enabled so that we
267  * can easily identify missing methods.
268  *
269  * We decide it here, but only check it later in the wrappers.
270  */
271  ctx->twophase = (ctx->callbacks.begin_prepare_cb != NULL) ||
272  (ctx->callbacks.prepare_cb != NULL) ||
273  (ctx->callbacks.commit_prepared_cb != NULL) ||
274  (ctx->callbacks.rollback_prepared_cb != NULL) ||
275  (ctx->callbacks.stream_prepare_cb != NULL) ||
276  (ctx->callbacks.filter_prepare_cb != NULL);
277 
278  /*
279  * Callback to support decoding at prepare time.
280  */
285 
286  /*
287  * Callback to support updating progress during sending data of a
288  * transaction (and its subtransactions) to the output plugin.
289  */
291 
292  ctx->out = makeStringInfo();
293  ctx->prepare_write = prepare_write;
294  ctx->write = do_write;
295  ctx->update_progress = update_progress;
296 
297  ctx->output_plugin_options = output_plugin_options;
298 
299  ctx->fast_forward = fast_forward;
300 
301  MemoryContextSwitchTo(old_context);
302 
303  return ctx;
304 }
static void change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
Definition: logical.c:1097
static void commit_prepared_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:1005
static void update_progress_txn_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr lsn)
Definition: logical.c:1640
static void stream_start_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr first_lsn)
Definition: logical.c:1278
static void commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:877
static void begin_prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
Definition: logical.c:916
static void stream_prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
Definition: logical.c:1417
static void prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
Definition: logical.c:960
static void stream_truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
Definition: logical.c:1593
static void truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
Definition: logical.c:1136
static void begin_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
Definition: logical.c:846
static void rollback_prepared_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_end_lsn, TimestampTz prepare_time)
Definition: logical.c:1050
static void stream_change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
Definition: logical.c:1503
static void stream_abort_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr abort_lsn)
Definition: logical.c:1376
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:1552
static void stream_commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:1462
static void LoadOutputPlugin(OutputPluginCallbacks *callbacks, const char *plugin)
Definition: logical.c:744
static void stream_stop_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr last_lsn)
Definition: logical.c:1327
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:1241
void * palloc0(Size size)
Definition: mcxt.c:1231
MemoryContext CurrentMemoryContext
Definition: mcxt.c:135
#define AllocSetContextCreate
Definition: memutils.h:128
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:152
#define PROC_IN_LOGICAL_DECODING
Definition: proc.h:61
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:68
PROC_HDR * ProcGlobal
Definition: proc.c:80
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:238
int pgxactoff
Definition: proc.h:180
uint8 * statusFlags
Definition: proc.h:395
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:4851
int wal_segment_size
Definition: xlog.c:148
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 1376 of file logical.c.

1378 {
1379  LogicalDecodingContext *ctx = cache->private_data;
1381  ErrorContextCallback errcallback;
1382 
1383  Assert(!ctx->fast_forward);
1384 
1385  /* We're only supposed to call this when streaming is supported. */
1386  Assert(ctx->streaming);
1387 
1388  /* Push callback + info on the error context stack */
1389  state.ctx = ctx;
1390  state.callback_name = "stream_abort";
1391  state.report_location = abort_lsn;
1392  errcallback.callback = output_plugin_error_callback;
1393  errcallback.arg = (void *) &state;
1394  errcallback.previous = error_context_stack;
1395  error_context_stack = &errcallback;
1396 
1397  /* set output state */
1398  ctx->accept_writes = true;
1399  ctx->write_xid = txn->xid;
1400  ctx->write_location = abort_lsn;
1401  ctx->end_xact = true;
1402 
1403  /* in streaming mode, stream_abort_cb is required */
1404  if (ctx->callbacks.stream_abort_cb == NULL)
1405  ereport(ERROR,
1406  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1407  errmsg("logical streaming requires a %s callback",
1408  "stream_abort_cb")));
1409 
1410  ctx->callbacks.stream_abort_cb(ctx, txn, abort_lsn);
1411 
1412  /* Pop the error context stack */
1413  error_context_stack = errcallback.previous;
1414 }

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 1503 of file logical.c.

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

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 1462 of file logical.c.

1464 {
1465  LogicalDecodingContext *ctx = cache->private_data;
1467  ErrorContextCallback errcallback;
1468 
1469  Assert(!ctx->fast_forward);
1470 
1471  /* We're only supposed to call this when streaming is supported. */
1472  Assert(ctx->streaming);
1473 
1474  /* Push callback + info on the error context stack */
1475  state.ctx = ctx;
1476  state.callback_name = "stream_commit";
1477  state.report_location = txn->final_lsn;
1478  errcallback.callback = output_plugin_error_callback;
1479  errcallback.arg = (void *) &state;
1480  errcallback.previous = error_context_stack;
1481  error_context_stack = &errcallback;
1482 
1483  /* set output state */
1484  ctx->accept_writes = true;
1485  ctx->write_xid = txn->xid;
1486  ctx->write_location = txn->end_lsn;
1487  ctx->end_xact = true;
1488 
1489  /* in streaming mode, stream_commit_cb is required */
1490  if (ctx->callbacks.stream_commit_cb == NULL)
1491  ereport(ERROR,
1492  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1493  errmsg("logical streaming requires a %s callback",
1494  "stream_commit_cb")));
1495 
1496  ctx->callbacks.stream_commit_cb(ctx, txn, commit_lsn);
1497 
1498  /* Pop the error context stack */
1499  error_context_stack = errcallback.previous;
1500 }

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 1552 of file logical.c.

1555 {
1556  LogicalDecodingContext *ctx = cache->private_data;
1558  ErrorContextCallback errcallback;
1559 
1560  Assert(!ctx->fast_forward);
1561 
1562  /* We're only supposed to call this when streaming is supported. */
1563  Assert(ctx->streaming);
1564 
1565  /* this callback is optional */
1566  if (ctx->callbacks.stream_message_cb == NULL)
1567  return;
1568 
1569  /* Push callback + info on the error context stack */
1570  state.ctx = ctx;
1571  state.callback_name = "stream_message";
1572  state.report_location = message_lsn;
1573  errcallback.callback = output_plugin_error_callback;
1574  errcallback.arg = (void *) &state;
1575  errcallback.previous = error_context_stack;
1576  error_context_stack = &errcallback;
1577 
1578  /* set output state */
1579  ctx->accept_writes = true;
1580  ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
1581  ctx->write_location = message_lsn;
1582  ctx->end_xact = false;
1583 
1584  /* do the actual work: call callback */
1585  ctx->callbacks.stream_message_cb(ctx, txn, message_lsn, transactional, prefix,
1586  message_size, message);
1587 
1588  /* Pop the error context stack */
1589  error_context_stack = errcallback.previous;
1590 }

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 1417 of file logical.c.

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

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 1278 of file logical.c.

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

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 1327 of file logical.c.

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

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 1593 of file logical.c.

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

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 1136 of file logical.c.

1138 {
1139  LogicalDecodingContext *ctx = cache->private_data;
1141  ErrorContextCallback errcallback;
1142 
1143  Assert(!ctx->fast_forward);
1144 
1145  if (!ctx->callbacks.truncate_cb)
1146  return;
1147 
1148  /* Push callback + info on the error context stack */
1149  state.ctx = ctx;
1150  state.callback_name = "truncate";
1151  state.report_location = change->lsn;
1152  errcallback.callback = output_plugin_error_callback;
1153  errcallback.arg = (void *) &state;
1154  errcallback.previous = error_context_stack;
1155  error_context_stack = &errcallback;
1156 
1157  /* set output state */
1158  ctx->accept_writes = true;
1159  ctx->write_xid = txn->xid;
1160 
1161  /*
1162  * Report this change's lsn so replies from clients can give an up-to-date
1163  * answer. This won't ever be enough (and shouldn't be!) to confirm
1164  * receipt of this transaction, but it might allow another transaction's
1165  * commit to be confirmed with one message.
1166  */
1167  ctx->write_location = change->lsn;
1168 
1169  ctx->end_xact = false;
1170 
1171  ctx->callbacks.truncate_cb(ctx, txn, nrelations, relations, change);
1172 
1173  /* Pop the error context stack */
1174  error_context_stack = errcallback.previous;
1175 }
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 1640 of file logical.c.

1642 {
1643  LogicalDecodingContext *ctx = cache->private_data;
1645  ErrorContextCallback errcallback;
1646 
1647  Assert(!ctx->fast_forward);
1648 
1649  /* Push callback + info on the error context stack */
1650  state.ctx = ctx;
1651  state.callback_name = "update_progress_txn";
1652  state.report_location = lsn;
1653  errcallback.callback = output_plugin_error_callback;
1654  errcallback.arg = (void *) &state;
1655  errcallback.previous = error_context_stack;
1656  error_context_stack = &errcallback;
1657 
1658  /* set output state */
1659  ctx->accept_writes = false;
1660  ctx->write_xid = txn->xid;
1661 
1662  /*
1663  * Report this change's lsn so replies from clients can give an up-to-date
1664  * answer. This won't ever be enough (and shouldn't be!) to confirm
1665  * receipt of this transaction, but it might allow another transaction's
1666  * commit to be confirmed with one message.
1667  */
1668  ctx->write_location = lsn;
1669 
1670  ctx->end_xact = false;
1671 
1672  OutputPluginUpdateProgress(ctx, false);
1673 
1674  /* Pop the error context stack */
1675  error_context_stack = errcallback.previous;
1676 }
void OutputPluginUpdateProgress(struct LogicalDecodingContext *ctx, bool skipped_xact)
Definition: logical.c:729

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 1926 of file logical.c.

1927 {
1928  ReorderBuffer *rb = ctx->reorder;
1929  PgStat_StatReplSlotEntry repSlotStat;
1930 
1931  /* Nothing to do if we don't have any replication stats to be sent. */
1932  if (rb->spillBytes <= 0 && rb->streamBytes <= 0 && rb->totalBytes <= 0)
1933  return;
1934 
1935  elog(DEBUG2, "UpdateDecodingStats: updating stats %p %lld %lld %lld %lld %lld %lld %lld %lld",
1936  rb,
1937  (long long) rb->spillTxns,
1938  (long long) rb->spillCount,
1939  (long long) rb->spillBytes,
1940  (long long) rb->streamTxns,
1941  (long long) rb->streamCount,
1942  (long long) rb->streamBytes,
1943  (long long) rb->totalTxns,
1944  (long long) rb->totalBytes);
1945 
1946  repSlotStat.spill_txns = rb->spillTxns;
1947  repSlotStat.spill_count = rb->spillCount;
1948  repSlotStat.spill_bytes = rb->spillBytes;
1949  repSlotStat.stream_txns = rb->streamTxns;
1950  repSlotStat.stream_count = rb->streamCount;
1951  repSlotStat.stream_bytes = rb->streamBytes;
1952  repSlotStat.total_txns = rb->totalTxns;
1953  repSlotStat.total_bytes = rb->totalBytes;
1954 
1955  pgstat_report_replslot(ctx->slot, &repSlotStat);
1956 
1957  rb->spillTxns = 0;
1958  rb->spillCount = 0;
1959  rb->spillBytes = 0;
1960  rb->streamTxns = 0;
1961  rb->streamCount = 0;
1962  rb->streamBytes = 0;
1963  rb->totalTxns = 0;
1964  rb->totalBytes = 0;
1965 }
#define DEBUG2
Definition: elog.h:29
void pgstat_report_replslot(ReplicationSlot *slot, const PgStat_StatReplSlotEntry *repSlotStat)
PgStat_Counter stream_count
Definition: pgstat.h:373
PgStat_Counter total_txns
Definition: pgstat.h:375
PgStat_Counter total_bytes
Definition: pgstat.h:376
PgStat_Counter spill_txns
Definition: pgstat.h:369
PgStat_Counter stream_txns
Definition: pgstat.h:372
PgStat_Counter spill_count
Definition: pgstat.h:370
PgStat_Counter stream_bytes
Definition: pgstat.h:374
PgStat_Counter spill_bytes
Definition: pgstat.h:371

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().