1 /*-------------------------------------------------------------------------
4 * Support functions for replication slots
6 * Copyright (c) 2012-2024, PostgreSQL Global Development Group
9 * src/backend/replication/slotfuncs.c
11 *-------------------------------------------------------------------------
15 #include "access/htup_details.h"
16 #include "access/xlog_internal.h"
17 #include "access/xlogrecovery.h"
18 #include "access/xlogutils.h"
20 #include "miscadmin.h"
21 #include "replication/decode.h"
22 #include "replication/logical.h"
23 #include "replication/slot.h"
24 #include "replication/slotsync.h"
25 #include "utils/builtins.h"
26 #include "utils/guc.h"
27 #include "utils/inval.h"
28 #include "utils/pg_lsn.h"
29 #include "utils/resowner.h"
32 * Helper function for creating a new physical replication slot with
33 * given arguments. Note that this function doesn't release the created
36 * If restart_lsn is a valid value, we use it without WAL reservation
37 * routine. So the caller must guarantee that WAL is available.
40 create_physical_replication_slot(char *name
, bool immediately_reserve
,
41 bool temporary
, XLogRecPtr restart_lsn
)
43 Assert(!MyReplicationSlot
);
45 /* acquire replication slot, this will check for conflicting names */
46 ReplicationSlotCreate(name
, false,
47 temporary
? RS_TEMPORARY
: RS_PERSISTENT
, false,
50 if (immediately_reserve
)
52 /* Reserve WAL as the user asked for it */
53 if (XLogRecPtrIsInvalid(restart_lsn
))
54 ReplicationSlotReserveWal();
56 MyReplicationSlot
->data
.restart_lsn
= restart_lsn
;
58 /* Write this slot to disk */
59 ReplicationSlotMarkDirty();
60 ReplicationSlotSave();
65 * SQL function for creating a new physical (streaming replication)
69 pg_create_physical_replication_slot(PG_FUNCTION_ARGS
)
71 Name name
= PG_GETARG_NAME(0);
72 bool immediately_reserve
= PG_GETARG_BOOL(1);
73 bool temporary
= PG_GETARG_BOOL(2);
80 if (get_call_result_type(fcinfo
, NULL
, &tupdesc
) != TYPEFUNC_COMPOSITE
)
81 elog(ERROR
, "return type must be a row type");
83 CheckSlotPermissions();
85 CheckSlotRequirements();
87 create_physical_replication_slot(NameStr(*name
),
92 values
[0] = NameGetDatum(&MyReplicationSlot
->data
.name
);
95 if (immediately_reserve
)
97 values
[1] = LSNGetDatum(MyReplicationSlot
->data
.restart_lsn
);
103 tuple
= heap_form_tuple(tupdesc
, values
, nulls
);
104 result
= HeapTupleGetDatum(tuple
);
106 ReplicationSlotRelease();
108 PG_RETURN_DATUM(result
);
113 * Helper function for creating a new logical replication slot with
114 * given arguments. Note that this function doesn't release the created
117 * When find_startpoint is false, the slot's confirmed_flush is not set; it's
118 * caller's responsibility to ensure it's set to something sensible.
121 create_logical_replication_slot(char *name
, char *plugin
,
122 bool temporary
, bool two_phase
,
124 XLogRecPtr restart_lsn
,
125 bool find_startpoint
)
127 LogicalDecodingContext
*ctx
= NULL
;
129 Assert(!MyReplicationSlot
);
132 * Acquire a logical decoding slot, this will check for conflicting names.
133 * Initially create persistent slot as ephemeral - that allows us to
134 * nicely handle errors during initialization because it'll get dropped if
135 * this transaction fails. We'll make it persistent at the end. Temporary
136 * slots can be created as temporary from beginning as they get dropped on
139 ReplicationSlotCreate(name
, true,
140 temporary
? RS_TEMPORARY
: RS_EPHEMERAL
, two_phase
,
144 * Create logical decoding context to find start point or, if we don't
145 * need it, to 1) bump slot's restart_lsn and xmin 2) check plugin sanity.
147 * Note: when !find_startpoint this is still important, because it's at
148 * this point that the output plugin is validated.
150 ctx
= CreateInitDecodingContext(plugin
, NIL
,
151 false, /* just catalogs is OK */
153 XL_ROUTINE(.page_read
= read_local_xlog_page
,
154 .segment_open
= wal_segment_open
,
155 .segment_close
= wal_segment_close
),
159 * If caller needs us to determine the decoding start point, do so now.
160 * This might take a while.
163 DecodingContextFindStartpoint(ctx
);
165 /* don't need the decoding context anymore */
166 FreeDecodingContext(ctx
);
170 * SQL function for creating a new logical replication slot.
173 pg_create_logical_replication_slot(PG_FUNCTION_ARGS
)
175 Name name
= PG_GETARG_NAME(0);
176 Name plugin
= PG_GETARG_NAME(1);
177 bool temporary
= PG_GETARG_BOOL(2);
178 bool two_phase
= PG_GETARG_BOOL(3);
179 bool failover
= PG_GETARG_BOOL(4);
186 if (get_call_result_type(fcinfo
, NULL
, &tupdesc
) != TYPEFUNC_COMPOSITE
)
187 elog(ERROR
, "return type must be a row type");
189 CheckSlotPermissions();
191 CheckLogicalDecodingRequirements();
193 create_logical_replication_slot(NameStr(*name
),
201 values
[0] = NameGetDatum(&MyReplicationSlot
->data
.name
);
202 values
[1] = LSNGetDatum(MyReplicationSlot
->data
.confirmed_flush
);
204 memset(nulls
, 0, sizeof(nulls
));
206 tuple
= heap_form_tuple(tupdesc
, values
, nulls
);
207 result
= HeapTupleGetDatum(tuple
);
209 /* ok, slot is now fully created, mark it as persistent if needed */
211 ReplicationSlotPersist();
212 ReplicationSlotRelease();
214 PG_RETURN_DATUM(result
);
219 * SQL function for dropping a replication slot.
222 pg_drop_replication_slot(PG_FUNCTION_ARGS
)
224 Name name
= PG_GETARG_NAME(0);
226 CheckSlotPermissions();
228 CheckSlotRequirements();
230 ReplicationSlotDrop(NameStr(*name
), true);
236 * pg_get_replication_slots - SQL SRF showing all replication slots
237 * that currently exist on the database cluster.
240 pg_get_replication_slots(PG_FUNCTION_ARGS
)
242 #define PG_GET_REPLICATION_SLOTS_COLS 19
243 ReturnSetInfo
*rsinfo
= (ReturnSetInfo
*) fcinfo
->resultinfo
;
248 * We don't require any special permission to see this function's data
249 * because nothing should be sensitive. The most critical being the slot
250 * name, which shouldn't contain anything particularly sensitive.
253 InitMaterializedSRF(fcinfo
, 0);
255 currlsn
= GetXLogWriteRecPtr();
257 LWLockAcquire(ReplicationSlotControlLock
, LW_SHARED
);
258 for (slotno
= 0; slotno
< max_replication_slots
; slotno
++)
260 ReplicationSlot
*slot
= &ReplicationSlotCtl
->replication_slots
[slotno
];
261 ReplicationSlot slot_contents
;
262 Datum values
[PG_GET_REPLICATION_SLOTS_COLS
];
263 bool nulls
[PG_GET_REPLICATION_SLOTS_COLS
];
264 WALAvailability walstate
;
266 ReplicationSlotInvalidationCause cause
;
271 /* Copy slot contents while holding spinlock, then examine at leisure */
272 SpinLockAcquire(&slot
->mutex
);
273 slot_contents
= *slot
;
274 SpinLockRelease(&slot
->mutex
);
276 memset(values
, 0, sizeof(values
));
277 memset(nulls
, 0, sizeof(nulls
));
280 values
[i
++] = NameGetDatum(&slot_contents
.data
.name
);
282 if (slot_contents
.data
.database
== InvalidOid
)
285 values
[i
++] = NameGetDatum(&slot_contents
.data
.plugin
);
287 if (slot_contents
.data
.database
== InvalidOid
)
288 values
[i
++] = CStringGetTextDatum("physical");
290 values
[i
++] = CStringGetTextDatum("logical");
292 if (slot_contents
.data
.database
== InvalidOid
)
295 values
[i
++] = ObjectIdGetDatum(slot_contents
.data
.database
);
297 values
[i
++] = BoolGetDatum(slot_contents
.data
.persistency
== RS_TEMPORARY
);
298 values
[i
++] = BoolGetDatum(slot_contents
.active_pid
!= 0);
300 if (slot_contents
.active_pid
!= 0)
301 values
[i
++] = Int32GetDatum(slot_contents
.active_pid
);
305 if (slot_contents
.data
.xmin
!= InvalidTransactionId
)
306 values
[i
++] = TransactionIdGetDatum(slot_contents
.data
.xmin
);
310 if (slot_contents
.data
.catalog_xmin
!= InvalidTransactionId
)
311 values
[i
++] = TransactionIdGetDatum(slot_contents
.data
.catalog_xmin
);
315 if (slot_contents
.data
.restart_lsn
!= InvalidXLogRecPtr
)
316 values
[i
++] = LSNGetDatum(slot_contents
.data
.restart_lsn
);
320 if (slot_contents
.data
.confirmed_flush
!= InvalidXLogRecPtr
)
321 values
[i
++] = LSNGetDatum(slot_contents
.data
.confirmed_flush
);
326 * If the slot has not been invalidated, test availability from
329 if (slot_contents
.data
.invalidated
!= RS_INVAL_NONE
)
330 walstate
= WALAVAIL_REMOVED
;
332 walstate
= GetWALAvailability(slot_contents
.data
.restart_lsn
);
336 case WALAVAIL_INVALID_LSN
:
340 case WALAVAIL_RESERVED
:
341 values
[i
++] = CStringGetTextDatum("reserved");
344 case WALAVAIL_EXTENDED
:
345 values
[i
++] = CStringGetTextDatum("extended");
348 case WALAVAIL_UNRESERVED
:
349 values
[i
++] = CStringGetTextDatum("unreserved");
352 case WALAVAIL_REMOVED
:
355 * If we read the restart_lsn long enough ago, maybe that file
356 * has been removed by now. However, the walsender could have
357 * moved forward enough that it jumped to another file after
358 * we looked. If checkpointer signalled the process to
359 * termination, then it's definitely lost; but if a process is
360 * still alive, then "unreserved" seems more appropriate.
362 * If we do change it, save the state for safe_wal_size below.
364 if (!XLogRecPtrIsInvalid(slot_contents
.data
.restart_lsn
))
368 SpinLockAcquire(&slot
->mutex
);
369 pid
= slot
->active_pid
;
370 slot_contents
.data
.restart_lsn
= slot
->data
.restart_lsn
;
371 SpinLockRelease(&slot
->mutex
);
374 values
[i
++] = CStringGetTextDatum("unreserved");
375 walstate
= WALAVAIL_UNRESERVED
;
379 values
[i
++] = CStringGetTextDatum("lost");
384 * safe_wal_size is only computed for slots that have not been lost,
385 * and only if there's a configured maximum size.
387 if (walstate
== WALAVAIL_REMOVED
|| max_slot_wal_keep_size_mb
< 0)
397 XLByteToSeg(slot_contents
.data
.restart_lsn
, targetSeg
, wal_segment_size
);
399 /* determine how many segments can be kept by slots */
400 slotKeepSegs
= XLogMBVarToSegs(max_slot_wal_keep_size_mb
, wal_segment_size
);
401 /* ditto for wal_keep_size */
402 keepSegs
= XLogMBVarToSegs(wal_keep_size_mb
, wal_segment_size
);
404 /* if currpos reaches failLSN, we lose our segment */
405 failSeg
= targetSeg
+ Max(slotKeepSegs
, keepSegs
) + 1;
406 XLogSegNoOffsetToRecPtr(failSeg
, 0, wal_segment_size
, failLSN
);
408 values
[i
++] = Int64GetDatum(failLSN
- currlsn
);
411 values
[i
++] = BoolGetDatum(slot_contents
.data
.two_phase
);
413 if (slot_contents
.inactive_since
> 0)
414 values
[i
++] = TimestampTzGetDatum(slot_contents
.inactive_since
);
418 cause
= slot_contents
.data
.invalidated
;
420 if (SlotIsPhysical(&slot_contents
))
425 * rows_removed and wal_level_insufficient are the only two
426 * reasons for the logical slot's conflict with recovery.
428 if (cause
== RS_INVAL_HORIZON
||
429 cause
== RS_INVAL_WAL_LEVEL
)
430 values
[i
++] = BoolGetDatum(true);
432 values
[i
++] = BoolGetDatum(false);
435 if (cause
== RS_INVAL_NONE
)
438 values
[i
++] = CStringGetTextDatum(SlotInvalidationCauses
[cause
]);
440 values
[i
++] = BoolGetDatum(slot_contents
.data
.failover
);
442 values
[i
++] = BoolGetDatum(slot_contents
.data
.synced
);
444 Assert(i
== PG_GET_REPLICATION_SLOTS_COLS
);
446 tuplestore_putvalues(rsinfo
->setResult
, rsinfo
->setDesc
,
450 LWLockRelease(ReplicationSlotControlLock
);
456 * Helper function for advancing our physical replication slot forward.
458 * The LSN position to move to is compared simply to the slot's restart_lsn,
459 * knowing that any position older than that would be removed by successive
463 pg_physical_replication_slot_advance(XLogRecPtr moveto
)
465 XLogRecPtr startlsn
= MyReplicationSlot
->data
.restart_lsn
;
466 XLogRecPtr retlsn
= startlsn
;
468 Assert(moveto
!= InvalidXLogRecPtr
);
470 if (startlsn
< moveto
)
472 SpinLockAcquire(&MyReplicationSlot
->mutex
);
473 MyReplicationSlot
->data
.restart_lsn
= moveto
;
474 SpinLockRelease(&MyReplicationSlot
->mutex
);
478 * Dirty the slot so as it is written out at the next checkpoint. Note
479 * that the LSN position advanced may still be lost in the event of a
480 * crash, but this makes the data consistent after a clean shutdown.
482 ReplicationSlotMarkDirty();
485 * Wake up logical walsenders holding logical failover slots after
486 * updating the restart_lsn of the physical slot.
488 PhysicalWakeupLogicalWalSnd();
495 * Advance our logical replication slot forward. See
496 * LogicalSlotAdvanceAndCheckSnapState for details.
499 pg_logical_replication_slot_advance(XLogRecPtr moveto
)
501 return LogicalSlotAdvanceAndCheckSnapState(moveto
, NULL
);
505 * SQL function for moving the position in a replication slot.
508 pg_replication_slot_advance(PG_FUNCTION_ARGS
)
510 Name slotname
= PG_GETARG_NAME(0);
511 XLogRecPtr moveto
= PG_GETARG_LSN(1);
520 Assert(!MyReplicationSlot
);
522 CheckSlotPermissions();
524 if (XLogRecPtrIsInvalid(moveto
))
526 (errcode(ERRCODE_INVALID_PARAMETER_VALUE
),
527 errmsg("invalid target WAL LSN")));
529 /* Build a tuple descriptor for our result type */
530 if (get_call_result_type(fcinfo
, NULL
, &tupdesc
) != TYPEFUNC_COMPOSITE
)
531 elog(ERROR
, "return type must be a row type");
534 * We can't move slot past what's been flushed/replayed so clamp the
535 * target position accordingly.
537 if (!RecoveryInProgress())
538 moveto
= Min(moveto
, GetFlushRecPtr(NULL
));
540 moveto
= Min(moveto
, GetXLogReplayRecPtr(NULL
));
542 /* Acquire the slot so we "own" it */
543 ReplicationSlotAcquire(NameStr(*slotname
), true);
545 /* A slot whose restart_lsn has never been reserved cannot be advanced */
546 if (XLogRecPtrIsInvalid(MyReplicationSlot
->data
.restart_lsn
))
548 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE
),
549 errmsg("replication slot \"%s\" cannot be advanced",
551 errdetail("This slot has never previously reserved WAL, or it has been invalidated.")));
554 * Check if the slot is not moving backwards. Physical slots rely simply
555 * on restart_lsn as a minimum point, while logical slots have confirmed
556 * consumption up to confirmed_flush, meaning that in both cases data
557 * older than that is not available anymore.
559 if (OidIsValid(MyReplicationSlot
->data
.database
))
560 minlsn
= MyReplicationSlot
->data
.confirmed_flush
;
562 minlsn
= MyReplicationSlot
->data
.restart_lsn
;
566 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE
),
567 errmsg("cannot advance replication slot to %X/%X, minimum is %X/%X",
568 LSN_FORMAT_ARGS(moveto
), LSN_FORMAT_ARGS(minlsn
))));
570 /* Do the actual slot update, depending on the slot type */
571 if (OidIsValid(MyReplicationSlot
->data
.database
))
572 endlsn
= pg_logical_replication_slot_advance(moveto
);
574 endlsn
= pg_physical_replication_slot_advance(moveto
);
576 values
[0] = NameGetDatum(&MyReplicationSlot
->data
.name
);
580 * Recompute the minimum LSN and xmin across all slots to adjust with the
581 * advancing potentially done.
583 ReplicationSlotsComputeRequiredXmin(false);
584 ReplicationSlotsComputeRequiredLSN();
586 ReplicationSlotRelease();
588 /* Return the reached position. */
589 values
[1] = LSNGetDatum(endlsn
);
592 tuple
= heap_form_tuple(tupdesc
, values
, nulls
);
593 result
= HeapTupleGetDatum(tuple
);
595 PG_RETURN_DATUM(result
);
599 * Helper function of copying a replication slot.
602 copy_replication_slot(FunctionCallInfo fcinfo
, bool logical_slot
)
604 Name src_name
= PG_GETARG_NAME(0);
605 Name dst_name
= PG_GETARG_NAME(1);
606 ReplicationSlot
*src
= NULL
;
607 ReplicationSlot first_slot_contents
;
608 ReplicationSlot second_slot_contents
;
609 XLogRecPtr src_restart_lsn
;
619 if (get_call_result_type(fcinfo
, NULL
, &tupdesc
) != TYPEFUNC_COMPOSITE
)
620 elog(ERROR
, "return type must be a row type");
622 CheckSlotPermissions();
625 CheckLogicalDecodingRequirements();
627 CheckSlotRequirements();
629 LWLockAcquire(ReplicationSlotControlLock
, LW_SHARED
);
632 * We need to prevent the source slot's reserved WAL from being removed,
633 * but we don't want to lock that slot for very long, and it can advance
634 * in the meantime. So obtain the source slot's data, and create a new
635 * slot using its restart_lsn. Afterwards we lock the source slot again
636 * and verify that the data we copied (name, type) has not changed
637 * incompatibly. No inconvenient WAL removal can occur once the new slot
638 * is created -- but since WAL removal could have occurred before we
639 * managed to create the new slot, we advance the new slot's restart_lsn
640 * to the source slot's updated restart_lsn the second time we lock it.
642 for (int i
= 0; i
< max_replication_slots
; i
++)
644 ReplicationSlot
*s
= &ReplicationSlotCtl
->replication_slots
[i
];
646 if (s
->in_use
&& strcmp(NameStr(s
->data
.name
), NameStr(*src_name
)) == 0)
648 /* Copy the slot contents while holding spinlock */
649 SpinLockAcquire(&s
->mutex
);
650 first_slot_contents
= *s
;
651 SpinLockRelease(&s
->mutex
);
657 LWLockRelease(ReplicationSlotControlLock
);
661 (errcode(ERRCODE_UNDEFINED_OBJECT
),
662 errmsg("replication slot \"%s\" does not exist", NameStr(*src_name
))));
664 src_islogical
= SlotIsLogical(&first_slot_contents
);
665 src_restart_lsn
= first_slot_contents
.data
.restart_lsn
;
666 temporary
= (first_slot_contents
.data
.persistency
== RS_TEMPORARY
);
667 plugin
= logical_slot
? NameStr(first_slot_contents
.data
.plugin
) : NULL
;
669 /* Check type of replication slot */
670 if (src_islogical
!= logical_slot
)
672 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED
),
674 errmsg("cannot copy physical replication slot \"%s\" as a logical replication slot",
675 NameStr(*src_name
)) :
676 errmsg("cannot copy logical replication slot \"%s\" as a physical replication slot",
677 NameStr(*src_name
))));
679 /* Copying non-reserved slot doesn't make sense */
680 if (XLogRecPtrIsInvalid(src_restart_lsn
))
682 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE
),
683 errmsg("cannot copy a replication slot that doesn't reserve WAL")));
685 /* Overwrite params from optional arguments */
687 temporary
= PG_GETARG_BOOL(2);
690 Assert(logical_slot
);
691 plugin
= NameStr(*(PG_GETARG_NAME(3)));
694 /* Create new slot and acquire it */
698 * We must not try to read WAL, since we haven't reserved it yet --
699 * hence pass find_startpoint false. confirmed_flush will be set
700 * below, by copying from the source slot.
702 * To avoid potential issues with the slot synchronization where the
703 * restart_lsn of a replication slot can go backward, we set the
704 * failover option to false here. This situation occurs when a slot
705 * on the primary server is dropped and immediately replaced with a
706 * new slot of the same name, created by copying from another existing
707 * slot. However, the slot synchronization will only observe the
708 * restart_lsn of the same slot going backward.
710 create_logical_replication_slot(NameStr(*dst_name
),
719 create_physical_replication_slot(NameStr(*dst_name
),
725 * Update the destination slot to current values of the source slot;
726 * recheck that the source slot is still the one we saw previously.
729 TransactionId copy_effective_xmin
;
730 TransactionId copy_effective_catalog_xmin
;
731 TransactionId copy_xmin
;
732 TransactionId copy_catalog_xmin
;
733 XLogRecPtr copy_restart_lsn
;
734 XLogRecPtr copy_confirmed_flush
;
738 /* Copy data of source slot again */
739 SpinLockAcquire(&src
->mutex
);
740 second_slot_contents
= *src
;
741 SpinLockRelease(&src
->mutex
);
743 copy_effective_xmin
= second_slot_contents
.effective_xmin
;
744 copy_effective_catalog_xmin
= second_slot_contents
.effective_catalog_xmin
;
746 copy_xmin
= second_slot_contents
.data
.xmin
;
747 copy_catalog_xmin
= second_slot_contents
.data
.catalog_xmin
;
748 copy_restart_lsn
= second_slot_contents
.data
.restart_lsn
;
749 copy_confirmed_flush
= second_slot_contents
.data
.confirmed_flush
;
751 /* for existence check */
752 copy_name
= NameStr(second_slot_contents
.data
.name
);
753 copy_islogical
= SlotIsLogical(&second_slot_contents
);
756 * Check if the source slot still exists and is valid. We regard it as
757 * invalid if the type of replication slot or name has been changed,
758 * or the restart_lsn either is invalid or has gone backward. (The
759 * restart_lsn could go backwards if the source slot is dropped and
760 * copied from an older slot during installation.)
762 * Since erroring out will release and drop the destination slot we
763 * don't need to release it here.
765 if (copy_restart_lsn
< src_restart_lsn
||
766 src_islogical
!= copy_islogical
||
767 strcmp(copy_name
, NameStr(*src_name
)) != 0)
769 (errmsg("could not copy replication slot \"%s\"",
771 errdetail("The source replication slot was modified incompatibly during the copy operation.")));
773 /* The source slot must have a consistent snapshot */
774 if (src_islogical
&& XLogRecPtrIsInvalid(copy_confirmed_flush
))
776 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED
),
777 errmsg("cannot copy unfinished logical replication slot \"%s\"",
779 errhint("Retry when the source replication slot's confirmed_flush_lsn is valid.")));
781 /* Install copied values again */
782 SpinLockAcquire(&MyReplicationSlot
->mutex
);
783 MyReplicationSlot
->effective_xmin
= copy_effective_xmin
;
784 MyReplicationSlot
->effective_catalog_xmin
= copy_effective_catalog_xmin
;
786 MyReplicationSlot
->data
.xmin
= copy_xmin
;
787 MyReplicationSlot
->data
.catalog_xmin
= copy_catalog_xmin
;
788 MyReplicationSlot
->data
.restart_lsn
= copy_restart_lsn
;
789 MyReplicationSlot
->data
.confirmed_flush
= copy_confirmed_flush
;
790 SpinLockRelease(&MyReplicationSlot
->mutex
);
792 ReplicationSlotMarkDirty();
793 ReplicationSlotsComputeRequiredXmin(false);
794 ReplicationSlotsComputeRequiredLSN();
795 ReplicationSlotSave();
797 #ifdef USE_ASSERT_CHECKING
798 /* Check that the restart_lsn is available */
802 XLByteToSeg(copy_restart_lsn
, segno
, wal_segment_size
);
803 Assert(XLogGetLastRemovedSegno() < segno
);
808 /* target slot fully created, mark as persistent if needed */
809 if (logical_slot
&& !temporary
)
810 ReplicationSlotPersist();
812 /* All done. Set up the return values */
813 values
[0] = NameGetDatum(dst_name
);
815 if (!XLogRecPtrIsInvalid(MyReplicationSlot
->data
.confirmed_flush
))
817 values
[1] = LSNGetDatum(MyReplicationSlot
->data
.confirmed_flush
);
823 tuple
= heap_form_tuple(tupdesc
, values
, nulls
);
824 result
= HeapTupleGetDatum(tuple
);
826 ReplicationSlotRelease();
828 PG_RETURN_DATUM(result
);
831 /* The wrappers below are all to appease opr_sanity */
833 pg_copy_logical_replication_slot_a(PG_FUNCTION_ARGS
)
835 return copy_replication_slot(fcinfo
, true);
839 pg_copy_logical_replication_slot_b(PG_FUNCTION_ARGS
)
841 return copy_replication_slot(fcinfo
, true);
845 pg_copy_logical_replication_slot_c(PG_FUNCTION_ARGS
)
847 return copy_replication_slot(fcinfo
, true);
851 pg_copy_physical_replication_slot_a(PG_FUNCTION_ARGS
)
853 return copy_replication_slot(fcinfo
, false);
857 pg_copy_physical_replication_slot_b(PG_FUNCTION_ARGS
)
859 return copy_replication_slot(fcinfo
, false);
863 * Synchronize failover enabled replication slots to a standby server
864 * from the primary server.
867 pg_sync_replication_slots(PG_FUNCTION_ARGS
)
869 WalReceiverConn
*wrconn
;
871 StringInfoData app_name
;
873 CheckSlotPermissions();
875 if (!RecoveryInProgress())
877 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE
),
878 errmsg("replication slots can only be synchronized to a standby server"));
880 ValidateSlotSyncParams(ERROR
);
882 /* Load the libpq-specific functions */
883 load_file("libpqwalreceiver", false);
885 (void) CheckAndGetDbnameFromConninfo();
887 initStringInfo(&app_name
);
889 appendStringInfo(&app_name
, "%s_slotsync", cluster_name
);
891 appendStringInfoString(&app_name
, "slotsync");
893 /* Connect to the primary server. */
894 wrconn
= walrcv_connect(PrimaryConnInfo
, false, false, false,
895 app_name
.data
, &err
);
896 pfree(app_name
.data
);
900 errcode(ERRCODE_CONNECTION_FAILURE
),
901 errmsg("synchronization worker \"%s\" could not connect to the primary server: %s",
902 app_name
.data
, err
));
904 SyncReplicationSlots(wrconn
);
906 walrcv_disconnect(wrconn
);