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