PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
logical.c File Reference
#include "postgres.h"
#include "access/xact.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/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 (void)
 
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, 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, RepOriginId 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)
 
bool LogicalReplicationSlotHasPendingWal (XLogRecPtr end_of_wal)
 
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 833 of file logical.c.

834{
837 ErrorContextCallback errcallback;
838
839 Assert(!ctx->fast_forward);
840
841 /* Push callback + info on the error context stack */
842 state.ctx = ctx;
843 state.callback_name = "begin";
844 state.report_location = txn->first_lsn;
846 errcallback.arg = &state;
847 errcallback.previous = error_context_stack;
848 error_context_stack = &errcallback;
849
850 /* set output state */
851 ctx->accept_writes = true;
852 ctx->write_xid = txn->xid;
853 ctx->write_location = txn->first_lsn;
854 ctx->end_xact = false;
855
856 /* do the actual work: call callback */
857 ctx->callbacks.begin_cb(ctx, txn);
858
859 /* Pop the error context stack */
860 error_context_stack = errcallback.previous;
861}
ErrorContextCallback * error_context_stack
Definition: elog.c:95
Assert(PointerIsAligned(start, uint64))
static void output_plugin_error_callback(void *arg)
Definition: logical.c:753
struct ErrorContextCallback * previous
Definition: elog.h:297
void(* callback)(void *arg)
Definition: elog.h:298
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
void * private_data
Definition: regguts.h:323

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

904{
907 ErrorContextCallback errcallback;
908
909 Assert(!ctx->fast_forward);
910
911 /* We're only supposed to call this when two-phase commits are supported */
912 Assert(ctx->twophase);
913
914 /* Push callback + info on the error context stack */
915 state.ctx = ctx;
916 state.callback_name = "begin_prepare";
917 state.report_location = txn->first_lsn;
919 errcallback.arg = &state;
920 errcallback.previous = error_context_stack;
921 error_context_stack = &errcallback;
922
923 /* set output state */
924 ctx->accept_writes = true;
925 ctx->write_xid = txn->xid;
926 ctx->write_location = txn->first_lsn;
927 ctx->end_xact = false;
928
929 /*
930 * If the plugin supports two-phase commits then begin prepare callback is
931 * mandatory
932 */
933 if (ctx->callbacks.begin_prepare_cb == NULL)
935 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
936 errmsg("logical replication at prepare time requires a %s callback",
937 "begin_prepare_cb")));
938
939 /* do the actual work: call callback */
940 ctx->callbacks.begin_prepare_cb(ctx, txn);
941
942 /* Pop the error context stack */
943 error_context_stack = errcallback.previous;
944}
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
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, 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 1084 of file logical.c.

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

Definition at line 109 of file logical.c.

110{
112
113 /*
114 * NB: Adding a new requirement likely means that RestoreSlotFromDisk()
115 * needs the same check.
116 */
117
120 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
121 errmsg("logical decoding requires \"wal_level\" >= \"logical\"")));
122
125 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
126 errmsg("logical decoding requires a database connection")));
127
128 if (RecoveryInProgress())
129 {
130 /*
131 * This check may have race conditions, but whenever
132 * XLOG_PARAMETER_CHANGE indicates that wal_level has changed, we
133 * verify that there are no existing logical replication slots. And to
134 * avoid races around creating a new slot,
135 * CheckLogicalDecodingRequirements() is called once before creating
136 * the slot, and once when logical decoding is initially starting up.
137 */
140 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
141 errmsg("logical decoding on standby requires \"wal_level\" >= \"logical\" on the primary")));
142 }
143}
Oid MyDatabaseId
Definition: globals.c:95
#define InvalidOid
Definition: postgres_ext.h:35
void CheckSlotRequirements(void)
Definition: slot.c:1413
bool RecoveryInProgress(void)
Definition: xlog.c:6522
int wal_level
Definition: xlog.c:131
WalLevel GetActiveWalLevelOnStandby(void)
Definition: xlog.c:5040
@ WAL_LEVEL_LOGICAL
Definition: xlog.h:76

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

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

◆ commit_cb_wrapper()

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

Definition at line 864 of file logical.c.

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

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

503{
505 ReplicationSlot *slot;
506 MemoryContext old_context;
507
508 /* shorter lines... */
509 slot = MyReplicationSlot;
510
511 /* first some sanity checks that are unlikely to be violated */
512 if (slot == NULL)
513 elog(ERROR, "cannot perform logical decoding without an acquired slot");
514
515 /* make sure the passed slot is suitable, these are user facing errors */
516 if (SlotIsPhysical(slot))
518 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
519 errmsg("cannot use physical replication slot for logical decoding")));
520
521 /*
522 * We need to access the system tables during decoding to build the
523 * logical changes unless we are in fast_forward mode where no changes are
524 * generated.
525 */
526 if (slot->data.database != MyDatabaseId && !fast_forward)
528 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
529 errmsg("replication slot \"%s\" was not created in this database",
530 NameStr(slot->data.name))));
531
532 /*
533 * The slots being synced from the primary can't be used for decoding as
534 * they are used after failover. However, we do allow advancing the LSNs
535 * during the synchronization of slots. See update_local_synced_slot.
536 */
539 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
540 errmsg("cannot use replication slot \"%s\" for logical decoding",
541 NameStr(slot->data.name)),
542 errdetail("This replication slot is being synchronized from the primary server."),
543 errhint("Specify another replication slot."));
544
545 /* slot must be valid to allow decoding */
548
549 if (start_lsn == InvalidXLogRecPtr)
550 {
551 /* continue from last position */
552 start_lsn = slot->data.confirmed_flush;
553 }
554 else if (start_lsn < slot->data.confirmed_flush)
555 {
556 /*
557 * It might seem like we should error out in this case, but it's
558 * pretty common for a client to acknowledge a LSN it doesn't have to
559 * do anything for, and thus didn't store persistently, because the
560 * xlog records didn't result in anything relevant for logical
561 * decoding. Clients have to be able to do that to support synchronous
562 * replication.
563 *
564 * Starting at a different LSN than requested might not catch certain
565 * kinds of client errors; so the client may wish to check that
566 * confirmed_flush_lsn matches its expectations.
567 */
568 elog(LOG, "%X/%X has been already streamed, forwarding to %X/%X",
569 LSN_FORMAT_ARGS(start_lsn),
571
572 start_lsn = slot->data.confirmed_flush;
573 }
574
575 ctx = StartupDecodingContext(output_plugin_options,
576 start_lsn, InvalidTransactionId, false,
577 fast_forward, false, xl_routine, prepare_write,
578 do_write, update_progress);
579
580 /* call output plugin initialization callback */
581 old_context = MemoryContextSwitchTo(ctx->context);
582 if (ctx->callbacks.startup_cb != NULL)
583 startup_cb_wrapper(ctx, &ctx->options, false);
584 MemoryContextSwitchTo(old_context);
585
586 /*
587 * We allow decoding of prepared transactions when the two_phase is
588 * enabled at the time of slot creation, or when the two_phase option is
589 * given at the streaming start, provided the plugin supports all the
590 * callbacks for two-phase.
591 */
592 ctx->twophase &= (slot->data.two_phase || ctx->twophase_opt_given);
593
594 /* Mark slot to allow two_phase decoding if not already marked */
595 if (ctx->twophase && !slot->data.two_phase)
596 {
597 SpinLockAcquire(&slot->mutex);
598 slot->data.two_phase = true;
599 slot->data.two_phase_at = start_lsn;
600 SpinLockRelease(&slot->mutex);
604 }
605
607
608 ereport(LOG,
609 (errmsg("starting logical decoding for slot \"%s\"",
610 NameStr(slot->data.name)),
611 errdetail("Streaming transactions committing after %X/%X, reading WAL from %X/%X.",
614
615 return ctx;
616}
#define NameStr(name)
Definition: c.h:717
int errdetail(const char *fmt,...)
Definition: elog.c:1204
int errhint(const char *fmt,...)
Definition: elog.c:1318
#define LOG
Definition: elog.h:31
#define elog(elevel,...)
Definition: elog.h:226
static void startup_cb_wrapper(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
Definition: logical.c:772
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:150
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
const void * data
void ReplicationSlotMarkDirty(void)
Definition: slot.c:1061
ReplicationSlot * MyReplicationSlot
Definition: slot.c:147
void ReplicationSlotSave(void)
Definition: slot.c:1043
#define SlotIsPhysical(slot)
Definition: slot.h:220
@ RS_INVAL_NONE
Definition: slot.h:53
bool IsSyncingReplicationSlots(void)
Definition: slotsync.c:1653
void SnapBuildSetTwoPhaseAt(SnapBuild *builder, XLogRecPtr ptr)
Definition: snapbuild.c:295
#define SpinLockRelease(lock)
Definition: spin.h:61
#define SpinLockAcquire(lock)
Definition: spin.h:59
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
XLogRecPtr confirmed_flush
Definition: slot.h:111
ReplicationSlotInvalidationCause invalidated
Definition: slot.h:103
slock_t mutex
Definition: slot.h:158
ReplicationSlotPersistentData data
Definition: slot.h:185
#define InvalidTransactionId
Definition: transam.h:31
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28

References Assert(), LogicalDecodingContext::callbacks, ReplicationSlotPersistentData::confirmed_flush, LogicalDecodingContext::context, ReplicationSlot::data, data, ReplicationSlotPersistentData::database, elog, ereport, errcode(), errdetail(), errhint(), errmsg(), ERROR, ReplicationSlotPersistentData::invalidated, InvalidTransactionId, InvalidXLogRecPtr, IsSyncingReplicationSlots(), LOG, 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, and LogicalDecodingContext::twophase_opt_given.

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

◆ CreateInitDecodingContext()

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

Definition at line 330 of file logical.c.

338{
339 TransactionId xmin_horizon = InvalidTransactionId;
340 ReplicationSlot *slot;
341 NameData plugin_name;
343 MemoryContext old_context;
344
345 /*
346 * On a standby, this check is also required while creating the slot.
347 * Check the comments in the function.
348 */
350
351 /* shorter lines... */
352 slot = MyReplicationSlot;
353
354 /* first some sanity checks that are unlikely to be violated */
355 if (slot == NULL)
356 elog(ERROR, "cannot perform logical decoding without an acquired slot");
357
358 if (plugin == NULL)
359 elog(ERROR, "cannot initialize logical decoding without a specified plugin");
360
361 /* Make sure the passed slot is suitable. These are user facing errors. */
362 if (SlotIsPhysical(slot))
364 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
365 errmsg("cannot use physical replication slot for logical decoding")));
366
367 if (slot->data.database != MyDatabaseId)
369 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
370 errmsg("replication slot \"%s\" was not created in this database",
371 NameStr(slot->data.name))));
372
373 if (IsTransactionState() &&
376 (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
377 errmsg("cannot create logical replication slot in transaction that has performed writes")));
378
379 /*
380 * Register output plugin name with slot. We need the mutex to avoid
381 * concurrent reading of a partially copied string. But we don't want any
382 * complicated code while holding a spinlock, so do namestrcpy() outside.
383 */
384 namestrcpy(&plugin_name, plugin);
385 SpinLockAcquire(&slot->mutex);
386 slot->data.plugin = plugin_name;
387 SpinLockRelease(&slot->mutex);
388
389 if (XLogRecPtrIsInvalid(restart_lsn))
391 else
392 {
393 SpinLockAcquire(&slot->mutex);
394 slot->data.restart_lsn = restart_lsn;
395 SpinLockRelease(&slot->mutex);
396 }
397
398 /* ----
399 * This is a bit tricky: We need to determine a safe xmin horizon to start
400 * decoding from, to avoid starting from a running xacts record referring
401 * to xids whose rows have been vacuumed or pruned
402 * already. GetOldestSafeDecodingTransactionId() returns such a value, but
403 * without further interlock its return value might immediately be out of
404 * date.
405 *
406 * So we have to acquire the ProcArrayLock to prevent computation of new
407 * xmin horizons by other backends, get the safe decoding xid, and inform
408 * the slot machinery about the new limit. Once that's done the
409 * ProcArrayLock can be released as the slot machinery now is
410 * protecting against vacuum.
411 *
412 * Note that, temporarily, the data, not just the catalog, xmin has to be
413 * reserved if a data snapshot is to be exported. Otherwise the initial
414 * data snapshot created here is not guaranteed to be valid. After that
415 * the data xmin doesn't need to be managed anymore and the global xmin
416 * should be recomputed. As we are fine with losing the pegged data xmin
417 * after crash - no chance a snapshot would get exported anymore - we can
418 * get away with just setting the slot's
419 * effective_xmin. ReplicationSlotRelease will reset it again.
420 *
421 * ----
422 */
423 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
424
425 xmin_horizon = GetOldestSafeDecodingTransactionId(!need_full_snapshot);
426
427 SpinLockAcquire(&slot->mutex);
428 slot->effective_catalog_xmin = xmin_horizon;
429 slot->data.catalog_xmin = xmin_horizon;
430 if (need_full_snapshot)
431 slot->effective_xmin = xmin_horizon;
432 SpinLockRelease(&slot->mutex);
433
435
436 LWLockRelease(ProcArrayLock);
437
440
441 ctx = StartupDecodingContext(NIL, restart_lsn, xmin_horizon,
442 need_full_snapshot, false, true,
443 xl_routine, prepare_write, do_write,
444 update_progress);
445
446 /* call output plugin initialization callback */
447 old_context = MemoryContextSwitchTo(ctx->context);
448 if (ctx->callbacks.startup_cb != NULL)
449 startup_cb_wrapper(ctx, &ctx->options, true);
450 MemoryContextSwitchTo(old_context);
451
452 /*
453 * We allow decoding of prepared transactions when the two_phase is
454 * enabled at the time of slot creation, or when the two_phase option is
455 * given at the streaming start, provided the plugin supports all the
456 * callbacks for two-phase.
457 */
458 ctx->twophase &= slot->data.two_phase;
459
461
462 return ctx;
463}
uint32 TransactionId
Definition: c.h:623
void CheckLogicalDecodingRequirements(void)
Definition: logical.c:109
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1182
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1902
@ LW_EXCLUSIVE
Definition: lwlock.h:114
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:2945
void ReplicationSlotReserveWal(void)
Definition: slot.c:1452
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:1100
TransactionId catalog_xmin
Definition: slot.h:97
TransactionId effective_catalog_xmin
Definition: slot.h:182
TransactionId effective_xmin
Definition: slot.h:181
Definition: c.h:712
bool IsTransactionState(void)
Definition: xact.c:387
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:441
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29

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, 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 XLogRecPtrIsInvalid.

Referenced by create_logical_replication_slot(), and CreateReplicationSlot().

◆ DecodingContextFindStartpoint()

void DecodingContextFindStartpoint ( LogicalDecodingContext ctx)

Definition at line 631 of file logical.c.

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

References CHECK_FOR_INTERRUPTS, ReplicationSlotPersistentData::confirmed_flush, ReplicationSlot::data, DEBUG1, DecodingContextReady(), elog, XLogReaderState::EndRecPtr, err(), ERROR, 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(), and CreateReplicationSlot().

◆ DecodingContextReady()

bool DecodingContextReady ( LogicalDecodingContext ctx)

Definition at line 622 of file logical.c.

623{
625}
SnapBuildState SnapBuildCurrentState(SnapBuild *builder)
Definition: snapbuild.c:277
@ SNAPBUILD_CONSISTENT
Definition: snapbuild.h:50

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,
RepOriginId  origin_id 
)

Definition at line 1197 of file logical.c.

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

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

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

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

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

Referenced by StartupDecodingContext().

◆ LogicalConfirmReceivedLocation()

void LogicalConfirmReceivedLocation ( XLogRecPtr  lsn)

Definition at line 1818 of file logical.c.

1819{
1820 Assert(lsn != InvalidXLogRecPtr);
1821
1822 /* Do an unlocked check for candidate_lsn first. */
1825 {
1826 bool updated_xmin = false;
1827 bool updated_restart = false;
1828
1830
1832
1833 /* if we're past the location required for bumping xmin, do so */
1836 {
1837 /*
1838 * We have to write the changed xmin to disk *before* we change
1839 * the in-memory value, otherwise after a crash we wouldn't know
1840 * that some catalog tuples might have been removed already.
1841 *
1842 * Ensure that by first writing to ->xmin and only update
1843 * ->effective_xmin once the new state is synced to disk. After a
1844 * crash ->effective_xmin is set to ->xmin.
1845 */
1848 {
1852 updated_xmin = true;
1853 }
1854 }
1855
1858 {
1860
1864 updated_restart = true;
1865 }
1866
1868
1869 /* first write new xmin to disk, so we know what's up after a crash */
1870 if (updated_xmin || updated_restart)
1871 {
1874 elog(DEBUG1, "updated xmin: %u restart: %u", updated_xmin, updated_restart);
1875 }
1876
1877 /*
1878 * Now the new xmin is safely on disk, we can let the global value
1879 * advance. We do not take ProcArrayLock or similar since we only
1880 * advance xmin here and there's not much harm done by a concurrent
1881 * computation missing that.
1882 */
1883 if (updated_xmin)
1884 {
1888
1891 }
1892 }
1893 else
1894 {
1898 }
1899}
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:1156
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:201
XLogRecPtr candidate_restart_valid
Definition: slot.h:202
XLogRecPtr candidate_restart_lsn
Definition: slot.h:203
TransactionId candidate_catalog_xmin
Definition: slot.h:200
#define TransactionIdIsValid(xid)
Definition: transam.h:41

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

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

◆ LogicalIncreaseRestartDecodingForSlot()

void LogicalIncreaseRestartDecodingForSlot ( XLogRecPtr  current_lsn,
XLogRecPtr  restart_lsn 
)

Definition at line 1742 of file logical.c.

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

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

Referenced by SnapBuildProcessRunningXacts().

◆ LogicalIncreaseXminForSlot()

void LogicalIncreaseXminForSlot ( XLogRecPtr  current_lsn,
TransactionId  xmin 
)

Definition at line 1674 of file logical.c.

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

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

Referenced by SnapBuildProcessRunningXacts().

◆ LogicalReplicationSlotHasPendingWal()

bool LogicalReplicationSlotHasPendingWal ( XLogRecPtr  end_of_wal)

Definition at line 1962 of file logical.c.

1963{
1964 bool has_pending_wal = false;
1965
1967
1968 PG_TRY();
1969 {
1971
1972 /*
1973 * Create our decoding context in fast_forward mode, passing start_lsn
1974 * as InvalidXLogRecPtr, so that we start processing from the slot's
1975 * confirmed_flush.
1976 */
1978 NIL,
1979 true, /* fast_forward */
1980 XL_ROUTINE(.page_read = read_local_xlog_page,
1981 .segment_open = wal_segment_open,
1982 .segment_close = wal_segment_close),
1983 NULL, NULL, NULL);
1984
1985 /*
1986 * Start reading at the slot's restart_lsn, which we know points to a
1987 * valid record.
1988 */
1990
1991 /* Invalidate non-timetravel entries */
1993
1994 /* Loop until the end of WAL or some changes are processed */
1995 while (!has_pending_wal && ctx->reader->EndRecPtr < end_of_wal)
1996 {
1997 XLogRecord *record;
1998 char *errm = NULL;
1999
2000 record = XLogReadRecord(ctx->reader, &errm);
2001
2002 if (errm)
2003 elog(ERROR, "could not find record for logical decoding: %s", errm);
2004
2005 if (record != NULL)
2007
2008 has_pending_wal = ctx->processing_required;
2009
2011 }
2012
2013 /* Clean up */
2016 }
2017 PG_CATCH();
2018 {
2019 /* clear all timetravel entries */
2021
2022 PG_RE_THROW();
2023 }
2024 PG_END_TRY();
2025
2026 return has_pending_wal;
2027}
#define PG_RE_THROW()
Definition: elog.h:405
#define PG_TRY(...)
Definition: elog.h:372
#define PG_END_TRY(...)
Definition: elog.h:397
#define PG_CATCH(...)
Definition: elog.h:382
void InvalidateSystemCaches(void)
Definition: inval.c:916
void FreeDecodingContext(LogicalDecodingContext *ctx)
Definition: logical.c:675
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:496
#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, 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, ReplicationSlotPersistentData::restart_lsn, wal_segment_close(), wal_segment_open(), XL_ROUTINE, XLogBeginRead(), and XLogReadRecord().

Referenced by binary_upgrade_logical_slot_has_caught_up().

◆ LogicalSlotAdvanceAndCheckSnapState()

XLogRecPtr LogicalSlotAdvanceAndCheckSnapState ( XLogRecPtr  moveto,
bool *  found_consistent_snapshot 
)

Definition at line 2044 of file logical.c.

2046{
2048 ResourceOwner old_resowner = CurrentResourceOwner;
2049 XLogRecPtr retlsn;
2050
2051 Assert(moveto != InvalidXLogRecPtr);
2052
2053 if (found_consistent_snapshot)
2054 *found_consistent_snapshot = false;
2055
2056 PG_TRY();
2057 {
2058 /*
2059 * Create our decoding context in fast_forward mode, passing start_lsn
2060 * as InvalidXLogRecPtr, so that we start processing from my slot's
2061 * confirmed_flush.
2062 */
2064 NIL,
2065 true, /* fast_forward */
2066 XL_ROUTINE(.page_read = read_local_xlog_page,
2067 .segment_open = wal_segment_open,
2068 .segment_close = wal_segment_close),
2069 NULL, NULL, NULL);
2070
2071 /*
2072 * Wait for specified streaming replication standby servers (if any)
2073 * to confirm receipt of WAL up to moveto lsn.
2074 */
2076
2077 /*
2078 * Start reading at the slot's restart_lsn, which we know to point to
2079 * a valid record.
2080 */
2082
2083 /* invalidate non-timetravel entries */
2085
2086 /* Decode records until we reach the requested target */
2087 while (ctx->reader->EndRecPtr < moveto)
2088 {
2089 char *errm = NULL;
2090 XLogRecord *record;
2091
2092 /*
2093 * Read records. No changes are generated in fast_forward mode,
2094 * but snapbuilder/slot statuses are updated properly.
2095 */
2096 record = XLogReadRecord(ctx->reader, &errm);
2097 if (errm)
2098 elog(ERROR, "could not find record while advancing replication slot: %s",
2099 errm);
2100
2101 /*
2102 * Process the record. Storage-level changes are ignored in
2103 * fast_forward mode, but other modules (such as snapbuilder)
2104 * might still have critical updates to do.
2105 */
2106 if (record)
2108
2110 }
2111
2112 if (found_consistent_snapshot && DecodingContextReady(ctx))
2113 *found_consistent_snapshot = true;
2114
2115 /*
2116 * Logical decoding could have clobbered CurrentResourceOwner during
2117 * transaction management, so restore the executor's value. (This is
2118 * a kluge, but it's not worth cleaning up right now.)
2119 */
2120 CurrentResourceOwner = old_resowner;
2121
2122 if (ctx->reader->EndRecPtr != InvalidXLogRecPtr)
2123 {
2125
2126 /*
2127 * If only the confirmed_flush LSN has changed the slot won't get
2128 * marked as dirty by the above. Callers on the walsender
2129 * interface are expected to keep track of their own progress and
2130 * don't need it written out. But SQL-interface users cannot
2131 * specify their own start positions and it's harder for them to
2132 * keep track of their progress, so we should make more of an
2133 * effort to save it for them.
2134 *
2135 * Dirty the slot so it is written out at the next checkpoint. The
2136 * LSN position advanced to may still be lost on a crash but this
2137 * makes the data consistent after a clean shutdown.
2138 */
2140 }
2141
2143
2144 /* free context, call shutdown callback */
2146
2148 }
2149 PG_CATCH();
2150 {
2151 /* clear all timetravel entries */
2153
2154 PG_RE_THROW();
2155 }
2156 PG_END_TRY();
2157
2158 return retlsn;
2159}
ResourceOwner CurrentResourceOwner
Definition: resowner.c:173
void WaitForStandbyConfirmation(XLogRecPtr wait_for_lsn)
Definition: slot.c:2959

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

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

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

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert(), ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, error_context_stack, LogicalDecodingContext::fast_forward, 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 753 of file logical.c.

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

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

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

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

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

Referenced by pg_decode_change(), pg_decode_commit_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()

void OutputPluginUpdateProgress ( struct LogicalDecodingContext ctx,
bool  skipped_xact 
)

◆ OutputPluginWrite()

◆ prepare_cb_wrapper()

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

Definition at line 947 of file logical.c.

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

1906{
1908 bsysscan = false;
1909}
bool bsysscan
Definition: xact.c:100
TransactionId CheckXidAlive
Definition: xact.c:99

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

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

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

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

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

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::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 150 of file logical.c.

160{
161 ReplicationSlot *slot;
162 MemoryContext context,
163 old_context;
165
166 /* shorter lines... */
167 slot = MyReplicationSlot;
168
170 "Logical decoding context",
172 old_context = MemoryContextSwitchTo(context);
173 ctx = palloc0(sizeof(LogicalDecodingContext));
174
175 ctx->context = context;
176
177 /*
178 * (re-)load output plugins, so we detect a bad (removed) output plugin
179 * now.
180 */
181 if (!fast_forward)
183
184 /*
185 * Now that the slot's xmin has been set, we can announce ourselves as a
186 * logical decoding backend which doesn't need to be checked individually
187 * when computing the xmin horizon because the xmin is enforced via
188 * replication slots.
189 *
190 * We can only do so if we're outside of a transaction (i.e. the case when
191 * streaming changes via walsender), otherwise an already setup
192 * snapshot/xid would end up being ignored. That's not a particularly
193 * bothersome restriction since the SQL interface can't be used for
194 * streaming anyway.
195 */
197 {
198 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
201 LWLockRelease(ProcArrayLock);
202 }
203
204 ctx->slot = slot;
205
206 ctx->reader = XLogReaderAllocate(wal_segment_size, NULL, xl_routine, ctx);
207 if (!ctx->reader)
209 (errcode(ERRCODE_OUT_OF_MEMORY),
210 errmsg("out of memory"),
211 errdetail("Failed while allocating a WAL reading processor.")));
212
214 ctx->snapshot_builder =
215 AllocateSnapshotBuilder(ctx->reorder, xmin_horizon, start_lsn,
216 need_full_snapshot, in_create, slot->data.two_phase_at);
217
218 ctx->reorder->private_data = ctx;
219
220 /* wrap output plugin callbacks, so we can add error context information */
226
227 /*
228 * To support streaming, we require start/stop/abort/commit/change
229 * callbacks. The message and truncate callbacks are optional, similar to
230 * regular output plugins. We however enable streaming when at least one
231 * of the methods is enabled so that we can easily identify missing
232 * methods.
233 *
234 * We decide it here, but only check it later in the wrappers.
235 */
236 ctx->streaming = (ctx->callbacks.stream_start_cb != NULL) ||
237 (ctx->callbacks.stream_stop_cb != NULL) ||
238 (ctx->callbacks.stream_abort_cb != NULL) ||
239 (ctx->callbacks.stream_commit_cb != NULL) ||
240 (ctx->callbacks.stream_change_cb != NULL) ||
241 (ctx->callbacks.stream_message_cb != NULL) ||
242 (ctx->callbacks.stream_truncate_cb != NULL);
243
244 /*
245 * streaming callbacks
246 *
247 * stream_message and stream_truncate callbacks are optional, so we do not
248 * fail with ERROR when missing, but the wrappers simply do nothing. We
249 * must set the ReorderBuffer callbacks to something, otherwise the calls
250 * from there will crash (we don't want to move the checks there).
251 */
260
261
262 /*
263 * To support two-phase logical decoding, we require
264 * begin_prepare/prepare/commit-prepare/abort-prepare callbacks. The
265 * filter_prepare callback is optional. We however enable two-phase
266 * logical decoding when at least one of the methods is enabled so that we
267 * can easily identify missing methods.
268 *
269 * We decide it here, but only check it later in the wrappers.
270 */
271 ctx->twophase = (ctx->callbacks.begin_prepare_cb != NULL) ||
272 (ctx->callbacks.prepare_cb != NULL) ||
273 (ctx->callbacks.commit_prepared_cb != NULL) ||
274 (ctx->callbacks.rollback_prepared_cb != NULL) ||
275 (ctx->callbacks.stream_prepare_cb != NULL) ||
276 (ctx->callbacks.filter_prepare_cb != NULL);
277
278 /*
279 * Callback to support decoding at prepare time.
280 */
285
286 /*
287 * Callback to support updating progress during sending data of a
288 * transaction (and its subtransactions) to the output plugin.
289 */
291
292 ctx->out = makeStringInfo();
293 ctx->prepare_write = prepare_write;
294 ctx->write = do_write;
295 ctx->update_progress = update_progress;
296
297 ctx->output_plugin_options = output_plugin_options;
298
299 ctx->fast_forward = fast_forward;
300
301 MemoryContextSwitchTo(old_context);
302
303 return ctx;
304}
static void change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
Definition: logical.c:1084
static void commit_prepared_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:992
static void update_progress_txn_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr lsn)
Definition: logical.c:1627
static void stream_start_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr first_lsn)
Definition: logical.c:1265
static void commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:864
static void begin_prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
Definition: logical.c:903
static void stream_prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
Definition: logical.c:1404
static void prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
Definition: logical.c:947
static void stream_truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
Definition: logical.c:1580
static void truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
Definition: logical.c:1123
static void begin_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
Definition: logical.c:833
static void rollback_prepared_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_end_lsn, TimestampTz prepare_time)
Definition: logical.c:1037
static void stream_change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
Definition: logical.c:1490
static void stream_abort_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr abort_lsn)
Definition: logical.c:1363
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:1539
static void stream_commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:1449
static void LoadOutputPlugin(OutputPluginCallbacks *callbacks, const char *plugin)
Definition: logical.c:731
static void stream_stop_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr last_lsn)
Definition: logical.c:1314
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:1228
void * palloc0(Size size)
Definition: mcxt.c:1970
MemoryContext CurrentMemoryContext
Definition: mcxt.c:159
#define AllocSetContextCreate
Definition: memutils.h:149
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:180
#define PROC_IN_LOGICAL_DECODING
Definition: proc.h:61
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:185
PGPROC * MyProc
Definition: proc.c:67
PROC_HDR * ProcGlobal
Definition: proc.c:79
StringInfo makeStringInfo(void)
Definition: stringinfo.c:72
StringInfo out
Definition: logical.h:71
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:243
int pgxactoff
Definition: proc.h:185
uint8 * statusFlags
Definition: proc.h:387
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:4989
int wal_segment_size
Definition: xlog.c:143
XLogReaderState * XLogReaderAllocate(int wal_segment_size, const char *waldir, XLogReaderRoutine *routine, void *private_data)
Definition: xlogreader.c:107

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, OutputPluginCallbacks::filter_prepare_cb, IsTransactionOrTransactionBlock(), LoadOutputPlugin(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), makeStringInfo(), MemoryContextSwitchTo(), ReorderBuffer::message, message_cb_wrapper(), MyProc, MyReplicationSlot, NameStr, LogicalDecodingContext::out, LogicalDecodingContext::output_plugin_options, palloc0(), 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 1363 of file logical.c.

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

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert(), ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, ereport, errcode(), errmsg(), ERROR, error_context_stack, LogicalDecodingContext::fast_forward, 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 1490 of file logical.c.

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

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert(), ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, ereport, errcode(), errmsg(), ERROR, error_context_stack, LogicalDecodingContext::fast_forward, 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 1449 of file logical.c.

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

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

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

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert(), ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, error_context_stack, LogicalDecodingContext::fast_forward, 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 1404 of file logical.c.

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

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

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

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert(), ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, ereport, errcode(), errmsg(), ERROR, error_context_stack, LogicalDecodingContext::fast_forward, 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 1314 of file logical.c.

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

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert(), ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, ereport, errcode(), errmsg(), ERROR, error_context_stack, LogicalDecodingContext::fast_forward, 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 1580 of file logical.c.

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

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert(), ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, error_context_stack, LogicalDecodingContext::fast_forward, 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 1123 of file logical.c.

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

References LogicalDecodingContext::accept_writes, ErrorContextCallback::arg, Assert(), ErrorContextCallback::callback, LogicalDecodingContext::callbacks, LogicalDecodingContext::end_xact, error_context_stack, LogicalDecodingContext::fast_forward, 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 1627 of file logical.c.

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

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

1916{
1917 ReorderBuffer *rb = ctx->reorder;
1918 PgStat_StatReplSlotEntry repSlotStat;
1919
1920 /* Nothing to do if we don't have any replication stats to be sent. */
1921 if (rb->spillBytes <= 0 && rb->streamBytes <= 0 && rb->totalBytes <= 0)
1922 return;
1923
1924 elog(DEBUG2, "UpdateDecodingStats: updating stats %p %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,
1925 rb,
1926 rb->spillTxns,
1927 rb->spillCount,
1928 rb->spillBytes,
1929 rb->streamTxns,
1930 rb->streamCount,
1931 rb->streamBytes,
1932 rb->totalTxns,
1933 rb->totalBytes);
1934
1935 repSlotStat.spill_txns = rb->spillTxns;
1936 repSlotStat.spill_count = rb->spillCount;
1937 repSlotStat.spill_bytes = rb->spillBytes;
1938 repSlotStat.stream_txns = rb->streamTxns;
1939 repSlotStat.stream_count = rb->streamCount;
1940 repSlotStat.stream_bytes = rb->streamBytes;
1941 repSlotStat.total_txns = rb->totalTxns;
1942 repSlotStat.total_bytes = rb->totalBytes;
1943
1944 pgstat_report_replslot(ctx->slot, &repSlotStat);
1945
1946 rb->spillTxns = 0;
1947 rb->spillCount = 0;
1948 rb->spillBytes = 0;
1949 rb->streamTxns = 0;
1950 rb->streamCount = 0;
1951 rb->streamBytes = 0;
1952 rb->totalTxns = 0;
1953 rb->totalBytes = 0;
1954}
#define DEBUG2
Definition: elog.h:29
void pgstat_report_replslot(ReplicationSlot *slot, const PgStat_StatReplSlotEntry *repSlotStat)
PgStat_Counter stream_count
Definition: pgstat.h:394
PgStat_Counter total_txns
Definition: pgstat.h:396
PgStat_Counter total_bytes
Definition: pgstat.h:397
PgStat_Counter spill_txns
Definition: pgstat.h:390
PgStat_Counter stream_txns
Definition: pgstat.h:393
PgStat_Counter spill_count
Definition: pgstat.h:391
PgStat_Counter stream_bytes
Definition: pgstat.h:395
PgStat_Counter spill_bytes
Definition: pgstat.h:392

References DEBUG2, elog, pgstat_report_replslot(), LogicalDecodingContext::reorder, LogicalDecodingContext::slot, PgStat_StatReplSlotEntry::spill_bytes, PgStat_StatReplSlotEntry::spill_count, PgStat_StatReplSlotEntry::spill_txns, ReorderBuffer::spillBytes, ReorderBuffer::spillCount, ReorderBuffer::spillTxns, PgStat_StatReplSlotEntry::stream_bytes, PgStat_StatReplSlotEntry::stream_count, PgStat_StatReplSlotEntry::stream_txns, ReorderBuffer::streamBytes, ReorderBuffer::streamCount, ReorderBuffer::streamTxns, PgStat_StatReplSlotEntry::total_bytes, PgStat_StatReplSlotEntry::total_txns, ReorderBuffer::totalBytes, and ReorderBuffer::totalTxns.

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