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

788 {
789  LogicalDecodingContext *ctx = cache->private_data;
791  ErrorContextCallback errcallback;
792 
793  Assert(!ctx->fast_forward);
794 
795  /* Push callback + info on the error context stack */
796  state.ctx = ctx;
797  state.callback_name = "begin";
798  state.report_location = txn->first_lsn;
800  errcallback.arg = (void *) &state;
801  errcallback.previous = error_context_stack;
802  error_context_stack = &errcallback;
803 
804  /* set output state */
805  ctx->accept_writes = true;
806  ctx->write_xid = txn->xid;
807  ctx->write_location = txn->first_lsn;
808 
809  /* do the actual work: call callback */
810  ctx->callbacks.begin_cb(ctx, txn);
811 
812  /* Pop the error context stack */
813  error_context_stack = errcallback.previous;
814 }
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:709

◆ begin_prepare_cb_wrapper()

static void begin_prepare_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn 
)
static

Definition at line 855 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().

856 {
857  LogicalDecodingContext *ctx = cache->private_data;
859  ErrorContextCallback errcallback;
860 
861  Assert(!ctx->fast_forward);
862 
863  /* We're only supposed to call this when two-phase commits are supported */
864  Assert(ctx->twophase);
865 
866  /* Push callback + info on the error context stack */
867  state.ctx = ctx;
868  state.callback_name = "begin_prepare";
869  state.report_location = txn->first_lsn;
871  errcallback.arg = (void *) &state;
872  errcallback.previous = error_context_stack;
873  error_context_stack = &errcallback;
874 
875  /* set output state */
876  ctx->accept_writes = true;
877  ctx->write_xid = txn->xid;
878  ctx->write_location = txn->first_lsn;
879 
880  /*
881  * If the plugin supports two-phase commits then begin prepare callback is
882  * mandatory
883  */
884  if (ctx->callbacks.begin_prepare_cb == NULL)
885  ereport(ERROR,
886  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
887  errmsg("logical replication at prepare time requires a %s callback",
888  "begin_prepare_cb")));
889 
890  /* do the actual work: call callback */
891  ctx->callbacks.begin_prepare_cb(ctx, txn);
892 
893  /* Pop the error context stack */
894  error_context_stack = errcallback.previous;
895 }
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:709

◆ change_cb_wrapper()

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

Definition at line 1032 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().

1034 {
1035  LogicalDecodingContext *ctx = cache->private_data;
1037  ErrorContextCallback errcallback;
1038 
1039  Assert(!ctx->fast_forward);
1040 
1041  /* Push callback + info on the error context stack */
1042  state.ctx = ctx;
1043  state.callback_name = "change";
1044  state.report_location = change->lsn;
1045  errcallback.callback = output_plugin_error_callback;
1046  errcallback.arg = (void *) &state;
1047  errcallback.previous = error_context_stack;
1048  error_context_stack = &errcallback;
1049 
1050  /* set output state */
1051  ctx->accept_writes = true;
1052  ctx->write_xid = txn->xid;
1053 
1054  /*
1055  * Report this change's lsn so replies from clients can give an up-to-date
1056  * answer. This won't ever be enough (and shouldn't be!) to confirm
1057  * receipt of this transaction, but it might allow another transaction's
1058  * commit to be confirmed with one message.
1059  */
1060  ctx->write_location = change->lsn;
1061 
1062  ctx->callbacks.change_cb(ctx, txn, relation, change);
1063 
1064  /* Pop the error context stack */
1065  error_context_stack = errcallback.previous;
1066 }
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:709

◆ 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:8328
#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 817 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().

819 {
820  LogicalDecodingContext *ctx = cache->private_data;
822  ErrorContextCallback errcallback;
823 
824  Assert(!ctx->fast_forward);
825 
826  /* Push callback + info on the error context stack */
827  state.ctx = ctx;
828  state.callback_name = "commit";
829  state.report_location = txn->final_lsn; /* beginning of commit record */
831  errcallback.arg = (void *) &state;
832  errcallback.previous = error_context_stack;
833  error_context_stack = &errcallback;
834 
835  /* set output state */
836  ctx->accept_writes = true;
837  ctx->write_xid = txn->xid;
838  ctx->write_location = txn->end_lsn; /* points to the end of the record */
839 
840  /* do the actual work: call callback */
841  ctx->callbacks.commit_cb(ctx, txn, commit_lsn);
842 
843  /* Pop the error context stack */
844  error_context_stack = errcallback.previous;
845 }
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:709

◆ commit_prepared_cb_wrapper()

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

Definition at line 942 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().

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

◆ 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, 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  * Starting at a different LSN than requested might not catch certain
525  * kinds of client errors; so the client may wish to check that
526  * confirmed_flush_lsn matches its expectations.
527  */
528  elog(LOG, "%X/%X has been already streamed, forwarding to %X/%X",
529  LSN_FORMAT_ARGS(start_lsn),
531 
532  start_lsn = slot->data.confirmed_flush;
533  }
534 
535  ctx = StartupDecodingContext(output_plugin_options,
536  start_lsn, InvalidTransactionId, false,
537  fast_forward, xl_routine, prepare_write,
538  do_write, update_progress);
539 
540  /* call output plugin initialization callback */
541  old_context = MemoryContextSwitchTo(ctx->context);
542  if (ctx->callbacks.startup_cb != NULL)
543  startup_cb_wrapper(ctx, &ctx->options, false);
544  MemoryContextSwitchTo(old_context);
545 
546  /*
547  * We allow decoding of prepared transactions when the two_phase is
548  * enabled at the time of slot creation, or when the two_phase option is
549  * given at the streaming start, provided the plugin supports all the
550  * callbacks for two-phase.
551  */
552  ctx->twophase &= (slot->data.two_phase || ctx->twophase_opt_given);
553 
554  /* Mark slot to allow two_phase decoding if not already marked */
555  if (ctx->twophase && !slot->data.two_phase)
556  {
557  slot->data.two_phase = true;
558  slot->data.two_phase_at = start_lsn;
561  SnapBuildSetTwoPhaseAt(ctx->snapshot_builder, start_lsn);
562  }
563 
565 
566  ereport(LOG,
567  (errmsg("starting logical decoding for slot \"%s\"",
568  NameStr(slot->data.name)),
569  errdetail("Streaming transactions committing after %X/%X, reading WAL from %X/%X.",
571  LSN_FORMAT_ARGS(slot->data.restart_lsn))));
572 
573  return ctx;
574 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
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:728
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:1081
#define ERROR
Definition: elog.h:46
static void startup_cb_wrapper(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
Definition: logical.c:728
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 589 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().

590 {
591  ReplicationSlot *slot = ctx->slot;
592 
593  /* Initialize from where to start reading WAL. */
594  XLogBeginRead(ctx->reader, slot->data.restart_lsn);
595 
596  elog(DEBUG1, "searching for logical decoding starting point, starting at %X/%X",
598 
599  /* Wait for a consistent starting point */
600  for (;;)
601  {
602  XLogRecord *record;
603  char *err = NULL;
604 
605  /* the read_page callback waits for new WAL */
606  record = XLogReadRecord(ctx->reader, &err);
607  if (err)
608  elog(ERROR, "%s", err);
609  if (!record)
610  elog(ERROR, "no record found"); /* shouldn't happen */
611 
613 
614  /* only continue till we found a consistent spot */
615  if (DecodingContextReady(ctx))
616  break;
617 
619  }
620 
621  SpinLockAcquire(&slot->mutex);
622  slot->data.confirmed_flush = ctx->reader->EndRecPtr;
623  if (slot->data.two_phase)
624  slot->data.two_phase_at = ctx->reader->EndRecPtr;
625  SpinLockRelease(&slot->mutex);
626 }
#define DEBUG1
Definition: elog.h:25
bool DecodingContextReady(LogicalDecodingContext *ctx)
Definition: logical.c:580
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 580 of file logical.c.

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

Referenced by DecodingContextFindStartpoint().

581 {
583 }
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 1140 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().

1141 {
1143  ErrorContextCallback errcallback;
1144  bool ret;
1145 
1146  Assert(!ctx->fast_forward);
1147 
1148  /* Push callback + info on the error context stack */
1149  state.ctx = ctx;
1150  state.callback_name = "filter_by_origin";
1152  errcallback.callback = output_plugin_error_callback;
1153  errcallback.arg = (void *) &state;
1154  errcallback.previous = error_context_stack;
1155  error_context_stack = &errcallback;
1156 
1157  /* set output state */
1158  ctx->accept_writes = false;
1159 
1160  /* do the actual work: call callback */
1161  ret = ctx->callbacks.filter_by_origin_cb(ctx, origin_id);
1162 
1163  /* Pop the error context stack */
1164  error_context_stack = errcallback.previous;
1165 
1166  return ret;
1167 }
#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:709
LogicalDecodeFilterByOriginCB filter_by_origin_cb

◆ filter_prepare_cb_wrapper()

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

Definition at line 1109 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().

1111 {
1113  ErrorContextCallback errcallback;
1114  bool ret;
1115 
1116  Assert(!ctx->fast_forward);
1117 
1118  /* Push callback + info on the error context stack */
1119  state.ctx = ctx;
1120  state.callback_name = "filter_prepare";
1122  errcallback.callback = output_plugin_error_callback;
1123  errcallback.arg = (void *) &state;
1124  errcallback.previous = error_context_stack;
1125  error_context_stack = &errcallback;
1126 
1127  /* set output state */
1128  ctx->accept_writes = false;
1129 
1130  /* do the actual work: call callback */
1131  ret = ctx->callbacks.filter_prepare_cb(ctx, xid, gid);
1132 
1133  /* Pop the error context stack */
1134  error_context_stack = errcallback.previous;
1135 
1136  return ret;
1137 }
#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:709

◆ FreeDecodingContext()

void FreeDecodingContext ( LogicalDecodingContext ctx)

Definition at line 633 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().

634 {
635  if (ctx->callbacks.shutdown_cb != NULL)
636  shutdown_cb_wrapper(ctx);
637 
640  XLogReaderFree(ctx->reader);
642 }
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:755
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 687 of file logical.c.

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

Referenced by StartupDecodingContext().

688 {
689  LogicalOutputPluginInit plugin_init;
690 
691  plugin_init = (LogicalOutputPluginInit)
692  load_external_function(plugin, "_PG_output_plugin_init", false, NULL);
693 
694  if (plugin_init == NULL)
695  elog(ERROR, "output plugins have to declare the _PG_output_plugin_init symbol");
696 
697  /* ask the output plugin to fill the callback struct */
698  plugin_init(callbacks);
699 
700  if (callbacks->begin_cb == NULL)
701  elog(ERROR, "output plugins have to register a begin callback");
702  if (callbacks->change_cb == NULL)
703  elog(ERROR, "output plugins have to register a change callback");
704  if (callbacks->commit_cb == NULL)
705  elog(ERROR, "output plugins have to register a commit callback");
706 }
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 1706 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().

1707 {
1708  Assert(lsn != InvalidXLogRecPtr);
1709 
1710  /* Do an unlocked check for candidate_lsn first. */
1713  {
1714  bool updated_xmin = false;
1715  bool updated_restart = false;
1716 
1718 
1720 
1721  /* if we're past the location required for bumping xmin, do so */
1724  {
1725  /*
1726  * We have to write the changed xmin to disk *before* we change
1727  * the in-memory value, otherwise after a crash we wouldn't know
1728  * that some catalog tuples might have been removed already.
1729  *
1730  * Ensure that by first writing to ->xmin and only update
1731  * ->effective_xmin once the new state is synced to disk. After a
1732  * crash ->effective_xmin is set to ->xmin.
1733  */
1736  {
1740  updated_xmin = true;
1741  }
1742  }
1743 
1746  {
1748 
1752  updated_restart = true;
1753  }
1754 
1756 
1757  /* first write new xmin to disk, so we know what's up after a crash */
1758  if (updated_xmin || updated_restart)
1759  {
1762  elog(DEBUG1, "updated xmin: %u restart: %u", updated_xmin, updated_restart);
1763  }
1764 
1765  /*
1766  * Now the new xmin is safely on disk, we can let the global value
1767  * advance. We do not take ProcArrayLock or similar since we only
1768  * advance xmin here and there's not much harm done by a concurrent
1769  * computation missing that.
1770  */
1771  if (updated_xmin)
1772  {
1776 
1779  }
1780  }
1781  else
1782  {
1786  }
1787 }
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 1632 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().

1633 {
1634  bool updated_lsn = false;
1635  ReplicationSlot *slot;
1636 
1637  slot = MyReplicationSlot;
1638 
1639  Assert(slot != NULL);
1640  Assert(restart_lsn != InvalidXLogRecPtr);
1641  Assert(current_lsn != InvalidXLogRecPtr);
1642 
1643  SpinLockAcquire(&slot->mutex);
1644 
1645  /* don't overwrite if have a newer restart lsn */
1646  if (restart_lsn <= slot->data.restart_lsn)
1647  {
1648  }
1649 
1650  /*
1651  * We might have already flushed far enough to directly accept this lsn,
1652  * in this case there is no need to check for existing candidate LSNs
1653  */
1654  else if (current_lsn <= slot->data.confirmed_flush)
1655  {
1656  slot->candidate_restart_valid = current_lsn;
1657  slot->candidate_restart_lsn = restart_lsn;
1658 
1659  /* our candidate can directly be used */
1660  updated_lsn = true;
1661  }
1662 
1663  /*
1664  * Only increase if the previous values have been applied, otherwise we
1665  * might never end up updating if the receiver acks too slowly. A missed
1666  * value here will just cause some extra effort after reconnecting.
1667  */
1669  {
1670  slot->candidate_restart_valid = current_lsn;
1671  slot->candidate_restart_lsn = restart_lsn;
1672  SpinLockRelease(&slot->mutex);
1673 
1674  elog(DEBUG1, "got new restart lsn %X/%X at %X/%X",
1675  LSN_FORMAT_ARGS(restart_lsn),
1676  LSN_FORMAT_ARGS(current_lsn));
1677  }
1678  else
1679  {
1680  XLogRecPtr candidate_restart_lsn;
1681  XLogRecPtr candidate_restart_valid;
1682  XLogRecPtr confirmed_flush;
1683 
1684  candidate_restart_lsn = slot->candidate_restart_lsn;
1685  candidate_restart_valid = slot->candidate_restart_valid;
1686  confirmed_flush = slot->data.confirmed_flush;
1687  SpinLockRelease(&slot->mutex);
1688 
1689  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",
1690  LSN_FORMAT_ARGS(restart_lsn),
1691  LSN_FORMAT_ARGS(current_lsn),
1692  LSN_FORMAT_ARGS(candidate_restart_lsn),
1693  LSN_FORMAT_ARGS(candidate_restart_valid),
1694  LSN_FORMAT_ARGS(confirmed_flush));
1695  }
1696 
1697  /* candidates are already valid with the current flush position, apply */
1698  if (updated_lsn)
1700 }
#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:1706
#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 1564 of file logical.c.

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

Referenced by SnapBuildProcessRunningXacts().

1565 {
1566  bool updated_xmin = false;
1567  ReplicationSlot *slot;
1568  bool got_new_xmin = false;
1569 
1570  slot = MyReplicationSlot;
1571 
1572  Assert(slot != NULL);
1573 
1574  SpinLockAcquire(&slot->mutex);
1575 
1576  /*
1577  * don't overwrite if we already have a newer xmin. This can happen if we
1578  * restart decoding in a slot.
1579  */
1581  {
1582  }
1583 
1584  /*
1585  * If the client has already confirmed up to this lsn, we directly can
1586  * mark this as accepted. This can happen if we restart decoding in a
1587  * slot.
1588  */
1589  else if (current_lsn <= slot->data.confirmed_flush)
1590  {
1591  slot->candidate_catalog_xmin = xmin;
1592  slot->candidate_xmin_lsn = current_lsn;
1593 
1594  /* our candidate can directly be used */
1595  updated_xmin = true;
1596  }
1597 
1598  /*
1599  * Only increase if the previous values have been applied, otherwise we
1600  * might never end up updating if the receiver acks too slowly.
1601  */
1602  else if (slot->candidate_xmin_lsn == InvalidXLogRecPtr)
1603  {
1604  slot->candidate_catalog_xmin = xmin;
1605  slot->candidate_xmin_lsn = current_lsn;
1606 
1607  /*
1608  * Log new xmin at an appropriate log level after releasing the
1609  * spinlock.
1610  */
1611  got_new_xmin = true;
1612  }
1613  SpinLockRelease(&slot->mutex);
1614 
1615  if (got_new_xmin)
1616  elog(DEBUG1, "got new catalog xmin %u at %X/%X", xmin,
1617  LSN_FORMAT_ARGS(current_lsn));
1618 
1619  /* candidate already valid with the current flush position, apply */
1620  if (updated_xmin)
1622 }
TransactionId candidate_catalog_xmin
Definition: slot.h:162
#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
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:1706
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:163
#define elog(elevel,...)
Definition: elog.h:232
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 1170 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().

1173 {
1174  LogicalDecodingContext *ctx = cache->private_data;
1176  ErrorContextCallback errcallback;
1177 
1178  Assert(!ctx->fast_forward);
1179 
1180  if (ctx->callbacks.message_cb == NULL)
1181  return;
1182 
1183  /* Push callback + info on the error context stack */
1184  state.ctx = ctx;
1185  state.callback_name = "message";
1186  state.report_location = message_lsn;
1187  errcallback.callback = output_plugin_error_callback;
1188  errcallback.arg = (void *) &state;
1189  errcallback.previous = error_context_stack;
1190  error_context_stack = &errcallback;
1191 
1192  /* set output state */
1193  ctx->accept_writes = true;
1194  ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
1195  ctx->write_location = message_lsn;
1196 
1197  /* do the actual work: call callback */
1198  ctx->callbacks.message_cb(ctx, txn, message_lsn, transactional, prefix,
1199  message_size, message);
1200 
1201  /* Pop the error context stack */
1202  error_context_stack = errcallback.previous;
1203 }
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:709

◆ output_plugin_error_callback()

static void output_plugin_error_callback ( void *  arg)
static

Definition at line 709 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().

710 {
712 
713  /* not all callbacks have an associated LSN */
714  if (state->report_location != InvalidXLogRecPtr)
715  errcontext("slot \"%s\", output plugin \"%s\", in the %s callback, associated LSN %X/%X",
716  NameStr(state->ctx->slot->data.name),
717  NameStr(state->ctx->slot->data.plugin),
718  state->callback_name,
720  else
721  errcontext("slot \"%s\", output plugin \"%s\", in the %s callback",
722  NameStr(state->ctx->slot->data.name),
723  NameStr(state->ctx->slot->data.plugin),
724  state->callback_name);
725 }
#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 648 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_prepare_txn(), pgoutput_stream_start(), pgoutput_stream_stop(), pgoutput_truncate(), send_relation_and_attrs(), and send_repl_origin().

649 {
650  if (!ctx->accept_writes)
651  elog(ERROR, "writes are only accepted in commit, begin and change callbacks");
652 
653  ctx->prepare_write(ctx, ctx->write_location, ctx->write_xid, last_write);
654  ctx->prepared_write = true;
655 }
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)

◆ OutputPluginWrite()

void OutputPluginWrite ( struct LogicalDecodingContext ctx,
bool  last_write 
)

Definition at line 661 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_prepare_txn(), pgoutput_stream_start(), pgoutput_stream_stop(), pgoutput_truncate(), send_relation_and_attrs(), and send_repl_origin().

662 {
663  if (!ctx->prepared_write)
664  elog(ERROR, "OutputPluginPrepareWrite needs to be called before OutputPluginWrite");
665 
666  ctx->write(ctx, ctx->write_location, ctx->write_xid, last_write);
667  ctx->prepared_write = false;
668 }
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 898 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().

900 {
901  LogicalDecodingContext *ctx = cache->private_data;
903  ErrorContextCallback errcallback;
904 
905  Assert(!ctx->fast_forward);
906 
907  /* We're only supposed to call this when two-phase commits are supported */
908  Assert(ctx->twophase);
909 
910  /* Push callback + info on the error context stack */
911  state.ctx = ctx;
912  state.callback_name = "prepare";
913  state.report_location = txn->final_lsn; /* beginning of prepare record */
915  errcallback.arg = (void *) &state;
916  errcallback.previous = error_context_stack;
917  error_context_stack = &errcallback;
918 
919  /* set output state */
920  ctx->accept_writes = true;
921  ctx->write_xid = txn->xid;
922  ctx->write_location = txn->end_lsn; /* points to the end of the record */
923 
924  /*
925  * If the plugin supports two-phase commits then prepare callback is
926  * mandatory
927  */
928  if (ctx->callbacks.prepare_cb == NULL)
929  ereport(ERROR,
930  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
931  errmsg("logical replication at prepare time requires a %s callback",
932  "prepare_cb")));
933 
934  /* do the actual work: call callback */
935  ctx->callbacks.prepare_cb(ctx, txn, prepare_lsn);
936 
937  /* Pop the error context stack */
938  error_context_stack = errcallback.previous;
939 }
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:709

◆ ResetLogicalStreamingState()

void ResetLogicalStreamingState ( void  )

Definition at line 1793 of file logical.c.

References bsysscan, CheckXidAlive, and InvalidTransactionId.

Referenced by AbortSubTransaction(), and AbortTransaction().

1794 {
1796  bsysscan = false;
1797 }
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 986 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().

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

◆ shutdown_cb_wrapper()

static void shutdown_cb_wrapper ( LogicalDecodingContext ctx)
static

Definition at line 755 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().

756 {
758  ErrorContextCallback errcallback;
759 
760  Assert(!ctx->fast_forward);
761 
762  /* Push callback + info on the error context stack */
763  state.ctx = ctx;
764  state.callback_name = "shutdown";
767  errcallback.arg = (void *) &state;
768  errcallback.previous = error_context_stack;
769  error_context_stack = &errcallback;
770 
771  /* set output state */
772  ctx->accept_writes = false;
773 
774  /* do the actual work: call callback */
775  ctx->callbacks.shutdown_cb(ctx);
776 
777  /* Pop the error context stack */
778  error_context_stack = errcallback.previous;
779 }
#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:709

◆ startup_cb_wrapper()

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

Definition at line 728 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().

729 {
731  ErrorContextCallback errcallback;
732 
733  Assert(!ctx->fast_forward);
734 
735  /* Push callback + info on the error context stack */
736  state.ctx = ctx;
737  state.callback_name = "startup";
740  errcallback.arg = (void *) &state;
741  errcallback.previous = error_context_stack;
742  error_context_stack = &errcallback;
743 
744  /* set output state */
745  ctx->accept_writes = false;
746 
747  /* do the actual work: call callback */
748  ctx->callbacks.startup_cb(ctx, opt, is_init);
749 
750  /* Pop the error context stack */
751  error_context_stack = errcallback.previous;
752 }
#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:709

◆ 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:1253
static void stream_abort_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr abort_lsn)
Definition: logical.c:1300
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:1032
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:817
static void stream_prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
Definition: logical.c:1340
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:4711
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:898
static void rollback_prepared_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_end_lsn, TimestampTz prepare_time)
Definition: logical.c:986
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:1384
#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:942
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:1471
void * palloc0(Size size)
Definition: mcxt.c:1093
static void stream_start_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr first_lsn)
Definition: logical.c:1206
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:1511
ReorderBufferStreamStartCB stream_start
#define ereport(elevel,...)
Definition: elog.h:157
static void begin_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
Definition: logical.c:787
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:855
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:1069
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:1170
LogicalDecodeStreamStopCB stream_stop_cb
uint8 * statusFlags
Definition: proc.h:333
ReorderBufferBeginCB begin
static void LoadOutputPlugin(OutputPluginCallbacks *callbacks, const char *plugin)
Definition: logical.c:687
LogicalDecodeStreamChangeCB stream_change_cb
ReorderBufferCommitPreparedCB commit_prepared
static void stream_change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
Definition: logical.c:1424
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 1300 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().

1302 {
1303  LogicalDecodingContext *ctx = cache->private_data;
1305  ErrorContextCallback errcallback;
1306 
1307  Assert(!ctx->fast_forward);
1308 
1309  /* We're only supposed to call this when streaming is supported. */
1310  Assert(ctx->streaming);
1311 
1312  /* Push callback + info on the error context stack */
1313  state.ctx = ctx;
1314  state.callback_name = "stream_abort";
1315  state.report_location = abort_lsn;
1316  errcallback.callback = output_plugin_error_callback;
1317  errcallback.arg = (void *) &state;
1318  errcallback.previous = error_context_stack;
1319  error_context_stack = &errcallback;
1320 
1321  /* set output state */
1322  ctx->accept_writes = true;
1323  ctx->write_xid = txn->xid;
1324  ctx->write_location = abort_lsn;
1325 
1326  /* in streaming mode, stream_abort_cb is required */
1327  if (ctx->callbacks.stream_abort_cb == NULL)
1328  ereport(ERROR,
1329  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1330  errmsg("logical streaming requires a %s callback",
1331  "stream_abort_cb")));
1332 
1333  ctx->callbacks.stream_abort_cb(ctx, txn, abort_lsn);
1334 
1335  /* Pop the error context stack */
1336  error_context_stack = errcallback.previous;
1337 }
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:709

◆ stream_change_cb_wrapper()

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

Definition at line 1424 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().

1426 {
1427  LogicalDecodingContext *ctx = cache->private_data;
1429  ErrorContextCallback errcallback;
1430 
1431  Assert(!ctx->fast_forward);
1432 
1433  /* We're only supposed to call this when streaming is supported. */
1434  Assert(ctx->streaming);
1435 
1436  /* Push callback + info on the error context stack */
1437  state.ctx = ctx;
1438  state.callback_name = "stream_change";
1439  state.report_location = change->lsn;
1440  errcallback.callback = output_plugin_error_callback;
1441  errcallback.arg = (void *) &state;
1442  errcallback.previous = error_context_stack;
1443  error_context_stack = &errcallback;
1444 
1445  /* set output state */
1446  ctx->accept_writes = true;
1447  ctx->write_xid = txn->xid;
1448 
1449  /*
1450  * Report this change's lsn so replies from clients can give an up-to-date
1451  * answer. This won't ever be enough (and shouldn't be!) to confirm
1452  * receipt of this transaction, but it might allow another transaction's
1453  * commit to be confirmed with one message.
1454  */
1455  ctx->write_location = change->lsn;
1456 
1457  /* in streaming mode, stream_change_cb is required */
1458  if (ctx->callbacks.stream_change_cb == NULL)
1459  ereport(ERROR,
1460  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1461  errmsg("logical streaming requires a %s callback",
1462  "stream_change_cb")));
1463 
1464  ctx->callbacks.stream_change_cb(ctx, txn, relation, change);
1465 
1466  /* Pop the error context stack */
1467  error_context_stack = errcallback.previous;
1468 }
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:709
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 1384 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().

1386 {
1387  LogicalDecodingContext *ctx = cache->private_data;
1389  ErrorContextCallback errcallback;
1390 
1391  Assert(!ctx->fast_forward);
1392 
1393  /* We're only supposed to call this when streaming is supported. */
1394  Assert(ctx->streaming);
1395 
1396  /* Push callback + info on the error context stack */
1397  state.ctx = ctx;
1398  state.callback_name = "stream_commit";
1399  state.report_location = txn->final_lsn;
1400  errcallback.callback = output_plugin_error_callback;
1401  errcallback.arg = (void *) &state;
1402  errcallback.previous = error_context_stack;
1403  error_context_stack = &errcallback;
1404 
1405  /* set output state */
1406  ctx->accept_writes = true;
1407  ctx->write_xid = txn->xid;
1408  ctx->write_location = txn->end_lsn;
1409 
1410  /* in streaming mode, stream_commit_cb is required */
1411  if (ctx->callbacks.stream_commit_cb == NULL)
1412  ereport(ERROR,
1413  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1414  errmsg("logical streaming requires a %s callback",
1415  "stream_commit_cb")));
1416 
1417  ctx->callbacks.stream_commit_cb(ctx, txn, commit_lsn);
1418 
1419  /* Pop the error context stack */
1420  error_context_stack = errcallback.previous;
1421 }
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:709

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

1474 {
1475  LogicalDecodingContext *ctx = cache->private_data;
1477  ErrorContextCallback errcallback;
1478 
1479  Assert(!ctx->fast_forward);
1480 
1481  /* We're only supposed to call this when streaming is supported. */
1482  Assert(ctx->streaming);
1483 
1484  /* this callback is optional */
1485  if (ctx->callbacks.stream_message_cb == NULL)
1486  return;
1487 
1488  /* Push callback + info on the error context stack */
1489  state.ctx = ctx;
1490  state.callback_name = "stream_message";
1491  state.report_location = message_lsn;
1492  errcallback.callback = output_plugin_error_callback;
1493  errcallback.arg = (void *) &state;
1494  errcallback.previous = error_context_stack;
1495  error_context_stack = &errcallback;
1496 
1497  /* set output state */
1498  ctx->accept_writes = true;
1499  ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
1500  ctx->write_location = message_lsn;
1501 
1502  /* do the actual work: call callback */
1503  ctx->callbacks.stream_message_cb(ctx, txn, message_lsn, transactional, prefix,
1504  message_size, message);
1505 
1506  /* Pop the error context stack */
1507  error_context_stack = errcallback.previous;
1508 }
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:709

◆ stream_prepare_cb_wrapper()

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

Definition at line 1340 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().

1342 {
1343  LogicalDecodingContext *ctx = cache->private_data;
1345  ErrorContextCallback errcallback;
1346 
1347  Assert(!ctx->fast_forward);
1348 
1349  /*
1350  * We're only supposed to call this when streaming and two-phase commits
1351  * are supported.
1352  */
1353  Assert(ctx->streaming);
1354  Assert(ctx->twophase);
1355 
1356  /* Push callback + info on the error context stack */
1357  state.ctx = ctx;
1358  state.callback_name = "stream_prepare";
1359  state.report_location = txn->final_lsn;
1360  errcallback.callback = output_plugin_error_callback;
1361  errcallback.arg = (void *) &state;
1362  errcallback.previous = error_context_stack;
1363  error_context_stack = &errcallback;
1364 
1365  /* set output state */
1366  ctx->accept_writes = true;
1367  ctx->write_xid = txn->xid;
1368  ctx->write_location = txn->end_lsn;
1369 
1370  /* in streaming mode with two-phase commits, stream_prepare_cb is required */
1371  if (ctx->callbacks.stream_prepare_cb == NULL)
1372  ereport(ERROR,
1373  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1374  errmsg("logical streaming at prepare time requires a %s callback",
1375  "stream_prepare_cb")));
1376 
1377  ctx->callbacks.stream_prepare_cb(ctx, txn, prepare_lsn);
1378 
1379  /* Pop the error context stack */
1380  error_context_stack = errcallback.previous;
1381 }
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:709

◆ stream_start_cb_wrapper()

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

Definition at line 1206 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().

1208 {
1209  LogicalDecodingContext *ctx = cache->private_data;
1211  ErrorContextCallback errcallback;
1212 
1213  Assert(!ctx->fast_forward);
1214 
1215  /* We're only supposed to call this when streaming is supported. */
1216  Assert(ctx->streaming);
1217 
1218  /* Push callback + info on the error context stack */
1219  state.ctx = ctx;
1220  state.callback_name = "stream_start";
1221  state.report_location = first_lsn;
1222  errcallback.callback = output_plugin_error_callback;
1223  errcallback.arg = (void *) &state;
1224  errcallback.previous = error_context_stack;
1225  error_context_stack = &errcallback;
1226 
1227  /* set output state */
1228  ctx->accept_writes = true;
1229  ctx->write_xid = txn->xid;
1230 
1231  /*
1232  * Report this message's lsn so replies from clients can give an
1233  * up-to-date answer. This won't ever be enough (and shouldn't be!) to
1234  * confirm receipt of this transaction, but it might allow another
1235  * transaction's commit to be confirmed with one message.
1236  */
1237  ctx->write_location = first_lsn;
1238 
1239  /* in streaming mode, stream_start_cb is required */
1240  if (ctx->callbacks.stream_start_cb == NULL)
1241  ereport(ERROR,
1242  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1243  errmsg("logical streaming requires a %s callback",
1244  "stream_start_cb")));
1245 
1246  ctx->callbacks.stream_start_cb(ctx, txn);
1247 
1248  /* Pop the error context stack */
1249  error_context_stack = errcallback.previous;
1250 }
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:709

◆ stream_stop_cb_wrapper()

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

Definition at line 1253 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().

1255 {
1256  LogicalDecodingContext *ctx = cache->private_data;
1258  ErrorContextCallback errcallback;
1259 
1260  Assert(!ctx->fast_forward);
1261 
1262  /* We're only supposed to call this when streaming is supported. */
1263  Assert(ctx->streaming);
1264 
1265  /* Push callback + info on the error context stack */
1266  state.ctx = ctx;
1267  state.callback_name = "stream_stop";
1268  state.report_location = last_lsn;
1269  errcallback.callback = output_plugin_error_callback;
1270  errcallback.arg = (void *) &state;
1271  errcallback.previous = error_context_stack;
1272  error_context_stack = &errcallback;
1273 
1274  /* set output state */
1275  ctx->accept_writes = true;
1276  ctx->write_xid = txn->xid;
1277 
1278  /*
1279  * Report this message's lsn so replies from clients can give an
1280  * up-to-date answer. This won't ever be enough (and shouldn't be!) to
1281  * confirm receipt of this transaction, but it might allow another
1282  * transaction's commit to be confirmed with one message.
1283  */
1284  ctx->write_location = last_lsn;
1285 
1286  /* in streaming mode, stream_stop_cb is required */
1287  if (ctx->callbacks.stream_stop_cb == NULL)
1288  ereport(ERROR,
1289  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1290  errmsg("logical streaming requires a %s callback",
1291  "stream_stop_cb")));
1292 
1293  ctx->callbacks.stream_stop_cb(ctx, txn);
1294 
1295  /* Pop the error context stack */
1296  error_context_stack = errcallback.previous;
1297 }
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:709

◆ stream_truncate_cb_wrapper()

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

Definition at line 1511 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().

1514 {
1515  LogicalDecodingContext *ctx = cache->private_data;
1517  ErrorContextCallback errcallback;
1518 
1519  Assert(!ctx->fast_forward);
1520 
1521  /* We're only supposed to call this when streaming is supported. */
1522  Assert(ctx->streaming);
1523 
1524  /* this callback is optional */
1525  if (!ctx->callbacks.stream_truncate_cb)
1526  return;
1527 
1528  /* Push callback + info on the error context stack */
1529  state.ctx = ctx;
1530  state.callback_name = "stream_truncate";
1531  state.report_location = change->lsn;
1532  errcallback.callback = output_plugin_error_callback;
1533  errcallback.arg = (void *) &state;
1534  errcallback.previous = error_context_stack;
1535  error_context_stack = &errcallback;
1536 
1537  /* set output state */
1538  ctx->accept_writes = true;
1539  ctx->write_xid = txn->xid;
1540 
1541  /*
1542  * Report this change's lsn so replies from clients can give an up-to-date
1543  * answer. This won't ever be enough (and shouldn't be!) to confirm
1544  * receipt of this transaction, but it might allow another transaction's
1545  * commit to be confirmed with one message.
1546  */
1547  ctx->write_location = change->lsn;
1548 
1549  ctx->callbacks.stream_truncate_cb(ctx, txn, nrelations, relations, change);
1550 
1551  /* Pop the error context stack */
1552  error_context_stack = errcallback.previous;
1553 }
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:709

◆ truncate_cb_wrapper()

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

Definition at line 1069 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().

1071 {
1072  LogicalDecodingContext *ctx = cache->private_data;
1074  ErrorContextCallback errcallback;
1075 
1076  Assert(!ctx->fast_forward);
1077 
1078  if (!ctx->callbacks.truncate_cb)
1079  return;
1080 
1081  /* Push callback + info on the error context stack */
1082  state.ctx = ctx;
1083  state.callback_name = "truncate";
1084  state.report_location = change->lsn;
1085  errcallback.callback = output_plugin_error_callback;
1086  errcallback.arg = (void *) &state;
1087  errcallback.previous = error_context_stack;
1088  error_context_stack = &errcallback;
1089 
1090  /* set output state */
1091  ctx->accept_writes = true;
1092  ctx->write_xid = txn->xid;
1093 
1094  /*
1095  * Report this change's lsn so replies from clients can give an up-to-date
1096  * answer. This won't ever be enough (and shouldn't be!) to confirm
1097  * receipt of this transaction, but it might allow another transaction's
1098  * commit to be confirmed with one message.
1099  */
1100  ctx->write_location = change->lsn;
1101 
1102  ctx->callbacks.truncate_cb(ctx, txn, nrelations, relations, change);
1103 
1104  /* Pop the error context stack */
1105  error_context_stack = errcallback.previous;
1106 }
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:709

◆ UpdateDecodingStats()

void UpdateDecodingStats ( LogicalDecodingContext ctx)

Definition at line 1803 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().

1804 {
1805  ReorderBuffer *rb = ctx->reorder;
1806  PgStat_StatReplSlotEntry repSlotStat;
1807 
1808  /* Nothing to do if we don't have any replication stats to be sent. */
1809  if (rb->spillBytes <= 0 && rb->streamBytes <= 0 && rb->totalBytes <= 0)
1810  return;
1811 
1812  elog(DEBUG2, "UpdateDecodingStats: updating stats %p %lld %lld %lld %lld %lld %lld %lld %lld",
1813  rb,
1814  (long long) rb->spillTxns,
1815  (long long) rb->spillCount,
1816  (long long) rb->spillBytes,
1817  (long long) rb->streamTxns,
1818  (long long) rb->streamCount,
1819  (long long) rb->streamBytes,
1820  (long long) rb->totalTxns,
1821  (long long) rb->totalBytes);
1822 
1823  namestrcpy(&repSlotStat.slotname, NameStr(ctx->slot->data.name));
1824  repSlotStat.spill_txns = rb->spillTxns;
1825  repSlotStat.spill_count = rb->spillCount;
1826  repSlotStat.spill_bytes = rb->spillBytes;
1827  repSlotStat.stream_txns = rb->streamTxns;
1828  repSlotStat.stream_count = rb->streamCount;
1829  repSlotStat.stream_bytes = rb->streamBytes;
1830  repSlotStat.total_txns = rb->totalTxns;
1831  repSlotStat.total_bytes = rb->totalBytes;
1832 
1833  pgstat_report_replslot(&repSlotStat);
1834 
1835  rb->spillTxns = 0;
1836  rb->spillCount = 0;
1837  rb->spillBytes = 0;
1838  rb->streamTxns = 0;
1839  rb->streamCount = 0;
1840  rb->streamBytes = 0;
1841  rb->totalTxns = 0;
1842  rb->totalBytes = 0;
1843 }
struct ReorderBuffer * reorder
Definition: logical.h:42
PgStat_Counter spill_bytes
Definition: pgstat.h:923
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:928
PgStat_Counter stream_bytes
Definition: pgstat.h:926
PgStat_Counter spill_txns
Definition: pgstat.h:921
void pgstat_report_replslot(const PgStat_StatReplSlotEntry *repSlotStat)
Definition: pgstat.c:1814
PgStat_Counter spill_count
Definition: pgstat.h:922
ReplicationSlot * slot
Definition: logical.h:38
PgStat_Counter stream_count
Definition: pgstat.h:925
#define elog(elevel,...)
Definition: elog.h:232
#define NameStr(name)
Definition: c.h:681
PgStat_Counter total_txns
Definition: pgstat.h:927
PgStat_Counter stream_txns
Definition: pgstat.h:924