diff --git a/src/backend/replication/slotfuncs.c b/src/backend/replication/slotfuncs.c index 2c9d5de6d9..7acde4c7c9 100644 --- a/src/backend/replication/slotfuncs.c +++ b/src/backend/replication/slotfuncs.c @@ -121,7 +121,8 @@ pg_create_physical_replication_slot(PG_FUNCTION_ARGS) */ static void create_logical_replication_slot(char *name, char *plugin, - bool temporary, XLogRecPtr restart_lsn) + bool temporary, XLogRecPtr restart_lsn, + bool build_snapshot) { LogicalDecodingContext *ctx = NULL; @@ -148,7 +149,8 @@ create_logical_replication_slot(char *name, char *plugin, NULL); /* build initial snapshot, might take a while */ - DecodingContextFindStartpoint(ctx); + if (build_snapshot) + DecodingContextFindStartpoint(ctx); /* don't need the decoding context anymore */ FreeDecodingContext(ctx); @@ -179,7 +181,8 @@ pg_create_logical_replication_slot(PG_FUNCTION_ARGS) create_logical_replication_slot(NameStr(*name), NameStr(*plugin), temporary, - InvalidXLogRecPtr); + InvalidXLogRecPtr, + true); values[0] = NameGetDatum(&MyReplicationSlot->data.name); values[1] = LSNGetDatum(MyReplicationSlot->data.confirmed_flush); @@ -596,6 +599,7 @@ copy_replication_slot(FunctionCallInfo fcinfo, bool logical_slot) Name dst_name = PG_GETARG_NAME(1); ReplicationSlot *src = NULL; XLogRecPtr src_restart_lsn; + XLogRecPtr src_confirmed_flush; bool src_islogical; bool temporary; char *plugin; @@ -637,6 +641,7 @@ copy_replication_slot(FunctionCallInfo fcinfo, bool logical_slot) SpinLockAcquire(&s->mutex); src_islogical = SlotIsLogical(s); src_restart_lsn = s->data.restart_lsn; + src_confirmed_flush = s->data.confirmed_flush; temporary = s->data.persistency == RS_TEMPORARY; plugin = logical_slot ? pstrdup(NameStr(s->data.plugin)) : NULL; SpinLockRelease(&s->mutex); @@ -672,6 +677,13 @@ copy_replication_slot(FunctionCallInfo fcinfo, bool logical_slot) errmsg("cannot copy a replication slot that doesn't reserve WAL"))); } + /* The source slot needs to have a consistent snapshot */ + if (src_islogical && XLogRecPtrIsInvalid(src_confirmed_flush)) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot copy a logical replication slot that doesn't have confirmed flush WAL location (LSN)"), + errhint("Retry once the source replication slot found consistent point"))); + /* Overwrite params from optional arguments */ if (PG_NARGS() >= 3) temporary = PG_GETARG_BOOL(2); @@ -683,10 +695,19 @@ copy_replication_slot(FunctionCallInfo fcinfo, bool logical_slot) /* Create new slot and acquire it */ if (logical_slot) + { + /* + * WAL required for building snapshot could be removed as we don't reserve + * WAL yet. So we create a new logical replication slot without building + * an initial snapshot. A resonable start point for decoding will be set by + * copying from the source slot. + */ create_logical_replication_slot(NameStr(*dst_name), plugin, temporary, - src_restart_lsn); + src_restart_lsn, + false); + } else create_physical_replication_slot(NameStr(*dst_name), true, @@ -703,6 +724,7 @@ copy_replication_slot(FunctionCallInfo fcinfo, bool logical_slot) TransactionId copy_xmin; TransactionId copy_catalog_xmin; XLogRecPtr copy_restart_lsn; + XLogRecPtr copy_confirmed_flush; bool copy_islogical; char *copy_name; @@ -714,6 +736,7 @@ copy_replication_slot(FunctionCallInfo fcinfo, bool logical_slot) copy_xmin = src->data.xmin; copy_catalog_xmin = src->data.catalog_xmin; copy_restart_lsn = src->data.restart_lsn; + copy_confirmed_flush = src->data.confirmed_flush; /* for existence check */ copy_name = pstrdup(NameStr(src->data.name)); @@ -746,6 +769,7 @@ copy_replication_slot(FunctionCallInfo fcinfo, bool logical_slot) MyReplicationSlot->data.xmin = copy_xmin; MyReplicationSlot->data.catalog_xmin = copy_catalog_xmin; MyReplicationSlot->data.restart_lsn = copy_restart_lsn; + MyReplicationSlot->data.confirmed_flush = copy_confirmed_flush; SpinLockRelease(&MyReplicationSlot->mutex); ReplicationSlotMarkDirty();