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

Go to the source code of this file.

Data Structures

struct  LogicalErrorCallbackState
 

Typedefs

typedef struct LogicalErrorCallbackState LogicalErrorCallbackState
 

Functions

static void output_plugin_error_callback (void *arg)
 
static void startup_cb_wrapper (LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
 
static void shutdown_cb_wrapper (LogicalDecodingContext *ctx)
 
static void begin_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn)
 
static void commit_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
 
static void change_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
 
static void truncate_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
 
static void message_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr message_lsn, bool transactional, const char *prefix, Size message_size, const char *message)
 
static void LoadOutputPlugin (OutputPluginCallbacks *callbacks, char *plugin)
 
void CheckLogicalDecodingRequirements (void)
 
static LogicalDecodingContextStartupDecodingContext (List *output_plugin_options, XLogRecPtr start_lsn, TransactionId xmin_horizon, bool need_full_snapshot, bool fast_forward, XLogPageReadCB read_page, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
 
LogicalDecodingContextCreateInitDecodingContext (char *plugin, List *output_plugin_options, bool need_full_snapshot, XLogRecPtr restart_lsn, XLogPageReadCB read_page, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
 
LogicalDecodingContextCreateDecodingContext (XLogRecPtr start_lsn, List *output_plugin_options, bool fast_forward, XLogPageReadCB read_page, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
 
bool DecodingContextReady (LogicalDecodingContext *ctx)
 
void DecodingContextFindStartpoint (LogicalDecodingContext *ctx)
 
void FreeDecodingContext (LogicalDecodingContext *ctx)
 
void OutputPluginPrepareWrite (struct LogicalDecodingContext *ctx, bool last_write)
 
void OutputPluginWrite (struct LogicalDecodingContext *ctx, bool last_write)
 
void OutputPluginUpdateProgress (struct LogicalDecodingContext *ctx)
 
bool filter_by_origin_cb_wrapper (LogicalDecodingContext *ctx, RepOriginId origin_id)
 
void LogicalIncreaseXminForSlot (XLogRecPtr current_lsn, TransactionId xmin)
 
void LogicalIncreaseRestartDecodingForSlot (XLogRecPtr current_lsn, XLogRecPtr restart_lsn)
 
void LogicalConfirmReceivedLocation (XLogRecPtr lsn)
 

Typedef Documentation

◆ LogicalErrorCallbackState

Function Documentation

◆ begin_cb_wrapper()

static void begin_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn 
)
static

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

661 {
662  LogicalDecodingContext *ctx = cache->private_data;
664  ErrorContextCallback errcallback;
665 
666  Assert(!ctx->fast_forward);
667 
668  /* Push callback + info on the error context stack */
669  state.ctx = ctx;
670  state.callback_name = "begin";
671  state.report_location = txn->first_lsn;
673  errcallback.arg = (void *) &state;
674  errcallback.previous = error_context_stack;
675  error_context_stack = &errcallback;
676 
677  /* set output state */
678  ctx->accept_writes = true;
679  ctx->write_xid = txn->xid;
680  ctx->write_location = txn->first_lsn;
681 
682  /* do the actual work: call callback */
683  ctx->callbacks.begin_cb(ctx, txn);
684 
685  /* Pop the error context stack */
686  error_context_stack = errcallback.previous;
687 }
XLogRecPtr first_lsn
TransactionId write_xid
Definition: logical.h:88
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:52
void(* callback)(void *arg)
Definition: elog.h:256
struct ErrorContextCallback * previous
Definition: elog.h:255
const char * callback_name
Definition: logical.c:48
XLogRecPtr write_location
Definition: logical.h:87
ErrorContextCallback * error_context_stack
Definition: elog.c:91
TransactionId xid
#define Assert(condition)
Definition: c.h:739
Definition: regguts.h:298
XLogRecPtr report_location
Definition: logical.c:49
LogicalDecodeBeginCB begin_cb
LogicalDecodingContext * ctx
Definition: logical.c:47
static void output_plugin_error_callback(void *arg)
Definition: logical.c:581

◆ change_cb_wrapper()

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

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

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

◆ CheckLogicalDecodingRequirements()

void CheckLogicalDecodingRequirements ( void  )

Definition at line 75 of file logical.c.

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

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

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

◆ commit_cb_wrapper()

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

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

692 {
693  LogicalDecodingContext *ctx = cache->private_data;
695  ErrorContextCallback errcallback;
696 
697  Assert(!ctx->fast_forward);
698 
699  /* Push callback + info on the error context stack */
700  state.ctx = ctx;
701  state.callback_name = "commit";
702  state.report_location = txn->final_lsn; /* beginning of commit record */
704  errcallback.arg = (void *) &state;
705  errcallback.previous = error_context_stack;
706  error_context_stack = &errcallback;
707 
708  /* set output state */
709  ctx->accept_writes = true;
710  ctx->write_xid = txn->xid;
711  ctx->write_location = txn->end_lsn; /* points to the end of the record */
712 
713  /* do the actual work: call callback */
714  ctx->callbacks.commit_cb(ctx, txn, commit_lsn);
715 
716  /* Pop the error context stack */
717  error_context_stack = errcallback.previous;
718 }
TransactionId write_xid
Definition: logical.h:88
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:52
void(* callback)(void *arg)
Definition: elog.h:256
struct ErrorContextCallback * previous
Definition: elog.h:255
const char * callback_name
Definition: logical.c:48
XLogRecPtr write_location
Definition: logical.h:87
ErrorContextCallback * error_context_stack
Definition: elog.c:91
LogicalDecodeCommitCB commit_cb
XLogRecPtr final_lsn
TransactionId xid
#define Assert(condition)
Definition: c.h:739
Definition: regguts.h:298
XLogRecPtr end_lsn
XLogRecPtr report_location
Definition: logical.c:49
LogicalDecodingContext * ctx
Definition: logical.c:47
static void output_plugin_error_callback(void *arg)
Definition: logical.c:581

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

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

◆ CreateInitDecodingContext()

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

Definition at line 227 of file logical.c.

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

Referenced by create_logical_replication_slot(), and CreateReplicationSlot().

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

◆ DecodingContextFindStartpoint()

void DecodingContextFindStartpoint ( LogicalDecodingContext ctx)

Definition at line 462 of file logical.c.

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

Referenced by create_logical_replication_slot(), and CreateReplicationSlot().

463 {
464  ReplicationSlot *slot = ctx->slot;
465 
466  /* Initialize from where to start reading WAL. */
467  XLogBeginRead(ctx->reader, slot->data.restart_lsn);
468 
469  elog(DEBUG1, "searching for logical decoding starting point, starting at %X/%X",
470  (uint32) (slot->data.restart_lsn >> 32),
471  (uint32) slot->data.restart_lsn);
472 
473  /* Wait for a consistent starting point */
474  for (;;)
475  {
476  XLogRecord *record;
477  char *err = NULL;
478 
479  /* the read_page callback waits for new WAL */
480  record = XLogReadRecord(ctx->reader, &err);
481  if (err)
482  elog(ERROR, "%s", err);
483  if (!record)
484  elog(ERROR, "no record found"); /* shouldn't happen */
485 
487 
488  /* only continue till we found a consistent spot */
489  if (DecodingContextReady(ctx))
490  break;
491 
493  }
494 
495  SpinLockAcquire(&slot->mutex);
496  slot->data.confirmed_flush = ctx->reader->EndRecPtr;
497  SpinLockRelease(&slot->mutex);
498 }
#define DEBUG1
Definition: elog.h:25
bool DecodingContextReady(LogicalDecodingContext *ctx)
Definition: logical.c:453
ReplicationSlotPersistentData data
Definition: slot.h:132
XLogRecPtr confirmed_flush
Definition: slot.h:80
XLogRecPtr EndRecPtr
Definition: xlogreader.h:135
#define SpinLockAcquire(lock)
Definition: spin.h:62
XLogRecord * XLogReadRecord(XLogReaderState *state, char **errormsg)
Definition: xlogreader.c:261
#define ERROR
Definition: elog.h:43
void LogicalDecodingProcessRecord(LogicalDecodingContext *ctx, XLogReaderState *record)
Definition: decode.c:94
void XLogBeginRead(XLogReaderState *state, XLogRecPtr RecPtr)
Definition: xlogreader.c:233
unsigned int uint32
Definition: c.h:359
#define SpinLockRelease(lock)
Definition: spin.h:64
XLogRecPtr restart_lsn
Definition: slot.h:72
XLogReaderState * reader
Definition: logical.h:41
ReplicationSlot * slot
Definition: logical.h:38
#define elog(elevel,...)
Definition: elog.h:228
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
slock_t mutex
Definition: slot.h:105

◆ DecodingContextReady()

bool DecodingContextReady ( LogicalDecodingContext ctx)

Definition at line 453 of file logical.c.

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

Referenced by DecodingContextFindStartpoint().

454 {
456 }
SnapBuildState SnapBuildCurrentState(SnapBuild *builder)
Definition: snapbuild.c:395
struct SnapBuild * snapshot_builder
Definition: logical.h:43

◆ filter_by_origin_cb_wrapper()

bool filter_by_origin_cb_wrapper ( LogicalDecodingContext ctx,
RepOriginId  origin_id 
)

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

799 {
801  ErrorContextCallback errcallback;
802  bool ret;
803 
804  Assert(!ctx->fast_forward);
805 
806  /* Push callback + info on the error context stack */
807  state.ctx = ctx;
808  state.callback_name = "filter_by_origin";
811  errcallback.arg = (void *) &state;
812  errcallback.previous = error_context_stack;
813  error_context_stack = &errcallback;
814 
815  /* set output state */
816  ctx->accept_writes = false;
817 
818  /* do the actual work: call callback */
819  ret = ctx->callbacks.filter_by_origin_cb(ctx, origin_id);
820 
821  /* Pop the error context stack */
822  error_context_stack = errcallback.previous;
823 
824  return ret;
825 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:52
void(* callback)(void *arg)
Definition: elog.h:256
struct ErrorContextCallback * previous
Definition: elog.h:255
const char * callback_name
Definition: logical.c:48
ErrorContextCallback * error_context_stack
Definition: elog.c:91
#define Assert(condition)
Definition: c.h:739
Definition: regguts.h:298
XLogRecPtr report_location
Definition: logical.c:49
LogicalDecodingContext * ctx
Definition: logical.c:47
static void output_plugin_error_callback(void *arg)
Definition: logical.c:581
LogicalDecodeFilterByOriginCB filter_by_origin_cb

◆ FreeDecodingContext()

void FreeDecodingContext ( LogicalDecodingContext ctx)

Definition at line 505 of file logical.c.

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

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

506 {
507  if (ctx->callbacks.shutdown_cb != NULL)
508  shutdown_cb_wrapper(ctx);
509 
512  XLogReaderFree(ctx->reader);
514 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
struct ReorderBuffer * reorder
Definition: logical.h:42
OutputPluginCallbacks callbacks
Definition: logical.h:52
void ReorderBufferFree(ReorderBuffer *rb)
MemoryContext context
Definition: logical.h:35
static void shutdown_cb_wrapper(LogicalDecodingContext *ctx)
Definition: logical.c:628
void XLogReaderFree(XLogReaderState *state)
Definition: xlogreader.c:135
void FreeSnapshotBuilder(SnapBuild *builder)
Definition: snapbuild.c:351
struct SnapBuild * snapshot_builder
Definition: logical.h:43
LogicalDecodeShutdownCB shutdown_cb
XLogReaderState * reader
Definition: logical.h:41

◆ LoadOutputPlugin()

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

Definition at line 559 of file logical.c.

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

Referenced by StartupDecodingContext().

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

◆ LogicalConfirmReceivedLocation()

void LogicalConfirmReceivedLocation ( XLogRecPtr  lsn)

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

999 {
1000  Assert(lsn != InvalidXLogRecPtr);
1001 
1002  /* Do an unlocked check for candidate_lsn first. */
1005  {
1006  bool updated_xmin = false;
1007  bool updated_restart = false;
1008 
1010 
1012 
1013  /* if we're past the location required for bumping xmin, do so */
1016  {
1017  /*
1018  * We have to write the changed xmin to disk *before* we change
1019  * the in-memory value, otherwise after a crash we wouldn't know
1020  * that some catalog tuples might have been removed already.
1021  *
1022  * Ensure that by first writing to ->xmin and only update
1023  * ->effective_xmin once the new state is synced to disk. After a
1024  * crash ->effective_xmin is set to ->xmin.
1025  */
1028  {
1032  updated_xmin = true;
1033  }
1034  }
1035 
1038  {
1040 
1044  updated_restart = true;
1045  }
1046 
1048 
1049  /* first write new xmin to disk, so we know what's up after a crash */
1050  if (updated_xmin || updated_restart)
1051  {
1054  elog(DEBUG1, "updated xmin: %u restart: %u", updated_xmin, updated_restart);
1055  }
1056 
1057  /*
1058  * Now the new xmin is safely on disk, we can let the global value
1059  * advance. We do not take ProcArrayLock or similar since we only
1060  * advance xmin here and there's not much harm done by a concurrent
1061  * computation missing that.
1062  */
1063  if (updated_xmin)
1064  {
1068 
1071  }
1072  }
1073  else
1074  {
1078  }
1079 }
TransactionId candidate_catalog_xmin
Definition: slot.h:147
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
void ReplicationSlotSave(void)
Definition: slot.c:645
ReplicationSlotPersistentData data
Definition: slot.h:132
XLogRecPtr confirmed_flush
Definition: slot.h:80
#define SpinLockAcquire(lock)
Definition: spin.h:62
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:748
XLogRecPtr candidate_restart_valid
Definition: slot.h:149
TransactionId catalog_xmin
Definition: slot.h:69
#define InvalidTransactionId
Definition: transam.h:31
#define SpinLockRelease(lock)
Definition: spin.h:64
TransactionId effective_catalog_xmin
Definition: slot.h:129
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:739
XLogRecPtr restart_lsn
Definition: slot.h:72
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:148
#define elog(elevel,...)
Definition: elog.h:228
slock_t mutex
Definition: slot.h:105
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:702
XLogRecPtr candidate_restart_lsn
Definition: slot.h:150
void ReplicationSlotMarkDirty(void)
Definition: slot.c:663

◆ LogicalIncreaseRestartDecodingForSlot()

void LogicalIncreaseRestartDecodingForSlot ( XLogRecPtr  current_lsn,
XLogRecPtr  restart_lsn 
)

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

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

◆ LogicalIncreaseXminForSlot()

void LogicalIncreaseXminForSlot ( XLogRecPtr  current_lsn,
TransactionId  xmin 
)

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

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

◆ message_cb_wrapper()

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

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

831 {
832  LogicalDecodingContext *ctx = cache->private_data;
834  ErrorContextCallback errcallback;
835 
836  Assert(!ctx->fast_forward);
837 
838  if (ctx->callbacks.message_cb == NULL)
839  return;
840 
841  /* Push callback + info on the error context stack */
842  state.ctx = ctx;
843  state.callback_name = "message";
844  state.report_location = message_lsn;
846  errcallback.arg = (void *) &state;
847  errcallback.previous = error_context_stack;
848  error_context_stack = &errcallback;
849 
850  /* set output state */
851  ctx->accept_writes = true;
852  ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
853  ctx->write_location = message_lsn;
854 
855  /* do the actual work: call callback */
856  ctx->callbacks.message_cb(ctx, txn, message_lsn, transactional, prefix,
857  message_size, message);
858 
859  /* Pop the error context stack */
860  error_context_stack = errcallback.previous;
861 }
TransactionId write_xid
Definition: logical.h:88
void * private_data
LogicalDecodeMessageCB message_cb
OutputPluginCallbacks callbacks
Definition: logical.h:52
void(* callback)(void *arg)
Definition: elog.h:256
struct ErrorContextCallback * previous
Definition: elog.h:255
const char * callback_name
Definition: logical.c:48
XLogRecPtr write_location
Definition: logical.h:87
ErrorContextCallback * error_context_stack
Definition: elog.c:91
#define InvalidTransactionId
Definition: transam.h:31
TransactionId xid
#define Assert(condition)
Definition: c.h:739
Definition: regguts.h:298
XLogRecPtr report_location
Definition: logical.c:49
LogicalDecodingContext * ctx
Definition: logical.c:47
static void output_plugin_error_callback(void *arg)
Definition: logical.c:581

◆ output_plugin_error_callback()

static void output_plugin_error_callback ( void *  arg)
static

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

582 {
584 
585  /* not all callbacks have an associated LSN */
586  if (state->report_location != InvalidXLogRecPtr)
587  errcontext("slot \"%s\", output plugin \"%s\", in the %s callback, associated LSN %X/%X",
588  NameStr(state->ctx->slot->data.name),
589  NameStr(state->ctx->slot->data.plugin),
590  state->callback_name,
591  (uint32) (state->report_location >> 32),
592  (uint32) state->report_location);
593  else
594  errcontext("slot \"%s\", output plugin \"%s\", in the %s callback",
595  NameStr(state->ctx->slot->data.name),
596  NameStr(state->ctx->slot->data.plugin),
597  state->callback_name);
598 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
ReplicationSlotPersistentData data
Definition: slot.h:132
const char * callback_name
Definition: logical.c:48
unsigned int uint32
Definition: c.h:359
Definition: regguts.h:298
XLogRecPtr report_location
Definition: logical.c:49
ReplicationSlot * slot
Definition: logical.h:38
#define NameStr(name)
Definition: c.h:616
#define errcontext
Definition: elog.h:183
void * arg
LogicalDecodingContext * ctx
Definition: logical.c:47

◆ OutputPluginPrepareWrite()

void OutputPluginPrepareWrite ( struct LogicalDecodingContext ctx,
bool  last_write 
)

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

521 {
522  if (!ctx->accept_writes)
523  elog(ERROR, "writes are only accepted in commit, begin and change callbacks");
524 
525  ctx->prepare_write(ctx, ctx->write_location, ctx->write_xid, last_write);
526  ctx->prepared_write = true;
527 }
TransactionId write_xid
Definition: logical.h:88
XLogRecPtr write_location
Definition: logical.h:87
#define ERROR
Definition: elog.h:43
LogicalOutputPluginWriterPrepareWrite prepare_write
Definition: logical.h:63
#define elog(elevel,...)
Definition: elog.h:228

◆ OutputPluginUpdateProgress()

void OutputPluginUpdateProgress ( struct LogicalDecodingContext ctx)

Definition at line 546 of file logical.c.

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

Referenced by pgoutput_commit_txn().

547 {
548  if (!ctx->update_progress)
549  return;
550 
551  ctx->update_progress(ctx, ctx->write_location, ctx->write_xid);
552 }
TransactionId write_xid
Definition: logical.h:88
XLogRecPtr write_location
Definition: logical.h:87
LogicalOutputPluginWriterUpdateProgress update_progress
Definition: logical.h:65

◆ OutputPluginWrite()

void OutputPluginWrite ( struct LogicalDecodingContext ctx,
bool  last_write 
)

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

534 {
535  if (!ctx->prepared_write)
536  elog(ERROR, "OutputPluginPrepareWrite needs to be called before OutputPluginWrite");
537 
538  ctx->write(ctx, ctx->write_location, ctx->write_xid, last_write);
539  ctx->prepared_write = false;
540 }
TransactionId write_xid
Definition: logical.h:88
XLogRecPtr write_location
Definition: logical.h:87
#define ERROR
Definition: elog.h:43
LogicalOutputPluginWriterWrite write
Definition: logical.h:64
#define elog(elevel,...)
Definition: elog.h:228

◆ shutdown_cb_wrapper()

static void shutdown_cb_wrapper ( LogicalDecodingContext ctx)
static

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

629 {
631  ErrorContextCallback errcallback;
632 
633  Assert(!ctx->fast_forward);
634 
635  /* Push callback + info on the error context stack */
636  state.ctx = ctx;
637  state.callback_name = "shutdown";
640  errcallback.arg = (void *) &state;
641  errcallback.previous = error_context_stack;
642  error_context_stack = &errcallback;
643 
644  /* set output state */
645  ctx->accept_writes = false;
646 
647  /* do the actual work: call callback */
648  ctx->callbacks.shutdown_cb(ctx);
649 
650  /* Pop the error context stack */
651  error_context_stack = errcallback.previous;
652 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:52
void(* callback)(void *arg)
Definition: elog.h:256
struct ErrorContextCallback * previous
Definition: elog.h:255
const char * callback_name
Definition: logical.c:48
ErrorContextCallback * error_context_stack
Definition: elog.c:91
#define Assert(condition)
Definition: c.h:739
Definition: regguts.h:298
LogicalDecodeShutdownCB shutdown_cb
XLogRecPtr report_location
Definition: logical.c:49
LogicalDecodingContext * ctx
Definition: logical.c:47
static void output_plugin_error_callback(void *arg)
Definition: logical.c:581

◆ startup_cb_wrapper()

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

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

602 {
604  ErrorContextCallback errcallback;
605 
606  Assert(!ctx->fast_forward);
607 
608  /* Push callback + info on the error context stack */
609  state.ctx = ctx;
610  state.callback_name = "startup";
613  errcallback.arg = (void *) &state;
614  errcallback.previous = error_context_stack;
615  error_context_stack = &errcallback;
616 
617  /* set output state */
618  ctx->accept_writes = false;
619 
620  /* do the actual work: call callback */
621  ctx->callbacks.startup_cb(ctx, opt, is_init);
622 
623  /* Pop the error context stack */
624  error_context_stack = errcallback.previous;
625 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:52
void(* callback)(void *arg)
Definition: elog.h:256
struct ErrorContextCallback * previous
Definition: elog.h:255
const char * callback_name
Definition: logical.c:48
ErrorContextCallback * error_context_stack
Definition: elog.c:91
#define Assert(condition)
Definition: c.h:739
Definition: regguts.h:298
LogicalDecodeStartupCB startup_cb
XLogRecPtr report_location
Definition: logical.c:49
LogicalDecodingContext * ctx
Definition: logical.c:47
static void output_plugin_error_callback(void *arg)
Definition: logical.c:581

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

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

Referenced by CreateDecodingContext(), and CreateInitDecodingContext().

127 {
128  ReplicationSlot *slot;
129  MemoryContext context,
130  old_context;
132 
133  /* shorter lines... */
134  slot = MyReplicationSlot;
135 
137  "Logical decoding context",
139  old_context = MemoryContextSwitchTo(context);
140  ctx = palloc0(sizeof(LogicalDecodingContext));
141 
142  ctx->context = context;
143 
144  /*
145  * (re-)load output plugins, so we detect a bad (removed) output plugin
146  * now.
147  */
148  if (!fast_forward)
150 
151  /*
152  * Now that the slot's xmin has been set, we can announce ourselves as a
153  * logical decoding backend which doesn't need to be checked individually
154  * when computing the xmin horizon because the xmin is enforced via
155  * replication slots.
156  *
157  * We can only do so if we're outside of a transaction (i.e. the case when
158  * streaming changes via walsender), otherwise an already setup
159  * snapshot/xid would end up being ignored. That's not a particularly
160  * bothersome restriction since the SQL interface can't be used for
161  * streaming anyway.
162  */
164  {
165  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
167  LWLockRelease(ProcArrayLock);
168  }
169 
170  ctx->slot = slot;
171 
172  ctx->reader = XLogReaderAllocate(wal_segment_size, NULL, read_page, ctx);
173  if (!ctx->reader)
174  ereport(ERROR,
175  (errcode(ERRCODE_OUT_OF_MEMORY),
176  errmsg("out of memory")));
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
#define AllocSetContextCreate
Definition: memutils.h:170
void * private_data
static void change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
Definition: logical.c:721
struct ReorderBuffer * reorder
Definition: logical.h:42
#define PROC_IN_LOGICAL_DECODING
Definition: proc.h:57
int wal_segment_size
Definition: xlog.c:112
static void LoadOutputPlugin(OutputPluginCallbacks *callbacks, char *plugin)
Definition: logical.c:559
static void commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:690
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
OutputPluginCallbacks callbacks
Definition: logical.h:52
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:608
bool IsTransactionOrTransactionBlock(void)
Definition: xact.c:4653
MemoryContext context
Definition: logical.h:35
List * output_plugin_options
Definition: logical.h:58
ReorderBufferCommitCB commit
ReplicationSlotPersistentData data
Definition: slot.h:132
PGXACT * MyPgXact
Definition: proc.c:68
uint8 vacuumFlags
Definition: proc.h:233
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
#define ERROR
Definition: elog.h:43
LogicalOutputPluginWriterPrepareWrite prepare_write
Definition: logical.h:63
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
LogicalOutputPluginWriterUpdateProgress update_progress
Definition: logical.h:65
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
ReorderBufferMessageCB message
#define ereport(elevel, rest)
Definition: elog.h:141
void * palloc0(Size size)
Definition: mcxt.c:980
static void begin_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
Definition: logical.c:660
struct SnapBuild * snapshot_builder
Definition: logical.h:43
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
ReorderBuffer * ReorderBufferAllocate(void)
XLogReaderState * XLogReaderAllocate(int wal_segment_size, const char *waldir, XLogPageReadCB pagereadfunc, void *private_data)
Definition: xlogreader.c:73
static void truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
Definition: logical.c:758
int errmsg(const char *fmt,...)
Definition: elog.c:822
XLogReaderState * reader
Definition: logical.h:41
ReplicationSlot * slot
Definition: logical.h:38
ReorderBufferApplyTruncateCB apply_truncate
LogicalOutputPluginWriterWrite write
Definition: logical.h:64
StringInfo out
Definition: logical.h:70
#define NameStr(name)
Definition: c.h:616
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:828
ReorderBufferBeginCB begin
SnapBuild * AllocateSnapshotBuilder(ReorderBuffer *reorder, TransactionId xmin_horizon, XLogRecPtr start_lsn, bool need_full_snapshot)
Definition: snapbuild.c:310

◆ truncate_cb_wrapper()

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

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

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