↑↓ 选择 ↵ 打开 ⌫ 改范围 完整检索页

pgsql.cc 提供对 postgresql.org 官网内容的中文翻译,由 Pigsty 团队维护。

百科 / 命令行工具 / 服务端程序

pg_createsubscriber

pg_createsubscriber — 将物理副本转换为新的逻辑副本

当前查看 PostgreSQL 18.6。

说明

pg_createsubscriber — 将物理副本转换为新的逻辑副本

手册中的可执行程序
pg_createsubscriber
程序版本
18.6
参考清单
服务端程序
选项定义组
18

用法

pg_createsubscriber [ option ...] { -d | --database } dbname { -D | --pgdata } datadir { -P | --publisher-server } connstr

手册中的选项

选项与参数说明
-a --all在目标服务器上的每个数据库中创建一个订阅。模板数据库以及不允许连接的数据库除外。为发现所有数据库的列表,工具会使用 --publisher-server 连接字符串中指定的数据库名连接到源服务器;如果未指定,则使用 postgres 数据库;如果该数据库不存在,则使用 template1 。指定此选项时,会使用自动生成的订阅、发布和复制槽名称。此选项不能与 --database 、 --publication 、 --replication-slot 或 --subscription 一起使用。
-d dbname --database= dbname要在其中创建订阅的数据库名称。通过多次指定 -d 可以选择多个数据库。此选项不能与 -a 一起使用。如果未提供 -d 选项,数据库名将从 -P 选项中获取。如果在 -d 选项或 -P 选项中都未指定数据库名,且又未指定 -a 选项,则会报告错误。
-D directory --pgdata= directory包含物理副本中的集簇目录的目标目录。
-n --dry-run执行除实际修改目标目录之外的所有步骤。
-p port --subscriber-port= port目标服务器监听连接的端口号。默认为让目标服务器在 50432 端口上运行,以避免意外的客户端连接。
-P connstr --publisher-server= connstr到发布者的连接字符串。详情见 第 32.1.1 节 。
-s dir --socketdir= dir目标服务器上 postmaster 套接字所使用的目录。默认值为当前目录。
-t seconds --recovery-timeout= seconds等待恢复结束的最长时间,单位为秒。设为 0 表示禁用该限制。默认值为 0。
-T --enable-two-phase为订阅启用 two_phase 两阶段提交。当指定多个数据库时,此选项会统一应用于在这些数据库上创建的所有订阅。默认值为 false 。
-U username --subscriber-username= username连接目标服务器所使用的用户名。默认是当前操作系统用户名。
-v --verbose启用详细模式。这将使 pg_createsubscriber 向标准错误输出进度消息以及每个步骤的详细信息。重复指定该选项会让更多调试级消息出现在标准错误中。
--clean= objtype从目标服务器上的指定数据库中删除指定类型的所有对象。
--config-file= filename为目标数据目录使用指定的主配置文件。 pg_createsubscriber 在内部使用 pg_ctl 命令来启动和停止目标服务器。如果实际的 postgresql.conf 配置文件存放在数据目录之外,此选项允许你显式指定它。
--publication= name用于建立逻辑复制的发布名称。通过多次指定 --publication 可以指定多个发布。发布名称的数量必须与指定的数据库数量一致,否则会报告错误。多个发布名称开关的顺序必须与数据库开关的顺序一致。如果未指定此选项,则会为发布分配一个生成的名称。此选项不能与 --all 一起使用。
--replication-slot= name用于建立逻辑复制的复制槽名称。通过多次指定 --replication-slot 可以指定多个复制槽。复制槽名称的数量必须与指定的数据库数量一致,否则会报告错误。多个复制槽名称开关的顺序必须与数据库开关的顺序一致。如果未指定此选项,则使用订阅名称作为复制槽名称。此选项不能与 --all 一起使用。
--subscription= name用于建立逻辑复制的订阅名称。通过多次指定 --subscription 可以指定多个订阅。订阅名称的数量必须与指定的数据库数量一致,否则会报告错误。多个订阅名称开关的顺序必须与数据库开关的顺序一致。如果未指定此选项,则会为订阅分配一个生成的名称。此选项不能与 --all 一起使用。
-V --version打印 pg_createsubscriber 版本并退出。
-? --help显示 pg_createsubscriber 命令行参数的帮助并退出。

手册定义

pg_createsubscriber

pg_createsubscriber — 将物理副本转换为新的逻辑副本

大纲

pg_createsubscriber [option...] { -d | --database }dbname { -D | --pgdata }datadir { -P | --publisher-server }connstr

说明

pg_createsubscriber从物理备库创建新的逻辑副本。指定数据库中的所有表都会包含在逻辑复制配置中。每个数据库都会创建一对发布和订阅对象。该工具必须在目标服务器上运行。

成功运行后,目标服务器的状态类似于一个全新的逻辑复制配置。逻辑复制配置与pg_createsubscriber之间的主要区别在于数据同步的完成方式。pg_createsubscriber 不会复制初始表数据。它只执行同步阶段,以确保每个表都达到同步状态。

pg_createsubscriber主要面向大型数据库系统,因为在逻辑复制配置中,大部分时间都花在复制初始数据上。此外,在数据同步上花费较长时间的一个副作用通常是,会有大量在初始数据复制期间产生的更改需要应用,这会进一步延后逻辑副本可用的时间。对于较小的数据库,建议建立带初始数据同步的逻辑复制。详见CREATE SUBSCRIPTION的 copy_data选项。

选项

pg_createsubscriber接受以下命令行参数:

-a
--all

在目标服务器上的每个数据库中创建一个订阅。模板数据库以及不允许连接的数据库除外。为发现所有数据库的列表,工具会使用--publisher-server 连接字符串中指定的数据库名连接到源服务器;如果未指定,则使用 postgres数据库;如果该数据库不存在,则使用 template1。指定此选项时,会使用自动生成的订阅、发布和复制槽名称。此选项不能与--database、--publication、--replication-slot或--subscription一起使用。

-d dbname
--database=dbname

要在其中创建订阅的数据库名称。通过多次指定-d 可以选择多个数据库。此选项不能与-a一起使用。如果未提供-d选项,数据库名将从-P 选项中获取。如果在-d选项或-P 选项中都未指定数据库名,且又未指定-a选项,则会报告错误。

-D directory
--pgdata=directory

包含物理副本中的集簇目录的目标目录。

-n
--dry-run

执行除实际修改目标目录之外的所有步骤。

-p port
--subscriber-port=port

目标服务器监听连接的端口号。默认为让目标服务器在 50432 端口上运行,以避免意外的客户端连接。

-P connstr
--publisher-server=connstr

到发布者的连接字符串。详情见第 32.1.1 节。

-s dir
--socketdir=dir

目标服务器上 postmaster 套接字所使用的目录。默认值为当前目录。

-t seconds
--recovery-timeout=seconds

等待恢复结束的最长时间,单位为秒。设为 0 表示禁用该限制。默认值为 0。

-T
--enable-two-phase

为订阅启用 two_phase 两阶段提交。当指定多个数据库时,此选项会统一应用于在这些数据库上创建的所有订阅。默认值为false。

-U username
--subscriber-username=username

连接目标服务器所使用的用户名。默认是当前操作系统用户名。

-v
--verbose

启用详细模式。这将使 pg_createsubscriber向标准错误输出进度消息以及每个步骤的详细信息。重复指定该选项会让更多调试级消息出现在标准错误中。

--clean=objtype

从目标服务器上的指定数据库中删除指定类型的所有对象。

    • publications:为该订阅者建立的FOR ALL TABLES发布总是会被删除;指定此对象类型还会删除从源服务器复制过来的其他所有发布。

被选中要删除的对象都会逐个记录到日志中,包括在--dry-run 期间也是如此。没有机会干预或停止这些对象的删除,因此可以考虑使用 pg_dump先对它们进行备份。

--config-file=filename

为目标数据目录使用指定的主配置文件。pg_createsubscriber在内部使用 pg_ctl命令来启动和停止目标服务器。如果实际的postgresql.conf配置文件存放在数据目录之外,此选项允许你显式指定它。

--publication=name

用于建立逻辑复制的发布名称。通过多次指定--publication 可以指定多个发布。发布名称的数量必须与指定的数据库数量一致,否则会报告错误。多个发布名称开关的顺序必须与数据库开关的顺序一致。如果未指定此选项,则会为发布分配一个生成的名称。此选项不能与 --all一起使用。

--replication-slot=name

用于建立逻辑复制的复制槽名称。通过多次指定--replication-slot 可以指定多个复制槽。复制槽名称的数量必须与指定的数据库数量一致,否则会报告错误。多个复制槽名称开关的顺序必须与数据库开关的顺序一致。如果未指定此选项,则使用订阅名称作为复制槽名称。此选项不能与 --all一起使用。

--subscription=name

用于建立逻辑复制的订阅名称。通过多次指定--subscription 可以指定多个订阅。订阅名称的数量必须与指定的数据库数量一致,否则会报告错误。多个订阅名称开关的顺序必须与数据库开关的顺序一致。如果未指定此选项,则会为订阅分配一个生成的名称。此选项不能与 --all一起使用。

-V
--version

打印pg_createsubscriber版本并退出。

-?
--help

显示pg_createsubscriber命令行参数的帮助并退出。

注解

前置条件

要让pg_createsubscriber将目标服务器转换为逻辑副本,需要满足一些前提条件。如果不满足这些条件,就会报告错误。源服务器和目标服务器的主版本必须与 pg_createsubscriber相同。给定的目标数据目录必须与源数据目录具有相同的系统标识符。为目标数据目录指定的数据库用户必须具备创建订阅以及使用pg_replication_origin_advance() 的权限。

目标服务器必须作为物理备库使用。目标服务器必须将max_active_replication_origins和max_logical_replication_workers配置为大于等于指定数据库数量的值。目标服务器必须将max_worker_processes配置为大于指定数据库数量的值。目标服务器必须接受本地连接。如果计划使用--enable-two-phase 开关,还需要适当地设置max_prepared_transactions。

源服务器必须接受来自目标服务器的连接。源服务器不能处于恢复中。源服务器必须将wal_level设置为logical。源服务器必须将max_replication_slots配置为大于等于指定数据库数量加现有复制槽数量的值。源服务器必须将max_wal_senders配置为大于等于指定数据库数量与现有 WAL 发送进程数量之和的值。

警告

若pg_createsubscriber在目标服务器被提升后失败,数据目录很可能已处于不可恢复状态。此时建议重新创建新的备库。

在转换过程中,pg_createsubscriber通常会使用不同的连接设置来启动目标服务器。因此,对目标服务器的连接应该会失败。

由于逻辑复制不复制 DDL 命令,运行pg_createsubscriber期间应避免执行会更改数据库模式的 DDL 命令。若目标服务器已转换为逻辑副本,相关 DDL 可能不会被复制,从而引发错误。

若pg_createsubscriber处理过程中失败,会删除在源服务器上创建的对象(发布、复制槽)。如果目标服务器无法连接到源服务器,删除可能失败。在这种情况下,警告消息会提示遗留的对象。如果目标服务器正在运行,它会被停止。

若复制使用了primary_slot_name,在逻辑复制配置完成后会从源服务器移除该复制槽。

如果目标服务器是同步副本,运行pg_createsubscriber期间,主库上的事务提交可能会在等待复制时阻塞。

除非指定--enable-two-phase,pg_createsubscriber会在禁用两阶段提交的情况下建立逻辑复制。这意味着任何预备事务都会在COMMIT PREPARED时被复制,而不会事先进行预备。配置完成后,你可以手动删除并重新创建订阅,并启用two_phase 选项。

pg_createsubscriber会使用pg_resetwal 修改系统标识符。这样可以避免目标服务器可能使用源服务器的 WAL 文件。如果目标服务器还有备库,复制将会中断,应创建一个新的备库。

若缺少必需 WAL 文件,复制可能失败。为避免该问题,源服务器必须将max_slot_wal_keep_size设置为-1,以确保必需 WAL 文件不会被提前移除。

工作原理

基本思路是从源服务器获得复制起点,并从该位置开始建立逻辑复制:

  1. 使用指定命令行选项启动目标服务器。若目标服务器已在运行,pg_createsubscriber会报错终止。

  2. 检查目标服务器是否可以转换,同时也会对源服务器进行一些检查。若任一前置条件不满足,pg_createsubscriber会报错终止。

  3. 在源服务器上为每个指定的数据库创建一个发布和一个复制槽。每个发布都以FOR ALL TABLES创建。若未指定--publication,发布名称模式为“pg_createsubscriber_%u_%x”(参数:数据库oid、随机int)。若未指定--replication-slot,复制槽的名称模式如下:“pg_createsubscriber_%u_%x”(参数:数据库oid、随机int)。这些复制槽将在后续步骤中被订阅使用。最后一个复制槽的 LSN 会在recovery_target_lsn参数中用作停止点,也会被订阅用作复制起点。这样可以保证不会丢失任何事务。

  4. 将恢复参数写入目标数据目录并重启目标服务器。它指定了恢复将推进到的预写式日志位置的 LSN(recovery_target_lsn)。它还将promote指定为服务器在达到恢复目标后应执行的动作。还会添加其他恢复参数,以避免恢复过程中出现意外行为,例如一达到一致状态就结束恢复(WAL 应继续应用到复制起始位置),或者因指定多个恢复目标而失败。当服务器退出备库模式并接受读写事务时,该步骤结束。如果设置了--recovery-timeout选项,而恢复在给定秒数内没有结束,pg_createsubscriber就会终止。

  5. 在目标服务器上为每个指定数据库创建订阅。若未指定--subscription,名称模式为“pg_createsubscriber_%u_%x”(参数:数据库oid、随机int)。该订阅不会复制源服务器上的现有数据,也不会创建复制槽,而是使用前一步中创建的复制槽。订阅会被创建,但尚不启用,因为必须在启动复制之前先将复制进度设置到复制起点。

  6. 删除在目标服务器上被复制过来的发布(这些发布是在复制起点前创建的),它们在订阅者上没有用途。

  7. 将每个订阅的复制进度设置为复制起点。当目标服务器开始恢复过程时,它会追赶到复制起点。这正是每个订阅要用作初始复制位置的 LSN。由于订阅已经创建,因此可以取得复制源名称。使用复制源名称和复制起点调用 pg_replication_origin_advance() 以设置初始复制位置。

  8. 在目标服务器上启用每个指定数据库的订阅。订阅将从复制起点开始应用事务。

  9. 若备库使用了primary_slot_name,该复制槽之后不再有用,故将其删除。

  10. 若备库包含故障切换复制槽,它们后续无法继续同步,故将其删除。

  11. 更新目标服务器上的系统标识符。会运行pg_resetwal来修改系统标识符。由于pg_resetwal的要求,目标服务器会被停止。

示例

从 foo 上的物理副本为 hr 和 finance 数据库创建逻辑副本:

$ pg_createsubscriber -D /usr/local/pgsql/data -P "host=foo" -d hr -d finance

文档与源码

来源构建
版本
18.6
构建
https://ftp.postgresql.org/pub/source/v18.6/postgresql-18.6.tar.bz2
来源指纹
ee8d1a3612338fd9adf250730cb640fcc5233b5491337cc00a316a44e3a0b9f8

版本比较

PostgreSQL 16 → 17: 新增收录。

以下差异保留原始字段名与英文源描述。

--- PostgreSQL 16
+++ PostgreSQL 17
@@ -1 +1,124 @@
-该版未收录
+{
+  "environment": [],
+  "options": [
+    {
+      "description": "The name of the database in which to create a subscription. Multiple databases can be selected by writing multiple -d switches.",
+      "names": [
+        "-d dbname",
+        "--database= dbname"
+      ],
+      "signature": "-d dbname --database= dbname"
+    },
+    {
+      "description": "The target directory that contains a cluster directory from a physical replica.",
+      "names": [
+        "-D directory",
+        "--pgdata= directory"
+      ],
+      "signature": "-D directory --pgdata= directory"
+    },
+    {
+      "description": "Do everything except actually modifying the target directory.",
+      "names": [
+        "-n",
+        "--dry-run"
+      ],
+      "signature": "-n --dry-run"
+    },
+    {
+      "description": "The port number on which the target server is listening for connections. Defaults to running the target server on port 50432 to avoid unintended client connections.",
+      "names": [
+        "-p port",
+        "--subscriber-port= port"
+      ],
+      "signature": "-p port --subscriber-port= port"
+    },
+    {
+      "description": "The connection string to the publisher. For details see Section 32.1.1 .",
+      "names": [
+        "-P connstr",
+        "--publisher-server= connstr"
+      ],
+      "signature": "-P connstr --publisher-server= connstr"
+    },
+    {
+      "description": "The directory to use for postmaster sockets on target server. The default is current directory.",
+      "names": [
+        "-s dir",
+        "--socketdir= dir"
+      ],
+      "signature": "-s dir --socketdir= dir"
+    },
+    {
+      "description": "The maximum number of seconds to wait for recovery to end. Setting to 0 disables. The default is 0.",
+      "names": [
+        "-t seconds",
+        "--recovery-timeout= seconds"
+      ],
+      "signature": "-t seconds --recovery-timeout= seconds"
+    },
+    {
+      "description": "The user name to connect as on target server. Defaults to the current operating system user name.",
+      "names": [
+        "-U username",
+        "--subscriber-username= username"
+      ],
+      "signature": "-U username --subscriber-username= username"
+    },
+    {
+      "description": "Enables verbose mode. This will cause pg_createsubscriber to output progress messages and detailed information about each step to standard error. Repeating the option causes additional debug-level messages to appear on standard error.",
+      "names": [
+        "-v",
+        "--verbose"
+      ],
+      "signature": "-v --verbose"
+    },
+    {
+      "description": "Use the specified main server configuration file for the target data directory. pg_createsubscriber internally uses the pg_ctl command to start and stop the target server. It allows you to specify the actual postgresql.conf configuration file if it is stored outside the data directory.",
+      "names": [
+        "--config-file= filename"
+      ],
+      "signature": "--config-file= filename"
+    },
+    {
+      "description": "The publication name to set up the logical replication. Multiple publications can be specified by writing multiple --publication switches. The number of publication names must match the number of specified databases, otherwise an error is reported. The order of the multiple publication name switches must match the order of database switches. If this option is not specified, a generated name is assigned to the publication name.",
+      "names": [
+        "--publication= name"
+      ],
+      "signature": "--publication= name"
+    },
+    {
+      "description": "The replication slot name to set up the logical replication. Multiple replication slots can be specified by writing multiple --replication-slot switches. The number of replication slot names must match the number of specified databases, otherwise an error is reported. The order of the multiple replication slot name switches must match the order of database switches. If this option is not specified, the subscription name is assigned to the replication slot name.",
+      "names": [
+        "--replication-slot= name"
+      ],
+      "signature": "--replication-slot= name"
+    },
+    {
+      "description": "The subscription name to set up the logical replication. Multiple subscriptions can be specified by writing multiple --subscription switches. The number of subscription names must match the number of specified databases, otherwise an error is reported. The order of the multiple subscription name switches must match the order of database switches. If this option is not specified, a generated name is assigned to the subscription name.",
+      "names": [
+        "--subscription= name"
+      ],
+      "signature": "--subscription= name"
+    },
+    {
+      "description": "Print the pg_createsubscriber version and exit.",
+      "names": [
+        "-V",
+        "--version"
+      ],
+      "signature": "-V --version"
+    },
+    {
+      "description": "Show help about pg_createsubscriber command line arguments, and exit.",
+      "names": [
+        "-?",
+        "--help"
+      ],
+      "signature": "-? --help"
+    }
+  ],
+  "synopsis": [
+    "pg_createsubscriber [ option ...] { -d | --database } dbname { -D | --pgdata } datadir { -P | --publisher-server } connstr"
+  ]
+}

比较已记录的接口与属性,排除来源指纹和构建元数据。某个样本中没有记录,不能据此判断实际引入或移除的版本。

相关条目

导出 JSON · 返回命令行工具 · 收录范围为 PostgreSQL 17 至 20;最早采样版本不一定是实际引入版本。