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, 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 29 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 78 of file logical.c.

References CheckSlotRequirements(), ereport, errcode(), errmsg(), ERROR, InvalidOid, MyDatabaseId, RecoveryInProgress(), wal_level, and WAL_LEVEL_LOGICAL.

Referenced by CreateReplicationSlot(), pg_create_logical_replication_slot(), pg_logical_slot_get_changes_guts(), and StartLogicalReplication().

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

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

360 {
362  ReplicationSlot *slot;
363  MemoryContext old_context;
364 
365  /* shorter lines... */
366  slot = MyReplicationSlot;
367 
368  /* first some sanity checks that are unlikely to be violated */
369  if (slot == NULL)
370  elog(ERROR, "cannot perform logical decoding without an acquired slot");
371 
372  /* make sure the passed slot is suitable, these are user facing errors */
373  if (SlotIsPhysical(slot))
374  ereport(ERROR,
375  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
376  (errmsg("cannot use physical replication slot for logical decoding"))));
377 
378  if (slot->data.database != MyDatabaseId)
379  ereport(ERROR,
380  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
381  (errmsg("replication slot \"%s\" was not created in this database",
382  NameStr(slot->data.name)))));
383 
384  if (start_lsn == InvalidXLogRecPtr)
385  {
386  /* continue from last position */
387  start_lsn = slot->data.confirmed_flush;
388  }
389  else if (start_lsn < slot->data.confirmed_flush)
390  {
391  /*
392  * It might seem like we should error out in this case, but it's
393  * pretty common for a client to acknowledge a LSN it doesn't have to
394  * do anything for, and thus didn't store persistently, because the
395  * xlog records didn't result in anything relevant for logical
396  * decoding. Clients have to be able to do that to support synchronous
397  * replication.
398  */
399  elog(DEBUG1, "cannot stream from %X/%X, minimum is %X/%X, forwarding",
400  (uint32) (start_lsn >> 32), (uint32) start_lsn,
401  (uint32) (slot->data.confirmed_flush >> 32),
402  (uint32) slot->data.confirmed_flush);
403 
404  start_lsn = slot->data.confirmed_flush;
405  }
406 
407  ctx = StartupDecodingContext(output_plugin_options,
408  start_lsn, InvalidTransactionId, false,
409  fast_forward, read_page, prepare_write,
410  do_write, update_progress);
411 
412  /* call output plugin initialization callback */
413  old_context = MemoryContextSwitchTo(ctx->context);
414  if (ctx->callbacks.startup_cb != NULL)
415  startup_cb_wrapper(ctx, &ctx->options, false);
416  MemoryContextSwitchTo(old_context);
417 
419 
420  ereport(LOG,
421  (errmsg("starting logical decoding for slot \"%s\"",
422  NameStr(slot->data.name)),
423  errdetail("Streaming transactions committing after %X/%X, reading WAL from %X/%X.",
424  (uint32) (slot->data.confirmed_flush >> 32),
425  (uint32) slot->data.confirmed_flush,
426  (uint32) (slot->data.restart_lsn >> 32),
427  (uint32) slot->data.restart_lsn)));
428 
429  return ctx;
430 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
struct ReorderBuffer * reorder
Definition: logical.h:45
OutputPluginOptions options
Definition: logical.h:56
OutputPluginCallbacks callbacks
Definition: logical.h:55
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
MemoryContext context
Definition: logical.h:38
ReplicationSlotPersistentData data
Definition: slot.h:120
#define LOG
Definition: elog.h:26
#define SlotIsPhysical(slot)
Definition: slot.h:141
XLogRecPtr confirmed_flush
Definition: slot.h:81
#define ERROR
Definition: elog.h:43
static void startup_cb_wrapper(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
Definition: logical.c:584
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define InvalidTransactionId
Definition: transam.h:31
unsigned int uint32
Definition: c.h:325
#define ereport(elevel, rest)
Definition: elog.h:122
Oid MyDatabaseId
Definition: globals.c:84
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
XLogRecPtr restart_lsn
Definition: slot.h:73
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:116
LogicalDecodeStartupCB startup_cb
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define NameStr(name)
Definition: c.h:576
#define elog
Definition: elog.h:219

◆ CreateInitDecodingContext()

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

Definition at line 223 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, InvalidXLogRecPtr, 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(), SlotIsPhysical, SpinLockAcquire, SpinLockRelease, OutputPluginCallbacks::startup_cb, startup_cb_wrapper(), StartupDecodingContext(), and StrNCpy.

Referenced by CreateReplicationSlot(), and pg_create_logical_replication_slot().

230 {
231  TransactionId xmin_horizon = InvalidTransactionId;
232  ReplicationSlot *slot;
234  MemoryContext old_context;
235 
236  /* shorter lines... */
237  slot = MyReplicationSlot;
238 
239  /* first some sanity checks that are unlikely to be violated */
240  if (slot == NULL)
241  elog(ERROR, "cannot perform logical decoding without an acquired slot");
242 
243  if (plugin == NULL)
244  elog(ERROR, "cannot initialize logical decoding without a specified plugin");
245 
246  /* Make sure the passed slot is suitable. These are user facing errors. */
247  if (SlotIsPhysical(slot))
248  ereport(ERROR,
249  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
250  errmsg("cannot use physical replication slot for logical decoding")));
251 
252  if (slot->data.database != MyDatabaseId)
253  ereport(ERROR,
254  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
255  errmsg("replication slot \"%s\" was not created in this database",
256  NameStr(slot->data.name))));
257 
258  if (IsTransactionState() &&
260  ereport(ERROR,
261  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
262  errmsg("cannot create logical replication slot in transaction that has performed writes")));
263 
264  /* register output plugin name with slot */
265  SpinLockAcquire(&slot->mutex);
267  SpinLockRelease(&slot->mutex);
268 
270 
271  /* ----
272  * This is a bit tricky: We need to determine a safe xmin horizon to start
273  * decoding from, to avoid starting from a running xacts record referring
274  * to xids whose rows have been vacuumed or pruned
275  * already. GetOldestSafeDecodingTransactionId() returns such a value, but
276  * without further interlock its return value might immediately be out of
277  * date.
278  *
279  * So we have to acquire the ProcArrayLock to prevent computation of new
280  * xmin horizons by other backends, get the safe decoding xid, and inform
281  * the slot machinery about the new limit. Once that's done the
282  * ProcArrayLock can be released as the slot machinery now is
283  * protecting against vacuum.
284  *
285  * Note that, temporarily, the data, not just the catalog, xmin has to be
286  * reserved if a data snapshot is to be exported. Otherwise the initial
287  * data snapshot created here is not guaranteed to be valid. After that
288  * the data xmin doesn't need to be managed anymore and the global xmin
289  * should be recomputed. As we are fine with losing the pegged data xmin
290  * after crash - no chance a snapshot would get exported anymore - we can
291  * get away with just setting the slot's
292  * effective_xmin. ReplicationSlotRelease will reset it again.
293  *
294  * ----
295  */
296  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
297 
298  xmin_horizon = GetOldestSafeDecodingTransactionId(!need_full_snapshot);
299 
300  slot->effective_catalog_xmin = xmin_horizon;
301  slot->data.catalog_xmin = xmin_horizon;
302  if (need_full_snapshot)
303  slot->effective_xmin = xmin_horizon;
304 
306 
307  LWLockRelease(ProcArrayLock);
308 
311 
312  ctx = StartupDecodingContext(NIL, InvalidXLogRecPtr, xmin_horizon,
313  need_full_snapshot, true,
314  read_page, prepare_write, do_write,
315  update_progress);
316 
317  /* call output plugin initialization callback */
318  old_context = MemoryContextSwitchTo(ctx->context);
319  if (ctx->callbacks.startup_cb != NULL)
320  startup_cb_wrapper(ctx, &ctx->options, true);
321  MemoryContextSwitchTo(old_context);
322 
324 
325  return ctx;
326 }
#define NIL
Definition: pg_list.h:69
static const char * plugin
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
TransactionId GetOldestSafeDecodingTransactionId(bool catalogOnly)
Definition: procarray.c:2158
struct ReorderBuffer * reorder
Definition: logical.h:45
uint32 TransactionId
Definition: c.h:474
OutputPluginOptions options
Definition: logical.h:56
OutputPluginCallbacks callbacks
Definition: logical.h:55
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
MemoryContext context
Definition: logical.h:38
void ReplicationSlotSave(void)
Definition: slot.c:638
ReplicationSlotPersistentData data
Definition: slot.h:120
#define SlotIsPhysical(slot)
Definition: slot.h:141
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1725
#define NAMEDATALEN
#define SpinLockAcquire(lock)
Definition: spin.h:62
void ReplicationSlotReserveWal(void)
Definition: slot.c:985
#define ERROR
Definition: elog.h:43
static void startup_cb_wrapper(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
Definition: logical.c:584
TransactionId effective_xmin
Definition: slot.h:116
TransactionId catalog_xmin
Definition: slot.h:70
#define InvalidTransactionId
Definition: transam.h:31
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:404
#define ereport(elevel, rest)
Definition: elog.h:122
#define SpinLockRelease(lock)
Definition: spin.h:64
TransactionId effective_catalog_xmin
Definition: slot.h:117
Oid MyDatabaseId
Definition: globals.c:84
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define StrNCpy(dst, src, len)
Definition: c.h:881
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:116
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1121
bool IsTransactionState(void)
Definition: xact.c:350
LogicalDecodeStartupCB startup_cb
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define NameStr(name)
Definition: c.h:576
#define elog
Definition: elog.h:219
slock_t mutex
Definition: slot.h:93
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:695
void ReplicationSlotMarkDirty(void)
Definition: slot.c:656

◆ DecodingContextFindStartpoint()

void DecodingContextFindStartpoint ( LogicalDecodingContext ctx)

Definition at line 445 of file logical.c.

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

Referenced by CreateReplicationSlot(), and pg_create_logical_replication_slot().

446 {
448 
449  /* Initialize from where to start reading WAL. */
450  startptr = ctx->slot->data.restart_lsn;
451 
452  elog(DEBUG1, "searching for logical decoding starting point, starting at %X/%X",
453  (uint32) (ctx->slot->data.restart_lsn >> 32),
454  (uint32) ctx->slot->data.restart_lsn);
455 
456  /* Wait for a consistent starting point */
457  for (;;)
458  {
459  XLogRecord *record;
460  char *err = NULL;
461 
462  /* the read_page callback waits for new WAL */
463  record = XLogReadRecord(ctx->reader, startptr, &err);
464  if (err)
465  elog(ERROR, "%s", err);
466  if (!record)
467  elog(ERROR, "no record found"); /* shouldn't happen */
468 
469  startptr = InvalidXLogRecPtr;
470 
472 
473  /* only continue till we found a consistent spot */
474  if (DecodingContextReady(ctx))
475  break;
476 
478  }
479 
480  ctx->slot->data.confirmed_flush = ctx->reader->EndRecPtr;
481 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
bool DecodingContextReady(LogicalDecodingContext *ctx)
Definition: logical.c:436
ReplicationSlotPersistentData data
Definition: slot.h:120
XLogRecord * XLogReadRecord(XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
Definition: xlogreader.c:193
XLogRecPtr confirmed_flush
Definition: slot.h:81
XLogRecPtr EndRecPtr
Definition: xlogreader.h:120
#define ERROR
Definition: elog.h:43
void LogicalDecodingProcessRecord(LogicalDecodingContext *ctx, XLogReaderState *record)
Definition: decode.c:97
unsigned int uint32
Definition: c.h:325
uint64 XLogRecPtr
Definition: xlogdefs.h:21
XLogRecPtr restart_lsn
Definition: slot.h:73
XLogReaderState * reader
Definition: logical.h:44
ReplicationSlot * slot
Definition: logical.h:41
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
#define elog
Definition: elog.h:219
static XLogRecPtr startptr
Definition: basebackup.c:106

◆ DecodingContextReady()

bool DecodingContextReady ( LogicalDecodingContext ctx)

Definition at line 436 of file logical.c.

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

Referenced by DecodingContextFindStartpoint().

437 {
439 }
SnapBuildState SnapBuildCurrentState(SnapBuild *builder)
Definition: snapbuild.c:401
struct SnapBuild * snapshot_builder
Definition: logical.h:46

◆ filter_by_origin_cb_wrapper()

bool filter_by_origin_cb_wrapper ( LogicalDecodingContext ctx,
RepOriginId  origin_id 
)

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

782 {
784  ErrorContextCallback errcallback;
785  bool ret;
786 
787  Assert(!ctx->fast_forward);
788 
789  /* Push callback + info on the error context stack */
790  state.ctx = ctx;
791  state.callback_name = "filter_by_origin";
794  errcallback.arg = (void *) &state;
795  errcallback.previous = error_context_stack;
796  error_context_stack = &errcallback;
797 
798  /* set output state */
799  ctx->accept_writes = false;
800 
801  /* do the actual work: call callback */
802  ret = ctx->callbacks.filter_by_origin_cb(ctx, origin_id);
803 
804  /* Pop the error context stack */
805  error_context_stack = errcallback.previous;
806 
807  return ret;
808 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:55
void(* callback)(void *arg)
Definition: elog.h:239
struct ErrorContextCallback * previous
Definition: elog.h:238
const char * callback_name
Definition: logical.c:51
ErrorContextCallback * error_context_stack
Definition: elog.c:88
#define Assert(condition)
Definition: c.h:699
Definition: regguts.h:298
XLogRecPtr report_location
Definition: logical.c:52
LogicalDecodingContext * ctx
Definition: logical.c:50
static void output_plugin_error_callback(void *arg)
Definition: logical.c:564
LogicalDecodeFilterByOriginCB filter_by_origin_cb

◆ FreeDecodingContext()

void FreeDecodingContext ( LogicalDecodingContext ctx)

Definition at line 488 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 CreateReplicationSlot(), pg_create_logical_replication_slot(), pg_logical_replication_slot_advance(), pg_logical_slot_get_changes_guts(), and StartLogicalReplication().

489 {
490  if (ctx->callbacks.shutdown_cb != NULL)
491  shutdown_cb_wrapper(ctx);
492 
495  XLogReaderFree(ctx->reader);
497 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
struct ReorderBuffer * reorder
Definition: logical.h:45
OutputPluginCallbacks callbacks
Definition: logical.h:55
void ReorderBufferFree(ReorderBuffer *rb)
MemoryContext context
Definition: logical.h:38
static void shutdown_cb_wrapper(LogicalDecodingContext *ctx)
Definition: logical.c:611
void XLogReaderFree(XLogReaderState *state)
Definition: xlogreader.c:125
void FreeSnapshotBuilder(SnapBuild *builder)
Definition: snapbuild.c:357
struct SnapBuild * snapshot_builder
Definition: logical.h:46
LogicalDecodeShutdownCB shutdown_cb
XLogReaderState * reader
Definition: logical.h:44

◆ LogicalConfirmReceivedLocation()

void LogicalConfirmReceivedLocation ( XLogRecPtr  lsn)

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

982 {
983  Assert(lsn != InvalidXLogRecPtr);
984 
985  /* Do an unlocked check for candidate_lsn first. */
988  {
989  bool updated_xmin = false;
990  bool updated_restart = false;
991 
993 
995 
996  /* if we're past the location required for bumping xmin, do so */
999  {
1000  /*
1001  * We have to write the changed xmin to disk *before* we change
1002  * the in-memory value, otherwise after a crash we wouldn't know
1003  * that some catalog tuples might have been removed already.
1004  *
1005  * Ensure that by first writing to ->xmin and only update
1006  * ->effective_xmin once the new state is synced to disk. After a
1007  * crash ->effective_xmin is set to ->xmin.
1008  */
1011  {
1015  updated_xmin = true;
1016  }
1017  }
1018 
1021  {
1023 
1027  updated_restart = true;
1028  }
1029 
1031 
1032  /* first write new xmin to disk, so we know what's up after a crash */
1033  if (updated_xmin || updated_restart)
1034  {
1037  elog(DEBUG1, "updated xmin: %u restart: %u", updated_xmin, updated_restart);
1038  }
1039 
1040  /*
1041  * Now the new xmin is safely on disk, we can let the global value
1042  * advance. We do not take ProcArrayLock or similar since we only
1043  * advance xmin here and there's not much harm done by a concurrent
1044  * computation missing that.
1045  */
1046  if (updated_xmin)
1047  {
1051 
1054  }
1055  }
1056  else
1057  {
1061  }
1062 }
TransactionId candidate_catalog_xmin
Definition: slot.h:135
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
void ReplicationSlotSave(void)
Definition: slot.c:638
ReplicationSlotPersistentData data
Definition: slot.h:120
XLogRecPtr confirmed_flush
Definition: slot.h:81
#define SpinLockAcquire(lock)
Definition: spin.h:62
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:741
XLogRecPtr candidate_restart_valid
Definition: slot.h:137
TransactionId catalog_xmin
Definition: slot.h:70
#define InvalidTransactionId
Definition: transam.h:31
#define SpinLockRelease(lock)
Definition: spin.h:64
TransactionId effective_catalog_xmin
Definition: slot.h:117
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:699
XLogRecPtr restart_lsn
Definition: slot.h:73
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:136
#define elog
Definition: elog.h:219
slock_t mutex
Definition: slot.h:93
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:695
XLogRecPtr candidate_restart_lsn
Definition: slot.h:138
void ReplicationSlotMarkDirty(void)
Definition: slot.c:656

◆ LogicalIncreaseRestartDecodingForSlot()

void LogicalIncreaseRestartDecodingForSlot ( XLogRecPtr  current_lsn,
XLogRecPtr  restart_lsn 
)

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

913 {
914  bool updated_lsn = false;
915  ReplicationSlot *slot;
916 
917  slot = MyReplicationSlot;
918 
919  Assert(slot != NULL);
920  Assert(restart_lsn != InvalidXLogRecPtr);
921  Assert(current_lsn != InvalidXLogRecPtr);
922 
923  SpinLockAcquire(&slot->mutex);
924 
925  /* don't overwrite if have a newer restart lsn */
926  if (restart_lsn <= slot->data.restart_lsn)
927  {
928  }
929 
930  /*
931  * We might have already flushed far enough to directly accept this lsn,
932  * in this case there is no need to check for existing candidate LSNs
933  */
934  else if (current_lsn <= slot->data.confirmed_flush)
935  {
936  slot->candidate_restart_valid = current_lsn;
937  slot->candidate_restart_lsn = restart_lsn;
938 
939  /* our candidate can directly be used */
940  updated_lsn = true;
941  }
942 
943  /*
944  * Only increase if the previous values have been applied, otherwise we
945  * might never end up updating if the receiver acks too slowly. A missed
946  * value here will just cause some extra effort after reconnecting.
947  */
949  {
950  slot->candidate_restart_valid = current_lsn;
951  slot->candidate_restart_lsn = restart_lsn;
952 
953  elog(DEBUG1, "got new restart lsn %X/%X at %X/%X",
954  (uint32) (restart_lsn >> 32), (uint32) restart_lsn,
955  (uint32) (current_lsn >> 32), (uint32) current_lsn);
956  }
957  else
958  {
959  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",
960  (uint32) (restart_lsn >> 32), (uint32) restart_lsn,
961  (uint32) (current_lsn >> 32), (uint32) current_lsn,
962  (uint32) (slot->candidate_restart_lsn >> 32),
964  (uint32) (slot->candidate_restart_valid >> 32),
966  (uint32) (slot->data.confirmed_flush >> 32),
967  (uint32) slot->data.confirmed_flush
968  );
969  }
970  SpinLockRelease(&slot->mutex);
971 
972  /* candidates are already valid with the current flush position, apply */
973  if (updated_lsn)
975 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
ReplicationSlotPersistentData data
Definition: slot.h:120
XLogRecPtr confirmed_flush
Definition: slot.h:81
#define SpinLockAcquire(lock)
Definition: spin.h:62
XLogRecPtr candidate_restart_valid
Definition: slot.h:137
unsigned int uint32
Definition: c.h:325
#define SpinLockRelease(lock)
Definition: spin.h:64
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:699
void LogicalConfirmReceivedLocation(XLogRecPtr lsn)
Definition: logical.c:981
#define elog
Definition: elog.h:219
slock_t mutex
Definition: slot.h:93
XLogRecPtr candidate_restart_lsn
Definition: slot.h:138

◆ LogicalIncreaseXminForSlot()

void LogicalIncreaseXminForSlot ( XLogRecPtr  lsn,
TransactionId  xmin 
)

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

856 {
857  bool updated_xmin = false;
858  ReplicationSlot *slot;
859 
860  slot = MyReplicationSlot;
861 
862  Assert(slot != NULL);
863 
864  SpinLockAcquire(&slot->mutex);
865 
866  /*
867  * don't overwrite if we already have a newer xmin. This can happen if we
868  * restart decoding in a slot.
869  */
871  {
872  }
873 
874  /*
875  * If the client has already confirmed up to this lsn, we directly can
876  * mark this as accepted. This can happen if we restart decoding in a
877  * slot.
878  */
879  else if (current_lsn <= slot->data.confirmed_flush)
880  {
881  slot->candidate_catalog_xmin = xmin;
882  slot->candidate_xmin_lsn = current_lsn;
883 
884  /* our candidate can directly be used */
885  updated_xmin = true;
886  }
887 
888  /*
889  * Only increase if the previous values have been applied, otherwise we
890  * might never end up updating if the receiver acks too slowly.
891  */
892  else if (slot->candidate_xmin_lsn == InvalidXLogRecPtr)
893  {
894  slot->candidate_catalog_xmin = xmin;
895  slot->candidate_xmin_lsn = current_lsn;
896  }
897  SpinLockRelease(&slot->mutex);
898 
899  /* candidate already valid with the current flush position, apply */
900  if (updated_xmin)
902 }
TransactionId candidate_catalog_xmin
Definition: slot.h:135
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
ReplicationSlotPersistentData data
Definition: slot.h:120
XLogRecPtr confirmed_flush
Definition: slot.h:81
#define SpinLockAcquire(lock)
Definition: spin.h:62
bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:319
TransactionId catalog_xmin
Definition: slot.h:70
#define SpinLockRelease(lock)
Definition: spin.h:64
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:699
void LogicalConfirmReceivedLocation(XLogRecPtr lsn)
Definition: logical.c:981
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:136
slock_t mutex
Definition: slot.h:93