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(即 immediately_reserve 设置为 TRUE 执行了 pg_create_physical_replication_slot())创建。
变更历史
- 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_replication_slots 报告了 LSN,pg_copy_physical_replication_slot() 在 lsn 列中也不会返回值。
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 文档: 复制管理函数
