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

Go to the source code of this file.

Data Structures

struct  LogicalErrorCallbackState
 

Typedefs

typedef struct LogicalErrorCallbackState LogicalErrorCallbackState
 

Functions

static void output_plugin_error_callback (void *arg)
 
static void startup_cb_wrapper (LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
 
static void shutdown_cb_wrapper (LogicalDecodingContext *ctx)
 
static void begin_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn)
 
static void commit_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
 
static void begin_prepare_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn)
 
static void prepare_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
 
static void commit_prepared_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
 
static void rollback_prepared_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_end_lsn, TimestampTz prepare_time)
 
static void change_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
 
static void truncate_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
 
static void message_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr message_lsn, bool transactional, const char *prefix, Size message_size, const char *message)
 
static void stream_start_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr first_lsn)
 
static void stream_stop_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr last_lsn)
 
static void stream_abort_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr abort_lsn)
 
static void stream_prepare_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
 
static void stream_commit_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
 
static void stream_change_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
 
static void stream_message_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr message_lsn, bool transactional, const char *prefix, Size message_size, const char *message)
 
static void stream_truncate_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
 
static void update_progress_txn_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr lsn)
 
static void LoadOutputPlugin (OutputPluginCallbacks *callbacks, const char *plugin)
 
void CheckLogicalDecodingRequirements (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 835 of file logical.c.

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

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

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

112{
114
115 /*
116 * NB: Adding a new requirement likely means that RestoreSlotFromDisk()
117 * needs the same check.
118 */
119
122 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
123 errmsg("logical decoding requires \"wal_level\" >= \"logical\"")));
124
127 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
128 errmsg("logical decoding requires a database connection")));
129
130 if (RecoveryInProgress())
131 {
132 /*
133 * This check may have race conditions, but whenever
134 * XLOG_PARAMETER_CHANGE indicates that wal_level has changed, we
135 * verify that there are no existing logical replication slots. And to
136 * avoid races around creating a new slot,
137 * CheckLogicalDecodingRequirements() is called once before creating
138 * the slot, and once when logical decoding is initially starting up.
139 */
142 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
143 errmsg("logical decoding on standby requires \"wal_level\" >= \"logical\" on the primary")));
144 }
145}
Oid MyDatabaseId
Definition: globals.c:94
#define InvalidOid
Definition: postgres_ext.h:37
void CheckSlotRequirements(void)
Definition: slot.c:1533
bool RecoveryInProgress(void)
Definition: xlog.c:6406
int wal_level
Definition: xlog.c:133
WalLevel GetActiveWalLevelOnStandby(void)
Definition: xlog.c:4915
@ 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 866 of file logical.c.

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

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

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

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

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

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

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 XLogRecPtrIsValid.

Referenced by create_logical_replication_slot(), and CreateReplicationSlot().

◆ DecodingContextFindStartpoint()

void DecodingContextFindStartpoint ( LogicalDecodingContext ctx)

Definition at line 633 of file logical.c.

634{
635 ReplicationSlot *slot = ctx->slot;
636
637 /* Initialize from where to start reading WAL. */
639
640 elog(DEBUG1, "searching for logical decoding starting point, starting at %X/%08X",
642
643 /* Wait for a consistent starting point */
644 for (;;)
645 {
646 XLogRecord *record;
647 char *err = NULL;
648
649 /* the read_page callback waits for new WAL */
650 record = XLogReadRecord(ctx->reader, &err);
651 if (err)
652 elog(ERROR, "could not find logical decoding starting point: %s", err);
653 if (!record)
654 elog(ERROR, "could not find logical decoding starting point");
655
657
658 /* only continue till we found a consistent spot */
659 if (DecodingContextReady(ctx))
660 break;
661
663 }
664
665 SpinLockAcquire(&slot->mutex);
666 slot->data.confirmed_flush = ctx->reader->EndRecPtr;
667 if (slot->data.two_phase)
668 slot->data.two_phase_at = ctx->reader->EndRecPtr;
669 SpinLockRelease(&slot->mutex);
670}
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:624
#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:206
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 624 of file logical.c.

625{
627}
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 1199 of file logical.c.

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

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

Referenced by FilterByOrigin().

◆ filter_prepare_cb_wrapper()

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

Definition at line 1167 of file logical.c.

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

734{
735 LogicalOutputPluginInit plugin_init;
736
737 plugin_init = (LogicalOutputPluginInit)
738 load_external_function(plugin, "_PG_output_plugin_init", false, NULL);
739
740 if (plugin_init == NULL)
741 elog(ERROR, "output plugins have to declare the _PG_output_plugin_init symbol");
742
743 /* ask the output plugin to fill the callback struct */
744 plugin_init(callbacks);
745
746 if (callbacks->begin_cb == NULL)
747 elog(ERROR, "output plugins have to register a begin callback");
748 if (callbacks->change_cb == NULL)
749 elog(ERROR, "output plugins have to register a change callback");
750 if (callbacks->commit_cb == NULL)
751 elog(ERROR, "output plugins have to register a commit callback");
752}
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 1820 of file logical.c.

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

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

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

◆ LogicalIncreaseRestartDecodingForSlot()

void LogicalIncreaseRestartDecodingForSlot ( XLogRecPtr  current_lsn,
XLogRecPtr  restart_lsn 
)

Definition at line 1744 of file logical.c.

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

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

Referenced by SnapBuildProcessRunningXacts().

◆ LogicalIncreaseXminForSlot()

void LogicalIncreaseXminForSlot ( XLogRecPtr  current_lsn,
TransactionId  xmin 
)

Definition at line 1676 of file logical.c.

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

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

Referenced by SnapBuildProcessRunningXacts().

◆ LogicalReplicationSlotHasPendingWal()

bool LogicalReplicationSlotHasPendingWal ( XLogRecPtr  end_of_wal)

Definition at line 2003 of file logical.c.

2004{
2005 bool has_pending_wal = false;
2006
2008
2009 PG_TRY();
2010 {
2012
2013 /*
2014 * Create our decoding context in fast_forward mode, passing start_lsn
2015 * as InvalidXLogRecPtr, so that we start processing from the slot's
2016 * confirmed_flush.
2017 */
2019 NIL,
2020 true, /* fast_forward */
2021 XL_ROUTINE(.page_read = read_local_xlog_page,
2022 .segment_open = wal_segment_open,
2023 .segment_close = wal_segment_close),
2024 NULL, NULL, NULL);
2025
2026 /*
2027 * Start reading at the slot's restart_lsn, which we know points to a
2028 * valid record.
2029 */
2031
2032 /* Invalidate non-timetravel entries */
2034
2035 /* Loop until the end of WAL or some changes are processed */
2036 while (!has_pending_wal && ctx->reader->EndRecPtr < end_of_wal)
2037 {
2038 XLogRecord *record;
2039 char *errm = NULL;
2040
2041 record = XLogReadRecord(ctx->reader, &errm);
2042
2043 if (errm)
2044 elog(ERROR, "could not find record for logical decoding: %s", errm);
2045
2046 if (record != NULL)
2048
2049 has_pending_wal = ctx->processing_required;
2050
2052 }
2053
2054 /* Clean up */
2057 }
2058 PG_CATCH();
2059 {
2060 /* clear all timetravel entries */
2062
2063 PG_RE_THROW();
2064 }
2065 PG_END_TRY();
2066
2067 return has_pending_wal;
2068}
#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:677
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:498
#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 2085 of file logical.c.

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

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, PG_USED_FOR_ASSERTS_ONLY, read_local_xlog_page(), LogicalDecodingContext::reader, ReplicationSlotMarkDirty(), ReplicationSlotPersistentData::restart_lsn, WaitForStandbyConfirmation(), wal_segment_close(), wal_segment_open(), XL_ROUTINE, XLogBeginRead(), XLogReadRecord(), and XLogRecPtrIsValid.

Referenced by pg_logical_replication_slot_advance(), and update_local_synced_slot().

◆ message_cb_wrapper()

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

Definition at line 1230 of file logical.c.

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

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

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

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

◆ OutputPluginPrepareWrite()

void OutputPluginPrepareWrite ( struct LogicalDecodingContext ctx,
bool  last_write 
)

Definition at line 692 of file logical.c.

693{
694 if (!ctx->accept_writes)
695 elog(ERROR, "writes are only accepted in commit, begin and change callbacks");
696
697 ctx->prepare_write(ctx, ctx->write_location, ctx->write_xid, last_write);
698 ctx->prepared_write = true;
699}
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 949 of file logical.c.

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

1943{
1945 bsysscan = false;
1946}
bool bsysscan
Definition: xact.c:101
TransactionId CheckXidAlive
Definition: xact.c:100

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

References DEBUG2, elog, PgStat_StatReplSlotEntry::mem_exceeded_count, ReorderBuffer::memExceededCount, 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(), ReorderBufferCheckMemoryLimit(), ReorderBufferSerializeTXN(), and ReorderBufferStreamTXN().