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