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 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:972
int wal_level
Definition: xlog.c:103
int errcode(int sqlerrcode)
Definition: elog.c:608
bool RecoveryInProgress(void)
Definition: xlog.c:7935
#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:822

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

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

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

235 {
236  TransactionId xmin_horizon = InvalidTransactionId;
237  ReplicationSlot *slot;
239  MemoryContext old_context;
240 
241  /* shorter lines... */
242  slot = MyReplicationSlot;
243 
244  /* first some sanity checks that are unlikely to be violated */
245  if (slot == NULL)
246  elog(ERROR, "cannot perform logical decoding without an acquired slot");
247 
248  if (plugin == NULL)
249  elog(ERROR, "cannot initialize logical decoding without a specified plugin");
250 
251  /* Make sure the passed slot is suitable. These are user facing errors. */
252  if (SlotIsPhysical(slot))
253  ereport(ERROR,
254  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
255  errmsg("cannot use physical replication slot for logical decoding")));
256 
257  if (slot->data.database != MyDatabaseId)
258  ereport(ERROR,
259  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
260  errmsg("replication slot \"%s\" was not created in this database",
261  NameStr(slot->data.name))));
262 
263  if (IsTransactionState() &&
265  ereport(ERROR,
266  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
267  errmsg("cannot create logical replication slot in transaction that has performed writes")));
268 
269  /* register output plugin name with slot */
270  SpinLockAcquire(&slot->mutex);
272  SpinLockRelease(&slot->mutex);
273 
274  if (XLogRecPtrIsInvalid(restart_lsn))
276  else
277  {
278  SpinLockAcquire(&slot->mutex);
279  slot->data.restart_lsn = restart_lsn;
280  SpinLockRelease(&slot->mutex);
281  }
282 
283  /* ----
284  * This is a bit tricky: We need to determine a safe xmin horizon to start
285  * decoding from, to avoid starting from a running xacts record referring
286  * to xids whose rows have been vacuumed or pruned
287  * already. GetOldestSafeDecodingTransactionId() returns such a value, but
288  * without further interlock its return value might immediately be out of
289  * date.
290  *
291  * So we have to acquire the ProcArrayLock to prevent computation of new
292  * xmin horizons by other backends, get the safe decoding xid, and inform
293  * the slot machinery about the new limit. Once that's done the
294  * ProcArrayLock can be released as the slot machinery now is
295  * protecting against vacuum.
296  *
297  * Note that, temporarily, the data, not just the catalog, xmin has to be
298  * reserved if a data snapshot is to be exported. Otherwise the initial
299  * data snapshot created here is not guaranteed to be valid. After that
300  * the data xmin doesn't need to be managed anymore and the global xmin
301  * should be recomputed. As we are fine with losing the pegged data xmin
302  * after crash - no chance a snapshot would get exported anymore - we can
303  * get away with just setting the slot's
304  * effective_xmin. ReplicationSlotRelease will reset it again.
305  *
306  * ----
307  */
308  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
309 
310  xmin_horizon = GetOldestSafeDecodingTransactionId(!need_full_snapshot);
311 
312  SpinLockAcquire(&slot->mutex);
313  slot->effective_catalog_xmin = xmin_horizon;
314  slot->data.catalog_xmin = xmin_horizon;
315  if (need_full_snapshot)
316  slot->effective_xmin = xmin_horizon;
317  SpinLockRelease(&slot->mutex);
318 
320 
321  LWLockRelease(ProcArrayLock);
322 
325 
326  ctx = StartupDecodingContext(NIL, restart_lsn, xmin_horizon,
327  need_full_snapshot, false,
328  read_page, prepare_write, do_write,
329  update_progress);
330 
331  /* call output plugin initialization callback */
332  old_context = MemoryContextSwitchTo(ctx->context);
333  if (ctx->callbacks.startup_cb != NULL)
334  startup_cb_wrapper(ctx, &ctx->options, true);
335  MemoryContextSwitchTo(old_context);
336 
338 
339  return ctx;
340 }
#define NIL
Definition: pg_list.h:65
static const char * plugin
TransactionId GetOldestSafeDecodingTransactionId(bool catalogOnly)
Definition: procarray.c:2177
struct ReorderBuffer * reorder
Definition: logical.h:42
uint32 TransactionId
Definition: c.h:514
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:608
MemoryContext context
Definition: logical.h:35
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:604
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:409
#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:935
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:118
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
bool IsTransactionState(void)
Definition: xact.c:355
LogicalDecodeStartupCB startup_cb
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228
#define NameStr(name)
Definition: c.h:616
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 462 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().

463 {
465  ReplicationSlot *slot = ctx->slot;
466 
467  /* Initialize from where to start reading WAL. */
468  startptr = slot->data.restart_lsn;
469 
470  elog(DEBUG1, "searching for logical decoding starting point, starting at %X/%X",
471  (uint32) (slot->data.restart_lsn >> 32),
472  (uint32) slot->data.restart_lsn);
473 
474  /* Wait for a consistent starting point */
475  for (;;)
476  {
477  XLogRecord *record;
478  char *err = NULL;
479 
480  /* the read_page callback waits for new WAL */
481  record = XLogReadRecord(ctx->reader, startptr, &err);
482  if (err)
483  elog(ERROR, "%s", err);
484  if (!record)
485  elog(ERROR, "no record found"); /* shouldn't happen */
486 
487  startptr = InvalidXLogRecPtr;
488 
490 
491  /* only continue till we found a consistent spot */
492  if (DecodingContextReady(ctx))
493  break;
494 
496  }
497 
498  SpinLockAcquire(&slot->mutex);
499  slot->data.confirmed_flush = ctx->reader->EndRecPtr;
500  SpinLockRelease(&slot->mutex);
501 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
bool DecodingContextReady(LogicalDecodingContext *ctx)
Definition: logical.c:453
ReplicationSlotPersistentData data
Definition: slot.h:132
XLogRecord * XLogReadRecord(XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
Definition: xlogreader.c:238
XLogRecPtr confirmed_flush
Definition: slot.h:80
XLogRecPtr EndRecPtr
Definition: xlogreader.h:132
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define ERROR
Definition: elog.h:43
void LogicalDecodingProcessRecord(LogicalDecodingContext *ctx, XLogReaderState *record)
Definition: decode.c:94
unsigned int uint32
Definition: c.h:359
#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:41
ReplicationSlot * slot
Definition: logical.h:38
#define elog(elevel,...)
Definition: elog.h:228
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
slock_t mutex
Definition: slot.h:105
static XLogRecPtr startptr
Definition: basebackup.c:117

◆ DecodingContextReady()

bool DecodingContextReady ( LogicalDecodingContext ctx)

Definition at line 453 of file logical.c.

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

Referenced by DecodingContextFindStartpoint().

454 {
456 }
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 801 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().

802 {
804  ErrorContextCallback errcallback;
805  bool ret;
806 
807  Assert(!ctx->fast_forward);
808 
809  /* Push callback + info on the error context stack */
810  state.ctx = ctx;
811  state.callback_name = "filter_by_origin";
814  errcallback.arg = (void *) &state;
815  errcallback.previous = error_context_stack;
816  error_context_stack = &errcallback;
817 
818  /* set output state */
819  ctx->accept_writes = false;
820 
821  /* do the actual work: call callback */
822  ret = ctx->callbacks.filter_by_origin_cb(ctx, origin_id);
823 
824  /* Pop the error context stack */
825  error_context_stack = errcallback.previous;
826 
827  return ret;
828 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:52
void(* callback)(void *arg)
Definition: elog.h:256
struct ErrorContextCallback * previous
Definition: elog.h:255
const char * callback_name
Definition: logical.c:48
ErrorContextCallback * error_context_stack
Definition: elog.c:91
#define Assert(condition)
Definition: c.h:739
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:584
LogicalDecodeFilterByOriginCB filter_by_origin_cb

◆ FreeDecodingContext()

void FreeDecodingContext ( LogicalDecodingContext ctx)

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

509 {
510  if (ctx->callbacks.shutdown_cb != NULL)
511  shutdown_cb_wrapper(ctx);
512 
515  XLogReaderFree(ctx->reader);
517 }
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:631
void XLogReaderFree(XLogReaderState *state)
Definition: xlogreader.c:135
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 1001 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().

1002 {
1003  Assert(lsn != InvalidXLogRecPtr);
1004 
1005  /* Do an unlocked check for candidate_lsn first. */
1008  {
1009  bool updated_xmin = false;
1010  bool updated_restart = false;
1011 
1013 
1015 
1016  /* if we're past the location required for bumping xmin, do so */
1019  {
1020  /*
1021  * We have to write the changed xmin to disk *before* we change
1022  * the in-memory value, otherwise after a crash we wouldn't know
1023  * that some catalog tuples might have been removed already.
1024  *
1025  * Ensure that by first writing to ->xmin and only update
1026  * ->effective_xmin once the new state is synced to disk. After a
1027  * crash ->effective_xmin is set to ->xmin.
1028  */
1031  {
1035  updated_xmin = true;
1036  }
1037  }
1038 
1041  {
1043 
1047  updated_restart = true;
1048  }
1049 
1051 
1052  /* first write new xmin to disk, so we know what's up after a crash */
1053  if (updated_xmin || updated_restart)
1054  {
1057  elog(DEBUG1, "updated xmin: %u restart: %u", updated_xmin, updated_restart);
1058  }
1059 
1060  /*
1061  * Now the new xmin is safely on disk, we can let the global value
1062  * advance. We do not take ProcArrayLock or similar since we only
1063  * advance xmin here and there's not much harm done by a concurrent
1064  * computation missing that.
1065  */
1066  if (updated_xmin)
1067  {
1071 
1074  }
1075  }
1076  else
1077  {
1081  }
1082 }
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:739
XLogRecPtr restart_lsn
Definition: slot.h:72
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:148
#define elog(elevel,...)
Definition: elog.h:228
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 932 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().

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

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