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