Postgresql 中文操作指南

CREATE SUBSCRIPTION

CREATE SUBSCRIPTION — 定义新订阅

CREATE SUBSCRIPTION — define a new subscription

Synopsis

CREATE SUBSCRIPTION subscription_name
    CONNECTION 'conninfo'
    PUBLICATION publication_name [, ...]
    [ WITH ( subscription_parameter [= value] [, ... ] ) ]

Description

CREATE SUBSCRIPTION 添加新的逻辑复制订阅。创建订阅的用户将成为订阅的所有者。订阅名称必须不同于当前数据库中任何现有订阅的名称。

CREATE SUBSCRIPTION adds a new logical-replication subscription. The user that creates a subscription becomes the owner of the subscription. The subscription name must be distinct from the name of any existing subscription in the current database.

订阅表示与发布者的复制连接。因此,除了在本地目录中添加定义外,此命令通常还会在发布者上创建一个复制槽。

A subscription represents a replication connection to the publisher. Hence, in addition to adding definitions in the local catalogs, this command normally creates a replication slot on the publisher.

逻辑复制工作程序将在运行此命令的事务提交时开始复制新订阅的数据,除非订阅最初已禁用。

A logical replication worker will be started to replicate data for the new subscription at the commit of the transaction where this command is run, unless the subscription is initially disabled.

要能够创建订阅,您必须拥有 pg_create_subscription 角色的权限,以及对当前数据库的 CREATE 权限。

To be able to create a subscription, you must have the privileges of the the pg_create_subscription role, as well as CREATE privileges on the current database.

有关订阅和逻辑复制整体的更多信息,请访问 Section 31.2Chapter 31

Additional information about subscriptions and logical replication as a whole is available at Section 31.2 and Chapter 31.

Parameters

  • subscription_name #

    • The name of the new subscription.

  • CONNECTION '_conninfo'_ #

    • The libpq connection string defining how to connect to the publisher database. For details see Section 34.1.1.

  • PUBLICATION _publication_name [, …​]_ #

    • Names of the publications on the publisher to subscribe to.

  • WITH ( _subscription_parameter [= value] [, …​ ] )_ #

    • This clause specifies optional parameters for a subscription.

    • The following parameters control what happens during subscription creation:

    • The following parameters control the subscription’s replication behavior after it has been created:

  • connect (boolean) #

    • Specifies whether the CREATE SUBSCRIPTION command should connect to the publisher at all. The default is true. Setting this to false will force the values of create_slot, enabled and copy_data to false. (You cannot combine setting connect to false with setting create_slot, enabled, or copy_data to true.)

    • Since no connection is made when this option is false, no tables are subscribed. To initiate replication, you must manually create the replication slot, enable the subscription, and refresh the subscription. See Section 31.2.3 for examples.

  • create_slot (boolean) #

    • Specifies whether the command should create the replication slot on the publisher. The default is true.

    • If set to false, you are responsible for creating the publisher’s slot in some other way. See Section 31.2.3 for examples.

  • enabled (boolean) #

    • Specifies whether the subscription should be actively replicating or whether it should just be set up but not started yet. The default is true.

  • slot_name (string) #

    • Name of the publisher’s replication slot to use. The default is to use the name of the subscription for the slot name.

    • Setting slot_name to NONE means there will be no replication slot associated with the subscription. Such subscriptions must also have both enabled and create_slot set to false. Use this when you will be creating the replication slot later manually. See Section 31.2.3 for examples.

  • binary (boolean) #

    • Specifies whether the subscription will request the publisher to send the data in binary format (as opposed to text). The default is false. Any initial table synchronization copy (see copy_data) also uses the same format. Binary format can be faster than the text format, but it is less portable across machine architectures and PostgreSQL versions. Binary format is very data type specific; for example, it will not allow copying from a smallint column to an integer column, even though that would work fine in text format. Even when this option is enabled, only data types having binary send and receive functions will be transferred in binary. Note that the initial synchronization requires all data types to have binary send and receive functions, otherwise the synchronization will fail (see CREATE TYPE for more about send/receive functions).

    • When doing cross-version replication, it could be that the publisher has a binary send function for some data type, but the subscriber lacks a binary receive function for that type. In such a case, data transfer will fail, and the binary option cannot be used.

    • If the publisher is a PostgreSQL version before 16, then any initial table synchronization will use text format even if binary = true.

  • copy_data (boolean) #

    • Specifies whether to copy pre-existing data in the publications that are being subscribed to when the replication starts. The default is true.

    • If the publications contain WHERE clauses, it will affect what data is copied. Refer to the Notes for details.

    • See Notes for details of how copy_data = true can interact with the origin parameter.

  • streaming (enum) #

    • Specifies whether to enable streaming of in-progress transactions for this subscription. The default value is off, meaning all transactions are fully decoded on the publisher and only then sent to the subscriber as a whole.

    • If set to on, the incoming changes are written to temporary files and then applied only after the transaction is committed on the publisher and received by the subscriber.

    • If set to parallel, incoming changes are directly applied via one of the parallel apply workers, if available. If no parallel apply worker is free to handle streaming transactions then the changes are written to temporary files and applied after the transaction is committed. Note that if an error happens in a parallel apply worker, the finish LSN of the remote transaction might not be reported in the server log.

  • synchronous_commit (enum) #

    • The value of this parameter overrides the synchronous_commit setting within this subscription’s apply worker processes. The default value is off.

    • It is safe to use off for logical replication: If the subscriber loses transactions because of missing synchronization, the data will be sent again from the publisher.

    • A different setting might be appropriate when doing synchronous logical replication. The logical replication workers report the positions of writes and flushes to the publisher, and when using synchronous replication, the publisher will wait for the actual flush. This means that setting synchronous_commit for the subscriber to off when the subscription is used for synchronous replication might increase the latency for COMMIT on the publisher. In this scenario, it can be advantageous to set synchronous_commit to local or higher.

  • two_phase (boolean) #

    • Specifies whether two-phase commit is enabled for this subscription. The default is false.

    • When two-phase commit is enabled, prepared transactions are sent to the subscriber at the time of PREPARE TRANSACTION, and are processed as two-phase transactions on the subscriber too. Otherwise, prepared transactions are sent to the subscriber only when committed, and are then processed immediately by the subscriber.

    • The implementation of two-phase commit requires that replication has successfully finished the initial table synchronization phase. So even when two_phase is enabled for a subscription, the internal two-phase state remains temporarily “pending” until the initialization phase completes. See column subtwophasestate of pg_subscription to know the actual two-phase state.

  • disable_on_error (boolean) #

    • Specifies whether the subscription should be automatically disabled if any errors are detected by subscription workers during data replication from the publisher. The default is false.

  • password_required (boolean) #

    • If set to true, connections to the publisher made as a result of this subscription must use password authentication and the password must be specified as a part of the connection string. This setting is ignored when the subscription is owned by a superuser. The default is true. Only superusers can set this value to false.

  • run_as_owner (boolean) #

    • If true, all replication actions are performed as the subscription owner. If false, replication workers will perform actions on each table as the owner of that table. The latter configuration is generally much more secure; for details, see Section 31.9. The default is false.

  • origin (string) #

    • Specifies whether the subscription will request the publisher to only send changes that don’t have an origin or send changes regardless of origin. Setting origin to none means that the subscription will request the publisher to only send changes that don’t have an origin. Setting origin to any means that the publisher sends changes regardless of their origin. The default is any.

    • See Notes for details of how copy_data = true can interact with the origin parameter.

在指定类型为 boolean 的参数时,可以省略 = value 部分,这等同于指定 TRUE

When specifying a parameter of type boolean, the = value part can be omitted, which is equivalent to specifying TRUE.

Notes

有关如何配置订阅和发布实例之间的访问控制的详细信息,请参阅 Section 31.9

See Section 31.9 for details on how to configure access control between the subscription and the publication instance.

创建复制槽(默认行为)时,无法在事务块中执行 CREATE SUBSCRIPTION

When creating a replication slot (the default behavior), CREATE SUBSCRIPTION cannot be executed inside a transaction block.

仅当复制槽没有创建为同一命令的一部分时,创建连接到同一数据库集群的订阅(例如,在同一集群中的数据库之间复制或在同一数据库中复制)才会成功。否则, CREATE SUBSCRIPTION 调用将挂起。要使其正常工作,请分别创建复制槽(使用带有插件名称 pgoutput 的函数 pg_create_logical_replication_slot )并使用参数 create_slot = false 创建订阅。具体示例,请参阅 Section 31.2.3 。这是一项实现限制,可能会在未来的版本中取消。

Creating a subscription that connects to the same database cluster (for example, to replicate between databases in the same cluster or to replicate within the same database) will only succeed if the replication slot is not created as part of the same command. Otherwise, the CREATE SUBSCRIPTION call will hang. To make this work, create the replication slot separately (using the function pg_create_logical_replication_slot with the plugin name pgoutput) and create the subscription using the parameter create_slot = false. See Section 31.2.3 for examples. This is an implementation restriction that might be lifted in a future release.

如果发布中的任何表都有 WHERE 子句,则 expression 求值为 false 或 null 的行将不会发布。如果订阅有多个发布,并且在其中同一张表已使用不同的 WHERE 子句发布,则只要任何表达式(引用该发布操作)满足,该行都将发布。在使用不同的 WHERE 子句的情况下,如果某个发布没有 WHERE 子句(引用该发布操作)或发布被声明为 FOR ALL TABLESFOR TABLES IN SCHEMA ,则无论其他表达式的定义如何,总是发布行。如果订阅者是版本低于 15 的 PostgreSQL,则在初始数据同步阶段将忽略任何行筛选。对于这种情况,用户可能希望考虑删除任何与后续筛选不兼容的最初复制的数据。由于初始数据同步在复制现有表数据时不考虑发布 publish 参数,因此可能会复制一些不会使用 DML 复制的行。有关示例,请参阅 Section 31.2.2

If any table in the publication has a WHERE clause, rows for which the expression evaluates to false or null will not be published. If the subscription has several publications in which the same table has been published with different WHERE clauses, a row will be published if any of the expressions (referring to that publish operation) are satisfied. In the case of different WHERE clauses, if one of the publications has no WHERE clause (referring to that publish operation) or the publication is declared as FOR ALL TABLES or FOR TABLES IN SCHEMA, rows are always published regardless of the definition of the other expressions. If the subscriber is a PostgreSQL version before 15, then any row filtering is ignored during the initial data synchronization phase. For this case, the user might want to consider deleting any initially copied data that would be incompatible with subsequent filtering. Because initial data synchronization does not take into account the publication publish parameter when copying existing table data, some rows may be copied that would not be replicated using DML. See Section 31.2.2 for examples.

不支持在同一表已使用不同的列列表发布的多个发布中订阅。

Subscriptions having several publications in which the same table has been published with different column lists are not supported.

我们允许指定不存在的发布,以便用户稍后添加这些发布。这意味着 pg_subscription 可能有不存在的发布。

We allow non-existent publications to be specified so that users can add those later. This means pg_subscription can have non-existent publications.

当使用 copy_data = trueorigin = NONE 的订阅参数组合时,初始同步表数据直接从发布者复制,这意味着不可能了解该数据的真实来源。如果发布者也有订阅,那么复制的表数据可能源自更上游。场景已被检测到,并且已记录了 WARNING 给用户,但该警告仅表示存在潜在问题;用户有责任执行必要的检查以确保复制的数据源是否确实如预期的那样。

When using a subscription parameter combination of copy_data = true and origin = NONE, the initial sync table data is copied directly from the publisher, meaning that knowledge of the true origin of that data is not possible. If the publisher also has subscriptions then the copied table data might have originated from further upstream. This scenario is detected and a WARNING is logged to the user, but the warning is only an indication of a potential problem; it is the user’s responsibility to make the necessary checks to ensure the copied data origins are really as wanted or not.

要查找哪些表可能包含非本地源(由于在发布者上创建的其他订阅),请尝试此 SQL 查询:

To find which tables might potentially include non-local origins (due to other subscriptions created on the publisher) try this SQL query:

# substitute <pub-names> below with your publication name(s) to be queried
SELECT DISTINCT PT.schemaname, PT.tablename
FROM pg_publication_tables PT,
     pg_subscription_rel PS
     JOIN pg_class C ON (C.oid = PS.srrelid)
     JOIN pg_namespace N ON (N.oid = C.relnamespace)
WHERE N.nspname = PT.schemaname AND
      C.relname = PT.tablename AND
      PT.pubname IN (<pub-names>);

Examples

创建一个对远程服务器的订阅,该服务器复制 mypublicationinsert_only 发布中的表并立即开始在提交时进行复制:

Create a subscription to a remote server that replicates tables in the publications mypublication and insert_only and starts replicating immediately on commit:

CREATE SUBSCRIPTION mysub
         CONNECTION 'host=192.168.1.50 port=5432 user=foo dbname=foodb'
        PUBLICATION mypublication, insert_only;

创建一个对远程服务器的订阅,该服务器复制 insert_only 发布中的表并且在稍后启用之前不开始复制。

Create a subscription to a remote server that replicates tables in the insert_only publication and does not start replicating until enabled at a later time.

CREATE SUBSCRIPTION mysub
         CONNECTION 'host=192.168.1.50 port=5432 user=foo dbname=foodb'
        PUBLICATION insert_only
               WITH (enabled = false);

Compatibility

CREATE SUBSCRIPTION 是一个 PostgreSQL 扩展。

CREATE SUBSCRIPTION is a PostgreSQL extension.