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-2020, 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);
62 static void change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
63  Relation relation, ReorderBufferChange *change);
64 static void truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
65  int nrelations, Relation relations[], ReorderBufferChange *change);
66 static void message_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
67  XLogRecPtr message_lsn, bool transactional,
68  const char *prefix, Size message_size, const char *message);
69 
70 /* streaming callbacks */
72  XLogRecPtr first_lsn);
74  XLogRecPtr last_lsn);
76  XLogRecPtr abort_lsn);
78  XLogRecPtr commit_lsn);
80  Relation relation, ReorderBufferChange *change);
82  XLogRecPtr message_lsn, bool transactional,
83  const char *prefix, Size message_size, const char *message);
85  int nrelations, Relation relations[], ReorderBufferChange *change);
86 
87 static void LoadOutputPlugin(OutputPluginCallbacks *callbacks, const char *plugin);
88 
89 /*
90  * Make sure the current settings & environment are capable of doing logical
91  * decoding.
92  */
93 void
95 {
97 
98  /*
99  * NB: Adding a new requirement likely means that RestoreSlotFromDisk()
100  * needs the same check.
101  */
102 
104  ereport(ERROR,
105  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
106  errmsg("logical decoding requires wal_level >= logical")));
107 
108  if (MyDatabaseId == InvalidOid)
109  ereport(ERROR,
110  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
111  errmsg("logical decoding requires a database connection")));
112 
113  /* ----
114  * TODO: We got to change that someday soon...
115  *
116  * There's basically three things missing to allow this:
117  * 1) We need to be able to correctly and quickly identify the timeline a
118  * LSN belongs to
119  * 2) We need to force hot_standby_feedback to be enabled at all times so
120  * the primary cannot remove rows we need.
121  * 3) support dropping replication slots referring to a database, in
122  * dbase_redo. There can't be any active ones due to HS recovery
123  * conflicts, so that should be relatively easy.
124  * ----
125  */
126  if (RecoveryInProgress())
127  ereport(ERROR,
128  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
129  errmsg("logical decoding cannot be used while in recovery")));
130 }
131 
132 /*
133  * Helper function for CreateInitDecodingContext() and
134  * CreateDecodingContext() performing common tasks.
135  */
136 static LogicalDecodingContext *
137 StartupDecodingContext(List *output_plugin_options,
138  XLogRecPtr start_lsn,
139  TransactionId xmin_horizon,
140  bool need_full_snapshot,
141  bool fast_forward,
142  XLogReaderRoutine *xl_routine,
146 {
147  ReplicationSlot *slot;
148  MemoryContext context,
149  old_context;
151 
152  /* shorter lines... */
153  slot = MyReplicationSlot;
154 
156  "Logical decoding context",
158  old_context = MemoryContextSwitchTo(context);
159  ctx = palloc0(sizeof(LogicalDecodingContext));
160 
161  ctx->context = context;
162 
163  /*
164  * (re-)load output plugins, so we detect a bad (removed) output plugin
165  * now.
166  */
167  if (!fast_forward)
169 
170  /*
171  * Now that the slot's xmin has been set, we can announce ourselves as a
172  * logical decoding backend which doesn't need to be checked individually
173  * when computing the xmin horizon because the xmin is enforced via
174  * replication slots.
175  *
176  * We can only do so if we're outside of a transaction (i.e. the case when
177  * streaming changes via walsender), otherwise an already setup
178  * snapshot/xid would end up being ignored. That's not a particularly
179  * bothersome restriction since the SQL interface can't be used for
180  * streaming anyway.
181  */
183  {
184  LWLockAcquire(ProcArrayLock, LW_SHARED);
187  LWLockRelease(ProcArrayLock);
188  }
189 
190  ctx->slot = slot;
191 
192  ctx->reader = XLogReaderAllocate(wal_segment_size, NULL, xl_routine, ctx);
193  if (!ctx->reader)
194  ereport(ERROR,
195  (errcode(ERRCODE_OUT_OF_MEMORY),
196  errmsg("out of memory")));
197 
199  ctx->snapshot_builder =
200  AllocateSnapshotBuilder(ctx->reorder, xmin_horizon, start_lsn,
201  need_full_snapshot);
202 
203  ctx->reorder->private_data = ctx;
204 
205  /* wrap output plugin callbacks, so we can add error context information */
211 
212  /*
213  * To support streaming, we require start/stop/abort/commit/change
214  * callbacks. The message and truncate callbacks are optional, similar to
215  * regular output plugins. We however enable streaming when at least one
216  * of the methods is enabled so that we can easily identify missing
217  * methods.
218  *
219  * We decide it here, but only check it later in the wrappers.
220  */
221  ctx->streaming = (ctx->callbacks.stream_start_cb != NULL) ||
222  (ctx->callbacks.stream_stop_cb != NULL) ||
223  (ctx->callbacks.stream_abort_cb != NULL) ||
224  (ctx->callbacks.stream_commit_cb != NULL) ||
225  (ctx->callbacks.stream_change_cb != NULL) ||
226  (ctx->callbacks.stream_message_cb != NULL) ||
227  (ctx->callbacks.stream_truncate_cb != NULL);
228 
229  /*
230  * streaming callbacks
231  *
232  * stream_message and stream_truncate callbacks are optional, so we do not
233  * fail with ERROR when missing, but the wrappers simply do nothing. We
234  * must set the ReorderBuffer callbacks to something, otherwise the calls
235  * from there will crash (we don't want to move the checks there).
236  */
244 
245  ctx->out = makeStringInfo();
246  ctx->prepare_write = prepare_write;
247  ctx->write = do_write;
248  ctx->update_progress = update_progress;
249 
250  ctx->output_plugin_options = output_plugin_options;
251 
252  ctx->fast_forward = fast_forward;
253 
254  MemoryContextSwitchTo(old_context);
255 
256  return ctx;
257 }
258 
259 /*
260  * Create a new decoding context, for a new logical slot.
261  *
262  * plugin -- contains the name of the output plugin
263  * output_plugin_options -- contains options passed to the output plugin
264  * need_full_snapshot -- if true, must obtain a snapshot able to read all
265  * tables; if false, one that can read only catalogs is acceptable.
266  * restart_lsn -- if given as invalid, it's this routine's responsibility to
267  * mark WAL as reserved by setting a convenient restart_lsn for the slot.
268  * Otherwise, we set for decoding to start from the given LSN without
269  * marking WAL reserved beforehand. In that scenario, it's up to the
270  * caller to guarantee that WAL remains available.
271  * xl_routine -- XLogReaderRoutine for underlying XLogReader
272  * prepare_write, do_write, update_progress --
273  * callbacks that perform the use-case dependent, actual, work.
274  *
275  * Needs to be called while in a memory context that's at least as long lived
276  * as the decoding context because further memory contexts will be created
277  * inside it.
278  *
279  * Returns an initialized decoding context after calling the output plugin's
280  * startup function.
281  */
284  List *output_plugin_options,
285  bool need_full_snapshot,
286  XLogRecPtr restart_lsn,
287  XLogReaderRoutine *xl_routine,
291 {
292  TransactionId xmin_horizon = InvalidTransactionId;
293  ReplicationSlot *slot;
294  NameData plugin_name;
296  MemoryContext old_context;
297 
298  /* shorter lines... */
299  slot = MyReplicationSlot;
300 
301  /* first some sanity checks that are unlikely to be violated */
302  if (slot == NULL)
303  elog(ERROR, "cannot perform logical decoding without an acquired slot");
304 
305  if (plugin == NULL)
306  elog(ERROR, "cannot initialize logical decoding without a specified plugin");
307 
308  /* Make sure the passed slot is suitable. These are user facing errors. */
309  if (SlotIsPhysical(slot))
310  ereport(ERROR,
311  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
312  errmsg("cannot use physical replication slot for logical decoding")));
313 
314  if (slot->data.database != MyDatabaseId)
315  ereport(ERROR,
316  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
317  errmsg("replication slot \"%s\" was not created in this database",
318  NameStr(slot->data.name))));
319 
320  if (IsTransactionState() &&
322  ereport(ERROR,
323  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
324  errmsg("cannot create logical replication slot in transaction that has performed writes")));
325 
326  /*
327  * Register output plugin name with slot. We need the mutex to avoid
328  * concurrent reading of a partially copied string. But we don't want any
329  * complicated code while holding a spinlock, so do namestrcpy() outside.
330  */
331  namestrcpy(&plugin_name, plugin);
332  SpinLockAcquire(&slot->mutex);
333  slot->data.plugin = plugin_name;
334  SpinLockRelease(&slot->mutex);
335 
336  if (XLogRecPtrIsInvalid(restart_lsn))
338  else
339  {
340  SpinLockAcquire(&slot->mutex);
341  slot->data.restart_lsn = restart_lsn;
342  SpinLockRelease(&slot->mutex);
343  }
344 
345  /* ----
346  * This is a bit tricky: We need to determine a safe xmin horizon to start
347  * decoding from, to avoid starting from a running xacts record referring
348  * to xids whose rows have been vacuumed or pruned
349  * already. GetOldestSafeDecodingTransactionId() returns such a value, but
350  * without further interlock its return value might immediately be out of
351  * date.
352  *
353  * So we have to acquire the ProcArrayLock to prevent computation of new
354  * xmin horizons by other backends, get the safe decoding xid, and inform
355  * the slot machinery about the new limit. Once that's done the
356  * ProcArrayLock can be released as the slot machinery now is
357  * protecting against vacuum.
358  *
359  * Note that, temporarily, the data, not just the catalog, xmin has to be
360  * reserved if a data snapshot is to be exported. Otherwise the initial
361  * data snapshot created here is not guaranteed to be valid. After that
362  * the data xmin doesn't need to be managed anymore and the global xmin
363  * should be recomputed. As we are fine with losing the pegged data xmin
364  * after crash - no chance a snapshot would get exported anymore - we can
365  * get away with just setting the slot's
366  * effective_xmin. ReplicationSlotRelease will reset it again.
367  *
368  * ----
369  */
370  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
371 
372  xmin_horizon = GetOldestSafeDecodingTransactionId(!need_full_snapshot);
373 
374  SpinLockAcquire(&slot->mutex);
375  slot->effective_catalog_xmin = xmin_horizon;
376  slot->data.catalog_xmin = xmin_horizon;
377  if (need_full_snapshot)
378  slot->effective_xmin = xmin_horizon;
379  SpinLockRelease(&slot->mutex);
380 
382 
383  LWLockRelease(ProcArrayLock);
384 
387 
388  ctx = StartupDecodingContext(NIL, restart_lsn, xmin_horizon,
389  need_full_snapshot, false,
390  xl_routine, prepare_write, do_write,
391  update_progress);
392 
393  /* call output plugin initialization callback */
394  old_context = MemoryContextSwitchTo(ctx->context);
395  if (ctx->callbacks.startup_cb != NULL)
396  startup_cb_wrapper(ctx, &ctx->options, true);
397  MemoryContextSwitchTo(old_context);
398 
400 
401  return ctx;
402 }
403 
404 /*
405  * Create a new decoding context, for a logical slot that has previously been
406  * used already.
407  *
408  * start_lsn
409  * The LSN at which to start decoding. If InvalidXLogRecPtr, restart
410  * from the slot's confirmed_flush; otherwise, start from the specified
411  * location (but move it forwards to confirmed_flush if it's older than
412  * that, see below).
413  *
414  * output_plugin_options
415  * options passed to the output plugin.
416  *
417  * fast_forward
418  * bypass the generation of logical changes.
419  *
420  * xl_routine
421  * XLogReaderRoutine used by underlying xlogreader
422  *
423  * prepare_write, do_write, update_progress
424  * callbacks that have to be filled to perform the use-case dependent,
425  * actual work.
426  *
427  * Needs to be called while in a memory context that's at least as long lived
428  * as the decoding context because further memory contexts will be created
429  * inside it.
430  *
431  * Returns an initialized decoding context after calling the output plugin's
432  * startup function.
433  */
436  List *output_plugin_options,
437  bool fast_forward,
438  XLogReaderRoutine *xl_routine,
442 {
444  ReplicationSlot *slot;
445  MemoryContext old_context;
446 
447  /* shorter lines... */
448  slot = MyReplicationSlot;
449 
450  /* first some sanity checks that are unlikely to be violated */
451  if (slot == NULL)
452  elog(ERROR, "cannot perform logical decoding without an acquired slot");
453 
454  /* make sure the passed slot is suitable, these are user facing errors */
455  if (SlotIsPhysical(slot))
456  ereport(ERROR,
457  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
458  errmsg("cannot use physical replication slot for logical decoding")));
459 
460  if (slot->data.database != MyDatabaseId)
461  ereport(ERROR,
462  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
463  errmsg("replication slot \"%s\" was not created in this database",
464  NameStr(slot->data.name))));
465 
466  if (start_lsn == InvalidXLogRecPtr)
467  {
468  /* continue from last position */
469  start_lsn = slot->data.confirmed_flush;
470  }
471  else if (start_lsn < slot->data.confirmed_flush)
472  {
473  /*
474  * It might seem like we should error out in this case, but it's
475  * pretty common for a client to acknowledge a LSN it doesn't have to
476  * do anything for, and thus didn't store persistently, because the
477  * xlog records didn't result in anything relevant for logical
478  * decoding. Clients have to be able to do that to support synchronous
479  * replication.
480  */
481  elog(DEBUG1, "cannot stream from %X/%X, minimum is %X/%X, forwarding",
482  (uint32) (start_lsn >> 32), (uint32) start_lsn,
483  (uint32) (slot->data.confirmed_flush >> 32),
484  (uint32) slot->data.confirmed_flush);
485 
486  start_lsn = slot->data.confirmed_flush;
487  }
488 
489  ctx = StartupDecodingContext(output_plugin_options,
490  start_lsn, InvalidTransactionId, false,
491  fast_forward, xl_routine, prepare_write,
492  do_write, update_progress);
493 
494  /* call output plugin initialization callback */
495  old_context = MemoryContextSwitchTo(ctx->context);
496  if (ctx->callbacks.startup_cb != NULL)
497  startup_cb_wrapper(ctx, &ctx->options, false);
498  MemoryContextSwitchTo(old_context);
499 
501 
502  ereport(LOG,
503  (errmsg("starting logical decoding for slot \"%s\"",
504  NameStr(slot->data.name)),
505  errdetail("Streaming transactions committing after %X/%X, reading WAL from %X/%X.",
506  (uint32) (slot->data.confirmed_flush >> 32),
507  (uint32) slot->data.confirmed_flush,
508  (uint32) (slot->data.restart_lsn >> 32),
509  (uint32) slot->data.restart_lsn)));
510 
511  return ctx;
512 }
513 
514 /*
515  * Returns true if a consistent initial decoding snapshot has been built.
516  */
517 bool
519 {
521 }
522 
523 /*
524  * Read from the decoding slot, until it is ready to start extracting changes.
525  */
526 void
528 {
529  ReplicationSlot *slot = ctx->slot;
530 
531  /* Initialize from where to start reading WAL. */
532  XLogBeginRead(ctx->reader, slot->data.restart_lsn);
533 
534  elog(DEBUG1, "searching for logical decoding starting point, starting at %X/%X",
535  (uint32) (slot->data.restart_lsn >> 32),
536  (uint32) slot->data.restart_lsn);
537 
538  /* Wait for a consistent starting point */
539  for (;;)
540  {
541  XLogRecord *record;
542  char *err = NULL;
543 
544  /* the read_page callback waits for new WAL */
545  record = XLogReadRecord(ctx->reader, &err);
546  if (err)
547  elog(ERROR, "%s", err);
548  if (!record)
549  elog(ERROR, "no record found"); /* shouldn't happen */
550 
552 
553  /* only continue till we found a consistent spot */
554  if (DecodingContextReady(ctx))
555  break;
556 
558  }
559 
560  SpinLockAcquire(&slot->mutex);
561  slot->data.confirmed_flush = ctx->reader->EndRecPtr;
562  SpinLockRelease(&slot->mutex);
563 }
564 
565 /*
566  * Free a previously allocated decoding context, invoking the shutdown
567  * callback if necessary.
568  */
569 void
571 {
572  if (ctx->callbacks.shutdown_cb != NULL)
573  shutdown_cb_wrapper(ctx);
574 
577  XLogReaderFree(ctx->reader);
579 }
580 
581 /*
582  * Prepare a write using the context's output routine.
583  */
584 void
586 {
587  if (!ctx->accept_writes)
588  elog(ERROR, "writes are only accepted in commit, begin and change callbacks");
589 
590  ctx->prepare_write(ctx, ctx->write_location, ctx->write_xid, last_write);
591  ctx->prepared_write = true;
592 }
593 
594 /*
595  * Perform a write using the context's output routine.
596  */
597 void
599 {
600  if (!ctx->prepared_write)
601  elog(ERROR, "OutputPluginPrepareWrite needs to be called before OutputPluginWrite");
602 
603  ctx->write(ctx, ctx->write_location, ctx->write_xid, last_write);
604  ctx->prepared_write = false;
605 }
606 
607 /*
608  * Update progress tracking (if supported).
609  */
610 void
612 {
613  if (!ctx->update_progress)
614  return;
615 
616  ctx->update_progress(ctx, ctx->write_location, ctx->write_xid);
617 }
618 
619 /*
620  * Load the output plugin, lookup its output plugin init function, and check
621  * that it provides the required callbacks.
622  */
623 static void
625 {
626  LogicalOutputPluginInit plugin_init;
627 
628  plugin_init = (LogicalOutputPluginInit)
629  load_external_function(plugin, "_PG_output_plugin_init", false, NULL);
630 
631  if (plugin_init == NULL)
632  elog(ERROR, "output plugins have to declare the _PG_output_plugin_init symbol");
633 
634  /* ask the output plugin to fill the callback struct */
635  plugin_init(callbacks);
636 
637  if (callbacks->begin_cb == NULL)
638  elog(ERROR, "output plugins have to register a begin callback");
639  if (callbacks->change_cb == NULL)
640  elog(ERROR, "output plugins have to register a change callback");
641  if (callbacks->commit_cb == NULL)
642  elog(ERROR, "output plugins have to register a commit callback");
643 }
644 
645 static void
647 {
649 
650  /* not all callbacks have an associated LSN */
651  if (state->report_location != InvalidXLogRecPtr)
652  errcontext("slot \"%s\", output plugin \"%s\", in the %s callback, associated LSN %X/%X",
653  NameStr(state->ctx->slot->data.name),
654  NameStr(state->ctx->slot->data.plugin),
655  state->callback_name,
656  (uint32) (state->report_location >> 32),
657  (uint32) state->report_location);
658  else
659  errcontext("slot \"%s\", output plugin \"%s\", in the %s callback",
660  NameStr(state->ctx->slot->data.name),
661  NameStr(state->ctx->slot->data.plugin),
662  state->callback_name);
663 }
664 
665 static void
667 {
669  ErrorContextCallback errcallback;
670 
671  Assert(!ctx->fast_forward);
672 
673  /* Push callback + info on the error context stack */
674  state.ctx = ctx;
675  state.callback_name = "startup";
678  errcallback.arg = (void *) &state;
679  errcallback.previous = error_context_stack;
680  error_context_stack = &errcallback;
681 
682  /* set output state */
683  ctx->accept_writes = false;
684 
685  /* do the actual work: call callback */
686  ctx->callbacks.startup_cb(ctx, opt, is_init);
687 
688  /* Pop the error context stack */
689  error_context_stack = errcallback.previous;
690 }
691 
692 static void
694 {
696  ErrorContextCallback errcallback;
697 
698  Assert(!ctx->fast_forward);
699 
700  /* Push callback + info on the error context stack */
701  state.ctx = ctx;
702  state.callback_name = "shutdown";
705  errcallback.arg = (void *) &state;
706  errcallback.previous = error_context_stack;
707  error_context_stack = &errcallback;
708 
709  /* set output state */
710  ctx->accept_writes = false;
711 
712  /* do the actual work: call callback */
713  ctx->callbacks.shutdown_cb(ctx);
714 
715  /* Pop the error context stack */
716  error_context_stack = errcallback.previous;
717 }
718 
719 
720 /*
721  * Callbacks for ReorderBuffer which add in some more information and then call
722  * output_plugin.h plugins.
723  */
724 static void
726 {
729  ErrorContextCallback errcallback;
730 
731  Assert(!ctx->fast_forward);
732 
733  /* Push callback + info on the error context stack */
734  state.ctx = ctx;
735  state.callback_name = "begin";
736  state.report_location = txn->first_lsn;
738  errcallback.arg = (void *) &state;
739  errcallback.previous = error_context_stack;
740  error_context_stack = &errcallback;
741 
742  /* set output state */
743  ctx->accept_writes = true;
744  ctx->write_xid = txn->xid;
745  ctx->write_location = txn->first_lsn;
746 
747  /* do the actual work: call callback */
748  ctx->callbacks.begin_cb(ctx, txn);
749 
750  /* Pop the error context stack */
751  error_context_stack = errcallback.previous;
752 }
753 
754 static void
756  XLogRecPtr commit_lsn)
757 {
760  ErrorContextCallback errcallback;
761 
762  Assert(!ctx->fast_forward);
763 
764  /* Push callback + info on the error context stack */
765  state.ctx = ctx;
766  state.callback_name = "commit";
767  state.report_location = txn->final_lsn; /* beginning of commit record */
769  errcallback.arg = (void *) &state;
770  errcallback.previous = error_context_stack;
771  error_context_stack = &errcallback;
772 
773  /* set output state */
774  ctx->accept_writes = true;
775  ctx->write_xid = txn->xid;
776  ctx->write_location = txn->end_lsn; /* points to the end of the record */
777 
778  /* do the actual work: call callback */
779  ctx->callbacks.commit_cb(ctx, txn, commit_lsn);
780 
781  /* Pop the error context stack */
782  error_context_stack = errcallback.previous;
783 }
784 
785 static void
787  Relation relation, ReorderBufferChange *change)
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 = "change";
798  state.report_location = change->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 
808  /*
809  * report this change's lsn so replies from clients can give an up2date
810  * answer. This won't ever be enough (and shouldn't be!) to confirm
811  * receipt of this transaction, but it might allow another transaction's
812  * commit to be confirmed with one message.
813  */
814  ctx->write_location = change->lsn;
815 
816  ctx->callbacks.change_cb(ctx, txn, relation, change);
817 
818  /* Pop the error context stack */
819  error_context_stack = errcallback.previous;
820 }
821 
822 static void
824  int nrelations, Relation relations[], ReorderBufferChange *change)
825 {
828  ErrorContextCallback errcallback;
829 
830  Assert(!ctx->fast_forward);
831 
832  if (!ctx->callbacks.truncate_cb)
833  return;
834 
835  /* Push callback + info on the error context stack */
836  state.ctx = ctx;
837  state.callback_name = "truncate";
838  state.report_location = change->lsn;
840  errcallback.arg = (void *) &state;
841  errcallback.previous = error_context_stack;
842  error_context_stack = &errcallback;
843 
844  /* set output state */
845  ctx->accept_writes = true;
846  ctx->write_xid = txn->xid;
847 
848  /*
849  * report this change's lsn so replies from clients can give an up2date
850  * answer. This won't ever be enough (and shouldn't be!) to confirm
851  * receipt of this transaction, but it might allow another transaction's
852  * commit to be confirmed with one message.
853  */
854  ctx->write_location = change->lsn;
855 
856  ctx->callbacks.truncate_cb(ctx, txn, nrelations, relations, change);
857 
858  /* Pop the error context stack */
859  error_context_stack = errcallback.previous;
860 }
861 
862 bool
864 {
866  ErrorContextCallback errcallback;
867  bool ret;
868 
869  Assert(!ctx->fast_forward);
870 
871  /* Push callback + info on the error context stack */
872  state.ctx = ctx;
873  state.callback_name = "filter_by_origin";
876  errcallback.arg = (void *) &state;
877  errcallback.previous = error_context_stack;
878  error_context_stack = &errcallback;
879 
880  /* set output state */
881  ctx->accept_writes = false;
882 
883  /* do the actual work: call callback */
884  ret = ctx->callbacks.filter_by_origin_cb(ctx, origin_id);
885 
886  /* Pop the error context stack */
887  error_context_stack = errcallback.previous;
888 
889  return ret;
890 }
891 
892 static void
894  XLogRecPtr message_lsn, bool transactional,
895  const char *prefix, Size message_size, const char *message)
896 {
899  ErrorContextCallback errcallback;
900 
901  Assert(!ctx->fast_forward);
902 
903  if (ctx->callbacks.message_cb == NULL)
904  return;
905 
906  /* Push callback + info on the error context stack */
907  state.ctx = ctx;
908  state.callback_name = "message";
909  state.report_location = message_lsn;
911  errcallback.arg = (void *) &state;
912  errcallback.previous = error_context_stack;
913  error_context_stack = &errcallback;
914 
915  /* set output state */
916  ctx->accept_writes = true;
917  ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
918  ctx->write_location = message_lsn;
919 
920  /* do the actual work: call callback */
921  ctx->callbacks.message_cb(ctx, txn, message_lsn, transactional, prefix,
922  message_size, message);
923 
924  /* Pop the error context stack */
925  error_context_stack = errcallback.previous;
926 }
927 
928 static void
930  XLogRecPtr first_lsn)
931 {
934  ErrorContextCallback errcallback;
935 
936  Assert(!ctx->fast_forward);
937 
938  /* We're only supposed to call this when streaming is supported. */
939  Assert(ctx->streaming);
940 
941  /* Push callback + info on the error context stack */
942  state.ctx = ctx;
943  state.callback_name = "stream_start";
944  state.report_location = first_lsn;
946  errcallback.arg = (void *) &state;
947  errcallback.previous = error_context_stack;
948  error_context_stack = &errcallback;
949 
950  /* set output state */
951  ctx->accept_writes = true;
952  ctx->write_xid = txn->xid;
953 
954  /*
955  * report this message's lsn so replies from clients can give an up2date
956  * answer. This won't ever be enough (and shouldn't be!) to confirm
957  * receipt of this transaction, but it might allow another transaction's
958  * commit to be confirmed with one message.
959  */
960  ctx->write_location = first_lsn;
961 
962  /* in streaming mode, stream_start_cb is required */
963  if (ctx->callbacks.stream_start_cb == NULL)
964  ereport(ERROR,
965  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
966  errmsg("logical streaming requires a stream_start_cb callback")));
967 
968  ctx->callbacks.stream_start_cb(ctx, txn);
969 
970  /* Pop the error context stack */
971  error_context_stack = errcallback.previous;
972 }
973 
974 static void
976  XLogRecPtr last_lsn)
977 {
980  ErrorContextCallback errcallback;
981 
982  Assert(!ctx->fast_forward);
983 
984  /* We're only supposed to call this when streaming is supported. */
985  Assert(ctx->streaming);
986 
987  /* Push callback + info on the error context stack */
988  state.ctx = ctx;
989  state.callback_name = "stream_stop";
990  state.report_location = last_lsn;
992  errcallback.arg = (void *) &state;
993  errcallback.previous = error_context_stack;
994  error_context_stack = &errcallback;
995 
996  /* set output state */
997  ctx->accept_writes = true;
998  ctx->write_xid = txn->xid;
999 
1000  /*
1001  * report this message's lsn so replies from clients can give an up2date
1002  * answer. This won't ever be enough (and shouldn't be!) to confirm
1003  * receipt of this transaction, but it might allow another transaction's
1004  * commit to be confirmed with one message.
1005  */
1006  ctx->write_location = last_lsn;
1007 
1008  /* in streaming mode, stream_stop_cb is required */
1009  if (ctx->callbacks.stream_stop_cb == NULL)
1010  ereport(ERROR,
1011  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1012  errmsg("logical streaming requires a stream_stop_cb callback")));
1013 
1014  ctx->callbacks.stream_stop_cb(ctx, txn);
1015 
1016  /* Pop the error context stack */
1017  error_context_stack = errcallback.previous;
1018 }
1019 
1020 static void
1022  XLogRecPtr abort_lsn)
1023 {
1026  ErrorContextCallback errcallback;
1027 
1028  Assert(!ctx->fast_forward);
1029 
1030  /* We're only supposed to call this when streaming is supported. */
1031  Assert(ctx->streaming);
1032 
1033  /* Push callback + info on the error context stack */
1034  state.ctx = ctx;
1035  state.callback_name = "stream_abort";
1036  state.report_location = abort_lsn;
1037  errcallback.callback = output_plugin_error_callback;
1038  errcallback.arg = (void *) &state;
1039  errcallback.previous = error_context_stack;
1040  error_context_stack = &errcallback;
1041 
1042  /* set output state */
1043  ctx->accept_writes = true;
1044  ctx->write_xid = txn->xid;
1045  ctx->write_location = abort_lsn;
1046 
1047  /* in streaming mode, stream_abort_cb is required */
1048  if (ctx->callbacks.stream_abort_cb == NULL)
1049  ereport(ERROR,
1050  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1051  errmsg("logical streaming requires a stream_abort_cb callback")));
1052 
1053  ctx->callbacks.stream_abort_cb(ctx, txn, abort_lsn);
1054 
1055  /* Pop the error context stack */
1056  error_context_stack = errcallback.previous;
1057 }
1058 
1059 static void
1061  XLogRecPtr commit_lsn)
1062 {
1065  ErrorContextCallback errcallback;
1066 
1067  Assert(!ctx->fast_forward);
1068 
1069  /* We're only supposed to call this when streaming is supported. */
1070  Assert(ctx->streaming);
1071 
1072  /* Push callback + info on the error context stack */
1073  state.ctx = ctx;
1074  state.callback_name = "stream_commit";
1075  state.report_location = txn->final_lsn;
1076  errcallback.callback = output_plugin_error_callback;
1077  errcallback.arg = (void *) &state;
1078  errcallback.previous = error_context_stack;
1079  error_context_stack = &errcallback;
1080 
1081  /* set output state */
1082  ctx->accept_writes = true;
1083  ctx->write_xid = txn->xid;
1084  ctx->write_location = txn->end_lsn;
1085 
1086  /* in streaming mode, stream_abort_cb is required */
1087  if (ctx->callbacks.stream_commit_cb == NULL)
1088  ereport(ERROR,
1089  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1090  errmsg("logical streaming requires a stream_commit_cb callback")));
1091 
1092  ctx->callbacks.stream_commit_cb(ctx, txn, commit_lsn);
1093 
1094  /* Pop the error context stack */
1095  error_context_stack = errcallback.previous;
1096 }
1097 
1098 static void
1100  Relation relation, ReorderBufferChange *change)
1101 {
1104  ErrorContextCallback errcallback;
1105 
1106  Assert(!ctx->fast_forward);
1107 
1108  /* We're only supposed to call this when streaming is supported. */
1109  Assert(ctx->streaming);
1110 
1111  /* Push callback + info on the error context stack */
1112  state.ctx = ctx;
1113  state.callback_name = "stream_change";
1114  state.report_location = change->lsn;
1115  errcallback.callback = output_plugin_error_callback;
1116  errcallback.arg = (void *) &state;
1117  errcallback.previous = error_context_stack;
1118  error_context_stack = &errcallback;
1119 
1120  /* set output state */
1121  ctx->accept_writes = true;
1122  ctx->write_xid = txn->xid;
1123 
1124  /*
1125  * report this change's lsn so replies from clients can give an up2date
1126  * answer. This won't ever be enough (and shouldn't be!) to confirm
1127  * receipt of this transaction, but it might allow another transaction's
1128  * commit to be confirmed with one message.
1129  */
1130  ctx->write_location = change->lsn;
1131 
1132  /* in streaming mode, stream_change_cb is required */
1133  if (ctx->callbacks.stream_change_cb == NULL)
1134  ereport(ERROR,
1135  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1136  errmsg("logical streaming requires a stream_change_cb callback")));
1137 
1138  ctx->callbacks.stream_change_cb(ctx, txn, relation, change);
1139 
1140  /* Pop the error context stack */
1141  error_context_stack = errcallback.previous;
1142 }
1143 
1144 static void
1146  XLogRecPtr message_lsn, bool transactional,
1147  const char *prefix, Size message_size, const char *message)
1148 {
1151  ErrorContextCallback errcallback;
1152 
1153  Assert(!ctx->fast_forward);
1154 
1155  /* We're only supposed to call this when streaming is supported. */
1156  Assert(ctx->streaming);
1157 
1158  /* this callback is optional */
1159  if (ctx->callbacks.stream_message_cb == NULL)
1160  return;
1161 
1162  /* Push callback + info on the error context stack */
1163  state.ctx = ctx;
1164  state.callback_name = "stream_message";
1165  state.report_location = message_lsn;
1166  errcallback.callback = output_plugin_error_callback;
1167  errcallback.arg = (void *) &state;
1168  errcallback.previous = error_context_stack;
1169  error_context_stack = &errcallback;
1170 
1171  /* set output state */
1172  ctx->accept_writes = true;
1173  ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
1174  ctx->write_location = message_lsn;
1175 
1176  /* do the actual work: call callback */
1177  ctx->callbacks.stream_message_cb(ctx, txn, message_lsn, transactional, prefix,
1178  message_size, message);
1179 
1180  /* Pop the error context stack */
1181  error_context_stack = errcallback.previous;
1182 }
1183 
1184 static void
1186  int nrelations, Relation relations[],
1187  ReorderBufferChange *change)
1188 {
1191  ErrorContextCallback errcallback;
1192 
1193  Assert(!ctx->fast_forward);
1194 
1195  /* We're only supposed to call this when streaming is supported. */
1196  Assert(ctx->streaming);
1197 
1198  /* this callback is optional */
1199  if (!ctx->callbacks.stream_truncate_cb)
1200  return;
1201 
1202  /* Push callback + info on the error context stack */
1203  state.ctx = ctx;
1204  state.callback_name = "stream_truncate";
1205  state.report_location = change->lsn;
1206  errcallback.callback = output_plugin_error_callback;
1207  errcallback.arg = (void *) &state;
1208  errcallback.previous = error_context_stack;
1209  error_context_stack = &errcallback;
1210 
1211  /* set output state */
1212  ctx->accept_writes = true;
1213  ctx->write_xid = txn->xid;
1214 
1215  /*
1216  * report this change's lsn so replies from clients can give an up2date
1217  * answer. This won't ever be enough (and shouldn't be!) to confirm
1218  * receipt of this transaction, but it might allow another transaction's
1219  * commit to be confirmed with one message.
1220  */
1221  ctx->write_location = change->lsn;
1222 
1223  ctx->callbacks.stream_truncate_cb(ctx, txn, nrelations, relations, change);
1224 
1225  /* Pop the error context stack */
1226  error_context_stack = errcallback.previous;
1227 }
1228 
1229 /*
1230  * Set the required catalog xmin horizon for historic snapshots in the current
1231  * replication slot.
1232  *
1233  * Note that in the most cases, we won't be able to immediately use the xmin
1234  * to increase the xmin horizon: we need to wait till the client has confirmed
1235  * receiving current_lsn with LogicalConfirmReceivedLocation().
1236  */
1237 void
1239 {
1240  bool updated_xmin = false;
1241  ReplicationSlot *slot;
1242 
1243  slot = MyReplicationSlot;
1244 
1245  Assert(slot != NULL);
1246 
1247  SpinLockAcquire(&slot->mutex);
1248 
1249  /*
1250  * don't overwrite if we already have a newer xmin. This can happen if we
1251  * restart decoding in a slot.
1252  */
1254  {
1255  }
1256 
1257  /*
1258  * If the client has already confirmed up to this lsn, we directly can
1259  * mark this as accepted. This can happen if we restart decoding in a
1260  * slot.
1261  */
1262  else if (current_lsn <= slot->data.confirmed_flush)
1263  {
1264  slot->candidate_catalog_xmin = xmin;
1265  slot->candidate_xmin_lsn = current_lsn;
1266 
1267  /* our candidate can directly be used */
1268  updated_xmin = true;
1269  }
1270 
1271  /*
1272  * Only increase if the previous values have been applied, otherwise we
1273  * might never end up updating if the receiver acks too slowly.
1274  */
1275  else if (slot->candidate_xmin_lsn == InvalidXLogRecPtr)
1276  {
1277  slot->candidate_catalog_xmin = xmin;
1278  slot->candidate_xmin_lsn = current_lsn;
1279  }
1280  SpinLockRelease(&slot->mutex);
1281 
1282  /* candidate already valid with the current flush position, apply */
1283  if (updated_xmin)
1285 }
1286 
1287 /*
1288  * Mark the minimal LSN (restart_lsn) we need to read to replay all
1289  * transactions that have not yet committed at current_lsn.
1290  *
1291  * Just like LogicalIncreaseXminForSlot this only takes effect when the
1292  * client has confirmed to have received current_lsn.
1293  */
1294 void
1296 {
1297  bool updated_lsn = false;
1298  ReplicationSlot *slot;
1299 
1300  slot = MyReplicationSlot;
1301 
1302  Assert(slot != NULL);
1303  Assert(restart_lsn != InvalidXLogRecPtr);
1304  Assert(current_lsn != InvalidXLogRecPtr);
1305 
1306  SpinLockAcquire(&slot->mutex);
1307 
1308  /* don't overwrite if have a newer restart lsn */
1309  if (restart_lsn <= slot->data.restart_lsn)
1310  {
1311  }
1312 
1313  /*
1314  * We might have already flushed far enough to directly accept this lsn,
1315  * in this case there is no need to check for existing candidate LSNs
1316  */
1317  else if (current_lsn <= slot->data.confirmed_flush)
1318  {
1319  slot->candidate_restart_valid = current_lsn;
1320  slot->candidate_restart_lsn = restart_lsn;
1321 
1322  /* our candidate can directly be used */
1323  updated_lsn = true;
1324  }
1325 
1326  /*
1327  * Only increase if the previous values have been applied, otherwise we
1328  * might never end up updating if the receiver acks too slowly. A missed
1329  * value here will just cause some extra effort after reconnecting.
1330  */
1332  {
1333  slot->candidate_restart_valid = current_lsn;
1334  slot->candidate_restart_lsn = restart_lsn;
1335  SpinLockRelease(&slot->mutex);
1336 
1337  elog(DEBUG1, "got new restart lsn %X/%X at %X/%X",
1338  (uint32) (restart_lsn >> 32), (uint32) restart_lsn,
1339  (uint32) (current_lsn >> 32), (uint32) current_lsn);
1340  }
1341  else
1342  {
1343  XLogRecPtr candidate_restart_lsn;
1344  XLogRecPtr candidate_restart_valid;
1345  XLogRecPtr confirmed_flush;
1346 
1347  candidate_restart_lsn = slot->candidate_restart_lsn;
1348  candidate_restart_valid = slot->candidate_restart_valid;
1349  confirmed_flush = slot->data.confirmed_flush;
1350  SpinLockRelease(&slot->mutex);
1351 
1352  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",
1353  (uint32) (restart_lsn >> 32), (uint32) restart_lsn,
1354  (uint32) (current_lsn >> 32), (uint32) current_lsn,
1355  (uint32) (candidate_restart_lsn >> 32),
1356  (uint32) candidate_restart_lsn,
1357  (uint32) (candidate_restart_valid >> 32),
1358  (uint32) candidate_restart_valid,
1359  (uint32) (confirmed_flush >> 32),
1360  (uint32) confirmed_flush);
1361  }
1362 
1363  /* candidates are already valid with the current flush position, apply */
1364  if (updated_lsn)
1366 }
1367 
1368 /*
1369  * Handle a consumer's confirmation having received all changes up to lsn.
1370  */
1371 void
1373 {
1374  Assert(lsn != InvalidXLogRecPtr);
1375 
1376  /* Do an unlocked check for candidate_lsn first. */
1379  {
1380  bool updated_xmin = false;
1381  bool updated_restart = false;
1382 
1384 
1386 
1387  /* if we're past the location required for bumping xmin, do so */
1390  {
1391  /*
1392  * We have to write the changed xmin to disk *before* we change
1393  * the in-memory value, otherwise after a crash we wouldn't know
1394  * that some catalog tuples might have been removed already.
1395  *
1396  * Ensure that by first writing to ->xmin and only update
1397  * ->effective_xmin once the new state is synced to disk. After a
1398  * crash ->effective_xmin is set to ->xmin.
1399  */
1402  {
1406  updated_xmin = true;
1407  }
1408  }
1409 
1412  {
1414 
1418  updated_restart = true;
1419  }
1420 
1422 
1423  /* first write new xmin to disk, so we know what's up after a crash */
1424  if (updated_xmin || updated_restart)
1425  {
1428  elog(DEBUG1, "updated xmin: %u restart: %u", updated_xmin, updated_restart);
1429  }
1430 
1431  /*
1432  * Now the new xmin is safely on disk, we can let the global value
1433  * advance. We do not take ProcArrayLock or similar since we only
1434  * advance xmin here and there's not much harm done by a concurrent
1435  * computation missing that.
1436  */
1437  if (updated_xmin)
1438  {
1442 
1445  }
1446  }
1447  else
1448  {
1452  }
1453 }
1454 
1455 /*
1456  * Clear logical streaming state during (sub)transaction abort.
1457  */
1458 void
1460 {
1462  bsysscan = false;
1463 }
1464 
1465 /*
1466  * Report stats for a slot.
1467  */
1468 void
1470 {
1471  ReorderBuffer *rb = ctx->reorder;
1472 
1473  /*
1474  * Nothing to do if we haven't spilled or streamed anything since the last
1475  * time the stats has been sent.
1476  */
1477  if (rb->spillBytes <= 0 && rb->streamBytes <= 0)
1478  return;
1479 
1480  elog(DEBUG2, "UpdateDecodingStats: updating stats %p %lld %lld %lld %lld %lld %lld",
1481  rb,
1482  (long long) rb->spillTxns,
1483  (long long) rb->spillCount,
1484  (long long) rb->spillBytes,
1485  (long long) rb->streamTxns,
1486  (long long) rb->streamCount,
1487  (long long) rb->streamBytes);
1488 
1490  rb->spillTxns, rb->spillCount, rb->spillBytes,
1491  rb->streamTxns, rb->streamCount, rb->streamBytes);
1492  rb->spillTxns = 0;
1493  rb->spillCount = 0;
1494  rb->spillBytes = 0;
1495  rb->streamTxns = 0;
1496  rb->streamCount = 0;
1497  rb->streamBytes = 0;
1498 }
XLogRecPtr first_lsn
LogicalDecodeTruncateCB truncate_cb
#define NIL
Definition: pg_list.h:65
TransactionId write_xid
Definition: logical.h:93
static void stream_stop_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr last_lsn)
Definition: logical.c:975
static const char * plugin
void CheckSlotRequirements(void)
Definition: slot.c:1053
TransactionId candidate_catalog_xmin
Definition: slot.h:158
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
static void stream_abort_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr abort_lsn)
Definition: logical.c:1021
TransactionId GetOldestSafeDecodingTransactionId(bool catalogOnly)
Definition: procarray.c:2852
ReorderBufferApplyChangeCB apply_change
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:212
#define AllocSetContextCreate
Definition: memutils.h:170
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:786
struct ReorderBuffer * reorder
Definition: logical.h:42
#define PROC_IN_LOGICAL_DECODING
Definition: proc.h:57
int wal_segment_size
Definition: xlog.c:117
uint32 TransactionId
Definition: c.h:575
SnapBuildState SnapBuildCurrentState(SnapBuild *builder)
Definition: snapbuild.c:395
void namestrcpy(Name name, const char *str)
Definition: name.c:233
bool DecodingContextReady(LogicalDecodingContext *ctx)
Definition: logical.c:518
PGPROC * MyProc
Definition: proc.c:67
struct LogicalErrorCallbackState LogicalErrorCallbackState
static void commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:755
OutputPluginOptions options
Definition: logical.h:53
void LogicalIncreaseRestartDecodingForSlot(XLogRecPtr current_lsn, XLogRecPtr restart_lsn)
Definition: logical.c:1295
ReorderBufferStreamAbortCB stream_abort
int wal_level
Definition: xlog.c:107
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:58
int errcode(int sqlerrcode)
Definition: elog.c:691
bool IsTransactionOrTransactionBlock(void)
Definition: xact.c:4703
PROC_HDR * ProcGlobal
Definition: proc.c:79
MemoryContext context
Definition: logical.h:35
void ReplicationSlotSave(void)
Definition: slot.c:719
uint8 statusFlags
Definition: proc.h:183
LogicalDecodeStreamAbortCB stream_abort_cb
List * output_plugin_options
Definition: logical.h:58
ReorderBufferCommitCB commit
ReplicationSlotPersistentData data
Definition: slot.h:143
#define LOG
Definition: elog.h:26
bool RecoveryInProgress(void)
Definition: xlog.c:8070
ReorderBufferStreamCommitCB stream_commit
void(* callback)(void *arg)
Definition: elog.h:243
struct ErrorContextCallback * previous
Definition: elog.h:242
#define SlotIsPhysical(slot)
Definition: slot.h:164
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
XLogRecPtr confirmed_flush
Definition: slot.h:91
XLogRecPtr write_location
Definition: logical.h:92
XLogRecPtr EndRecPtr
Definition: xlogreader.h:176
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1811
ErrorContextCallback * error_context_stack
Definition: elog.c:92
#define SpinLockAcquire(lock)
Definition: spin.h:62
ReorderBufferStreamMessageCB stream_message
void ReplicationSlotReserveWal(void)
Definition: slot.c:1078
void DecodingContextFindStartpoint(LogicalDecodingContext *ctx)
Definition: logical.c:527
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:826
XLogRecord * XLogReadRecord(XLogReaderState *state, char **errormsg)
Definition: xlogreader.c:268
static void stream_commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:1060
bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:319
#define ERROR
Definition: elog.h:43
void LogicalDecodingProcessRecord(LogicalDecodingContext *ctx, XLogReaderState *record)
Definition: decode.c:94
static void startup_cb_wrapper(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
Definition: logical.c:666
LogicalOutputPluginWriterPrepareWrite prepare_write
Definition: logical.h:63
bool bsysscan
Definition: xact.c:96
void ResetLogicalStreamingState(void)
Definition: logical.c:1459
LogicalDecodeCommitCB commit_cb
TransactionId effective_xmin
Definition: slot.h:139
void(* LogicalOutputPluginInit)(struct OutputPluginCallbacks *cb)
Definition: output_plugin.h:36
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
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:435
static void shutdown_cb_wrapper(LogicalDecodingContext *ctx)
Definition: logical.c:693
Definition: c.h:671
#define DEBUG2
Definition: elog.h:24
XLogRecPtr candidate_restart_valid
Definition: slot.h:160
void pgstat_report_replslot(const char *slotname, int spilltxns, int spillcount, int spillbytes, int streamtxns, int streamcount, int streambytes)
Definition: pgstat.c:1710
int errdetail(const char *fmt,...)
Definition: elog.c:1035
TransactionId catalog_xmin
Definition: slot.h:77
#define InvalidTransactionId
Definition: transam.h:31
void XLogBeginRead(XLogReaderState *state, XLogRecPtr RecPtr)
Definition: xlogreader.c:240
LogicalOutputPluginWriterUpdateProgress update_progress
Definition: logical.h:65
unsigned int uint32
Definition: c.h:429
XLogRecPtr final_lsn
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
bool filter_by_origin_cb_wrapper(LogicalDecodingContext *ctx, RepOriginId origin_id)
Definition: logical.c:863
XLogReaderState * XLogReaderAllocate(int wal_segment_size, const char *waldir, XLogReaderRoutine *routine, void *private_data)
Definition: xlogreader.c:75
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:425
ReorderBufferMessageCB message
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:1145
TransactionId CheckXidAlive
Definition: xact.c:95
void XLogReaderFree(XLogReaderState *state)
Definition: xlogreader.c:139
#define SpinLockRelease(lock)
Definition: spin.h:64
void OutputPluginUpdateProgress(struct LogicalDecodingContext *ctx)
Definition: logical.c:611
void UpdateDecodingStats(LogicalDecodingContext *ctx)
Definition: logical.c:1469
void * palloc0(Size size)
Definition: mcxt.c:981
static void stream_start_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr first_lsn)
Definition: logical.c:929
LogicalDecodeChangeCB change_cb
TransactionId effective_catalog_xmin
Definition: slot.h:140
LogicalDecodeStreamTruncateCB stream_truncate_cb
TransactionId xid
Oid MyDatabaseId
Definition: globals.c:85
static void stream_truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
Definition: logical.c:1185
#define InvalidOid
Definition: postgres_ext.h:36
ReorderBufferStreamStartCB stream_start
#define ereport(elevel,...)
Definition: elog.h:155
void FreeSnapshotBuilder(SnapBuild *builder)
Definition: snapbuild.c:351
static void begin_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
Definition: logical.c:725
void OutputPluginPrepareWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:585
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:800
Definition: regguts.h:298
void FreeDecodingContext(LogicalDecodingContext *ctx)
Definition: logical.c:570
XLogRecPtr restart_lsn
Definition: slot.h:80
void LogicalIncreaseXminForSlot(XLogRecPtr current_lsn, TransactionId xmin)
Definition: logical.c:1238
XLogRecPtr end_lsn
size_t Size
Definition: c.h:528
LogicalDecodeShutdownCB shutdown_cb
LogicalDecodeStreamCommitCB stream_commit_cb
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1207
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:1372
LogicalDecodeStartupCB startup_cb
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:159
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:823
int errmsg(const char *fmt,...)
Definition: elog.c:902
XLogReaderState * reader
Definition: logical.h:41
ReplicationSlot * slot
Definition: logical.h:38
ReorderBufferApplyTruncateCB apply_truncate
void OutputPluginWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:598
LogicalOutputPluginWriterWrite write
Definition: logical.h:64
#define elog(elevel,...)
Definition: elog.h:228
StringInfo out
Definition: logical.h:70
int pgxactoff
Definition: proc.h:144
#define errcontext
Definition: elog.h:199
#define NameStr(name)
Definition: c.h:677
void * arg
LogicalDecodeBeginCB begin_cb
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
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:893
LogicalDecodeStreamStopCB stream_stop_cb
uint8 * statusFlags
Definition: proc.h:327
slock_t mutex
Definition: slot.h:116
#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:646
static void LoadOutputPlugin(OutputPluginCallbacks *callbacks, const char *plugin)
Definition: logical.c:624
LogicalDecodeFilterByOriginCB filter_by_origin_cb
Definition: pg_list.h:50
void CheckLogicalDecodingRequirements(void)
Definition: logical.c:94
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:283
LogicalDecodeStreamChangeCB stream_change_cb
SnapBuild * AllocateSnapshotBuilder(ReorderBuffer *reorder, TransactionId xmin_horizon, XLogRecPtr start_lsn, bool need_full_snapshot)
Definition: snapbuild.c:310
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:776
XLogRecPtr candidate_restart_lsn
Definition: slot.h:161
void ReplicationSlotMarkDirty(void)
Definition: slot.c:737
static void stream_change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
Definition: logical.c:1099
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:137
ReorderBufferStreamStopCB stream_stop