PostgreSQL Source Code  git master
logical.h File Reference
Include dependency graph for logical.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  LogicalDecodingContext
 

Typedefs

typedef void(* LogicalOutputPluginWriterWrite) (struct LogicalDecodingContext *lr, XLogRecPtr Ptr, TransactionId xid, bool last_write)
 
typedef LogicalOutputPluginWriterWrite LogicalOutputPluginWriterPrepareWrite
 
typedef void(* LogicalOutputPluginWriterUpdateProgress) (struct LogicalDecodingContext *lr, XLogRecPtr Ptr, TransactionId xid, bool skipped_xact)
 
typedef struct LogicalDecodingContext LogicalDecodingContext
 

Functions

void CheckLogicalDecodingRequirements (void)
 
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)
 
void DecodingContextFindStartpoint (LogicalDecodingContext *ctx)
 
bool DecodingContextReady (LogicalDecodingContext *ctx)
 
void FreeDecodingContext (LogicalDecodingContext *ctx)
 
void LogicalIncreaseXminForSlot (XLogRecPtr current_lsn, TransactionId xmin)
 
void LogicalIncreaseRestartDecodingForSlot (XLogRecPtr current_lsn, XLogRecPtr restart_lsn)
 
void LogicalConfirmReceivedLocation (XLogRecPtr lsn)
 
bool filter_prepare_cb_wrapper (LogicalDecodingContext *ctx, TransactionId xid, const char *gid)
 
bool filter_by_origin_cb_wrapper (LogicalDecodingContext *ctx, RepOriginId origin_id)
 
void ResetLogicalStreamingState (void)
 
void UpdateDecodingStats (LogicalDecodingContext *ctx)
 

Typedef Documentation

◆ LogicalDecodingContext

◆ LogicalOutputPluginWriterPrepareWrite

◆ LogicalOutputPluginWriterUpdateProgress

typedef void(* LogicalOutputPluginWriterUpdateProgress) (struct LogicalDecodingContext *lr, XLogRecPtr Ptr, TransactionId xid, bool skipped_xact)

Definition at line 27 of file logical.h.

◆ LogicalOutputPluginWriterWrite

typedef void(* LogicalOutputPluginWriterWrite) (struct LogicalDecodingContext *lr, XLogRecPtr Ptr, TransactionId xid, bool last_write)

Definition at line 19 of file logical.h.

Function Documentation

◆ CheckLogicalDecodingRequirements()

void CheckLogicalDecodingRequirements ( void  )

Definition at line 103 of file logical.c.

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

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

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

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

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

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

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

582 {
584 }
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 1156 of file logical.c.

1157 {
1159  ErrorContextCallback errcallback;
1160  bool ret;
1161 
1162  Assert(!ctx->fast_forward);
1163 
1164  /* Push callback + info on the error context stack */
1165  state.ctx = ctx;
1166  state.callback_name = "filter_by_origin";
1167  state.report_location = InvalidXLogRecPtr;
1168  errcallback.callback = output_plugin_error_callback;
1169  errcallback.arg = (void *) &state;
1170  errcallback.previous = error_context_stack;
1171  error_context_stack = &errcallback;
1172 
1173  /* set output state */
1174  ctx->accept_writes = false;
1175  ctx->end_xact = false;
1176 
1177  /* do the actual work: call callback */
1178  ret = ctx->callbacks.filter_by_origin_cb(ctx, origin_id);
1179 
1180  /* Pop the error context stack */
1181  error_context_stack = errcallback.previous;
1182 
1183  return ret;
1184 }
ErrorContextCallback * error_context_stack
Definition: elog.c:94
Assert(fmt[strlen(fmt) - 1] !='\n')
static void output_plugin_error_callback(void *arg)
Definition: logical.c:712
struct ErrorContextCallback * previous
Definition: elog.h:234
void(* callback)(void *arg)
Definition: elog.h:235
LogicalDecodeFilterByOriginCB filter_by_origin_cb
Definition: regguts.h:318

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

1126 {
1128  ErrorContextCallback errcallback;
1129  bool ret;
1130 
1131  Assert(!ctx->fast_forward);
1132 
1133  /* Push callback + info on the error context stack */
1134  state.ctx = ctx;
1135  state.callback_name = "filter_prepare";
1136  state.report_location = InvalidXLogRecPtr;
1137  errcallback.callback = output_plugin_error_callback;
1138  errcallback.arg = (void *) &state;
1139  errcallback.previous = error_context_stack;
1140  error_context_stack = &errcallback;
1141 
1142  /* set output state */
1143  ctx->accept_writes = false;
1144  ctx->end_xact = false;
1145 
1146  /* do the actual work: call callback */
1147  ret = ctx->callbacks.filter_prepare_cb(ctx, xid, gid);
1148 
1149  /* Pop the error context stack */
1150  error_context_stack = errcallback.previous;
1151 
1152  return ret;
1153 }
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 634 of file logical.c.

635 {
636  if (ctx->callbacks.shutdown_cb != NULL)
637  shutdown_cb_wrapper(ctx);
638 
641  XLogReaderFree(ctx->reader);
643 }
static void shutdown_cb_wrapper(LogicalDecodingContext *ctx)
Definition: logical.c:759
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:376
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().

◆ LogicalConfirmReceivedLocation()

void LogicalConfirmReceivedLocation ( XLogRecPtr  lsn)

Definition at line 1736 of file logical.c.

1737 {
1738  Assert(lsn != InvalidXLogRecPtr);
1739 
1740  /* Do an unlocked check for candidate_lsn first. */
1743  {
1744  bool updated_xmin = false;
1745  bool updated_restart = false;
1746 
1748 
1750 
1751  /* if we're past the location required for bumping xmin, do so */
1754  {
1755  /*
1756  * We have to write the changed xmin to disk *before* we change
1757  * the in-memory value, otherwise after a crash we wouldn't know
1758  * that some catalog tuples might have been removed already.
1759  *
1760  * Ensure that by first writing to ->xmin and only update
1761  * ->effective_xmin once the new state is synced to disk. After a
1762  * crash ->effective_xmin is set to ->xmin.
1763  */
1766  {
1770  updated_xmin = true;
1771  }
1772  }
1773 
1776  {
1778 
1782  updated_restart = true;
1783  }
1784 
1786 
1787  /* first write new xmin to disk, so we know what's up after a crash */
1788  if (updated_xmin || updated_restart)
1789  {
1792  elog(DEBUG1, "updated xmin: %u restart: %u", updated_xmin, updated_restart);
1793  }
1794 
1795  /*
1796  * Now the new xmin is safely on disk, we can let the global value
1797  * advance. We do not take ProcArrayLock or similar since we only
1798  * advance xmin here and there's not much harm done by a concurrent
1799  * computation missing that.
1800  */
1801  if (updated_xmin)
1802  {
1806 
1809  }
1810  }
1811  else
1812  {
1816  }
1817 }
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 1662 of file logical.c.

1663 {
1664  bool updated_lsn = false;
1665  ReplicationSlot *slot;
1666 
1667  slot = MyReplicationSlot;
1668 
1669  Assert(slot != NULL);
1670  Assert(restart_lsn != InvalidXLogRecPtr);
1671  Assert(current_lsn != InvalidXLogRecPtr);
1672 
1673  SpinLockAcquire(&slot->mutex);
1674 
1675  /* don't overwrite if have a newer restart lsn */
1676  if (restart_lsn <= slot->data.restart_lsn)
1677  {
1678  }
1679 
1680  /*
1681  * We might have already flushed far enough to directly accept this lsn,
1682  * in this case there is no need to check for existing candidate LSNs
1683  */
1684  else if (current_lsn <= slot->data.confirmed_flush)
1685  {
1686  slot->candidate_restart_valid = current_lsn;
1687  slot->candidate_restart_lsn = restart_lsn;
1688 
1689  /* our candidate can directly be used */
1690  updated_lsn = true;
1691  }
1692 
1693  /*
1694  * Only increase if the previous values have been applied, otherwise we
1695  * might never end up updating if the receiver acks too slowly. A missed
1696  * value here will just cause some extra effort after reconnecting.
1697  */
1699  {
1700  slot->candidate_restart_valid = current_lsn;
1701  slot->candidate_restart_lsn = restart_lsn;
1702  SpinLockRelease(&slot->mutex);
1703 
1704  elog(DEBUG1, "got new restart lsn %X/%X at %X/%X",
1705  LSN_FORMAT_ARGS(restart_lsn),
1706  LSN_FORMAT_ARGS(current_lsn));
1707  }
1708  else
1709  {
1710  XLogRecPtr candidate_restart_lsn;
1711  XLogRecPtr candidate_restart_valid;
1712  XLogRecPtr confirmed_flush;
1713 
1714  candidate_restart_lsn = slot->candidate_restart_lsn;
1715  candidate_restart_valid = slot->candidate_restart_valid;
1716  confirmed_flush = slot->data.confirmed_flush;
1717  SpinLockRelease(&slot->mutex);
1718 
1719  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",
1720  LSN_FORMAT_ARGS(restart_lsn),
1721  LSN_FORMAT_ARGS(current_lsn),
1722  LSN_FORMAT_ARGS(candidate_restart_lsn),
1723  LSN_FORMAT_ARGS(candidate_restart_valid),
1724  LSN_FORMAT_ARGS(confirmed_flush));
1725  }
1726 
1727  /* candidates are already valid with the current flush position, apply */
1728  if (updated_lsn)
1730 }
void LogicalConfirmReceivedLocation(XLogRecPtr lsn)
Definition: logical.c:1736
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 1594 of file logical.c.

1595 {
1596  bool updated_xmin = false;
1597  ReplicationSlot *slot;
1598  bool got_new_xmin = false;
1599 
1600  slot = MyReplicationSlot;
1601 
1602  Assert(slot != NULL);
1603 
1604  SpinLockAcquire(&slot->mutex);
1605 
1606  /*
1607  * don't overwrite if we already have a newer xmin. This can happen if we
1608  * restart decoding in a slot.
1609  */
1611  {
1612  }
1613 
1614  /*
1615  * If the client has already confirmed up to this lsn, we directly can
1616  * mark this as accepted. This can happen if we restart decoding in a
1617  * slot.
1618  */
1619  else if (current_lsn <= slot->data.confirmed_flush)
1620  {
1621  slot->candidate_catalog_xmin = xmin;
1622  slot->candidate_xmin_lsn = current_lsn;
1623 
1624  /* our candidate can directly be used */
1625  updated_xmin = true;
1626  }
1627 
1628  /*
1629  * Only increase if the previous values have been applied, otherwise we
1630  * might never end up updating if the receiver acks too slowly.
1631  */
1632  else if (slot->candidate_xmin_lsn == InvalidXLogRecPtr)
1633  {
1634  slot->candidate_catalog_xmin = xmin;
1635  slot->candidate_xmin_lsn = current_lsn;
1636 
1637  /*
1638  * Log new xmin at an appropriate log level after releasing the
1639  * spinlock.
1640  */
1641  got_new_xmin = true;
1642  }
1643  SpinLockRelease(&slot->mutex);
1644 
1645  if (got_new_xmin)
1646  elog(DEBUG1, "got new catalog xmin %u at %X/%X", xmin,
1647  LSN_FORMAT_ARGS(current_lsn));
1648 
1649  /* candidate already valid with the current flush position, apply */
1650  if (updated_xmin)
1652 }
bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:292

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

◆ ResetLogicalStreamingState()

void ResetLogicalStreamingState ( void  )

Definition at line 1823 of file logical.c.

1824 {
1826  bsysscan = false;
1827 }
bool bsysscan
Definition: xact.c:99
TransactionId CheckXidAlive
Definition: xact.c:98

References bsysscan, CheckXidAlive, and InvalidTransactionId.

Referenced by AbortSubTransaction(), and AbortTransaction().

◆ UpdateDecodingStats()

void UpdateDecodingStats ( LogicalDecodingContext ctx)

Definition at line 1833 of file logical.c.

1834 {
1835  ReorderBuffer *rb = ctx->reorder;
1836  PgStat_StatReplSlotEntry repSlotStat;
1837 
1838  /* Nothing to do if we don't have any replication stats to be sent. */
1839  if (rb->spillBytes <= 0 && rb->streamBytes <= 0 && rb->totalBytes <= 0)
1840  return;
1841 
1842  elog(DEBUG2, "UpdateDecodingStats: updating stats %p %lld %lld %lld %lld %lld %lld %lld %lld",
1843  rb,
1844  (long long) rb->spillTxns,
1845  (long long) rb->spillCount,
1846  (long long) rb->spillBytes,
1847  (long long) rb->streamTxns,
1848  (long long) rb->streamCount,
1849  (long long) rb->streamBytes,
1850  (long long) rb->totalTxns,
1851  (long long) rb->totalBytes);
1852 
1853  repSlotStat.spill_txns = rb->spillTxns;
1854  repSlotStat.spill_count = rb->spillCount;
1855  repSlotStat.spill_bytes = rb->spillBytes;
1856  repSlotStat.stream_txns = rb->streamTxns;
1857  repSlotStat.stream_count = rb->streamCount;
1858  repSlotStat.stream_bytes = rb->streamBytes;
1859  repSlotStat.total_txns = rb->totalTxns;
1860  repSlotStat.total_bytes = rb->totalBytes;
1861 
1862  pgstat_report_replslot(ctx->slot, &repSlotStat);
1863 
1864  rb->spillTxns = 0;
1865  rb->spillCount = 0;
1866  rb->spillBytes = 0;
1867  rb->streamTxns = 0;
1868  rb->streamCount = 0;
1869  rb->streamBytes = 0;
1870  rb->totalTxns = 0;
1871  rb->totalBytes = 0;
1872 }
#define DEBUG2
Definition: elog.h:25
void pgstat_report_replslot(ReplicationSlot *slot, const PgStat_StatReplSlotEntry *repSlotStat)
PgStat_Counter stream_count
Definition: pgstat.h:328
PgStat_Counter total_txns
Definition: pgstat.h:330
PgStat_Counter total_bytes
Definition: pgstat.h:331
PgStat_Counter spill_txns
Definition: pgstat.h:324
PgStat_Counter stream_txns
Definition: pgstat.h:327
PgStat_Counter spill_count
Definition: pgstat.h:325
PgStat_Counter stream_bytes
Definition: pgstat.h:329
PgStat_Counter spill_bytes
Definition: pgstat.h:326

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