pg_create_logical_replication_slot() 是一个用于创建逻辑复制槽的系统函数。
pg_create_logical_replication_slot() 添加于 PostgreSQL 9.4。
用法
pg_create_logical_replication_slot (slot_namename,pluginname[,temporaryboolean,two_phaseboolean,failoverboolean] )
→ record (slot_namename,lsnpg_lsn)
pg_create_logical_replication_slot (slot_namename,pluginname[,temporaryboolean,two_phaseboolean] )
→ record (slot_namename,lsnpg_lsn)
pg_create_logical_replication_slot (slot_namename,pluginname[,temporaryboolean] )
→ record (slot_namename,lsnpg_lsn)
PostgreSQL 9.4 ~ PostgreSQL 9.6
pg_create_logical_replication_slot (slot_namename,pluginname)
→ record (slot_namename,lsnpg_lsn)
在 PostgreSQL 16 之前,pg_create_logical_replication_slot() 不能在备机上执行。
变更历史
- PostgreSQL 17
- 添加了
failover选项 (提交 c393308b)
- 添加了
- PostgreSQL 16
- 可以在备机上执行 (提交 0fdab27a)
- PostgreSQL 14
- 添加了
two_phase选项 (提交 19890a06)
- 添加了
- PostgreSQL 10
- 添加了
temporary选项 (提交 a924c327)
- 添加了
- PostgreSQL 9.4
- 添加 (提交 b89e1510)
示例
使用 pg_create_logical_replication_slot() 创建带有 test_decoding 示例输出插件的逻辑复制槽
postgres=# SELECT * FROM pg_create_logical_replication_slot('test_slot_1', 'test_decoding');
slot_name | lsn
-------------+-----------
test_slot_1 | 0/3000758
(1 row)
postgres=# SELECT * FROM pg_replication_slots\gx
-[ RECORD 1 ]-------+--------------
slot_name | test_slot_1
plugin | test_decoding
slot_type | logical
datoid | 13756
database | postgres
temporary | f
active | f
active_pid |
xmin |
catalog_xmin | 722
restart_lsn | 0/3000720
confirmed_flush_lsn | 0/3000758
wal_status | reserved
safe_wal_size |
two_phase | f
尝试在备机上执行 pg_create_logical_replication_slot() ( PostgreSQL 15 及更早版本)
postgres=# SELECT * FROM pg_create_logical_replication_slot('test_slot_1', 'test_decoding');
ERROR: logical decoding cannot be used while in recovery
尝试创建名称包含无效字符的逻辑复制槽
postgres=# SELECT * FROM pg_create_logical_replication_slot('!"#$ FOO', 'some_plugin');
ERROR: replication slot name "!"#$ FOO" contains invalid character
HINT: Replication slot names may only contain lower case letters, numbers, and the underscore character.
尝试使用不存在的插件创建逻辑复制槽
postgres=# SELECT * FROM pg_create_logical_replication_slot('test_slot', 'some_plugin');
ERROR: could not access file "some_plugin": No such file or directory
参考资料
- PostgreSQL 文档: 复制管理函数
