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

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

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

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

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

◆ CreateInitDecodingContext()

LogicalDecodingContext* CreateInitDecodingContext ( 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 280 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, NAMEDATALEN, NameStr, 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(), StrNCpy, and XLogRecPtrIsInvalid.

Referenced by create_logical_replication_slot(), and CreateReplicationSlot().

288 {
289  TransactionId xmin_horizon = InvalidTransactionId;
290  ReplicationSlot *slot;
292  MemoryContext old_context;
293 
294  /* shorter lines... */
295  slot = MyReplicationSlot;
296 
297  /* first some sanity checks that are unlikely to be violated */
298  if (slot == NULL)
299  elog(ERROR, "cannot perform logical decoding without an acquired slot");
300 
301  if (plugin == NULL)
302  elog(ERROR, "cannot initialize logical decoding without a specified plugin");
303 
304  /* Make sure the passed slot is suitable. These are user facing errors. */
305  if (SlotIsPhysical(slot))
306  ereport(ERROR,
307  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
308  errmsg("cannot use physical replication slot for logical decoding")));
309 
310  if (slot->data.database != MyDatabaseId)
311  ereport(ERROR,
312  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
313  errmsg("replication slot \"%s\" was not created in this database",
314  NameStr(slot->data.name))));
315 
316  if (IsTransactionState() &&
318  ereport(ERROR,
319  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
320  errmsg("cannot create logical replication slot in transaction that has performed writes")));
321 
322  /* register output plugin name with slot */
323  SpinLockAcquire(&slot->mutex);
325  SpinLockRelease(&slot->mutex);
326 
327  if (XLogRecPtrIsInvalid(restart_lsn))
329  else
330  {
331  SpinLockAcquire(&slot->mutex);
332  slot->data.restart_lsn = restart_lsn;
333  SpinLockRelease(&slot->mutex);
334  }
335 
336  /* ----
337  * This is a bit tricky: We need to determine a safe xmin horizon to start
338  * decoding from, to avoid starting from a running xacts record referring
339  * to xids whose rows have been vacuumed or pruned
340  * already. GetOldestSafeDecodingTransactionId() returns such a value, but
341  * without further interlock its return value might immediately be out of
342  * date.
343  *
344  * So we have to acquire the ProcArrayLock to prevent computation of new
345  * xmin horizons by other backends, get the safe decoding xid, and inform
346  * the slot machinery about the new limit. Once that's done the
347  * ProcArrayLock can be released as the slot machinery now is
348  * protecting against vacuum.
349  *
350  * Note that, temporarily, the data, not just the catalog, xmin has to be
351  * reserved if a data snapshot is to be exported. Otherwise the initial
352  * data snapshot created here is not guaranteed to be valid. After that
353  * the data xmin doesn't need to be managed anymore and the global xmin
354  * should be recomputed. As we are fine with losing the pegged data xmin
355  * after crash - no chance a snapshot would get exported anymore - we can
356  * get away with just setting the slot's
357  * effective_xmin. ReplicationSlotRelease will reset it again.
358  *
359  * ----
360  */
361  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
362 
363  xmin_horizon = GetOldestSafeDecodingTransactionId(!need_full_snapshot);
364 
365  SpinLockAcquire(&slot->mutex);
366  slot->effective_catalog_xmin = xmin_horizon;
367  slot->data.catalog_xmin = xmin_horizon;
368  if (need_full_snapshot)
369  slot->effective_xmin = xmin_horizon;
370  SpinLockRelease(&slot->mutex);
371 
373 
374  LWLockRelease(ProcArrayLock);
375 
378 
379  ctx = StartupDecodingContext(NIL, restart_lsn, xmin_horizon,
380  need_full_snapshot, false,
381  xl_routine, prepare_write, do_write,
382  update_progress);
383 
384  /* call output plugin initialization callback */
385  old_context = MemoryContextSwitchTo(ctx->context);
386  if (ctx->callbacks.startup_cb != NULL)
387  startup_cb_wrapper(ctx, &ctx->options, true);
388  MemoryContextSwitchTo(old_context);
389 
391 
392  return ctx;
393 }
#define NIL
Definition: pg_list.h:65
static const char * plugin
TransactionId GetOldestSafeDecodingTransactionId(bool catalogOnly)
Definition: procarray.c:2176
struct ReorderBuffer * reorder
Definition: logical.h:42
uint32 TransactionId
Definition: c.h:520
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:697
ReplicationSlotPersistentData data
Definition: slot.h:143
#define SlotIsPhysical(slot)
Definition: slot.h:164
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
#define NAMEDATALEN
#define SpinLockAcquire(lock)
Definition: spin.h:62
void ReplicationSlotReserveWal(void)
Definition: slot.c:1056
#define ERROR
Definition: elog.h:43
static void startup_cb_wrapper(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
Definition: logical.c:657
TransactionId effective_xmin
Definition: slot.h:139
TransactionId catalog_xmin
Definition: slot.h:77
#define InvalidTransactionId
Definition: transam.h:31
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:412
#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
#define StrNCpy(dst, src, len)
Definition: c.h:951
XLogRecPtr restart_lsn
Definition: slot.h:80
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
bool IsTransactionState(void)
Definition: xact.c:358
LogicalDecodeStartupCB startup_cb
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214
#define NameStr(name)
Definition: c.h:622
slock_t mutex
Definition: slot.h:116
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:754
void ReplicationSlotMarkDirty(void)
Definition: slot.c:715
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:135

◆ DecodingContextFindStartpoint()

void DecodingContextFindStartpoint ( LogicalDecodingContext ctx)

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

519 {
520  ReplicationSlot *slot = ctx->slot;
521 
522  /* Initialize from where to start reading WAL. */
523  XLogBeginRead(ctx->reader, slot->data.restart_lsn);
524 
525  elog(DEBUG1, "searching for logical decoding starting point, starting at %X/%X",
526  (uint32) (slot->data.restart_lsn >> 32),
527  (uint32) slot->data.restart_lsn);
528 
529  /* Wait for a consistent starting point */
530  for (;;)
531  {
532  XLogRecord *record;
533  char *err = NULL;
534 
535  /* the read_page callback waits for new WAL */
536  record = XLogReadRecord(ctx->reader, &err);
537  if (err)
538  elog(ERROR, "%s", err);
539  if (!record)
540  elog(ERROR, "no record found"); /* shouldn't happen */
541 
543 
544  /* only continue till we found a consistent spot */
545  if (DecodingContextReady(ctx))
546  break;
547 
549  }
550 
551  SpinLockAcquire(&slot->mutex);
552  slot->data.confirmed_flush = ctx->reader->EndRecPtr;
553  SpinLockRelease(&slot->mutex);
554 }
#define DEBUG1
Definition: elog.h:25
bool DecodingContextReady(LogicalDecodingContext *ctx)
Definition: logical.c:509
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:374
#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 509 of file logical.c.

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

Referenced by DecodingContextFindStartpoint().

510 {
512 }
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 854 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().

855 {
857  ErrorContextCallback errcallback;
858  bool ret;
859 
860  Assert(!ctx->fast_forward);
861 
862  /* Push callback + info on the error context stack */
863  state.ctx = ctx;
864  state.callback_name = "filter_by_origin";
867  errcallback.arg = (void *) &state;
868  errcallback.previous = error_context_stack;
869  error_context_stack = &errcallback;
870 
871  /* set output state */
872  ctx->accept_writes = false;
873 
874  /* do the actual work: call callback */
875  ret = ctx->callbacks.filter_by_origin_cb(ctx, origin_id);
876 
877  /* Pop the error context stack */
878  error_context_stack = errcallback.previous;
879 
880  return ret;
881 }
#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:48
ErrorContextCallback * error_context_stack
Definition: elog.c:92
#define Assert(condition)
Definition: c.h:745
Definition: regguts.h:298
XLogRecPtr report_location
Definition: logical.c:49
LogicalDecodingContext * ctx
Definition: logical.c:47
static void output_plugin_error_callback(void *arg)
Definition: logical.c:637
LogicalDecodeFilterByOriginCB filter_by_origin_cb

◆ FreeDecodingContext()

void FreeDecodingContext ( LogicalDecodingContext ctx)

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

562 {
563  if (ctx->callbacks.shutdown_cb != NULL)
564  shutdown_cb_wrapper(ctx);
565 
568  XLogReaderFree(ctx->reader);
570 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
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:684
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 1363 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().

1364 {
1365  Assert(lsn != InvalidXLogRecPtr);
1366 
1367  /* Do an unlocked check for candidate_lsn first. */
1370  {
1371  bool updated_xmin = false;
1372  bool updated_restart = false;
1373 
1375 
1377 
1378  /* if we're past the location required for bumping xmin, do so */
1381  {
1382  /*
1383  * We have to write the changed xmin to disk *before* we change
1384  * the in-memory value, otherwise after a crash we wouldn't know
1385  * that some catalog tuples might have been removed already.
1386  *
1387  * Ensure that by first writing to ->xmin and only update
1388  * ->effective_xmin once the new state is synced to disk. After a
1389  * crash ->effective_xmin is set to ->xmin.
1390  */
1393  {
1397  updated_xmin = true;
1398  }
1399  }
1400 
1403  {
1405 
1409  updated_restart = true;
1410  }
1411 
1413 
1414  /* first write new xmin to disk, so we know what's up after a crash */
1415  if (updated_xmin || updated_restart)
1416  {
1419  elog(DEBUG1, "updated xmin: %u restart: %u", updated_xmin, updated_restart);
1420  }
1421 
1422  /*
1423  * Now the new xmin is safely on disk, we can let the global value
1424  * advance. We do not take ProcArrayLock or similar since we only
1425  * advance xmin here and there's not much harm done by a concurrent
1426  * computation missing that.
1427  */
1428  if (updated_xmin)
1429  {
1433 
1436  }
1437  }
1438  else
1439  {
1443  }
1444 }
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:697
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:804
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:745
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:754
XLogRecPtr candidate_restart_lsn
Definition: slot.h:161
void ReplicationSlotMarkDirty(void)
Definition: slot.c:715

◆ LogicalIncreaseRestartDecodingForSlot()

void LogicalIncreaseRestartDecodingForSlot ( XLogRecPtr  current_lsn,
XLogRecPtr  restart_lsn 
)

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

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

1230 {
1231  bool updated_xmin = false;
1232  ReplicationSlot *slot;
1233 
1234  slot = MyReplicationSlot;
1235 
1236  Assert(slot != NULL);
1237 
1238  SpinLockAcquire(&slot->mutex);
1239 
1240  /*
1241  * don't overwrite if we already have a newer xmin. This can happen if we
1242  * restart decoding in a slot.
1243  */
1245  {
1246  }
1247 
1248  /*
1249  * If the client has already confirmed up to this lsn, we directly can
1250  * mark this as accepted. This can happen if we restart decoding in a
1251  * slot.
1252  */
1253  else if (current_lsn <= slot->data.confirmed_flush)
1254  {
1255  slot->candidate_catalog_xmin = xmin;
1256  slot->candidate_xmin_lsn = current_lsn;
1257 
1258  /* our candidate can directly be used */
1259  updated_xmin = true;
1260  }
1261 
1262  /*
1263  * Only increase if the previous values have been applied, otherwise we
1264  * might never end up updating if the receiver acks too slowly.
1265  */
1266  else if (slot->candidate_xmin_lsn == InvalidXLogRecPtr)
1267  {
1268  slot->candidate_catalog_xmin = xmin;
1269  slot->candidate_xmin_lsn = current_lsn;
1270  }
1271  SpinLockRelease(&slot->mutex);
1272 
1273  /* candidate already valid with the current flush position, apply */
1274  if (updated_xmin)
1276 }
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:745
void LogicalConfirmReceivedLocation(XLogRecPtr lsn)
Definition: logical.c:1363
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:159
slock_t mutex
Definition: slot.h:116