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

76 {
78 
79  /*
80  * NB: Adding a new requirement likely means that RestoreSlotFromDisk()
81  * needs the same check.
82  */
83 
85  ereport(ERROR,
86  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
87  errmsg("logical decoding requires wal_level >= logical")));
88 
89  if (MyDatabaseId == InvalidOid)
90  ereport(ERROR,
91  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
92  errmsg("logical decoding requires a database connection")));
93 
94  /* ----
95  * TODO: We got to change that someday soon...
96  *
97  * There's basically three things missing to allow this:
98  * 1) We need to be able to correctly and quickly identify the timeline a
99  * LSN belongs to
100  * 2) We need to force hot_standby_feedback to be enabled at all times so
101  * the primary cannot remove rows we need.
102  * 3) support dropping replication slots referring to a database, in
103  * dbase_redo. There can't be any active ones due to HS recovery
104  * conflicts, so that should be relatively easy.
105  * ----
106  */
107  if (RecoveryInProgress())
108  ereport(ERROR,
109  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
110  errmsg("logical decoding cannot be used while in recovery")));
111 }
void CheckSlotRequirements(void)
Definition: slot.c:988
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 376 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().

383 {
385  ReplicationSlot *slot;
386  MemoryContext old_context;
387 
388  /* shorter lines... */
389  slot = MyReplicationSlot;
390 
391  /* first some sanity checks that are unlikely to be violated */
392  if (slot == NULL)
393  elog(ERROR, "cannot perform logical decoding without an acquired slot");
394 
395  /* make sure the passed slot is suitable, these are user facing errors */
396  if (SlotIsPhysical(slot))
397  ereport(ERROR,
398  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
399  errmsg("cannot use physical replication slot for logical decoding")));
400 
401  if (slot->data.database != MyDatabaseId)
402  ereport(ERROR,
403  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
404  errmsg("replication slot \"%s\" was not created in this database",
405  NameStr(slot->data.name))));
406 
407  if (start_lsn == InvalidXLogRecPtr)
408  {
409  /* continue from last position */
410  start_lsn = slot->data.confirmed_flush;
411  }
412  else if (start_lsn < slot->data.confirmed_flush)
413  {
414  /*
415  * It might seem like we should error out in this case, but it's
416  * pretty common for a client to acknowledge a LSN it doesn't have to
417  * do anything for, and thus didn't store persistently, because the
418  * xlog records didn't result in anything relevant for logical
419  * decoding. Clients have to be able to do that to support synchronous
420  * replication.
421  */
422  elog(DEBUG1, "cannot stream from %X/%X, minimum is %X/%X, forwarding",
423  (uint32) (start_lsn >> 32), (uint32) start_lsn,
424  (uint32) (slot->data.confirmed_flush >> 32),
425  (uint32) slot->data.confirmed_flush);
426 
427  start_lsn = slot->data.confirmed_flush;
428  }
429 
430  ctx = StartupDecodingContext(output_plugin_options,
431  start_lsn, InvalidTransactionId, false,
432  fast_forward, xl_routine, prepare_write,
433  do_write, update_progress);
434 
435  /* call output plugin initialization callback */
436  old_context = MemoryContextSwitchTo(ctx->context);
437  if (ctx->callbacks.startup_cb != NULL)
438  startup_cb_wrapper(ctx, &ctx->options, false);
439  MemoryContextSwitchTo(old_context);
440 
442 
443  ereport(LOG,
444  (errmsg("starting logical decoding for slot \"%s\"",
445  NameStr(slot->data.name)),
446  errdetail("Streaming transactions committing after %X/%X, reading WAL from %X/%X.",
447  (uint32) (slot->data.confirmed_flush >> 32),
448  (uint32) slot->data.confirmed_flush,
449  (uint32) (slot->data.restart_lsn >> 32),
450  (uint32) slot->data.restart_lsn)));
451 
452  return ctx;
453 }
#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:140
#define LOG
Definition: elog.h:26
#define SlotIsPhysical(slot)
Definition: slot.h:161
XLogRecPtr confirmed_flush
Definition: slot.h:88
#define ERROR
Definition: elog.h:43
static void startup_cb_wrapper(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
Definition: logical.c:607
int errdetail(const char *fmt,...)
Definition: elog.c:957
#define InvalidTransactionId
Definition: transam.h:31
unsigned int uint32
Definition: c.h:367
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:615
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:118

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

238 {
239  TransactionId xmin_horizon = InvalidTransactionId;
240  ReplicationSlot *slot;
242  MemoryContext old_context;
243 
244  /* shorter lines... */
245  slot = MyReplicationSlot;
246 
247  /* first some sanity checks that are unlikely to be violated */
248  if (slot == NULL)
249  elog(ERROR, "cannot perform logical decoding without an acquired slot");
250 
251  if (plugin == NULL)
252  elog(ERROR, "cannot initialize logical decoding without a specified plugin");
253 
254  /* Make sure the passed slot is suitable. These are user facing errors. */
255  if (SlotIsPhysical(slot))
256  ereport(ERROR,
257  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
258  errmsg("cannot use physical replication slot for logical decoding")));
259 
260  if (slot->data.database != MyDatabaseId)
261  ereport(ERROR,
262  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
263  errmsg("replication slot \"%s\" was not created in this database",
264  NameStr(slot->data.name))));
265 
266  if (IsTransactionState() &&
268  ereport(ERROR,
269  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
270  errmsg("cannot create logical replication slot in transaction that has performed writes")));
271 
272  /* register output plugin name with slot */
273  SpinLockAcquire(&slot->mutex);
275  SpinLockRelease(&slot->mutex);
276 
277  if (XLogRecPtrIsInvalid(restart_lsn))
279  else
280  {
281  SpinLockAcquire(&slot->mutex);
282  slot->data.restart_lsn = restart_lsn;
283  SpinLockRelease(&slot->mutex);
284  }
285 
286  /* ----
287  * This is a bit tricky: We need to determine a safe xmin horizon to start
288  * decoding from, to avoid starting from a running xacts record referring
289  * to xids whose rows have been vacuumed or pruned
290  * already. GetOldestSafeDecodingTransactionId() returns such a value, but
291  * without further interlock its return value might immediately be out of
292  * date.
293  *
294  * So we have to acquire the ProcArrayLock to prevent computation of new
295  * xmin horizons by other backends, get the safe decoding xid, and inform
296  * the slot machinery about the new limit. Once that's done the
297  * ProcArrayLock can be released as the slot machinery now is
298  * protecting against vacuum.
299  *
300  * Note that, temporarily, the data, not just the catalog, xmin has to be
301  * reserved if a data snapshot is to be exported. Otherwise the initial
302  * data snapshot created here is not guaranteed to be valid. After that
303  * the data xmin doesn't need to be managed anymore and the global xmin
304  * should be recomputed. As we are fine with losing the pegged data xmin
305  * after crash - no chance a snapshot would get exported anymore - we can
306  * get away with just setting the slot's
307  * effective_xmin. ReplicationSlotRelease will reset it again.
308  *
309  * ----
310  */
311  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
312 
313  xmin_horizon = GetOldestSafeDecodingTransactionId(!need_full_snapshot);
314 
315  SpinLockAcquire(&slot->mutex);
316  slot->effective_catalog_xmin = xmin_horizon;
317  slot->data.catalog_xmin = xmin_horizon;
318  if (need_full_snapshot)
319  slot->effective_xmin = xmin_horizon;
320  SpinLockRelease(&slot->mutex);
321 
323 
324  LWLockRelease(ProcArrayLock);
325 
328 
329  ctx = StartupDecodingContext(NIL, restart_lsn, xmin_horizon,
330  need_full_snapshot, false,
331  xl_routine, prepare_write, do_write,
332  update_progress);
333 
334  /* call output plugin initialization callback */
335  old_context = MemoryContextSwitchTo(ctx->context);
336  if (ctx->callbacks.startup_cb != NULL)
337  startup_cb_wrapper(ctx, &ctx->options, true);
338  MemoryContextSwitchTo(old_context);
339 
341 
342  return ctx;
343 }
#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:513
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:654
ReplicationSlotPersistentData data
Definition: slot.h:140
#define SlotIsPhysical(slot)
Definition: slot.h:161
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
#define NAMEDATALEN
#define SpinLockAcquire(lock)
Definition: spin.h:62
void ReplicationSlotReserveWal(void)
Definition: slot.c:1013
#define ERROR
Definition: elog.h:43
static void startup_cb_wrapper(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
Definition: logical.c:607
TransactionId effective_xmin
Definition: slot.h:136
TransactionId catalog_xmin
Definition: slot.h:77
#define InvalidTransactionId
Definition: transam.h:31
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:410
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
#define SpinLockRelease(lock)
Definition: spin.h:64
TransactionId effective_catalog_xmin
Definition: slot.h:137
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:944
XLogRecPtr restart_lsn
Definition: slot.h:80
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
bool IsTransactionState(void)
Definition: xact.c:356
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:615
slock_t mutex
Definition: slot.h:113
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:711
void ReplicationSlotMarkDirty(void)
Definition: slot.c:672
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:118

◆ DecodingContextFindStartpoint()

void DecodingContextFindStartpoint ( LogicalDecodingContext ctx)

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

469 {
470  ReplicationSlot *slot = ctx->slot;
471 
472  /* Initialize from where to start reading WAL. */
473  XLogBeginRead(ctx->reader, slot->data.restart_lsn);
474 
475  elog(DEBUG1, "searching for logical decoding starting point, starting at %X/%X",
476  (uint32) (slot->data.restart_lsn >> 32),
477  (uint32) slot->data.restart_lsn);
478 
479  /* Wait for a consistent starting point */
480  for (;;)
481  {
482  XLogRecord *record;
483  char *err = NULL;
484 
485  /* the read_page callback waits for new WAL */
486  record = XLogReadRecord(ctx->reader, &err);
487  if (err)
488  elog(ERROR, "%s", err);
489  if (!record)
490  elog(ERROR, "no record found"); /* shouldn't happen */
491 
493 
494  /* only continue till we found a consistent spot */
495  if (DecodingContextReady(ctx))
496  break;
497 
499  }
500 
501  SpinLockAcquire(&slot->mutex);
502  slot->data.confirmed_flush = ctx->reader->EndRecPtr;
503  SpinLockRelease(&slot->mutex);
504 }
#define DEBUG1
Definition: elog.h:25
bool DecodingContextReady(LogicalDecodingContext *ctx)
Definition: logical.c:459
ReplicationSlotPersistentData data
Definition: slot.h:140
XLogRecPtr confirmed_flush
Definition: slot.h:88
XLogRecPtr EndRecPtr
Definition: xlogreader.h:176
#define SpinLockAcquire(lock)
Definition: spin.h:62
XLogRecord * XLogReadRecord(XLogReaderState *state, char **errormsg)
Definition: xlogreader.c:266
#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:238
unsigned int uint32
Definition: c.h:367
#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:113

◆ DecodingContextReady()

bool DecodingContextReady ( LogicalDecodingContext ctx)

Definition at line 459 of file logical.c.

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

Referenced by DecodingContextFindStartpoint().

460 {
462 }
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 804 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().

805 {
807  ErrorContextCallback errcallback;
808  bool ret;
809 
810  Assert(!ctx->fast_forward);
811 
812  /* Push callback + info on the error context stack */
813  state.ctx = ctx;
814  state.callback_name = "filter_by_origin";
817  errcallback.arg = (void *) &state;
818  errcallback.previous = error_context_stack;
819  error_context_stack = &errcallback;
820 
821  /* set output state */
822  ctx->accept_writes = false;
823 
824  /* do the actual work: call callback */
825  ret = ctx->callbacks.filter_by_origin_cb(ctx, origin_id);
826 
827  /* Pop the error context stack */
828  error_context_stack = errcallback.previous;
829 
830  return ret;
831 }
#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:738
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:587
LogicalDecodeFilterByOriginCB filter_by_origin_cb

◆ FreeDecodingContext()

void FreeDecodingContext ( LogicalDecodingContext ctx)

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

512 {
513  if (ctx->callbacks.shutdown_cb != NULL)
514  shutdown_cb_wrapper(ctx);
515 
518  XLogReaderFree(ctx->reader);
520 }
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:634
void XLogReaderFree(XLogReaderState *state)
Definition: xlogreader.c:137
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 1004 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().

1005 {
1006  Assert(lsn != InvalidXLogRecPtr);
1007 
1008  /* Do an unlocked check for candidate_lsn first. */
1011  {
1012  bool updated_xmin = false;
1013  bool updated_restart = false;
1014 
1016 
1018 
1019  /* if we're past the location required for bumping xmin, do so */
1022  {
1023  /*
1024  * We have to write the changed xmin to disk *before* we change
1025  * the in-memory value, otherwise after a crash we wouldn't know
1026  * that some catalog tuples might have been removed already.
1027  *
1028  * Ensure that by first writing to ->xmin and only update
1029  * ->effective_xmin once the new state is synced to disk. After a
1030  * crash ->effective_xmin is set to ->xmin.
1031  */
1034  {
1038  updated_xmin = true;
1039  }
1040  }
1041 
1044  {
1046 
1050  updated_restart = true;
1051  }
1052 
1054 
1055  /* first write new xmin to disk, so we know what's up after a crash */
1056  if (updated_xmin || updated_restart)
1057  {
1060  elog(DEBUG1, "updated xmin: %u restart: %u", updated_xmin, updated_restart);
1061  }
1062 
1063  /*
1064  * Now the new xmin is safely on disk, we can let the global value
1065  * advance. We do not take ProcArrayLock or similar since we only
1066  * advance xmin here and there's not much harm done by a concurrent
1067  * computation missing that.
1068  */
1069  if (updated_xmin)
1070  {
1074 
1077  }
1078  }
1079  else
1080  {
1084  }
1085 }
TransactionId candidate_catalog_xmin
Definition: slot.h:155
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
void ReplicationSlotSave(void)
Definition: slot.c:654
ReplicationSlotPersistentData data
Definition: slot.h:140
XLogRecPtr confirmed_flush
Definition: slot.h:88
#define SpinLockAcquire(lock)
Definition: spin.h:62
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:761
XLogRecPtr candidate_restart_valid
Definition: slot.h:157
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:137
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:738
XLogRecPtr restart_lsn
Definition: slot.h:80
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:156
#define elog(elevel,...)
Definition: elog.h:214
slock_t mutex
Definition: slot.h:113
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:711
XLogRecPtr candidate_restart_lsn
Definition: slot.h:158
void ReplicationSlotMarkDirty(void)
Definition: slot.c:672

◆ LogicalIncreaseRestartDecodingForSlot()

void LogicalIncreaseRestartDecodingForSlot ( XLogRecPtr  current_lsn,
XLogRecPtr  restart_lsn 
)

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

936 {
937  bool updated_lsn = false;
938  ReplicationSlot *slot;
939 
940  slot = MyReplicationSlot;
941 
942  Assert(slot != NULL);
943  Assert(restart_lsn != InvalidXLogRecPtr);
944  Assert(current_lsn != InvalidXLogRecPtr);
945 
946  SpinLockAcquire(&slot->mutex);
947 
948  /* don't overwrite if have a newer restart lsn */
949  if (restart_lsn <= slot->data.restart_lsn)
950  {
951  }
952 
953  /*
954  * We might have already flushed far enough to directly accept this lsn,
955  * in this case there is no need to check for existing candidate LSNs
956  */
957  else if (current_lsn <= slot->data.confirmed_flush)
958  {
959  slot->candidate_restart_valid = current_lsn;
960  slot->candidate_restart_lsn = restart_lsn;
961 
962  /* our candidate can directly be used */
963  updated_lsn = true;
964  }
965 
966  /*
967  * Only increase if the previous values have been applied, otherwise we
968  * might never end up updating if the receiver acks too slowly. A missed
969  * value here will just cause some extra effort after reconnecting.
970  */
972  {
973  slot->candidate_restart_valid = current_lsn;
974  slot->candidate_restart_lsn = restart_lsn;
975 
976  elog(DEBUG1, "got new restart lsn %X/%X at %X/%X",
977  (uint32) (restart_lsn >> 32), (uint32) restart_lsn,
978  (uint32) (current_lsn >> 32), (uint32) current_lsn);
979  }
980  else
981  {
982  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",
983  (uint32) (restart_lsn >> 32), (uint32) restart_lsn,
984  (uint32) (current_lsn >> 32), (uint32) current_lsn,
985  (uint32) (slot->candidate_restart_lsn >> 32),
987  (uint32) (slot->candidate_restart_valid >> 32),
989  (uint32) (slot->data.confirmed_flush >> 32),
990  (uint32) slot->data.confirmed_flush
991  );
992  }
993  SpinLockRelease(&slot->mutex);
994 
995  /* candidates are already valid with the current flush position, apply */
996  if (updated_lsn)
998 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
ReplicationSlotPersistentData data
Definition: slot.h:140
XLogRecPtr confirmed_flush
Definition: slot.h:88
#define SpinLockAcquire(lock)
Definition: spin.h:62
XLogRecPtr candidate_restart_valid
Definition: slot.h:157
unsigned int uint32
Definition: c.h:367
#define SpinLockRelease(lock)
Definition: spin.h:64
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:738
void LogicalConfirmReceivedLocation(XLogRecPtr lsn)
Definition: logical.c:1004
#define elog(elevel,...)
Definition: elog.h:214
slock_t mutex
Definition: slot.h:113
XLogRecPtr candidate_restart_lsn
Definition: slot.h:158

◆ LogicalIncreaseXminForSlot()

void LogicalIncreaseXminForSlot ( XLogRecPtr  lsn,
TransactionId  xmin 
)

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

879 {
880  bool updated_xmin = false;
881  ReplicationSlot *slot;
882 
883  slot = MyReplicationSlot;
884 
885  Assert(slot != NULL);
886 
887  SpinLockAcquire(&slot->mutex);
888 
889  /*
890  * don't overwrite if we already have a newer xmin. This can happen if we
891  * restart decoding in a slot.
892  */
894  {
895  }
896 
897  /*
898  * If the client has already confirmed up to this lsn, we directly can
899  * mark this as accepted. This can happen if we restart decoding in a
900  * slot.
901  */
902  else if (current_lsn <= slot->data.confirmed_flush)
903  {
904  slot->candidate_catalog_xmin = xmin;
905  slot->candidate_xmin_lsn = current_lsn;
906 
907  /* our candidate can directly be used */
908  updated_xmin = true;
909  }
910 
911  /*
912  * Only increase if the previous values have been applied, otherwise we
913  * might never end up updating if the receiver acks too slowly.
914  */
915  else if (slot->candidate_xmin_lsn == InvalidXLogRecPtr)
916  {
917  slot->candidate_catalog_xmin = xmin;
918  slot->candidate_xmin_lsn = current_lsn;
919  }
920  SpinLockRelease(&slot->mutex);
921 
922  /* candidate already valid with the current flush position, apply */
923  if (updated_xmin)
925 }
TransactionId candidate_catalog_xmin
Definition: slot.h:155
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
ReplicationSlotPersistentData data
Definition: slot.h:140
XLogRecPtr confirmed_flush
Definition: slot.h:88
#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:738
void LogicalConfirmReceivedLocation(XLogRecPtr lsn)
Definition: logical.c:1004
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:156
slock_t mutex
Definition: slot.h:113