pg_create_logical_replication_slot()
是一个用于创建逻辑复制槽的系统函数。
pg_create_logical_replication_slot()
添加于 PostgreSQL 9.4。
用法
pg_create_logical_replication_slot (slot_name
name
,plugin
name
[,temporary
boolean
,two_phase
boolean
,failover
boolean
] )
→ record (slot_name
name
,lsn
pg_lsn
)
pg_create_logical_replication_slot (slot_name
name
,plugin
name
[,temporary
boolean
,two_phase
boolean
] )
→ record (slot_name
name
,lsn
pg_lsn
)
pg_create_logical_replication_slot (slot_name
name
,plugin
name
[,temporary
boolean
] )
→ record (slot_name
name
,lsn
pg_lsn
)
PostgreSQL 9.4 ~ PostgreSQL 9.6
pg_create_logical_replication_slot (slot_name
name
,plugin
name
)
→ record (slot_name
name
,lsn
pg_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 文档: 复制管理函数