{"Entry":{"collection":"sql","key":"create-subscription","name":"CREATE SUBSCRIPTION","aliases":["createsubscription"],"metadata":{"aliases":["createsubscription"],"changed_in":["19"],"changes":[{"from":"9.6","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":[],"removed":[]},"status":"added","synopsis":null,"to":"10"},{"from":"10","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters"],"removed":[]},"status":"changed","synopsis":null,"to":"11"},{"from":"11","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","notes"],"removed":[]},"status":"changed","synopsis":null,"to":"12"},{"from":"13","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","notes"],"removed":[]},"status":"changed","synopsis":null,"to":"14"},{"from":"14","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","notes"],"removed":[]},"status":"changed","synopsis":null,"to":"15"},{"from":"15","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","notes"],"removed":[]},"status":"changed","synopsis":null,"to":"16"},{"from":"16","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","notes"],"removed":[]},"status":"changed","synopsis":null,"to":"17"},{"from":"17","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters","notes"],"removed":[]},"status":"changed","synopsis":null,"to":"18"},{"from":"18","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters","notes"],"removed":[]},"status":"changed","synopsis":{"added":["{ SERVER server_name | CONNECTION 'conninfo' }"],"removed":[]},"to":"19"},{"from":"19","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters"],"removed":[]},"status":"changed","synopsis":null,"to":"20"}],"content_hash":"261e8dab62f664d76d940430f64a9f423990856fb3c4c0fae640e8e7852353b3","derived_content_hash":"6489871a9da1eefb5dfae8663834181d51c44e8d673633c280993e5859095414","editorial":{},"first_version":"10","group":"replication","imported_at":"2026-09-30T17:43:38.928555+08:00","last_version":"20","name":"CREATE SUBSCRIPTION","object":"SUBSCRIPTION","position":10003,"present_in":["10","11","12","13","14","15","16","17","18","19","20"],"purpose":"define a new subscription","purpose_zh":"","related":["alter-subscription","drop-subscription","create-publication","alter-publication"],"repair_provenance":{"original_source_revision":"a709ab85","receipt_sha256":"06be501bb51a5805458b93223edc6a15f92763d5ea4a8f5b95aa03b05208467f","repair":"wiki-repair-20261023"},"slug":"create-subscription","source_rev":"wiki-repair-20261023:4f5bafbde7419f7aa2c920b4344cc6292443799218090c5ab0cda5e55456064c","synopsis":"CREATE SUBSCRIPTION subscription_name\n{ SERVER server_name | CONNECTION 'conninfo' }\nPUBLICATION publication_name [, ...]\n[ WITH ( subscription_parameter [= value] [, ... ] ) ]","verb":"CREATE"}},"Definition":{"Collection":"sql","Key":"create-subscription","SourceDatabase":"center","Version":"19","SourceTable":"sqlcmd","SourceKey":"create-subscription","SourceRevision":"wiki-repair-20261023:4f5bafbde7419f7aa2c920b4344cc6292443799218090c5ab0cda5e55456064c","Facts":{"anchor":"SQL-CREATESUBSCRIPTION","description_zh":"定义一个新的订阅","description_zh_html":"\u003cpre\u003eCREATE SUBSCRIPTION subscription_name\n{ SERVER server_name | CONNECTION \u0026#x27;conninfo\u0026#x27; }\nPUBLICATION publication_name [, ...]\n[ WITH ( subscription_parameter [= value] [, ... ] ) ]\u003c/pre\u003e\u003csection\u003e\u003ch2\u003e描述\u003c/h2\u003e\u003cp\u003e\n\u003ccode class=\"command\"\u003eCREATE SUBSCRIPTION\u003c/code\u003e 添加一个新的逻辑复制订阅。创建订阅的用户将成为该订阅的所有者。订阅名称必须与当前数据库中任何现有订阅的名称不同。\n  \u003c/p\u003e\u003cp\u003e\n   订阅表示与发布者的复制连接。因此，除了在本地系统目录中添加定义之外，该命令通常还会在发布者上创建一个复制槽。\n  \u003c/p\u003e\u003cp\u003e\n   除非订阅初始即被禁用，否则在执行该命令所在事务提交时，会启动一个逻辑复制工作进程为新订阅复制数据。\n  \u003c/p\u003e\u003cp\u003e\n   要能够创建订阅，必须具有 \u003ccode class=\"literal\"\u003epg_create_subscription\u003c/code\u003e 角色的权限，以及当前数据库上的 \u003ccode class=\"literal\"\u003eCREATE\u003c/code\u003e 权限。\n  \u003c/p\u003e\u003cp\u003e\n   关于订阅以及整个逻辑复制的更多信息，请参见\u003ca class=\"xref\" href=\"/zh/docs/19/logical-replication-subscription.html\" title=\"29.2. 订阅\"\u003e第 29.2 节\u003c/a\u003e和\u003ca class=\"xref\" href=\"/zh/docs/19/logical-replication.html\" title=\"第 29 章 逻辑复制\"\u003e第 29 章\u003c/a\u003e。\n  \u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e参数\u003c/h2\u003e\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-NAME\"\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003esubscription_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n      新订阅的名称。\n     \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-SERVER\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSERVER \u003cem class=\"replaceable\"\u003e\u003ccode\u003eserver_name\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n      供连接使用的外部服务器。该服务器的外部数据包装器必须已注册\n      \u003cem class=\"replaceable\"\u003e\u003ccode\u003econnection_function\u003c/code\u003e\u003c/em\u003e，并且该服务器上必须存在订阅所有者的用户映射。此外，订阅所有者必须在\n      \u003cem class=\"replaceable\"\u003e\u003ccode\u003eserver_name\u003c/code\u003e\u003c/em\u003e 上具有 \u003ccode class=\"literal\"\u003eUSAGE\u003c/code\u003e 权限。\n     \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-CONNECTION\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eCONNECTION '\u003cem class=\"replaceable\"\u003e\u003ccode\u003econninfo\u003c/code\u003e\u003c/em\u003e'\u003c/code\u003e\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n      定义如何连接到发布者数据库的 \u003cspan class=\"application\"\u003elibpq\u003c/span\u003e 连接字符串。详情请参见\u003ca class=\"xref\" href=\"/zh/docs/19/libpq-connect.html#LIBPQ-CONNSTRING\" title=\"32.1.1. 连接字符串\"\u003e第 32.1.1 节\u003c/a\u003e。\n     \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-PUBLICATION\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003ePUBLICATION \u003cem class=\"replaceable\"\u003e\u003ccode\u003epublication_name\u003c/code\u003e\u003c/em\u003e [, ...]\u003c/code\u003e\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n      要订阅的发布者上的发布名称。\n     \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eWITH ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003esubscription_parameter\u003c/code\u003e\u003c/em\u003e [= \u003cem class=\"replaceable\"\u003e\u003ccode\u003evalue\u003c/code\u003e\u003c/em\u003e] [, ... ] )\u003c/code\u003e\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n      该子句为订阅指定可选参数。\n     \u003c/p\u003e\u003cp\u003e\n      下列参数控制订阅创建期间的行为：\n\n      \u003c/p\u003e\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-CONNECT\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003econnect\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          指定 \u003ccode class=\"command\"\u003eCREATE SUBSCRIPTION\u003c/code\u003e 命令是否连接到发布者。默认值为 \u003ccode class=\"literal\"\u003etrue\u003c/code\u003e。将其设为\n          \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e 会强制把 \u003ccode class=\"literal\"\u003ecreate_slot\u003c/code\u003e、\u003ccode class=\"literal\"\u003eenabled\u003c/code\u003e 和 \u003ccode class=\"literal\"\u003ecopy_data\u003c/code\u003e 的值设为 \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e。（不能将 \u003ccode class=\"literal\"\u003econnect\u003c/code\u003e\n          设为 \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e 的同时，再把 \u003ccode class=\"literal\"\u003ecreate_slot\u003c/code\u003e、\u003ccode class=\"literal\"\u003eenabled\u003c/code\u003e 或 \u003ccode class=\"literal\"\u003ecopy_data\u003c/code\u003e 设为\n          \u003ccode class=\"literal\"\u003etrue\u003c/code\u003e。）\n         \u003c/p\u003e\u003cp\u003e\n          由于该选项为 \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e 时不会建立连接，因此不会订阅任何表和序列。要启动复制，你必须手工创建复制槽、在需要时启用故障切换、启用订阅，并刷新订阅。示例见\u003ca class=\"xref\" href=\"/zh/docs/19/logical-replication-subscription.html#LOGICAL-REPLICATION-SUBSCRIPTION-EXAMPLES-DEFERRED-SLOT\" title=\"29.2.3. 示例：延迟创建逻辑复制槽\"\u003e第 29.2.3 节\u003c/a\u003e。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-CREATE-SLOT\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003ecreate_slot\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          指定该命令是否应在发布者上创建复制槽。默认值为\n          \u003ccode class=\"literal\"\u003etrue\u003c/code\u003e。\n         \u003c/p\u003e\u003cp\u003e\n          如果设为 \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e，则必须以其他方式自行创建发布者上的复制槽。示例见\u003ca class=\"xref\" href=\"/zh/docs/19/logical-replication-subscription.html#LOGICAL-REPLICATION-SUBSCRIPTION-EXAMPLES-DEFERRED-SLOT\" title=\"29.2.3. 示例：延迟创建逻辑复制槽\"\u003e第 29.2.3 节\u003c/a\u003e。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-ENABLED\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eenabled\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          指定订阅是应当主动进行复制，还是仅完成设置但暂不启动。默认值为 \u003ccode class=\"literal\"\u003etrue\u003c/code\u003e。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-SLOT-NAME\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eslot_name\u003c/code\u003e (\u003ccode class=\"type\"\u003estring\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          要使用的发布者上的复制槽名称。默认使用订阅名称作为槽名。该名称不能为\n          \u003ccode class=\"literal\"\u003epg_conflict_detection\u003c/code\u003e，因为它保留给冲突检测使用。\n         \u003c/p\u003e\u003cp\u003e\n          将 \u003ccode class=\"literal\"\u003eslot_name\u003c/code\u003e 设为 \u003ccode class=\"literal\"\u003eNONE\u003c/code\u003e 表示该订阅不关联任何复制槽。这类订阅还必须同时将 \u003ccode class=\"literal\"\u003eenabled\u003c/code\u003e\n          和 \u003ccode class=\"literal\"\u003ecreate_slot\u003c/code\u003e 设为 \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e。当你打算稍后手工创建复制槽时，可使用此设置。示例见\u003ca class=\"xref\" href=\"/zh/docs/19/logical-replication-subscription.html#LOGICAL-REPLICATION-SUBSCRIPTION-EXAMPLES-DEFERRED-SLOT\" title=\"29.2.3. 示例：延迟创建逻辑复制槽\"\u003e第 29.2.3 节\u003c/a\u003e。\n         \u003c/p\u003e\u003cp\u003e\n          将 \u003ccode class=\"literal\"\u003eslot_name\u003c/code\u003e 设为有效名称且\n          \u003ccode class=\"literal\"\u003ecreate_slot\u003c/code\u003e 设为 \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e 时，所指定复制槽的 \u003ccode class=\"literal\"\u003efailover\u003c/code\u003e 属性值可能与订阅中对应的\n          \u003ccode class=\"literal\"\u003efailover\u003c/code\u003e 参数不同。应始终确保该槽的\n          \u003ccode class=\"literal\"\u003efailover\u003c/code\u003e 属性与订阅中的对应参数一致，反之亦然。否则，发布者上的该槽的行为可能与这些订阅选项所表明的不同：例如，即使订阅的 \u003ccode class=\"literal\"\u003efailover\u003c/code\u003e 选项已禁用，发布者上的该槽仍可能被同步到备库；或者即使订阅的 \u003ccode class=\"literal\"\u003efailover\u003c/code\u003e 选项已启用，该槽也可能不会被同步到备库。\n         \u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003cp\u003e\n\u003c/p\u003e\u003cp\u003e\n      下列参数控制订阅创建后的复制行为：\n\n      \u003c/p\u003e\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-BINARY\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003ebinary\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          指定订阅是否请求发布者以二进制格式（而不是文本格式）发送数据。默认值为 \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e。任何初始表同步复制（见 \u003ccode class=\"literal\"\u003ecopy_data\u003c/code\u003e）也使用相同格式。二进制格式可能比文本格式更快，但在不同机器架构和\n          \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 版本之间的可移植性较差。二进制格式对数据类型非常敏感；例如，虽然在文本格式下可以正常工作，但它不允许从 \u003ccode class=\"type\"\u003esmallint\u003c/code\u003e 列复制到 \u003ccode class=\"type\"\u003einteger\u003c/code\u003e 列。即使启用了此选项，也只有具有二进制发送和接收函数的数据类型才会以二进制方式传输。注意，初始同步要求所有数据类型都具有二进制发送和接收函数，否则同步将失败（关于发送/接收函数的更多信息，请参见 \u003ca class=\"xref\" href=\"/zh/docs/19/sql-createtype.html\" title=\"CREATE TYPE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE TYPE\u003c/span\u003e\u003c/a\u003e）。此参数对序列没有影响。\n         \u003c/p\u003e\u003cp\u003e\n          在进行跨版本复制时，可能会出现这样的情况：发布者对某种数据类型有二进制发送函数，但订阅者缺少该类型的二进制接收函数。在这种情况下，数据传输会失败，因此不能使用 \u003ccode class=\"literal\"\u003ebinary\u003c/code\u003e 选项。\n         \u003c/p\u003e\u003cp\u003e\n          如果发布者使用的是 16 之前版本的\n          \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e，那么即使\n          \u003ccode class=\"literal\"\u003ebinary = true\u003c/code\u003e，任何初始表同步也都会使用文本格式。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-COPY-DATA\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003ecopy_data\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          指定复制开始时是否复制所订阅发布中的预先存在的数据。默认值为 \u003ccode class=\"literal\"\u003etrue\u003c/code\u003e。\n         \u003c/p\u003e\u003cp\u003e\n          如果这些发布包含 \u003ccode class=\"literal\"\u003eWHERE\u003c/code\u003e 子句，将会影响被复制的数据。详情请参见\u003ca class=\"xref\" href=\"/zh/docs/19/sql-createsubscription.html#SQL-CREATESUBSCRIPTION-NOTES\" title=\"注解\"\u003e注意\u003c/a\u003e。\n         \u003c/p\u003e\u003cp\u003e\n          关于 \u003ccode class=\"literal\"\u003ecopy_data = true\u003c/code\u003e 如何与\n          \u003ccode class=\"literal\"\u003eorigin\u003c/code\u003e 参数相互作用的细节，请参见\u003ca class=\"xref\" href=\"/zh/docs/19/sql-createsubscription.html#SQL-CREATESUBSCRIPTION-NOTES\" title=\"注解\"\u003e注意\u003c/a\u003e。\n         \u003c/p\u003e\u003cp\u003e\n          设置 \u003ccode class=\"literal\"\u003ecopy_data = true\u003c/code\u003e 时，可能出现关于发布者与订阅者之间序列定义差异的警告。处理这些警告的建议见\u003ca class=\"xref\" href=\"/zh/docs/19/logical-replication-sequences.html#SEQUENCE-DEFINITION-MISMATCHES\" title=\"29.7.1. 序列定义不匹配\"\u003e第 29.7.1 节\u003c/a\u003e。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-STREAMING\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003estreaming\u003c/code\u003e (\u003ccode class=\"type\"\u003eenum\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          指定是否为该订阅启用进行中事务的流式传输。默认值为\n          \u003ccode class=\"literal\"\u003eparallel\u003c/code\u003e，表示如果有可用的并行应用工作进程，接收到的更改会直接通过其中之一应用。如果没有空闲的并行应用工作进程可以处理流式事务，那么这些更改会写入临时文件，并在事务提交后再应用。注意，如果并行应用工作进程中发生错误，远端事务的完成 LSN\n          可能不会记录到服务器日志中。此参数对序列没有影响。\n         \u003c/p\u003e\u003cdiv class=\"caution\"\u003e\u003ch3 class=\"title\"\u003e小心\u003c/h3\u003e\u003cp\u003e\n           当发布者与订阅者的模式不同时，存在发生死锁的风险，尽管这种情况很少见。应用工作进程会自动重试这些事务。\n          \u003c/p\u003e\u003c/div\u003e\u003cp\u003e\n          如果设为 \u003ccode class=\"literal\"\u003eon\u003c/code\u003e，接收到的更改会写入临时文件，然后仅在事务在发布者上提交且被订阅者接收之后才应用。\n         \u003c/p\u003e\u003cp\u003e\n          如果设为 \u003ccode class=\"literal\"\u003eoff\u003c/code\u003e，所有事务都会先在发布者上完全解码，然后才整体发送给订阅者。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-SYNCHRONOUS-COMMIT\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003esynchronous_commit\u003c/code\u003e (\u003ccode class=\"type\"\u003eenum\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          该参数的值会覆盖此订阅应用工作进程中的 \u003ca class=\"xref\" href=\"/zh/docs/19/runtime-config-wal.html#GUC-SYNCHRONOUS-COMMIT\"\u003esynchronous_commit\u003c/a\u003e 设置。默认值为\n          \u003ccode class=\"literal\"\u003eoff\u003c/code\u003e。此参数对序列没有影响。\n         \u003c/p\u003e\u003cp\u003e\n          对逻辑复制来说，使用 \u003ccode class=\"literal\"\u003eoff\u003c/code\u003e 是安全的：如果订阅者因缺少同步而丢失了事务，数据会再次从发布者发送过来。\n         \u003c/p\u003e\u003cp\u003e\n          在进行同步逻辑复制时，可能更适合使用不同的设置。逻辑复制工作进程会向发布者报告写入和刷盘位置，而在使用同步复制时，发布者会等待真正的刷盘完成。这意味着，当订阅被用于同步复制时，将订阅者的\n          \u003ccode class=\"literal\"\u003esynchronous_commit\u003c/code\u003e 设为 \u003ccode class=\"literal\"\u003eoff\u003c/code\u003e\n          可能会增加发布者上 \u003ccode class=\"command\"\u003eCOMMIT\u003c/code\u003e 的延迟。在这种场景下，将 \u003ccode class=\"literal\"\u003esynchronous_commit\u003c/code\u003e 设为 \u003ccode class=\"literal\"\u003elocal\u003c/code\u003e\n          或更高可能更有利。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-TWO-PHASE\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003etwo_phase\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          指定是否为该订阅启用两阶段提交。默认值为\n          \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e。此参数对序列没有影响。\n         \u003c/p\u003e\u003cp\u003e\n          启用两阶段提交时，预备事务会在\n          \u003ccode class=\"command\"\u003ePREPARE TRANSACTION\u003c/code\u003e 时发送给订阅者，并且在订阅者上也会作为两阶段事务处理。否则，预备事务只有在提交时才会发送给订阅者，随后由订阅者立即处理。\n         \u003c/p\u003e\u003cp\u003e\n          两阶段提交的实现要求复制已经成功完成初始表同步阶段。因此，即使订阅启用了 \u003ccode class=\"literal\"\u003etwo_phase\u003c/code\u003e，内部的两阶段状态也会暂时保持为\u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003epending\u003c/span\u003e”\u003c/span\u003e，直到初始化阶段完成。要了解实际的两阶段状态，请参见\n          \u003ca class=\"link\" href=\"/zh/docs/19/catalog-pg-subscription.html\" title=\"52.54. pg_subscription\"\u003e\u003ccode class=\"structname\"\u003epg_subscription\u003c/code\u003e\u003c/a\u003e\n          的 \u003ccode class=\"structfield\"\u003esubtwophasestate\u003c/code\u003e 列。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-DISABLE-ON-ERROR\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003edisable_on_error\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          指定如果订阅工作进程在从发布者复制数据期间检测到任何错误，是否自动禁用该订阅。默认值为 \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-PASSWORD-REQUIRED\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003epassword_required\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          如果设为 \u003ccode class=\"literal\"\u003etrue\u003c/code\u003e，则由于该订阅而建立的到发布者的连接必须使用密码认证，并且密码必须作为连接字符串的一部分指定。如果该订阅由超级用户拥有，则此设置会被忽略。默认值为 \u003ccode class=\"literal\"\u003etrue\u003c/code\u003e。只有超级用户才能将该值设为 \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-RUN-AS-OWNER\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003erun_as_owner\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          如果为 \u003ccode class=\"literal\"\u003etrue\u003c/code\u003e，所有复制操作都以订阅所有者的身份执行。如果为 \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e，复制工作进程会在每张表或序列上以该关系所有者的身份执行操作。后一种配置通常安全得多；详情请参见\u003ca class=\"xref\" href=\"/zh/docs/19/logical-replication-security.html\" title=\"29.12. 安全性\"\u003e第 29.12 节\u003c/a\u003e。默认值为 \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-ORIGIN\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eorigin\u003c/code\u003e (\u003ccode class=\"type\"\u003estring\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          指定订阅是请求发布者仅发送没有复制源的更改，还是无论复制源如何都发送更改。将 \u003ccode class=\"literal\"\u003eorigin\u003c/code\u003e 设为 \u003ccode class=\"literal\"\u003enone\u003c/code\u003e 表示订阅会请求发布者仅发送没有复制源的更改。将 \u003ccode class=\"literal\"\u003eorigin\u003c/code\u003e 设为\n          \u003ccode class=\"literal\"\u003eany\u003c/code\u003e 表示发布者无论复制源如何都发送更改。默认值为\n          \u003ccode class=\"literal\"\u003eany\u003c/code\u003e。此参数对序列没有影响。\n         \u003c/p\u003e\u003cp\u003e\n          关于 \u003ccode class=\"literal\"\u003ecopy_data = true\u003c/code\u003e 如何与\n          \u003ccode class=\"literal\"\u003eorigin\u003c/code\u003e 参数相互作用的细节，请参见\u003ca class=\"xref\" href=\"/zh/docs/19/sql-createsubscription.html#SQL-CREATESUBSCRIPTION-NOTES\" title=\"注解\"\u003e注意\u003c/a\u003e。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-FAILOVER\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003efailover\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          指定与该订阅关联的复制槽是否启用同步到备库，以便在故障切换后能够从新的主库继续逻辑复制。默认值为 \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-RETAIN-DEAD-TUPLES\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eretain_dead_tuples\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          指定是否保留订阅者上冲突检测所需的信息（例如死元组、提交时间戳和来源）。默认值为 \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e。如果设为\n          \u003ccode class=\"literal\"\u003etrue\u003c/code\u003e，则会启用 \u003ca class=\"xref\" href=\"/zh/docs/19/logical-replication-conflicts.html#CONFLICT-UPDATE-DELETED\"\u003eupdate_deleted\u003c/a\u003e 的检测，并在订阅者上创建一个名为\u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003e\u003ccode class=\"literal\"\u003epg_conflict_detection\u003c/code\u003e\u003c/span\u003e”\u003c/span\u003e的物理复制槽，以防止用于检测冲突的信息被移除。此参数对序列没有影响。\n         \u003c/p\u003e\u003cp\u003e\n          请注意，只有在创建该槽之后，才会保留用于冲突检测的信息。可以通过查询\n          \u003ca class=\"link\" href=\"/zh/docs/19/view-pg-replication-slots.html\" title=\"53.22. pg_replication_slots\"\u003epg_replication_slots\u003c/a\u003e 来验证该槽是否存在。即使同一节点上的多个订阅启用此选项，也只会创建一个复制槽。此外，必须将 \u003ccode class=\"varname\"\u003ewal_level\u003c/code\u003e 设置为 \u003ccode class=\"literal\"\u003ereplica\u003c/code\u003e\n          或更高，才能允许使用该复制槽。\n         \u003c/p\u003e\u003cdiv class=\"caution\"\u003e\u003ch3 class=\"title\"\u003e小心\u003c/h3\u003e\u003cp\u003e\n           请注意，如果订阅被禁用，用于冲突检测的信息将无法被清除；因此，这些信息会一直累积，直到订阅被启用。为了防止过度累积，建议在订阅将长时间处于非活动状态时禁用 \u003ccode class=\"literal\"\u003eretain_dead_tuples\u003c/code\u003e。如果订阅已启用，但其应用工作进程没有运行（例如工作进程反复无法应用某项更改），情况也相同。可以通过 \u003ca class=\"link\" href=\"/zh/docs/19/view-pg-replication-slots.html\" title=\"53.22. pg_replication_slots\"\u003epg_replication_slots\u003c/a\u003e\n           中\u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003e\u003ccode class=\"literal\"\u003epg_conflict_detection\u003c/code\u003e\u003c/span\u003e”\u003c/span\u003e复制槽的\n           \u003ccode class=\"structfield\"\u003exmin\u003c/code\u003e 查看所保留的最旧信息。\n          \u003c/p\u003e\u003cp\u003e\n           此外，在逻辑复制中为冲突检测启用 \u003ccode class=\"literal\"\u003eretain_dead_tuples\u003c/code\u003e 时，设计复制拓扑以平衡数据保留需求与整体系统性能非常重要。该选项在适当使用时只会带来极小的性能开销。以下场景展示了启用该选项时的有效用法模式。\n          \u003c/p\u003e\u003cp\u003e\n           a. 双向写入的大表：对于在发布者和订阅者节点上都存在并发写入的大表，发布者可以在创建发布时定义行过滤器来分割数据。这样可以让多个订阅并行复制该表的互斥子集，从而优化吞吐量。\n          \u003c/p\u003e\u003cp\u003e\n           b. 可写入的订阅者：如果预期订阅者节点需要执行写操作，则可以通过多个发布和订阅来组织复制。通过将表分配到这些发布中，工作负载会分散到多个应用工作进程之间，从而提高并发性并减少争用。\n          \u003c/p\u003e\u003cp\u003e\n           c. 只读订阅者：在涉及一个或多个发布者节点执行并发写操作的配置中，只读订阅者节点如果执行的是索引扫描，则可能不会看到性能影响。然而，如果订阅者由于复制延迟或扫描性能（例如顺序扫描）而受到影响，则需要采用前述两种策略之一来在订阅者上分散负载。\n          \u003c/p\u003e\u003c/div\u003e\u003cp\u003e\n          如果发布者是物理备库，则不能启用此选项。\n         \u003c/p\u003e\u003cp\u003e\n          启用此选项只能保证保留用于冲突检测的信息，且仅针对在发布者本地发生的更改。对于来自不同来源的更改，无法保证可靠的冲突检测。\n         \u003c/p\u003e\u003cp\u003e\n          检测 \u003ca class=\"xref\" href=\"/zh/docs/19/logical-replication-conflicts.html#CONFLICT-UPDATE-DELETED\"\u003eupdate_deleted\u003c/a\u003e 还要求在订阅者上启用\n          \u003ca class=\"link\" href=\"/zh/docs/19/runtime-config-replication.html#GUC-TRACK-COMMIT-TIMESTAMP\"\u003e\u003ccode class=\"varname\"\u003etrack_commit_timestamp\u003c/code\u003e\u003c/a\u003e。如果它未启用，而启用了 \u003ccode class=\"literal\"\u003eretain_dead_tuples\u003c/code\u003e，则会发出警告。如果之后禁用了 \u003ccode class=\"varname\"\u003etrack_commit_timestamp\u003c/code\u003e，用于冲突检测的信息仍会继续保留，但冲突会改为报告为 \u003ca class=\"xref\" href=\"/zh/docs/19/logical-replication-conflicts.html#CONFLICT-UPDATE-MISSING\"\u003eupdate_missing\u003c/a\u003e，因此保留这些信息便失去了作用。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-MAX-RETENTION-DURATION\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003emax_retention_duration\u003c/code\u003e (\u003ccode class=\"type\"\u003einteger\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          当启用 \u003ccode class=\"literal\"\u003eretain_dead_tuples\u003c/code\u003e 时，此订阅的应用工作进程允许保留用于冲突检测的信息的最长时长，单位为毫秒。默认值为\n          \u003ccode class=\"literal\"\u003e0\u003c/code\u003e，表示在不再需要进行检测之前一直保留这些信息。\n         \u003c/p\u003e\u003cp\u003e\n          如果所有与启用了 \u003ccode class=\"literal\"\u003eretain_dead_tuples\u003c/code\u003e 的订阅相关联的应用工作进程都确认保留时长已经超过相应订阅中设置的\n          \u003ccode class=\"literal\"\u003emax_retention_duration\u003c/code\u003e，则用于冲突检测的信息将不再被保留。只要至少有一个应用工作进程确认保留时长仍在指定限制内，或者创建了一个新的 \u003ccode class=\"literal\"\u003eretain_dead_tuples = true\u003c/code\u003e 订阅，保留就会自动恢复。或者，也可以通过重新启用\n          \u003ccode class=\"literal\"\u003eretain_dead_tuples\u003c/code\u003e 手工恢复保留。\n         \u003c/p\u003e\u003cp\u003e\n          注意，如果其他订阅为此参数设置了大于 0 的值且尚未超过该值，或者将此选项设为 \u003ccode class=\"literal\"\u003e0\u003c/code\u003e，则整体保留不会停止。\n         \u003c/p\u003e\u003cp\u003e\n          只有当启用了 \u003ccode class=\"literal\"\u003eretain_dead_tuples\u003c/code\u003e 且与该订阅关联的应用工作进程处于活动状态时，此选项才有效。\n         \u003c/p\u003e\u003cdiv class=\"caution\"\u003e\u003ch3 class=\"title\"\u003e小心\u003c/h3\u003e\u003cp\u003e\n           注意，订阅被禁用或其应用工作进程未运行时，不会评估保留时长。因此，无论此设置如何，用于冲突检测的信息都会继续累积，直到订阅被启用或其应用工作进程恢复运行。为防止过度累积，如果订阅将长时间处于非活动状态，应考虑禁用\n           \u003ccode class=\"literal\"\u003eretain_dead_tuples\u003c/code\u003e。\n          \u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"warning\"\u003e\u003ch3 class=\"title\"\u003e警告\u003c/h3\u003e\u003cp\u003e\n           注意，将此选项设为非零值可能导致用于冲突检测的信息被过早移除，从而可能造成错误的冲突检测。\n          \u003c/p\u003e\u003c/div\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-WAL-RECEIVER-TIMEOUT\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003ewal_receiver_timeout\u003c/code\u003e (\u003ccode class=\"type\"\u003etext\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          该参数的值会覆盖此订阅应用工作进程中的 \u003ca class=\"xref\" href=\"/zh/docs/19/runtime-config-replication.html#GUC-WAL-RECEIVER-TIMEOUT\"\u003ewal_receiver_timeout\u003c/a\u003e 设置。默认值为\n          \u003ccode class=\"literal\"\u003e-1\u003c/code\u003e，表示不覆盖全局设置，也就是说将改用服务器配置、命令行、角色或数据库设置中的值。\n         \u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003cp\u003e\n   当指定 \u003ccode class=\"type\"\u003eboolean\u003c/code\u003e 类型的参数时，可以省略\n   \u003ccode class=\"literal\"\u003e=\u003c/code\u003e \u003cem class=\"replaceable\"\u003e\u003ccode\u003evalue\u003c/code\u003e\u003c/em\u003e\n   部分，这等价于指定 \u003ccode class=\"literal\"\u003eTRUE\u003c/code\u003e。\n  \u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e注解\u003c/h2\u003e\u003cp\u003e\n   关于如何在订阅与发布实例之间配置访问控制，详见\u003ca class=\"xref\" href=\"/zh/docs/19/logical-replication-security.html\" title=\"29.12. 安全性\"\u003e第 29.12 节\u003c/a\u003e。\n  \u003c/p\u003e\u003cp\u003e\n   当创建复制槽时（这是默认行为），\u003ccode class=\"command\"\u003eCREATE\n   SUBSCRIPTION\u003c/code\u003e 不能在事务块内部执行。\n  \u003c/p\u003e\u003cp\u003e\n   创建一个连接到同一数据库集簇的订阅（例如在同一集簇中的不同数据库之间复制，或者在同一数据库内复制）时，只有在复制槽不作为同一命令的一部分创建的情况下才会成功。否则，\u003ccode class=\"command\"\u003eCREATE SUBSCRIPTION\u003c/code\u003e\n   调用将挂起。要实现这种用法，应分别创建复制槽（使用函数\n   \u003ccode class=\"function\"\u003epg_create_logical_replication_slot\u003c/code\u003e 并指定插件名\n   \u003ccode class=\"literal\"\u003epgoutput\u003c/code\u003e），然后使用参数\n   \u003ccode class=\"literal\"\u003ecreate_slot = false\u003c/code\u003e 创建订阅。示例见\u003ca class=\"xref\" href=\"/zh/docs/19/logical-replication-subscription.html#LOGICAL-REPLICATION-SUBSCRIPTION-EXAMPLES-DEFERRED-SLOT\" title=\"29.2.3. 示例：延迟创建逻辑复制槽\"\u003e第 29.2.3 节\u003c/a\u003e。这是一个实现限制，未来版本中可能会解除。\n  \u003c/p\u003e\u003cp\u003e\n   如果发布中的任何表带有 \u003ccode class=\"literal\"\u003eWHERE\u003c/code\u003e 子句，则对\n   \u003cem class=\"replaceable\"\u003e\u003ccode\u003eexpression\u003c/code\u003e\u003c/em\u003e 求值为\n   \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e 或 \u003ccode class=\"literal\"\u003eNULL\u003c/code\u003e 的行不会被发布。如果订阅包含多个发布，且同一张表在这些发布中使用了不同的\n   \u003ccode class=\"literal\"\u003eWHERE\u003c/code\u003e 子句，那么只要任意一个表达式（针对相应的发布操作）满足，该行就会被发布。对于不同的\n   \u003ccode class=\"literal\"\u003eWHERE\u003c/code\u003e 子句，如果其中某个发布没有\n   \u003ccode class=\"literal\"\u003eWHERE\u003c/code\u003e 子句（针对相应的发布操作），或者该发布被声明为\n   \u003ca class=\"link\" href=\"/zh/docs/19/sql-createpublication.html#SQL-CREATEPUBLICATION-PARAMS-FOR-ALL-TABLES\"\u003e\u003ccode class=\"literal\"\u003eFOR ALL TABLES\u003c/code\u003e\u003c/a\u003e\n   或\n   \u003ca class=\"link\" href=\"/zh/docs/19/sql-createpublication.html#SQL-CREATEPUBLICATION-PARAMS-FOR-TABLES-IN-SCHEMA\"\u003e\u003ccode class=\"literal\"\u003eFOR TABLES IN SCHEMA\u003c/code\u003e\u003c/a\u003e，则无论其他表达式如何定义，该行都会被发布。如果订阅者使用的是 15 之前版本的 \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e，那么在初始数据同步阶段会忽略所有行过滤。在这种情况下，用户可能需要考虑删除那些初始复制过来、但与后续过滤不兼容的数据。由于初始数据同步在复制现有表数据时不会考虑发布的\n   \u003ca class=\"link\" href=\"/zh/docs/19/sql-createpublication.html#SQL-CREATEPUBLICATION-PARAMS-WITH-PUBLISH\"\u003e\u003ccode class=\"literal\"\u003epublish\u003c/code\u003e\u003c/a\u003e\n   参数，因此有些使用 DML 时本不会复制的行，也可能在此阶段被复制。示例见\u003ca class=\"xref\" href=\"/zh/docs/19/logical-replication-subscription.html#LOGICAL-REPLICATION-SUBSCRIPTION-EXAMPLES\" title=\"29.2.2. 示例：建立逻辑复制\"\u003e第 29.2.2 节\u003c/a\u003e。\n  \u003c/p\u003e\u003cp\u003e\n   如果订阅包含多个发布，而其中同一张表使用了不同的列列表进行发布，则不受支持。\n  \u003c/p\u003e\u003cp\u003e\n   允许指定不存在的发布，以便用户稍后再创建这些发布。这意味着\n   \u003ca class=\"link\" href=\"/zh/docs/19/catalog-pg-subscription.html\" title=\"52.54. pg_subscription\"\u003e\u003ccode class=\"structname\"\u003epg_subscription\u003c/code\u003e\u003c/a\u003e\n   中可能包含不存在的发布。\n  \u003c/p\u003e\u003cp\u003e\n   当使用 \u003ccode class=\"literal\"\u003ecopy_data = true\u003c/code\u003e 和\n   \u003ccode class=\"literal\"\u003eorigin = NONE\u003c/code\u003e 这一订阅参数组合时，初始同步的表数据会直接从发布者复制过来，因此无法得知这些数据的真实来源。如果发布者本身也有订阅，那么复制过来的表数据可能来自更上游。系统会检测到这种情况，并向用户发出一条 \u003ccode class=\"literal\"\u003eWARNING\u003c/code\u003e，但这条警告只表明可能存在问题；用户仍需自行进行必要检查，以确认复制过来的数据来源是否确实符合预期。\n  \u003c/p\u003e\u003cp\u003e\n   若要找出哪些表可能包含非本地来源的数据（由于在发布者上创建了其他订阅），可以尝试执行以下 SQL 查询：\n\u003c/p\u003e\u003cpre class=\"programlisting\"\u003e\n# substitute \u0026lt;pub-names\u0026gt; below with your publication name(s) to be queried\nSELECT DISTINCT PT.schemaname, PT.tablename\nFROM pg_publication_tables PT\n     JOIN pg_class C ON (C.relname = PT.tablename)\n     JOIN pg_namespace N ON (N.nspname = PT.schemaname),\n     pg_subscription_rel PS\nWHERE C.relnamespace = N.oid AND\n      (PS.srrelid = C.oid OR\n      C.oid IN (SELECT relid FROM pg_partition_ancestors(PS.srrelid) UNION\n                SELECT relid FROM pg_partition_tree(PS.srrelid))) AND\n      PT.pubname IN (\u0026lt;pub-names\u0026gt;);\n\u003c/pre\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e示例\u003c/h2\u003e\u003cp\u003e\n   创建一个指向远端服务器的订阅，复制发布\n   \u003ccode class=\"literal\"\u003emypublication\u003c/code\u003e 和 \u003ccode class=\"literal\"\u003einsert_only\u003c/code\u003e 中的表，并在提交时立即开始复制：\n\u003c/p\u003e\u003cpre class=\"programlisting\"\u003e\nCREATE SUBSCRIPTION mysub\n         CONNECTION 'host=192.168.1.50 port=5432 user=foo dbname=foodb'\n        PUBLICATION mypublication, insert_only;\n\u003c/pre\u003e\u003cp\u003e\n\u003c/p\u003e\u003cp\u003e\n   创建一个指向远端服务器的订阅，复制 \u003ccode class=\"literal\"\u003einsert_only\u003c/code\u003e 发布中的表，并且要等到稍后启用时才开始复制。\n\u003c/p\u003e\u003cpre class=\"programlisting\"\u003e\nCREATE SUBSCRIPTION mysub\n         CONNECTION 'host=192.168.1.50 port=5432 user=foo dbname=foodb'\n        PUBLICATION insert_only\n               WITH (enabled = false);\n\u003c/pre\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e兼容性\u003c/h2\u003e\u003cp\u003e\n\u003ccode class=\"command\"\u003eCREATE SUBSCRIPTION\u003c/code\u003e 是\n   \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 扩展。\n  \u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e另见\u003c/h2\u003e\u003cspan class=\"simplelist\"\u003e\u003ca class=\"xref\" href=\"/zh/docs/19/sql-altersubscription.html\" title=\"ALTER SUBSCRIPTION\"\u003e\u003cspan class=\"refentrytitle\"\u003eALTER SUBSCRIPTION\u003c/span\u003e\u003c/a\u003e, \u003ca class=\"xref\" href=\"/zh/docs/19/sql-dropsubscription.html\" title=\"DROP SUBSCRIPTION\"\u003e\u003cspan class=\"refentrytitle\"\u003eDROP SUBSCRIPTION\u003c/span\u003e\u003c/a\u003e, \u003ca class=\"xref\" href=\"/zh/docs/19/sql-createpublication.html\" title=\"CREATE PUBLICATION\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE PUBLICATION\u003c/span\u003e\u003c/a\u003e, \u003ca class=\"xref\" href=\"/zh/docs/19/sql-alterpublication.html\" title=\"ALTER PUBLICATION\"\u003e\u003cspan class=\"refentrytitle\"\u003eALTER PUBLICATION\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e\u003c/section\u003e","file":"sql-createsubscription.html","lang":"en","name":"CREATE SUBSCRIPTION","purpose":"define a new subscription","purpose_zh":"定义一个新的订阅","related":["alter-subscription","drop-subscription","create-publication","alter-publication"],"sections":[{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003eCREATE SUBSCRIPTION\u003c/code\u003e 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.\u003c/p\u003e\u003cp\u003eA 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.\u003c/p\u003e\u003cp\u003eA 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.\u003c/p\u003e\u003cp\u003eTo be able to create a subscription, you must have the privileges of the \u003ccode class=\"literal\"\u003epg_create_subscription\u003c/code\u003e role, as well as \u003ccode class=\"literal\"\u003eCREATE\u003c/code\u003e privileges on the current database.\u003c/p\u003e\u003cp\u003eAdditional information about subscriptions and logical replication as a whole is available at \u003ca href=\"/docs/19/logical-replication-subscription.html\" title=\"29.2. Subscription\"\u003eSection 29.2\u003c/a\u003e and \u003ca href=\"/docs/19/logical-replication.html\" title=\"Chapter 29. Logical Replication\"\u003eChapter 29\u003c/a\u003e.\u003c/p\u003e","key":"description","title":"Description"},{"html":"\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003esubscription_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe name of the new subscription.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSERVER \u003cem class=\"replaceable\"\u003e\u003ccode\u003eserver_name\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eA foreign server to use for the connection. The server's foreign data wrapper must have a \u003cem class=\"replaceable\"\u003e\u003ccode\u003econnection_function\u003c/code\u003e\u003c/em\u003e registered, and a user mapping for the subscription owner on the server must exist. Additionally, the subscription owner must have \u003ccode class=\"literal\"\u003eUSAGE\u003c/code\u003e privileges on \u003cem class=\"replaceable\"\u003e\u003ccode\u003eserver_name\u003c/code\u003e\u003c/em\u003e.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eCONNECTION '\u003cem class=\"replaceable\"\u003e\u003ccode\u003econninfo\u003c/code\u003e\u003c/em\u003e'\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe \u003cspan class=\"application\"\u003elibpq\u003c/span\u003e connection string defining how to connect to the publisher database. For details see \u003ca href=\"/docs/19/libpq-connect.html#LIBPQ-CONNSTRING\" title=\"32.1.1. Connection Strings\"\u003eSection 32.1.1\u003c/a\u003e.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003ePUBLICATION \u003cem class=\"replaceable\"\u003e\u003ccode\u003epublication_name\u003c/code\u003e\u003c/em\u003e [, ...]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eNames of the publications on the publisher to subscribe to.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eWITH ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003esubscription_parameter\u003c/code\u003e\u003c/em\u003e [= \u003cem class=\"replaceable\"\u003e\u003ccode\u003evalue\u003c/code\u003e\u003c/em\u003e] [, ... ] )\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis clause specifies optional parameters for a subscription.\u003c/p\u003e\u003cp\u003eThe following parameters control what happens during subscription creation:\u003c/p\u003e\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003econnect\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e)\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eSpecifies whether the \u003ccode class=\"command\"\u003eCREATE SUBSCRIPTION\u003c/code\u003e command should connect to the publisher at all. The default is \u003ccode class=\"literal\"\u003etrue\u003c/code\u003e. Setting this to \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e will force the values of \u003ccode class=\"literal\"\u003ecreate_slot\u003c/code\u003e, \u003ccode class=\"literal\"\u003eenabled\u003c/code\u003e and \u003ccode class=\"literal\"\u003ecopy_data\u003c/code\u003e to \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e. (You cannot combine setting \u003ccode class=\"literal\"\u003econnect\u003c/code\u003e to \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e with setting \u003ccode class=\"literal\"\u003ecreate_slot\u003c/code\u003e, \u003ccode class=\"literal\"\u003eenabled\u003c/code\u003e, or \u003ccode class=\"literal\"\u003ecopy_data\u003c/code\u003e to \u003ccode class=\"literal\"\u003etrue\u003c/code\u003e.)\u003c/p\u003e\u003cp\u003eSince no connection is made when this option is \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e, no tables and sequences are subscribed. To initiate replication, you must manually create the replication slot, enable the failover if required, enable the subscription, and refresh the subscription. See \u003ca href=\"/docs/19/logical-replication-subscription.html#LOGICAL-REPLICATION-SUBSCRIPTION-EXAMPLES-DEFERRED-SLOT\" title=\"29.2.3. Examples: Deferred Logical Replication Slot Creation\"\u003eSection 29.2.3\u003c/a\u003e for examples.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003ecreate_slot\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e)\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eSpecifies whether the command should create the replication slot on the publisher. The default is \u003ccode class=\"literal\"\u003etrue\u003c/code\u003e.\u003c/p\u003e\u003cp\u003eIf set to \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e, you are responsible for creating the publisher's slot in some other way. See \u003ca href=\"/docs/19/logical-replication-subscription.html#LOGICAL-REPLICATION-SUBSCRIPTION-EXAMPLES-DEFERRED-SLOT\" title=\"29.2.3. Examples: Deferred Logical Replication Slot Creation\"\u003eSection 29.2.3\u003c/a\u003e for examples.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eenabled\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e)\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eSpecifies whether the subscription should be actively replicating or whether it should just be set up but not started yet. The default is \u003ccode class=\"literal\"\u003etrue\u003c/code\u003e.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eslot_name\u003c/code\u003e (\u003ccode class=\"type\"\u003estring\u003c/code\u003e)\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eName of the publisher's replication slot to use. The default is to use the name of the subscription for the slot name. The name cannot be \u003ccode class=\"literal\"\u003epg_conflict_detection\u003c/code\u003e as it is reserved for the conflict detection.\u003c/p\u003e\u003cp\u003eSetting \u003ccode class=\"literal\"\u003eslot_name\u003c/code\u003e to \u003ccode class=\"literal\"\u003eNONE\u003c/code\u003e means there will be no replication slot associated with the subscription. Such subscriptions must also have both \u003ccode class=\"literal\"\u003eenabled\u003c/code\u003e and \u003ccode class=\"literal\"\u003ecreate_slot\u003c/code\u003e set to \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e. Use this when you will be creating the replication slot later manually. See \u003ca href=\"/docs/19/logical-replication-subscription.html#LOGICAL-REPLICATION-SUBSCRIPTION-EXAMPLES-DEFERRED-SLOT\" title=\"29.2.3. Examples: Deferred Logical Replication Slot Creation\"\u003eSection 29.2.3\u003c/a\u003e for examples.\u003c/p\u003e\u003cp\u003eWhen setting \u003ccode class=\"literal\"\u003eslot_name\u003c/code\u003e to a valid name and \u003ccode class=\"literal\"\u003ecreate_slot\u003c/code\u003e to \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e, the \u003ccode class=\"literal\"\u003efailover\u003c/code\u003e property value of the named slot may differ from the counterpart \u003ccode class=\"literal\"\u003efailover\u003c/code\u003e parameter specified in the subscription. Always ensure the slot property \u003ccode class=\"literal\"\u003efailover\u003c/code\u003e matches the counterpart parameter of the subscription and vice versa. Otherwise, the slot on the publisher may behave differently from what these subscription options say: for example, the slot on the publisher could either be synced to the standbys even when the subscription's \u003ccode class=\"literal\"\u003efailover\u003c/code\u003e option is disabled or could be disabled for sync even when the subscription's \u003ccode class=\"literal\"\u003efailover\u003c/code\u003e option is enabled.\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003cp\u003eThe following parameters control the subscription's replication behavior after it has been created:\u003c/p\u003e\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003ebinary\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e)\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eSpecifies whether the subscription will request the publisher to send the data in binary format (as opposed to text). The default is \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e. Any initial table synchronization copy (see \u003ccode class=\"literal\"\u003ecopy_data\u003c/code\u003e) also uses the same format. Binary format can be faster than the text format, but it is less portable across machine architectures and \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e versions. Binary format is very data type specific; for example, it will not allow copying from a \u003ccode class=\"type\"\u003esmallint\u003c/code\u003e column to an \u003ccode class=\"type\"\u003einteger\u003c/code\u003e 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 \u003ca href=\"/docs/19/sql-createtype.html\" title=\"CREATE TYPE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE TYPE\u003c/span\u003e\u003c/a\u003e for more about send/receive functions). This parameter has no effect for sequences.\u003c/p\u003e\u003cp\u003eWhen 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 \u003ccode class=\"literal\"\u003ebinary\u003c/code\u003e option cannot be used.\u003c/p\u003e\u003cp\u003eIf the publisher is a \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e version before 16, then any initial table synchronization will use text format even if \u003ccode class=\"literal\"\u003ebinary = true\u003c/code\u003e.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003ecopy_data\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e)\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eSpecifies whether to copy pre-existing data in the publications that are being subscribed to when the replication starts. The default is \u003ccode class=\"literal\"\u003etrue\u003c/code\u003e.\u003c/p\u003e\u003cp\u003eIf the publications contain \u003ccode class=\"literal\"\u003eWHERE\u003c/code\u003e clauses, it will affect what data is copied. Refer to the \u003ca href=\"/docs/19/sql-createsubscription.html#SQL-CREATESUBSCRIPTION-NOTES\" title=\"Notes\"\u003eNotes\u003c/a\u003e for details.\u003c/p\u003e\u003cp\u003eSee \u003ca href=\"/docs/19/sql-createsubscription.html#SQL-CREATESUBSCRIPTION-NOTES\" title=\"Notes\"\u003eNotes\u003c/a\u003e for details of how \u003ccode class=\"literal\"\u003ecopy_data = true\u003c/code\u003e can interact with the \u003ccode class=\"literal\"\u003eorigin\u003c/code\u003e parameter.\u003c/p\u003e\u003cp\u003eSee \u003ca href=\"/docs/19/logical-replication-sequences.html#SEQUENCE-DEFINITION-MISMATCHES\" title=\"29.7.1. Sequence Definition Mismatches\"\u003eSection 29.7.1\u003c/a\u003e for recommendations on how to handle any warnings about sequence definition differences between the publisher and the subscriber, which might occur when \u003ccode class=\"literal\"\u003ecopy_data = true\u003c/code\u003e.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003estreaming\u003c/code\u003e (\u003ccode class=\"type\"\u003eenum\u003c/code\u003e)\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eSpecifies whether to enable streaming of in-progress transactions for this subscription. The default value is \u003ccode class=\"literal\"\u003eparallel\u003c/code\u003e, meaning 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. This parameter has no effect for sequences.\u003c/p\u003e\u003cdiv class=\"caution\"\u003e\u003ch3\u003eCaution\u003c/h3\u003e\u003cp\u003eThere is a risk of deadlock when the schemas of the publisher and subscriber differ, although such cases are rare. The apply worker is equipped to retry these transactions automatically.\u003c/p\u003e\u003c/div\u003e\u003cp\u003eIf set to \u003ccode class=\"literal\"\u003eon\u003c/code\u003e, 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.\u003c/p\u003e\u003cp\u003eIf set to \u003ccode class=\"literal\"\u003eoff\u003c/code\u003e, all transactions are fully decoded on the publisher and only then sent to the subscriber as a whole.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003esynchronous_commit\u003c/code\u003e (\u003ccode class=\"type\"\u003eenum\u003c/code\u003e)\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe value of this parameter overrides the \u003ca href=\"/docs/19/runtime-config-wal.html#GUC-SYNCHRONOUS-COMMIT\"\u003esynchronous_commit\u003c/a\u003e setting within this subscription's apply worker processes. The default value is \u003ccode class=\"literal\"\u003eoff\u003c/code\u003e. This parameter has no effect for sequences.\u003c/p\u003e\u003cp\u003eIt is safe to use \u003ccode class=\"literal\"\u003eoff\u003c/code\u003e for logical replication: If the subscriber loses transactions because of missing synchronization, the data will be sent again from the publisher.\u003c/p\u003e\u003cp\u003eA 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 \u003ccode class=\"literal\"\u003esynchronous_commit\u003c/code\u003e for the subscriber to \u003ccode class=\"literal\"\u003eoff\u003c/code\u003e when the subscription is used for synchronous replication might increase the latency for \u003ccode class=\"command\"\u003eCOMMIT\u003c/code\u003e on the publisher. In this scenario, it can be advantageous to set \u003ccode class=\"literal\"\u003esynchronous_commit\u003c/code\u003e to \u003ccode class=\"literal\"\u003elocal\u003c/code\u003e or higher.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003etwo_phase\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e)\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eSpecifies whether two-phase commit is enabled for this subscription. The default is \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e. This parameter has no effect for sequences.\u003c/p\u003e\u003cp\u003eWhen two-phase commit is enabled, prepared transactions are sent to the subscriber at the time of \u003ccode class=\"command\"\u003ePREPARE TRANSACTION\u003c/code\u003e, 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.\u003c/p\u003e\u003cp\u003eThe implementation of two-phase commit requires that replication has successfully finished the initial table synchronization phase. So even when \u003ccode class=\"literal\"\u003etwo_phase\u003c/code\u003e is enabled for a subscription, the internal two-phase state remains temporarily \u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003epending\u003c/span\u003e”\u003c/span\u003e until the initialization phase completes. See column \u003ccode class=\"structfield\"\u003esubtwophasestate\u003c/code\u003e of \u003ca href=\"/docs/19/catalog-pg-subscription.html\" title=\"52.54. pg_subscription\"\u003e\u003ccode class=\"structname\"\u003epg_subscription\u003c/code\u003e\u003c/a\u003e to know the actual two-phase state.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003edisable_on_error\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e)\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eSpecifies whether the subscription should be automatically disabled if any errors are detected by subscription workers during data replication from the publisher. The default is \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003epassword_required\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e)\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eIf set to \u003ccode class=\"literal\"\u003etrue\u003c/code\u003e, 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 \u003ccode class=\"literal\"\u003etrue\u003c/code\u003e. Only superusers can set this value to \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003erun_as_owner\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e)\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eIf true, all replication actions are performed as the subscription owner. If false, replication workers will perform actions on each table or sequence as the owner of that relation. The latter configuration is generally much more secure; for details, see \u003ca href=\"/docs/19/logical-replication-security.html\" title=\"29.12. Security\"\u003eSection 29.12\u003c/a\u003e. The default is \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eorigin\u003c/code\u003e (\u003ccode class=\"type\"\u003estring\u003c/code\u003e)\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eSpecifies whether the subscription will request the publisher to only send changes that don't have an origin or send changes regardless of origin. Setting \u003ccode class=\"literal\"\u003eorigin\u003c/code\u003e to \u003ccode class=\"literal\"\u003enone\u003c/code\u003e means that the subscription will request the publisher to only send changes that don't have an origin. Setting \u003ccode class=\"literal\"\u003eorigin\u003c/code\u003e to \u003ccode class=\"literal\"\u003eany\u003c/code\u003e means that the publisher sends changes regardless of their origin. The default is \u003ccode class=\"literal\"\u003eany\u003c/code\u003e. This parameter has no effect for sequences.\u003c/p\u003e\u003cp\u003eSee \u003ca href=\"/docs/19/sql-createsubscription.html#SQL-CREATESUBSCRIPTION-NOTES\" title=\"Notes\"\u003eNotes\u003c/a\u003e for details of how \u003ccode class=\"literal\"\u003ecopy_data = true\u003c/code\u003e can interact with the \u003ccode class=\"literal\"\u003eorigin\u003c/code\u003e parameter.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003efailover\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e)\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eSpecifies whether the replication slots associated with the subscription are enabled to be synced to the standbys so that logical replication can be resumed from the new primary after failover. The default is \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eretain_dead_tuples\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e)\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eSpecifies whether the information (e.g., dead tuples, commit timestamps, and origins) required for conflict detection on the subscriber is retained. The default is \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e. If set to \u003ccode class=\"literal\"\u003etrue\u003c/code\u003e, the detection of \u003ca href=\"/docs/19/logical-replication-conflicts.html#CONFLICT-UPDATE-DELETED\"\u003eupdate_deleted\u003c/a\u003e is enabled, and a physical replication slot named \u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003e\u003ccode class=\"literal\"\u003epg_conflict_detection\u003c/code\u003e\u003c/span\u003e”\u003c/span\u003e is created on the subscriber to prevent the information for detecting conflicts from being removed. This parameter has no effect for sequences.\u003c/p\u003e\u003cp\u003eNote that the information useful for conflict detection is retained only after the creation of the slot. You can verify the existence of this slot by querying \u003ca href=\"/docs/19/view-pg-replication-slots.html\" title=\"53.22. pg_replication_slots\"\u003epg_replication_slots\u003c/a\u003e. And even if multiple subscriptions on one node enable this option, only one replication slot will be created. Also, \u003ccode class=\"varname\"\u003ewal_level\u003c/code\u003e must be set to \u003ccode class=\"literal\"\u003ereplica\u003c/code\u003e or higher to allow the replication slot to be used.\u003c/p\u003e\u003cdiv class=\"caution\"\u003e\u003ch3\u003eCaution\u003c/h3\u003e\u003cp\u003eNote that the information for conflict detection cannot be purged if the subscription is disabled; thus, the information will accumulate until the subscription is enabled. To prevent excessive accumulation, it is recommended to disable \u003ccode class=\"literal\"\u003eretain_dead_tuples\u003c/code\u003e if the subscription will be inactive for an extended period. The same applies while the subscription is enabled but its apply worker is not running, for example because the worker repeatedly fails to apply a change. The oldest information being retained can be seen as the \u003ccode class=\"structfield\"\u003exmin\u003c/code\u003e of the \u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003e\u003ccode class=\"literal\"\u003epg_conflict_detection\u003c/code\u003e\u003c/span\u003e”\u003c/span\u003e slot in \u003ca href=\"/docs/19/view-pg-replication-slots.html\" title=\"53.22. pg_replication_slots\"\u003epg_replication_slots\u003c/a\u003e.\u003c/p\u003e\u003cp\u003eAdditionally when enabling \u003ccode class=\"literal\"\u003eretain_dead_tuples\u003c/code\u003e for conflict detection in logical replication, it is important to design the replication topology to balance data retention requirements with overall system performance. This option provides minimal performance overhead when applied appropriately. The following scenarios illustrate effective usage patterns when enabling this option.\u003c/p\u003e\u003cp\u003ea. Large Tables with Bidirectional Writes: For large tables subject to concurrent writes on both publisher and subscriber nodes, publishers can define row filters when creating publications to segment data. This allows multiple subscriptions to replicate exclusive subsets of the table in parallel, optimizing the throughput.\u003c/p\u003e\u003cp\u003eb. Write-Enabled Subscribers: If a subscriber node is expected to perform write operations, replication can be structured using multiple publications and subscriptions. By distributing tables across these publications, the workload is spread among several apply workers, improving concurrency and reducing contention.\u003c/p\u003e\u003cp\u003ec. Read-Only Subscribers: In configurations involving single or multiple publisher nodes performing concurrent write operations, read-only subscriber nodes may replicate changes without seeing a performance impact if it does index scan. However, if the subscriber is impacted due to replication lag or scan performance (say due to sequential scans), it needs to follow one of the two previous strategies to distribute the workload on the subscriber.\u003c/p\u003e\u003c/div\u003e\u003cp\u003eThis option cannot be enabled if the publisher is a physical standby.\u003c/p\u003e\u003cp\u003eEnabling this option ensures retention of information useful for conflict detection solely for changes occurring locally on the publisher. For the changes originating from different origins, reliable conflict detection cannot be guaranteed.\u003c/p\u003e\u003cp\u003eDetection of \u003ca href=\"/docs/19/logical-replication-conflicts.html#CONFLICT-UPDATE-DELETED\"\u003eupdate_deleted\u003c/a\u003e also requires \u003ca href=\"/docs/19/runtime-config-replication.html#GUC-TRACK-COMMIT-TIMESTAMP\"\u003e\u003ccode class=\"varname\"\u003etrack_commit_timestamp\u003c/code\u003e\u003c/a\u003e to be enabled on the subscriber, and a warning is issued if \u003ccode class=\"literal\"\u003eretain_dead_tuples\u003c/code\u003e is enabled while it is not. If \u003ccode class=\"varname\"\u003etrack_commit_timestamp\u003c/code\u003e is disabled afterwards, the information for conflict detection continues to be retained, but the conflict is reported as \u003ca href=\"/docs/19/logical-replication-conflicts.html#CONFLICT-UPDATE-MISSING\"\u003eupdate_missing\u003c/a\u003e instead, so the retention no longer serves its purpose.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003emax_retention_duration\u003c/code\u003e (\u003ccode class=\"type\"\u003einteger\u003c/code\u003e)\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eMaximum duration in milliseconds for which this subscription's apply worker is allowed to retain the information useful for conflict detection when \u003ccode class=\"literal\"\u003eretain_dead_tuples\u003c/code\u003e is enabled. The default value is \u003ccode class=\"literal\"\u003e0\u003c/code\u003e, indicating that the information is retained until it is no longer needed for detection purposes.\u003c/p\u003e\u003cp\u003eThe information useful for conflict detection is no longer retained if all apply workers associated with the subscriptions, where \u003ccode class=\"literal\"\u003eretain_dead_tuples\u003c/code\u003e is enabled, confirm that the retention duration has exceeded the \u003ccode class=\"literal\"\u003emax_retention_duration\u003c/code\u003e set within the corresponding subscription. The retention will automatically resume when at least one apply worker confirms that the retention duration is within the specified limit, or when a new subscription is created with \u003ccode class=\"literal\"\u003eretain_dead_tuples = true\u003c/code\u003e. Alternatively, retention can be manually resumed by re-enabling \u003ccode class=\"literal\"\u003eretain_dead_tuples\u003c/code\u003e.\u003c/p\u003e\u003cp\u003eNote that overall retention will not stop if other subscriptions that have a value greater than 0 for this parameter have not exceeded it, or if they set this option to 0.\u003c/p\u003e\u003cp\u003eThis option is effective only when \u003ccode class=\"literal\"\u003eretain_dead_tuples\u003c/code\u003e is enabled and the apply worker associated with the subscription is active.\u003c/p\u003e\u003cdiv class=\"caution\"\u003e\u003ch3\u003eCaution\u003c/h3\u003e\u003cp\u003eNote that the retention duration is not evaluated while the subscription is disabled or its apply worker is not running. Thus, the information retained for conflict detection will continue to accumulate regardless of this setting until the subscription is enabled or its apply worker resumes. To prevent excessive accumulation, consider disabling \u003ccode class=\"literal\"\u003eretain_dead_tuples\u003c/code\u003e if the subscription will be inactive for an extended period.\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"warning\"\u003e\u003ch3\u003eWarning\u003c/h3\u003e\u003cp\u003eNote that setting a non-zero value for this option could lead to information for conflict detection being removed prematurely, potentially resulting in incorrect conflict detection.\u003c/p\u003e\u003c/div\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003ewal_receiver_timeout\u003c/code\u003e (\u003ccode class=\"type\"\u003etext\u003c/code\u003e)\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe value of this parameter overrides the \u003ca href=\"/docs/19/runtime-config-replication.html#GUC-WAL-RECEIVER-TIMEOUT\"\u003ewal_receiver_timeout\u003c/a\u003e setting within this subscription's apply worker processes. The default value is \u003ccode class=\"literal\"\u003e-1\u003c/code\u003e, which means it does not override the global setting, i.e., the value from the server configuration, command line, role or database settings will be used instead.\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003cp\u003eWhen specifying a parameter of type \u003ccode class=\"type\"\u003eboolean\u003c/code\u003e, the \u003ccode class=\"literal\"\u003e=\u003c/code\u003e \u003cem class=\"replaceable\"\u003e\u003ccode\u003evalue\u003c/code\u003e\u003c/em\u003e part can be omitted, which is equivalent to specifying \u003ccode class=\"literal\"\u003eTRUE\u003c/code\u003e.\u003c/p\u003e","key":"parameters","title":"Parameters"},{"html":"\u003cp\u003eSee \u003ca href=\"/docs/19/logical-replication-security.html\" title=\"29.12. Security\"\u003eSection 29.12\u003c/a\u003e for details on how to configure access control between the subscription and the publication instance.\u003c/p\u003e\u003cp\u003eWhen creating a replication slot (the default behavior), \u003ccode class=\"command\"\u003eCREATE SUBSCRIPTION\u003c/code\u003e cannot be executed inside a transaction block.\u003c/p\u003e\u003cp\u003eCreating 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 \u003ccode class=\"command\"\u003eCREATE SUBSCRIPTION\u003c/code\u003e call will hang. To make this work, create the replication slot separately (using the function \u003ccode class=\"function\"\u003epg_create_logical_replication_slot\u003c/code\u003e with the plugin name \u003ccode class=\"literal\"\u003epgoutput\u003c/code\u003e) and create the subscription using the parameter \u003ccode class=\"literal\"\u003ecreate_slot = false\u003c/code\u003e. See \u003ca href=\"/docs/19/logical-replication-subscription.html#LOGICAL-REPLICATION-SUBSCRIPTION-EXAMPLES-DEFERRED-SLOT\" title=\"29.2.3. Examples: Deferred Logical Replication Slot Creation\"\u003eSection 29.2.3\u003c/a\u003e for examples. This is an implementation restriction that might be lifted in a future release.\u003c/p\u003e\u003cp\u003eIf any table in the publication has a \u003ccode class=\"literal\"\u003eWHERE\u003c/code\u003e clause, rows for which the \u003cem class=\"replaceable\"\u003e\u003ccode\u003eexpression\u003c/code\u003e\u003c/em\u003e evaluates to \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e or \u003ccode class=\"literal\"\u003eNULL\u003c/code\u003e will not be published. If the subscription has several publications in which the same table has been published with different \u003ccode class=\"literal\"\u003eWHERE\u003c/code\u003e clauses, a row will be published if any of the expressions (referring to that publish operation) are satisfied. In the case of different \u003ccode class=\"literal\"\u003eWHERE\u003c/code\u003e clauses, if one of the publications has no \u003ccode class=\"literal\"\u003eWHERE\u003c/code\u003e clause (referring to that publish operation) or the publication is declared as \u003ca href=\"/docs/19/sql-createpublication.html#SQL-CREATEPUBLICATION-PARAMS-FOR-ALL-TABLES\"\u003e\u003ccode class=\"literal\"\u003eFOR ALL TABLES\u003c/code\u003e\u003c/a\u003e or \u003ca href=\"/docs/19/sql-createpublication.html#SQL-CREATEPUBLICATION-PARAMS-FOR-TABLES-IN-SCHEMA\"\u003e\u003ccode class=\"literal\"\u003eFOR TABLES IN SCHEMA\u003c/code\u003e\u003c/a\u003e, rows are always published regardless of the definition of the other expressions. If the subscriber is a \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 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 \u003ca href=\"/docs/19/sql-createpublication.html#SQL-CREATEPUBLICATION-PARAMS-WITH-PUBLISH\"\u003e\u003ccode class=\"literal\"\u003epublish\u003c/code\u003e\u003c/a\u003e parameter when copying existing table data, some rows may be copied that would not be replicated using DML. See \u003ca href=\"/docs/19/logical-replication-subscription.html#LOGICAL-REPLICATION-SUBSCRIPTION-EXAMPLES\" title=\"29.2.2. Examples: Set Up Logical Replication\"\u003eSection 29.2.2\u003c/a\u003e for examples.\u003c/p\u003e\u003cp\u003eSubscriptions having several publications in which the same table has been published with different column lists are not supported.\u003c/p\u003e\u003cp\u003eWe allow non-existent publications to be specified so that users can add those later. This means \u003ca href=\"/docs/19/catalog-pg-subscription.html\" title=\"52.54. pg_subscription\"\u003e\u003ccode class=\"structname\"\u003epg_subscription\u003c/code\u003e\u003c/a\u003e can have non-existent publications.\u003c/p\u003e\u003cp\u003eWhen using a subscription parameter combination of \u003ccode class=\"literal\"\u003ecopy_data = true\u003c/code\u003e and \u003ccode class=\"literal\"\u003eorigin = NONE\u003c/code\u003e, 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.\u003c/p\u003e\u003cp\u003eTo find which tables might potentially include non-local origins (due to other subscriptions created on the publisher) try this SQL query:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003e# substitute \u0026lt;pub-names\u0026gt; below with your publication name(s) to be queried\nSELECT DISTINCT PT.schemaname, PT.tablename\nFROM pg_publication_tables PT\n     JOIN pg_class C ON (C.relname = PT.tablename)\n     JOIN pg_namespace N ON (N.nspname = PT.schemaname),\n     pg_subscription_rel PS\nWHERE C.relnamespace = N.oid AND\n      (PS.srrelid = C.oid OR\n      C.oid IN (SELECT relid FROM pg_partition_ancestors(PS.srrelid) UNION\n                SELECT relid FROM pg_partition_tree(PS.srrelid))) AND\n      PT.pubname IN (\u0026lt;pub-names\u0026gt;);\n\u003c/pre\u003e","key":"notes","title":"Notes"},{"html":"\u003cp\u003eCreate a subscription to a remote server that replicates tables in the publications \u003ccode class=\"literal\"\u003emypublication\u003c/code\u003e and \u003ccode class=\"literal\"\u003einsert_only\u003c/code\u003e and starts replicating immediately on commit:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE SUBSCRIPTION mysub\n         CONNECTION 'host=192.168.1.50 port=5432 user=foo dbname=foodb'\n        PUBLICATION mypublication, insert_only;\n\u003c/pre\u003e\u003cp\u003eCreate a subscription to a remote server that replicates tables in the \u003ccode class=\"literal\"\u003einsert_only\u003c/code\u003e publication and does not start replicating until enabled at a later time.\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE SUBSCRIPTION mysub\n         CONNECTION 'host=192.168.1.50 port=5432 user=foo dbname=foodb'\n        PUBLICATION insert_only\n               WITH (enabled = false);\n\u003c/pre\u003e","key":"examples","title":"Examples"},{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003eCREATE SUBSCRIPTION\u003c/code\u003e is a \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e extension.\u003c/p\u003e","key":"compatibility","title":"Compatibility"},{"html":"\u003cspan class=\"simplelist\"\u003e\u003ca href=\"/wiki/sql/alter-subscription/?v=19\" title=\"ALTER SUBSCRIPTION\"\u003e\u003cspan class=\"refentrytitle\"\u003eALTER SUBSCRIPTION\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/drop-subscription/?v=19\" title=\"DROP SUBSCRIPTION\"\u003e\u003cspan class=\"refentrytitle\"\u003eDROP SUBSCRIPTION\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/create-publication/?v=19\" title=\"CREATE PUBLICATION\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE PUBLICATION\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/alter-publication/?v=19\" title=\"ALTER PUBLICATION\"\u003e\u003cspan class=\"refentrytitle\"\u003eALTER PUBLICATION\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e","key":"see_also","title":"See Also"}],"sections_same_as":"","slug":"19","synopsis_html":"CREATE SUBSCRIPTION \u003cem class=\"replaceable\"\u003e\u003ccode\u003esubscription_name\u003c/code\u003e\u003c/em\u003e\n    { SERVER \u003cem class=\"replaceable\"\u003e\u003ccode\u003eserver_name\u003c/code\u003e\u003c/em\u003e | CONNECTION '\u003cem class=\"replaceable\"\u003e\u003ccode\u003econninfo\u003c/code\u003e\u003c/em\u003e' }\n    PUBLICATION \u003cem class=\"replaceable\"\u003e\u003ccode\u003epublication_name\u003c/code\u003e\u003c/em\u003e [, ...]\n    [ WITH ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003esubscription_parameter\u003c/code\u003e\u003c/em\u003e [= \u003cem class=\"replaceable\"\u003e\u003ccode\u003evalue\u003c/code\u003e\u003c/em\u003e] [, ... ] ) ]","synopsis_text":"CREATE SUBSCRIPTION subscription_name\n{ SERVER server_name | CONNECTION 'conninfo' }\nPUBLICATION publication_name [, ...]\n[ WITH ( subscription_parameter [= value] [, ... ] ) ]","translation_provenance":{"inputs":[{"kind":"current-pgdoc-translation","path":"/Users/vonng/pgsty/pgdoc/zh/19/html/sql-createsubscription.html","sha256":"340dfc203db7c36e5ef0e3b48d20befa38fd2dcccf0633de8a91264ad7cd38a5"}],"kind":"same-version-pgdoc","locale":"zh-Hans","source_revision":"b1b500221dbe5437cfca004d6b04d3f6e422765d","version":"19"},"zh_from":"same-version-pgdoc"},"ManualEvidence":{},"MeasuredEvidence":{}},"Text":{"Collection":"sql","Key":"create-subscription","SourceDatabase":"center","Version":"19","Locale":"zh-Hans","Title":"CREATE SUBSCRIPTION","Summary":"定义一个新的订阅","BodyHTML":"\u003cpre\u003eCREATE SUBSCRIPTION subscription_name\n{ SERVER server_name | CONNECTION \u0026#39;conninfo\u0026#39; }\nPUBLICATION publication_name [, ...]\n[ WITH ( subscription_parameter [= value] [, ... ] ) ]\u003c/pre\u003e\u003csection\u003e\u003ch2\u003e描述\u003c/h2\u003e\u003cp\u003e\n\u003ccode\u003eCREATE SUBSCRIPTION\u003c/code\u003e 添加一个新的逻辑复制订阅。创建订阅的用户将成为该订阅的所有者。订阅名称必须与当前数据库中任何现有订阅的名称不同。\n  \u003c/p\u003e\u003cp\u003e\n   订阅表示与发布者的复制连接。因此，除了在本地系统目录中添加定义之外，该命令通常还会在发布者上创建一个复制槽。\n  \u003c/p\u003e\u003cp\u003e\n   除非订阅初始即被禁用，否则在执行该命令所在事务提交时，会启动一个逻辑复制工作进程为新订阅复制数据。\n  \u003c/p\u003e\u003cp\u003e\n   要能够创建订阅，必须具有 \u003ccode\u003epg_create_subscription\u003c/code\u003e 角色的权限，以及当前数据库上的 \u003ccode\u003eCREATE\u003c/code\u003e 权限。\n  \u003c/p\u003e\u003cp\u003e\n   关于订阅以及整个逻辑复制的更多信息，请参见\u003ca href=\"/zh/docs/19/logical-replication-subscription.html\" rel=\"nofollow\"\u003e第 29.2 节\u003c/a\u003e和\u003ca href=\"/zh/docs/19/logical-replication.html\" rel=\"nofollow\"\u003e第 29 章\u003c/a\u003e。\n  \u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e参数\u003c/h2\u003e\u003cdiv\u003e\u003cdl\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-NAME\"\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003esubscription_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n      新订阅的名称。\n     \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-SERVER\"\u003e\u003cspan\u003e\u003ccode\u003eSERVER \u003cem\u003e\u003ccode\u003eserver_name\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n      供连接使用的外部服务器。该服务器的外部数据包装器必须已注册\n      \u003cem\u003e\u003ccode\u003econnection_function\u003c/code\u003e\u003c/em\u003e，并且该服务器上必须存在订阅所有者的用户映射。此外，订阅所有者必须在\n      \u003cem\u003e\u003ccode\u003eserver_name\u003c/code\u003e\u003c/em\u003e 上具有 \u003ccode\u003eUSAGE\u003c/code\u003e 权限。\n     \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-CONNECTION\"\u003e\u003cspan\u003e\u003ccode\u003eCONNECTION \u0026#39;\u003cem\u003e\u003ccode\u003econninfo\u003c/code\u003e\u003c/em\u003e\u0026#39;\u003c/code\u003e\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n      定义如何连接到发布者数据库的 \u003cspan\u003elibpq\u003c/span\u003e 连接字符串。详情请参见\u003ca href=\"/zh/docs/19/libpq-connect.html#LIBPQ-CONNSTRING\" rel=\"nofollow\"\u003e第 32.1.1 节\u003c/a\u003e。\n     \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-PUBLICATION\"\u003e\u003cspan\u003e\u003ccode\u003ePUBLICATION \u003cem\u003e\u003ccode\u003epublication_name\u003c/code\u003e\u003c/em\u003e [, ...]\u003c/code\u003e\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n      要订阅的发布者上的发布名称。\n     \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH\"\u003e\u003cspan\u003e\u003ccode\u003eWITH ( \u003cem\u003e\u003ccode\u003esubscription_parameter\u003c/code\u003e\u003c/em\u003e [= \u003cem\u003e\u003ccode\u003evalue\u003c/code\u003e\u003c/em\u003e] [, ... ] )\u003c/code\u003e\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n      该子句为订阅指定可选参数。\n     \u003c/p\u003e\u003cp\u003e\n      下列参数控制订阅创建期间的行为：\n\n      \u003c/p\u003e\u003cdiv\u003e\u003cdl\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-CONNECT\"\u003e\u003cspan\u003e\u003ccode\u003econnect\u003c/code\u003e (\u003ccode\u003eboolean\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          指定 \u003ccode\u003eCREATE SUBSCRIPTION\u003c/code\u003e 命令是否连接到发布者。默认值为 \u003ccode\u003etrue\u003c/code\u003e。将其设为\n          \u003ccode\u003efalse\u003c/code\u003e 会强制把 \u003ccode\u003ecreate_slot\u003c/code\u003e、\u003ccode\u003eenabled\u003c/code\u003e 和 \u003ccode\u003ecopy_data\u003c/code\u003e 的值设为 \u003ccode\u003efalse\u003c/code\u003e。（不能将 \u003ccode\u003econnect\u003c/code\u003e\n          设为 \u003ccode\u003efalse\u003c/code\u003e 的同时，再把 \u003ccode\u003ecreate_slot\u003c/code\u003e、\u003ccode\u003eenabled\u003c/code\u003e 或 \u003ccode\u003ecopy_data\u003c/code\u003e 设为\n          \u003ccode\u003etrue\u003c/code\u003e。）\n         \u003c/p\u003e\u003cp\u003e\n          由于该选项为 \u003ccode\u003efalse\u003c/code\u003e 时不会建立连接，因此不会订阅任何表和序列。要启动复制，你必须手工创建复制槽、在需要时启用故障切换、启用订阅，并刷新订阅。示例见\u003ca href=\"/zh/docs/19/logical-replication-subscription.html#LOGICAL-REPLICATION-SUBSCRIPTION-EXAMPLES-DEFERRED-SLOT\" rel=\"nofollow\"\u003e第 29.2.3 节\u003c/a\u003e。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-CREATE-SLOT\"\u003e\u003cspan\u003e\u003ccode\u003ecreate_slot\u003c/code\u003e (\u003ccode\u003eboolean\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          指定该命令是否应在发布者上创建复制槽。默认值为\n          \u003ccode\u003etrue\u003c/code\u003e。\n         \u003c/p\u003e\u003cp\u003e\n          如果设为 \u003ccode\u003efalse\u003c/code\u003e，则必须以其他方式自行创建发布者上的复制槽。示例见\u003ca href=\"/zh/docs/19/logical-replication-subscription.html#LOGICAL-REPLICATION-SUBSCRIPTION-EXAMPLES-DEFERRED-SLOT\" rel=\"nofollow\"\u003e第 29.2.3 节\u003c/a\u003e。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-ENABLED\"\u003e\u003cspan\u003e\u003ccode\u003eenabled\u003c/code\u003e (\u003ccode\u003eboolean\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          指定订阅是应当主动进行复制，还是仅完成设置但暂不启动。默认值为 \u003ccode\u003etrue\u003c/code\u003e。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-SLOT-NAME\"\u003e\u003cspan\u003e\u003ccode\u003eslot_name\u003c/code\u003e (\u003ccode\u003estring\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          要使用的发布者上的复制槽名称。默认使用订阅名称作为槽名。该名称不能为\n          \u003ccode\u003epg_conflict_detection\u003c/code\u003e，因为它保留给冲突检测使用。\n         \u003c/p\u003e\u003cp\u003e\n          将 \u003ccode\u003eslot_name\u003c/code\u003e 设为 \u003ccode\u003eNONE\u003c/code\u003e 表示该订阅不关联任何复制槽。这类订阅还必须同时将 \u003ccode\u003eenabled\u003c/code\u003e\n          和 \u003ccode\u003ecreate_slot\u003c/code\u003e 设为 \u003ccode\u003efalse\u003c/code\u003e。当你打算稍后手工创建复制槽时，可使用此设置。示例见\u003ca href=\"/zh/docs/19/logical-replication-subscription.html#LOGICAL-REPLICATION-SUBSCRIPTION-EXAMPLES-DEFERRED-SLOT\" rel=\"nofollow\"\u003e第 29.2.3 节\u003c/a\u003e。\n         \u003c/p\u003e\u003cp\u003e\n          将 \u003ccode\u003eslot_name\u003c/code\u003e 设为有效名称且\n          \u003ccode\u003ecreate_slot\u003c/code\u003e 设为 \u003ccode\u003efalse\u003c/code\u003e 时，所指定复制槽的 \u003ccode\u003efailover\u003c/code\u003e 属性值可能与订阅中对应的\n          \u003ccode\u003efailover\u003c/code\u003e 参数不同。应始终确保该槽的\n          \u003ccode\u003efailover\u003c/code\u003e 属性与订阅中的对应参数一致，反之亦然。否则，发布者上的该槽的行为可能与这些订阅选项所表明的不同：例如，即使订阅的 \u003ccode\u003efailover\u003c/code\u003e 选项已禁用，发布者上的该槽仍可能被同步到备库；或者即使订阅的 \u003ccode\u003efailover\u003c/code\u003e 选项已启用，该槽也可能不会被同步到备库。\n         \u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003cp\u003e\n\u003c/p\u003e\u003cp\u003e\n      下列参数控制订阅创建后的复制行为：\n\n      \u003c/p\u003e\u003cdiv\u003e\u003cdl\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-BINARY\"\u003e\u003cspan\u003e\u003ccode\u003ebinary\u003c/code\u003e (\u003ccode\u003eboolean\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          指定订阅是否请求发布者以二进制格式（而不是文本格式）发送数据。默认值为 \u003ccode\u003efalse\u003c/code\u003e。任何初始表同步复制（见 \u003ccode\u003ecopy_data\u003c/code\u003e）也使用相同格式。二进制格式可能比文本格式更快，但在不同机器架构和\n          \u003cspan\u003ePostgreSQL\u003c/span\u003e 版本之间的可移植性较差。二进制格式对数据类型非常敏感；例如，虽然在文本格式下可以正常工作，但它不允许从 \u003ccode\u003esmallint\u003c/code\u003e 列复制到 \u003ccode\u003einteger\u003c/code\u003e 列。即使启用了此选项，也只有具有二进制发送和接收函数的数据类型才会以二进制方式传输。注意，初始同步要求所有数据类型都具有二进制发送和接收函数，否则同步将失败（关于发送/接收函数的更多信息，请参见 \u003ca href=\"/zh/docs/19/sql-createtype.html\" title=\"CREATE TYPE\" rel=\"nofollow\"\u003e\u003cspan\u003eCREATE TYPE\u003c/span\u003e\u003c/a\u003e）。此参数对序列没有影响。\n         \u003c/p\u003e\u003cp\u003e\n          在进行跨版本复制时，可能会出现这样的情况：发布者对某种数据类型有二进制发送函数，但订阅者缺少该类型的二进制接收函数。在这种情况下，数据传输会失败，因此不能使用 \u003ccode\u003ebinary\u003c/code\u003e 选项。\n         \u003c/p\u003e\u003cp\u003e\n          如果发布者使用的是 16 之前版本的\n          \u003cspan\u003ePostgreSQL\u003c/span\u003e，那么即使\n          \u003ccode\u003ebinary = true\u003c/code\u003e，任何初始表同步也都会使用文本格式。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-COPY-DATA\"\u003e\u003cspan\u003e\u003ccode\u003ecopy_data\u003c/code\u003e (\u003ccode\u003eboolean\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          指定复制开始时是否复制所订阅发布中的预先存在的数据。默认值为 \u003ccode\u003etrue\u003c/code\u003e。\n         \u003c/p\u003e\u003cp\u003e\n          如果这些发布包含 \u003ccode\u003eWHERE\u003c/code\u003e 子句，将会影响被复制的数据。详情请参见\u003ca href=\"/zh/docs/19/sql-createsubscription.html#SQL-CREATESUBSCRIPTION-NOTES\" title=\"注解\" rel=\"nofollow\"\u003e注意\u003c/a\u003e。\n         \u003c/p\u003e\u003cp\u003e\n          关于 \u003ccode\u003ecopy_data = true\u003c/code\u003e 如何与\n          \u003ccode\u003eorigin\u003c/code\u003e 参数相互作用的细节，请参见\u003ca href=\"/zh/docs/19/sql-createsubscription.html#SQL-CREATESUBSCRIPTION-NOTES\" title=\"注解\" rel=\"nofollow\"\u003e注意\u003c/a\u003e。\n         \u003c/p\u003e\u003cp\u003e\n          设置 \u003ccode\u003ecopy_data = true\u003c/code\u003e 时，可能出现关于发布者与订阅者之间序列定义差异的警告。处理这些警告的建议见\u003ca href=\"/zh/docs/19/logical-replication-sequences.html#SEQUENCE-DEFINITION-MISMATCHES\" rel=\"nofollow\"\u003e第 29.7.1 节\u003c/a\u003e。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-STREAMING\"\u003e\u003cspan\u003e\u003ccode\u003estreaming\u003c/code\u003e (\u003ccode\u003eenum\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          指定是否为该订阅启用进行中事务的流式传输。默认值为\n          \u003ccode\u003eparallel\u003c/code\u003e，表示如果有可用的并行应用工作进程，接收到的更改会直接通过其中之一应用。如果没有空闲的并行应用工作进程可以处理流式事务，那么这些更改会写入临时文件，并在事务提交后再应用。注意，如果并行应用工作进程中发生错误，远端事务的完成 LSN\n          可能不会记录到服务器日志中。此参数对序列没有影响。\n         \u003c/p\u003e\u003cdiv\u003e\u003ch3\u003e小心\u003c/h3\u003e\u003cp\u003e\n           当发布者与订阅者的模式不同时，存在发生死锁的风险，尽管这种情况很少见。应用工作进程会自动重试这些事务。\n          \u003c/p\u003e\u003c/div\u003e\u003cp\u003e\n          如果设为 \u003ccode\u003eon\u003c/code\u003e，接收到的更改会写入临时文件，然后仅在事务在发布者上提交且被订阅者接收之后才应用。\n         \u003c/p\u003e\u003cp\u003e\n          如果设为 \u003ccode\u003eoff\u003c/code\u003e，所有事务都会先在发布者上完全解码，然后才整体发送给订阅者。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-SYNCHRONOUS-COMMIT\"\u003e\u003cspan\u003e\u003ccode\u003esynchronous_commit\u003c/code\u003e (\u003ccode\u003eenum\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          该参数的值会覆盖此订阅应用工作进程中的 \u003ca href=\"/zh/docs/19/runtime-config-wal.html#GUC-SYNCHRONOUS-COMMIT\" rel=\"nofollow\"\u003esynchronous_commit\u003c/a\u003e 设置。默认值为\n          \u003ccode\u003eoff\u003c/code\u003e。此参数对序列没有影响。\n         \u003c/p\u003e\u003cp\u003e\n          对逻辑复制来说，使用 \u003ccode\u003eoff\u003c/code\u003e 是安全的：如果订阅者因缺少同步而丢失了事务，数据会再次从发布者发送过来。\n         \u003c/p\u003e\u003cp\u003e\n          在进行同步逻辑复制时，可能更适合使用不同的设置。逻辑复制工作进程会向发布者报告写入和刷盘位置，而在使用同步复制时，发布者会等待真正的刷盘完成。这意味着，当订阅被用于同步复制时，将订阅者的\n          \u003ccode\u003esynchronous_commit\u003c/code\u003e 设为 \u003ccode\u003eoff\u003c/code\u003e\n          可能会增加发布者上 \u003ccode\u003eCOMMIT\u003c/code\u003e 的延迟。在这种场景下，将 \u003ccode\u003esynchronous_commit\u003c/code\u003e 设为 \u003ccode\u003elocal\u003c/code\u003e\n          或更高可能更有利。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-TWO-PHASE\"\u003e\u003cspan\u003e\u003ccode\u003etwo_phase\u003c/code\u003e (\u003ccode\u003eboolean\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          指定是否为该订阅启用两阶段提交。默认值为\n          \u003ccode\u003efalse\u003c/code\u003e。此参数对序列没有影响。\n         \u003c/p\u003e\u003cp\u003e\n          启用两阶段提交时，预备事务会在\n          \u003ccode\u003ePREPARE TRANSACTION\u003c/code\u003e 时发送给订阅者，并且在订阅者上也会作为两阶段事务处理。否则，预备事务只有在提交时才会发送给订阅者，随后由订阅者立即处理。\n         \u003c/p\u003e\u003cp\u003e\n          两阶段提交的实现要求复制已经成功完成初始表同步阶段。因此，即使订阅启用了 \u003ccode\u003etwo_phase\u003c/code\u003e，内部的两阶段状态也会暂时保持为\u003cspan\u003e“\u003cspan\u003epending\u003c/span\u003e”\u003c/span\u003e，直到初始化阶段完成。要了解实际的两阶段状态，请参见\n          \u003ca href=\"/zh/docs/19/catalog-pg-subscription.html\" rel=\"nofollow\"\u003e\u003ccode\u003epg_subscription\u003c/code\u003e\u003c/a\u003e\n          的 \u003ccode\u003esubtwophasestate\u003c/code\u003e 列。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-DISABLE-ON-ERROR\"\u003e\u003cspan\u003e\u003ccode\u003edisable_on_error\u003c/code\u003e (\u003ccode\u003eboolean\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          指定如果订阅工作进程在从发布者复制数据期间检测到任何错误，是否自动禁用该订阅。默认值为 \u003ccode\u003efalse\u003c/code\u003e。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-PASSWORD-REQUIRED\"\u003e\u003cspan\u003e\u003ccode\u003epassword_required\u003c/code\u003e (\u003ccode\u003eboolean\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          如果设为 \u003ccode\u003etrue\u003c/code\u003e，则由于该订阅而建立的到发布者的连接必须使用密码认证，并且密码必须作为连接字符串的一部分指定。如果该订阅由超级用户拥有，则此设置会被忽略。默认值为 \u003ccode\u003etrue\u003c/code\u003e。只有超级用户才能将该值设为 \u003ccode\u003efalse\u003c/code\u003e。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-RUN-AS-OWNER\"\u003e\u003cspan\u003e\u003ccode\u003erun_as_owner\u003c/code\u003e (\u003ccode\u003eboolean\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          如果为 \u003ccode\u003etrue\u003c/code\u003e，所有复制操作都以订阅所有者的身份执行。如果为 \u003ccode\u003efalse\u003c/code\u003e，复制工作进程会在每张表或序列上以该关系所有者的身份执行操作。后一种配置通常安全得多；详情请参见\u003ca href=\"/zh/docs/19/logical-replication-security.html\" rel=\"nofollow\"\u003e第 29.12 节\u003c/a\u003e。默认值为 \u003ccode\u003efalse\u003c/code\u003e。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-ORIGIN\"\u003e\u003cspan\u003e\u003ccode\u003eorigin\u003c/code\u003e (\u003ccode\u003estring\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          指定订阅是请求发布者仅发送没有复制源的更改，还是无论复制源如何都发送更改。将 \u003ccode\u003eorigin\u003c/code\u003e 设为 \u003ccode\u003enone\u003c/code\u003e 表示订阅会请求发布者仅发送没有复制源的更改。将 \u003ccode\u003eorigin\u003c/code\u003e 设为\n          \u003ccode\u003eany\u003c/code\u003e 表示发布者无论复制源如何都发送更改。默认值为\n          \u003ccode\u003eany\u003c/code\u003e。此参数对序列没有影响。\n         \u003c/p\u003e\u003cp\u003e\n          关于 \u003ccode\u003ecopy_data = true\u003c/code\u003e 如何与\n          \u003ccode\u003eorigin\u003c/code\u003e 参数相互作用的细节，请参见\u003ca href=\"/zh/docs/19/sql-createsubscription.html#SQL-CREATESUBSCRIPTION-NOTES\" title=\"注解\" rel=\"nofollow\"\u003e注意\u003c/a\u003e。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-FAILOVER\"\u003e\u003cspan\u003e\u003ccode\u003efailover\u003c/code\u003e (\u003ccode\u003eboolean\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          指定与该订阅关联的复制槽是否启用同步到备库，以便在故障切换后能够从新的主库继续逻辑复制。默认值为 \u003ccode\u003efalse\u003c/code\u003e。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-RETAIN-DEAD-TUPLES\"\u003e\u003cspan\u003e\u003ccode\u003eretain_dead_tuples\u003c/code\u003e (\u003ccode\u003eboolean\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          指定是否保留订阅者上冲突检测所需的信息（例如死元组、提交时间戳和来源）。默认值为 \u003ccode\u003efalse\u003c/code\u003e。如果设为\n          \u003ccode\u003etrue\u003c/code\u003e，则会启用 \u003ca href=\"/zh/docs/19/logical-replication-conflicts.html#CONFLICT-UPDATE-DELETED\" rel=\"nofollow\"\u003eupdate_deleted\u003c/a\u003e 的检测，并在订阅者上创建一个名为\u003cspan\u003e“\u003cspan\u003e\u003ccode\u003epg_conflict_detection\u003c/code\u003e\u003c/span\u003e”\u003c/span\u003e的物理复制槽，以防止用于检测冲突的信息被移除。此参数对序列没有影响。\n         \u003c/p\u003e\u003cp\u003e\n          请注意，只有在创建该槽之后，才会保留用于冲突检测的信息。可以通过查询\n          \u003ca href=\"/zh/docs/19/view-pg-replication-slots.html\" rel=\"nofollow\"\u003epg_replication_slots\u003c/a\u003e 来验证该槽是否存在。即使同一节点上的多个订阅启用此选项，也只会创建一个复制槽。此外，必须将 \u003ccode\u003ewal_level\u003c/code\u003e 设置为 \u003ccode\u003ereplica\u003c/code\u003e\n          或更高，才能允许使用该复制槽。\n         \u003c/p\u003e\u003cdiv\u003e\u003ch3\u003e小心\u003c/h3\u003e\u003cp\u003e\n           请注意，如果订阅被禁用，用于冲突检测的信息将无法被清除；因此，这些信息会一直累积，直到订阅被启用。为了防止过度累积，建议在订阅将长时间处于非活动状态时禁用 \u003ccode\u003eretain_dead_tuples\u003c/code\u003e。如果订阅已启用，但其应用工作进程没有运行（例如工作进程反复无法应用某项更改），情况也相同。可以通过 \u003ca href=\"/zh/docs/19/view-pg-replication-slots.html\" rel=\"nofollow\"\u003epg_replication_slots\u003c/a\u003e\n           中\u003cspan\u003e“\u003cspan\u003e\u003ccode\u003epg_conflict_detection\u003c/code\u003e\u003c/span\u003e”\u003c/span\u003e复制槽的\n           \u003ccode\u003exmin\u003c/code\u003e 查看所保留的最旧信息。\n          \u003c/p\u003e\u003cp\u003e\n           此外，在逻辑复制中为冲突检测启用 \u003ccode\u003eretain_dead_tuples\u003c/code\u003e 时，设计复制拓扑以平衡数据保留需求与整体系统性能非常重要。该选项在适当使用时只会带来极小的性能开销。以下场景展示了启用该选项时的有效用法模式。\n          \u003c/p\u003e\u003cp\u003e\n           a. 双向写入的大表：对于在发布者和订阅者节点上都存在并发写入的大表，发布者可以在创建发布时定义行过滤器来分割数据。这样可以让多个订阅并行复制该表的互斥子集，从而优化吞吐量。\n          \u003c/p\u003e\u003cp\u003e\n           b. 可写入的订阅者：如果预期订阅者节点需要执行写操作，则可以通过多个发布和订阅来组织复制。通过将表分配到这些发布中，工作负载会分散到多个应用工作进程之间，从而提高并发性并减少争用。\n          \u003c/p\u003e\u003cp\u003e\n           c. 只读订阅者：在涉及一个或多个发布者节点执行并发写操作的配置中，只读订阅者节点如果执行的是索引扫描，则可能不会看到性能影响。然而，如果订阅者由于复制延迟或扫描性能（例如顺序扫描）而受到影响，则需要采用前述两种策略之一来在订阅者上分散负载。\n          \u003c/p\u003e\u003c/div\u003e\u003cp\u003e\n          如果发布者是物理备库，则不能启用此选项。\n         \u003c/p\u003e\u003cp\u003e\n          启用此选项只能保证保留用于冲突检测的信息，且仅针对在发布者本地发生的更改。对于来自不同来源的更改，无法保证可靠的冲突检测。\n         \u003c/p\u003e\u003cp\u003e\n          检测 \u003ca href=\"/zh/docs/19/logical-replication-conflicts.html#CONFLICT-UPDATE-DELETED\" rel=\"nofollow\"\u003eupdate_deleted\u003c/a\u003e 还要求在订阅者上启用\n          \u003ca href=\"/zh/docs/19/runtime-config-replication.html#GUC-TRACK-COMMIT-TIMESTAMP\" rel=\"nofollow\"\u003e\u003ccode\u003etrack_commit_timestamp\u003c/code\u003e\u003c/a\u003e。如果它未启用，而启用了 \u003ccode\u003eretain_dead_tuples\u003c/code\u003e，则会发出警告。如果之后禁用了 \u003ccode\u003etrack_commit_timestamp\u003c/code\u003e，用于冲突检测的信息仍会继续保留，但冲突会改为报告为 \u003ca href=\"/zh/docs/19/logical-replication-conflicts.html#CONFLICT-UPDATE-MISSING\" rel=\"nofollow\"\u003eupdate_missing\u003c/a\u003e，因此保留这些信息便失去了作用。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-MAX-RETENTION-DURATION\"\u003e\u003cspan\u003e\u003ccode\u003emax_retention_duration\u003c/code\u003e (\u003ccode\u003einteger\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          当启用 \u003ccode\u003eretain_dead_tuples\u003c/code\u003e 时，此订阅的应用工作进程允许保留用于冲突检测的信息的最长时长，单位为毫秒。默认值为\n          \u003ccode\u003e0\u003c/code\u003e，表示在不再需要进行检测之前一直保留这些信息。\n         \u003c/p\u003e\u003cp\u003e\n          如果所有与启用了 \u003ccode\u003eretain_dead_tuples\u003c/code\u003e 的订阅相关联的应用工作进程都确认保留时长已经超过相应订阅中设置的\n          \u003ccode\u003emax_retention_duration\u003c/code\u003e，则用于冲突检测的信息将不再被保留。只要至少有一个应用工作进程确认保留时长仍在指定限制内，或者创建了一个新的 \u003ccode\u003eretain_dead_tuples = true\u003c/code\u003e 订阅，保留就会自动恢复。或者，也可以通过重新启用\n          \u003ccode\u003eretain_dead_tuples\u003c/code\u003e 手工恢复保留。\n         \u003c/p\u003e\u003cp\u003e\n          注意，如果其他订阅为此参数设置了大于 0 的值且尚未超过该值，或者将此选项设为 \u003ccode\u003e0\u003c/code\u003e，则整体保留不会停止。\n         \u003c/p\u003e\u003cp\u003e\n          只有当启用了 \u003ccode\u003eretain_dead_tuples\u003c/code\u003e 且与该订阅关联的应用工作进程处于活动状态时，此选项才有效。\n         \u003c/p\u003e\u003cdiv\u003e\u003ch3\u003e小心\u003c/h3\u003e\u003cp\u003e\n           注意，订阅被禁用或其应用工作进程未运行时，不会评估保留时长。因此，无论此设置如何，用于冲突检测的信息都会继续累积，直到订阅被启用或其应用工作进程恢复运行。为防止过度累积，如果订阅将长时间处于非活动状态，应考虑禁用\n           \u003ccode\u003eretain_dead_tuples\u003c/code\u003e。\n          \u003c/p\u003e\u003c/div\u003e\u003cdiv\u003e\u003ch3\u003e警告\u003c/h3\u003e\u003cp\u003e\n           注意，将此选项设为非零值可能导致用于冲突检测的信息被过早移除，从而可能造成错误的冲突检测。\n          \u003c/p\u003e\u003c/div\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-WAL-RECEIVER-TIMEOUT\"\u003e\u003cspan\u003e\u003ccode\u003ewal_receiver_timeout\u003c/code\u003e (\u003ccode\u003etext\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          该参数的值会覆盖此订阅应用工作进程中的 \u003ca href=\"/zh/docs/19/runtime-config-replication.html#GUC-WAL-RECEIVER-TIMEOUT\" rel=\"nofollow\"\u003ewal_receiver_timeout\u003c/a\u003e 设置。默认值为\n          \u003ccode\u003e-1\u003c/code\u003e，表示不覆盖全局设置，也就是说将改用服务器配置、命令行、角色或数据库设置中的值。\n         \u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003cp\u003e\n   当指定 \u003ccode\u003eboolean\u003c/code\u003e 类型的参数时，可以省略\n   \u003ccode\u003e=\u003c/code\u003e \u003cem\u003e\u003ccode\u003evalue\u003c/code\u003e\u003c/em\u003e\n   部分，这等价于指定 \u003ccode\u003eTRUE\u003c/code\u003e。\n  \u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e注解\u003c/h2\u003e\u003cp\u003e\n   关于如何在订阅与发布实例之间配置访问控制，详见\u003ca href=\"/zh/docs/19/logical-replication-security.html\" rel=\"nofollow\"\u003e第 29.12 节\u003c/a\u003e。\n  \u003c/p\u003e\u003cp\u003e\n   当创建复制槽时（这是默认行为），\u003ccode\u003eCREATE\n   SUBSCRIPTION\u003c/code\u003e 不能在事务块内部执行。\n  \u003c/p\u003e\u003cp\u003e\n   创建一个连接到同一数据库集簇的订阅（例如在同一集簇中的不同数据库之间复制，或者在同一数据库内复制）时，只有在复制槽不作为同一命令的一部分创建的情况下才会成功。否则，\u003ccode\u003eCREATE SUBSCRIPTION\u003c/code\u003e\n   调用将挂起。要实现这种用法，应分别创建复制槽（使用函数\n   \u003ccode\u003epg_create_logical_replication_slot\u003c/code\u003e 并指定插件名\n   \u003ccode\u003epgoutput\u003c/code\u003e），然后使用参数\n   \u003ccode\u003ecreate_slot = false\u003c/code\u003e 创建订阅。示例见\u003ca href=\"/zh/docs/19/logical-replication-subscription.html#LOGICAL-REPLICATION-SUBSCRIPTION-EXAMPLES-DEFERRED-SLOT\" rel=\"nofollow\"\u003e第 29.2.3 节\u003c/a\u003e。这是一个实现限制，未来版本中可能会解除。\n  \u003c/p\u003e\u003cp\u003e\n   如果发布中的任何表带有 \u003ccode\u003eWHERE\u003c/code\u003e 子句，则对\n   \u003cem\u003e\u003ccode\u003eexpression\u003c/code\u003e\u003c/em\u003e 求值为\n   \u003ccode\u003efalse\u003c/code\u003e 或 \u003ccode\u003eNULL\u003c/code\u003e 的行不会被发布。如果订阅包含多个发布，且同一张表在这些发布中使用了不同的\n   \u003ccode\u003eWHERE\u003c/code\u003e 子句，那么只要任意一个表达式（针对相应的发布操作）满足，该行就会被发布。对于不同的\n   \u003ccode\u003eWHERE\u003c/code\u003e 子句，如果其中某个发布没有\n   \u003ccode\u003eWHERE\u003c/code\u003e 子句（针对相应的发布操作），或者该发布被声明为\n   \u003ca href=\"/zh/docs/19/sql-createpublication.html#SQL-CREATEPUBLICATION-PARAMS-FOR-ALL-TABLES\" rel=\"nofollow\"\u003e\u003ccode\u003eFOR ALL TABLES\u003c/code\u003e\u003c/a\u003e\n   或\n   \u003ca href=\"/zh/docs/19/sql-createpublication.html#SQL-CREATEPUBLICATION-PARAMS-FOR-TABLES-IN-SCHEMA\" rel=\"nofollow\"\u003e\u003ccode\u003eFOR TABLES IN SCHEMA\u003c/code\u003e\u003c/a\u003e，则无论其他表达式如何定义，该行都会被发布。如果订阅者使用的是 15 之前版本的 \u003cspan\u003ePostgreSQL\u003c/span\u003e，那么在初始数据同步阶段会忽略所有行过滤。在这种情况下，用户可能需要考虑删除那些初始复制过来、但与后续过滤不兼容的数据。由于初始数据同步在复制现有表数据时不会考虑发布的\n   \u003ca href=\"/zh/docs/19/sql-createpublication.html#SQL-CREATEPUBLICATION-PARAMS-WITH-PUBLISH\" rel=\"nofollow\"\u003e\u003ccode\u003epublish\u003c/code\u003e\u003c/a\u003e\n   参数，因此有些使用 DML 时本不会复制的行，也可能在此阶段被复制。示例见\u003ca href=\"/zh/docs/19/logical-replication-subscription.html#LOGICAL-REPLICATION-SUBSCRIPTION-EXAMPLES\" rel=\"nofollow\"\u003e第 29.2.2 节\u003c/a\u003e。\n  \u003c/p\u003e\u003cp\u003e\n   如果订阅包含多个发布，而其中同一张表使用了不同的列列表进行发布，则不受支持。\n  \u003c/p\u003e\u003cp\u003e\n   允许指定不存在的发布，以便用户稍后再创建这些发布。这意味着\n   \u003ca href=\"/zh/docs/19/catalog-pg-subscription.html\" rel=\"nofollow\"\u003e\u003ccode\u003epg_subscription\u003c/code\u003e\u003c/a\u003e\n   中可能包含不存在的发布。\n  \u003c/p\u003e\u003cp\u003e\n   当使用 \u003ccode\u003ecopy_data = true\u003c/code\u003e 和\n   \u003ccode\u003eorigin = NONE\u003c/code\u003e 这一订阅参数组合时，初始同步的表数据会直接从发布者复制过来，因此无法得知这些数据的真实来源。如果发布者本身也有订阅，那么复制过来的表数据可能来自更上游。系统会检测到这种情况，并向用户发出一条 \u003ccode\u003eWARNING\u003c/code\u003e，但这条警告只表明可能存在问题；用户仍需自行进行必要检查，以确认复制过来的数据来源是否确实符合预期。\n  \u003c/p\u003e\u003cp\u003e\n   若要找出哪些表可能包含非本地来源的数据（由于在发布者上创建了其他订阅），可以尝试执行以下 SQL 查询：\n\u003c/p\u003e\u003cpre\u003e\n# substitute \u0026lt;pub-names\u0026gt; below with your publication name(s) to be queried\nSELECT DISTINCT PT.schemaname, PT.tablename\nFROM pg_publication_tables PT\n     JOIN pg_class C ON (C.relname = PT.tablename)\n     JOIN pg_namespace N ON (N.nspname = PT.schemaname),\n     pg_subscription_rel PS\nWHERE C.relnamespace = N.oid AND\n      (PS.srrelid = C.oid OR\n      C.oid IN (SELECT relid FROM pg_partition_ancestors(PS.srrelid) UNION\n                SELECT relid FROM pg_partition_tree(PS.srrelid))) AND\n      PT.pubname IN (\u0026lt;pub-names\u0026gt;);\n\u003c/pre\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e示例\u003c/h2\u003e\u003cp\u003e\n   创建一个指向远端服务器的订阅，复制发布\n   \u003ccode\u003emypublication\u003c/code\u003e 和 \u003ccode\u003einsert_only\u003c/code\u003e 中的表，并在提交时立即开始复制：\n\u003c/p\u003e\u003cpre\u003e\nCREATE SUBSCRIPTION mysub\n         CONNECTION \u0026#39;host=192.168.1.50 port=5432 user=foo dbname=foodb\u0026#39;\n        PUBLICATION mypublication, insert_only;\n\u003c/pre\u003e\u003cp\u003e\n\u003c/p\u003e\u003cp\u003e\n   创建一个指向远端服务器的订阅，复制 \u003ccode\u003einsert_only\u003c/code\u003e 发布中的表，并且要等到稍后启用时才开始复制。\n\u003c/p\u003e\u003cpre\u003e\nCREATE SUBSCRIPTION mysub\n         CONNECTION \u0026#39;host=192.168.1.50 port=5432 user=foo dbname=foodb\u0026#39;\n        PUBLICATION insert_only\n               WITH (enabled = false);\n\u003c/pre\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e兼容性\u003c/h2\u003e\u003cp\u003e\n\u003ccode\u003eCREATE SUBSCRIPTION\u003c/code\u003e 是\n   \u003cspan\u003ePostgreSQL\u003c/span\u003e 扩展。\n  \u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e另见\u003c/h2\u003e\u003cspan\u003e\u003ca href=\"/zh/docs/19/sql-altersubscription.html\" title=\"ALTER SUBSCRIPTION\" rel=\"nofollow\"\u003e\u003cspan\u003eALTER SUBSCRIPTION\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/zh/docs/19/sql-dropsubscription.html\" title=\"DROP SUBSCRIPTION\" rel=\"nofollow\"\u003e\u003cspan\u003eDROP SUBSCRIPTION\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/zh/docs/19/sql-createpublication.html\" title=\"CREATE PUBLICATION\" rel=\"nofollow\"\u003e\u003cspan\u003eCREATE PUBLICATION\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/zh/docs/19/sql-alterpublication.html\" title=\"ALTER PUBLICATION\" rel=\"nofollow\"\u003e\u003cspan\u003eALTER PUBLICATION\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e\u003c/section\u003e","SourceRevision":"wiki-repair-20261023:4f5bafbde7419f7aa2c920b4344cc6292443799218090c5ab0cda5e55456064c","ContentHash":"4d579082d6aa356cdb6eff1ecc4d434e5952c1cdc53629b9a0f7192dbda0aee4","Payload":{"description_zh":"定义一个新的订阅","description_zh_html":"\u003cpre\u003eCREATE SUBSCRIPTION subscription_name\n{ SERVER server_name | CONNECTION \u0026#x27;conninfo\u0026#x27; }\nPUBLICATION publication_name [, ...]\n[ WITH ( subscription_parameter [= value] [, ... ] ) ]\u003c/pre\u003e\u003csection\u003e\u003ch2\u003e描述\u003c/h2\u003e\u003cp\u003e\n\u003ccode class=\"command\"\u003eCREATE SUBSCRIPTION\u003c/code\u003e 添加一个新的逻辑复制订阅。创建订阅的用户将成为该订阅的所有者。订阅名称必须与当前数据库中任何现有订阅的名称不同。\n  \u003c/p\u003e\u003cp\u003e\n   订阅表示与发布者的复制连接。因此，除了在本地系统目录中添加定义之外，该命令通常还会在发布者上创建一个复制槽。\n  \u003c/p\u003e\u003cp\u003e\n   除非订阅初始即被禁用，否则在执行该命令所在事务提交时，会启动一个逻辑复制工作进程为新订阅复制数据。\n  \u003c/p\u003e\u003cp\u003e\n   要能够创建订阅，必须具有 \u003ccode class=\"literal\"\u003epg_create_subscription\u003c/code\u003e 角色的权限，以及当前数据库上的 \u003ccode class=\"literal\"\u003eCREATE\u003c/code\u003e 权限。\n  \u003c/p\u003e\u003cp\u003e\n   关于订阅以及整个逻辑复制的更多信息，请参见\u003ca class=\"xref\" href=\"/zh/docs/19/logical-replication-subscription.html\" title=\"29.2. 订阅\"\u003e第 29.2 节\u003c/a\u003e和\u003ca class=\"xref\" href=\"/zh/docs/19/logical-replication.html\" title=\"第 29 章 逻辑复制\"\u003e第 29 章\u003c/a\u003e。\n  \u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e参数\u003c/h2\u003e\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-NAME\"\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003esubscription_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n      新订阅的名称。\n     \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-SERVER\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSERVER \u003cem class=\"replaceable\"\u003e\u003ccode\u003eserver_name\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n      供连接使用的外部服务器。该服务器的外部数据包装器必须已注册\n      \u003cem class=\"replaceable\"\u003e\u003ccode\u003econnection_function\u003c/code\u003e\u003c/em\u003e，并且该服务器上必须存在订阅所有者的用户映射。此外，订阅所有者必须在\n      \u003cem class=\"replaceable\"\u003e\u003ccode\u003eserver_name\u003c/code\u003e\u003c/em\u003e 上具有 \u003ccode class=\"literal\"\u003eUSAGE\u003c/code\u003e 权限。\n     \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-CONNECTION\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eCONNECTION '\u003cem class=\"replaceable\"\u003e\u003ccode\u003econninfo\u003c/code\u003e\u003c/em\u003e'\u003c/code\u003e\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n      定义如何连接到发布者数据库的 \u003cspan class=\"application\"\u003elibpq\u003c/span\u003e 连接字符串。详情请参见\u003ca class=\"xref\" href=\"/zh/docs/19/libpq-connect.html#LIBPQ-CONNSTRING\" title=\"32.1.1. 连接字符串\"\u003e第 32.1.1 节\u003c/a\u003e。\n     \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-PUBLICATION\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003ePUBLICATION \u003cem class=\"replaceable\"\u003e\u003ccode\u003epublication_name\u003c/code\u003e\u003c/em\u003e [, ...]\u003c/code\u003e\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n      要订阅的发布者上的发布名称。\n     \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eWITH ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003esubscription_parameter\u003c/code\u003e\u003c/em\u003e [= \u003cem class=\"replaceable\"\u003e\u003ccode\u003evalue\u003c/code\u003e\u003c/em\u003e] [, ... ] )\u003c/code\u003e\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n      该子句为订阅指定可选参数。\n     \u003c/p\u003e\u003cp\u003e\n      下列参数控制订阅创建期间的行为：\n\n      \u003c/p\u003e\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-CONNECT\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003econnect\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          指定 \u003ccode class=\"command\"\u003eCREATE SUBSCRIPTION\u003c/code\u003e 命令是否连接到发布者。默认值为 \u003ccode class=\"literal\"\u003etrue\u003c/code\u003e。将其设为\n          \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e 会强制把 \u003ccode class=\"literal\"\u003ecreate_slot\u003c/code\u003e、\u003ccode class=\"literal\"\u003eenabled\u003c/code\u003e 和 \u003ccode class=\"literal\"\u003ecopy_data\u003c/code\u003e 的值设为 \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e。（不能将 \u003ccode class=\"literal\"\u003econnect\u003c/code\u003e\n          设为 \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e 的同时，再把 \u003ccode class=\"literal\"\u003ecreate_slot\u003c/code\u003e、\u003ccode class=\"literal\"\u003eenabled\u003c/code\u003e 或 \u003ccode class=\"literal\"\u003ecopy_data\u003c/code\u003e 设为\n          \u003ccode class=\"literal\"\u003etrue\u003c/code\u003e。）\n         \u003c/p\u003e\u003cp\u003e\n          由于该选项为 \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e 时不会建立连接，因此不会订阅任何表和序列。要启动复制，你必须手工创建复制槽、在需要时启用故障切换、启用订阅，并刷新订阅。示例见\u003ca class=\"xref\" href=\"/zh/docs/19/logical-replication-subscription.html#LOGICAL-REPLICATION-SUBSCRIPTION-EXAMPLES-DEFERRED-SLOT\" title=\"29.2.3. 示例：延迟创建逻辑复制槽\"\u003e第 29.2.3 节\u003c/a\u003e。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-CREATE-SLOT\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003ecreate_slot\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          指定该命令是否应在发布者上创建复制槽。默认值为\n          \u003ccode class=\"literal\"\u003etrue\u003c/code\u003e。\n         \u003c/p\u003e\u003cp\u003e\n          如果设为 \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e，则必须以其他方式自行创建发布者上的复制槽。示例见\u003ca class=\"xref\" href=\"/zh/docs/19/logical-replication-subscription.html#LOGICAL-REPLICATION-SUBSCRIPTION-EXAMPLES-DEFERRED-SLOT\" title=\"29.2.3. 示例：延迟创建逻辑复制槽\"\u003e第 29.2.3 节\u003c/a\u003e。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-ENABLED\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eenabled\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          指定订阅是应当主动进行复制，还是仅完成设置但暂不启动。默认值为 \u003ccode class=\"literal\"\u003etrue\u003c/code\u003e。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-SLOT-NAME\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eslot_name\u003c/code\u003e (\u003ccode class=\"type\"\u003estring\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          要使用的发布者上的复制槽名称。默认使用订阅名称作为槽名。该名称不能为\n          \u003ccode class=\"literal\"\u003epg_conflict_detection\u003c/code\u003e，因为它保留给冲突检测使用。\n         \u003c/p\u003e\u003cp\u003e\n          将 \u003ccode class=\"literal\"\u003eslot_name\u003c/code\u003e 设为 \u003ccode class=\"literal\"\u003eNONE\u003c/code\u003e 表示该订阅不关联任何复制槽。这类订阅还必须同时将 \u003ccode class=\"literal\"\u003eenabled\u003c/code\u003e\n          和 \u003ccode class=\"literal\"\u003ecreate_slot\u003c/code\u003e 设为 \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e。当你打算稍后手工创建复制槽时，可使用此设置。示例见\u003ca class=\"xref\" href=\"/zh/docs/19/logical-replication-subscription.html#LOGICAL-REPLICATION-SUBSCRIPTION-EXAMPLES-DEFERRED-SLOT\" title=\"29.2.3. 示例：延迟创建逻辑复制槽\"\u003e第 29.2.3 节\u003c/a\u003e。\n         \u003c/p\u003e\u003cp\u003e\n          将 \u003ccode class=\"literal\"\u003eslot_name\u003c/code\u003e 设为有效名称且\n          \u003ccode class=\"literal\"\u003ecreate_slot\u003c/code\u003e 设为 \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e 时，所指定复制槽的 \u003ccode class=\"literal\"\u003efailover\u003c/code\u003e 属性值可能与订阅中对应的\n          \u003ccode class=\"literal\"\u003efailover\u003c/code\u003e 参数不同。应始终确保该槽的\n          \u003ccode class=\"literal\"\u003efailover\u003c/code\u003e 属性与订阅中的对应参数一致，反之亦然。否则，发布者上的该槽的行为可能与这些订阅选项所表明的不同：例如，即使订阅的 \u003ccode class=\"literal\"\u003efailover\u003c/code\u003e 选项已禁用，发布者上的该槽仍可能被同步到备库；或者即使订阅的 \u003ccode class=\"literal\"\u003efailover\u003c/code\u003e 选项已启用，该槽也可能不会被同步到备库。\n         \u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003cp\u003e\n\u003c/p\u003e\u003cp\u003e\n      下列参数控制订阅创建后的复制行为：\n\n      \u003c/p\u003e\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-BINARY\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003ebinary\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          指定订阅是否请求发布者以二进制格式（而不是文本格式）发送数据。默认值为 \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e。任何初始表同步复制（见 \u003ccode class=\"literal\"\u003ecopy_data\u003c/code\u003e）也使用相同格式。二进制格式可能比文本格式更快，但在不同机器架构和\n          \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 版本之间的可移植性较差。二进制格式对数据类型非常敏感；例如，虽然在文本格式下可以正常工作，但它不允许从 \u003ccode class=\"type\"\u003esmallint\u003c/code\u003e 列复制到 \u003ccode class=\"type\"\u003einteger\u003c/code\u003e 列。即使启用了此选项，也只有具有二进制发送和接收函数的数据类型才会以二进制方式传输。注意，初始同步要求所有数据类型都具有二进制发送和接收函数，否则同步将失败（关于发送/接收函数的更多信息，请参见 \u003ca class=\"xref\" href=\"/zh/docs/19/sql-createtype.html\" title=\"CREATE TYPE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE TYPE\u003c/span\u003e\u003c/a\u003e）。此参数对序列没有影响。\n         \u003c/p\u003e\u003cp\u003e\n          在进行跨版本复制时，可能会出现这样的情况：发布者对某种数据类型有二进制发送函数，但订阅者缺少该类型的二进制接收函数。在这种情况下，数据传输会失败，因此不能使用 \u003ccode class=\"literal\"\u003ebinary\u003c/code\u003e 选项。\n         \u003c/p\u003e\u003cp\u003e\n          如果发布者使用的是 16 之前版本的\n          \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e，那么即使\n          \u003ccode class=\"literal\"\u003ebinary = true\u003c/code\u003e，任何初始表同步也都会使用文本格式。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-COPY-DATA\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003ecopy_data\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          指定复制开始时是否复制所订阅发布中的预先存在的数据。默认值为 \u003ccode class=\"literal\"\u003etrue\u003c/code\u003e。\n         \u003c/p\u003e\u003cp\u003e\n          如果这些发布包含 \u003ccode class=\"literal\"\u003eWHERE\u003c/code\u003e 子句，将会影响被复制的数据。详情请参见\u003ca class=\"xref\" href=\"/zh/docs/19/sql-createsubscription.html#SQL-CREATESUBSCRIPTION-NOTES\" title=\"注解\"\u003e注意\u003c/a\u003e。\n         \u003c/p\u003e\u003cp\u003e\n          关于 \u003ccode class=\"literal\"\u003ecopy_data = true\u003c/code\u003e 如何与\n          \u003ccode class=\"literal\"\u003eorigin\u003c/code\u003e 参数相互作用的细节，请参见\u003ca class=\"xref\" href=\"/zh/docs/19/sql-createsubscription.html#SQL-CREATESUBSCRIPTION-NOTES\" title=\"注解\"\u003e注意\u003c/a\u003e。\n         \u003c/p\u003e\u003cp\u003e\n          设置 \u003ccode class=\"literal\"\u003ecopy_data = true\u003c/code\u003e 时，可能出现关于发布者与订阅者之间序列定义差异的警告。处理这些警告的建议见\u003ca class=\"xref\" href=\"/zh/docs/19/logical-replication-sequences.html#SEQUENCE-DEFINITION-MISMATCHES\" title=\"29.7.1. 序列定义不匹配\"\u003e第 29.7.1 节\u003c/a\u003e。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-STREAMING\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003estreaming\u003c/code\u003e (\u003ccode class=\"type\"\u003eenum\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          指定是否为该订阅启用进行中事务的流式传输。默认值为\n          \u003ccode class=\"literal\"\u003eparallel\u003c/code\u003e，表示如果有可用的并行应用工作进程，接收到的更改会直接通过其中之一应用。如果没有空闲的并行应用工作进程可以处理流式事务，那么这些更改会写入临时文件，并在事务提交后再应用。注意，如果并行应用工作进程中发生错误，远端事务的完成 LSN\n          可能不会记录到服务器日志中。此参数对序列没有影响。\n         \u003c/p\u003e\u003cdiv class=\"caution\"\u003e\u003ch3 class=\"title\"\u003e小心\u003c/h3\u003e\u003cp\u003e\n           当发布者与订阅者的模式不同时，存在发生死锁的风险，尽管这种情况很少见。应用工作进程会自动重试这些事务。\n          \u003c/p\u003e\u003c/div\u003e\u003cp\u003e\n          如果设为 \u003ccode class=\"literal\"\u003eon\u003c/code\u003e，接收到的更改会写入临时文件，然后仅在事务在发布者上提交且被订阅者接收之后才应用。\n         \u003c/p\u003e\u003cp\u003e\n          如果设为 \u003ccode class=\"literal\"\u003eoff\u003c/code\u003e，所有事务都会先在发布者上完全解码，然后才整体发送给订阅者。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-SYNCHRONOUS-COMMIT\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003esynchronous_commit\u003c/code\u003e (\u003ccode class=\"type\"\u003eenum\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          该参数的值会覆盖此订阅应用工作进程中的 \u003ca class=\"xref\" href=\"/zh/docs/19/runtime-config-wal.html#GUC-SYNCHRONOUS-COMMIT\"\u003esynchronous_commit\u003c/a\u003e 设置。默认值为\n          \u003ccode class=\"literal\"\u003eoff\u003c/code\u003e。此参数对序列没有影响。\n         \u003c/p\u003e\u003cp\u003e\n          对逻辑复制来说，使用 \u003ccode class=\"literal\"\u003eoff\u003c/code\u003e 是安全的：如果订阅者因缺少同步而丢失了事务，数据会再次从发布者发送过来。\n         \u003c/p\u003e\u003cp\u003e\n          在进行同步逻辑复制时，可能更适合使用不同的设置。逻辑复制工作进程会向发布者报告写入和刷盘位置，而在使用同步复制时，发布者会等待真正的刷盘完成。这意味着，当订阅被用于同步复制时，将订阅者的\n          \u003ccode class=\"literal\"\u003esynchronous_commit\u003c/code\u003e 设为 \u003ccode class=\"literal\"\u003eoff\u003c/code\u003e\n          可能会增加发布者上 \u003ccode class=\"command\"\u003eCOMMIT\u003c/code\u003e 的延迟。在这种场景下，将 \u003ccode class=\"literal\"\u003esynchronous_commit\u003c/code\u003e 设为 \u003ccode class=\"literal\"\u003elocal\u003c/code\u003e\n          或更高可能更有利。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-TWO-PHASE\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003etwo_phase\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          指定是否为该订阅启用两阶段提交。默认值为\n          \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e。此参数对序列没有影响。\n         \u003c/p\u003e\u003cp\u003e\n          启用两阶段提交时，预备事务会在\n          \u003ccode class=\"command\"\u003ePREPARE TRANSACTION\u003c/code\u003e 时发送给订阅者，并且在订阅者上也会作为两阶段事务处理。否则，预备事务只有在提交时才会发送给订阅者，随后由订阅者立即处理。\n         \u003c/p\u003e\u003cp\u003e\n          两阶段提交的实现要求复制已经成功完成初始表同步阶段。因此，即使订阅启用了 \u003ccode class=\"literal\"\u003etwo_phase\u003c/code\u003e，内部的两阶段状态也会暂时保持为\u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003epending\u003c/span\u003e”\u003c/span\u003e，直到初始化阶段完成。要了解实际的两阶段状态，请参见\n          \u003ca class=\"link\" href=\"/zh/docs/19/catalog-pg-subscription.html\" title=\"52.54. pg_subscription\"\u003e\u003ccode class=\"structname\"\u003epg_subscription\u003c/code\u003e\u003c/a\u003e\n          的 \u003ccode class=\"structfield\"\u003esubtwophasestate\u003c/code\u003e 列。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-DISABLE-ON-ERROR\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003edisable_on_error\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          指定如果订阅工作进程在从发布者复制数据期间检测到任何错误，是否自动禁用该订阅。默认值为 \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-PASSWORD-REQUIRED\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003epassword_required\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          如果设为 \u003ccode class=\"literal\"\u003etrue\u003c/code\u003e，则由于该订阅而建立的到发布者的连接必须使用密码认证，并且密码必须作为连接字符串的一部分指定。如果该订阅由超级用户拥有，则此设置会被忽略。默认值为 \u003ccode class=\"literal\"\u003etrue\u003c/code\u003e。只有超级用户才能将该值设为 \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-RUN-AS-OWNER\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003erun_as_owner\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          如果为 \u003ccode class=\"literal\"\u003etrue\u003c/code\u003e，所有复制操作都以订阅所有者的身份执行。如果为 \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e，复制工作进程会在每张表或序列上以该关系所有者的身份执行操作。后一种配置通常安全得多；详情请参见\u003ca class=\"xref\" href=\"/zh/docs/19/logical-replication-security.html\" title=\"29.12. 安全性\"\u003e第 29.12 节\u003c/a\u003e。默认值为 \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-ORIGIN\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eorigin\u003c/code\u003e (\u003ccode class=\"type\"\u003estring\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          指定订阅是请求发布者仅发送没有复制源的更改，还是无论复制源如何都发送更改。将 \u003ccode class=\"literal\"\u003eorigin\u003c/code\u003e 设为 \u003ccode class=\"literal\"\u003enone\u003c/code\u003e 表示订阅会请求发布者仅发送没有复制源的更改。将 \u003ccode class=\"literal\"\u003eorigin\u003c/code\u003e 设为\n          \u003ccode class=\"literal\"\u003eany\u003c/code\u003e 表示发布者无论复制源如何都发送更改。默认值为\n          \u003ccode class=\"literal\"\u003eany\u003c/code\u003e。此参数对序列没有影响。\n         \u003c/p\u003e\u003cp\u003e\n          关于 \u003ccode class=\"literal\"\u003ecopy_data = true\u003c/code\u003e 如何与\n          \u003ccode class=\"literal\"\u003eorigin\u003c/code\u003e 参数相互作用的细节，请参见\u003ca class=\"xref\" href=\"/zh/docs/19/sql-createsubscription.html#SQL-CREATESUBSCRIPTION-NOTES\" title=\"注解\"\u003e注意\u003c/a\u003e。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-FAILOVER\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003efailover\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          指定与该订阅关联的复制槽是否启用同步到备库，以便在故障切换后能够从新的主库继续逻辑复制。默认值为 \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-RETAIN-DEAD-TUPLES\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eretain_dead_tuples\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          指定是否保留订阅者上冲突检测所需的信息（例如死元组、提交时间戳和来源）。默认值为 \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e。如果设为\n          \u003ccode class=\"literal\"\u003etrue\u003c/code\u003e，则会启用 \u003ca class=\"xref\" href=\"/zh/docs/19/logical-replication-conflicts.html#CONFLICT-UPDATE-DELETED\"\u003eupdate_deleted\u003c/a\u003e 的检测，并在订阅者上创建一个名为\u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003e\u003ccode class=\"literal\"\u003epg_conflict_detection\u003c/code\u003e\u003c/span\u003e”\u003c/span\u003e的物理复制槽，以防止用于检测冲突的信息被移除。此参数对序列没有影响。\n         \u003c/p\u003e\u003cp\u003e\n          请注意，只有在创建该槽之后，才会保留用于冲突检测的信息。可以通过查询\n          \u003ca class=\"link\" href=\"/zh/docs/19/view-pg-replication-slots.html\" title=\"53.22. pg_replication_slots\"\u003epg_replication_slots\u003c/a\u003e 来验证该槽是否存在。即使同一节点上的多个订阅启用此选项，也只会创建一个复制槽。此外，必须将 \u003ccode class=\"varname\"\u003ewal_level\u003c/code\u003e 设置为 \u003ccode class=\"literal\"\u003ereplica\u003c/code\u003e\n          或更高，才能允许使用该复制槽。\n         \u003c/p\u003e\u003cdiv class=\"caution\"\u003e\u003ch3 class=\"title\"\u003e小心\u003c/h3\u003e\u003cp\u003e\n           请注意，如果订阅被禁用，用于冲突检测的信息将无法被清除；因此，这些信息会一直累积，直到订阅被启用。为了防止过度累积，建议在订阅将长时间处于非活动状态时禁用 \u003ccode class=\"literal\"\u003eretain_dead_tuples\u003c/code\u003e。如果订阅已启用，但其应用工作进程没有运行（例如工作进程反复无法应用某项更改），情况也相同。可以通过 \u003ca class=\"link\" href=\"/zh/docs/19/view-pg-replication-slots.html\" title=\"53.22. pg_replication_slots\"\u003epg_replication_slots\u003c/a\u003e\n           中\u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003e\u003ccode class=\"literal\"\u003epg_conflict_detection\u003c/code\u003e\u003c/span\u003e”\u003c/span\u003e复制槽的\n           \u003ccode class=\"structfield\"\u003exmin\u003c/code\u003e 查看所保留的最旧信息。\n          \u003c/p\u003e\u003cp\u003e\n           此外，在逻辑复制中为冲突检测启用 \u003ccode class=\"literal\"\u003eretain_dead_tuples\u003c/code\u003e 时，设计复制拓扑以平衡数据保留需求与整体系统性能非常重要。该选项在适当使用时只会带来极小的性能开销。以下场景展示了启用该选项时的有效用法模式。\n          \u003c/p\u003e\u003cp\u003e\n           a. 双向写入的大表：对于在发布者和订阅者节点上都存在并发写入的大表，发布者可以在创建发布时定义行过滤器来分割数据。这样可以让多个订阅并行复制该表的互斥子集，从而优化吞吐量。\n          \u003c/p\u003e\u003cp\u003e\n           b. 可写入的订阅者：如果预期订阅者节点需要执行写操作，则可以通过多个发布和订阅来组织复制。通过将表分配到这些发布中，工作负载会分散到多个应用工作进程之间，从而提高并发性并减少争用。\n          \u003c/p\u003e\u003cp\u003e\n           c. 只读订阅者：在涉及一个或多个发布者节点执行并发写操作的配置中，只读订阅者节点如果执行的是索引扫描，则可能不会看到性能影响。然而，如果订阅者由于复制延迟或扫描性能（例如顺序扫描）而受到影响，则需要采用前述两种策略之一来在订阅者上分散负载。\n          \u003c/p\u003e\u003c/div\u003e\u003cp\u003e\n          如果发布者是物理备库，则不能启用此选项。\n         \u003c/p\u003e\u003cp\u003e\n          启用此选项只能保证保留用于冲突检测的信息，且仅针对在发布者本地发生的更改。对于来自不同来源的更改，无法保证可靠的冲突检测。\n         \u003c/p\u003e\u003cp\u003e\n          检测 \u003ca class=\"xref\" href=\"/zh/docs/19/logical-replication-conflicts.html#CONFLICT-UPDATE-DELETED\"\u003eupdate_deleted\u003c/a\u003e 还要求在订阅者上启用\n          \u003ca class=\"link\" href=\"/zh/docs/19/runtime-config-replication.html#GUC-TRACK-COMMIT-TIMESTAMP\"\u003e\u003ccode class=\"varname\"\u003etrack_commit_timestamp\u003c/code\u003e\u003c/a\u003e。如果它未启用，而启用了 \u003ccode class=\"literal\"\u003eretain_dead_tuples\u003c/code\u003e，则会发出警告。如果之后禁用了 \u003ccode class=\"varname\"\u003etrack_commit_timestamp\u003c/code\u003e，用于冲突检测的信息仍会继续保留，但冲突会改为报告为 \u003ca class=\"xref\" href=\"/zh/docs/19/logical-replication-conflicts.html#CONFLICT-UPDATE-MISSING\"\u003eupdate_missing\u003c/a\u003e，因此保留这些信息便失去了作用。\n         \u003c/p\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-MAX-RETENTION-DURATION\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003emax_retention_duration\u003c/code\u003e (\u003ccode class=\"type\"\u003einteger\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          当启用 \u003ccode class=\"literal\"\u003eretain_dead_tuples\u003c/code\u003e 时，此订阅的应用工作进程允许保留用于冲突检测的信息的最长时长，单位为毫秒。默认值为\n          \u003ccode class=\"literal\"\u003e0\u003c/code\u003e，表示在不再需要进行检测之前一直保留这些信息。\n         \u003c/p\u003e\u003cp\u003e\n          如果所有与启用了 \u003ccode class=\"literal\"\u003eretain_dead_tuples\u003c/code\u003e 的订阅相关联的应用工作进程都确认保留时长已经超过相应订阅中设置的\n          \u003ccode class=\"literal\"\u003emax_retention_duration\u003c/code\u003e，则用于冲突检测的信息将不再被保留。只要至少有一个应用工作进程确认保留时长仍在指定限制内，或者创建了一个新的 \u003ccode class=\"literal\"\u003eretain_dead_tuples = true\u003c/code\u003e 订阅，保留就会自动恢复。或者，也可以通过重新启用\n          \u003ccode class=\"literal\"\u003eretain_dead_tuples\u003c/code\u003e 手工恢复保留。\n         \u003c/p\u003e\u003cp\u003e\n          注意，如果其他订阅为此参数设置了大于 0 的值且尚未超过该值，或者将此选项设为 \u003ccode class=\"literal\"\u003e0\u003c/code\u003e，则整体保留不会停止。\n         \u003c/p\u003e\u003cp\u003e\n          只有当启用了 \u003ccode class=\"literal\"\u003eretain_dead_tuples\u003c/code\u003e 且与该订阅关联的应用工作进程处于活动状态时，此选项才有效。\n         \u003c/p\u003e\u003cdiv class=\"caution\"\u003e\u003ch3 class=\"title\"\u003e小心\u003c/h3\u003e\u003cp\u003e\n           注意，订阅被禁用或其应用工作进程未运行时，不会评估保留时长。因此，无论此设置如何，用于冲突检测的信息都会继续累积，直到订阅被启用或其应用工作进程恢复运行。为防止过度累积，如果订阅将长时间处于非活动状态，应考虑禁用\n           \u003ccode class=\"literal\"\u003eretain_dead_tuples\u003c/code\u003e。\n          \u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"warning\"\u003e\u003ch3 class=\"title\"\u003e警告\u003c/h3\u003e\u003cp\u003e\n           注意，将此选项设为非零值可能导致用于冲突检测的信息被过早移除，从而可能造成错误的冲突检测。\n          \u003c/p\u003e\u003c/div\u003e\u003c/dd\u003e\u003cdt id=\"SQL-CREATESUBSCRIPTION-PARAMS-WITH-WAL-RECEIVER-TIMEOUT\"\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003ewal_receiver_timeout\u003c/code\u003e (\u003ccode class=\"type\"\u003etext\u003c/code\u003e)\u003c/span\u003e \u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n          该参数的值会覆盖此订阅应用工作进程中的 \u003ca class=\"xref\" href=\"/zh/docs/19/runtime-config-replication.html#GUC-WAL-RECEIVER-TIMEOUT\"\u003ewal_receiver_timeout\u003c/a\u003e 设置。默认值为\n          \u003ccode class=\"literal\"\u003e-1\u003c/code\u003e，表示不覆盖全局设置，也就是说将改用服务器配置、命令行、角色或数据库设置中的值。\n         \u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003cp\u003e\n   当指定 \u003ccode class=\"type\"\u003eboolean\u003c/code\u003e 类型的参数时，可以省略\n   \u003ccode class=\"literal\"\u003e=\u003c/code\u003e \u003cem class=\"replaceable\"\u003e\u003ccode\u003evalue\u003c/code\u003e\u003c/em\u003e\n   部分，这等价于指定 \u003ccode class=\"literal\"\u003eTRUE\u003c/code\u003e。\n  \u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e注解\u003c/h2\u003e\u003cp\u003e\n   关于如何在订阅与发布实例之间配置访问控制，详见\u003ca class=\"xref\" href=\"/zh/docs/19/logical-replication-security.html\" title=\"29.12. 安全性\"\u003e第 29.12 节\u003c/a\u003e。\n  \u003c/p\u003e\u003cp\u003e\n   当创建复制槽时（这是默认行为），\u003ccode class=\"command\"\u003eCREATE\n   SUBSCRIPTION\u003c/code\u003e 不能在事务块内部执行。\n  \u003c/p\u003e\u003cp\u003e\n   创建一个连接到同一数据库集簇的订阅（例如在同一集簇中的不同数据库之间复制，或者在同一数据库内复制）时，只有在复制槽不作为同一命令的一部分创建的情况下才会成功。否则，\u003ccode class=\"command\"\u003eCREATE SUBSCRIPTION\u003c/code\u003e\n   调用将挂起。要实现这种用法，应分别创建复制槽（使用函数\n   \u003ccode class=\"function\"\u003epg_create_logical_replication_slot\u003c/code\u003e 并指定插件名\n   \u003ccode class=\"literal\"\u003epgoutput\u003c/code\u003e），然后使用参数\n   \u003ccode class=\"literal\"\u003ecreate_slot = false\u003c/code\u003e 创建订阅。示例见\u003ca class=\"xref\" href=\"/zh/docs/19/logical-replication-subscription.html#LOGICAL-REPLICATION-SUBSCRIPTION-EXAMPLES-DEFERRED-SLOT\" title=\"29.2.3. 示例：延迟创建逻辑复制槽\"\u003e第 29.2.3 节\u003c/a\u003e。这是一个实现限制，未来版本中可能会解除。\n  \u003c/p\u003e\u003cp\u003e\n   如果发布中的任何表带有 \u003ccode class=\"literal\"\u003eWHERE\u003c/code\u003e 子句，则对\n   \u003cem class=\"replaceable\"\u003e\u003ccode\u003eexpression\u003c/code\u003e\u003c/em\u003e 求值为\n   \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e 或 \u003ccode class=\"literal\"\u003eNULL\u003c/code\u003e 的行不会被发布。如果订阅包含多个发布，且同一张表在这些发布中使用了不同的\n   \u003ccode class=\"literal\"\u003eWHERE\u003c/code\u003e 子句，那么只要任意一个表达式（针对相应的发布操作）满足，该行就会被发布。对于不同的\n   \u003ccode class=\"literal\"\u003eWHERE\u003c/code\u003e 子句，如果其中某个发布没有\n   \u003ccode class=\"literal\"\u003eWHERE\u003c/code\u003e 子句（针对相应的发布操作），或者该发布被声明为\n   \u003ca class=\"link\" href=\"/zh/docs/19/sql-createpublication.html#SQL-CREATEPUBLICATION-PARAMS-FOR-ALL-TABLES\"\u003e\u003ccode class=\"literal\"\u003eFOR ALL TABLES\u003c/code\u003e\u003c/a\u003e\n   或\n   \u003ca class=\"link\" href=\"/zh/docs/19/sql-createpublication.html#SQL-CREATEPUBLICATION-PARAMS-FOR-TABLES-IN-SCHEMA\"\u003e\u003ccode class=\"literal\"\u003eFOR TABLES IN SCHEMA\u003c/code\u003e\u003c/a\u003e，则无论其他表达式如何定义，该行都会被发布。如果订阅者使用的是 15 之前版本的 \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e，那么在初始数据同步阶段会忽略所有行过滤。在这种情况下，用户可能需要考虑删除那些初始复制过来、但与后续过滤不兼容的数据。由于初始数据同步在复制现有表数据时不会考虑发布的\n   \u003ca class=\"link\" href=\"/zh/docs/19/sql-createpublication.html#SQL-CREATEPUBLICATION-PARAMS-WITH-PUBLISH\"\u003e\u003ccode class=\"literal\"\u003epublish\u003c/code\u003e\u003c/a\u003e\n   参数，因此有些使用 DML 时本不会复制的行，也可能在此阶段被复制。示例见\u003ca class=\"xref\" href=\"/zh/docs/19/logical-replication-subscription.html#LOGICAL-REPLICATION-SUBSCRIPTION-EXAMPLES\" title=\"29.2.2. 示例：建立逻辑复制\"\u003e第 29.2.2 节\u003c/a\u003e。\n  \u003c/p\u003e\u003cp\u003e\n   如果订阅包含多个发布，而其中同一张表使用了不同的列列表进行发布，则不受支持。\n  \u003c/p\u003e\u003cp\u003e\n   允许指定不存在的发布，以便用户稍后再创建这些发布。这意味着\n   \u003ca class=\"link\" href=\"/zh/docs/19/catalog-pg-subscription.html\" title=\"52.54. pg_subscription\"\u003e\u003ccode class=\"structname\"\u003epg_subscription\u003c/code\u003e\u003c/a\u003e\n   中可能包含不存在的发布。\n  \u003c/p\u003e\u003cp\u003e\n   当使用 \u003ccode class=\"literal\"\u003ecopy_data = true\u003c/code\u003e 和\n   \u003ccode class=\"literal\"\u003eorigin = NONE\u003c/code\u003e 这一订阅参数组合时，初始同步的表数据会直接从发布者复制过来，因此无法得知这些数据的真实来源。如果发布者本身也有订阅，那么复制过来的表数据可能来自更上游。系统会检测到这种情况，并向用户发出一条 \u003ccode class=\"literal\"\u003eWARNING\u003c/code\u003e，但这条警告只表明可能存在问题；用户仍需自行进行必要检查，以确认复制过来的数据来源是否确实符合预期。\n  \u003c/p\u003e\u003cp\u003e\n   若要找出哪些表可能包含非本地来源的数据（由于在发布者上创建了其他订阅），可以尝试执行以下 SQL 查询：\n\u003c/p\u003e\u003cpre class=\"programlisting\"\u003e\n# substitute \u0026lt;pub-names\u0026gt; below with your publication name(s) to be queried\nSELECT DISTINCT PT.schemaname, PT.tablename\nFROM pg_publication_tables PT\n     JOIN pg_class C ON (C.relname = PT.tablename)\n     JOIN pg_namespace N ON (N.nspname = PT.schemaname),\n     pg_subscription_rel PS\nWHERE C.relnamespace = N.oid AND\n      (PS.srrelid = C.oid OR\n      C.oid IN (SELECT relid FROM pg_partition_ancestors(PS.srrelid) UNION\n                SELECT relid FROM pg_partition_tree(PS.srrelid))) AND\n      PT.pubname IN (\u0026lt;pub-names\u0026gt;);\n\u003c/pre\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e示例\u003c/h2\u003e\u003cp\u003e\n   创建一个指向远端服务器的订阅，复制发布\n   \u003ccode class=\"literal\"\u003emypublication\u003c/code\u003e 和 \u003ccode class=\"literal\"\u003einsert_only\u003c/code\u003e 中的表，并在提交时立即开始复制：\n\u003c/p\u003e\u003cpre class=\"programlisting\"\u003e\nCREATE SUBSCRIPTION mysub\n         CONNECTION 'host=192.168.1.50 port=5432 user=foo dbname=foodb'\n        PUBLICATION mypublication, insert_only;\n\u003c/pre\u003e\u003cp\u003e\n\u003c/p\u003e\u003cp\u003e\n   创建一个指向远端服务器的订阅，复制 \u003ccode class=\"literal\"\u003einsert_only\u003c/code\u003e 发布中的表，并且要等到稍后启用时才开始复制。\n\u003c/p\u003e\u003cpre class=\"programlisting\"\u003e\nCREATE SUBSCRIPTION mysub\n         CONNECTION 'host=192.168.1.50 port=5432 user=foo dbname=foodb'\n        PUBLICATION insert_only\n               WITH (enabled = false);\n\u003c/pre\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e兼容性\u003c/h2\u003e\u003cp\u003e\n\u003ccode class=\"command\"\u003eCREATE SUBSCRIPTION\u003c/code\u003e 是\n   \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 扩展。\n  \u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e另见\u003c/h2\u003e\u003cspan class=\"simplelist\"\u003e\u003ca class=\"xref\" href=\"/zh/docs/19/sql-altersubscription.html\" title=\"ALTER SUBSCRIPTION\"\u003e\u003cspan class=\"refentrytitle\"\u003eALTER SUBSCRIPTION\u003c/span\u003e\u003c/a\u003e, \u003ca class=\"xref\" href=\"/zh/docs/19/sql-dropsubscription.html\" title=\"DROP SUBSCRIPTION\"\u003e\u003cspan class=\"refentrytitle\"\u003eDROP SUBSCRIPTION\u003c/span\u003e\u003c/a\u003e, \u003ca class=\"xref\" href=\"/zh/docs/19/sql-createpublication.html\" title=\"CREATE PUBLICATION\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE PUBLICATION\u003c/span\u003e\u003c/a\u003e, \u003ca class=\"xref\" href=\"/zh/docs/19/sql-alterpublication.html\" title=\"ALTER PUBLICATION\"\u003e\u003cspan class=\"refentrytitle\"\u003eALTER PUBLICATION\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e\u003c/section\u003e","purpose_zh":"定义一个新的订阅","translation_provenance":{"inputs":[{"kind":"current-pgdoc-translation","path":"/Users/vonng/pgsty/pgdoc/zh/19/html/sql-createsubscription.html","sha256":"340dfc203db7c36e5ef0e3b48d20befa38fd2dcccf0633de8a91264ad7cd38a5"}],"kind":"same-version-pgdoc","locale":"zh-Hans","source_revision":"b1b500221dbe5437cfca004d6b04d3f6e422765d","version":"19"},"zh_from":"same-version-pgdoc"}},"RequestedLocale":"zh-Hans","Fallback":false,"Versions":["10","11","12","13","14","15","16","17","18","19","20"],"Locales":["en","zh-Hans"],"Signatures":null,"Spellings":null,"SQLState":null,"Evidence":null}
