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-2019, 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 "fmgr.h"
32 #include "miscadmin.h"
33 
34 #include "access/xact.h"
35 #include "access/xlog_internal.h"
36 
37 #include "replication/decode.h"
38 #include "replication/logical.h"
40 #include "replication/origin.h"
41 #include "replication/snapbuild.h"
42 
43 #include "storage/proc.h"
44 #include "storage/procarray.h"
45 
46 #include "utils/memutils.h"
47 
48 /* data for errcontext callback */
50 {
52  const char *callback_name;
55 
56 /* wrappers around output plugin callbacks */
57 static void output_plugin_error_callback(void *arg);
59  bool is_init);
61 static void begin_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn);
62 static void commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
63  XLogRecPtr commit_lsn);
64 static void change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
65  Relation relation, ReorderBufferChange *change);
66 static void truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
67  int nrelations, Relation relations[], ReorderBufferChange *change);
68 static void message_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
69  XLogRecPtr message_lsn, bool transactional,
70  const char *prefix, Size message_size, const char *message);
71 
72 static void LoadOutputPlugin(OutputPluginCallbacks *callbacks, char *plugin);
73 
74 /*
75  * Make sure the current settings & environment are capable of doing logical
76  * decoding.
77  */
78 void
80 {
82 
83  /*
84  * NB: Adding a new requirement likely means that RestoreSlotFromDisk()
85  * needs the same check.
86  */
87 
89  ereport(ERROR,
90  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
91  errmsg("logical decoding requires wal_level >= logical")));
92 
93  if (MyDatabaseId == InvalidOid)
94  ereport(ERROR,
95  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
96  errmsg("logical decoding requires a database connection")));
97 
98  /* ----
99  * TODO: We got to change that someday soon...
100  *
101  * There's basically three things missing to allow this:
102  * 1) We need to be able to correctly and quickly identify the timeline a
103  * LSN belongs to
104  * 2) We need to force hot_standby_feedback to be enabled at all times so
105  * the primary cannot remove rows we need.
106  * 3) support dropping replication slots referring to a database, in
107  * dbase_redo. There can't be any active ones due to HS recovery
108  * conflicts, so that should be relatively easy.
109  * ----
110  */
111  if (RecoveryInProgress())
112  ereport(ERROR,
113  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
114  errmsg("logical decoding cannot be used while in recovery")));
115 }
116 
117 /*
118  * Helper function for CreateInitDecodingContext() and
119  * CreateDecodingContext() performing common tasks.
120  */
121 static LogicalDecodingContext *
122 StartupDecodingContext(List *output_plugin_options,
123  XLogRecPtr start_lsn,
124  TransactionId xmin_horizon,
125  bool need_full_snapshot,
126  bool fast_forward,
127  XLogPageReadCB read_page,
131 {
132  ReplicationSlot *slot;
133  MemoryContext context,
134  old_context;
136 
137  /* shorter lines... */
138  slot = MyReplicationSlot;
139 
141  "Logical decoding context",
143  old_context = MemoryContextSwitchTo(context);
144  ctx = palloc0(sizeof(LogicalDecodingContext));
145 
146  ctx->context = context;
147 
148  /*
149  * (re-)load output plugins, so we detect a bad (removed) output plugin
150  * now.
151  */
152  if (!fast_forward)
154 
155  /*
156  * Now that the slot's xmin has been set, we can announce ourselves as a
157  * logical decoding backend which doesn't need to be checked individually
158  * when computing the xmin horizon because the xmin is enforced via
159  * replication slots.
160  *
161  * We can only do so if we're outside of a transaction (i.e. the case when
162  * streaming changes via walsender), otherwise an already setup
163  * snapshot/xid would end up being ignored. That's not a particularly
164  * bothersome restriction since the SQL interface can't be used for
165  * streaming anyway.
166  */
168  {
169  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
171  LWLockRelease(ProcArrayLock);
172  }
173 
174  ctx->slot = slot;
175 
176  ctx->reader = XLogReaderAllocate(wal_segment_size, NULL, read_page, ctx);
177  if (!ctx->reader)
178  ereport(ERROR,
179  (errcode(ERRCODE_OUT_OF_MEMORY),
180  errmsg("out of memory")));
181 
183  ctx->snapshot_builder =
184  AllocateSnapshotBuilder(ctx->reorder, xmin_horizon, start_lsn,
185  need_full_snapshot);
186 
187  ctx->reorder->private_data = ctx;
188 
189  /* wrap output plugin callbacks, so we can add error context information */
195 
196  ctx->out = makeStringInfo();
197  ctx->prepare_write = prepare_write;
198  ctx->write = do_write;
199  ctx->update_progress = update_progress;
200 
201  ctx->output_plugin_options = output_plugin_options;
202 
203  ctx->fast_forward = fast_forward;
204 
205  MemoryContextSwitchTo(old_context);
206 
207  return ctx;
208 }
209 
210 /*
211  * Create a new decoding context, for a new logical slot.
212  *
213  * plugin -- contains the name of the output plugin
214  * output_plugin_options -- contains options passed to the output plugin
215  * restart_lsn -- if given as invalid, it's this routine's responsibility to
216  * mark WAL as reserved by setting a convenient restart_lsn for the slot.
217  * Otherwise, we set for decoding to start from the given LSN without
218  * marking WAL reserved beforehand. In that scenario, it's up to the
219  * caller to guarantee that WAL remains available.
220  * read_page, prepare_write, do_write, update_progress --
221  * callbacks that perform the use-case dependent, actual, work.
222  *
223  * Needs to be called while in a memory context that's at least as long lived
224  * as the decoding context because further memory contexts will be created
225  * inside it.
226  *
227  * Returns an initialized decoding context after calling the output plugin's
228  * startup function.
229  */
232  List *output_plugin_options,
233  bool need_full_snapshot,
234  XLogRecPtr restart_lsn,
235  XLogPageReadCB read_page,
239 {
240  TransactionId xmin_horizon = InvalidTransactionId;
241  ReplicationSlot *slot;
243  MemoryContext old_context;
244 
245  /* shorter lines... */
246  slot = MyReplicationSlot;
247 
248  /* first some sanity checks that are unlikely to be violated */
249  if (slot == NULL)
250  elog(ERROR, "cannot perform logical decoding without an acquired slot");
251 
252  if (plugin == NULL)
253  elog(ERROR, "cannot initialize logical decoding without a specified plugin");
254 
255  /* Make sure the passed slot is suitable. These are user facing errors. */
256  if (SlotIsPhysical(slot))
257  ereport(ERROR,
258  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
259  errmsg("cannot use physical replication slot for logical decoding")));
260 
261  if (slot->data.database != MyDatabaseId)
262  ereport(ERROR,
263  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
264  errmsg("replication slot \"%s\" was not created in this database",
265  NameStr(slot->data.name))));
266 
267  if (IsTransactionState() &&
269  ereport(ERROR,
270  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
271  errmsg("cannot create logical replication slot in transaction that has performed writes")));
272 
273  /* register output plugin name with slot */
274  SpinLockAcquire(&slot->mutex);
275  StrNCpy(NameStr(slot->data.plugin), plugin, NAMEDATALEN);
276  SpinLockRelease(&slot->mutex);
277 
278  if (XLogRecPtrIsInvalid(restart_lsn))
280  else
281  {
282  SpinLockAcquire(&slot->mutex);
283  slot->data.restart_lsn = restart_lsn;
284  SpinLockRelease(&slot->mutex);
285  }
286 
287  /* ----
288  * This is a bit tricky: We need to determine a safe xmin horizon to start
289  * decoding from, to avoid starting from a running xacts record referring
290  * to xids whose rows have been vacuumed or pruned
291  * already. GetOldestSafeDecodingTransactionId() returns such a value, but
292  * without further interlock its return value might immediately be out of
293  * date.
294  *
295  * So we have to acquire the ProcArrayLock to prevent computation of new
296  * xmin horizons by other backends, get the safe decoding xid, and inform
297  * the slot machinery about the new limit. Once that's done the
298  * ProcArrayLock can be released as the slot machinery now is
299  * protecting against vacuum.
300  *
301  * Note that, temporarily, the data, not just the catalog, xmin has to be
302  * reserved if a data snapshot is to be exported. Otherwise the initial
303  * data snapshot created here is not guaranteed to be valid. After that
304  * the data xmin doesn't need to be managed anymore and the global xmin
305  * should be recomputed. As we are fine with losing the pegged data xmin
306  * after crash - no chance a snapshot would get exported anymore - we can
307  * get away with just setting the slot's
308  * effective_xmin. ReplicationSlotRelease will reset it again.
309  *
310  * ----
311  */
312  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
313 
314  xmin_horizon = GetOldestSafeDecodingTransactionId(!need_full_snapshot);
315 
316  SpinLockAcquire(&slot->mutex);
317  slot->effective_catalog_xmin = xmin_horizon;
318  slot->data.catalog_xmin = xmin_horizon;
319  if (need_full_snapshot)
320  slot->effective_xmin = xmin_horizon;
321  SpinLockRelease(&slot->mutex);
322 
324 
325  LWLockRelease(ProcArrayLock);
326 
329 
330  ctx = StartupDecodingContext(NIL, restart_lsn, xmin_horizon,
331  need_full_snapshot, false,
332  read_page, prepare_write, do_write,
333  update_progress);
334 
335  /* call output plugin initialization callback */
336  old_context = MemoryContextSwitchTo(ctx->context);
337  if (ctx->callbacks.startup_cb != NULL)
338  startup_cb_wrapper(ctx, &ctx->options, true);
339  MemoryContextSwitchTo(old_context);
340 
342 
343  return ctx;
344 }
345 
346 /*
347  * Create a new decoding context, for a logical slot that has previously been
348  * used already.
349  *
350  * start_lsn
351  * The LSN at which to start decoding. If InvalidXLogRecPtr, restart
352  * from the slot's confirmed_flush; otherwise, start from the specified
353  * location (but move it forwards to confirmed_flush if it's older than
354  * that, see below).
355  *
356  * output_plugin_options
357  * options passed to the output plugin.
358  *
359  * fast_forward
360  * bypass the generation of logical changes.
361  *
362  * read_page, prepare_write, do_write, update_progress
363  * callbacks that have to be filled to perform the use-case dependent,
364  * actual work.
365  *
366  * Needs to be called while in a memory context that's at least as long lived
367  * as the decoding context because further memory contexts will be created
368  * inside it.
369  *
370  * Returns an initialized decoding context after calling the output plugin's
371  * startup function.
372  */
375  List *output_plugin_options,
376  bool fast_forward,
377  XLogPageReadCB read_page,
381 {
383  ReplicationSlot *slot;
384  MemoryContext old_context;
385 
386  /* shorter lines... */
387  slot = MyReplicationSlot;
388 
389  /* first some sanity checks that are unlikely to be violated */
390  if (slot == NULL)
391  elog(ERROR, "cannot perform logical decoding without an acquired slot");
392 
393  /* make sure the passed slot is suitable, these are user facing errors */
394  if (SlotIsPhysical(slot))
395  ereport(ERROR,
396  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
397  (errmsg("cannot use physical replication slot for logical decoding"))));
398 
399  if (slot->data.database != MyDatabaseId)
400  ereport(ERROR,
401  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
402  (errmsg("replication slot \"%s\" was not created in this database",
403  NameStr(slot->data.name)))));
404 
405  if (start_lsn == InvalidXLogRecPtr)
406  {
407  /* continue from last position */
408  start_lsn = slot->data.confirmed_flush;
409  }
410  else if (start_lsn < slot->data.confirmed_flush)
411  {
412  /*
413  * It might seem like we should error out in this case, but it's
414  * pretty common for a client to acknowledge a LSN it doesn't have to
415  * do anything for, and thus didn't store persistently, because the
416  * xlog records didn't result in anything relevant for logical
417  * decoding. Clients have to be able to do that to support synchronous
418  * replication.
419  */
420  elog(DEBUG1, "cannot stream from %X/%X, minimum is %X/%X, forwarding",
421  (uint32) (start_lsn >> 32), (uint32) start_lsn,
422  (uint32) (slot->data.confirmed_flush >> 32),
423  (uint32) slot->data.confirmed_flush);
424 
425  start_lsn = slot->data.confirmed_flush;
426  }
427 
428  ctx = StartupDecodingContext(output_plugin_options,
429  start_lsn, InvalidTransactionId, false,
430  fast_forward, read_page, prepare_write,
431  do_write, update_progress);
432 
433  /* call output plugin initialization callback */
434  old_context = MemoryContextSwitchTo(ctx->context);
435  if (ctx->callbacks.startup_cb != NULL)
436  startup_cb_wrapper(ctx, &ctx->options, false);
437  MemoryContextSwitchTo(old_context);
438 
440 
441  ereport(LOG,
442  (errmsg("starting logical decoding for slot \"%s\"",
443  NameStr(slot->data.name)),
444  errdetail("Streaming transactions committing after %X/%X, reading WAL from %X/%X.",
445  (uint32) (slot->data.confirmed_flush >> 32),
446  (uint32) slot->data.confirmed_flush,
447  (uint32) (slot->data.restart_lsn >> 32),
448  (uint32) slot->data.restart_lsn)));
449 
450  return ctx;
451 }
452 
453 /*
454  * Returns true if a consistent initial decoding snapshot has been built.
455  */
456 bool
458 {
460 }
461 
462 /*
463  * Read from the decoding slot, until it is ready to start extracting changes.
464  */
465 void
467 {
469  ReplicationSlot *slot = ctx->slot;
470 
471  /* Initialize from where to start reading WAL. */
472  startptr = slot->data.restart_lsn;
473 
474  elog(DEBUG1, "searching for logical decoding starting point, starting at %X/%X",
475  (uint32) (slot->data.restart_lsn >> 32),
476  (uint32) slot->data.restart_lsn);
477 
478  /* Wait for a consistent starting point */
479  for (;;)
480  {
481  XLogRecord *record;
482  char *err = NULL;
483 
484  /* the read_page callback waits for new WAL */
485  record = XLogReadRecord(ctx->reader, startptr, &err);
486  if (err)
487  elog(ERROR, "%s", err);
488  if (!record)
489  elog(ERROR, "no record found"); /* shouldn't happen */
490 
491  startptr = InvalidXLogRecPtr;
492 
494 
495  /* only continue till we found a consistent spot */
496  if (DecodingContextReady(ctx))
497  break;
498 
500  }
501 
502  SpinLockAcquire(&slot->mutex);
503  slot->data.confirmed_flush = ctx->reader->EndRecPtr;
504  SpinLockRelease(&slot->mutex);
505 }
506 
507 /*
508  * Free a previously allocated decoding context, invoking the shutdown
509  * callback if necessary.
510  */
511 void
513 {
514  if (ctx->callbacks.shutdown_cb != NULL)
515  shutdown_cb_wrapper(ctx);
516 
519  XLogReaderFree(ctx->reader);
521 }
522 
523 /*
524  * Prepare a write using the context's output routine.
525  */
526 void
528 {
529  if (!ctx->accept_writes)
530  elog(ERROR, "writes are only accepted in commit, begin and change callbacks");
531 
532  ctx->prepare_write(ctx, ctx->write_location, ctx->write_xid, last_write);
533  ctx->prepared_write = true;
534 }
535 
536 /*
537  * Perform a write using the context's output routine.
538  */
539 void
541 {
542  if (!ctx->prepared_write)
543  elog(ERROR, "OutputPluginPrepareWrite needs to be called before OutputPluginWrite");
544 
545  ctx->write(ctx, ctx->write_location, ctx->write_xid, last_write);
546  ctx->prepared_write = false;
547 }
548 
549 /*
550  * Update progress tracking (if supported).
551  */
552 void
554 {
555  if (!ctx->update_progress)
556  return;
557 
558  ctx->update_progress(ctx, ctx->write_location, ctx->write_xid);
559 }
560 
561 /*
562  * Load the output plugin, lookup its output plugin init function, and check
563  * that it provides the required callbacks.
564  */
565 static void
567 {
568  LogicalOutputPluginInit plugin_init;
569 
570  plugin_init = (LogicalOutputPluginInit)
571  load_external_function(plugin, "_PG_output_plugin_init", false, NULL);
572 
573  if (plugin_init == NULL)
574  elog(ERROR, "output plugins have to declare the _PG_output_plugin_init symbol");
575 
576  /* ask the output plugin to fill the callback struct */
577  plugin_init(callbacks);
578 
579  if (callbacks->begin_cb == NULL)
580  elog(ERROR, "output plugins have to register a begin callback");
581  if (callbacks->change_cb == NULL)
582  elog(ERROR, "output plugins have to register a change callback");
583  if (callbacks->commit_cb == NULL)
584  elog(ERROR, "output plugins have to register a commit callback");
585 }
586 
587 static void
589 {
591 
592  /* not all callbacks have an associated LSN */
593  if (state->report_location != InvalidXLogRecPtr)
594  errcontext("slot \"%s\", output plugin \"%s\", in the %s callback, associated LSN %X/%X",
595  NameStr(state->ctx->slot->data.name),
596  NameStr(state->ctx->slot->data.plugin),
597  state->callback_name,
598  (uint32) (state->report_location >> 32),
599  (uint32) state->report_location);
600  else
601  errcontext("slot \"%s\", output plugin \"%s\", in the %s callback",
602  NameStr(state->ctx->slot->data.name),
603  NameStr(state->ctx->slot->data.plugin),
604  state->callback_name);
605 }
606 
607 static void
609 {
611  ErrorContextCallback errcallback;
612 
613  Assert(!ctx->fast_forward);
614 
615  /* Push callback + info on the error context stack */
616  state.ctx = ctx;
617  state.callback_name = "startup";
620  errcallback.arg = (void *) &state;
621  errcallback.previous = error_context_stack;
622  error_context_stack = &errcallback;
623 
624  /* set output state */
625  ctx->accept_writes = false;
626 
627  /* do the actual work: call callback */
628  ctx->callbacks.startup_cb(ctx, opt, is_init);
629 
630  /* Pop the error context stack */
631  error_context_stack = errcallback.previous;
632 }
633 
634 static void
636 {
638  ErrorContextCallback errcallback;
639 
640  Assert(!ctx->fast_forward);
641 
642  /* Push callback + info on the error context stack */
643  state.ctx = ctx;
644  state.callback_name = "shutdown";
647  errcallback.arg = (void *) &state;
648  errcallback.previous = error_context_stack;
649  error_context_stack = &errcallback;
650 
651  /* set output state */
652  ctx->accept_writes = false;
653 
654  /* do the actual work: call callback */
655  ctx->callbacks.shutdown_cb(ctx);
656 
657  /* Pop the error context stack */
658  error_context_stack = errcallback.previous;
659 }
660 
661 
662 /*
663  * Callbacks for ReorderBuffer which add in some more information and then call
664  * output_plugin.h plugins.
665  */
666 static void
668 {
671  ErrorContextCallback errcallback;
672 
673  Assert(!ctx->fast_forward);
674 
675  /* Push callback + info on the error context stack */
676  state.ctx = ctx;
677  state.callback_name = "begin";
678  state.report_location = txn->first_lsn;
680  errcallback.arg = (void *) &state;
681  errcallback.previous = error_context_stack;
682  error_context_stack = &errcallback;
683 
684  /* set output state */
685  ctx->accept_writes = true;
686  ctx->write_xid = txn->xid;
687  ctx->write_location = txn->first_lsn;
688 
689  /* do the actual work: call callback */
690  ctx->callbacks.begin_cb(ctx, txn);
691 
692  /* Pop the error context stack */
693  error_context_stack = errcallback.previous;
694 }
695 
696 static void
698  XLogRecPtr commit_lsn)
699 {
702  ErrorContextCallback errcallback;
703 
704  Assert(!ctx->fast_forward);
705 
706  /* Push callback + info on the error context stack */
707  state.ctx = ctx;
708  state.callback_name = "commit";
709  state.report_location = txn->final_lsn; /* beginning of commit record */
711  errcallback.arg = (void *) &state;
712  errcallback.previous = error_context_stack;
713  error_context_stack = &errcallback;
714 
715  /* set output state */
716  ctx->accept_writes = true;
717  ctx->write_xid = txn->xid;
718  ctx->write_location = txn->end_lsn; /* points to the end of the record */
719 
720  /* do the actual work: call callback */
721  ctx->callbacks.commit_cb(ctx, txn, commit_lsn);
722 
723  /* Pop the error context stack */
724  error_context_stack = errcallback.previous;
725 }
726 
727 static void
729  Relation relation, ReorderBufferChange *change)
730 {
733  ErrorContextCallback errcallback;
734 
735  Assert(!ctx->fast_forward);
736 
737  /* Push callback + info on the error context stack */
738  state.ctx = ctx;
739  state.callback_name = "change";
740  state.report_location = change->lsn;
742  errcallback.arg = (void *) &state;
743  errcallback.previous = error_context_stack;
744  error_context_stack = &errcallback;
745 
746  /* set output state */
747  ctx->accept_writes = true;
748  ctx->write_xid = txn->xid;
749 
750  /*
751  * report this change's lsn so replies from clients can give an up2date
752  * answer. This won't ever be enough (and shouldn't be!) to confirm
753  * receipt of this transaction, but it might allow another transaction's
754  * commit to be confirmed with one message.
755  */
756  ctx->write_location = change->lsn;
757 
758  ctx->callbacks.change_cb(ctx, txn, relation, change);
759 
760  /* Pop the error context stack */
761  error_context_stack = errcallback.previous;
762 }
763 
764 static void
766  int nrelations, Relation relations[], ReorderBufferChange *change)
767 {
770  ErrorContextCallback errcallback;
771 
772  Assert(!ctx->fast_forward);
773 
774  if (!ctx->callbacks.truncate_cb)
775  return;
776 
777  /* Push callback + info on the error context stack */
778  state.ctx = ctx;
779  state.callback_name = "truncate";
780  state.report_location = change->lsn;
782  errcallback.arg = (void *) &state;
783  errcallback.previous = error_context_stack;
784  error_context_stack = &errcallback;
785 
786  /* set output state */
787  ctx->accept_writes = true;
788  ctx->write_xid = txn->xid;
789 
790  /*
791  * report this change's lsn so replies from clients can give an up2date
792  * answer. This won't ever be enough (and shouldn't be!) to confirm
793  * receipt of this transaction, but it might allow another transaction's
794  * commit to be confirmed with one message.
795  */
796  ctx->write_location = change->lsn;
797 
798  ctx->callbacks.truncate_cb(ctx, txn, nrelations, relations, change);
799 
800  /* Pop the error context stack */
801  error_context_stack = errcallback.previous;
802 }
803 
804 bool
806 {
808  ErrorContextCallback errcallback;
809  bool ret;
810 
811  Assert(!ctx->fast_forward);
812 
813  /* Push callback + info on the error context stack */
814  state.ctx = ctx;
815  state.callback_name = "filter_by_origin";
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 = false;
824 
825  /* do the actual work: call callback */
826  ret = ctx->callbacks.filter_by_origin_cb(ctx, origin_id);
827 
828  /* Pop the error context stack */
829  error_context_stack = errcallback.previous;
830 
831  return ret;
832 }
833 
834 static void
836  XLogRecPtr message_lsn, bool transactional,
837  const char *prefix, Size message_size, const char *message)
838 {
841  ErrorContextCallback errcallback;
842 
843  Assert(!ctx->fast_forward);
844 
845  if (ctx->callbacks.message_cb == NULL)
846  return;
847 
848  /* Push callback + info on the error context stack */
849  state.ctx = ctx;
850  state.callback_name = "message";
851  state.report_location = message_lsn;
853  errcallback.arg = (void *) &state;
854  errcallback.previous = error_context_stack;
855  error_context_stack = &errcallback;
856 
857  /* set output state */
858  ctx->accept_writes = true;
859  ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
860  ctx->write_location = message_lsn;
861 
862  /* do the actual work: call callback */
863  ctx->callbacks.message_cb(ctx, txn, message_lsn, transactional, prefix,
864  message_size, message);
865 
866  /* Pop the error context stack */
867  error_context_stack = errcallback.previous;
868 }
869 
870 /*
871  * Set the required catalog xmin horizon for historic snapshots in the current
872  * replication slot.
873  *
874  * Note that in the most cases, we won't be able to immediately use the xmin
875  * to increase the xmin horizon: we need to wait till the client has confirmed
876  * receiving current_lsn with LogicalConfirmReceivedLocation().
877  */
878 void
880 {
881  bool updated_xmin = false;
882  ReplicationSlot *slot;
883 
884  slot = MyReplicationSlot;
885 
886  Assert(slot != NULL);
887 
888  SpinLockAcquire(&slot->mutex);
889 
890  /*
891  * don't overwrite if we already have a newer xmin. This can happen if we
892  * restart decoding in a slot.
893  */
895  {
896  }
897 
898  /*
899  * If the client has already confirmed up to this lsn, we directly can
900  * mark this as accepted. This can happen if we restart decoding in a
901  * slot.
902  */
903  else if (current_lsn <= slot->data.confirmed_flush)
904  {
905  slot->candidate_catalog_xmin = xmin;
906  slot->candidate_xmin_lsn = current_lsn;
907 
908  /* our candidate can directly be used */
909  updated_xmin = true;
910  }
911 
912  /*
913  * Only increase if the previous values have been applied, otherwise we
914  * might never end up updating if the receiver acks too slowly.
915  */
916  else if (slot->candidate_xmin_lsn == InvalidXLogRecPtr)
917  {
918  slot->candidate_catalog_xmin = xmin;
919  slot->candidate_xmin_lsn = current_lsn;
920  }
921  SpinLockRelease(&slot->mutex);
922 
923  /* candidate already valid with the current flush position, apply */
924  if (updated_xmin)
926 }
927 
928 /*
929  * Mark the minimal LSN (restart_lsn) we need to read to replay all
930  * transactions that have not yet committed at current_lsn.
931  *
932  * Just like LogicalIncreaseXminForSlot this only takes effect when the
933  * client has confirmed to have received current_lsn.
934  */
935 void
937 {
938  bool updated_lsn = false;
939  ReplicationSlot *slot;
940 
941  slot = MyReplicationSlot;
942 
943  Assert(slot != NULL);
944  Assert(restart_lsn != InvalidXLogRecPtr);
945  Assert(current_lsn != InvalidXLogRecPtr);
946 
947  SpinLockAcquire(&slot->mutex);
948 
949  /* don't overwrite if have a newer restart lsn */
950  if (restart_lsn <= slot->data.restart_lsn)
951  {
952  }
953 
954  /*
955  * We might have already flushed far enough to directly accept this lsn,
956  * in this case there is no need to check for existing candidate LSNs
957  */
958  else if (current_lsn <= slot->data.confirmed_flush)
959  {
960  slot->candidate_restart_valid = current_lsn;
961  slot->candidate_restart_lsn = restart_lsn;
962 
963  /* our candidate can directly be used */
964  updated_lsn = true;
965  }
966 
967  /*
968  * Only increase if the previous values have been applied, otherwise we
969  * might never end up updating if the receiver acks too slowly. A missed
970  * value here will just cause some extra effort after reconnecting.
971  */
973  {
974  slot->candidate_restart_valid = current_lsn;
975  slot->candidate_restart_lsn = restart_lsn;
976 
977  elog(DEBUG1, "got new restart lsn %X/%X at %X/%X",
978  (uint32) (restart_lsn >> 32), (uint32) restart_lsn,
979  (uint32) (current_lsn >> 32), (uint32) current_lsn);
980  }
981  else
982  {
983  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",
984  (uint32) (restart_lsn >> 32), (uint32) restart_lsn,
985  (uint32) (current_lsn >> 32), (uint32) current_lsn,
986  (uint32) (slot->candidate_restart_lsn >> 32),
988  (uint32) (slot->candidate_restart_valid >> 32),
990  (uint32) (slot->data.confirmed_flush >> 32),
991  (uint32) slot->data.confirmed_flush
992  );
993  }
994  SpinLockRelease(&slot->mutex);
995 
996  /* candidates are already valid with the current flush position, apply */
997  if (updated_lsn)
999 }
1000 
1001 /*
1002  * Handle a consumer's confirmation having received all changes up to lsn.
1003  */
1004 void
1006 {
1007  Assert(lsn != InvalidXLogRecPtr);
1008 
1009  /* Do an unlocked check for candidate_lsn first. */
1012  {
1013  bool updated_xmin = false;
1014  bool updated_restart = false;
1015 
1017 
1019 
1020  /* if we're past the location required for bumping xmin, do so */
1023  {
1024  /*
1025  * We have to write the changed xmin to disk *before* we change
1026  * the in-memory value, otherwise after a crash we wouldn't know
1027  * that some catalog tuples might have been removed already.
1028  *
1029  * Ensure that by first writing to ->xmin and only update
1030  * ->effective_xmin once the new state is synced to disk. After a
1031  * crash ->effective_xmin is set to ->xmin.
1032  */
1035  {
1039  updated_xmin = true;
1040  }
1041  }
1042 
1045  {
1047 
1051  updated_restart = true;
1052  }
1053 
1055 
1056  /* first write new xmin to disk, so we know what's up after a crash */
1057  if (updated_xmin || updated_restart)
1058  {
1061  elog(DEBUG1, "updated xmin: %u restart: %u", updated_xmin, updated_restart);
1062  }
1063 
1064  /*
1065  * Now the new xmin is safely on disk, we can let the global value
1066  * advance. We do not take ProcArrayLock or similar since we only
1067  * advance xmin here and there's not much harm done by a concurrent
1068  * computation missing that.
1069  */
1070  if (updated_xmin)
1071  {
1075 
1078  }
1079  }
1080  else
1081  {
1085  }
1086 }
XLogRecPtr first_lsn
LogicalDecodeTruncateCB truncate_cb
#define NIL
Definition: pg_list.h:65
TransactionId write_xid
Definition: logical.h:89
static const char * plugin
void CheckSlotRequirements(void)
Definition: slot.c:972
TransactionId candidate_catalog_xmin
Definition: slot.h:147
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
TransactionId GetOldestSafeDecodingTransactionId(bool catalogOnly)
Definition: procarray.c:2175
ReorderBufferApplyChangeCB apply_change
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
#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:728
struct ReorderBuffer * reorder
Definition: logical.h:43
#define PROC_IN_LOGICAL_DECODING
Definition: proc.h:57
int wal_segment_size
Definition: xlog.c:112
uint32 TransactionId
Definition: c.h:507
SnapBuildState SnapBuildCurrentState(SnapBuild *builder)
Definition: snapbuild.c:400
static void LoadOutputPlugin(OutputPluginCallbacks *callbacks, char *plugin)
Definition: logical.c:566
bool DecodingContextReady(LogicalDecodingContext *ctx)
Definition: logical.c:457
struct LogicalErrorCallbackState LogicalErrorCallbackState
static void commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:697
OutputPluginOptions options
Definition: logical.h:54
void LogicalIncreaseRestartDecodingForSlot(XLogRecPtr current_lsn, XLogRecPtr restart_lsn)
Definition: logical.c:936
int wal_level
Definition: xlog.c:103
StringInfo makeStringInfo(void)
Definition: stringinfo.c:28
LogicalDecodeMessageCB message_cb
OutputPluginCallbacks callbacks
Definition: logical.h:53
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:570
bool IsTransactionOrTransactionBlock(void)
Definition: xact.c:4651
MemoryContext context
Definition: logical.h:36
void ReplicationSlotSave(void)
Definition: slot.c:645
List * output_plugin_options
Definition: logical.h:59
ReorderBufferCommitCB commit
ReplicationSlotPersistentData data
Definition: slot.h:132
#define LOG
Definition: elog.h:26
bool RecoveryInProgress(void)
Definition: xlog.c:7913
void(* callback)(void *arg)
Definition: elog.h:254
struct ErrorContextCallback * previous
Definition: elog.h:253
#define SlotIsPhysical(slot)
Definition: slot.h:153
LogicalOutputPluginWriterWrite LogicalOutputPluginWriterPrepareWrite
Definition: logical.h:26
void(* LogicalOutputPluginWriterUpdateProgress)(struct LogicalDecodingContext *lr, XLogRecPtr Ptr, TransactionId xid)
Definition: logical.h:28
const char * callback_name
Definition: logical.c:52
XLogRecord * XLogReadRecord(XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
Definition: xlogreader.c:237
XLogRecPtr confirmed_flush
Definition: slot.h:80
PGXACT * MyPgXact
Definition: proc.c:69
XLogRecPtr write_location
Definition: logical.h:88
XLogRecPtr EndRecPtr
Definition: xlogreader.h:133
uint8 vacuumFlags
Definition: proc.h:233
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
ErrorContextCallback * error_context_stack
Definition: elog.c:88
#define NAMEDATALEN
#define SpinLockAcquire(lock)
Definition: spin.h:62
void ReplicationSlotReserveWal(void)
Definition: slot.c:997
void DecodingContextFindStartpoint(LogicalDecodingContext *ctx)
Definition: logical.c:466
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:748
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:97
static void startup_cb_wrapper(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
Definition: logical.c:608
LogicalOutputPluginWriterPrepareWrite prepare_write
Definition: logical.h:64
LogicalDecodeCommitCB commit_cb
TransactionId effective_xmin
Definition: slot.h:128
void(* LogicalOutputPluginInit)(struct OutputPluginCallbacks *cb)
Definition: output_plugin.h:36
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
static void shutdown_cb_wrapper(LogicalDecodingContext *ctx)
Definition: logical.c:635
XLogRecPtr candidate_restart_valid
Definition: slot.h:149
int errdetail(const char *fmt,...)
Definition: elog.c:860
TransactionId catalog_xmin
Definition: slot.h:69
#define InvalidTransactionId
Definition: transam.h:31
LogicalOutputPluginWriterUpdateProgress update_progress
Definition: logical.h:66
unsigned int uint32
Definition: c.h:358
XLogRecPtr final_lsn
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
bool filter_by_origin_cb_wrapper(LogicalDecodingContext *ctx, RepOriginId origin_id)
Definition: logical.c:805
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:410
ReorderBufferMessageCB message
PGFunction load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
Definition: dfmgr.c:107
#define ereport(elevel, rest)
Definition: elog.h:141
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
void XLogReaderFree(XLogReaderState *state)
Definition: xlogreader.c:133
#define SpinLockRelease(lock)
Definition: spin.h:64
LogicalDecodingContext * CreateInitDecodingContext(char *plugin, List *output_plugin_options, bool need_full_snapshot, XLogRecPtr restart_lsn, XLogPageReadCB read_page, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
Definition: logical.c:231
void OutputPluginUpdateProgress(struct LogicalDecodingContext *ctx)
Definition: logical.c:553
void * palloc0(Size size)
Definition: mcxt.c:980
LogicalDecodeChangeCB change_cb
TransactionId effective_catalog_xmin
Definition: slot.h:129
LogicalDecodingContext * CreateDecodingContext(XLogRecPtr start_lsn, List *output_plugin_options, bool fast_forward, XLogPageReadCB read_page, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
Definition: logical.c:374
TransactionId xid
Oid MyDatabaseId
Definition: globals.c:85
#define InvalidOid
Definition: postgres_ext.h:36
void FreeSnapshotBuilder(SnapBuild *builder)
Definition: snapbuild.c:356
static void begin_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
Definition: logical.c:667
void OutputPluginPrepareWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:527
struct SnapBuild * snapshot_builder
Definition: logical.h:44
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:732
#define StrNCpy(dst, src, len)
Definition: c.h:928
Definition: regguts.h:298
void FreeDecodingContext(LogicalDecodingContext *ctx)
Definition: logical.c:512
XLogRecPtr restart_lsn
Definition: slot.h:72
void LogicalIncreaseXminForSlot(XLogRecPtr current_lsn, TransactionId xmin)
Definition: logical.c:879
XLogRecPtr end_lsn
size_t Size
Definition: c.h:466
static LogicalDecodingContext * StartupDecodingContext(List *output_plugin_options, XLogRecPtr start_lsn, TransactionId xmin_horizon, bool need_full_snapshot, bool fast_forward, XLogPageReadCB read_page, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
Definition: logical.c:122
LogicalDecodeShutdownCB shutdown_cb
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
bool IsTransactionState(void)
Definition: xact.c:356
ReorderBuffer * ReorderBufferAllocate(void)
void(* LogicalOutputPluginWriterWrite)(struct LogicalDecodingContext *lr, XLogRecPtr Ptr, TransactionId xid, bool last_write)
Definition: logical.h:20
void LogicalConfirmReceivedLocation(XLogRecPtr lsn)
Definition: logical.c:1005
LogicalDecodeStartupCB startup_cb
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:148
XLogReaderState * XLogReaderAllocate(int wal_segment_size, const char *waldir, XLogPageReadCB pagereadfunc, void *private_data)
Definition: xlogreader.c:71
XLogRecPtr report_location
Definition: logical.c:53
static void truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
Definition: logical.c:765
int errmsg(const char *fmt,...)
Definition: elog.c:784
XLogReaderState * reader
Definition: logical.h:42
ReplicationSlot * slot
Definition: logical.h:39
ReorderBufferApplyTruncateCB apply_truncate
void OutputPluginWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:540
LogicalOutputPluginWriterWrite write
Definition: logical.h:65
#define elog(elevel,...)
Definition: elog.h:226
StringInfo out
Definition: logical.h:71
#define errcontext
Definition: elog.h:183
#define NameStr(name)
Definition: c.h:609
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:835
int(* XLogPageReadCB)(XLogReaderState *xlogreader, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetRecPtr, char *readBuf)
Definition: xlogreader.h:53
slock_t mutex
Definition: slot.h:105
#define TransactionIdIsValid(xid)
Definition: transam.h:41
LogicalDecodingContext * ctx
Definition: logical.c:51
ReorderBufferBeginCB begin
static void output_plugin_error_callback(void *arg)
Definition: logical.c:588
LogicalDecodeFilterByOriginCB filter_by_origin_cb
Definition: pg_list.h:50
void CheckLogicalDecodingRequirements(void)
Definition: logical.c:79
SnapBuild * AllocateSnapshotBuilder(ReorderBuffer *reorder, TransactionId xmin_horizon, XLogRecPtr start_lsn, bool need_full_snapshot)
Definition: snapbuild.c:315
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:702
XLogRecPtr candidate_restart_lsn
Definition: slot.h:150
void ReplicationSlotMarkDirty(void)
Definition: slot.c:663
static XLogRecPtr startptr
Definition: basebackup.c:118