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

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, OutputPluginCallbacks::begin_cb, ErrorContextCallback::callback, LogicalErrorCallbackState::callback_name, LogicalDecodingContext::callbacks, LogicalErrorCallbackState::ctx, error_context_stack, 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().

627 {
628  LogicalDecodingContext *ctx = cache->private_data;
630  ErrorContextCallback errcallback;
631 
632  /* Push callback + info on the error context stack */
633  state.ctx = ctx;
634  state.callback_name = "begin";
635  state.report_location = txn->first_lsn;
637  errcallback.arg = (void *) &state;
638  errcallback.previous = error_context_stack;
639  error_context_stack = &errcallback;
640 
641  /* set output state */
642  ctx->accept_writes = true;
643  ctx->write_xid = txn->xid;
644  ctx->write_location = txn->first_lsn;
645 
646  /* do the actual work: call callback */
647  ctx->callbacks.begin_cb(ctx, txn);
648 
649  /* Pop the error context stack */
650  error_context_stack = errcallback.previous;
651 }
XLogRecPtr first_lsn
TransactionId write_xid
Definition: logical.h:84
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:48
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:83
ErrorContextCallback * error_context_stack
Definition: elog.c:88
TransactionId xid
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:551

◆ change_cb_wrapper()

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

Definition at line 683 of file logical.c.

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, ErrorContextCallback::callback, LogicalErrorCallbackState::callback_name, LogicalDecodingContext::callbacks, OutputPluginCallbacks::change_cb, LogicalErrorCallbackState::ctx, error_context_stack, 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().

685 {
686  LogicalDecodingContext *ctx = cache->private_data;
688  ErrorContextCallback errcallback;
689 
690  /* Push callback + info on the error context stack */
691  state.ctx = ctx;
692  state.callback_name = "change";
693  state.report_location = change->lsn;
695  errcallback.arg = (void *) &state;
696  errcallback.previous = error_context_stack;
697  error_context_stack = &errcallback;
698 
699  /* set output state */
700  ctx->accept_writes = true;
701  ctx->write_xid = txn->xid;
702 
703  /*
704  * report this change's lsn so replies from clients can give an up2date
705  * answer. This won't ever be enough (and shouldn't be!) to confirm
706  * receipt of this transaction, but it might allow another transaction's
707  * commit to be confirmed with one message.
708  */
709  ctx->write_location = change->lsn;
710 
711  ctx->callbacks.change_cb(ctx, txn, relation, change);
712 
713  /* Pop the error context stack */
714  error_context_stack = errcallback.previous;
715 }
TransactionId write_xid
Definition: logical.h:84
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:48
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:83
ErrorContextCallback * error_context_stack
Definition: elog.c:88
LogicalDecodeChangeCB change_cb
Definition: output_plugin.h:99
TransactionId xid
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:551

◆ 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:7922
#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 654 of file logical.c.

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, ErrorContextCallback::callback, LogicalErrorCallbackState::callback_name, LogicalDecodingContext::callbacks, OutputPluginCallbacks::commit_cb, LogicalErrorCallbackState::ctx, ReorderBufferTXN::end_lsn, error_context_stack, 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().

656 {
657  LogicalDecodingContext *ctx = cache->private_data;
659  ErrorContextCallback errcallback;
660 
661  /* Push callback + info on the error context stack */
662  state.ctx = ctx;
663  state.callback_name = "commit";
664  state.report_location = txn->final_lsn; /* beginning of commit record */
666  errcallback.arg = (void *) &state;
667  errcallback.previous = error_context_stack;
668  error_context_stack = &errcallback;
669 
670  /* set output state */
671  ctx->accept_writes = true;
672  ctx->write_xid = txn->xid;
673  ctx->write_location = txn->end_lsn; /* points to the end of the record */
674 
675  /* do the actual work: call callback */
676  ctx->callbacks.commit_cb(ctx, txn, commit_lsn);
677 
678  /* Pop the error context stack */
679  error_context_stack = errcallback.previous;
680 }
TransactionId write_xid
Definition: logical.h:84
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:48
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:83
ErrorContextCallback * error_context_stack
Definition: elog.c:88
LogicalDecodeCommitCB commit_cb
XLogRecPtr final_lsn
TransactionId xid
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:551

◆ CreateDecodingContext()

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

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

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

223 {
224  TransactionId xmin_horizon = InvalidTransactionId;
225  ReplicationSlot *slot;
227  MemoryContext old_context;
228 
229  /* shorter lines... */
230  slot = MyReplicationSlot;
231 
232  /* first some sanity checks that are unlikely to be violated */
233  if (slot == NULL)
234  elog(ERROR, "cannot perform logical decoding without an acquired slot");
235 
236  if (plugin == NULL)
237  elog(ERROR, "cannot initialize logical decoding without a specified plugin");
238 
239  /* Make sure the passed slot is suitable. These are user facing errors. */
240  if (SlotIsPhysical(slot))
241  ereport(ERROR,
242  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
243  errmsg("cannot use physical replication slot for logical decoding")));
244 
245  if (slot->data.database != MyDatabaseId)
246  ereport(ERROR,
247  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
248  errmsg("replication slot \"%s\" was not created in this database",
249  NameStr(slot->data.name))));
250 
251  if (IsTransactionState() &&
253  ereport(ERROR,
254  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
255  errmsg("cannot create logical replication slot in transaction that has performed writes")));
256 
257  /* register output plugin name with slot */
258  SpinLockAcquire(&slot->mutex);
260  SpinLockRelease(&slot->mutex);
261 
263 
264  /* ----
265  * This is a bit tricky: We need to determine a safe xmin horizon to start
266  * decoding from, to avoid starting from a running xacts record referring
267  * to xids whose rows have been vacuumed or pruned
268  * already. GetOldestSafeDecodingTransactionId() returns such a value, but
269  * without further interlock its return value might immediately be out of
270  * date.
271  *
272  * So we have to acquire the ProcArrayLock to prevent computation of new
273  * xmin horizons by other backends, get the safe decoding xid, and inform
274  * the slot machinery about the new limit. Once that's done the
275  * ProcArrayLock can be released as the slot machinery now is
276  * protecting against vacuum.
277  *
278  * Note that, temporarily, the data, not just the catalog, xmin has to be
279  * reserved if a data snapshot is to be exported. Otherwise the initial
280  * data snapshot created here is not guaranteed to be valid. After that
281  * the data xmin doesn't need to be managed anymore and the global xmin
282  * should be recomputed. As we are fine with losing the pegged data xmin
283  * after crash - no chance a snapshot would get exported anymore - we can
284  * get away with just setting the slot's
285  * effective_xmin. ReplicationSlotRelease will reset it again.
286  *
287  * ----
288  */
289  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
290 
291  xmin_horizon = GetOldestSafeDecodingTransactionId(!need_full_snapshot);
292 
293  slot->effective_catalog_xmin = xmin_horizon;
294  slot->data.catalog_xmin = xmin_horizon;
295  if (need_full_snapshot)
296  slot->effective_xmin = xmin_horizon;
297 
299 
300  LWLockRelease(ProcArrayLock);
301 
304 
305  ctx = StartupDecodingContext(NIL, InvalidXLogRecPtr, xmin_horizon,
306  need_full_snapshot, read_page, prepare_write,
307  do_write, update_progress);
308 
309  /* call output plugin initialization callback */
310  old_context = MemoryContextSwitchTo(ctx->context);
311  if (ctx->callbacks.startup_cb != NULL)
312  startup_cb_wrapper(ctx, &ctx->options, true);
313  MemoryContextSwitchTo(old_context);
314 
315  return ctx;
316 }
#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:455
OutputPluginOptions options
Definition: logical.h:49
OutputPluginCallbacks callbacks
Definition: logical.h:48
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
static LogicalDecodingContext * StartupDecodingContext(List *output_plugin_options, XLogRecPtr start_lsn, TransactionId xmin_horizon, bool need_full_snapshot, XLogPageReadCB read_page, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
Definition: logical.c:114
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1722
#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:571
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:836
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1118
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:557
#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 432 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().

433 {
434  XLogRecPtr startptr;
435 
436  /* Initialize from where to start reading WAL. */
437  startptr = ctx->slot->data.restart_lsn;
438 
439  elog(DEBUG1, "searching for logical decoding starting point, starting at %X/%X",
440  (uint32) (ctx->slot->data.restart_lsn >> 32),
441  (uint32) ctx->slot->data.restart_lsn);
442 
443  /* Wait for a consistent starting point */
444  for (;;)
445  {
446  XLogRecord *record;
447  char *err = NULL;
448 
449  /* the read_page callback waits for new WAL */
450  record = XLogReadRecord(ctx->reader, startptr, &err);
451  if (err)
452  elog(ERROR, "%s", err);
453  if (!record)
454  elog(ERROR, "no record found"); /* shouldn't happen */
455 
456  startptr = InvalidXLogRecPtr;
457 
459 
460  /* only continue till we found a consistent spot */
461  if (DecodingContextReady(ctx))
462  break;
463 
465  }
466 
467  ctx->slot->data.confirmed_flush = ctx->reader->EndRecPtr;
468 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
bool DecodingContextReady(LogicalDecodingContext *ctx)
Definition: logical.c:423
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:93
unsigned int uint32
Definition: c.h:306
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 423 of file logical.c.

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

Referenced by DecodingContextFindStartpoint().

424 {
426 }
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 718 of file logical.c.

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

Referenced by FilterByOrigin().

719 {
721  ErrorContextCallback errcallback;
722  bool ret;
723 
724  /* Push callback + info on the error context stack */
725  state.ctx = ctx;
726  state.callback_name = "filter_by_origin";
729  errcallback.arg = (void *) &state;
730  errcallback.previous = error_context_stack;
731  error_context_stack = &errcallback;
732 
733  /* set output state */
734  ctx->accept_writes = false;
735 
736  /* do the actual work: call callback */
737  ret = ctx->callbacks.filter_by_origin_cb(ctx, origin_id);
738 
739  /* Pop the error context stack */
740  error_context_stack = errcallback.previous;
741 
742  return ret;
743 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:48
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
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:551
LogicalDecodeFilterByOriginCB filter_by_origin_cb

◆ FreeDecodingContext()

void FreeDecodingContext ( LogicalDecodingContext ctx)

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

476 {
477  if (ctx->callbacks.shutdown_cb != NULL)
478  shutdown_cb_wrapper(ctx);
479 
482  XLogReaderFree(ctx->reader);
484 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:198
struct ReorderBuffer * reorder
Definition: logical.h:45
OutputPluginCallbacks callbacks
Definition: logical.h:48
void ReorderBufferFree(ReorderBuffer *rb)
MemoryContext context
Definition: logical.h:38
static void shutdown_cb_wrapper(LogicalDecodingContext *ctx)
Definition: logical.c:596
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 529 of file logical.c.

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

Referenced by StartupDecodingContext().

530 {
531  LogicalOutputPluginInit plugin_init;
532 
533  plugin_init = (LogicalOutputPluginInit)
534  load_external_function(plugin, "_PG_output_plugin_init", false, NULL);
535 
536  if (plugin_init == NULL)
537  elog(ERROR, "output plugins have to declare the _PG_output_plugin_init symbol");
538 
539  /* ask the output plugin to fill the callback struct */
540  plugin_init(callbacks);
541 
542  if (callbacks->begin_cb == NULL)
543  elog(ERROR, "output plugins have to register a begin callback");
544  if (callbacks->change_cb == NULL)
545  elog(ERROR, "output plugins have to register a change callback");
546  if (callbacks->commit_cb == NULL)
547  elog(ERROR, "output plugins have to register a commit callback");
548 }
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 914 of file logical.c.

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

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

915 {
916  Assert(lsn != InvalidXLogRecPtr);
917 
918  /* Do an unlocked check for candidate_lsn first. */
921  {
922  bool updated_xmin = false;
923  bool updated_restart = false;
924 
926 
928 
929  /* if we're past the location required for bumping xmin, do so */
932  {
933  /*
934  * We have to write the changed xmin to disk *before* we change
935  * the in-memory value, otherwise after a crash we wouldn't know
936  * that some catalog tuples might have been removed already.
937  *
938  * Ensure that by first writing to ->xmin and only update
939  * ->effective_xmin once the new state is synced to disk. After a
940  * crash ->effective_xmin is set to ->xmin.
941  */
944  {
948  updated_xmin = true;
949  }
950  }
951 
954  {
956 
960  updated_restart = true;
961  }
962 
964 
965  /* first write new xmin to disk, so we know what's up after a crash */
966  if (updated_xmin || updated_restart)
967  {
970  elog(DEBUG1, "updated xmin: %u restart: %u", updated_xmin, updated_restart);
971  }
972 
973  /*
974  * Now the new xmin is safely on disk, we can let the global value
975  * advance. We do not take ProcArrayLock or similar since we only
976  * advance xmin here and there's not much harm done by a concurrent
977  * computation missing that.
978  */
979  if (updated_xmin)
980  {
984 
987  }
988  }
989  else
990  {
994  }
995 }
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:680
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 845 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().

846 {
847  bool updated_lsn = false;
848  ReplicationSlot *slot;
849 
850  slot = MyReplicationSlot;
851 
852  Assert(slot != NULL);
853  Assert(restart_lsn != InvalidXLogRecPtr);
854  Assert(current_lsn != InvalidXLogRecPtr);
855 
856  SpinLockAcquire(&slot->mutex);
857 
858  /* don't overwrite if have a newer restart lsn */
859  if (restart_lsn <= slot->data.restart_lsn)
860  {
861  }
862 
863  /*
864  * We might have already flushed far enough to directly accept this lsn,
865  * in this case there is no need to check for existing candidate LSNs
866  */
867  else if (current_lsn <= slot->data.confirmed_flush)
868  {
869  slot->candidate_restart_valid = current_lsn;
870  slot->candidate_restart_lsn = restart_lsn;
871 
872  /* our candidate can directly be used */
873  updated_lsn = true;
874  }
875 
876  /*
877  * Only increase if the previous values have been applied, otherwise we
878  * might never end up updating if the receiver acks too slowly. A missed
879  * value here will just cause some extra effort after reconnecting.
880  */
882  {
883  slot->candidate_restart_valid = current_lsn;
884  slot->candidate_restart_lsn = restart_lsn;
885 
886  elog(DEBUG1, "got new restart lsn %X/%X at %X/%X",
887  (uint32) (restart_lsn >> 32), (uint32) restart_lsn,
888  (uint32) (current_lsn >> 32), (uint32) current_lsn);
889  }
890  else
891  {
892  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",
893  (uint32) (restart_lsn >> 32), (uint32) restart_lsn,
894  (uint32) (current_lsn >> 32), (uint32) current_lsn,
895  (uint32) (slot->candidate_restart_lsn >> 32),
897  (uint32) (slot->candidate_restart_valid >> 32),
899  (uint32) (slot->data.confirmed_flush >> 32),
900  (uint32) slot->data.confirmed_flush
901  );
902  }
903  SpinLockRelease(&slot->mutex);
904 
905  /* candidates are already valid with the current flush position, apply */
906  if (updated_lsn)
908 }
#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:306
#define SpinLockRelease(lock)
Definition: spin.h:64
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:680
void LogicalConfirmReceivedLocation(XLogRecPtr lsn)
Definition: logical.c:914
#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 788 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().

789 {
790  bool updated_xmin = false;
791  ReplicationSlot *slot;
792 
793  slot = MyReplicationSlot;
794 
795  Assert(slot != NULL);
796 
797  SpinLockAcquire(&slot->mutex);
798 
799  /*
800  * don't overwrite if we already have a newer xmin. This can happen if we
801  * restart decoding in a slot.
802  */
804  {
805  }
806 
807  /*
808  * If the client has already confirmed up to this lsn, we directly can
809  * mark this as accepted. This can happen if we restart decoding in a
810  * slot.
811  */
812  else if (current_lsn <= slot->data.confirmed_flush)
813  {
814  slot->candidate_catalog_xmin = xmin;
815  slot->candidate_xmin_lsn = current_lsn;
816 
817  /* our candidate can directly be used */
818  updated_xmin = true;
819  }
820 
821  /*
822  * Only increase if the previous values have been applied, otherwise we
823  * might never end up updating if the receiver acks too slowly.
824  */
825  else if (slot->candidate_xmin_lsn == InvalidXLogRecPtr)
826  {
827  slot->candidate_catalog_xmin = xmin;
828  slot->candidate_xmin_lsn = current_lsn;
829  }
830  SpinLockRelease(&slot->mutex);
831 
832  /* candidate already valid with the current flush position, apply */
833  if (updated_xmin)
835 }
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:680
void LogicalConfirmReceivedLocation(XLogRecPtr lsn)
Definition: logical.c:914
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 746 of file logical.c.

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, ErrorContextCallback::callback, LogicalErrorCallbackState::callback_name, LogicalDecodingContext::callbacks, LogicalErrorCallbackState::ctx, error_context_stack, 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().

749 {
750  LogicalDecodingContext *ctx = cache->private_data;
752  ErrorContextCallback errcallback;
753 
754  if (ctx->callbacks.message_cb == NULL)
755  return;
756 
757  /* Push callback + info on the error context stack */
758  state.ctx = ctx;
759  state.callback_name = "message";
760  state.report_location = message_lsn;
762  errcallback.arg = (void *) &state;
763  errcallback.previous = error_context_stack;
764  error_context_stack = &errcallback;
765 
766  /* set output state */
767  ctx->accept_writes = true;
768  ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
769  ctx->write_location = message_lsn;
770 
771  /* do the actual work: call callback */
772  ctx->callbacks.message_cb(ctx, txn, message_lsn, transactional, prefix,
773  message_size, message);
774 
775  /* Pop the error context stack */
776  error_context_stack = errcallback.previous;
777 }
TransactionId write_xid
Definition: logical.h:84
void * private_data
LogicalDecodeMessageCB message_cb
OutputPluginCallbacks callbacks
Definition: logical.h:48
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:83
ErrorContextCallback * error_context_stack
Definition: elog.c:88
#define InvalidTransactionId
Definition: transam.h:31
TransactionId xid
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:551

◆ output_plugin_error_callback()

static void output_plugin_error_callback ( void *  arg)
static

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

552 {
554 
555  /* not all callbacks have an associated LSN */
556  if (state->report_location != InvalidXLogRecPtr)
557  errcontext("slot \"%s\", output plugin \"%s\", in the %s callback, associated LSN %X/%X",
558  NameStr(state->ctx->slot->data.name),
559  NameStr(state->ctx->slot->data.plugin),
560  state->callback_name,
561  (uint32) (state->report_location >> 32),
562  (uint32) state->report_location);
563  else
564  errcontext("slot \"%s\", output plugin \"%s\", in the %s callback",
565  NameStr(state->ctx->slot->data.name),
566  NameStr(state->ctx->slot->data.plugin),
567  state->callback_name);
568 }
#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:306
Definition: regguts.h:298
XLogRecPtr report_location
Definition: logical.c:52
ReplicationSlot * slot
Definition: logical.h:41
#define NameStr(name)
Definition: c.h:557
#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 490 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().

491 {
492  if (!ctx->accept_writes)
493  elog(ERROR, "writes are only accepted in commit, begin and change callbacks");
494 
495  ctx->prepare_write(ctx, ctx->write_location, ctx->write_xid, last_write);
496  ctx->prepared_write = true;
497 }
TransactionId write_xid
Definition: logical.h:84
XLogRecPtr write_location
Definition: logical.h:83
#define ERROR
Definition: elog.h:43
LogicalOutputPluginWriterPrepareWrite prepare_write
Definition: logical.h:59
#define elog
Definition: elog.h:219

◆ OutputPluginUpdateProgress()

void OutputPluginUpdateProgress ( struct LogicalDecodingContext ctx)

Definition at line 516 of file logical.c.

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

Referenced by pgoutput_commit_txn().

517 {
518  if (!ctx->update_progress)
519  return;
520 
521  ctx->update_progress(ctx, ctx->write_location, ctx->write_xid);
522 }
TransactionId write_xid
Definition: logical.h:84
XLogRecPtr write_location
Definition: logical.h:83
LogicalOutputPluginWriterUpdateProgress update_progress
Definition: logical.h:61

◆ OutputPluginWrite()

void OutputPluginWrite ( struct LogicalDecodingContext ctx,
bool  last_write 
)

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

504 {
505  if (!ctx->prepared_write)
506  elog(ERROR, "OutputPluginPrepareWrite needs to be called before OutputPluginWrite");
507 
508  ctx->write(ctx, ctx->write_location, ctx->write_xid, last_write);
509  ctx->prepared_write = false;
510 }
TransactionId write_xid
Definition: logical.h:84
XLogRecPtr write_location
Definition: logical.h:83
#define ERROR
Definition: elog.h:43
LogicalOutputPluginWriterWrite write
Definition: logical.h:60
#define elog
Definition: elog.h:219

◆ shutdown_cb_wrapper()

static void shutdown_cb_wrapper ( LogicalDecodingContext ctx)
static

Definition at line 596 of file logical.c.

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

Referenced by FreeDecodingContext().

597 {
599  ErrorContextCallback errcallback;
600 
601  /* Push callback + info on the error context stack */
602  state.ctx = ctx;
603  state.callback_name = "shutdown";
606  errcallback.arg = (void *) &state;
607  errcallback.previous = error_context_stack;
608  error_context_stack = &errcallback;
609 
610  /* set output state */
611  ctx->accept_writes = false;
612 
613  /* do the actual work: call callback */
614  ctx->callbacks.shutdown_cb(ctx);
615 
616  /* Pop the error context stack */
617  error_context_stack = errcallback.previous;
618 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:48
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
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:551

◆ startup_cb_wrapper()

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

Definition at line 571 of file logical.c.

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

Referenced by CreateDecodingContext(), and CreateInitDecodingContext().

572 {
574  ErrorContextCallback errcallback;
575 
576  /* Push callback + info on the error context stack */
577  state.ctx = ctx;
578  state.callback_name = "startup";
581  errcallback.arg = (void *) &state;
582  errcallback.previous = error_context_stack;
583  error_context_stack = &errcallback;
584 
585  /* set output state */
586  ctx->accept_writes = false;
587 
588  /* do the actual work: call callback */
589  ctx->callbacks.startup_cb(ctx, opt, is_init);
590 
591  /* Pop the error context stack */
592  error_context_stack = errcallback.previous;
593 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:48
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
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:551

◆ StartupDecodingContext()

static LogicalDecodingContext* StartupDecodingContext ( List output_plugin_options,
XLogRecPtr  start_lsn,
TransactionId  xmin_horizon,
bool  need_full_snapshot,
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, 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().

122 {
123  ReplicationSlot *slot;
124  MemoryContext context,
125  old_context;
127 
128  /* shorter lines... */
129  slot = MyReplicationSlot;
130 
132  "Logical decoding context",
134  old_context = MemoryContextSwitchTo(context);
135  ctx = palloc0(sizeof(LogicalDecodingContext));
136 
137  ctx->context = context;
138 
139  /*
140  * (re-)load output plugins, so we detect a bad (removed) output plugin
141  * now.
142  */
144 
145  /*
146  * Now that the slot's xmin has been set, we can announce ourselves as a
147  * logical decoding backend which doesn't need to be checked individually
148  * when computing the xmin horizon because the xmin is enforced via
149  * replication slots.
150  *
151  * We can only do so if we're outside of a transaction (i.e. the case when
152  * streaming changes via walsender), otherwise an already setup
153  * snapshot/xid would end up being ignored. That's not a particularly
154  * bothersome restriction since the SQL interface can't be used for
155  * streaming anyway.
156  */
158  {
159  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
161  LWLockRelease(ProcArrayLock);
162  }
163 
164  ctx->slot = slot;
165 
166  ctx->reader = XLogReaderAllocate(wal_segment_size, read_page, ctx);
167  if (!ctx->reader)
168  ereport(ERROR,
169  (errcode(ERRCODE_OUT_OF_MEMORY),
170  errmsg("out of memory")));
171 
172  ctx->reader->private_data = ctx;
173 
175  ctx->snapshot_builder =
176  AllocateSnapshotBuilder(ctx->reorder, xmin_horizon, start_lsn,
177  need_full_snapshot);
178 
179  ctx->reorder->private_data = ctx;
180 
181  /* wrap output plugin callbacks, so we can add error context information */
186 
187  ctx->out = makeStringInfo();
188  ctx->prepare_write = prepare_write;
189  ctx->write = do_write;
190  ctx->update_progress = update_progress;
191 
192  ctx->output_plugin_options = output_plugin_options;
193 
194  MemoryContextSwitchTo(old_context);
195 
196  return ctx;
197 }
ReorderBufferApplyChangeCB apply_change
void * private_data
static void change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
Definition: logical.c:683
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:529
static void commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:654
StringInfo makeStringInfo(void)
Definition: stringinfo.c:28
OutputPluginCallbacks callbacks
Definition: logical.h:48
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:54
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:1722
#define ERROR
Definition: elog.h:43
LogicalOutputPluginWriterPrepareWrite prepare_write
Definition: logical.h:59
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:197
LogicalOutputPluginWriterUpdateProgress update_progress
Definition: logical.h:61
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:626
struct SnapBuild * snapshot_builder
Definition: logical.h:46
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1118
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:60
StringInfo out
Definition: logical.h:66
#define NameStr(name)
Definition: c.h:557
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:746
ReorderBufferBeginCB begin
SnapBuild * AllocateSnapshotBuilder(ReorderBuffer *reorder, TransactionId xmin_horizon, XLogRecPtr start_lsn, bool need_full_snapshot)
Definition: snapbuild.c:316