pg_copy_physical_replication_slot()
是一个用于复制现有物理复制槽的系统函数。
pg_copy_physical_replication_slot()
在 PostgreSQL 12 中添加。
用法
pg_copy_physical_replication_slot ( src_slot_name name, dst_slot_name name [, temporary boolean ] ) → record ( slot_name name, lsn pg_lsn )
要复制的槽必须使用预留 WAL 创建(即 pg_create_physical_replication_slot()
执行时 immediately_reserve
设置为 TRUE
)。
变更历史
- PostgreSQL 12
- 添加 (提交 9f06d79e)
示例
使用 pg_copy_physical_replication_slot()
复制复制槽
postgres=# SELECT * FROM pg_create_physical_replication_slot('foo', immediately_reserve := TRUE); slot_name | lsn -----------+----------- foo | 0/30004B0 (1 row) postgres=# SELECT * FROM pg_copy_physical_replication_slot('foo', 'bar'); slot_name | lsn -----------+----- bar | (1 row)
请注意,从 PostgreSQL 14 开始,pg_copy_physical_replication_slot()
不会在 lsn
列中返回值,即使通过 pg_replication_slots 报告了一个值。
postgres=# SELECT slot_name, slot_type, restart_lsn, wal_status FROM pg_replication_slots; slot_name | slot_type | restart_lsn | wal_status -----------+-----------+-------------+------------ foo | physical | 0/30004B0 | reserved bar | physical | 0/30004B0 | reserved (2 rows)
源复制槽必须使用预留 WAL 创建
postgres=# SELECT * FROM pg_create_physical_replication_slot('boo'); slot_name | lsn -----------+----- boo | (1 row) postgres=# SELECT * FROM pg_copy_physical_replication_slot('boo', 'baz'); ERROR: cannot copy a replication slot that doesn't reserve WAL
尝试复制不存在的复制槽
postgres=# SELECT * FROM pg_copy_physical_replication_slot('zoo', 'baz'); ERROR: replication slot "zoo" does not exist
参考文献
- PostgreSQL 文档: 复制管理函数