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

649 {
650  LogicalDecodingContext *ctx = cache->private_data;
652  ErrorContextCallback errcallback;
653 
654  Assert(!ctx->fast_forward);
655 
656  /* Push callback + info on the error context stack */
657  state.ctx = ctx;
658  state.callback_name = "begin";
659  state.report_location = txn->first_lsn;
661  errcallback.arg = (void *) &state;
662  errcallback.previous = error_context_stack;
663  error_context_stack = &errcallback;
664 
665  /* set output state */
666  ctx->accept_writes = true;
667  ctx->write_xid = txn->xid;
668  ctx->write_location = txn->first_lsn;
669 
670  /* do the actual work: call callback */
671  ctx->callbacks.begin_cb(ctx, txn);
672 
673  /* Pop the error context stack */
674  error_context_stack = errcallback.previous;
675 }
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:699
Definition: regguts.h:298
XLogRecPtr report_location
Definition: logical.c:52
LogicalDecodeBeginCB begin_cb
LogicalDecodingContext * ctx
Definition: logical.c:50
static void output_plugin_error_callback(void *arg)
Definition: logical.c:569

◆ change_cb_wrapper()

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

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

711 {
712  LogicalDecodingContext *ctx = cache->private_data;
714  ErrorContextCallback errcallback;
715 
716  Assert(!ctx->fast_forward);
717 
718  /* Push callback + info on the error context stack */
719  state.ctx = ctx;
720  state.callback_name = "change";
721  state.report_location = change->lsn;
723  errcallback.arg = (void *) &state;
724  errcallback.previous = error_context_stack;
725  error_context_stack = &errcallback;
726 
727  /* set output state */
728  ctx->accept_writes = true;
729  ctx->write_xid = txn->xid;
730 
731  /*
732  * report this change's lsn so replies from clients can give an up2date
733  * answer. This won't ever be enough (and shouldn't be!) to confirm
734  * receipt of this transaction, but it might allow another transaction's
735  * commit to be confirmed with one message.
736  */
737  ctx->write_location = change->lsn;
738 
739  ctx->callbacks.change_cb(ctx, txn, relation, change);
740 
741  /* Pop the error context stack */
742  error_context_stack = errcallback.previous;
743 }
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
TransactionId xid
#define Assert(condition)
Definition: c.h:699
Definition: regguts.h:298
XLogRecPtr report_location
Definition: logical.c:52
LogicalDecodingContext * ctx
Definition: logical.c:50
static void output_plugin_error_callback(void *arg)
Definition: logical.c:569

◆ CheckLogicalDecodingRequirements()

void CheckLogicalDecodingRequirements ( void  )

Definition at line 78 of file logical.c.

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

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

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

◆ commit_cb_wrapper()

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

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

680 {
681  LogicalDecodingContext *ctx = cache->private_data;
683  ErrorContextCallback errcallback;
684 
685  Assert(!ctx->fast_forward);
686 
687  /* Push callback + info on the error context stack */
688  state.ctx = ctx;
689  state.callback_name = "commit";
690  state.report_location = txn->final_lsn; /* beginning of commit record */
692  errcallback.arg = (void *) &state;
693  errcallback.previous = error_context_stack;
694  error_context_stack = &errcallback;
695 
696  /* set output state */
697  ctx->accept_writes = true;
698  ctx->write_xid = txn->xid;
699  ctx->write_location = txn->end_lsn; /* points to the end of the record */
700 
701  /* do the actual work: call callback */
702  ctx->callbacks.commit_cb(ctx, txn, commit_lsn);
703 
704  /* Pop the error context stack */
705  error_context_stack = errcallback.previous;
706 }
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:699
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:569

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

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

◆ CreateInitDecodingContext()

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

Definition at line 223 of file logical.c.

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

Referenced by CreateReplicationSlot(), and pg_create_logical_replication_slot().

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

◆ DecodingContextFindStartpoint()

void DecodingContextFindStartpoint ( LogicalDecodingContext ctx)

Definition at line 447 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 CreateReplicationSlot(), and pg_create_logical_replication_slot().

448 {
450  ReplicationSlot *slot = ctx->slot;
451 
452  /* Initialize from where to start reading WAL. */
453  startptr = slot->data.restart_lsn;
454 
455  elog(DEBUG1, "searching for logical decoding starting point, starting at %X/%X",
456  (uint32) (slot->data.restart_lsn >> 32),
457  (uint32) slot->data.restart_lsn);
458 
459  /* Wait for a consistent starting point */
460  for (;;)
461  {
462  XLogRecord *record;
463  char *err = NULL;
464 
465  /* the read_page callback waits for new WAL */
466  record = XLogReadRecord(ctx->reader, startptr, &err);
467  if (err)
468  elog(ERROR, "%s", err);
469  if (!record)
470  elog(ERROR, "no record found"); /* shouldn't happen */
471 
472  startptr = InvalidXLogRecPtr;
473 
475 
476  /* only continue till we found a consistent spot */
477  if (DecodingContextReady(ctx))
478  break;
479 
481  }
482 
483  SpinLockAcquire(&slot->mutex);
484  slot->data.confirmed_flush = ctx->reader->EndRecPtr;
485  SpinLockRelease(&slot->mutex);
486 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
bool DecodingContextReady(LogicalDecodingContext *ctx)
Definition: logical.c:438
ReplicationSlotPersistentData data
Definition: slot.h:133
XLogRecord * XLogReadRecord(XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
Definition: xlogreader.c:216
XLogRecPtr confirmed_flush
Definition: slot.h:81
XLogRecPtr EndRecPtr
Definition: xlogreader.h:120
#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:325
#define SpinLockRelease(lock)
Definition: spin.h:64
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
slock_t mutex
Definition: slot.h:106
static XLogRecPtr startptr
Definition: basebackup.c:106

◆ DecodingContextReady()

bool DecodingContextReady ( LogicalDecodingContext ctx)

Definition at line 438 of file logical.c.

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

Referenced by DecodingContextFindStartpoint().

439 {
441 }
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 786 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().

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

◆ FreeDecodingContext()

void FreeDecodingContext ( LogicalDecodingContext ctx)

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

494 {
495  if (ctx->callbacks.shutdown_cb != NULL)
496  shutdown_cb_wrapper(ctx);
497 
500  XLogReaderFree(ctx->reader);
502 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
struct ReorderBuffer * reorder
Definition: logical.h:45
OutputPluginCallbacks callbacks
Definition: logical.h:55
void ReorderBufferFree(ReorderBuffer *rb)
MemoryContext context
Definition: logical.h:38
static void shutdown_cb_wrapper(LogicalDecodingContext *ctx)
Definition: logical.c:616
void XLogReaderFree(XLogReaderState *state)
Definition: xlogreader.c:129
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 547 of file logical.c.

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

Referenced by StartupDecodingContext().

548 {
549  LogicalOutputPluginInit plugin_init;
550 
551  plugin_init = (LogicalOutputPluginInit)
552  load_external_function(plugin, "_PG_output_plugin_init", false, NULL);
553 
554  if (plugin_init == NULL)
555  elog(ERROR, "output plugins have to declare the _PG_output_plugin_init symbol");
556 
557  /* ask the output plugin to fill the callback struct */
558  plugin_init(callbacks);
559 
560  if (callbacks->begin_cb == NULL)
561  elog(ERROR, "output plugins have to register a begin callback");
562  if (callbacks->change_cb == NULL)
563  elog(ERROR, "output plugins have to register a change callback");
564  if (callbacks->commit_cb == NULL)
565  elog(ERROR, "output plugins have to register a commit callback");
566 }
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:94
LogicalDecodeChangeCB change_cb
LogicalDecodeBeginCB begin_cb
#define elog
Definition: elog.h:219

◆ LogicalConfirmReceivedLocation()

void LogicalConfirmReceivedLocation ( XLogRecPtr  lsn)

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

987 {
988  Assert(lsn != InvalidXLogRecPtr);
989 
990  /* Do an unlocked check for candidate_lsn first. */
993  {
994  bool updated_xmin = false;
995  bool updated_restart = false;
996 
998 
1000 
1001  /* if we're past the location required for bumping xmin, do so */
1004  {
1005  /*
1006  * We have to write the changed xmin to disk *before* we change
1007  * the in-memory value, otherwise after a crash we wouldn't know
1008  * that some catalog tuples might have been removed already.
1009  *
1010  * Ensure that by first writing to ->xmin and only update
1011  * ->effective_xmin once the new state is synced to disk. After a
1012  * crash ->effective_xmin is set to ->xmin.
1013  */
1016  {
1020  updated_xmin = true;
1021  }
1022  }
1023 
1026  {
1028 
1032  updated_restart = true;
1033  }
1034 
1036 
1037  /* first write new xmin to disk, so we know what's up after a crash */
1038  if (updated_xmin || updated_restart)
1039  {
1042  elog(DEBUG1, "updated xmin: %u restart: %u", updated_xmin, updated_restart);
1043  }
1044 
1045  /*
1046  * Now the new xmin is safely on disk, we can let the global value
1047  * advance. We do not take ProcArrayLock or similar since we only
1048  * advance xmin here and there's not much harm done by a concurrent
1049  * computation missing that.
1050  */
1051  if (updated_xmin)
1052  {
1056 
1059  }
1060  }
1061  else
1062  {
1066  }
1067 }
TransactionId candidate_catalog_xmin
Definition: slot.h:148
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
void ReplicationSlotSave(void)
Definition: slot.c:638
ReplicationSlotPersistentData data
Definition: slot.h:133
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:150
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:130
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:699
XLogRecPtr restart_lsn
Definition: slot.h:73
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:149
#define elog
Definition: elog.h:219
slock_t mutex
Definition: slot.h:106
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:695
XLogRecPtr candidate_restart_lsn
Definition: slot.h:151
void ReplicationSlotMarkDirty(void)
Definition: slot.c:656

◆ LogicalIncreaseRestartDecodingForSlot()

void LogicalIncreaseRestartDecodingForSlot ( XLogRecPtr  current_lsn,
XLogRecPtr  restart_lsn 
)

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

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

◆ LogicalIncreaseXminForSlot()

void LogicalIncreaseXminForSlot ( XLogRecPtr  current_lsn,
TransactionId  xmin 
)

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

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

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

819 {
820  LogicalDecodingContext *ctx = cache->private_data;
822  ErrorContextCallback errcallback;
823 
824  Assert(!ctx->fast_forward);
825 
826  if (ctx->callbacks.message_cb == NULL)
827  return;
828 
829  /* Push callback + info on the error context stack */
830  state.ctx = ctx;
831  state.callback_name = "message";
832  state.report_location = message_lsn;
834  errcallback.arg = (void *) &state;
835  errcallback.previous = error_context_stack;
836  error_context_stack = &errcallback;
837 
838  /* set output state */
839  ctx->accept_writes = true;
840  ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
841  ctx->write_location = message_lsn;
842 
843  /* do the actual work: call callback */
844  ctx->callbacks.message_cb(ctx, txn, message_lsn, transactional, prefix,
845  message_size, message);
846 
847  /* Pop the error context stack */
848  error_context_stack = errcallback.previous;
849 }
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:699
Definition: regguts.h:298
XLogRecPtr report_location
Definition: logical.c:52
LogicalDecodingContext * ctx
Definition: logical.c:50
static void output_plugin_error_callback(void *arg)
Definition: logical.c:569

◆ output_plugin_error_callback()

static void output_plugin_error_callback ( void *  arg)
static

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

570 {
572 
573  /* not all callbacks have an associated LSN */
574  if (state->report_location != InvalidXLogRecPtr)
575  errcontext("slot \"%s\", output plugin \"%s\", in the %s callback, associated LSN %X/%X",
576  NameStr(state->ctx->slot->data.name),
577  NameStr(state->ctx->slot->data.plugin),
578  state->callback_name,
579  (uint32) (state->report_location >> 32),
580  (uint32) state->report_location);
581  else
582  errcontext("slot \"%s\", output plugin \"%s\", in the %s callback",
583  NameStr(state->ctx->slot->data.name),
584  NameStr(state->ctx->slot->data.plugin),
585  state->callback_name);
586 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
ReplicationSlotPersistentData data
Definition: slot.h:133
const char * callback_name
Definition: logical.c:51
unsigned int uint32
Definition: c.h:325
Definition: regguts.h:298
XLogRecPtr report_location
Definition: logical.c:52
ReplicationSlot * slot
Definition: logical.h:41
#define NameStr(name)
Definition: c.h:576
#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 508 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().

509 {
510  if (!ctx->accept_writes)
511  elog(ERROR, "writes are only accepted in commit, begin and change callbacks");
512 
513  ctx->prepare_write(ctx, ctx->write_location, ctx->write_xid, last_write);
514  ctx->prepared_write = true;
515 }
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 534 of file logical.c.

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

Referenced by pgoutput_commit_txn().

535 {
536  if (!ctx->update_progress)
537  return;
538 
539  ctx->update_progress(ctx, ctx->write_location, ctx->write_xid);
540 }
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 521 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().

522 {
523  if (!ctx->prepared_write)
524  elog(ERROR, "OutputPluginPrepareWrite needs to be called before OutputPluginWrite");
525 
526  ctx->write(ctx, ctx->write_location, ctx->write_xid, last_write);
527  ctx->prepared_write = false;
528 }
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 616 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().

617 {
619  ErrorContextCallback errcallback;
620 
621  Assert(!ctx->fast_forward);
622 
623  /* Push callback + info on the error context stack */
624  state.ctx = ctx;
625  state.callback_name = "shutdown";
628  errcallback.arg = (void *) &state;
629  errcallback.previous = error_context_stack;
630  error_context_stack = &errcallback;
631 
632  /* set output state */
633  ctx->accept_writes = false;
634 
635  /* do the actual work: call callback */
636  ctx->callbacks.shutdown_cb(ctx);
637 
638  /* Pop the error context stack */
639  error_context_stack = errcallback.previous;
640 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:55
void(* callback)(void *arg)
Definition: elog.h:239
struct ErrorContextCallback * previous
Definition: elog.h:238
const char * callback_name
Definition: logical.c:51
ErrorContextCallback * error_context_stack
Definition: elog.c:88
#define Assert(condition)
Definition: c.h:699
Definition: regguts.h:298
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:569

◆ startup_cb_wrapper()

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

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

590 {
592  ErrorContextCallback errcallback;
593 
594  Assert(!ctx->fast_forward);
595 
596  /* Push callback + info on the error context stack */
597  state.ctx = ctx;
598  state.callback_name = "startup";
601  errcallback.arg = (void *) &state;
602  errcallback.previous = error_context_stack;
603  error_context_stack = &errcallback;
604 
605  /* set output state */
606  ctx->accept_writes = false;
607 
608  /* do the actual work: call callback */
609  ctx->callbacks.startup_cb(ctx, opt, is_init);
610 
611  /* Pop the error context stack */
612  error_context_stack = errcallback.previous;
613 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:55
void(* callback)(void *arg)
Definition: elog.h:239
struct ErrorContextCallback * previous
Definition: elog.h:238
const char * callback_name
Definition: logical.c:51
ErrorContextCallback * error_context_stack
Definition: elog.c:88
#define Assert(condition)
Definition: c.h:699
Definition: regguts.h:298
LogicalDecodeStartupCB startup_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:569

◆ 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 116 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, XLogReaderState::private_data, 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().

125 {
126  ReplicationSlot *slot;
127  MemoryContext context,
128  old_context;
130 
131  /* shorter lines... */
132  slot = MyReplicationSlot;
133 
135  "Logical decoding context",
137  old_context = MemoryContextSwitchTo(context);
138  ctx = palloc0(sizeof(LogicalDecodingContext));
139 
140  ctx->context = context;
141 
142  /*
143  * (re-)load output plugins, so we detect a bad (removed) output plugin
144  * now.
145  */
146  if (!fast_forward)
148 
149  /*
150  * Now that the slot's xmin has been set, we can announce ourselves as a
151  * logical decoding backend which doesn't need to be checked individually
152  * when computing the xmin horizon because the xmin is enforced via
153  * replication slots.
154  *
155  * We can only do so if we're outside of a transaction (i.e. the case when
156  * streaming changes via walsender), otherwise an already setup
157  * snapshot/xid would end up being ignored. That's not a particularly
158  * bothersome restriction since the SQL interface can't be used for
159  * streaming anyway.
160  */
162  {
163  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
165  LWLockRelease(ProcArrayLock);
166  }
167 
168  ctx->slot = slot;
169 
170  ctx->reader = XLogReaderAllocate(wal_segment_size, read_page, ctx);
171  if (!ctx->reader)
172  ereport(ERROR,
173  (errcode(ERRCODE_OUT_OF_MEMORY),
174  errmsg("out of memory")));
175 
176  ctx->reader->private_data = ctx;
177 
179  ctx->snapshot_builder =
180  AllocateSnapshotBuilder(ctx->reorder, xmin_horizon, start_lsn,
181  need_full_snapshot);
182 
183  ctx->reorder->private_data = ctx;
184 
185  /* wrap output plugin callbacks, so we can add error context information */
191 
192  ctx->out = makeStringInfo();
193  ctx->prepare_write = prepare_write;
194  ctx->write = do_write;
195  ctx->update_progress = update_progress;
196 
197  ctx->output_plugin_options = output_plugin_options;
198 
199  ctx->fast_forward = fast_forward;
200 
201  MemoryContextSwitchTo(old_context);
202 
203  return ctx;
204 }
ReorderBufferApplyChangeCB apply_change
void * private_data
static void change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
Definition: logical.c:709
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:547
static void commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:678
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:4440
MemoryContext context
Definition: logical.h:38
List * output_plugin_options
Definition: logical.h:61
ReorderBufferCommitCB commit
ReplicationSlotPersistentData data
Definition: slot.h:133
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:1725
#define ERROR
Definition: elog.h:43
LogicalOutputPluginWriterPrepareWrite prepare_write
Definition: logical.h:66
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
LogicalOutputPluginWriterUpdateProgress update_progress
Definition: logical.h:68
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
ReorderBufferMessageCB message
#define ereport(elevel, rest)
Definition: elog.h:122
#define AllocSetContextCreate(parent, name, allocparams)
Definition: memutils.h:170
XLogReaderState * XLogReaderAllocate(int wal_segment_size, XLogPageReadCB pagereadfunc, void *private_data)
Definition: xlogreader.c:69
void * palloc0(Size size)
Definition: mcxt.c:955
static void begin_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
Definition: logical.c:648
struct SnapBuild * snapshot_builder
Definition: logical.h:46
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1121
ReorderBuffer * ReorderBufferAllocate(void)
static void truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
Definition: logical.c:746
int errmsg(const char *fmt,...)
Definition: elog.c:797
XLogReaderState * reader
Definition: logical.h:44
ReplicationSlot * slot
Definition: logical.h:41
ReorderBufferApplyTruncateCB apply_truncate
LogicalOutputPluginWriterWrite write
Definition: logical.h:67
StringInfo out
Definition: logical.h:73
#define NameStr(name)
Definition: c.h:576
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:816
ReorderBufferBeginCB begin
SnapBuild * AllocateSnapshotBuilder(ReorderBuffer *reorder, TransactionId xmin_horizon, XLogRecPtr start_lsn, bool need_full_snapshot)
Definition: snapbuild.c:316

◆ truncate_cb_wrapper()

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

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

748 {
749  LogicalDecodingContext *ctx = cache->private_data;
751  ErrorContextCallback errcallback;
752 
753  Assert(!ctx->fast_forward);
754 
755  if (!ctx->callbacks.truncate_cb)
756  return;
757 
758  /* Push callback + info on the error context stack */
759  state.ctx = ctx;
760  state.callback_name = "truncate";
761  state.report_location = change->lsn;
763  errcallback.arg = (void *) &state;
764  errcallback.previous = error_context_stack;
765  error_context_stack = &errcallback;
766 
767  /* set output state */
768  ctx->accept_writes = true;
769  ctx->write_xid = txn->xid;
770 
771  /*
772  * report this change's lsn so replies from clients can give an up2date
773  * answer. This won't ever be enough (and shouldn't be!) to confirm
774  * receipt of this transaction, but it might allow another transaction's
775  * commit to be confirmed with one message.
776  */
777  ctx->write_location = change->lsn;
778 
779  ctx->callbacks.truncate_cb(ctx, txn, nrelations, relations, change);
780 
781  /* Pop the error context stack */
782  error_context_stack = errcallback.previous;
783 }
LogicalDecodeTruncateCB truncate_cb
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:699
Definition: regguts.h:298
XLogRecPtr report_location
Definition: logical.c:52
LogicalDecodingContext * ctx
Definition: logical.c:50
static void output_plugin_error_callback(void *arg)
Definition: logical.c:569