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

Go to the source code of this file.

Data Structures

struct  LogicalErrorCallbackState
 

Typedefs

typedef struct LogicalErrorCallbackState LogicalErrorCallbackState
 

Functions

static void output_plugin_error_callback (void *arg)
 
static void startup_cb_wrapper (LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
 
static void shutdown_cb_wrapper (LogicalDecodingContext *ctx)
 
static void begin_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn)
 
static void commit_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
 
static void begin_prepare_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn)
 
static void prepare_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
 
static void commit_prepared_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
 
static void rollback_prepared_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_end_lsn, TimestampTz prepare_time)
 
static void change_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
 
static void truncate_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
 
static void message_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr message_lsn, bool transactional, const char *prefix, Size message_size, const char *message)
 
static void stream_start_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr first_lsn)
 
static void stream_stop_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr last_lsn)
 
static void stream_abort_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr abort_lsn)
 
static void stream_prepare_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
 
static void stream_commit_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
 
static void stream_change_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
 
static void stream_message_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr message_lsn, bool transactional, const char *prefix, Size message_size, const char *message)
 
static void stream_truncate_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
 
static void update_progress_txn_cb_wrapper (ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr lsn)
 
static void LoadOutputPlugin (OutputPluginCallbacks *callbacks, const char *plugin)
 
void CheckLogicalDecodingRequirements (bool repack)
 
static LogicalDecodingContextStartupDecodingContext (List *output_plugin_options, XLogRecPtr start_lsn, TransactionId xmin_horizon, bool need_full_snapshot, bool fast_forward, bool in_create, XLogReaderRoutine *xl_routine, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
 
LogicalDecodingContextCreateInitDecodingContext (const char *plugin, List *output_plugin_options, bool need_full_snapshot, bool for_repack, XLogRecPtr restart_lsn, XLogReaderRoutine *xl_routine, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
 
LogicalDecodingContextCreateDecodingContext (XLogRecPtr start_lsn, List *output_plugin_options, bool fast_forward, XLogReaderRoutine *xl_routine, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
 
bool DecodingContextReady (LogicalDecodingContext *ctx)
 
void DecodingContextFindStartpoint (LogicalDecodingContext *ctx)
 
void FreeDecodingContext (LogicalDecodingContext *ctx)
 
void OutputPluginPrepareWrite (struct LogicalDecodingContext *ctx, bool last_write)
 
void OutputPluginWrite (struct LogicalDecodingContext *ctx, bool last_write)
 
void OutputPluginUpdateProgress (struct LogicalDecodingContext *ctx, bool skipped_xact)
 
bool filter_prepare_cb_wrapper (LogicalDecodingContext *ctx, TransactionId xid, const char *gid)
 
bool filter_by_origin_cb_wrapper (LogicalDecodingContext *ctx, ReplOriginId origin_id)
 
void LogicalIncreaseXminForSlot (XLogRecPtr current_lsn, TransactionId xmin)
 
void LogicalIncreaseRestartDecodingForSlot (XLogRecPtr current_lsn, XLogRecPtr restart_lsn)
 
void LogicalConfirmReceivedLocation (XLogRecPtr lsn)
 
void ResetLogicalStreamingState (void)
 
void UpdateDecodingStats (LogicalDecodingContext *ctx)
 
XLogRecPtr LogicalReplicationSlotCheckPendingWal (XLogRecPtr end_of_wal, XLogRecPtr scan_cutoff_lsn)
 
XLogRecPtr LogicalSlotAdvanceAndCheckSnapState (XLogRecPtr moveto, bool *found_consistent_snapshot)
 

Typedef Documentation

◆ LogicalErrorCallbackState

Function Documentation

◆ begin_cb_wrapper()

static void begin_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn 
)
static

Definition at line 828 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ begin_prepare_cb_wrapper()

static void begin_prepare_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn 
)
static

Definition at line 898 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ change_cb_wrapper()

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

Definition at line 1079 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ CheckLogicalDecodingRequirements()

void CheckLogicalDecodingRequirements ( bool  repack)

Definition at line 111 of file logical.c.

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

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

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

◆ commit_cb_wrapper()

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

Definition at line 859 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ commit_prepared_cb_wrapper()

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

Definition at line 987 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ CreateDecodingContext()

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

Definition at line 491 of file logical.c.

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

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

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

◆ CreateInitDecodingContext()

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

Definition at line 322 of file logical.c.

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

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

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

◆ DecodingContextFindStartpoint()

void DecodingContextFindStartpoint ( LogicalDecodingContext ctx)

Definition at line 626 of file logical.c.

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

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

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

◆ DecodingContextReady()

bool DecodingContextReady ( LogicalDecodingContext ctx)

Definition at line 617 of file logical.c.

618{
620}
SnapBuildState SnapBuildCurrentState(SnapBuild *builder)
Definition snapbuild.c:281
@ SNAPBUILD_CONSISTENT
Definition snapbuild.h:58

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

Referenced by DecodingContextFindStartpoint(), and LogicalSlotAdvanceAndCheckSnapState().

◆ filter_by_origin_cb_wrapper()

bool filter_by_origin_cb_wrapper ( LogicalDecodingContext ctx,
ReplOriginId  origin_id 
)

Definition at line 1192 of file logical.c.

1193{
1195 ErrorContextCallback errcallback;
1196 bool ret;
1197
1198 Assert(!ctx->fast_forward);
1199
1200 /* Push callback + info on the error context stack */
1201 state.ctx = ctx;
1202 state.callback_name = "filter_by_origin";
1203 state.report_location = InvalidXLogRecPtr;
1205 errcallback.arg = &state;
1206 errcallback.previous = error_context_stack;
1207 error_context_stack = &errcallback;
1208
1209 /* set output state */
1210 ctx->accept_writes = false;
1211 ctx->end_xact = false;
1212
1213 /* do the actual work: call callback */
1214 ret = ctx->callbacks.filter_by_origin_cb(ctx, origin_id);
1215
1216 /* Pop the error context stack */
1217 error_context_stack = errcallback.previous;
1218
1219 return ret;
1220}
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 1160 of file logical.c.

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

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

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

Referenced by StartupDecodingContext().

◆ LogicalConfirmReceivedLocation()

void LogicalConfirmReceivedLocation ( XLogRecPtr  lsn)

Definition at line 1813 of file logical.c.

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

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

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

◆ LogicalIncreaseRestartDecodingForSlot()

void LogicalIncreaseRestartDecodingForSlot ( XLogRecPtr  current_lsn,
XLogRecPtr  restart_lsn 
)

Definition at line 1737 of file logical.c.

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

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

Referenced by decode_concurrent_changes(), and SnapBuildProcessRunningXacts().

◆ LogicalIncreaseXminForSlot()

void LogicalIncreaseXminForSlot ( XLogRecPtr  current_lsn,
TransactionId  xmin 
)

Definition at line 1669 of file logical.c.

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

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

Referenced by SnapBuildProcessRunningXacts().

◆ LogicalReplicationSlotCheckPendingWal()

XLogRecPtr LogicalReplicationSlotCheckPendingWal ( XLogRecPtr  end_of_wal,
XLogRecPtr  scan_cutoff_lsn 
)

Definition at line 2006 of file logical.c.

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

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

Referenced by binary_upgrade_check_logical_slot_pending_wal().

◆ LogicalSlotAdvanceAndCheckSnapState()

XLogRecPtr LogicalSlotAdvanceAndCheckSnapState ( XLogRecPtr  moveto,
bool found_consistent_snapshot 
)

Definition at line 2102 of file logical.c.

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

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

Referenced by pg_logical_replication_slot_advance(), and update_local_synced_slot().

◆ message_cb_wrapper()

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

Definition at line 1223 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ output_plugin_error_callback()

static void output_plugin_error_callback ( void arg)
static

Definition at line 748 of file logical.c.

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

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

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

◆ OutputPluginPrepareWrite()

void OutputPluginPrepareWrite ( struct LogicalDecodingContext ctx,
bool  last_write 
)

Definition at line 685 of file logical.c.

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

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

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

◆ OutputPluginUpdateProgress()

◆ OutputPluginWrite()

◆ prepare_cb_wrapper()

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

Definition at line 942 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ ResetLogicalStreamingState()

void ResetLogicalStreamingState ( void  )

Definition at line 1941 of file logical.c.

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

References bsysscan, CheckXidAlive, and InvalidTransactionId.

Referenced by AbortSubTransaction(), and AbortTransaction().

◆ rollback_prepared_cb_wrapper()

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

Definition at line 1032 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ shutdown_cb_wrapper()

static void shutdown_cb_wrapper ( LogicalDecodingContext ctx)
static

Definition at line 795 of file logical.c.

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

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

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

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

Referenced by CreateDecodingContext(), and CreateInitDecodingContext().

◆ StartupDecodingContext()

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

Definition at line 141 of file logical.c.

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

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

Referenced by CreateDecodingContext(), and CreateInitDecodingContext().

◆ stream_abort_cb_wrapper()

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

Definition at line 1358 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ stream_change_cb_wrapper()

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

Definition at line 1485 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ stream_commit_cb_wrapper()

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

Definition at line 1444 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ stream_message_cb_wrapper()

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

Definition at line 1534 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ stream_prepare_cb_wrapper()

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

Definition at line 1399 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ stream_start_cb_wrapper()

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

Definition at line 1260 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ stream_stop_cb_wrapper()

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

Definition at line 1309 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ stream_truncate_cb_wrapper()

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

Definition at line 1575 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ truncate_cb_wrapper()

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

Definition at line 1118 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ update_progress_txn_cb_wrapper()

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

Definition at line 1622 of file logical.c.

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

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

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

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

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