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

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

◆ 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 435 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, MemoryContextSwitchTo(), MyDatabaseId, MyReplicationSlot, ReplicationSlotPersistentData::name, NameStr, LogicalDecodingContext::options, ReorderBuffer::output_rewrites, OutputPluginOptions::receive_rewrites, LogicalDecodingContext::reorder, ReplicationSlotPersistentData::restart_lsn, SlotIsPhysical, OutputPluginCallbacks::startup_cb, startup_cb_wrapper(), and StartupDecodingContext().

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

442 {
444  ReplicationSlot *slot;
445  MemoryContext old_context;
446 
447  /* shorter lines... */
448  slot = MyReplicationSlot;
449 
450  /* first some sanity checks that are unlikely to be violated */
451  if (slot == NULL)
452  elog(ERROR, "cannot perform logical decoding without an acquired slot");
453 
454  /* make sure the passed slot is suitable, these are user facing errors */
455  if (SlotIsPhysical(slot))
456  ereport(ERROR,
457  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
458  errmsg("cannot use physical replication slot for logical decoding")));
459 
460  if (slot->data.database != MyDatabaseId)
461  ereport(ERROR,
462  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
463  errmsg("replication slot \"%s\" was not created in this database",
464  NameStr(slot->data.name))));
465 
466  if (start_lsn == InvalidXLogRecPtr)
467  {
468  /* continue from last position */
469  start_lsn = slot->data.confirmed_flush;
470  }
471  else if (start_lsn < slot->data.confirmed_flush)
472  {
473  /*
474  * It might seem like we should error out in this case, but it's
475  * pretty common for a client to acknowledge a LSN it doesn't have to
476  * do anything for, and thus didn't store persistently, because the
477  * xlog records didn't result in anything relevant for logical
478  * decoding. Clients have to be able to do that to support synchronous
479  * replication.
480  */
481  elog(DEBUG1, "cannot stream from %X/%X, minimum is %X/%X, forwarding",
482  (uint32) (start_lsn >> 32), (uint32) start_lsn,
483  (uint32) (slot->data.confirmed_flush >> 32),
484  (uint32) slot->data.confirmed_flush);
485 
486  start_lsn = slot->data.confirmed_flush;
487  }
488 
489  ctx = StartupDecodingContext(output_plugin_options,
490  start_lsn, InvalidTransactionId, false,
491  fast_forward, xl_routine, prepare_write,
492  do_write, update_progress);
493 
494  /* call output plugin initialization callback */
495  old_context = MemoryContextSwitchTo(ctx->context);
496  if (ctx->callbacks.startup_cb != NULL)
497  startup_cb_wrapper(ctx, &ctx->options, false);
498  MemoryContextSwitchTo(old_context);
499 
501 
502  ereport(LOG,
503  (errmsg("starting logical decoding for slot \"%s\"",
504  NameStr(slot->data.name)),
505  errdetail("Streaming transactions committing after %X/%X, reading WAL from %X/%X.",
506  (uint32) (slot->data.confirmed_flush >> 32),
507  (uint32) slot->data.confirmed_flush,
508  (uint32) (slot->data.restart_lsn >> 32),
509  (uint32) slot->data.restart_lsn)));
510 
511  return ctx;
512 }
#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:610
MemoryContext context
Definition: logical.h:35
ReplicationSlotPersistentData data
Definition: slot.h:143
#define LOG
Definition: elog.h:26
#define SlotIsPhysical(slot)
Definition: slot.h:164
XLogRecPtr confirmed_flush
Definition: slot.h:91
#define ERROR
Definition: elog.h:43
static void startup_cb_wrapper(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
Definition: logical.c:666
int errdetail(const char *fmt,...)
Definition: elog.c:954
#define InvalidTransactionId
Definition: transam.h:31
unsigned int uint32
Definition: c.h:375
Oid MyDatabaseId
Definition: globals.c:85
#define ereport(elevel,...)
Definition: elog.h:144
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
XLogRecPtr restart_lsn
Definition: slot.h:80
LogicalDecodeStartupCB startup_cb
int errmsg(const char *fmt,...)
Definition: elog.c:821
#define elog(elevel,...)
Definition: elog.h:214
#define NameStr(name)
Definition: c.h:623
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:137

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

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

Referenced by create_logical_replication_slot(), and CreateReplicationSlot().

291 {
292  TransactionId xmin_horizon = InvalidTransactionId;
293  ReplicationSlot *slot;
294  NameData plugin_name;
296  MemoryContext old_context;
297 
298  /* shorter lines... */
299  slot = MyReplicationSlot;
300 
301  /* first some sanity checks that are unlikely to be violated */
302  if (slot == NULL)
303  elog(ERROR, "cannot perform logical decoding without an acquired slot");
304 
305  if (plugin == NULL)
306  elog(ERROR, "cannot initialize logical decoding without a specified plugin");
307 
308  /* Make sure the passed slot is suitable. These are user facing errors. */
309  if (SlotIsPhysical(slot))
310  ereport(ERROR,
311  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
312  errmsg("cannot use physical replication slot for logical decoding")));
313 
314  if (slot->data.database != MyDatabaseId)
315  ereport(ERROR,
316  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
317  errmsg("replication slot \"%s\" was not created in this database",
318  NameStr(slot->data.name))));
319 
320  if (IsTransactionState() &&
322  ereport(ERROR,
323  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
324  errmsg("cannot create logical replication slot in transaction that has performed writes")));
325 
326  /*
327  * Register output plugin name with slot. We need the mutex to avoid
328  * concurrent reading of a partially copied string. But we don't want any
329  * complicated code while holding a spinlock, so do namestrcpy() outside.
330  */
331  namestrcpy(&plugin_name, plugin);
332  SpinLockAcquire(&slot->mutex);
333  slot->data.plugin = plugin_name;
334  SpinLockRelease(&slot->mutex);
335 
336  if (XLogRecPtrIsInvalid(restart_lsn))
338  else
339  {
340  SpinLockAcquire(&slot->mutex);
341  slot->data.restart_lsn = restart_lsn;
342  SpinLockRelease(&slot->mutex);
343  }
344 
345  /* ----
346  * This is a bit tricky: We need to determine a safe xmin horizon to start
347  * decoding from, to avoid starting from a running xacts record referring
348  * to xids whose rows have been vacuumed or pruned
349  * already. GetOldestSafeDecodingTransactionId() returns such a value, but
350  * without further interlock its return value might immediately be out of
351  * date.
352  *
353  * So we have to acquire the ProcArrayLock to prevent computation of new
354  * xmin horizons by other backends, get the safe decoding xid, and inform
355  * the slot machinery about the new limit. Once that's done the
356  * ProcArrayLock can be released as the slot machinery now is
357  * protecting against vacuum.
358  *
359  * Note that, temporarily, the data, not just the catalog, xmin has to be
360  * reserved if a data snapshot is to be exported. Otherwise the initial
361  * data snapshot created here is not guaranteed to be valid. After that
362  * the data xmin doesn't need to be managed anymore and the global xmin
363  * should be recomputed. As we are fine with losing the pegged data xmin
364  * after crash - no chance a snapshot would get exported anymore - we can
365  * get away with just setting the slot's
366  * effective_xmin. ReplicationSlotRelease will reset it again.
367  *
368  * ----
369  */
370  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
371 
372  xmin_horizon = GetOldestSafeDecodingTransactionId(!need_full_snapshot);
373 
374  SpinLockAcquire(&slot->mutex);
375  slot->effective_catalog_xmin = xmin_horizon;
376  slot->data.catalog_xmin = xmin_horizon;
377  if (need_full_snapshot)
378  slot->effective_xmin = xmin_horizon;
379  SpinLockRelease(&slot->mutex);
380 
382 
383  LWLockRelease(ProcArrayLock);
384 
387 
388  ctx = StartupDecodingContext(NIL, restart_lsn, xmin_horizon,
389  need_full_snapshot, false,
390  xl_routine, prepare_write, do_write,
391  update_progress);
392 
393  /* call output plugin initialization callback */
394  old_context = MemoryContextSwitchTo(ctx->context);
395  if (ctx->callbacks.startup_cb != NULL)
396  startup_cb_wrapper(ctx, &ctx->options, true);
397  MemoryContextSwitchTo(old_context);
398 
400 
401  return ctx;
402 }
#define NIL
Definition: pg_list.h:65
static const char * plugin
TransactionId GetOldestSafeDecodingTransactionId(bool catalogOnly)
Definition: procarray.c:2794
struct ReorderBuffer * reorder
Definition: logical.h:42
uint32 TransactionId
Definition: c.h:521
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:610
MemoryContext context
Definition: logical.h:35
void ReplicationSlotSave(void)
Definition: slot.c:719
ReplicationSlotPersistentData data
Definition: slot.h:143
#define SlotIsPhysical(slot)
Definition: slot.h:164
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
#define SpinLockAcquire(lock)
Definition: spin.h:62
void ReplicationSlotReserveWal(void)
Definition: slot.c:1078
#define ERROR
Definition: elog.h:43
static void startup_cb_wrapper(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
Definition: logical.c:666
TransactionId effective_xmin
Definition: slot.h:139
Definition: c.h:617
TransactionId catalog_xmin
Definition: slot.h:77
#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:140
Oid MyDatabaseId
Definition: globals.c:85
#define ereport(elevel,...)
Definition: elog.h:144
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
XLogRecPtr restart_lsn
Definition: slot.h:80
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
bool IsTransactionState(void)
Definition: xact.c:371
LogicalDecodeStartupCB startup_cb
int errmsg(const char *fmt,...)
Definition: elog.c:821
#define elog(elevel,...)
Definition: elog.h:214
#define NameStr(name)
Definition: c.h:623
slock_t mutex
Definition: slot.h:116
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:776
void ReplicationSlotMarkDirty(void)
Definition: slot.c:737
static LogicalDecodingContext * StartupDecodingContext(List *output_plugin_options, XLogRecPtr start_lsn, TransactionId xmin_horizon, bool need_full_snapshot, bool fast_forward, XLogReaderRoutine *xl_routine, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
Definition: logical.c:137

◆ DecodingContextFindStartpoint()

void DecodingContextFindStartpoint ( LogicalDecodingContext ctx)

Definition at line 527 of file logical.c.

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

Referenced by create_logical_replication_slot(), and CreateReplicationSlot().

528 {
529  ReplicationSlot *slot = ctx->slot;
530 
531  /* Initialize from where to start reading WAL. */
532  XLogBeginRead(ctx->reader, slot->data.restart_lsn);
533 
534  elog(DEBUG1, "searching for logical decoding starting point, starting at %X/%X",
535  (uint32) (slot->data.restart_lsn >> 32),
536  (uint32) slot->data.restart_lsn);
537 
538  /* Wait for a consistent starting point */
539  for (;;)
540  {
541  XLogRecord *record;
542  char *err = NULL;
543 
544  /* the read_page callback waits for new WAL */
545  record = XLogReadRecord(ctx->reader, &err);
546  if (err)
547  elog(ERROR, "%s", err);
548  if (!record)
549  elog(ERROR, "no record found"); /* shouldn't happen */
550 
552 
553  /* only continue till we found a consistent spot */
554  if (DecodingContextReady(ctx))
555  break;
556 
558  }
559 
560  SpinLockAcquire(&slot->mutex);
561  slot->data.confirmed_flush = ctx->reader->EndRecPtr;
562  SpinLockRelease(&slot->mutex);
563 }
#define DEBUG1
Definition: elog.h:25
bool DecodingContextReady(LogicalDecodingContext *ctx)
Definition: logical.c:518
ReplicationSlotPersistentData data
Definition: slot.h:143
XLogRecPtr confirmed_flush
Definition: slot.h:91
XLogRecPtr EndRecPtr
Definition: xlogreader.h:176
#define SpinLockAcquire(lock)
Definition: spin.h:62
XLogRecord * XLogReadRecord(XLogReaderState *state, char **errormsg)
Definition: xlogreader.c:268
#define ERROR
Definition: elog.h:43
void LogicalDecodingProcessRecord(LogicalDecodingContext *ctx, XLogReaderState *record)
Definition: decode.c:94
void XLogBeginRead(XLogReaderState *state, XLogRecPtr RecPtr)
Definition: xlogreader.c:240
unsigned int uint32
Definition: c.h:375
#define SpinLockRelease(lock)
Definition: spin.h:64
XLogRecPtr restart_lsn
Definition: slot.h:80
XLogReaderState * reader
Definition: logical.h:41
ReplicationSlot * slot
Definition: logical.h:38
#define elog(elevel,...)
Definition: elog.h:214
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
slock_t mutex
Definition: slot.h:116

◆ DecodingContextReady()

bool DecodingContextReady ( LogicalDecodingContext ctx)

Definition at line 518 of file logical.c.

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

Referenced by DecodingContextFindStartpoint().

519 {
521 }
SnapBuildState SnapBuildCurrentState(SnapBuild *builder)
Definition: snapbuild.c:395
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 863 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().

864 {
866  ErrorContextCallback errcallback;
867  bool ret;
868 
869  Assert(!ctx->fast_forward);
870 
871  /* Push callback + info on the error context stack */
872  state.ctx = ctx;
873  state.callback_name = "filter_by_origin";
876  errcallback.arg = (void *) &state;
877  errcallback.previous = error_context_stack;
878  error_context_stack = &errcallback;
879 
880  /* set output state */
881  ctx->accept_writes = false;
882 
883  /* do the actual work: call callback */
884  ret = ctx->callbacks.filter_by_origin_cb(ctx, origin_id);
885 
886  /* Pop the error context stack */
887  error_context_stack = errcallback.previous;
888 
889  return ret;
890 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:52
void(* callback)(void *arg)
Definition: elog.h:229
struct ErrorContextCallback * previous
Definition: elog.h:228
const char * callback_name
Definition: logical.c:50
ErrorContextCallback * error_context_stack
Definition: elog.c:92
#define Assert(condition)
Definition: c.h:746
Definition: regguts.h:298
XLogRecPtr report_location
Definition: logical.c:51
LogicalDecodingContext * ctx
Definition: logical.c:49
static void output_plugin_error_callback(void *arg)
Definition: logical.c:646
LogicalDecodeFilterByOriginCB filter_by_origin_cb

◆ FreeDecodingContext()

void FreeDecodingContext ( LogicalDecodingContext ctx)

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

571 {
572  if (ctx->callbacks.shutdown_cb != NULL)
573  shutdown_cb_wrapper(ctx);
574 
577  XLogReaderFree(ctx->reader);
579 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:212
struct ReorderBuffer * reorder
Definition: logical.h:42
OutputPluginCallbacks callbacks
Definition: logical.h:52
void ReorderBufferFree(ReorderBuffer *rb)
MemoryContext context
Definition: logical.h:35
static void shutdown_cb_wrapper(LogicalDecodingContext *ctx)
Definition: logical.c:693
void XLogReaderFree(XLogReaderState *state)
Definition: xlogreader.c:139
void FreeSnapshotBuilder(SnapBuild *builder)
Definition: snapbuild.c:351
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 1372 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().

1373 {
1374  Assert(lsn != InvalidXLogRecPtr);
1375 
1376  /* Do an unlocked check for candidate_lsn first. */
1379  {
1380  bool updated_xmin = false;
1381  bool updated_restart = false;
1382 
1384 
1386 
1387  /* if we're past the location required for bumping xmin, do so */
1390  {
1391  /*
1392  * We have to write the changed xmin to disk *before* we change
1393  * the in-memory value, otherwise after a crash we wouldn't know
1394  * that some catalog tuples might have been removed already.
1395  *
1396  * Ensure that by first writing to ->xmin and only update
1397  * ->effective_xmin once the new state is synced to disk. After a
1398  * crash ->effective_xmin is set to ->xmin.
1399  */
1402  {
1406  updated_xmin = true;
1407  }
1408  }
1409 
1412  {
1414 
1418  updated_restart = true;
1419  }
1420 
1422 
1423  /* first write new xmin to disk, so we know what's up after a crash */
1424  if (updated_xmin || updated_restart)
1425  {
1428  elog(DEBUG1, "updated xmin: %u restart: %u", updated_xmin, updated_restart);
1429  }
1430 
1431  /*
1432  * Now the new xmin is safely on disk, we can let the global value
1433  * advance. We do not take ProcArrayLock or similar since we only
1434  * advance xmin here and there's not much harm done by a concurrent
1435  * computation missing that.
1436  */
1437  if (updated_xmin)
1438  {
1442 
1445  }
1446  }
1447  else
1448  {
1452  }
1453 }
TransactionId candidate_catalog_xmin
Definition: slot.h:158
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
void ReplicationSlotSave(void)
Definition: slot.c:719
ReplicationSlotPersistentData data
Definition: slot.h:143
XLogRecPtr confirmed_flush
Definition: slot.h:91
#define SpinLockAcquire(lock)
Definition: spin.h:62
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:826
XLogRecPtr candidate_restart_valid
Definition: slot.h:160
TransactionId catalog_xmin
Definition: slot.h:77
#define InvalidTransactionId
Definition: transam.h:31
#define SpinLockRelease(lock)
Definition: spin.h:64
TransactionId effective_catalog_xmin
Definition: slot.h:140
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:746
XLogRecPtr restart_lsn
Definition: slot.h:80
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:159
#define elog(elevel,...)
Definition: elog.h:214
slock_t mutex
Definition: slot.h:116
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:776
XLogRecPtr candidate_restart_lsn
Definition: slot.h:161
void ReplicationSlotMarkDirty(void)
Definition: slot.c:737

◆ LogicalIncreaseRestartDecodingForSlot()

void LogicalIncreaseRestartDecodingForSlot ( XLogRecPtr  current_lsn,
XLogRecPtr  restart_lsn 
)

Definition at line 1295 of file logical.c.

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

Referenced by SnapBuildProcessRunningXacts().

1296 {
1297  bool updated_lsn = false;
1298  ReplicationSlot *slot;
1299 
1300  slot = MyReplicationSlot;
1301 
1302  Assert(slot != NULL);
1303  Assert(restart_lsn != InvalidXLogRecPtr);
1304  Assert(current_lsn != InvalidXLogRecPtr);
1305 
1306  SpinLockAcquire(&slot->mutex);
1307 
1308  /* don't overwrite if have a newer restart lsn */
1309  if (restart_lsn <= slot->data.restart_lsn)
1310  {
1311  }
1312 
1313  /*
1314  * We might have already flushed far enough to directly accept this lsn,
1315  * in this case there is no need to check for existing candidate LSNs
1316  */
1317  else if (current_lsn <= slot->data.confirmed_flush)
1318  {
1319  slot->candidate_restart_valid = current_lsn;
1320  slot->candidate_restart_lsn = restart_lsn;
1321 
1322  /* our candidate can directly be used */
1323  updated_lsn = true;
1324  }
1325 
1326  /*
1327  * Only increase if the previous values have been applied, otherwise we
1328  * might never end up updating if the receiver acks too slowly. A missed
1329  * value here will just cause some extra effort after reconnecting.
1330  */
1332  {
1333  slot->candidate_restart_valid = current_lsn;
1334  slot->candidate_restart_lsn = restart_lsn;
1335  SpinLockRelease(&slot->mutex);
1336 
1337  elog(DEBUG1, "got new restart lsn %X/%X at %X/%X",
1338  (uint32) (restart_lsn >> 32), (uint32) restart_lsn,
1339  (uint32) (current_lsn >> 32), (uint32) current_lsn);
1340  }
1341  else
1342  {
1343  XLogRecPtr candidate_restart_lsn;
1344  XLogRecPtr candidate_restart_valid;
1345  XLogRecPtr confirmed_flush;
1346 
1347  candidate_restart_lsn = slot->candidate_restart_lsn;
1348  candidate_restart_valid = slot->candidate_restart_valid;
1349  confirmed_flush = slot->data.confirmed_flush;
1350  SpinLockRelease(&slot->mutex);
1351 
1352  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",
1353  (uint32) (restart_lsn >> 32), (uint32) restart_lsn,
1354  (uint32) (current_lsn >> 32), (uint32) current_lsn,
1355  (uint32) (candidate_restart_lsn >> 32),
1356  (uint32) candidate_restart_lsn,
1357  (uint32) (candidate_restart_valid >> 32),
1358  (uint32) candidate_restart_valid,
1359  (uint32) (confirmed_flush >> 32),
1360  (uint32) confirmed_flush);
1361  }
1362 
1363  /* candidates are already valid with the current flush position, apply */
1364  if (updated_lsn)
1366 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
ReplicationSlotPersistentData data
Definition: slot.h:143
XLogRecPtr confirmed_flush
Definition: slot.h:91
#define SpinLockAcquire(lock)
Definition: spin.h:62
XLogRecPtr candidate_restart_valid
Definition: slot.h:160
unsigned int uint32
Definition: c.h:375
#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:746
void LogicalConfirmReceivedLocation(XLogRecPtr lsn)
Definition: logical.c:1372
#define elog(elevel,...)
Definition: elog.h:214
slock_t mutex
Definition: slot.h:116
XLogRecPtr candidate_restart_lsn
Definition: slot.h:161

◆ LogicalIncreaseXminForSlot()

void LogicalIncreaseXminForSlot ( XLogRecPtr  lsn,
TransactionId  xmin 
)

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

1239 {
1240  bool updated_xmin = false;
1241  ReplicationSlot *slot;
1242 
1243  slot = MyReplicationSlot;
1244 
1245  Assert(slot != NULL);
1246 
1247  SpinLockAcquire(&slot->mutex);
1248 
1249  /*
1250  * don't overwrite if we already have a newer xmin. This can happen if we
1251  * restart decoding in a slot.
1252  */
1254  {
1255  }
1256 
1257  /*
1258  * If the client has already confirmed up to this lsn, we directly can
1259  * mark this as accepted. This can happen if we restart decoding in a
1260  * slot.
1261  */
1262  else if (current_lsn <= slot->data.confirmed_flush)
1263  {
1264  slot->candidate_catalog_xmin = xmin;
1265  slot->candidate_xmin_lsn = current_lsn;
1266 
1267  /* our candidate can directly be used */
1268  updated_xmin = true;
1269  }
1270 
1271  /*
1272  * Only increase if the previous values have been applied, otherwise we
1273  * might never end up updating if the receiver acks too slowly.
1274  */
1275  else if (slot->candidate_xmin_lsn == InvalidXLogRecPtr)
1276  {
1277  slot->candidate_catalog_xmin = xmin;
1278  slot->candidate_xmin_lsn = current_lsn;
1279  }
1280  SpinLockRelease(&slot->mutex);
1281 
1282  /* candidate already valid with the current flush position, apply */
1283  if (updated_xmin)
1285 }
TransactionId candidate_catalog_xmin
Definition: slot.h:158
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
ReplicationSlotPersistentData data
Definition: slot.h:143
XLogRecPtr confirmed_flush
Definition: slot.h:91
#define SpinLockAcquire(lock)
Definition: spin.h:62
bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:319
TransactionId catalog_xmin
Definition: slot.h:77
#define SpinLockRelease(lock)
Definition: spin.h:64
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:746
void LogicalConfirmReceivedLocation(XLogRecPtr lsn)
Definition: logical.c:1372
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:159
slock_t mutex
Definition: slot.h:116

◆ ResetLogicalStreamingState()

void ResetLogicalStreamingState ( void  )

Definition at line 1459 of file logical.c.

References bsysscan, CheckXidAlive, and InvalidTransactionId.

Referenced by AbortSubTransaction(), and AbortTransaction().

1460 {
1462  bsysscan = false;
1463 }
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 1469 of file logical.c.

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

Referenced by DecodeAbort(), and DecodeCommit().

1470 {
1471  ReorderBuffer *rb = ctx->reorder;
1472 
1473  /*
1474  * Nothing to do if we haven't spilled anything since the last time the
1475  * stats has been sent.
1476  */
1477  if (rb->spillBytes <= 0)
1478  return;
1479 
1480  elog(DEBUG2, "UpdateDecodingStats: updating stats %p %lld %lld %lld",
1481  rb,
1482  (long long) rb->spillTxns,
1483  (long long) rb->spillCount,
1484  (long long) rb->spillBytes);
1485 
1487  rb->spillTxns, rb->spillCount, rb->spillBytes);
1488  rb->spillTxns = 0;
1489  rb->spillCount = 0;
1490  rb->spillBytes = 0;
1491 }
struct ReorderBuffer * reorder
Definition: logical.h:42
void pgstat_report_replslot(const char *slotname, int spilltxns, int spillcount, int spillbytes)
Definition: pgstat.c:1710
ReplicationSlotPersistentData data
Definition: slot.h:143
#define DEBUG2
Definition: elog.h:24
ReplicationSlot * slot
Definition: logical.h:38
#define elog(elevel,...)
Definition: elog.h:214
#define NameStr(name)
Definition: c.h:623