PostgreSQL Source Code git master
Loading...
Searching...
No Matches
logical.c File Reference
#include "postgres.h"
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "access/xlogutils.h"
#include "fmgr.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/decode.h"
#include "replication/logical.h"
#include "replication/reorderbuffer.h"
#include "replication/slotsync.h"
#include "replication/snapbuild.h"
#include "storage/proc.h"
#include "storage/procarray.h"
#include "utils/builtins.h"
#include "utils/injection_point.h"
#include "utils/inval.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 begin_prepare_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn)
 
static void prepare_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
 
static void commit_prepared_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
 
static void rollback_prepared_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_end_lsn, TimestampTz prepare_time)
 
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 stream_start_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr first_lsn)
 
static void stream_stop_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr last_lsn)
 
static void stream_abort_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr abort_lsn)
 
static void stream_prepare_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
 
static void stream_commit_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
 
static void stream_change_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
 
static void stream_message_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr message_lsn, bool transactional, const char *prefix, Size message_size, const char *message)
 
static void stream_truncate_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
 
static void update_progress_txn_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr lsn)
 
static void LoadOutputPlugin (OutputPluginCallbacks *callbacks, const char *plugin)
 
void CheckLogicalDecodingRequirements (bool repack)
 
static LogicalDecodingContextStartupDecodingContext (List *output_plugin_options, XLogRecPtr start_lsn, TransactionId xmin_horizon, bool need_full_snapshot, bool fast_forward, bool in_create, XLogReaderRoutine *xl_routine, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
 
LogicalDecodingContextCreateInitDecodingContext (const char *plugin, List *output_plugin_options, bool need_full_snapshot, bool for_repack, XLogRecPtr restart_lsn, XLogReaderRoutine *xl_routine, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
 
LogicalDecodingContextCreateDecodingContext (XLogRecPtr start_lsn, List *output_plugin_options, bool fast_forward, XLogReaderRoutine *xl_routine, 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 skipped_xact)
 
bool filter_prepare_cb_wrapper (LogicalDecodingContext *ctx, TransactionId xid, const char *gid)
 
bool filter_by_origin_cb_wrapper (LogicalDecodingContext *ctx, ReplOriginId origin_id)
 
void LogicalIncreaseXminForSlot (XLogRecPtr current_lsn, TransactionId xmin)
 
void LogicalIncreaseRestartDecodingForSlot (XLogRecPtr current_lsn, XLogRecPtr restart_lsn)
 
void LogicalConfirmReceivedLocation (XLogRecPtr lsn)
 
void ResetLogicalStreamingState (void)
 
void UpdateDecodingStats (LogicalDecodingContext *ctx)
 
XLogRecPtr LogicalReplicationSlotCheckPendingWal (XLogRecPtr end_of_wal, XLogRecPtr scan_cutoff_lsn)
 
XLogRecPtr LogicalSlotAdvanceAndCheckSnapState (XLogRecPtr moveto, bool *found_consistent_snapshot)
 

Typedef Documentation

◆ LogicalErrorCallbackState

Function Documentation

◆ begin_cb_wrapper()

static void begin_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn 
)
static

Definition at line 831 of file logical.c.

832{
835 ErrorContextCallback errcallback;
836
837 Assert(!ctx->fast_forward);
838
839 /* Push callback + info on the error context stack */
840 state.ctx = ctx;
841 state.callback_name = "begin";
842 state.report_location = txn->first_lsn;
844 errcallback.arg = &state;
845 errcallback.previous = error_context_stack;
846 error_context_stack = &errcallback;
847
848 /* set output state */
849 ctx->accept_writes = true;
850 ctx->write_xid = txn->xid;
851 ctx->write_location = txn->first_lsn;
852 ctx->end_xact = false;
853
854 /* do the actual work: call callback */
855 ctx->callbacks.begin_cb(ctx, txn);
856
857 /* Pop the error context stack */
858 error_context_stack = errcallback.previous;
859}
#define Assert(condition)
Definition c.h:943
ErrorContextCallback * error_context_stack
Definition elog.c:100
static void output_plugin_error_callback(void *arg)
Definition logical.c:751
struct ErrorContextCallback * previous
Definition elog.h:299
void(* callback)(void *arg)
Definition elog.h:300
XLogRecPtr write_location
Definition logical.h:108
OutputPluginCallbacks callbacks
Definition logical.h:53
TransactionId write_xid
Definition logical.h:109
LogicalDecodeBeginCB begin_cb
XLogRecPtr first_lsn
TransactionId xid

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert, OutputPluginCallbacks::begin_cb, ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, error_context_stack, LogicalDecodingContext::fast_forward, ReorderBufferTXN::first_lsn, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ begin_prepare_cb_wrapper()

static void begin_prepare_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn 
)
static

Definition at line 901 of file logical.c.

902{
905 ErrorContextCallback errcallback;
906
907 Assert(!ctx->fast_forward);
908
909 /* We're only supposed to call this when two-phase commits are supported */
910 Assert(ctx->twophase);
911
912 /* Push callback + info on the error context stack */
913 state.ctx = ctx;
914 state.callback_name = "begin_prepare";
915 state.report_location = txn->first_lsn;
917 errcallback.arg = &state;
918 errcallback.previous = error_context_stack;
919 error_context_stack = &errcallback;
920
921 /* set output state */
922 ctx->accept_writes = true;
923 ctx->write_xid = txn->xid;
924 ctx->write_location = txn->first_lsn;
925 ctx->end_xact = false;
926
927 /*
928 * If the plugin supports two-phase commits then begin prepare callback is
929 * mandatory
930 */
931 if (ctx->callbacks.begin_prepare_cb == NULL)
934 errmsg("logical replication at prepare time requires a %s callback",
935 "begin_prepare_cb")));
936
937 /* do the actual work: call callback */
938 ctx->callbacks.begin_prepare_cb(ctx, txn);
939
940 /* Pop the error context stack */
941 error_context_stack = errcallback.previous;
942}
int errcode(int sqlerrcode)
Definition elog.c:875
#define ERROR
Definition elog.h:40
#define ereport(elevel,...)
Definition elog.h:152
static char * errmsg
static int fb(int x)
LogicalDecodeBeginPrepareCB begin_prepare_cb

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert, OutputPluginCallbacks::begin_prepare_cb, ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, ereport, errcode(), errmsg, ERROR, error_context_stack, LogicalDecodingContext::fast_forward, fb(), ReorderBufferTXN::first_lsn, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, LogicalDecodingContext::twophase, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ change_cb_wrapper()

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

Definition at line 1082 of file logical.c.

1084{
1087 ErrorContextCallback errcallback;
1088
1089 Assert(!ctx->fast_forward);
1090
1091 /* Push callback + info on the error context stack */
1092 state.ctx = ctx;
1093 state.callback_name = "change";
1094 state.report_location = change->lsn;
1096 errcallback.arg = &state;
1097 errcallback.previous = error_context_stack;
1098 error_context_stack = &errcallback;
1099
1100 /* set output state */
1101 ctx->accept_writes = true;
1102 ctx->write_xid = txn->xid;
1103
1104 /*
1105 * Report this change's lsn so replies from clients can give an up-to-date
1106 * answer. This won't ever be enough (and shouldn't be!) to confirm
1107 * receipt of this transaction, but it might allow another transaction's
1108 * commit to be confirmed with one message.
1109 */
1110 ctx->write_location = change->lsn;
1111
1112 ctx->end_xact = false;
1113
1114 ctx->callbacks.change_cb(ctx, txn, relation, change);
1115
1116 /* Pop the error context stack */
1117 error_context_stack = errcallback.previous;
1118}
LogicalDecodeChangeCB change_cb

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert, ErrorContextCallback::callback, LogicalDecodingContext::callbacks, OutputPluginCallbacks::change_cb, LogicalDecodingContext::end_xact, error_context_stack, LogicalDecodingContext::fast_forward, ReorderBufferChange::lsn, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ CheckLogicalDecodingRequirements()

void CheckLogicalDecodingRequirements ( bool  repack)

Definition at line 111 of file logical.c.

112{
114
115 /*
116 * NB: Adding a new requirement likely means that RestoreSlotFromDisk()
117 * needs the same check.
118 */
119
123 errmsg("logical decoding requires a database connection")));
124
125 /* CheckSlotRequirements() has already checked if wal_level >= 'replica' */
127
128 /* Check if logical decoding is available on standby */
132 errmsg("logical decoding on standby requires \"effective_wal_level\" >= \"logical\" on the primary"),
133 errhint("Set \"wal_level\" >= \"logical\" or create at least one logical slot when \"wal_level\" = \"replica\".")));
134}
int errhint(const char *fmt,...) pg_attribute_printf(1
Oid MyDatabaseId
Definition globals.c:96
bool IsLogicalDecodingEnabled(void)
Definition logicalctl.c:202
#define InvalidOid
void CheckSlotRequirements(bool repack)
Definition slot.c:1665
bool RecoveryInProgress(void)
Definition xlog.c:6830
int wal_level
Definition xlog.c:138
@ WAL_LEVEL_REPLICA
Definition xlog.h:77

References Assert, CheckSlotRequirements(), ereport, errcode(), errhint(), errmsg, ERROR, fb(), InvalidOid, IsLogicalDecodingEnabled(), MyDatabaseId, RecoveryInProgress(), wal_level, and WAL_LEVEL_REPLICA.

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

◆ commit_cb_wrapper()

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

Definition at line 862 of file logical.c.

864{
867 ErrorContextCallback errcallback;
868
869 Assert(!ctx->fast_forward);
870
871 /* Push callback + info on the error context stack */
872 state.ctx = ctx;
873 state.callback_name = "commit";
874 state.report_location = txn->final_lsn; /* beginning of commit record */
876 errcallback.arg = &state;
877 errcallback.previous = error_context_stack;
878 error_context_stack = &errcallback;
879
880 /* set output state */
881 ctx->accept_writes = true;
882 ctx->write_xid = txn->xid;
883 ctx->write_location = txn->end_lsn; /* points to the end of the record */
884 ctx->end_xact = true;
885
886 /* do the actual work: call callback */
887 ctx->callbacks.commit_cb(ctx, txn, commit_lsn);
888
889 /* Pop the error context stack */
890 error_context_stack = errcallback.previous;
891}
LogicalDecodeCommitCB commit_cb
XLogRecPtr final_lsn

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert, ErrorContextCallback::callback, LogicalDecodingContext::callbacks, OutputPluginCallbacks::commit_cb, ReorderBufferTXN::end_lsn, LogicalDecodingContext::end_xact, error_context_stack, LogicalDecodingContext::fast_forward, ReorderBufferTXN::final_lsn, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ commit_prepared_cb_wrapper()

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

Definition at line 990 of file logical.c.

992{
995 ErrorContextCallback errcallback;
996
997 Assert(!ctx->fast_forward);
998
999 /* We're only supposed to call this when two-phase commits are supported */
1000 Assert(ctx->twophase);
1001
1002 /* Push callback + info on the error context stack */
1003 state.ctx = ctx;
1004 state.callback_name = "commit_prepared";
1005 state.report_location = txn->final_lsn; /* beginning of commit record */
1007 errcallback.arg = &state;
1008 errcallback.previous = error_context_stack;
1009 error_context_stack = &errcallback;
1010
1011 /* set output state */
1012 ctx->accept_writes = true;
1013 ctx->write_xid = txn->xid;
1014 ctx->write_location = txn->end_lsn; /* points to the end of the record */
1015 ctx->end_xact = true;
1016
1017 /*
1018 * If the plugin support two-phase commits then commit prepared callback
1019 * is mandatory
1020 */
1021 if (ctx->callbacks.commit_prepared_cb == NULL)
1022 ereport(ERROR,
1024 errmsg("logical replication at prepare time requires a %s callback",
1025 "commit_prepared_cb")));
1026
1027 /* do the actual work: call callback */
1028 ctx->callbacks.commit_prepared_cb(ctx, txn, commit_lsn);
1029
1030 /* Pop the error context stack */
1031 error_context_stack = errcallback.previous;
1032}
LogicalDecodeCommitPreparedCB commit_prepared_cb

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert, ErrorContextCallback::callback, LogicalDecodingContext::callbacks, OutputPluginCallbacks::commit_prepared_cb, ReorderBufferTXN::end_lsn, LogicalDecodingContext::end_xact, ereport, errcode(), errmsg, ERROR, error_context_stack, LogicalDecodingContext::fast_forward, fb(), ReorderBufferTXN::final_lsn, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, LogicalDecodingContext::twophase, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ CreateDecodingContext()

LogicalDecodingContext * CreateDecodingContext ( XLogRecPtr  start_lsn,
List output_plugin_options,
bool  fast_forward,
XLogReaderRoutine xl_routine,
LogicalOutputPluginWriterPrepareWrite  prepare_write,
LogicalOutputPluginWriterWrite  do_write,
LogicalOutputPluginWriterUpdateProgress  update_progress 
)

Definition at line 494 of file logical.c.

501{
503 ReplicationSlot *slot;
505
506 /* shorter lines... */
507 slot = MyReplicationSlot;
508
509 /* first some sanity checks that are unlikely to be violated */
510 if (slot == NULL)
511 elog(ERROR, "cannot perform logical decoding without an acquired slot");
512
513 /* make sure the passed slot is suitable, these are user facing errors */
514 if (SlotIsPhysical(slot))
517 errmsg("cannot use physical replication slot for logical decoding")));
518
519 /*
520 * We need to access the system tables during decoding to build the
521 * logical changes unless we are in fast_forward mode where no changes are
522 * generated.
523 */
524 if (slot->data.database != MyDatabaseId && !fast_forward)
527 errmsg("replication slot \"%s\" was not created in this database",
528 NameStr(slot->data.name))));
529
530 /*
531 * The slots being synced from the primary can't be used for decoding as
532 * they are used after failover. However, we do allow advancing the LSNs
533 * during the synchronization of slots. See update_local_synced_slot.
534 */
538 errmsg("cannot use replication slot \"%s\" for logical decoding",
539 NameStr(slot->data.name)),
540 errdetail("This replication slot is being synchronized from the primary server."),
541 errhint("Specify another replication slot."));
542
543 /* slot must be valid to allow decoding */
546
547 if (!XLogRecPtrIsValid(start_lsn))
548 {
549 /* continue from last position */
550 start_lsn = slot->data.confirmed_flush;
551 }
552 else if (start_lsn < slot->data.confirmed_flush)
553 {
554 /*
555 * It might seem like we should error out in this case, but it's
556 * pretty common for a client to acknowledge a LSN it doesn't have to
557 * do anything for, and thus didn't store persistently, because the
558 * xlog records didn't result in anything relevant for logical
559 * decoding. Clients have to be able to do that to support synchronous
560 * replication.
561 *
562 * Starting at a different LSN than requested might not catch certain
563 * kinds of client errors; so the client may wish to check that
564 * confirmed_flush_lsn matches its expectations.
565 */
566 elog(LOG, "%X/%08X has been already streamed, forwarding to %X/%08X",
567 LSN_FORMAT_ARGS(start_lsn),
569
570 start_lsn = slot->data.confirmed_flush;
571 }
572
573 ctx = StartupDecodingContext(output_plugin_options,
574 start_lsn, InvalidTransactionId, false,
575 fast_forward, false, xl_routine, prepare_write,
576 do_write, update_progress);
577
578 /* call output plugin initialization callback */
580 if (ctx->callbacks.startup_cb != NULL)
581 startup_cb_wrapper(ctx, &ctx->options, false);
583
584 /*
585 * We allow decoding of prepared transactions when the two_phase is
586 * enabled at the time of slot creation, or when the two_phase option is
587 * given at the streaming start, provided the plugin supports all the
588 * callbacks for two-phase.
589 */
590 ctx->twophase &= (slot->data.two_phase || ctx->twophase_opt_given);
591
592 /* Mark slot to allow two_phase decoding if not already marked */
593 if (ctx->twophase && !slot->data.two_phase)
594 {
595 SpinLockAcquire(&slot->mutex);
596 slot->data.two_phase = true;
597 slot->data.two_phase_at = start_lsn;
598 SpinLockRelease(&slot->mutex);
602 }
603
605
607 (errmsg("starting logical decoding for slot \"%s\"",
608 NameStr(slot->data.name)),
609 errdetail("Streaming transactions committing after %X/%08X, reading WAL from %X/%08X.",
612
613 return ctx;
614}
#define NameStr(name)
Definition c.h:835
#define LOG
Definition elog.h:32
int errdetail(const char *fmt,...) pg_attribute_printf(1
#define elog(elevel,...)
Definition elog.h:228
static void startup_cb_wrapper(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
Definition logical.c:770
static LogicalDecodingContext * StartupDecodingContext(List *output_plugin_options, XLogRecPtr start_lsn, TransactionId xmin_horizon, bool need_full_snapshot, bool fast_forward, bool in_create, XLogReaderRoutine *xl_routine, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
Definition logical.c:141
#define LogicalDecodingLogLevel()
Definition logical.h:175
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
const void * data
void ReplicationSlotMarkDirty(void)
Definition slot.c:1184
ReplicationSlot * MyReplicationSlot
Definition slot.c:158
void ReplicationSlotSave(void)
Definition slot.c:1166
#define SlotIsPhysical(slot)
Definition slot.h:287
@ RS_INVAL_NONE
Definition slot.h:60
bool IsSyncingReplicationSlots(void)
Definition slotsync.c:1909
void SnapBuildSetTwoPhaseAt(SnapBuild *builder, XLogRecPtr ptr)
Definition snapbuild.c:313
static void SpinLockRelease(volatile slock_t *lock)
Definition spin.h:62
static void SpinLockAcquire(volatile slock_t *lock)
Definition spin.h:56
OutputPluginOptions options
Definition logical.h:54
MemoryContext context
Definition logical.h:36
struct SnapBuild * snapshot_builder
Definition logical.h:44
struct ReorderBuffer * reorder
Definition logical.h:43
LogicalDecodeStartupCB startup_cb
ReplicationSlotInvalidationCause invalidated
Definition slot.h:128
slock_t mutex
Definition slot.h:183
ReplicationSlotPersistentData data
Definition slot.h:213
#define InvalidTransactionId
Definition transam.h:31
#define XLogRecPtrIsValid(r)
Definition xlogdefs.h:29
#define LSN_FORMAT_ARGS(lsn)
Definition xlogdefs.h:47

References Assert, LogicalDecodingContext::callbacks, ReplicationSlotPersistentData::confirmed_flush, LogicalDecodingContext::context, ReplicationSlot::data, data, ReplicationSlotPersistentData::database, elog, ereport, errcode(), errdetail(), errhint(), errmsg, ERROR, fb(), ReplicationSlotPersistentData::invalidated, InvalidTransactionId, IsSyncingReplicationSlots(), LOG, LogicalDecodingLogLevel, LSN_FORMAT_ARGS, MemoryContextSwitchTo(), ReplicationSlot::mutex, MyDatabaseId, MyReplicationSlot, ReplicationSlotPersistentData::name, NameStr, LogicalDecodingContext::options, ReorderBuffer::output_rewrites, OutputPluginOptions::receive_rewrites, RecoveryInProgress(), LogicalDecodingContext::reorder, ReplicationSlotMarkDirty(), ReplicationSlotSave(), ReplicationSlotPersistentData::restart_lsn, RS_INVAL_NONE, SlotIsPhysical, SnapBuildSetTwoPhaseAt(), LogicalDecodingContext::snapshot_builder, SpinLockAcquire(), SpinLockRelease(), OutputPluginCallbacks::startup_cb, startup_cb_wrapper(), StartupDecodingContext(), ReplicationSlotPersistentData::synced, ReplicationSlotPersistentData::two_phase, ReplicationSlotPersistentData::two_phase_at, LogicalDecodingContext::twophase, LogicalDecodingContext::twophase_opt_given, and XLogRecPtrIsValid.

Referenced by LogicalReplicationSlotCheckPendingWal(), LogicalSlotAdvanceAndCheckSnapState(), pg_logical_slot_get_changes_guts(), and StartLogicalReplication().

◆ CreateInitDecodingContext()

LogicalDecodingContext * CreateInitDecodingContext ( const char plugin,
List output_plugin_options,
bool  need_full_snapshot,
bool  for_repack,
XLogRecPtr  restart_lsn,
XLogReaderRoutine xl_routine,
LogicalOutputPluginWriterPrepareWrite  prepare_write,
LogicalOutputPluginWriterWrite  do_write,
LogicalOutputPluginWriterUpdateProgress  update_progress 
)

Definition at line 325 of file logical.c.

334{
336 ReplicationSlot *slot;
340
341 /*
342 * On a standby, this check is also required while creating the slot.
343 * Check the comments in the function.
344 */
346
347 /* shorter lines... */
348 slot = MyReplicationSlot;
349
350 /* first some sanity checks that are unlikely to be violated */
351 if (slot == NULL)
352 elog(ERROR, "cannot perform logical decoding without an acquired slot");
353
354 if (plugin == NULL)
355 elog(ERROR, "cannot initialize logical decoding without a specified plugin");
356
357 /* Make sure the passed slot is suitable. These are user facing errors. */
358 if (SlotIsPhysical(slot))
361 errmsg("cannot use physical replication slot for logical decoding")));
362
363 if (slot->data.database != MyDatabaseId)
366 errmsg("replication slot \"%s\" was not created in this database",
367 NameStr(slot->data.name))));
368
369 if (IsTransactionState() &&
373 errmsg("cannot create logical replication slot in transaction that has performed writes")));
374
375 /*
376 * Register output plugin name with slot. We need the mutex to avoid
377 * concurrent reading of a partially copied string. But we don't want any
378 * complicated code while holding a spinlock, so do namestrcpy() outside.
379 */
381 SpinLockAcquire(&slot->mutex);
382 slot->data.plugin = plugin_name;
383 SpinLockRelease(&slot->mutex);
384
385 if (!XLogRecPtrIsValid(restart_lsn))
387 else
388 {
389 SpinLockAcquire(&slot->mutex);
390 slot->data.restart_lsn = restart_lsn;
391 SpinLockRelease(&slot->mutex);
392 }
393
394 /* ----
395 * This is a bit tricky: We need to determine a safe xmin horizon to start
396 * decoding from, to avoid starting from a running xacts record referring
397 * to xids whose rows have been vacuumed or pruned
398 * already. GetOldestSafeDecodingTransactionId() returns such a value, but
399 * without further interlock its return value might immediately be out of
400 * date.
401 *
402 * So we have to acquire both the ReplicationSlotControlLock and the
403 * ProcArrayLock to prevent concurrent computation and update of new xmin
404 * horizons by other backends, get the safe decoding xid, and inform the
405 * slot machinery about the new limit. Once that's done both locks can be
406 * released as the slot machinery now is protecting against vacuum.
407 *
408 * Note that, temporarily, the data, not just the catalog, xmin has to be
409 * reserved if a data snapshot is to be exported. Otherwise the initial
410 * data snapshot created here is not guaranteed to be valid. After that
411 * the data xmin doesn't need to be managed anymore and the global xmin
412 * should be recomputed. As we are fine with losing the pegged data xmin
413 * after crash - no chance a snapshot would get exported anymore - we can
414 * get away with just setting the slot's
415 * effective_xmin. ReplicationSlotRelease will reset it again.
416 *
417 * ----
418 */
421
423
424 SpinLockAcquire(&slot->mutex);
429 SpinLockRelease(&slot->mutex);
430
432
435
438
439 ctx = StartupDecodingContext(NIL, restart_lsn, xmin_horizon,
440 need_full_snapshot, false, true,
441 xl_routine, prepare_write, do_write,
442 update_progress);
443
444 /* call output plugin initialization callback */
446 if (ctx->callbacks.startup_cb != NULL)
447 startup_cb_wrapper(ctx, &ctx->options, true);
449
450 /*
451 * We allow decoding of prepared transactions when the two_phase is
452 * enabled at the time of slot creation, or when the two_phase option is
453 * given at the streaming start, provided the plugin supports all the
454 * callbacks for two-phase.
455 */
456 ctx->twophase &= slot->data.two_phase;
457
459
460 return ctx;
461}
uint32 TransactionId
Definition c.h:736
void CheckLogicalDecodingRequirements(bool repack)
Definition logical.c:111
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1150
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1767
@ LW_EXCLUSIVE
Definition lwlock.h:104
void namestrcpy(Name name, const char *str)
Definition name.c:233
#define NIL
Definition pg_list.h:68
static const char * plugin
TransactionId GetOldestSafeDecodingTransactionId(bool catalogOnly)
Definition procarray.c:2919
void ReplicationSlotReserveWal(void)
Definition slot.c:1711
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition slot.c:1226
TransactionId catalog_xmin
Definition slot.h:122
TransactionId effective_catalog_xmin
Definition slot.h:210
TransactionId effective_xmin
Definition slot.h:209
Definition c.h:830
bool IsTransactionState(void)
Definition xact.c:389
TransactionId GetTopTransactionIdIfAny(void)
Definition xact.c:443

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

Referenced by create_logical_replication_slot(), CreateReplicationSlot(), and repack_setup_logical_decoding().

◆ DecodingContextFindStartpoint()

void DecodingContextFindStartpoint ( LogicalDecodingContext ctx)

Definition at line 629 of file logical.c.

630{
631 ReplicationSlot *slot = ctx->slot;
632
633 /* Initialize from where to start reading WAL. */
635
636 elog(DEBUG1, "searching for logical decoding starting point, starting at %X/%08X",
638
639 /* Wait for a consistent starting point */
640 for (;;)
641 {
642 XLogRecord *record;
643 char *err = NULL;
644
645 /* the read_page callback waits for new WAL */
646 record = XLogReadRecord(ctx->reader, &err);
647 if (err)
648 elog(ERROR, "could not find logical decoding starting point: %s", err);
649 if (!record)
650 elog(ERROR, "could not find logical decoding starting point");
651
653
654 /* only continue till we found a consistent spot */
655 if (DecodingContextReady(ctx))
656 break;
657
659 }
660
661 SpinLockAcquire(&slot->mutex);
662 slot->data.confirmed_flush = ctx->reader->EndRecPtr;
663 if (slot->data.two_phase)
664 slot->data.two_phase_at = ctx->reader->EndRecPtr;
665 SpinLockRelease(&slot->mutex);
666}
void LogicalDecodingProcessRecord(LogicalDecodingContext *ctx, XLogReaderState *record)
Definition decode.c:89
#define DEBUG1
Definition elog.h:31
void err(int eval, const char *fmt,...)
Definition err.c:43
bool DecodingContextReady(LogicalDecodingContext *ctx)
Definition logical.c:620
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:125
XLogReaderState * reader
Definition logical.h:42
ReplicationSlot * slot
Definition logical.h:39
XLogRecPtr EndRecPtr
Definition xlogreader.h:206
XLogRecord * XLogReadRecord(XLogReaderState *state, char **errormsg)
Definition xlogreader.c:391
void XLogBeginRead(XLogReaderState *state, XLogRecPtr RecPtr)
Definition xlogreader.c:233

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

Referenced by create_logical_replication_slot(), CreateReplicationSlot(), and repack_setup_logical_decoding().

◆ DecodingContextReady()

bool DecodingContextReady ( LogicalDecodingContext ctx)

Definition at line 620 of file logical.c.

621{
623}
SnapBuildState SnapBuildCurrentState(SnapBuild *builder)
Definition snapbuild.c:295
@ SNAPBUILD_CONSISTENT
Definition snapbuild.h:58

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

Referenced by DecodingContextFindStartpoint(), and LogicalSlotAdvanceAndCheckSnapState().

◆ filter_by_origin_cb_wrapper()

bool filter_by_origin_cb_wrapper ( LogicalDecodingContext ctx,
ReplOriginId  origin_id 
)

Definition at line 1195 of file logical.c.

1196{
1198 ErrorContextCallback errcallback;
1199 bool ret;
1200
1201 Assert(!ctx->fast_forward);
1202
1203 /* Push callback + info on the error context stack */
1204 state.ctx = ctx;
1205 state.callback_name = "filter_by_origin";
1206 state.report_location = InvalidXLogRecPtr;
1208 errcallback.arg = &state;
1209 errcallback.previous = error_context_stack;
1210 error_context_stack = &errcallback;
1211
1212 /* set output state */
1213 ctx->accept_writes = false;
1214 ctx->end_xact = false;
1215
1216 /* do the actual work: call callback */
1217 ret = ctx->callbacks.filter_by_origin_cb(ctx, origin_id);
1218
1219 /* Pop the error context stack */
1220 error_context_stack = errcallback.previous;
1221
1222 return ret;
1223}
LogicalDecodeFilterByOriginCB filter_by_origin_cb
#define InvalidXLogRecPtr
Definition xlogdefs.h:28

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert, ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, error_context_stack, LogicalDecodingContext::fast_forward, OutputPluginCallbacks::filter_by_origin_cb, InvalidXLogRecPtr, output_plugin_error_callback(), and ErrorContextCallback::previous.

Referenced by FilterByOrigin().

◆ filter_prepare_cb_wrapper()

bool filter_prepare_cb_wrapper ( LogicalDecodingContext ctx,
TransactionId  xid,
const char gid 
)

Definition at line 1163 of file logical.c.

1165{
1167 ErrorContextCallback errcallback;
1168 bool ret;
1169
1170 Assert(!ctx->fast_forward);
1171
1172 /* Push callback + info on the error context stack */
1173 state.ctx = ctx;
1174 state.callback_name = "filter_prepare";
1175 state.report_location = InvalidXLogRecPtr;
1177 errcallback.arg = &state;
1178 errcallback.previous = error_context_stack;
1179 error_context_stack = &errcallback;
1180
1181 /* set output state */
1182 ctx->accept_writes = false;
1183 ctx->end_xact = false;
1184
1185 /* do the actual work: call callback */
1186 ret = ctx->callbacks.filter_prepare_cb(ctx, xid, gid);
1187
1188 /* Pop the error context stack */
1189 error_context_stack = errcallback.previous;
1190
1191 return ret;
1192}
LogicalDecodeFilterPrepareCB filter_prepare_cb

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert, ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, error_context_stack, LogicalDecodingContext::fast_forward, OutputPluginCallbacks::filter_prepare_cb, InvalidXLogRecPtr, output_plugin_error_callback(), and ErrorContextCallback::previous.

Referenced by FilterPrepare().

◆ FreeDecodingContext()

◆ LoadOutputPlugin()

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

Definition at line 729 of file logical.c.

730{
732
734 load_external_function(plugin, "_PG_output_plugin_init", false, NULL);
735
736 if (plugin_init == NULL)
737 elog(ERROR, "output plugins have to declare the _PG_output_plugin_init symbol");
738
739 /* ask the output plugin to fill the callback struct */
740 plugin_init(callbacks);
741
742 if (callbacks->begin_cb == NULL)
743 elog(ERROR, "output plugins have to register a begin callback");
744 if (callbacks->change_cb == NULL)
745 elog(ERROR, "output plugins have to register a change callback");
746 if (callbacks->commit_cb == NULL)
747 elog(ERROR, "output plugins have to register a commit callback");
748}
void * load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
Definition dfmgr.c:95
void(* LogicalOutputPluginInit)(struct OutputPluginCallbacks *cb)

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

Referenced by StartupDecodingContext().

◆ LogicalConfirmReceivedLocation()

void LogicalConfirmReceivedLocation ( XLogRecPtr  lsn)

Definition at line 1816 of file logical.c.

1817{
1819
1820 /* Do an unlocked check for candidate_lsn first. */
1823 {
1824 bool updated_xmin = false;
1825 bool updated_restart = false;
1826 XLogRecPtr restart_lsn pg_attribute_unused();
1827
1829
1830 /* remember the old restart lsn */
1831 restart_lsn = MyReplicationSlot->data.restart_lsn;
1832
1833 /*
1834 * Prevent moving the confirmed_flush backwards, as this could lead to
1835 * data duplication issues caused by replicating already replicated
1836 * changes.
1837 *
1838 * This can happen when a client acknowledges an LSN it doesn't have
1839 * to do anything for, and thus didn't store persistently. After a
1840 * restart, the client can send the prior LSN that it stored
1841 * persistently as an acknowledgement, but we need to ignore such an
1842 * LSN. See similar case handling in CreateDecodingContext.
1843 */
1846
1847 /* if we're past the location required for bumping xmin, do so */
1850 {
1851 /*
1852 * We have to write the changed xmin to disk *before* we change
1853 * the in-memory value, otherwise after a crash we wouldn't know
1854 * that some catalog tuples might have been removed already.
1855 *
1856 * Ensure that by first writing to ->xmin and only update
1857 * ->effective_xmin once the new state is synced to disk. After a
1858 * crash ->effective_xmin is set to ->xmin.
1859 */
1862 {
1866 updated_xmin = true;
1867 }
1868 }
1869
1872 {
1874
1878 updated_restart = true;
1879 }
1880
1882
1883 /* first write new xmin to disk, so we know what's up after a crash */
1885 {
1886#ifdef USE_INJECTION_POINTS
1888 seg2;
1889
1890 XLByteToSeg(restart_lsn, seg1, wal_segment_size);
1892
1893 /* trigger injection point, but only if segment changes */
1894 if (seg1 != seg2)
1895 INJECTION_POINT("logical-replication-slot-advance-segment", NULL);
1896#endif
1897
1900 elog(DEBUG1, "updated xmin: %u restart: %u", updated_xmin, updated_restart);
1901 }
1902
1903 /*
1904 * Now the new xmin is safely on disk, we can let the global value
1905 * advance. We do not take ProcArrayLock or similar since we only
1906 * advance xmin here and there's not much harm done by a concurrent
1907 * computation missing that.
1908 */
1909 if (updated_xmin)
1910 {
1914
1917 }
1918 }
1919 else
1920 {
1922
1923 /*
1924 * Prevent moving the confirmed_flush backwards. See comments above
1925 * for the details.
1926 */
1929
1931 }
1932}
#define pg_attribute_unused()
Definition c.h:149
#define INJECTION_POINT(name, arg)
void ReplicationSlotsComputeRequiredLSN(void)
Definition slot.c:1308
XLogRecPtr candidate_xmin_lsn
Definition slot.h:229
XLogRecPtr candidate_restart_valid
Definition slot.h:230
XLogRecPtr candidate_restart_lsn
Definition slot.h:231
TransactionId candidate_catalog_xmin
Definition slot.h:228
#define TransactionIdIsValid(xid)
Definition transam.h:41
int wal_segment_size
Definition xlog.c:150
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)
uint64 XLogRecPtr
Definition xlogdefs.h:21
uint64 XLogSegNo
Definition xlogdefs.h:52

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, fb(), INJECTION_POINT, InvalidTransactionId, InvalidXLogRecPtr, ReplicationSlot::mutex, MyReplicationSlot, pg_attribute_unused, ReplicationSlotMarkDirty(), ReplicationSlotSave(), ReplicationSlotsComputeRequiredLSN(), ReplicationSlotsComputeRequiredXmin(), ReplicationSlotPersistentData::restart_lsn, SpinLockAcquire(), SpinLockRelease(), TransactionIdIsValid, wal_segment_size, XLByteToSeg, and XLogRecPtrIsValid.

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

◆ LogicalIncreaseRestartDecodingForSlot()

void LogicalIncreaseRestartDecodingForSlot ( XLogRecPtr  current_lsn,
XLogRecPtr  restart_lsn 
)

Definition at line 1740 of file logical.c.

1741{
1742 bool updated_lsn = false;
1743 ReplicationSlot *slot;
1744
1745 slot = MyReplicationSlot;
1746
1747 Assert(slot != NULL);
1748 Assert(XLogRecPtrIsValid(restart_lsn));
1750
1751 SpinLockAcquire(&slot->mutex);
1752
1753 /* don't overwrite if have a newer restart lsn */
1754 if (restart_lsn <= slot->data.restart_lsn)
1755 {
1756 SpinLockRelease(&slot->mutex);
1757 }
1758
1759 /*
1760 * We might have already flushed far enough to directly accept this lsn,
1761 * in this case there is no need to check for existing candidate LSNs
1762 */
1763 else if (current_lsn <= slot->data.confirmed_flush)
1764 {
1766 slot->candidate_restart_lsn = restart_lsn;
1767 SpinLockRelease(&slot->mutex);
1768
1769 /* our candidate can directly be used */
1770 updated_lsn = true;
1771 }
1772
1773 /*
1774 * Only increase if the previous values have been applied, otherwise we
1775 * might never end up updating if the receiver acks too slowly. A missed
1776 * value here will just cause some extra effort after reconnecting.
1777 */
1779 {
1781 slot->candidate_restart_lsn = restart_lsn;
1782 SpinLockRelease(&slot->mutex);
1783
1784 elog(DEBUG1, "got new restart lsn %X/%08X at %X/%08X",
1785 LSN_FORMAT_ARGS(restart_lsn),
1787 }
1788 else
1789 {
1790 XLogRecPtr candidate_restart_lsn;
1791 XLogRecPtr candidate_restart_valid;
1792 XLogRecPtr confirmed_flush;
1793
1794 candidate_restart_lsn = slot->candidate_restart_lsn;
1795 candidate_restart_valid = slot->candidate_restart_valid;
1796 confirmed_flush = slot->data.confirmed_flush;
1797 SpinLockRelease(&slot->mutex);
1798
1799 elog(DEBUG1, "failed to increase restart lsn: proposed %X/%08X, after %X/%08X, current candidate %X/%08X, current after %X/%08X, flushed up to %X/%08X",
1800 LSN_FORMAT_ARGS(restart_lsn),
1802 LSN_FORMAT_ARGS(candidate_restart_lsn),
1803 LSN_FORMAT_ARGS(candidate_restart_valid),
1804 LSN_FORMAT_ARGS(confirmed_flush));
1805 }
1806
1807 /* candidates are already valid with the current flush position, apply */
1808 if (updated_lsn)
1810}
void LogicalConfirmReceivedLocation(XLogRecPtr lsn)
Definition logical.c:1816

References Assert, ReplicationSlot::candidate_restart_lsn, ReplicationSlot::candidate_restart_valid, ReplicationSlotPersistentData::confirmed_flush, ReplicationSlot::data, data, DEBUG1, elog, fb(), LogicalConfirmReceivedLocation(), LSN_FORMAT_ARGS, ReplicationSlot::mutex, MyReplicationSlot, SpinLockAcquire(), SpinLockRelease(), and XLogRecPtrIsValid.

Referenced by SnapBuildProcessRunningXacts().

◆ LogicalIncreaseXminForSlot()

void LogicalIncreaseXminForSlot ( XLogRecPtr  current_lsn,
TransactionId  xmin 
)

Definition at line 1672 of file logical.c.

1673{
1674 bool updated_xmin = false;
1675 ReplicationSlot *slot;
1676 bool got_new_xmin = false;
1677
1678 slot = MyReplicationSlot;
1679
1680 Assert(slot != NULL);
1681
1682 SpinLockAcquire(&slot->mutex);
1683
1684 /*
1685 * don't overwrite if we already have a newer xmin. This can happen if we
1686 * restart decoding in a slot.
1687 */
1689 {
1690 }
1691
1692 /*
1693 * If the client has already confirmed up to this lsn, we directly can
1694 * mark this as accepted. This can happen if we restart decoding in a
1695 * slot.
1696 */
1697 else if (current_lsn <= slot->data.confirmed_flush)
1698 {
1699 slot->candidate_catalog_xmin = xmin;
1701
1702 /* our candidate can directly be used */
1703 updated_xmin = true;
1704 }
1705
1706 /*
1707 * Only increase if the previous values have been applied, otherwise we
1708 * might never end up updating if the receiver acks too slowly.
1709 */
1710 else if (!XLogRecPtrIsValid(slot->candidate_xmin_lsn))
1711 {
1712 slot->candidate_catalog_xmin = xmin;
1714
1715 /*
1716 * Log new xmin at an appropriate log level after releasing the
1717 * spinlock.
1718 */
1719 got_new_xmin = true;
1720 }
1721 SpinLockRelease(&slot->mutex);
1722
1723 if (got_new_xmin)
1724 elog(DEBUG1, "got new catalog xmin %u at %X/%08X", xmin,
1726
1727 /* candidate already valid with the current flush position, apply */
1728 if (updated_xmin)
1730}
static bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition transam.h:282

References Assert, ReplicationSlot::candidate_catalog_xmin, ReplicationSlot::candidate_xmin_lsn, ReplicationSlotPersistentData::catalog_xmin, ReplicationSlotPersistentData::confirmed_flush, ReplicationSlot::data, data, DEBUG1, elog, fb(), LogicalConfirmReceivedLocation(), LSN_FORMAT_ARGS, ReplicationSlot::mutex, MyReplicationSlot, SpinLockAcquire(), SpinLockRelease(), TransactionIdPrecedesOrEquals(), and XLogRecPtrIsValid.

Referenced by SnapBuildProcessRunningXacts().

◆ LogicalReplicationSlotCheckPendingWal()

XLogRecPtr LogicalReplicationSlotCheckPendingWal ( XLogRecPtr  end_of_wal,
XLogRecPtr  scan_cutoff_lsn 
)

Definition at line 2003 of file logical.c.

2005{
2007
2009 Assert(end_of_wal >= scan_cutoff_lsn);
2010
2011 PG_TRY();
2012 {
2014
2015 /*
2016 * Create our decoding context in fast_forward mode, passing start_lsn
2017 * as InvalidXLogRecPtr, so that we start processing from the slot's
2018 * confirmed_flush.
2019 */
2021 NIL,
2022 true, /* fast_forward */
2023 XL_ROUTINE(.page_read = read_local_xlog_page,
2024 .segment_open = wal_segment_open,
2025 .segment_close = wal_segment_close),
2026 NULL, NULL, NULL);
2027
2028 /*
2029 * Start reading at the slot's restart_lsn, which we know points to a
2030 * valid record.
2031 */
2033
2034 /* Invalidate non-timetravel entries */
2036
2037 while (ctx->reader->EndRecPtr < end_of_wal)
2038 {
2039 XLogRecord *record;
2040 char *errm = NULL;
2041
2042 record = XLogReadRecord(ctx->reader, &errm);
2043
2044 if (errm)
2045 elog(ERROR, "could not find record for logical decoding: %s", errm);
2046
2047 if (record != NULL)
2049
2050 if (ctx->processing_required)
2051 {
2053
2054 /*
2055 * If we find a decodable WAL after the scan_cutoff_lsn point,
2056 * we can terminate the scan early.
2057 */
2059 break;
2060
2061 /* Reset the flag and continue checking */
2062 ctx->processing_required = false;
2063 }
2064
2066 }
2067
2068 /* Clean up */
2071 }
2072 PG_CATCH();
2073 {
2074 /* clear all timetravel entries */
2076
2077 PG_RE_THROW();
2078 }
2079 PG_END_TRY();
2080
2081 return last_pending_wal;
2082}
#define PG_RE_THROW()
Definition elog.h:407
#define PG_TRY(...)
Definition elog.h:374
#define PG_END_TRY(...)
Definition elog.h:399
#define PG_CATCH(...)
Definition elog.h:384
void InvalidateSystemCaches(void)
Definition inval.c:916
void FreeDecodingContext(LogicalDecodingContext *ctx)
Definition logical.c:673
LogicalDecodingContext * CreateDecodingContext(XLogRecPtr start_lsn, List *output_plugin_options, bool fast_forward, XLogReaderRoutine *xl_routine, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
Definition logical.c:494
XLogRecPtr ReadRecPtr
Definition xlogreader.h:205
#define XL_ROUTINE(...)
Definition xlogreader.h:117
void wal_segment_close(XLogReaderState *state)
Definition xlogutils.c:831
void wal_segment_open(XLogReaderState *state, XLogSegNo nextSegNo, TimeLineID *tli_p)
Definition xlogutils.c:806
int read_local_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetRecPtr, char *cur_page)
Definition xlogutils.c:845

References Assert, CHECK_FOR_INTERRUPTS, CreateDecodingContext(), ReplicationSlot::data, elog, XLogReaderState::EndRecPtr, ERROR, fb(), FreeDecodingContext(), InvalidateSystemCaches(), InvalidXLogRecPtr, LogicalDecodingProcessRecord(), MyReplicationSlot, NIL, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, LogicalDecodingContext::processing_required, read_local_xlog_page(), LogicalDecodingContext::reader, XLogReaderState::ReadRecPtr, ReplicationSlotPersistentData::restart_lsn, wal_segment_close(), wal_segment_open(), XL_ROUTINE, XLogBeginRead(), and XLogReadRecord().

Referenced by binary_upgrade_check_logical_slot_pending_wal().

◆ LogicalSlotAdvanceAndCheckSnapState()

XLogRecPtr LogicalSlotAdvanceAndCheckSnapState ( XLogRecPtr  moveto,
bool found_consistent_snapshot 
)

Definition at line 2099 of file logical.c.

2101{
2105
2107
2110
2111 PG_TRY();
2112 {
2113 /*
2114 * Create our decoding context in fast_forward mode, passing start_lsn
2115 * as InvalidXLogRecPtr, so that we start processing from my slot's
2116 * confirmed_flush.
2117 */
2119 NIL,
2120 true, /* fast_forward */
2121 XL_ROUTINE(.page_read = read_local_xlog_page,
2122 .segment_open = wal_segment_open,
2123 .segment_close = wal_segment_close),
2124 NULL, NULL, NULL);
2125
2126 /*
2127 * Wait for specified streaming replication standby servers (if any)
2128 * to confirm receipt of WAL up to moveto lsn.
2129 */
2131
2132 /*
2133 * Start reading at the slot's restart_lsn, which we know to point to
2134 * a valid record.
2135 */
2137
2138 /* invalidate non-timetravel entries */
2140
2141 /* Decode records until we reach the requested target */
2142 while (ctx->reader->EndRecPtr < moveto)
2143 {
2144 char *errm = NULL;
2145 XLogRecord *record;
2146
2147 /*
2148 * Read records. No changes are generated in fast_forward mode,
2149 * but snapbuilder/slot statuses are updated properly.
2150 */
2151 record = XLogReadRecord(ctx->reader, &errm);
2152 if (errm)
2153 elog(ERROR, "could not find record while advancing replication slot: %s",
2154 errm);
2155
2156 /*
2157 * Process the record. Storage-level changes are ignored in
2158 * fast_forward mode, but other modules (such as snapbuilder)
2159 * might still have critical updates to do.
2160 */
2161 if (record)
2162 {
2164
2165 /*
2166 * We used to have bugs where logical decoding would fail to
2167 * preserve the resource owner. That's important here, so
2168 * verify that that doesn't happen anymore. XXX this could be
2169 * removed once it's been battle-tested.
2170 */
2172 }
2173
2175 }
2176
2179
2181 {
2183
2184 /*
2185 * If only the confirmed_flush LSN has changed the slot won't get
2186 * marked as dirty by the above. Callers on the walsender
2187 * interface are expected to keep track of their own progress and
2188 * don't need it written out. But SQL-interface users cannot
2189 * specify their own start positions and it's harder for them to
2190 * keep track of their progress, so we should make more of an
2191 * effort to save it for them.
2192 *
2193 * Dirty the slot so it is written out at the next checkpoint. The
2194 * LSN position advanced to may still be lost on a crash but this
2195 * makes the data consistent after a clean shutdown.
2196 */
2198 }
2199
2201
2202 /* free context, call shutdown callback */
2204
2206 }
2207 PG_CATCH();
2208 {
2209 /* clear all timetravel entries */
2211
2212 PG_RE_THROW();
2213 }
2214 PG_END_TRY();
2215
2216 return retlsn;
2217}
#define PG_USED_FOR_ASSERTS_ONLY
Definition c.h:249
ResourceOwner CurrentResourceOwner
Definition resowner.c:173
void WaitForStandbyConfirmation(XLogRecPtr wait_for_lsn)
Definition slot.c:3261

References Assert, CHECK_FOR_INTERRUPTS, ReplicationSlotPersistentData::confirmed_flush, CreateDecodingContext(), CurrentResourceOwner, ReplicationSlot::data, DecodingContextReady(), elog, XLogReaderState::EndRecPtr, ERROR, fb(), FreeDecodingContext(), InvalidateSystemCaches(), InvalidXLogRecPtr, LogicalConfirmReceivedLocation(), LogicalDecodingProcessRecord(), MyReplicationSlot, NIL, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, PG_USED_FOR_ASSERTS_ONLY, read_local_xlog_page(), LogicalDecodingContext::reader, ReplicationSlotMarkDirty(), ReplicationSlotPersistentData::restart_lsn, WaitForStandbyConfirmation(), wal_segment_close(), wal_segment_open(), XL_ROUTINE, XLogBeginRead(), XLogReadRecord(), and XLogRecPtrIsValid.

Referenced by pg_logical_replication_slot_advance(), and update_local_synced_slot().

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

1229{
1232 ErrorContextCallback errcallback;
1233
1234 Assert(!ctx->fast_forward);
1235
1236 if (ctx->callbacks.message_cb == NULL)
1237 return;
1238
1239 /* Push callback + info on the error context stack */
1240 state.ctx = ctx;
1241 state.callback_name = "message";
1242 state.report_location = message_lsn;
1244 errcallback.arg = &state;
1245 errcallback.previous = error_context_stack;
1246 error_context_stack = &errcallback;
1247
1248 /* set output state */
1249 ctx->accept_writes = true;
1250 ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
1252 ctx->end_xact = false;
1253
1254 /* do the actual work: call callback */
1255 ctx->callbacks.message_cb(ctx, txn, message_lsn, transactional, prefix,
1256 message_size, message);
1257
1258 /* Pop the error context stack */
1259 error_context_stack = errcallback.previous;
1260}
LogicalDecodeMessageCB message_cb

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert, ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, error_context_stack, LogicalDecodingContext::fast_forward, fb(), InvalidTransactionId, OutputPluginCallbacks::message_cb, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ output_plugin_error_callback()

static void output_plugin_error_callback ( void arg)
static

Definition at line 751 of file logical.c.

752{
754
755 /* not all callbacks have an associated LSN */
756 if (XLogRecPtrIsValid(state->report_location))
757 errcontext("slot \"%s\", output plugin \"%s\", in the %s callback, associated LSN %X/%08X",
758 NameStr(state->ctx->slot->data.name),
759 NameStr(state->ctx->slot->data.plugin),
760 state->callback_name,
761 LSN_FORMAT_ARGS(state->report_location));
762 else
763 errcontext("slot \"%s\", output plugin \"%s\", in the %s callback",
764 NameStr(state->ctx->slot->data.name),
765 NameStr(state->ctx->slot->data.plugin),
766 state->callback_name);
767}
Datum arg
Definition elog.c:1323
#define errcontext
Definition elog.h:200

References arg, errcontext, LSN_FORMAT_ARGS, NameStr, and XLogRecPtrIsValid.

Referenced by begin_cb_wrapper(), begin_prepare_cb_wrapper(), change_cb_wrapper(), commit_cb_wrapper(), commit_prepared_cb_wrapper(), filter_by_origin_cb_wrapper(), filter_prepare_cb_wrapper(), message_cb_wrapper(), prepare_cb_wrapper(), rollback_prepared_cb_wrapper(), shutdown_cb_wrapper(), startup_cb_wrapper(), stream_abort_cb_wrapper(), stream_change_cb_wrapper(), stream_commit_cb_wrapper(), stream_message_cb_wrapper(), stream_prepare_cb_wrapper(), stream_start_cb_wrapper(), stream_stop_cb_wrapper(), stream_truncate_cb_wrapper(), truncate_cb_wrapper(), and update_progress_txn_cb_wrapper().

◆ OutputPluginPrepareWrite()

void OutputPluginPrepareWrite ( struct LogicalDecodingContext ctx,
bool  last_write 
)

Definition at line 688 of file logical.c.

689{
690 if (!ctx->accept_writes)
691 elog(ERROR, "writes are only accepted in commit, begin and change callbacks");
692
693 ctx->prepare_write(ctx, ctx->write_location, ctx->write_xid, last_write);
694 ctx->prepared_write = true;
695}
LogicalOutputPluginWriterPrepareWrite prepare_write
Definition logical.h:64

References LogicalDecodingContext::accept_writes, elog, ERROR, fb(), LogicalDecodingContext::prepare_write, LogicalDecodingContext::prepared_write, LogicalDecodingContext::write_location, and LogicalDecodingContext::write_xid.

Referenced by pg_decode_change(), pg_decode_commit_prepared_txn(), pg_decode_commit_txn(), pg_decode_message(), pg_decode_prepare_txn(), pg_decode_rollback_prepared_txn(), pg_decode_stream_abort(), pg_decode_stream_change(), pg_decode_stream_commit(), pg_decode_stream_message(), pg_decode_stream_prepare(), pg_decode_stream_stop(), pg_decode_stream_truncate(), pg_decode_truncate(), pg_output_begin(), pg_output_stream_start(), pgoutput_begin_prepare_txn(), pgoutput_change(), pgoutput_commit_prepared_txn(), pgoutput_commit_txn(), pgoutput_message(), pgoutput_prepare_txn(), pgoutput_rollback_prepared_txn(), pgoutput_send_begin(), pgoutput_stream_abort(), pgoutput_stream_commit(), pgoutput_stream_prepare_txn(), pgoutput_stream_start(), pgoutput_stream_stop(), pgoutput_truncate(), send_relation_and_attrs(), and send_repl_origin().

◆ OutputPluginUpdateProgress()

◆ OutputPluginWrite()

◆ prepare_cb_wrapper()

static void prepare_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn,
XLogRecPtr  prepare_lsn 
)
static

Definition at line 945 of file logical.c.

947{
950 ErrorContextCallback errcallback;
951
952 Assert(!ctx->fast_forward);
953
954 /* We're only supposed to call this when two-phase commits are supported */
955 Assert(ctx->twophase);
956
957 /* Push callback + info on the error context stack */
958 state.ctx = ctx;
959 state.callback_name = "prepare";
960 state.report_location = txn->final_lsn; /* beginning of prepare record */
962 errcallback.arg = &state;
963 errcallback.previous = error_context_stack;
964 error_context_stack = &errcallback;
965
966 /* set output state */
967 ctx->accept_writes = true;
968 ctx->write_xid = txn->xid;
969 ctx->write_location = txn->end_lsn; /* points to the end of the record */
970 ctx->end_xact = true;
971
972 /*
973 * If the plugin supports two-phase commits then prepare callback is
974 * mandatory
975 */
976 if (ctx->callbacks.prepare_cb == NULL)
979 errmsg("logical replication at prepare time requires a %s callback",
980 "prepare_cb")));
981
982 /* do the actual work: call callback */
983 ctx->callbacks.prepare_cb(ctx, txn, prepare_lsn);
984
985 /* Pop the error context stack */
986 error_context_stack = errcallback.previous;
987}
LogicalDecodePrepareCB prepare_cb

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert, ErrorContextCallback::callback, LogicalDecodingContext::callbacks, ReorderBufferTXN::end_lsn, LogicalDecodingContext::end_xact, ereport, errcode(), errmsg, ERROR, error_context_stack, LogicalDecodingContext::fast_forward, fb(), ReorderBufferTXN::final_lsn, output_plugin_error_callback(), OutputPluginCallbacks::prepare_cb, ErrorContextCallback::previous, ReorderBuffer::private_data, LogicalDecodingContext::twophase, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ ResetLogicalStreamingState()

void ResetLogicalStreamingState ( void  )

Definition at line 1938 of file logical.c.

1939{
1941 bsysscan = false;
1942}
bool bsysscan
Definition xact.c:102
TransactionId CheckXidAlive
Definition xact.c:101

References bsysscan, CheckXidAlive, and InvalidTransactionId.

Referenced by AbortSubTransaction(), and AbortTransaction().

◆ rollback_prepared_cb_wrapper()

static void rollback_prepared_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn,
XLogRecPtr  prepare_end_lsn,
TimestampTz  prepare_time 
)
static

Definition at line 1035 of file logical.c.

1038{
1041 ErrorContextCallback errcallback;
1042
1043 Assert(!ctx->fast_forward);
1044
1045 /* We're only supposed to call this when two-phase commits are supported */
1046 Assert(ctx->twophase);
1047
1048 /* Push callback + info on the error context stack */
1049 state.ctx = ctx;
1050 state.callback_name = "rollback_prepared";
1051 state.report_location = txn->final_lsn; /* beginning of commit record */
1053 errcallback.arg = &state;
1054 errcallback.previous = error_context_stack;
1055 error_context_stack = &errcallback;
1056
1057 /* set output state */
1058 ctx->accept_writes = true;
1059 ctx->write_xid = txn->xid;
1060 ctx->write_location = txn->end_lsn; /* points to the end of the record */
1061 ctx->end_xact = true;
1062
1063 /*
1064 * If the plugin support two-phase commits then rollback prepared callback
1065 * is mandatory
1066 */
1068 ereport(ERROR,
1070 errmsg("logical replication at prepare time requires a %s callback",
1071 "rollback_prepared_cb")));
1072
1073 /* do the actual work: call callback */
1074 ctx->callbacks.rollback_prepared_cb(ctx, txn, prepare_end_lsn,
1075 prepare_time);
1076
1077 /* Pop the error context stack */
1078 error_context_stack = errcallback.previous;
1079}
LogicalDecodeRollbackPreparedCB rollback_prepared_cb

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert, ErrorContextCallback::callback, LogicalDecodingContext::callbacks, ReorderBufferTXN::end_lsn, LogicalDecodingContext::end_xact, ereport, errcode(), errmsg, ERROR, error_context_stack, LogicalDecodingContext::fast_forward, fb(), ReorderBufferTXN::final_lsn, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, OutputPluginCallbacks::rollback_prepared_cb, LogicalDecodingContext::twophase, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ shutdown_cb_wrapper()

static void shutdown_cb_wrapper ( LogicalDecodingContext ctx)
static

Definition at line 798 of file logical.c.

799{
801 ErrorContextCallback errcallback;
802
803 Assert(!ctx->fast_forward);
804
805 /* Push callback + info on the error context stack */
806 state.ctx = ctx;
807 state.callback_name = "shutdown";
808 state.report_location = InvalidXLogRecPtr;
810 errcallback.arg = &state;
811 errcallback.previous = error_context_stack;
812 error_context_stack = &errcallback;
813
814 /* set output state */
815 ctx->accept_writes = false;
816 ctx->end_xact = false;
817
818 /* do the actual work: call callback */
819 ctx->callbacks.shutdown_cb(ctx);
820
821 /* Pop the error context stack */
822 error_context_stack = errcallback.previous;
823}

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert, ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, error_context_stack, LogicalDecodingContext::fast_forward, InvalidXLogRecPtr, output_plugin_error_callback(), ErrorContextCallback::previous, and OutputPluginCallbacks::shutdown_cb.

Referenced by FreeDecodingContext().

◆ startup_cb_wrapper()

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

Definition at line 770 of file logical.c.

771{
773 ErrorContextCallback errcallback;
774
775 Assert(!ctx->fast_forward);
776
777 /* Push callback + info on the error context stack */
778 state.ctx = ctx;
779 state.callback_name = "startup";
780 state.report_location = InvalidXLogRecPtr;
782 errcallback.arg = &state;
783 errcallback.previous = error_context_stack;
784 error_context_stack = &errcallback;
785
786 /* set output state */
787 ctx->accept_writes = false;
788 ctx->end_xact = false;
789
790 /* do the actual work: call callback */
791 ctx->callbacks.startup_cb(ctx, opt, is_init);
792
793 /* Pop the error context stack */
794 error_context_stack = errcallback.previous;
795}

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert, ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, error_context_stack, LogicalDecodingContext::fast_forward, fb(), InvalidXLogRecPtr, output_plugin_error_callback(), ErrorContextCallback::previous, and OutputPluginCallbacks::startup_cb.

Referenced by CreateDecodingContext(), and CreateInitDecodingContext().

◆ StartupDecodingContext()

static LogicalDecodingContext * StartupDecodingContext ( List output_plugin_options,
XLogRecPtr  start_lsn,
TransactionId  xmin_horizon,
bool  need_full_snapshot,
bool  fast_forward,
bool  in_create,
XLogReaderRoutine xl_routine,
LogicalOutputPluginWriterPrepareWrite  prepare_write,
LogicalOutputPluginWriterWrite  do_write,
LogicalOutputPluginWriterUpdateProgress  update_progress 
)
static

Definition at line 141 of file logical.c.

151{
152 ReplicationSlot *slot;
153 MemoryContext context,
156
157 /* shorter lines... */
158 slot = MyReplicationSlot;
159
161 "Logical decoding context",
165
166 ctx->context = context;
167
168 /*
169 * (re-)load output plugins, so we detect a bad (removed) output plugin
170 * now.
171 */
172 if (!fast_forward)
174
175 /*
176 * Now that the slot's xmin has been set, we can announce ourselves as a
177 * logical decoding backend which doesn't need to be checked individually
178 * when computing the xmin horizon because the xmin is enforced via
179 * replication slots.
180 *
181 * We can only do so if we're outside of a transaction (i.e. the case when
182 * streaming changes via walsender), otherwise an already setup
183 * snapshot/xid would end up being ignored. That's not a particularly
184 * bothersome restriction since the SQL interface can't be used for
185 * streaming anyway.
186 */
188 {
193 }
194
195 ctx->slot = slot;
196
198 if (!ctx->reader)
201 errmsg("out of memory"),
202 errdetail("Failed while allocating a WAL reading processor.")));
203
205 ctx->snapshot_builder =
208
209 ctx->reorder->private_data = ctx;
210
211 /* wrap output plugin callbacks, so we can add error context information */
217
218 /*
219 * To support streaming, we require start/stop/abort/commit/change
220 * callbacks. The message and truncate callbacks are optional, similar to
221 * regular output plugins. We however enable streaming when at least one
222 * of the methods is enabled so that we can easily identify missing
223 * methods.
224 *
225 * We decide it here, but only check it later in the wrappers.
226 */
227 ctx->streaming = (ctx->callbacks.stream_start_cb != NULL) ||
228 (ctx->callbacks.stream_stop_cb != NULL) ||
229 (ctx->callbacks.stream_abort_cb != NULL) ||
230 (ctx->callbacks.stream_commit_cb != NULL) ||
231 (ctx->callbacks.stream_change_cb != NULL) ||
232 (ctx->callbacks.stream_message_cb != NULL) ||
234
235 /*
236 * streaming callbacks
237 *
238 * stream_message and stream_truncate callbacks are optional, so we do not
239 * fail with ERROR when missing, but the wrappers simply do nothing. We
240 * must set the ReorderBuffer callbacks to something, otherwise the calls
241 * from there will crash (we don't want to move the checks there).
242 */
251
252
253 /*
254 * To support two-phase logical decoding, we require
255 * begin_prepare/prepare/commit-prepare/abort-prepare callbacks. The
256 * filter_prepare callback is optional. We however enable two-phase
257 * logical decoding when at least one of the methods is enabled so that we
258 * can easily identify missing methods.
259 *
260 * We decide it here, but only check it later in the wrappers.
261 */
262 ctx->twophase = (ctx->callbacks.begin_prepare_cb != NULL) ||
263 (ctx->callbacks.prepare_cb != NULL) ||
266 (ctx->callbacks.stream_prepare_cb != NULL) ||
268
269 /*
270 * Callback to support decoding at prepare time.
271 */
276
277 /*
278 * Callback to support updating progress during sending data of a
279 * transaction (and its subtransactions) to the output plugin.
280 */
282
283 ctx->out = makeStringInfo();
284 ctx->prepare_write = prepare_write;
285 ctx->write = do_write;
286 ctx->update_progress = update_progress;
287
288 /* Assume shared catalog access. The startup callback can change it. */
289 ctx->options.need_shared_catalogs = true;
290
291 ctx->output_plugin_options = output_plugin_options;
292
293 ctx->fast_forward = fast_forward;
294
296
297 return ctx;
298}
#define palloc0_object(type)
Definition fe_memutils.h:75
static void change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
Definition logical.c:1082
static void commit_prepared_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition logical.c:990
static void update_progress_txn_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr lsn)
Definition logical.c:1625
static void stream_start_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr first_lsn)
Definition logical.c:1263
static void commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition logical.c:862
static void begin_prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
Definition logical.c:901
static void stream_prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
Definition logical.c:1402
static void prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
Definition logical.c:945
static void stream_truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
Definition logical.c:1578
static void truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
Definition logical.c:1121
static void begin_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
Definition logical.c:831
static void rollback_prepared_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_end_lsn, TimestampTz prepare_time)
Definition logical.c:1035
static void stream_change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
Definition logical.c:1488
static void stream_abort_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr abort_lsn)
Definition logical.c:1361
static void stream_message_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr message_lsn, bool transactional, const char *prefix, Size message_size, const char *message)
Definition logical.c:1537
static void stream_commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition logical.c:1447
static void LoadOutputPlugin(OutputPluginCallbacks *callbacks, const char *plugin)
Definition logical.c:729
static void stream_stop_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr last_lsn)
Definition logical.c:1312
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:1226
MemoryContext CurrentMemoryContext
Definition mcxt.c:160
#define AllocSetContextCreate
Definition memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition memutils.h:160
#define PROC_IN_LOGICAL_DECODING
Definition proc.h:65
ReorderBuffer * ReorderBufferAllocate(void)
SnapBuild * AllocateSnapshotBuilder(ReorderBuffer *reorder, TransactionId xmin_horizon, XLogRecPtr start_lsn, bool need_full_snapshot, bool in_slot_creation, XLogRecPtr two_phase_at)
Definition snapbuild.c:197
PGPROC * MyProc
Definition proc.c:71
PROC_HDR * ProcGlobal
Definition proc.c:74
StringInfo makeStringInfo(void)
Definition stringinfo.c:72
List * output_plugin_options
Definition logical.h:59
LogicalDecodeStreamChangeCB stream_change_cb
LogicalDecodeStreamTruncateCB stream_truncate_cb
LogicalDecodeStreamMessageCB stream_message_cb
LogicalDecodeStreamStopCB stream_stop_cb
LogicalDecodeStreamCommitCB stream_commit_cb
LogicalDecodeStreamPrepareCB stream_prepare_cb
LogicalDecodeStreamStartCB stream_start_cb
LogicalDecodeStreamAbortCB stream_abort_cb
uint8 statusFlags
Definition proc.h:210
int pgxactoff
Definition proc.h:207
uint8 * statusFlags
Definition proc.h:456
ReorderBufferStreamMessageCB stream_message
ReorderBufferStreamChangeCB stream_change
ReorderBufferBeginCB begin_prepare
ReorderBufferStreamTruncateCB stream_truncate
ReorderBufferCommitPreparedCB commit_prepared
ReorderBufferUpdateProgressTxnCB update_progress_txn
ReorderBufferMessageCB message
ReorderBufferRollbackPreparedCB rollback_prepared
ReorderBufferPrepareCB prepare
ReorderBufferStreamStopCB stream_stop
ReorderBufferApplyChangeCB apply_change
ReorderBufferStreamPrepareCB stream_prepare
ReorderBufferStreamAbortCB stream_abort
ReorderBufferCommitCB commit
ReorderBufferStreamStartCB stream_start
ReorderBufferStreamCommitCB stream_commit
ReorderBufferApplyTruncateCB apply_truncate
ReorderBufferBeginCB begin
bool IsTransactionOrTransactionBlock(void)
Definition xact.c:5040
XLogReaderState * XLogReaderAllocate(int wal_segment_size, const char *waldir, XLogReaderRoutine *routine, void *private_data)
Definition xlogreader.c:108

References AllocateSnapshotBuilder(), ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, ReorderBuffer::apply_change, ReorderBuffer::apply_truncate, ReorderBuffer::begin, begin_cb_wrapper(), ReorderBuffer::begin_prepare, OutputPluginCallbacks::begin_prepare_cb, begin_prepare_cb_wrapper(), LogicalDecodingContext::callbacks, change_cb_wrapper(), ReorderBuffer::commit, commit_cb_wrapper(), ReorderBuffer::commit_prepared, OutputPluginCallbacks::commit_prepared_cb, commit_prepared_cb_wrapper(), LogicalDecodingContext::context, CurrentMemoryContext, ReplicationSlot::data, ereport, errcode(), errdetail(), errmsg, ERROR, LogicalDecodingContext::fast_forward, fb(), OutputPluginCallbacks::filter_prepare_cb, IsTransactionOrTransactionBlock(), LoadOutputPlugin(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), makeStringInfo(), MemoryContextSwitchTo(), ReorderBuffer::message, message_cb_wrapper(), MyProc, MyReplicationSlot, NameStr, OutputPluginOptions::need_shared_catalogs, LogicalDecodingContext::options, LogicalDecodingContext::out, LogicalDecodingContext::output_plugin_options, palloc0_object, PGPROC::pgxactoff, ReplicationSlotPersistentData::plugin, ReorderBuffer::prepare, OutputPluginCallbacks::prepare_cb, prepare_cb_wrapper(), LogicalDecodingContext::prepare_write, ReorderBuffer::private_data, PROC_IN_LOGICAL_DECODING, ProcGlobal, LogicalDecodingContext::reader, LogicalDecodingContext::reorder, ReorderBufferAllocate(), ReorderBuffer::rollback_prepared, OutputPluginCallbacks::rollback_prepared_cb, rollback_prepared_cb_wrapper(), LogicalDecodingContext::slot, LogicalDecodingContext::snapshot_builder, PGPROC::statusFlags, PROC_HDR::statusFlags, ReorderBuffer::stream_abort, OutputPluginCallbacks::stream_abort_cb, stream_abort_cb_wrapper(), ReorderBuffer::stream_change, OutputPluginCallbacks::stream_change_cb, stream_change_cb_wrapper(), ReorderBuffer::stream_commit, OutputPluginCallbacks::stream_commit_cb, stream_commit_cb_wrapper(), ReorderBuffer::stream_message, OutputPluginCallbacks::stream_message_cb, stream_message_cb_wrapper(), ReorderBuffer::stream_prepare, OutputPluginCallbacks::stream_prepare_cb, stream_prepare_cb_wrapper(), ReorderBuffer::stream_start, OutputPluginCallbacks::stream_start_cb, stream_start_cb_wrapper(), ReorderBuffer::stream_stop, OutputPluginCallbacks::stream_stop_cb, stream_stop_cb_wrapper(), ReorderBuffer::stream_truncate, OutputPluginCallbacks::stream_truncate_cb, stream_truncate_cb_wrapper(), LogicalDecodingContext::streaming, truncate_cb_wrapper(), ReplicationSlotPersistentData::two_phase_at, LogicalDecodingContext::twophase, LogicalDecodingContext::update_progress, ReorderBuffer::update_progress_txn, update_progress_txn_cb_wrapper(), wal_segment_size, LogicalDecodingContext::write, and XLogReaderAllocate().

Referenced by CreateDecodingContext(), and CreateInitDecodingContext().

◆ stream_abort_cb_wrapper()

static void stream_abort_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn,
XLogRecPtr  abort_lsn 
)
static

Definition at line 1361 of file logical.c.

1363{
1366 ErrorContextCallback errcallback;
1367
1368 Assert(!ctx->fast_forward);
1369
1370 /* We're only supposed to call this when streaming is supported. */
1371 Assert(ctx->streaming);
1372
1373 /* Push callback + info on the error context stack */
1374 state.ctx = ctx;
1375 state.callback_name = "stream_abort";
1376 state.report_location = abort_lsn;
1378 errcallback.arg = &state;
1379 errcallback.previous = error_context_stack;
1380 error_context_stack = &errcallback;
1381
1382 /* set output state */
1383 ctx->accept_writes = true;
1384 ctx->write_xid = txn->xid;
1385 ctx->write_location = abort_lsn;
1386 ctx->end_xact = true;
1387
1388 /* in streaming mode, stream_abort_cb is required */
1389 if (ctx->callbacks.stream_abort_cb == NULL)
1390 ereport(ERROR,
1392 errmsg("logical streaming requires a %s callback",
1393 "stream_abort_cb")));
1394
1395 ctx->callbacks.stream_abort_cb(ctx, txn, abort_lsn);
1396
1397 /* Pop the error context stack */
1398 error_context_stack = errcallback.previous;
1399}

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert, ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, ereport, errcode(), errmsg, ERROR, error_context_stack, LogicalDecodingContext::fast_forward, fb(), output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, OutputPluginCallbacks::stream_abort_cb, LogicalDecodingContext::streaming, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ stream_change_cb_wrapper()

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

Definition at line 1488 of file logical.c.

1490{
1493 ErrorContextCallback errcallback;
1494
1495 Assert(!ctx->fast_forward);
1496
1497 /* We're only supposed to call this when streaming is supported. */
1498 Assert(ctx->streaming);
1499
1500 /* Push callback + info on the error context stack */
1501 state.ctx = ctx;
1502 state.callback_name = "stream_change";
1503 state.report_location = change->lsn;
1505 errcallback.arg = &state;
1506 errcallback.previous = error_context_stack;
1507 error_context_stack = &errcallback;
1508
1509 /* set output state */
1510 ctx->accept_writes = true;
1511 ctx->write_xid = txn->xid;
1512
1513 /*
1514 * Report this change's lsn so replies from clients can give an up-to-date
1515 * answer. This won't ever be enough (and shouldn't be!) to confirm
1516 * receipt of this transaction, but it might allow another transaction's
1517 * commit to be confirmed with one message.
1518 */
1519 ctx->write_location = change->lsn;
1520
1521 ctx->end_xact = false;
1522
1523 /* in streaming mode, stream_change_cb is required */
1524 if (ctx->callbacks.stream_change_cb == NULL)
1525 ereport(ERROR,
1527 errmsg("logical streaming requires a %s callback",
1528 "stream_change_cb")));
1529
1530 ctx->callbacks.stream_change_cb(ctx, txn, relation, change);
1531
1532 /* Pop the error context stack */
1533 error_context_stack = errcallback.previous;
1534}

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert, ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, ereport, errcode(), errmsg, ERROR, error_context_stack, LogicalDecodingContext::fast_forward, fb(), ReorderBufferChange::lsn, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, OutputPluginCallbacks::stream_change_cb, LogicalDecodingContext::streaming, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ stream_commit_cb_wrapper()

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

Definition at line 1447 of file logical.c.

1449{
1452 ErrorContextCallback errcallback;
1453
1454 Assert(!ctx->fast_forward);
1455
1456 /* We're only supposed to call this when streaming is supported. */
1457 Assert(ctx->streaming);
1458
1459 /* Push callback + info on the error context stack */
1460 state.ctx = ctx;
1461 state.callback_name = "stream_commit";
1462 state.report_location = txn->final_lsn;
1464 errcallback.arg = &state;
1465 errcallback.previous = error_context_stack;
1466 error_context_stack = &errcallback;
1467
1468 /* set output state */
1469 ctx->accept_writes = true;
1470 ctx->write_xid = txn->xid;
1471 ctx->write_location = txn->end_lsn;
1472 ctx->end_xact = true;
1473
1474 /* in streaming mode, stream_commit_cb is required */
1475 if (ctx->callbacks.stream_commit_cb == NULL)
1476 ereport(ERROR,
1478 errmsg("logical streaming requires a %s callback",
1479 "stream_commit_cb")));
1480
1481 ctx->callbacks.stream_commit_cb(ctx, txn, commit_lsn);
1482
1483 /* Pop the error context stack */
1484 error_context_stack = errcallback.previous;
1485}

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert, ErrorContextCallback::callback, LogicalDecodingContext::callbacks, ReorderBufferTXN::end_lsn, LogicalDecodingContext::end_xact, ereport, errcode(), errmsg, ERROR, error_context_stack, LogicalDecodingContext::fast_forward, fb(), ReorderBufferTXN::final_lsn, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, OutputPluginCallbacks::stream_commit_cb, LogicalDecodingContext::streaming, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ stream_message_cb_wrapper()

static void stream_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 1537 of file logical.c.

1540{
1543 ErrorContextCallback errcallback;
1544
1545 Assert(!ctx->fast_forward);
1546
1547 /* We're only supposed to call this when streaming is supported. */
1548 Assert(ctx->streaming);
1549
1550 /* this callback is optional */
1551 if (ctx->callbacks.stream_message_cb == NULL)
1552 return;
1553
1554 /* Push callback + info on the error context stack */
1555 state.ctx = ctx;
1556 state.callback_name = "stream_message";
1557 state.report_location = message_lsn;
1559 errcallback.arg = &state;
1560 errcallback.previous = error_context_stack;
1561 error_context_stack = &errcallback;
1562
1563 /* set output state */
1564 ctx->accept_writes = true;
1565 ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
1567 ctx->end_xact = false;
1568
1569 /* do the actual work: call callback */
1570 ctx->callbacks.stream_message_cb(ctx, txn, message_lsn, transactional, prefix,
1571 message_size, message);
1572
1573 /* Pop the error context stack */
1574 error_context_stack = errcallback.previous;
1575}

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert, ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, error_context_stack, LogicalDecodingContext::fast_forward, fb(), InvalidTransactionId, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, OutputPluginCallbacks::stream_message_cb, LogicalDecodingContext::streaming, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ stream_prepare_cb_wrapper()

static void stream_prepare_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn,
XLogRecPtr  prepare_lsn 
)
static

Definition at line 1402 of file logical.c.

1404{
1407 ErrorContextCallback errcallback;
1408
1409 Assert(!ctx->fast_forward);
1410
1411 /*
1412 * We're only supposed to call this when streaming and two-phase commits
1413 * are supported.
1414 */
1415 Assert(ctx->streaming);
1416 Assert(ctx->twophase);
1417
1418 /* Push callback + info on the error context stack */
1419 state.ctx = ctx;
1420 state.callback_name = "stream_prepare";
1421 state.report_location = txn->final_lsn;
1423 errcallback.arg = &state;
1424 errcallback.previous = error_context_stack;
1425 error_context_stack = &errcallback;
1426
1427 /* set output state */
1428 ctx->accept_writes = true;
1429 ctx->write_xid = txn->xid;
1430 ctx->write_location = txn->end_lsn;
1431 ctx->end_xact = true;
1432
1433 /* in streaming mode with two-phase commits, stream_prepare_cb is required */
1434 if (ctx->callbacks.stream_prepare_cb == NULL)
1435 ereport(ERROR,
1437 errmsg("logical streaming at prepare time requires a %s callback",
1438 "stream_prepare_cb")));
1439
1440 ctx->callbacks.stream_prepare_cb(ctx, txn, prepare_lsn);
1441
1442 /* Pop the error context stack */
1443 error_context_stack = errcallback.previous;
1444}

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert, ErrorContextCallback::callback, LogicalDecodingContext::callbacks, ReorderBufferTXN::end_lsn, LogicalDecodingContext::end_xact, ereport, errcode(), errmsg, ERROR, error_context_stack, LogicalDecodingContext::fast_forward, fb(), ReorderBufferTXN::final_lsn, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, OutputPluginCallbacks::stream_prepare_cb, LogicalDecodingContext::streaming, LogicalDecodingContext::twophase, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ stream_start_cb_wrapper()

static void stream_start_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn,
XLogRecPtr  first_lsn 
)
static

Definition at line 1263 of file logical.c.

1265{
1268 ErrorContextCallback errcallback;
1269
1270 Assert(!ctx->fast_forward);
1271
1272 /* We're only supposed to call this when streaming is supported. */
1273 Assert(ctx->streaming);
1274
1275 /* Push callback + info on the error context stack */
1276 state.ctx = ctx;
1277 state.callback_name = "stream_start";
1278 state.report_location = first_lsn;
1280 errcallback.arg = &state;
1281 errcallback.previous = error_context_stack;
1282 error_context_stack = &errcallback;
1283
1284 /* set output state */
1285 ctx->accept_writes = true;
1286 ctx->write_xid = txn->xid;
1287
1288 /*
1289 * Report this message's lsn so replies from clients can give an
1290 * up-to-date answer. This won't ever be enough (and shouldn't be!) to
1291 * confirm receipt of this transaction, but it might allow another
1292 * transaction's commit to be confirmed with one message.
1293 */
1294 ctx->write_location = first_lsn;
1295
1296 ctx->end_xact = false;
1297
1298 /* in streaming mode, stream_start_cb is required */
1299 if (ctx->callbacks.stream_start_cb == NULL)
1300 ereport(ERROR,
1302 errmsg("logical streaming requires a %s callback",
1303 "stream_start_cb")));
1304
1305 ctx->callbacks.stream_start_cb(ctx, txn);
1306
1307 /* Pop the error context stack */
1308 error_context_stack = errcallback.previous;
1309}

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert, ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, ereport, errcode(), errmsg, ERROR, error_context_stack, LogicalDecodingContext::fast_forward, fb(), output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, OutputPluginCallbacks::stream_start_cb, LogicalDecodingContext::streaming, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ stream_stop_cb_wrapper()

static void stream_stop_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn,
XLogRecPtr  last_lsn 
)
static

Definition at line 1312 of file logical.c.

1314{
1317 ErrorContextCallback errcallback;
1318
1319 Assert(!ctx->fast_forward);
1320
1321 /* We're only supposed to call this when streaming is supported. */
1322 Assert(ctx->streaming);
1323
1324 /* Push callback + info on the error context stack */
1325 state.ctx = ctx;
1326 state.callback_name = "stream_stop";
1327 state.report_location = last_lsn;
1329 errcallback.arg = &state;
1330 errcallback.previous = error_context_stack;
1331 error_context_stack = &errcallback;
1332
1333 /* set output state */
1334 ctx->accept_writes = true;
1335 ctx->write_xid = txn->xid;
1336
1337 /*
1338 * Report this message's lsn so replies from clients can give an
1339 * up-to-date answer. This won't ever be enough (and shouldn't be!) to
1340 * confirm receipt of this transaction, but it might allow another
1341 * transaction's commit to be confirmed with one message.
1342 */
1343 ctx->write_location = last_lsn;
1344
1345 ctx->end_xact = false;
1346
1347 /* in streaming mode, stream_stop_cb is required */
1348 if (ctx->callbacks.stream_stop_cb == NULL)
1349 ereport(ERROR,
1351 errmsg("logical streaming requires a %s callback",
1352 "stream_stop_cb")));
1353
1354 ctx->callbacks.stream_stop_cb(ctx, txn);
1355
1356 /* Pop the error context stack */
1357 error_context_stack = errcallback.previous;
1358}

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert, ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, ereport, errcode(), errmsg, ERROR, error_context_stack, LogicalDecodingContext::fast_forward, fb(), output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, OutputPluginCallbacks::stream_stop_cb, LogicalDecodingContext::streaming, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ stream_truncate_cb_wrapper()

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

Definition at line 1578 of file logical.c.

1581{
1584 ErrorContextCallback errcallback;
1585
1586 Assert(!ctx->fast_forward);
1587
1588 /* We're only supposed to call this when streaming is supported. */
1589 Assert(ctx->streaming);
1590
1591 /* this callback is optional */
1592 if (!ctx->callbacks.stream_truncate_cb)
1593 return;
1594
1595 /* Push callback + info on the error context stack */
1596 state.ctx = ctx;
1597 state.callback_name = "stream_truncate";
1598 state.report_location = change->lsn;
1600 errcallback.arg = &state;
1601 errcallback.previous = error_context_stack;
1602 error_context_stack = &errcallback;
1603
1604 /* set output state */
1605 ctx->accept_writes = true;
1606 ctx->write_xid = txn->xid;
1607
1608 /*
1609 * Report this change's lsn so replies from clients can give an up-to-date
1610 * answer. This won't ever be enough (and shouldn't be!) to confirm
1611 * receipt of this transaction, but it might allow another transaction's
1612 * commit to be confirmed with one message.
1613 */
1614 ctx->write_location = change->lsn;
1615
1616 ctx->end_xact = false;
1617
1618 ctx->callbacks.stream_truncate_cb(ctx, txn, nrelations, relations, change);
1619
1620 /* Pop the error context stack */
1621 error_context_stack = errcallback.previous;
1622}

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert, ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, error_context_stack, LogicalDecodingContext::fast_forward, fb(), ReorderBufferChange::lsn, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, OutputPluginCallbacks::stream_truncate_cb, LogicalDecodingContext::streaming, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ truncate_cb_wrapper()

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

Definition at line 1121 of file logical.c.

1123{
1126 ErrorContextCallback errcallback;
1127
1128 Assert(!ctx->fast_forward);
1129
1130 if (!ctx->callbacks.truncate_cb)
1131 return;
1132
1133 /* Push callback + info on the error context stack */
1134 state.ctx = ctx;
1135 state.callback_name = "truncate";
1136 state.report_location = change->lsn;
1138 errcallback.arg = &state;
1139 errcallback.previous = error_context_stack;
1140 error_context_stack = &errcallback;
1141
1142 /* set output state */
1143 ctx->accept_writes = true;
1144 ctx->write_xid = txn->xid;
1145
1146 /*
1147 * Report this change's lsn so replies from clients can give an up-to-date
1148 * answer. This won't ever be enough (and shouldn't be!) to confirm
1149 * receipt of this transaction, but it might allow another transaction's
1150 * commit to be confirmed with one message.
1151 */
1152 ctx->write_location = change->lsn;
1153
1154 ctx->end_xact = false;
1155
1156 ctx->callbacks.truncate_cb(ctx, txn, nrelations, relations, change);
1157
1158 /* Pop the error context stack */
1159 error_context_stack = errcallback.previous;
1160}
LogicalDecodeTruncateCB truncate_cb

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert, ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, error_context_stack, LogicalDecodingContext::fast_forward, fb(), ReorderBufferChange::lsn, output_plugin_error_callback(), ErrorContextCallback::previous, ReorderBuffer::private_data, OutputPluginCallbacks::truncate_cb, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ update_progress_txn_cb_wrapper()

static void update_progress_txn_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn,
XLogRecPtr  lsn 
)
static

Definition at line 1625 of file logical.c.

1627{
1630 ErrorContextCallback errcallback;
1631
1632 Assert(!ctx->fast_forward);
1633
1634 /* Push callback + info on the error context stack */
1635 state.ctx = ctx;
1636 state.callback_name = "update_progress_txn";
1637 state.report_location = lsn;
1639 errcallback.arg = &state;
1640 errcallback.previous = error_context_stack;
1641 error_context_stack = &errcallback;
1642
1643 /* set output state */
1644 ctx->accept_writes = false;
1645 ctx->write_xid = txn->xid;
1646
1647 /*
1648 * Report this change's lsn so replies from clients can give an up-to-date
1649 * answer. This won't ever be enough (and shouldn't be!) to confirm
1650 * receipt of this transaction, but it might allow another transaction's
1651 * commit to be confirmed with one message.
1652 */
1653 ctx->write_location = lsn;
1654
1655 ctx->end_xact = false;
1656
1657 OutputPluginUpdateProgress(ctx, false);
1658
1659 /* Pop the error context stack */
1660 error_context_stack = errcallback.previous;
1661}
void OutputPluginUpdateProgress(struct LogicalDecodingContext *ctx, bool skipped_xact)
Definition logical.c:714

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert, ErrorContextCallback::callback, LogicalDecodingContext::end_xact, error_context_stack, LogicalDecodingContext::fast_forward, output_plugin_error_callback(), OutputPluginUpdateProgress(), ErrorContextCallback::previous, ReorderBuffer::private_data, LogicalDecodingContext::write_location, LogicalDecodingContext::write_xid, and ReorderBufferTXN::xid.

Referenced by StartupDecodingContext().

◆ UpdateDecodingStats()

void UpdateDecodingStats ( LogicalDecodingContext ctx)

Definition at line 1948 of file logical.c.

1949{
1950 ReorderBuffer *rb = ctx->reorder;
1952
1953 /* Nothing to do if we don't have any replication stats to be sent. */
1954 if (rb->spillBytes <= 0 && rb->streamBytes <= 0 && rb->totalBytes <= 0 &&
1955 rb->memExceededCount <= 0)
1956 return;
1957
1958 elog(DEBUG2, "UpdateDecodingStats: updating stats %p %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,
1959 rb,
1960 rb->spillTxns,
1961 rb->spillCount,
1962 rb->spillBytes,
1963 rb->streamTxns,
1964 rb->streamCount,
1965 rb->streamBytes,
1966 rb->memExceededCount,
1967 rb->totalTxns,
1968 rb->totalBytes);
1969
1970 repSlotStat.spill_txns = rb->spillTxns;
1971 repSlotStat.spill_count = rb->spillCount;
1972 repSlotStat.spill_bytes = rb->spillBytes;
1973 repSlotStat.stream_txns = rb->streamTxns;
1974 repSlotStat.stream_count = rb->streamCount;
1975 repSlotStat.stream_bytes = rb->streamBytes;
1976 repSlotStat.mem_exceeded_count = rb->memExceededCount;
1977 repSlotStat.total_txns = rb->totalTxns;
1978 repSlotStat.total_bytes = rb->totalBytes;
1979
1981
1982 rb->spillTxns = 0;
1983 rb->spillCount = 0;
1984 rb->spillBytes = 0;
1985 rb->streamTxns = 0;
1986 rb->streamCount = 0;
1987 rb->streamBytes = 0;
1988 rb->memExceededCount = 0;
1989 rb->totalTxns = 0;
1990 rb->totalBytes = 0;
1991}
#define DEBUG2
Definition elog.h:30
void pgstat_report_replslot(ReplicationSlot *slot, const PgStat_StatReplSlotEntry *repSlotStat)

References DEBUG2, elog, fb(), pgstat_report_replslot(), LogicalDecodingContext::reorder, and LogicalDecodingContext::slot.

Referenced by DecodeAbort(), DecodeCommit(), DecodePrepare(), ReorderBufferCheckMemoryLimit(), ReorderBufferSerializeTXN(), and ReorderBufferStreamTXN().