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-2017, 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",
132  old_context = MemoryContextSwitchTo(context);
133  ctx = palloc0(sizeof(LogicalDecodingContext));
134 
135  ctx->context = context;
136 
137  /*
138  * (re-)load output plugins, so we detect a bad (removed) output plugin
139  * now.
140  */
142 
143  /*
144  * Now that the slot's xmin has been set, we can announce ourselves as a
145  * logical decoding backend which doesn't need to be checked individually
146  * when computing the xmin horizon because the xmin is enforced via
147  * replication slots.
148  *
149  * We can only do so if we're outside of a transaction (i.e. the case when
150  * streaming changes via walsender), otherwise an already setup
151  * snapshot/xid would end up being ignored. That's not a particularly
152  * bothersome restriction since the SQL interface can't be used for
153  * streaming anyway.
154  */
156  {
157  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
159  LWLockRelease(ProcArrayLock);
160  }
161 
162  ctx->slot = slot;
163 
164  ctx->reader = XLogReaderAllocate(read_page, ctx);
165  if (!ctx->reader)
166  ereport(ERROR,
167  (errcode(ERRCODE_OUT_OF_MEMORY),
168  errmsg("out of memory")));
169 
170  ctx->reader->private_data = ctx;
171 
173  ctx->snapshot_builder =
174  AllocateSnapshotBuilder(ctx->reorder, xmin_horizon, start_lsn);
175 
176  ctx->reorder->private_data = ctx;
177 
178  /* wrap output plugin callbacks, so we can add error context information */
183 
184  ctx->out = makeStringInfo();
185  ctx->prepare_write = prepare_write;
186  ctx->write = do_write;
187 
188  ctx->output_plugin_options = output_plugin_options;
189 
190  MemoryContextSwitchTo(old_context);
191 
192  return ctx;
193 }
194 
195 /*
196  * Create a new decoding context, for a new logical slot.
197  *
198  * plugin contains the name of the output plugin
199  * output_plugin_options contains options passed to the output plugin
200  * read_page, prepare_write, do_write are callbacks that have to be filled to
201  * perform the use-case dependent, actual, work.
202  *
203  * Needs to be called while in a memory context that's at least as long lived
204  * as the decoding context because further memory contexts will be created
205  * inside it.
206  *
207  * Returns an initialized decoding context after calling the output plugin's
208  * startup function.
209  */
212  List *output_plugin_options,
213  XLogPageReadCB read_page,
216 {
217  TransactionId xmin_horizon = InvalidTransactionId;
218  ReplicationSlot *slot;
220  MemoryContext old_context;
221 
222  /* shorter lines... */
223  slot = MyReplicationSlot;
224 
225  /* first some sanity checks that are unlikely to be violated */
226  if (slot == NULL)
227  elog(ERROR, "cannot perform logical decoding without an acquired slot");
228 
229  if (plugin == NULL)
230  elog(ERROR, "cannot initialize logical decoding without a specified plugin");
231 
232  /* Make sure the passed slot is suitable. These are user facing errors. */
233  if (SlotIsPhysical(slot))
234  ereport(ERROR,
235  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
236  errmsg("cannot use physical replication slot for logical decoding")));
237 
238  if (slot->data.database != MyDatabaseId)
239  ereport(ERROR,
240  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
241  errmsg("replication slot \"%s\" was not created in this database",
242  NameStr(slot->data.name))));
243 
244  if (IsTransactionState() &&
246  ereport(ERROR,
247  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
248  errmsg("cannot create logical replication slot in transaction that has performed writes")));
249 
250  /* register output plugin name with slot */
251  SpinLockAcquire(&slot->mutex);
252  StrNCpy(NameStr(slot->data.plugin), plugin, NAMEDATALEN);
253  SpinLockRelease(&slot->mutex);
254 
256 
257  /* ----
258  * This is a bit tricky: We need to determine a safe xmin horizon to start
259  * decoding from, to avoid starting from a running xacts record referring
260  * to xids whose rows have been vacuumed or pruned
261  * already. GetOldestSafeDecodingTransactionId() returns such a value, but
262  * without further interlock its return value might immediately be out of
263  * date.
264  *
265  * So we have to acquire the ProcArrayLock to prevent computation of new
266  * xmin horizons by other backends, get the safe decoding xid, and inform
267  * the slot machinery about the new limit. Once that's done the
268  * ProcArrayLock can be released as the slot machinery now is
269  * protecting against vacuum.
270  * ----
271  */
272  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
273 
276 
278 
279  LWLockRelease(ProcArrayLock);
280 
281  /*
282  * tell the snapshot builder to only assemble snapshot once reaching the
283  * running_xact's record with the respective xmin.
284  */
285  xmin_horizon = slot->data.catalog_xmin;
286 
289 
290  ctx = StartupDecodingContext(NIL, InvalidXLogRecPtr, xmin_horizon,
291  read_page, prepare_write, do_write);
292 
293  /* call output plugin initialization callback */
294  old_context = MemoryContextSwitchTo(ctx->context);
295  if (ctx->callbacks.startup_cb != NULL)
296  startup_cb_wrapper(ctx, &ctx->options, true);
297  MemoryContextSwitchTo(old_context);
298 
299  return ctx;
300 }
301 
302 /*
303  * Create a new decoding context, for a logical slot that has previously been
304  * used already.
305  *
306  * start_lsn
307  * The LSN at which to start decoding. If InvalidXLogRecPtr, restart
308  * from the slot's confirmed_flush; otherwise, start from the specified
309  * location (but move it forwards to confirmed_flush if it's older than
310  * that, see below).
311  *
312  * output_plugin_options
313  * contains options passed to the output plugin.
314  *
315  * read_page, prepare_write, do_write
316  * callbacks that have to be filled to perform the use-case dependent,
317  * actual work.
318  *
319  * Needs to be called while in a memory context that's at least as long lived
320  * as the decoding context because further memory contexts will be created
321  * inside it.
322  *
323  * Returns an initialized decoding context after calling the output plugin's
324  * startup function.
325  */
328  List *output_plugin_options,
329  XLogPageReadCB read_page,
332 {
334  ReplicationSlot *slot;
335  MemoryContext old_context;
336 
337  /* shorter lines... */
338  slot = MyReplicationSlot;
339 
340  /* first some sanity checks that are unlikely to be violated */
341  if (slot == NULL)
342  elog(ERROR, "cannot perform logical decoding without an acquired slot");
343 
344  /* make sure the passed slot is suitable, these are user facing errors */
345  if (SlotIsPhysical(slot))
346  ereport(ERROR,
347  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
348  (errmsg("cannot use physical replication slot for logical decoding"))));
349 
350  if (slot->data.database != MyDatabaseId)
351  ereport(ERROR,
352  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
353  (errmsg("replication slot \"%s\" was not created in this database",
354  NameStr(slot->data.name)))));
355 
356  if (start_lsn == InvalidXLogRecPtr)
357  {
358  /* continue from last position */
359  start_lsn = slot->data.confirmed_flush;
360  }
361  else if (start_lsn < slot->data.confirmed_flush)
362  {
363  /*
364  * It might seem like we should error out in this case, but it's
365  * pretty common for a client to acknowledge a LSN it doesn't have to
366  * do anything for, and thus didn't store persistently, because the
367  * xlog records didn't result in anything relevant for logical
368  * decoding. Clients have to be able to do that to support synchronous
369  * replication.
370  */
371  elog(DEBUG1, "cannot stream from %X/%X, minimum is %X/%X, forwarding",
372  (uint32) (start_lsn >> 32), (uint32) start_lsn,
373  (uint32) (slot->data.confirmed_flush >> 32),
374  (uint32) slot->data.confirmed_flush);
375 
376  start_lsn = slot->data.confirmed_flush;
377  }
378 
379  ctx = StartupDecodingContext(output_plugin_options,
380  start_lsn, InvalidTransactionId,
381  read_page, prepare_write, do_write);
382 
383  /* call output plugin initialization callback */
384  old_context = MemoryContextSwitchTo(ctx->context);
385  if (ctx->callbacks.startup_cb != NULL)
386  startup_cb_wrapper(ctx, &ctx->options, false);
387  MemoryContextSwitchTo(old_context);
388 
389  ereport(LOG,
390  (errmsg("starting logical decoding for slot \"%s\"",
391  NameStr(slot->data.name)),
392  errdetail("streaming transactions committing after %X/%X, reading WAL from %X/%X",
393  (uint32) (slot->data.confirmed_flush >> 32),
394  (uint32) slot->data.confirmed_flush,
395  (uint32) (slot->data.restart_lsn >> 32),
396  (uint32) slot->data.restart_lsn)));
397 
398  return ctx;
399 }
400 
401 /*
402  * Returns true if a consistent initial decoding snapshot has been built.
403  */
404 bool
406 {
408 }
409 
410 /*
411  * Read from the decoding slot, until it is ready to start extracting changes.
412  */
413 void
415 {
416  XLogRecPtr startptr;
417 
418  /* Initialize from where to start reading WAL. */
419  startptr = ctx->slot->data.restart_lsn;
420 
421  elog(DEBUG1, "searching for logical decoding starting point, starting at %X/%X",
422  (uint32) (ctx->slot->data.restart_lsn >> 32),
423  (uint32) ctx->slot->data.restart_lsn);
424 
425  /* Wait for a consistent starting point */
426  for (;;)
427  {
428  XLogRecord *record;
429  char *err = NULL;
430 
431  /* the read_page callback waits for new WAL */
432  record = XLogReadRecord(ctx->reader, startptr, &err);
433  if (err)
434  elog(ERROR, "%s", err);
435  if (!record)
436  elog(ERROR, "no record found"); /* shouldn't happen */
437 
438  startptr = InvalidXLogRecPtr;
439 
441 
442  /* only continue till we found a consistent spot */
443  if (DecodingContextReady(ctx))
444  break;
445 
447  }
448 
449  ctx->slot->data.confirmed_flush = ctx->reader->EndRecPtr;
450 }
451 
452 /*
453  * Free a previously allocated decoding context, invoking the shutdown
454  * callback if necessary.
455  */
456 void
458 {
459  if (ctx->callbacks.shutdown_cb != NULL)
460  shutdown_cb_wrapper(ctx);
461 
464  XLogReaderFree(ctx->reader);
466 }
467 
468 /*
469  * Prepare a write using the context's output routine.
470  */
471 void
473 {
474  if (!ctx->accept_writes)
475  elog(ERROR, "writes are only accepted in commit, begin and change callbacks");
476 
477  ctx->prepare_write(ctx, ctx->write_location, ctx->write_xid, last_write);
478  ctx->prepared_write = true;
479 }
480 
481 /*
482  * Perform a write using the context's output routine.
483  */
484 void
485 OutputPluginWrite(struct LogicalDecodingContext *ctx, bool last_write)
486 {
487  if (!ctx->prepared_write)
488  elog(ERROR, "OutputPluginPrepareWrite needs to be called before OutputPluginWrite");
489 
490  ctx->write(ctx, ctx->write_location, ctx->write_xid, last_write);
491  ctx->prepared_write = false;
492 }
493 
494 /*
495  * Load the output plugin, lookup its output plugin init function, and check
496  * that it provides the required callbacks.
497  */
498 static void
500 {
501  LogicalOutputPluginInit plugin_init;
502 
503  plugin_init = (LogicalOutputPluginInit)
504  load_external_function(plugin, "_PG_output_plugin_init", false, NULL);
505 
506  if (plugin_init == NULL)
507  elog(ERROR, "output plugins have to declare the _PG_output_plugin_init symbol");
508 
509  /* ask the output plugin to fill the callback struct */
510  plugin_init(callbacks);
511 
512  if (callbacks->begin_cb == NULL)
513  elog(ERROR, "output plugins have to register a begin callback");
514  if (callbacks->change_cb == NULL)
515  elog(ERROR, "output plugins have to register a change callback");
516  if (callbacks->commit_cb == NULL)
517  elog(ERROR, "output plugins have to register a commit callback");
518 }
519 
520 static void
522 {
524 
525  /* not all callbacks have an associated LSN */
526  if (state->report_location != InvalidXLogRecPtr)
527  errcontext("slot \"%s\", output plugin \"%s\", in the %s callback, associated LSN %X/%X",
528  NameStr(state->ctx->slot->data.name),
529  NameStr(state->ctx->slot->data.plugin),
530  state->callback_name,
531  (uint32) (state->report_location >> 32),
532  (uint32) state->report_location);
533  else
534  errcontext("slot \"%s\", output plugin \"%s\", in the %s callback",
535  NameStr(state->ctx->slot->data.name),
536  NameStr(state->ctx->slot->data.plugin),
537  state->callback_name);
538 }
539 
540 static void
542 {
544  ErrorContextCallback errcallback;
545 
546  /* Push callback + info on the error context stack */
547  state.ctx = ctx;
548  state.callback_name = "startup";
551  errcallback.arg = (void *) &state;
552  errcallback.previous = error_context_stack;
553  error_context_stack = &errcallback;
554 
555  /* set output state */
556  ctx->accept_writes = false;
557 
558  /* do the actual work: call callback */
559  ctx->callbacks.startup_cb(ctx, opt, is_init);
560 
561  /* Pop the error context stack */
562  error_context_stack = errcallback.previous;
563 }
564 
565 static void
567 {
569  ErrorContextCallback errcallback;
570 
571  /* Push callback + info on the error context stack */
572  state.ctx = ctx;
573  state.callback_name = "shutdown";
576  errcallback.arg = (void *) &state;
577  errcallback.previous = error_context_stack;
578  error_context_stack = &errcallback;
579 
580  /* set output state */
581  ctx->accept_writes = false;
582 
583  /* do the actual work: call callback */
584  ctx->callbacks.shutdown_cb(ctx);
585 
586  /* Pop the error context stack */
587  error_context_stack = errcallback.previous;
588 }
589 
590 
591 /*
592  * Callbacks for ReorderBuffer which add in some more information and then call
593  * output_plugin.h plugins.
594  */
595 static void
597 {
598  LogicalDecodingContext *ctx = cache->private_data;
600  ErrorContextCallback errcallback;
601 
602  /* Push callback + info on the error context stack */
603  state.ctx = ctx;
604  state.callback_name = "begin";
605  state.report_location = txn->first_lsn;
607  errcallback.arg = (void *) &state;
608  errcallback.previous = error_context_stack;
609  error_context_stack = &errcallback;
610 
611  /* set output state */
612  ctx->accept_writes = true;
613  ctx->write_xid = txn->xid;
614  ctx->write_location = txn->first_lsn;
615 
616  /* do the actual work: call callback */
617  ctx->callbacks.begin_cb(ctx, txn);
618 
619  /* Pop the error context stack */
620  error_context_stack = errcallback.previous;
621 }
622 
623 static void
625  XLogRecPtr commit_lsn)
626 {
627  LogicalDecodingContext *ctx = cache->private_data;
629  ErrorContextCallback errcallback;
630 
631  /* Push callback + info on the error context stack */
632  state.ctx = ctx;
633  state.callback_name = "commit";
634  state.report_location = txn->final_lsn; /* beginning of commit record */
636  errcallback.arg = (void *) &state;
637  errcallback.previous = error_context_stack;
638  error_context_stack = &errcallback;
639 
640  /* set output state */
641  ctx->accept_writes = true;
642  ctx->write_xid = txn->xid;
643  ctx->write_location = txn->end_lsn; /* points to the end of the record */
644 
645  /* do the actual work: call callback */
646  ctx->callbacks.commit_cb(ctx, txn, commit_lsn);
647 
648  /* Pop the error context stack */
649  error_context_stack = errcallback.previous;
650 }
651 
652 static void
654  Relation relation, ReorderBufferChange *change)
655 {
656  LogicalDecodingContext *ctx = cache->private_data;
658  ErrorContextCallback errcallback;
659 
660  /* Push callback + info on the error context stack */
661  state.ctx = ctx;
662  state.callback_name = "change";
663  state.report_location = change->lsn;
665  errcallback.arg = (void *) &state;
666  errcallback.previous = error_context_stack;
667  error_context_stack = &errcallback;
668 
669  /* set output state */
670  ctx->accept_writes = true;
671  ctx->write_xid = txn->xid;
672 
673  /*
674  * report this change's lsn so replies from clients can give an up2date
675  * answer. This won't ever be enough (and shouldn't be!) to confirm
676  * receipt of this transaction, but it might allow another transaction's
677  * commit to be confirmed with one message.
678  */
679  ctx->write_location = change->lsn;
680 
681  ctx->callbacks.change_cb(ctx, txn, relation, change);
682 
683  /* Pop the error context stack */
684  error_context_stack = errcallback.previous;
685 }
686 
687 bool
689 {
691  ErrorContextCallback errcallback;
692  bool ret;
693 
694  /* Push callback + info on the error context stack */
695  state.ctx = ctx;
696  state.callback_name = "filter_by_origin";
699  errcallback.arg = (void *) &state;
700  errcallback.previous = error_context_stack;
701  error_context_stack = &errcallback;
702 
703  /* set output state */
704  ctx->accept_writes = false;
705 
706  /* do the actual work: call callback */
707  ret = ctx->callbacks.filter_by_origin_cb(ctx, origin_id);
708 
709  /* Pop the error context stack */
710  error_context_stack = errcallback.previous;
711 
712  return ret;
713 }
714 
715 static void
717  XLogRecPtr message_lsn, bool transactional,
718  const char *prefix, Size message_size, const char *message)
719 {
720  LogicalDecodingContext *ctx = cache->private_data;
722  ErrorContextCallback errcallback;
723 
724  if (ctx->callbacks.message_cb == NULL)
725  return;
726 
727  /* Push callback + info on the error context stack */
728  state.ctx = ctx;
729  state.callback_name = "message";
730  state.report_location = message_lsn;
732  errcallback.arg = (void *) &state;
733  errcallback.previous = error_context_stack;
734  error_context_stack = &errcallback;
735 
736  /* set output state */
737  ctx->accept_writes = true;
738  ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
739  ctx->write_location = message_lsn;
740 
741  /* do the actual work: call callback */
742  ctx->callbacks.message_cb(ctx, txn, message_lsn, transactional, prefix,
743  message_size, message);
744 
745  /* Pop the error context stack */
746  error_context_stack = errcallback.previous;
747 }
748 
749 /*
750  * Set the required catalog xmin horizon for historic snapshots in the current
751  * replication slot.
752  *
753  * Note that in the most cases, we won't be able to immediately use the xmin
754  * to increase the xmin horizon: we need to wait till the client has confirmed
755  * receiving current_lsn with LogicalConfirmReceivedLocation().
756  */
757 void
759 {
760  bool updated_xmin = false;
761  ReplicationSlot *slot;
762 
763  slot = MyReplicationSlot;
764 
765  Assert(slot != NULL);
766 
767  SpinLockAcquire(&slot->mutex);
768 
769  /*
770  * don't overwrite if we already have a newer xmin. This can happen if we
771  * restart decoding in a slot.
772  */
774  {
775  }
776 
777  /*
778  * If the client has already confirmed up to this lsn, we directly can
779  * mark this as accepted. This can happen if we restart decoding in a
780  * slot.
781  */
782  else if (current_lsn <= slot->data.confirmed_flush)
783  {
784  slot->candidate_catalog_xmin = xmin;
785  slot->candidate_xmin_lsn = current_lsn;
786 
787  /* our candidate can directly be used */
788  updated_xmin = true;
789  }
790 
791  /*
792  * Only increase if the previous values have been applied, otherwise we
793  * might never end up updating if the receiver acks too slowly.
794  */
795  else if (slot->candidate_xmin_lsn == InvalidXLogRecPtr)
796  {
797  slot->candidate_catalog_xmin = xmin;
798  slot->candidate_xmin_lsn = current_lsn;
799  }
800  SpinLockRelease(&slot->mutex);
801 
802  /* candidate already valid with the current flush position, apply */
803  if (updated_xmin)
805 }
806 
807 /*
808  * Mark the minimal LSN (restart_lsn) we need to read to replay all
809  * transactions that have not yet committed at current_lsn.
810  *
811  * Just like IncreaseRestartDecodingForSlot this only takes effect when the
812  * client has confirmed to have received current_lsn.
813  */
814 void
816 {
817  bool updated_lsn = false;
818  ReplicationSlot *slot;
819 
820  slot = MyReplicationSlot;
821 
822  Assert(slot != NULL);
823  Assert(restart_lsn != InvalidXLogRecPtr);
824  Assert(current_lsn != InvalidXLogRecPtr);
825 
826  SpinLockAcquire(&slot->mutex);
827 
828  /* don't overwrite if have a newer restart lsn */
829  if (restart_lsn <= slot->data.restart_lsn)
830  {
831  }
832 
833  /*
834  * We might have already flushed far enough to directly accept this lsn,
835  * in this case there is no need to check for existing candidate LSNs
836  */
837  else if (current_lsn <= slot->data.confirmed_flush)
838  {
839  slot->candidate_restart_valid = current_lsn;
840  slot->candidate_restart_lsn = restart_lsn;
841 
842  /* our candidate can directly be used */
843  updated_lsn = true;
844  }
845 
846  /*
847  * Only increase if the previous values have been applied, otherwise we
848  * might never end up updating if the receiver acks too slowly. A missed
849  * value here will just cause some extra effort after reconnecting.
850  */
852  {
853  slot->candidate_restart_valid = current_lsn;
854  slot->candidate_restart_lsn = restart_lsn;
855 
856  elog(DEBUG1, "got new restart lsn %X/%X at %X/%X",
857  (uint32) (restart_lsn >> 32), (uint32) restart_lsn,
858  (uint32) (current_lsn >> 32), (uint32) current_lsn);
859  }
860  else
861  {
862  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",
863  (uint32) (restart_lsn >> 32), (uint32) restart_lsn,
864  (uint32) (current_lsn >> 32), (uint32) current_lsn,
865  (uint32) (slot->candidate_restart_lsn >> 32),
867  (uint32) (slot->candidate_restart_valid >> 32),
869  (uint32) (slot->data.confirmed_flush >> 32),
870  (uint32) slot->data.confirmed_flush
871  );
872  }
873  SpinLockRelease(&slot->mutex);
874 
875  /* candidates are already valid with the current flush position, apply */
876  if (updated_lsn)
878 }
879 
880 /*
881  * Handle a consumer's confirmation having received all changes up to lsn.
882  */
883 void
885 {
886  Assert(lsn != InvalidXLogRecPtr);
887 
888  /* Do an unlocked check for candidate_lsn first. */
891  {
892  bool updated_xmin = false;
893  bool updated_restart = false;
894 
896 
898 
899  /* if we're past the location required for bumping xmin, do so */
902  {
903  /*
904  * We have to write the changed xmin to disk *before* we change
905  * the in-memory value, otherwise after a crash we wouldn't know
906  * that some catalog tuples might have been removed already.
907  *
908  * Ensure that by first writing to ->xmin and only update
909  * ->effective_xmin once the new state is synced to disk. After a
910  * crash ->effective_xmin is set to ->xmin.
911  */
914  {
918  updated_xmin = true;
919  }
920  }
921 
924  {
926 
930  updated_restart = true;
931  }
932 
934 
935  /* first write new xmin to disk, so we know what's up after a crash */
936  if (updated_xmin || updated_restart)
937  {
940  elog(DEBUG1, "updated xmin: %u restart: %u", updated_xmin, updated_restart);
941  }
942 
943  /*
944  * Now the new xmin is safely on disk, we can let the global value
945  * advance. We do not take ProcArrayLock or similar since we only
946  * advance xmin here and there's not much harm done by a concurrent
947  * computation missing that.
948  */
949  if (updated_xmin)
950  {
954 
957  }
958  }
959  else
960  {
964  }
965 }
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:804
TransactionId candidate_catalog_xmin
Definition: slot.h:127
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
ReorderBufferApplyChangeCB apply_change
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
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:653
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:47
uint32 TransactionId
Definition: c.h:394
SnapBuildState SnapBuildCurrentState(SnapBuild *builder)
Definition: snapbuild.c:364
static void LoadOutputPlugin(OutputPluginCallbacks *callbacks, char *plugin)
Definition: logical.c:499
bool DecodingContextReady(LogicalDecodingContext *ctx)
Definition: logical.c:405
struct LogicalErrorCallbackState LogicalErrorCallbackState
static void commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: logical.c:624
OutputPluginOptions options
Definition: logical.h:41
void LogicalIncreaseRestartDecodingForSlot(XLogRecPtr current_lsn, XLogRecPtr restart_lsn)
Definition: logical.c:815
int wal_level
Definition: xlog.c:103
StringInfo makeStringInfo(void)
Definition: stringinfo.c:29
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:4320
MemoryContext context
Definition: logical.h:32
void ReplicationSlotSave(void)
Definition: slot.c:562
List * output_plugin_options
Definition: logical.h:46
ReorderBufferCommitCB commit
ReplicationSlotPersistentData data
Definition: slot.h:115
void * private_data
Definition: xlogreader.h:108
#define LOG
Definition: elog.h:26
bool RecoveryInProgress(void)
Definition: xlog.c:7805
struct ErrorContextCallback * previous
Definition: elog.h:238
#define SlotIsPhysical(slot)
Definition: slot.h:133
LogicalOutputPluginWriterWrite LogicalOutputPluginWriterPrepareWrite
Definition: logical.h:27
const char * callback_name
Definition: logical.c:51
XLogRecord * XLogReadRecord(XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
Definition: xlogreader.c:193
XLogRecPtr confirmed_flush
Definition: slot.h:76
PGXACT * MyPgXact
Definition: proc.c:68
XLogRecPtr write_location
Definition: logical.h:74
XLogRecPtr EndRecPtr
Definition: xlogreader.h:115
uint8 vacuumFlags
Definition: proc.h:208
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1714
ErrorContextCallback * error_context_stack
Definition: elog.c:88
#define NAMEDATALEN
#define SpinLockAcquire(lock)
Definition: spin.h:62
void ReplicationSlotReserveWal(void)
Definition: slot.c:824
void DecodingContextFindStartpoint(LogicalDecodingContext *ctx)
Definition: logical.c:414
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:664
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:541
LogicalOutputPluginWriterPrepareWrite prepare_write
Definition: logical.h:51
LogicalDecodeCommitCB commit_cb
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:145
static void shutdown_cb_wrapper(LogicalDecodingContext *ctx)
Definition: logical.c:566
XLogRecPtr candidate_restart_valid
Definition: slot.h:129
LogicalDecodingContext * CreateDecodingContext(XLogRecPtr start_lsn, List *output_plugin_options, XLogPageReadCB read_page, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write)
Definition: logical.c:327
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:65
#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:688
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:403
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:440
void * palloc0(Size size)
Definition: mcxt.c:920
LogicalDecodeChangeCB change_cb
Definition: output_plugin.h:99
TransactionId effective_catalog_xmin
Definition: slot.h:112
TransactionId xid
Oid MyDatabaseId
Definition: globals.c:76
#define InvalidOid
Definition: postgres_ext.h:36
void FreeSnapshotBuilder(SnapBuild *builder)
Definition: snapbuild.c:320
static void begin_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
Definition: logical.c:596
void OutputPluginPrepareWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:472
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:95
#define NULL
Definition: c.h:226
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:671
#define StrNCpy(dst, src, len)
Definition: c.h:826
Definition: regguts.h:298
void FreeDecodingContext(LogicalDecodingContext *ctx)
Definition: logical.c:457
XLogRecPtr restart_lsn
Definition: slot.h:68
void LogicalIncreaseXminForSlot(XLogRecPtr current_lsn, TransactionId xmin)
Definition: logical.c:758
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:353
LogicalDecodeShutdownCB shutdown_cb
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1110
bool IsTransactionState(void)
Definition: xact.c:349
ReorderBuffer * ReorderBufferAllocate(void)
void LogicalConfirmReceivedLocation(XLogRecPtr lsn)
Definition: logical.c:884
LogicalDecodeStartupCB startup_cb
Definition: output_plugin.h:97
TransactionId GetOldestSafeDecodingTransactionId(void)
Definition: procarray.c:2149
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:128
XLogRecPtr report_location
Definition: logical.c:52
void(* callback)(void *arg)
Definition: elog.h:239
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:485
LogicalOutputPluginWriterWrite write
Definition: logical.h:52
StringInfo out
Definition: logical.h:57
LogicalDecodingContext * CreateInitDecodingContext(char *plugin, List *output_plugin_options, XLogPageReadCB read_page, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write)
Definition: logical.c:211
#define errcontext
Definition: elog.h:164
#define NameStr(name)
Definition: c.h:495
void * arg
LogicalDecodeBeginCB begin_cb
Definition: output_plugin.h:98
#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:716
#define elog
Definition: elog.h:219
slock_t mutex
Definition: slot.h:88
#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:521
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:616
XLogRecPtr candidate_restart_lsn
Definition: slot.h:130
void ReplicationSlotMarkDirty(void)
Definition: slot.c:580