Thanks to visit codestin.com
Credit goes to doxygen.postgresql.org

PostgreSQL Source Code git master
logical.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 * logical.c
3 * PostgreSQL logical decoding coordination
4 *
5 * Copyright (c) 2012-2025, PostgreSQL Global Development Group
6 *
7 * IDENTIFICATION
8 * src/backend/replication/logical/logical.c
9 *
10 * NOTES
11 * This file coordinates interaction between the various modules that
12 * together provide logical decoding, primarily by providing so
13 * called LogicalDecodingContexts. The goal is to encapsulate most of the
14 * internal complexity for consumers of logical decoding, so they can
15 * create and consume a changestream with a low amount of code. Builtin
16 * consumers are the walsender and SQL SRF interface, but it's possible to
17 * add further ones without changing core code, e.g. to consume changes in
18 * a bgworker.
19 *
20 * The idea is that a consumer provides three callbacks, one to read WAL,
21 * one to prepare a data write, and a final one for actually writing since
22 * their implementation depends on the type of consumer. Check
23 * logicalfuncs.c for an example implementation of a fairly simple consumer
24 * and an implementation of a WAL reading callback that's suitable for
25 * simple consumers.
26 *-------------------------------------------------------------------------
27 */
28
29#include "postgres.h"
30
31#include "access/xact.h"
33#include "access/xlogutils.h"
34#include "fmgr.h"
35#include "miscadmin.h"
36#include "pgstat.h"
37#include "replication/decode.h"
38#include "replication/logical.h"
42#include "storage/proc.h"
43#include "storage/procarray.h"
44#include "utils/builtins.h"
46#include "utils/inval.h"
47#include "utils/memutils.h"
48
49/* data for errcontext callback */
51{
53 const char *callback_name;
56
57/* wrappers around output plugin callbacks */
58static void output_plugin_error_callback(void *arg);
60 bool is_init);
62static void begin_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn);
63static void commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
64 XLogRecPtr commit_lsn);
67 XLogRecPtr prepare_lsn);
69 XLogRecPtr commit_lsn);
71 XLogRecPtr prepare_end_lsn, TimestampTz prepare_time);
72static void change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
73 Relation relation, ReorderBufferChange *change);
75 int nrelations, Relation relations[], ReorderBufferChange *change);
77 XLogRecPtr message_lsn, bool transactional,
78 const char *prefix, Size message_size, const char *message);
79
80/* streaming callbacks */
82 XLogRecPtr first_lsn);
84 XLogRecPtr last_lsn);
86 XLogRecPtr abort_lsn);
88 XLogRecPtr prepare_lsn);
90 XLogRecPtr commit_lsn);
92 Relation relation, ReorderBufferChange *change);
94 XLogRecPtr message_lsn, bool transactional,
95 const char *prefix, Size message_size, const char *message);
97 int nrelations, Relation relations[], ReorderBufferChange *change);
98
99/* callback to update txn's progress */
101 ReorderBufferTXN *txn,
102 XLogRecPtr lsn);
103
104static void LoadOutputPlugin(OutputPluginCallbacks *callbacks, const char *plugin);
105
106/*
107 * Make sure the current settings & environment are capable of doing logical
108 * decoding.
109 */
110void
112{
114
115 /*
116 * NB: Adding a new requirement likely means that RestoreSlotFromDisk()
117 * needs the same check.
118 */
119
122 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
123 errmsg("logical decoding requires \"wal_level\" >= \"logical\"")));
124
127 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
128 errmsg("logical decoding requires a database connection")));
129
130 if (RecoveryInProgress())
131 {
132 /*
133 * This check may have race conditions, but whenever
134 * XLOG_PARAMETER_CHANGE indicates that wal_level has changed, we
135 * verify that there are no existing logical replication slots. And to
136 * avoid races around creating a new slot,
137 * CheckLogicalDecodingRequirements() is called once before creating
138 * the slot, and once when logical decoding is initially starting up.
139 */
142 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
143 errmsg("logical decoding on standby requires \"wal_level\" >= \"logical\" on the primary")));
144 }
145}
146
147/*
148 * Helper function for CreateInitDecodingContext() and
149 * CreateDecodingContext() performing common tasks.
150 */
152StartupDecodingContext(List *output_plugin_options,
153 XLogRecPtr start_lsn,
154 TransactionId xmin_horizon,
155 bool need_full_snapshot,
156 bool fast_forward,
157 bool in_create,
158 XLogReaderRoutine *xl_routine,
162{
163 ReplicationSlot *slot;
164 MemoryContext context,
165 old_context;
167
168 /* shorter lines... */
169 slot = MyReplicationSlot;
170
172 "Logical decoding context",
174 old_context = MemoryContextSwitchTo(context);
175 ctx = palloc0(sizeof(LogicalDecodingContext));
176
177 ctx->context = context;
178
179 /*
180 * (re-)load output plugins, so we detect a bad (removed) output plugin
181 * now.
182 */
183 if (!fast_forward)
185
186 /*
187 * Now that the slot's xmin has been set, we can announce ourselves as a
188 * logical decoding backend which doesn't need to be checked individually
189 * when computing the xmin horizon because the xmin is enforced via
190 * replication slots.
191 *
192 * We can only do so if we're outside of a transaction (i.e. the case when
193 * streaming changes via walsender), otherwise an already setup
194 * snapshot/xid would end up being ignored. That's not a particularly
195 * bothersome restriction since the SQL interface can't be used for
196 * streaming anyway.
197 */
199 {
200 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
203 LWLockRelease(ProcArrayLock);
204 }
205
206 ctx->slot = slot;
207
208 ctx->reader = XLogReaderAllocate(wal_segment_size, NULL, xl_routine, ctx);
209 if (!ctx->reader)
211 (errcode(ERRCODE_OUT_OF_MEMORY),
212 errmsg("out of memory"),
213 errdetail("Failed while allocating a WAL reading processor.")));
214
216 ctx->snapshot_builder =
217 AllocateSnapshotBuilder(ctx->reorder, xmin_horizon, start_lsn,
218 need_full_snapshot, in_create, slot->data.two_phase_at);
219
220 ctx->reorder->private_data = ctx;
221
222 /* wrap output plugin callbacks, so we can add error context information */
228
229 /*
230 * To support streaming, we require start/stop/abort/commit/change
231 * callbacks. The message and truncate callbacks are optional, similar to
232 * regular output plugins. We however enable streaming when at least one
233 * of the methods is enabled so that we can easily identify missing
234 * methods.
235 *
236 * We decide it here, but only check it later in the wrappers.
237 */
238 ctx->streaming = (ctx->callbacks.stream_start_cb != NULL) ||
239 (ctx->callbacks.stream_stop_cb != NULL) ||
240 (ctx->callbacks.stream_abort_cb != NULL) ||
241 (ctx->callbacks.stream_commit_cb != NULL) ||
242 (ctx->callbacks.stream_change_cb != NULL) ||
243 (ctx->callbacks.stream_message_cb != NULL) ||
244 (ctx->callbacks.stream_truncate_cb != NULL);
245
246 /*
247 * streaming callbacks
248 *
249 * stream_message and stream_truncate callbacks are optional, so we do not
250 * fail with ERROR when missing, but the wrappers simply do nothing. We
251 * must set the ReorderBuffer callbacks to something, otherwise the calls
252 * from there will crash (we don't want to move the checks there).
253 */
262
263
264 /*
265 * To support two-phase logical decoding, we require
266 * begin_prepare/prepare/commit-prepare/abort-prepare callbacks. The
267 * filter_prepare callback is optional. We however enable two-phase
268 * logical decoding when at least one of the methods is enabled so that we
269 * can easily identify missing methods.
270 *
271 * We decide it here, but only check it later in the wrappers.
272 */
273 ctx->twophase = (ctx->callbacks.begin_prepare_cb != NULL) ||
274 (ctx->callbacks.prepare_cb != NULL) ||
275 (ctx->callbacks.commit_prepared_cb != NULL) ||
276 (ctx->callbacks.rollback_prepared_cb != NULL) ||
277 (ctx->callbacks.stream_prepare_cb != NULL) ||
278 (ctx->callbacks.filter_prepare_cb != NULL);
279
280 /*
281 * Callback to support decoding at prepare time.
282 */
287
288 /*
289 * Callback to support updating progress during sending data of a
290 * transaction (and its subtransactions) to the output plugin.
291 */
293
294 ctx->out = makeStringInfo();
295 ctx->prepare_write = prepare_write;
296 ctx->write = do_write;
297 ctx->update_progress = update_progress;
298
299 ctx->output_plugin_options = output_plugin_options;
300
301 ctx->fast_forward = fast_forward;
302
303 MemoryContextSwitchTo(old_context);
304
305 return ctx;
306}
307
308/*
309 * Create a new decoding context, for a new logical slot.
310 *
311 * plugin -- contains the name of the output plugin
312 * output_plugin_options -- contains options passed to the output plugin
313 * need_full_snapshot -- if true, must obtain a snapshot able to read all
314 * tables; if false, one that can read only catalogs is acceptable.
315 * restart_lsn -- if given as invalid, it's this routine's responsibility to
316 * mark WAL as reserved by setting a convenient restart_lsn for the slot.
317 * Otherwise, we set for decoding to start from the given LSN without
318 * marking WAL reserved beforehand. In that scenario, it's up to the
319 * caller to guarantee that WAL remains available.
320 * xl_routine -- XLogReaderRoutine for underlying XLogReader
321 * prepare_write, do_write, update_progress --
322 * callbacks that perform the use-case dependent, actual, work.
323 *
324 * Needs to be called while in a memory context that's at least as long lived
325 * as the decoding context because further memory contexts will be created
326 * inside it.
327 *
328 * Returns an initialized decoding context after calling the output plugin's
329 * startup function.
330 */
333 List *output_plugin_options,
334 bool need_full_snapshot,
335 XLogRecPtr restart_lsn,
336 XLogReaderRoutine *xl_routine,
340{
341 TransactionId xmin_horizon = InvalidTransactionId;
342 ReplicationSlot *slot;
343 NameData plugin_name;
345 MemoryContext old_context;
346
347 /*
348 * On a standby, this check is also required while creating the slot.
349 * Check the comments in the function.
350 */
352
353 /* shorter lines... */
354 slot = MyReplicationSlot;
355
356 /* first some sanity checks that are unlikely to be violated */
357 if (slot == NULL)
358 elog(ERROR, "cannot perform logical decoding without an acquired slot");
359
360 if (plugin == NULL)
361 elog(ERROR, "cannot initialize logical decoding without a specified plugin");
362
363 /* Make sure the passed slot is suitable. These are user facing errors. */
364 if (SlotIsPhysical(slot))
366 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
367 errmsg("cannot use physical replication slot for logical decoding")));
368
369 if (slot->data.database != MyDatabaseId)
371 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
372 errmsg("replication slot \"%s\" was not created in this database",
373 NameStr(slot->data.name))));
374
375 if (IsTransactionState() &&
378 (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
379 errmsg("cannot create logical replication slot in transaction that has performed writes")));
380
381 /*
382 * Register output plugin name with slot. We need the mutex to avoid
383 * concurrent reading of a partially copied string. But we don't want any
384 * complicated code while holding a spinlock, so do namestrcpy() outside.
385 */
386 namestrcpy(&plugin_name, plugin);
387 SpinLockAcquire(&slot->mutex);
388 slot->data.plugin = plugin_name;
389 SpinLockRelease(&slot->mutex);
390
391 if (XLogRecPtrIsInvalid(restart_lsn))
393 else
394 {
395 SpinLockAcquire(&slot->mutex);
396 slot->data.restart_lsn = restart_lsn;
397 SpinLockRelease(&slot->mutex);
398 }
399
400 /* ----
401 * This is a bit tricky: We need to determine a safe xmin horizon to start
402 * decoding from, to avoid starting from a running xacts record referring
403 * to xids whose rows have been vacuumed or pruned
404 * already. GetOldestSafeDecodingTransactionId() returns such a value, but
405 * without further interlock its return value might immediately be out of
406 * date.
407 *
408 * So we have to acquire the ProcArrayLock to prevent computation of new
409 * xmin horizons by other backends, get the safe decoding xid, and inform
410 * the slot machinery about the new limit. Once that's done the
411 * ProcArrayLock can be released as the slot machinery now is
412 * protecting against vacuum.
413 *
414 * Note that, temporarily, the data, not just the catalog, xmin has to be
415 * reserved if a data snapshot is to be exported. Otherwise the initial
416 * data snapshot created here is not guaranteed to be valid. After that
417 * the data xmin doesn't need to be managed anymore and the global xmin
418 * should be recomputed. As we are fine with losing the pegged data xmin
419 * after crash - no chance a snapshot would get exported anymore - we can
420 * get away with just setting the slot's
421 * effective_xmin. ReplicationSlotRelease will reset it again.
422 *
423 * ----
424 */
425 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
426
427 xmin_horizon = GetOldestSafeDecodingTransactionId(!need_full_snapshot);
428
429 SpinLockAcquire(&slot->mutex);
430 slot->effective_catalog_xmin = xmin_horizon;
431 slot->data.catalog_xmin = xmin_horizon;
432 if (need_full_snapshot)
433 slot->effective_xmin = xmin_horizon;
434 SpinLockRelease(&slot->mutex);
435
437
438 LWLockRelease(ProcArrayLock);
439
442
443 ctx = StartupDecodingContext(NIL, restart_lsn, xmin_horizon,
444 need_full_snapshot, false, true,
445 xl_routine, prepare_write, do_write,
446 update_progress);
447
448 /* call output plugin initialization callback */
449 old_context = MemoryContextSwitchTo(ctx->context);
450 if (ctx->callbacks.startup_cb != NULL)
451 startup_cb_wrapper(ctx, &ctx->options, true);
452 MemoryContextSwitchTo(old_context);
453
454 /*
455 * We allow decoding of prepared transactions when the two_phase is
456 * enabled at the time of slot creation, or when the two_phase option is
457 * given at the streaming start, provided the plugin supports all the
458 * callbacks for two-phase.
459 */
460 ctx->twophase &= slot->data.two_phase;
461
463
464 return ctx;
465}
466
467/*
468 * Create a new decoding context, for a logical slot that has previously been
469 * used already.
470 *
471 * start_lsn
472 * The LSN at which to start decoding. If InvalidXLogRecPtr, restart
473 * from the slot's confirmed_flush; otherwise, start from the specified
474 * location (but move it forwards to confirmed_flush if it's older than
475 * that, see below).
476 *
477 * output_plugin_options
478 * options passed to the output plugin.
479 *
480 * fast_forward
481 * bypass the generation of logical changes.
482 *
483 * xl_routine
484 * XLogReaderRoutine used by underlying xlogreader
485 *
486 * prepare_write, do_write, update_progress
487 * callbacks that have to be filled to perform the use-case dependent,
488 * actual work.
489 *
490 * Needs to be called while in a memory context that's at least as long lived
491 * as the decoding context because further memory contexts will be created
492 * inside it.
493 *
494 * Returns an initialized decoding context after calling the output plugin's
495 * startup function.
496 */
499 List *output_plugin_options,
500 bool fast_forward,
501 XLogReaderRoutine *xl_routine,
505{
507 ReplicationSlot *slot;
508 MemoryContext old_context;
509
510 /* shorter lines... */
511 slot = MyReplicationSlot;
512
513 /* first some sanity checks that are unlikely to be violated */
514 if (slot == NULL)
515 elog(ERROR, "cannot perform logical decoding without an acquired slot");
516
517 /* make sure the passed slot is suitable, these are user facing errors */
518 if (SlotIsPhysical(slot))
520 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
521 errmsg("cannot use physical replication slot for logical decoding")));
522
523 /*
524 * We need to access the system tables during decoding to build the
525 * logical changes unless we are in fast_forward mode where no changes are
526 * generated.
527 */
528 if (slot->data.database != MyDatabaseId && !fast_forward)
530 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
531 errmsg("replication slot \"%s\" was not created in this database",
532 NameStr(slot->data.name))));
533
534 /*
535 * The slots being synced from the primary can't be used for decoding as
536 * they are used after failover. However, we do allow advancing the LSNs
537 * during the synchronization of slots. See update_local_synced_slot.
538 */
541 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
542 errmsg("cannot use replication slot \"%s\" for logical decoding",
543 NameStr(slot->data.name)),
544 errdetail("This replication slot is being synchronized from the primary server."),
545 errhint("Specify another replication slot."));
546
547 /* slot must be valid to allow decoding */
550
551 if (start_lsn == InvalidXLogRecPtr)
552 {
553 /* continue from last position */
554 start_lsn = slot->data.confirmed_flush;
555 }
556 else if (start_lsn < slot->data.confirmed_flush)
557 {
558 /*
559 * It might seem like we should error out in this case, but it's
560 * pretty common for a client to acknowledge a LSN it doesn't have to
561 * do anything for, and thus didn't store persistently, because the
562 * xlog records didn't result in anything relevant for logical
563 * decoding. Clients have to be able to do that to support synchronous
564 * replication.
565 *
566 * Starting at a different LSN than requested might not catch certain
567 * kinds of client errors; so the client may wish to check that
568 * confirmed_flush_lsn matches its expectations.
569 */
570 elog(LOG, "%X/%08X has been already streamed, forwarding to %X/%08X",
571 LSN_FORMAT_ARGS(start_lsn),
573
574 start_lsn = slot->data.confirmed_flush;
575 }
576
577 ctx = StartupDecodingContext(output_plugin_options,
578 start_lsn, InvalidTransactionId, false,
579 fast_forward, false, xl_routine, prepare_write,
580 do_write, update_progress);
581
582 /* call output plugin initialization callback */
583 old_context = MemoryContextSwitchTo(ctx->context);
584 if (ctx->callbacks.startup_cb != NULL)
585 startup_cb_wrapper(ctx, &ctx->options, false);
586 MemoryContextSwitchTo(old_context);
587
588 /*
589 * We allow decoding of prepared transactions when the two_phase is
590 * enabled at the time of slot creation, or when the two_phase option is
591 * given at the streaming start, provided the plugin supports all the
592 * callbacks for two-phase.
593 */
594 ctx->twophase &= (slot->data.two_phase || ctx->twophase_opt_given);
595
596 /* Mark slot to allow two_phase decoding if not already marked */
597 if (ctx->twophase && !slot->data.two_phase)
598 {
599 SpinLockAcquire(&slot->mutex);
600 slot->data.two_phase = true;
601 slot->data.two_phase_at = start_lsn;
602 SpinLockRelease(&slot->mutex);
606 }
607
609
610 ereport(LOG,
611 (errmsg("starting logical decoding for slot \"%s\"",
612 NameStr(slot->data.name)),
613 errdetail("Streaming transactions committing after %X/%08X, reading WAL from %X/%08X.",
616
617 return ctx;
618}
619
620/*
621 * Returns true if a consistent initial decoding snapshot has been built.
622 */
623bool
625{
627}
628
629/*
630 * Read from the decoding slot, until it is ready to start extracting changes.
631 */
632void
634{
635 ReplicationSlot *slot = ctx->slot;
636
637 /* Initialize from where to start reading WAL. */
639
640 elog(DEBUG1, "searching for logical decoding starting point, starting at %X/%08X",
642
643 /* Wait for a consistent starting point */
644 for (;;)
645 {
646 XLogRecord *record;
647 char *err = NULL;
648
649 /* the read_page callback waits for new WAL */
650 record = XLogReadRecord(ctx->reader, &err);
651 if (err)
652 elog(ERROR, "could not find logical decoding starting point: %s", err);
653 if (!record)
654 elog(ERROR, "could not find logical decoding starting point");
655
657
658 /* only continue till we found a consistent spot */
659 if (DecodingContextReady(ctx))
660 break;
661
663 }
664
665 SpinLockAcquire(&slot->mutex);
666 slot->data.confirmed_flush = ctx->reader->EndRecPtr;
667 if (slot->data.two_phase)
668 slot->data.two_phase_at = ctx->reader->EndRecPtr;
669 SpinLockRelease(&slot->mutex);
670}
671
672/*
673 * Free a previously allocated decoding context, invoking the shutdown
674 * callback if necessary.
675 */
676void
678{
679 if (ctx->callbacks.shutdown_cb != NULL)
681
686}
687
688/*
689 * Prepare a write using the context's output routine.
690 */
691void
693{
694 if (!ctx->accept_writes)
695 elog(ERROR, "writes are only accepted in commit, begin and change callbacks");
696
697 ctx->prepare_write(ctx, ctx->write_location, ctx->write_xid, last_write);
698 ctx->prepared_write = true;
699}
700
701/*
702 * Perform a write using the context's output routine.
703 */
704void
705OutputPluginWrite(struct LogicalDecodingContext *ctx, bool last_write)
706{
707 if (!ctx->prepared_write)
708 elog(ERROR, "OutputPluginPrepareWrite needs to be called before OutputPluginWrite");
709
710 ctx->write(ctx, ctx->write_location, ctx->write_xid, last_write);
711 ctx->prepared_write = false;
712}
713
714/*
715 * Update progress tracking (if supported).
716 */
717void
719 bool skipped_xact)
720{
721 if (!ctx->update_progress)
722 return;
723
724 ctx->update_progress(ctx, ctx->write_location, ctx->write_xid,
725 skipped_xact);
726}
727
728/*
729 * Load the output plugin, lookup its output plugin init function, and check
730 * that it provides the required callbacks.
731 */
732static void
734{
735 LogicalOutputPluginInit plugin_init;
736
737 plugin_init = (LogicalOutputPluginInit)
738 load_external_function(plugin, "_PG_output_plugin_init", false, NULL);
739
740 if (plugin_init == NULL)
741 elog(ERROR, "output plugins have to declare the _PG_output_plugin_init symbol");
742
743 /* ask the output plugin to fill the callback struct */
744 plugin_init(callbacks);
745
746 if (callbacks->begin_cb == NULL)
747 elog(ERROR, "output plugins have to register a begin callback");
748 if (callbacks->change_cb == NULL)
749 elog(ERROR, "output plugins have to register a change callback");
750 if (callbacks->commit_cb == NULL)
751 elog(ERROR, "output plugins have to register a commit callback");
752}
753
754static void
756{
758
759 /* not all callbacks have an associated LSN */
760 if (state->report_location != InvalidXLogRecPtr)
761 errcontext("slot \"%s\", output plugin \"%s\", in the %s callback, associated LSN %X/%08X",
762 NameStr(state->ctx->slot->data.name),
763 NameStr(state->ctx->slot->data.plugin),
764 state->callback_name,
765 LSN_FORMAT_ARGS(state->report_location));
766 else
767 errcontext("slot \"%s\", output plugin \"%s\", in the %s callback",
768 NameStr(state->ctx->slot->data.name),
769 NameStr(state->ctx->slot->data.plugin),
770 state->callback_name);
771}
772
773static void
775{
777 ErrorContextCallback errcallback;
778
779 Assert(!ctx->fast_forward);
780
781 /* Push callback + info on the error context stack */
782 state.ctx = ctx;
783 state.callback_name = "startup";
784 state.report_location = InvalidXLogRecPtr;
786 errcallback.arg = &state;
787 errcallback.previous = error_context_stack;
788 error_context_stack = &errcallback;
789
790 /* set output state */
791 ctx->accept_writes = false;
792 ctx->end_xact = false;
793
794 /* do the actual work: call callback */
795 ctx->callbacks.startup_cb(ctx, opt, is_init);
796
797 /* Pop the error context stack */
798 error_context_stack = errcallback.previous;
799}
800
801static void
803{
805 ErrorContextCallback errcallback;
806
807 Assert(!ctx->fast_forward);
808
809 /* Push callback + info on the error context stack */
810 state.ctx = ctx;
811 state.callback_name = "shutdown";
812 state.report_location = InvalidXLogRecPtr;
814 errcallback.arg = &state;
815 errcallback.previous = error_context_stack;
816 error_context_stack = &errcallback;
817
818 /* set output state */
819 ctx->accept_writes = false;
820 ctx->end_xact = false;
821
822 /* do the actual work: call callback */
823 ctx->callbacks.shutdown_cb(ctx);
824
825 /* Pop the error context stack */
826 error_context_stack = errcallback.previous;
827}
828
829
830/*
831 * Callbacks for ReorderBuffer which add in some more information and then call
832 * output_plugin.h plugins.
833 */
834static void
836{
839 ErrorContextCallback errcallback;
840
841 Assert(!ctx->fast_forward);
842
843 /* Push callback + info on the error context stack */
844 state.ctx = ctx;
845 state.callback_name = "begin";
846 state.report_location = txn->first_lsn;
848 errcallback.arg = &state;
849 errcallback.previous = error_context_stack;
850 error_context_stack = &errcallback;
851
852 /* set output state */
853 ctx->accept_writes = true;
854 ctx->write_xid = txn->xid;
855 ctx->write_location = txn->first_lsn;
856 ctx->end_xact = false;
857
858 /* do the actual work: call callback */
859 ctx->callbacks.begin_cb(ctx, txn);
860
861 /* Pop the error context stack */
862 error_context_stack = errcallback.previous;
863}
864
865static void
867 XLogRecPtr commit_lsn)
868{
871 ErrorContextCallback errcallback;
872
873 Assert(!ctx->fast_forward);
874
875 /* Push callback + info on the error context stack */
876 state.ctx = ctx;
877 state.callback_name = "commit";
878 state.report_location = txn->final_lsn; /* beginning of commit record */
880 errcallback.arg = &state;
881 errcallback.previous = error_context_stack;
882 error_context_stack = &errcallback;
883
884 /* set output state */
885 ctx->accept_writes = true;
886 ctx->write_xid = txn->xid;
887 ctx->write_location = txn->end_lsn; /* points to the end of the record */
888 ctx->end_xact = true;
889
890 /* do the actual work: call callback */
891 ctx->callbacks.commit_cb(ctx, txn, commit_lsn);
892
893 /* Pop the error context stack */
894 error_context_stack = errcallback.previous;
895}
896
897/*
898 * The functionality of begin_prepare is quite similar to begin with the
899 * exception that this will have gid (global transaction id) information which
900 * can be used by plugin. Now, we thought about extending the existing begin
901 * but that would break the replication protocol and additionally this looks
902 * cleaner.
903 */
904static void
906{
909 ErrorContextCallback errcallback;
910
911 Assert(!ctx->fast_forward);
912
913 /* We're only supposed to call this when two-phase commits are supported */
914 Assert(ctx->twophase);
915
916 /* Push callback + info on the error context stack */
917 state.ctx = ctx;
918 state.callback_name = "begin_prepare";
919 state.report_location = txn->first_lsn;
921 errcallback.arg = &state;
922 errcallback.previous = error_context_stack;
923 error_context_stack = &errcallback;
924
925 /* set output state */
926 ctx->accept_writes = true;
927 ctx->write_xid = txn->xid;
928 ctx->write_location = txn->first_lsn;
929 ctx->end_xact = false;
930
931 /*
932 * If the plugin supports two-phase commits then begin prepare callback is
933 * mandatory
934 */
935 if (ctx->callbacks.begin_prepare_cb == NULL)
937 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
938 errmsg("logical replication at prepare time requires a %s callback",
939 "begin_prepare_cb")));
940
941 /* do the actual work: call callback */
942 ctx->callbacks.begin_prepare_cb(ctx, txn);
943
944 /* Pop the error context stack */
945 error_context_stack = errcallback.previous;
946}
947
948static void
950 XLogRecPtr prepare_lsn)
951{
954 ErrorContextCallback errcallback;
955
956 Assert(!ctx->fast_forward);
957
958 /* We're only supposed to call this when two-phase commits are supported */
959 Assert(ctx->twophase);
960
961 /* Push callback + info on the error context stack */
962 state.ctx = ctx;
963 state.callback_name = "prepare";
964 state.report_location = txn->final_lsn; /* beginning of prepare record */
966 errcallback.arg = &state;
967 errcallback.previous = error_context_stack;
968 error_context_stack = &errcallback;
969
970 /* set output state */
971 ctx->accept_writes = true;
972 ctx->write_xid = txn->xid;
973 ctx->write_location = txn->end_lsn; /* points to the end of the record */
974 ctx->end_xact = true;
975
976 /*
977 * If the plugin supports two-phase commits then prepare callback is
978 * mandatory
979 */
980 if (ctx->callbacks.prepare_cb == NULL)
982 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
983 errmsg("logical replication at prepare time requires a %s callback",
984 "prepare_cb")));
985
986 /* do the actual work: call callback */
987 ctx->callbacks.prepare_cb(ctx, txn, prepare_lsn);
988
989 /* Pop the error context stack */
990 error_context_stack = errcallback.previous;
991}
992
993static void
995 XLogRecPtr commit_lsn)
996{
999 ErrorContextCallback errcallback;
1000
1001 Assert(!ctx->fast_forward);
1002
1003 /* We're only supposed to call this when two-phase commits are supported */
1004 Assert(ctx->twophase);
1005
1006 /* Push callback + info on the error context stack */
1007 state.ctx = ctx;
1008 state.callback_name = "commit_prepared";
1009 state.report_location = txn->final_lsn; /* beginning of commit record */
1011 errcallback.arg = &state;
1012 errcallback.previous = error_context_stack;
1013 error_context_stack = &errcallback;
1014
1015 /* set output state */
1016 ctx->accept_writes = true;
1017 ctx->write_xid = txn->xid;
1018 ctx->write_location = txn->end_lsn; /* points to the end of the record */
1019 ctx->end_xact = true;
1020
1021 /*
1022 * If the plugin support two-phase commits then commit prepared callback
1023 * is mandatory
1024 */
1025 if (ctx->callbacks.commit_prepared_cb == NULL)
1026 ereport(ERROR,
1027 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1028 errmsg("logical replication at prepare time requires a %s callback",
1029 "commit_prepared_cb")));
1030
1031 /* do the actual work: call callback */
1032 ctx->callbacks.commit_prepared_cb(ctx, txn, commit_lsn);
1033
1034 /* Pop the error context stack */
1035 error_context_stack = errcallback.previous;
1036}
1037
1038static void
1040 XLogRecPtr prepare_end_lsn,
1041 TimestampTz prepare_time)
1042{
1045 ErrorContextCallback errcallback;
1046
1047 Assert(!ctx->fast_forward);
1048
1049 /* We're only supposed to call this when two-phase commits are supported */
1050 Assert(ctx->twophase);
1051
1052 /* Push callback + info on the error context stack */
1053 state.ctx = ctx;
1054 state.callback_name = "rollback_prepared";
1055 state.report_location = txn->final_lsn; /* beginning of commit record */
1057 errcallback.arg = &state;
1058 errcallback.previous = error_context_stack;
1059 error_context_stack = &errcallback;
1060
1061 /* set output state */
1062 ctx->accept_writes = true;
1063 ctx->write_xid = txn->xid;
1064 ctx->write_location = txn->end_lsn; /* points to the end of the record */
1065 ctx->end_xact = true;
1066
1067 /*
1068 * If the plugin support two-phase commits then rollback prepared callback
1069 * is mandatory
1070 */
1071 if (ctx->callbacks.rollback_prepared_cb == NULL)
1072 ereport(ERROR,
1073 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1074 errmsg("logical replication at prepare time requires a %s callback",
1075 "rollback_prepared_cb")));
1076
1077 /* do the actual work: call callback */
1078 ctx->callbacks.rollback_prepared_cb(ctx, txn, prepare_end_lsn,
1079 prepare_time);
1080
1081 /* Pop the error context stack */
1082 error_context_stack = errcallback.previous;
1083}
1084
1085static void
1087 Relation relation, ReorderBufferChange *change)
1088{
1091 ErrorContextCallback errcallback;
1092
1093 Assert(!ctx->fast_forward);
1094
1095 /* Push callback + info on the error context stack */
1096 state.ctx = ctx;
1097 state.callback_name = "change";
1098 state.report_location = change->lsn;
1100 errcallback.arg = &state;
1101 errcallback.previous = error_context_stack;
1102 error_context_stack = &errcallback;
1103
1104 /* set output state */
1105 ctx->accept_writes = true;
1106 ctx->write_xid = txn->xid;
1107
1108 /*
1109 * Report this change's lsn so replies from clients can give an up-to-date
1110 * answer. This won't ever be enough (and shouldn't be!) to confirm
1111 * receipt of this transaction, but it might allow another transaction's
1112 * commit to be confirmed with one message.
1113 */
1114 ctx->write_location = change->lsn;
1115
1116 ctx->end_xact = false;
1117
1118 ctx->callbacks.change_cb(ctx, txn, relation, change);
1119
1120 /* Pop the error context stack */
1121 error_context_stack = errcallback.previous;
1122}
1123
1124static void
1126 int nrelations, Relation relations[], ReorderBufferChange *change)
1127{
1130 ErrorContextCallback errcallback;
1131
1132 Assert(!ctx->fast_forward);
1133
1134 if (!ctx->callbacks.truncate_cb)
1135 return;
1136
1137 /* Push callback + info on the error context stack */
1138 state.ctx = ctx;
1139 state.callback_name = "truncate";
1140 state.report_location = change->lsn;
1142 errcallback.arg = &state;
1143 errcallback.previous = error_context_stack;
1144 error_context_stack = &errcallback;
1145
1146 /* set output state */
1147 ctx->accept_writes = true;
1148 ctx->write_xid = txn->xid;
1149
1150 /*
1151 * Report this change's lsn so replies from clients can give an up-to-date
1152 * answer. This won't ever be enough (and shouldn't be!) to confirm
1153 * receipt of this transaction, but it might allow another transaction's
1154 * commit to be confirmed with one message.
1155 */
1156 ctx->write_location = change->lsn;
1157
1158 ctx->end_xact = false;
1159
1160 ctx->callbacks.truncate_cb(ctx, txn, nrelations, relations, change);
1161
1162 /* Pop the error context stack */
1163 error_context_stack = errcallback.previous;
1164}
1165
1166bool
1168 const char *gid)
1169{
1171 ErrorContextCallback errcallback;
1172 bool ret;
1173
1174 Assert(!ctx->fast_forward);
1175
1176 /* Push callback + info on the error context stack */
1177 state.ctx = ctx;
1178 state.callback_name = "filter_prepare";
1179 state.report_location = InvalidXLogRecPtr;
1181 errcallback.arg = &state;
1182 errcallback.previous = error_context_stack;
1183 error_context_stack = &errcallback;
1184
1185 /* set output state */
1186 ctx->accept_writes = false;
1187 ctx->end_xact = false;
1188
1189 /* do the actual work: call callback */
1190 ret = ctx->callbacks.filter_prepare_cb(ctx, xid, gid);
1191
1192 /* Pop the error context stack */
1193 error_context_stack = errcallback.previous;
1194
1195 return ret;
1196}
1197
1198bool
1200{
1202 ErrorContextCallback errcallback;
1203 bool ret;
1204
1205 Assert(!ctx->fast_forward);
1206
1207 /* Push callback + info on the error context stack */
1208 state.ctx = ctx;
1209 state.callback_name = "filter_by_origin";
1210 state.report_location = InvalidXLogRecPtr;
1212 errcallback.arg = &state;
1213 errcallback.previous = error_context_stack;
1214 error_context_stack = &errcallback;
1215
1216 /* set output state */
1217 ctx->accept_writes = false;
1218 ctx->end_xact = false;
1219
1220 /* do the actual work: call callback */
1221 ret = ctx->callbacks.filter_by_origin_cb(ctx, origin_id);
1222
1223 /* Pop the error context stack */
1224 error_context_stack = errcallback.previous;
1225
1226 return ret;
1227}
1228
1229static void
1231 XLogRecPtr message_lsn, bool transactional,
1232 const char *prefix, Size message_size, const char *message)
1233{
1236 ErrorContextCallback errcallback;
1237
1238 Assert(!ctx->fast_forward);
1239
1240 if (ctx->callbacks.message_cb == NULL)
1241 return;
1242
1243 /* Push callback + info on the error context stack */
1244 state.ctx = ctx;
1245 state.callback_name = "message";
1246 state.report_location = message_lsn;
1248 errcallback.arg = &state;
1249 errcallback.previous = error_context_stack;
1250 error_context_stack = &errcallback;
1251
1252 /* set output state */
1253 ctx->accept_writes = true;
1254 ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
1255 ctx->write_location = message_lsn;
1256 ctx->end_xact = false;
1257
1258 /* do the actual work: call callback */
1259 ctx->callbacks.message_cb(ctx, txn, message_lsn, transactional, prefix,
1260 message_size, message);
1261
1262 /* Pop the error context stack */
1263 error_context_stack = errcallback.previous;
1264}
1265
1266static void
1268 XLogRecPtr first_lsn)
1269{
1272 ErrorContextCallback errcallback;
1273
1274 Assert(!ctx->fast_forward);
1275
1276 /* We're only supposed to call this when streaming is supported. */
1277 Assert(ctx->streaming);
1278
1279 /* Push callback + info on the error context stack */
1280 state.ctx = ctx;
1281 state.callback_name = "stream_start";
1282 state.report_location = first_lsn;
1284 errcallback.arg = &state;
1285 errcallback.previous = error_context_stack;
1286 error_context_stack = &errcallback;
1287
1288 /* set output state */
1289 ctx->accept_writes = true;
1290 ctx->write_xid = txn->xid;
1291
1292 /*
1293 * Report this message's lsn so replies from clients can give an
1294 * up-to-date answer. This won't ever be enough (and shouldn't be!) to
1295 * confirm receipt of this transaction, but it might allow another
1296 * transaction's commit to be confirmed with one message.
1297 */
1298 ctx->write_location = first_lsn;
1299
1300 ctx->end_xact = false;
1301
1302 /* in streaming mode, stream_start_cb is required */
1303 if (ctx->callbacks.stream_start_cb == NULL)
1304 ereport(ERROR,
1305 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1306 errmsg("logical streaming requires a %s callback",
1307 "stream_start_cb")));
1308
1309 ctx->callbacks.stream_start_cb(ctx, txn);
1310
1311 /* Pop the error context stack */
1312 error_context_stack = errcallback.previous;
1313}
1314
1315static void
1317 XLogRecPtr last_lsn)
1318{
1321 ErrorContextCallback errcallback;
1322
1323 Assert(!ctx->fast_forward);
1324
1325 /* We're only supposed to call this when streaming is supported. */
1326 Assert(ctx->streaming);
1327
1328 /* Push callback + info on the error context stack */
1329 state.ctx = ctx;
1330 state.callback_name = "stream_stop";
1331 state.report_location = last_lsn;
1333 errcallback.arg = &state;
1334 errcallback.previous = error_context_stack;
1335 error_context_stack = &errcallback;
1336
1337 /* set output state */
1338 ctx->accept_writes = true;
1339 ctx->write_xid = txn->xid;
1340
1341 /*
1342 * Report this message's lsn so replies from clients can give an
1343 * up-to-date answer. This won't ever be enough (and shouldn't be!) to
1344 * confirm receipt of this transaction, but it might allow another
1345 * transaction's commit to be confirmed with one message.
1346 */
1347 ctx->write_location = last_lsn;
1348
1349 ctx->end_xact = false;
1350
1351 /* in streaming mode, stream_stop_cb is required */
1352 if (ctx->callbacks.stream_stop_cb == NULL)
1353 ereport(ERROR,
1354 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1355 errmsg("logical streaming requires a %s callback",
1356 "stream_stop_cb")));
1357
1358 ctx->callbacks.stream_stop_cb(ctx, txn);
1359
1360 /* Pop the error context stack */
1361 error_context_stack = errcallback.previous;
1362}
1363
1364static void
1366 XLogRecPtr abort_lsn)
1367{
1370 ErrorContextCallback errcallback;
1371
1372 Assert(!ctx->fast_forward);
1373
1374 /* We're only supposed to call this when streaming is supported. */
1375 Assert(ctx->streaming);
1376
1377 /* Push callback + info on the error context stack */
1378 state.ctx = ctx;
1379 state.callback_name = "stream_abort";
1380 state.report_location = abort_lsn;
1382 errcallback.arg = &state;
1383 errcallback.previous = error_context_stack;
1384 error_context_stack = &errcallback;
1385
1386 /* set output state */
1387 ctx->accept_writes = true;
1388 ctx->write_xid = txn->xid;
1389 ctx->write_location = abort_lsn;
1390 ctx->end_xact = true;
1391
1392 /* in streaming mode, stream_abort_cb is required */
1393 if (ctx->callbacks.stream_abort_cb == NULL)
1394 ereport(ERROR,
1395 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1396 errmsg("logical streaming requires a %s callback",
1397 "stream_abort_cb")));
1398
1399 ctx->callbacks.stream_abort_cb(ctx, txn, abort_lsn);
1400
1401 /* Pop the error context stack */
1402 error_context_stack = errcallback.previous;
1403}
1404
1405static void
1407 XLogRecPtr prepare_lsn)
1408{
1411 ErrorContextCallback errcallback;
1412
1413 Assert(!ctx->fast_forward);
1414
1415 /*
1416 * We're only supposed to call this when streaming and two-phase commits
1417 * are supported.
1418 */
1419 Assert(ctx->streaming);
1420 Assert(ctx->twophase);
1421
1422 /* Push callback + info on the error context stack */
1423 state.ctx = ctx;
1424 state.callback_name = "stream_prepare";
1425 state.report_location = txn->final_lsn;
1427 errcallback.arg = &state;
1428 errcallback.previous = error_context_stack;
1429 error_context_stack = &errcallback;
1430
1431 /* set output state */
1432 ctx->accept_writes = true;
1433 ctx->write_xid = txn->xid;
1434 ctx->write_location = txn->end_lsn;
1435 ctx->end_xact = true;
1436
1437 /* in streaming mode with two-phase commits, stream_prepare_cb is required */
1438 if (ctx->callbacks.stream_prepare_cb == NULL)
1439 ereport(ERROR,
1440 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1441 errmsg("logical streaming at prepare time requires a %s callback",
1442 "stream_prepare_cb")));
1443
1444 ctx->callbacks.stream_prepare_cb(ctx, txn, prepare_lsn);
1445
1446 /* Pop the error context stack */
1447 error_context_stack = errcallback.previous;
1448}
1449
1450static void
1452 XLogRecPtr commit_lsn)
1453{
1456 ErrorContextCallback errcallback;
1457
1458 Assert(!ctx->fast_forward);
1459
1460 /* We're only supposed to call this when streaming is supported. */
1461 Assert(ctx->streaming);
1462
1463 /* Push callback + info on the error context stack */
1464 state.ctx = ctx;
1465 state.callback_name = "stream_commit";
1466 state.report_location = txn->final_lsn;
1468 errcallback.arg = &state;
1469 errcallback.previous = error_context_stack;
1470 error_context_stack = &errcallback;
1471
1472 /* set output state */
1473 ctx->accept_writes = true;
1474 ctx->write_xid = txn->xid;
1475 ctx->write_location = txn->end_lsn;
1476 ctx->end_xact = true;
1477
1478 /* in streaming mode, stream_commit_cb is required */
1479 if (ctx->callbacks.stream_commit_cb == NULL)
1480 ereport(ERROR,
1481 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1482 errmsg("logical streaming requires a %s callback",
1483 "stream_commit_cb")));
1484
1485 ctx->callbacks.stream_commit_cb(ctx, txn, commit_lsn);
1486
1487 /* Pop the error context stack */
1488 error_context_stack = errcallback.previous;
1489}
1490
1491static void
1493 Relation relation, ReorderBufferChange *change)
1494{
1497 ErrorContextCallback errcallback;
1498
1499 Assert(!ctx->fast_forward);
1500
1501 /* We're only supposed to call this when streaming is supported. */
1502 Assert(ctx->streaming);
1503
1504 /* Push callback + info on the error context stack */
1505 state.ctx = ctx;
1506 state.callback_name = "stream_change";
1507 state.report_location = change->lsn;
1509 errcallback.arg = &state;
1510 errcallback.previous = error_context_stack;
1511 error_context_stack = &errcallback;
1512
1513 /* set output state */
1514 ctx->accept_writes = true;
1515 ctx->write_xid = txn->xid;
1516
1517 /*
1518 * Report this change's lsn so replies from clients can give an up-to-date
1519 * answer. This won't ever be enough (and shouldn't be!) to confirm
1520 * receipt of this transaction, but it might allow another transaction's
1521 * commit to be confirmed with one message.
1522 */
1523 ctx->write_location = change->lsn;
1524
1525 ctx->end_xact = false;
1526
1527 /* in streaming mode, stream_change_cb is required */
1528 if (ctx->callbacks.stream_change_cb == NULL)
1529 ereport(ERROR,
1530 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1531 errmsg("logical streaming requires a %s callback",
1532 "stream_change_cb")));
1533
1534 ctx->callbacks.stream_change_cb(ctx, txn, relation, change);
1535
1536 /* Pop the error context stack */
1537 error_context_stack = errcallback.previous;
1538}
1539
1540static void
1542 XLogRecPtr message_lsn, bool transactional,
1543 const char *prefix, Size message_size, const char *message)
1544{
1547 ErrorContextCallback errcallback;
1548
1549 Assert(!ctx->fast_forward);
1550
1551 /* We're only supposed to call this when streaming is supported. */
1552 Assert(ctx->streaming);
1553
1554 /* this callback is optional */
1555 if (ctx->callbacks.stream_message_cb == NULL)
1556 return;
1557
1558 /* Push callback + info on the error context stack */
1559 state.ctx = ctx;
1560 state.callback_name = "stream_message";
1561 state.report_location = message_lsn;
1563 errcallback.arg = &state;
1564 errcallback.previous = error_context_stack;
1565 error_context_stack = &errcallback;
1566
1567 /* set output state */
1568 ctx->accept_writes = true;
1569 ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
1570 ctx->write_location = message_lsn;
1571 ctx->end_xact = false;
1572
1573 /* do the actual work: call callback */
1574 ctx->callbacks.stream_message_cb(ctx, txn, message_lsn, transactional, prefix,
1575 message_size, message);
1576
1577 /* Pop the error context stack */
1578 error_context_stack = errcallback.previous;
1579}
1580
1581static void
1583 int nrelations, Relation relations[],
1584 ReorderBufferChange *change)
1585{
1588 ErrorContextCallback errcallback;
1589
1590 Assert(!ctx->fast_forward);
1591
1592 /* We're only supposed to call this when streaming is supported. */
1593 Assert(ctx->streaming);
1594
1595 /* this callback is optional */
1596 if (!ctx->callbacks.stream_truncate_cb)
1597 return;
1598
1599 /* Push callback + info on the error context stack */
1600 state.ctx = ctx;
1601 state.callback_name = "stream_truncate";
1602 state.report_location = change->lsn;
1604 errcallback.arg = &state;
1605 errcallback.previous = error_context_stack;
1606 error_context_stack = &errcallback;
1607
1608 /* set output state */
1609 ctx->accept_writes = true;
1610 ctx->write_xid = txn->xid;
1611
1612 /*
1613 * Report this change's lsn so replies from clients can give an up-to-date
1614 * answer. This won't ever be enough (and shouldn't be!) to confirm
1615 * receipt of this transaction, but it might allow another transaction's
1616 * commit to be confirmed with one message.
1617 */
1618 ctx->write_location = change->lsn;
1619
1620 ctx->end_xact = false;
1621
1622 ctx->callbacks.stream_truncate_cb(ctx, txn, nrelations, relations, change);
1623
1624 /* Pop the error context stack */
1625 error_context_stack = errcallback.previous;
1626}
1627
1628static void
1630 XLogRecPtr lsn)
1631{
1634 ErrorContextCallback errcallback;
1635
1636 Assert(!ctx->fast_forward);
1637
1638 /* Push callback + info on the error context stack */
1639 state.ctx = ctx;
1640 state.callback_name = "update_progress_txn";
1641 state.report_location = lsn;
1643 errcallback.arg = &state;
1644 errcallback.previous = error_context_stack;
1645 error_context_stack = &errcallback;
1646
1647 /* set output state */
1648 ctx->accept_writes = false;
1649 ctx->write_xid = txn->xid;
1650
1651 /*
1652 * Report this change's lsn so replies from clients can give an up-to-date
1653 * answer. This won't ever be enough (and shouldn't be!) to confirm
1654 * receipt of this transaction, but it might allow another transaction's
1655 * commit to be confirmed with one message.
1656 */
1657 ctx->write_location = lsn;
1658
1659 ctx->end_xact = false;
1660
1661 OutputPluginUpdateProgress(ctx, false);
1662
1663 /* Pop the error context stack */
1664 error_context_stack = errcallback.previous;
1665}
1666
1667/*
1668 * Set the required catalog xmin horizon for historic snapshots in the current
1669 * replication slot.
1670 *
1671 * Note that in the most cases, we won't be able to immediately use the xmin
1672 * to increase the xmin horizon: we need to wait till the client has confirmed
1673 * receiving current_lsn with LogicalConfirmReceivedLocation().
1674 */
1675void
1677{
1678 bool updated_xmin = false;
1679 ReplicationSlot *slot;
1680 bool got_new_xmin = false;
1681
1682 slot = MyReplicationSlot;
1683
1684 Assert(slot != NULL);
1685
1686 SpinLockAcquire(&slot->mutex);
1687
1688 /*
1689 * don't overwrite if we already have a newer xmin. This can happen if we
1690 * restart decoding in a slot.
1691 */
1693 {
1694 }
1695
1696 /*
1697 * If the client has already confirmed up to this lsn, we directly can
1698 * mark this as accepted. This can happen if we restart decoding in a
1699 * slot.
1700 */
1701 else if (current_lsn <= slot->data.confirmed_flush)
1702 {
1703 slot->candidate_catalog_xmin = xmin;
1704 slot->candidate_xmin_lsn = current_lsn;
1705
1706 /* our candidate can directly be used */
1707 updated_xmin = true;
1708 }
1709
1710 /*
1711 * Only increase if the previous values have been applied, otherwise we
1712 * might never end up updating if the receiver acks too slowly.
1713 */
1714 else if (slot->candidate_xmin_lsn == InvalidXLogRecPtr)
1715 {
1716 slot->candidate_catalog_xmin = xmin;
1717 slot->candidate_xmin_lsn = current_lsn;
1718
1719 /*
1720 * Log new xmin at an appropriate log level after releasing the
1721 * spinlock.
1722 */
1723 got_new_xmin = true;
1724 }
1725 SpinLockRelease(&slot->mutex);
1726
1727 if (got_new_xmin)
1728 elog(DEBUG1, "got new catalog xmin %u at %X/%08X", xmin,
1729 LSN_FORMAT_ARGS(current_lsn));
1730
1731 /* candidate already valid with the current flush position, apply */
1732 if (updated_xmin)
1734}
1735
1736/*
1737 * Mark the minimal LSN (restart_lsn) we need to read to replay all
1738 * transactions that have not yet committed at current_lsn.
1739 *
1740 * Just like LogicalIncreaseXminForSlot this only takes effect when the
1741 * client has confirmed to have received current_lsn.
1742 */
1743void
1745{
1746 bool updated_lsn = false;
1747 ReplicationSlot *slot;
1748
1749 slot = MyReplicationSlot;
1750
1751 Assert(slot != NULL);
1752 Assert(restart_lsn != InvalidXLogRecPtr);
1753 Assert(current_lsn != InvalidXLogRecPtr);
1754
1755 SpinLockAcquire(&slot->mutex);
1756
1757 /* don't overwrite if have a newer restart lsn */
1758 if (restart_lsn <= slot->data.restart_lsn)
1759 {
1760 SpinLockRelease(&slot->mutex);
1761 }
1762
1763 /*
1764 * We might have already flushed far enough to directly accept this lsn,
1765 * in this case there is no need to check for existing candidate LSNs
1766 */
1767 else if (current_lsn <= slot->data.confirmed_flush)
1768 {
1769 slot->candidate_restart_valid = current_lsn;
1770 slot->candidate_restart_lsn = restart_lsn;
1771 SpinLockRelease(&slot->mutex);
1772
1773 /* our candidate can directly be used */
1774 updated_lsn = true;
1775 }
1776
1777 /*
1778 * Only increase if the previous values have been applied, otherwise we
1779 * might never end up updating if the receiver acks too slowly. A missed
1780 * value here will just cause some extra effort after reconnecting.
1781 */
1783 {
1784 slot->candidate_restart_valid = current_lsn;
1785 slot->candidate_restart_lsn = restart_lsn;
1786 SpinLockRelease(&slot->mutex);
1787
1788 elog(DEBUG1, "got new restart lsn %X/%08X at %X/%08X",
1789 LSN_FORMAT_ARGS(restart_lsn),
1790 LSN_FORMAT_ARGS(current_lsn));
1791 }
1792 else
1793 {
1794 XLogRecPtr candidate_restart_lsn;
1795 XLogRecPtr candidate_restart_valid;
1796 XLogRecPtr confirmed_flush;
1797
1798 candidate_restart_lsn = slot->candidate_restart_lsn;
1799 candidate_restart_valid = slot->candidate_restart_valid;
1800 confirmed_flush = slot->data.confirmed_flush;
1801 SpinLockRelease(&slot->mutex);
1802
1803 elog(DEBUG1, "failed to increase restart lsn: proposed %X/%08X, after %X/%08X, current candidate %X/%08X, current after %X/%08X, flushed up to %X/%08X",
1804 LSN_FORMAT_ARGS(restart_lsn),
1805 LSN_FORMAT_ARGS(current_lsn),
1806 LSN_FORMAT_ARGS(candidate_restart_lsn),
1807 LSN_FORMAT_ARGS(candidate_restart_valid),
1808 LSN_FORMAT_ARGS(confirmed_flush));
1809 }
1810
1811 /* candidates are already valid with the current flush position, apply */
1812 if (updated_lsn)
1814}
1815
1816/*
1817 * Handle a consumer's confirmation having received all changes up to lsn.
1818 */
1819void
1821{
1822 Assert(lsn != InvalidXLogRecPtr);
1823
1824 /* Do an unlocked check for candidate_lsn first. */
1827 {
1828 bool updated_xmin = false;
1829 bool updated_restart = false;
1830 XLogRecPtr restart_lsn pg_attribute_unused();
1831
1833
1834 /* remember the old restart lsn */
1835 restart_lsn = MyReplicationSlot->data.restart_lsn;
1836
1837 /*
1838 * Prevent moving the confirmed_flush backwards, as this could lead to
1839 * data duplication issues caused by replicating already replicated
1840 * changes.
1841 *
1842 * This can happen when a client acknowledges an LSN it doesn't have
1843 * to do anything for, and thus didn't store persistently. After a
1844 * restart, the client can send the prior LSN that it stored
1845 * persistently as an acknowledgement, but we need to ignore such an
1846 * LSN. See similar case handling in CreateDecodingContext.
1847 */
1850
1851 /* if we're past the location required for bumping xmin, do so */
1854 {
1855 /*
1856 * We have to write the changed xmin to disk *before* we change
1857 * the in-memory value, otherwise after a crash we wouldn't know
1858 * that some catalog tuples might have been removed already.
1859 *
1860 * Ensure that by first writing to ->xmin and only update
1861 * ->effective_xmin once the new state is synced to disk. After a
1862 * crash ->effective_xmin is set to ->xmin.
1863 */
1866 {
1870 updated_xmin = true;
1871 }
1872 }
1873
1876 {
1878
1882 updated_restart = true;
1883 }
1884
1886
1887 /* first write new xmin to disk, so we know what's up after a crash */
1888 if (updated_xmin || updated_restart)
1889 {
1890#ifdef USE_INJECTION_POINTS
1891 XLogSegNo seg1,
1892 seg2;
1893
1894 XLByteToSeg(restart_lsn, seg1, wal_segment_size);
1896
1897 /* trigger injection point, but only if segment changes */
1898 if (seg1 != seg2)
1899 INJECTION_POINT("logical-replication-slot-advance-segment", NULL);
1900#endif
1901
1904 elog(DEBUG1, "updated xmin: %u restart: %u", updated_xmin, updated_restart);
1905 }
1906
1907 /*
1908 * Now the new xmin is safely on disk, we can let the global value
1909 * advance. We do not take ProcArrayLock or similar since we only
1910 * advance xmin here and there's not much harm done by a concurrent
1911 * computation missing that.
1912 */
1913 if (updated_xmin)
1914 {
1918
1921 }
1922 }
1923 else
1924 {
1926
1927 /*
1928 * Prevent moving the confirmed_flush backwards. See comments above
1929 * for the details.
1930 */
1933
1935 }
1936}
1937
1938/*
1939 * Clear logical streaming state during (sub)transaction abort.
1940 */
1941void
1943{
1945 bsysscan = false;
1946}
1947
1948/*
1949 * Report stats for a slot.
1950 */
1951void
1953{
1954 ReorderBuffer *rb = ctx->reorder;
1955 PgStat_StatReplSlotEntry repSlotStat;
1956
1957 /* Nothing to do if we don't have any replication stats to be sent. */
1958 if (rb->spillBytes <= 0 && rb->streamBytes <= 0 && rb->totalBytes <= 0)
1959 return;
1960
1961 elog(DEBUG2, "UpdateDecodingStats: updating stats %p %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,
1962 rb,
1963 rb->spillTxns,
1964 rb->spillCount,
1965 rb->spillBytes,
1966 rb->streamTxns,
1967 rb->streamCount,
1968 rb->streamBytes,
1969 rb->totalTxns,
1970 rb->totalBytes);
1971
1972 repSlotStat.spill_txns = rb->spillTxns;
1973 repSlotStat.spill_count = rb->spillCount;
1974 repSlotStat.spill_bytes = rb->spillBytes;
1975 repSlotStat.stream_txns = rb->streamTxns;
1976 repSlotStat.stream_count = rb->streamCount;
1977 repSlotStat.stream_bytes = rb->streamBytes;
1978 repSlotStat.total_txns = rb->totalTxns;
1979 repSlotStat.total_bytes = rb->totalBytes;
1980
1981 pgstat_report_replslot(ctx->slot, &repSlotStat);
1982
1983 rb->spillTxns = 0;
1984 rb->spillCount = 0;
1985 rb->spillBytes = 0;
1986 rb->streamTxns = 0;
1987 rb->streamCount = 0;
1988 rb->streamBytes = 0;
1989 rb->totalTxns = 0;
1990 rb->totalBytes = 0;
1991}
1992
1993/*
1994 * Read up to the end of WAL starting from the decoding slot's restart_lsn.
1995 * Return true if any meaningful/decodable WAL records are encountered,
1996 * otherwise false.
1997 */
1998bool
2000{
2001 bool has_pending_wal = false;
2002
2004
2005 PG_TRY();
2006 {
2008
2009 /*
2010 * Create our decoding context in fast_forward mode, passing start_lsn
2011 * as InvalidXLogRecPtr, so that we start processing from the slot's
2012 * confirmed_flush.
2013 */
2015 NIL,
2016 true, /* fast_forward */
2017 XL_ROUTINE(.page_read = read_local_xlog_page,
2018 .segment_open = wal_segment_open,
2019 .segment_close = wal_segment_close),
2020 NULL, NULL, NULL);
2021
2022 /*
2023 * Start reading at the slot's restart_lsn, which we know points to a
2024 * valid record.
2025 */
2027
2028 /* Invalidate non-timetravel entries */
2030
2031 /* Loop until the end of WAL or some changes are processed */
2032 while (!has_pending_wal && ctx->reader->EndRecPtr < end_of_wal)
2033 {
2034 XLogRecord *record;
2035 char *errm = NULL;
2036
2037 record = XLogReadRecord(ctx->reader, &errm);
2038
2039 if (errm)
2040 elog(ERROR, "could not find record for logical decoding: %s", errm);
2041
2042 if (record != NULL)
2044
2045 has_pending_wal = ctx->processing_required;
2046
2048 }
2049
2050 /* Clean up */
2053 }
2054 PG_CATCH();
2055 {
2056 /* clear all timetravel entries */
2058
2059 PG_RE_THROW();
2060 }
2061 PG_END_TRY();
2062
2063 return has_pending_wal;
2064}
2065
2066/*
2067 * Helper function for advancing our logical replication slot forward.
2068 *
2069 * The slot's restart_lsn is used as start point for reading records, while
2070 * confirmed_flush is used as base point for the decoding context.
2071 *
2072 * We cannot just do LogicalConfirmReceivedLocation to update confirmed_flush,
2073 * because we need to digest WAL to advance restart_lsn allowing to recycle
2074 * WAL and removal of old catalog tuples. As decoding is done in fast_forward
2075 * mode, no changes are generated anyway.
2076 *
2077 * *found_consistent_snapshot will be true if the initial decoding snapshot has
2078 * been built; Otherwise, it will be false.
2079 */
2082 bool *found_consistent_snapshot)
2083{
2086 XLogRecPtr retlsn;
2087
2088 Assert(moveto != InvalidXLogRecPtr);
2089
2090 if (found_consistent_snapshot)
2091 *found_consistent_snapshot = false;
2092
2093 PG_TRY();
2094 {
2095 /*
2096 * Create our decoding context in fast_forward mode, passing start_lsn
2097 * as InvalidXLogRecPtr, so that we start processing from my slot's
2098 * confirmed_flush.
2099 */
2101 NIL,
2102 true, /* fast_forward */
2103 XL_ROUTINE(.page_read = read_local_xlog_page,
2104 .segment_open = wal_segment_open,
2105 .segment_close = wal_segment_close),
2106 NULL, NULL, NULL);
2107
2108 /*
2109 * Wait for specified streaming replication standby servers (if any)
2110 * to confirm receipt of WAL up to moveto lsn.
2111 */
2113
2114 /*
2115 * Start reading at the slot's restart_lsn, which we know to point to
2116 * a valid record.
2117 */
2119
2120 /* invalidate non-timetravel entries */
2122
2123 /* Decode records until we reach the requested target */
2124 while (ctx->reader->EndRecPtr < moveto)
2125 {
2126 char *errm = NULL;
2127 XLogRecord *record;
2128
2129 /*
2130 * Read records. No changes are generated in fast_forward mode,
2131 * but snapbuilder/slot statuses are updated properly.
2132 */
2133 record = XLogReadRecord(ctx->reader, &errm);
2134 if (errm)
2135 elog(ERROR, "could not find record while advancing replication slot: %s",
2136 errm);
2137
2138 /*
2139 * Process the record. Storage-level changes are ignored in
2140 * fast_forward mode, but other modules (such as snapbuilder)
2141 * might still have critical updates to do.
2142 */
2143 if (record)
2144 {
2146
2147 /*
2148 * We used to have bugs where logical decoding would fail to
2149 * preserve the resource owner. That's important here, so
2150 * verify that that doesn't happen anymore. XXX this could be
2151 * removed once it's been battle-tested.
2152 */
2153 Assert(CurrentResourceOwner == old_resowner);
2154 }
2155
2157 }
2158
2159 if (found_consistent_snapshot && DecodingContextReady(ctx))
2160 *found_consistent_snapshot = true;
2161
2162 if (ctx->reader->EndRecPtr != InvalidXLogRecPtr)
2163 {
2165
2166 /*
2167 * If only the confirmed_flush LSN has changed the slot won't get
2168 * marked as dirty by the above. Callers on the walsender
2169 * interface are expected to keep track of their own progress and
2170 * don't need it written out. But SQL-interface users cannot
2171 * specify their own start positions and it's harder for them to
2172 * keep track of their progress, so we should make more of an
2173 * effort to save it for them.
2174 *
2175 * Dirty the slot so it is written out at the next checkpoint. The
2176 * LSN position advanced to may still be lost on a crash but this
2177 * makes the data consistent after a clean shutdown.
2178 */
2180 }
2181
2183
2184 /* free context, call shutdown callback */
2186
2188 }
2189 PG_CATCH();
2190 {
2191 /* clear all timetravel entries */
2193
2194 PG_RE_THROW();
2195 }
2196 PG_END_TRY();
2197
2198 return retlsn;
2199}
#define NameStr(name)
Definition: c.h:752
#define pg_attribute_unused()
Definition: c.h:132
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:223
uint32 TransactionId
Definition: c.h:658
size_t Size
Definition: c.h:611
int64 TimestampTz
Definition: timestamp.h:39
void LogicalDecodingProcessRecord(LogicalDecodingContext *ctx, XLogReaderState *record)
Definition: decode.c:88
void * load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
Definition: dfmgr.c:95
int errdetail(const char *fmt,...)
Definition: elog.c:1207
ErrorContextCallback * error_context_stack
Definition: elog.c:95
int errhint(const char *fmt,...)
Definition: elog.c:1321
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define LOG
Definition: elog.h:31
#define PG_RE_THROW()
Definition: elog.h:405
#define errcontext
Definition: elog.h:198
#define PG_TRY(...)
Definition: elog.h:372
#define DEBUG2
Definition: elog.h:29
#define PG_END_TRY(...)
Definition: elog.h:397
#define DEBUG1
Definition: elog.h:30
#define ERROR
Definition: elog.h:39
#define PG_CATCH(...)
Definition: elog.h:382
#define elog(elevel,...)
Definition: elog.h:226
#define ereport(elevel,...)
Definition: elog.h:150
void err(int eval, const char *fmt,...)
Definition: err.c:43
Oid MyDatabaseId
Definition: globals.c:94
Assert(PointerIsAligned(start, uint64))
#define INJECTION_POINT(name, arg)
void InvalidateSystemCaches(void)
Definition: inval.c:916
static void change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
Definition: logical.c:1086
static void commit_prepared_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:994
static void update_progress_txn_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr lsn)
Definition: logical.c:1629
XLogRecPtr LogicalSlotAdvanceAndCheckSnapState(XLogRecPtr moveto, bool *found_consistent_snapshot)
Definition: logical.c:2081
void LogicalConfirmReceivedLocation(XLogRecPtr lsn)
Definition: logical.c:1820
void FreeDecodingContext(LogicalDecodingContext *ctx)
Definition: logical.c:677
bool LogicalReplicationSlotHasPendingWal(XLogRecPtr end_of_wal)
Definition: logical.c:1999
static void stream_start_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr first_lsn)
Definition: logical.c:1267
static void commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:866
static void output_plugin_error_callback(void *arg)
Definition: logical.c:755
static void begin_prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
Definition: logical.c:905
static void stream_prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
Definition: logical.c:1406
LogicalDecodingContext * CreateDecodingContext(XLogRecPtr start_lsn, List *output_plugin_options, bool fast_forward, XLogReaderRoutine *xl_routine, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
Definition: logical.c:498
static void prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
Definition: logical.c:949
void OutputPluginWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:705
static void stream_truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
Definition: logical.c:1582
static void truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
Definition: logical.c:1125
void DecodingContextFindStartpoint(LogicalDecodingContext *ctx)
Definition: logical.c:633
static void begin_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
Definition: logical.c:835
static void rollback_prepared_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_end_lsn, TimestampTz prepare_time)
Definition: logical.c:1039
bool DecodingContextReady(LogicalDecodingContext *ctx)
Definition: logical.c:624
void OutputPluginUpdateProgress(struct LogicalDecodingContext *ctx, bool skipped_xact)
Definition: logical.c:718
static void startup_cb_wrapper(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
Definition: logical.c:774
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: logical.c:332
void UpdateDecodingStats(LogicalDecodingContext *ctx)
Definition: logical.c:1952
void LogicalIncreaseRestartDecodingForSlot(XLogRecPtr current_lsn, XLogRecPtr restart_lsn)
Definition: logical.c:1744
static void stream_change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
Definition: logical.c:1492
static void stream_abort_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr abort_lsn)
Definition: logical.c:1365
void ResetLogicalStreamingState(void)
Definition: logical.c:1942
void LogicalIncreaseXminForSlot(XLogRecPtr current_lsn, TransactionId xmin)
Definition: logical.c:1676
static LogicalDecodingContext * StartupDecodingContext(List *output_plugin_options, XLogRecPtr start_lsn, TransactionId xmin_horizon, bool need_full_snapshot, bool fast_forward, bool in_create, XLogReaderRoutine *xl_routine, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
Definition: logical.c:152
struct LogicalErrorCallbackState LogicalErrorCallbackState
static void stream_message_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr message_lsn, bool transactional, const char *prefix, Size message_size, const char *message)
Definition: logical.c:1541
static void stream_commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:1451
bool filter_prepare_cb_wrapper(LogicalDecodingContext *ctx, TransactionId xid, const char *gid)
Definition: logical.c:1167
static void shutdown_cb_wrapper(LogicalDecodingContext *ctx)
Definition: logical.c:802
void OutputPluginPrepareWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:692
void CheckLogicalDecodingRequirements(void)
Definition: logical.c:111
bool filter_by_origin_cb_wrapper(LogicalDecodingContext *ctx, RepOriginId origin_id)
Definition: logical.c:1199
static void LoadOutputPlugin(OutputPluginCallbacks *callbacks, const char *plugin)
Definition: logical.c:733
static void stream_stop_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr last_lsn)
Definition: logical.c:1316
static void message_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr message_lsn, bool transactional, const char *prefix, Size message_size, const char *message)
Definition: logical.c:1230
void(* LogicalOutputPluginWriterUpdateProgress)(struct LogicalDecodingContext *lr, XLogRecPtr Ptr, TransactionId xid, bool skipped_xact)
Definition: logical.h:27
void(* LogicalOutputPluginWriterWrite)(struct LogicalDecodingContext *lr, XLogRecPtr Ptr, TransactionId xid, bool last_write)
Definition: logical.h:19
LogicalOutputPluginWriterWrite LogicalOutputPluginWriterPrepareWrite
Definition: logical.h:25
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1174
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1894
@ LW_EXCLUSIVE
Definition: lwlock.h:112
void * palloc0(Size size)
Definition: mcxt.c:1395
MemoryContext CurrentMemoryContext
Definition: mcxt.c:160
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:469
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
void namestrcpy(Name name, const char *str)
Definition: name.c:233
void(* LogicalOutputPluginInit)(struct OutputPluginCallbacks *cb)
Definition: output_plugin.h:36
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
void * arg
const void * data
#define NIL
Definition: pg_list.h:68
static const char * plugin
void pgstat_report_replslot(ReplicationSlot *slot, const PgStat_StatReplSlotEntry *repSlotStat)
#define InvalidOid
Definition: postgres_ext.h:37
#define PROC_IN_LOGICAL_DECODING
Definition: proc.h:61
TransactionId GetOldestSafeDecodingTransactionId(bool catalogOnly)
Definition: procarray.c:2907
ReorderBuffer * ReorderBufferAllocate(void)
void ReorderBufferFree(ReorderBuffer *rb)
ResourceOwner CurrentResourceOwner
Definition: resowner.c:173
void ReplicationSlotMarkDirty(void)
Definition: slot.c:1106
void ReplicationSlotReserveWal(void)
Definition: slot.c:1539
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:1145
ReplicationSlot * MyReplicationSlot
Definition: slot.c:148
void ReplicationSlotSave(void)
Definition: slot.c:1088
void WaitForStandbyConfirmation(XLogRecPtr wait_for_lsn)
Definition: slot.c:3059
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:1201
void CheckSlotRequirements(void)
Definition: slot.c:1500
#define SlotIsPhysical(slot)
Definition: slot.h:254
@ RS_INVAL_NONE
Definition: slot.h:60
bool IsSyncingReplicationSlots(void)
Definition: slotsync.c:1668
void SnapBuildSetTwoPhaseAt(SnapBuild *builder, XLogRecPtr ptr)
Definition: snapbuild.c:295
SnapBuildState SnapBuildCurrentState(SnapBuild *builder)
Definition: snapbuild.c:277
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
void FreeSnapshotBuilder(SnapBuild *builder)
Definition: snapbuild.c:233
@ SNAPBUILD_CONSISTENT
Definition: snapbuild.h:50
#define SpinLockRelease(lock)
Definition: spin.h:61
#define SpinLockAcquire(lock)
Definition: spin.h:59
PGPROC * MyProc
Definition: proc.c:66
PROC_HDR * ProcGlobal
Definition: proc.c:78
StringInfo makeStringInfo(void)
Definition: stringinfo.c:72
struct ErrorContextCallback * previous
Definition: elog.h:297
void(* callback)(void *arg)
Definition: elog.h:298
Definition: pg_list.h:54
OutputPluginOptions options
Definition: logical.h:54
XLogReaderState * reader
Definition: logical.h:42
MemoryContext context
Definition: logical.h:36
struct SnapBuild * snapshot_builder
Definition: logical.h:44
StringInfo out
Definition: logical.h:71
XLogRecPtr write_location
Definition: logical.h:108
LogicalOutputPluginWriterPrepareWrite prepare_write
Definition: logical.h:64
OutputPluginCallbacks callbacks
Definition: logical.h:53
TransactionId write_xid
Definition: logical.h:109
List * output_plugin_options
Definition: logical.h:59
ReplicationSlot * slot
Definition: logical.h:39
LogicalOutputPluginWriterWrite write
Definition: logical.h:65
struct ReorderBuffer * reorder
Definition: logical.h:43
LogicalOutputPluginWriterUpdateProgress update_progress
Definition: logical.h:66
XLogRecPtr report_location
Definition: logical.c:54
LogicalDecodingContext * ctx
Definition: logical.c:52
const char * callback_name
Definition: logical.c:53
LogicalDecodeStreamChangeCB stream_change_cb
LogicalDecodeMessageCB message_cb
LogicalDecodeStreamTruncateCB stream_truncate_cb
LogicalDecodeStreamMessageCB stream_message_cb
LogicalDecodeFilterPrepareCB filter_prepare_cb
LogicalDecodeFilterByOriginCB filter_by_origin_cb
LogicalDecodeTruncateCB truncate_cb
LogicalDecodeStreamStopCB stream_stop_cb
LogicalDecodeStreamCommitCB stream_commit_cb
LogicalDecodeRollbackPreparedCB rollback_prepared_cb
LogicalDecodeStreamPrepareCB stream_prepare_cb
LogicalDecodeCommitPreparedCB commit_prepared_cb
LogicalDecodeStreamStartCB stream_start_cb
LogicalDecodePrepareCB prepare_cb
LogicalDecodeStartupCB startup_cb
LogicalDecodeCommitCB commit_cb
LogicalDecodeBeginCB begin_cb
LogicalDecodeStreamAbortCB stream_abort_cb
LogicalDecodeBeginPrepareCB begin_prepare_cb
LogicalDecodeChangeCB change_cb
LogicalDecodeShutdownCB shutdown_cb
uint8 statusFlags
Definition: proc.h:259
int pgxactoff
Definition: proc.h:201
uint8 * statusFlags
Definition: proc.h:403
PgStat_Counter stream_count
Definition: pgstat.h:395
PgStat_Counter total_txns
Definition: pgstat.h:397
PgStat_Counter total_bytes
Definition: pgstat.h:398
PgStat_Counter spill_txns
Definition: pgstat.h:391
PgStat_Counter stream_txns
Definition: pgstat.h:394
PgStat_Counter spill_count
Definition: pgstat.h:392
PgStat_Counter stream_bytes
Definition: pgstat.h:396
PgStat_Counter spill_bytes
Definition: pgstat.h:393
XLogRecPtr first_lsn
XLogRecPtr final_lsn
XLogRecPtr end_lsn
TransactionId xid
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
void * private_data
TransactionId catalog_xmin
Definition: slot.h:104
XLogRecPtr confirmed_flush
Definition: slot.h:118
ReplicationSlotInvalidationCause invalidated
Definition: slot.h:110
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:208
TransactionId effective_catalog_xmin
Definition: slot.h:189
slock_t mutex
Definition: slot.h:165
XLogRecPtr candidate_restart_valid
Definition: slot.h:209
TransactionId effective_xmin
Definition: slot.h:188
XLogRecPtr candidate_restart_lsn
Definition: slot.h:210
TransactionId candidate_catalog_xmin
Definition: slot.h:207
ReplicationSlotPersistentData data
Definition: slot.h:192
XLogRecPtr EndRecPtr
Definition: xlogreader.h:207
Definition: c.h:747
Definition: regguts.h:323
bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:299
#define InvalidTransactionId
Definition: transam.h:31
#define TransactionIdIsValid(xid)
Definition: transam.h:41
bool IsTransactionOrTransactionBlock(void)
Definition: xact.c:5001
bool bsysscan
Definition: xact.c:100
TransactionId CheckXidAlive
Definition: xact.c:99
bool IsTransactionState(void)
Definition: xact.c:387
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:441
bool RecoveryInProgress(void)
Definition: xlog.c:6383
int wal_level
Definition: xlog.c:132
int wal_segment_size
Definition: xlog.c:144
WalLevel GetActiveWalLevelOnStandby(void)
Definition: xlog.c:4898
@ WAL_LEVEL_LOGICAL
Definition: xlog.h:76
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:46
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
uint16 RepOriginId
Definition: xlogdefs.h:68
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
uint64 XLogSegNo
Definition: xlogdefs.h:51
XLogReaderState * XLogReaderAllocate(int wal_segment_size, const char *waldir, XLogReaderRoutine *routine, void *private_data)
Definition: xlogreader.c:107
XLogRecord * XLogReadRecord(XLogReaderState *state, char **errormsg)
Definition: xlogreader.c:390
void XLogReaderFree(XLogReaderState *state)
Definition: xlogreader.c:162
void XLogBeginRead(XLogReaderState *state, XLogRecPtr RecPtr)
Definition: xlogreader.c:232
#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