Debian Codegraph @

src/backend/replication/logical/logical.c

raw
1 /*-------------------------------------------------------------------------
2 * logical.c
3 * PostgreSQL logical decoding coordination
4 *
5 * Copyright (c) 2012-2025, PostgreSQL Global Development Group
6 *
7 * IDENTIFICATION
8 * src/backend/replication/logical/logical.c
9 *
10 * NOTES
11 * This file coordinates interaction between the various modules that
12 * together provide logical decoding, primarily by providing so
13 * called LogicalDecodingContexts. The goal is to encapsulate most of the
14 * internal complexity for consumers of logical decoding, so they can
15 * create and consume a changestream with a low amount of code. Builtin
16 * consumers are the walsender and SQL SRF interface, but it's possible to
17 * add further ones without changing core code, e.g. to consume changes in
18 * a bgworker.
19 *
20 * The idea is that a consumer provides three callbacks, one to read WAL,
21 * one to prepare a data write, and a final one for actually writing since
22 * their implementation depends on the type of consumer. Check
23 * logicalfuncs.c for an example implementation of a fairly simple consumer
24 * and an implementation of a WAL reading callback that's suitable for
25 * simple consumers.
26 *-------------------------------------------------------------------------
27 */
28
29 #include "postgres.h"
30
31 #include "access/xact.h"
32 #include "access/xlog_internal.h"
33 #include "access/xlogutils.h"
34 #include "fmgr.h"
35 #include "miscadmin.h"
36 #include "pgstat.h"
37 #include "replication/decode.h"
38 #include "replication/logical.h"
39 #include "replication/reorderbuffer.h"
40 #include "replication/slotsync.h"
41 #include "replication/snapbuild.h"
42 #include "storage/proc.h"
43 #include "storage/procarray.h"
44 #include "utils/builtins.h"
45 #include "utils/guc.h"
46 #include "utils/injection_point.h"
47 #include "utils/inval.h"
48 #include "utils/memutils.h"
49 #include "utils/varlena.h"
50
51 /* data for errcontext callback */
52 typedef struct LogicalErrorCallbackState
53 {
54 LogicalDecodingContext *ctx;
55 const char *callback_name;
56 XLogRecPtr report_location;
57 } LogicalErrorCallbackState;
58
59 /* GUC variables */
60 char *output_plugin_libraries_string;
61
62 /* wrappers around output plugin callbacks */
63 static void output_plugin_error_callback(void *arg);
64 static void startup_cb_wrapper(LogicalDecodingContext *ctx, OutputPluginOptions *opt,
65 bool is_init);
66 static void shutdown_cb_wrapper(LogicalDecodingContext *ctx);
67 static void begin_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn);
68 static void commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
69 XLogRecPtr commit_lsn);
70 static void begin_prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn);
71 static void prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
72 XLogRecPtr prepare_lsn);
73 static void commit_prepared_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
74 XLogRecPtr commit_lsn);
75 static void rollback_prepared_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
76 XLogRecPtr prepare_end_lsn, TimestampTz prepare_time);
77 static void change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
78 Relation relation, ReorderBufferChange *change);
79 static void truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
80 int nrelations, Relation relations[], ReorderBufferChange *change);
81 static void message_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
82 XLogRecPtr message_lsn, bool transactional,
83 const char *prefix, Size message_size, const char *message);
84
85 /* streaming callbacks */
86 static void stream_start_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
87 XLogRecPtr first_lsn);
88 static void stream_stop_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
89 XLogRecPtr last_lsn);
90 static void stream_abort_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
91 XLogRecPtr abort_lsn);
92 static void stream_prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
93 XLogRecPtr prepare_lsn);
94 static void stream_commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
95 XLogRecPtr commit_lsn);
96 static void stream_change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
97 Relation relation, ReorderBufferChange *change);
98 static void stream_message_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
99 XLogRecPtr message_lsn, bool transactional,
100 const char *prefix, Size message_size, const char *message);
101 static void stream_truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
102 int nrelations, Relation relations[], ReorderBufferChange *change);
103
104 /* callback to update txn's progress */
105 static void update_progress_txn_cb_wrapper(ReorderBuffer *cache,
106 ReorderBufferTXN *txn,
107 XLogRecPtr lsn);
108
109 static void LoadOutputPlugin(OutputPluginCallbacks *callbacks, const char *plugin);
110
111 /*
112 * Make sure the current settings & environment are capable of doing logical
113 * decoding.
114 */
115 void
116 CheckLogicalDecodingRequirements(void)
117 {
118 CheckSlotRequirements();
119
120 /*
121 * NB: Adding a new requirement likely means that RestoreSlotFromDisk()
122 * needs the same check.
123 */
124
125 if (wal_level < WAL_LEVEL_LOGICAL)
126 ereport(ERROR,
127 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
128 errmsg("logical decoding requires \"wal_level\" >= \"logical\"")));
129
130 if (MyDatabaseId == InvalidOid)
131 ereport(ERROR,
132 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
133 errmsg("logical decoding requires a database connection")));
134
135 if (RecoveryInProgress())
136 {
137 /*
138 * This check may have race conditions, but whenever
139 * XLOG_PARAMETER_CHANGE indicates that wal_level has changed, we
140 * verify that there are no existing logical replication slots. And to
141 * avoid races around creating a new slot,
142 * CheckLogicalDecodingRequirements() is called once before creating
143 * the slot, and once when logical decoding is initially starting up.
144 */
145 if (GetActiveWalLevelOnStandby() < WAL_LEVEL_LOGICAL)
146 ereport(ERROR,
147 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
148 errmsg("logical decoding on standby requires \"wal_level\" >= \"logical\" on the primary")));
149 }
150 }
151
152 /*
153 * Helper function for CreateInitDecodingContext() and
154 * CreateDecodingContext() performing common tasks.
155 */
156 static LogicalDecodingContext *
157 StartupDecodingContext(List *output_plugin_options,
158 XLogRecPtr start_lsn,
159 TransactionId xmin_horizon,
160 bool need_full_snapshot,
161 bool fast_forward,
162 bool in_create,
163 XLogReaderRoutine *xl_routine,
164 LogicalOutputPluginWriterPrepareWrite prepare_write,
165 LogicalOutputPluginWriterWrite do_write,
166 LogicalOutputPluginWriterUpdateProgress update_progress)
167 {
168 ReplicationSlot *slot;
169 MemoryContext context,
170 old_context;
171 LogicalDecodingContext *ctx;
172
173 /* shorter lines... */
174 slot = MyReplicationSlot;
175
176 context = AllocSetContextCreate(CurrentMemoryContext,
177 "Logical decoding context",
178 ALLOCSET_DEFAULT_SIZES);
179 old_context = MemoryContextSwitchTo(context);
180 ctx = palloc0(sizeof(LogicalDecodingContext));
181
182 ctx->context = context;
183
184 /*
185 * (re-)load output plugins, so we detect a bad (removed) output plugin
186 * now.
187 */
188 if (!fast_forward)
189 {
190 /*
191 * Before loading this library, make sure it's been blessed for
192 * logical decoding.
193 */
194 const char *plugin = NameStr(slot->data.plugin);
195 bool plugin_allowed = false;
196
197 if (output_plugin_libraries_string && output_plugin_libraries_string[0])
198 {
199 /* Check this plugin against output_plugin_libraries. */
200 char *rawstring;
201 List *elemlist = NIL;
202
203 /* Need a modifiable copy */
204 rawstring = pstrdup(output_plugin_libraries_string);
205
206 if (!SplitGUCList(rawstring, ',', &elemlist))
207 {
208 /* syntax error in list */
209 ereport(LOG,
210 (errcode(ERRCODE_SYNTAX_ERROR),
211 errmsg("invalid list syntax in parameter \"%s\"",
212 "output_plugin_libraries")));
213
214 list_free(elemlist);
215 elemlist = NIL;
216 }
217
218 foreach_ptr(char, allowed, elemlist)
219 {
220 if (strcmp(allowed, plugin) == 0)
221 {
222 plugin_allowed = true;
223 break;
224 }
225 }
226
227 list_free(elemlist);
228 pfree(rawstring);
229 }
230
231 if (!plugin_allowed)
232 {
233 /*
234 * Use the same error message as check_restricted_library_name(),
235 * but provide additional context for the DBA in the logs. (The
236 * HINT will be sent to the client, but that's not a secret.)
237 */
238 ereport(ERROR,
239 errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
240 errmsg("library \"%s\" may not be used as an output plugin",
241 plugin),
242 /*- translator: The second %s is the value of the output_plugin_libraries GUC, which may contain whitespace, commas, and double-quotes (") */
243 errdetail_log("The configuration parameter \"%s\" (currently '%s') does not name this library as a trusted output plugin.",
244 "output_plugin_libraries",
245 output_plugin_libraries_string),
246 errhint("If it is safe for all REPLICATION users to use this library as an output plugin, add it to \"%s\" and reload the server configuration.",
247 "output_plugin_libraries"));
248 }
249
250 LoadOutputPlugin(&ctx->callbacks, plugin);
251 }
252
253 /*
254 * Now that the slot's xmin has been set, we can announce ourselves as a
255 * logical decoding backend which doesn't need to be checked individually
256 * when computing the xmin horizon because the xmin is enforced via
257 * replication slots.
258 *
259 * We can only do so if we're outside of a transaction (i.e. the case when
260 * streaming changes via walsender), otherwise an already setup
261 * snapshot/xid would end up being ignored. That's not a particularly
262 * bothersome restriction since the SQL interface can't be used for
263 * streaming anyway.
264 */
265 if (!IsTransactionOrTransactionBlock())
266 {
267 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
268 MyProc->statusFlags |= PROC_IN_LOGICAL_DECODING;
269 ProcGlobal->statusFlags[MyProc->pgxactoff] = MyProc->statusFlags;
270 LWLockRelease(ProcArrayLock);
271 }
272
273 ctx->slot = slot;
274
275 ctx->reader = XLogReaderAllocate(wal_segment_size, NULL, xl_routine, ctx);
276 if (!ctx->reader)
277 ereport(ERROR,
278 (errcode(ERRCODE_OUT_OF_MEMORY),
279 errmsg("out of memory"),
280 errdetail("Failed while allocating a WAL reading processor.")));
281
282 ctx->reorder = ReorderBufferAllocate();
283 ctx->snapshot_builder =
284 AllocateSnapshotBuilder(ctx->reorder, xmin_horizon, start_lsn,
285 need_full_snapshot, in_create, slot->data.two_phase_at);
286
287 ctx->reorder->private_data = ctx;
288
289 /* wrap output plugin callbacks, so we can add error context information */
290 ctx->reorder->begin = begin_cb_wrapper;
291 ctx->reorder->apply_change = change_cb_wrapper;
292 ctx->reorder->apply_truncate = truncate_cb_wrapper;
293 ctx->reorder->commit = commit_cb_wrapper;
294 ctx->reorder->message = message_cb_wrapper;
295
296 /*
297 * To support streaming, we require start/stop/abort/commit/change
298 * callbacks. The message and truncate callbacks are optional, similar to
299 * regular output plugins. We however enable streaming when at least one
300 * of the methods is enabled so that we can easily identify missing
301 * methods.
302 *
303 * We decide it here, but only check it later in the wrappers.
304 */
305 ctx->streaming = (ctx->callbacks.stream_start_cb != NULL) ||
306 (ctx->callbacks.stream_stop_cb != NULL) ||
307 (ctx->callbacks.stream_abort_cb != NULL) ||
308 (ctx->callbacks.stream_commit_cb != NULL) ||
309 (ctx->callbacks.stream_change_cb != NULL) ||
310 (ctx->callbacks.stream_message_cb != NULL) ||
311 (ctx->callbacks.stream_truncate_cb != NULL);
312
313 /*
314 * streaming callbacks
315 *
316 * stream_message and stream_truncate callbacks are optional, so we do not
317 * fail with ERROR when missing, but the wrappers simply do nothing. We
318 * must set the ReorderBuffer callbacks to something, otherwise the calls
319 * from there will crash (we don't want to move the checks there).
320 */
321 ctx->reorder->stream_start = stream_start_cb_wrapper;
322 ctx->reorder->stream_stop = stream_stop_cb_wrapper;
323 ctx->reorder->stream_abort = stream_abort_cb_wrapper;
324 ctx->reorder->stream_prepare = stream_prepare_cb_wrapper;
325 ctx->reorder->stream_commit = stream_commit_cb_wrapper;
326 ctx->reorder->stream_change = stream_change_cb_wrapper;
327 ctx->reorder->stream_message = stream_message_cb_wrapper;
328 ctx->reorder->stream_truncate = stream_truncate_cb_wrapper;
329
330
331 /*
332 * To support two-phase logical decoding, we require
333 * begin_prepare/prepare/commit-prepare/abort-prepare callbacks. The
334 * filter_prepare callback is optional. We however enable two-phase
335 * logical decoding when at least one of the methods is enabled so that we
336 * can easily identify missing methods.
337 *
338 * We decide it here, but only check it later in the wrappers.
339 */
340 ctx->twophase = (ctx->callbacks.begin_prepare_cb != NULL) ||
341 (ctx->callbacks.prepare_cb != NULL) ||
342 (ctx->callbacks.commit_prepared_cb != NULL) ||
343 (ctx->callbacks.rollback_prepared_cb != NULL) ||
344 (ctx->callbacks.stream_prepare_cb != NULL) ||
345 (ctx->callbacks.filter_prepare_cb != NULL);
346
347 /*
348 * Callback to support decoding at prepare time.
349 */
350 ctx->reorder->begin_prepare = begin_prepare_cb_wrapper;
351 ctx->reorder->prepare = prepare_cb_wrapper;
352 ctx->reorder->commit_prepared = commit_prepared_cb_wrapper;
353 ctx->reorder->rollback_prepared = rollback_prepared_cb_wrapper;
354
355 /*
356 * Callback to support updating progress during sending data of a
357 * transaction (and its subtransactions) to the output plugin.
358 */
359 ctx->reorder->update_progress_txn = update_progress_txn_cb_wrapper;
360
361 ctx->out = makeStringInfo();
362 ctx->prepare_write = prepare_write;
363 ctx->write = do_write;
364 ctx->update_progress = update_progress;
365
366 ctx->output_plugin_options = output_plugin_options;
367
368 ctx->fast_forward = fast_forward;
369
370 MemoryContextSwitchTo(old_context);
371
372 return ctx;
373 }
374
375 /*
376 * Create a new decoding context, for a new logical slot.
377 *
378 * plugin -- contains the name of the output plugin
379 * output_plugin_options -- contains options passed to the output plugin
380 * need_full_snapshot -- if true, must obtain a snapshot able to read all
381 * tables; if false, one that can read only catalogs is acceptable.
382 * restart_lsn -- if given as invalid, it's this routine's responsibility to
383 * mark WAL as reserved by setting a convenient restart_lsn for the slot.
384 * Otherwise, we set for decoding to start from the given LSN without
385 * marking WAL reserved beforehand. In that scenario, it's up to the
386 * caller to guarantee that WAL remains available.
387 * xl_routine -- XLogReaderRoutine for underlying XLogReader
388 * prepare_write, do_write, update_progress --
389 * callbacks that perform the use-case dependent, actual, work.
390 *
391 * Needs to be called while in a memory context that's at least as long lived
392 * as the decoding context because further memory contexts will be created
393 * inside it.
394 *
395 * Returns an initialized decoding context after calling the output plugin's
396 * startup function.
397 */
398 LogicalDecodingContext *
399 CreateInitDecodingContext(const char *plugin,
400 List *output_plugin_options,
401 bool need_full_snapshot,
402 XLogRecPtr restart_lsn,
403 XLogReaderRoutine *xl_routine,
404 LogicalOutputPluginWriterPrepareWrite prepare_write,
405 LogicalOutputPluginWriterWrite do_write,
406 LogicalOutputPluginWriterUpdateProgress update_progress)
407 {
408 TransactionId xmin_horizon = InvalidTransactionId;
409 ReplicationSlot *slot;
410 NameData plugin_name;
411 LogicalDecodingContext *ctx;
412 MemoryContext old_context;
413
414 /*
415 * On a standby, this check is also required while creating the slot.
416 * Check the comments in the function.
417 */
418 CheckLogicalDecodingRequirements();
419
420 /* shorter lines... */
421 slot = MyReplicationSlot;
422
423 /* first some sanity checks that are unlikely to be violated */
424 if (slot == NULL)
425 elog(ERROR, "cannot perform logical decoding without an acquired slot");
426
427 if (plugin == NULL)
428 elog(ERROR, "cannot initialize logical decoding without a specified plugin");
429
430 /* Make sure the passed slot is suitable. These are user facing errors. */
431 if (SlotIsPhysical(slot))
432 ereport(ERROR,
433 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
434 errmsg("cannot use physical replication slot for logical decoding")));
435
436 if (slot->data.database != MyDatabaseId)
437 ereport(ERROR,
438 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
439 errmsg("replication slot \"%s\" was not created in this database",
440 NameStr(slot->data.name))));
441
442 if (IsTransactionState() &&
443 GetTopTransactionIdIfAny() != InvalidTransactionId)
444 ereport(ERROR,
445 (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
446 errmsg("cannot create logical replication slot in transaction that has performed writes")));
447
448 /*
449 * Register output plugin name with slot. We need the mutex to avoid
450 * concurrent reading of a partially copied string. But we don't want any
451 * complicated code while holding a spinlock, so do namestrcpy() outside.
452 */
453 namestrcpy(&plugin_name, plugin);
454 SpinLockAcquire(&slot->mutex);
455 slot->data.plugin = plugin_name;
456 SpinLockRelease(&slot->mutex);
457
458 if (XLogRecPtrIsInvalid(restart_lsn))
459 ReplicationSlotReserveWal();
460 else
461 {
462 SpinLockAcquire(&slot->mutex);
463 slot->data.restart_lsn = restart_lsn;
464 SpinLockRelease(&slot->mutex);
465 }
466
467 /* ----
468 * This is a bit tricky: We need to determine a safe xmin horizon to start
469 * decoding from, to avoid starting from a running xacts record referring
470 * to xids whose rows have been vacuumed or pruned
471 * already. GetOldestSafeDecodingTransactionId() returns such a value, but
472 * without further interlock its return value might immediately be out of
473 * date.
474 *
475 * So we have to acquire both the ReplicationSlotControlLock and the
476 * ProcArrayLock to prevent concurrent computation and update of new xmin
477 * horizons by other backends, get the safe decoding xid, and inform the
478 * slot machinery about the new limit. Once that's done both locks can be
479 * released as the slot machinery now is protecting against vacuum.
480 *
481 * Note that, temporarily, the data, not just the catalog, xmin has to be
482 * reserved if a data snapshot is to be exported. Otherwise the initial
483 * data snapshot created here is not guaranteed to be valid. After that
484 * the data xmin doesn't need to be managed anymore and the global xmin
485 * should be recomputed. As we are fine with losing the pegged data xmin
486 * after crash - no chance a snapshot would get exported anymore - we can
487 * get away with just setting the slot's
488 * effective_xmin. ReplicationSlotRelease will reset it again.
489 *
490 * ----
491 */
492 LWLockAcquire(ReplicationSlotControlLock, LW_EXCLUSIVE);
493 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
494
495 xmin_horizon = GetOldestSafeDecodingTransactionId(!need_full_snapshot);
496
497 SpinLockAcquire(&slot->mutex);
498 slot->effective_catalog_xmin = xmin_horizon;
499 slot->data.catalog_xmin = xmin_horizon;
500 if (need_full_snapshot)
501 slot->effective_xmin = xmin_horizon;
502 SpinLockRelease(&slot->mutex);
503
504 ReplicationSlotsComputeRequiredXmin(true);
505
506 LWLockRelease(ProcArrayLock);
507 LWLockRelease(ReplicationSlotControlLock);
508
509 ReplicationSlotMarkDirty();
510 ReplicationSlotSave();
511
512 ctx = StartupDecodingContext(NIL, restart_lsn, xmin_horizon,
513 need_full_snapshot, false, true,
514 xl_routine, prepare_write, do_write,
515 update_progress);
516
517 /* call output plugin initialization callback */
518 old_context = MemoryContextSwitchTo(ctx->context);
519 if (ctx->callbacks.startup_cb != NULL)
520 startup_cb_wrapper(ctx, &ctx->options, true);
521 MemoryContextSwitchTo(old_context);
522
523 /*
524 * We allow decoding of prepared transactions when the two_phase is
525 * enabled at the time of slot creation, or when the two_phase option is
526 * given at the streaming start, provided the plugin supports all the
527 * callbacks for two-phase.
528 */
529 ctx->twophase &= slot->data.two_phase;
530
531 ctx->reorder->output_rewrites = ctx->options.receive_rewrites;
532
533 return ctx;
534 }
535
536 /*
537 * Create a new decoding context, for a logical slot that has previously been
538 * used already.
539 *
540 * start_lsn
541 * The LSN at which to start decoding. If InvalidXLogRecPtr, restart
542 * from the slot's confirmed_flush; otherwise, start from the specified
543 * location (but move it forwards to confirmed_flush if it's older than
544 * that, see below).
545 *
546 * output_plugin_options
547 * options passed to the output plugin.
548 *
549 * fast_forward
550 * bypass the generation of logical changes.
551 *
552 * xl_routine
553 * XLogReaderRoutine used by underlying xlogreader
554 *
555 * prepare_write, do_write, update_progress
556 * callbacks that have to be filled to perform the use-case dependent,
557 * actual work.
558 *
559 * Needs to be called while in a memory context that's at least as long lived
560 * as the decoding context because further memory contexts will be created
561 * inside it.
562 *
563 * Returns an initialized decoding context after calling the output plugin's
564 * startup function.
565 */
566 LogicalDecodingContext *
567 CreateDecodingContext(XLogRecPtr start_lsn,
568 List *output_plugin_options,
569 bool fast_forward,
570 XLogReaderRoutine *xl_routine,
571 LogicalOutputPluginWriterPrepareWrite prepare_write,
572 LogicalOutputPluginWriterWrite do_write,
573 LogicalOutputPluginWriterUpdateProgress update_progress)
574 {
575 LogicalDecodingContext *ctx;
576 ReplicationSlot *slot;
577 MemoryContext old_context;
578
579 /* shorter lines... */
580 slot = MyReplicationSlot;
581
582 /* first some sanity checks that are unlikely to be violated */
583 if (slot == NULL)
584 elog(ERROR, "cannot perform logical decoding without an acquired slot");
585
586 /* make sure the passed slot is suitable, these are user facing errors */
587 if (SlotIsPhysical(slot))
588 ereport(ERROR,
589 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
590 errmsg("cannot use physical replication slot for logical decoding")));
591
592 /*
593 * We need to access the system tables during decoding to build the
594 * logical changes unless we are in fast_forward mode where no changes are
595 * generated.
596 */
597 if (slot->data.database != MyDatabaseId && !fast_forward)
598 ereport(ERROR,
599 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
600 errmsg("replication slot \"%s\" was not created in this database",
601 NameStr(slot->data.name))));
602
603 /*
604 * The slots being synced from the primary can't be used for decoding as
605 * they are used after failover. However, we do allow advancing the LSNs
606 * during the synchronization of slots. See update_local_synced_slot.
607 */
608 if (RecoveryInProgress() && slot->data.synced && !IsSyncingReplicationSlots())
609 ereport(ERROR,
610 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
611 errmsg("cannot use replication slot \"%s\" for logical decoding",
612 NameStr(slot->data.name)),
613 errdetail("This replication slot is being synchronized from the primary server."),
614 errhint("Specify another replication slot."));
615
616 /* slot must be valid to allow decoding */
617 Assert(slot->data.invalidated == RS_INVAL_NONE);
618 Assert(slot->data.restart_lsn != InvalidXLogRecPtr);
619
620 if (start_lsn == InvalidXLogRecPtr)
621 {
622 /* continue from last position */
623 start_lsn = slot->data.confirmed_flush;
624 }
625 else if (start_lsn < slot->data.confirmed_flush)
626 {
627 /*
628 * It might seem like we should error out in this case, but it's
629 * pretty common for a client to acknowledge a LSN it doesn't have to
630 * do anything for, and thus didn't store persistently, because the
631 * xlog records didn't result in anything relevant for logical
632 * decoding. Clients have to be able to do that to support synchronous
633 * replication.
634 *
635 * Starting at a different LSN than requested might not catch certain
636 * kinds of client errors; so the client may wish to check that
637 * confirmed_flush_lsn matches its expectations.
638 */
639 elog(LOG, "%X/%X has been already streamed, forwarding to %X/%X",
640 LSN_FORMAT_ARGS(start_lsn),
641 LSN_FORMAT_ARGS(slot->data.confirmed_flush));
642
643 start_lsn = slot->data.confirmed_flush;
644 }
645
646 ctx = StartupDecodingContext(output_plugin_options,
647 start_lsn, InvalidTransactionId, false,
648 fast_forward, false, xl_routine, prepare_write,
649 do_write, update_progress);
650
651 /* call output plugin initialization callback */
652 old_context = MemoryContextSwitchTo(ctx->context);
653 if (ctx->callbacks.startup_cb != NULL)
654 startup_cb_wrapper(ctx, &ctx->options, false);
655 MemoryContextSwitchTo(old_context);
656
657 /*
658 * We allow decoding of prepared transactions when the two_phase is
659 * enabled at the time of slot creation, or when the two_phase option is
660 * given at the streaming start, provided the plugin supports all the
661 * callbacks for two-phase.
662 */
663 ctx->twophase &= (slot->data.two_phase || ctx->twophase_opt_given);
664
665 /* Mark slot to allow two_phase decoding if not already marked */
666 if (ctx->twophase && !slot->data.two_phase)
667 {
668 SpinLockAcquire(&slot->mutex);
669 slot->data.two_phase = true;
670 slot->data.two_phase_at = start_lsn;
671 SpinLockRelease(&slot->mutex);
672 ReplicationSlotMarkDirty();
673 ReplicationSlotSave();
674 SnapBuildSetTwoPhaseAt(ctx->snapshot_builder, start_lsn);
675 }
676
677 ctx->reorder->output_rewrites = ctx->options.receive_rewrites;
678
679 ereport(LOG,
680 (errmsg("starting logical decoding for slot \"%s\"",
681 NameStr(slot->data.name)),
682 errdetail("Streaming transactions committing after %X/%X, reading WAL from %X/%X.",
683 LSN_FORMAT_ARGS(slot->data.confirmed_flush),
684 LSN_FORMAT_ARGS(slot->data.restart_lsn))));
685
686 return ctx;
687 }
688
689 /*
690 * Returns true if a consistent initial decoding snapshot has been built.
691 */
692 bool
693 DecodingContextReady(LogicalDecodingContext *ctx)
694 {
695 return SnapBuildCurrentState(ctx->snapshot_builder) == SNAPBUILD_CONSISTENT;
696 }
697
698 /*
699 * Read from the decoding slot, until it is ready to start extracting changes.
700 */
701 void
702 DecodingContextFindStartpoint(LogicalDecodingContext *ctx)
703 {
704 ReplicationSlot *slot = ctx->slot;
705
706 /* Initialize from where to start reading WAL. */
707 XLogBeginRead(ctx->reader, slot->data.restart_lsn);
708
709 elog(DEBUG1, "searching for logical decoding starting point, starting at %X/%X",
710 LSN_FORMAT_ARGS(slot->data.restart_lsn));
711
712 /* Wait for a consistent starting point */
713 for (;;)
714 {
715 XLogRecord *record;
716 char *err = NULL;
717
718 /* the read_page callback waits for new WAL */
719 record = XLogReadRecord(ctx->reader, &err);
720 if (err)
721 elog(ERROR, "could not find logical decoding starting point: %s", err);
722 if (!record)
723 elog(ERROR, "could not find logical decoding starting point");
724
725 LogicalDecodingProcessRecord(ctx, ctx->reader);
726
727 /* only continue till we found a consistent spot */
728 if (DecodingContextReady(ctx))
729 break;
730
731 CHECK_FOR_INTERRUPTS();
732 }
733
734 SpinLockAcquire(&slot->mutex);
735 slot->data.confirmed_flush = ctx->reader->EndRecPtr;
736 if (slot->data.two_phase)
737 slot->data.two_phase_at = ctx->reader->EndRecPtr;
738 SpinLockRelease(&slot->mutex);
739 }
740
741 /*
742 * Free a previously allocated decoding context, invoking the shutdown
743 * callback if necessary.
744 */
745 void
746 FreeDecodingContext(LogicalDecodingContext *ctx)
747 {
748 if (ctx->callbacks.shutdown_cb != NULL)
749 shutdown_cb_wrapper(ctx);
750
751 ReorderBufferFree(ctx->reorder);
752 FreeSnapshotBuilder(ctx->snapshot_builder);
753 XLogReaderFree(ctx->reader);
754 MemoryContextDelete(ctx->context);
755 }
756
757 /*
758 * Prepare a write using the context's output routine.
759 */
760 void
761 OutputPluginPrepareWrite(struct LogicalDecodingContext *ctx, bool last_write)
762 {
763 if (!ctx->accept_writes)
764 elog(ERROR, "writes are only accepted in commit, begin and change callbacks");
765
766 ctx->prepare_write(ctx, ctx->write_location, ctx->write_xid, last_write);
767 ctx->prepared_write = true;
768 }
769
770 /*
771 * Perform a write using the context's output routine.
772 */
773 void
774 OutputPluginWrite(struct LogicalDecodingContext *ctx, bool last_write)
775 {
776 if (!ctx->prepared_write)
777 elog(ERROR, "OutputPluginPrepareWrite needs to be called before OutputPluginWrite");
778
779 ctx->write(ctx, ctx->write_location, ctx->write_xid, last_write);
780 ctx->prepared_write = false;
781 }
782
783 /*
784 * Update progress tracking (if supported).
785 */
786 void
787 OutputPluginUpdateProgress(struct LogicalDecodingContext *ctx,
788 bool skipped_xact)
789 {
790 if (!ctx->update_progress)
791 return;
792
793 ctx->update_progress(ctx, ctx->write_location, ctx->write_xid,
794 skipped_xact);
795 }
796
797 /*
798 * Load the output plugin, lookup its output plugin init function, and check
799 * that it provides the required callbacks. The caller must have checked that
800 * the current user has the necessary privileges to load the given plugin;
801 * standard LOAD restrictions are not applied here.
802 */
803 static void
804 LoadOutputPlugin(OutputPluginCallbacks *callbacks, const char *plugin)
805 {
806 LogicalOutputPluginInit plugin_init;
807
808 plugin_init = (LogicalOutputPluginInit)
809 load_external_function(plugin, "_PG_output_plugin_init", false, NULL);
810
811 if (plugin_init == NULL)
812 elog(ERROR, "output plugins have to declare the _PG_output_plugin_init symbol");
813
814 /* ask the output plugin to fill the callback struct */
815 plugin_init(callbacks);
816
817 if (callbacks->begin_cb == NULL)
818 elog(ERROR, "output plugins have to register a begin callback");
819 if (callbacks->change_cb == NULL)
820 elog(ERROR, "output plugins have to register a change callback");
821 if (callbacks->commit_cb == NULL)
822 elog(ERROR, "output plugins have to register a commit callback");
823 }
824
825 static void
826 output_plugin_error_callback(void *arg)
827 {
828 LogicalErrorCallbackState *state = (LogicalErrorCallbackState *) arg;
829
830 /* not all callbacks have an associated LSN */
831 if (state->report_location != InvalidXLogRecPtr)
832 errcontext("slot \"%s\", output plugin \"%s\", in the %s callback, associated LSN %X/%X",
833 NameStr(state->ctx->slot->data.name),
834 NameStr(state->ctx->slot->data.plugin),
835 state->callback_name,
836 LSN_FORMAT_ARGS(state->report_location));
837 else
838 errcontext("slot \"%s\", output plugin \"%s\", in the %s callback",
839 NameStr(state->ctx->slot->data.name),
840 NameStr(state->ctx->slot->data.plugin),
841 state->callback_name);
842 }
843
844 static void
845 startup_cb_wrapper(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
846 {
847 LogicalErrorCallbackState state;
848 ErrorContextCallback errcallback;
849
850 Assert(!ctx->fast_forward);
851
852 /* Push callback + info on the error context stack */
853 state.ctx = ctx;
854 state.callback_name = "startup";
855 state.report_location = InvalidXLogRecPtr;
856 errcallback.callback = output_plugin_error_callback;
857 errcallback.arg = &state;
858 errcallback.previous = error_context_stack;
859 error_context_stack = &errcallback;
860
861 /* set output state */
862 ctx->accept_writes = false;
863 ctx->end_xact = false;
864
865 /* do the actual work: call callback */
866 ctx->callbacks.startup_cb(ctx, opt, is_init);
867
868 /* Pop the error context stack */
869 error_context_stack = errcallback.previous;
870 }
871
872 static void
873 shutdown_cb_wrapper(LogicalDecodingContext *ctx)
874 {
875 LogicalErrorCallbackState state;
876 ErrorContextCallback errcallback;
877
878 Assert(!ctx->fast_forward);
879
880 /* Push callback + info on the error context stack */
881 state.ctx = ctx;
882 state.callback_name = "shutdown";
883 state.report_location = InvalidXLogRecPtr;
884 errcallback.callback = output_plugin_error_callback;
885 errcallback.arg = &state;
886 errcallback.previous = error_context_stack;
887 error_context_stack = &errcallback;
888
889 /* set output state */
890 ctx->accept_writes = false;
891 ctx->end_xact = false;
892
893 /* do the actual work: call callback */
894 ctx->callbacks.shutdown_cb(ctx);
895
896 /* Pop the error context stack */
897 error_context_stack = errcallback.previous;
898 }
899
900
901 /*
902 * Callbacks for ReorderBuffer which add in some more information and then call
903 * output_plugin.h plugins.
904 */
905 static void
906 begin_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
907 {
908 LogicalDecodingContext *ctx = cache->private_data;
909 LogicalErrorCallbackState state;
910 ErrorContextCallback errcallback;
911
912 Assert(!ctx->fast_forward);
913
914 /* Push callback + info on the error context stack */
915 state.ctx = ctx;
916 state.callback_name = "begin";
917 state.report_location = txn->first_lsn;
918 errcallback.callback = output_plugin_error_callback;
919 errcallback.arg = &state;
920 errcallback.previous = error_context_stack;
921 error_context_stack = &errcallback;
922
923 /* set output state */
924 ctx->accept_writes = true;
925 ctx->write_xid = txn->xid;
926 ctx->write_location = txn->first_lsn;
927 ctx->end_xact = false;
928
929 /* do the actual work: call callback */
930 ctx->callbacks.begin_cb(ctx, txn);
931
932 /* Pop the error context stack */
933 error_context_stack = errcallback.previous;
934 }
935
936 static void
937 commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
938 XLogRecPtr commit_lsn)
939 {
940 LogicalDecodingContext *ctx = cache->private_data;
941 LogicalErrorCallbackState state;
942 ErrorContextCallback errcallback;
943
944 Assert(!ctx->fast_forward);
945
946 /* Push callback + info on the error context stack */
947 state.ctx = ctx;
948 state.callback_name = "commit";
949 state.report_location = txn->final_lsn; /* beginning of commit record */
950 errcallback.callback = output_plugin_error_callback;
951 errcallback.arg = &state;
952 errcallback.previous = error_context_stack;
953 error_context_stack = &errcallback;
954
955 /* set output state */
956 ctx->accept_writes = true;
957 ctx->write_xid = txn->xid;
958 ctx->write_location = txn->end_lsn; /* points to the end of the record */
959 ctx->end_xact = true;
960
961 /* do the actual work: call callback */
962 ctx->callbacks.commit_cb(ctx, txn, commit_lsn);
963
964 /* Pop the error context stack */
965 error_context_stack = errcallback.previous;
966 }
967
968 /*
969 * The functionality of begin_prepare is quite similar to begin with the
970 * exception that this will have gid (global transaction id) information which
971 * can be used by plugin. Now, we thought about extending the existing begin
972 * but that would break the replication protocol and additionally this looks
973 * cleaner.
974 */
975 static void
976 begin_prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn)
977 {
978 LogicalDecodingContext *ctx = cache->private_data;
979 LogicalErrorCallbackState state;
980 ErrorContextCallback errcallback;
981
982 Assert(!ctx->fast_forward);
983
984 /* We're only supposed to call this when two-phase commits are supported */
985 Assert(ctx->twophase);
986
987 /* Push callback + info on the error context stack */
988 state.ctx = ctx;
989 state.callback_name = "begin_prepare";
990 state.report_location = txn->first_lsn;
991 errcallback.callback = output_plugin_error_callback;
992 errcallback.arg = &state;
993 errcallback.previous = error_context_stack;
994 error_context_stack = &errcallback;
995
996 /* set output state */
997 ctx->accept_writes = true;
998 ctx->write_xid = txn->xid;
999 ctx->write_location = txn->first_lsn;
1000 ctx->end_xact = false;
1001
1002 /*
1003 * If the plugin supports two-phase commits then begin prepare callback is
1004 * mandatory
1005 */
1006 if (ctx->callbacks.begin_prepare_cb == NULL)
1007 ereport(ERROR,
1008 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1009 errmsg("logical replication at prepare time requires a %s callback",
1010 "begin_prepare_cb")));
1011
1012 /* do the actual work: call callback */
1013 ctx->callbacks.begin_prepare_cb(ctx, txn);
1014
1015 /* Pop the error context stack */
1016 error_context_stack = errcallback.previous;
1017 }
1018
1019 static void
1020 prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
1021 XLogRecPtr prepare_lsn)
1022 {
1023 LogicalDecodingContext *ctx = cache->private_data;
1024 LogicalErrorCallbackState state;
1025 ErrorContextCallback errcallback;
1026
1027 Assert(!ctx->fast_forward);
1028
1029 /* We're only supposed to call this when two-phase commits are supported */
1030 Assert(ctx->twophase);
1031
1032 /* Push callback + info on the error context stack */
1033 state.ctx = ctx;
1034 state.callback_name = "prepare";
1035 state.report_location = txn->final_lsn; /* beginning of prepare record */
1036 errcallback.callback = output_plugin_error_callback;
1037 errcallback.arg = &state;
1038 errcallback.previous = error_context_stack;
1039 error_context_stack = &errcallback;
1040
1041 /* set output state */
1042 ctx->accept_writes = true;
1043 ctx->write_xid = txn->xid;
1044 ctx->write_location = txn->end_lsn; /* points to the end of the record */
1045 ctx->end_xact = true;
1046
1047 /*
1048 * If the plugin supports two-phase commits then prepare callback is
1049 * mandatory
1050 */
1051 if (ctx->callbacks.prepare_cb == NULL)
1052 ereport(ERROR,
1053 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1054 errmsg("logical replication at prepare time requires a %s callback",
1055 "prepare_cb")));
1056
1057 /* do the actual work: call callback */
1058 ctx->callbacks.prepare_cb(ctx, txn, prepare_lsn);
1059
1060 /* Pop the error context stack */
1061 error_context_stack = errcallback.previous;
1062 }
1063
1064 static void
1065 commit_prepared_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
1066 XLogRecPtr commit_lsn)
1067 {
1068 LogicalDecodingContext *ctx = cache->private_data;
1069 LogicalErrorCallbackState state;
1070 ErrorContextCallback errcallback;
1071
1072 Assert(!ctx->fast_forward);
1073
1074 /* We're only supposed to call this when two-phase commits are supported */
1075 Assert(ctx->twophase);
1076
1077 /* Push callback + info on the error context stack */
1078 state.ctx = ctx;
1079 state.callback_name = "commit_prepared";
1080 state.report_location = txn->final_lsn; /* beginning of commit record */
1081 errcallback.callback = output_plugin_error_callback;
1082 errcallback.arg = &state;
1083 errcallback.previous = error_context_stack;
1084 error_context_stack = &errcallback;
1085
1086 /* set output state */
1087 ctx->accept_writes = true;
1088 ctx->write_xid = txn->xid;
1089 ctx->write_location = txn->end_lsn; /* points to the end of the record */
1090 ctx->end_xact = true;
1091
1092 /*
1093 * If the plugin support two-phase commits then commit prepared callback
1094 * is mandatory
1095 */
1096 if (ctx->callbacks.commit_prepared_cb == NULL)
1097 ereport(ERROR,
1098 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1099 errmsg("logical replication at prepare time requires a %s callback",
1100 "commit_prepared_cb")));
1101
1102 /* do the actual work: call callback */
1103 ctx->callbacks.commit_prepared_cb(ctx, txn, commit_lsn);
1104
1105 /* Pop the error context stack */
1106 error_context_stack = errcallback.previous;
1107 }
1108
1109 static void
1110 rollback_prepared_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
1111 XLogRecPtr prepare_end_lsn,
1112 TimestampTz prepare_time)
1113 {
1114 LogicalDecodingContext *ctx = cache->private_data;
1115 LogicalErrorCallbackState state;
1116 ErrorContextCallback errcallback;
1117
1118 Assert(!ctx->fast_forward);
1119
1120 /* We're only supposed to call this when two-phase commits are supported */
1121 Assert(ctx->twophase);
1122
1123 /* Push callback + info on the error context stack */
1124 state.ctx = ctx;
1125 state.callback_name = "rollback_prepared";
1126 state.report_location = txn->final_lsn; /* beginning of commit record */
1127 errcallback.callback = output_plugin_error_callback;
1128 errcallback.arg = &state;
1129 errcallback.previous = error_context_stack;
1130 error_context_stack = &errcallback;
1131
1132 /* set output state */
1133 ctx->accept_writes = true;
1134 ctx->write_xid = txn->xid;
1135 ctx->write_location = txn->end_lsn; /* points to the end of the record */
1136 ctx->end_xact = true;
1137
1138 /*
1139 * If the plugin support two-phase commits then rollback prepared callback
1140 * is mandatory
1141 */
1142 if (ctx->callbacks.rollback_prepared_cb == NULL)
1143 ereport(ERROR,
1144 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1145 errmsg("logical replication at prepare time requires a %s callback",
1146 "rollback_prepared_cb")));
1147
1148 /* do the actual work: call callback */
1149 ctx->callbacks.rollback_prepared_cb(ctx, txn, prepare_end_lsn,
1150 prepare_time);
1151
1152 /* Pop the error context stack */
1153 error_context_stack = errcallback.previous;
1154 }
1155
1156 static void
1157 change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
1158 Relation relation, ReorderBufferChange *change)
1159 {
1160 LogicalDecodingContext *ctx = cache->private_data;
1161 LogicalErrorCallbackState state;
1162 ErrorContextCallback errcallback;
1163
1164 Assert(!ctx->fast_forward);
1165
1166 /* Push callback + info on the error context stack */
1167 state.ctx = ctx;
1168 state.callback_name = "change";
1169 state.report_location = change->lsn;
1170 errcallback.callback = output_plugin_error_callback;
1171 errcallback.arg = &state;
1172 errcallback.previous = error_context_stack;
1173 error_context_stack = &errcallback;
1174
1175 /* set output state */
1176 ctx->accept_writes = true;
1177 ctx->write_xid = txn->xid;
1178
1179 /*
1180 * Report this change's lsn so replies from clients can give an up-to-date
1181 * answer. This won't ever be enough (and shouldn't be!) to confirm
1182 * receipt of this transaction, but it might allow another transaction's
1183 * commit to be confirmed with one message.
1184 */
1185 ctx->write_location = change->lsn;
1186
1187 ctx->end_xact = false;
1188
1189 ctx->callbacks.change_cb(ctx, txn, relation, change);
1190
1191 /* Pop the error context stack */
1192 error_context_stack = errcallback.previous;
1193 }
1194
1195 static void
1196 truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
1197 int nrelations, Relation relations[], ReorderBufferChange *change)
1198 {
1199 LogicalDecodingContext *ctx = cache->private_data;
1200 LogicalErrorCallbackState state;
1201 ErrorContextCallback errcallback;
1202
1203 Assert(!ctx->fast_forward);
1204
1205 if (!ctx->callbacks.truncate_cb)
1206 return;
1207
1208 /* Push callback + info on the error context stack */
1209 state.ctx = ctx;
1210 state.callback_name = "truncate";
1211 state.report_location = change->lsn;
1212 errcallback.callback = output_plugin_error_callback;
1213 errcallback.arg = &state;
1214 errcallback.previous = error_context_stack;
1215 error_context_stack = &errcallback;
1216
1217 /* set output state */
1218 ctx->accept_writes = true;
1219 ctx->write_xid = txn->xid;
1220
1221 /*
1222 * Report this change's lsn so replies from clients can give an up-to-date
1223 * answer. This won't ever be enough (and shouldn't be!) to confirm
1224 * receipt of this transaction, but it might allow another transaction's
1225 * commit to be confirmed with one message.
1226 */
1227 ctx->write_location = change->lsn;
1228
1229 ctx->end_xact = false;
1230
1231 ctx->callbacks.truncate_cb(ctx, txn, nrelations, relations, change);
1232
1233 /* Pop the error context stack */
1234 error_context_stack = errcallback.previous;
1235 }
1236
1237 bool
1238 filter_prepare_cb_wrapper(LogicalDecodingContext *ctx, TransactionId xid,
1239 const char *gid)
1240 {
1241 LogicalErrorCallbackState state;
1242 ErrorContextCallback errcallback;
1243 bool ret;
1244
1245 Assert(!ctx->fast_forward);
1246
1247 /* Push callback + info on the error context stack */
1248 state.ctx = ctx;
1249 state.callback_name = "filter_prepare";
1250 state.report_location = InvalidXLogRecPtr;
1251 errcallback.callback = output_plugin_error_callback;
1252 errcallback.arg = &state;
1253 errcallback.previous = error_context_stack;
1254 error_context_stack = &errcallback;
1255
1256 /* set output state */
1257 ctx->accept_writes = false;
1258 ctx->end_xact = false;
1259
1260 /* do the actual work: call callback */
1261 ret = ctx->callbacks.filter_prepare_cb(ctx, xid, gid);
1262
1263 /* Pop the error context stack */
1264 error_context_stack = errcallback.previous;
1265
1266 return ret;
1267 }
1268
1269 bool
1270 filter_by_origin_cb_wrapper(LogicalDecodingContext *ctx, RepOriginId origin_id)
1271 {
1272 LogicalErrorCallbackState state;
1273 ErrorContextCallback errcallback;
1274 bool ret;
1275
1276 Assert(!ctx->fast_forward);
1277
1278 /* Push callback + info on the error context stack */
1279 state.ctx = ctx;
1280 state.callback_name = "filter_by_origin";
1281 state.report_location = InvalidXLogRecPtr;
1282 errcallback.callback = output_plugin_error_callback;
1283 errcallback.arg = &state;
1284 errcallback.previous = error_context_stack;
1285 error_context_stack = &errcallback;
1286
1287 /* set output state */
1288 ctx->accept_writes = false;
1289 ctx->end_xact = false;
1290
1291 /* do the actual work: call callback */
1292 ret = ctx->callbacks.filter_by_origin_cb(ctx, origin_id);
1293
1294 /* Pop the error context stack */
1295 error_context_stack = errcallback.previous;
1296
1297 return ret;
1298 }
1299
1300 static void
1301 message_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
1302 XLogRecPtr message_lsn, bool transactional,
1303 const char *prefix, Size message_size, const char *message)
1304 {
1305 LogicalDecodingContext *ctx = cache->private_data;
1306 LogicalErrorCallbackState state;
1307 ErrorContextCallback errcallback;
1308
1309 Assert(!ctx->fast_forward);
1310
1311 if (ctx->callbacks.message_cb == NULL)
1312 return;
1313
1314 /* Push callback + info on the error context stack */
1315 state.ctx = ctx;
1316 state.callback_name = "message";
1317 state.report_location = message_lsn;
1318 errcallback.callback = output_plugin_error_callback;
1319 errcallback.arg = &state;
1320 errcallback.previous = error_context_stack;
1321 error_context_stack = &errcallback;
1322
1323 /* set output state */
1324 ctx->accept_writes = true;
1325 ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
1326 ctx->write_location = message_lsn;
1327 ctx->end_xact = false;
1328
1329 /* do the actual work: call callback */
1330 ctx->callbacks.message_cb(ctx, txn, message_lsn, transactional, prefix,
1331 message_size, message);
1332
1333 /* Pop the error context stack */
1334 error_context_stack = errcallback.previous;
1335 }
1336
1337 static void
1338 stream_start_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
1339 XLogRecPtr first_lsn)
1340 {
1341 LogicalDecodingContext *ctx = cache->private_data;
1342 LogicalErrorCallbackState state;
1343 ErrorContextCallback errcallback;
1344
1345 Assert(!ctx->fast_forward);
1346
1347 /* We're only supposed to call this when streaming is supported. */
1348 Assert(ctx->streaming);
1349
1350 /* Push callback + info on the error context stack */
1351 state.ctx = ctx;
1352 state.callback_name = "stream_start";
1353 state.report_location = first_lsn;
1354 errcallback.callback = output_plugin_error_callback;
1355 errcallback.arg = &state;
1356 errcallback.previous = error_context_stack;
1357 error_context_stack = &errcallback;
1358
1359 /* set output state */
1360 ctx->accept_writes = true;
1361 ctx->write_xid = txn->xid;
1362
1363 /*
1364 * Report this message's lsn so replies from clients can give an
1365 * up-to-date answer. This won't ever be enough (and shouldn't be!) to
1366 * confirm receipt of this transaction, but it might allow another
1367 * transaction's commit to be confirmed with one message.
1368 */
1369 ctx->write_location = first_lsn;
1370
1371 ctx->end_xact = false;
1372
1373 /* in streaming mode, stream_start_cb is required */
1374 if (ctx->callbacks.stream_start_cb == NULL)
1375 ereport(ERROR,
1376 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1377 errmsg("logical streaming requires a %s callback",
1378 "stream_start_cb")));
1379
1380 ctx->callbacks.stream_start_cb(ctx, txn);
1381
1382 /* Pop the error context stack */
1383 error_context_stack = errcallback.previous;
1384 }
1385
1386 static void
1387 stream_stop_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
1388 XLogRecPtr last_lsn)
1389 {
1390 LogicalDecodingContext *ctx = cache->private_data;
1391 LogicalErrorCallbackState state;
1392 ErrorContextCallback errcallback;
1393
1394 Assert(!ctx->fast_forward);
1395
1396 /* We're only supposed to call this when streaming is supported. */
1397 Assert(ctx->streaming);
1398
1399 /* Push callback + info on the error context stack */
1400 state.ctx = ctx;
1401 state.callback_name = "stream_stop";
1402 state.report_location = last_lsn;
1403 errcallback.callback = output_plugin_error_callback;
1404 errcallback.arg = &state;
1405 errcallback.previous = error_context_stack;
1406 error_context_stack = &errcallback;
1407
1408 /* set output state */
1409 ctx->accept_writes = true;
1410 ctx->write_xid = txn->xid;
1411
1412 /*
1413 * Report this message's lsn so replies from clients can give an
1414 * up-to-date answer. This won't ever be enough (and shouldn't be!) to
1415 * confirm receipt of this transaction, but it might allow another
1416 * transaction's commit to be confirmed with one message.
1417 */
1418 ctx->write_location = last_lsn;
1419
1420 ctx->end_xact = false;
1421
1422 /* in streaming mode, stream_stop_cb is required */
1423 if (ctx->callbacks.stream_stop_cb == NULL)
1424 ereport(ERROR,
1425 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1426 errmsg("logical streaming requires a %s callback",
1427 "stream_stop_cb")));
1428
1429 ctx->callbacks.stream_stop_cb(ctx, txn);
1430
1431 /* Pop the error context stack */
1432 error_context_stack = errcallback.previous;
1433 }
1434
1435 static void
1436 stream_abort_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
1437 XLogRecPtr abort_lsn)
1438 {
1439 LogicalDecodingContext *ctx = cache->private_data;
1440 LogicalErrorCallbackState state;
1441 ErrorContextCallback errcallback;
1442
1443 Assert(!ctx->fast_forward);
1444
1445 /* We're only supposed to call this when streaming is supported. */
1446 Assert(ctx->streaming);
1447
1448 /* Push callback + info on the error context stack */
1449 state.ctx = ctx;
1450 state.callback_name = "stream_abort";
1451 state.report_location = abort_lsn;
1452 errcallback.callback = output_plugin_error_callback;
1453 errcallback.arg = &state;
1454 errcallback.previous = error_context_stack;
1455 error_context_stack = &errcallback;
1456
1457 /* set output state */
1458 ctx->accept_writes = true;
1459 ctx->write_xid = txn->xid;
1460 ctx->write_location = abort_lsn;
1461 ctx->end_xact = true;
1462
1463 /* in streaming mode, stream_abort_cb is required */
1464 if (ctx->callbacks.stream_abort_cb == NULL)
1465 ereport(ERROR,
1466 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1467 errmsg("logical streaming requires a %s callback",
1468 "stream_abort_cb")));
1469
1470 ctx->callbacks.stream_abort_cb(ctx, txn, abort_lsn);
1471
1472 /* Pop the error context stack */
1473 error_context_stack = errcallback.previous;
1474 }
1475
1476 static void
1477 stream_prepare_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
1478 XLogRecPtr prepare_lsn)
1479 {
1480 LogicalDecodingContext *ctx = cache->private_data;
1481 LogicalErrorCallbackState state;
1482 ErrorContextCallback errcallback;
1483
1484 Assert(!ctx->fast_forward);
1485
1486 /*
1487 * We're only supposed to call this when streaming and two-phase commits
1488 * are supported.
1489 */
1490 Assert(ctx->streaming);
1491 Assert(ctx->twophase);
1492
1493 /* Push callback + info on the error context stack */
1494 state.ctx = ctx;
1495 state.callback_name = "stream_prepare";
1496 state.report_location = txn->final_lsn;
1497 errcallback.callback = output_plugin_error_callback;
1498 errcallback.arg = &state;
1499 errcallback.previous = error_context_stack;
1500 error_context_stack = &errcallback;
1501
1502 /* set output state */
1503 ctx->accept_writes = true;
1504 ctx->write_xid = txn->xid;
1505 ctx->write_location = txn->end_lsn;
1506 ctx->end_xact = true;
1507
1508 /* in streaming mode with two-phase commits, stream_prepare_cb is required */
1509 if (ctx->callbacks.stream_prepare_cb == NULL)
1510 ereport(ERROR,
1511 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1512 errmsg("logical streaming at prepare time requires a %s callback",
1513 "stream_prepare_cb")));
1514
1515 ctx->callbacks.stream_prepare_cb(ctx, txn, prepare_lsn);
1516
1517 /* Pop the error context stack */
1518 error_context_stack = errcallback.previous;
1519 }
1520
1521 static void
1522 stream_commit_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
1523 XLogRecPtr commit_lsn)
1524 {
1525 LogicalDecodingContext *ctx = cache->private_data;
1526 LogicalErrorCallbackState state;
1527 ErrorContextCallback errcallback;
1528
1529 Assert(!ctx->fast_forward);
1530
1531 /* We're only supposed to call this when streaming is supported. */
1532 Assert(ctx->streaming);
1533
1534 /* Push callback + info on the error context stack */
1535 state.ctx = ctx;
1536 state.callback_name = "stream_commit";
1537 state.report_location = txn->final_lsn;
1538 errcallback.callback = output_plugin_error_callback;
1539 errcallback.arg = &state;
1540 errcallback.previous = error_context_stack;
1541 error_context_stack = &errcallback;
1542
1543 /* set output state */
1544 ctx->accept_writes = true;
1545 ctx->write_xid = txn->xid;
1546 ctx->write_location = txn->end_lsn;
1547 ctx->end_xact = true;
1548
1549 /* in streaming mode, stream_commit_cb is required */
1550 if (ctx->callbacks.stream_commit_cb == NULL)
1551 ereport(ERROR,
1552 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1553 errmsg("logical streaming requires a %s callback",
1554 "stream_commit_cb")));
1555
1556 ctx->callbacks.stream_commit_cb(ctx, txn, commit_lsn);
1557
1558 /* Pop the error context stack */
1559 error_context_stack = errcallback.previous;
1560 }
1561
1562 static void
1563 stream_change_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
1564 Relation relation, ReorderBufferChange *change)
1565 {
1566 LogicalDecodingContext *ctx = cache->private_data;
1567 LogicalErrorCallbackState state;
1568 ErrorContextCallback errcallback;
1569
1570 Assert(!ctx->fast_forward);
1571
1572 /* We're only supposed to call this when streaming is supported. */
1573 Assert(ctx->streaming);
1574
1575 /* Push callback + info on the error context stack */
1576 state.ctx = ctx;
1577 state.callback_name = "stream_change";
1578 state.report_location = change->lsn;
1579 errcallback.callback = output_plugin_error_callback;
1580 errcallback.arg = &state;
1581 errcallback.previous = error_context_stack;
1582 error_context_stack = &errcallback;
1583
1584 /* set output state */
1585 ctx->accept_writes = true;
1586 ctx->write_xid = txn->xid;
1587
1588 /*
1589 * Report this change's lsn so replies from clients can give an up-to-date
1590 * answer. This won't ever be enough (and shouldn't be!) to confirm
1591 * receipt of this transaction, but it might allow another transaction's
1592 * commit to be confirmed with one message.
1593 */
1594 ctx->write_location = change->lsn;
1595
1596 ctx->end_xact = false;
1597
1598 /* in streaming mode, stream_change_cb is required */
1599 if (ctx->callbacks.stream_change_cb == NULL)
1600 ereport(ERROR,
1601 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1602 errmsg("logical streaming requires a %s callback",
1603 "stream_change_cb")));
1604
1605 ctx->callbacks.stream_change_cb(ctx, txn, relation, change);
1606
1607 /* Pop the error context stack */
1608 error_context_stack = errcallback.previous;
1609 }
1610
1611 static void
1612 stream_message_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
1613 XLogRecPtr message_lsn, bool transactional,
1614 const char *prefix, Size message_size, const char *message)
1615 {
1616 LogicalDecodingContext *ctx = cache->private_data;
1617 LogicalErrorCallbackState state;
1618 ErrorContextCallback errcallback;
1619
1620 Assert(!ctx->fast_forward);
1621
1622 /* We're only supposed to call this when streaming is supported. */
1623 Assert(ctx->streaming);
1624
1625 /* this callback is optional */
1626 if (ctx->callbacks.stream_message_cb == NULL)
1627 return;
1628
1629 /* Push callback + info on the error context stack */
1630 state.ctx = ctx;
1631 state.callback_name = "stream_message";
1632 state.report_location = message_lsn;
1633 errcallback.callback = output_plugin_error_callback;
1634 errcallback.arg = &state;
1635 errcallback.previous = error_context_stack;
1636 error_context_stack = &errcallback;
1637
1638 /* set output state */
1639 ctx->accept_writes = true;
1640 ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId;
1641 ctx->write_location = message_lsn;
1642 ctx->end_xact = false;
1643
1644 /* do the actual work: call callback */
1645 ctx->callbacks.stream_message_cb(ctx, txn, message_lsn, transactional, prefix,
1646 message_size, message);
1647
1648 /* Pop the error context stack */
1649 error_context_stack = errcallback.previous;
1650 }
1651
1652 static void
1653 stream_truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
1654 int nrelations, Relation relations[],
1655 ReorderBufferChange *change)
1656 {
1657 LogicalDecodingContext *ctx = cache->private_data;
1658 LogicalErrorCallbackState state;
1659 ErrorContextCallback errcallback;
1660
1661 Assert(!ctx->fast_forward);
1662
1663 /* We're only supposed to call this when streaming is supported. */
1664 Assert(ctx->streaming);
1665
1666 /* this callback is optional */
1667 if (!ctx->callbacks.stream_truncate_cb)
1668 return;
1669
1670 /* Push callback + info on the error context stack */
1671 state.ctx = ctx;
1672 state.callback_name = "stream_truncate";
1673 state.report_location = change->lsn;
1674 errcallback.callback = output_plugin_error_callback;
1675 errcallback.arg = &state;
1676 errcallback.previous = error_context_stack;
1677 error_context_stack = &errcallback;
1678
1679 /* set output state */
1680 ctx->accept_writes = true;
1681 ctx->write_xid = txn->xid;
1682
1683 /*
1684 * Report this change's lsn so replies from clients can give an up-to-date
1685 * answer. This won't ever be enough (and shouldn't be!) to confirm
1686 * receipt of this transaction, but it might allow another transaction's
1687 * commit to be confirmed with one message.
1688 */
1689 ctx->write_location = change->lsn;
1690
1691 ctx->end_xact = false;
1692
1693 ctx->callbacks.stream_truncate_cb(ctx, txn, nrelations, relations, change);
1694
1695 /* Pop the error context stack */
1696 error_context_stack = errcallback.previous;
1697 }
1698
1699 static void
1700 update_progress_txn_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn,
1701 XLogRecPtr lsn)
1702 {
1703 LogicalDecodingContext *ctx = cache->private_data;
1704 LogicalErrorCallbackState state;
1705 ErrorContextCallback errcallback;
1706
1707 Assert(!ctx->fast_forward);
1708
1709 /* Push callback + info on the error context stack */
1710 state.ctx = ctx;
1711 state.callback_name = "update_progress_txn";
1712 state.report_location = lsn;
1713 errcallback.callback = output_plugin_error_callback;
1714 errcallback.arg = &state;
1715 errcallback.previous = error_context_stack;
1716 error_context_stack = &errcallback;
1717
1718 /* set output state */
1719 ctx->accept_writes = false;
1720 ctx->write_xid = txn->xid;
1721
1722 /*
1723 * Report this change's lsn so replies from clients can give an up-to-date
1724 * answer. This won't ever be enough (and shouldn't be!) to confirm
1725 * receipt of this transaction, but it might allow another transaction's
1726 * commit to be confirmed with one message.
1727 */
1728 ctx->write_location = lsn;
1729
1730 ctx->end_xact = false;
1731
1732 OutputPluginUpdateProgress(ctx, false);
1733
1734 /* Pop the error context stack */
1735 error_context_stack = errcallback.previous;
1736 }
1737
1738 /*
1739 * Set the required catalog xmin horizon for historic snapshots in the current
1740 * replication slot.
1741 *
1742 * Note that in the most cases, we won't be able to immediately use the xmin
1743 * to increase the xmin horizon: we need to wait till the client has confirmed
1744 * receiving current_lsn with LogicalConfirmReceivedLocation().
1745 */
1746 void
1747 LogicalIncreaseXminForSlot(XLogRecPtr current_lsn, TransactionId xmin)
1748 {
1749 bool updated_xmin = false;
1750 ReplicationSlot *slot;
1751 bool got_new_xmin = false;
1752
1753 slot = MyReplicationSlot;
1754
1755 Assert(slot != NULL);
1756
1757 SpinLockAcquire(&slot->mutex);
1758
1759 /*
1760 * don't overwrite if we already have a newer xmin. This can happen if we
1761 * restart decoding in a slot.
1762 */
1763 if (TransactionIdPrecedesOrEquals(xmin, slot->data.catalog_xmin))
1764 {
1765 }
1766
1767 /*
1768 * If the client has already confirmed up to this lsn, we directly can
1769 * mark this as accepted. This can happen if we restart decoding in a
1770 * slot.
1771 */
1772 else if (current_lsn <= slot->data.confirmed_flush)
1773 {
1774 slot->candidate_catalog_xmin = xmin;
1775 slot->candidate_xmin_lsn = current_lsn;
1776
1777 /* our candidate can directly be used */
1778 updated_xmin = true;
1779 }
1780
1781 /*
1782 * Only increase if the previous values have been applied, otherwise we
1783 * might never end up updating if the receiver acks too slowly.
1784 */
1785 else if (slot->candidate_xmin_lsn == InvalidXLogRecPtr)
1786 {
1787 slot->candidate_catalog_xmin = xmin;
1788 slot->candidate_xmin_lsn = current_lsn;
1789
1790 /*
1791 * Log new xmin at an appropriate log level after releasing the
1792 * spinlock.
1793 */
1794 got_new_xmin = true;
1795 }
1796 SpinLockRelease(&slot->mutex);
1797
1798 if (got_new_xmin)
1799 elog(DEBUG1, "got new catalog xmin %u at %X/%X", xmin,
1800 LSN_FORMAT_ARGS(current_lsn));
1801
1802 /* candidate already valid with the current flush position, apply */
1803 if (updated_xmin)
1804 LogicalConfirmReceivedLocation(slot->data.confirmed_flush);
1805 }
1806
1807 /*
1808 * Mark the minimal LSN (restart_lsn) we need to read to replay all
1809 * transactions that have not yet committed at current_lsn.
1810 *
1811 * Just like LogicalIncreaseXminForSlot this only takes effect when the
1812 * client has confirmed to have received current_lsn.
1813 */
1814 void
1815 LogicalIncreaseRestartDecodingForSlot(XLogRecPtr current_lsn, XLogRecPtr restart_lsn)
1816 {
1817 bool updated_lsn = false;
1818 ReplicationSlot *slot;
1819
1820 slot = MyReplicationSlot;
1821
1822 Assert(slot != NULL);
1823 Assert(restart_lsn != InvalidXLogRecPtr);
1824 Assert(current_lsn != InvalidXLogRecPtr);
1825
1826 SpinLockAcquire(&slot->mutex);
1827
1828 /* don't overwrite if have a newer restart lsn */
1829 if (restart_lsn <= slot->data.restart_lsn)
1830 {
1831 SpinLockRelease(&slot->mutex);
1832 }
1833
1834 /*
1835 * We might have already flushed far enough to directly accept this lsn,
1836 * in this case there is no need to check for existing candidate LSNs
1837 */
1838 else if (current_lsn <= slot->data.confirmed_flush)
1839 {
1840 slot->candidate_restart_valid = current_lsn;
1841 slot->candidate_restart_lsn = restart_lsn;
1842 SpinLockRelease(&slot->mutex);
1843
1844 /* our candidate can directly be used */
1845 updated_lsn = true;
1846 }
1847
1848 /*
1849 * Only increase if the previous values have been applied, otherwise we
1850 * might never end up updating if the receiver acks too slowly. A missed
1851 * value here will just cause some extra effort after reconnecting.
1852 */
1853 else if (slot->candidate_restart_valid == InvalidXLogRecPtr)
1854 {
1855 slot->candidate_restart_valid = current_lsn;
1856 slot->candidate_restart_lsn = restart_lsn;
1857 SpinLockRelease(&slot->mutex);
1858
1859 elog(DEBUG1, "got new restart lsn %X/%X at %X/%X",
1860 LSN_FORMAT_ARGS(restart_lsn),
1861 LSN_FORMAT_ARGS(current_lsn));
1862 }
1863 else
1864 {
1865 XLogRecPtr candidate_restart_lsn;
1866 XLogRecPtr candidate_restart_valid;
1867 XLogRecPtr confirmed_flush;
1868
1869 candidate_restart_lsn = slot->candidate_restart_lsn;
1870 candidate_restart_valid = slot->candidate_restart_valid;
1871 confirmed_flush = slot->data.confirmed_flush;
1872 SpinLockRelease(&slot->mutex);
1873
1874 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",
1875 LSN_FORMAT_ARGS(restart_lsn),
1876 LSN_FORMAT_ARGS(current_lsn),
1877 LSN_FORMAT_ARGS(candidate_restart_lsn),
1878 LSN_FORMAT_ARGS(candidate_restart_valid),
1879 LSN_FORMAT_ARGS(confirmed_flush));
1880 }
1881
1882 /* candidates are already valid with the current flush position, apply */
1883 if (updated_lsn)
1884 LogicalConfirmReceivedLocation(slot->data.confirmed_flush);
1885 }
1886
1887 /*
1888 * Handle a consumer's confirmation having received all changes up to lsn.
1889 */
1890 void
1891 LogicalConfirmReceivedLocation(XLogRecPtr lsn)
1892 {
1893 Assert(lsn != InvalidXLogRecPtr);
1894
1895 /* Do an unlocked check for candidate_lsn first. */
1896 if (MyReplicationSlot->candidate_xmin_lsn != InvalidXLogRecPtr ||
1897 MyReplicationSlot->candidate_restart_valid != InvalidXLogRecPtr)
1898 {
1899 bool updated_xmin = false;
1900 bool updated_restart = false;
1901 XLogRecPtr restart_lsn pg_attribute_unused();
1902
1903 SpinLockAcquire(&MyReplicationSlot->mutex);
1904
1905 /* remember the old restart lsn */
1906 restart_lsn = MyReplicationSlot->data.restart_lsn;
1907
1908 /*
1909 * Prevent moving the confirmed_flush backwards, as this could lead to
1910 * data duplication issues caused by replicating already replicated
1911 * changes.
1912 *
1913 * This can happen when a client acknowledges an LSN it doesn't have
1914 * to do anything for, and thus didn't store persistently. After a
1915 * restart, the client can send the prior LSN that it stored
1916 * persistently as an acknowledgement, but we need to ignore such an
1917 * LSN. See similar case handling in CreateDecodingContext.
1918 */
1919 if (lsn > MyReplicationSlot->data.confirmed_flush)
1920 MyReplicationSlot->data.confirmed_flush = lsn;
1921
1922 /* if we're past the location required for bumping xmin, do so */
1923 if (MyReplicationSlot->candidate_xmin_lsn != InvalidXLogRecPtr &&
1924 MyReplicationSlot->candidate_xmin_lsn <= lsn)
1925 {
1926 /*
1927 * We have to write the changed xmin to disk *before* we change
1928 * the in-memory value, otherwise after a crash we wouldn't know
1929 * that some catalog tuples might have been removed already.
1930 *
1931 * Ensure that by first writing to ->xmin and only update
1932 * ->effective_xmin once the new state is synced to disk. After a
1933 * crash ->effective_xmin is set to ->xmin.
1934 */
1935 if (TransactionIdIsValid(MyReplicationSlot->candidate_catalog_xmin) &&
1936 MyReplicationSlot->data.catalog_xmin != MyReplicationSlot->candidate_catalog_xmin)
1937 {
1938 MyReplicationSlot->data.catalog_xmin = MyReplicationSlot->candidate_catalog_xmin;
1939 MyReplicationSlot->candidate_catalog_xmin = InvalidTransactionId;
1940 MyReplicationSlot->candidate_xmin_lsn = InvalidXLogRecPtr;
1941 updated_xmin = true;
1942 }
1943 }
1944
1945 if (MyReplicationSlot->candidate_restart_valid != InvalidXLogRecPtr &&
1946 MyReplicationSlot->candidate_restart_valid <= lsn)
1947 {
1948 Assert(MyReplicationSlot->candidate_restart_lsn != InvalidXLogRecPtr);
1949
1950 MyReplicationSlot->data.restart_lsn = MyReplicationSlot->candidate_restart_lsn;
1951 MyReplicationSlot->candidate_restart_lsn = InvalidXLogRecPtr;
1952 MyReplicationSlot->candidate_restart_valid = InvalidXLogRecPtr;
1953 updated_restart = true;
1954 }
1955
1956 SpinLockRelease(&MyReplicationSlot->mutex);
1957
1958 /* first write new xmin to disk, so we know what's up after a crash */
1959 if (updated_xmin || updated_restart)
1960 {
1961 #ifdef USE_INJECTION_POINTS
1962 XLogSegNo seg1,
1963 seg2;
1964
1965 XLByteToSeg(restart_lsn, seg1, wal_segment_size);
1966 XLByteToSeg(MyReplicationSlot->data.restart_lsn, seg2, wal_segment_size);
1967
1968 /* trigger injection point, but only if segment changes */
1969 if (seg1 != seg2)
1970 INJECTION_POINT("logical-replication-slot-advance-segment", NULL);
1971 #endif
1972
1973 ReplicationSlotMarkDirty();
1974 ReplicationSlotSave();
1975 elog(DEBUG1, "updated xmin: %u restart: %u", updated_xmin, updated_restart);
1976 }
1977
1978 /*
1979 * Now the new xmin is safely on disk, we can let the global value
1980 * advance. We do not take ProcArrayLock or similar since we only
1981 * advance xmin here and there's not much harm done by a concurrent
1982 * computation missing that.
1983 */
1984 if (updated_xmin)
1985 {
1986 SpinLockAcquire(&MyReplicationSlot->mutex);
1987 MyReplicationSlot->effective_catalog_xmin = MyReplicationSlot->data.catalog_xmin;
1988 SpinLockRelease(&MyReplicationSlot->mutex);
1989
1990 ReplicationSlotsComputeRequiredXmin(false);
1991 ReplicationSlotsComputeRequiredLSN();
1992 }
1993 }
1994 else
1995 {
1996 SpinLockAcquire(&MyReplicationSlot->mutex);
1997
1998 /*
1999 * Prevent moving the confirmed_flush backwards. See comments above
2000 * for the details.
2001 */
2002 if (lsn > MyReplicationSlot->data.confirmed_flush)
2003 MyReplicationSlot->data.confirmed_flush = lsn;
2004
2005 SpinLockRelease(&MyReplicationSlot->mutex);
2006 }
2007 }
2008
2009 /*
2010 * Clear logical streaming state during (sub)transaction abort.
2011 */
2012 void
2013 ResetLogicalStreamingState(void)
2014 {
2015 CheckXidAlive = InvalidTransactionId;
2016 bsysscan = false;
2017 }
2018
2019 /*
2020 * Report stats for a slot.
2021 */
2022 void
2023 UpdateDecodingStats(LogicalDecodingContext *ctx)
2024 {
2025 ReorderBuffer *rb = ctx->reorder;
2026 PgStat_StatReplSlotEntry repSlotStat;
2027
2028 /* Nothing to do if we don't have any replication stats to be sent. */
2029 if (rb->spillBytes <= 0 && rb->streamBytes <= 0 && rb->totalBytes <= 0)
2030 return;
2031
2032 elog(DEBUG2, "UpdateDecodingStats: updating stats %p %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,
2033 rb,
2034 rb->spillTxns,
2035 rb->spillCount,
2036 rb->spillBytes,
2037 rb->streamTxns,
2038 rb->streamCount,
2039 rb->streamBytes,
2040 rb->totalTxns,
2041 rb->totalBytes);
2042
2043 repSlotStat.spill_txns = rb->spillTxns;
2044 repSlotStat.spill_count = rb->spillCount;
2045 repSlotStat.spill_bytes = rb->spillBytes;
2046 repSlotStat.stream_txns = rb->streamTxns;
2047 repSlotStat.stream_count = rb->streamCount;
2048 repSlotStat.stream_bytes = rb->streamBytes;
2049 repSlotStat.total_txns = rb->totalTxns;
2050 repSlotStat.total_bytes = rb->totalBytes;
2051
2052 pgstat_report_replslot(ctx->slot, &repSlotStat);
2053
2054 rb->spillTxns = 0;
2055 rb->spillCount = 0;
2056 rb->spillBytes = 0;
2057 rb->streamTxns = 0;
2058 rb->streamCount = 0;
2059 rb->streamBytes = 0;
2060 rb->totalTxns = 0;
2061 rb->totalBytes = 0;
2062 }
2063
2064 /*
2065 * Read up to the end of WAL starting from the decoding slot's restart_lsn.
2066 * Return true if any meaningful/decodable WAL records are encountered,
2067 * otherwise false.
2068 */
2069 bool
2070 LogicalReplicationSlotHasPendingWal(XLogRecPtr end_of_wal)
2071 {
2072 bool has_pending_wal = false;
2073
2074 Assert(MyReplicationSlot);
2075
2076 PG_TRY();
2077 {
2078 LogicalDecodingContext *ctx;
2079
2080 /*
2081 * Create our decoding context in fast_forward mode, passing start_lsn
2082 * as InvalidXLogRecPtr, so that we start processing from the slot's
2083 * confirmed_flush.
2084 */
2085 ctx = CreateDecodingContext(InvalidXLogRecPtr,
2086 NIL,
2087 true, /* fast_forward */
2088 XL_ROUTINE(.page_read = read_local_xlog_page,
2089 .segment_open = wal_segment_open,
2090 .segment_close = wal_segment_close),
2091 NULL, NULL, NULL);
2092
2093 /*
2094 * Start reading at the slot's restart_lsn, which we know points to a
2095 * valid record.
2096 */
2097 XLogBeginRead(ctx->reader, MyReplicationSlot->data.restart_lsn);
2098
2099 /* Invalidate non-timetravel entries */
2100 InvalidateSystemCaches();
2101
2102 /* Loop until the end of WAL or some changes are processed */
2103 while (!has_pending_wal && ctx->reader->EndRecPtr < end_of_wal)
2104 {
2105 XLogRecord *record;
2106 char *errm = NULL;
2107
2108 record = XLogReadRecord(ctx->reader, &errm);
2109
2110 if (errm)
2111 elog(ERROR, "could not find record for logical decoding: %s", errm);
2112
2113 if (record != NULL)
2114 LogicalDecodingProcessRecord(ctx, ctx->reader);
2115
2116 has_pending_wal = ctx->processing_required;
2117
2118 CHECK_FOR_INTERRUPTS();
2119 }
2120
2121 /* Clean up */
2122 FreeDecodingContext(ctx);
2123 InvalidateSystemCaches();
2124 }
2125 PG_CATCH();
2126 {
2127 /* clear all timetravel entries */
2128 InvalidateSystemCaches();
2129
2130 PG_RE_THROW();
2131 }
2132 PG_END_TRY();
2133
2134 return has_pending_wal;
2135 }
2136
2137 /*
2138 * Helper function for advancing our logical replication slot forward.
2139 *
2140 * The slot's restart_lsn is used as start point for reading records, while
2141 * confirmed_flush is used as base point for the decoding context.
2142 *
2143 * We cannot just do LogicalConfirmReceivedLocation to update confirmed_flush,
2144 * because we need to digest WAL to advance restart_lsn allowing to recycle
2145 * WAL and removal of old catalog tuples. As decoding is done in fast_forward
2146 * mode, no changes are generated anyway.
2147 *
2148 * *found_consistent_snapshot will be true if the initial decoding snapshot has
2149 * been built; Otherwise, it will be false.
2150 */
2151 XLogRecPtr
2152 LogicalSlotAdvanceAndCheckSnapState(XLogRecPtr moveto,
2153 bool *found_consistent_snapshot)
2154 {
2155 LogicalDecodingContext *ctx;
2156 ResourceOwner old_resowner = CurrentResourceOwner;
2157 XLogRecPtr retlsn;
2158
2159 Assert(moveto != InvalidXLogRecPtr);
2160
2161 if (found_consistent_snapshot)
2162 *found_consistent_snapshot = false;
2163
2164 PG_TRY();
2165 {
2166 /*
2167 * Create our decoding context in fast_forward mode, passing start_lsn
2168 * as InvalidXLogRecPtr, so that we start processing from my slot's
2169 * confirmed_flush.
2170 */
2171 ctx = CreateDecodingContext(InvalidXLogRecPtr,
2172 NIL,
2173 true, /* fast_forward */
2174 XL_ROUTINE(.page_read = read_local_xlog_page,
2175 .segment_open = wal_segment_open,
2176 .segment_close = wal_segment_close),
2177 NULL, NULL, NULL);
2178
2179 /*
2180 * Wait for specified streaming replication standby servers (if any)
2181 * to confirm receipt of WAL up to moveto lsn.
2182 */
2183 WaitForStandbyConfirmation(moveto);
2184
2185 /*
2186 * Start reading at the slot's restart_lsn, which we know to point to
2187 * a valid record.
2188 */
2189 XLogBeginRead(ctx->reader, MyReplicationSlot->data.restart_lsn);
2190
2191 /* invalidate non-timetravel entries */
2192 InvalidateSystemCaches();
2193
2194 /* Decode records until we reach the requested target */
2195 while (ctx->reader->EndRecPtr < moveto)
2196 {
2197 char *errm = NULL;
2198 XLogRecord *record;
2199
2200 /*
2201 * Read records. No changes are generated in fast_forward mode,
2202 * but snapbuilder/slot statuses are updated properly.
2203 */
2204 record = XLogReadRecord(ctx->reader, &errm);
2205 if (errm)
2206 elog(ERROR, "could not find record while advancing replication slot: %s",
2207 errm);
2208
2209 /*
2210 * Process the record. Storage-level changes are ignored in
2211 * fast_forward mode, but other modules (such as snapbuilder)
2212 * might still have critical updates to do.
2213 */
2214 if (record)
2215 LogicalDecodingProcessRecord(ctx, ctx->reader);
2216
2217 CHECK_FOR_INTERRUPTS();
2218 }
2219
2220 if (found_consistent_snapshot && DecodingContextReady(ctx))
2221 *found_consistent_snapshot = true;
2222
2223 /*
2224 * Logical decoding could have clobbered CurrentResourceOwner during
2225 * transaction management, so restore the executor's value. (This is
2226 * a kluge, but it's not worth cleaning up right now.)
2227 */
2228 CurrentResourceOwner = old_resowner;
2229
2230 if (ctx->reader->EndRecPtr != InvalidXLogRecPtr)
2231 {
2232 LogicalConfirmReceivedLocation(moveto);
2233
2234 /*
2235 * If only the confirmed_flush LSN has changed the slot won't get
2236 * marked as dirty by the above. Callers on the walsender
2237 * interface are expected to keep track of their own progress and
2238 * don't need it written out. But SQL-interface users cannot
2239 * specify their own start positions and it's harder for them to
2240 * keep track of their progress, so we should make more of an
2241 * effort to save it for them.
2242 *
2243 * Dirty the slot so it is written out at the next checkpoint. The
2244 * LSN position advanced to may still be lost on a crash but this
2245 * makes the data consistent after a clean shutdown.
2246 */
2247 ReplicationSlotMarkDirty();
2248 }
2249
2250 retlsn = MyReplicationSlot->data.confirmed_flush;
2251
2252 /* free context, call shutdown callback */
2253 FreeDecodingContext(ctx);
2254
2255 InvalidateSystemCaches();
2256 }
2257 PG_CATCH();
2258 {
2259 /* clear all timetravel entries */
2260 InvalidateSystemCaches();
2261
2262 PG_RE_THROW();
2263 }
2264 PG_END_TRY();
2265
2266 return retlsn;
2267 }