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/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 /* callback to update txn's progress */
98  ReorderBufferTXN *txn,
99  XLogRecPtr lsn);
100 
101 static void LoadOutputPlugin(OutputPluginCallbacks *callbacks, const char *plugin);
102 
103 /*
104  * Make sure the current settings & environment are capable of doing logical
105  * decoding.
106  */
107 void
109 {
111 
112  /*
113  * NB: Adding a new requirement likely means that RestoreSlotFromDisk()
114  * needs the same check.
115  */
116 
118  ereport(ERROR,
119  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
120  errmsg("logical decoding requires wal_level >= logical")));
121 
122  if (MyDatabaseId == InvalidOid)
123  ereport(ERROR,
124  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
125  errmsg("logical decoding requires a database connection")));
126 
127  /* ----
128  * TODO: We got to change that someday soon...
129  *
130  * There's basically three things missing to allow this:
131  * 1) We need to be able to correctly and quickly identify the timeline a
132  * LSN belongs to
133  * 2) We need to force hot_standby_feedback to be enabled at all times so
134  * the primary cannot remove rows we need.
135  * 3) support dropping replication slots referring to a database, in
136  * dbase_redo. There can't be any active ones due to HS recovery
137  * conflicts, so that should be relatively easy.
138  * ----
139  */
140  if (RecoveryInProgress())
141  ereport(ERROR,
142  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
143  errmsg("logical decoding cannot be used while in recovery")));
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  /* shorter lines... */
346  slot = MyReplicationSlot;
347 
348  /* first some sanity checks that are unlikely to be violated */
349  if (slot == NULL)
350  elog(ERROR, "cannot perform logical decoding without an acquired slot");
351 
352  if (plugin == NULL)
353  elog(ERROR, "cannot initialize logical decoding without a specified plugin");
354 
355  /* Make sure the passed slot is suitable. These are user facing errors. */
356  if (SlotIsPhysical(slot))
357  ereport(ERROR,
358  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
359  errmsg("cannot use physical replication slot for logical decoding")));
360 
361  if (slot->data.database != MyDatabaseId)
362  ereport(ERROR,
363  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
364  errmsg("replication slot \"%s\" was not created in this database",
365  NameStr(slot->data.name))));
366 
367  if (IsTransactionState() &&
369  ereport(ERROR,
370  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
371  errmsg("cannot create logical replication slot in transaction that has performed writes")));
372 
373  /*
374  * Register output plugin name with slot. We need the mutex to avoid
375  * concurrent reading of a partially copied string. But we don't want any
376  * complicated code while holding a spinlock, so do namestrcpy() outside.
377  */
378  namestrcpy(&plugin_name, plugin);
379  SpinLockAcquire(&slot->mutex);
380  slot->data.plugin = plugin_name;
381  SpinLockRelease(&slot->mutex);
382 
383  if (XLogRecPtrIsInvalid(restart_lsn))
385  else
386  {
387  SpinLockAcquire(&slot->mutex);
388  slot->data.restart_lsn = restart_lsn;
389  SpinLockRelease(&slot->mutex);
390  }
391 
392  /* ----
393  * This is a bit tricky: We need to determine a safe xmin horizon to start
394  * decoding from, to avoid starting from a running xacts record referring
395  * to xids whose rows have been vacuumed or pruned
396  * already. GetOldestSafeDecodingTransactionId() returns such a value, but
397  * without further interlock its return value might immediately be out of
398  * date.
399  *
400  * So we have to acquire the ProcArrayLock to prevent computation of new
401  * xmin horizons by other backends, get the safe decoding xid, and inform
402  * the slot machinery about the new limit. Once that's done the
403  * ProcArrayLock can be released as the slot machinery now is
404  * protecting against vacuum.
405  *
406  * Note that, temporarily, the data, not just the catalog, xmin has to be
407  * reserved if a data snapshot is to be exported. Otherwise the initial
408  * data snapshot created here is not guaranteed to be valid. After that
409  * the data xmin doesn't need to be managed anymore and the global xmin
410  * should be recomputed. As we are fine with losing the pegged data xmin
411  * after crash - no chance a snapshot would get exported anymore - we can
412  * get away with just setting the slot's
413  * effective_xmin. ReplicationSlotRelease will reset it again.
414  *
415  * ----
416  */
417  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
418 
419  xmin_horizon = GetOldestSafeDecodingTransactionId(!need_full_snapshot);
420 
421  SpinLockAcquire(&slot->mutex);
422  slot->effective_catalog_xmin = xmin_horizon;
423  slot->data.catalog_xmin = xmin_horizon;
424  if (need_full_snapshot)
425  slot->effective_xmin = xmin_horizon;
426  SpinLockRelease(&slot->mutex);
427 
429 
430  LWLockRelease(ProcArrayLock);
431 
434 
435  ctx = StartupDecodingContext(NIL, restart_lsn, xmin_horizon,
436  need_full_snapshot, false,
437  xl_routine, prepare_write, do_write,
438  update_progress);
439 
440  /* call output plugin initialization callback */
441  old_context = MemoryContextSwitchTo(ctx->context);
442  if (ctx->callbacks.startup_cb != NULL)
443  startup_cb_wrapper(ctx, &ctx->options, true);
444  MemoryContextSwitchTo(old_context);
445 
446  /*
447  * We allow decoding of prepared transactions when the two_phase is
448  * enabled at the time of slot creation, or when the two_phase option is
449  * given at the streaming start, provided the plugin supports all the
450  * callbacks for two-phase.
451  */
452  ctx->twophase &= slot->data.two_phase;
453 
455 
456  return ctx;
457 }
458 
459 /*
460  * Create a new decoding context, for a logical slot that has previously been
461  * used already.
462  *
463  * start_lsn
464  * The LSN at which to start decoding. If InvalidXLogRecPtr, restart
465  * from the slot's confirmed_flush; otherwise, start from the specified
466  * location (but move it forwards to confirmed_flush if it's older than
467  * that, see below).
468  *
469  * output_plugin_options
470  * options passed to the output plugin.
471  *
472  * fast_forward
473  * bypass the generation of logical changes.
474  *
475  * xl_routine
476  * XLogReaderRoutine used by underlying xlogreader
477  *
478  * prepare_write, do_write, update_progress
479  * callbacks that have to be filled to perform the use-case dependent,
480  * actual work.
481  *
482  * Needs to be called while in a memory context that's at least as long lived
483  * as the decoding context because further memory contexts will be created
484  * inside it.
485  *
486  * Returns an initialized decoding context after calling the output plugin's
487  * startup function.
488  */
491  List *output_plugin_options,
492  bool fast_forward,
493  XLogReaderRoutine *xl_routine,
497 {
499  ReplicationSlot *slot;
500  MemoryContext old_context;
501 
502  /* shorter lines... */
503  slot = MyReplicationSlot;
504 
505  /* first some sanity checks that are unlikely to be violated */
506  if (slot == NULL)
507  elog(ERROR, "cannot perform logical decoding without an acquired slot");
508 
509  /* make sure the passed slot is suitable, these are user facing errors */
510  if (SlotIsPhysical(slot))
511  ereport(ERROR,
512  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
513  errmsg("cannot use physical replication slot for logical decoding")));
514 
515  if (slot->data.database != MyDatabaseId)
516  ereport(ERROR,
517  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
518  errmsg("replication slot \"%s\" was not created in this database",
519  NameStr(slot->data.name))));
520 
521  if (start_lsn == InvalidXLogRecPtr)
522  {
523  /* continue from last position */
524  start_lsn = slot->data.confirmed_flush;
525  }
526  else if (start_lsn < slot->data.confirmed_flush)
527  {
528  /*
529  * It might seem like we should error out in this case, but it's
530  * pretty common for a client to acknowledge a LSN it doesn't have to
531  * do anything for, and thus didn't store persistently, because the
532  * xlog records didn't result in anything relevant for logical
533  * decoding. Clients have to be able to do that to support synchronous
534  * replication.
535  *
536  * Starting at a different LSN than requested might not catch certain
537  * kinds of client errors; so the client may wish to check that
538  * confirmed_flush_lsn matches its expectations.
539  */
540  elog(LOG, "%X/%X has been already streamed, forwarding to %X/%X",
541  LSN_FORMAT_ARGS(start_lsn),
543 
544  start_lsn = slot->data.confirmed_flush;
545  }
546 
547  ctx = StartupDecodingContext(output_plugin_options,
548  start_lsn, InvalidTransactionId, false,
549  fast_forward, xl_routine, prepare_write,
550  do_write, update_progress);
551 
552  /* call output plugin initialization callback */
553  old_context = MemoryContextSwitchTo(ctx->context);
554  if (ctx->callbacks.startup_cb != NULL)
555  startup_cb_wrapper(ctx, &ctx->options, false);
556  MemoryContextSwitchTo(old_context);
557 
558  /*
559  * We allow decoding of prepared transactions when the two_phase is
560  * enabled at the time of slot creation, or when the two_phase option is
561  * given at the streaming start, provided the plugin supports all the
562  * callbacks for two-phase.
563  */
564  ctx->twophase &= (slot->data.two_phase || ctx->twophase_opt_given);
565 
566  /* Mark slot to allow two_phase decoding if not already marked */
567  if (ctx->twophase && !slot->data.two_phase)
568  {
569  SpinLockAcquire(&slot->mutex);
570  slot->data.two_phase = true;
571  slot->data.two_phase_at = start_lsn;
572  SpinLockRelease(&slot->mutex);
575  SnapBuildSetTwoPhaseAt(ctx->snapshot_builder, start_lsn);
576  }
577 
579 
580  ereport(LOG,
581  (errmsg("starting logical decoding for slot \"%s\"",
582  NameStr(slot->data.name)),
583  errdetail("Streaming transactions committing after %X/%X, reading WAL from %X/%X.",
585  LSN_FORMAT_ARGS(slot->data.restart_lsn))));
586 
587  return ctx;
588 }
589 
590 /*
591  * Returns true if a consistent initial decoding snapshot has been built.
592  */
593 bool
595 {
597 }
598 
599 /*
600  * Read from the decoding slot, until it is ready to start extracting changes.
601  */
602 void
604 {
605  ReplicationSlot *slot = ctx->slot;
606 
607  /* Initialize from where to start reading WAL. */
608  XLogBeginRead(ctx->reader, slot->data.restart_lsn);
609 
610  elog(DEBUG1, "searching for logical decoding starting point, starting at %X/%X",
612 
613  /* Wait for a consistent starting point */
614  for (;;)
615  {
616  XLogRecord *record;
617  char *err = NULL;
618 
619  /* the read_page callback waits for new WAL */
620  record = XLogReadRecord(ctx->reader, &err);
621  if (err)
622  elog(ERROR, "could not find logical decoding starting point: %s", err);
623  if (!record)
624  elog(ERROR, "could not find logical decoding starting point");
625 
627 
628  /* only continue till we found a consistent spot */
629  if (DecodingContextReady(ctx))
630  break;
631 
633  }
634 
635  SpinLockAcquire(&slot->mutex);
636  slot->data.confirmed_flush = ctx->reader->EndRecPtr;
637  if (slot->data.two_phase)
638  slot->data.two_phase_at = ctx->reader->EndRecPtr;
639  SpinLockRelease(&slot->mutex);
640 }
641 
642 /*
643  * Free a previously allocated decoding context, invoking the shutdown
644  * callback if necessary.
645  */
646 void
648 {
649  if (ctx->callbacks.shutdown_cb != NULL)
650  shutdown_cb_wrapper(ctx);
651 
654  XLogReaderFree(ctx->reader);
656 }
657 
658 /*
659  * Prepare a write using the context's output routine.
660  */
661 void
663 {
664  if (!ctx->accept_writes)
665  elog(ERROR, "writes are only accepted in commit, begin and change callbacks");
666 
667  ctx->prepare_write(ctx, ctx->write_location, ctx->write_xid, last_write);
668  ctx->prepared_write = true;
669 }
670 
671 /*
672  * Perform a write using the context's output routine.
673  */
674 void
675 OutputPluginWrite(struct LogicalDecodingContext *ctx, bool last_write)
676 {
677  if (!ctx->prepared_write)
678  elog(ERROR, "OutputPluginPrepareWrite needs to be called before OutputPluginWrite");
679 
680  ctx->write(ctx, ctx->write_location, ctx->write_xid, last_write);
681  ctx->prepared_write = false;
682 }
683 
684 /*
685  * Update progress tracking (if supported).
686  */
687 void
689  bool skipped_xact)
690 {
691  if (!ctx->update_progress)
692  return;
693 
694  ctx->update_progress(ctx, ctx->write_location, ctx->write_xid,
695  skipped_xact);
696 }
697 
698 /*
699  * Load the output plugin, lookup its output plugin init function, and check
700  * that it provides the required callbacks.
701  */
702 static void
704 {
705  LogicalOutputPluginInit plugin_init;
706 
707  plugin_init = (LogicalOutputPluginInit)
708  load_external_function(plugin, "_PG_output_plugin_init", false, NULL);
709 
710  if (plugin_init == NULL)
711  elog(ERROR, "output plugins have to declare the _PG_output_plugin_init symbol");
712 
713  /* ask the output plugin to fill the callback struct */
714  plugin_init(callbacks);
715 
716  if (callbacks->begin_cb == NULL)
717  elog(ERROR, "output plugins have to register a begin callback");
718  if (callbacks->change_cb == NULL)
719  elog(ERROR, "output plugins have to register a change callback");
720  if (callbacks->commit_cb == NULL)
721  elog(ERROR, "output plugins have to register a commit callback");
722 }
723 
724 static void
726 {
728 
729  /* not all callbacks have an associated LSN */
730  if (state->report_location != InvalidXLogRecPtr)
731  errcontext("slot \"%s\", output plugin \"%s\", in the %s callback, associated LSN %X/%X",
732  NameStr(state->ctx->slot->data.name),
733  NameStr(state->ctx->slot->data.plugin),
734  state->callback_name,
735  LSN_FORMAT_ARGS(state->report_location));
736  else
737  errcontext("slot \"%s\", output plugin \"%s\", in the %s callback",
738  NameStr(state->ctx->slot->data.name),
739  NameStr(state->ctx->slot->data.plugin),
740  state->callback_name);
741 }
742 
743 static void
745 {
747  ErrorContextCallback errcallback;
748 
749  Assert(!ctx->fast_forward);
750 
751  /* Push callback + info on the error context stack */
752  state.ctx = ctx;
753  state.callback_name = "startup";
754  state.report_location = InvalidXLogRecPtr;
756  errcallback.arg = (void *) &state;
757  errcallback.previous = error_context_stack;
758  error_context_stack = &errcallback;
759 
760  /* set output state */
761  ctx->accept_writes = false;
762  ctx->end_xact = false;
763 
764  /* do the actual work: call callback */
765  ctx->callbacks.startup_cb(ctx, opt, is_init);
766 
767  /* Pop the error context stack */
768  error_context_stack = errcallback.previous;
769 }
770 
771 static void
773 {
775  ErrorContextCallback errcallback;
776 
777  Assert(!ctx->fast_forward);
778 
779  /* Push callback + info on the error context stack */
780  state.ctx = ctx;
781  state.callback_name = "shutdown";
782  state.report_location = InvalidXLogRecPtr;
784  errcallback.arg = (void *) &state;
785  errcallback.previous = error_context_stack;
786  error_context_stack = &errcallback;
787 
788  /* set output state */
789  ctx->accept_writes = false;
790  ctx->end_xact = false;
791 
792  /* do the actual work: call callback */
793  ctx->callbacks.shutdown_cb(ctx);
794 
795  /* Pop the error context stack */
796  error_context_stack = errcallback.previous;
797 }
798 
799 
800 /*
801  * Callbacks for ReorderBuffer which add in some more information and then call
802  * output_plugin.h plugins.
803  */
804 static void
806 {
807  LogicalDecodingContext *ctx = cache->private_data;
809  ErrorContextCallback errcallback;
810 
811  Assert(!ctx->fast_forward);
812 
813  /* Push callback + info on the error context stack */
814  state.ctx = ctx;
815  state.callback_name = "begin";
816  state.report_location = txn->first_lsn;
818  errcallback.arg = (void *) &state;
819  errcallback.previous = error_context_stack;
820  error_context_stack = &errcallback;
821 
822  /* set output state */
823  ctx->accept_writes = true;
824  ctx->write_xid = txn->xid;
825  ctx->write_location = txn->first_lsn;
826  ctx->end_xact = false;
827 
828  /* do the actual work: call callback */
829  ctx->callbacks.begin_cb(ctx, txn);
830 
831  /* Pop the error context stack */
832  error_context_stack = errcallback.previous;
833 }
834 
835 static void
837  XLogRecPtr commit_lsn)
838 {
839  LogicalDecodingContext *ctx = cache->private_data;
841  ErrorContextCallback errcallback;
842 
843  Assert(!ctx->fast_forward);
844 
845  /* Push callback + info on the error context stack */
846  state.ctx = ctx;
847  state.callback_name = "commit";
848  state.report_location = txn->final_lsn; /* beginning of commit record */
850  errcallback.arg = (void *) &state;
851  errcallback.previous = error_context_stack;
852  error_context_stack = &errcallback;
853 
854  /* set output state */
855  ctx->accept_writes = true;
856  ctx->write_xid = txn->xid;
857  ctx->write_location = txn->end_lsn; /* points to the end of the record */
858  ctx->end_xact = true;
859 
860  /* do the actual work: call callback */
861  ctx->callbacks.commit_cb(ctx, txn, commit_lsn);
862 
863  /* Pop the error context stack */
864  error_context_stack = errcallback.previous;
865 }
866 
867 /*
868  * The functionality of begin_prepare is quite similar to begin with the
869  * exception that this will have gid (global transaction id) information which
870  * can be used by plugin. Now, we thought about extending the existing begin
871  * but that would break the replication protocol and additionally this looks
872  * cleaner.
873  */
874 static void
876 {
877  LogicalDecodingContext *ctx = cache->private_data;
879  ErrorContextCallback errcallback;
880 
881  Assert(!ctx->fast_forward);
882 
883  /* We're only supposed to call this when two-phase commits are supported */
884  Assert(ctx->twophase);
885 
886  /* Push callback + info on the error context stack */
887  state.ctx = ctx;
888  state.callback_name = "begin_prepare";
889  state.report_location = txn->first_lsn;
891  errcallback.arg = (void *) &state;
892  errcallback.previous = error_context_stack;
893  error_context_stack = &errcallback;
894 
895  /* set output state */
896  ctx->accept_writes = true;
897  ctx->write_xid = txn->xid;
898  ctx->write_location = txn->first_lsn;
899  ctx->end_xact = false;
900 
901  /*
902  * If the plugin supports two-phase commits then begin prepare callback is
903  * mandatory
904  */
905  if (ctx->callbacks.begin_prepare_cb == NULL)
906  ereport(ERROR,
907  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
908  errmsg("logical replication at prepare time requires a %s callback",
909  "begin_prepare_cb")));
910 
911  /* do the actual work: call callback */
912  ctx->callbacks.begin_prepare_cb(ctx, txn);
913 
914  /* Pop the error context stack */
915  error_context_stack = errcallback.previous;
916 }
917 
918 static void
920  XLogRecPtr prepare_lsn)
921 {
922  LogicalDecodingContext *ctx = cache->private_data;
924  ErrorContextCallback errcallback;
925 
926  Assert(!ctx->fast_forward);
927 
928  /* We're only supposed to call this when two-phase commits are supported */
929  Assert(ctx->twophase);
930 
931  /* Push callback + info on the error context stack */
932  state.ctx = ctx;
933  state.callback_name = "prepare";
934  state.report_location = txn->final_lsn; /* beginning of prepare record */
936  errcallback.arg = (void *) &state;
937  errcallback.previous = error_context_stack;
938  error_context_stack = &errcallback;
939 
940  /* set output state */
941  ctx->accept_writes = true;
942  ctx->write_xid = txn->xid;
943  ctx->write_location = txn->end_lsn; /* points to the end of the record */
944  ctx->end_xact = true;
945 
946  /*
947  * If the plugin supports two-phase commits then prepare callback is
948  * mandatory
949  */
950  if (ctx->callbacks.prepare_cb == NULL)
951  ereport(ERROR,
952  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
953  errmsg("logical replication at prepare time requires a %s callback",
954  "prepare_cb")));
955 
956  /* do the actual work: call callback */
957  ctx->callbacks.prepare_cb(ctx, txn, prepare_lsn);
958 
959  /* Pop the error context stack */
960  error_context_stack = errcallback.previous;
961 }
962 
963 static void
965  XLogRecPtr commit_lsn)
966 {
967  LogicalDecodingContext *ctx = cache->private_data;
969  ErrorContextCallback errcallback;
970 
971  Assert(!ctx->fast_forward);
972 
973  /* We're only supposed to call this when two-phase commits are supported */
974  Assert(ctx->twophase);
975 
976  /* Push callback + info on the error context stack */
977  state.ctx = ctx;
978  state.callback_name = "commit_prepared";
979  state.report_location = txn->final_lsn; /* beginning of commit record */
981  errcallback.arg = (void *) &state;
982  errcallback.previous = error_context_stack;
983  error_context_stack = &errcallback;
984 
985  /* set output state */
986  ctx->accept_writes = true;
987  ctx->write_xid = txn->xid;
988  ctx->write_location = txn->end_lsn; /* points to the end of the record */
989  ctx->end_xact = true;
990 
991  /*
992  * If the plugin support two-phase commits then commit prepared callback
993  * is mandatory
994  */
995  if (ctx->callbacks.commit_prepared_cb == NULL)
996  ereport(ERROR,
997  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
998  errmsg("logical replication at prepare time requires a %s callback",
999  "commit_prepared_cb")));
1000 
1001  /* do the actual work: call callback */
1002  ctx->callbacks.commit_prepared_cb(ctx, txn, commit_lsn);
1003 
1004  /* Pop the error context stack */
1005  error_context_stack = errcallback.previous;
1006 }
1007 
1008 static void
1010  XLogRecPtr prepare_end_lsn,
1011  TimestampTz prepare_time)
1012 {
1013  LogicalDecodingContext *ctx = cache->private_data;
1015  ErrorContextCallback errcallback;
1016 
1017  Assert(!ctx->fast_forward);
1018 
1019  /* We're only supposed to call this when two-phase commits are supported */
1020  Assert(ctx->twophase);
1021 
1022  /* Push callback + info on the error context stack */
1023  state.ctx = ctx;
1024  state.callback_name = "rollback_prepared";
1025  state.report_location = txn->final_lsn; /* beginning of commit record */
1026  errcallback.callback = output_plugin_error_callback;
1027  errcallback.arg = (void *) &state;
1028  errcallback.previous = error_context_stack;
1029  error_context_stack = &errcallback;
1030 
1031  /* set output state */
1032  ctx->accept_writes = true;
1033  ctx->write_xid = txn->xid;
1034  ctx->write_location = txn->end_lsn; /* points to the end of the record */
1035  ctx->end_xact = true;
1036 
1037  /*
1038  * If the plugin support two-phase commits then rollback prepared callback
1039  * is mandatory
1040  */
1041  if (ctx->callbacks.rollback_prepared_cb == NULL)
1042  ereport(ERROR,
1043  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1044  errmsg("logical replication at prepare time requires a %s callback",
1045  "rollback_prepared_cb")));
1046 
1047  /* do the actual work: call callback */
1048  ctx->callbacks.rollback_prepared_cb(ctx, txn, prepare_end_lsn,
1049  prepare_time);
1050 
1051  /* Pop the error context stack */
1052  error_context_stack = errcallback.previous;
1053 }
1054 
1055 static void
1057  Relation relation, ReorderBufferChange *change)
1058 {
1059  LogicalDecodingContext *ctx = cache->private_data;
1061  ErrorContextCallback errcallback;
1062 
1063  Assert(!ctx->fast_forward);
1064 
1065  /* Push callback + info on the error context stack */
1066  state.ctx = ctx;
1067  state.callback_name = "change";
1068  state.report_location = change->lsn;
1069  errcallback.callback = output_plugin_error_callback;
1070  errcallback.arg = (void *) &state;
1071  errcallback.previous = error_context_stack;
1072  error_context_stack = &errcallback;
1073 
1074  /* set output state */
1075  ctx->accept_writes = true;
1076  ctx->write_xid = txn->xid;
1077 
1078  /*
1079  * Report this change's lsn so replies from clients can give an up-to-date
1080  * answer. This won't ever be enough (and shouldn't be!) to confirm
1081  * receipt of this transaction, but it might allow another transaction's
1082  * commit to be confirmed with one message.
1083  */
1084  ctx->write_location = change->lsn;
1085 
1086  ctx->end_xact = false;
1087 
1088  ctx->callbacks.change_cb(ctx, txn, relation, change);
1089 
1090  /* Pop the error context stack */
1091  error_context_stack = errcallback.previous;
1092 }
1093 
1094 static void
1096  int nrelations, Relation relations[], ReorderBufferChange *change)
1097 {
1098  LogicalDecodingContext *ctx = cache->private_data;
1100  ErrorContextCallback errcallback;
1101 
1102  Assert(!ctx->fast_forward);
1103 
1104  if (!ctx->callbacks.truncate_cb)
1105  return;
1106 
1107  /* Push callback + info on the error context stack */
1108  state.ctx = ctx;
1109  state.callback_name = "truncate";
1110  state.report_location = change->lsn;
1111  errcallback.callback = output_plugin_error_callback;
1112  errcallback.arg = (void *) &state;
1113  errcallback.previous = error_context_stack;
1114  error_context_stack = &errcallback;
1115 
1116  /* set output state */
1117  ctx->accept_writes = true;
1118  ctx->write_xid = txn->xid;
1119 
1120  /*
1121  * Report this change's lsn so replies from clients can give an up-to-date
1122  * answer. This won't ever be enough (and shouldn't be!) to confirm
1123  * receipt of this transaction, but it might allow another transaction's
1124  * commit to be confirmed with one message.
1125  */
1126  ctx->write_location = change->lsn;
1127 
1128  ctx->end_xact = false;
1129 
1130  ctx->callbacks.truncate_cb(ctx, txn, nrelations, relations, change);
1131 
1132  /* Pop the error context stack */
1133  error_context_stack = errcallback.previous;
1134 }
1135 
1136 bool
1138  const char *gid)
1139 {
1141  ErrorContextCallback errcallback;
1142  bool ret;
1143 
1144  Assert(!ctx->fast_forward);
1145 
1146  /* Push callback + info on the error context stack */
1147  state.ctx = ctx;
1148  state.callback_name = "filter_prepare";
1149  state.report_location = InvalidXLogRecPtr;
1150  errcallback.callback = output_plugin_error_callback;
1151  errcallback.arg = (void *) &state;
1152  errcallback.previous = error_context_stack;
1153  error_context_stack = &errcallback;
1154 
1155  /* set output state */
1156  ctx->accept_writes = false;
1157  ctx->end_xact = false;
1158 
1159  /* do the actual work: call callback */
1160  ret = ctx->callbacks.filter_prepare_cb(ctx, xid, gid);
1161 
1162  /* Pop the error context stack */
1163  error_context_stack = errcallback.previous;
1164 
1165  return ret;
1166 }
1167 
1168 bool
1170 {
1172  ErrorContextCallback errcallback;
1173  bool ret;
1174 
1175  Assert(!ctx->fast_forward);
1176 
1177  /* Push callback + info on the error context stack */
1178  state.ctx = ctx;
1179  state.callback_name = "filter_by_origin";
1180  state.report_location = InvalidXLogRecPtr;
1181  errcallback.callback = output_plugin_error_callback;
1182  errcallback.arg = (void *) &state;
1183  errcallback.previous = error_context_stack;
1184  error_context_stack = &errcallback;
1185 
1186  /* set output state */
1187  ctx->accept_writes = false;
1188  ctx->end_xact = false;
1189 
1190  /* do the actual work: call callback */
1191  ret = ctx->callbacks.filter_by_origin_cb(ctx, origin_id);
1192 
1193  /* Pop the error context stack */
1194  error_context_stack = errcallback.previous;
1195 
1196  return ret;
1197 }
1198 
1199 static void
1201  XLogRecPtr message_lsn, bool transactional,
1202  const char *prefix, Size message_size, const char *message)
1203 {
1204  LogicalDecodingContext *ctx = cache->private_data;
1206  ErrorContextCallback errcallback;
1207 
1208  Assert(!ctx->fast_forward);
1209 
1210  if (ctx->callbacks.message_cb == NULL)
1211  return;
1212 
1213  /* Push callback + info on the error context stack */
1214  state.ctx = ctx;
1215  state.callback_name = "message";
1216  state.report_location = message_lsn;
1217  errcallback.callback = output_plugin_error_callback;
1218  errcallback.arg = (void *) &state;
1219  errcallback.previous = error_context_stack;
1220  error_context_stack = &errcallback;
1221 
1222  /* set output state */
1223  ctx->accept_writes = true;
1224  ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
1225  ctx->write_location = message_lsn;
1226  ctx->end_xact = false;
1227 
1228  /* do the actual work: call callback */
1229  ctx->callbacks.message_cb(ctx, txn, message_lsn, transactional, prefix,
1230  message_size, message);
1231 
1232  /* Pop the error context stack */
1233  error_context_stack = errcallback.previous;
1234 }
1235 
1236 static void
1238  XLogRecPtr first_lsn)
1239 {
1240  LogicalDecodingContext *ctx = cache->private_data;
1242  ErrorContextCallback errcallback;
1243 
1244  Assert(!ctx->fast_forward);
1245 
1246  /* We're only supposed to call this when streaming is supported. */
1247  Assert(ctx->streaming);
1248 
1249  /* Push callback + info on the error context stack */
1250  state.ctx = ctx;
1251  state.callback_name = "stream_start";
1252  state.report_location = first_lsn;
1253  errcallback.callback = output_plugin_error_callback;
1254  errcallback.arg = (void *) &state;
1255  errcallback.previous = error_context_stack;
1256  error_context_stack = &errcallback;
1257 
1258  /* set output state */
1259  ctx->accept_writes = true;
1260  ctx->write_xid = txn->xid;
1261 
1262  /*
1263  * Report this message's lsn so replies from clients can give an
1264  * up-to-date answer. This won't ever be enough (and shouldn't be!) to
1265  * confirm receipt of this transaction, but it might allow another
1266  * transaction's commit to be confirmed with one message.
1267  */
1268  ctx->write_location = first_lsn;
1269 
1270  ctx->end_xact = false;
1271 
1272  /* in streaming mode, stream_start_cb is required */
1273  if (ctx->callbacks.stream_start_cb == NULL)
1274  ereport(ERROR,
1275  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1276  errmsg("logical streaming requires a %s callback",
1277  "stream_start_cb")));
1278 
1279  ctx->callbacks.stream_start_cb(ctx, txn);
1280 
1281  /* Pop the error context stack */
1282  error_context_stack = errcallback.previous;
1283 }
1284 
1285 static void
1287  XLogRecPtr last_lsn)
1288 {
1289  LogicalDecodingContext *ctx = cache->private_data;
1291  ErrorContextCallback errcallback;
1292 
1293  Assert(!ctx->fast_forward);
1294 
1295  /* We're only supposed to call this when streaming is supported. */
1296  Assert(ctx->streaming);
1297 
1298  /* Push callback + info on the error context stack */
1299  state.ctx = ctx;
1300  state.callback_name = "stream_stop";
1301  state.report_location = last_lsn;
1302  errcallback.callback = output_plugin_error_callback;
1303  errcallback.arg = (void *) &state;
1304  errcallback.previous = error_context_stack;
1305  error_context_stack = &errcallback;
1306 
1307  /* set output state */
1308  ctx->accept_writes = true;
1309  ctx->write_xid = txn->xid;
1310 
1311  /*
1312  * Report this message's lsn so replies from clients can give an
1313  * up-to-date answer. This won't ever be enough (and shouldn't be!) to
1314  * confirm receipt of this transaction, but it might allow another
1315  * transaction's commit to be confirmed with one message.
1316  */
1317  ctx->write_location = last_lsn;
1318 
1319  ctx->end_xact = false;
1320 
1321  /* in streaming mode, stream_stop_cb is required */
1322  if (ctx->callbacks.stream_stop_cb == NULL)
1323  ereport(ERROR,
1324  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1325  errmsg("logical streaming requires a %s callback",
1326  "stream_stop_cb")));
1327 
1328  ctx->callbacks.stream_stop_cb(ctx, txn);
1329 
1330  /* Pop the error context stack */
1331  error_context_stack = errcallback.previous;
1332 }
1333 
1334 static void
1336  XLogRecPtr abort_lsn)
1337 {
1338  LogicalDecodingContext *ctx = cache->private_data;
1340  ErrorContextCallback errcallback;
1341 
1342  Assert(!ctx->fast_forward);
1343 
1344  /* We're only supposed to call this when streaming is supported. */
1345  Assert(ctx->streaming);
1346 
1347  /* Push callback + info on the error context stack */
1348  state.ctx = ctx;
1349  state.callback_name = "stream_abort";
1350  state.report_location = abort_lsn;
1351  errcallback.callback = output_plugin_error_callback;
1352  errcallback.arg = (void *) &state;
1353  errcallback.previous = error_context_stack;
1354  error_context_stack = &errcallback;
1355 
1356  /* set output state */
1357  ctx->accept_writes = true;
1358  ctx->write_xid = txn->xid;
1359  ctx->write_location = abort_lsn;
1360  ctx->end_xact = true;
1361 
1362  /* in streaming mode, stream_abort_cb is required */
1363  if (ctx->callbacks.stream_abort_cb == NULL)
1364  ereport(ERROR,
1365  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1366  errmsg("logical streaming requires a %s callback",
1367  "stream_abort_cb")));
1368 
1369  ctx->callbacks.stream_abort_cb(ctx, txn, abort_lsn);
1370 
1371  /* Pop the error context stack */
1372  error_context_stack = errcallback.previous;
1373 }
1374 
1375 static void
1377  XLogRecPtr prepare_lsn)
1378 {
1379  LogicalDecodingContext *ctx = cache->private_data;
1381  ErrorContextCallback errcallback;
1382 
1383  Assert(!ctx->fast_forward);
1384 
1385  /*
1386  * We're only supposed to call this when streaming and two-phase commits
1387  * are supported.
1388  */
1389  Assert(ctx->streaming);
1390  Assert(ctx->twophase);
1391 
1392  /* Push callback + info on the error context stack */
1393  state.ctx = ctx;
1394  state.callback_name = "stream_prepare";
1395  state.report_location = txn->final_lsn;
1396  errcallback.callback = output_plugin_error_callback;
1397  errcallback.arg = (void *) &state;
1398  errcallback.previous = error_context_stack;
1399  error_context_stack = &errcallback;
1400 
1401  /* set output state */
1402  ctx->accept_writes = true;
1403  ctx->write_xid = txn->xid;
1404  ctx->write_location = txn->end_lsn;
1405  ctx->end_xact = true;
1406 
1407  /* in streaming mode with two-phase commits, stream_prepare_cb is required */
1408  if (ctx->callbacks.stream_prepare_cb == NULL)
1409  ereport(ERROR,
1410  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1411  errmsg("logical streaming at prepare time requires a %s callback",
1412  "stream_prepare_cb")));
1413 
1414  ctx->callbacks.stream_prepare_cb(ctx, txn, prepare_lsn);
1415 
1416  /* Pop the error context stack */
1417  error_context_stack = errcallback.previous;
1418 }
1419 
1420 static void
1422  XLogRecPtr commit_lsn)
1423 {
1424  LogicalDecodingContext *ctx = cache->private_data;
1426  ErrorContextCallback errcallback;
1427 
1428  Assert(!ctx->fast_forward);
1429 
1430  /* We're only supposed to call this when streaming is supported. */
1431  Assert(ctx->streaming);
1432 
1433  /* Push callback + info on the error context stack */
1434  state.ctx = ctx;
1435  state.callback_name = "stream_commit";
1436  state.report_location = txn->final_lsn;
1437  errcallback.callback = output_plugin_error_callback;
1438  errcallback.arg = (void *) &state;
1439  errcallback.previous = error_context_stack;
1440  error_context_stack = &errcallback;
1441 
1442  /* set output state */
1443  ctx->accept_writes = true;
1444  ctx->write_xid = txn->xid;
1445  ctx->write_location = txn->end_lsn;
1446  ctx->end_xact = true;
1447 
1448  /* in streaming mode, stream_commit_cb is required */
1449  if (ctx->callbacks.stream_commit_cb == NULL)
1450  ereport(ERROR,
1451  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1452  errmsg("logical streaming requires a %s callback",
1453  "stream_commit_cb")));
1454 
1455  ctx->callbacks.stream_commit_cb(ctx, txn, commit_lsn);
1456 
1457  /* Pop the error context stack */
1458  error_context_stack = errcallback.previous;
1459 }
1460 
1461 static void
1463  Relation relation, ReorderBufferChange *change)
1464 {
1465  LogicalDecodingContext *ctx = cache->private_data;
1467  ErrorContextCallback errcallback;
1468 
1469  Assert(!ctx->fast_forward);
1470 
1471  /* We're only supposed to call this when streaming is supported. */
1472  Assert(ctx->streaming);
1473 
1474  /* Push callback + info on the error context stack */
1475  state.ctx = ctx;
1476  state.callback_name = "stream_change";
1477  state.report_location = change->lsn;
1478  errcallback.callback = output_plugin_error_callback;
1479  errcallback.arg = (void *) &state;
1480  errcallback.previous = error_context_stack;
1481  error_context_stack = &errcallback;
1482 
1483  /* set output state */
1484  ctx->accept_writes = true;
1485  ctx->write_xid = txn->xid;
1486 
1487  /*
1488  * Report this change's lsn so replies from clients can give an up-to-date
1489  * answer. This won't ever be enough (and shouldn't be!) to confirm
1490  * receipt of this transaction, but it might allow another transaction's
1491  * commit to be confirmed with one message.
1492  */
1493  ctx->write_location = change->lsn;
1494 
1495  ctx->end_xact = false;
1496 
1497  /* in streaming mode, stream_change_cb is required */
1498  if (ctx->callbacks.stream_change_cb == NULL)
1499  ereport(ERROR,
1500  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1501  errmsg("logical streaming requires a %s callback",
1502  "stream_change_cb")));
1503 
1504  ctx->callbacks.stream_change_cb(ctx, txn, relation, change);
1505 
1506  /* Pop the error context stack */
1507  error_context_stack = errcallback.previous;
1508 }
1509 
1510 static void
1512  XLogRecPtr message_lsn, bool transactional,
1513  const char *prefix, Size message_size, const char *message)
1514 {
1515  LogicalDecodingContext *ctx = cache->private_data;
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_message_cb == NULL)
1526  return;
1527 
1528  /* Push callback + info on the error context stack */
1529  state.ctx = ctx;
1530  state.callback_name = "stream_message";
1531  state.report_location = message_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 != NULL ? txn->xid : InvalidTransactionId;
1540  ctx->write_location = message_lsn;
1541  ctx->end_xact = false;
1542 
1543  /* do the actual work: call callback */
1544  ctx->callbacks.stream_message_cb(ctx, txn, message_lsn, transactional, prefix,
1545  message_size, message);
1546 
1547  /* Pop the error context stack */
1548  error_context_stack = errcallback.previous;
1549 }
1550 
1551 static void
1553  int nrelations, Relation relations[],
1554  ReorderBufferChange *change)
1555 {
1556  LogicalDecodingContext *ctx = cache->private_data;
1558  ErrorContextCallback errcallback;
1559 
1560  Assert(!ctx->fast_forward);
1561 
1562  /* We're only supposed to call this when streaming is supported. */
1563  Assert(ctx->streaming);
1564 
1565  /* this callback is optional */
1566  if (!ctx->callbacks.stream_truncate_cb)
1567  return;
1568 
1569  /* Push callback + info on the error context stack */
1570  state.ctx = ctx;
1571  state.callback_name = "stream_truncate";
1572  state.report_location = change->lsn;
1573  errcallback.callback = output_plugin_error_callback;
1574  errcallback.arg = (void *) &state;
1575  errcallback.previous = error_context_stack;
1576  error_context_stack = &errcallback;
1577 
1578  /* set output state */
1579  ctx->accept_writes = true;
1580  ctx->write_xid = txn->xid;
1581 
1582  /*
1583  * Report this change's lsn so replies from clients can give an up-to-date
1584  * answer. This won't ever be enough (and shouldn't be!) to confirm
1585  * receipt of this transaction, but it might allow another transaction's
1586  * commit to be confirmed with one message.
1587  */
1588  ctx->write_location = change->lsn;
1589 
1590  ctx->end_xact = false;
1591 
1592  ctx->callbacks.stream_truncate_cb(ctx, txn, nrelations, relations, change);
1593 
1594  /* Pop the error context stack */
1595  error_context_stack = errcallback.previous;
1596 }
1597 
1598 static void
1600  XLogRecPtr lsn)
1601 {
1602  LogicalDecodingContext *ctx = cache->private_data;
1604  ErrorContextCallback errcallback;
1605 
1606  Assert(!ctx->fast_forward);
1607 
1608  /* Push callback + info on the error context stack */
1609  state.ctx = ctx;
1610  state.callback_name = "update_progress_txn";
1611  state.report_location = lsn;
1612  errcallback.callback = output_plugin_error_callback;
1613  errcallback.arg = (void *) &state;
1614  errcallback.previous = error_context_stack;
1615  error_context_stack = &errcallback;
1616 
1617  /* set output state */
1618  ctx->accept_writes = false;
1619  ctx->write_xid = txn->xid;
1620 
1621  /*
1622  * Report this change's lsn so replies from clients can give an up-to-date
1623  * answer. This won't ever be enough (and shouldn't be!) to confirm
1624  * receipt of this transaction, but it might allow another transaction's
1625  * commit to be confirmed with one message.
1626  */
1627  ctx->write_location = lsn;
1628 
1629  ctx->end_xact = false;
1630 
1631  OutputPluginUpdateProgress(ctx, false);
1632 
1633  /* Pop the error context stack */
1634  error_context_stack = errcallback.previous;
1635 }
1636 
1637 /*
1638  * Set the required catalog xmin horizon for historic snapshots in the current
1639  * replication slot.
1640  *
1641  * Note that in the most cases, we won't be able to immediately use the xmin
1642  * to increase the xmin horizon: we need to wait till the client has confirmed
1643  * receiving current_lsn with LogicalConfirmReceivedLocation().
1644  */
1645 void
1647 {
1648  bool updated_xmin = false;
1649  ReplicationSlot *slot;
1650  bool got_new_xmin = false;
1651 
1652  slot = MyReplicationSlot;
1653 
1654  Assert(slot != NULL);
1655 
1656  SpinLockAcquire(&slot->mutex);
1657 
1658  /*
1659  * don't overwrite if we already have a newer xmin. This can happen if we
1660  * restart decoding in a slot.
1661  */
1663  {
1664  }
1665 
1666  /*
1667  * If the client has already confirmed up to this lsn, we directly can
1668  * mark this as accepted. This can happen if we restart decoding in a
1669  * slot.
1670  */
1671  else if (current_lsn <= slot->data.confirmed_flush)
1672  {
1673  slot->candidate_catalog_xmin = xmin;
1674  slot->candidate_xmin_lsn = current_lsn;
1675 
1676  /* our candidate can directly be used */
1677  updated_xmin = true;
1678  }
1679 
1680  /*
1681  * Only increase if the previous values have been applied, otherwise we
1682  * might never end up updating if the receiver acks too slowly.
1683  */
1684  else if (slot->candidate_xmin_lsn == InvalidXLogRecPtr)
1685  {
1686  slot->candidate_catalog_xmin = xmin;
1687  slot->candidate_xmin_lsn = current_lsn;
1688 
1689  /*
1690  * Log new xmin at an appropriate log level after releasing the
1691  * spinlock.
1692  */
1693  got_new_xmin = true;
1694  }
1695  SpinLockRelease(&slot->mutex);
1696 
1697  if (got_new_xmin)
1698  elog(DEBUG1, "got new catalog xmin %u at %X/%X", xmin,
1699  LSN_FORMAT_ARGS(current_lsn));
1700 
1701  /* candidate already valid with the current flush position, apply */
1702  if (updated_xmin)
1704 }
1705 
1706 /*
1707  * Mark the minimal LSN (restart_lsn) we need to read to replay all
1708  * transactions that have not yet committed at current_lsn.
1709  *
1710  * Just like LogicalIncreaseXminForSlot this only takes effect when the
1711  * client has confirmed to have received current_lsn.
1712  */
1713 void
1715 {
1716  bool updated_lsn = false;
1717  ReplicationSlot *slot;
1718 
1719  slot = MyReplicationSlot;
1720 
1721  Assert(slot != NULL);
1722  Assert(restart_lsn != InvalidXLogRecPtr);
1723  Assert(current_lsn != InvalidXLogRecPtr);
1724 
1725  SpinLockAcquire(&slot->mutex);
1726 
1727  /* don't overwrite if have a newer restart lsn */
1728  if (restart_lsn <= slot->data.restart_lsn)
1729  {
1730  }
1731 
1732  /*
1733  * We might have already flushed far enough to directly accept this lsn,
1734  * in this case there is no need to check for existing candidate LSNs
1735  */
1736  else if (current_lsn <= slot->data.confirmed_flush)
1737  {
1738  slot->candidate_restart_valid = current_lsn;
1739  slot->candidate_restart_lsn = restart_lsn;
1740 
1741  /* our candidate can directly be used */
1742  updated_lsn = true;
1743  }
1744 
1745  /*
1746  * Only increase if the previous values have been applied, otherwise we
1747  * might never end up updating if the receiver acks too slowly. A missed
1748  * value here will just cause some extra effort after reconnecting.
1749  */
1751  {
1752  slot->candidate_restart_valid = current_lsn;
1753  slot->candidate_restart_lsn = restart_lsn;
1754  SpinLockRelease(&slot->mutex);
1755 
1756  elog(DEBUG1, "got new restart lsn %X/%X at %X/%X",
1757  LSN_FORMAT_ARGS(restart_lsn),
1758  LSN_FORMAT_ARGS(current_lsn));
1759  }
1760  else
1761  {
1762  XLogRecPtr candidate_restart_lsn;
1763  XLogRecPtr candidate_restart_valid;
1764  XLogRecPtr confirmed_flush;
1765 
1766  candidate_restart_lsn = slot->candidate_restart_lsn;
1767  candidate_restart_valid = slot->candidate_restart_valid;
1768  confirmed_flush = slot->data.confirmed_flush;
1769  SpinLockRelease(&slot->mutex);
1770 
1771  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",
1772  LSN_FORMAT_ARGS(restart_lsn),
1773  LSN_FORMAT_ARGS(current_lsn),
1774  LSN_FORMAT_ARGS(candidate_restart_lsn),
1775  LSN_FORMAT_ARGS(candidate_restart_valid),
1776  LSN_FORMAT_ARGS(confirmed_flush));
1777  }
1778 
1779  /* candidates are already valid with the current flush position, apply */
1780  if (updated_lsn)
1782 }
1783 
1784 /*
1785  * Handle a consumer's confirmation having received all changes up to lsn.
1786  */
1787 void
1789 {
1790  Assert(lsn != InvalidXLogRecPtr);
1791 
1792  /* Do an unlocked check for candidate_lsn first. */
1795  {
1796  bool updated_xmin = false;
1797  bool updated_restart = false;
1798 
1800 
1802 
1803  /* if we're past the location required for bumping xmin, do so */
1806  {
1807  /*
1808  * We have to write the changed xmin to disk *before* we change
1809  * the in-memory value, otherwise after a crash we wouldn't know
1810  * that some catalog tuples might have been removed already.
1811  *
1812  * Ensure that by first writing to ->xmin and only update
1813  * ->effective_xmin once the new state is synced to disk. After a
1814  * crash ->effective_xmin is set to ->xmin.
1815  */
1818  {
1822  updated_xmin = true;
1823  }
1824  }
1825 
1828  {
1830 
1834  updated_restart = true;
1835  }
1836 
1838 
1839  /* first write new xmin to disk, so we know what's up after a crash */
1840  if (updated_xmin || updated_restart)
1841  {
1844  elog(DEBUG1, "updated xmin: %u restart: %u", updated_xmin, updated_restart);
1845  }
1846 
1847  /*
1848  * Now the new xmin is safely on disk, we can let the global value
1849  * advance. We do not take ProcArrayLock or similar since we only
1850  * advance xmin here and there's not much harm done by a concurrent
1851  * computation missing that.
1852  */
1853  if (updated_xmin)
1854  {
1858 
1861  }
1862  }
1863  else
1864  {
1868  }
1869 }
1870 
1871 /*
1872  * Clear logical streaming state during (sub)transaction abort.
1873  */
1874 void
1876 {
1878  bsysscan = false;
1879 }
1880 
1881 /*
1882  * Report stats for a slot.
1883  */
1884 void
1886 {
1887  ReorderBuffer *rb = ctx->reorder;
1888  PgStat_StatReplSlotEntry repSlotStat;
1889 
1890  /* Nothing to do if we don't have any replication stats to be sent. */
1891  if (rb->spillBytes <= 0 && rb->streamBytes <= 0 && rb->totalBytes <= 0)
1892  return;
1893 
1894  elog(DEBUG2, "UpdateDecodingStats: updating stats %p %lld %lld %lld %lld %lld %lld %lld %lld",
1895  rb,
1896  (long long) rb->spillTxns,
1897  (long long) rb->spillCount,
1898  (long long) rb->spillBytes,
1899  (long long) rb->streamTxns,
1900  (long long) rb->streamCount,
1901  (long long) rb->streamBytes,
1902  (long long) rb->totalTxns,
1903  (long long) rb->totalBytes);
1904 
1905  repSlotStat.spill_txns = rb->spillTxns;
1906  repSlotStat.spill_count = rb->spillCount;
1907  repSlotStat.spill_bytes = rb->spillBytes;
1908  repSlotStat.stream_txns = rb->streamTxns;
1909  repSlotStat.stream_count = rb->streamCount;
1910  repSlotStat.stream_bytes = rb->streamBytes;
1911  repSlotStat.total_txns = rb->totalTxns;
1912  repSlotStat.total_bytes = rb->totalBytes;
1913 
1914  pgstat_report_replslot(ctx->slot, &repSlotStat);
1915 
1916  rb->spillTxns = 0;
1917  rb->spillCount = 0;
1918  rb->spillBytes = 0;
1919  rb->streamTxns = 0;
1920  rb->streamCount = 0;
1921  rb->streamBytes = 0;
1922  rb->totalTxns = 0;
1923  rb->totalBytes = 0;
1924 }
#define NameStr(name)
Definition: c.h:730
uint32 TransactionId
Definition: c.h:636
size_t Size
Definition: c.h:589
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 errcontext
Definition: elog.h:196
#define DEBUG2
Definition: elog.h:29
#define DEBUG1
Definition: elog.h:30
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
void err(int eval, const char *fmt,...)
Definition: err.c:43
Oid MyDatabaseId
Definition: globals.c:89
Assert(fmt[strlen(fmt) - 1] !='\n')
static void change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
Definition: logical.c:1056
static void commit_prepared_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:964
static void update_progress_txn_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr lsn)
Definition: logical.c:1599
void LogicalConfirmReceivedLocation(XLogRecPtr lsn)
Definition: logical.c:1788
void FreeDecodingContext(LogicalDecodingContext *ctx)
Definition: logical.c:647
static void stream_start_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr first_lsn)
Definition: logical.c:1237
static void commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:836
static void output_plugin_error_callback(void *arg)
Definition: logical.c:725
static void begin_prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
Definition: logical.c:875
static void stream_prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
Definition: logical.c:1376
static void prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
Definition: logical.c:919
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:675
static void stream_truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
Definition: logical.c:1552
static void truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
Definition: logical.c:1095
void DecodingContextFindStartpoint(LogicalDecodingContext *ctx)
Definition: logical.c:603
static void begin_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
Definition: logical.c:805
static void rollback_prepared_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr prepare_end_lsn, TimestampTz prepare_time)
Definition: logical.c:1009
bool DecodingContextReady(LogicalDecodingContext *ctx)
Definition: logical.c:594
void OutputPluginUpdateProgress(struct LogicalDecodingContext *ctx, bool skipped_xact)
Definition: logical.c:688
static void startup_cb_wrapper(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
Definition: logical.c:744
void UpdateDecodingStats(LogicalDecodingContext *ctx)
Definition: logical.c:1885
void LogicalIncreaseRestartDecodingForSlot(XLogRecPtr current_lsn, XLogRecPtr restart_lsn)
Definition: logical.c:1714
static void stream_change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
Definition: logical.c:1462
static void stream_abort_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr abort_lsn)
Definition: logical.c:1335
void ResetLogicalStreamingState(void)
Definition: logical.c:1875
void LogicalIncreaseXminForSlot(XLogRecPtr current_lsn, TransactionId xmin)
Definition: logical.c:1646
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:1511
static void stream_commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:1421
bool filter_prepare_cb_wrapper(LogicalDecodingContext *ctx, TransactionId xid, const char *gid)
Definition: logical.c:1137
static void shutdown_cb_wrapper(LogicalDecodingContext *ctx)
Definition: logical.c:772
void OutputPluginPrepareWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:662
void CheckLogicalDecodingRequirements(void)
Definition: logical.c:108
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:1169
static void LoadOutputPlugin(OutputPluginCallbacks *callbacks, const char *plugin)
Definition: logical.c:703
static void stream_stop_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr last_lsn)
Definition: logical.c:1286
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:1200
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:490
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:1803
@ LW_EXCLUSIVE
Definition: lwlock.h:115
void * palloc0(Size size)
Definition: mcxt.c:1241
MemoryContext CurrentMemoryContext
Definition: mcxt.c:135
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:387
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:153
#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:2992
ReorderBuffer * ReorderBufferAllocate(void)
void ReorderBufferFree(ReorderBuffer *rb)
void ReplicationSlotMarkDirty(void)
Definition: slot.c:796
void ReplicationSlotReserveWal(void)
Definition: slot.c:1158
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:835
ReplicationSlot * MyReplicationSlot
Definition: slot.c:98
void ReplicationSlotSave(void)
Definition: slot.c:778
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:892
void CheckSlotRequirements(void)
Definition: slot.c:1119
#define SlotIsPhysical(slot)
Definition: slot.h:168
void SnapBuildSetTwoPhaseAt(SnapBuild *builder, XLogRecPtr ptr)
Definition: snapbuild.c:420
SnapBuildState SnapBuildCurrentState(SnapBuild *builder)
Definition: snapbuild.c:402
void FreeSnapshotBuilder(SnapBuild *builder)
Definition: snapbuild.c:358
SnapBuild * AllocateSnapshotBuilder(ReorderBuffer *reorder, TransactionId xmin_horizon, XLogRecPtr start_lsn, bool need_full_snapshot, XLogRecPtr two_phase_at)
Definition: snapbuild.c:312
@ 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:51
LogicalDecodingContext * ctx
Definition: logical.c:49
const char * callback_name
Definition: logical.c:50
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:367
PgStat_Counter total_txns
Definition: pgstat.h:369
PgStat_Counter total_bytes
Definition: pgstat.h:370
PgStat_Counter spill_txns
Definition: pgstat.h:363
PgStat_Counter stream_txns
Definition: pgstat.h:366
PgStat_Counter spill_count
Definition: pgstat.h:364
PgStat_Counter stream_bytes
Definition: pgstat.h:368
PgStat_Counter spill_bytes
Definition: pgstat.h:365
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
XLogRecPtr two_phase_at
Definition: slot.h:90
TransactionId catalog_xmin
Definition: slot.h:70
XLogRecPtr restart_lsn
Definition: slot.h:73
XLogRecPtr confirmed_flush
Definition: slot.h:84
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:163
TransactionId effective_catalog_xmin
Definition: slot.h:144
slock_t mutex
Definition: slot.h:120
XLogRecPtr candidate_restart_valid
Definition: slot.h:164
TransactionId effective_xmin
Definition: slot.h:143
XLogRecPtr candidate_restart_lsn
Definition: slot.h:165
TransactionId candidate_catalog_xmin
Definition: slot.h:162
ReplicationSlotPersistentData data
Definition: slot.h:147
XLogRecPtr EndRecPtr
Definition: xlogreader.h:207
Definition: c.h:725
Definition: regguts.h:318
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:4841
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:5908
int wal_level
Definition: xlog.c:134
int wal_segment_size
Definition: xlog.c:146
@ WAL_LEVEL_LOGICAL
Definition: xlog.h:71
#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:422
void XLogReaderFree(XLogReaderState *state)
Definition: xlogreader.c:170
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:264