PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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, XLogPageReadCB read_page, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write)
 
LogicalDecodingContextCreateInitDecodingContext (char *plugin, List *output_plugin_options, XLogPageReadCB read_page, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write)
 
LogicalDecodingContextCreateDecodingContext (XLogRecPtr start_lsn, List *output_plugin_options, XLogPageReadCB read_page, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write)
 
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)
 
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

Function Documentation

static void begin_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn 
)
static

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

597 {
598  LogicalDecodingContext *ctx = cache->private_data;
600  ErrorContextCallback errcallback;
601 
602  /* Push callback + info on the error context stack */
603  state.ctx = ctx;
604  state.callback_name = "begin";
605  state.report_location = txn->first_lsn;
607  errcallback.arg = (void *) &state;
608  errcallback.previous = error_context_stack;
609  error_context_stack = &errcallback;
610 
611  /* set output state */
612  ctx->accept_writes = true;
613  ctx->write_xid = txn->xid;
614  ctx->write_location = txn->first_lsn;
615 
616  /* do the actual work: call callback */
617  ctx->callbacks.begin_cb(ctx, txn);
618 
619  /* Pop the error context stack */
620  error_context_stack = errcallback.previous;
621 }
XLogRecPtr first_lsn
TransactionId write_xid
Definition: logical.h:77
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:42
struct ErrorContextCallback * previous
Definition: elog.h:238
const char * callback_name
Definition: logical.c:51
XLogRecPtr write_location
Definition: logical.h:76
ErrorContextCallback * error_context_stack
Definition: elog.c:88
TransactionId xid
Definition: regguts.h:298
XLogRecPtr report_location
Definition: logical.c:52
void(* callback)(void *arg)
Definition: elog.h:239
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:521
static void change_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn,
Relation  relation,
ReorderBufferChange change 
)
static

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

655 {
656  LogicalDecodingContext *ctx = cache->private_data;
658  ErrorContextCallback errcallback;
659 
660  /* Push callback + info on the error context stack */
661  state.ctx = ctx;
662  state.callback_name = "change";
663  state.report_location = change->lsn;
665  errcallback.arg = (void *) &state;
666  errcallback.previous = error_context_stack;
667  error_context_stack = &errcallback;
668 
669  /* set output state */
670  ctx->accept_writes = true;
671  ctx->write_xid = txn->xid;
672 
673  /*
674  * report this change's lsn so replies from clients can give an up2date
675  * answer. This won't ever be enough (and shouldn't be!) to confirm
676  * receipt of this transaction, but it might allow another transaction's
677  * commit to be confirmed with one message.
678  */
679  ctx->write_location = change->lsn;
680 
681  ctx->callbacks.change_cb(ctx, txn, relation, change);
682 
683  /* Pop the error context stack */
684  error_context_stack = errcallback.previous;
685 }
TransactionId write_xid
Definition: logical.h:77
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:42
struct ErrorContextCallback * previous
Definition: elog.h:238
const char * callback_name
Definition: logical.c:51
XLogRecPtr write_location
Definition: logical.h:76
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
void(* callback)(void *arg)
Definition: elog.h:239
LogicalDecodingContext * ctx
Definition: logical.c:50
static void output_plugin_error_callback(void *arg)
Definition: logical.c:521
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:889
int wal_level
Definition: xlog.c:104
int errcode(int sqlerrcode)
Definition: elog.c:575
bool RecoveryInProgress(void)
Definition: xlog.c:7874
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
Oid MyDatabaseId
Definition: globals.c:76
#define InvalidOid
Definition: postgres_ext.h:36
int errmsg(const char *fmt,...)
Definition: elog.c:797
static void commit_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn,
XLogRecPtr  commit_lsn 
)
static

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

626 {
627  LogicalDecodingContext *ctx = cache->private_data;
629  ErrorContextCallback errcallback;
630 
631  /* Push callback + info on the error context stack */
632  state.ctx = ctx;
633  state.callback_name = "commit";
634  state.report_location = txn->final_lsn; /* beginning of commit record */
636  errcallback.arg = (void *) &state;
637  errcallback.previous = error_context_stack;
638  error_context_stack = &errcallback;
639 
640  /* set output state */
641  ctx->accept_writes = true;
642  ctx->write_xid = txn->xid;
643  ctx->write_location = txn->end_lsn; /* points to the end of the record */
644 
645  /* do the actual work: call callback */
646  ctx->callbacks.commit_cb(ctx, txn, commit_lsn);
647 
648  /* Pop the error context stack */
649  error_context_stack = errcallback.previous;
650 }
TransactionId write_xid
Definition: logical.h:77
void * private_data
OutputPluginCallbacks callbacks
Definition: logical.h:42
struct ErrorContextCallback * previous
Definition: elog.h:238
const char * callback_name
Definition: logical.c:51
XLogRecPtr write_location
Definition: logical.h:76
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
void(* callback)(void *arg)
Definition: elog.h:239
LogicalDecodingContext * ctx
Definition: logical.c:50
static void output_plugin_error_callback(void *arg)
Definition: logical.c:521
LogicalDecodingContext* CreateDecodingContext ( XLogRecPtr  start_lsn,
List output_plugin_options,
XLogPageReadCB  read_page,
LogicalOutputPluginWriterPrepareWrite  prepare_write,
LogicalOutputPluginWriterWrite  do_write 
)

Definition at line 327 of file logical.c.

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

Referenced by pg_logical_slot_get_changes_guts(), and StartLogicalReplication().

332 {
334  ReplicationSlot *slot;
335  MemoryContext old_context;
336 
337  /* shorter lines... */
338  slot = MyReplicationSlot;
339 
340  /* first some sanity checks that are unlikely to be violated */
341  if (slot == NULL)
342  elog(ERROR, "cannot perform logical decoding without an acquired slot");
343 
344  /* make sure the passed slot is suitable, these are user facing errors */
345  if (SlotIsPhysical(slot))
346  ereport(ERROR,
347  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
348  (errmsg("cannot use physical replication slot for logical decoding"))));
349 
350  if (slot->data.database != MyDatabaseId)
351  ereport(ERROR,
352  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
353  (errmsg("replication slot \"%s\" was not created in this database",
354  NameStr(slot->data.name)))));
355 
356  if (start_lsn == InvalidXLogRecPtr)
357  {
358  /* continue from last position */
359  start_lsn = slot->data.confirmed_flush;
360  }
361  else if (start_lsn < slot->data.confirmed_flush)
362  {
363  /*
364  * It might seem like we should error out in this case, but it's
365  * pretty common for a client to acknowledge a LSN it doesn't have to
366  * do anything for, and thus didn't store persistently, because the
367  * xlog records didn't result in anything relevant for logical
368  * decoding. Clients have to be able to do that to support synchronous
369  * replication.
370  */
371  elog(DEBUG1, "cannot stream from %X/%X, minimum is %X/%X, forwarding",
372  (uint32) (start_lsn >> 32), (uint32) start_lsn,
373  (uint32) (slot->data.confirmed_flush >> 32),
374  (uint32) slot->data.confirmed_flush);
375 
376  start_lsn = slot->data.confirmed_flush;
377  }
378 
379  ctx = StartupDecodingContext(output_plugin_options,
380  start_lsn, InvalidTransactionId,
381  read_page, prepare_write, do_write);
382 
383  /* call output plugin initialization callback */
384  old_context = MemoryContextSwitchTo(ctx->context);
385  if (ctx->callbacks.startup_cb != NULL)
386  startup_cb_wrapper(ctx, &ctx->options, false);
387  MemoryContextSwitchTo(old_context);
388 
389  ereport(LOG,
390  (errmsg("starting logical decoding for slot \"%s\"",
391  NameStr(slot->data.name)),
392  errdetail("streaming transactions committing after %X/%X, reading WAL from %X/%X",
393  (uint32) (slot->data.confirmed_flush >> 32),
394  (uint32) slot->data.confirmed_flush,
395  (uint32) (slot->data.restart_lsn >> 32),
396  (uint32) slot->data.restart_lsn)));
397 
398  return ctx;
399 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
OutputPluginOptions options
Definition: logical.h:43
OutputPluginCallbacks callbacks
Definition: logical.h:42
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
MemoryContext context
Definition: logical.h:32
ReplicationSlotPersistentData data
Definition: slot.h:115
#define LOG
Definition: elog.h:26
#define SlotIsPhysical(slot)
Definition: slot.h:133
XLogRecPtr confirmed_flush
Definition: slot.h:76
#define ERROR
Definition: elog.h:43
static void startup_cb_wrapper(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
Definition: logical.c:541
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define InvalidTransactionId
Definition: transam.h:31
unsigned int uint32
Definition: c.h:268
#define ereport(elevel, rest)
Definition: elog.h:122
static LogicalDecodingContext * StartupDecodingContext(List *output_plugin_options, XLogRecPtr start_lsn, TransactionId xmin_horizon, XLogPageReadCB read_page, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write)
Definition: logical.c:114
Oid MyDatabaseId
Definition: globals.c:76
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define NULL
Definition: c.h:229
XLogRecPtr restart_lsn
Definition: slot.h:68
LogicalDecodeStartupCB startup_cb
Definition: output_plugin.h:97
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define NameStr(name)
Definition: c.h:499
#define elog
Definition: elog.h:219
LogicalDecodingContext* CreateInitDecodingContext ( char *  plugin,
List output_plugin_options,
XLogPageReadCB  read_page,
LogicalOutputPluginWriterPrepareWrite  prepare_write,
LogicalOutputPluginWriterWrite  do_write 
)

Definition at line 211 of file logical.c.

References LogicalDecodingContext::callbacks, ReplicationSlotPersistentData::catalog_xmin, LogicalDecodingContext::context, ReplicationSlot::data, ReplicationSlotPersistentData::database, ReplicationSlot::effective_catalog_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, NULL, 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().

216 {
217  TransactionId xmin_horizon = InvalidTransactionId;
218  ReplicationSlot *slot;
220  MemoryContext old_context;
221 
222  /* shorter lines... */
223  slot = MyReplicationSlot;
224 
225  /* first some sanity checks that are unlikely to be violated */
226  if (slot == NULL)
227  elog(ERROR, "cannot perform logical decoding without an acquired slot");
228 
229  if (plugin == NULL)
230  elog(ERROR, "cannot initialize logical decoding without a specified plugin");
231 
232  /* Make sure the passed slot is suitable. These are user facing errors. */
233  if (SlotIsPhysical(slot))
234  ereport(ERROR,
235  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
236  errmsg("cannot use physical replication slot for logical decoding")));
237 
238  if (slot->data.database != MyDatabaseId)
239  ereport(ERROR,
240  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
241  errmsg("replication slot \"%s\" was not created in this database",
242  NameStr(slot->data.name))));
243 
244  if (IsTransactionState() &&
246  ereport(ERROR,
247  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
248  errmsg("cannot create logical replication slot in transaction that has performed writes")));
249 
250  /* register output plugin name with slot */
251  SpinLockAcquire(&slot->mutex);
253  SpinLockRelease(&slot->mutex);
254 
256 
257  /* ----
258  * This is a bit tricky: We need to determine a safe xmin horizon to start
259  * decoding from, to avoid starting from a running xacts record referring
260  * to xids whose rows have been vacuumed or pruned
261  * already. GetOldestSafeDecodingTransactionId() returns such a value, but
262  * without further interlock its return value might immediately be out of
263  * date.
264  *
265  * So we have to acquire the ProcArrayLock to prevent computation of new
266  * xmin horizons by other backends, get the safe decoding xid, and inform
267  * the slot machinery about the new limit. Once that's done the
268  * ProcArrayLock can be released as the slot machinery now is
269  * protecting against vacuum.
270  * ----
271  */
272  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
273 
276 
278 
279  LWLockRelease(ProcArrayLock);
280 
281  /*
282  * tell the snapshot builder to only assemble snapshot once reaching the
283  * running_xact's record with the respective xmin.
284  */
285  xmin_horizon = slot->data.catalog_xmin;
286 
289 
290  ctx = StartupDecodingContext(NIL, InvalidXLogRecPtr, xmin_horizon,
291  read_page, prepare_write, do_write);
292 
293  /* call output plugin initialization callback */
294  old_context = MemoryContextSwitchTo(ctx->context);
295  if (ctx->callbacks.startup_cb != NULL)
296  startup_cb_wrapper(ctx, &ctx->options, true);
297  MemoryContextSwitchTo(old_context);
298 
299  return ctx;
300 }
#define NIL
Definition: pg_list.h:69
static const char * plugin
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
uint32 TransactionId
Definition: c.h:397
OutputPluginOptions options
Definition: logical.h:43
OutputPluginCallbacks callbacks
Definition: logical.h:42
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
MemoryContext context
Definition: logical.h:32
void ReplicationSlotSave(void)
Definition: slot.c:563
ReplicationSlotPersistentData data
Definition: slot.h:115
#define SlotIsPhysical(slot)
Definition: slot.h:133
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
#define NAMEDATALEN
#define SpinLockAcquire(lock)
Definition: spin.h:62
void ReplicationSlotReserveWal(void)
Definition: slot.c:909
#define ERROR
Definition: elog.h:43
static void startup_cb_wrapper(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
Definition: logical.c:541
TransactionId catalog_xmin
Definition: slot.h:65
#define InvalidTransactionId
Definition: transam.h:31
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:404
#define ereport(elevel, rest)
Definition: elog.h:122
static LogicalDecodingContext * StartupDecodingContext(List *output_plugin_options, XLogRecPtr start_lsn, TransactionId xmin_horizon, XLogPageReadCB read_page, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write)
Definition: logical.c:114
#define SpinLockRelease(lock)
Definition: spin.h:64
TransactionId effective_catalog_xmin
Definition: slot.h:112
Oid MyDatabaseId
Definition: globals.c:76
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define NULL
Definition: c.h:229
#define StrNCpy(dst, src, len)
Definition: c.h:830
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
bool IsTransactionState(void)
Definition: xact.c:350
LogicalDecodeStartupCB startup_cb
Definition: output_plugin.h:97
TransactionId GetOldestSafeDecodingTransactionId(void)
Definition: procarray.c:2154
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define NameStr(name)
Definition: c.h:499
#define elog
Definition: elog.h:219
slock_t mutex
Definition: slot.h:88
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:617
void ReplicationSlotMarkDirty(void)
Definition: slot.c:581
void DecodingContextFindStartpoint ( LogicalDecodingContext ctx)

Definition at line 414 of file logical.c.

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

Referenced by CreateReplicationSlot(), and pg_create_logical_replication_slot().

415 {
416  XLogRecPtr startptr;
417 
418  /* Initialize from where to start reading WAL. */
419  startptr = ctx->slot->data.restart_lsn;
420 
421  elog(DEBUG1, "searching for logical decoding starting point, starting at %X/%X",
422  (uint32) (ctx->slot->data.restart_lsn >> 32),
423  (uint32) ctx->slot->data.restart_lsn);
424 
425  /* Wait for a consistent starting point */
426  for (;;)
427  {
428  XLogRecord *record;
429  char *err = NULL;
430 
431  /* the read_page callback waits for new WAL */
432  record = XLogReadRecord(ctx->reader, startptr, &err);
433  if (err)
434  elog(ERROR, "%s", err);
435  if (!record)
436  elog(ERROR, "no record found"); /* shouldn't happen */
437 
438  startptr = InvalidXLogRecPtr;
439 
441 
442  /* only continue till we found a consistent spot */
443  if (DecodingContextReady(ctx))
444  break;
445 
447  }
448 
449  ctx->slot->data.confirmed_flush = ctx->reader->EndRecPtr;
450 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
bool DecodingContextReady(LogicalDecodingContext *ctx)
Definition: logical.c:405
ReplicationSlotPersistentData data
Definition: slot.h:115
XLogRecord * XLogReadRecord(XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
Definition: xlogreader.c:193
XLogRecPtr confirmed_flush
Definition: slot.h:76
XLogRecPtr EndRecPtr
Definition: xlogreader.h:115
#define ERROR
Definition: elog.h:43
void LogicalDecodingProcessRecord(LogicalDecodingContext *ctx, XLogReaderState *record)
Definition: decode.c:93
unsigned int uint32
Definition: c.h:268
#define NULL
Definition: c.h:229
uint64 XLogRecPtr
Definition: xlogdefs.h:21
XLogRecPtr restart_lsn
Definition: slot.h:68
XLogReaderState * reader
Definition: logical.h:38
ReplicationSlot * slot
Definition: logical.h:35
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
#define elog
Definition: elog.h:219
bool DecodingContextReady ( LogicalDecodingContext ctx)

Definition at line 405 of file logical.c.

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

Referenced by DecodingContextFindStartpoint().

406 {
408 }
SnapBuildState SnapBuildCurrentState(SnapBuild *builder)
Definition: snapbuild.c:365
struct SnapBuild * snapshot_builder
Definition: logical.h:40
bool filter_by_origin_cb_wrapper ( LogicalDecodingContext ctx,
RepOriginId  origin_id 
)

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

689 {
691  ErrorContextCallback errcallback;
692  bool ret;
693 
694  /* Push callback + info on the error context stack */
695  state.ctx = ctx;
696  state.callback_name = "filter_by_origin";
699  errcallback.arg = (void *) &state;
700  errcallback.previous = error_context_stack;
701  error_context_stack = &errcallback;
702 
703  /* set output state */
704  ctx->accept_writes = false;
705 
706  /* do the actual work: call callback */
707  ret = ctx->callbacks.filter_by_origin_cb(ctx, origin_id);
708 
709  /* Pop the error context stack */
710  error_context_stack = errcallback.previous;
711 
712  return ret;
713 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:42
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
void(* callback)(void *arg)
Definition: elog.h:239
LogicalDecodingContext * ctx
Definition: logical.c:50
static void output_plugin_error_callback(void *arg)
Definition: logical.c:521
LogicalDecodeFilterByOriginCB filter_by_origin_cb
void FreeDecodingContext ( LogicalDecodingContext ctx)

Definition at line 457 of file logical.c.

References LogicalDecodingContext::callbacks, LogicalDecodingContext::context, FreeSnapshotBuilder(), MemoryContextDelete(), NULL, 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().

458 {
459  if (ctx->callbacks.shutdown_cb != NULL)
460  shutdown_cb_wrapper(ctx);
461 
464  XLogReaderFree(ctx->reader);
466 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
struct ReorderBuffer * reorder
Definition: logical.h:39
OutputPluginCallbacks callbacks
Definition: logical.h:42
void ReorderBufferFree(ReorderBuffer *rb)
MemoryContext context
Definition: logical.h:32
static void shutdown_cb_wrapper(LogicalDecodingContext *ctx)
Definition: logical.c:566
void XLogReaderFree(XLogReaderState *state)
Definition: xlogreader.c:125
void FreeSnapshotBuilder(SnapBuild *builder)
Definition: snapbuild.c:321
struct SnapBuild * snapshot_builder
Definition: logical.h:40
#define NULL
Definition: c.h:229
LogicalDecodeShutdownCB shutdown_cb
XLogReaderState * reader
Definition: logical.h:38
static void LoadOutputPlugin ( OutputPluginCallbacks callbacks,
char *  plugin 
)
static

Definition at line 499 of file logical.c.

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

Referenced by StartupDecodingContext().

500 {
501  LogicalOutputPluginInit plugin_init;
502 
503  plugin_init = (LogicalOutputPluginInit)
504  load_external_function(plugin, "_PG_output_plugin_init", false, NULL);
505 
506  if (plugin_init == NULL)
507  elog(ERROR, "output plugins have to declare the _PG_output_plugin_init symbol");
508 
509  /* ask the output plugin to fill the callback struct */
510  plugin_init(callbacks);
511 
512  if (callbacks->begin_cb == NULL)
513  elog(ERROR, "output plugins have to register a begin callback");
514  if (callbacks->change_cb == NULL)
515  elog(ERROR, "output plugins have to register a change callback");
516  if (callbacks->commit_cb == NULL)
517  elog(ERROR, "output plugins have to register a commit callback");
518 }
static const char * plugin
void(* LogicalOutputPluginInit)(struct OutputPluginCallbacks *cb)
Definition: output_plugin.h:35
#define ERROR
Definition: elog.h:43
LogicalDecodeCommitCB commit_cb
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
#define NULL
Definition: c.h:229
LogicalDecodeBeginCB begin_cb
Definition: output_plugin.h:98
#define elog
Definition: elog.h:219
void LogicalConfirmReceivedLocation ( XLogRecPtr  lsn)

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

885 {
886  Assert(lsn != InvalidXLogRecPtr);
887 
888  /* Do an unlocked check for candidate_lsn first. */
891  {
892  bool updated_xmin = false;
893  bool updated_restart = false;
894 
896 
898 
899  /* if we're past the location required for bumping xmin, do so */
902  {
903  /*
904  * We have to write the changed xmin to disk *before* we change
905  * the in-memory value, otherwise after a crash we wouldn't know
906  * that some catalog tuples might have been removed already.
907  *
908  * Ensure that by first writing to ->xmin and only update
909  * ->effective_xmin once the new state is synced to disk. After a
910  * crash ->effective_xmin is set to ->xmin.
911  */
914  {
918  updated_xmin = true;
919  }
920  }
921 
924  {
926 
930  updated_restart = true;
931  }
932 
934 
935  /* first write new xmin to disk, so we know what's up after a crash */
936  if (updated_xmin || updated_restart)
937  {
940  elog(DEBUG1, "updated xmin: %u restart: %u", updated_xmin, updated_restart);
941  }
942 
943  /*
944  * Now the new xmin is safely on disk, we can let the global value
945  * advance. We do not take ProcArrayLock or similar since we only
946  * advance xmin here and there's not much harm done by a concurrent
947  * computation missing that.
948  */
949  if (updated_xmin)
950  {
954 
957  }
958  }
959  else
960  {
964  }
965 }
TransactionId candidate_catalog_xmin
Definition: slot.h:127
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
void ReplicationSlotSave(void)
Definition: slot.c:563
ReplicationSlotPersistentData data
Definition: slot.h:115
XLogRecPtr confirmed_flush
Definition: slot.h:76
#define SpinLockAcquire(lock)
Definition: spin.h:62
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:665
XLogRecPtr candidate_restart_valid
Definition: slot.h:129
TransactionId catalog_xmin
Definition: slot.h:65
#define InvalidTransactionId
Definition: transam.h:31
#define SpinLockRelease(lock)
Definition: spin.h:64
TransactionId effective_catalog_xmin
Definition: slot.h:112
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:675
XLogRecPtr restart_lsn
Definition: slot.h:68
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:128
#define elog
Definition: elog.h:219
slock_t mutex
Definition: slot.h:88
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:617
XLogRecPtr candidate_restart_lsn
Definition: slot.h:130
void ReplicationSlotMarkDirty(void)
Definition: slot.c:581
void LogicalIncreaseRestartDecodingForSlot ( XLogRecPtr  current_lsn,
XLogRecPtr  restart_lsn 
)

Definition at line 815 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, NULL, SpinLockAcquire, and SpinLockRelease.

Referenced by SnapBuildProcessRunningXacts().

816 {
817  bool updated_lsn = false;
818  ReplicationSlot *slot;
819 
820  slot = MyReplicationSlot;
821 
822  Assert(slot != NULL);
823  Assert(restart_lsn != InvalidXLogRecPtr);
824  Assert(current_lsn != InvalidXLogRecPtr);
825 
826  SpinLockAcquire(&slot->mutex);
827 
828  /* don't overwrite if have a newer restart lsn */
829  if (restart_lsn <= slot->data.restart_lsn)
830  {
831  }
832 
833  /*
834  * We might have already flushed far enough to directly accept this lsn,
835  * in this case there is no need to check for existing candidate LSNs
836  */
837  else if (current_lsn <= slot->data.confirmed_flush)
838  {
839  slot->candidate_restart_valid = current_lsn;
840  slot->candidate_restart_lsn = restart_lsn;
841 
842  /* our candidate can directly be used */
843  updated_lsn = true;
844  }
845 
846  /*
847  * Only increase if the previous values have been applied, otherwise we
848  * might never end up updating if the receiver acks too slowly. A missed
849  * value here will just cause some extra effort after reconnecting.
850  */
852  {
853  slot->candidate_restart_valid = current_lsn;
854  slot->candidate_restart_lsn = restart_lsn;
855 
856  elog(DEBUG1, "got new restart lsn %X/%X at %X/%X",
857  (uint32) (restart_lsn >> 32), (uint32) restart_lsn,
858  (uint32) (current_lsn >> 32), (uint32) current_lsn);
859  }
860  else
861  {
862  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",
863  (uint32) (restart_lsn >> 32), (uint32) restart_lsn,
864  (uint32) (current_lsn >> 32), (uint32) current_lsn,
865  (uint32) (slot->candidate_restart_lsn >> 32),
867  (uint32) (slot->candidate_restart_valid >> 32),
869  (uint32) (slot->data.confirmed_flush >> 32),
870  (uint32) slot->data.confirmed_flush
871  );
872  }
873  SpinLockRelease(&slot->mutex);
874 
875  /* candidates are already valid with the current flush position, apply */
876  if (updated_lsn)
878 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
ReplicationSlotPersistentData data
Definition: slot.h:115
XLogRecPtr confirmed_flush
Definition: slot.h:76
#define SpinLockAcquire(lock)
Definition: spin.h:62
XLogRecPtr candidate_restart_valid
Definition: slot.h:129
unsigned int uint32
Definition: c.h:268
#define SpinLockRelease(lock)
Definition: spin.h:64
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
void LogicalConfirmReceivedLocation(XLogRecPtr lsn)
Definition: logical.c:884
#define elog
Definition: elog.h:219
slock_t mutex
Definition: slot.h:88
XLogRecPtr candidate_restart_lsn
Definition: slot.h:130
void LogicalIncreaseXminForSlot ( XLogRecPtr  current_lsn,
TransactionId  xmin 
)

Definition at line 758 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, NULL, SpinLockAcquire, SpinLockRelease, and TransactionIdPrecedesOrEquals().

Referenced by SnapBuildProcessRunningXacts().

759 {
760  bool updated_xmin = false;
761  ReplicationSlot *slot;
762 
763  slot = MyReplicationSlot;
764 
765  Assert(slot != NULL);
766 
767  SpinLockAcquire(&slot->mutex);
768 
769  /*
770  * don't overwrite if we already have a newer xmin. This can happen if we
771  * restart decoding in a slot.
772  */
774  {
775  }
776 
777  /*
778  * If the client has already confirmed up to this lsn, we directly can
779  * mark this as accepted. This can happen if we restart decoding in a
780  * slot.
781  */
782  else if (current_lsn <= slot->data.confirmed_flush)
783  {
784  slot->candidate_catalog_xmin = xmin;
785  slot->candidate_xmin_lsn = current_lsn;
786 
787  /* our candidate can directly be used */
788  updated_xmin = true;
789  }
790 
791  /*
792  * Only increase if the previous values have been applied, otherwise we
793  * might never end up updating if the receiver acks too slowly.
794  */
795  else if (slot->candidate_xmin_lsn == InvalidXLogRecPtr)
796  {
797  slot->candidate_catalog_xmin = xmin;
798  slot->candidate_xmin_lsn = current_lsn;
799  }
800  SpinLockRelease(&slot->mutex);
801 
802  /* candidate already valid with the current flush position, apply */
803  if (updated_xmin)
805 }
TransactionId candidate_catalog_xmin
Definition: slot.h:127
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
ReplicationSlotPersistentData data
Definition: slot.h:115
XLogRecPtr confirmed_flush
Definition: slot.h:76
#define SpinLockAcquire(lock)
Definition: spin.h:62
bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:319
TransactionId catalog_xmin
Definition: slot.h:65
#define SpinLockRelease(lock)
Definition: spin.h:64
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
void LogicalConfirmReceivedLocation(XLogRecPtr lsn)
Definition: logical.c:884
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:128
slock_t mutex
Definition: slot.h:88
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 716 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, NULL, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, LogicalErrorCallbackState::report_location, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

719 {
720  LogicalDecodingContext *ctx = cache->private_data;
722  ErrorContextCallback errcallback;
723 
724  if (ctx->callbacks.message_cb == NULL)
725  return;
726 
727  /* Push callback + info on the error context stack */
728  state.ctx = ctx;
729  state.callback_name = "message";
730  state.report_location = message_lsn;
732  errcallback.arg = (void *) &state;
733  errcallback.previous = error_context_stack;
734  error_context_stack = &errcallback;
735 
736  /* set output state */
737  ctx->accept_writes = true;
738  ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
739  ctx->write_location = message_lsn;
740 
741  /* do the actual work: call callback */
742  ctx->callbacks.message_cb(ctx, txn, message_lsn, transactional, prefix,
743  message_size, message);
744 
745  /* Pop the error context stack */
746  error_context_stack = errcallback.previous;
747 }
TransactionId write_xid
Definition: logical.h:77
void * private_data
LogicalDecodeMessageCB message_cb
OutputPluginCallbacks callbacks
Definition: logical.h:42
struct ErrorContextCallback * previous
Definition: elog.h:238
const char * callback_name
Definition: logical.c:51
XLogRecPtr write_location
Definition: logical.h:76
ErrorContextCallback * error_context_stack
Definition: elog.c:88
#define InvalidTransactionId
Definition: transam.h:31
TransactionId xid
#define NULL
Definition: c.h:229
Definition: regguts.h:298
XLogRecPtr report_location
Definition: logical.c:52
void(* callback)(void *arg)
Definition: elog.h:239
LogicalDecodingContext * ctx
Definition: logical.c:50
static void output_plugin_error_callback(void *arg)
Definition: logical.c:521
static void output_plugin_error_callback ( void *  arg)
static

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

522 {
524 
525  /* not all callbacks have an associated LSN */
526  if (state->report_location != InvalidXLogRecPtr)
527  errcontext("slot \"%s\", output plugin \"%s\", in the %s callback, associated LSN %X/%X",
528  NameStr(state->ctx->slot->data.name),
529  NameStr(state->ctx->slot->data.plugin),
530  state->callback_name,
531  (uint32) (state->report_location >> 32),
532  (uint32) state->report_location);
533  else
534  errcontext("slot \"%s\", output plugin \"%s\", in the %s callback",
535  NameStr(state->ctx->slot->data.name),
536  NameStr(state->ctx->slot->data.plugin),
537  state->callback_name);
538 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
ReplicationSlotPersistentData data
Definition: slot.h:115
const char * callback_name
Definition: logical.c:51
unsigned int uint32
Definition: c.h:268
Definition: regguts.h:298
XLogRecPtr report_location
Definition: logical.c:52
ReplicationSlot * slot
Definition: logical.h:35
#define NameStr(name)
Definition: c.h:499
#define errcontext
Definition: elog.h:164
void * arg
LogicalDecodingContext * ctx
Definition: logical.c:50
void OutputPluginPrepareWrite ( struct LogicalDecodingContext ctx,
bool  last_write 
)

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

473 {
474  if (!ctx->accept_writes)
475  elog(ERROR, "writes are only accepted in commit, begin and change callbacks");
476 
477  ctx->prepare_write(ctx, ctx->write_location, ctx->write_xid, last_write);
478  ctx->prepared_write = true;
479 }
TransactionId write_xid
Definition: logical.h:77
XLogRecPtr write_location
Definition: logical.h:76
#define ERROR
Definition: elog.h:43
LogicalOutputPluginWriterPrepareWrite prepare_write
Definition: logical.h:53
#define elog
Definition: elog.h:219
void OutputPluginWrite ( struct LogicalDecodingContext ctx,
bool  last_write 
)

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

486 {
487  if (!ctx->prepared_write)
488  elog(ERROR, "OutputPluginPrepareWrite needs to be called before OutputPluginWrite");
489 
490  ctx->write(ctx, ctx->write_location, ctx->write_xid, last_write);
491  ctx->prepared_write = false;
492 }
TransactionId write_xid
Definition: logical.h:77
XLogRecPtr write_location
Definition: logical.h:76
#define ERROR
Definition: elog.h:43
LogicalOutputPluginWriterWrite write
Definition: logical.h:54
#define elog
Definition: elog.h:219
static void shutdown_cb_wrapper ( LogicalDecodingContext ctx)
static

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

567 {
569  ErrorContextCallback errcallback;
570 
571  /* Push callback + info on the error context stack */
572  state.ctx = ctx;
573  state.callback_name = "shutdown";
576  errcallback.arg = (void *) &state;
577  errcallback.previous = error_context_stack;
578  error_context_stack = &errcallback;
579 
580  /* set output state */
581  ctx->accept_writes = false;
582 
583  /* do the actual work: call callback */
584  ctx->callbacks.shutdown_cb(ctx);
585 
586  /* Pop the error context stack */
587  error_context_stack = errcallback.previous;
588 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:42
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
void(* callback)(void *arg)
Definition: elog.h:239
LogicalDecodingContext * ctx
Definition: logical.c:50
static void output_plugin_error_callback(void *arg)
Definition: logical.c:521
static void startup_cb_wrapper ( LogicalDecodingContext ctx,
OutputPluginOptions opt,
bool  is_init 
)
static

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

542 {
544  ErrorContextCallback errcallback;
545 
546  /* Push callback + info on the error context stack */
547  state.ctx = ctx;
548  state.callback_name = "startup";
551  errcallback.arg = (void *) &state;
552  errcallback.previous = error_context_stack;
553  error_context_stack = &errcallback;
554 
555  /* set output state */
556  ctx->accept_writes = false;
557 
558  /* do the actual work: call callback */
559  ctx->callbacks.startup_cb(ctx, opt, is_init);
560 
561  /* Pop the error context stack */
562  error_context_stack = errcallback.previous;
563 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
OutputPluginCallbacks callbacks
Definition: logical.h:42
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
void(* callback)(void *arg)
Definition: elog.h:239
LogicalDecodingContext * ctx
Definition: logical.c:50
static void output_plugin_error_callback(void *arg)
Definition: logical.c:521
static LogicalDecodingContext* StartupDecodingContext ( List output_plugin_options,
XLogRecPtr  start_lsn,
TransactionId  xmin_horizon,
XLogPageReadCB  read_page,
LogicalOutputPluginWriterPrepareWrite  prepare_write,
LogicalOutputPluginWriterWrite  do_write 
)
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, 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, PGXACT::vacuumFlags, LogicalDecodingContext::write, and XLogReaderAllocate().

Referenced by CreateDecodingContext(), and CreateInitDecodingContext().

120 {
121  ReplicationSlot *slot;
122  MemoryContext context,
123  old_context;
125 
126  /* shorter lines... */
127  slot = MyReplicationSlot;
128 
130  "Logical decoding context",
132  old_context = MemoryContextSwitchTo(context);
133  ctx = palloc0(sizeof(LogicalDecodingContext));
134 
135  ctx->context = context;
136 
137  /*
138  * (re-)load output plugins, so we detect a bad (removed) output plugin
139  * now.
140  */
142 
143  /*
144  * Now that the slot's xmin has been set, we can announce ourselves as a
145  * logical decoding backend which doesn't need to be checked individually
146  * when computing the xmin horizon because the xmin is enforced via
147  * replication slots.
148  *
149  * We can only do so if we're outside of a transaction (i.e. the case when
150  * streaming changes via walsender), otherwise an already setup
151  * snapshot/xid would end up being ignored. That's not a particularly
152  * bothersome restriction since the SQL interface can't be used for
153  * streaming anyway.
154  */
156  {
157  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
159  LWLockRelease(ProcArrayLock);
160  }
161 
162  ctx->slot = slot;
163 
164  ctx->reader = XLogReaderAllocate(read_page, ctx);
165  if (!ctx->reader)
166  ereport(ERROR,
167  (errcode(ERRCODE_OUT_OF_MEMORY),
168  errmsg("out of memory")));
169 
170  ctx->reader->private_data = ctx;
171 
173  ctx->snapshot_builder =
174  AllocateSnapshotBuilder(ctx->reorder, xmin_horizon, start_lsn);
175 
176  ctx->reorder->private_data = ctx;
177 
178  /* wrap output plugin callbacks, so we can add error context information */
183 
184  ctx->out = makeStringInfo();
185  ctx->prepare_write = prepare_write;
186  ctx->write = do_write;
187 
188  ctx->output_plugin_options = output_plugin_options;
189 
190  MemoryContextSwitchTo(old_context);
191 
192  return ctx;
193 }
XLogReaderState * XLogReaderAllocate(XLogPageReadCB pagereadfunc, void *private_data)
Definition: xlogreader.c:67
ReorderBufferApplyChangeCB apply_change
void * private_data
static void change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
Definition: logical.c:653
struct ReorderBuffer * reorder
Definition: logical.h:39
#define PROC_IN_LOGICAL_DECODING
Definition: proc.h:56
static void LoadOutputPlugin(OutputPluginCallbacks *callbacks, char *plugin)
Definition: logical.c:499
static void commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:624
StringInfo makeStringInfo(void)
Definition: stringinfo.c:29
OutputPluginCallbacks callbacks
Definition: logical.h:42
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
bool IsTransactionOrTransactionBlock(void)
Definition: xact.c:4322
MemoryContext context
Definition: logical.h:32
List * output_plugin_options
Definition: logical.h:48
ReorderBufferCommitCB commit
ReplicationSlotPersistentData data
Definition: slot.h:115
void * private_data
Definition: xlogreader.h:108
PGXACT * MyPgXact
Definition: proc.c:68
uint8 vacuumFlags
Definition: proc.h:218
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
#define ERROR
Definition: elog.h:43
LogicalOutputPluginWriterPrepareWrite prepare_write
Definition: logical.h:53
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:165
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
ReorderBufferMessageCB message
#define ereport(elevel, rest)
Definition: elog.h:122
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
void * palloc0(Size size)
Definition: mcxt.c:878
static void begin_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
Definition: logical.c:596
SnapBuild * AllocateSnapshotBuilder(ReorderBuffer *reorder, TransactionId xmin_horizon, XLogRecPtr start_lsn)
Definition: snapbuild.c:282
struct SnapBuild * snapshot_builder
Definition: logical.h:40
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
ReorderBuffer * ReorderBufferAllocate(void)
int errmsg(const char *fmt,...)
Definition: elog.c:797
XLogReaderState * reader
Definition: logical.h:38
ReplicationSlot * slot
Definition: logical.h:35
LogicalOutputPluginWriterWrite write
Definition: logical.h:54
StringInfo out
Definition: logical.h:59
#define NameStr(name)
Definition: c.h:499
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:716
ReorderBufferBeginCB begin