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 update_progress_txn_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr lsn)
 
static void LoadOutputPlugin (OutputPluginCallbacks *callbacks, const char *plugin)
 
void CheckLogicalDecodingRequirements (void)
 
static LogicalDecodingContextStartupDecodingContext (List *output_plugin_options, XLogRecPtr start_lsn, TransactionId xmin_horizon, bool need_full_snapshot, bool fast_forward, XLogReaderRoutine *xl_routine, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
 
LogicalDecodingContextCreateInitDecodingContext (const char *plugin, List *output_plugin_options, bool need_full_snapshot, XLogRecPtr restart_lsn, XLogReaderRoutine *xl_routine, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
 
LogicalDecodingContextCreateDecodingContext (XLogRecPtr start_lsn, List *output_plugin_options, bool fast_forward, XLogReaderRoutine *xl_routine, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
 
bool DecodingContextReady (LogicalDecodingContext *ctx)
 
void DecodingContextFindStartpoint (LogicalDecodingContext *ctx)
 
void FreeDecodingContext (LogicalDecodingContext *ctx)
 
void OutputPluginPrepareWrite (struct LogicalDecodingContext *ctx, bool last_write)
 
void OutputPluginWrite (struct LogicalDecodingContext *ctx, bool last_write)
 
void OutputPluginUpdateProgress (struct LogicalDecodingContext *ctx, bool skipped_xact)
 
bool filter_prepare_cb_wrapper (LogicalDecodingContext *ctx, TransactionId xid, const char *gid)
 
bool filter_by_origin_cb_wrapper (LogicalDecodingContext *ctx, RepOriginId origin_id)
 
void LogicalIncreaseXminForSlot (XLogRecPtr current_lsn, TransactionId xmin)
 
void LogicalIncreaseRestartDecodingForSlot (XLogRecPtr current_lsn, XLogRecPtr restart_lsn)
 
void LogicalConfirmReceivedLocation (XLogRecPtr lsn)
 
void ResetLogicalStreamingState (void)
 
void UpdateDecodingStats (LogicalDecodingContext *ctx)
 

Typedef Documentation

◆ LogicalErrorCallbackState

Function Documentation

◆ begin_cb_wrapper()

static void begin_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn 
)
static

Definition at line 805 of file logical.c.

806 {
807  LogicalDecodingContext *ctx = cache->private_data;
809  ErrorContextCallback errcallback;
810 
811  Assert(!ctx->fast_forward);
812 
813  /* Push callback + info on the error context stack */
814  state.ctx = ctx;
815  state.callback_name = "begin";
816  state.report_location = txn->first_lsn;
818  errcallback.arg = (void *) &state;
819  errcallback.previous = error_context_stack;
820  error_context_stack = &errcallback;
821 
822  /* set output state */
823  ctx->accept_writes = true;
824  ctx->write_xid = txn->xid;
825  ctx->write_location = txn->first_lsn;
826  ctx->end_xact = false;
827 
828  /* do the actual work: call callback */
829  ctx->callbacks.begin_cb(ctx, txn);
830 
831  /* Pop the error context stack */
832  error_context_stack = errcallback.previous;
833 }
ErrorContextCallback * error_context_stack
Definition: elog.c:95
Assert(fmt[strlen(fmt) - 1] !='\n')
static void output_plugin_error_callback(void *arg)
Definition: logical.c:725
struct ErrorContextCallback * previous
Definition: elog.h:295
void(* callback)(void *arg)
Definition: elog.h:296
XLogRecPtr write_location
Definition: logical.h:108
OutputPluginCallbacks callbacks
Definition: logical.h:53
TransactionId write_xid
Definition: logical.h:109
LogicalDecodeBeginCB begin_cb
XLogRecPtr first_lsn
TransactionId xid
void * private_data
Definition: regguts.h:318

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

Referenced by StartupDecodingContext().

◆ begin_prepare_cb_wrapper()

static void begin_prepare_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn 
)
static

Definition at line 875 of file logical.c.

876 {
877  LogicalDecodingContext *ctx = cache->private_data;
879  ErrorContextCallback errcallback;
880 
881  Assert(!ctx->fast_forward);
882 
883  /* We're only supposed to call this when two-phase commits are supported */
884  Assert(ctx->twophase);
885 
886  /* Push callback + info on the error context stack */
887  state.ctx = ctx;
888  state.callback_name = "begin_prepare";
889  state.report_location = txn->first_lsn;
891  errcallback.arg = (void *) &state;
892  errcallback.previous = error_context_stack;
893  error_context_stack = &errcallback;
894 
895  /* set output state */
896  ctx->accept_writes = true;
897  ctx->write_xid = txn->xid;
898  ctx->write_location = txn->first_lsn;
899  ctx->end_xact = false;
900 
901  /*
902  * If the plugin supports two-phase commits then begin prepare callback is
903  * mandatory
904  */
905  if (ctx->callbacks.begin_prepare_cb == NULL)
906  ereport(ERROR,
907  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
908  errmsg("logical replication at prepare time requires a %s callback",
909  "begin_prepare_cb")));
910 
911  /* do the actual work: call callback */
912  ctx->callbacks.begin_prepare_cb(ctx, txn);
913 
914  /* Pop the error context stack */
915  error_context_stack = errcallback.previous;
916 }
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
LogicalDecodeBeginPrepareCB begin_prepare_cb

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

Referenced by StartupDecodingContext().

◆ change_cb_wrapper()

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

Definition at line 1056 of file logical.c.

1058 {
1059  LogicalDecodingContext *ctx = cache->private_data;
1061  ErrorContextCallback errcallback;
1062 
1063  Assert(!ctx->fast_forward);
1064 
1065  /* Push callback + info on the error context stack */
1066  state.ctx = ctx;
1067  state.callback_name = "change";
1068  state.report_location = change->lsn;
1069  errcallback.callback = output_plugin_error_callback;
1070  errcallback.arg = (void *) &state;
1071  errcallback.previous = error_context_stack;
1072  error_context_stack = &errcallback;
1073 
1074  /* set output state */
1075  ctx->accept_writes = true;
1076  ctx->write_xid = txn->xid;
1077 
1078  /*
1079  * Report this change's lsn so replies from clients can give an up-to-date
1080  * answer. This won't ever be enough (and shouldn't be!) to confirm
1081  * receipt of this transaction, but it might allow another transaction's
1082  * commit to be confirmed with one message.
1083  */
1084  ctx->write_location = change->lsn;
1085 
1086  ctx->end_xact = false;
1087 
1088  ctx->callbacks.change_cb(ctx, txn, relation, change);
1089 
1090  /* Pop the error context stack */
1091  error_context_stack = errcallback.previous;
1092 }
LogicalDecodeChangeCB change_cb

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

Referenced by StartupDecodingContext().

◆ CheckLogicalDecodingRequirements()

void CheckLogicalDecodingRequirements ( void  )

Definition at line 108 of file logical.c.

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

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

◆ commit_cb_wrapper()

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

Definition at line 836 of file logical.c.

838 {
839  LogicalDecodingContext *ctx = cache->private_data;
841  ErrorContextCallback errcallback;
842 
843  Assert(!ctx->fast_forward);
844 
845  /* Push callback + info on the error context stack */
846  state.ctx = ctx;
847  state.callback_name = "commit";
848  state.report_location = txn->final_lsn; /* beginning of commit record */
850  errcallback.arg = (void *) &state;
851  errcallback.previous = error_context_stack;
852  error_context_stack = &errcallback;
853 
854  /* set output state */
855  ctx->accept_writes = true;
856  ctx->write_xid = txn->xid;
857  ctx->write_location = txn->end_lsn; /* points to the end of the record */
858  ctx->end_xact = true;
859 
860  /* do the actual work: call callback */
861  ctx->callbacks.commit_cb(ctx, txn, commit_lsn);
862 
863  /* Pop the error context stack */
864  error_context_stack = errcallback.previous;
865 }
LogicalDecodeCommitCB commit_cb
XLogRecPtr final_lsn
XLogRecPtr end_lsn

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

Referenced by StartupDecodingContext().

◆ commit_prepared_cb_wrapper()

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

Definition at line 964 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ CreateDecodingContext()

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

Definition at line 490 of file logical.c.

497 {
499  ReplicationSlot *slot;
500  MemoryContext old_context;
501 
502  /* shorter lines... */
503  slot = MyReplicationSlot;
504 
505  /* first some sanity checks that are unlikely to be violated */
506  if (slot == NULL)
507  elog(ERROR, "cannot perform logical decoding without an acquired slot");
508 
509  /* make sure the passed slot is suitable, these are user facing errors */
510  if (SlotIsPhysical(slot))
511  ereport(ERROR,
512  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
513  errmsg("cannot use physical replication slot for logical decoding")));
514 
515  if (slot->data.database != MyDatabaseId)
516  ereport(ERROR,
517  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
518  errmsg("replication slot \"%s\" was not created in this database",
519  NameStr(slot->data.name))));
520 
521  if (start_lsn == InvalidXLogRecPtr)
522  {
523  /* continue from last position */
524  start_lsn = slot->data.confirmed_flush;
525  }
526  else if (start_lsn < slot->data.confirmed_flush)
527  {
528  /*
529  * It might seem like we should error out in this case, but it's
530  * pretty common for a client to acknowledge a LSN it doesn't have to
531  * do anything for, and thus didn't store persistently, because the
532  * xlog records didn't result in anything relevant for logical
533  * decoding. Clients have to be able to do that to support synchronous
534  * replication.
535  *
536  * Starting at a different LSN than requested might not catch certain
537  * kinds of client errors; so the client may wish to check that
538  * confirmed_flush_lsn matches its expectations.
539  */
540  elog(LOG, "%X/%X has been already streamed, forwarding to %X/%X",
541  LSN_FORMAT_ARGS(start_lsn),
543 
544  start_lsn = slot->data.confirmed_flush;
545  }
546 
547  ctx = StartupDecodingContext(output_plugin_options,
548  start_lsn, InvalidTransactionId, false,
549  fast_forward, xl_routine, prepare_write,
550  do_write, update_progress);
551 
552  /* call output plugin initialization callback */
553  old_context = MemoryContextSwitchTo(ctx->context);
554  if (ctx->callbacks.startup_cb != NULL)
555  startup_cb_wrapper(ctx, &ctx->options, false);
556  MemoryContextSwitchTo(old_context);
557 
558  /*
559  * We allow decoding of prepared transactions when the two_phase is
560  * enabled at the time of slot creation, or when the two_phase option is
561  * given at the streaming start, provided the plugin supports all the
562  * callbacks for two-phase.
563  */
564  ctx->twophase &= (slot->data.two_phase || ctx->twophase_opt_given);
565 
566  /* Mark slot to allow two_phase decoding if not already marked */
567  if (ctx->twophase && !slot->data.two_phase)
568  {
569  SpinLockAcquire(&slot->mutex);
570  slot->data.two_phase = true;
571  slot->data.two_phase_at = start_lsn;
572  SpinLockRelease(&slot->mutex);
575  SnapBuildSetTwoPhaseAt(ctx->snapshot_builder, start_lsn);
576  }
577 
579 
580  ereport(LOG,
581  (errmsg("starting logical decoding for slot \"%s\"",
582  NameStr(slot->data.name)),
583  errdetail("Streaming transactions committing after %X/%X, reading WAL from %X/%X.",
585  LSN_FORMAT_ARGS(slot->data.restart_lsn))));
586 
587  return ctx;
588 }
#define NameStr(name)
Definition: c.h:730
int errdetail(const char *fmt,...)
Definition: elog.c:1202
#define LOG
Definition: elog.h:31
static LogicalDecodingContext * StartupDecodingContext(List *output_plugin_options, XLogRecPtr start_lsn, TransactionId xmin_horizon, bool need_full_snapshot, bool fast_forward, XLogReaderRoutine *xl_routine, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
Definition: logical.c:151
static void startup_cb_wrapper(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
Definition: logical.c:744
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:138
const void * data
void ReplicationSlotMarkDirty(void)
Definition: slot.c:796
ReplicationSlot * MyReplicationSlot
Definition: slot.c:98
void ReplicationSlotSave(void)
Definition: slot.c:778
#define SlotIsPhysical(slot)
Definition: slot.h:168
void SnapBuildSetTwoPhaseAt(SnapBuild *builder, XLogRecPtr ptr)
Definition: snapbuild.c:420
#define SpinLockRelease(lock)
Definition: spin.h:64
#define SpinLockAcquire(lock)
Definition: spin.h:62
OutputPluginOptions options
Definition: logical.h:54
MemoryContext context
Definition: logical.h:36
struct SnapBuild * snapshot_builder
Definition: logical.h:44
struct ReorderBuffer * reorder
Definition: logical.h:43
LogicalDecodeStartupCB startup_cb
XLogRecPtr two_phase_at
Definition: slot.h:90
XLogRecPtr restart_lsn
Definition: slot.h:73
XLogRecPtr confirmed_flush
Definition: slot.h:84
slock_t mutex
Definition: slot.h:120
ReplicationSlotPersistentData data
Definition: slot.h:147
#define InvalidTransactionId
Definition: transam.h:31
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28

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

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

◆ CreateInitDecodingContext()

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

Definition at line 330 of file logical.c.

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

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

Referenced by create_logical_replication_slot(), and CreateReplicationSlot().

◆ DecodingContextFindStartpoint()

void DecodingContextFindStartpoint ( LogicalDecodingContext ctx)

Definition at line 603 of file logical.c.

604 {
605  ReplicationSlot *slot = ctx->slot;
606 
607  /* Initialize from where to start reading WAL. */
608  XLogBeginRead(ctx->reader, slot->data.restart_lsn);
609 
610  elog(DEBUG1, "searching for logical decoding starting point, starting at %X/%X",
612 
613  /* Wait for a consistent starting point */
614  for (;;)
615  {
616  XLogRecord *record;
617  char *err = NULL;
618 
619  /* the read_page callback waits for new WAL */
620  record = XLogReadRecord(ctx->reader, &err);
621  if (err)
622  elog(ERROR, "could not find logical decoding starting point: %s", err);
623  if (!record)
624  elog(ERROR, "could not find logical decoding starting point");
625 
627 
628  /* only continue till we found a consistent spot */
629  if (DecodingContextReady(ctx))
630  break;
631 
633  }
634 
635  SpinLockAcquire(&slot->mutex);
636  slot->data.confirmed_flush = ctx->reader->EndRecPtr;
637  if (slot->data.two_phase)
638  slot->data.two_phase_at = ctx->reader->EndRecPtr;
639  SpinLockRelease(&slot->mutex);
640 }
void LogicalDecodingProcessRecord(LogicalDecodingContext *ctx, XLogReaderState *record)
Definition: decode.c:91
#define DEBUG1
Definition: elog.h:30
void err(int eval, const char *fmt,...)
Definition: err.c:43
bool DecodingContextReady(LogicalDecodingContext *ctx)
Definition: logical.c:594
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
XLogReaderState * reader
Definition: logical.h:42
ReplicationSlot * slot
Definition: logical.h:39
XLogRecPtr EndRecPtr
Definition: xlogreader.h:207
XLogRecord * XLogReadRecord(XLogReaderState *state, char **errormsg)
Definition: xlogreader.c:422
void XLogBeginRead(XLogReaderState *state, XLogRecPtr RecPtr)
Definition: xlogreader.c:264

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

Referenced by create_logical_replication_slot(), and CreateReplicationSlot().

◆ DecodingContextReady()

bool DecodingContextReady ( LogicalDecodingContext ctx)

Definition at line 594 of file logical.c.

595 {
597 }
SnapBuildState SnapBuildCurrentState(SnapBuild *builder)
Definition: snapbuild.c:402
@ SNAPBUILD_CONSISTENT
Definition: snapbuild.h:46

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

Referenced by DecodingContextFindStartpoint().

◆ filter_by_origin_cb_wrapper()

bool filter_by_origin_cb_wrapper ( LogicalDecodingContext ctx,
RepOriginId  origin_id 
)

Definition at line 1169 of file logical.c.

1170 {
1172  ErrorContextCallback errcallback;
1173  bool ret;
1174 
1175  Assert(!ctx->fast_forward);
1176 
1177  /* Push callback + info on the error context stack */
1178  state.ctx = ctx;
1179  state.callback_name = "filter_by_origin";
1180  state.report_location = InvalidXLogRecPtr;
1181  errcallback.callback = output_plugin_error_callback;
1182  errcallback.arg = (void *) &state;
1183  errcallback.previous = error_context_stack;
1184  error_context_stack = &errcallback;
1185 
1186  /* set output state */
1187  ctx->accept_writes = false;
1188  ctx->end_xact = false;
1189 
1190  /* do the actual work: call callback */
1191  ret = ctx->callbacks.filter_by_origin_cb(ctx, origin_id);
1192 
1193  /* Pop the error context stack */
1194  error_context_stack = errcallback.previous;
1195 
1196  return ret;
1197 }
LogicalDecodeFilterByOriginCB filter_by_origin_cb

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

Referenced by FilterByOrigin().

◆ filter_prepare_cb_wrapper()

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

Definition at line 1137 of file logical.c.

1139 {
1141  ErrorContextCallback errcallback;
1142  bool ret;
1143 
1144  Assert(!ctx->fast_forward);
1145 
1146  /* Push callback + info on the error context stack */
1147  state.ctx = ctx;
1148  state.callback_name = "filter_prepare";
1149  state.report_location = InvalidXLogRecPtr;
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 = false;
1157  ctx->end_xact = false;
1158 
1159  /* do the actual work: call callback */
1160  ret = ctx->callbacks.filter_prepare_cb(ctx, xid, gid);
1161 
1162  /* Pop the error context stack */
1163  error_context_stack = errcallback.previous;
1164 
1165  return ret;
1166 }
LogicalDecodeFilterPrepareCB filter_prepare_cb

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

Referenced by FilterPrepare().

◆ FreeDecodingContext()

void FreeDecodingContext ( LogicalDecodingContext ctx)

Definition at line 647 of file logical.c.

648 {
649  if (ctx->callbacks.shutdown_cb != NULL)
650  shutdown_cb_wrapper(ctx);
651 
654  XLogReaderFree(ctx->reader);
656 }
static void shutdown_cb_wrapper(LogicalDecodingContext *ctx)
Definition: logical.c:772
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:387
void ReorderBufferFree(ReorderBuffer *rb)
void FreeSnapshotBuilder(SnapBuild *builder)
Definition: snapbuild.c:358
LogicalDecodeShutdownCB shutdown_cb
void XLogReaderFree(XLogReaderState *state)
Definition: xlogreader.c:170

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

◆ LoadOutputPlugin()

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

Definition at line 703 of file logical.c.

704 {
705  LogicalOutputPluginInit plugin_init;
706 
707  plugin_init = (LogicalOutputPluginInit)
708  load_external_function(plugin, "_PG_output_plugin_init", false, NULL);
709 
710  if (plugin_init == NULL)
711  elog(ERROR, "output plugins have to declare the _PG_output_plugin_init symbol");
712 
713  /* ask the output plugin to fill the callback struct */
714  plugin_init(callbacks);
715 
716  if (callbacks->begin_cb == NULL)
717  elog(ERROR, "output plugins have to register a begin callback");
718  if (callbacks->change_cb == NULL)
719  elog(ERROR, "output plugins have to register a change callback");
720  if (callbacks->commit_cb == NULL)
721  elog(ERROR, "output plugins have to register a commit callback");
722 }
void * load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
Definition: dfmgr.c:105
void(* LogicalOutputPluginInit)(struct OutputPluginCallbacks *cb)
Definition: output_plugin.h:36

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

Referenced by StartupDecodingContext().

◆ LogicalConfirmReceivedLocation()

void LogicalConfirmReceivedLocation ( XLogRecPtr  lsn)

Definition at line 1788 of file logical.c.

1789 {
1790  Assert(lsn != InvalidXLogRecPtr);
1791 
1792  /* Do an unlocked check for candidate_lsn first. */
1795  {
1796  bool updated_xmin = false;
1797  bool updated_restart = false;
1798 
1800 
1802 
1803  /* if we're past the location required for bumping xmin, do so */
1806  {
1807  /*
1808  * We have to write the changed xmin to disk *before* we change
1809  * the in-memory value, otherwise after a crash we wouldn't know
1810  * that some catalog tuples might have been removed already.
1811  *
1812  * Ensure that by first writing to ->xmin and only update
1813  * ->effective_xmin once the new state is synced to disk. After a
1814  * crash ->effective_xmin is set to ->xmin.
1815  */
1818  {
1822  updated_xmin = true;
1823  }
1824  }
1825 
1828  {
1830 
1834  updated_restart = true;
1835  }
1836 
1838 
1839  /* first write new xmin to disk, so we know what's up after a crash */
1840  if (updated_xmin || updated_restart)
1841  {
1844  elog(DEBUG1, "updated xmin: %u restart: %u", updated_xmin, updated_restart);
1845  }
1846 
1847  /*
1848  * Now the new xmin is safely on disk, we can let the global value
1849  * advance. We do not take ProcArrayLock or similar since we only
1850  * advance xmin here and there's not much harm done by a concurrent
1851  * computation missing that.
1852  */
1853  if (updated_xmin)
1854  {
1858 
1861  }
1862  }
1863  else
1864  {
1868  }
1869 }
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:892
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:163
XLogRecPtr candidate_restart_valid
Definition: slot.h:164
XLogRecPtr candidate_restart_lsn
Definition: slot.h:165
TransactionId candidate_catalog_xmin
Definition: slot.h:162
#define TransactionIdIsValid(xid)
Definition: transam.h:41

References Assert(), ReplicationSlot::candidate_catalog_xmin, ReplicationSlot::candidate_restart_lsn, ReplicationSlot::candidate_restart_valid, ReplicationSlot::candidate_xmin_lsn, ReplicationSlotPersistentData::catalog_xmin, ReplicationSlotPersistentData::confirmed_flush, ReplicationSlot::data, DEBUG1, ReplicationSlot::effective_catalog_xmin, elog(), InvalidTransactionId, InvalidXLogRecPtr, ReplicationSlot::mutex, MyReplicationSlot, ReplicationSlotMarkDirty(), ReplicationSlotSave(), ReplicationSlotsComputeRequiredLSN(), ReplicationSlotsComputeRequiredXmin(), ReplicationSlotPersistentData::restart_lsn, SpinLockAcquire, SpinLockRelease, and TransactionIdIsValid.

Referenced by LogicalIncreaseRestartDecodingForSlot(), LogicalIncreaseXminForSlot(), pg_logical_replication_slot_advance(), pg_logical_slot_get_changes_guts(), and ProcessStandbyReplyMessage().

◆ LogicalIncreaseRestartDecodingForSlot()

void LogicalIncreaseRestartDecodingForSlot ( XLogRecPtr  current_lsn,
XLogRecPtr  restart_lsn 
)

Definition at line 1714 of file logical.c.

1715 {
1716  bool updated_lsn = false;
1717  ReplicationSlot *slot;
1718 
1719  slot = MyReplicationSlot;
1720 
1721  Assert(slot != NULL);
1722  Assert(restart_lsn != InvalidXLogRecPtr);
1723  Assert(current_lsn != InvalidXLogRecPtr);
1724 
1725  SpinLockAcquire(&slot->mutex);
1726 
1727  /* don't overwrite if have a newer restart lsn */
1728  if (restart_lsn <= slot->data.restart_lsn)
1729  {
1730  }
1731 
1732  /*
1733  * We might have already flushed far enough to directly accept this lsn,
1734  * in this case there is no need to check for existing candidate LSNs
1735  */
1736  else if (current_lsn <= slot->data.confirmed_flush)
1737  {
1738  slot->candidate_restart_valid = current_lsn;
1739  slot->candidate_restart_lsn = restart_lsn;
1740 
1741  /* our candidate can directly be used */
1742  updated_lsn = true;
1743  }
1744 
1745  /*
1746  * Only increase if the previous values have been applied, otherwise we
1747  * might never end up updating if the receiver acks too slowly. A missed
1748  * value here will just cause some extra effort after reconnecting.
1749  */
1751  {
1752  slot->candidate_restart_valid = current_lsn;
1753  slot->candidate_restart_lsn = restart_lsn;
1754  SpinLockRelease(&slot->mutex);
1755 
1756  elog(DEBUG1, "got new restart lsn %X/%X at %X/%X",
1757  LSN_FORMAT_ARGS(restart_lsn),
1758  LSN_FORMAT_ARGS(current_lsn));
1759  }
1760  else
1761  {
1762  XLogRecPtr candidate_restart_lsn;
1763  XLogRecPtr candidate_restart_valid;
1764  XLogRecPtr confirmed_flush;
1765 
1766  candidate_restart_lsn = slot->candidate_restart_lsn;
1767  candidate_restart_valid = slot->candidate_restart_valid;
1768  confirmed_flush = slot->data.confirmed_flush;
1769  SpinLockRelease(&slot->mutex);
1770 
1771  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",
1772  LSN_FORMAT_ARGS(restart_lsn),
1773  LSN_FORMAT_ARGS(current_lsn),
1774  LSN_FORMAT_ARGS(candidate_restart_lsn),
1775  LSN_FORMAT_ARGS(candidate_restart_valid),
1776  LSN_FORMAT_ARGS(confirmed_flush));
1777  }
1778 
1779  /* candidates are already valid with the current flush position, apply */
1780  if (updated_lsn)
1782 }
void LogicalConfirmReceivedLocation(XLogRecPtr lsn)
Definition: logical.c:1788
uint64 XLogRecPtr
Definition: xlogdefs.h:21

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

Referenced by SnapBuildProcessRunningXacts().

◆ LogicalIncreaseXminForSlot()

void LogicalIncreaseXminForSlot ( XLogRecPtr  current_lsn,
TransactionId  xmin 
)

Definition at line 1646 of file logical.c.

1647 {
1648  bool updated_xmin = false;
1649  ReplicationSlot *slot;
1650  bool got_new_xmin = false;
1651 
1652  slot = MyReplicationSlot;
1653 
1654  Assert(slot != NULL);
1655 
1656  SpinLockAcquire(&slot->mutex);
1657 
1658  /*
1659  * don't overwrite if we already have a newer xmin. This can happen if we
1660  * restart decoding in a slot.
1661  */
1663  {
1664  }
1665 
1666  /*
1667  * If the client has already confirmed up to this lsn, we directly can
1668  * mark this as accepted. This can happen if we restart decoding in a
1669  * slot.
1670  */
1671  else if (current_lsn <= slot->data.confirmed_flush)
1672  {
1673  slot->candidate_catalog_xmin = xmin;
1674  slot->candidate_xmin_lsn = current_lsn;
1675 
1676  /* our candidate can directly be used */
1677  updated_xmin = true;
1678  }
1679 
1680  /*
1681  * Only increase if the previous values have been applied, otherwise we
1682  * might never end up updating if the receiver acks too slowly.
1683  */
1684  else if (slot->candidate_xmin_lsn == InvalidXLogRecPtr)
1685  {
1686  slot->candidate_catalog_xmin = xmin;
1687  slot->candidate_xmin_lsn = current_lsn;
1688 
1689  /*
1690  * Log new xmin at an appropriate log level after releasing the
1691  * spinlock.
1692  */
1693  got_new_xmin = true;
1694  }
1695  SpinLockRelease(&slot->mutex);
1696 
1697  if (got_new_xmin)
1698  elog(DEBUG1, "got new catalog xmin %u at %X/%X", xmin,
1699  LSN_FORMAT_ARGS(current_lsn));
1700 
1701  /* candidate already valid with the current flush position, apply */
1702  if (updated_xmin)
1704 }
bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:299

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

Referenced by SnapBuildProcessRunningXacts().

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

1203 {
1204  LogicalDecodingContext *ctx = cache->private_data;
1206  ErrorContextCallback errcallback;
1207 
1208  Assert(!ctx->fast_forward);
1209 
1210  if (ctx->callbacks.message_cb == NULL)
1211  return;
1212 
1213  /* Push callback + info on the error context stack */
1214  state.ctx = ctx;
1215  state.callback_name = "message";
1216  state.report_location = message_lsn;
1217  errcallback.callback = output_plugin_error_callback;
1218  errcallback.arg = (void *) &state;
1219  errcallback.previous = error_context_stack;
1220  error_context_stack = &errcallback;
1221 
1222  /* set output state */
1223  ctx->accept_writes = true;
1224  ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
1225  ctx->write_location = message_lsn;
1226  ctx->end_xact = false;
1227 
1228  /* do the actual work: call callback */
1229  ctx->callbacks.message_cb(ctx, txn, message_lsn, transactional, prefix,
1230  message_size, message);
1231 
1232  /* Pop the error context stack */
1233  error_context_stack = errcallback.previous;
1234 }
LogicalDecodeMessageCB message_cb

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

Referenced by StartupDecodingContext().

◆ output_plugin_error_callback()

static void output_plugin_error_callback ( void *  arg)
static

Definition at line 725 of file logical.c.

726 {
728 
729  /* not all callbacks have an associated LSN */
730  if (state->report_location != InvalidXLogRecPtr)
731  errcontext("slot \"%s\", output plugin \"%s\", in the %s callback, associated LSN %X/%X",
732  NameStr(state->ctx->slot->data.name),
733  NameStr(state->ctx->slot->data.plugin),
734  state->callback_name,
735  LSN_FORMAT_ARGS(state->report_location));
736  else
737  errcontext("slot \"%s\", output plugin \"%s\", in the %s callback",
738  NameStr(state->ctx->slot->data.name),
739  NameStr(state->ctx->slot->data.plugin),
740  state->callback_name);
741 }
#define errcontext
Definition: elog.h:196
void * arg

References arg, errcontext, InvalidXLogRecPtr, LSN_FORMAT_ARGS, and NameStr.

Referenced by begin_cb_wrapper(), begin_prepare_cb_wrapper(), change_cb_wrapper(), commit_cb_wrapper(), commit_prepared_cb_wrapper(), filter_by_origin_cb_wrapper(), filter_prepare_cb_wrapper(), message_cb_wrapper(), prepare_cb_wrapper(), rollback_prepared_cb_wrapper(), shutdown_cb_wrapper(), startup_cb_wrapper(), stream_abort_cb_wrapper(), stream_change_cb_wrapper(), stream_commit_cb_wrapper(), stream_message_cb_wrapper(), stream_prepare_cb_wrapper(), stream_start_cb_wrapper(), stream_stop_cb_wrapper(), stream_truncate_cb_wrapper(), truncate_cb_wrapper(), and update_progress_txn_cb_wrapper().

◆ OutputPluginPrepareWrite()

void OutputPluginPrepareWrite ( struct LogicalDecodingContext ctx,
bool  last_write 
)

Definition at line 662 of file logical.c.

663 {
664  if (!ctx->accept_writes)
665  elog(ERROR, "writes are only accepted in commit, begin and change callbacks");
666 
667  ctx->prepare_write(ctx, ctx->write_location, ctx->write_xid, last_write);
668  ctx->prepared_write = true;
669 }
LogicalOutputPluginWriterPrepareWrite prepare_write
Definition: logical.h:64

References LogicalDecodingContext::accept_writes, elog(), ERROR, LogicalDecodingContext::prepare_write, LogicalDecodingContext::prepared_write, LogicalDecodingContext::write_location, and LogicalDecodingContext::write_xid.

Referenced by pg_decode_change(), pg_decode_commit_prepared_txn(), pg_decode_commit_txn(), pg_decode_message(), pg_decode_prepare_txn(), pg_decode_rollback_prepared_txn(), pg_decode_stream_abort(), pg_decode_stream_change(), pg_decode_stream_commit(), pg_decode_stream_message(), pg_decode_stream_prepare(), pg_decode_stream_stop(), pg_decode_stream_truncate(), pg_decode_truncate(), pg_output_begin(), pg_output_stream_start(), pgoutput_begin_prepare_txn(), pgoutput_change(), pgoutput_commit_prepared_txn(), pgoutput_commit_txn(), pgoutput_message(), pgoutput_prepare_txn(), pgoutput_rollback_prepared_txn(), pgoutput_send_begin(), pgoutput_stream_abort(), pgoutput_stream_commit(), pgoutput_stream_prepare_txn(), pgoutput_stream_start(), pgoutput_stream_stop(), pgoutput_truncate(), send_relation_and_attrs(), and send_repl_origin().

◆ OutputPluginUpdateProgress()

void OutputPluginUpdateProgress ( struct LogicalDecodingContext ctx,
bool  skipped_xact 
)

◆ OutputPluginWrite()

◆ prepare_cb_wrapper()

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

Definition at line 919 of file logical.c.

921 {
922  LogicalDecodingContext *ctx = cache->private_data;
924  ErrorContextCallback errcallback;
925 
926  Assert(!ctx->fast_forward);
927 
928  /* We're only supposed to call this when two-phase commits are supported */
929  Assert(ctx->twophase);
930 
931  /* Push callback + info on the error context stack */
932  state.ctx = ctx;
933  state.callback_name = "prepare";
934  state.report_location = txn->final_lsn; /* beginning of prepare record */
936  errcallback.arg = (void *) &state;
937  errcallback.previous = error_context_stack;
938  error_context_stack = &errcallback;
939 
940  /* set output state */
941  ctx->accept_writes = true;
942  ctx->write_xid = txn->xid;
943  ctx->write_location = txn->end_lsn; /* points to the end of the record */
944  ctx->end_xact = true;
945 
946  /*
947  * If the plugin supports two-phase commits then prepare callback is
948  * mandatory
949  */
950  if (ctx->callbacks.prepare_cb == NULL)
951  ereport(ERROR,
952  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
953  errmsg("logical replication at prepare time requires a %s callback",
954  "prepare_cb")));
955 
956  /* do the actual work: call callback */
957  ctx->callbacks.prepare_cb(ctx, txn, prepare_lsn);
958 
959  /* Pop the error context stack */
960  error_context_stack = errcallback.previous;
961 }
LogicalDecodePrepareCB prepare_cb

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

Referenced by StartupDecodingContext().

◆ ResetLogicalStreamingState()

void ResetLogicalStreamingState ( void  )

Definition at line 1875 of file logical.c.

1876 {
1878  bsysscan = false;
1879 }
bool bsysscan
Definition: xact.c:100
TransactionId CheckXidAlive
Definition: xact.c:99

References bsysscan, CheckXidAlive, and InvalidTransactionId.

Referenced by AbortSubTransaction(), and AbortTransaction().

◆ rollback_prepared_cb_wrapper()

static void rollback_prepared_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn,
XLogRecPtr  prepare_end_lsn,
TimestampTz  prepare_time 
)
static

Definition at line 1009 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ shutdown_cb_wrapper()

static void shutdown_cb_wrapper ( LogicalDecodingContext ctx)
static

Definition at line 772 of file logical.c.

773 {
775  ErrorContextCallback errcallback;
776 
777  Assert(!ctx->fast_forward);
778 
779  /* Push callback + info on the error context stack */
780  state.ctx = ctx;
781  state.callback_name = "shutdown";
782  state.report_location = InvalidXLogRecPtr;
784  errcallback.arg = (void *) &state;
785  errcallback.previous = error_context_stack;
786  error_context_stack = &errcallback;
787 
788  /* set output state */
789  ctx->accept_writes = false;
790  ctx->end_xact = false;
791 
792  /* do the actual work: call callback */
793  ctx->callbacks.shutdown_cb(ctx);
794 
795  /* Pop the error context stack */
796  error_context_stack = errcallback.previous;
797 }

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

Referenced by FreeDecodingContext().

◆ startup_cb_wrapper()

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

Definition at line 744 of file logical.c.

745 {
747  ErrorContextCallback errcallback;
748 
749  Assert(!ctx->fast_forward);
750 
751  /* Push callback + info on the error context stack */
752  state.ctx = ctx;
753  state.callback_name = "startup";
754  state.report_location = InvalidXLogRecPtr;
756  errcallback.arg = (void *) &state;
757  errcallback.previous = error_context_stack;
758  error_context_stack = &errcallback;
759 
760  /* set output state */
761  ctx->accept_writes = false;
762  ctx->end_xact = false;
763 
764  /* do the actual work: call callback */
765  ctx->callbacks.startup_cb(ctx, opt, is_init);
766 
767  /* Pop the error context stack */
768  error_context_stack = errcallback.previous;
769 }

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

Referenced by CreateDecodingContext(), and CreateInitDecodingContext().

◆ StartupDecodingContext()

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

Definition at line 151 of file logical.c.

160 {
161  ReplicationSlot *slot;
162  MemoryContext context,
163  old_context;
165 
166  /* shorter lines... */
167  slot = MyReplicationSlot;
168 
170  "Logical decoding context",
172  old_context = MemoryContextSwitchTo(context);
173  ctx = palloc0(sizeof(LogicalDecodingContext));
174 
175  ctx->context = context;
176 
177  /*
178  * (re-)load output plugins, so we detect a bad (removed) output plugin
179  * now.
180  */
181  if (!fast_forward)
183 
184  /*
185  * Now that the slot's xmin has been set, we can announce ourselves as a
186  * logical decoding backend which doesn't need to be checked individually
187  * when computing the xmin horizon because the xmin is enforced via
188  * replication slots.
189  *
190  * We can only do so if we're outside of a transaction (i.e. the case when
191  * streaming changes via walsender), otherwise an already setup
192  * snapshot/xid would end up being ignored. That's not a particularly
193  * bothersome restriction since the SQL interface can't be used for
194  * streaming anyway.
195  */
197  {
198  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
201  LWLockRelease(ProcArrayLock);
202  }
203 
204  ctx->slot = slot;
205 
206  ctx->reader = XLogReaderAllocate(wal_segment_size, NULL, xl_routine, ctx);
207  if (!ctx->reader)
208  ereport(ERROR,
209  (errcode(ERRCODE_OUT_OF_MEMORY),
210  errmsg("out of memory"),
211  errdetail("Failed while allocating a WAL reading processor.")));
212 
214  ctx->snapshot_builder =
215  AllocateSnapshotBuilder(ctx->reorder, xmin_horizon, start_lsn,
216  need_full_snapshot, slot->data.two_phase_at);
217 
218  ctx->reorder->private_data = ctx;
219 
220  /* wrap output plugin callbacks, so we can add error context information */
226 
227  /*
228  * To support streaming, we require start/stop/abort/commit/change
229  * callbacks. The message and truncate callbacks are optional, similar to
230  * regular output plugins. We however enable streaming when at least one
231  * of the methods is enabled so that we can easily identify missing
232  * methods.
233  *
234  * We decide it here, but only check it later in the wrappers.
235  */
236  ctx->streaming = (ctx->callbacks.stream_start_cb != NULL) ||
237  (ctx->callbacks.stream_stop_cb != NULL) ||
238  (ctx->callbacks.stream_abort_cb != NULL) ||
239  (ctx->callbacks.stream_commit_cb != NULL) ||
240  (ctx->callbacks.stream_change_cb != NULL) ||
241  (ctx->callbacks.stream_message_cb != NULL) ||
242  (ctx->callbacks.stream_truncate_cb != NULL);
243 
244  /*
245  * streaming callbacks
246  *
247  * stream_message and stream_truncate callbacks are optional, so we do not
248  * fail with ERROR when missing, but the wrappers simply do nothing. We
249  * must set the ReorderBuffer callbacks to something, otherwise the calls
250  * from there will crash (we don't want to move the checks there).
251  */
260 
261 
262  /*
263  * To support two-phase logical decoding, we require
264  * begin_prepare/prepare/commit-prepare/abort-prepare callbacks. The
265  * filter_prepare callback is optional. We however enable two-phase
266  * logical decoding when at least one of the methods is enabled so that we
267  * can easily identify missing methods.
268  *
269  * We decide it here, but only check it later in the wrappers.
270  */
271  ctx->twophase = (ctx->callbacks.begin_prepare_cb != NULL) ||
272  (ctx->callbacks.prepare_cb != NULL) ||
273  (ctx->callbacks.commit_prepared_cb != NULL) ||
274  (ctx->callbacks.rollback_prepared_cb != NULL) ||
275  (ctx->callbacks.stream_prepare_cb != NULL) ||
276  (ctx->callbacks.filter_prepare_cb != NULL);
277 
278  /*
279  * Callback to support decoding at prepare time.
280  */
285 
286  /*
287  * Callback to support updating progress during sending data of a
288  * transaction (and its subtransactions) to the output plugin.
289  */
291 
292  ctx->out = makeStringInfo();
293  ctx->prepare_write = prepare_write;
294  ctx->write = do_write;
295  ctx->update_progress = update_progress;
296 
297  ctx->output_plugin_options = output_plugin_options;
298 
299  ctx->fast_forward = fast_forward;
300 
301  MemoryContextSwitchTo(old_context);
302 
303  return ctx;
304 }
static void change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
Definition: logical.c:1056
static void commit_prepared_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:964
static void update_progress_txn_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr lsn)
Definition: logical.c:1599
static void stream_start_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr first_lsn)
Definition: logical.c:1237
static void commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:836
static void begin_prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
Definition: logical.c:875
static void stream_prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
Definition: logical.c:1376
static void prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
Definition: logical.c:919
static void stream_truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
Definition: logical.c:1552
static void truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
Definition: logical.c:1095
static void begin_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
Definition: logical.c:805
static void rollback_prepared_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_end_lsn, TimestampTz prepare_time)
Definition: logical.c:1009
static void stream_change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
Definition: logical.c:1462
static void stream_abort_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr abort_lsn)
Definition: logical.c:1335
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:1511
static void stream_commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:1421
static void LoadOutputPlugin(OutputPluginCallbacks *callbacks, const char *plugin)
Definition: logical.c:703
static void stream_stop_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr last_lsn)
Definition: logical.c:1286
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:1200
void * palloc0(Size size)
Definition: mcxt.c:1241
MemoryContext CurrentMemoryContext
Definition: mcxt.c:135
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:153
#define PROC_IN_LOGICAL_DECODING
Definition: proc.h:60
ReorderBuffer * ReorderBufferAllocate(void)
SnapBuild * AllocateSnapshotBuilder(ReorderBuffer *reorder, TransactionId xmin_horizon, XLogRecPtr start_lsn, bool need_full_snapshot, XLogRecPtr two_phase_at)
Definition: snapbuild.c:312
PGPROC * MyProc
Definition: proc.c:66
PROC_HDR * ProcGlobal
Definition: proc.c:78
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
StringInfo out
Definition: logical.h:71
List * output_plugin_options
Definition: logical.h:59
LogicalDecodeStreamChangeCB stream_change_cb
LogicalDecodeStreamTruncateCB stream_truncate_cb
LogicalDecodeStreamMessageCB stream_message_cb
LogicalDecodeStreamStopCB stream_stop_cb
LogicalDecodeStreamCommitCB stream_commit_cb
LogicalDecodeStreamPrepareCB stream_prepare_cb
LogicalDecodeStreamStartCB stream_start_cb
LogicalDecodeStreamAbortCB stream_abort_cb
uint8 statusFlags
Definition: proc.h:233
int pgxactoff
Definition: proc.h:188
uint8 * statusFlags
Definition: proc.h:377
ReorderBufferStreamMessageCB stream_message
ReorderBufferStreamChangeCB stream_change
ReorderBufferBeginCB begin_prepare
ReorderBufferStreamTruncateCB stream_truncate
ReorderBufferCommitPreparedCB commit_prepared
ReorderBufferUpdateProgressTxnCB update_progress_txn
ReorderBufferMessageCB message
ReorderBufferRollbackPreparedCB rollback_prepared
ReorderBufferPrepareCB prepare
ReorderBufferStreamStopCB stream_stop
ReorderBufferApplyChangeCB apply_change
ReorderBufferStreamPrepareCB stream_prepare
ReorderBufferStreamAbortCB stream_abort
ReorderBufferCommitCB commit
ReorderBufferStreamStartCB stream_start
ReorderBufferStreamCommitCB stream_commit
ReorderBufferApplyTruncateCB apply_truncate
ReorderBufferBeginCB begin
bool IsTransactionOrTransactionBlock(void)
Definition: xact.c:4841
int wal_segment_size
Definition: xlog.c:146
XLogReaderState * XLogReaderAllocate(int wal_segment_size, const char *waldir, XLogReaderRoutine *routine, void *private_data)
Definition: xlogreader.c:108

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

Referenced by CreateDecodingContext(), and CreateInitDecodingContext().

◆ stream_abort_cb_wrapper()

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

Definition at line 1335 of file logical.c.

1337 {
1338  LogicalDecodingContext *ctx = cache->private_data;
1340  ErrorContextCallback errcallback;
1341 
1342  Assert(!ctx->fast_forward);
1343 
1344  /* We're only supposed to call this when streaming is supported. */
1345  Assert(ctx->streaming);
1346 
1347  /* Push callback + info on the error context stack */
1348  state.ctx = ctx;
1349  state.callback_name = "stream_abort";
1350  state.report_location = abort_lsn;
1351  errcallback.callback = output_plugin_error_callback;
1352  errcallback.arg = (void *) &state;
1353  errcallback.previous = error_context_stack;
1354  error_context_stack = &errcallback;
1355 
1356  /* set output state */
1357  ctx->accept_writes = true;
1358  ctx->write_xid = txn->xid;
1359  ctx->write_location = abort_lsn;
1360  ctx->end_xact = true;
1361 
1362  /* in streaming mode, stream_abort_cb is required */
1363  if (ctx->callbacks.stream_abort_cb == NULL)
1364  ereport(ERROR,
1365  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1366  errmsg("logical streaming requires a %s callback",
1367  "stream_abort_cb")));
1368 
1369  ctx->callbacks.stream_abort_cb(ctx, txn, abort_lsn);
1370 
1371  /* Pop the error context stack */
1372  error_context_stack = errcallback.previous;
1373 }

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

Referenced by StartupDecodingContext().

◆ stream_change_cb_wrapper()

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

Definition at line 1462 of file logical.c.

1464 {
1465  LogicalDecodingContext *ctx = cache->private_data;
1467  ErrorContextCallback errcallback;
1468 
1469  Assert(!ctx->fast_forward);
1470 
1471  /* We're only supposed to call this when streaming is supported. */
1472  Assert(ctx->streaming);
1473 
1474  /* Push callback + info on the error context stack */
1475  state.ctx = ctx;
1476  state.callback_name = "stream_change";
1477  state.report_location = change->lsn;
1478  errcallback.callback = output_plugin_error_callback;
1479  errcallback.arg = (void *) &state;
1480  errcallback.previous = error_context_stack;
1481  error_context_stack = &errcallback;
1482 
1483  /* set output state */
1484  ctx->accept_writes = true;
1485  ctx->write_xid = txn->xid;
1486 
1487  /*
1488  * Report this change's lsn so replies from clients can give an up-to-date
1489  * answer. This won't ever be enough (and shouldn't be!) to confirm
1490  * receipt of this transaction, but it might allow another transaction's
1491  * commit to be confirmed with one message.
1492  */
1493  ctx->write_location = change->lsn;
1494 
1495  ctx->end_xact = false;
1496 
1497  /* in streaming mode, stream_change_cb is required */
1498  if (ctx->callbacks.stream_change_cb == NULL)
1499  ereport(ERROR,
1500  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1501  errmsg("logical streaming requires a %s callback",
1502  "stream_change_cb")));
1503 
1504  ctx->callbacks.stream_change_cb(ctx, txn, relation, change);
1505 
1506  /* Pop the error context stack */
1507  error_context_stack = errcallback.previous;
1508 }

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

Referenced by StartupDecodingContext().

◆ stream_commit_cb_wrapper()

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

Definition at line 1421 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ stream_message_cb_wrapper()

static void stream_message_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn,
XLogRecPtr  message_lsn,
bool  transactional,
const char *  prefix,
Size  message_size,
const char *  message 
)
static

Definition at line 1511 of file logical.c.

1514 {
1515  LogicalDecodingContext *ctx = cache->private_data;
1517  ErrorContextCallback errcallback;
1518 
1519  Assert(!ctx->fast_forward);
1520 
1521  /* We're only supposed to call this when streaming is supported. */
1522  Assert(ctx->streaming);
1523 
1524  /* this callback is optional */
1525  if (ctx->callbacks.stream_message_cb == NULL)
1526  return;
1527 
1528  /* Push callback + info on the error context stack */
1529  state.ctx = ctx;
1530  state.callback_name = "stream_message";
1531  state.report_location = message_lsn;
1532  errcallback.callback = output_plugin_error_callback;
1533  errcallback.arg = (void *) &state;
1534  errcallback.previous = error_context_stack;
1535  error_context_stack = &errcallback;
1536 
1537  /* set output state */
1538  ctx->accept_writes = true;
1539  ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
1540  ctx->write_location = message_lsn;
1541  ctx->end_xact = false;
1542 
1543  /* do the actual work: call callback */
1544  ctx->callbacks.stream_message_cb(ctx, txn, message_lsn, transactional, prefix,
1545  message_size, message);
1546 
1547  /* Pop the error context stack */
1548  error_context_stack = errcallback.previous;
1549 }

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

Referenced by StartupDecodingContext().

◆ stream_prepare_cb_wrapper()

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

Definition at line 1376 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ stream_start_cb_wrapper()

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

Definition at line 1237 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ stream_stop_cb_wrapper()

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

Definition at line 1286 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ stream_truncate_cb_wrapper()

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

Definition at line 1552 of file logical.c.

1555 {
1556  LogicalDecodingContext *ctx = cache->private_data;
1558  ErrorContextCallback errcallback;
1559 
1560  Assert(!ctx->fast_forward);
1561 
1562  /* We're only supposed to call this when streaming is supported. */
1563  Assert(ctx->streaming);
1564 
1565  /* this callback is optional */
1566  if (!ctx->callbacks.stream_truncate_cb)
1567  return;
1568 
1569  /* Push callback + info on the error context stack */
1570  state.ctx = ctx;
1571  state.callback_name = "stream_truncate";
1572  state.report_location = change->lsn;
1573  errcallback.callback = output_plugin_error_callback;
1574  errcallback.arg = (void *) &state;
1575  errcallback.previous = error_context_stack;
1576  error_context_stack = &errcallback;
1577 
1578  /* set output state */
1579  ctx->accept_writes = true;
1580  ctx->write_xid = txn->xid;
1581 
1582  /*
1583  * Report this change's lsn so replies from clients can give an up-to-date
1584  * answer. This won't ever be enough (and shouldn't be!) to confirm
1585  * receipt of this transaction, but it might allow another transaction's
1586  * commit to be confirmed with one message.
1587  */
1588  ctx->write_location = change->lsn;
1589 
1590  ctx->end_xact = false;
1591 
1592  ctx->callbacks.stream_truncate_cb(ctx, txn, nrelations, relations, change);
1593 
1594  /* Pop the error context stack */
1595  error_context_stack = errcallback.previous;
1596 }

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

Referenced by StartupDecodingContext().

◆ truncate_cb_wrapper()

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

Definition at line 1095 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ update_progress_txn_cb_wrapper()

static void update_progress_txn_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn,
XLogRecPtr  lsn 
)
static

Definition at line 1599 of file logical.c.

1601 {
1602  LogicalDecodingContext *ctx = cache->private_data;
1604  ErrorContextCallback errcallback;
1605 
1606  Assert(!ctx->fast_forward);
1607 
1608  /* Push callback + info on the error context stack */
1609  state.ctx = ctx;
1610  state.callback_name = "update_progress_txn";
1611  state.report_location = lsn;
1612  errcallback.callback = output_plugin_error_callback;
1613  errcallback.arg = (void *) &state;
1614  errcallback.previous = error_context_stack;
1615  error_context_stack = &errcallback;
1616 
1617  /* set output state */
1618  ctx->accept_writes = false;
1619  ctx->write_xid = txn->xid;
1620 
1621  /*
1622  * Report this change's lsn so replies from clients can give an up-to-date
1623  * answer. This won't ever be enough (and shouldn't be!) to confirm
1624  * receipt of this transaction, but it might allow another transaction's
1625  * commit to be confirmed with one message.
1626  */
1627  ctx->write_location = lsn;
1628 
1629  ctx->end_xact = false;
1630 
1631  OutputPluginUpdateProgress(ctx, false);
1632 
1633  /* Pop the error context stack */
1634  error_context_stack = errcallback.previous;
1635 }
void OutputPluginUpdateProgress(struct LogicalDecodingContext *ctx, bool skipped_xact)
Definition: logical.c:688

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert(), ErrorContextCallback::callback, LogicalDecodingContext::end_xact, error_context_stack, LogicalDecodingContext::fast_forward, output_plugin_error_callback(), OutputPluginUpdateProgress(), ErrorContextCallback::previous, ReorderBuffer::private_data, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ UpdateDecodingStats()

void UpdateDecodingStats ( LogicalDecodingContext ctx)

Definition at line 1885 of file logical.c.

1886 {
1887  ReorderBuffer *rb = ctx->reorder;
1888  PgStat_StatReplSlotEntry repSlotStat;
1889 
1890  /* Nothing to do if we don't have any replication stats to be sent. */
1891  if (rb->spillBytes <= 0 && rb->streamBytes <= 0 && rb->totalBytes <= 0)
1892  return;
1893 
1894  elog(DEBUG2, "UpdateDecodingStats: updating stats %p %lld %lld %lld %lld %lld %lld %lld %lld",
1895  rb,
1896  (long long) rb->spillTxns,
1897  (long long) rb->spillCount,
1898  (long long) rb->spillBytes,
1899  (long long) rb->streamTxns,
1900  (long long) rb->streamCount,
1901  (long long) rb->streamBytes,
1902  (long long) rb->totalTxns,
1903  (long long) rb->totalBytes);
1904 
1905  repSlotStat.spill_txns = rb->spillTxns;
1906  repSlotStat.spill_count = rb->spillCount;
1907  repSlotStat.spill_bytes = rb->spillBytes;
1908  repSlotStat.stream_txns = rb->streamTxns;
1909  repSlotStat.stream_count = rb->streamCount;
1910  repSlotStat.stream_bytes = rb->streamBytes;
1911  repSlotStat.total_txns = rb->totalTxns;
1912  repSlotStat.total_bytes = rb->totalBytes;
1913 
1914  pgstat_report_replslot(ctx->slot, &repSlotStat);
1915 
1916  rb->spillTxns = 0;
1917  rb->spillCount = 0;
1918  rb->spillBytes = 0;
1919  rb->streamTxns = 0;
1920  rb->streamCount = 0;
1921  rb->streamBytes = 0;
1922  rb->totalTxns = 0;
1923  rb->totalBytes = 0;
1924 }
#define DEBUG2
Definition: elog.h:29
void pgstat_report_replslot(ReplicationSlot *slot, const PgStat_StatReplSlotEntry *repSlotStat)
PgStat_Counter stream_count
Definition: pgstat.h:366
PgStat_Counter total_txns
Definition: pgstat.h:368
PgStat_Counter total_bytes
Definition: pgstat.h:369
PgStat_Counter spill_txns
Definition: pgstat.h:362
PgStat_Counter stream_txns
Definition: pgstat.h:365
PgStat_Counter spill_count
Definition: pgstat.h:363
PgStat_Counter stream_bytes
Definition: pgstat.h:367
PgStat_Counter spill_bytes
Definition: pgstat.h:364

References DEBUG2, elog(), pgstat_report_replslot(), LogicalDecodingContext::reorder, LogicalDecodingContext::slot, PgStat_StatReplSlotEntry::spill_bytes, PgStat_StatReplSlotEntry::spill_count, PgStat_StatReplSlotEntry::spill_txns, ReorderBuffer::spillBytes, ReorderBuffer::spillCount, ReorderBuffer::spillTxns, PgStat_StatReplSlotEntry::stream_bytes, PgStat_StatReplSlotEntry::stream_count, PgStat_StatReplSlotEntry::stream_txns, ReorderBuffer::streamBytes, ReorderBuffer::streamCount, ReorderBuffer::streamTxns, PgStat_StatReplSlotEntry::total_bytes, PgStat_StatReplSlotEntry::total_txns, ReorderBuffer::totalBytes, and ReorderBuffer::totalTxns.

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