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 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 }
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
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:5907
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 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
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
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 }
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
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 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().

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

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

◆ 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:368
PgStat_Counter total_txns
Definition: pgstat.h:370
PgStat_Counter total_bytes
Definition: pgstat.h:371
PgStat_Counter spill_txns
Definition: pgstat.h:364
PgStat_Counter stream_txns
Definition: pgstat.h:367
PgStat_Counter spill_count
Definition: pgstat.h:365
PgStat_Counter stream_bytes
Definition: pgstat.h:369
PgStat_Counter spill_bytes
Definition: pgstat.h:366

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