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

Go to the source code of this file.

Data Structures

struct  LogicalErrorCallbackState
 

Typedefs

typedef struct LogicalErrorCallbackState LogicalErrorCallbackState
 

Functions

static void output_plugin_error_callback (void *arg)
 
static void startup_cb_wrapper (LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
 
static void shutdown_cb_wrapper (LogicalDecodingContext *ctx)
 
static void begin_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn)
 
static void commit_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
 
static void begin_prepare_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn)
 
static void prepare_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
 
static void commit_prepared_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
 
static void rollback_prepared_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_end_lsn, TimestampTz prepare_time)
 
static void change_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
 
static void truncate_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
 
static void message_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr message_lsn, bool transactional, const char *prefix, Size message_size, const char *message)
 
static void stream_start_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr first_lsn)
 
static void stream_stop_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr last_lsn)
 
static void stream_abort_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr abort_lsn)
 
static void stream_prepare_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
 
static void stream_commit_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
 
static void stream_change_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
 
static void stream_message_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr message_lsn, bool transactional, const char *prefix, Size message_size, const char *message)
 
static void stream_truncate_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
 
static void 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 filter_prepare_cb_wrapper (LogicalDecodingContext *ctx, TransactionId xid, const char *gid)
 
bool filter_by_origin_cb_wrapper (LogicalDecodingContext *ctx, RepOriginId origin_id)
 
void LogicalIncreaseXminForSlot (XLogRecPtr current_lsn, TransactionId xmin)
 
void LogicalIncreaseRestartDecodingForSlot (XLogRecPtr current_lsn, XLogRecPtr restart_lsn)
 
void LogicalConfirmReceivedLocation (XLogRecPtr lsn)
 
void ResetLogicalStreamingState (void)
 
void UpdateDecodingStats (LogicalDecodingContext *ctx)
 

Typedef Documentation

◆ LogicalErrorCallbackState

Function Documentation

◆ begin_cb_wrapper()

static void begin_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn 
)
static

Definition at line 783 of file logical.c.

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

Referenced by StartupDecodingContext().

784 {
785  LogicalDecodingContext *ctx = cache->private_data;
787  ErrorContextCallback errcallback;
788 
789  Assert(!ctx->fast_forward);
790 
791  /* Push callback + info on the error context stack */
792  state.ctx = ctx;
793  state.callback_name = "begin";
794  state.report_location = txn->first_lsn;
796  errcallback.arg = (void *) &state;
797  errcallback.previous = error_context_stack;
798  error_context_stack = &errcallback;
799 
800  /* set output state */
801  ctx->accept_writes = true;
802  ctx->write_xid = txn->xid;
803  ctx->write_location = txn->first_lsn;
804 
805  /* do the actual work: call callback */
806  ctx->callbacks.begin_cb(ctx, txn);
807 
808  /* Pop the error context stack */
809  error_context_stack = errcallback.previous;
810 }
XLogRecPtr first_lsn
TransactionId write_xid
Definition: logical.h:108
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:52
void(* callback)(void *arg)
Definition: elog.h:247
struct ErrorContextCallback * previous
Definition: elog.h:246
const char * callback_name
Definition: logical.c:50
XLogRecPtr write_location
Definition: logical.h:107
ErrorContextCallback * error_context_stack
Definition: elog.c:93
TransactionId xid
#define Assert(condition)
Definition: c.h:804
Definition: regguts.h:317
XLogRecPtr report_location
Definition: logical.c:51
LogicalDecodeBeginCB begin_cb
LogicalDecodingContext * ctx
Definition: logical.c:49
static void output_plugin_error_callback(void *arg)
Definition: logical.c:705

◆ begin_prepare_cb_wrapper()

static void begin_prepare_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn 
)
static

Definition at line 851 of file logical.c.

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

Referenced by StartupDecodingContext().

852 {
853  LogicalDecodingContext *ctx = cache->private_data;
855  ErrorContextCallback errcallback;
856 
857  Assert(!ctx->fast_forward);
858 
859  /* We're only supposed to call this when two-phase commits are supported */
860  Assert(ctx->twophase);
861 
862  /* Push callback + info on the error context stack */
863  state.ctx = ctx;
864  state.callback_name = "begin_prepare";
865  state.report_location = txn->first_lsn;
867  errcallback.arg = (void *) &state;
868  errcallback.previous = error_context_stack;
869  error_context_stack = &errcallback;
870 
871  /* set output state */
872  ctx->accept_writes = true;
873  ctx->write_xid = txn->xid;
874  ctx->write_location = txn->first_lsn;
875 
876  /*
877  * If the plugin supports two-phase commits then begin prepare callback is
878  * mandatory
879  */
880  if (ctx->callbacks.begin_prepare_cb == NULL)
881  ereport(ERROR,
882  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
883  errmsg("logical replication at prepare time requires a %s callback",
884  "begin_prepare_cb")));
885 
886  /* do the actual work: call callback */
887  ctx->callbacks.begin_prepare_cb(ctx, txn);
888 
889  /* Pop the error context stack */
890  error_context_stack = errcallback.previous;
891 }
XLogRecPtr first_lsn
TransactionId write_xid
Definition: logical.h:108
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:52
int errcode(int sqlerrcode)
Definition: elog.c:698
void(* callback)(void *arg)
Definition: elog.h:247
struct ErrorContextCallback * previous
Definition: elog.h:246
const char * callback_name
Definition: logical.c:50
XLogRecPtr write_location
Definition: logical.h:107
LogicalDecodeBeginPrepareCB begin_prepare_cb
ErrorContextCallback * error_context_stack
Definition: elog.c:93
#define ERROR
Definition: elog.h:46
TransactionId xid
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
Definition: regguts.h:317
XLogRecPtr report_location
Definition: logical.c:51
int errmsg(const char *fmt,...)
Definition: elog.c:909
LogicalDecodingContext * ctx
Definition: logical.c:49
static void output_plugin_error_callback(void *arg)
Definition: logical.c:705

◆ change_cb_wrapper()

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

Definition at line 1028 of file logical.c.

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

Referenced by StartupDecodingContext().

1030 {
1031  LogicalDecodingContext *ctx = cache->private_data;
1033  ErrorContextCallback errcallback;
1034 
1035  Assert(!ctx->fast_forward);
1036 
1037  /* Push callback + info on the error context stack */
1038  state.ctx = ctx;
1039  state.callback_name = "change";
1040  state.report_location = change->lsn;
1041  errcallback.callback = output_plugin_error_callback;
1042  errcallback.arg = (void *) &state;
1043  errcallback.previous = error_context_stack;
1044  error_context_stack = &errcallback;
1045 
1046  /* set output state */
1047  ctx->accept_writes = true;
1048  ctx->write_xid = txn->xid;
1049 
1050  /*
1051  * report this change's lsn so replies from clients can give an up2date
1052  * answer. This won't ever be enough (and shouldn't be!) to confirm
1053  * receipt of this transaction, but it might allow another transaction's
1054  * commit to be confirmed with one message.
1055  */
1056  ctx->write_location = change->lsn;
1057 
1058  ctx->callbacks.change_cb(ctx, txn, relation, change);
1059 
1060  /* Pop the error context stack */
1061  error_context_stack = errcallback.previous;
1062 }
TransactionId write_xid
Definition: logical.h:108
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:52
void(* callback)(void *arg)
Definition: elog.h:247
struct ErrorContextCallback * previous
Definition: elog.h:246
const char * callback_name
Definition: logical.c:50
XLogRecPtr write_location
Definition: logical.h:107
ErrorContextCallback * error_context_stack
Definition: elog.c:93
LogicalDecodeChangeCB change_cb
TransactionId xid
#define Assert(condition)
Definition: c.h:804
Definition: regguts.h:317
XLogRecPtr report_location
Definition: logical.c:51
LogicalDecodingContext * ctx
Definition: logical.c:49
static void output_plugin_error_callback(void *arg)
Definition: logical.c:705

◆ CheckLogicalDecodingRequirements()

void CheckLogicalDecodingRequirements ( void  )

Definition at line 103 of file logical.c.

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

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

104 {
106 
107  /*
108  * NB: Adding a new requirement likely means that RestoreSlotFromDisk()
109  * needs the same check.
110  */
111 
113  ereport(ERROR,
114  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
115  errmsg("logical decoding requires wal_level >= logical")));
116 
117  if (MyDatabaseId == InvalidOid)
118  ereport(ERROR,
119  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
120  errmsg("logical decoding requires a database connection")));
121 
122  /* ----
123  * TODO: We got to change that someday soon...
124  *
125  * There's basically three things missing to allow this:
126  * 1) We need to be able to correctly and quickly identify the timeline a
127  * LSN belongs to
128  * 2) We need to force hot_standby_feedback to be enabled at all times so
129  * the primary cannot remove rows we need.
130  * 3) support dropping replication slots referring to a database, in
131  * dbase_redo. There can't be any active ones due to HS recovery
132  * conflicts, so that should be relatively easy.
133  * ----
134  */
135  if (RecoveryInProgress())
136  ereport(ERROR,
137  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
138  errmsg("logical decoding cannot be used while in recovery")));
139 }
void CheckSlotRequirements(void)
Definition: slot.c:1044
int wal_level
Definition: xlog.c:108
int errcode(int sqlerrcode)
Definition: elog.c:698
bool RecoveryInProgress(void)
Definition: xlog.c:8248
#define ERROR
Definition: elog.h:46
Oid MyDatabaseId
Definition: globals.c:88
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ commit_cb_wrapper()

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

Definition at line 813 of file logical.c.

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

Referenced by StartupDecodingContext().

815 {
816  LogicalDecodingContext *ctx = cache->private_data;
818  ErrorContextCallback errcallback;
819 
820  Assert(!ctx->fast_forward);
821 
822  /* Push callback + info on the error context stack */
823  state.ctx = ctx;
824  state.callback_name = "commit";
825  state.report_location = txn->final_lsn; /* beginning of commit record */
827  errcallback.arg = (void *) &state;
828  errcallback.previous = error_context_stack;
829  error_context_stack = &errcallback;
830 
831  /* set output state */
832  ctx->accept_writes = true;
833  ctx->write_xid = txn->xid;
834  ctx->write_location = txn->end_lsn; /* points to the end of the record */
835 
836  /* do the actual work: call callback */
837  ctx->callbacks.commit_cb(ctx, txn, commit_lsn);
838 
839  /* Pop the error context stack */
840  error_context_stack = errcallback.previous;
841 }
TransactionId write_xid
Definition: logical.h:108
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:52
void(* callback)(void *arg)
Definition: elog.h:247
struct ErrorContextCallback * previous
Definition: elog.h:246
const char * callback_name
Definition: logical.c:50
XLogRecPtr write_location
Definition: logical.h:107
ErrorContextCallback * error_context_stack
Definition: elog.c:93
LogicalDecodeCommitCB commit_cb
XLogRecPtr final_lsn
TransactionId xid
#define Assert(condition)
Definition: c.h:804
Definition: regguts.h:317
XLogRecPtr end_lsn
XLogRecPtr report_location
Definition: logical.c:51
LogicalDecodingContext * ctx
Definition: logical.c:49
static void output_plugin_error_callback(void *arg)
Definition: logical.c:705

◆ commit_prepared_cb_wrapper()

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

Definition at line 938 of file logical.c.

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

Referenced by StartupDecodingContext().

940 {
941  LogicalDecodingContext *ctx = cache->private_data;
943  ErrorContextCallback errcallback;
944 
945  Assert(!ctx->fast_forward);
946 
947  /* We're only supposed to call this when two-phase commits are supported */
948  Assert(ctx->twophase);
949 
950  /* Push callback + info on the error context stack */
951  state.ctx = ctx;
952  state.callback_name = "commit_prepared";
953  state.report_location = txn->final_lsn; /* beginning of commit record */
955  errcallback.arg = (void *) &state;
956  errcallback.previous = error_context_stack;
957  error_context_stack = &errcallback;
958 
959  /* set output state */
960  ctx->accept_writes = true;
961  ctx->write_xid = txn->xid;
962  ctx->write_location = txn->end_lsn; /* points to the end of the record */
963 
964  /*
965  * If the plugin support two-phase commits then commit prepared callback
966  * is mandatory
967  */
968  if (ctx->callbacks.commit_prepared_cb == NULL)
969  ereport(ERROR,
970  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
971  errmsg("logical replication at prepare time requires a %s callback",
972  "commit_prepared_cb")));
973 
974  /* do the actual work: call callback */
975  ctx->callbacks.commit_prepared_cb(ctx, txn, commit_lsn);
976 
977  /* Pop the error context stack */
978  error_context_stack = errcallback.previous;
979 }
TransactionId write_xid
Definition: logical.h:108
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:52
int errcode(int sqlerrcode)
Definition: elog.c:698
void(* callback)(void *arg)
Definition: elog.h:247
struct ErrorContextCallback * previous
Definition: elog.h:246
const char * callback_name
Definition: logical.c:50
XLogRecPtr write_location
Definition: logical.h:107
ErrorContextCallback * error_context_stack
Definition: elog.c:93
#define ERROR
Definition: elog.h:46
XLogRecPtr final_lsn
LogicalDecodeCommitPreparedCB commit_prepared_cb
TransactionId xid
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
Definition: regguts.h:317
XLogRecPtr end_lsn
XLogRecPtr report_location
Definition: logical.c:51
int errmsg(const char *fmt,...)
Definition: elog.c:909
LogicalDecodingContext * ctx
Definition: logical.c:49
static void output_plugin_error_callback(void *arg)
Definition: logical.c:705

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

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

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

485 {
487  ReplicationSlot *slot;
488  MemoryContext old_context;
489 
490  /* shorter lines... */
491  slot = MyReplicationSlot;
492 
493  /* first some sanity checks that are unlikely to be violated */
494  if (slot == NULL)
495  elog(ERROR, "cannot perform logical decoding without an acquired slot");
496 
497  /* make sure the passed slot is suitable, these are user facing errors */
498  if (SlotIsPhysical(slot))
499  ereport(ERROR,
500  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
501  errmsg("cannot use physical replication slot for logical decoding")));
502 
503  if (slot->data.database != MyDatabaseId)
504  ereport(ERROR,
505  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
506  errmsg("replication slot \"%s\" was not created in this database",
507  NameStr(slot->data.name))));
508 
509  if (start_lsn == InvalidXLogRecPtr)
510  {
511  /* continue from last position */
512  start_lsn = slot->data.confirmed_flush;
513  }
514  else if (start_lsn < slot->data.confirmed_flush)
515  {
516  /*
517  * It might seem like we should error out in this case, but it's
518  * pretty common for a client to acknowledge a LSN it doesn't have to
519  * do anything for, and thus didn't store persistently, because the
520  * xlog records didn't result in anything relevant for logical
521  * decoding. Clients have to be able to do that to support synchronous
522  * replication.
523  */
524  elog(DEBUG1, "cannot stream from %X/%X, minimum is %X/%X, forwarding",
525  LSN_FORMAT_ARGS(start_lsn),
527 
528  start_lsn = slot->data.confirmed_flush;
529  }
530 
531  ctx = StartupDecodingContext(output_plugin_options,
532  start_lsn, InvalidTransactionId, false,
533  fast_forward, xl_routine, prepare_write,
534  do_write, update_progress);
535 
536  /* call output plugin initialization callback */
537  old_context = MemoryContextSwitchTo(ctx->context);
538  if (ctx->callbacks.startup_cb != NULL)
539  startup_cb_wrapper(ctx, &ctx->options, false);
540  MemoryContextSwitchTo(old_context);
541 
542  /*
543  * We allow decoding of prepared transactions when the two_phase is
544  * enabled at the time of slot creation, or when the two_phase option is
545  * given at the streaming start, provided the plugin supports all the
546  * callbacks for two-phase.
547  */
548  ctx->twophase &= (slot->data.two_phase || ctx->twophase_opt_given);
549 
550  /* Mark slot to allow two_phase decoding if not already marked */
551  if (ctx->twophase && !slot->data.two_phase)
552  {
553  slot->data.two_phase = true;
554  slot->data.two_phase_at = start_lsn;
557  SnapBuildSetTwoPhaseAt(ctx->snapshot_builder, start_lsn);
558  }
559 
561 
562  ereport(LOG,
563  (errmsg("starting logical decoding for slot \"%s\"",
564  NameStr(slot->data.name)),
565  errdetail("Streaming transactions committing after %X/%X, reading WAL from %X/%X.",
567  LSN_FORMAT_ARGS(slot->data.restart_lsn))));
568 
569  return ctx;
570 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
struct ReorderBuffer * reorder
Definition: logical.h:42
OutputPluginOptions options
Definition: logical.h:53
OutputPluginCallbacks callbacks
Definition: logical.h:52
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:698
MemoryContext context
Definition: logical.h:35
void ReplicationSlotSave(void)
Definition: slot.c:710
ReplicationSlotPersistentData data
Definition: slot.h:147
#define LOG
Definition: elog.h:26
#define SlotIsPhysical(slot)
Definition: slot.h:168
XLogRecPtr confirmed_flush
Definition: slot.h:84
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
#define ERROR
Definition: elog.h:46
static void startup_cb_wrapper(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
Definition: logical.c:724
int errdetail(const char *fmt,...)
Definition: elog.c:1042
#define InvalidTransactionId
Definition: transam.h:31
void SnapBuildSetTwoPhaseAt(SnapBuild *builder, XLogRecPtr ptr)
Definition: snapbuild.c:385
Oid MyDatabaseId
Definition: globals.c:88
#define ereport(elevel,...)
Definition: elog.h:157
struct SnapBuild * snapshot_builder
Definition: logical.h:43
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
XLogRecPtr restart_lsn
Definition: slot.h:73
LogicalDecodeStartupCB startup_cb
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
#define NameStr(name)
Definition: c.h:681
void ReplicationSlotMarkDirty(void)
Definition: slot.c:728
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:146
XLogRecPtr two_phase_at
Definition: slot.h:90

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

References LogicalDecodingContext::callbacks, ReplicationSlotPersistentData::catalog_xmin, LogicalDecodingContext::context, LogicalErrorCallbackState::ctx, 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, 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().

326 {
327  TransactionId xmin_horizon = InvalidTransactionId;
328  ReplicationSlot *slot;
329  NameData plugin_name;
331  MemoryContext old_context;
332 
333  /* shorter lines... */
334  slot = MyReplicationSlot;
335 
336  /* first some sanity checks that are unlikely to be violated */
337  if (slot == NULL)
338  elog(ERROR, "cannot perform logical decoding without an acquired slot");
339 
340  if (plugin == NULL)
341  elog(ERROR, "cannot initialize logical decoding without a specified plugin");
342 
343  /* Make sure the passed slot is suitable. These are user facing errors. */
344  if (SlotIsPhysical(slot))
345  ereport(ERROR,
346  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
347  errmsg("cannot use physical replication slot for logical decoding")));
348 
349  if (slot->data.database != MyDatabaseId)
350  ereport(ERROR,
351  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
352  errmsg("replication slot \"%s\" was not created in this database",
353  NameStr(slot->data.name))));
354 
355  if (IsTransactionState() &&
357  ereport(ERROR,
358  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
359  errmsg("cannot create logical replication slot in transaction that has performed writes")));
360 
361  /*
362  * Register output plugin name with slot. We need the mutex to avoid
363  * concurrent reading of a partially copied string. But we don't want any
364  * complicated code while holding a spinlock, so do namestrcpy() outside.
365  */
366  namestrcpy(&plugin_name, plugin);
367  SpinLockAcquire(&slot->mutex);
368  slot->data.plugin = plugin_name;
369  SpinLockRelease(&slot->mutex);
370 
371  if (XLogRecPtrIsInvalid(restart_lsn))
373  else
374  {
375  SpinLockAcquire(&slot->mutex);
376  slot->data.restart_lsn = restart_lsn;
377  SpinLockRelease(&slot->mutex);
378  }
379 
380  /* ----
381  * This is a bit tricky: We need to determine a safe xmin horizon to start
382  * decoding from, to avoid starting from a running xacts record referring
383  * to xids whose rows have been vacuumed or pruned
384  * already. GetOldestSafeDecodingTransactionId() returns such a value, but
385  * without further interlock its return value might immediately be out of
386  * date.
387  *
388  * So we have to acquire the ProcArrayLock to prevent computation of new
389  * xmin horizons by other backends, get the safe decoding xid, and inform
390  * the slot machinery about the new limit. Once that's done the
391  * ProcArrayLock can be released as the slot machinery now is
392  * protecting against vacuum.
393  *
394  * Note that, temporarily, the data, not just the catalog, xmin has to be
395  * reserved if a data snapshot is to be exported. Otherwise the initial
396  * data snapshot created here is not guaranteed to be valid. After that
397  * the data xmin doesn't need to be managed anymore and the global xmin
398  * should be recomputed. As we are fine with losing the pegged data xmin
399  * after crash - no chance a snapshot would get exported anymore - we can
400  * get away with just setting the slot's
401  * effective_xmin. ReplicationSlotRelease will reset it again.
402  *
403  * ----
404  */
405  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
406 
407  xmin_horizon = GetOldestSafeDecodingTransactionId(!need_full_snapshot);
408 
409  SpinLockAcquire(&slot->mutex);
410  slot->effective_catalog_xmin = xmin_horizon;
411  slot->data.catalog_xmin = xmin_horizon;
412  if (need_full_snapshot)
413  slot->effective_xmin = xmin_horizon;
414  SpinLockRelease(&slot->mutex);
415 
417 
418  LWLockRelease(ProcArrayLock);
419 
422 
423  ctx = StartupDecodingContext(NIL, restart_lsn, xmin_horizon,
424  need_full_snapshot, false,
425  xl_routine, prepare_write, do_write,
426  update_progress);
427 
428  /* call output plugin initialization callback */
429  old_context = MemoryContextSwitchTo(ctx->context);
430  if (ctx->callbacks.startup_cb != NULL)
431  startup_cb_wrapper(ctx, &ctx->options, true);
432  MemoryContextSwitchTo(old_context);
433 
434  /*
435  * We allow decoding of prepared transactions when the two_phase is
436  * enabled at the time of slot creation, or when the two_phase option is
437  * given at the streaming start, provided the plugin supports all the
438  * callbacks for two-phase.
439  */
440  ctx->twophase &= slot->data.two_phase;
441 
443 
444  return ctx;
445 }
#define NIL
Definition: pg_list.h:65
static const char * plugin
TransactionId GetOldestSafeDecodingTransactionId(bool catalogOnly)
Definition: procarray.c:2947
struct ReorderBuffer * reorder
Definition: logical.h:42
uint32 TransactionId
Definition: c.h:587
void namestrcpy(Name name, const char *str)
Definition: name.c:233
OutputPluginOptions options
Definition: logical.h:53
OutputPluginCallbacks callbacks
Definition: logical.h:52
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:698
MemoryContext context
Definition: logical.h:35
void ReplicationSlotSave(void)
Definition: slot.c:710
ReplicationSlotPersistentData data
Definition: slot.h:147
#define SlotIsPhysical(slot)
Definition: slot.h:168
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
#define SpinLockAcquire(lock)
Definition: spin.h:62
void ReplicationSlotReserveWal(void)
Definition: slot.c:1069
#define ERROR
Definition: elog.h:46
static void startup_cb_wrapper(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
Definition: logical.c:724
TransactionId effective_xmin
Definition: slot.h:143
Definition: c.h:675
TransactionId catalog_xmin
Definition: slot.h:70
#define InvalidTransactionId
Definition: transam.h:31
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:425
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
#define SpinLockRelease(lock)
Definition: spin.h:64
TransactionId effective_catalog_xmin
Definition: slot.h:144
Oid MyDatabaseId
Definition: globals.c:88
#define ereport(elevel,...)
Definition: elog.h:157
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
XLogRecPtr restart_lsn
Definition: slot.h:73
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
bool IsTransactionState(void)
Definition: xact.c:371
LogicalDecodeStartupCB startup_cb
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
#define NameStr(name)
Definition: c.h:681
slock_t mutex
Definition: slot.h:120
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:767
void ReplicationSlotMarkDirty(void)
Definition: slot.c:728
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:146

◆ DecodingContextFindStartpoint()

void DecodingContextFindStartpoint ( LogicalDecodingContext ctx)

Definition at line 585 of file logical.c.

References CHECK_FOR_INTERRUPTS, ReplicationSlotPersistentData::confirmed_flush, ReplicationSlot::data, DEBUG1, DecodingContextReady(), elog, XLogReaderState::EndRecPtr, 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().

586 {
587  ReplicationSlot *slot = ctx->slot;
588 
589  /* Initialize from where to start reading WAL. */
590  XLogBeginRead(ctx->reader, slot->data.restart_lsn);
591 
592  elog(DEBUG1, "searching for logical decoding starting point, starting at %X/%X",
594 
595  /* Wait for a consistent starting point */
596  for (;;)
597  {
598  XLogRecord *record;
599  char *err = NULL;
600 
601  /* the read_page callback waits for new WAL */
602  record = XLogReadRecord(ctx->reader, &err);
603  if (err)
604  elog(ERROR, "%s", err);
605  if (!record)
606  elog(ERROR, "no record found"); /* shouldn't happen */
607 
609 
610  /* only continue till we found a consistent spot */
611  if (DecodingContextReady(ctx))
612  break;
613 
615  }
616 
617  SpinLockAcquire(&slot->mutex);
618  slot->data.confirmed_flush = ctx->reader->EndRecPtr;
619  if (slot->data.two_phase)
620  slot->data.two_phase_at = ctx->reader->EndRecPtr;
621  SpinLockRelease(&slot->mutex);
622 }
#define DEBUG1
Definition: elog.h:25
bool DecodingContextReady(LogicalDecodingContext *ctx)
Definition: logical.c:576
ReplicationSlotPersistentData data
Definition: slot.h:147
XLogRecPtr confirmed_flush
Definition: slot.h:84
XLogRecPtr EndRecPtr
Definition: xlogreader.h:176
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
XLogRecord * XLogReadRecord(XLogReaderState *state, char **errormsg)
Definition: xlogreader.c:271
#define ERROR
Definition: elog.h:46
void LogicalDecodingProcessRecord(LogicalDecodingContext *ctx, XLogReaderState *record)
Definition: decode.c:106
void XLogBeginRead(XLogReaderState *state, XLogRecPtr RecPtr)
Definition: xlogreader.c:243
#define SpinLockRelease(lock)
Definition: spin.h:64
XLogRecPtr restart_lsn
Definition: slot.h:73
XLogReaderState * reader
Definition: logical.h:41
ReplicationSlot * slot
Definition: logical.h:38
#define elog(elevel,...)
Definition: elog.h:232
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:120
slock_t mutex
Definition: slot.h:120
XLogRecPtr two_phase_at
Definition: slot.h:90

◆ DecodingContextReady()

bool DecodingContextReady ( LogicalDecodingContext ctx)

Definition at line 576 of file logical.c.

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

Referenced by DecodingContextFindStartpoint().

577 {
579 }
SnapBuildState SnapBuildCurrentState(SnapBuild *builder)
Definition: snapbuild.c:367
struct SnapBuild * snapshot_builder
Definition: logical.h:43

◆ filter_by_origin_cb_wrapper()

bool filter_by_origin_cb_wrapper ( LogicalDecodingContext ctx,
RepOriginId  origin_id 
)

Definition at line 1136 of file logical.c.

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

Referenced by FilterByOrigin().

1137 {
1139  ErrorContextCallback errcallback;
1140  bool ret;
1141 
1142  Assert(!ctx->fast_forward);
1143 
1144  /* Push callback + info on the error context stack */
1145  state.ctx = ctx;
1146  state.callback_name = "filter_by_origin";
1148  errcallback.callback = output_plugin_error_callback;
1149  errcallback.arg = (void *) &state;
1150  errcallback.previous = error_context_stack;
1151  error_context_stack = &errcallback;
1152 
1153  /* set output state */
1154  ctx->accept_writes = false;
1155 
1156  /* do the actual work: call callback */
1157  ret = ctx->callbacks.filter_by_origin_cb(ctx, origin_id);
1158 
1159  /* Pop the error context stack */
1160  error_context_stack = errcallback.previous;
1161 
1162  return ret;
1163 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:52
void(* callback)(void *arg)
Definition: elog.h:247
struct ErrorContextCallback * previous
Definition: elog.h:246
const char * callback_name
Definition: logical.c:50
ErrorContextCallback * error_context_stack
Definition: elog.c:93
#define Assert(condition)
Definition: c.h:804
Definition: regguts.h:317
XLogRecPtr report_location
Definition: logical.c:51
LogicalDecodingContext * ctx
Definition: logical.c:49
static void output_plugin_error_callback(void *arg)
Definition: logical.c:705
LogicalDecodeFilterByOriginCB filter_by_origin_cb

◆ filter_prepare_cb_wrapper()

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

Definition at line 1105 of file logical.c.

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

Referenced by FilterPrepare().

1107 {
1109  ErrorContextCallback errcallback;
1110  bool ret;
1111 
1112  Assert(!ctx->fast_forward);
1113 
1114  /* Push callback + info on the error context stack */
1115  state.ctx = ctx;
1116  state.callback_name = "filter_prepare";
1118  errcallback.callback = output_plugin_error_callback;
1119  errcallback.arg = (void *) &state;
1120  errcallback.previous = error_context_stack;
1121  error_context_stack = &errcallback;
1122 
1123  /* set output state */
1124  ctx->accept_writes = false;
1125 
1126  /* do the actual work: call callback */
1127  ret = ctx->callbacks.filter_prepare_cb(ctx, xid, gid);
1128 
1129  /* Pop the error context stack */
1130  error_context_stack = errcallback.previous;
1131 
1132  return ret;
1133 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:52
void(* callback)(void *arg)
Definition: elog.h:247
struct ErrorContextCallback * previous
Definition: elog.h:246
const char * callback_name
Definition: logical.c:50
ErrorContextCallback * error_context_stack
Definition: elog.c:93
LogicalDecodeFilterPrepareCB filter_prepare_cb
#define Assert(condition)
Definition: c.h:804
Definition: regguts.h:317
XLogRecPtr report_location
Definition: logical.c:51
LogicalDecodingContext * ctx
Definition: logical.c:49
static void output_plugin_error_callback(void *arg)
Definition: logical.c:705

◆ FreeDecodingContext()

void FreeDecodingContext ( LogicalDecodingContext ctx)

Definition at line 629 of file logical.c.

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

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

630 {
631  if (ctx->callbacks.shutdown_cb != NULL)
632  shutdown_cb_wrapper(ctx);
633 
636  XLogReaderFree(ctx->reader);
638 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
struct ReorderBuffer * reorder
Definition: logical.h:42
OutputPluginCallbacks callbacks
Definition: logical.h:52
void ReorderBufferFree(ReorderBuffer *rb)
MemoryContext context
Definition: logical.h:35
static void shutdown_cb_wrapper(LogicalDecodingContext *ctx)
Definition: logical.c:751
void XLogReaderFree(XLogReaderState *state)
Definition: xlogreader.c:142
void FreeSnapshotBuilder(SnapBuild *builder)
Definition: snapbuild.c:323
struct SnapBuild * snapshot_builder
Definition: logical.h:43
LogicalDecodeShutdownCB shutdown_cb
XLogReaderState * reader
Definition: logical.h:41

◆ LoadOutputPlugin()

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

Definition at line 683 of file logical.c.

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

Referenced by StartupDecodingContext().

684 {
685  LogicalOutputPluginInit plugin_init;
686 
687  plugin_init = (LogicalOutputPluginInit)
688  load_external_function(plugin, "_PG_output_plugin_init", false, NULL);
689 
690  if (plugin_init == NULL)
691  elog(ERROR, "output plugins have to declare the _PG_output_plugin_init symbol");
692 
693  /* ask the output plugin to fill the callback struct */
694  plugin_init(callbacks);
695 
696  if (callbacks->begin_cb == NULL)
697  elog(ERROR, "output plugins have to register a begin callback");
698  if (callbacks->change_cb == NULL)
699  elog(ERROR, "output plugins have to register a change callback");
700  if (callbacks->commit_cb == NULL)
701  elog(ERROR, "output plugins have to register a commit callback");
702 }
static const char * plugin
#define ERROR
Definition: elog.h:46
LogicalDecodeCommitCB commit_cb
void(* LogicalOutputPluginInit)(struct OutputPluginCallbacks *cb)
Definition: output_plugin.h:36
void * load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
Definition: dfmgr.c:107
LogicalDecodeChangeCB change_cb
#define elog(elevel,...)
Definition: elog.h:232
LogicalDecodeBeginCB begin_cb

◆ LogicalConfirmReceivedLocation()

void LogicalConfirmReceivedLocation ( XLogRecPtr  lsn)

Definition at line 1691 of file logical.c.

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

1692 {
1693  Assert(lsn != InvalidXLogRecPtr);
1694 
1695  /* Do an unlocked check for candidate_lsn first. */
1698  {
1699  bool updated_xmin = false;
1700  bool updated_restart = false;
1701 
1703 
1705 
1706  /* if we're past the location required for bumping xmin, do so */
1709  {
1710  /*
1711  * We have to write the changed xmin to disk *before* we change
1712  * the in-memory value, otherwise after a crash we wouldn't know
1713  * that some catalog tuples might have been removed already.
1714  *
1715  * Ensure that by first writing to ->xmin and only update
1716  * ->effective_xmin once the new state is synced to disk. After a
1717  * crash ->effective_xmin is set to ->xmin.
1718  */
1721  {
1725  updated_xmin = true;
1726  }
1727  }
1728 
1731  {
1733 
1737  updated_restart = true;
1738  }
1739 
1741 
1742  /* first write new xmin to disk, so we know what's up after a crash */
1743  if (updated_xmin || updated_restart)
1744  {
1747  elog(DEBUG1, "updated xmin: %u restart: %u", updated_xmin, updated_restart);
1748  }
1749 
1750  /*
1751  * Now the new xmin is safely on disk, we can let the global value
1752  * advance. We do not take ProcArrayLock or similar since we only
1753  * advance xmin here and there's not much harm done by a concurrent
1754  * computation missing that.
1755  */
1756  if (updated_xmin)
1757  {
1761 
1764  }
1765  }
1766  else
1767  {
1771  }
1772 }
TransactionId candidate_catalog_xmin
Definition: slot.h:162
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
void ReplicationSlotSave(void)
Definition: slot.c:710
ReplicationSlotPersistentData data
Definition: slot.h:147
XLogRecPtr confirmed_flush
Definition: slot.h:84
#define SpinLockAcquire(lock)
Definition: spin.h:62
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:817
XLogRecPtr candidate_restart_valid
Definition: slot.h:164
TransactionId catalog_xmin
Definition: slot.h:70
#define InvalidTransactionId
Definition: transam.h:31
#define SpinLockRelease(lock)
Definition: spin.h:64
TransactionId effective_catalog_xmin
Definition: slot.h:144
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:804
XLogRecPtr restart_lsn
Definition: slot.h:73
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:163
#define elog(elevel,...)
Definition: elog.h:232
slock_t mutex
Definition: slot.h:120
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:767
XLogRecPtr candidate_restart_lsn
Definition: slot.h:165
void ReplicationSlotMarkDirty(void)
Definition: slot.c:728

◆ LogicalIncreaseRestartDecodingForSlot()

void LogicalIncreaseRestartDecodingForSlot ( XLogRecPtr  current_lsn,
XLogRecPtr  restart_lsn 
)

Definition at line 1617 of file logical.c.

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

Referenced by SnapBuildProcessRunningXacts().

1618 {
1619  bool updated_lsn = false;
1620  ReplicationSlot *slot;
1621 
1622  slot = MyReplicationSlot;
1623 
1624  Assert(slot != NULL);
1625  Assert(restart_lsn != InvalidXLogRecPtr);
1626  Assert(current_lsn != InvalidXLogRecPtr);
1627 
1628  SpinLockAcquire(&slot->mutex);
1629 
1630  /* don't overwrite if have a newer restart lsn */
1631  if (restart_lsn <= slot->data.restart_lsn)
1632  {
1633  }
1634 
1635  /*
1636  * We might have already flushed far enough to directly accept this lsn,
1637  * in this case there is no need to check for existing candidate LSNs
1638  */
1639  else if (current_lsn <= slot->data.confirmed_flush)
1640  {
1641  slot->candidate_restart_valid = current_lsn;
1642  slot->candidate_restart_lsn = restart_lsn;
1643 
1644  /* our candidate can directly be used */
1645  updated_lsn = true;
1646  }
1647 
1648  /*
1649  * Only increase if the previous values have been applied, otherwise we
1650  * might never end up updating if the receiver acks too slowly. A missed
1651  * value here will just cause some extra effort after reconnecting.
1652  */
1654  {
1655  slot->candidate_restart_valid = current_lsn;
1656  slot->candidate_restart_lsn = restart_lsn;
1657  SpinLockRelease(&slot->mutex);
1658 
1659  elog(DEBUG1, "got new restart lsn %X/%X at %X/%X",
1660  LSN_FORMAT_ARGS(restart_lsn),
1661  LSN_FORMAT_ARGS(current_lsn));
1662  }
1663  else
1664  {
1665  XLogRecPtr candidate_restart_lsn;
1666  XLogRecPtr candidate_restart_valid;
1667  XLogRecPtr confirmed_flush;
1668 
1669  candidate_restart_lsn = slot->candidate_restart_lsn;
1670  candidate_restart_valid = slot->candidate_restart_valid;
1671  confirmed_flush = slot->data.confirmed_flush;
1672  SpinLockRelease(&slot->mutex);
1673 
1674  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",
1675  LSN_FORMAT_ARGS(restart_lsn),
1676  LSN_FORMAT_ARGS(current_lsn),
1677  LSN_FORMAT_ARGS(candidate_restart_lsn),
1678  LSN_FORMAT_ARGS(candidate_restart_valid),
1679  LSN_FORMAT_ARGS(confirmed_flush));
1680  }
1681 
1682  /* candidates are already valid with the current flush position, apply */
1683  if (updated_lsn)
1685 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
ReplicationSlotPersistentData data
Definition: slot.h:147
XLogRecPtr confirmed_flush
Definition: slot.h:84
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
XLogRecPtr candidate_restart_valid
Definition: slot.h:164
#define SpinLockRelease(lock)
Definition: spin.h:64
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:804
void LogicalConfirmReceivedLocation(XLogRecPtr lsn)
Definition: logical.c:1691
#define elog(elevel,...)
Definition: elog.h:232
slock_t mutex
Definition: slot.h:120
XLogRecPtr candidate_restart_lsn
Definition: slot.h:165

◆ LogicalIncreaseXminForSlot()

void LogicalIncreaseXminForSlot ( XLogRecPtr  current_lsn,
TransactionId  xmin 
)

Definition at line 1560 of file logical.c.

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

Referenced by SnapBuildProcessRunningXacts().

1561 {
1562  bool updated_xmin = false;
1563  ReplicationSlot *slot;
1564 
1565  slot = MyReplicationSlot;
1566 
1567  Assert(slot != NULL);
1568 
1569  SpinLockAcquire(&slot->mutex);
1570 
1571  /*
1572  * don't overwrite if we already have a newer xmin. This can happen if we
1573  * restart decoding in a slot.
1574  */
1576  {
1577  }
1578 
1579  /*
1580  * If the client has already confirmed up to this lsn, we directly can
1581  * mark this as accepted. This can happen if we restart decoding in a
1582  * slot.
1583  */
1584  else if (current_lsn <= slot->data.confirmed_flush)
1585  {
1586  slot->candidate_catalog_xmin = xmin;
1587  slot->candidate_xmin_lsn = current_lsn;
1588 
1589  /* our candidate can directly be used */
1590  updated_xmin = true;
1591  }
1592 
1593  /*
1594  * Only increase if the previous values have been applied, otherwise we
1595  * might never end up updating if the receiver acks too slowly.
1596  */
1597  else if (slot->candidate_xmin_lsn == InvalidXLogRecPtr)
1598  {
1599  slot->candidate_catalog_xmin = xmin;
1600  slot->candidate_xmin_lsn = current_lsn;
1601  }
1602  SpinLockRelease(&slot->mutex);
1603 
1604  /* candidate already valid with the current flush position, apply */
1605  if (updated_xmin)
1607 }
TransactionId candidate_catalog_xmin
Definition: slot.h:162
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
ReplicationSlotPersistentData data
Definition: slot.h:147
XLogRecPtr confirmed_flush
Definition: slot.h:84
#define SpinLockAcquire(lock)
Definition: spin.h:62
bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:319
TransactionId catalog_xmin
Definition: slot.h:70
#define SpinLockRelease(lock)
Definition: spin.h:64
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:804
void LogicalConfirmReceivedLocation(XLogRecPtr lsn)
Definition: logical.c:1691
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:163
slock_t mutex
Definition: slot.h:120

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

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

Referenced by StartupDecodingContext().

1169 {
1170  LogicalDecodingContext *ctx = cache->private_data;
1172  ErrorContextCallback errcallback;
1173 
1174  Assert(!ctx->fast_forward);
1175 
1176  if (ctx->callbacks.message_cb == NULL)
1177  return;
1178 
1179  /* Push callback + info on the error context stack */
1180  state.ctx = ctx;
1181  state.callback_name = "message";
1182  state.report_location = message_lsn;
1183  errcallback.callback = output_plugin_error_callback;
1184  errcallback.arg = (void *) &state;
1185  errcallback.previous = error_context_stack;
1186  error_context_stack = &errcallback;
1187 
1188  /* set output state */
1189  ctx->accept_writes = true;
1190  ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
1191  ctx->write_location = message_lsn;
1192 
1193  /* do the actual work: call callback */
1194  ctx->callbacks.message_cb(ctx, txn, message_lsn, transactional, prefix,
1195  message_size, message);
1196 
1197  /* Pop the error context stack */
1198  error_context_stack = errcallback.previous;
1199 }
TransactionId write_xid
Definition: logical.h:108
void * private_data
LogicalDecodeMessageCB message_cb
OutputPluginCallbacks callbacks
Definition: logical.h:52
void(* callback)(void *arg)
Definition: elog.h:247
struct ErrorContextCallback * previous
Definition: elog.h:246
const char * callback_name
Definition: logical.c:50
XLogRecPtr write_location
Definition: logical.h:107
ErrorContextCallback * error_context_stack
Definition: elog.c:93
#define InvalidTransactionId
Definition: transam.h:31
TransactionId xid
#define Assert(condition)
Definition: c.h:804
Definition: regguts.h:317
XLogRecPtr report_location
Definition: logical.c:51
LogicalDecodingContext * ctx
Definition: logical.c:49
static void output_plugin_error_callback(void *arg)
Definition: logical.c:705

◆ output_plugin_error_callback()

static void output_plugin_error_callback ( void *  arg)
static

Definition at line 705 of file logical.c.

References LogicalErrorCallbackState::callback_name, LogicalErrorCallbackState::ctx, ReplicationSlot::data, errcontext, InvalidXLogRecPtr, LSN_FORMAT_ARGS, ReplicationSlotPersistentData::name, NameStr, ReplicationSlotPersistentData::plugin, LogicalErrorCallbackState::report_location, and LogicalDecodingContext::slot.

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(), and truncate_cb_wrapper().

706 {
708 
709  /* not all callbacks have an associated LSN */
710  if (state->report_location != InvalidXLogRecPtr)
711  errcontext("slot \"%s\", output plugin \"%s\", in the %s callback, associated LSN %X/%X",
712  NameStr(state->ctx->slot->data.name),
713  NameStr(state->ctx->slot->data.plugin),
714  state->callback_name,
716  else
717  errcontext("slot \"%s\", output plugin \"%s\", in the %s callback",
718  NameStr(state->ctx->slot->data.name),
719  NameStr(state->ctx->slot->data.plugin),
720  state->callback_name);
721 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
ReplicationSlotPersistentData data
Definition: slot.h:147
const char * callback_name
Definition: logical.c:50
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
Definition: regguts.h:317
XLogRecPtr report_location
Definition: logical.c:51
ReplicationSlot * slot
Definition: logical.h:38
#define NameStr(name)
Definition: c.h:681
#define errcontext
Definition: elog.h:204
void * arg
LogicalDecodingContext * ctx
Definition: logical.c:49

◆ OutputPluginPrepareWrite()

void OutputPluginPrepareWrite ( struct LogicalDecodingContext ctx,
bool  last_write 
)

Definition at line 644 of file logical.c.

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_begin_txn(), pgoutput_change(), pgoutput_commit_prepared_txn(), pgoutput_commit_txn(), pgoutput_message(), pgoutput_prepare_txn(), pgoutput_rollback_prepared_txn(), pgoutput_stream_abort(), pgoutput_stream_commit(), pgoutput_stream_start(), pgoutput_stream_stop(), pgoutput_truncate(), send_relation_and_attrs(), and send_repl_origin().

645 {
646  if (!ctx->accept_writes)
647  elog(ERROR, "writes are only accepted in commit, begin and change callbacks");
648 
649  ctx->prepare_write(ctx, ctx->write_location, ctx->write_xid, last_write);
650  ctx->prepared_write = true;
651 }
TransactionId write_xid
Definition: logical.h:108
XLogRecPtr write_location
Definition: logical.h:107
#define ERROR
Definition: elog.h:46
LogicalOutputPluginWriterPrepareWrite prepare_write
Definition: logical.h:63
#define elog(elevel,...)
Definition: elog.h:232

◆ OutputPluginUpdateProgress()

void OutputPluginUpdateProgress ( struct LogicalDecodingContext ctx)

Definition at line 670 of file logical.c.

References LogicalDecodingContext::update_progress, LogicalDecodingContext::write_location, and LogicalDecodingContext::write_xid.

Referenced by pgoutput_commit_prepared_txn(), pgoutput_commit_txn(), pgoutput_prepare_txn(), pgoutput_rollback_prepared_txn(), and pgoutput_stream_commit().

671 {
672  if (!ctx->update_progress)
673  return;
674 
675  ctx->update_progress(ctx, ctx->write_location, ctx->write_xid);
676 }
TransactionId write_xid
Definition: logical.h:108
XLogRecPtr write_location
Definition: logical.h:107
LogicalOutputPluginWriterUpdateProgress update_progress
Definition: logical.h:65

◆ OutputPluginWrite()

void OutputPluginWrite ( struct LogicalDecodingContext ctx,
bool  last_write 
)

Definition at line 657 of file logical.c.

References elog, ERROR, LogicalDecodingContext::prepared_write, LogicalDecodingContext::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_begin_txn(), pgoutput_change(), pgoutput_commit_prepared_txn(), pgoutput_commit_txn(), pgoutput_message(), pgoutput_prepare_txn(), pgoutput_rollback_prepared_txn(), pgoutput_stream_abort(), pgoutput_stream_commit(), pgoutput_stream_start(), pgoutput_stream_stop(), pgoutput_truncate(), send_relation_and_attrs(), and send_repl_origin().

658 {
659  if (!ctx->prepared_write)
660  elog(ERROR, "OutputPluginPrepareWrite needs to be called before OutputPluginWrite");
661 
662  ctx->write(ctx, ctx->write_location, ctx->write_xid, last_write);
663  ctx->prepared_write = false;
664 }
TransactionId write_xid
Definition: logical.h:108
XLogRecPtr write_location
Definition: logical.h:107
#define ERROR
Definition: elog.h:46
LogicalOutputPluginWriterWrite write
Definition: logical.h:64
#define elog(elevel,...)
Definition: elog.h:232

◆ prepare_cb_wrapper()

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

Definition at line 894 of file logical.c.

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert, ErrorContextCallback::callback, LogicalErrorCallbackState::callback_name, LogicalDecodingContext::callbacks, LogicalErrorCallbackState::ctx, ReorderBufferTXN::end_lsn, 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, LogicalErrorCallbackState::report_location, LogicalDecodingContext::twophase, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

896 {
897  LogicalDecodingContext *ctx = cache->private_data;
899  ErrorContextCallback errcallback;
900 
901  Assert(!ctx->fast_forward);
902 
903  /* We're only supposed to call this when two-phase commits are supported */
904  Assert(ctx->twophase);
905 
906  /* Push callback + info on the error context stack */
907  state.ctx = ctx;
908  state.callback_name = "prepare";
909  state.report_location = txn->final_lsn; /* beginning of prepare record */
911  errcallback.arg = (void *) &state;
912  errcallback.previous = error_context_stack;
913  error_context_stack = &errcallback;
914 
915  /* set output state */
916  ctx->accept_writes = true;
917  ctx->write_xid = txn->xid;
918  ctx->write_location = txn->end_lsn; /* points to the end of the record */
919 
920  /*
921  * If the plugin supports two-phase commits then prepare callback is
922  * mandatory
923  */
924  if (ctx->callbacks.prepare_cb == NULL)
925  ereport(ERROR,
926  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
927  errmsg("logical replication at prepare time requires a %s callback",
928  "prepare_cb")));
929 
930  /* do the actual work: call callback */
931  ctx->callbacks.prepare_cb(ctx, txn, prepare_lsn);
932 
933  /* Pop the error context stack */
934  error_context_stack = errcallback.previous;
935 }
TransactionId write_xid
Definition: logical.h:108
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:52
int errcode(int sqlerrcode)
Definition: elog.c:698
void(* callback)(void *arg)
Definition: elog.h:247
struct ErrorContextCallback * previous
Definition: elog.h:246
const char * callback_name
Definition: logical.c:50
XLogRecPtr write_location
Definition: logical.h:107
ErrorContextCallback * error_context_stack
Definition: elog.c:93
LogicalDecodePrepareCB prepare_cb
#define ERROR
Definition: elog.h:46
XLogRecPtr final_lsn
TransactionId xid
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
Definition: regguts.h:317
XLogRecPtr end_lsn
XLogRecPtr report_location
Definition: logical.c:51
int errmsg(const char *fmt,...)
Definition: elog.c:909
LogicalDecodingContext * ctx
Definition: logical.c:49
static void output_plugin_error_callback(void *arg)
Definition: logical.c:705

◆ ResetLogicalStreamingState()

void ResetLogicalStreamingState ( void  )

Definition at line 1778 of file logical.c.

References bsysscan, CheckXidAlive, and InvalidTransactionId.

Referenced by AbortSubTransaction(), and AbortTransaction().

1779 {
1781  bsysscan = false;
1782 }
bool bsysscan
Definition: xact.c:96
#define InvalidTransactionId
Definition: transam.h:31
TransactionId CheckXidAlive
Definition: xact.c:95

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

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

Referenced by StartupDecodingContext().

985 {
986  LogicalDecodingContext *ctx = cache->private_data;
988  ErrorContextCallback errcallback;
989 
990  Assert(!ctx->fast_forward);
991 
992  /* We're only supposed to call this when two-phase commits are supported */
993  Assert(ctx->twophase);
994 
995  /* Push callback + info on the error context stack */
996  state.ctx = ctx;
997  state.callback_name = "rollback_prepared";
998  state.report_location = txn->final_lsn; /* beginning of commit record */
1000  errcallback.arg = (void *) &state;
1001  errcallback.previous = error_context_stack;
1002  error_context_stack = &errcallback;
1003 
1004  /* set output state */
1005  ctx->accept_writes = true;
1006  ctx->write_xid = txn->xid;
1007  ctx->write_location = txn->end_lsn; /* points to the end of the record */
1008 
1009  /*
1010  * If the plugin support two-phase commits then rollback prepared callback
1011  * is mandatory
1012  */
1013  if (ctx->callbacks.rollback_prepared_cb == NULL)
1014  ereport(ERROR,
1015  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1016  errmsg("logical replication at prepare time requires a %s callback",
1017  "rollback_prepared_cb")));
1018 
1019  /* do the actual work: call callback */
1020  ctx->callbacks.rollback_prepared_cb(ctx, txn, prepare_end_lsn,
1021  prepare_time);
1022 
1023  /* Pop the error context stack */
1024  error_context_stack = errcallback.previous;
1025 }
TransactionId write_xid
Definition: logical.h:108
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:52
int errcode(int sqlerrcode)
Definition: elog.c:698
void(* callback)(void *arg)
Definition: elog.h:247
struct ErrorContextCallback * previous
Definition: elog.h:246
const char * callback_name
Definition: logical.c:50
XLogRecPtr write_location
Definition: logical.h:107
ErrorContextCallback * error_context_stack
Definition: elog.c:93
#define ERROR
Definition: elog.h:46
XLogRecPtr final_lsn
LogicalDecodeRollbackPreparedCB rollback_prepared_cb
TransactionId xid
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
Definition: regguts.h:317
XLogRecPtr end_lsn
XLogRecPtr report_location
Definition: logical.c:51
int errmsg(const char *fmt,...)
Definition: elog.c:909
LogicalDecodingContext * ctx
Definition: logical.c:49
static void output_plugin_error_callback(void *arg)
Definition: logical.c:705

◆ shutdown_cb_wrapper()

static void shutdown_cb_wrapper ( LogicalDecodingContext ctx)
static

Definition at line 751 of file logical.c.

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

Referenced by FreeDecodingContext().

752 {
754  ErrorContextCallback errcallback;
755 
756  Assert(!ctx->fast_forward);
757 
758  /* Push callback + info on the error context stack */
759  state.ctx = ctx;
760  state.callback_name = "shutdown";
763  errcallback.arg = (void *) &state;
764  errcallback.previous = error_context_stack;
765  error_context_stack = &errcallback;
766 
767  /* set output state */
768  ctx->accept_writes = false;
769 
770  /* do the actual work: call callback */
771  ctx->callbacks.shutdown_cb(ctx);
772 
773  /* Pop the error context stack */
774  error_context_stack = errcallback.previous;
775 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:52
void(* callback)(void *arg)
Definition: elog.h:247
struct ErrorContextCallback * previous
Definition: elog.h:246
const char * callback_name
Definition: logical.c:50
ErrorContextCallback * error_context_stack
Definition: elog.c:93
#define Assert(condition)
Definition: c.h:804
Definition: regguts.h:317
LogicalDecodeShutdownCB shutdown_cb
XLogRecPtr report_location
Definition: logical.c:51
LogicalDecodingContext * ctx
Definition: logical.c:49
static void output_plugin_error_callback(void *arg)
Definition: logical.c:705

◆ startup_cb_wrapper()

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

Definition at line 724 of file logical.c.

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

Referenced by CreateDecodingContext(), and CreateInitDecodingContext().

725 {
727  ErrorContextCallback errcallback;
728 
729  Assert(!ctx->fast_forward);
730 
731  /* Push callback + info on the error context stack */
732  state.ctx = ctx;
733  state.callback_name = "startup";
736  errcallback.arg = (void *) &state;
737  errcallback.previous = error_context_stack;
738  error_context_stack = &errcallback;
739 
740  /* set output state */
741  ctx->accept_writes = false;
742 
743  /* do the actual work: call callback */
744  ctx->callbacks.startup_cb(ctx, opt, is_init);
745 
746  /* Pop the error context stack */
747  error_context_stack = errcallback.previous;
748 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:52
void(* callback)(void *arg)
Definition: elog.h:247
struct ErrorContextCallback * previous
Definition: elog.h:246
const char * callback_name
Definition: logical.c:50
ErrorContextCallback * error_context_stack
Definition: elog.c:93
#define Assert(condition)
Definition: c.h:804
Definition: regguts.h:317
LogicalDecodeStartupCB startup_cb
XLogRecPtr report_location
Definition: logical.c:51
LogicalDecodingContext * ctx
Definition: logical.c:49
static void output_plugin_error_callback(void *arg)
Definition: logical.c:705

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

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, LogicalErrorCallbackState::ctx, CurrentMemoryContext, ReplicationSlot::data, ereport, errcode(), 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, wal_segment_size, LogicalDecodingContext::write, and XLogReaderAllocate().

Referenced by CreateDecodingContext(), and CreateInitDecodingContext().

155 {
156  ReplicationSlot *slot;
157  MemoryContext context,
158  old_context;
160 
161  /* shorter lines... */
162  slot = MyReplicationSlot;
163 
165  "Logical decoding context",
167  old_context = MemoryContextSwitchTo(context);
168  ctx = palloc0(sizeof(LogicalDecodingContext));
169 
170  ctx->context = context;
171 
172  /*
173  * (re-)load output plugins, so we detect a bad (removed) output plugin
174  * now.
175  */
176  if (!fast_forward)
178 
179  /*
180  * Now that the slot's xmin has been set, we can announce ourselves as a
181  * logical decoding backend which doesn't need to be checked individually
182  * when computing the xmin horizon because the xmin is enforced via
183  * replication slots.
184  *
185  * We can only do so if we're outside of a transaction (i.e. the case when
186  * streaming changes via walsender), otherwise an already setup
187  * snapshot/xid would end up being ignored. That's not a particularly
188  * bothersome restriction since the SQL interface can't be used for
189  * streaming anyway.
190  */
192  {
193  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
196  LWLockRelease(ProcArrayLock);
197  }
198 
199  ctx->slot = slot;
200 
201  ctx->reader = XLogReaderAllocate(wal_segment_size, NULL, xl_routine, ctx);
202  if (!ctx->reader)
203  ereport(ERROR,
204  (errcode(ERRCODE_OUT_OF_MEMORY),
205  errmsg("out of memory")));
206 
208  ctx->snapshot_builder =
209  AllocateSnapshotBuilder(ctx->reorder, xmin_horizon, start_lsn,
210  need_full_snapshot, slot->data.two_phase_at);
211 
212  ctx->reorder->private_data = ctx;
213 
214  /* wrap output plugin callbacks, so we can add error context information */
220 
221  /*
222  * To support streaming, we require start/stop/abort/commit/change
223  * callbacks. The message and truncate callbacks are optional, similar to
224  * regular output plugins. We however enable streaming when at least one
225  * of the methods is enabled so that we can easily identify missing
226  * methods.
227  *
228  * We decide it here, but only check it later in the wrappers.
229  */
230  ctx->streaming = (ctx->callbacks.stream_start_cb != NULL) ||
231  (ctx->callbacks.stream_stop_cb != NULL) ||
232  (ctx->callbacks.stream_abort_cb != NULL) ||
233  (ctx->callbacks.stream_commit_cb != NULL) ||
234  (ctx->callbacks.stream_change_cb != NULL) ||
235  (ctx->callbacks.stream_message_cb != NULL) ||
236  (ctx->callbacks.stream_truncate_cb != NULL);
237 
238  /*
239  * streaming callbacks
240  *
241  * stream_message and stream_truncate callbacks are optional, so we do not
242  * fail with ERROR when missing, but the wrappers simply do nothing. We
243  * must set the ReorderBuffer callbacks to something, otherwise the calls
244  * from there will crash (we don't want to move the checks there).
245  */
254 
255 
256  /*
257  * To support two-phase logical decoding, we require
258  * begin_prepare/prepare/commit-prepare/abort-prepare callbacks. The
259  * filter_prepare callback is optional. We however enable two-phase
260  * logical decoding when at least one of the methods is enabled so that we
261  * can easily identify missing methods.
262  *
263  * We decide it here, but only check it later in the wrappers.
264  */
265  ctx->twophase = (ctx->callbacks.begin_prepare_cb != NULL) ||
266  (ctx->callbacks.prepare_cb != NULL) ||
267  (ctx->callbacks.commit_prepared_cb != NULL) ||
268  (ctx->callbacks.rollback_prepared_cb != NULL) ||
269  (ctx->callbacks.stream_prepare_cb != NULL) ||
270  (ctx->callbacks.filter_prepare_cb != NULL);
271 
272  /*
273  * Callback to support decoding at prepare time.
274  */
279 
280  ctx->out = makeStringInfo();
281  ctx->prepare_write = prepare_write;
282  ctx->write = do_write;
283  ctx->update_progress = update_progress;
284 
285  ctx->output_plugin_options = output_plugin_options;
286 
287  ctx->fast_forward = fast_forward;
288 
289  MemoryContextSwitchTo(old_context);
290 
291  return ctx;
292 }
static void stream_stop_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr last_lsn)
Definition: logical.c:1249
static void stream_abort_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr abort_lsn)
Definition: logical.c:1296
ReorderBufferApplyChangeCB apply_change
#define AllocSetContextCreate
Definition: memutils.h:173
void * private_data
static void change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
Definition: logical.c:1028
struct ReorderBuffer * reorder
Definition: logical.h:42
#define PROC_IN_LOGICAL_DECODING
Definition: proc.h:61
int wal_segment_size
Definition: xlog.c:119
LogicalDecodeStreamPrepareCB stream_prepare_cb
PGPROC * MyProc
Definition: proc.c:68
static void commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:813
static void stream_prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
Definition: logical.c:1336
ReorderBufferStreamAbortCB stream_abort
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
OutputPluginCallbacks callbacks
Definition: logical.h:52
LogicalDecodeStreamMessageCB stream_message_cb
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:698
bool IsTransactionOrTransactionBlock(void)
Definition: xact.c:4701
PROC_HDR * ProcGlobal
Definition: proc.c:80
MemoryContext context
Definition: logical.h:35
uint8 statusFlags
Definition: proc.h:189
LogicalDecodeStreamAbortCB stream_abort_cb
List * output_plugin_options
Definition: logical.h:58
ReorderBufferCommitCB commit
ReplicationSlotPersistentData data
Definition: slot.h:147
ReorderBufferStreamCommitCB stream_commit
static void prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
Definition: logical.c:894
static void rollback_prepared_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_end_lsn, TimestampTz prepare_time)
Definition: logical.c:982
LogicalDecodeBeginPrepareCB begin_prepare_cb
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
ReorderBufferStreamMessageCB stream_message
LogicalDecodePrepareCB prepare_cb
static void stream_commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:1380
#define ERROR
Definition: elog.h:46
LogicalOutputPluginWriterPrepareWrite prepare_write
Definition: logical.h:63
ReorderBufferStreamPrepareCB stream_prepare
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:195
LogicalOutputPluginWriterUpdateProgress update_progress
Definition: logical.h:65
LogicalDecodeRollbackPreparedCB rollback_prepared_cb
ReorderBufferBeginCB begin_prepare
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
XLogReaderState * XLogReaderAllocate(int wal_segment_size, const char *waldir, XLogReaderRoutine *routine, void *private_data)
Definition: xlogreader.c:78
ReorderBufferMessageCB message
LogicalDecodeCommitPreparedCB commit_prepared_cb
static void commit_prepared_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:938
ReorderBufferStreamChangeCB stream_change
ReorderBufferStreamTruncateCB stream_truncate
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:1467
void * palloc0(Size size)
Definition: mcxt.c:1093
static void stream_start_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr first_lsn)
Definition: logical.c:1202
LogicalDecodeFilterPrepareCB filter_prepare_cb
LogicalDecodeStreamTruncateCB stream_truncate_cb
static void stream_truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
Definition: logical.c:1507
ReorderBufferStreamStartCB stream_start
#define ereport(elevel,...)
Definition: elog.h:157
static void begin_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
Definition: logical.c:783
struct SnapBuild * snapshot_builder
Definition: logical.h:43
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
ReorderBufferRollbackPreparedCB rollback_prepared
SnapBuild * AllocateSnapshotBuilder(ReorderBuffer *reorder, TransactionId xmin_horizon, XLogRecPtr start_lsn, bool need_full_snapshot, XLogRecPtr two_phase_at)
Definition: snapbuild.c:280
LogicalDecodeStreamCommitCB stream_commit_cb
static void begin_prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
Definition: logical.c:851
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
ReorderBuffer * ReorderBufferAllocate(void)
LogicalDecodeStreamStartCB stream_start_cb
static void truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
Definition: logical.c:1065
int errmsg(const char *fmt,...)
Definition: elog.c:909
XLogReaderState * reader
Definition: logical.h:41
ReplicationSlot * slot
Definition: logical.h:38
ReorderBufferApplyTruncateCB apply_truncate
LogicalOutputPluginWriterWrite write
Definition: logical.h:64
StringInfo out
Definition: logical.h:70
int pgxactoff
Definition: proc.h:148
#define NameStr(name)
Definition: c.h:681
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:1166
LogicalDecodeStreamStopCB stream_stop_cb
uint8 * statusFlags
Definition: proc.h:333
ReorderBufferBeginCB begin
static void LoadOutputPlugin(OutputPluginCallbacks *callbacks, const char *plugin)
Definition: logical.c:683
LogicalDecodeStreamChangeCB stream_change_cb
ReorderBufferCommitPreparedCB commit_prepared
static void stream_change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
Definition: logical.c:1420
XLogRecPtr two_phase_at
Definition: slot.h:90
ReorderBufferPrepareCB prepare
ReorderBufferStreamStopCB stream_stop

◆ stream_abort_cb_wrapper()

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

Definition at line 1296 of file logical.c.

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

Referenced by StartupDecodingContext().

1298 {
1299  LogicalDecodingContext *ctx = cache->private_data;
1301  ErrorContextCallback errcallback;
1302 
1303  Assert(!ctx->fast_forward);
1304 
1305  /* We're only supposed to call this when streaming is supported. */
1306  Assert(ctx->streaming);
1307 
1308  /* Push callback + info on the error context stack */
1309  state.ctx = ctx;
1310  state.callback_name = "stream_abort";
1311  state.report_location = abort_lsn;
1312  errcallback.callback = output_plugin_error_callback;
1313  errcallback.arg = (void *) &state;
1314  errcallback.previous = error_context_stack;
1315  error_context_stack = &errcallback;
1316 
1317  /* set output state */
1318  ctx->accept_writes = true;
1319  ctx->write_xid = txn->xid;
1320  ctx->write_location = abort_lsn;
1321 
1322  /* in streaming mode, stream_abort_cb is required */
1323  if (ctx->callbacks.stream_abort_cb == NULL)
1324  ereport(ERROR,
1325  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1326  errmsg("logical streaming requires a %s callback",
1327  "stream_abort_cb")));
1328 
1329  ctx->callbacks.stream_abort_cb(ctx, txn, abort_lsn);
1330 
1331  /* Pop the error context stack */
1332  error_context_stack = errcallback.previous;
1333 }
TransactionId write_xid
Definition: logical.h:108
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:52
int errcode(int sqlerrcode)
Definition: elog.c:698
LogicalDecodeStreamAbortCB stream_abort_cb
void(* callback)(void *arg)
Definition: elog.h:247
struct ErrorContextCallback * previous
Definition: elog.h:246
const char * callback_name
Definition: logical.c:50
XLogRecPtr write_location
Definition: logical.h:107
ErrorContextCallback * error_context_stack
Definition: elog.c:93
#define ERROR
Definition: elog.h:46
TransactionId xid
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
Definition: regguts.h:317
XLogRecPtr report_location
Definition: logical.c:51
int errmsg(const char *fmt,...)
Definition: elog.c:909
LogicalDecodingContext * ctx
Definition: logical.c:49
static void output_plugin_error_callback(void *arg)
Definition: logical.c:705

◆ stream_change_cb_wrapper()

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

Definition at line 1420 of file logical.c.

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

Referenced by StartupDecodingContext().

1422 {
1423  LogicalDecodingContext *ctx = cache->private_data;
1425  ErrorContextCallback errcallback;
1426 
1427  Assert(!ctx->fast_forward);
1428 
1429  /* We're only supposed to call this when streaming is supported. */
1430  Assert(ctx->streaming);
1431 
1432  /* Push callback + info on the error context stack */
1433  state.ctx = ctx;
1434  state.callback_name = "stream_change";
1435  state.report_location = change->lsn;
1436  errcallback.callback = output_plugin_error_callback;
1437  errcallback.arg = (void *) &state;
1438  errcallback.previous = error_context_stack;
1439  error_context_stack = &errcallback;
1440 
1441  /* set output state */
1442  ctx->accept_writes = true;
1443  ctx->write_xid = txn->xid;
1444 
1445  /*
1446  * report this change's lsn so replies from clients can give an up2date
1447  * answer. This won't ever be enough (and shouldn't be!) to confirm
1448  * receipt of this transaction, but it might allow another transaction's
1449  * commit to be confirmed with one message.
1450  */
1451  ctx->write_location = change->lsn;
1452 
1453  /* in streaming mode, stream_change_cb is required */
1454  if (ctx->callbacks.stream_change_cb == NULL)
1455  ereport(ERROR,
1456  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1457  errmsg("logical streaming requires a %s callback",
1458  "stream_change_cb")));
1459 
1460  ctx->callbacks.stream_change_cb(ctx, txn, relation, change);
1461 
1462  /* Pop the error context stack */
1463  error_context_stack = errcallback.previous;
1464 }
TransactionId write_xid
Definition: logical.h:108
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:52
int errcode(int sqlerrcode)
Definition: elog.c:698
void(* callback)(void *arg)
Definition: elog.h:247
struct ErrorContextCallback * previous
Definition: elog.h:246
const char * callback_name
Definition: logical.c:50
XLogRecPtr write_location
Definition: logical.h:107
ErrorContextCallback * error_context_stack
Definition: elog.c:93
#define ERROR
Definition: elog.h:46
TransactionId xid
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
Definition: regguts.h:317
XLogRecPtr report_location
Definition: logical.c:51
int errmsg(const char *fmt,...)
Definition: elog.c:909
LogicalDecodingContext * ctx
Definition: logical.c:49
static void output_plugin_error_callback(void *arg)
Definition: logical.c:705
LogicalDecodeStreamChangeCB stream_change_cb

◆ stream_commit_cb_wrapper()

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

Definition at line 1380 of file logical.c.

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

Referenced by StartupDecodingContext().

1382 {
1383  LogicalDecodingContext *ctx = cache->private_data;
1385  ErrorContextCallback errcallback;
1386 
1387  Assert(!ctx->fast_forward);
1388 
1389  /* We're only supposed to call this when streaming is supported. */
1390  Assert(ctx->streaming);
1391 
1392  /* Push callback + info on the error context stack */
1393  state.ctx = ctx;
1394  state.callback_name = "stream_commit";
1395  state.report_location = txn->final_lsn;
1396  errcallback.callback = output_plugin_error_callback;
1397  errcallback.arg = (void *) &state;
1398  errcallback.previous = error_context_stack;
1399  error_context_stack = &errcallback;
1400 
1401  /* set output state */
1402  ctx->accept_writes = true;
1403  ctx->write_xid = txn->xid;
1404  ctx->write_location = txn->end_lsn;
1405 
1406  /* in streaming mode, stream_commit_cb is required */
1407  if (ctx->callbacks.stream_commit_cb == NULL)
1408  ereport(ERROR,
1409  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1410  errmsg("logical streaming requires a %s callback",
1411  "stream_commit_cb")));
1412 
1413  ctx->callbacks.stream_commit_cb(ctx, txn, commit_lsn);
1414 
1415  /* Pop the error context stack */
1416  error_context_stack = errcallback.previous;
1417 }
TransactionId write_xid
Definition: logical.h:108
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:52
int errcode(int sqlerrcode)
Definition: elog.c:698
void(* callback)(void *arg)
Definition: elog.h:247
struct ErrorContextCallback * previous
Definition: elog.h:246
const char * callback_name
Definition: logical.c:50
XLogRecPtr write_location
Definition: logical.h:107
ErrorContextCallback * error_context_stack
Definition: elog.c:93
#define ERROR
Definition: elog.h:46
XLogRecPtr final_lsn
TransactionId xid
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
Definition: regguts.h:317
XLogRecPtr end_lsn
LogicalDecodeStreamCommitCB stream_commit_cb
XLogRecPtr report_location
Definition: logical.c:51
int errmsg(const char *fmt,...)
Definition: elog.c:909
LogicalDecodingContext * ctx
Definition: logical.c:49
static void output_plugin_error_callback(void *arg)
Definition: logical.c:705

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

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

Referenced by StartupDecodingContext().

1470 {
1471  LogicalDecodingContext *ctx = cache->private_data;
1473  ErrorContextCallback errcallback;
1474 
1475  Assert(!ctx->fast_forward);
1476 
1477  /* We're only supposed to call this when streaming is supported. */
1478  Assert(ctx->streaming);
1479 
1480  /* this callback is optional */
1481  if (ctx->callbacks.stream_message_cb == NULL)
1482  return;
1483 
1484  /* Push callback + info on the error context stack */
1485  state.ctx = ctx;
1486  state.callback_name = "stream_message";
1487  state.report_location = message_lsn;
1488  errcallback.callback = output_plugin_error_callback;
1489  errcallback.arg = (void *) &state;
1490  errcallback.previous = error_context_stack;
1491  error_context_stack = &errcallback;
1492 
1493  /* set output state */
1494  ctx->accept_writes = true;
1495  ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
1496  ctx->write_location = message_lsn;
1497 
1498  /* do the actual work: call callback */
1499  ctx->callbacks.stream_message_cb(ctx, txn, message_lsn, transactional, prefix,
1500  message_size, message);
1501 
1502  /* Pop the error context stack */
1503  error_context_stack = errcallback.previous;
1504 }
TransactionId write_xid
Definition: logical.h:108
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:52
LogicalDecodeStreamMessageCB stream_message_cb
void(* callback)(void *arg)
Definition: elog.h:247
struct ErrorContextCallback * previous
Definition: elog.h:246
const char * callback_name
Definition: logical.c:50
XLogRecPtr write_location
Definition: logical.h:107
ErrorContextCallback * error_context_stack
Definition: elog.c:93
#define InvalidTransactionId
Definition: transam.h:31
TransactionId xid
#define Assert(condition)
Definition: c.h:804
Definition: regguts.h:317
XLogRecPtr report_location
Definition: logical.c:51
LogicalDecodingContext * ctx
Definition: logical.c:49
static void output_plugin_error_callback(void *arg)
Definition: logical.c:705

◆ stream_prepare_cb_wrapper()

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

Definition at line 1336 of file logical.c.

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

Referenced by StartupDecodingContext().

1338 {
1339  LogicalDecodingContext *ctx = cache->private_data;
1341  ErrorContextCallback errcallback;
1342 
1343  Assert(!ctx->fast_forward);
1344 
1345  /*
1346  * We're only supposed to call this when streaming and two-phase commits
1347  * are supported.
1348  */
1349  Assert(ctx->streaming);
1350  Assert(ctx->twophase);
1351 
1352  /* Push callback + info on the error context stack */
1353  state.ctx = ctx;
1354  state.callback_name = "stream_prepare";
1355  state.report_location = txn->final_lsn;
1356  errcallback.callback = output_plugin_error_callback;
1357  errcallback.arg = (void *) &state;
1358  errcallback.previous = error_context_stack;
1359  error_context_stack = &errcallback;
1360 
1361  /* set output state */
1362  ctx->accept_writes = true;
1363  ctx->write_xid = txn->xid;
1364  ctx->write_location = txn->end_lsn;
1365 
1366  /* in streaming mode with two-phase commits, stream_prepare_cb is required */
1367  if (ctx->callbacks.stream_prepare_cb == NULL)
1368  ereport(ERROR,
1369  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1370  errmsg("logical streaming at prepare time requires a %s callback",
1371  "stream_prepare_cb")));
1372 
1373  ctx->callbacks.stream_prepare_cb(ctx, txn, prepare_lsn);
1374 
1375  /* Pop the error context stack */
1376  error_context_stack = errcallback.previous;
1377 }
TransactionId write_xid
Definition: logical.h:108
void * private_data
LogicalDecodeStreamPrepareCB stream_prepare_cb
OutputPluginCallbacks callbacks
Definition: logical.h:52
int errcode(int sqlerrcode)
Definition: elog.c:698
void(* callback)(void *arg)
Definition: elog.h:247
struct ErrorContextCallback * previous
Definition: elog.h:246
const char * callback_name
Definition: logical.c:50
XLogRecPtr write_location
Definition: logical.h:107
ErrorContextCallback * error_context_stack
Definition: elog.c:93
#define ERROR
Definition: elog.h:46
XLogRecPtr final_lsn
TransactionId xid
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
Definition: regguts.h:317
XLogRecPtr end_lsn
XLogRecPtr report_location
Definition: logical.c:51
int errmsg(const char *fmt,...)
Definition: elog.c:909
LogicalDecodingContext * ctx
Definition: logical.c:49
static void output_plugin_error_callback(void *arg)
Definition: logical.c:705

◆ stream_start_cb_wrapper()

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

Definition at line 1202 of file logical.c.

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

Referenced by StartupDecodingContext().

1204 {
1205  LogicalDecodingContext *ctx = cache->private_data;
1207  ErrorContextCallback errcallback;
1208 
1209  Assert(!ctx->fast_forward);
1210 
1211  /* We're only supposed to call this when streaming is supported. */
1212  Assert(ctx->streaming);
1213 
1214  /* Push callback + info on the error context stack */
1215  state.ctx = ctx;
1216  state.callback_name = "stream_start";
1217  state.report_location = first_lsn;
1218  errcallback.callback = output_plugin_error_callback;
1219  errcallback.arg = (void *) &state;
1220  errcallback.previous = error_context_stack;
1221  error_context_stack = &errcallback;
1222 
1223  /* set output state */
1224  ctx->accept_writes = true;
1225  ctx->write_xid = txn->xid;
1226 
1227  /*
1228  * report this message's lsn so replies from clients can give an up2date
1229  * answer. This won't ever be enough (and shouldn't be!) to confirm
1230  * receipt of this transaction, but it might allow another transaction's
1231  * commit to be confirmed with one message.
1232  */
1233  ctx->write_location = first_lsn;
1234 
1235  /* in streaming mode, stream_start_cb is required */
1236  if (ctx->callbacks.stream_start_cb == NULL)
1237  ereport(ERROR,
1238  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1239  errmsg("logical streaming requires a %s callback",
1240  "stream_start_cb")));
1241 
1242  ctx->callbacks.stream_start_cb(ctx, txn);
1243 
1244  /* Pop the error context stack */
1245  error_context_stack = errcallback.previous;
1246 }
TransactionId write_xid
Definition: logical.h:108
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:52
int errcode(int sqlerrcode)
Definition: elog.c:698
void(* callback)(void *arg)
Definition: elog.h:247
struct ErrorContextCallback * previous
Definition: elog.h:246
const char * callback_name
Definition: logical.c:50
XLogRecPtr write_location
Definition: logical.h:107
ErrorContextCallback * error_context_stack
Definition: elog.c:93
#define ERROR
Definition: elog.h:46
TransactionId xid
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
Definition: regguts.h:317
XLogRecPtr report_location
Definition: logical.c:51
LogicalDecodeStreamStartCB stream_start_cb
int errmsg(const char *fmt,...)
Definition: elog.c:909
LogicalDecodingContext * ctx
Definition: logical.c:49
static void output_plugin_error_callback(void *arg)
Definition: logical.c:705

◆ stream_stop_cb_wrapper()

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

Definition at line 1249 of file logical.c.

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

Referenced by StartupDecodingContext().

1251 {
1252  LogicalDecodingContext *ctx = cache->private_data;
1254  ErrorContextCallback errcallback;
1255 
1256  Assert(!ctx->fast_forward);
1257 
1258  /* We're only supposed to call this when streaming is supported. */
1259  Assert(ctx->streaming);
1260 
1261  /* Push callback + info on the error context stack */
1262  state.ctx = ctx;
1263  state.callback_name = "stream_stop";
1264  state.report_location = last_lsn;
1265  errcallback.callback = output_plugin_error_callback;
1266  errcallback.arg = (void *) &state;
1267  errcallback.previous = error_context_stack;
1268  error_context_stack = &errcallback;
1269 
1270  /* set output state */
1271  ctx->accept_writes = true;
1272  ctx->write_xid = txn->xid;
1273 
1274  /*
1275  * report this message's lsn so replies from clients can give an up2date
1276  * answer. This won't ever be enough (and shouldn't be!) to confirm
1277  * receipt of this transaction, but it might allow another transaction's
1278  * commit to be confirmed with one message.
1279  */
1280  ctx->write_location = last_lsn;
1281 
1282  /* in streaming mode, stream_stop_cb is required */
1283  if (ctx->callbacks.stream_stop_cb == NULL)
1284  ereport(ERROR,
1285  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1286  errmsg("logical streaming requires a %s callback",
1287  "stream_stop_cb")));
1288 
1289  ctx->callbacks.stream_stop_cb(ctx, txn);
1290 
1291  /* Pop the error context stack */
1292  error_context_stack = errcallback.previous;
1293 }
TransactionId write_xid
Definition: logical.h:108
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:52
int errcode(int sqlerrcode)
Definition: elog.c:698
void(* callback)(void *arg)
Definition: elog.h:247
struct ErrorContextCallback * previous
Definition: elog.h:246
const char * callback_name
Definition: logical.c:50
XLogRecPtr write_location
Definition: logical.h:107
ErrorContextCallback * error_context_stack
Definition: elog.c:93
#define ERROR
Definition: elog.h:46
TransactionId xid
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
Definition: regguts.h:317
XLogRecPtr report_location
Definition: logical.c:51
int errmsg(const char *fmt,...)
Definition: elog.c:909
LogicalDecodeStreamStopCB stream_stop_cb
LogicalDecodingContext * ctx
Definition: logical.c:49
static void output_plugin_error_callback(void *arg)
Definition: logical.c:705

◆ stream_truncate_cb_wrapper()

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

Definition at line 1507 of file logical.c.

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

Referenced by StartupDecodingContext().

1510 {
1511  LogicalDecodingContext *ctx = cache->private_data;
1513  ErrorContextCallback errcallback;
1514 
1515  Assert(!ctx->fast_forward);
1516 
1517  /* We're only supposed to call this when streaming is supported. */
1518  Assert(ctx->streaming);
1519 
1520  /* this callback is optional */
1521  if (!ctx->callbacks.stream_truncate_cb)
1522  return;
1523 
1524  /* Push callback + info on the error context stack */
1525  state.ctx = ctx;
1526  state.callback_name = "stream_truncate";
1527  state.report_location = change->lsn;
1528  errcallback.callback = output_plugin_error_callback;
1529  errcallback.arg = (void *) &state;
1530  errcallback.previous = error_context_stack;
1531  error_context_stack = &errcallback;
1532 
1533  /* set output state */
1534  ctx->accept_writes = true;
1535  ctx->write_xid = txn->xid;
1536 
1537  /*
1538  * report this change's lsn so replies from clients can give an up2date
1539  * answer. This won't ever be enough (and shouldn't be!) to confirm
1540  * receipt of this transaction, but it might allow another transaction's
1541  * commit to be confirmed with one message.
1542  */
1543  ctx->write_location = change->lsn;
1544 
1545  ctx->callbacks.stream_truncate_cb(ctx, txn, nrelations, relations, change);
1546 
1547  /* Pop the error context stack */
1548  error_context_stack = errcallback.previous;
1549 }
TransactionId write_xid
Definition: logical.h:108
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:52
void(* callback)(void *arg)
Definition: elog.h:247
struct ErrorContextCallback * previous
Definition: elog.h:246
const char * callback_name
Definition: logical.c:50
XLogRecPtr write_location
Definition: logical.h:107
ErrorContextCallback * error_context_stack
Definition: elog.c:93
LogicalDecodeStreamTruncateCB stream_truncate_cb
TransactionId xid
#define Assert(condition)
Definition: c.h:804
Definition: regguts.h:317
XLogRecPtr report_location
Definition: logical.c:51
LogicalDecodingContext * ctx
Definition: logical.c:49
static void output_plugin_error_callback(void *arg)
Definition: logical.c:705

◆ truncate_cb_wrapper()

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

Definition at line 1065 of file logical.c.

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

Referenced by StartupDecodingContext().

1067 {
1068  LogicalDecodingContext *ctx = cache->private_data;
1070  ErrorContextCallback errcallback;
1071 
1072  Assert(!ctx->fast_forward);
1073 
1074  if (!ctx->callbacks.truncate_cb)
1075  return;
1076 
1077  /* Push callback + info on the error context stack */
1078  state.ctx = ctx;
1079  state.callback_name = "truncate";
1080  state.report_location = change->lsn;
1081  errcallback.callback = output_plugin_error_callback;
1082  errcallback.arg = (void *) &state;
1083  errcallback.previous = error_context_stack;
1084  error_context_stack = &errcallback;
1085 
1086  /* set output state */
1087  ctx->accept_writes = true;
1088  ctx->write_xid = txn->xid;
1089 
1090  /*
1091  * report this change's lsn so replies from clients can give an up2date
1092  * answer. This won't ever be enough (and shouldn't be!) to confirm
1093  * receipt of this transaction, but it might allow another transaction's
1094  * commit to be confirmed with one message.
1095  */
1096  ctx->write_location = change->lsn;
1097 
1098  ctx->callbacks.truncate_cb(ctx, txn, nrelations, relations, change);
1099 
1100  /* Pop the error context stack */
1101  error_context_stack = errcallback.previous;
1102 }
LogicalDecodeTruncateCB truncate_cb
TransactionId write_xid
Definition: logical.h:108
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:52
void(* callback)(void *arg)
Definition: elog.h:247
struct ErrorContextCallback * previous
Definition: elog.h:246
const char * callback_name
Definition: logical.c:50
XLogRecPtr write_location
Definition: logical.h:107
ErrorContextCallback * error_context_stack
Definition: elog.c:93
TransactionId xid
#define Assert(condition)
Definition: c.h:804
Definition: regguts.h:317
XLogRecPtr report_location
Definition: logical.c:51
LogicalDecodingContext * ctx
Definition: logical.c:49
static void output_plugin_error_callback(void *arg)
Definition: logical.c:705

◆ UpdateDecodingStats()

void UpdateDecodingStats ( LogicalDecodingContext ctx)

Definition at line 1788 of file logical.c.

References ReplicationSlot::data, DEBUG2, elog, ReplicationSlotPersistentData::name, NameStr, namestrcpy(), pgstat_report_replslot(), LogicalDecodingContext::reorder, LogicalDecodingContext::slot, PgStat_StatReplSlotEntry::slotname, 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().

1789 {
1790  ReorderBuffer *rb = ctx->reorder;
1791  PgStat_StatReplSlotEntry repSlotStat;
1792 
1793  /* Nothing to do if we don't have any replication stats to be sent. */
1794  if (rb->spillBytes <= 0 && rb->streamBytes <= 0 && rb->totalBytes <= 0)
1795  return;
1796 
1797  elog(DEBUG2, "UpdateDecodingStats: updating stats %p %lld %lld %lld %lld %lld %lld %lld %lld",
1798  rb,
1799  (long long) rb->spillTxns,
1800  (long long) rb->spillCount,
1801  (long long) rb->spillBytes,
1802  (long long) rb->streamTxns,
1803  (long long) rb->streamCount,
1804  (long long) rb->streamBytes,
1805  (long long) rb->totalTxns,
1806  (long long) rb->totalBytes);
1807 
1808  namestrcpy(&repSlotStat.slotname, NameStr(ctx->slot->data.name));
1809  repSlotStat.spill_txns = rb->spillTxns;
1810  repSlotStat.spill_count = rb->spillCount;
1811  repSlotStat.spill_bytes = rb->spillBytes;
1812  repSlotStat.stream_txns = rb->streamTxns;
1813  repSlotStat.stream_count = rb->streamCount;
1814  repSlotStat.stream_bytes = rb->streamBytes;
1815  repSlotStat.total_txns = rb->totalTxns;
1816  repSlotStat.total_bytes = rb->totalBytes;
1817 
1818  pgstat_report_replslot(&repSlotStat);
1819 
1820  rb->spillTxns = 0;
1821  rb->spillCount = 0;
1822  rb->spillBytes = 0;
1823  rb->streamTxns = 0;
1824  rb->streamCount = 0;
1825  rb->streamBytes = 0;
1826  rb->totalTxns = 0;
1827  rb->totalBytes = 0;
1828 }
struct ReorderBuffer * reorder
Definition: logical.h:42
PgStat_Counter spill_bytes
Definition: pgstat.h:902
void namestrcpy(Name name, const char *str)
Definition: name.c:233
ReplicationSlotPersistentData data
Definition: slot.h:147
#define DEBUG2
Definition: elog.h:24
PgStat_Counter total_bytes
Definition: pgstat.h:907
PgStat_Counter stream_bytes
Definition: pgstat.h:905
PgStat_Counter spill_txns
Definition: pgstat.h:900
void pgstat_report_replslot(const PgStat_StatReplSlotEntry *repSlotStat)
Definition: pgstat.c:1808
PgStat_Counter spill_count
Definition: pgstat.h:901
ReplicationSlot * slot
Definition: logical.h:38
PgStat_Counter stream_count
Definition: pgstat.h:904
#define elog(elevel,...)
Definition: elog.h:232
#define NameStr(name)
Definition: c.h:681
PgStat_Counter total_txns
Definition: pgstat.h:906
PgStat_Counter stream_txns
Definition: pgstat.h:903