pg_create_physical_replication_slot() 是一个用于创建物理复制槽的系统函数。
pg_create_physical_replication_slot() 添加于 PostgreSQL 9.4。
用法
PostgreSQL 10 及更高版本
pg_create_physical_replication_slot (slot_namename[,immediately_reserveboolean,temporaryboolean] )
→ record (slot_namename,lsnpg_lsn)
pg_create_physical_replication_slot (slot_namename[,immediately_reserveboolean)
→ record (slot_namename,lsnpg_lsn)
PostgreSQL 9.4 和 PostgreSQL 9.5
pg_create_physical_replication_slot (slot_namename)
→ record (slot_namename,lsnpg_lsn)
槽名必须仅包含小写字母、数字和下划线字符的组合,最多可达 63 个字符(NAMEDATALEN)。如果名称超过此限制,它将被静默截断为 63 个字符。
如果提供了 immediately_reserve 参数并将其设置为 TRUE,则将从最近一次 检查点 开始保留 WAL。
如果提供了 temporary 参数并将其设置为 TRUE,则复制槽将不会永久存储,并且仅在当前会话期间可用。
变更历史
- PostgreSQL 10
- 为创建临时复制槽添加了可选参数
temporary(提交 a924c327)
- 为创建临时复制槽添加了可选参数
- PostgreSQL 9.6
- 添加了可选参数
immediately_reserve以从槽创建之时起保留 WAL(提交 6fcd8851)
- 添加了可选参数
- PostgreSQL 9.4
- 添加 (提交 858ec118)
示例
使用 pg_create_physical_replication_slot() 创建复制槽
postgres=# SELECT * FROM pg_create_physical_replication_slot('foo');
slot_name | lsn
-----------+-----
foo |
(1 row)
创建复制槽并确保从创建槽的那个时间点开始保留 WAL
postgres=# SELECT cc.checkpoint_lsn,
cprs.*
FROM pg_control_checkpoint() cc,
pg_create_physical_replication_slot(
slot_name := 'bar',
immediately_reserve := TRUE
) cprs;
checkpoint_lsn | slot_name | lsn
----------------+-----------+-----------
0/30004E8 | bar | 0/30004B0
(1 row)
创建临时复制槽;请注意,槽在 pg_replslot 目录中的文件仅在当前会话期间有效
postgres=# SELECT * FROM pg_create_physical_replication_slot('tmp_slot', temporary := TRUE);
slot_name | lsn
-----------+-----
tmp_slot |
(1 row)
postgres=# SELECT pg_ls_dir('./pg_replslot');
pg_ls_dir
-----------
tmp_slot
(1 row)
postgres=# \c - postgres
You are now connected to database "postgres" as user "postgres".
postgres=# SELECT pg_ls_dir('./pg_replslot');
pg_ls_dir
-----------
(0 rows)
尝试创建一个长度为 64 个或更多字符的复制槽名称
postgres=# SELECT * FROM pg_create_physical_replication_slot(repeat('x', 100));
slot_name | lsn
-----------------------------------------------------------------+-----
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
(1 row)
尝试创建一个包含无效字符的复制槽名称
postgres=# SELECT * FROM pg_create_physical_replication_slot('!"#$ ');
ERROR: replication slot name "!"#$ " contains invalid character
HINT: Replication slot names may only contain lower case letters, numbers, and the underscore character.
参考资料
- PostgreSQL 文档: 复制管理函数
