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

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.

References CheckSlotRequirements(), ereport, errcode(), errmsg(), ERROR, InvalidOid, MyDatabaseId, RecoveryInProgress(), wal_level, and WAL_LEVEL_LOGICAL.

Referenced by copy_replication_slot(), CreateReplicationSlot(), pg_create_logical_replication_slot(), pg_logical_slot_get_changes_guts(), and StartLogicalReplication().

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

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

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

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

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

◆ CreateInitDecodingContext()

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

Definition at line 318 of file logical.c.

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

Referenced by create_logical_replication_slot(), and CreateReplicationSlot().

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

◆ DecodingContextFindStartpoint()

void DecodingContextFindStartpoint ( LogicalDecodingContext ctx)

Definition at line 571 of file logical.c.

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

Referenced by create_logical_replication_slot(), and CreateReplicationSlot().

572 {
573  ReplicationSlot *slot = ctx->slot;
574 
575  /* Initialize from where to start reading WAL. */
576  XLogBeginRead(ctx->reader, slot->data.restart_lsn);
577 
578  elog(DEBUG1, "searching for logical decoding starting point, starting at %X/%X",
580 
581  /* Wait for a consistent starting point */
582  for (;;)
583  {
584  XLogRecord *record;
585  char *err = NULL;
586 
587  /* the read_page callback waits for new WAL */
588  record = XLogReadRecord(ctx->reader, &err);
589  if (err)
590  elog(ERROR, "%s", err);
591  if (!record)
592  elog(ERROR, "no record found"); /* shouldn't happen */
593 
595 
596  /* only continue till we found a consistent spot */
597  if (DecodingContextReady(ctx))
598  break;
599 
601  }
602 
603  SpinLockAcquire(&slot->mutex);
604  slot->data.confirmed_flush = ctx->reader->EndRecPtr;
606  SpinLockRelease(&slot->mutex);
607 }
#define DEBUG1
Definition: elog.h:25
bool DecodingContextReady(LogicalDecodingContext *ctx)
Definition: logical.c:562
ReplicationSlotPersistentData data
Definition: slot.h:156
XLogRecPtr confirmed_flush
Definition: slot.h:92
XLogRecPtr EndRecPtr
Definition: xlogreader.h:176
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
XLogRecord * XLogReadRecord(XLogReaderState *state, char **errormsg)
Definition: xlogreader.c:268
#define ERROR
Definition: elog.h:46
void LogicalDecodingProcessRecord(LogicalDecodingContext *ctx, XLogReaderState *record)
Definition: decode.c:106
void XLogBeginRead(XLogReaderState *state, XLogRecPtr RecPtr)
Definition: xlogreader.c:240
XLogRecPtr initial_consistent_point
Definition: slot.h:99
#define SpinLockRelease(lock)
Definition: spin.h:64
XLogRecPtr restart_lsn
Definition: slot.h:81
XLogReaderState * reader
Definition: logical.h:41
ReplicationSlot * slot
Definition: logical.h:38
#define elog(elevel,...)
Definition: elog.h:232
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:102
slock_t mutex
Definition: slot.h:129

◆ DecodingContextReady()

bool DecodingContextReady ( LogicalDecodingContext ctx)

Definition at line 562 of file logical.c.

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

Referenced by DecodingContextFindStartpoint().

563 {
565 }
SnapBuildState SnapBuildCurrentState(SnapBuild *builder)
Definition: snapbuild.c:367
struct SnapBuild * snapshot_builder
Definition: logical.h:43

◆ filter_by_origin_cb_wrapper()

bool filter_by_origin_cb_wrapper ( LogicalDecodingContext ctx,
RepOriginId  origin_id 
)

Definition at line 1117 of file logical.c.

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

Referenced by FilterByOrigin().

1118 {
1120  ErrorContextCallback errcallback;
1121  bool ret;
1122 
1123  Assert(!ctx->fast_forward);
1124 
1125  /* Push callback + info on the error context stack */
1126  state.ctx = ctx;
1127  state.callback_name = "filter_by_origin";
1129  errcallback.callback = output_plugin_error_callback;
1130  errcallback.arg = (void *) &state;
1131  errcallback.previous = error_context_stack;
1132  error_context_stack = &errcallback;
1133 
1134  /* set output state */
1135  ctx->accept_writes = false;
1136 
1137  /* do the actual work: call callback */
1138  ret = ctx->callbacks.filter_by_origin_cb(ctx, origin_id);
1139 
1140  /* Pop the error context stack */
1141  error_context_stack = errcallback.previous;
1142 
1143  return ret;
1144 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:52
void(* callback)(void *arg)
Definition: elog.h:247
struct ErrorContextCallback * previous
Definition: elog.h:246
const char * callback_name
Definition: logical.c:50
ErrorContextCallback * error_context_stack
Definition: elog.c:93
#define Assert(condition)
Definition: c.h:804
Definition: regguts.h:317
XLogRecPtr report_location
Definition: logical.c:51
LogicalDecodingContext * ctx
Definition: logical.c:49
static void output_plugin_error_callback(void *arg)
Definition: logical.c:690
LogicalDecodeFilterByOriginCB filter_by_origin_cb

◆ filter_prepare_cb_wrapper()

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

Definition at line 1086 of file logical.c.

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

Referenced by FilterPrepare().

1088 {
1090  ErrorContextCallback errcallback;
1091  bool ret;
1092 
1093  Assert(!ctx->fast_forward);
1094 
1095  /* Push callback + info on the error context stack */
1096  state.ctx = ctx;
1097  state.callback_name = "filter_prepare";
1099  errcallback.callback = output_plugin_error_callback;
1100  errcallback.arg = (void *) &state;
1101  errcallback.previous = error_context_stack;
1102  error_context_stack = &errcallback;
1103 
1104  /* set output state */
1105  ctx->accept_writes = false;
1106 
1107  /* do the actual work: call callback */
1108  ret = ctx->callbacks.filter_prepare_cb(ctx, xid, gid);
1109 
1110  /* Pop the error context stack */
1111  error_context_stack = errcallback.previous;
1112 
1113  return ret;
1114 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:52
void(* callback)(void *arg)
Definition: elog.h:247
struct ErrorContextCallback * previous
Definition: elog.h:246
const char * callback_name
Definition: logical.c:50
ErrorContextCallback * error_context_stack
Definition: elog.c:93
LogicalDecodeFilterPrepareCB filter_prepare_cb
#define Assert(condition)
Definition: c.h:804
Definition: regguts.h:317
XLogRecPtr report_location
Definition: logical.c:51
LogicalDecodingContext * ctx
Definition: logical.c:49
static void output_plugin_error_callback(void *arg)
Definition: logical.c:690

◆ FreeDecodingContext()

void FreeDecodingContext ( LogicalDecodingContext ctx)

Definition at line 614 of file logical.c.

References LogicalDecodingContext::callbacks, LogicalDecodingContext::context, FreeSnapshotBuilder(), MemoryContextDelete(), LogicalDecodingContext::reader, LogicalDecodingContext::reorder, ReorderBufferFree(), OutputPluginCallbacks::shutdown_cb, shutdown_cb_wrapper(), LogicalDecodingContext::snapshot_builder, and XLogReaderFree().

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

615 {
616  if (ctx->callbacks.shutdown_cb != NULL)
617  shutdown_cb_wrapper(ctx);
618 
621  XLogReaderFree(ctx->reader);
623 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
struct ReorderBuffer * reorder
Definition: logical.h:42
OutputPluginCallbacks callbacks
Definition: logical.h:52
void ReorderBufferFree(ReorderBuffer *rb)
MemoryContext context
Definition: logical.h:35
static void shutdown_cb_wrapper(LogicalDecodingContext *ctx)
Definition: logical.c:736
void XLogReaderFree(XLogReaderState *state)
Definition: xlogreader.c:139
void FreeSnapshotBuilder(SnapBuild *builder)
Definition: snapbuild.c:323
struct SnapBuild * snapshot_builder
Definition: logical.h:43
LogicalDecodeShutdownCB shutdown_cb
XLogReaderState * reader
Definition: logical.h:41

◆ LogicalConfirmReceivedLocation()

void LogicalConfirmReceivedLocation ( XLogRecPtr  lsn)

Definition at line 1666 of file logical.c.

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

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

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

◆ LogicalIncreaseRestartDecodingForSlot()

void LogicalIncreaseRestartDecodingForSlot ( XLogRecPtr  current_lsn,
XLogRecPtr  restart_lsn 
)

Definition at line 1592 of file logical.c.

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

Referenced by SnapBuildProcessRunningXacts().

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

◆ LogicalIncreaseXminForSlot()

void LogicalIncreaseXminForSlot ( XLogRecPtr  lsn,
TransactionId  xmin 
)

Definition at line 1535 of file logical.c.

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

Referenced by SnapBuildProcessRunningXacts().

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

◆ ResetLogicalStreamingState()

void ResetLogicalStreamingState ( void  )

Definition at line 1753 of file logical.c.

References bsysscan, CheckXidAlive, and InvalidTransactionId.

Referenced by AbortSubTransaction(), and AbortTransaction().

1754 {
1756  bsysscan = false;
1757 }
bool bsysscan
Definition: xact.c:96
#define InvalidTransactionId
Definition: transam.h:31
TransactionId CheckXidAlive
Definition: xact.c:95

◆ UpdateDecodingStats()

void UpdateDecodingStats ( LogicalDecodingContext ctx)

Definition at line 1763 of file logical.c.

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

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

1764 {
1765  ReorderBuffer *rb = ctx->reorder;
1766  PgStat_StatReplSlotEntry repSlotStat;
1767 
1768  /* Nothing to do if we don't have any replication stats to be sent. */
1769  if (rb->spillBytes <= 0 && rb->streamBytes <= 0 && rb->totalBytes <= 0)
1770  return;
1771 
1772  elog(DEBUG2, "UpdateDecodingStats: updating stats %p %lld %lld %lld %lld %lld %lld %lld %lld",
1773  rb,
1774  (long long) rb->spillTxns,
1775  (long long) rb->spillCount,
1776  (long long) rb->spillBytes,
1777  (long long) rb->streamTxns,
1778  (long long) rb->streamCount,
1779  (long long) rb->streamBytes,
1780  (long long) rb->totalTxns,
1781  (long long) rb->totalBytes);
1782 
1783  namestrcpy(&repSlotStat.slotname, NameStr(ctx->slot->data.name));
1784  repSlotStat.spill_txns = rb->spillTxns;
1785  repSlotStat.spill_count = rb->spillCount;
1786  repSlotStat.spill_bytes = rb->spillBytes;
1787  repSlotStat.stream_txns = rb->streamTxns;
1788  repSlotStat.stream_count = rb->streamCount;
1789  repSlotStat.stream_bytes = rb->streamBytes;
1790  repSlotStat.total_txns = rb->totalTxns;
1791  repSlotStat.total_bytes = rb->totalBytes;
1792 
1793  pgstat_report_replslot(&repSlotStat);
1794 
1795  rb->spillTxns = 0;
1796  rb->spillCount = 0;
1797  rb->spillBytes = 0;
1798  rb->streamTxns = 0;
1799  rb->streamCount = 0;
1800  rb->streamBytes = 0;
1801  rb->totalTxns = 0;
1802  rb->totalBytes = 0;
1803 }
struct ReorderBuffer * reorder
Definition: logical.h:42
PgStat_Counter spill_bytes
Definition: pgstat.h:902
void namestrcpy(Name name, const char *str)
Definition: name.c:233
ReplicationSlotPersistentData data
Definition: slot.h:156
#define DEBUG2
Definition: elog.h:24
PgStat_Counter total_bytes
Definition: pgstat.h:907
PgStat_Counter stream_bytes
Definition: pgstat.h:905
PgStat_Counter spill_txns
Definition: pgstat.h:900
void pgstat_report_replslot(const PgStat_StatReplSlotEntry *repSlotStat)
Definition: pgstat.c:1798
PgStat_Counter spill_count
Definition: pgstat.h:901
ReplicationSlot * slot
Definition: logical.h:38
PgStat_Counter stream_count
Definition: pgstat.h:904
#define elog(elevel,...)
Definition: elog.h:232
#define NameStr(name)
Definition: c.h:681
PgStat_Counter total_txns
Definition: pgstat.h:906
PgStat_Counter stream_txns
Definition: pgstat.h:903