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

Go to the source code of this file.

Data Structures

struct  LogicalErrorCallbackState
 

Typedefs

typedef struct LogicalErrorCallbackState LogicalErrorCallbackState
 

Functions

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

Typedef Documentation

◆ LogicalErrorCallbackState

Function Documentation

◆ begin_cb_wrapper()

static void begin_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn 
)
static

Definition at line 826 of file logical.c.

827{
830 ErrorContextCallback errcallback;
831
832 Assert(!ctx->fast_forward);
833
834 /* Push callback + info on the error context stack */
835 state.ctx = ctx;
836 state.callback_name = "begin";
837 state.report_location = txn->first_lsn;
839 errcallback.arg = &state;
840 errcallback.previous = error_context_stack;
841 error_context_stack = &errcallback;
842
843 /* set output state */
844 ctx->accept_writes = true;
845 ctx->write_xid = txn->xid;
846 ctx->write_location = txn->first_lsn;
847 ctx->end_xact = false;
848
849 /* do the actual work: call callback */
850 ctx->callbacks.begin_cb(ctx, txn);
851
852 /* Pop the error context stack */
853 error_context_stack = errcallback.previous;
854}
ErrorContextCallback * error_context_stack
Definition: elog.c:95
Assert(PointerIsAligned(start, uint64))
static void output_plugin_error_callback(void *arg)
Definition: logical.c:746
struct ErrorContextCallback * previous
Definition: elog.h:297
void(* callback)(void *arg)
Definition: elog.h:298
XLogRecPtr write_location
Definition: logical.h:108
OutputPluginCallbacks callbacks
Definition: logical.h:53
TransactionId write_xid
Definition: logical.h:109
LogicalDecodeBeginCB begin_cb
XLogRecPtr first_lsn
TransactionId xid
void * private_data
Definition: regguts.h:323

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

Referenced by StartupDecodingContext().

◆ begin_prepare_cb_wrapper()

static void begin_prepare_cb_wrapper ( ReorderBuffer cache,
ReorderBufferTXN txn 
)
static

Definition at line 896 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ change_cb_wrapper()

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

Definition at line 1077 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ CheckLogicalDecodingRequirements()

void CheckLogicalDecodingRequirements ( void  )

Definition at line 111 of file logical.c.

112{
114
115 /*
116 * NB: Adding a new requirement likely means that RestoreSlotFromDisk()
117 * needs the same check.
118 */
119
122 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
123 errmsg("logical decoding requires a database connection")));
124
125 /* CheckSlotRequirements() has already checked if wal_level >= 'replica' */
127
128 /* Check if logical decoding is available on standby */
131 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
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,...)
Definition: elog.c:1330
Oid MyDatabaseId
Definition: globals.c:94
bool IsLogicalDecodingEnabled(void)
Definition: logicalctl.c:204
#define InvalidOid
Definition: postgres_ext.h:37
void CheckSlotRequirements(void)
Definition: slot.c:1654
bool RecoveryInProgress(void)
Definition: xlog.c:6461
int wal_level
Definition: xlog.c:134
@ WAL_LEVEL_REPLICA
Definition: xlog.h:76

References Assert(), CheckSlotRequirements(), ereport, errcode(), errhint(), errmsg(), ERROR, 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(), and StartLogicalReplication().

◆ commit_cb_wrapper()

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

Definition at line 857 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ commit_prepared_cb_wrapper()

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

Definition at line 985 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ CreateDecodingContext()

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

Definition at line 489 of file logical.c.

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

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

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

◆ CreateInitDecodingContext()

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

Definition at line 321 of file logical.c.

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

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

Referenced by create_logical_replication_slot(), and CreateReplicationSlot().

◆ DecodingContextFindStartpoint()

void DecodingContextFindStartpoint ( LogicalDecodingContext ctx)

Definition at line 624 of file logical.c.

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

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

Referenced by create_logical_replication_slot(), and CreateReplicationSlot().

◆ DecodingContextReady()

bool DecodingContextReady ( LogicalDecodingContext ctx)

Definition at line 615 of file logical.c.

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

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

Referenced by DecodingContextFindStartpoint(), and LogicalSlotAdvanceAndCheckSnapState().

◆ filter_by_origin_cb_wrapper()

bool filter_by_origin_cb_wrapper ( LogicalDecodingContext ctx,
RepOriginId  origin_id 
)

Definition at line 1190 of file logical.c.

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

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

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

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

Referenced by StartupDecodingContext().

◆ LogicalConfirmReceivedLocation()

void LogicalConfirmReceivedLocation ( XLogRecPtr  lsn)

Definition at line 1811 of file logical.c.

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

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

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

◆ LogicalIncreaseRestartDecodingForSlot()

void LogicalIncreaseRestartDecodingForSlot ( XLogRecPtr  current_lsn,
XLogRecPtr  restart_lsn 
)

Definition at line 1735 of file logical.c.

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

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

Referenced by SnapBuildProcessRunningXacts().

◆ LogicalIncreaseXminForSlot()

void LogicalIncreaseXminForSlot ( XLogRecPtr  current_lsn,
TransactionId  xmin 
)

Definition at line 1667 of file logical.c.

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

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

Referenced by SnapBuildProcessRunningXacts().

◆ LogicalReplicationSlotHasPendingWal()

bool LogicalReplicationSlotHasPendingWal ( XLogRecPtr  end_of_wal)

Definition at line 1994 of file logical.c.

1995{
1996 bool has_pending_wal = false;
1997
1999
2000 PG_TRY();
2001 {
2003
2004 /*
2005 * Create our decoding context in fast_forward mode, passing start_lsn
2006 * as InvalidXLogRecPtr, so that we start processing from the slot's
2007 * confirmed_flush.
2008 */
2010 NIL,
2011 true, /* fast_forward */
2012 XL_ROUTINE(.page_read = read_local_xlog_page,
2013 .segment_open = wal_segment_open,
2014 .segment_close = wal_segment_close),
2015 NULL, NULL, NULL);
2016
2017 /*
2018 * Start reading at the slot's restart_lsn, which we know points to a
2019 * valid record.
2020 */
2022
2023 /* Invalidate non-timetravel entries */
2025
2026 /* Loop until the end of WAL or some changes are processed */
2027 while (!has_pending_wal && ctx->reader->EndRecPtr < end_of_wal)
2028 {
2029 XLogRecord *record;
2030 char *errm = NULL;
2031
2032 record = XLogReadRecord(ctx->reader, &errm);
2033
2034 if (errm)
2035 elog(ERROR, "could not find record for logical decoding: %s", errm);
2036
2037 if (record != NULL)
2039
2040 has_pending_wal = ctx->processing_required;
2041
2043 }
2044
2045 /* Clean up */
2048 }
2049 PG_CATCH();
2050 {
2051 /* clear all timetravel entries */
2053
2054 PG_RE_THROW();
2055 }
2056 PG_END_TRY();
2057
2058 return has_pending_wal;
2059}
#define PG_RE_THROW()
Definition: elog.h:405
#define PG_TRY(...)
Definition: elog.h:372
#define PG_END_TRY(...)
Definition: elog.h:397
#define PG_CATCH(...)
Definition: elog.h:382
void InvalidateSystemCaches(void)
Definition: inval.c:916
void FreeDecodingContext(LogicalDecodingContext *ctx)
Definition: logical.c:668
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:489
#define XL_ROUTINE(...)
Definition: xlogreader.h:117
void wal_segment_close(XLogReaderState *state)
Definition: xlogutils.c:831
void wal_segment_open(XLogReaderState *state, XLogSegNo nextSegNo, TimeLineID *tli_p)
Definition: xlogutils.c:806
int read_local_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetRecPtr, char *cur_page)
Definition: xlogutils.c:845

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

Referenced by binary_upgrade_logical_slot_has_caught_up().

◆ LogicalSlotAdvanceAndCheckSnapState()

XLogRecPtr LogicalSlotAdvanceAndCheckSnapState ( XLogRecPtr  moveto,
bool *  found_consistent_snapshot 
)

Definition at line 2076 of file logical.c.

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

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

Referenced by pg_logical_replication_slot_advance(), and update_local_synced_slot().

◆ message_cb_wrapper()

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

Definition at line 1221 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ output_plugin_error_callback()

static void output_plugin_error_callback ( void *  arg)
static

Definition at line 746 of file logical.c.

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

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

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

◆ OutputPluginPrepareWrite()

void OutputPluginPrepareWrite ( struct LogicalDecodingContext ctx,
bool  last_write 
)

Definition at line 683 of file logical.c.

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

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

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

◆ OutputPluginUpdateProgress()

void OutputPluginUpdateProgress ( struct LogicalDecodingContext ctx,
bool  skipped_xact 
)

◆ OutputPluginWrite()

◆ prepare_cb_wrapper()

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

Definition at line 940 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ ResetLogicalStreamingState()

void ResetLogicalStreamingState ( void  )

Definition at line 1933 of file logical.c.

1934{
1936 bsysscan = false;
1937}
bool bsysscan
Definition: xact.c:101
TransactionId CheckXidAlive
Definition: xact.c:100

References bsysscan, CheckXidAlive, and InvalidTransactionId.

Referenced by AbortSubTransaction(), and AbortTransaction().

◆ rollback_prepared_cb_wrapper()

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

Definition at line 1030 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ shutdown_cb_wrapper()

static void shutdown_cb_wrapper ( LogicalDecodingContext ctx)
static

Definition at line 793 of file logical.c.

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

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

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

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

Referenced by CreateDecodingContext(), and CreateInitDecodingContext().

◆ StartupDecodingContext()

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

Definition at line 141 of file logical.c.

151{
152 ReplicationSlot *slot;
153 MemoryContext context,
154 old_context;
156
157 /* shorter lines... */
158 slot = MyReplicationSlot;
159
161 "Logical decoding context",
163 old_context = MemoryContextSwitchTo(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 {
189 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
192 LWLockRelease(ProcArrayLock);
193 }
194
195 ctx->slot = slot;
196
197 ctx->reader = XLogReaderAllocate(wal_segment_size, NULL, xl_routine, ctx);
198 if (!ctx->reader)
200 (errcode(ERRCODE_OUT_OF_MEMORY),
201 errmsg("out of memory"),
202 errdetail("Failed while allocating a WAL reading processor.")));
203
205 ctx->snapshot_builder =
206 AllocateSnapshotBuilder(ctx->reorder, xmin_horizon, start_lsn,
207 need_full_snapshot, in_create, slot->data.two_phase_at);
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) ||
233 (ctx->callbacks.stream_truncate_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) ||
264 (ctx->callbacks.commit_prepared_cb != NULL) ||
265 (ctx->callbacks.rollback_prepared_cb != NULL) ||
266 (ctx->callbacks.stream_prepare_cb != NULL) ||
267 (ctx->callbacks.filter_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
292 MemoryContextSwitchTo(old_context);
293
294 return ctx;
295}
#define palloc0_object(type)
Definition: fe_memutils.h:75
static void change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
Definition: logical.c:1077
static void commit_prepared_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:985
static void update_progress_txn_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr lsn)
Definition: logical.c:1620
static void stream_start_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr first_lsn)
Definition: logical.c:1258
static void commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:857
static void begin_prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
Definition: logical.c:896
static void stream_prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
Definition: logical.c:1397
static void prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
Definition: logical.c:940
static void stream_truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
Definition: logical.c:1573
static void truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
Definition: logical.c:1116
static void begin_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
Definition: logical.c:826
static void rollback_prepared_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_end_lsn, TimestampTz prepare_time)
Definition: logical.c:1030
static void stream_change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
Definition: logical.c:1483
static void stream_abort_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr abort_lsn)
Definition: logical.c:1356
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:1532
static void stream_commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:1442
static void LoadOutputPlugin(OutputPluginCallbacks *callbacks, const char *plugin)
Definition: logical.c:724
static void stream_stop_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr last_lsn)
Definition: logical.c:1307
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:1221
MemoryContext CurrentMemoryContext
Definition: mcxt.c:160
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
#define PROC_IN_LOGICAL_DECODING
Definition: proc.h:61
ReorderBuffer * ReorderBufferAllocate(void)
SnapBuild * AllocateSnapshotBuilder(ReorderBuffer *reorder, TransactionId xmin_horizon, XLogRecPtr start_lsn, bool need_full_snapshot, bool in_slot_creation, XLogRecPtr two_phase_at)
Definition: snapbuild.c:185
PGPROC * MyProc
Definition: proc.c:67
PROC_HDR * ProcGlobal
Definition: proc.c:79
StringInfo makeStringInfo(void)
Definition: stringinfo.c:72
StringInfo out
Definition: logical.h:71
List * output_plugin_options
Definition: logical.h:59
LogicalDecodeStreamChangeCB stream_change_cb
LogicalDecodeStreamTruncateCB stream_truncate_cb
LogicalDecodeStreamMessageCB stream_message_cb
LogicalDecodeStreamStopCB stream_stop_cb
LogicalDecodeStreamCommitCB stream_commit_cb
LogicalDecodeStreamPrepareCB stream_prepare_cb
LogicalDecodeStreamStartCB stream_start_cb
LogicalDecodeStreamAbortCB stream_abort_cb
uint8 statusFlags
Definition: proc.h:259
int pgxactoff
Definition: proc.h:201
uint8 * statusFlags
Definition: proc.h:403
ReorderBufferStreamMessageCB stream_message
ReorderBufferStreamChangeCB stream_change
ReorderBufferBeginCB begin_prepare
ReorderBufferStreamTruncateCB stream_truncate
ReorderBufferCommitPreparedCB commit_prepared
ReorderBufferUpdateProgressTxnCB update_progress_txn
ReorderBufferMessageCB message
ReorderBufferRollbackPreparedCB rollback_prepared
ReorderBufferPrepareCB prepare
ReorderBufferStreamStopCB stream_stop
ReorderBufferApplyChangeCB apply_change
ReorderBufferStreamPrepareCB stream_prepare
ReorderBufferStreamAbortCB stream_abort
ReorderBufferCommitCB commit
ReorderBufferStreamStartCB stream_start
ReorderBufferStreamCommitCB stream_commit
ReorderBufferApplyTruncateCB apply_truncate
ReorderBufferBeginCB begin
bool IsTransactionOrTransactionBlock(void)
Definition: xact.c:5011
XLogReaderState * XLogReaderAllocate(int wal_segment_size, const char *waldir, XLogReaderRoutine *routine, void *private_data)
Definition: xlogreader.c:107

References AllocateSnapshotBuilder(), ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, ReorderBuffer::apply_change, ReorderBuffer::apply_truncate, ReorderBuffer::begin, begin_cb_wrapper(), ReorderBuffer::begin_prepare, OutputPluginCallbacks::begin_prepare_cb, begin_prepare_cb_wrapper(), LogicalDecodingContext::callbacks, change_cb_wrapper(), ReorderBuffer::commit, commit_cb_wrapper(), ReorderBuffer::commit_prepared, OutputPluginCallbacks::commit_prepared_cb, commit_prepared_cb_wrapper(), LogicalDecodingContext::context, CurrentMemoryContext, ReplicationSlot::data, ereport, errcode(), errdetail(), errmsg(), ERROR, LogicalDecodingContext::fast_forward, OutputPluginCallbacks::filter_prepare_cb, IsTransactionOrTransactionBlock(), LoadOutputPlugin(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), makeStringInfo(), MemoryContextSwitchTo(), ReorderBuffer::message, message_cb_wrapper(), MyProc, MyReplicationSlot, NameStr, LogicalDecodingContext::out, LogicalDecodingContext::output_plugin_options, palloc0_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 1356 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ stream_change_cb_wrapper()

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

Definition at line 1483 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ stream_commit_cb_wrapper()

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

Definition at line 1442 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ stream_message_cb_wrapper()

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

Definition at line 1532 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ stream_prepare_cb_wrapper()

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

Definition at line 1397 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ stream_start_cb_wrapper()

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

Definition at line 1258 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ stream_stop_cb_wrapper()

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

Definition at line 1307 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ stream_truncate_cb_wrapper()

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

Definition at line 1573 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ truncate_cb_wrapper()

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

Definition at line 1116 of file logical.c.

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

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

Referenced by StartupDecodingContext().

◆ update_progress_txn_cb_wrapper()

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

Definition at line 1620 of file logical.c.

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

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

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

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

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