pg_create_physical_replication_slot()

用于创建物理复制槽的函数

pg_create_physical_replication_slot() 是一个用于创建物理复制槽的系统函数。

pg_create_physical_replication_slot() 添加于 PostgreSQL 9.4

用法

PostgreSQL 10 及更高版本

pg_create_physical_replication_slot ( slot_name name [, immediately_reserve boolean, temporary boolean ] )
        → record ( slot_name name, lsn pg_lsn )

PostgreSQL 9.6:

pg_create_physical_replication_slot ( slot_name name [, immediately_reserve boolean )
        → record ( slot_name name, lsn pg_lsn )

PostgreSQL 9.4PostgreSQL 9.5

pg_create_physical_replication_slot ( slot_name name )
        → record ( slot_name name, lsn pg_lsn )

槽名必须仅包含小写字母、数字和下划线字符的组合,最多可达 63 个字符(NAMEDATALEN)。如果名称超过此限制,它将被静默截断为 63 个字符。

如果提供了 immediately_reserve 参数并将其设置为 TRUE,则将从最近一次 检查点 开始保留 WAL。

如果提供了 temporary 参数并将其设置为 TRUE,则复制槽将不会永久存储,并且仅在当前会话期间可用。

变更历史

示例

使用 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.

分类

复制, 复制槽, 系统函数, WAL

另请参阅

pg_replication_slots, pg_copy_physical_replication_slot(), pg_drop_replication_slot(), pg_create_logical_replication_slot()

反馈

在此处提交有关“pg_create_physical_replication_slot()”的任何评论、建议或更正 此处