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, XLogPageReadCB read_page, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
 
LogicalDecodingContextCreateDecodingContext (XLogRecPtr start_lsn, List *output_plugin_options, bool fast_forward, XLogPageReadCB read_page, 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 28 of file logical.h.

◆ LogicalOutputPluginWriterWrite

typedef void(* LogicalOutputPluginWriterWrite) (struct LogicalDecodingContext *lr, XLogRecPtr Ptr, TransactionId xid, bool last_write)

Definition at line 20 of file logical.h.

Function Documentation

◆ CheckLogicalDecodingRequirements()

void CheckLogicalDecodingRequirements ( void  )

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

80 {
82 
83  /*
84  * NB: Adding a new requirement likely means that RestoreSlotFromDisk()
85  * needs the same check.
86  */
87 
89  ereport(ERROR,
90  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
91  errmsg("logical decoding requires wal_level >= logical")));
92 
93  if (MyDatabaseId == InvalidOid)
94  ereport(ERROR,
95  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
96  errmsg("logical decoding requires a database connection")));
97 
98  /* ----
99  * TODO: We got to change that someday soon...
100  *
101  * There's basically three things missing to allow this:
102  * 1) We need to be able to correctly and quickly identify the timeline a
103  * LSN belongs to
104  * 2) We need to force hot_standby_feedback to be enabled at all times so
105  * the primary cannot remove rows we need.
106  * 3) support dropping replication slots referring to a database, in
107  * dbase_redo. There can't be any active ones due to HS recovery
108  * conflicts, so that should be relatively easy.
109  * ----
110  */
111  if (RecoveryInProgress())
112  ereport(ERROR,
113  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
114  errmsg("logical decoding cannot be used while in recovery")));
115 }
void CheckSlotRequirements(void)
Definition: slot.c:972
int wal_level
Definition: xlog.c:103
int errcode(int sqlerrcode)
Definition: elog.c:570
bool RecoveryInProgress(void)
Definition: xlog.c:7917
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
Oid MyDatabaseId
Definition: globals.c:85
#define InvalidOid
Definition: postgres_ext.h:36
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ CreateDecodingContext()

LogicalDecodingContext* CreateDecodingContext ( XLogRecPtr  start_lsn,
List output_plugin_options,
bool  fast_forward,
XLogPageReadCB  read_page,
LogicalOutputPluginWriterPrepareWrite  prepare_write,
LogicalOutputPluginWriterWrite  do_write,
LogicalOutputPluginWriterUpdateProgress  update_progress 
)

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

381 {
383  ReplicationSlot *slot;
384  MemoryContext old_context;
385 
386  /* shorter lines... */
387  slot = MyReplicationSlot;
388 
389  /* first some sanity checks that are unlikely to be violated */
390  if (slot == NULL)
391  elog(ERROR, "cannot perform logical decoding without an acquired slot");
392 
393  /* make sure the passed slot is suitable, these are user facing errors */
394  if (SlotIsPhysical(slot))
395  ereport(ERROR,
396  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
397  (errmsg("cannot use physical replication slot for logical decoding"))));
398 
399  if (slot->data.database != MyDatabaseId)
400  ereport(ERROR,
401  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
402  (errmsg("replication slot \"%s\" was not created in this database",
403  NameStr(slot->data.name)))));
404 
405  if (start_lsn == InvalidXLogRecPtr)
406  {
407  /* continue from last position */
408  start_lsn = slot->data.confirmed_flush;
409  }
410  else if (start_lsn < slot->data.confirmed_flush)
411  {
412  /*
413  * It might seem like we should error out in this case, but it's
414  * pretty common for a client to acknowledge a LSN it doesn't have to
415  * do anything for, and thus didn't store persistently, because the
416  * xlog records didn't result in anything relevant for logical
417  * decoding. Clients have to be able to do that to support synchronous
418  * replication.
419  */
420  elog(DEBUG1, "cannot stream from %X/%X, minimum is %X/%X, forwarding",
421  (uint32) (start_lsn >> 32), (uint32) start_lsn,
422  (uint32) (slot->data.confirmed_flush >> 32),
423  (uint32) slot->data.confirmed_flush);
424 
425  start_lsn = slot->data.confirmed_flush;
426  }
427 
428  ctx = StartupDecodingContext(output_plugin_options,
429  start_lsn, InvalidTransactionId, false,
430  fast_forward, read_page, prepare_write,
431  do_write, update_progress);
432 
433  /* call output plugin initialization callback */
434  old_context = MemoryContextSwitchTo(ctx->context);
435  if (ctx->callbacks.startup_cb != NULL)
436  startup_cb_wrapper(ctx, &ctx->options, false);
437  MemoryContextSwitchTo(old_context);
438 
440 
441  ereport(LOG,
442  (errmsg("starting logical decoding for slot \"%s\"",
443  NameStr(slot->data.name)),
444  errdetail("Streaming transactions committing after %X/%X, reading WAL from %X/%X.",
445  (uint32) (slot->data.confirmed_flush >> 32),
446  (uint32) slot->data.confirmed_flush,
447  (uint32) (slot->data.restart_lsn >> 32),
448  (uint32) slot->data.restart_lsn)));
449 
450  return ctx;
451 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
struct ReorderBuffer * reorder
Definition: logical.h:43
OutputPluginOptions options
Definition: logical.h:54
OutputPluginCallbacks callbacks
Definition: logical.h:53
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:570
MemoryContext context
Definition: logical.h:36
ReplicationSlotPersistentData data
Definition: slot.h:132
#define LOG
Definition: elog.h:26
#define SlotIsPhysical(slot)
Definition: slot.h:153
XLogRecPtr confirmed_flush
Definition: slot.h:80
#define ERROR
Definition: elog.h:43
static void startup_cb_wrapper(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
Definition: logical.c:608
int errdetail(const char *fmt,...)
Definition: elog.c:860
#define InvalidTransactionId
Definition: transam.h:31
unsigned int uint32
Definition: c.h:358
#define ereport(elevel, rest)
Definition: elog.h:141
Oid MyDatabaseId
Definition: globals.c:85
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
XLogRecPtr restart_lsn
Definition: slot.h:72
static LogicalDecodingContext * StartupDecodingContext(List *output_plugin_options, XLogRecPtr start_lsn, TransactionId xmin_horizon, bool need_full_snapshot, bool fast_forward, XLogPageReadCB read_page, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
Definition: logical.c:122
LogicalDecodeStartupCB startup_cb
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define elog(elevel,...)
Definition: elog.h:226
#define NameStr(name)
Definition: c.h:609

◆ CreateInitDecodingContext()

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

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

239 {
240  TransactionId xmin_horizon = InvalidTransactionId;
241  ReplicationSlot *slot;
243  MemoryContext old_context;
244 
245  /* shorter lines... */
246  slot = MyReplicationSlot;
247 
248  /* first some sanity checks that are unlikely to be violated */
249  if (slot == NULL)
250  elog(ERROR, "cannot perform logical decoding without an acquired slot");
251 
252  if (plugin == NULL)
253  elog(ERROR, "cannot initialize logical decoding without a specified plugin");
254 
255  /* Make sure the passed slot is suitable. These are user facing errors. */
256  if (SlotIsPhysical(slot))
257  ereport(ERROR,
258  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
259  errmsg("cannot use physical replication slot for logical decoding")));
260 
261  if (slot->data.database != MyDatabaseId)
262  ereport(ERROR,
263  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
264  errmsg("replication slot \"%s\" was not created in this database",
265  NameStr(slot->data.name))));
266 
267  if (IsTransactionState() &&
269  ereport(ERROR,
270  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
271  errmsg("cannot create logical replication slot in transaction that has performed writes")));
272 
273  /* register output plugin name with slot */
274  SpinLockAcquire(&slot->mutex);
276  SpinLockRelease(&slot->mutex);
277 
278  if (XLogRecPtrIsInvalid(restart_lsn))
280  else
281  {
282  SpinLockAcquire(&slot->mutex);
283  slot->data.restart_lsn = restart_lsn;
284  SpinLockRelease(&slot->mutex);
285  }
286 
287  /* ----
288  * This is a bit tricky: We need to determine a safe xmin horizon to start
289  * decoding from, to avoid starting from a running xacts record referring
290  * to xids whose rows have been vacuumed or pruned
291  * already. GetOldestSafeDecodingTransactionId() returns such a value, but
292  * without further interlock its return value might immediately be out of
293  * date.
294  *
295  * So we have to acquire the ProcArrayLock to prevent computation of new
296  * xmin horizons by other backends, get the safe decoding xid, and inform
297  * the slot machinery about the new limit. Once that's done the
298  * ProcArrayLock can be released as the slot machinery now is
299  * protecting against vacuum.
300  *
301  * Note that, temporarily, the data, not just the catalog, xmin has to be
302  * reserved if a data snapshot is to be exported. Otherwise the initial
303  * data snapshot created here is not guaranteed to be valid. After that
304  * the data xmin doesn't need to be managed anymore and the global xmin
305  * should be recomputed. As we are fine with losing the pegged data xmin
306  * after crash - no chance a snapshot would get exported anymore - we can
307  * get away with just setting the slot's
308  * effective_xmin. ReplicationSlotRelease will reset it again.
309  *
310  * ----
311  */
312  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
313 
314  xmin_horizon = GetOldestSafeDecodingTransactionId(!need_full_snapshot);
315 
316  SpinLockAcquire(&slot->mutex);
317  slot->effective_catalog_xmin = xmin_horizon;
318  slot->data.catalog_xmin = xmin_horizon;
319  if (need_full_snapshot)
320  slot->effective_xmin = xmin_horizon;
321  SpinLockRelease(&slot->mutex);
322 
324 
325  LWLockRelease(ProcArrayLock);
326 
329 
330  ctx = StartupDecodingContext(NIL, restart_lsn, xmin_horizon,
331  need_full_snapshot, false,
332  read_page, prepare_write, do_write,
333  update_progress);
334 
335  /* call output plugin initialization callback */
336  old_context = MemoryContextSwitchTo(ctx->context);
337  if (ctx->callbacks.startup_cb != NULL)
338  startup_cb_wrapper(ctx, &ctx->options, true);
339  MemoryContextSwitchTo(old_context);
340 
342 
343  return ctx;
344 }
#define NIL
Definition: pg_list.h:65
static const char * plugin
TransactionId GetOldestSafeDecodingTransactionId(bool catalogOnly)
Definition: procarray.c:2175
struct ReorderBuffer * reorder
Definition: logical.h:43
uint32 TransactionId
Definition: c.h:507
OutputPluginOptions options
Definition: logical.h:54
OutputPluginCallbacks callbacks
Definition: logical.h:53
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:570
MemoryContext context
Definition: logical.h:36
void ReplicationSlotSave(void)
Definition: slot.c:645
ReplicationSlotPersistentData data
Definition: slot.h:132
#define SlotIsPhysical(slot)
Definition: slot.h:153
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
#define NAMEDATALEN
#define SpinLockAcquire(lock)
Definition: spin.h:62
void ReplicationSlotReserveWal(void)
Definition: slot.c:997
#define ERROR
Definition: elog.h:43
static void startup_cb_wrapper(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
Definition: logical.c:608
TransactionId effective_xmin
Definition: slot.h:128
TransactionId catalog_xmin
Definition: slot.h:69
#define InvalidTransactionId
Definition: transam.h:31
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:410
#define ereport(elevel, rest)
Definition: elog.h:141
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
#define SpinLockRelease(lock)
Definition: spin.h:64
TransactionId effective_catalog_xmin
Definition: slot.h:129
Oid MyDatabaseId
Definition: globals.c:85
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define StrNCpy(dst, src, len)
Definition: c.h:928
XLogRecPtr restart_lsn
Definition: slot.h:72
static LogicalDecodingContext * StartupDecodingContext(List *output_plugin_options, XLogRecPtr start_lsn, TransactionId xmin_horizon, bool need_full_snapshot, bool fast_forward, XLogPageReadCB read_page, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
Definition: logical.c:122
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
bool IsTransactionState(void)
Definition: xact.c:356
LogicalDecodeStartupCB startup_cb
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define elog(elevel,...)
Definition: elog.h:226
#define NameStr(name)
Definition: c.h:609
slock_t mutex
Definition: slot.h:105
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:702
void ReplicationSlotMarkDirty(void)
Definition: slot.c:663

◆ DecodingContextFindStartpoint()

void DecodingContextFindStartpoint ( LogicalDecodingContext ctx)

Definition at line 466 of file logical.c.

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

Referenced by create_logical_replication_slot(), and CreateReplicationSlot().

467 {
469  ReplicationSlot *slot = ctx->slot;
470 
471  /* Initialize from where to start reading WAL. */
472  startptr = slot->data.restart_lsn;
473 
474  elog(DEBUG1, "searching for logical decoding starting point, starting at %X/%X",
475  (uint32) (slot->data.restart_lsn >> 32),
476  (uint32) slot->data.restart_lsn);
477 
478  /* Wait for a consistent starting point */
479  for (;;)
480  {
481  XLogRecord *record;
482  char *err = NULL;
483 
484  /* the read_page callback waits for new WAL */
485  record = XLogReadRecord(ctx->reader, startptr, &err);
486  if (err)
487  elog(ERROR, "%s", err);
488  if (!record)
489  elog(ERROR, "no record found"); /* shouldn't happen */
490 
491  startptr = InvalidXLogRecPtr;
492 
494 
495  /* only continue till we found a consistent spot */
496  if (DecodingContextReady(ctx))
497  break;
498 
500  }
501 
502  SpinLockAcquire(&slot->mutex);
503  slot->data.confirmed_flush = ctx->reader->EndRecPtr;
504  SpinLockRelease(&slot->mutex);
505 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
bool DecodingContextReady(LogicalDecodingContext *ctx)
Definition: logical.c:457
ReplicationSlotPersistentData data
Definition: slot.h:132
XLogRecord * XLogReadRecord(XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
Definition: xlogreader.c:237
XLogRecPtr confirmed_flush
Definition: slot.h:80
XLogRecPtr EndRecPtr
Definition: xlogreader.h:133
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define ERROR
Definition: elog.h:43
void LogicalDecodingProcessRecord(LogicalDecodingContext *ctx, XLogReaderState *record)
Definition: decode.c:97
unsigned int uint32
Definition: c.h:358
#define SpinLockRelease(lock)
Definition: spin.h:64
uint64 XLogRecPtr
Definition: xlogdefs.h:21
XLogRecPtr restart_lsn
Definition: slot.h:72
XLogReaderState * reader
Definition: logical.h:42
ReplicationSlot * slot
Definition: logical.h:39
#define elog(elevel,...)
Definition: elog.h:226
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
slock_t mutex
Definition: slot.h:105
static XLogRecPtr startptr
Definition: basebackup.c:118

◆ DecodingContextReady()

bool DecodingContextReady ( LogicalDecodingContext ctx)

Definition at line 457 of file logical.c.

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

Referenced by DecodingContextFindStartpoint().

458 {
460 }
SnapBuildState SnapBuildCurrentState(SnapBuild *builder)
Definition: snapbuild.c:400
struct SnapBuild * snapshot_builder
Definition: logical.h:44

◆ filter_by_origin_cb_wrapper()

bool filter_by_origin_cb_wrapper ( LogicalDecodingContext ctx,
RepOriginId  origin_id 
)

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

806 {
808  ErrorContextCallback errcallback;
809  bool ret;
810 
811  Assert(!ctx->fast_forward);
812 
813  /* Push callback + info on the error context stack */
814  state.ctx = ctx;
815  state.callback_name = "filter_by_origin";
818  errcallback.arg = (void *) &state;
819  errcallback.previous = error_context_stack;
820  error_context_stack = &errcallback;
821 
822  /* set output state */
823  ctx->accept_writes = false;
824 
825  /* do the actual work: call callback */
826  ret = ctx->callbacks.filter_by_origin_cb(ctx, origin_id);
827 
828  /* Pop the error context stack */
829  error_context_stack = errcallback.previous;
830 
831  return ret;
832 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:53
void(* callback)(void *arg)
Definition: elog.h:254
struct ErrorContextCallback * previous
Definition: elog.h:253
const char * callback_name
Definition: logical.c:52
ErrorContextCallback * error_context_stack
Definition: elog.c:88
#define Assert(condition)
Definition: c.h:732
Definition: regguts.h:298
XLogRecPtr report_location
Definition: logical.c:53
LogicalDecodingContext * ctx
Definition: logical.c:51
static void output_plugin_error_callback(void *arg)
Definition: logical.c:588
LogicalDecodeFilterByOriginCB filter_by_origin_cb

◆ FreeDecodingContext()

void FreeDecodingContext ( LogicalDecodingContext ctx)

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

513 {
514  if (ctx->callbacks.shutdown_cb != NULL)
515  shutdown_cb_wrapper(ctx);
516 
519  XLogReaderFree(ctx->reader);
521 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
struct ReorderBuffer * reorder
Definition: logical.h:43
OutputPluginCallbacks callbacks
Definition: logical.h:53
void ReorderBufferFree(ReorderBuffer *rb)
MemoryContext context
Definition: logical.h:36
static void shutdown_cb_wrapper(LogicalDecodingContext *ctx)
Definition: logical.c:635
void XLogReaderFree(XLogReaderState *state)
Definition: xlogreader.c:133
void FreeSnapshotBuilder(SnapBuild *builder)
Definition: snapbuild.c:356
struct SnapBuild * snapshot_builder
Definition: logical.h:44
LogicalDecodeShutdownCB shutdown_cb
XLogReaderState * reader
Definition: logical.h:42

◆ LogicalConfirmReceivedLocation()

void LogicalConfirmReceivedLocation ( XLogRecPtr  lsn)

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

1006 {
1007  Assert(lsn != InvalidXLogRecPtr);
1008 
1009  /* Do an unlocked check for candidate_lsn first. */
1012  {
1013  bool updated_xmin = false;
1014  bool updated_restart = false;
1015 
1017 
1019 
1020  /* if we're past the location required for bumping xmin, do so */
1023  {
1024  /*
1025  * We have to write the changed xmin to disk *before* we change
1026  * the in-memory value, otherwise after a crash we wouldn't know
1027  * that some catalog tuples might have been removed already.
1028  *
1029  * Ensure that by first writing to ->xmin and only update
1030  * ->effective_xmin once the new state is synced to disk. After a
1031  * crash ->effective_xmin is set to ->xmin.
1032  */
1035  {
1039  updated_xmin = true;
1040  }
1041  }
1042 
1045  {
1047 
1051  updated_restart = true;
1052  }
1053 
1055 
1056  /* first write new xmin to disk, so we know what's up after a crash */
1057  if (updated_xmin || updated_restart)
1058  {
1061  elog(DEBUG1, "updated xmin: %u restart: %u", updated_xmin, updated_restart);
1062  }
1063 
1064  /*
1065  * Now the new xmin is safely on disk, we can let the global value
1066  * advance. We do not take ProcArrayLock or similar since we only
1067  * advance xmin here and there's not much harm done by a concurrent
1068  * computation missing that.
1069  */
1070  if (updated_xmin)
1071  {
1075 
1078  }
1079  }
1080  else
1081  {
1085  }
1086 }
TransactionId candidate_catalog_xmin
Definition: slot.h:147
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
void ReplicationSlotSave(void)
Definition: slot.c:645
ReplicationSlotPersistentData data
Definition: slot.h:132
XLogRecPtr confirmed_flush
Definition: slot.h:80
#define SpinLockAcquire(lock)
Definition: spin.h:62
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:748
XLogRecPtr candidate_restart_valid
Definition: slot.h:149
TransactionId catalog_xmin
Definition: slot.h:69
#define InvalidTransactionId
Definition: transam.h:31
#define SpinLockRelease(lock)
Definition: spin.h:64
TransactionId effective_catalog_xmin
Definition: slot.h:129
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:732
XLogRecPtr restart_lsn
Definition: slot.h:72
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:148
#define elog(elevel,...)
Definition: elog.h:226
slock_t mutex
Definition: slot.h:105
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:702
XLogRecPtr candidate_restart_lsn
Definition: slot.h:150
void ReplicationSlotMarkDirty(void)
Definition: slot.c:663

◆ LogicalIncreaseRestartDecodingForSlot()

void LogicalIncreaseRestartDecodingForSlot ( XLogRecPtr  current_lsn,
XLogRecPtr  restart_lsn 
)

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

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

◆ LogicalIncreaseXminForSlot()

void LogicalIncreaseXminForSlot ( XLogRecPtr  lsn,
TransactionId  xmin 
)

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

880 {
881  bool updated_xmin = false;
882  ReplicationSlot *slot;
883 
884  slot = MyReplicationSlot;
885 
886  Assert(slot != NULL);
887 
888  SpinLockAcquire(&slot->mutex);
889 
890  /*
891  * don't overwrite if we already have a newer xmin. This can happen if we
892  * restart decoding in a slot.
893  */
895  {
896  }
897 
898  /*
899  * If the client has already confirmed up to this lsn, we directly can
900  * mark this as accepted. This can happen if we restart decoding in a
901  * slot.
902  */
903  else if (current_lsn <= slot->data.confirmed_flush)
904  {
905  slot->candidate_catalog_xmin = xmin;
906  slot->candidate_xmin_lsn = current_lsn;
907 
908  /* our candidate can directly be used */
909  updated_xmin = true;
910  }
911 
912  /*
913  * Only increase if the previous values have been applied, otherwise we
914  * might never end up updating if the receiver acks too slowly.
915  */
916  else if (slot->candidate_xmin_lsn == InvalidXLogRecPtr)
917  {
918  slot->candidate_catalog_xmin = xmin;
919  slot->candidate_xmin_lsn = current_lsn;
920  }
921  SpinLockRelease(&slot->mutex);
922 
923  /* candidate already valid with the current flush position, apply */
924  if (updated_xmin)
926 }
TransactionId candidate_catalog_xmin
Definition: slot.h:147
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
ReplicationSlotPersistentData data
Definition: slot.h:132
XLogRecPtr confirmed_flush
Definition: slot.h:80
#define SpinLockAcquire(lock)
Definition: spin.h:62
bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:319
TransactionId catalog_xmin
Definition: slot.h:69
#define SpinLockRelease(lock)
Definition: spin.h:64
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:732
void LogicalConfirmReceivedLocation(XLogRecPtr lsn)
Definition: logical.c:1005
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:148
slock_t mutex
Definition: slot.h:105