PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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 struct
LogicalDecodingContext 
LogicalDecodingContext
 

Functions

void CheckLogicalDecodingRequirements (void)
 
LogicalDecodingContextCreateInitDecodingContext (char *plugin, List *output_plugin_options, XLogPageReadCB read_page, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write)
 
LogicalDecodingContextCreateDecodingContext (XLogRecPtr start_lsn, List *output_plugin_options, XLogPageReadCB read_page, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write)
 
void DecodingContextFindStartpoint (LogicalDecodingContext *ctx)
 
bool DecodingContextReady (LogicalDecodingContext *ctx)
 
void FreeDecodingContext (LogicalDecodingContext *ctx)
 
LogicalDecodingContextCreateCopyDecodingContext (List *output_plugin_options, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write)
 
ListDecodingContextGetTableList (LogicalDecodingContext *ctx)
 
void LogicalIncreaseXminForSlot (XLogRecPtr lsn, TransactionId xmin)
 
void LogicalIncreaseRestartDecodingForSlot (XLogRecPtr current_lsn, XLogRecPtr restart_lsn)
 
void LogicalConfirmReceivedLocation (XLogRecPtr lsn)
 
void LagTrackerWrite (XLogRecPtr lsn, TimestampTz local_flush_time)
 
bool filter_by_origin_cb_wrapper (LogicalDecodingContext *ctx, RepOriginId origin_id)
 

Typedef Documentation

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

Definition at line 20 of file logical.h.

Function Documentation

void CheckLogicalDecodingRequirements ( void  )

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

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

Definition at line 327 of file logical.c.

References LogicalDecodingContext::callbacks, ReplicationSlotPersistentData::confirmed_flush, LogicalDecodingContext::context, ReplicationSlot::data, ReplicationSlotPersistentData::database, DEBUG1, elog, ereport, errcode(), errdetail(), errmsg(), ERROR, InvalidTransactionId, InvalidXLogRecPtr, LOG, MemoryContextSwitchTo(), MyDatabaseId, MyReplicationSlot, ReplicationSlotPersistentData::name, NameStr, NULL, LogicalDecodingContext::options, ReplicationSlotPersistentData::restart_lsn, SlotIsPhysical, OutputPluginCallbacks::startup_cb, startup_cb_wrapper(), and StartupDecodingContext().

Referenced by pg_logical_slot_get_changes_guts(), and StartLogicalReplication().

332 {
334  ReplicationSlot *slot;
335  MemoryContext old_context;
336 
337  /* shorter lines... */
338  slot = MyReplicationSlot;
339 
340  /* first some sanity checks that are unlikely to be violated */
341  if (slot == NULL)
342  elog(ERROR, "cannot perform logical decoding without an acquired slot");
343 
344  /* make sure the passed slot is suitable, these are user facing errors */
345  if (SlotIsPhysical(slot))
346  ereport(ERROR,
347  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
348  (errmsg("cannot use physical replication slot for logical decoding"))));
349 
350  if (slot->data.database != MyDatabaseId)
351  ereport(ERROR,
352  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
353  (errmsg("replication slot \"%s\" was not created in this database",
354  NameStr(slot->data.name)))));
355 
356  if (start_lsn == InvalidXLogRecPtr)
357  {
358  /* continue from last position */
359  start_lsn = slot->data.confirmed_flush;
360  }
361  else if (start_lsn < slot->data.confirmed_flush)
362  {
363  /*
364  * It might seem like we should error out in this case, but it's
365  * pretty common for a client to acknowledge a LSN it doesn't have to
366  * do anything for, and thus didn't store persistently, because the
367  * xlog records didn't result in anything relevant for logical
368  * decoding. Clients have to be able to do that to support synchronous
369  * replication.
370  */
371  elog(DEBUG1, "cannot stream from %X/%X, minimum is %X/%X, forwarding",
372  (uint32) (start_lsn >> 32), (uint32) start_lsn,
373  (uint32) (slot->data.confirmed_flush >> 32),
374  (uint32) slot->data.confirmed_flush);
375 
376  start_lsn = slot->data.confirmed_flush;
377  }
378 
379  ctx = StartupDecodingContext(output_plugin_options,
380  start_lsn, InvalidTransactionId,
381  read_page, prepare_write, do_write);
382 
383  /* call output plugin initialization callback */
384  old_context = MemoryContextSwitchTo(ctx->context);
385  if (ctx->callbacks.startup_cb != NULL)
386  startup_cb_wrapper(ctx, &ctx->options, false);
387  MemoryContextSwitchTo(old_context);
388 
389  ereport(LOG,
390  (errmsg("starting logical decoding for slot \"%s\"",
391  NameStr(slot->data.name)),
392  errdetail("streaming transactions committing after %X/%X, reading WAL from %X/%X",
393  (uint32) (slot->data.confirmed_flush >> 32),
394  (uint32) slot->data.confirmed_flush,
395  (uint32) (slot->data.restart_lsn >> 32),
396  (uint32) slot->data.restart_lsn)));
397 
398  return ctx;
399 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
OutputPluginOptions options
Definition: logical.h:43
OutputPluginCallbacks callbacks
Definition: logical.h:42
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
MemoryContext context
Definition: logical.h:32
ReplicationSlotPersistentData data
Definition: slot.h:115
#define LOG
Definition: elog.h:26
#define SlotIsPhysical(slot)
Definition: slot.h:133
XLogRecPtr confirmed_flush
Definition: slot.h:76
#define ERROR
Definition: elog.h:43
static void startup_cb_wrapper(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
Definition: logical.c:541
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define InvalidTransactionId
Definition: transam.h:31
unsigned int uint32
Definition: c.h:268
#define ereport(elevel, rest)
Definition: elog.h:122
static LogicalDecodingContext * StartupDecodingContext(List *output_plugin_options, XLogRecPtr start_lsn, TransactionId xmin_horizon, XLogPageReadCB read_page, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write)
Definition: logical.c:114
Oid MyDatabaseId
Definition: globals.c:76
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define NULL
Definition: c.h:229
XLogRecPtr restart_lsn
Definition: slot.h:68
LogicalDecodeStartupCB startup_cb
Definition: output_plugin.h:97
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define NameStr(name)
Definition: c.h:499
#define elog
Definition: elog.h:219
LogicalDecodingContext* CreateInitDecodingContext ( char *  plugin,
List output_plugin_options,
XLogPageReadCB  read_page,
LogicalOutputPluginWriterPrepareWrite  prepare_write,
LogicalOutputPluginWriterWrite  do_write 
)

Definition at line 211 of file logical.c.

References LogicalDecodingContext::callbacks, ReplicationSlotPersistentData::catalog_xmin, LogicalDecodingContext::context, ReplicationSlot::data, ReplicationSlotPersistentData::database, ReplicationSlot::effective_catalog_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, NULL, LogicalDecodingContext::options, ReplicationSlotPersistentData::plugin, 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().

216 {
217  TransactionId xmin_horizon = InvalidTransactionId;
218  ReplicationSlot *slot;
220  MemoryContext old_context;
221 
222  /* shorter lines... */
223  slot = MyReplicationSlot;
224 
225  /* first some sanity checks that are unlikely to be violated */
226  if (slot == NULL)
227  elog(ERROR, "cannot perform logical decoding without an acquired slot");
228 
229  if (plugin == NULL)
230  elog(ERROR, "cannot initialize logical decoding without a specified plugin");
231 
232  /* Make sure the passed slot is suitable. These are user facing errors. */
233  if (SlotIsPhysical(slot))
234  ereport(ERROR,
235  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
236  errmsg("cannot use physical replication slot for logical decoding")));
237 
238  if (slot->data.database != MyDatabaseId)
239  ereport(ERROR,
240  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
241  errmsg("replication slot \"%s\" was not created in this database",
242  NameStr(slot->data.name))));
243 
244  if (IsTransactionState() &&
246  ereport(ERROR,
247  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
248  errmsg("cannot create logical replication slot in transaction that has performed writes")));
249 
250  /* register output plugin name with slot */
251  SpinLockAcquire(&slot->mutex);
253  SpinLockRelease(&slot->mutex);
254 
256 
257  /* ----
258  * This is a bit tricky: We need to determine a safe xmin horizon to start
259  * decoding from, to avoid starting from a running xacts record referring
260  * to xids whose rows have been vacuumed or pruned
261  * already. GetOldestSafeDecodingTransactionId() returns such a value, but
262  * without further interlock its return value might immediately be out of
263  * date.
264  *
265  * So we have to acquire the ProcArrayLock to prevent computation of new
266  * xmin horizons by other backends, get the safe decoding xid, and inform
267  * the slot machinery about the new limit. Once that's done the
268  * ProcArrayLock can be released as the slot machinery now is
269  * protecting against vacuum.
270  * ----
271  */
272  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
273 
276 
278 
279  LWLockRelease(ProcArrayLock);
280 
281  /*
282  * tell the snapshot builder to only assemble snapshot once reaching the
283  * running_xact's record with the respective xmin.
284  */
285  xmin_horizon = slot->data.catalog_xmin;
286 
289 
290  ctx = StartupDecodingContext(NIL, InvalidXLogRecPtr, xmin_horizon,
291  read_page, prepare_write, do_write);
292 
293  /* call output plugin initialization callback */
294  old_context = MemoryContextSwitchTo(ctx->context);
295  if (ctx->callbacks.startup_cb != NULL)
296  startup_cb_wrapper(ctx, &ctx->options, true);
297  MemoryContextSwitchTo(old_context);
298 
299  return ctx;
300 }
#define NIL
Definition: pg_list.h:69
static const char * plugin
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
uint32 TransactionId
Definition: c.h:397
OutputPluginOptions options
Definition: logical.h:43
OutputPluginCallbacks callbacks
Definition: logical.h:42
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
MemoryContext context
Definition: logical.h:32
void ReplicationSlotSave(void)
Definition: slot.c:563
ReplicationSlotPersistentData data
Definition: slot.h:115
#define SlotIsPhysical(slot)
Definition: slot.h:133
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
#define NAMEDATALEN
#define SpinLockAcquire(lock)
Definition: spin.h:62
void ReplicationSlotReserveWal(void)
Definition: slot.c:825
#define ERROR
Definition: elog.h:43
static void startup_cb_wrapper(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
Definition: logical.c:541
TransactionId catalog_xmin
Definition: slot.h:65
#define InvalidTransactionId
Definition: transam.h:31
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:404
#define ereport(elevel, rest)
Definition: elog.h:122
static LogicalDecodingContext * StartupDecodingContext(List *output_plugin_options, XLogRecPtr start_lsn, TransactionId xmin_horizon, XLogPageReadCB read_page, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write)
Definition: logical.c:114
#define SpinLockRelease(lock)
Definition: spin.h:64
TransactionId effective_catalog_xmin
Definition: slot.h:112
Oid MyDatabaseId
Definition: globals.c:76
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define NULL
Definition: c.h:229
#define StrNCpy(dst, src, len)
Definition: c.h:830
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
bool IsTransactionState(void)
Definition: xact.c:350
LogicalDecodeStartupCB startup_cb
Definition: output_plugin.h:97
TransactionId GetOldestSafeDecodingTransactionId(void)
Definition: procarray.c:2150
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define NameStr(name)
Definition: c.h:499
#define elog
Definition: elog.h:219
slock_t mutex
Definition: slot.h:88
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:617
void ReplicationSlotMarkDirty(void)
Definition: slot.c:581
void DecodingContextFindStartpoint ( LogicalDecodingContext ctx)

Definition at line 414 of file logical.c.

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

Referenced by CreateReplicationSlot(), and pg_create_logical_replication_slot().

415 {
416  XLogRecPtr startptr;
417 
418  /* Initialize from where to start reading WAL. */
419  startptr = ctx->slot->data.restart_lsn;
420 
421  elog(DEBUG1, "searching for logical decoding starting point, starting at %X/%X",
422  (uint32) (ctx->slot->data.restart_lsn >> 32),
423  (uint32) ctx->slot->data.restart_lsn);
424 
425  /* Wait for a consistent starting point */
426  for (;;)
427  {
428  XLogRecord *record;
429  char *err = NULL;
430 
431  /* the read_page callback waits for new WAL */
432  record = XLogReadRecord(ctx->reader, startptr, &err);
433  if (err)
434  elog(ERROR, "%s", err);
435  if (!record)
436  elog(ERROR, "no record found"); /* shouldn't happen */
437 
438  startptr = InvalidXLogRecPtr;
439 
441 
442  /* only continue till we found a consistent spot */
443  if (DecodingContextReady(ctx))
444  break;
445 
447  }
448 
449  ctx->slot->data.confirmed_flush = ctx->reader->EndRecPtr;
450 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
bool DecodingContextReady(LogicalDecodingContext *ctx)
Definition: logical.c:405
ReplicationSlotPersistentData data
Definition: slot.h:115
XLogRecord * XLogReadRecord(XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
Definition: xlogreader.c:193
XLogRecPtr confirmed_flush
Definition: slot.h:76
XLogRecPtr EndRecPtr
Definition: xlogreader.h:115
#define ERROR
Definition: elog.h:43
void LogicalDecodingProcessRecord(LogicalDecodingContext *ctx, XLogReaderState *record)
Definition: decode.c:93
unsigned int uint32
Definition: c.h:268
#define NULL
Definition: c.h:229
uint64 XLogRecPtr
Definition: xlogdefs.h:21
XLogRecPtr restart_lsn
Definition: slot.h:68
XLogReaderState * reader
Definition: logical.h:38
ReplicationSlot * slot
Definition: logical.h:35
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
#define elog
Definition: elog.h:219
List* DecodingContextGetTableList ( LogicalDecodingContext ctx)
bool DecodingContextReady ( LogicalDecodingContext ctx)

Definition at line 405 of file logical.c.

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

Referenced by DecodingContextFindStartpoint().

406 {
408 }
SnapBuildState SnapBuildCurrentState(SnapBuild *builder)
Definition: snapbuild.c:365
struct SnapBuild * snapshot_builder
Definition: logical.h:40
bool filter_by_origin_cb_wrapper ( LogicalDecodingContext ctx,
RepOriginId  origin_id 
)

Definition at line 688 of file logical.c.

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, ErrorContextCallback::callback, LogicalErrorCallbackState::callback_name, LogicalDecodingContext::callbacks, LogicalErrorCallbackState::ctx, error_context_stack, OutputPluginCallbacks::filter_by_origin_cb, InvalidXLogRecPtr, output_plugin_error_callback(), ErrorContextCallback::previous, and LogicalErrorCallbackState::report_location.

Referenced by FilterByOrigin().

689 {
691  ErrorContextCallback errcallback;
692  bool ret;
693 
694  /* Push callback + info on the error context stack */
695  state.ctx = ctx;
696  state.callback_name = "filter_by_origin";
699  errcallback.arg = (void *) &state;
700  errcallback.previous = error_context_stack;
701  error_context_stack = &errcallback;
702 
703  /* set output state */
704  ctx->accept_writes = false;
705 
706  /* do the actual work: call callback */
707  ret = ctx->callbacks.filter_by_origin_cb(ctx, origin_id);
708 
709  /* Pop the error context stack */
710  error_context_stack = errcallback.previous;
711 
712  return ret;
713 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:42
struct ErrorContextCallback * previous
Definition: elog.h:238
const char * callback_name
Definition: logical.c:51
ErrorContextCallback * error_context_stack
Definition: elog.c:88
Definition: regguts.h:298
XLogRecPtr report_location
Definition: logical.c:52
void(* callback)(void *arg)
Definition: elog.h:239
LogicalDecodingContext * ctx
Definition: logical.c:50
static void output_plugin_error_callback(void *arg)
Definition: logical.c:521
LogicalDecodeFilterByOriginCB filter_by_origin_cb
void FreeDecodingContext ( LogicalDecodingContext ctx)

Definition at line 457 of file logical.c.

References LogicalDecodingContext::callbacks, LogicalDecodingContext::context, FreeSnapshotBuilder(), MemoryContextDelete(), NULL, 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_slot_get_changes_guts(), and StartLogicalReplication().

458 {
459  if (ctx->callbacks.shutdown_cb != NULL)
460  shutdown_cb_wrapper(ctx);
461 
464  XLogReaderFree(ctx->reader);
466 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
struct ReorderBuffer * reorder
Definition: logical.h:39
OutputPluginCallbacks callbacks
Definition: logical.h:42
void ReorderBufferFree(ReorderBuffer *rb)
MemoryContext context
Definition: logical.h:32
static void shutdown_cb_wrapper(LogicalDecodingContext *ctx)
Definition: logical.c:566
void XLogReaderFree(XLogReaderState *state)
Definition: xlogreader.c:125
void FreeSnapshotBuilder(SnapBuild *builder)
Definition: snapbuild.c:321
struct SnapBuild * snapshot_builder
Definition: logical.h:40
#define NULL
Definition: c.h:229
LogicalDecodeShutdownCB shutdown_cb
XLogReaderState * reader
Definition: logical.h:38
void LagTrackerWrite ( XLogRecPtr  lsn,
TimestampTz  local_flush_time 
)

Definition at line 3226 of file walsender.c.

References am_walsender, i, LAG_TRACKER_BUFFER_SIZE, LagTracker, and NUM_SYNC_REP_WAIT_MODE.

Referenced by XLogSendPhysical().

3227 {
3228  bool buffer_full;
3229  int new_write_head;
3230  int i;
3231 
3232  if (!am_walsender)
3233  return;
3234 
3235  /*
3236  * If the lsn hasn't advanced since last time, then do nothing. This way
3237  * we only record a new sample when new WAL has been written.
3238  */
3239  if (LagTracker.last_lsn == lsn)
3240  return;
3241  LagTracker.last_lsn = lsn;
3242 
3243  /*
3244  * If advancing the write head of the circular buffer would crash into any
3245  * of the read heads, then the buffer is full. In other words, the
3246  * slowest reader (presumably apply) is the one that controls the release
3247  * of space.
3248  */
3249  new_write_head = (LagTracker.write_head + 1) % LAG_TRACKER_BUFFER_SIZE;
3250  buffer_full = false;
3251  for (i = 0; i < NUM_SYNC_REP_WAIT_MODE; ++i)
3252  {
3253  if (new_write_head == LagTracker.read_heads[i])
3254  buffer_full = true;
3255  }
3256 
3257  /*
3258  * If the buffer is full, for now we just rewind by one slot and overwrite
3259  * the last sample, as a simple (if somewhat uneven) way to lower the
3260  * sampling rate. There may be better adaptive compaction algorithms.
3261  */
3262  if (buffer_full)
3263  {
3264  new_write_head = LagTracker.write_head;
3265  if (LagTracker.write_head > 0)
3266  LagTracker.write_head--;
3267  else
3268  LagTracker.write_head = LAG_TRACKER_BUFFER_SIZE - 1;
3269  }
3270 
3271  /* Store a sample at the current write head position. */
3272  LagTracker.buffer[LagTracker.write_head].lsn = lsn;
3273  LagTracker.buffer[LagTracker.write_head].time = local_flush_time;
3274  LagTracker.write_head = new_write_head;
3275 }
bool am_walsender
Definition: walsender.c:108
static struct @25 LagTracker
#define LAG_TRACKER_BUFFER_SIZE
Definition: walsender.c:201
int i
#define NUM_SYNC_REP_WAIT_MODE
Definition: syncrep.h:28
void LogicalConfirmReceivedLocation ( XLogRecPtr  lsn)

Definition at line 884 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_slot_get_changes_guts(), and ProcessStandbyReplyMessage().

885 {
886  Assert(lsn != InvalidXLogRecPtr);
887 
888  /* Do an unlocked check for candidate_lsn first. */
891  {
892  bool updated_xmin = false;
893  bool updated_restart = false;
894 
896 
898 
899  /* if we're past the location required for bumping xmin, do so */
902  {
903  /*
904  * We have to write the changed xmin to disk *before* we change
905  * the in-memory value, otherwise after a crash we wouldn't know
906  * that some catalog tuples might have been removed already.
907  *
908  * Ensure that by first writing to ->xmin and only update
909  * ->effective_xmin once the new state is synced to disk. After a
910  * crash ->effective_xmin is set to ->xmin.
911  */
914  {
918  updated_xmin = true;
919  }
920  }
921 
924  {
926 
930  updated_restart = true;
931  }
932 
934 
935  /* first write new xmin to disk, so we know what's up after a crash */
936  if (updated_xmin || updated_restart)
937  {
940  elog(DEBUG1, "updated xmin: %u restart: %u", updated_xmin, updated_restart);
941  }
942 
943  /*
944  * Now the new xmin is safely on disk, we can let the global value
945  * advance. We do not take ProcArrayLock or similar since we only
946  * advance xmin here and there's not much harm done by a concurrent
947  * computation missing that.
948  */
949  if (updated_xmin)
950  {
954 
957  }
958  }
959  else
960  {
964  }
965 }
TransactionId candidate_catalog_xmin
Definition: slot.h:127
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
void ReplicationSlotSave(void)
Definition: slot.c:563
ReplicationSlotPersistentData data
Definition: slot.h:115
XLogRecPtr confirmed_flush
Definition: slot.h:76
#define SpinLockAcquire(lock)
Definition: spin.h:62
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:665
XLogRecPtr candidate_restart_valid
Definition: slot.h:129
TransactionId catalog_xmin
Definition: slot.h:65
#define InvalidTransactionId
Definition: transam.h:31
#define SpinLockRelease(lock)
Definition: spin.h:64
TransactionId effective_catalog_xmin
Definition: slot.h:112
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:675
XLogRecPtr restart_lsn
Definition: slot.h:68
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:128
#define elog
Definition: elog.h:219
slock_t mutex
Definition: slot.h:88
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:617
XLogRecPtr candidate_restart_lsn
Definition: slot.h:130
void ReplicationSlotMarkDirty(void)
Definition: slot.c:581
void LogicalIncreaseRestartDecodingForSlot ( XLogRecPtr  current_lsn,
XLogRecPtr  restart_lsn 
)

Definition at line 815 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, NULL, SpinLockAcquire, and SpinLockRelease.

Referenced by SnapBuildProcessRunningXacts().

816 {
817  bool updated_lsn = false;
818  ReplicationSlot *slot;
819 
820  slot = MyReplicationSlot;
821 
822  Assert(slot != NULL);
823  Assert(restart_lsn != InvalidXLogRecPtr);
824  Assert(current_lsn != InvalidXLogRecPtr);
825 
826  SpinLockAcquire(&slot->mutex);
827 
828  /* don't overwrite if have a newer restart lsn */
829  if (restart_lsn <= slot->data.restart_lsn)
830  {
831  }
832 
833  /*
834  * We might have already flushed far enough to directly accept this lsn,
835  * in this case there is no need to check for existing candidate LSNs
836  */
837  else if (current_lsn <= slot->data.confirmed_flush)
838  {
839  slot->candidate_restart_valid = current_lsn;
840  slot->candidate_restart_lsn = restart_lsn;
841 
842  /* our candidate can directly be used */
843  updated_lsn = true;
844  }
845 
846  /*
847  * Only increase if the previous values have been applied, otherwise we
848  * might never end up updating if the receiver acks too slowly. A missed
849  * value here will just cause some extra effort after reconnecting.
850  */
852  {
853  slot->candidate_restart_valid = current_lsn;
854  slot->candidate_restart_lsn = restart_lsn;
855 
856  elog(DEBUG1, "got new restart lsn %X/%X at %X/%X",
857  (uint32) (restart_lsn >> 32), (uint32) restart_lsn,
858  (uint32) (current_lsn >> 32), (uint32) current_lsn);
859  }
860  else
861  {
862  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",
863  (uint32) (restart_lsn >> 32), (uint32) restart_lsn,
864  (uint32) (current_lsn >> 32), (uint32) current_lsn,
865  (uint32) (slot->candidate_restart_lsn >> 32),
867  (uint32) (slot->candidate_restart_valid >> 32),
869  (uint32) (slot->data.confirmed_flush >> 32),
870  (uint32) slot->data.confirmed_flush
871  );
872  }
873  SpinLockRelease(&slot->mutex);
874 
875  /* candidates are already valid with the current flush position, apply */
876  if (updated_lsn)
878 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
ReplicationSlotPersistentData data
Definition: slot.h:115
XLogRecPtr confirmed_flush
Definition: slot.h:76
#define SpinLockAcquire(lock)
Definition: spin.h:62
XLogRecPtr candidate_restart_valid
Definition: slot.h:129
unsigned int uint32
Definition: c.h:268
#define SpinLockRelease(lock)
Definition: spin.h:64
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
void LogicalConfirmReceivedLocation(XLogRecPtr lsn)
Definition: logical.c:884
#define elog
Definition: elog.h:219
slock_t mutex
Definition: slot.h:88
XLogRecPtr candidate_restart_lsn
Definition: slot.h:130
void LogicalIncreaseXminForSlot ( XLogRecPtr  lsn,
TransactionId  xmin 
)

Definition at line 758 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, NULL, SpinLockAcquire, SpinLockRelease, and TransactionIdPrecedesOrEquals().

Referenced by SnapBuildProcessRunningXacts().

759 {
760  bool updated_xmin = false;
761  ReplicationSlot *slot;
762 
763  slot = MyReplicationSlot;
764 
765  Assert(slot != NULL);
766 
767  SpinLockAcquire(&slot->mutex);
768 
769  /*
770  * don't overwrite if we already have a newer xmin. This can happen if we
771  * restart decoding in a slot.
772  */
774  {
775  }
776 
777  /*
778  * If the client has already confirmed up to this lsn, we directly can
779  * mark this as accepted. This can happen if we restart decoding in a
780  * slot.
781  */
782  else if (current_lsn <= slot->data.confirmed_flush)
783  {
784  slot->candidate_catalog_xmin = xmin;
785  slot->candidate_xmin_lsn = current_lsn;
786 
787  /* our candidate can directly be used */
788  updated_xmin = true;
789  }
790 
791  /*
792  * Only increase if the previous values have been applied, otherwise we
793  * might never end up updating if the receiver acks too slowly.
794  */
795  else if (slot->candidate_xmin_lsn == InvalidXLogRecPtr)
796  {
797  slot->candidate_catalog_xmin = xmin;
798  slot->candidate_xmin_lsn = current_lsn;
799  }
800  SpinLockRelease(&slot->mutex);
801 
802  /* candidate already valid with the current flush position, apply */
803  if (updated_xmin)
805 }
TransactionId candidate_catalog_xmin
Definition: slot.h:127
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
ReplicationSlotPersistentData data
Definition: slot.h:115
XLogRecPtr confirmed_flush
Definition: slot.h:76
#define SpinLockAcquire(lock)
Definition: spin.h:62
bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:319
TransactionId catalog_xmin
Definition: slot.h:65
#define SpinLockRelease(lock)
Definition: spin.h:64
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
void LogicalConfirmReceivedLocation(XLogRecPtr lsn)
Definition: logical.c:884
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:128
slock_t mutex
Definition: slot.h:88