PostgreSQL Source Code  git master
logical.c File Reference
#include "postgres.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 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, 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 636 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().

637 {
638  LogicalDecodingContext *ctx = cache->private_data;
640  ErrorContextCallback errcallback;
641 
642  Assert(!ctx->fast_forward);
643 
644  /* Push callback + info on the error context stack */
645  state.ctx = ctx;
646  state.callback_name = "begin";
647  state.report_location = txn->first_lsn;
649  errcallback.arg = (void *) &state;
650  errcallback.previous = error_context_stack;
651  error_context_stack = &errcallback;
652 
653  /* set output state */
654  ctx->accept_writes = true;
655  ctx->write_xid = txn->xid;
656  ctx->write_location = txn->first_lsn;
657 
658  /* do the actual work: call callback */
659  ctx->callbacks.begin_cb(ctx, txn);
660 
661  /* Pop the error context stack */
662  error_context_stack = errcallback.previous;
663 }
XLogRecPtr first_lsn
TransactionId write_xid
Definition: logical.h:91
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:55
void(* callback)(void *arg)
Definition: elog.h:239
struct ErrorContextCallback * previous
Definition: elog.h:238
const char * callback_name
Definition: logical.c:51
XLogRecPtr write_location
Definition: logical.h:90
ErrorContextCallback * error_context_stack
Definition: elog.c:88
TransactionId xid
#define Assert(condition)
Definition: c.h:688
Definition: regguts.h:298
XLogRecPtr report_location
Definition: logical.c:52
LogicalDecodeBeginCB begin_cb
Definition: output_plugin.h:98
LogicalDecodingContext * ctx
Definition: logical.c:50
static void output_plugin_error_callback(void *arg)
Definition: logical.c:557

◆ change_cb_wrapper()

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

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

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 = "change";
709  state.report_location = change->lsn;
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 
719  /*
720  * report this change's lsn so replies from clients can give an up2date
721  * answer. This won't ever be enough (and shouldn't be!) to confirm
722  * receipt of this transaction, but it might allow another transaction's
723  * commit to be confirmed with one message.
724  */
725  ctx->write_location = change->lsn;
726 
727  ctx->callbacks.change_cb(ctx, txn, relation, change);
728 
729  /* Pop the error context stack */
730  error_context_stack = errcallback.previous;
731 }
TransactionId write_xid
Definition: logical.h:91
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:55
void(* callback)(void *arg)
Definition: elog.h:239
struct ErrorContextCallback * previous
Definition: elog.h:238
const char * callback_name
Definition: logical.c:51
XLogRecPtr write_location
Definition: logical.h:90
ErrorContextCallback * error_context_stack
Definition: elog.c:88
LogicalDecodeChangeCB change_cb
Definition: output_plugin.h:99
TransactionId xid
#define Assert(condition)
Definition: c.h:688
Definition: regguts.h:298
XLogRecPtr report_location
Definition: logical.c:52
LogicalDecodingContext * ctx
Definition: logical.c:50
static void output_plugin_error_callback(void *arg)
Definition: logical.c:557

◆ CheckLogicalDecodingRequirements()

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:965
int wal_level
Definition: xlog.c:104
int errcode(int sqlerrcode)
Definition: elog.c:575
bool RecoveryInProgress(void)
Definition: xlog.c:7919
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
Oid MyDatabaseId
Definition: globals.c:77
#define InvalidOid
Definition: postgres_ext.h:36
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ commit_cb_wrapper()

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

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

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 = "commit";
678  state.report_location = txn->final_lsn; /* beginning of commit record */
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->end_lsn; /* points to the end of the record */
688 
689  /* do the actual work: call callback */
690  ctx->callbacks.commit_cb(ctx, txn, commit_lsn);
691 
692  /* Pop the error context stack */
693  error_context_stack = errcallback.previous;
694 }
TransactionId write_xid
Definition: logical.h:91
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:55
void(* callback)(void *arg)
Definition: elog.h:239
struct ErrorContextCallback * previous
Definition: elog.h:238
const char * callback_name
Definition: logical.c:51
XLogRecPtr write_location
Definition: logical.h:90
ErrorContextCallback * error_context_stack
Definition: elog.c:88
LogicalDecodeCommitCB commit_cb
XLogRecPtr final_lsn
TransactionId xid
#define Assert(condition)
Definition: c.h:688
Definition: regguts.h:298
XLogRecPtr end_lsn
XLogRecPtr report_location
Definition: logical.c:52
LogicalDecodingContext * ctx
Definition: logical.c:50
static void output_plugin_error_callback(void *arg)
Definition: logical.c:557

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

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

◆ CreateInitDecodingContext()

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

Definition at line 220 of file logical.c.

References LogicalDecodingContext::callbacks, ReplicationSlotPersistentData::catalog_xmin, LogicalDecodingContext::context, LogicalErrorCallbackState::ctx, ReplicationSlot::data, ReplicationSlotPersistentData::database, ReplicationSlot::effective_catalog_xmin, ReplicationSlot::effective_xmin, elog, ereport, errcode(), errmsg(), ERROR, GetOldestSafeDecodingTransactionId(), GetTopTransactionIdIfAny(), InvalidTransactionId, InvalidXLogRecPtr, IsTransactionState(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MemoryContextSwitchTo(), ReplicationSlot::mutex, MyDatabaseId, MyReplicationSlot, ReplicationSlotPersistentData::name, NAMEDATALEN, NameStr, NIL, LogicalDecodingContext::options, 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().

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

◆ DecodingContextFindStartpoint()

void DecodingContextFindStartpoint ( LogicalDecodingContext ctx)

Definition at line 438 of file logical.c.

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

Referenced by CreateReplicationSlot(), and pg_create_logical_replication_slot().

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

◆ DecodingContextReady()

bool DecodingContextReady ( LogicalDecodingContext ctx)

Definition at line 429 of file logical.c.

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

Referenced by DecodingContextFindStartpoint().

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

◆ filter_by_origin_cb_wrapper()

bool filter_by_origin_cb_wrapper ( LogicalDecodingContext ctx,
RepOriginId  origin_id 
)

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

735 {
737  ErrorContextCallback errcallback;
738  bool ret;
739 
740  Assert(!ctx->fast_forward);
741 
742  /* Push callback + info on the error context stack */
743  state.ctx = ctx;
744  state.callback_name = "filter_by_origin";
747  errcallback.arg = (void *) &state;
748  errcallback.previous = error_context_stack;
749  error_context_stack = &errcallback;
750 
751  /* set output state */
752  ctx->accept_writes = false;
753 
754  /* do the actual work: call callback */
755  ret = ctx->callbacks.filter_by_origin_cb(ctx, origin_id);
756 
757  /* Pop the error context stack */
758  error_context_stack = errcallback.previous;
759 
760  return ret;
761 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:55
void(* callback)(void *arg)
Definition: elog.h:239
struct ErrorContextCallback * previous
Definition: elog.h:238
const char * callback_name
Definition: logical.c:51
ErrorContextCallback * error_context_stack
Definition: elog.c:88
#define Assert(condition)
Definition: c.h:688
Definition: regguts.h:298
XLogRecPtr report_location
Definition: logical.c:52
LogicalDecodingContext * ctx
Definition: logical.c:50
static void output_plugin_error_callback(void *arg)
Definition: logical.c:557
LogicalDecodeFilterByOriginCB filter_by_origin_cb

◆ FreeDecodingContext()

void FreeDecodingContext ( LogicalDecodingContext ctx)

Definition at line 481 of file logical.c.

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

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

482 {
483  if (ctx->callbacks.shutdown_cb != NULL)
484  shutdown_cb_wrapper(ctx);
485 
488  XLogReaderFree(ctx->reader);
490 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:198
struct ReorderBuffer * reorder
Definition: logical.h:45
OutputPluginCallbacks callbacks
Definition: logical.h:55
void ReorderBufferFree(ReorderBuffer *rb)
MemoryContext context
Definition: logical.h:38
static void shutdown_cb_wrapper(LogicalDecodingContext *ctx)
Definition: logical.c:604
void XLogReaderFree(XLogReaderState *state)
Definition: xlogreader.c:127
void FreeSnapshotBuilder(SnapBuild *builder)
Definition: snapbuild.c:357
struct SnapBuild * snapshot_builder
Definition: logical.h:46
LogicalDecodeShutdownCB shutdown_cb
XLogReaderState * reader
Definition: logical.h:44

◆ LoadOutputPlugin()

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

Definition at line 535 of file logical.c.

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

Referenced by StartupDecodingContext().

536 {
537  LogicalOutputPluginInit plugin_init;
538 
539  plugin_init = (LogicalOutputPluginInit)
540  load_external_function(plugin, "_PG_output_plugin_init", false, NULL);
541 
542  if (plugin_init == NULL)
543  elog(ERROR, "output plugins have to declare the _PG_output_plugin_init symbol");
544 
545  /* ask the output plugin to fill the callback struct */
546  plugin_init(callbacks);
547 
548  if (callbacks->begin_cb == NULL)
549  elog(ERROR, "output plugins have to register a begin callback");
550  if (callbacks->change_cb == NULL)
551  elog(ERROR, "output plugins have to register a change callback");
552  if (callbacks->commit_cb == NULL)
553  elog(ERROR, "output plugins have to register a commit callback");
554 }
static const char * plugin
#define ERROR
Definition: elog.h:43
LogicalDecodeCommitCB commit_cb
void(* LogicalOutputPluginInit)(struct OutputPluginCallbacks *cb)
Definition: output_plugin.h:35
PGFunction load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
Definition: dfmgr.c:94
LogicalDecodeChangeCB change_cb
Definition: output_plugin.h:99
LogicalDecodeBeginCB begin_cb
Definition: output_plugin.h:98
#define elog
Definition: elog.h:219

◆ LogicalConfirmReceivedLocation()

void LogicalConfirmReceivedLocation ( XLogRecPtr  lsn)

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

935 {
936  Assert(lsn != InvalidXLogRecPtr);
937 
938  /* Do an unlocked check for candidate_lsn first. */
941  {
942  bool updated_xmin = false;
943  bool updated_restart = false;
944 
946 
948 
949  /* if we're past the location required for bumping xmin, do so */
952  {
953  /*
954  * We have to write the changed xmin to disk *before* we change
955  * the in-memory value, otherwise after a crash we wouldn't know
956  * that some catalog tuples might have been removed already.
957  *
958  * Ensure that by first writing to ->xmin and only update
959  * ->effective_xmin once the new state is synced to disk. After a
960  * crash ->effective_xmin is set to ->xmin.
961  */
964  {
968  updated_xmin = true;
969  }
970  }
971 
974  {
976 
980  updated_restart = true;
981  }
982 
984 
985  /* first write new xmin to disk, so we know what's up after a crash */
986  if (updated_xmin || updated_restart)
987  {
990  elog(DEBUG1, "updated xmin: %u restart: %u", updated_xmin, updated_restart);
991  }
992 
993  /*
994  * Now the new xmin is safely on disk, we can let the global value
995  * advance. We do not take ProcArrayLock or similar since we only
996  * advance xmin here and there's not much harm done by a concurrent
997  * computation missing that.
998  */
999  if (updated_xmin)
1000  {
1004 
1007  }
1008  }
1009  else
1010  {
1014  }
1015 }
TransactionId candidate_catalog_xmin
Definition: slot.h:135
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
void ReplicationSlotSave(void)
Definition: slot.c:638
ReplicationSlotPersistentData data
Definition: slot.h:120
XLogRecPtr confirmed_flush
Definition: slot.h:81
#define SpinLockAcquire(lock)
Definition: spin.h:62
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:741
XLogRecPtr candidate_restart_valid
Definition: slot.h:137
TransactionId catalog_xmin
Definition: slot.h:70
#define InvalidTransactionId
Definition: transam.h:31
#define SpinLockRelease(lock)
Definition: spin.h:64
TransactionId effective_catalog_xmin
Definition: slot.h:117
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:688
XLogRecPtr restart_lsn
Definition: slot.h:73
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:136
#define elog
Definition: elog.h:219
slock_t mutex
Definition: slot.h:93
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:695
XLogRecPtr candidate_restart_lsn
Definition: slot.h:138
void ReplicationSlotMarkDirty(void)
Definition: slot.c:656

◆ LogicalIncreaseRestartDecodingForSlot()

void LogicalIncreaseRestartDecodingForSlot ( XLogRecPtr  current_lsn,
XLogRecPtr  restart_lsn 
)

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

866 {
867  bool updated_lsn = false;
868  ReplicationSlot *slot;
869 
870  slot = MyReplicationSlot;
871 
872  Assert(slot != NULL);
873  Assert(restart_lsn != InvalidXLogRecPtr);
874  Assert(current_lsn != InvalidXLogRecPtr);
875 
876  SpinLockAcquire(&slot->mutex);
877 
878  /* don't overwrite if have a newer restart lsn */
879  if (restart_lsn <= slot->data.restart_lsn)
880  {
881  }
882 
883  /*
884  * We might have already flushed far enough to directly accept this lsn,
885  * in this case there is no need to check for existing candidate LSNs
886  */
887  else if (current_lsn <= slot->data.confirmed_flush)
888  {
889  slot->candidate_restart_valid = current_lsn;
890  slot->candidate_restart_lsn = restart_lsn;
891 
892  /* our candidate can directly be used */
893  updated_lsn = true;
894  }
895 
896  /*
897  * Only increase if the previous values have been applied, otherwise we
898  * might never end up updating if the receiver acks too slowly. A missed
899  * value here will just cause some extra effort after reconnecting.
900  */
902  {
903  slot->candidate_restart_valid = current_lsn;
904  slot->candidate_restart_lsn = restart_lsn;
905 
906  elog(DEBUG1, "got new restart lsn %X/%X at %X/%X",
907  (uint32) (restart_lsn >> 32), (uint32) restart_lsn,
908  (uint32) (current_lsn >> 32), (uint32) current_lsn);
909  }
910  else
911  {
912  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",
913  (uint32) (restart_lsn >> 32), (uint32) restart_lsn,
914  (uint32) (current_lsn >> 32), (uint32) current_lsn,
915  (uint32) (slot->candidate_restart_lsn >> 32),
917  (uint32) (slot->candidate_restart_valid >> 32),
919  (uint32) (slot->data.confirmed_flush >> 32),
920  (uint32) slot->data.confirmed_flush
921  );
922  }
923  SpinLockRelease(&slot->mutex);
924 
925  /* candidates are already valid with the current flush position, apply */
926  if (updated_lsn)
928 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
ReplicationSlotPersistentData data
Definition: slot.h:120
XLogRecPtr confirmed_flush
Definition: slot.h:81
#define SpinLockAcquire(lock)
Definition: spin.h:62
XLogRecPtr candidate_restart_valid
Definition: slot.h:137
unsigned int uint32
Definition: c.h:314
#define SpinLockRelease(lock)
Definition: spin.h:64
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:688
void LogicalConfirmReceivedLocation(XLogRecPtr lsn)
Definition: logical.c:934
#define elog
Definition: elog.h:219
slock_t mutex
Definition: slot.h:93
XLogRecPtr candidate_restart_lsn
Definition: slot.h:138

◆ LogicalIncreaseXminForSlot()

void LogicalIncreaseXminForSlot ( XLogRecPtr  current_lsn,
TransactionId  xmin 
)

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

809 {
810  bool updated_xmin = false;
811  ReplicationSlot *slot;
812 
813  slot = MyReplicationSlot;
814 
815  Assert(slot != NULL);
816 
817  SpinLockAcquire(&slot->mutex);
818 
819  /*
820  * don't overwrite if we already have a newer xmin. This can happen if we
821  * restart decoding in a slot.
822  */
824  {
825  }
826 
827  /*
828  * If the client has already confirmed up to this lsn, we directly can
829  * mark this as accepted. This can happen if we restart decoding in a
830  * slot.
831  */
832  else if (current_lsn <= slot->data.confirmed_flush)
833  {
834  slot->candidate_catalog_xmin = xmin;
835  slot->candidate_xmin_lsn = current_lsn;
836 
837  /* our candidate can directly be used */
838  updated_xmin = true;
839  }
840 
841  /*
842  * Only increase if the previous values have been applied, otherwise we
843  * might never end up updating if the receiver acks too slowly.
844  */
845  else if (slot->candidate_xmin_lsn == InvalidXLogRecPtr)
846  {
847  slot->candidate_catalog_xmin = xmin;
848  slot->candidate_xmin_lsn = current_lsn;
849  }
850  SpinLockRelease(&slot->mutex);
851 
852  /* candidate already valid with the current flush position, apply */
853  if (updated_xmin)
855 }
TransactionId candidate_catalog_xmin
Definition: slot.h:135
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
ReplicationSlotPersistentData data
Definition: slot.h:120
XLogRecPtr confirmed_flush
Definition: slot.h:81
#define SpinLockAcquire(lock)
Definition: spin.h:62
bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:319
TransactionId catalog_xmin
Definition: slot.h:70
#define SpinLockRelease(lock)
Definition: spin.h:64
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:688
void LogicalConfirmReceivedLocation(XLogRecPtr lsn)
Definition: logical.c:934
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:136
slock_t mutex
Definition: slot.h:93

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

767 {
768  LogicalDecodingContext *ctx = cache->private_data;
770  ErrorContextCallback errcallback;
771 
772  Assert(!ctx->fast_forward);
773 
774  if (ctx->callbacks.message_cb == NULL)
775  return;
776 
777  /* Push callback + info on the error context stack */
778  state.ctx = ctx;
779  state.callback_name = "message";
780  state.report_location = message_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 != NULL ? txn->xid : InvalidTransactionId;
789  ctx->write_location = message_lsn;
790 
791  /* do the actual work: call callback */
792  ctx->callbacks.message_cb(ctx, txn, message_lsn, transactional, prefix,
793  message_size, message);
794 
795  /* Pop the error context stack */
796  error_context_stack = errcallback.previous;
797 }
TransactionId write_xid
Definition: logical.h:91
void * private_data
LogicalDecodeMessageCB message_cb
OutputPluginCallbacks callbacks
Definition: logical.h:55
void(* callback)(void *arg)
Definition: elog.h:239
struct ErrorContextCallback * previous
Definition: elog.h:238
const char * callback_name
Definition: logical.c:51
XLogRecPtr write_location
Definition: logical.h:90
ErrorContextCallback * error_context_stack
Definition: elog.c:88
#define InvalidTransactionId
Definition: transam.h:31
TransactionId xid
#define Assert(condition)
Definition: c.h:688
Definition: regguts.h:298
XLogRecPtr report_location
Definition: logical.c:52
LogicalDecodingContext * ctx
Definition: logical.c:50
static void output_plugin_error_callback(void *arg)
Definition: logical.c:557

◆ output_plugin_error_callback()

static void output_plugin_error_callback ( void *  arg)
static

Definition at line 557 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(), and startup_cb_wrapper().

558 {
560 
561  /* not all callbacks have an associated LSN */
562  if (state->report_location != InvalidXLogRecPtr)
563  errcontext("slot \"%s\", output plugin \"%s\", in the %s callback, associated LSN %X/%X",
564  NameStr(state->ctx->slot->data.name),
565  NameStr(state->ctx->slot->data.plugin),
566  state->callback_name,
567  (uint32) (state->report_location >> 32),
568  (uint32) state->report_location);
569  else
570  errcontext("slot \"%s\", output plugin \"%s\", in the %s callback",
571  NameStr(state->ctx->slot->data.name),
572  NameStr(state->ctx->slot->data.plugin),
573  state->callback_name);
574 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
ReplicationSlotPersistentData data
Definition: slot.h:120
const char * callback_name
Definition: logical.c:51
unsigned int uint32
Definition: c.h:314
Definition: regguts.h:298
XLogRecPtr report_location
Definition: logical.c:52
ReplicationSlot * slot
Definition: logical.h:41
#define NameStr(name)
Definition: c.h:565
#define errcontext
Definition: elog.h:164
void * arg
LogicalDecodingContext * ctx
Definition: logical.c:50

◆ OutputPluginPrepareWrite()

void OutputPluginPrepareWrite ( struct LogicalDecodingContext ctx,
bool  last_write 
)

Definition at line 496 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 pg_decode_change(), pg_decode_commit_txn(), pg_decode_message(), pg_output_begin(), pgoutput_begin_txn(), pgoutput_change(), and pgoutput_commit_txn().

497 {
498  if (!ctx->accept_writes)
499  elog(ERROR, "writes are only accepted in commit, begin and change callbacks");
500 
501  ctx->prepare_write(ctx, ctx->write_location, ctx->write_xid, last_write);
502  ctx->prepared_write = true;
503 }
TransactionId write_xid
Definition: logical.h:91
XLogRecPtr write_location
Definition: logical.h:90
#define ERROR
Definition: elog.h:43
LogicalOutputPluginWriterPrepareWrite prepare_write
Definition: logical.h:66
#define elog
Definition: elog.h:219

◆ OutputPluginUpdateProgress()

void OutputPluginUpdateProgress ( struct LogicalDecodingContext ctx)

Definition at line 522 of file logical.c.

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

Referenced by pgoutput_commit_txn().

523 {
524  if (!ctx->update_progress)
525  return;
526 
527  ctx->update_progress(ctx, ctx->write_location, ctx->write_xid);
528 }
TransactionId write_xid
Definition: logical.h:91
XLogRecPtr write_location
Definition: logical.h:90
LogicalOutputPluginWriterUpdateProgress update_progress
Definition: logical.h:68

◆ OutputPluginWrite()

void OutputPluginWrite ( struct LogicalDecodingContext ctx,
bool  last_write 
)

Definition at line 509 of file logical.c.

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

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

510 {
511  if (!ctx->prepared_write)
512  elog(ERROR, "OutputPluginPrepareWrite needs to be called before OutputPluginWrite");
513 
514  ctx->write(ctx, ctx->write_location, ctx->write_xid, last_write);
515  ctx->prepared_write = false;
516 }
TransactionId write_xid
Definition: logical.h:91
XLogRecPtr write_location
Definition: logical.h:90
#define ERROR
Definition: elog.h:43
LogicalOutputPluginWriterWrite write
Definition: logical.h:67
#define elog
Definition: elog.h:219

◆ shutdown_cb_wrapper()

static void shutdown_cb_wrapper ( LogicalDecodingContext ctx)
static

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

605 {
607  ErrorContextCallback errcallback;
608 
609  Assert(!ctx->fast_forward);
610 
611  /* Push callback + info on the error context stack */
612  state.ctx = ctx;
613  state.callback_name = "shutdown";
616  errcallback.arg = (void *) &state;
617  errcallback.previous = error_context_stack;
618  error_context_stack = &errcallback;
619 
620  /* set output state */
621  ctx->accept_writes = false;
622 
623  /* do the actual work: call callback */
624  ctx->callbacks.shutdown_cb(ctx);
625 
626  /* Pop the error context stack */
627  error_context_stack = errcallback.previous;
628 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:55
void(* callback)(void *arg)
Definition: elog.h:239
struct ErrorContextCallback * previous
Definition: elog.h:238
const char * callback_name
Definition: logical.c:51
ErrorContextCallback * error_context_stack
Definition: elog.c:88
#define Assert(condition)
Definition: c.h:688
Definition: regguts.h:298
LogicalDecodeShutdownCB shutdown_cb
XLogRecPtr report_location
Definition: logical.c:52
LogicalDecodingContext * ctx
Definition: logical.c:50
static void output_plugin_error_callback(void *arg)
Definition: logical.c:557

◆ startup_cb_wrapper()

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

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

578 {
580  ErrorContextCallback errcallback;
581 
582  Assert(!ctx->fast_forward);
583 
584  /* Push callback + info on the error context stack */
585  state.ctx = ctx;
586  state.callback_name = "startup";
589  errcallback.arg = (void *) &state;
590  errcallback.previous = error_context_stack;
591  error_context_stack = &errcallback;
592 
593  /* set output state */
594  ctx->accept_writes = false;
595 
596  /* do the actual work: call callback */
597  ctx->callbacks.startup_cb(ctx, opt, is_init);
598 
599  /* Pop the error context stack */
600  error_context_stack = errcallback.previous;
601 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:55
void(* callback)(void *arg)
Definition: elog.h:239
struct ErrorContextCallback * previous
Definition: elog.h:238
const char * callback_name
Definition: logical.c:51
ErrorContextCallback * error_context_stack
Definition: elog.c:88
#define Assert(condition)
Definition: c.h:688
Definition: regguts.h:298
LogicalDecodeStartupCB startup_cb
Definition: output_plugin.h:97
XLogRecPtr report_location
Definition: logical.c:52
LogicalDecodingContext * ctx
Definition: logical.c:50
static void output_plugin_error_callback(void *arg)
Definition: logical.c:557

◆ 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 114 of file logical.c.

References AllocateSnapshotBuilder(), ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, ReorderBuffer::apply_change, 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, XLogReaderState::private_data, ReorderBuffer::private_data, PROC_IN_LOGICAL_DECODING, LogicalDecodingContext::reader, LogicalDecodingContext::reorder, ReorderBufferAllocate(), LogicalDecodingContext::slot, LogicalDecodingContext::snapshot_builder, LogicalDecodingContext::update_progress, PGXACT::vacuumFlags, wal_segment_size, LogicalDecodingContext::write, and XLogReaderAllocate().

Referenced by CreateDecodingContext(), and CreateInitDecodingContext().

123 {
124  ReplicationSlot *slot;
125  MemoryContext context,
126  old_context;
128 
129  /* shorter lines... */
130  slot = MyReplicationSlot;
131 
133  "Logical decoding context",
135  old_context = MemoryContextSwitchTo(context);
136  ctx = palloc0(sizeof(LogicalDecodingContext));
137 
138  ctx->context = context;
139 
140  /*
141  * (re-)load output plugins, so we detect a bad (removed) output plugin
142  * now.
143  */
144  if (!fast_forward)
146 
147  /*
148  * Now that the slot's xmin has been set, we can announce ourselves as a
149  * logical decoding backend which doesn't need to be checked individually
150  * when computing the xmin horizon because the xmin is enforced via
151  * replication slots.
152  *
153  * We can only do so if we're outside of a transaction (i.e. the case when
154  * streaming changes via walsender), otherwise an already setup
155  * snapshot/xid would end up being ignored. That's not a particularly
156  * bothersome restriction since the SQL interface can't be used for
157  * streaming anyway.
158  */
160  {
161  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
163  LWLockRelease(ProcArrayLock);
164  }
165 
166  ctx->slot = slot;
167 
168  ctx->reader = XLogReaderAllocate(wal_segment_size, read_page, ctx);
169  if (!ctx->reader)
170  ereport(ERROR,
171  (errcode(ERRCODE_OUT_OF_MEMORY),
172  errmsg("out of memory")));
173 
174  ctx->reader->private_data = ctx;
175 
177  ctx->snapshot_builder =
178  AllocateSnapshotBuilder(ctx->reorder, xmin_horizon, start_lsn,
179  need_full_snapshot);
180 
181  ctx->reorder->private_data = ctx;
182 
183  /* wrap output plugin callbacks, so we can add error context information */
188 
189  ctx->out = makeStringInfo();
190  ctx->prepare_write = prepare_write;
191  ctx->write = do_write;
192  ctx->update_progress = update_progress;
193 
194  ctx->output_plugin_options = output_plugin_options;
195 
196  ctx->fast_forward = fast_forward;
197 
198  MemoryContextSwitchTo(old_context);
199 
200  return ctx;
201 }
ReorderBufferApplyChangeCB apply_change
void * private_data
static void change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
Definition: logical.c:697
struct ReorderBuffer * reorder
Definition: logical.h:45
#define PROC_IN_LOGICAL_DECODING
Definition: proc.h:57
int wal_segment_size
Definition: xlog.c:113
static void LoadOutputPlugin(OutputPluginCallbacks *callbacks, char *plugin)
Definition: logical.c:535
static void commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:666
StringInfo makeStringInfo(void)
Definition: stringinfo.c:28
OutputPluginCallbacks callbacks
Definition: logical.h:55
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
bool IsTransactionOrTransactionBlock(void)
Definition: xact.c:4466
MemoryContext context
Definition: logical.h:38
List * output_plugin_options
Definition: logical.h:61
ReorderBufferCommitCB commit
ReplicationSlotPersistentData data
Definition: slot.h:120
void * private_data
Definition: xlogreader.h:113
PGXACT * MyPgXact
Definition: proc.c:68
uint8 vacuumFlags
Definition: proc.h:230
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1724
#define ERROR
Definition: elog.h:43
LogicalOutputPluginWriterPrepareWrite prepare_write
Definition: logical.h:66
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:197
LogicalOutputPluginWriterUpdateProgress update_progress
Definition: logical.h:68
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
ReorderBufferMessageCB message
#define ereport(elevel, rest)
Definition: elog.h:122
#define AllocSetContextCreate(parent, name, allocparams)
Definition: memutils.h:165
XLogReaderState * XLogReaderAllocate(int wal_segment_size, XLogPageReadCB pagereadfunc, void *private_data)
Definition: xlogreader.c:67
void * palloc0(Size size)
Definition: mcxt.c:864
static void begin_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
Definition: logical.c:636
struct SnapBuild * snapshot_builder
Definition: logical.h:46
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1120
ReorderBuffer * ReorderBufferAllocate(void)
int errmsg(const char *fmt,...)
Definition: elog.c:797
XLogReaderState * reader
Definition: logical.h:44
ReplicationSlot * slot
Definition: logical.h:41
LogicalOutputPluginWriterWrite write
Definition: logical.h:67
StringInfo out
Definition: logical.h:73
#define NameStr(name)
Definition: c.h:565
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:764
ReorderBufferBeginCB begin
SnapBuild * AllocateSnapshotBuilder(ReorderBuffer *reorder, TransactionId xmin_horizon, XLogRecPtr start_lsn, bool need_full_snapshot)
Definition: snapbuild.c:316