Commit 9f06d79e authored by Alvaro Herrera's avatar Alvaro Herrera

Add facility to copy replication slots

This allows the user to create duplicates of existing replication slots,
either logical or physical, and even changing properties such as whether
they are temporary or the output plugin used.

There are multiple uses for this, such as initializing multiple replicas
using the slot for one base backup; when doing investigation of logical
replication issues; and to select a different output plugins.

Author: Masahiko Sawada
Reviewed-by: Michael Paquier, Andres Freund, Petr Jelinek
Discussion: https://postgr.es/m/CAD21AoAm7XX8y_tOPP6j4Nzzch12FvA1wPqiO690RCk+uYVstg@mail.gmail.com
parent de2b3841
......@@ -150,3 +150,237 @@ SELECT pg_drop_replication_slot('regression_slot3');
(1 row)
--
-- Test copy functions for logical replication slots
--
-- Create and copy logical slots
SELECT 'init' FROM pg_create_logical_replication_slot('orig_slot1', 'test_decoding', false);
?column?
----------
init
(1 row)
SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot1', 'copied_slot1_no_change');
?column?
----------
copy
(1 row)
SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot1', 'copied_slot1_change_plugin', false, 'pgoutput');
?column?
----------
copy
(1 row)
SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot1', 'copied_slot1_change_plugin_temp', true, 'pgoutput');
?column?
----------
copy
(1 row)
-- Check all copied slots status
SELECT
o.slot_name, o.plugin, o.temporary, c.slot_name, c.plugin, c.temporary
FROM
(SELECT * FROM pg_replication_slots WHERE slot_name LIKE 'orig%') as o
LEFT JOIN pg_replication_slots as c ON o.restart_lsn = c.restart_lsn AND o.confirmed_flush_lsn = c.confirmed_flush_lsn
WHERE
o.slot_name != c.slot_name
ORDER BY o.slot_name, c.slot_name;
slot_name | plugin | temporary | slot_name | plugin | temporary
------------+---------------+-----------+---------------------------------+---------------+-----------
orig_slot1 | test_decoding | f | copied_slot1_change_plugin | pgoutput | f
orig_slot1 | test_decoding | f | copied_slot1_change_plugin_temp | pgoutput | t
orig_slot1 | test_decoding | f | copied_slot1_no_change | test_decoding | f
(3 rows)
-- Now we have maximum 4 replication slots. Check slots are properly
-- released even when raise error during creating the target slot.
SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot1', 'failed'); -- error
ERROR: all replication slots are in use
HINT: Free one or increase max_replication_slots.
-- temporary slots were dropped automatically
SELECT pg_drop_replication_slot('orig_slot1');
pg_drop_replication_slot
--------------------------
(1 row)
SELECT pg_drop_replication_slot('copied_slot1_no_change');
pg_drop_replication_slot
--------------------------
(1 row)
SELECT pg_drop_replication_slot('copied_slot1_change_plugin');
pg_drop_replication_slot
--------------------------
(1 row)
-- Test based on the temporary logical slot
SELECT 'init' FROM pg_create_logical_replication_slot('orig_slot2', 'test_decoding', true);
?column?
----------
init
(1 row)
SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot2', 'copied_slot2_no_change');
?column?
----------
copy
(1 row)
SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot2', 'copied_slot2_change_plugin', true, 'pgoutput');
?column?
----------
copy
(1 row)
SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot2', 'copied_slot2_change_plugin_temp', false, 'pgoutput');
?column?
----------
copy
(1 row)
-- Check all copied slots status
SELECT
o.slot_name, o.plugin, o.temporary, c.slot_name, c.plugin, c.temporary
FROM
(SELECT * FROM pg_replication_slots WHERE slot_name LIKE 'orig%') as o
LEFT JOIN pg_replication_slots as c ON o.restart_lsn = c.restart_lsn AND o.confirmed_flush_lsn = c.confirmed_flush_lsn
WHERE
o.slot_name != c.slot_name
ORDER BY o.slot_name, c.slot_name;
slot_name | plugin | temporary | slot_name | plugin | temporary
------------+---------------+-----------+---------------------------------+---------------+-----------
orig_slot2 | test_decoding | t | copied_slot2_change_plugin | pgoutput | t
orig_slot2 | test_decoding | t | copied_slot2_change_plugin_temp | pgoutput | f
orig_slot2 | test_decoding | t | copied_slot2_no_change | test_decoding | t
(3 rows)
-- Cannot copy a logical slot to a physical slot
SELECT 'copy' FROM pg_copy_physical_replication_slot('orig_slot2', 'failed'); -- error
ERROR: cannot copy physical replication slot "orig_slot2" as a logical replication slot
-- temporary slots were dropped automatically
SELECT pg_drop_replication_slot('copied_slot2_change_plugin_temp');
pg_drop_replication_slot
--------------------------
(1 row)
--
-- Test copy functions for physical replication slots
--
-- Create and copy physical slots
SELECT 'init' FROM pg_create_physical_replication_slot('orig_slot1', true);
?column?
----------
init
(1 row)
SELECT 'init' FROM pg_create_physical_replication_slot('orig_slot2', false);
?column?
----------
init
(1 row)
SELECT 'copy' FROM pg_copy_physical_replication_slot('orig_slot1', 'copied_slot1_no_change');
?column?
----------
copy
(1 row)
SELECT 'copy' FROM pg_copy_physical_replication_slot('orig_slot1', 'copied_slot1_temp', true);
?column?
----------
copy
(1 row)
-- Check all copied slots status. Since all slots don't reserve WAL we check only other fields.
SELECT slot_name, slot_type, temporary FROM pg_replication_slots;
slot_name | slot_type | temporary
------------------------+-----------+-----------
orig_slot1 | physical | f
orig_slot2 | physical | f
copied_slot1_no_change | physical | f
copied_slot1_temp | physical | t
(4 rows)
-- Cannot copy a physical slot to a logical slot
SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot1', 'failed'); -- error
ERROR: cannot copy logical replication slot "orig_slot1" as a physical replication slot
-- Cannot copy a physical slot that doesn't reserve WAL
SELECT 'copy' FROM pg_copy_physical_replication_slot('orig_slot2', 'failed'); -- error
ERROR: cannot copy a replication slot that doesn't reserve WAL
-- temporary slots were dropped automatically
SELECT pg_drop_replication_slot('orig_slot1');
pg_drop_replication_slot
--------------------------
(1 row)
SELECT pg_drop_replication_slot('orig_slot2');
pg_drop_replication_slot
--------------------------
(1 row)
SELECT pg_drop_replication_slot('copied_slot1_no_change');
pg_drop_replication_slot
--------------------------
(1 row)
-- Test based on the temporary physical slot
SELECT 'init' FROM pg_create_physical_replication_slot('orig_slot2', true, true);
?column?
----------
init
(1 row)
SELECT 'copy' FROM pg_copy_physical_replication_slot('orig_slot2', 'copied_slot2_no_change');
?column?
----------
copy
(1 row)
SELECT 'copy' FROM pg_copy_physical_replication_slot('orig_slot2', 'copied_slot2_notemp', false);
?column?
----------
copy
(1 row)
-- Check all copied slots status
SELECT
o.slot_name, o.temporary, c.slot_name, c.temporary
FROM
(SELECT * FROM pg_replication_slots WHERE slot_name LIKE 'orig%') as o
LEFT JOIN pg_replication_slots as c ON o.restart_lsn = c.restart_lsn
WHERE
o.slot_name != c.slot_name
ORDER BY o.slot_name, c.slot_name;
slot_name | temporary | slot_name | temporary
------------+-----------+------------------------+-----------
orig_slot2 | t | copied_slot2_no_change | t
orig_slot2 | t | copied_slot2_notemp | f
(2 rows)
SELECT pg_drop_replication_slot('orig_slot2');
pg_drop_replication_slot
--------------------------
(1 row)
SELECT pg_drop_replication_slot('copied_slot2_no_change');
pg_drop_replication_slot
--------------------------
(1 row)
SELECT pg_drop_replication_slot('copied_slot2_notemp');
pg_drop_replication_slot
--------------------------
(1 row)
......@@ -76,3 +76,97 @@ SELECT slot_name FROM pg_create_physical_replication_slot('regression_slot3');
SELECT pg_replication_slot_advance('regression_slot3', '0/0'); -- invalid LSN
SELECT pg_replication_slot_advance('regression_slot3', '0/1'); -- error
SELECT pg_drop_replication_slot('regression_slot3');
--
-- Test copy functions for logical replication slots
--
-- Create and copy logical slots
SELECT 'init' FROM pg_create_logical_replication_slot('orig_slot1', 'test_decoding', false);
SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot1', 'copied_slot1_no_change');
SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot1', 'copied_slot1_change_plugin', false, 'pgoutput');
SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot1', 'copied_slot1_change_plugin_temp', true, 'pgoutput');
-- Check all copied slots status
SELECT
o.slot_name, o.plugin, o.temporary, c.slot_name, c.plugin, c.temporary
FROM
(SELECT * FROM pg_replication_slots WHERE slot_name LIKE 'orig%') as o
LEFT JOIN pg_replication_slots as c ON o.restart_lsn = c.restart_lsn AND o.confirmed_flush_lsn = c.confirmed_flush_lsn
WHERE
o.slot_name != c.slot_name
ORDER BY o.slot_name, c.slot_name;
-- Now we have maximum 4 replication slots. Check slots are properly
-- released even when raise error during creating the target slot.
SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot1', 'failed'); -- error
-- temporary slots were dropped automatically
SELECT pg_drop_replication_slot('orig_slot1');
SELECT pg_drop_replication_slot('copied_slot1_no_change');
SELECT pg_drop_replication_slot('copied_slot1_change_plugin');
-- Test based on the temporary logical slot
SELECT 'init' FROM pg_create_logical_replication_slot('orig_slot2', 'test_decoding', true);
SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot2', 'copied_slot2_no_change');
SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot2', 'copied_slot2_change_plugin', true, 'pgoutput');
SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot2', 'copied_slot2_change_plugin_temp', false, 'pgoutput');
-- Check all copied slots status
SELECT
o.slot_name, o.plugin, o.temporary, c.slot_name, c.plugin, c.temporary
FROM
(SELECT * FROM pg_replication_slots WHERE slot_name LIKE 'orig%') as o
LEFT JOIN pg_replication_slots as c ON o.restart_lsn = c.restart_lsn AND o.confirmed_flush_lsn = c.confirmed_flush_lsn
WHERE
o.slot_name != c.slot_name
ORDER BY o.slot_name, c.slot_name;
-- Cannot copy a logical slot to a physical slot
SELECT 'copy' FROM pg_copy_physical_replication_slot('orig_slot2', 'failed'); -- error
-- temporary slots were dropped automatically
SELECT pg_drop_replication_slot('copied_slot2_change_plugin_temp');
--
-- Test copy functions for physical replication slots
--
-- Create and copy physical slots
SELECT 'init' FROM pg_create_physical_replication_slot('orig_slot1', true);
SELECT 'init' FROM pg_create_physical_replication_slot('orig_slot2', false);
SELECT 'copy' FROM pg_copy_physical_replication_slot('orig_slot1', 'copied_slot1_no_change');
SELECT 'copy' FROM pg_copy_physical_replication_slot('orig_slot1', 'copied_slot1_temp', true);
-- Check all copied slots status. Since all slots don't reserve WAL we check only other fields.
SELECT slot_name, slot_type, temporary FROM pg_replication_slots;
-- Cannot copy a physical slot to a logical slot
SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot1', 'failed'); -- error
-- Cannot copy a physical slot that doesn't reserve WAL
SELECT 'copy' FROM pg_copy_physical_replication_slot('orig_slot2', 'failed'); -- error
-- temporary slots were dropped automatically
SELECT pg_drop_replication_slot('orig_slot1');
SELECT pg_drop_replication_slot('orig_slot2');
SELECT pg_drop_replication_slot('copied_slot1_no_change');
-- Test based on the temporary physical slot
SELECT 'init' FROM pg_create_physical_replication_slot('orig_slot2', true, true);
SELECT 'copy' FROM pg_copy_physical_replication_slot('orig_slot2', 'copied_slot2_no_change');
SELECT 'copy' FROM pg_copy_physical_replication_slot('orig_slot2', 'copied_slot2_notemp', false);
-- Check all copied slots status
SELECT
o.slot_name, o.temporary, c.slot_name, c.temporary
FROM
(SELECT * FROM pg_replication_slots WHERE slot_name LIKE 'orig%') as o
LEFT JOIN pg_replication_slots as c ON o.restart_lsn = c.restart_lsn
WHERE
o.slot_name != c.slot_name
ORDER BY o.slot_name, c.slot_name;
SELECT pg_drop_replication_slot('orig_slot2');
SELECT pg_drop_replication_slot('copied_slot2_no_change');
SELECT pg_drop_replication_slot('copied_slot2_notemp');
......@@ -20431,6 +20431,47 @@ postgres=# SELECT * FROM pg_walfile_name_offset(pg_stop_backup());
</entry>
</row>
<row>
<entry>
<indexterm>
<primary>pg_copy_physical_replication_slot</primary>
</indexterm>
<literal><function>pg_copy_physical_replication_slot(<parameter>src_slot_name</parameter> <type>name</type>, <parameter>dst_slot_name</parameter> <type>name</type> <optional>, <parameter>temporary</parameter> <type>boolean</type></optional>)</function></literal>
</entry>
<entry>
(<parameter>slot_name</parameter> <type>name</type>, <parameter>lsn</parameter> <type>pg_lsn</type>)
</entry>
<entry>
Copies an existing physical replication slot name <parameter>src_slot_name</parameter>
to a physical replication slot named <parameter>dst_slot_name</parameter>.
The copied physical slot starts to reserve WAL from the same <acronym>LSN</acronym> as the
source slot.
<parameter>temporary</parameter> is optional. If <parameter>temporary</parameter>
is omitted, the same value as the source slot is used.
</entry>
</row>
<row>
<entry>
<indexterm>
<primary>pg_copy_logical_replication_slot</primary>
</indexterm>
<literal><function>pg_copy_logical_replication_slot(<parameter>src_slot_name</parameter> <type>name</type>, <parameter>dst_slot_name</parameter> <type>name</type> <optional>, <parameter>temporary</parameter> <type>boolean</type> <optional>, <parameter>plugin</parameter> <type>name</type></optional></optional>)</function></literal>
</entry>
<entry>
(<parameter>slot_name</parameter> <type>name</type>, <parameter>lsn</parameter> <type>pg_lsn</type>)
</entry>
<entry>
Copies an existing logical replication slot name <parameter>src_slot_name</parameter>
to a logical replication slot named <parameter>dst_slot_name</parameter>
while changing the output plugin and persistence. The copied logical slot starts
from the same <acronym>LSN</acronym> as the source logical slot. Both
<parameter>temporary</parameter> and <parameter>plugin</parameter> are optional.
If <parameter>temporary</parameter> or <parameter>plugin</parameter> are omitted,
the same values as the source logical slot are used.
</entry>
</row>
<row>
<entry>
<indexterm>
......
......@@ -211,11 +211,15 @@ StartupDecodingContext(List *output_plugin_options,
/*
* Create a new decoding context, for a new logical slot.
*
* plugin contains the name of the output plugin
* output_plugin_options contains options passed to the output plugin
* read_page, prepare_write, do_write, update_progress
* callbacks that have to be filled to perform the use-case dependent,
* actual, work.
* plugin -- contains the name of the output plugin
* output_plugin_options -- contains options passed to the output plugin
* restart_lsn -- if given as invalid, it's this routine's responsibility to
* mark WAL as reserved by setting a convenient restart_lsn for the slot.
* Otherwise, we set for decoding to start from the given LSN without
* marking WAL reserved beforehand. In that scenario, it's up to the
* caller to guarantee that WAL remains available.
* read_page, prepare_write, do_write, update_progress --
* callbacks that perform the use-case dependent, actual, work.
*
* Needs to be called while in a memory context that's at least as long lived
* as the decoding context because further memory contexts will be created
......@@ -228,6 +232,7 @@ LogicalDecodingContext *
CreateInitDecodingContext(char *plugin,
List *output_plugin_options,
bool need_full_snapshot,
XLogRecPtr restart_lsn,
XLogPageReadCB read_page,
LogicalOutputPluginWriterPrepareWrite prepare_write,
LogicalOutputPluginWriterWrite do_write,
......@@ -271,7 +276,14 @@ CreateInitDecodingContext(char *plugin,
StrNCpy(NameStr(slot->data.plugin), plugin, NAMEDATALEN);
SpinLockRelease(&slot->mutex);
if (XLogRecPtrIsInvalid(restart_lsn))
ReplicationSlotReserveWal();
else
{
SpinLockAcquire(&slot->mutex);
slot->data.restart_lsn = restart_lsn;
SpinLockRelease(&slot->mutex);
}
/* ----
* This is a bit tricky: We need to determine a safe xmin horizon to start
......@@ -316,7 +328,7 @@ CreateInitDecodingContext(char *plugin,
ReplicationSlotMarkDirty();
ReplicationSlotSave();
ctx = StartupDecodingContext(NIL, InvalidXLogRecPtr, xmin_horizon,
ctx = StartupDecodingContext(NIL, restart_lsn, xmin_horizon,
need_full_snapshot, false,
read_page, prepare_write, do_write,
update_progress);
......
This diff is collapsed.
......@@ -934,6 +934,7 @@ CreateReplicationSlot(CreateReplicationSlotCmd *cmd)
}
ctx = CreateInitDecodingContext(cmd->plugin, NIL, need_full_snapshot,
InvalidXLogRecPtr,
logical_read_xlog_page,
WalSndPrepareWrite, WalSndWriteData,
WalSndUpdateProgress);
......
......@@ -53,6 +53,6 @@
*/
/* yyyymmddN */
#define CATALOG_VERSION_NO 201904031
#define CATALOG_VERSION_NO 201904051
#endif
......@@ -9774,6 +9774,20 @@
proargmodes => '{i,i,i,o,o}',
proargnames => '{slot_name,immediately_reserve,temporary,slot_name,lsn}',
prosrc => 'pg_create_physical_replication_slot' },
{ oid => '4220', descr => 'copy a physical replication slot, changing temporality',
proname => 'pg_copy_physical_replication_slot', provolatile => 'v',
proparallel => 'u', prorettype => 'record', proargtypes => 'name name bool',
proallargtypes => '{name,name,bool,name,pg_lsn}',
proargmodes => '{i,i,i,o,o}',
proargnames => '{src_slot_name,dst_slot_name,temporary,slot_name,lsn}',
prosrc => 'pg_copy_physical_replication_slot_a' },
{ oid => '4221', descr => 'copy a physical replication slot',
proname => 'pg_copy_physical_replication_slot', provolatile => 'v',
proparallel => 'u', prorettype => 'record', proargtypes => 'name name',
proallargtypes => '{name,name,name,pg_lsn}',
proargmodes => '{i,i,o,o}',
proargnames => '{src_slot_name,dst_slot_name,slot_name,lsn}',
prosrc => 'pg_copy_physical_replication_slot_b' },
{ oid => '3780', descr => 'drop a replication slot',
proname => 'pg_drop_replication_slot', provolatile => 'v', proparallel => 'u',
prorettype => 'void', proargtypes => 'name',
......@@ -9794,6 +9808,27 @@
proargmodes => '{i,i,i,o,o}',
proargnames => '{slot_name,plugin,temporary,slot_name,lsn}',
prosrc => 'pg_create_logical_replication_slot' },
{ oid => '4222', descr => 'copy a logical replication slot, changing temporality and plugin',
proname => 'pg_copy_logical_replication_slot', provolatile => 'v',
proparallel => 'u', prorettype => 'record', proargtypes => 'name name bool name',
proallargtypes => '{name,name,bool,name,name,pg_lsn}',
proargmodes => '{i,i,i,i,o,o}',
proargnames => '{src_slot_name,dst_slot_name,temporary,plugin,slot_name,lsn}',
prosrc => 'pg_copy_logical_replication_slot_a' },
{ oid => '4223', descr => 'copy a logical replication slot, changing temporality',
proname => 'pg_copy_logical_replication_slot', provolatile => 'v',
proparallel => 'u', prorettype => 'record', proargtypes => 'name name bool',
proallargtypes => '{name,name,bool,name,pg_lsn}',
proargmodes => '{i,i,i,o,o}',
proargnames => '{src_slot_name,dst_slot_name,temporary,slot_name,lsn}',
prosrc => 'pg_copy_logical_replication_slot_b' },
{ oid => '4224', descr => 'copy a logical replication slot',
proname => 'pg_copy_logical_replication_slot', provolatile => 'v',
proparallel => 'u', prorettype => 'record', proargtypes => 'name name',
proallargtypes => '{name,name,name,pg_lsn}',
proargmodes => '{i,i,o,o}',
proargnames => '{src_slot_name,dst_slot_name,slot_name,lsn}',
prosrc => 'pg_copy_logical_replication_slot_c' },
{ oid => '3782', descr => 'get changes from replication slot',
proname => 'pg_logical_slot_get_changes', procost => '1000',
prorows => '1000', provariadic => 'text', proisstrict => 'f',
......
......@@ -97,6 +97,7 @@ extern void CheckLogicalDecodingRequirements(void);
extern LogicalDecodingContext *CreateInitDecodingContext(char *plugin,
List *output_plugin_options,
bool need_full_snapshot,
XLogRecPtr restart_lsn,
XLogPageReadCB read_page,
LogicalOutputPluginWriterPrepareWrite prepare_write,
LogicalOutputPluginWriterWrite do_write,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment