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