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, LogicalDecodingXLogPageReadCB page_read, WALSegmentCleanupCB cleanup_cb, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
 
LogicalDecodingContextCreateInitDecodingContext (const char *plugin, List *output_plugin_options, bool need_full_snapshot, XLogRecPtr restart_lsn, LogicalDecodingXLogPageReadCB page_read, WALSegmentCleanupCB cleanup_cb, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
 
LogicalDecodingContextCreateDecodingContext (XLogRecPtr start_lsn, List *output_plugin_options, bool fast_forward, LogicalDecodingXLogPageReadCB page_read, WALSegmentCleanupCB cleanup_cb, 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 778 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().

779 {
780  LogicalDecodingContext *ctx = cache->private_data;
782  ErrorContextCallback errcallback;
783 
784  Assert(!ctx->fast_forward);
785 
786  /* Push callback + info on the error context stack */
787  state.ctx = ctx;
788  state.callback_name = "begin";
789  state.report_location = txn->first_lsn;
791  errcallback.arg = (void *) &state;
792  errcallback.previous = error_context_stack;
793  error_context_stack = &errcallback;
794 
795  /* set output state */
796  ctx->accept_writes = true;
797  ctx->write_xid = txn->xid;
798  ctx->write_location = txn->first_lsn;
799 
800  /* do the actual work: call callback */
801  ctx->callbacks.begin_cb(ctx, txn);
802 
803  /* Pop the error context stack */
804  error_context_stack = errcallback.previous;
805 }
XLogRecPtr first_lsn
TransactionId write_xid
Definition: logical.h:101
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:55
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:100
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:700

◆ begin_prepare_cb_wrapper()

static void begin_prepare_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn 
)
static

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

847 {
848  LogicalDecodingContext *ctx = cache->private_data;
850  ErrorContextCallback errcallback;
851 
852  Assert(!ctx->fast_forward);
853 
854  /* We're only supposed to call this when two-phase commits are supported */
855  Assert(ctx->twophase);
856 
857  /* Push callback + info on the error context stack */
858  state.ctx = ctx;
859  state.callback_name = "begin_prepare";
860  state.report_location = txn->first_lsn;
862  errcallback.arg = (void *) &state;
863  errcallback.previous = error_context_stack;
864  error_context_stack = &errcallback;
865 
866  /* set output state */
867  ctx->accept_writes = true;
868  ctx->write_xid = txn->xid;
869  ctx->write_location = txn->first_lsn;
870 
871  /*
872  * If the plugin supports two-phase commits then begin prepare callback is
873  * mandatory
874  */
875  if (ctx->callbacks.begin_prepare_cb == NULL)
876  ereport(ERROR,
877  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
878  errmsg("logical replication at prepare time requires begin_prepare_cb callback")));
879 
880  /* do the actual work: call callback */
881  ctx->callbacks.begin_prepare_cb(ctx, txn);
882 
883  /* Pop the error context stack */
884  error_context_stack = errcallback.previous;
885 }
XLogRecPtr first_lsn
TransactionId write_xid
Definition: logical.h:101
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:55
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:100
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:700

◆ change_cb_wrapper()

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

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

1021 {
1022  LogicalDecodingContext *ctx = cache->private_data;
1024  ErrorContextCallback errcallback;
1025 
1026  Assert(!ctx->fast_forward);
1027 
1028  /* Push callback + info on the error context stack */
1029  state.ctx = ctx;
1030  state.callback_name = "change";
1031  state.report_location = change->lsn;
1032  errcallback.callback = output_plugin_error_callback;
1033  errcallback.arg = (void *) &state;
1034  errcallback.previous = error_context_stack;
1035  error_context_stack = &errcallback;
1036 
1037  /* set output state */
1038  ctx->accept_writes = true;
1039  ctx->write_xid = txn->xid;
1040 
1041  /*
1042  * report this change's lsn so replies from clients can give an up2date
1043  * answer. This won't ever be enough (and shouldn't be!) to confirm
1044  * receipt of this transaction, but it might allow another transaction's
1045  * commit to be confirmed with one message.
1046  */
1047  ctx->write_location = change->lsn;
1048 
1049  ctx->callbacks.change_cb(ctx, txn, relation, change);
1050 
1051  /* Pop the error context stack */
1052  error_context_stack = errcallback.previous;
1053 }
TransactionId write_xid
Definition: logical.h:101
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:55
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:100
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:700

◆ 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:1068
int wal_level
Definition: xlog.c:109
int errcode(int sqlerrcode)
Definition: elog.c:698
bool RecoveryInProgress(void)
Definition: xlog.c:8237
#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 808 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().

810 {
811  LogicalDecodingContext *ctx = cache->private_data;
813  ErrorContextCallback errcallback;
814 
815  Assert(!ctx->fast_forward);
816 
817  /* Push callback + info on the error context stack */
818  state.ctx = ctx;
819  state.callback_name = "commit";
820  state.report_location = txn->final_lsn; /* beginning of commit record */
822  errcallback.arg = (void *) &state;
823  errcallback.previous = error_context_stack;
824  error_context_stack = &errcallback;
825 
826  /* set output state */
827  ctx->accept_writes = true;
828  ctx->write_xid = txn->xid;
829  ctx->write_location = txn->end_lsn; /* points to the end of the record */
830 
831  /* do the actual work: call callback */
832  ctx->callbacks.commit_cb(ctx, txn, commit_lsn);
833 
834  /* Pop the error context stack */
835  error_context_stack = errcallback.previous;
836 }
TransactionId write_xid
Definition: logical.h:101
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:55
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:100
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:700

◆ commit_prepared_cb_wrapper()

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

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

933 {
934  LogicalDecodingContext *ctx = cache->private_data;
936  ErrorContextCallback errcallback;
937 
938  Assert(!ctx->fast_forward);
939 
940  /* We're only supposed to call this when two-phase commits are supported */
941  Assert(ctx->twophase);
942 
943  /* Push callback + info on the error context stack */
944  state.ctx = ctx;
945  state.callback_name = "commit_prepared";
946  state.report_location = txn->final_lsn; /* beginning of commit record */
948  errcallback.arg = (void *) &state;
949  errcallback.previous = error_context_stack;
950  error_context_stack = &errcallback;
951 
952  /* set output state */
953  ctx->accept_writes = true;
954  ctx->write_xid = txn->xid;
955  ctx->write_location = txn->end_lsn; /* points to the end of the record */
956 
957  /*
958  * If the plugin support two-phase commits then commit prepared callback
959  * is mandatory
960  */
961  if (ctx->callbacks.commit_prepared_cb == NULL)
962  ereport(ERROR,
963  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
964  errmsg("logical replication at prepare time requires commit_prepared_cb callback")));
965 
966  /* do the actual work: call callback */
967  ctx->callbacks.commit_prepared_cb(ctx, txn, commit_lsn);
968 
969  /* Pop the error context stack */
970  error_context_stack = errcallback.previous;
971 }
TransactionId write_xid
Definition: logical.h:101
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:55
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:100
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:700

◆ CreateDecodingContext()

LogicalDecodingContext* CreateDecodingContext ( XLogRecPtr  start_lsn,
List output_plugin_options,
bool  fast_forward,
LogicalDecodingXLogPageReadCB  page_read,
WALSegmentCleanupCB  cleanup_cb,
LogicalOutputPluginWriterPrepareWrite  prepare_write,
LogicalOutputPluginWriterWrite  do_write,
LogicalOutputPluginWriterUpdateProgress  update_progress 
)

Definition at line 479 of file logical.c.

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

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

487 {
489  ReplicationSlot *slot;
490  MemoryContext old_context;
491 
492  /* shorter lines... */
493  slot = MyReplicationSlot;
494 
495  /* first some sanity checks that are unlikely to be violated */
496  if (slot == NULL)
497  elog(ERROR, "cannot perform logical decoding without an acquired slot");
498 
499  /* make sure the passed slot is suitable, these are user facing errors */
500  if (SlotIsPhysical(slot))
501  ereport(ERROR,
502  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
503  errmsg("cannot use physical replication slot for logical decoding")));
504 
505  if (slot->data.database != MyDatabaseId)
506  ereport(ERROR,
507  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
508  errmsg("replication slot \"%s\" was not created in this database",
509  NameStr(slot->data.name))));
510 
511  if (start_lsn == InvalidXLogRecPtr)
512  {
513  /* continue from last position */
514  start_lsn = slot->data.confirmed_flush;
515  }
516  else if (start_lsn < slot->data.confirmed_flush)
517  {
518  /*
519  * It might seem like we should error out in this case, but it's
520  * pretty common for a client to acknowledge a LSN it doesn't have to
521  * do anything for, and thus didn't store persistently, because the
522  * xlog records didn't result in anything relevant for logical
523  * decoding. Clients have to be able to do that to support synchronous
524  * replication.
525  */
526  elog(DEBUG1, "cannot stream from %X/%X, minimum is %X/%X, forwarding",
527  LSN_FORMAT_ARGS(start_lsn),
529 
530  start_lsn = slot->data.confirmed_flush;
531  }
532 
533  ctx = StartupDecodingContext(output_plugin_options,
534  start_lsn, InvalidTransactionId, false,
535  fast_forward, page_read, cleanup_cb,
536  prepare_write, do_write, update_progress);
537 
538  /* call output plugin initialization callback */
539  old_context = MemoryContextSwitchTo(ctx->context);
540  if (ctx->callbacks.startup_cb != NULL)
541  startup_cb_wrapper(ctx, &ctx->options, false);
542  MemoryContextSwitchTo(old_context);
543 
544  /*
545  * We allow decoding of prepared transactions iff the two_phase option is
546  * enabled at the time of slot creation.
547  */
549 
551 
552  ereport(LOG,
553  (errmsg("starting logical decoding for slot \"%s\"",
554  NameStr(slot->data.name)),
555  errdetail("Streaming transactions committing after %X/%X, reading WAL from %X/%X.",
557  LSN_FORMAT_ARGS(slot->data.restart_lsn))));
558 
559  return ctx;
560 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
struct ReorderBuffer * reorder
Definition: logical.h:45
OutputPluginOptions options
Definition: logical.h:56
OutputPluginCallbacks callbacks
Definition: logical.h:55
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:698
MemoryContext context
Definition: logical.h:37
ReplicationSlotPersistentData data
Definition: slot.h:156
#define LOG
Definition: elog.h:26
static LogicalDecodingContext * StartupDecodingContext(List *output_plugin_options, XLogRecPtr start_lsn, TransactionId xmin_horizon, bool need_full_snapshot, bool fast_forward, LogicalDecodingXLogPageReadCB page_read, WALSegmentCleanupCB cleanup_cb, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
Definition: logical.c:146
#define SlotIsPhysical(slot)
Definition: slot.h:177
XLogRecPtr confirmed_flush
Definition: slot.h:92
#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:719
int errdetail(const char *fmt,...)
Definition: elog.c:1042
#define InvalidTransactionId
Definition: transam.h:31
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:81
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

◆ CreateInitDecodingContext()

LogicalDecodingContext* CreateInitDecodingContext ( const char *  plugin,
List output_plugin_options,
bool  need_full_snapshot,
XLogRecPtr  restart_lsn,
LogicalDecodingXLogPageReadCB  page_read,
WALSegmentCleanupCB  cleanup_cb,
LogicalOutputPluginWriterPrepareWrite  prepare_write,
LogicalOutputPluginWriterWrite  do_write,
LogicalOutputPluginWriterUpdateProgress  update_progress 
)

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

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

◆ DecodingContextFindStartpoint()

void DecodingContextFindStartpoint ( LogicalDecodingContext ctx)

Definition at line 575 of file logical.c.

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

Referenced by create_logical_replication_slot(), and CreateReplicationSlot().

576 {
577  ReplicationSlot *slot = ctx->slot;
578 
579  /* Initialize from where to start reading WAL. */
580  XLogBeginRead(ctx->reader, slot->data.restart_lsn);
581 
582  elog(DEBUG1, "searching for logical decoding starting point, starting at %X/%X",
584 
585  /* Wait for a consistent starting point */
586  for (;;)
587  {
588  XLogRecord *record;
589  char *err = NULL;
590 
591  /* the read_page callback waits for new WAL */
592  while (XLogReadRecord(ctx->reader, &record, &err) ==
594  {
595  if (!ctx->page_read(ctx->reader))
596  break;
597  }
598 
599  if (err)
600  elog(ERROR, "%s", err);
601  if (!record)
602  elog(ERROR, "no record found"); /* shouldn't happen */
603 
605 
606  /* only continue till we found a consistent spot */
607  if (DecodingContextReady(ctx))
608  break;
609 
611  }
612 
613  SpinLockAcquire(&slot->mutex);
614  slot->data.confirmed_flush = ctx->reader->EndRecPtr;
616  SpinLockRelease(&slot->mutex);
617 }
#define DEBUG1
Definition: elog.h:25
bool DecodingContextReady(LogicalDecodingContext *ctx)
Definition: logical.c:566
ReplicationSlotPersistentData data
Definition: slot.h:156
XLogReadRecordResult XLogReadRecord(XLogReaderState *state, XLogRecord **record, char **errormsg)
Definition: xlogreader.c:346
XLogRecPtr confirmed_flush
Definition: slot.h:92
XLogRecPtr EndRecPtr
Definition: xlogreader.h:179
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
#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:252
XLogRecPtr initial_consistent_point
Definition: slot.h:99
#define SpinLockRelease(lock)
Definition: spin.h:64
XLogRecPtr restart_lsn
Definition: slot.h:81
XLogReaderState * reader
Definition: logical.h:43
ReplicationSlot * slot
Definition: logical.h:40
#define elog(elevel,...)
Definition: elog.h:232
LogicalDecodingXLogPageReadCB page_read
Definition: logical.h:44
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:102
slock_t mutex
Definition: slot.h:129

◆ DecodingContextReady()

bool DecodingContextReady ( LogicalDecodingContext ctx)

Definition at line 566 of file logical.c.

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

Referenced by DecodingContextFindStartpoint().

567 {
569 }
SnapBuildState SnapBuildCurrentState(SnapBuild *builder)
Definition: snapbuild.c:367
struct SnapBuild * snapshot_builder
Definition: logical.h:46

◆ filter_by_origin_cb_wrapper()

bool filter_by_origin_cb_wrapper ( LogicalDecodingContext ctx,
RepOriginId  origin_id 
)

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

1128 {
1130  ErrorContextCallback errcallback;
1131  bool ret;
1132 
1133  Assert(!ctx->fast_forward);
1134 
1135  /* Push callback + info on the error context stack */
1136  state.ctx = ctx;
1137  state.callback_name = "filter_by_origin";
1139  errcallback.callback = output_plugin_error_callback;
1140  errcallback.arg = (void *) &state;
1141  errcallback.previous = error_context_stack;
1142  error_context_stack = &errcallback;
1143 
1144  /* set output state */
1145  ctx->accept_writes = false;
1146 
1147  /* do the actual work: call callback */
1148  ret = ctx->callbacks.filter_by_origin_cb(ctx, origin_id);
1149 
1150  /* Pop the error context stack */
1151  error_context_stack = errcallback.previous;
1152 
1153  return ret;
1154 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:55
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:700
LogicalDecodeFilterByOriginCB filter_by_origin_cb

◆ filter_prepare_cb_wrapper()

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

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

1098 {
1100  ErrorContextCallback errcallback;
1101  bool ret;
1102 
1103  Assert(!ctx->fast_forward);
1104 
1105  /* Push callback + info on the error context stack */
1106  state.ctx = ctx;
1107  state.callback_name = "filter_prepare";
1109  errcallback.callback = output_plugin_error_callback;
1110  errcallback.arg = (void *) &state;
1111  errcallback.previous = error_context_stack;
1112  error_context_stack = &errcallback;
1113 
1114  /* set output state */
1115  ctx->accept_writes = false;
1116 
1117  /* do the actual work: call callback */
1118  ret = ctx->callbacks.filter_prepare_cb(ctx, xid, gid);
1119 
1120  /* Pop the error context stack */
1121  error_context_stack = errcallback.previous;
1122 
1123  return ret;
1124 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:55
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:700

◆ FreeDecodingContext()

void FreeDecodingContext ( LogicalDecodingContext ctx)

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

625 {
626  if (ctx->callbacks.shutdown_cb != NULL)
627  shutdown_cb_wrapper(ctx);
628 
631  XLogReaderFree(ctx->reader);
633 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
struct ReorderBuffer * reorder
Definition: logical.h:45
OutputPluginCallbacks callbacks
Definition: logical.h:55
void ReorderBufferFree(ReorderBuffer *rb)
MemoryContext context
Definition: logical.h:37
static void shutdown_cb_wrapper(LogicalDecodingContext *ctx)
Definition: logical.c:746
void XLogReaderFree(XLogReaderState *state)
Definition: xlogreader.c:142
void FreeSnapshotBuilder(SnapBuild *builder)
Definition: snapbuild.c:323
struct SnapBuild * snapshot_builder
Definition: logical.h:46
LogicalDecodeShutdownCB shutdown_cb
XLogReaderState * reader
Definition: logical.h:43

◆ LoadOutputPlugin()

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

Definition at line 678 of file logical.c.

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

Referenced by StartupDecodingContext().

679 {
680  LogicalOutputPluginInit plugin_init;
681 
682  plugin_init = (LogicalOutputPluginInit)
683  load_external_function(plugin, "_PG_output_plugin_init", false, NULL);
684 
685  if (plugin_init == NULL)
686  elog(ERROR, "output plugins have to declare the _PG_output_plugin_init symbol");
687 
688  /* ask the output plugin to fill the callback struct */
689  plugin_init(callbacks);
690 
691  if (callbacks->begin_cb == NULL)
692  elog(ERROR, "output plugins have to register a begin callback");
693  if (callbacks->change_cb == NULL)
694  elog(ERROR, "output plugins have to register a change callback");
695  if (callbacks->commit_cb == NULL)
696  elog(ERROR, "output plugins have to register a commit callback");
697 }
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 1676 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().

1677 {
1678  Assert(lsn != InvalidXLogRecPtr);
1679 
1680  /* Do an unlocked check for candidate_lsn first. */
1683  {
1684  bool updated_xmin = false;
1685  bool updated_restart = false;
1686 
1688 
1690 
1691  /* if we're past the location required for bumping xmin, do so */
1694  {
1695  /*
1696  * We have to write the changed xmin to disk *before* we change
1697  * the in-memory value, otherwise after a crash we wouldn't know
1698  * that some catalog tuples might have been removed already.
1699  *
1700  * Ensure that by first writing to ->xmin and only update
1701  * ->effective_xmin once the new state is synced to disk. After a
1702  * crash ->effective_xmin is set to ->xmin.
1703  */
1706  {
1710  updated_xmin = true;
1711  }
1712  }
1713 
1716  {
1718 
1722  updated_restart = true;
1723  }
1724 
1726 
1727  /* first write new xmin to disk, so we know what's up after a crash */
1728  if (updated_xmin || updated_restart)
1729  {
1732  elog(DEBUG1, "updated xmin: %u restart: %u", updated_xmin, updated_restart);
1733  }
1734 
1735  /*
1736  * Now the new xmin is safely on disk, we can let the global value
1737  * advance. We do not take ProcArrayLock or similar since we only
1738  * advance xmin here and there's not much harm done by a concurrent
1739  * computation missing that.
1740  */
1741  if (updated_xmin)
1742  {
1746 
1749  }
1750  }
1751  else
1752  {
1756  }
1757 }
TransactionId candidate_catalog_xmin
Definition: slot.h:171
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
void ReplicationSlotSave(void)
Definition: slot.c:734
ReplicationSlotPersistentData data
Definition: slot.h:156
XLogRecPtr confirmed_flush
Definition: slot.h:92
#define SpinLockAcquire(lock)
Definition: spin.h:62
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:841
XLogRecPtr candidate_restart_valid
Definition: slot.h:173
TransactionId catalog_xmin
Definition: slot.h:78
#define InvalidTransactionId
Definition: transam.h:31
#define SpinLockRelease(lock)
Definition: spin.h:64
TransactionId effective_catalog_xmin
Definition: slot.h:153
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:804
XLogRecPtr restart_lsn
Definition: slot.h:81
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:172
#define elog(elevel,...)
Definition: elog.h:232
slock_t mutex
Definition: slot.h:129
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:791
XLogRecPtr candidate_restart_lsn
Definition: slot.h:174
void ReplicationSlotMarkDirty(void)
Definition: slot.c:752

◆ LogicalIncreaseRestartDecodingForSlot()

void LogicalIncreaseRestartDecodingForSlot ( XLogRecPtr  current_lsn,
XLogRecPtr  restart_lsn 
)

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

1603 {
1604  bool updated_lsn = false;
1605  ReplicationSlot *slot;
1606 
1607  slot = MyReplicationSlot;
1608 
1609  Assert(slot != NULL);
1610  Assert(restart_lsn != InvalidXLogRecPtr);
1611  Assert(current_lsn != InvalidXLogRecPtr);
1612 
1613  SpinLockAcquire(&slot->mutex);
1614 
1615  /* don't overwrite if have a newer restart lsn */
1616  if (restart_lsn <= slot->data.restart_lsn)
1617  {
1618  }
1619 
1620  /*
1621  * We might have already flushed far enough to directly accept this lsn,
1622  * in this case there is no need to check for existing candidate LSNs
1623  */
1624  else if (current_lsn <= slot->data.confirmed_flush)
1625  {
1626  slot->candidate_restart_valid = current_lsn;
1627  slot->candidate_restart_lsn = restart_lsn;
1628 
1629  /* our candidate can directly be used */
1630  updated_lsn = true;
1631  }
1632 
1633  /*
1634  * Only increase if the previous values have been applied, otherwise we
1635  * might never end up updating if the receiver acks too slowly. A missed
1636  * value here will just cause some extra effort after reconnecting.
1637  */
1639  {
1640  slot->candidate_restart_valid = current_lsn;
1641  slot->candidate_restart_lsn = restart_lsn;
1642  SpinLockRelease(&slot->mutex);
1643 
1644  elog(DEBUG1, "got new restart lsn %X/%X at %X/%X",
1645  LSN_FORMAT_ARGS(restart_lsn),
1646  LSN_FORMAT_ARGS(current_lsn));
1647  }
1648  else
1649  {
1650  XLogRecPtr candidate_restart_lsn;
1651  XLogRecPtr candidate_restart_valid;
1652  XLogRecPtr confirmed_flush;
1653 
1654  candidate_restart_lsn = slot->candidate_restart_lsn;
1655  candidate_restart_valid = slot->candidate_restart_valid;
1656  confirmed_flush = slot->data.confirmed_flush;
1657  SpinLockRelease(&slot->mutex);
1658 
1659  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",
1660  LSN_FORMAT_ARGS(restart_lsn),
1661  LSN_FORMAT_ARGS(current_lsn),
1662  LSN_FORMAT_ARGS(candidate_restart_lsn),
1663  LSN_FORMAT_ARGS(candidate_restart_valid),
1664  LSN_FORMAT_ARGS(confirmed_flush));
1665  }
1666 
1667  /* candidates are already valid with the current flush position, apply */
1668  if (updated_lsn)
1670 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
ReplicationSlotPersistentData data
Definition: slot.h:156
XLogRecPtr confirmed_flush
Definition: slot.h:92
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
XLogRecPtr candidate_restart_valid
Definition: slot.h:173
#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:1676
#define elog(elevel,...)
Definition: elog.h:232
slock_t mutex
Definition: slot.h:129
XLogRecPtr candidate_restart_lsn
Definition: slot.h:174

◆ LogicalIncreaseXminForSlot()

void LogicalIncreaseXminForSlot ( XLogRecPtr  current_lsn,
TransactionId  xmin 
)

Definition at line 1545 of file logical.c.

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

Referenced by SnapBuildProcessRunningXacts().

1546 {
1547  bool updated_xmin = false;
1548  ReplicationSlot *slot;
1549 
1550  slot = MyReplicationSlot;
1551 
1552  Assert(slot != NULL);
1553 
1554  SpinLockAcquire(&slot->mutex);
1555 
1556  /*
1557  * don't overwrite if we already have a newer xmin. This can happen if we
1558  * restart decoding in a slot.
1559  */
1561  {
1562  }
1563 
1564  /*
1565  * If the client has already confirmed up to this lsn, we directly can
1566  * mark this as accepted. This can happen if we restart decoding in a
1567  * slot.
1568  */
1569  else if (current_lsn <= slot->data.confirmed_flush)
1570  {
1571  slot->candidate_catalog_xmin = xmin;
1572  slot->candidate_xmin_lsn = current_lsn;
1573 
1574  /* our candidate can directly be used */
1575  updated_xmin = true;
1576  }
1577 
1578  /*
1579  * Only increase if the previous values have been applied, otherwise we
1580  * might never end up updating if the receiver acks too slowly.
1581  */
1582  else if (slot->candidate_xmin_lsn == InvalidXLogRecPtr)
1583  {
1584  slot->candidate_catalog_xmin = xmin;
1585  slot->candidate_xmin_lsn = current_lsn;
1586  }
1587  SpinLockRelease(&slot->mutex);
1588 
1589  /* candidate already valid with the current flush position, apply */
1590  if (updated_xmin)
1592 }
TransactionId candidate_catalog_xmin
Definition: slot.h:171
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
ReplicationSlotPersistentData data
Definition: slot.h:156
XLogRecPtr confirmed_flush
Definition: slot.h:92
#define SpinLockAcquire(lock)
Definition: spin.h:62
bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:319
TransactionId catalog_xmin
Definition: slot.h:78
#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:1676
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:172
slock_t mutex
Definition: slot.h:129

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

1160 {
1161  LogicalDecodingContext *ctx = cache->private_data;
1163  ErrorContextCallback errcallback;
1164 
1165  Assert(!ctx->fast_forward);
1166 
1167  if (ctx->callbacks.message_cb == NULL)
1168  return;
1169 
1170  /* Push callback + info on the error context stack */
1171  state.ctx = ctx;
1172  state.callback_name = "message";
1173  state.report_location = message_lsn;
1174  errcallback.callback = output_plugin_error_callback;
1175  errcallback.arg = (void *) &state;
1176  errcallback.previous = error_context_stack;
1177  error_context_stack = &errcallback;
1178 
1179  /* set output state */
1180  ctx->accept_writes = true;
1181  ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
1182  ctx->write_location = message_lsn;
1183 
1184  /* do the actual work: call callback */
1185  ctx->callbacks.message_cb(ctx, txn, message_lsn, transactional, prefix,
1186  message_size, message);
1187 
1188  /* Pop the error context stack */
1189  error_context_stack = errcallback.previous;
1190 }
TransactionId write_xid
Definition: logical.h:101
void * private_data
LogicalDecodeMessageCB message_cb
OutputPluginCallbacks callbacks
Definition: logical.h:55
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:100
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:700

◆ output_plugin_error_callback()

static void output_plugin_error_callback ( void *  arg)
static

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

701 {
703 
704  /* not all callbacks have an associated LSN */
705  if (state->report_location != InvalidXLogRecPtr)
706  errcontext("slot \"%s\", output plugin \"%s\", in the %s callback, associated LSN %X/%X",
707  NameStr(state->ctx->slot->data.name),
708  NameStr(state->ctx->slot->data.plugin),
709  state->callback_name,
711  else
712  errcontext("slot \"%s\", output plugin \"%s\", in the %s callback",
713  NameStr(state->ctx->slot->data.name),
714  NameStr(state->ctx->slot->data.plugin),
715  state->callback_name);
716 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
ReplicationSlotPersistentData data
Definition: slot.h:156
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:40
#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 639 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_txn(), pgoutput_change(), pgoutput_commit_txn(), pgoutput_message(), pgoutput_stream_abort(), pgoutput_stream_commit(), pgoutput_stream_start(), pgoutput_stream_stop(), pgoutput_truncate(), and send_relation_and_attrs().

640 {
641  if (!ctx->accept_writes)
642  elog(ERROR, "writes are only accepted in commit, begin and change callbacks");
643 
644  ctx->prepare_write(ctx, ctx->write_location, ctx->write_xid, last_write);
645  ctx->prepared_write = true;
646 }
TransactionId write_xid
Definition: logical.h:101
XLogRecPtr write_location
Definition: logical.h:100
#define ERROR
Definition: elog.h:46
LogicalOutputPluginWriterPrepareWrite prepare_write
Definition: logical.h:66
#define elog(elevel,...)
Definition: elog.h:232

◆ OutputPluginUpdateProgress()

void OutputPluginUpdateProgress ( struct LogicalDecodingContext ctx)

Definition at line 665 of file logical.c.

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

Referenced by pgoutput_commit_txn(), and pgoutput_stream_commit().

666 {
667  if (!ctx->update_progress)
668  return;
669 
670  ctx->update_progress(ctx, ctx->write_location, ctx->write_xid);
671 }
TransactionId write_xid
Definition: logical.h:101
XLogRecPtr write_location
Definition: logical.h:100
LogicalOutputPluginWriterUpdateProgress update_progress
Definition: logical.h:68

◆ OutputPluginWrite()

void OutputPluginWrite ( struct LogicalDecodingContext ctx,
bool  last_write 
)

Definition at line 652 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_txn(), pgoutput_change(), pgoutput_commit_txn(), pgoutput_message(), pgoutput_stream_abort(), pgoutput_stream_commit(), pgoutput_stream_start(), pgoutput_stream_stop(), pgoutput_truncate(), and send_relation_and_attrs().

653 {
654  if (!ctx->prepared_write)
655  elog(ERROR, "OutputPluginPrepareWrite needs to be called before OutputPluginWrite");
656 
657  ctx->write(ctx, ctx->write_location, ctx->write_xid, last_write);
658  ctx->prepared_write = false;
659 }
TransactionId write_xid
Definition: logical.h:101
XLogRecPtr write_location
Definition: logical.h:100
#define ERROR
Definition: elog.h:46
LogicalOutputPluginWriterWrite write
Definition: logical.h:67
#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 888 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().

890 {
891  LogicalDecodingContext *ctx = cache->private_data;
893  ErrorContextCallback errcallback;
894 
895  Assert(!ctx->fast_forward);
896 
897  /* We're only supposed to call this when two-phase commits are supported */
898  Assert(ctx->twophase);
899 
900  /* Push callback + info on the error context stack */
901  state.ctx = ctx;
902  state.callback_name = "prepare";
903  state.report_location = txn->final_lsn; /* beginning of prepare record */
905  errcallback.arg = (void *) &state;
906  errcallback.previous = error_context_stack;
907  error_context_stack = &errcallback;
908 
909  /* set output state */
910  ctx->accept_writes = true;
911  ctx->write_xid = txn->xid;
912  ctx->write_location = txn->end_lsn; /* points to the end of the record */
913 
914  /*
915  * If the plugin supports two-phase commits then prepare callback is
916  * mandatory
917  */
918  if (ctx->callbacks.prepare_cb == NULL)
919  ereport(ERROR,
920  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
921  errmsg("logical replication at prepare time requires prepare_cb callback")));
922 
923  /* do the actual work: call callback */
924  ctx->callbacks.prepare_cb(ctx, txn, prepare_lsn);
925 
926  /* Pop the error context stack */
927  error_context_stack = errcallback.previous;
928 }
TransactionId write_xid
Definition: logical.h:101
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:55
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:100
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:700

◆ ResetLogicalStreamingState()

void ResetLogicalStreamingState ( void  )

Definition at line 1763 of file logical.c.

References bsysscan, CheckXidAlive, and InvalidTransactionId.

Referenced by AbortSubTransaction(), and AbortTransaction().

1764 {
1766  bsysscan = false;
1767 }
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 974 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().

977 {
978  LogicalDecodingContext *ctx = cache->private_data;
980  ErrorContextCallback errcallback;
981 
982  Assert(!ctx->fast_forward);
983 
984  /* We're only supposed to call this when two-phase commits are supported */
985  Assert(ctx->twophase);
986 
987  /* Push callback + info on the error context stack */
988  state.ctx = ctx;
989  state.callback_name = "rollback_prepared";
990  state.report_location = txn->final_lsn; /* beginning of commit record */
992  errcallback.arg = (void *) &state;
993  errcallback.previous = error_context_stack;
994  error_context_stack = &errcallback;
995 
996  /* set output state */
997  ctx->accept_writes = true;
998  ctx->write_xid = txn->xid;
999  ctx->write_location = txn->end_lsn; /* points to the end of the record */
1000 
1001  /*
1002  * If the plugin support two-phase commits then rollback prepared callback
1003  * is mandatory
1004  */
1005  if (ctx->callbacks.rollback_prepared_cb == NULL)
1006  ereport(ERROR,
1007  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1008  errmsg("logical replication at prepare time requires rollback_prepared_cb callback")));
1009 
1010  /* do the actual work: call callback */
1011  ctx->callbacks.rollback_prepared_cb(ctx, txn, prepare_end_lsn,
1012  prepare_time);
1013 
1014  /* Pop the error context stack */
1015  error_context_stack = errcallback.previous;
1016 }
TransactionId write_xid
Definition: logical.h:101
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:55
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:100
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:700

◆ shutdown_cb_wrapper()

static void shutdown_cb_wrapper ( LogicalDecodingContext ctx)
static

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

747 {
749  ErrorContextCallback errcallback;
750 
751  Assert(!ctx->fast_forward);
752 
753  /* Push callback + info on the error context stack */
754  state.ctx = ctx;
755  state.callback_name = "shutdown";
758  errcallback.arg = (void *) &state;
759  errcallback.previous = error_context_stack;
760  error_context_stack = &errcallback;
761 
762  /* set output state */
763  ctx->accept_writes = false;
764 
765  /* do the actual work: call callback */
766  ctx->callbacks.shutdown_cb(ctx);
767 
768  /* Pop the error context stack */
769  error_context_stack = errcallback.previous;
770 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:55
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:700

◆ startup_cb_wrapper()

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

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

720 {
722  ErrorContextCallback errcallback;
723 
724  Assert(!ctx->fast_forward);
725 
726  /* Push callback + info on the error context stack */
727  state.ctx = ctx;
728  state.callback_name = "startup";
731  errcallback.arg = (void *) &state;
732  errcallback.previous = error_context_stack;
733  error_context_stack = &errcallback;
734 
735  /* set output state */
736  ctx->accept_writes = false;
737 
738  /* do the actual work: call callback */
739  ctx->callbacks.startup_cb(ctx, opt, is_init);
740 
741  /* Pop the error context stack */
742  error_context_stack = errcallback.previous;
743 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:55
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:700

◆ StartupDecodingContext()

static LogicalDecodingContext* StartupDecodingContext ( List output_plugin_options,
XLogRecPtr  start_lsn,
TransactionId  xmin_horizon,
bool  need_full_snapshot,
bool  fast_forward,
LogicalDecodingXLogPageReadCB  page_read,
WALSegmentCleanupCB  cleanup_cb,
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, ReplicationSlotPersistentData::initial_consistent_point, IsTransactionOrTransactionBlock(), LoadOutputPlugin(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), makeStringInfo(), MemoryContextSwitchTo(), ReorderBuffer::message, message_cb_wrapper(), MyProc, MyReplicationSlot, NameStr, LogicalDecodingContext::out, LogicalDecodingContext::output_plugin_options, LogicalDecodingContext::page_read, 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(), LogicalDecodingContext::twophase, LogicalDecodingContext::update_progress, wal_segment_size, LogicalDecodingContext::write, and XLogReaderAllocate().

Referenced by CreateDecodingContext(), and CreateInitDecodingContext().

156 {
157  ReplicationSlot *slot;
158  MemoryContext context,
159  old_context;
161 
162  /* shorter lines... */
163  slot = MyReplicationSlot;
164 
166  "Logical decoding context",
168  old_context = MemoryContextSwitchTo(context);
169  ctx = palloc0(sizeof(LogicalDecodingContext));
170 
171  ctx->context = context;
172 
173  /*
174  * (re-)load output plugins, so we detect a bad (removed) output plugin
175  * now.
176  */
177  if (!fast_forward)
179 
180  /*
181  * Now that the slot's xmin has been set, we can announce ourselves as a
182  * logical decoding backend which doesn't need to be checked individually
183  * when computing the xmin horizon because the xmin is enforced via
184  * replication slots.
185  *
186  * We can only do so if we're outside of a transaction (i.e. the case when
187  * streaming changes via walsender), otherwise an already setup
188  * snapshot/xid would end up being ignored. That's not a particularly
189  * bothersome restriction since the SQL interface can't be used for
190  * streaming anyway.
191  */
193  {
194  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
197  LWLockRelease(ProcArrayLock);
198  }
199 
200  ctx->slot = slot;
201 
202  ctx->reader = XLogReaderAllocate(wal_segment_size, NULL, cleanup_cb);
203  if (!ctx->reader)
204  ereport(ERROR,
205  (errcode(ERRCODE_OUT_OF_MEMORY),
206  errmsg("out of memory")));
207  ctx->page_read = page_read;
208 
210  ctx->snapshot_builder =
211  AllocateSnapshotBuilder(ctx->reorder, xmin_horizon, start_lsn,
212  need_full_snapshot, slot->data.initial_consistent_point);
213 
214  ctx->reorder->private_data = ctx;
215 
216  /* wrap output plugin callbacks, so we can add error context information */
222 
223  /*
224  * To support streaming, we require start/stop/abort/commit/change
225  * callbacks. The message and truncate callbacks are optional, similar to
226  * regular output plugins. We however enable streaming when at least one
227  * of the methods is enabled so that we can easily identify missing
228  * methods.
229  *
230  * We decide it here, but only check it later in the wrappers.
231  */
232  ctx->streaming = (ctx->callbacks.stream_start_cb != NULL) ||
233  (ctx->callbacks.stream_stop_cb != NULL) ||
234  (ctx->callbacks.stream_abort_cb != NULL) ||
235  (ctx->callbacks.stream_commit_cb != NULL) ||
236  (ctx->callbacks.stream_change_cb != NULL) ||
237  (ctx->callbacks.stream_message_cb != NULL) ||
238  (ctx->callbacks.stream_truncate_cb != NULL);
239 
240  /*
241  * streaming callbacks
242  *
243  * stream_message and stream_truncate callbacks are optional, so we do not
244  * fail with ERROR when missing, but the wrappers simply do nothing. We
245  * must set the ReorderBuffer callbacks to something, otherwise the calls
246  * from there will crash (we don't want to move the checks there).
247  */
256 
257 
258  /*
259  * To support two-phase logical decoding, we require
260  * begin_prepare/prepare/commit-prepare/abort-prepare callbacks. The
261  * filter_prepare callback is optional. We however enable two-phase
262  * logical decoding when at least one of the methods is enabled so that we
263  * can easily identify missing methods.
264  *
265  * We decide it here, but only check it later in the wrappers.
266  */
267  ctx->twophase = (ctx->callbacks.begin_prepare_cb != NULL) ||
268  (ctx->callbacks.prepare_cb != NULL) ||
269  (ctx->callbacks.commit_prepared_cb != NULL) ||
270  (ctx->callbacks.rollback_prepared_cb != NULL) ||
271  (ctx->callbacks.stream_prepare_cb != NULL) ||
272  (ctx->callbacks.filter_prepare_cb != NULL);
273 
274  /*
275  * Callback to support decoding at prepare time.
276  */
281 
282  ctx->out = makeStringInfo();
283  ctx->prepare_write = prepare_write;
284  ctx->write = do_write;
285  ctx->update_progress = update_progress;
286 
287  ctx->output_plugin_options = output_plugin_options;
288 
289  ctx->fast_forward = fast_forward;
290 
291  MemoryContextSwitchTo(old_context);
292 
293  return ctx;
294 }
static void stream_stop_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr last_lsn)
Definition: logical.c:1239
static void stream_abort_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr abort_lsn)
Definition: logical.c:1285
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:1019
struct ReorderBuffer * reorder
Definition: logical.h:45
#define PROC_IN_LOGICAL_DECODING
Definition: proc.h:61
int wal_segment_size
Definition: xlog.c:121
XLogReaderState * XLogReaderAllocate(int wal_segment_size, const char *waldir, WALSegmentCleanupCB cleanup_cb)
Definition: xlogreader.c:82
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:808
static void stream_prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
Definition: logical.c:1324
ReorderBufferStreamAbortCB stream_abort
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
OutputPluginCallbacks callbacks
Definition: logical.h:55
LogicalDecodeStreamMessageCB stream_message_cb
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:698
bool IsTransactionOrTransactionBlock(void)
Definition: xact.c:4701
PROC_HDR * ProcGlobal
Definition: proc.c:80
MemoryContext context
Definition: logical.h:37
uint8 statusFlags
Definition: proc.h:189
LogicalDecodeStreamAbortCB stream_abort_cb
List * output_plugin_options
Definition: logical.h:61
ReorderBufferCommitCB commit
ReplicationSlotPersistentData data
Definition: slot.h:156
ReorderBufferStreamCommitCB stream_commit
static void prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
Definition: logical.c:888
static void rollback_prepared_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_end_lsn, TimestampTz prepare_time)
Definition: logical.c:974
LogicalDecodeBeginPrepareCB begin_prepare_cb
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1816
ReorderBufferStreamMessageCB stream_message
LogicalDecodePrepareCB prepare_cb
static void stream_commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:1367
#define ERROR
Definition: elog.h:46
LogicalOutputPluginWriterPrepareWrite prepare_write
Definition: logical.h:66
ReorderBufferStreamPrepareCB stream_prepare
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:195
LogicalOutputPluginWriterUpdateProgress update_progress
Definition: logical.h:68
LogicalDecodeRollbackPreparedCB rollback_prepared_cb
ReorderBufferBeginCB begin_prepare
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
ReorderBufferMessageCB message
LogicalDecodeCommitPreparedCB commit_prepared_cb
static void commit_prepared_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:931
ReorderBufferStreamChangeCB stream_change
ReorderBufferStreamTruncateCB stream_truncate
XLogRecPtr initial_consistent_point
Definition: slot.h:99
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:1452
void * palloc0(Size size)
Definition: mcxt.c:1093
static void stream_start_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr first_lsn)
Definition: logical.c:1193
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:1492
ReorderBufferStreamStartCB stream_start
#define ereport(elevel,...)
Definition: elog.h:157
static void begin_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
Definition: logical.c:778
struct SnapBuild * snapshot_builder
Definition: logical.h:46
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
ReorderBufferRollbackPreparedCB rollback_prepared
LogicalDecodeStreamCommitCB stream_commit_cb
static void begin_prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
Definition: logical.c:846
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1203
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:1056
int errmsg(const char *fmt,...)
Definition: elog.c:909
XLogReaderState * reader
Definition: logical.h:43
ReplicationSlot * slot
Definition: logical.h:40
ReorderBufferApplyTruncateCB apply_truncate
LogicalOutputPluginWriterWrite write
Definition: logical.h:67
StringInfo out
Definition: logical.h:73
int pgxactoff
Definition: proc.h:148
#define NameStr(name)
Definition: c.h:681
LogicalDecodingXLogPageReadCB page_read
Definition: logical.h:44
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:1157
LogicalDecodeStreamStopCB stream_stop_cb
uint8 * statusFlags
Definition: proc.h:333
ReorderBufferBeginCB begin
static void LoadOutputPlugin(OutputPluginCallbacks *callbacks, const char *plugin)
Definition: logical.c:678
LogicalDecodeStreamChangeCB stream_change_cb
ReorderBufferCommitPreparedCB commit_prepared
SnapBuild * AllocateSnapshotBuilder(ReorderBuffer *reorder, TransactionId xmin_horizon, XLogRecPtr start_lsn, bool need_full_snapshot, XLogRecPtr initial_consistent_point)
Definition: snapbuild.c:280
static void stream_change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
Definition: logical.c:1406
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 1285 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().

1287 {
1288  LogicalDecodingContext *ctx = cache->private_data;
1290  ErrorContextCallback errcallback;
1291 
1292  Assert(!ctx->fast_forward);
1293 
1294  /* We're only supposed to call this when streaming is supported. */
1295  Assert(ctx->streaming);
1296 
1297  /* Push callback + info on the error context stack */
1298  state.ctx = ctx;
1299  state.callback_name = "stream_abort";
1300  state.report_location = abort_lsn;
1301  errcallback.callback = output_plugin_error_callback;
1302  errcallback.arg = (void *) &state;
1303  errcallback.previous = error_context_stack;
1304  error_context_stack = &errcallback;
1305 
1306  /* set output state */
1307  ctx->accept_writes = true;
1308  ctx->write_xid = txn->xid;
1309  ctx->write_location = abort_lsn;
1310 
1311  /* in streaming mode, stream_abort_cb is required */
1312  if (ctx->callbacks.stream_abort_cb == NULL)
1313  ereport(ERROR,
1314  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1315  errmsg("logical streaming requires a stream_abort_cb callback")));
1316 
1317  ctx->callbacks.stream_abort_cb(ctx, txn, abort_lsn);
1318 
1319  /* Pop the error context stack */
1320  error_context_stack = errcallback.previous;
1321 }
TransactionId write_xid
Definition: logical.h:101
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:55
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:100
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:700

◆ stream_change_cb_wrapper()

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

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

1408 {
1409  LogicalDecodingContext *ctx = cache->private_data;
1411  ErrorContextCallback errcallback;
1412 
1413  Assert(!ctx->fast_forward);
1414 
1415  /* We're only supposed to call this when streaming is supported. */
1416  Assert(ctx->streaming);
1417 
1418  /* Push callback + info on the error context stack */
1419  state.ctx = ctx;
1420  state.callback_name = "stream_change";
1421  state.report_location = change->lsn;
1422  errcallback.callback = output_plugin_error_callback;
1423  errcallback.arg = (void *) &state;
1424  errcallback.previous = error_context_stack;
1425  error_context_stack = &errcallback;
1426 
1427  /* set output state */
1428  ctx->accept_writes = true;
1429  ctx->write_xid = txn->xid;
1430 
1431  /*
1432  * report this change's lsn so replies from clients can give an up2date
1433  * answer. This won't ever be enough (and shouldn't be!) to confirm
1434  * receipt of this transaction, but it might allow another transaction's
1435  * commit to be confirmed with one message.
1436  */
1437  ctx->write_location = change->lsn;
1438 
1439  /* in streaming mode, stream_change_cb is required */
1440  if (ctx->callbacks.stream_change_cb == NULL)
1441  ereport(ERROR,
1442  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1443  errmsg("logical streaming requires a stream_change_cb callback")));
1444 
1445  ctx->callbacks.stream_change_cb(ctx, txn, relation, change);
1446 
1447  /* Pop the error context stack */
1448  error_context_stack = errcallback.previous;
1449 }
TransactionId write_xid
Definition: logical.h:101
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:55
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:100
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:700
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 1367 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().

1369 {
1370  LogicalDecodingContext *ctx = cache->private_data;
1372  ErrorContextCallback errcallback;
1373 
1374  Assert(!ctx->fast_forward);
1375 
1376  /* We're only supposed to call this when streaming is supported. */
1377  Assert(ctx->streaming);
1378 
1379  /* Push callback + info on the error context stack */
1380  state.ctx = ctx;
1381  state.callback_name = "stream_commit";
1382  state.report_location = txn->final_lsn;
1383  errcallback.callback = output_plugin_error_callback;
1384  errcallback.arg = (void *) &state;
1385  errcallback.previous = error_context_stack;
1386  error_context_stack = &errcallback;
1387 
1388  /* set output state */
1389  ctx->accept_writes = true;
1390  ctx->write_xid = txn->xid;
1391  ctx->write_location = txn->end_lsn;
1392 
1393  /* in streaming mode, stream_commit_cb is required */
1394  if (ctx->callbacks.stream_commit_cb == NULL)
1395  ereport(ERROR,
1396  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1397  errmsg("logical streaming requires a stream_commit_cb callback")));
1398 
1399  ctx->callbacks.stream_commit_cb(ctx, txn, commit_lsn);
1400 
1401  /* Pop the error context stack */
1402  error_context_stack = errcallback.previous;
1403 }
TransactionId write_xid
Definition: logical.h:101
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:55
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:100
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:700

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

1455 {
1456  LogicalDecodingContext *ctx = cache->private_data;
1458  ErrorContextCallback errcallback;
1459 
1460  Assert(!ctx->fast_forward);
1461 
1462  /* We're only supposed to call this when streaming is supported. */
1463  Assert(ctx->streaming);
1464 
1465  /* this callback is optional */
1466  if (ctx->callbacks.stream_message_cb == NULL)
1467  return;
1468 
1469  /* Push callback + info on the error context stack */
1470  state.ctx = ctx;
1471  state.callback_name = "stream_message";
1472  state.report_location = message_lsn;
1473  errcallback.callback = output_plugin_error_callback;
1474  errcallback.arg = (void *) &state;
1475  errcallback.previous = error_context_stack;
1476  error_context_stack = &errcallback;
1477 
1478  /* set output state */
1479  ctx->accept_writes = true;
1480  ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
1481  ctx->write_location = message_lsn;
1482 
1483  /* do the actual work: call callback */
1484  ctx->callbacks.stream_message_cb(ctx, txn, message_lsn, transactional, prefix,
1485  message_size, message);
1486 
1487  /* Pop the error context stack */
1488  error_context_stack = errcallback.previous;
1489 }
TransactionId write_xid
Definition: logical.h:101
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:55
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:100
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:700

◆ stream_prepare_cb_wrapper()

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

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

1326 {
1327  LogicalDecodingContext *ctx = cache->private_data;
1329  ErrorContextCallback errcallback;
1330 
1331  Assert(!ctx->fast_forward);
1332 
1333  /*
1334  * We're only supposed to call this when streaming and two-phase commits
1335  * are supported.
1336  */
1337  Assert(ctx->streaming);
1338  Assert(ctx->twophase);
1339 
1340  /* Push callback + info on the error context stack */
1341  state.ctx = ctx;
1342  state.callback_name = "stream_prepare";
1343  state.report_location = txn->final_lsn;
1344  errcallback.callback = output_plugin_error_callback;
1345  errcallback.arg = (void *) &state;
1346  errcallback.previous = error_context_stack;
1347  error_context_stack = &errcallback;
1348 
1349  /* set output state */
1350  ctx->accept_writes = true;
1351  ctx->write_xid = txn->xid;
1352  ctx->write_location = txn->end_lsn;
1353 
1354  /* in streaming mode with two-phase commits, stream_prepare_cb is required */
1355  if (ctx->callbacks.stream_prepare_cb == NULL)
1356  ereport(ERROR,
1357  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1358  errmsg("logical streaming at prepare time requires a stream_prepare_cb callback")));
1359 
1360  ctx->callbacks.stream_prepare_cb(ctx, txn, prepare_lsn);
1361 
1362  /* Pop the error context stack */
1363  error_context_stack = errcallback.previous;
1364 }
TransactionId write_xid
Definition: logical.h:101
void * private_data
LogicalDecodeStreamPrepareCB stream_prepare_cb
OutputPluginCallbacks callbacks
Definition: logical.h:55
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:100
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:700

◆ stream_start_cb_wrapper()

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

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

1195 {
1196  LogicalDecodingContext *ctx = cache->private_data;
1198  ErrorContextCallback errcallback;
1199 
1200  Assert(!ctx->fast_forward);
1201 
1202  /* We're only supposed to call this when streaming is supported. */
1203  Assert(ctx->streaming);
1204 
1205  /* Push callback + info on the error context stack */
1206  state.ctx = ctx;
1207  state.callback_name = "stream_start";
1208  state.report_location = first_lsn;
1209  errcallback.callback = output_plugin_error_callback;
1210  errcallback.arg = (void *) &state;
1211  errcallback.previous = error_context_stack;
1212  error_context_stack = &errcallback;
1213 
1214  /* set output state */
1215  ctx->accept_writes = true;
1216  ctx->write_xid = txn->xid;
1217 
1218  /*
1219  * report this message's lsn so replies from clients can give an up2date
1220  * answer. This won't ever be enough (and shouldn't be!) to confirm
1221  * receipt of this transaction, but it might allow another transaction's
1222  * commit to be confirmed with one message.
1223  */
1224  ctx->write_location = first_lsn;
1225 
1226  /* in streaming mode, stream_start_cb is required */
1227  if (ctx->callbacks.stream_start_cb == NULL)
1228  ereport(ERROR,
1229  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1230  errmsg("logical streaming requires a stream_start_cb callback")));
1231 
1232  ctx->callbacks.stream_start_cb(ctx, txn);
1233 
1234  /* Pop the error context stack */
1235  error_context_stack = errcallback.previous;
1236 }
TransactionId write_xid
Definition: logical.h:101
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:55
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:100
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:700

◆ stream_stop_cb_wrapper()

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

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

1241 {
1242  LogicalDecodingContext *ctx = cache->private_data;
1244  ErrorContextCallback errcallback;
1245 
1246  Assert(!ctx->fast_forward);
1247 
1248  /* We're only supposed to call this when streaming is supported. */
1249  Assert(ctx->streaming);
1250 
1251  /* Push callback + info on the error context stack */
1252  state.ctx = ctx;
1253  state.callback_name = "stream_stop";
1254  state.report_location = last_lsn;
1255  errcallback.callback = output_plugin_error_callback;
1256  errcallback.arg = (void *) &state;
1257  errcallback.previous = error_context_stack;
1258  error_context_stack = &errcallback;
1259 
1260  /* set output state */
1261  ctx->accept_writes = true;
1262  ctx->write_xid = txn->xid;
1263 
1264  /*
1265  * report this message's lsn so replies from clients can give an up2date
1266  * answer. This won't ever be enough (and shouldn't be!) to confirm
1267  * receipt of this transaction, but it might allow another transaction's
1268  * commit to be confirmed with one message.
1269  */
1270  ctx->write_location = last_lsn;
1271 
1272  /* in streaming mode, stream_stop_cb is required */
1273  if (ctx->callbacks.stream_stop_cb == NULL)
1274  ereport(ERROR,
1275  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1276  errmsg("logical streaming requires a stream_stop_cb callback")));
1277 
1278  ctx->callbacks.stream_stop_cb(ctx, txn);
1279 
1280  /* Pop the error context stack */
1281  error_context_stack = errcallback.previous;
1282 }
TransactionId write_xid
Definition: logical.h:101
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:55
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:100
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:700

◆ stream_truncate_cb_wrapper()

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

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

1495 {
1496  LogicalDecodingContext *ctx = cache->private_data;
1498  ErrorContextCallback errcallback;
1499 
1500  Assert(!ctx->fast_forward);
1501 
1502  /* We're only supposed to call this when streaming is supported. */
1503  Assert(ctx->streaming);
1504 
1505  /* this callback is optional */
1506  if (!ctx->callbacks.stream_truncate_cb)
1507  return;
1508 
1509  /* Push callback + info on the error context stack */
1510  state.ctx = ctx;
1511  state.callback_name = "stream_truncate";
1512  state.report_location = change->lsn;
1513  errcallback.callback = output_plugin_error_callback;
1514  errcallback.arg = (void *) &state;
1515  errcallback.previous = error_context_stack;
1516  error_context_stack = &errcallback;
1517 
1518  /* set output state */
1519  ctx->accept_writes = true;
1520  ctx->write_xid = txn->xid;
1521 
1522  /*
1523  * report this change's lsn so replies from clients can give an up2date
1524  * answer. This won't ever be enough (and shouldn't be!) to confirm
1525  * receipt of this transaction, but it might allow another transaction's
1526  * commit to be confirmed with one message.
1527  */
1528  ctx->write_location = change->lsn;
1529 
1530  ctx->callbacks.stream_truncate_cb(ctx, txn, nrelations, relations, change);
1531 
1532  /* Pop the error context stack */
1533  error_context_stack = errcallback.previous;
1534 }
TransactionId write_xid
Definition: logical.h:101
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:55
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:100
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:700

◆ truncate_cb_wrapper()

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

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

1058 {
1059  LogicalDecodingContext *ctx = cache->private_data;
1061  ErrorContextCallback errcallback;
1062 
1063  Assert(!ctx->fast_forward);
1064 
1065  if (!ctx->callbacks.truncate_cb)
1066  return;
1067 
1068  /* Push callback + info on the error context stack */
1069  state.ctx = ctx;
1070  state.callback_name = "truncate";
1071  state.report_location = change->lsn;
1072  errcallback.callback = output_plugin_error_callback;
1073  errcallback.arg = (void *) &state;
1074  errcallback.previous = error_context_stack;
1075  error_context_stack = &errcallback;
1076 
1077  /* set output state */
1078  ctx->accept_writes = true;
1079  ctx->write_xid = txn->xid;
1080 
1081  /*
1082  * report this change's lsn so replies from clients can give an up2date
1083  * answer. This won't ever be enough (and shouldn't be!) to confirm
1084  * receipt of this transaction, but it might allow another transaction's
1085  * commit to be confirmed with one message.
1086  */
1087  ctx->write_location = change->lsn;
1088 
1089  ctx->callbacks.truncate_cb(ctx, txn, nrelations, relations, change);
1090 
1091  /* Pop the error context stack */
1092  error_context_stack = errcallback.previous;
1093 }
LogicalDecodeTruncateCB truncate_cb
TransactionId write_xid
Definition: logical.h:101
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:55
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:100
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:700

◆ UpdateDecodingStats()

void UpdateDecodingStats ( LogicalDecodingContext ctx)

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

1774 {
1775  ReorderBuffer *rb = ctx->reorder;
1776  PgStat_StatReplSlotEntry repSlotStat;
1777 
1778  /* Nothing to do if we don't have any replication stats to be sent. */
1779  if (rb->spillBytes <= 0 && rb->streamBytes <= 0 && rb->totalBytes <= 0)
1780  return;
1781 
1782  elog(DEBUG2, "UpdateDecodingStats: updating stats %p %lld %lld %lld %lld %lld %lld %lld %lld",
1783  rb,
1784  (long long) rb->spillTxns,
1785  (long long) rb->spillCount,
1786  (long long) rb->spillBytes,
1787  (long long) rb->streamTxns,
1788  (long long) rb->streamCount,
1789  (long long) rb->streamBytes,
1790  (long long) rb->totalTxns,
1791  (long long) rb->totalBytes);
1792 
1793  namestrcpy(&repSlotStat.slotname, NameStr(ctx->slot->data.name));
1794  repSlotStat.spill_txns = rb->spillTxns;
1795  repSlotStat.spill_count = rb->spillCount;
1796  repSlotStat.spill_bytes = rb->spillBytes;
1797  repSlotStat.stream_txns = rb->streamTxns;
1798  repSlotStat.stream_count = rb->streamCount;
1799  repSlotStat.stream_bytes = rb->streamBytes;
1800  repSlotStat.total_txns = rb->totalTxns;
1801  repSlotStat.total_bytes = rb->totalBytes;
1802 
1803  pgstat_report_replslot(&repSlotStat);
1804 
1805  rb->spillTxns = 0;
1806  rb->spillCount = 0;
1807  rb->spillBytes = 0;
1808  rb->streamTxns = 0;
1809  rb->streamCount = 0;
1810  rb->streamBytes = 0;
1811  rb->totalTxns = 0;
1812  rb->totalBytes = 0;
1813 }
struct ReorderBuffer * reorder
Definition: logical.h:45
PgStat_Counter spill_bytes
Definition: pgstat.h:926
void namestrcpy(Name name, const char *str)
Definition: name.c:233
ReplicationSlotPersistentData data
Definition: slot.h:156
#define DEBUG2
Definition: elog.h:24
PgStat_Counter total_bytes
Definition: pgstat.h:931
PgStat_Counter stream_bytes
Definition: pgstat.h:929
PgStat_Counter spill_txns
Definition: pgstat.h:924
void pgstat_report_replslot(const PgStat_StatReplSlotEntry *repSlotStat)
Definition: pgstat.c:1810
PgStat_Counter spill_count
Definition: pgstat.h:925
ReplicationSlot * slot
Definition: logical.h:40
PgStat_Counter stream_count
Definition: pgstat.h:928
#define elog(elevel,...)
Definition: elog.h:232
#define NameStr(name)
Definition: c.h:681
PgStat_Counter total_txns
Definition: pgstat.h:930
PgStat_Counter stream_txns
Definition: pgstat.h:927