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

Go to the source code of this file.

Data Structures

struct  LogicalErrorCallbackState
 

Typedefs

typedef struct LogicalErrorCallbackState LogicalErrorCallbackState
 

Functions

static void output_plugin_error_callback (void *arg)
 
static void startup_cb_wrapper (LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
 
static void shutdown_cb_wrapper (LogicalDecodingContext *ctx)
 
static void begin_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn)
 
static void commit_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
 
static void begin_prepare_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn)
 
static void prepare_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
 
static void commit_prepared_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
 
static void rollback_prepared_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_end_lsn, TimestampTz prepare_time)
 
static void change_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
 
static void truncate_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
 
static void message_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr message_lsn, bool transactional, const char *prefix, Size message_size, const char *message)
 
static void stream_start_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr first_lsn)
 
static void stream_stop_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr last_lsn)
 
static void stream_abort_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr abort_lsn)
 
static void stream_prepare_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
 
static void stream_commit_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
 
static void stream_change_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
 
static void stream_message_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr message_lsn, bool transactional, const char *prefix, Size message_size, const char *message)
 
static void stream_truncate_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
 
static void LoadOutputPlugin (OutputPluginCallbacks *callbacks, const char *plugin)
 
void CheckLogicalDecodingRequirements (void)
 
static LogicalDecodingContextStartupDecodingContext (List *output_plugin_options, XLogRecPtr start_lsn, TransactionId xmin_horizon, bool need_full_snapshot, bool fast_forward, XLogReaderRoutine *xl_routine, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
 
LogicalDecodingContextCreateInitDecodingContext (const char *plugin, List *output_plugin_options, bool need_full_snapshot, XLogRecPtr restart_lsn, XLogReaderRoutine *xl_routine, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
 
LogicalDecodingContextCreateDecodingContext (XLogRecPtr start_lsn, List *output_plugin_options, bool fast_forward, XLogReaderRoutine *xl_routine, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
 
bool DecodingContextReady (LogicalDecodingContext *ctx)
 
void DecodingContextFindStartpoint (LogicalDecodingContext *ctx)
 
void FreeDecodingContext (LogicalDecodingContext *ctx)
 
void OutputPluginPrepareWrite (struct LogicalDecodingContext *ctx, bool last_write)
 
void OutputPluginWrite (struct LogicalDecodingContext *ctx, bool last_write)
 
void OutputPluginUpdateProgress (struct LogicalDecodingContext *ctx)
 
bool filter_prepare_cb_wrapper (LogicalDecodingContext *ctx, 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 755 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().

756 {
757  LogicalDecodingContext *ctx = cache->private_data;
759  ErrorContextCallback errcallback;
760 
761  Assert(!ctx->fast_forward);
762 
763  /* Push callback + info on the error context stack */
764  state.ctx = ctx;
765  state.callback_name = "begin";
766  state.report_location = txn->first_lsn;
768  errcallback.arg = (void *) &state;
769  errcallback.previous = error_context_stack;
770  error_context_stack = &errcallback;
771 
772  /* set output state */
773  ctx->accept_writes = true;
774  ctx->write_xid = txn->xid;
775  ctx->write_location = txn->first_lsn;
776 
777  /* do the actual work: call callback */
778  ctx->callbacks.begin_cb(ctx, txn);
779 
780  /* Pop the error context stack */
781  error_context_stack = errcallback.previous;
782 }
XLogRecPtr first_lsn
TransactionId write_xid
Definition: logical.h:98
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:52
void(* callback)(void *arg)
Definition: elog.h:243
struct ErrorContextCallback * previous
Definition: elog.h:242
const char * callback_name
Definition: logical.c:50
XLogRecPtr write_location
Definition: logical.h:97
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:677

◆ begin_prepare_cb_wrapper()

static void begin_prepare_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn 
)
static

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

824 {
825  LogicalDecodingContext *ctx = cache->private_data;
827  ErrorContextCallback errcallback;
828 
829  Assert(!ctx->fast_forward);
830 
831  /* We're only supposed to call this when two-phase commits are supported */
832  Assert(ctx->twophase);
833 
834  /* Push callback + info on the error context stack */
835  state.ctx = ctx;
836  state.callback_name = "begin_prepare";
837  state.report_location = txn->first_lsn;
839  errcallback.arg = (void *) &state;
840  errcallback.previous = error_context_stack;
841  error_context_stack = &errcallback;
842 
843  /* set output state */
844  ctx->accept_writes = true;
845  ctx->write_xid = txn->xid;
846  ctx->write_location = txn->first_lsn;
847 
848  /*
849  * If the plugin supports two-phase commits then begin prepare callback is
850  * mandatory
851  */
852  if (ctx->callbacks.begin_prepare_cb == NULL)
853  ereport(ERROR,
854  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
855  errmsg("logical replication at prepare time requires begin_prepare_cb callback")));
856 
857  /* do the actual work: call callback */
858  ctx->callbacks.begin_prepare_cb(ctx, txn);
859 
860  /* Pop the error context stack */
861  error_context_stack = errcallback.previous;
862 }
XLogRecPtr first_lsn
TransactionId write_xid
Definition: logical.h:98
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:52
int errcode(int sqlerrcode)
Definition: elog.c:704
void(* callback)(void *arg)
Definition: elog.h:243
struct ErrorContextCallback * previous
Definition: elog.h:242
const char * callback_name
Definition: logical.c:50
XLogRecPtr write_location
Definition: logical.h:97
LogicalDecodeBeginPrepareCB begin_prepare_cb
ErrorContextCallback * error_context_stack
Definition: elog.c:93
#define ERROR
Definition: elog.h:45
TransactionId xid
#define ereport(elevel,...)
Definition: elog.h:155
#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:915
LogicalDecodingContext * ctx
Definition: logical.c:49
static void output_plugin_error_callback(void *arg)
Definition: logical.c:677

◆ change_cb_wrapper()

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

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

998 {
999  LogicalDecodingContext *ctx = cache->private_data;
1001  ErrorContextCallback errcallback;
1002 
1003  Assert(!ctx->fast_forward);
1004 
1005  /* Push callback + info on the error context stack */
1006  state.ctx = ctx;
1007  state.callback_name = "change";
1008  state.report_location = change->lsn;
1009  errcallback.callback = output_plugin_error_callback;
1010  errcallback.arg = (void *) &state;
1011  errcallback.previous = error_context_stack;
1012  error_context_stack = &errcallback;
1013 
1014  /* set output state */
1015  ctx->accept_writes = true;
1016  ctx->write_xid = txn->xid;
1017 
1018  /*
1019  * report this change's lsn so replies from clients can give an up2date
1020  * answer. This won't ever be enough (and shouldn't be!) to confirm
1021  * receipt of this transaction, but it might allow another transaction's
1022  * commit to be confirmed with one message.
1023  */
1024  ctx->write_location = change->lsn;
1025 
1026  ctx->callbacks.change_cb(ctx, txn, relation, change);
1027 
1028  /* Pop the error context stack */
1029  error_context_stack = errcallback.previous;
1030 }
TransactionId write_xid
Definition: logical.h:98
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:52
void(* callback)(void *arg)
Definition: elog.h:243
struct ErrorContextCallback * previous
Definition: elog.h:242
const char * callback_name
Definition: logical.c:50
XLogRecPtr write_location
Definition: logical.h:97
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:677

◆ 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:1053
int wal_level
Definition: xlog.c:108
int errcode(int sqlerrcode)
Definition: elog.c:704
bool RecoveryInProgress(void)
Definition: xlog.c:8132
#define ERROR
Definition: elog.h:45
Oid MyDatabaseId
Definition: globals.c:86
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:155
int errmsg(const char *fmt,...)
Definition: elog.c:915

◆ commit_cb_wrapper()

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

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

787 {
788  LogicalDecodingContext *ctx = cache->private_data;
790  ErrorContextCallback errcallback;
791 
792  Assert(!ctx->fast_forward);
793 
794  /* Push callback + info on the error context stack */
795  state.ctx = ctx;
796  state.callback_name = "commit";
797  state.report_location = txn->final_lsn; /* beginning of commit record */
799  errcallback.arg = (void *) &state;
800  errcallback.previous = error_context_stack;
801  error_context_stack = &errcallback;
802 
803  /* set output state */
804  ctx->accept_writes = true;
805  ctx->write_xid = txn->xid;
806  ctx->write_location = txn->end_lsn; /* points to the end of the record */
807 
808  /* do the actual work: call callback */
809  ctx->callbacks.commit_cb(ctx, txn, commit_lsn);
810 
811  /* Pop the error context stack */
812  error_context_stack = errcallback.previous;
813 }
TransactionId write_xid
Definition: logical.h:98
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:52
void(* callback)(void *arg)
Definition: elog.h:243
struct ErrorContextCallback * previous
Definition: elog.h:242
const char * callback_name
Definition: logical.c:50
XLogRecPtr write_location
Definition: logical.h:97
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:677

◆ commit_prepared_cb_wrapper()

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

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

910 {
911  LogicalDecodingContext *ctx = cache->private_data;
913  ErrorContextCallback errcallback;
914 
915  Assert(!ctx->fast_forward);
916 
917  /* We're only supposed to call this when two-phase commits are supported */
918  Assert(ctx->twophase);
919 
920  /* Push callback + info on the error context stack */
921  state.ctx = ctx;
922  state.callback_name = "commit_prepared";
923  state.report_location = txn->final_lsn; /* beginning of commit record */
925  errcallback.arg = (void *) &state;
926  errcallback.previous = error_context_stack;
927  error_context_stack = &errcallback;
928 
929  /* set output state */
930  ctx->accept_writes = true;
931  ctx->write_xid = txn->xid;
932  ctx->write_location = txn->end_lsn; /* points to the end of the record */
933 
934  /*
935  * If the plugin support two-phase commits then commit prepared callback
936  * is mandatory
937  */
938  if (ctx->callbacks.commit_prepared_cb == NULL)
939  ereport(ERROR,
940  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
941  errmsg("logical replication at prepare time requires commit_prepared_cb callback")));
942 
943  /* do the actual work: call callback */
944  ctx->callbacks.commit_prepared_cb(ctx, txn, commit_lsn);
945 
946  /* Pop the error context stack */
947  error_context_stack = errcallback.previous;
948 }
TransactionId write_xid
Definition: logical.h:98
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:52
int errcode(int sqlerrcode)
Definition: elog.c:704
void(* callback)(void *arg)
Definition: elog.h:243
struct ErrorContextCallback * previous
Definition: elog.h:242
const char * callback_name
Definition: logical.c:50
XLogRecPtr write_location
Definition: logical.h:97
ErrorContextCallback * error_context_stack
Definition: elog.c:93
#define ERROR
Definition: elog.h:45
XLogRecPtr final_lsn
LogicalDecodeCommitPreparedCB commit_prepared_cb
TransactionId xid
#define ereport(elevel,...)
Definition: elog.h:155
#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:915
LogicalDecodingContext * ctx
Definition: logical.c:49
static void output_plugin_error_callback(void *arg)
Definition: logical.c:677

◆ CreateDecodingContext()

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

Definition at line 470 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(), and StartupDecodingContext().

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

477 {
479  ReplicationSlot *slot;
480  MemoryContext old_context;
481 
482  /* shorter lines... */
483  slot = MyReplicationSlot;
484 
485  /* first some sanity checks that are unlikely to be violated */
486  if (slot == NULL)
487  elog(ERROR, "cannot perform logical decoding without an acquired slot");
488 
489  /* make sure the passed slot is suitable, these are user facing errors */
490  if (SlotIsPhysical(slot))
491  ereport(ERROR,
492  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
493  errmsg("cannot use physical replication slot for logical decoding")));
494 
495  if (slot->data.database != MyDatabaseId)
496  ereport(ERROR,
497  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
498  errmsg("replication slot \"%s\" was not created in this database",
499  NameStr(slot->data.name))));
500 
501  if (start_lsn == InvalidXLogRecPtr)
502  {
503  /* continue from last position */
504  start_lsn = slot->data.confirmed_flush;
505  }
506  else if (start_lsn < slot->data.confirmed_flush)
507  {
508  /*
509  * It might seem like we should error out in this case, but it's
510  * pretty common for a client to acknowledge a LSN it doesn't have to
511  * do anything for, and thus didn't store persistently, because the
512  * xlog records didn't result in anything relevant for logical
513  * decoding. Clients have to be able to do that to support synchronous
514  * replication.
515  */
516  elog(DEBUG1, "cannot stream from %X/%X, minimum is %X/%X, forwarding",
517  LSN_FORMAT_ARGS(start_lsn),
519 
520  start_lsn = slot->data.confirmed_flush;
521  }
522 
523  ctx = StartupDecodingContext(output_plugin_options,
524  start_lsn, InvalidTransactionId, false,
525  fast_forward, xl_routine, prepare_write,
526  do_write, update_progress);
527 
528  /* call output plugin initialization callback */
529  old_context = MemoryContextSwitchTo(ctx->context);
530  if (ctx->callbacks.startup_cb != NULL)
531  startup_cb_wrapper(ctx, &ctx->options, false);
532  MemoryContextSwitchTo(old_context);
533 
535 
536  ereport(LOG,
537  (errmsg("starting logical decoding for slot \"%s\"",
538  NameStr(slot->data.name)),
539  errdetail("Streaming transactions committing after %X/%X, reading WAL from %X/%X.",
541  LSN_FORMAT_ARGS(slot->data.restart_lsn))));
542 
543  return ctx;
544 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
struct ReorderBuffer * reorder
Definition: logical.h:42
OutputPluginOptions options
Definition: logical.h:53
OutputPluginCallbacks callbacks
Definition: logical.h:52
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:704
MemoryContext context
Definition: logical.h:35
ReplicationSlotPersistentData data
Definition: slot.h:144
#define LOG
Definition: elog.h:26
#define SlotIsPhysical(slot)
Definition: slot.h:165
XLogRecPtr confirmed_flush
Definition: slot.h:92
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
#define ERROR
Definition: elog.h:45
static void startup_cb_wrapper(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
Definition: logical.c:696
int errdetail(const char *fmt,...)
Definition: elog.c:1048
#define InvalidTransactionId
Definition: transam.h:31
Oid MyDatabaseId
Definition: globals.c:86
#define ereport(elevel,...)
Definition: elog.h:155
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
XLogRecPtr restart_lsn
Definition: slot.h:81
LogicalDecodeStartupCB startup_cb
int errmsg(const char *fmt,...)
Definition: elog.c:915
#define elog(elevel,...)
Definition: elog.h:228
#define NameStr(name)
Definition: c.h:681
static LogicalDecodingContext * StartupDecodingContext(List *output_plugin_options, XLogRecPtr start_lsn, TransactionId xmin_horizon, bool need_full_snapshot, bool fast_forward, XLogReaderRoutine *xl_routine, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
Definition: logical.c:146

◆ CreateInitDecodingContext()

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

Definition at line 318 of file logical.c.

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

Referenced by create_logical_replication_slot(), and CreateReplicationSlot().

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

◆ DecodingContextFindStartpoint()

void DecodingContextFindStartpoint ( LogicalDecodingContext ctx)

Definition at line 559 of file logical.c.

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

Referenced by create_logical_replication_slot(), and CreateReplicationSlot().

560 {
561  ReplicationSlot *slot = ctx->slot;
562 
563  /* Initialize from where to start reading WAL. */
564  XLogBeginRead(ctx->reader, slot->data.restart_lsn);
565 
566  elog(DEBUG1, "searching for logical decoding starting point, starting at %X/%X",
568 
569  /* Wait for a consistent starting point */
570  for (;;)
571  {
572  XLogRecord *record;
573  char *err = NULL;
574 
575  /* the read_page callback waits for new WAL */
576  record = XLogReadRecord(ctx->reader, &err);
577  if (err)
578  elog(ERROR, "%s", err);
579  if (!record)
580  elog(ERROR, "no record found"); /* shouldn't happen */
581 
583 
584  /* only continue till we found a consistent spot */
585  if (DecodingContextReady(ctx))
586  break;
587 
589  }
590 
591  SpinLockAcquire(&slot->mutex);
592  slot->data.confirmed_flush = ctx->reader->EndRecPtr;
593  SpinLockRelease(&slot->mutex);
594 }
#define DEBUG1
Definition: elog.h:25
bool DecodingContextReady(LogicalDecodingContext *ctx)
Definition: logical.c:550
ReplicationSlotPersistentData data
Definition: slot.h:144
XLogRecPtr confirmed_flush
Definition: slot.h:92
XLogRecPtr EndRecPtr
Definition: xlogreader.h:176
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
XLogRecord * XLogReadRecord(XLogReaderState *state, char **errormsg)
Definition: xlogreader.c:268
#define ERROR
Definition: elog.h:45
void LogicalDecodingProcessRecord(LogicalDecodingContext *ctx, XLogReaderState *record)
Definition: decode.c:105
void XLogBeginRead(XLogReaderState *state, XLogRecPtr RecPtr)
Definition: xlogreader.c:240
#define SpinLockRelease(lock)
Definition: spin.h:64
XLogRecPtr restart_lsn
Definition: slot.h:81
XLogReaderState * reader
Definition: logical.h:41
ReplicationSlot * slot
Definition: logical.h:38
#define elog(elevel,...)
Definition: elog.h:228
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:100
slock_t mutex
Definition: slot.h:117

◆ DecodingContextReady()

bool DecodingContextReady ( LogicalDecodingContext ctx)

Definition at line 550 of file logical.c.

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

Referenced by DecodingContextFindStartpoint().

551 {
553 }
SnapBuildState SnapBuildCurrentState(SnapBuild *builder)
Definition: snapbuild.c:354
struct SnapBuild * snapshot_builder
Definition: logical.h:43

◆ filter_by_origin_cb_wrapper()

bool filter_by_origin_cb_wrapper ( LogicalDecodingContext ctx,
RepOriginId  origin_id 
)

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

1104 {
1106  ErrorContextCallback errcallback;
1107  bool ret;
1108 
1109  Assert(!ctx->fast_forward);
1110 
1111  /* Push callback + info on the error context stack */
1112  state.ctx = ctx;
1113  state.callback_name = "filter_by_origin";
1115  errcallback.callback = output_plugin_error_callback;
1116  errcallback.arg = (void *) &state;
1117  errcallback.previous = error_context_stack;
1118  error_context_stack = &errcallback;
1119 
1120  /* set output state */
1121  ctx->accept_writes = false;
1122 
1123  /* do the actual work: call callback */
1124  ret = ctx->callbacks.filter_by_origin_cb(ctx, origin_id);
1125 
1126  /* Pop the error context stack */
1127  error_context_stack = errcallback.previous;
1128 
1129  return ret;
1130 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:52
void(* callback)(void *arg)
Definition: elog.h:243
struct ErrorContextCallback * previous
Definition: elog.h:242
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:677
LogicalDecodeFilterByOriginCB filter_by_origin_cb

◆ filter_prepare_cb_wrapper()

bool filter_prepare_cb_wrapper ( LogicalDecodingContext ctx,
const char *  gid 
)

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

1074 {
1076  ErrorContextCallback errcallback;
1077  bool ret;
1078 
1079  Assert(!ctx->fast_forward);
1080 
1081  /* Push callback + info on the error context stack */
1082  state.ctx = ctx;
1083  state.callback_name = "filter_prepare";
1085  errcallback.callback = output_plugin_error_callback;
1086  errcallback.arg = (void *) &state;
1087  errcallback.previous = error_context_stack;
1088  error_context_stack = &errcallback;
1089 
1090  /* set output state */
1091  ctx->accept_writes = false;
1092 
1093  /* do the actual work: call callback */
1094  ret = ctx->callbacks.filter_prepare_cb(ctx, gid);
1095 
1096  /* Pop the error context stack */
1097  error_context_stack = errcallback.previous;
1098 
1099  return ret;
1100 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:52
void(* callback)(void *arg)
Definition: elog.h:243
struct ErrorContextCallback * previous
Definition: elog.h:242
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:677

◆ FreeDecodingContext()

void FreeDecodingContext ( LogicalDecodingContext ctx)

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

602 {
603  if (ctx->callbacks.shutdown_cb != NULL)
604  shutdown_cb_wrapper(ctx);
605 
608  XLogReaderFree(ctx->reader);
610 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:212
struct ReorderBuffer * reorder
Definition: logical.h:42
OutputPluginCallbacks callbacks
Definition: logical.h:52
void ReorderBufferFree(ReorderBuffer *rb)
MemoryContext context
Definition: logical.h:35
static void shutdown_cb_wrapper(LogicalDecodingContext *ctx)
Definition: logical.c:723
void XLogReaderFree(XLogReaderState *state)
Definition: xlogreader.c:139
void FreeSnapshotBuilder(SnapBuild *builder)
Definition: snapbuild.c:310
struct SnapBuild * snapshot_builder
Definition: logical.h:43
LogicalDecodeShutdownCB shutdown_cb
XLogReaderState * reader
Definition: logical.h:41

◆ LoadOutputPlugin()

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

Definition at line 655 of file logical.c.

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

Referenced by StartupDecodingContext().

656 {
657  LogicalOutputPluginInit plugin_init;
658 
659  plugin_init = (LogicalOutputPluginInit)
660  load_external_function(plugin, "_PG_output_plugin_init", false, NULL);
661 
662  if (plugin_init == NULL)
663  elog(ERROR, "output plugins have to declare the _PG_output_plugin_init symbol");
664 
665  /* ask the output plugin to fill the callback struct */
666  plugin_init(callbacks);
667 
668  if (callbacks->begin_cb == NULL)
669  elog(ERROR, "output plugins have to register a begin callback");
670  if (callbacks->change_cb == NULL)
671  elog(ERROR, "output plugins have to register a change callback");
672  if (callbacks->commit_cb == NULL)
673  elog(ERROR, "output plugins have to register a commit callback");
674 }
static const char * plugin
#define ERROR
Definition: elog.h:45
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:228
LogicalDecodeBeginCB begin_cb

◆ LogicalConfirmReceivedLocation()

void LogicalConfirmReceivedLocation ( XLogRecPtr  lsn)

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

1653 {
1654  Assert(lsn != InvalidXLogRecPtr);
1655 
1656  /* Do an unlocked check for candidate_lsn first. */
1659  {
1660  bool updated_xmin = false;
1661  bool updated_restart = false;
1662 
1664 
1666 
1667  /* if we're past the location required for bumping xmin, do so */
1670  {
1671  /*
1672  * We have to write the changed xmin to disk *before* we change
1673  * the in-memory value, otherwise after a crash we wouldn't know
1674  * that some catalog tuples might have been removed already.
1675  *
1676  * Ensure that by first writing to ->xmin and only update
1677  * ->effective_xmin once the new state is synced to disk. After a
1678  * crash ->effective_xmin is set to ->xmin.
1679  */
1682  {
1686  updated_xmin = true;
1687  }
1688  }
1689 
1692  {
1694 
1698  updated_restart = true;
1699  }
1700 
1702 
1703  /* first write new xmin to disk, so we know what's up after a crash */
1704  if (updated_xmin || updated_restart)
1705  {
1708  elog(DEBUG1, "updated xmin: %u restart: %u", updated_xmin, updated_restart);
1709  }
1710 
1711  /*
1712  * Now the new xmin is safely on disk, we can let the global value
1713  * advance. We do not take ProcArrayLock or similar since we only
1714  * advance xmin here and there's not much harm done by a concurrent
1715  * computation missing that.
1716  */
1717  if (updated_xmin)
1718  {
1722 
1725  }
1726  }
1727  else
1728  {
1732  }
1733 }
TransactionId candidate_catalog_xmin
Definition: slot.h:159
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
void ReplicationSlotSave(void)
Definition: slot.c:719
ReplicationSlotPersistentData data
Definition: slot.h:144
XLogRecPtr confirmed_flush
Definition: slot.h:92
#define SpinLockAcquire(lock)
Definition: spin.h:62
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:826
XLogRecPtr candidate_restart_valid
Definition: slot.h:161
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:141
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:160
#define elog(elevel,...)
Definition: elog.h:228
slock_t mutex
Definition: slot.h:117
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:776
XLogRecPtr candidate_restart_lsn
Definition: slot.h:162
void ReplicationSlotMarkDirty(void)
Definition: slot.c:737

◆ LogicalIncreaseRestartDecodingForSlot()

void LogicalIncreaseRestartDecodingForSlot ( XLogRecPtr  current_lsn,
XLogRecPtr  restart_lsn 
)

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

1579 {
1580  bool updated_lsn = false;
1581  ReplicationSlot *slot;
1582 
1583  slot = MyReplicationSlot;
1584 
1585  Assert(slot != NULL);
1586  Assert(restart_lsn != InvalidXLogRecPtr);
1587  Assert(current_lsn != InvalidXLogRecPtr);
1588 
1589  SpinLockAcquire(&slot->mutex);
1590 
1591  /* don't overwrite if have a newer restart lsn */
1592  if (restart_lsn <= slot->data.restart_lsn)
1593  {
1594  }
1595 
1596  /*
1597  * We might have already flushed far enough to directly accept this lsn,
1598  * in this case there is no need to check for existing candidate LSNs
1599  */
1600  else if (current_lsn <= slot->data.confirmed_flush)
1601  {
1602  slot->candidate_restart_valid = current_lsn;
1603  slot->candidate_restart_lsn = restart_lsn;
1604 
1605  /* our candidate can directly be used */
1606  updated_lsn = true;
1607  }
1608 
1609  /*
1610  * Only increase if the previous values have been applied, otherwise we
1611  * might never end up updating if the receiver acks too slowly. A missed
1612  * value here will just cause some extra effort after reconnecting.
1613  */
1615  {
1616  slot->candidate_restart_valid = current_lsn;
1617  slot->candidate_restart_lsn = restart_lsn;
1618  SpinLockRelease(&slot->mutex);
1619 
1620  elog(DEBUG1, "got new restart lsn %X/%X at %X/%X",
1621  LSN_FORMAT_ARGS(restart_lsn),
1622  LSN_FORMAT_ARGS(current_lsn));
1623  }
1624  else
1625  {
1626  XLogRecPtr candidate_restart_lsn;
1627  XLogRecPtr candidate_restart_valid;
1628  XLogRecPtr confirmed_flush;
1629 
1630  candidate_restart_lsn = slot->candidate_restart_lsn;
1631  candidate_restart_valid = slot->candidate_restart_valid;
1632  confirmed_flush = slot->data.confirmed_flush;
1633  SpinLockRelease(&slot->mutex);
1634 
1635  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",
1636  LSN_FORMAT_ARGS(restart_lsn),
1637  LSN_FORMAT_ARGS(current_lsn),
1638  LSN_FORMAT_ARGS(candidate_restart_lsn),
1639  LSN_FORMAT_ARGS(candidate_restart_valid),
1640  LSN_FORMAT_ARGS(confirmed_flush));
1641  }
1642 
1643  /* candidates are already valid with the current flush position, apply */
1644  if (updated_lsn)
1646 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
ReplicationSlotPersistentData data
Definition: slot.h:144
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:161
#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:1652
#define elog(elevel,...)
Definition: elog.h:228
slock_t mutex
Definition: slot.h:117
XLogRecPtr candidate_restart_lsn
Definition: slot.h:162

◆ LogicalIncreaseXminForSlot()

void LogicalIncreaseXminForSlot ( XLogRecPtr  current_lsn,
TransactionId  xmin 
)

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

1522 {
1523  bool updated_xmin = false;
1524  ReplicationSlot *slot;
1525 
1526  slot = MyReplicationSlot;
1527 
1528  Assert(slot != NULL);
1529 
1530  SpinLockAcquire(&slot->mutex);
1531 
1532  /*
1533  * don't overwrite if we already have a newer xmin. This can happen if we
1534  * restart decoding in a slot.
1535  */
1537  {
1538  }
1539 
1540  /*
1541  * If the client has already confirmed up to this lsn, we directly can
1542  * mark this as accepted. This can happen if we restart decoding in a
1543  * slot.
1544  */
1545  else if (current_lsn <= slot->data.confirmed_flush)
1546  {
1547  slot->candidate_catalog_xmin = xmin;
1548  slot->candidate_xmin_lsn = current_lsn;
1549 
1550  /* our candidate can directly be used */
1551  updated_xmin = true;
1552  }
1553 
1554  /*
1555  * Only increase if the previous values have been applied, otherwise we
1556  * might never end up updating if the receiver acks too slowly.
1557  */
1558  else if (slot->candidate_xmin_lsn == InvalidXLogRecPtr)
1559  {
1560  slot->candidate_catalog_xmin = xmin;
1561  slot->candidate_xmin_lsn = current_lsn;
1562  }
1563  SpinLockRelease(&slot->mutex);
1564 
1565  /* candidate already valid with the current flush position, apply */
1566  if (updated_xmin)
1568 }
TransactionId candidate_catalog_xmin
Definition: slot.h:159
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
ReplicationSlotPersistentData data
Definition: slot.h:144
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:1652
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:160
slock_t mutex
Definition: slot.h:117

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

1136 {
1137  LogicalDecodingContext *ctx = cache->private_data;
1139  ErrorContextCallback errcallback;
1140 
1141  Assert(!ctx->fast_forward);
1142 
1143  if (ctx->callbacks.message_cb == NULL)
1144  return;
1145 
1146  /* Push callback + info on the error context stack */
1147  state.ctx = ctx;
1148  state.callback_name = "message";
1149  state.report_location = message_lsn;
1150  errcallback.callback = output_plugin_error_callback;
1151  errcallback.arg = (void *) &state;
1152  errcallback.previous = error_context_stack;
1153  error_context_stack = &errcallback;
1154 
1155  /* set output state */
1156  ctx->accept_writes = true;
1157  ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
1158  ctx->write_location = message_lsn;
1159 
1160  /* do the actual work: call callback */
1161  ctx->callbacks.message_cb(ctx, txn, message_lsn, transactional, prefix,
1162  message_size, message);
1163 
1164  /* Pop the error context stack */
1165  error_context_stack = errcallback.previous;
1166 }
TransactionId write_xid
Definition: logical.h:98
void * private_data
LogicalDecodeMessageCB message_cb
OutputPluginCallbacks callbacks
Definition: logical.h:52
void(* callback)(void *arg)
Definition: elog.h:243
struct ErrorContextCallback * previous
Definition: elog.h:242
const char * callback_name
Definition: logical.c:50
XLogRecPtr write_location
Definition: logical.h:97
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:677

◆ output_plugin_error_callback()

static void output_plugin_error_callback ( void *  arg)
static

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

678 {
680 
681  /* not all callbacks have an associated LSN */
682  if (state->report_location != InvalidXLogRecPtr)
683  errcontext("slot \"%s\", output plugin \"%s\", in the %s callback, associated LSN %X/%X",
684  NameStr(state->ctx->slot->data.name),
685  NameStr(state->ctx->slot->data.plugin),
686  state->callback_name,
688  else
689  errcontext("slot \"%s\", output plugin \"%s\", in the %s callback",
690  NameStr(state->ctx->slot->data.name),
691  NameStr(state->ctx->slot->data.plugin),
692  state->callback_name);
693 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
ReplicationSlotPersistentData data
Definition: slot.h:144
const char * callback_name
Definition: logical.c:50
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
Definition: regguts.h:317
XLogRecPtr report_location
Definition: logical.c:51
ReplicationSlot * slot
Definition: logical.h:38
#define NameStr(name)
Definition: c.h:681
#define errcontext
Definition: elog.h:199
void * arg
LogicalDecodingContext * ctx
Definition: logical.c:49

◆ OutputPluginPrepareWrite()

void OutputPluginPrepareWrite ( struct LogicalDecodingContext ctx,
bool  last_write 
)

Definition at line 616 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_stream_abort(), pgoutput_stream_commit(), pgoutput_stream_start(), pgoutput_stream_stop(), pgoutput_truncate(), and send_relation_and_attrs().

617 {
618  if (!ctx->accept_writes)
619  elog(ERROR, "writes are only accepted in commit, begin and change callbacks");
620 
621  ctx->prepare_write(ctx, ctx->write_location, ctx->write_xid, last_write);
622  ctx->prepared_write = true;
623 }
TransactionId write_xid
Definition: logical.h:98
XLogRecPtr write_location
Definition: logical.h:97
#define ERROR
Definition: elog.h:45
LogicalOutputPluginWriterPrepareWrite prepare_write
Definition: logical.h:63
#define elog(elevel,...)
Definition: elog.h:228

◆ OutputPluginUpdateProgress()

void OutputPluginUpdateProgress ( struct LogicalDecodingContext ctx)

Definition at line 642 of file logical.c.

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

Referenced by pgoutput_commit_txn(), and pgoutput_stream_commit().

643 {
644  if (!ctx->update_progress)
645  return;
646 
647  ctx->update_progress(ctx, ctx->write_location, ctx->write_xid);
648 }
TransactionId write_xid
Definition: logical.h:98
XLogRecPtr write_location
Definition: logical.h:97
LogicalOutputPluginWriterUpdateProgress update_progress
Definition: logical.h:65

◆ OutputPluginWrite()

void OutputPluginWrite ( struct LogicalDecodingContext ctx,
bool  last_write 
)

Definition at line 629 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_stream_abort(), pgoutput_stream_commit(), pgoutput_stream_start(), pgoutput_stream_stop(), pgoutput_truncate(), and send_relation_and_attrs().

630 {
631  if (!ctx->prepared_write)
632  elog(ERROR, "OutputPluginPrepareWrite needs to be called before OutputPluginWrite");
633 
634  ctx->write(ctx, ctx->write_location, ctx->write_xid, last_write);
635  ctx->prepared_write = false;
636 }
TransactionId write_xid
Definition: logical.h:98
XLogRecPtr write_location
Definition: logical.h:97
#define ERROR
Definition: elog.h:45
LogicalOutputPluginWriterWrite write
Definition: logical.h:64
#define elog(elevel,...)
Definition: elog.h:228

◆ prepare_cb_wrapper()

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

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

867 {
868  LogicalDecodingContext *ctx = cache->private_data;
870  ErrorContextCallback errcallback;
871 
872  Assert(!ctx->fast_forward);
873 
874  /* We're only supposed to call this when two-phase commits are supported */
875  Assert(ctx->twophase);
876 
877  /* Push callback + info on the error context stack */
878  state.ctx = ctx;
879  state.callback_name = "prepare";
880  state.report_location = txn->final_lsn; /* beginning of prepare record */
882  errcallback.arg = (void *) &state;
883  errcallback.previous = error_context_stack;
884  error_context_stack = &errcallback;
885 
886  /* set output state */
887  ctx->accept_writes = true;
888  ctx->write_xid = txn->xid;
889  ctx->write_location = txn->end_lsn; /* points to the end of the record */
890 
891  /*
892  * If the plugin supports two-phase commits then prepare callback is
893  * mandatory
894  */
895  if (ctx->callbacks.prepare_cb == NULL)
896  ereport(ERROR,
897  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
898  errmsg("logical replication at prepare time requires prepare_cb callback")));
899 
900  /* do the actual work: call callback */
901  ctx->callbacks.prepare_cb(ctx, txn, prepare_lsn);
902 
903  /* Pop the error context stack */
904  error_context_stack = errcallback.previous;
905 }
TransactionId write_xid
Definition: logical.h:98
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:52
int errcode(int sqlerrcode)
Definition: elog.c:704
void(* callback)(void *arg)
Definition: elog.h:243
struct ErrorContextCallback * previous
Definition: elog.h:242
const char * callback_name
Definition: logical.c:50
XLogRecPtr write_location
Definition: logical.h:97
ErrorContextCallback * error_context_stack
Definition: elog.c:93
LogicalDecodePrepareCB prepare_cb
#define ERROR
Definition: elog.h:45
XLogRecPtr final_lsn
TransactionId xid
#define ereport(elevel,...)
Definition: elog.h:155
#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:915
LogicalDecodingContext * ctx
Definition: logical.c:49
static void output_plugin_error_callback(void *arg)
Definition: logical.c:677

◆ ResetLogicalStreamingState()

void ResetLogicalStreamingState ( void  )

Definition at line 1739 of file logical.c.

References bsysscan, CheckXidAlive, and InvalidTransactionId.

Referenced by AbortSubTransaction(), and AbortTransaction().

1740 {
1742  bsysscan = false;
1743 }
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 951 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().

954 {
955  LogicalDecodingContext *ctx = cache->private_data;
957  ErrorContextCallback errcallback;
958 
959  Assert(!ctx->fast_forward);
960 
961  /* We're only supposed to call this when two-phase commits are supported */
962  Assert(ctx->twophase);
963 
964  /* Push callback + info on the error context stack */
965  state.ctx = ctx;
966  state.callback_name = "rollback_prepared";
967  state.report_location = txn->final_lsn; /* beginning of commit record */
969  errcallback.arg = (void *) &state;
970  errcallback.previous = error_context_stack;
971  error_context_stack = &errcallback;
972 
973  /* set output state */
974  ctx->accept_writes = true;
975  ctx->write_xid = txn->xid;
976  ctx->write_location = txn->end_lsn; /* points to the end of the record */
977 
978  /*
979  * If the plugin support two-phase commits then rollback prepared callback
980  * is mandatory
981  */
982  if (ctx->callbacks.rollback_prepared_cb == NULL)
983  ereport(ERROR,
984  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
985  errmsg("logical replication at prepare time requires rollback_prepared_cb callback")));
986 
987  /* do the actual work: call callback */
988  ctx->callbacks.rollback_prepared_cb(ctx, txn, prepare_end_lsn,
989  prepare_time);
990 
991  /* Pop the error context stack */
992  error_context_stack = errcallback.previous;
993 }
TransactionId write_xid
Definition: logical.h:98
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:52
int errcode(int sqlerrcode)
Definition: elog.c:704
void(* callback)(void *arg)
Definition: elog.h:243
struct ErrorContextCallback * previous
Definition: elog.h:242
const char * callback_name
Definition: logical.c:50
XLogRecPtr write_location
Definition: logical.h:97
ErrorContextCallback * error_context_stack
Definition: elog.c:93
#define ERROR
Definition: elog.h:45
XLogRecPtr final_lsn
LogicalDecodeRollbackPreparedCB rollback_prepared_cb
TransactionId xid
#define ereport(elevel,...)
Definition: elog.h:155
#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:915
LogicalDecodingContext * ctx
Definition: logical.c:49
static void output_plugin_error_callback(void *arg)
Definition: logical.c:677

◆ shutdown_cb_wrapper()

static void shutdown_cb_wrapper ( LogicalDecodingContext ctx)
static

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

724 {
726  ErrorContextCallback errcallback;
727 
728  Assert(!ctx->fast_forward);
729 
730  /* Push callback + info on the error context stack */
731  state.ctx = ctx;
732  state.callback_name = "shutdown";
735  errcallback.arg = (void *) &state;
736  errcallback.previous = error_context_stack;
737  error_context_stack = &errcallback;
738 
739  /* set output state */
740  ctx->accept_writes = false;
741 
742  /* do the actual work: call callback */
743  ctx->callbacks.shutdown_cb(ctx);
744 
745  /* Pop the error context stack */
746  error_context_stack = errcallback.previous;
747 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:52
void(* callback)(void *arg)
Definition: elog.h:243
struct ErrorContextCallback * previous
Definition: elog.h:242
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:677

◆ startup_cb_wrapper()

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

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

697 {
699  ErrorContextCallback errcallback;
700 
701  Assert(!ctx->fast_forward);
702 
703  /* Push callback + info on the error context stack */
704  state.ctx = ctx;
705  state.callback_name = "startup";
708  errcallback.arg = (void *) &state;
709  errcallback.previous = error_context_stack;
710  error_context_stack = &errcallback;
711 
712  /* set output state */
713  ctx->accept_writes = false;
714 
715  /* do the actual work: call callback */
716  ctx->callbacks.startup_cb(ctx, opt, is_init);
717 
718  /* Pop the error context stack */
719  error_context_stack = errcallback.previous;
720 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:52
void(* callback)(void *arg)
Definition: elog.h:243
struct ErrorContextCallback * previous
Definition: elog.h:242
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:677

◆ StartupDecodingContext()

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

Definition at line 146 of file logical.c.

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

Referenced by CreateDecodingContext(), and CreateInitDecodingContext().

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

1263 {
1264  LogicalDecodingContext *ctx = cache->private_data;
1266  ErrorContextCallback errcallback;
1267 
1268  Assert(!ctx->fast_forward);
1269 
1270  /* We're only supposed to call this when streaming is supported. */
1271  Assert(ctx->streaming);
1272 
1273  /* Push callback + info on the error context stack */
1274  state.ctx = ctx;
1275  state.callback_name = "stream_abort";
1276  state.report_location = abort_lsn;
1277  errcallback.callback = output_plugin_error_callback;
1278  errcallback.arg = (void *) &state;
1279  errcallback.previous = error_context_stack;
1280  error_context_stack = &errcallback;
1281 
1282  /* set output state */
1283  ctx->accept_writes = true;
1284  ctx->write_xid = txn->xid;
1285  ctx->write_location = abort_lsn;
1286 
1287  /* in streaming mode, stream_abort_cb is required */
1288  if (ctx->callbacks.stream_abort_cb == NULL)
1289  ereport(ERROR,
1290  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1291  errmsg("logical streaming requires a stream_abort_cb callback")));
1292 
1293  ctx->callbacks.stream_abort_cb(ctx, txn, abort_lsn);
1294 
1295  /* Pop the error context stack */
1296  error_context_stack = errcallback.previous;
1297 }
TransactionId write_xid
Definition: logical.h:98
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:52
int errcode(int sqlerrcode)
Definition: elog.c:704
LogicalDecodeStreamAbortCB stream_abort_cb
void(* callback)(void *arg)
Definition: elog.h:243
struct ErrorContextCallback * previous
Definition: elog.h:242
const char * callback_name
Definition: logical.c:50
XLogRecPtr write_location
Definition: logical.h:97
ErrorContextCallback * error_context_stack
Definition: elog.c:93
#define ERROR
Definition: elog.h:45
TransactionId xid
#define ereport(elevel,...)
Definition: elog.h:155
#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:915
LogicalDecodingContext * ctx
Definition: logical.c:49
static void output_plugin_error_callback(void *arg)
Definition: logical.c:677

◆ stream_change_cb_wrapper()

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

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

1384 {
1385  LogicalDecodingContext *ctx = cache->private_data;
1387  ErrorContextCallback errcallback;
1388 
1389  Assert(!ctx->fast_forward);
1390 
1391  /* We're only supposed to call this when streaming is supported. */
1392  Assert(ctx->streaming);
1393 
1394  /* Push callback + info on the error context stack */
1395  state.ctx = ctx;
1396  state.callback_name = "stream_change";
1397  state.report_location = change->lsn;
1398  errcallback.callback = output_plugin_error_callback;
1399  errcallback.arg = (void *) &state;
1400  errcallback.previous = error_context_stack;
1401  error_context_stack = &errcallback;
1402 
1403  /* set output state */
1404  ctx->accept_writes = true;
1405  ctx->write_xid = txn->xid;
1406 
1407  /*
1408  * report this change's lsn so replies from clients can give an up2date
1409  * answer. This won't ever be enough (and shouldn't be!) to confirm
1410  * receipt of this transaction, but it might allow another transaction's
1411  * commit to be confirmed with one message.
1412  */
1413  ctx->write_location = change->lsn;
1414 
1415  /* in streaming mode, stream_change_cb is required */
1416  if (ctx->callbacks.stream_change_cb == NULL)
1417  ereport(ERROR,
1418  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1419  errmsg("logical streaming requires a stream_change_cb callback")));
1420 
1421  ctx->callbacks.stream_change_cb(ctx, txn, relation, change);
1422 
1423  /* Pop the error context stack */
1424  error_context_stack = errcallback.previous;
1425 }
TransactionId write_xid
Definition: logical.h:98
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:52
int errcode(int sqlerrcode)
Definition: elog.c:704
void(* callback)(void *arg)
Definition: elog.h:243
struct ErrorContextCallback * previous
Definition: elog.h:242
const char * callback_name
Definition: logical.c:50
XLogRecPtr write_location
Definition: logical.h:97
ErrorContextCallback * error_context_stack
Definition: elog.c:93
#define ERROR
Definition: elog.h:45
TransactionId xid
#define ereport(elevel,...)
Definition: elog.h:155
#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:915
LogicalDecodingContext * ctx
Definition: logical.c:49
static void output_plugin_error_callback(void *arg)
Definition: logical.c:677
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 1343 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().

1345 {
1346  LogicalDecodingContext *ctx = cache->private_data;
1348  ErrorContextCallback errcallback;
1349 
1350  Assert(!ctx->fast_forward);
1351 
1352  /* We're only supposed to call this when streaming is supported. */
1353  Assert(ctx->streaming);
1354 
1355  /* Push callback + info on the error context stack */
1356  state.ctx = ctx;
1357  state.callback_name = "stream_commit";
1358  state.report_location = txn->final_lsn;
1359  errcallback.callback = output_plugin_error_callback;
1360  errcallback.arg = (void *) &state;
1361  errcallback.previous = error_context_stack;
1362  error_context_stack = &errcallback;
1363 
1364  /* set output state */
1365  ctx->accept_writes = true;
1366  ctx->write_xid = txn->xid;
1367  ctx->write_location = txn->end_lsn;
1368 
1369  /* in streaming mode, stream_commit_cb is required */
1370  if (ctx->callbacks.stream_commit_cb == NULL)
1371  ereport(ERROR,
1372  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1373  errmsg("logical streaming requires a stream_commit_cb callback")));
1374 
1375  ctx->callbacks.stream_commit_cb(ctx, txn, commit_lsn);
1376 
1377  /* Pop the error context stack */
1378  error_context_stack = errcallback.previous;
1379 }
TransactionId write_xid
Definition: logical.h:98
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:52
int errcode(int sqlerrcode)
Definition: elog.c:704
void(* callback)(void *arg)
Definition: elog.h:243
struct ErrorContextCallback * previous
Definition: elog.h:242
const char * callback_name
Definition: logical.c:50
XLogRecPtr write_location
Definition: logical.h:97
ErrorContextCallback * error_context_stack
Definition: elog.c:93
#define ERROR
Definition: elog.h:45
XLogRecPtr final_lsn
TransactionId xid
#define ereport(elevel,...)
Definition: elog.h:155
#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:915
LogicalDecodingContext * ctx
Definition: logical.c:49
static void output_plugin_error_callback(void *arg)
Definition: logical.c:677

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

1431 {
1432  LogicalDecodingContext *ctx = cache->private_data;
1434  ErrorContextCallback errcallback;
1435 
1436  Assert(!ctx->fast_forward);
1437 
1438  /* We're only supposed to call this when streaming is supported. */
1439  Assert(ctx->streaming);
1440 
1441  /* this callback is optional */
1442  if (ctx->callbacks.stream_message_cb == NULL)
1443  return;
1444 
1445  /* Push callback + info on the error context stack */
1446  state.ctx = ctx;
1447  state.callback_name = "stream_message";
1448  state.report_location = message_lsn;
1449  errcallback.callback = output_plugin_error_callback;
1450  errcallback.arg = (void *) &state;
1451  errcallback.previous = error_context_stack;
1452  error_context_stack = &errcallback;
1453 
1454  /* set output state */
1455  ctx->accept_writes = true;
1456  ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
1457  ctx->write_location = message_lsn;
1458 
1459  /* do the actual work: call callback */
1460  ctx->callbacks.stream_message_cb(ctx, txn, message_lsn, transactional, prefix,
1461  message_size, message);
1462 
1463  /* Pop the error context stack */
1464  error_context_stack = errcallback.previous;
1465 }
TransactionId write_xid
Definition: logical.h:98
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:52
LogicalDecodeStreamMessageCB stream_message_cb
void(* callback)(void *arg)
Definition: elog.h:243
struct ErrorContextCallback * previous
Definition: elog.h:242
const char * callback_name
Definition: logical.c:50
XLogRecPtr write_location
Definition: logical.h:97
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:677

◆ stream_prepare_cb_wrapper()

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

Definition at line 1300 of file logical.c.

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert, ErrorContextCallback::callback, LogicalErrorCallbackState::callback_name, LogicalDecodingContext::callbacks, LogicalErrorCallbackState::ctx, 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().

1302 {
1303  LogicalDecodingContext *ctx = cache->private_data;
1305  ErrorContextCallback errcallback;
1306 
1307  Assert(!ctx->fast_forward);
1308 
1309  /*
1310  * We're only supposed to call this when streaming and two-phase commits
1311  * are supported.
1312  */
1313  Assert(ctx->streaming);
1314  Assert(ctx->twophase);
1315 
1316  /* Push callback + info on the error context stack */
1317  state.ctx = ctx;
1318  state.callback_name = "stream_prepare";
1319  state.report_location = txn->final_lsn;
1320  errcallback.callback = output_plugin_error_callback;
1321  errcallback.arg = (void *) &state;
1322  errcallback.previous = error_context_stack;
1323  error_context_stack = &errcallback;
1324 
1325  /* set output state */
1326  ctx->accept_writes = true;
1327  ctx->write_xid = txn->xid;
1328  ctx->write_location = txn->end_lsn;
1329 
1330  /* in streaming mode with two-phase commits, stream_prepare_cb is required */
1331  if (ctx->callbacks.stream_prepare_cb == NULL)
1332  ereport(ERROR,
1333  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1334  errmsg("logical streaming at prepare time requires a stream_prepare_cb callback")));
1335 
1336  ctx->callbacks.stream_prepare_cb(ctx, txn, prepare_lsn);
1337 
1338  /* Pop the error context stack */
1339  error_context_stack = errcallback.previous;
1340 }
TransactionId write_xid
Definition: logical.h:98
void * private_data
LogicalDecodeStreamPrepareCB stream_prepare_cb
OutputPluginCallbacks callbacks
Definition: logical.h:52
int errcode(int sqlerrcode)
Definition: elog.c:704
void(* callback)(void *arg)
Definition: elog.h:243
struct ErrorContextCallback * previous
Definition: elog.h:242
const char * callback_name
Definition: logical.c:50
XLogRecPtr write_location
Definition: logical.h:97
ErrorContextCallback * error_context_stack
Definition: elog.c:93
#define ERROR
Definition: elog.h:45
XLogRecPtr final_lsn
TransactionId xid
#define ereport(elevel,...)
Definition: elog.h:155
#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:915
LogicalDecodingContext * ctx
Definition: logical.c:49
static void output_plugin_error_callback(void *arg)
Definition: logical.c:677

◆ stream_start_cb_wrapper()

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

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

1171 {
1172  LogicalDecodingContext *ctx = cache->private_data;
1174  ErrorContextCallback errcallback;
1175 
1176  Assert(!ctx->fast_forward);
1177 
1178  /* We're only supposed to call this when streaming is supported. */
1179  Assert(ctx->streaming);
1180 
1181  /* Push callback + info on the error context stack */
1182  state.ctx = ctx;
1183  state.callback_name = "stream_start";
1184  state.report_location = first_lsn;
1185  errcallback.callback = output_plugin_error_callback;
1186  errcallback.arg = (void *) &state;
1187  errcallback.previous = error_context_stack;
1188  error_context_stack = &errcallback;
1189 
1190  /* set output state */
1191  ctx->accept_writes = true;
1192  ctx->write_xid = txn->xid;
1193 
1194  /*
1195  * report this message's lsn so replies from clients can give an up2date
1196  * answer. This won't ever be enough (and shouldn't be!) to confirm
1197  * receipt of this transaction, but it might allow another transaction's
1198  * commit to be confirmed with one message.
1199  */
1200  ctx->write_location = first_lsn;
1201 
1202  /* in streaming mode, stream_start_cb is required */
1203  if (ctx->callbacks.stream_start_cb == NULL)
1204  ereport(ERROR,
1205  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1206  errmsg("logical streaming requires a stream_start_cb callback")));
1207 
1208  ctx->callbacks.stream_start_cb(ctx, txn);
1209 
1210  /* Pop the error context stack */
1211  error_context_stack = errcallback.previous;
1212 }
TransactionId write_xid
Definition: logical.h:98
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:52
int errcode(int sqlerrcode)
Definition: elog.c:704
void(* callback)(void *arg)
Definition: elog.h:243
struct ErrorContextCallback * previous
Definition: elog.h:242
const char * callback_name
Definition: logical.c:50
XLogRecPtr write_location
Definition: logical.h:97
ErrorContextCallback * error_context_stack
Definition: elog.c:93
#define ERROR
Definition: elog.h:45
TransactionId xid
#define ereport(elevel,...)
Definition: elog.h:155
#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:915
LogicalDecodingContext * ctx
Definition: logical.c:49
static void output_plugin_error_callback(void *arg)
Definition: logical.c:677

◆ stream_stop_cb_wrapper()

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

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

1217 {
1218  LogicalDecodingContext *ctx = cache->private_data;
1220  ErrorContextCallback errcallback;
1221 
1222  Assert(!ctx->fast_forward);
1223 
1224  /* We're only supposed to call this when streaming is supported. */
1225  Assert(ctx->streaming);
1226 
1227  /* Push callback + info on the error context stack */
1228  state.ctx = ctx;
1229  state.callback_name = "stream_stop";
1230  state.report_location = last_lsn;
1231  errcallback.callback = output_plugin_error_callback;
1232  errcallback.arg = (void *) &state;
1233  errcallback.previous = error_context_stack;
1234  error_context_stack = &errcallback;
1235 
1236  /* set output state */
1237  ctx->accept_writes = true;
1238  ctx->write_xid = txn->xid;
1239 
1240  /*
1241  * report this message's lsn so replies from clients can give an up2date
1242  * answer. This won't ever be enough (and shouldn't be!) to confirm
1243  * receipt of this transaction, but it might allow another transaction's
1244  * commit to be confirmed with one message.
1245  */
1246  ctx->write_location = last_lsn;
1247 
1248  /* in streaming mode, stream_stop_cb is required */
1249  if (ctx->callbacks.stream_stop_cb == NULL)
1250  ereport(ERROR,
1251  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1252  errmsg("logical streaming requires a stream_stop_cb callback")));
1253 
1254  ctx->callbacks.stream_stop_cb(ctx, txn);
1255 
1256  /* Pop the error context stack */
1257  error_context_stack = errcallback.previous;
1258 }
TransactionId write_xid
Definition: logical.h:98
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:52
int errcode(int sqlerrcode)
Definition: elog.c:704
void(* callback)(void *arg)
Definition: elog.h:243
struct ErrorContextCallback * previous
Definition: elog.h:242
const char * callback_name
Definition: logical.c:50
XLogRecPtr write_location
Definition: logical.h:97
ErrorContextCallback * error_context_stack
Definition: elog.c:93
#define ERROR
Definition: elog.h:45
TransactionId xid
#define ereport(elevel,...)
Definition: elog.h:155
#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:915
LogicalDecodeStreamStopCB stream_stop_cb
LogicalDecodingContext * ctx
Definition: logical.c:49
static void output_plugin_error_callback(void *arg)
Definition: logical.c:677

◆ stream_truncate_cb_wrapper()

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

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

1471 {
1472  LogicalDecodingContext *ctx = cache->private_data;
1474  ErrorContextCallback errcallback;
1475 
1476  Assert(!ctx->fast_forward);
1477 
1478  /* We're only supposed to call this when streaming is supported. */
1479  Assert(ctx->streaming);
1480 
1481  /* this callback is optional */
1482  if (!ctx->callbacks.stream_truncate_cb)
1483  return;
1484 
1485  /* Push callback + info on the error context stack */
1486  state.ctx = ctx;
1487  state.callback_name = "stream_truncate";
1488  state.report_location = change->lsn;
1489  errcallback.callback = output_plugin_error_callback;
1490  errcallback.arg = (void *) &state;
1491  errcallback.previous = error_context_stack;
1492  error_context_stack = &errcallback;
1493 
1494  /* set output state */
1495  ctx->accept_writes = true;
1496  ctx->write_xid = txn->xid;
1497 
1498  /*
1499  * report this change's lsn so replies from clients can give an up2date
1500  * answer. This won't ever be enough (and shouldn't be!) to confirm
1501  * receipt of this transaction, but it might allow another transaction's
1502  * commit to be confirmed with one message.
1503  */
1504  ctx->write_location = change->lsn;
1505 
1506  ctx->callbacks.stream_truncate_cb(ctx, txn, nrelations, relations, change);
1507 
1508  /* Pop the error context stack */
1509  error_context_stack = errcallback.previous;
1510 }
TransactionId write_xid
Definition: logical.h:98
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:52
void(* callback)(void *arg)
Definition: elog.h:243
struct ErrorContextCallback * previous
Definition: elog.h:242
const char * callback_name
Definition: logical.c:50
XLogRecPtr write_location
Definition: logical.h:97
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:677

◆ truncate_cb_wrapper()

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

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

1035 {
1036  LogicalDecodingContext *ctx = cache->private_data;
1038  ErrorContextCallback errcallback;
1039 
1040  Assert(!ctx->fast_forward);
1041 
1042  if (!ctx->callbacks.truncate_cb)
1043  return;
1044 
1045  /* Push callback + info on the error context stack */
1046  state.ctx = ctx;
1047  state.callback_name = "truncate";
1048  state.report_location = change->lsn;
1049  errcallback.callback = output_plugin_error_callback;
1050  errcallback.arg = (void *) &state;
1051  errcallback.previous = error_context_stack;
1052  error_context_stack = &errcallback;
1053 
1054  /* set output state */
1055  ctx->accept_writes = true;
1056  ctx->write_xid = txn->xid;
1057 
1058  /*
1059  * report this change's lsn so replies from clients can give an up2date
1060  * answer. This won't ever be enough (and shouldn't be!) to confirm
1061  * receipt of this transaction, but it might allow another transaction's
1062  * commit to be confirmed with one message.
1063  */
1064  ctx->write_location = change->lsn;
1065 
1066  ctx->callbacks.truncate_cb(ctx, txn, nrelations, relations, change);
1067 
1068  /* Pop the error context stack */
1069  error_context_stack = errcallback.previous;
1070 }
LogicalDecodeTruncateCB truncate_cb
TransactionId write_xid
Definition: logical.h:98
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:52
void(* callback)(void *arg)
Definition: elog.h:243
struct ErrorContextCallback * previous
Definition: elog.h:242
const char * callback_name
Definition: logical.c:50
XLogRecPtr write_location
Definition: logical.h:97
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:677

◆ UpdateDecodingStats()

void UpdateDecodingStats ( LogicalDecodingContext ctx)

Definition at line 1749 of file logical.c.

References ReplicationSlot::data, DEBUG2, elog, ReplicationSlotPersistentData::name, NameStr, pgstat_report_replslot(), LogicalDecodingContext::reorder, LogicalDecodingContext::slot, ReorderBuffer::spillBytes, ReorderBuffer::spillCount, ReorderBuffer::spillTxns, ReorderBuffer::streamBytes, ReorderBuffer::streamCount, and ReorderBuffer::streamTxns.

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

1750 {
1751  ReorderBuffer *rb = ctx->reorder;
1752 
1753  /*
1754  * Nothing to do if we haven't spilled or streamed anything since the last
1755  * time the stats has been sent.
1756  */
1757  if (rb->spillBytes <= 0 && rb->streamBytes <= 0)
1758  return;
1759 
1760  elog(DEBUG2, "UpdateDecodingStats: updating stats %p %lld %lld %lld %lld %lld %lld",
1761  rb,
1762  (long long) rb->spillTxns,
1763  (long long) rb->spillCount,
1764  (long long) rb->spillBytes,
1765  (long long) rb->streamTxns,
1766  (long long) rb->streamCount,
1767  (long long) rb->streamBytes);
1768 
1770  rb->spillTxns, rb->spillCount, rb->spillBytes,
1771  rb->streamTxns, rb->streamCount, rb->streamBytes);
1772  rb->spillTxns = 0;
1773  rb->spillCount = 0;
1774  rb->spillBytes = 0;
1775  rb->streamTxns = 0;
1776  rb->streamCount = 0;
1777  rb->streamBytes = 0;
1778 }
struct ReorderBuffer * reorder
Definition: logical.h:42
ReplicationSlotPersistentData data
Definition: slot.h:144
#define DEBUG2
Definition: elog.h:24
void pgstat_report_replslot(const char *slotname, int spilltxns, int spillcount, int spillbytes, int streamtxns, int streamcount, int streambytes)
Definition: pgstat.c:1776
ReplicationSlot * slot
Definition: logical.h:38
#define elog(elevel,...)
Definition: elog.h:228
#define NameStr(name)
Definition: c.h:681