PostgreSQL Source Code  git master
logical.c File Reference
#include "postgres.h"
#include "fmgr.h"
#include "miscadmin.h"
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "replication/decode.h"
#include "replication/logical.h"
#include "replication/reorderbuffer.h"
#include "replication/origin.h"
#include "replication/snapbuild.h"
#include "storage/proc.h"
#include "storage/procarray.h"
#include "utils/memutils.h"
Include dependency graph for logical.c:

Go to the source code of this file.

Data Structures

struct  LogicalErrorCallbackState
 

Typedefs

typedef struct LogicalErrorCallbackState LogicalErrorCallbackState
 

Functions

static void output_plugin_error_callback (void *arg)
 
static void startup_cb_wrapper (LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
 
static void shutdown_cb_wrapper (LogicalDecodingContext *ctx)
 
static void begin_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn)
 
static void commit_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
 
static void change_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
 
static void truncate_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
 
static void message_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr message_lsn, bool transactional, const char *prefix, Size message_size, const char *message)
 
static void LoadOutputPlugin (OutputPluginCallbacks *callbacks, char *plugin)
 
void CheckLogicalDecodingRequirements (void)
 
static LogicalDecodingContextStartupDecodingContext (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)
 
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)
 
bool DecodingContextReady (LogicalDecodingContext *ctx)
 
void DecodingContextFindStartpoint (LogicalDecodingContext *ctx)
 
void FreeDecodingContext (LogicalDecodingContext *ctx)
 
void OutputPluginPrepareWrite (struct LogicalDecodingContext *ctx, bool last_write)
 
void OutputPluginWrite (struct LogicalDecodingContext *ctx, bool last_write)
 
void OutputPluginUpdateProgress (struct LogicalDecodingContext *ctx)
 
bool filter_by_origin_cb_wrapper (LogicalDecodingContext *ctx, RepOriginId origin_id)
 
void LogicalIncreaseXminForSlot (XLogRecPtr current_lsn, TransactionId xmin)
 
void LogicalIncreaseRestartDecodingForSlot (XLogRecPtr current_lsn, XLogRecPtr restart_lsn)
 
void LogicalConfirmReceivedLocation (XLogRecPtr lsn)
 

Typedef Documentation

◆ LogicalErrorCallbackState

Function Documentation

◆ begin_cb_wrapper()

static void begin_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn 
)
static

Definition at line 667 of file logical.c.

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert, OutputPluginCallbacks::begin_cb, ErrorContextCallback::callback, LogicalErrorCallbackState::callback_name, LogicalDecodingContext::callbacks, LogicalErrorCallbackState::ctx, error_context_stack, LogicalDecodingContext::fast_forward, ReorderBufferTXN::first_lsn, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, LogicalErrorCallbackState::report_location, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

668 {
669  LogicalDecodingContext *ctx = cache->private_data;
671  ErrorContextCallback errcallback;
672 
673  Assert(!ctx->fast_forward);
674 
675  /* Push callback + info on the error context stack */
676  state.ctx = ctx;
677  state.callback_name = "begin";
678  state.report_location = txn->first_lsn;
680  errcallback.arg = (void *) &state;
681  errcallback.previous = error_context_stack;
682  error_context_stack = &errcallback;
683 
684  /* set output state */
685  ctx->accept_writes = true;
686  ctx->write_xid = txn->xid;
687  ctx->write_location = txn->first_lsn;
688 
689  /* do the actual work: call callback */
690  ctx->callbacks.begin_cb(ctx, txn);
691 
692  /* Pop the error context stack */
693  error_context_stack = errcallback.previous;
694 }
XLogRecPtr first_lsn
TransactionId write_xid
Definition: logical.h:89
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:53
void(* callback)(void *arg)
Definition: elog.h:254
struct ErrorContextCallback * previous
Definition: elog.h:253
const char * callback_name
Definition: logical.c:52
XLogRecPtr write_location
Definition: logical.h:88
ErrorContextCallback * error_context_stack
Definition: elog.c:88
TransactionId xid
#define Assert(condition)
Definition: c.h:732
Definition: regguts.h:298
XLogRecPtr report_location
Definition: logical.c:53
LogicalDecodeBeginCB begin_cb
LogicalDecodingContext * ctx
Definition: logical.c:51
static void output_plugin_error_callback(void *arg)
Definition: logical.c:588

◆ change_cb_wrapper()

static void change_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn,
Relation  relation,
ReorderBufferChange change 
)
static

Definition at line 728 of file logical.c.

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert, ErrorContextCallback::callback, LogicalErrorCallbackState::callback_name, LogicalDecodingContext::callbacks, OutputPluginCallbacks::change_cb, LogicalErrorCallbackState::ctx, error_context_stack, LogicalDecodingContext::fast_forward, ReorderBufferChange::lsn, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, LogicalErrorCallbackState::report_location, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

730 {
731  LogicalDecodingContext *ctx = cache->private_data;
733  ErrorContextCallback errcallback;
734 
735  Assert(!ctx->fast_forward);
736 
737  /* Push callback + info on the error context stack */
738  state.ctx = ctx;
739  state.callback_name = "change";
740  state.report_location = change->lsn;
742  errcallback.arg = (void *) &state;
743  errcallback.previous = error_context_stack;
744  error_context_stack = &errcallback;
745 
746  /* set output state */
747  ctx->accept_writes = true;
748  ctx->write_xid = txn->xid;
749 
750  /*
751  * report this change's lsn so replies from clients can give an up2date
752  * answer. This won't ever be enough (and shouldn't be!) to confirm
753  * receipt of this transaction, but it might allow another transaction's
754  * commit to be confirmed with one message.
755  */
756  ctx->write_location = change->lsn;
757 
758  ctx->callbacks.change_cb(ctx, txn, relation, change);
759 
760  /* Pop the error context stack */
761  error_context_stack = errcallback.previous;
762 }
TransactionId write_xid
Definition: logical.h:89
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:53
void(* callback)(void *arg)
Definition: elog.h:254
struct ErrorContextCallback * previous
Definition: elog.h:253
const char * callback_name
Definition: logical.c:52
XLogRecPtr write_location
Definition: logical.h:88
ErrorContextCallback * error_context_stack
Definition: elog.c:88
LogicalDecodeChangeCB change_cb
TransactionId xid
#define Assert(condition)
Definition: c.h:732
Definition: regguts.h:298
XLogRecPtr report_location
Definition: logical.c:53
LogicalDecodingContext * ctx
Definition: logical.c:51
static void output_plugin_error_callback(void *arg)
Definition: logical.c:588

◆ CheckLogicalDecodingRequirements()

void CheckLogicalDecodingRequirements ( void  )

Definition at line 79 of file logical.c.

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

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

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

◆ commit_cb_wrapper()

static void commit_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn,
XLogRecPtr  commit_lsn 
)
static

Definition at line 697 of file logical.c.

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert, ErrorContextCallback::callback, LogicalErrorCallbackState::callback_name, LogicalDecodingContext::callbacks, OutputPluginCallbacks::commit_cb, LogicalErrorCallbackState::ctx, ReorderBufferTXN::end_lsn, error_context_stack, LogicalDecodingContext::fast_forward, ReorderBufferTXN::final_lsn, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, LogicalErrorCallbackState::report_location, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

699 {
700  LogicalDecodingContext *ctx = cache->private_data;
702  ErrorContextCallback errcallback;
703 
704  Assert(!ctx->fast_forward);
705 
706  /* Push callback + info on the error context stack */
707  state.ctx = ctx;
708  state.callback_name = "commit";
709  state.report_location = txn->final_lsn; /* beginning of commit record */
711  errcallback.arg = (void *) &state;
712  errcallback.previous = error_context_stack;
713  error_context_stack = &errcallback;
714 
715  /* set output state */
716  ctx->accept_writes = true;
717  ctx->write_xid = txn->xid;
718  ctx->write_location = txn->end_lsn; /* points to the end of the record */
719 
720  /* do the actual work: call callback */
721  ctx->callbacks.commit_cb(ctx, txn, commit_lsn);
722 
723  /* Pop the error context stack */
724  error_context_stack = errcallback.previous;
725 }
TransactionId write_xid
Definition: logical.h:89
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:53
void(* callback)(void *arg)
Definition: elog.h:254
struct ErrorContextCallback * previous
Definition: elog.h:253
const char * callback_name
Definition: logical.c:52
XLogRecPtr write_location
Definition: logical.h:88
ErrorContextCallback * error_context_stack
Definition: elog.c:88
LogicalDecodeCommitCB commit_cb
XLogRecPtr final_lsn
TransactionId xid
#define Assert(condition)
Definition: c.h:732
Definition: regguts.h:298
XLogRecPtr end_lsn
XLogRecPtr report_location
Definition: logical.c:53
LogicalDecodingContext * ctx
Definition: logical.c:51
static void output_plugin_error_callback(void *arg)
Definition: logical.c:588

◆ CreateDecodingContext()

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

Definition at line 374 of file logical.c.

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

Referenced by pg_logical_replication_slot_advance(), pg_logical_slot_get_changes_guts(), and StartLogicalReplication().

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

◆ CreateInitDecodingContext()

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

Definition at line 231 of file logical.c.

References LogicalDecodingContext::callbacks, ReplicationSlotPersistentData::catalog_xmin, LogicalDecodingContext::context, LogicalErrorCallbackState::ctx, ReplicationSlot::data, ReplicationSlotPersistentData::database, ReplicationSlot::effective_catalog_xmin, ReplicationSlot::effective_xmin, elog, ereport, errcode(), errmsg(), ERROR, GetOldestSafeDecodingTransactionId(), GetTopTransactionIdIfAny(), InvalidTransactionId, IsTransactionState(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MemoryContextSwitchTo(), ReplicationSlot::mutex, MyDatabaseId, MyReplicationSlot, ReplicationSlotPersistentData::name, NAMEDATALEN, NameStr, NIL, LogicalDecodingContext::options, ReorderBuffer::output_rewrites, ReplicationSlotPersistentData::plugin, OutputPluginOptions::receive_rewrites, LogicalDecodingContext::reorder, ReplicationSlotMarkDirty(), ReplicationSlotReserveWal(), ReplicationSlotSave(), ReplicationSlotsComputeRequiredXmin(), ReplicationSlotPersistentData::restart_lsn, SlotIsPhysical, SpinLockAcquire, SpinLockRelease, OutputPluginCallbacks::startup_cb, startup_cb_wrapper(), StartupDecodingContext(), StrNCpy, and XLogRecPtrIsInvalid.

Referenced by create_logical_replication_slot(), and CreateReplicationSlot().

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

◆ DecodingContextFindStartpoint()

void DecodingContextFindStartpoint ( LogicalDecodingContext ctx)

Definition at line 466 of file logical.c.

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

Referenced by create_logical_replication_slot(), and CreateReplicationSlot().

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

◆ DecodingContextReady()

bool DecodingContextReady ( LogicalDecodingContext ctx)

Definition at line 457 of file logical.c.

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

Referenced by DecodingContextFindStartpoint().

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

◆ filter_by_origin_cb_wrapper()

bool filter_by_origin_cb_wrapper ( LogicalDecodingContext ctx,
RepOriginId  origin_id 
)

Definition at line 805 of file logical.c.

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

Referenced by FilterByOrigin().

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

◆ FreeDecodingContext()

void FreeDecodingContext ( LogicalDecodingContext ctx)

Definition at line 512 of file logical.c.

References LogicalDecodingContext::callbacks, LogicalDecodingContext::context, FreeSnapshotBuilder(), MemoryContextDelete(), LogicalDecodingContext::reader, LogicalDecodingContext::reorder, ReorderBufferFree(), OutputPluginCallbacks::shutdown_cb, shutdown_cb_wrapper(), LogicalDecodingContext::snapshot_builder, and XLogReaderFree().

Referenced by create_logical_replication_slot(), CreateReplicationSlot(), pg_logical_replication_slot_advance(), pg_logical_slot_get_changes_guts(), and StartLogicalReplication().

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

◆ LoadOutputPlugin()

static void LoadOutputPlugin ( OutputPluginCallbacks callbacks,
char *  plugin 
)
static

Definition at line 566 of file logical.c.

References OutputPluginCallbacks::begin_cb, OutputPluginCallbacks::change_cb, OutputPluginCallbacks::commit_cb, elog, ERROR, and load_external_function().

Referenced by StartupDecodingContext().

567 {
568  LogicalOutputPluginInit plugin_init;
569 
570  plugin_init = (LogicalOutputPluginInit)
571  load_external_function(plugin, "_PG_output_plugin_init", false, NULL);
572 
573  if (plugin_init == NULL)
574  elog(ERROR, "output plugins have to declare the _PG_output_plugin_init symbol");
575 
576  /* ask the output plugin to fill the callback struct */
577  plugin_init(callbacks);
578 
579  if (callbacks->begin_cb == NULL)
580  elog(ERROR, "output plugins have to register a begin callback");
581  if (callbacks->change_cb == NULL)
582  elog(ERROR, "output plugins have to register a change callback");
583  if (callbacks->commit_cb == NULL)
584  elog(ERROR, "output plugins have to register a commit callback");
585 }
static const char * plugin
#define ERROR
Definition: elog.h:43
LogicalDecodeCommitCB commit_cb
void(* LogicalOutputPluginInit)(struct OutputPluginCallbacks *cb)
Definition: output_plugin.h:36
PGFunction load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
Definition: dfmgr.c:107
LogicalDecodeChangeCB change_cb
#define elog(elevel,...)
Definition: elog.h:226
LogicalDecodeBeginCB begin_cb

◆ LogicalConfirmReceivedLocation()

void LogicalConfirmReceivedLocation ( XLogRecPtr  lsn)

Definition at line 1005 of file logical.c.

References Assert, ReplicationSlot::candidate_catalog_xmin, ReplicationSlot::candidate_restart_lsn, ReplicationSlot::candidate_restart_valid, ReplicationSlot::candidate_xmin_lsn, ReplicationSlotPersistentData::catalog_xmin, ReplicationSlotPersistentData::confirmed_flush, ReplicationSlot::data, DEBUG1, ReplicationSlot::effective_catalog_xmin, elog, InvalidTransactionId, InvalidXLogRecPtr, ReplicationSlot::mutex, MyReplicationSlot, ReplicationSlotMarkDirty(), ReplicationSlotSave(), ReplicationSlotsComputeRequiredLSN(), ReplicationSlotsComputeRequiredXmin(), ReplicationSlotPersistentData::restart_lsn, SpinLockAcquire, SpinLockRelease, and TransactionIdIsValid.

Referenced by LogicalIncreaseRestartDecodingForSlot(), LogicalIncreaseXminForSlot(), pg_logical_replication_slot_advance(), pg_logical_slot_get_changes_guts(), and ProcessStandbyReplyMessage().

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

◆ LogicalIncreaseRestartDecodingForSlot()

void LogicalIncreaseRestartDecodingForSlot ( XLogRecPtr  current_lsn,
XLogRecPtr  restart_lsn 
)

Definition at line 936 of file logical.c.

References Assert, ReplicationSlot::candidate_restart_lsn, ReplicationSlot::candidate_restart_valid, ReplicationSlotPersistentData::confirmed_flush, ReplicationSlot::data, DEBUG1, elog, InvalidXLogRecPtr, LogicalConfirmReceivedLocation(), ReplicationSlot::mutex, MyReplicationSlot, SpinLockAcquire, and SpinLockRelease.

Referenced by SnapBuildProcessRunningXacts().

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

◆ LogicalIncreaseXminForSlot()

void LogicalIncreaseXminForSlot ( XLogRecPtr  current_lsn,
TransactionId  xmin 
)

Definition at line 879 of file logical.c.

References Assert, ReplicationSlot::candidate_catalog_xmin, ReplicationSlot::candidate_xmin_lsn, ReplicationSlotPersistentData::catalog_xmin, ReplicationSlotPersistentData::confirmed_flush, ReplicationSlot::data, InvalidXLogRecPtr, LogicalConfirmReceivedLocation(), ReplicationSlot::mutex, MyReplicationSlot, SpinLockAcquire, SpinLockRelease, and TransactionIdPrecedesOrEquals().

Referenced by SnapBuildProcessRunningXacts().

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

◆ message_cb_wrapper()

static void message_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn,
XLogRecPtr  message_lsn,
bool  transactional,
const char *  prefix,
Size  message_size,
const char *  message 
)
static

Definition at line 835 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, InvalidTransactionId, OutputPluginCallbacks::message_cb, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, LogicalErrorCallbackState::report_location, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

838 {
839  LogicalDecodingContext *ctx = cache->private_data;
841  ErrorContextCallback errcallback;
842 
843  Assert(!ctx->fast_forward);
844 
845  if (ctx->callbacks.message_cb == NULL)
846  return;
847 
848  /* Push callback + info on the error context stack */
849  state.ctx = ctx;
850  state.callback_name = "message";
851  state.report_location = message_lsn;
853  errcallback.arg = (void *) &state;
854  errcallback.previous = error_context_stack;
855  error_context_stack = &errcallback;
856 
857  /* set output state */
858  ctx->accept_writes = true;
859  ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
860  ctx->write_location = message_lsn;
861 
862  /* do the actual work: call callback */
863  ctx->callbacks.message_cb(ctx, txn, message_lsn, transactional, prefix,
864  message_size, message);
865 
866  /* Pop the error context stack */
867  error_context_stack = errcallback.previous;
868 }
TransactionId write_xid
Definition: logical.h:89
void * private_data
LogicalDecodeMessageCB message_cb
OutputPluginCallbacks callbacks
Definition: logical.h:53
void(* callback)(void *arg)
Definition: elog.h:254
struct ErrorContextCallback * previous
Definition: elog.h:253
const char * callback_name
Definition: logical.c:52
XLogRecPtr write_location
Definition: logical.h:88
ErrorContextCallback * error_context_stack
Definition: elog.c:88
#define InvalidTransactionId
Definition: transam.h:31
TransactionId xid
#define Assert(condition)
Definition: c.h:732
Definition: regguts.h:298
XLogRecPtr report_location
Definition: logical.c:53
LogicalDecodingContext * ctx
Definition: logical.c:51
static void output_plugin_error_callback(void *arg)
Definition: logical.c:588

◆ output_plugin_error_callback()

static void output_plugin_error_callback ( void *  arg)
static

Definition at line 588 of file logical.c.

References LogicalErrorCallbackState::callback_name, LogicalErrorCallbackState::ctx, ReplicationSlot::data, errcontext, InvalidXLogRecPtr, ReplicationSlotPersistentData::name, NameStr, ReplicationSlotPersistentData::plugin, LogicalErrorCallbackState::report_location, and LogicalDecodingContext::slot.

Referenced by begin_cb_wrapper(), change_cb_wrapper(), commit_cb_wrapper(), filter_by_origin_cb_wrapper(), message_cb_wrapper(), shutdown_cb_wrapper(), startup_cb_wrapper(), and truncate_cb_wrapper().

589 {
591 
592  /* not all callbacks have an associated LSN */
593  if (state->report_location != InvalidXLogRecPtr)
594  errcontext("slot \"%s\", output plugin \"%s\", in the %s callback, associated LSN %X/%X",
595  NameStr(state->ctx->slot->data.name),
596  NameStr(state->ctx->slot->data.plugin),
597  state->callback_name,
598  (uint32) (state->report_location >> 32),
599  (uint32) state->report_location);
600  else
601  errcontext("slot \"%s\", output plugin \"%s\", in the %s callback",
602  NameStr(state->ctx->slot->data.name),
603  NameStr(state->ctx->slot->data.plugin),
604  state->callback_name);
605 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
ReplicationSlotPersistentData data
Definition: slot.h:132
const char * callback_name
Definition: logical.c:52
unsigned int uint32
Definition: c.h:358
Definition: regguts.h:298
XLogRecPtr report_location
Definition: logical.c:53
ReplicationSlot * slot
Definition: logical.h:39
#define NameStr(name)
Definition: c.h:609
#define errcontext
Definition: elog.h:183
void * arg
LogicalDecodingContext * ctx
Definition: logical.c:51

◆ OutputPluginPrepareWrite()

void OutputPluginPrepareWrite ( struct LogicalDecodingContext ctx,
bool  last_write 
)

Definition at line 527 of file logical.c.

References LogicalDecodingContext::accept_writes, elog, ERROR, LogicalDecodingContext::prepare_write, LogicalDecodingContext::prepared_write, LogicalDecodingContext::write_location, and LogicalDecodingContext::write_xid.

Referenced by maybe_send_schema(), pg_decode_change(), pg_decode_commit_txn(), pg_decode_message(), pg_decode_truncate(), pg_output_begin(), pgoutput_begin_txn(), pgoutput_change(), pgoutput_commit_txn(), and pgoutput_truncate().

528 {
529  if (!ctx->accept_writes)
530  elog(ERROR, "writes are only accepted in commit, begin and change callbacks");
531 
532  ctx->prepare_write(ctx, ctx->write_location, ctx->write_xid, last_write);
533  ctx->prepared_write = true;
534 }
TransactionId write_xid
Definition: logical.h:89
XLogRecPtr write_location
Definition: logical.h:88
#define ERROR
Definition: elog.h:43
LogicalOutputPluginWriterPrepareWrite prepare_write
Definition: logical.h:64
#define elog(elevel,...)
Definition: elog.h:226

◆ OutputPluginUpdateProgress()

void OutputPluginUpdateProgress ( struct LogicalDecodingContext ctx)

Definition at line 553 of file logical.c.

References LogicalDecodingContext::update_progress, LogicalDecodingContext::write_location, and LogicalDecodingContext::write_xid.

Referenced by pgoutput_commit_txn().

554 {
555  if (!ctx->update_progress)
556  return;
557 
558  ctx->update_progress(ctx, ctx->write_location, ctx->write_xid);
559 }
TransactionId write_xid
Definition: logical.h:89
XLogRecPtr write_location
Definition: logical.h:88
LogicalOutputPluginWriterUpdateProgress update_progress
Definition: logical.h:66

◆ OutputPluginWrite()

void OutputPluginWrite ( struct LogicalDecodingContext ctx,
bool  last_write 
)

Definition at line 540 of file logical.c.

References elog, ERROR, LogicalDecodingContext::prepared_write, LogicalDecodingContext::write, LogicalDecodingContext::write_location, and LogicalDecodingContext::write_xid.

Referenced by maybe_send_schema(), pg_decode_change(), pg_decode_commit_txn(), pg_decode_message(), pg_decode_truncate(), pg_output_begin(), pgoutput_begin_txn(), pgoutput_change(), pgoutput_commit_txn(), and pgoutput_truncate().

541 {
542  if (!ctx->prepared_write)
543  elog(ERROR, "OutputPluginPrepareWrite needs to be called before OutputPluginWrite");
544 
545  ctx->write(ctx, ctx->write_location, ctx->write_xid, last_write);
546  ctx->prepared_write = false;
547 }
TransactionId write_xid
Definition: logical.h:89
XLogRecPtr write_location
Definition: logical.h:88
#define ERROR
Definition: elog.h:43
LogicalOutputPluginWriterWrite write
Definition: logical.h:65
#define elog(elevel,...)
Definition: elog.h:226

◆ shutdown_cb_wrapper()

static void shutdown_cb_wrapper ( LogicalDecodingContext ctx)
static

Definition at line 635 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, InvalidXLogRecPtr, output_plugin_error_callback(), ErrorContextCallback::previous, LogicalErrorCallbackState::report_location, and OutputPluginCallbacks::shutdown_cb.

Referenced by FreeDecodingContext().

636 {
638  ErrorContextCallback errcallback;
639 
640  Assert(!ctx->fast_forward);
641 
642  /* Push callback + info on the error context stack */
643  state.ctx = ctx;
644  state.callback_name = "shutdown";
647  errcallback.arg = (void *) &state;
648  errcallback.previous = error_context_stack;
649  error_context_stack = &errcallback;
650 
651  /* set output state */
652  ctx->accept_writes = false;
653 
654  /* do the actual work: call callback */
655  ctx->callbacks.shutdown_cb(ctx);
656 
657  /* Pop the error context stack */
658  error_context_stack = errcallback.previous;
659 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:53
void(* callback)(void *arg)
Definition: elog.h:254
struct ErrorContextCallback * previous
Definition: elog.h:253
const char * callback_name
Definition: logical.c:52
ErrorContextCallback * error_context_stack
Definition: elog.c:88
#define Assert(condition)
Definition: c.h:732
Definition: regguts.h:298
LogicalDecodeShutdownCB shutdown_cb
XLogRecPtr report_location
Definition: logical.c:53
LogicalDecodingContext * ctx
Definition: logical.c:51
static void output_plugin_error_callback(void *arg)
Definition: logical.c:588

◆ startup_cb_wrapper()

static void startup_cb_wrapper ( LogicalDecodingContext ctx,
OutputPluginOptions opt,
bool  is_init 
)
static

Definition at line 608 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, InvalidXLogRecPtr, output_plugin_error_callback(), ErrorContextCallback::previous, LogicalErrorCallbackState::report_location, and OutputPluginCallbacks::startup_cb.

Referenced by CreateDecodingContext(), and CreateInitDecodingContext().

609 {
611  ErrorContextCallback errcallback;
612 
613  Assert(!ctx->fast_forward);
614 
615  /* Push callback + info on the error context stack */
616  state.ctx = ctx;
617  state.callback_name = "startup";
620  errcallback.arg = (void *) &state;
621  errcallback.previous = error_context_stack;
622  error_context_stack = &errcallback;
623 
624  /* set output state */
625  ctx->accept_writes = false;
626 
627  /* do the actual work: call callback */
628  ctx->callbacks.startup_cb(ctx, opt, is_init);
629 
630  /* Pop the error context stack */
631  error_context_stack = errcallback.previous;
632 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:53
void(* callback)(void *arg)
Definition: elog.h:254
struct ErrorContextCallback * previous
Definition: elog.h:253
const char * callback_name
Definition: logical.c:52
ErrorContextCallback * error_context_stack
Definition: elog.c:88
#define Assert(condition)
Definition: c.h:732
Definition: regguts.h:298
LogicalDecodeStartupCB startup_cb
XLogRecPtr report_location
Definition: logical.c:53
LogicalDecodingContext * ctx
Definition: logical.c:51
static void output_plugin_error_callback(void *arg)
Definition: logical.c:588

◆ StartupDecodingContext()

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 
)
static

Definition at line 122 of file logical.c.

References AllocateSnapshotBuilder(), ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, ReorderBuffer::apply_change, ReorderBuffer::apply_truncate, ReorderBuffer::begin, begin_cb_wrapper(), LogicalDecodingContext::callbacks, change_cb_wrapper(), ReorderBuffer::commit, commit_cb_wrapper(), LogicalDecodingContext::context, LogicalErrorCallbackState::ctx, CurrentMemoryContext, ReplicationSlot::data, ereport, errcode(), errmsg(), ERROR, LogicalDecodingContext::fast_forward, IsTransactionOrTransactionBlock(), LoadOutputPlugin(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), makeStringInfo(), MemoryContextSwitchTo(), ReorderBuffer::message, message_cb_wrapper(), MyPgXact, MyReplicationSlot, NameStr, LogicalDecodingContext::out, LogicalDecodingContext::output_plugin_options, palloc0(), ReplicationSlotPersistentData::plugin, LogicalDecodingContext::prepare_write, ReorderBuffer::private_data, PROC_IN_LOGICAL_DECODING, LogicalDecodingContext::reader, LogicalDecodingContext::reorder, ReorderBufferAllocate(), LogicalDecodingContext::slot, LogicalDecodingContext::snapshot_builder, truncate_cb_wrapper(), LogicalDecodingContext::update_progress, PGXACT::vacuumFlags, wal_segment_size, LogicalDecodingContext::write, and XLogReaderAllocate().

Referenced by CreateDecodingContext(), and CreateInitDecodingContext().

131 {
132  ReplicationSlot *slot;
133  MemoryContext context,
134  old_context;
136 
137  /* shorter lines... */
138  slot = MyReplicationSlot;
139 
141  "Logical decoding context",
143  old_context = MemoryContextSwitchTo(context);
144  ctx = palloc0(sizeof(LogicalDecodingContext));
145 
146  ctx->context = context;
147 
148  /*
149  * (re-)load output plugins, so we detect a bad (removed) output plugin
150  * now.
151  */
152  if (!fast_forward)
154 
155  /*
156  * Now that the slot's xmin has been set, we can announce ourselves as a
157  * logical decoding backend which doesn't need to be checked individually
158  * when computing the xmin horizon because the xmin is enforced via
159  * replication slots.
160  *
161  * We can only do so if we're outside of a transaction (i.e. the case when
162  * streaming changes via walsender), otherwise an already setup
163  * snapshot/xid would end up being ignored. That's not a particularly
164  * bothersome restriction since the SQL interface can't be used for
165  * streaming anyway.
166  */
168  {
169  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
171  LWLockRelease(ProcArrayLock);
172  }
173 
174  ctx->slot = slot;
175 
176  ctx->reader = XLogReaderAllocate(wal_segment_size, NULL, read_page, ctx);
177  if (!ctx->reader)
178  ereport(ERROR,
179  (errcode(ERRCODE_OUT_OF_MEMORY),
180  errmsg("out of memory")));
181 
183  ctx->snapshot_builder =
184  AllocateSnapshotBuilder(ctx->reorder, xmin_horizon, start_lsn,
185  need_full_snapshot);
186 
187  ctx->reorder->private_data = ctx;
188 
189  /* wrap output plugin callbacks, so we can add error context information */
195 
196  ctx->out = makeStringInfo();
197  ctx->prepare_write = prepare_write;
198  ctx->write = do_write;
199  ctx->update_progress = update_progress;
200 
201  ctx->output_plugin_options = output_plugin_options;
202 
203  ctx->fast_forward = fast_forward;
204 
205  MemoryContextSwitchTo(old_context);
206 
207  return ctx;
208 }
ReorderBufferApplyChangeCB apply_change
#define AllocSetContextCreate
Definition: memutils.h:170
void * private_data
static void change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
Definition: logical.c:728
struct ReorderBuffer * reorder
Definition: logical.h:43
#define PROC_IN_LOGICAL_DECODING
Definition: proc.h:57
int wal_segment_size
Definition: xlog.c:112
static void LoadOutputPlugin(OutputPluginCallbacks *callbacks, char *plugin)
Definition: logical.c:566
static void commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:697
StringInfo makeStringInfo(void)
Definition: stringinfo.c:28
OutputPluginCallbacks callbacks
Definition: logical.h:53
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:570
bool IsTransactionOrTransactionBlock(void)
Definition: xact.c:4651
MemoryContext context
Definition: logical.h:36
List * output_plugin_options
Definition: logical.h:59
ReorderBufferCommitCB commit
ReplicationSlotPersistentData data
Definition: slot.h:132
PGXACT * MyPgXact
Definition: proc.c:69
uint8 vacuumFlags
Definition: proc.h:233
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
#define ERROR
Definition: elog.h:43
LogicalOutputPluginWriterPrepareWrite prepare_write
Definition: logical.h:64
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
LogicalOutputPluginWriterUpdateProgress update_progress
Definition: logical.h:66
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
ReorderBufferMessageCB message
#define ereport(elevel, rest)
Definition: elog.h:141
void * palloc0(Size size)
Definition: mcxt.c:980
static void begin_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
Definition: logical.c:667
struct SnapBuild * snapshot_builder
Definition: logical.h:44
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
ReorderBuffer * ReorderBufferAllocate(void)
XLogReaderState * XLogReaderAllocate(int wal_segment_size, const char *waldir, XLogPageReadCB pagereadfunc, void *private_data)
Definition: xlogreader.c:71
static void truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
Definition: logical.c:765
int errmsg(const char *fmt,...)
Definition: elog.c:784
XLogReaderState * reader
Definition: logical.h:42
ReplicationSlot * slot
Definition: logical.h:39
ReorderBufferApplyTruncateCB apply_truncate
LogicalOutputPluginWriterWrite write
Definition: logical.h:65
StringInfo out
Definition: logical.h:71
#define NameStr(name)
Definition: c.h:609
static void message_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr message_lsn, bool transactional, const char *prefix, Size message_size, const char *message)
Definition: logical.c:835
ReorderBufferBeginCB begin
SnapBuild * AllocateSnapshotBuilder(ReorderBuffer *reorder, TransactionId xmin_horizon, XLogRecPtr start_lsn, bool need_full_snapshot)
Definition: snapbuild.c:315

◆ truncate_cb_wrapper()

static void truncate_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn,
int  nrelations,
Relation  relations[],
ReorderBufferChange change 
)
static

Definition at line 765 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, ReorderBufferChange::lsn, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, LogicalErrorCallbackState::report_location, OutputPluginCallbacks::truncate_cb, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

767 {
768  LogicalDecodingContext *ctx = cache->private_data;
770  ErrorContextCallback errcallback;
771 
772  Assert(!ctx->fast_forward);
773 
774  if (!ctx->callbacks.truncate_cb)
775  return;
776 
777  /* Push callback + info on the error context stack */
778  state.ctx = ctx;
779  state.callback_name = "truncate";
780  state.report_location = change->lsn;
782  errcallback.arg = (void *) &state;
783  errcallback.previous = error_context_stack;
784  error_context_stack = &errcallback;
785 
786  /* set output state */
787  ctx->accept_writes = true;
788  ctx->write_xid = txn->xid;
789 
790  /*
791  * report this change's lsn so replies from clients can give an up2date
792  * answer. This won't ever be enough (and shouldn't be!) to confirm
793  * receipt of this transaction, but it might allow another transaction's
794  * commit to be confirmed with one message.
795  */
796  ctx->write_location = change->lsn;
797 
798  ctx->callbacks.truncate_cb(ctx, txn, nrelations, relations, change);
799 
800  /* Pop the error context stack */
801  error_context_stack = errcallback.previous;
802 }
LogicalDecodeTruncateCB truncate_cb
TransactionId write_xid
Definition: logical.h:89
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:53
void(* callback)(void *arg)
Definition: elog.h:254
struct ErrorContextCallback * previous
Definition: elog.h:253
const char * callback_name
Definition: logical.c:52
XLogRecPtr write_location
Definition: logical.h:88
ErrorContextCallback * error_context_stack
Definition: elog.c:88
TransactionId xid
#define Assert(condition)
Definition: c.h:732
Definition: regguts.h:298
XLogRecPtr report_location
Definition: logical.c:53
LogicalDecodingContext * ctx
Definition: logical.c:51
static void output_plugin_error_callback(void *arg)
Definition: logical.c:588