????
Current Path : C:/opt/pgsql/doc/postgresql/html/ |
Current File : C:/opt/pgsql/doc/postgresql/html/sql-createsubscription.html |
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>CREATE SUBSCRIPTION</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="sql-createstatistics.html" title="CREATE STATISTICS" /><link rel="next" href="sql-createtable.html" title="CREATE TABLE" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">CREATE SUBSCRIPTION</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-createstatistics.html" title="CREATE STATISTICS">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-createtable.html" title="CREATE TABLE">Next</a></td></tr></table><hr /></div><div class="refentry" id="SQL-CREATESUBSCRIPTION"><div class="titlepage"></div><a id="id-1.9.3.84.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">CREATE SUBSCRIPTION</span></h2><p>CREATE SUBSCRIPTION — define a new subscription</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis"> CREATE SUBSCRIPTION <em class="replaceable"><code>subscription_name</code></em> CONNECTION '<em class="replaceable"><code>conninfo</code></em>' PUBLICATION <em class="replaceable"><code>publication_name</code></em> [, ...] [ WITH ( <em class="replaceable"><code>subscription_parameter</code></em> [= <em class="replaceable"><code>value</code></em>] [, ... ] ) ] </pre></div><div class="refsect1" id="id-1.9.3.84.5"><h2>Description</h2><p> <code class="command">CREATE SUBSCRIPTION</code> 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. </p><p> A subscription represents a replication connection to the publisher. Hence, in addition to adding definitions in the local catalogs, this command normally creates a replication slot on the publisher. </p><p> A logical replication worker will be started to replicate data for the new subscription at the commit of the transaction where this command is run, unless the subscription is initially disabled. </p><p> To be able to create a subscription, you must have the privileges of the the <code class="literal">pg_create_subscription</code> role, as well as <code class="literal">CREATE</code> privileges on the current database. </p><p> Additional information about subscriptions and logical replication as a whole is available at <a class="xref" href="logical-replication-subscription.html" title="31.2. Subscription">Section 31.2</a> and <a class="xref" href="logical-replication.html" title="Chapter 31. Logical Replication">Chapter 31</a>. </p></div><div class="refsect1" id="id-1.9.3.84.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt id="SQL-CREATESUBSCRIPTION-NAME"><span class="term"><em class="replaceable"><code>subscription_name</code></em></span> <a href="#SQL-CREATESUBSCRIPTION-NAME" class="id_link">#</a></dt><dd><p> The name of the new subscription. </p></dd><dt id="SQL-CREATESUBSCRIPTION-CONNECTION"><span class="term"><code class="literal">CONNECTION '<em class="replaceable"><code>conninfo</code></em>'</code></span> <a href="#SQL-CREATESUBSCRIPTION-CONNECTION" class="id_link">#</a></dt><dd><p> The <span class="application">libpq</span> connection string defining how to connect to the publisher database. For details see <a class="xref" href="libpq-connect.html#LIBPQ-CONNSTRING" title="34.1.1. Connection Strings">Section 34.1.1</a>. </p></dd><dt id="SQL-CREATESUBSCRIPTION-PUBLICATION"><span class="term"><code class="literal">PUBLICATION <em class="replaceable"><code>publication_name</code></em> [, ...]</code></span> <a href="#SQL-CREATESUBSCRIPTION-PUBLICATION" class="id_link">#</a></dt><dd><p> Names of the publications on the publisher to subscribe to. </p></dd><dt id="SQL-CREATESUBSCRIPTION-WITH"><span class="term"><code class="literal">WITH ( <em class="replaceable"><code>subscription_parameter</code></em> [= <em class="replaceable"><code>value</code></em>] [, ... ] )</code></span> <a href="#SQL-CREATESUBSCRIPTION-WITH" class="id_link">#</a></dt><dd><p> This clause specifies optional parameters for a subscription. </p><p> The following parameters control what happens during subscription creation: </p><div class="variablelist"><dl class="variablelist"><dt id="SQL-CREATESUBSCRIPTION-WITH-CONNECT"><span class="term"><code class="literal">connect</code> (<code class="type">boolean</code>)</span> <a href="#SQL-CREATESUBSCRIPTION-WITH-CONNECT" class="id_link">#</a></dt><dd><p> Specifies whether the <code class="command">CREATE SUBSCRIPTION</code> command should connect to the publisher at all. The default is <code class="literal">true</code>. Setting this to <code class="literal">false</code> will force the values of <code class="literal">create_slot</code>, <code class="literal">enabled</code> and <code class="literal">copy_data</code> to <code class="literal">false</code>. (You cannot combine setting <code class="literal">connect</code> to <code class="literal">false</code> with setting <code class="literal">create_slot</code>, <code class="literal">enabled</code>, or <code class="literal">copy_data</code> to <code class="literal">true</code>.) </p><p> Since no connection is made when this option is <code class="literal">false</code>, no tables are subscribed. To initiate replication, you must manually create the replication slot, enable the subscription, and refresh the subscription. See <a class="xref" href="logical-replication-subscription.html#LOGICAL-REPLICATION-SUBSCRIPTION-EXAMPLES-DEFERRED-SLOT" title="31.2.3. Examples: Deferred Replication Slot Creation">Section 31.2.3</a> for examples. </p></dd><dt id="SQL-CREATESUBSCRIPTION-WITH-CREATE-SLOT"><span class="term"><code class="literal">create_slot</code> (<code class="type">boolean</code>)</span> <a href="#SQL-CREATESUBSCRIPTION-WITH-CREATE-SLOT" class="id_link">#</a></dt><dd><p> Specifies whether the command should create the replication slot on the publisher. The default is <code class="literal">true</code>. </p><p> If set to <code class="literal">false</code>, you are responsible for creating the publisher's slot in some other way. See <a class="xref" href="logical-replication-subscription.html#LOGICAL-REPLICATION-SUBSCRIPTION-EXAMPLES-DEFERRED-SLOT" title="31.2.3. Examples: Deferred Replication Slot Creation">Section 31.2.3</a> for examples. </p></dd><dt id="SQL-CREATESUBSCRIPTION-WITH-ENABLED"><span class="term"><code class="literal">enabled</code> (<code class="type">boolean</code>)</span> <a href="#SQL-CREATESUBSCRIPTION-WITH-ENABLED" class="id_link">#</a></dt><dd><p> Specifies whether the subscription should be actively replicating or whether it should just be set up but not started yet. The default is <code class="literal">true</code>. </p></dd><dt id="SQL-CREATESUBSCRIPTION-WITH-SLOT-NAME"><span class="term"><code class="literal">slot_name</code> (<code class="type">string</code>)</span> <a href="#SQL-CREATESUBSCRIPTION-WITH-SLOT-NAME" class="id_link">#</a></dt><dd><p> Name of the publisher's replication slot to use. The default is to use the name of the subscription for the slot name. </p><p> Setting <code class="literal">slot_name</code> to <code class="literal">NONE</code> means there will be no replication slot associated with the subscription. Such subscriptions must also have both <code class="literal">enabled</code> and <code class="literal">create_slot</code> set to <code class="literal">false</code>. Use this when you will be creating the replication slot later manually. See <a class="xref" href="logical-replication-subscription.html#LOGICAL-REPLICATION-SUBSCRIPTION-EXAMPLES-DEFERRED-SLOT" title="31.2.3. Examples: Deferred Replication Slot Creation">Section 31.2.3</a> for examples. </p></dd></dl></div><p> </p><p> The following parameters control the subscription's replication behavior after it has been created: </p><div class="variablelist"><dl class="variablelist"><dt id="SQL-CREATESUBSCRIPTION-WITH-BINARY"><span class="term"><code class="literal">binary</code> (<code class="type">boolean</code>)</span> <a href="#SQL-CREATESUBSCRIPTION-WITH-BINARY" class="id_link">#</a></dt><dd><p> Specifies whether the subscription will request the publisher to send the data in binary format (as opposed to text). The default is <code class="literal">false</code>. Any initial table synchronization copy (see <code class="literal">copy_data</code>) also uses the same format. Binary format can be faster than the text format, but it is less portable across machine architectures and <span class="productname">PostgreSQL</span> versions. Binary format is very data type specific; for example, it will not allow copying from a <code class="type">smallint</code> column to an <code class="type">integer</code> 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 <a class="xref" href="sql-createtype.html" title="CREATE TYPE"><span class="refentrytitle">CREATE TYPE</span></a> for more about send/receive functions). </p><p> When doing cross-version replication, it could be that the publisher has a binary send function for some data type, but the subscriber lacks a binary receive function for that type. In such a case, data transfer will fail, and the <code class="literal">binary</code> option cannot be used. </p><p> If the publisher is a <span class="productname">PostgreSQL</span> version before 16, then any initial table synchronization will use text format even if <code class="literal">binary = true</code>. </p></dd><dt id="SQL-CREATESUBSCRIPTION-WITH-COPY-DATA"><span class="term"><code class="literal">copy_data</code> (<code class="type">boolean</code>)</span> <a href="#SQL-CREATESUBSCRIPTION-WITH-COPY-DATA" class="id_link">#</a></dt><dd><p> Specifies whether to copy pre-existing data in the publications that are being subscribed to when the replication starts. The default is <code class="literal">true</code>. </p><p> If the publications contain <code class="literal">WHERE</code> clauses, it will affect what data is copied. Refer to the <a class="xref" href="sql-createsubscription.html#SQL-CREATESUBSCRIPTION-NOTES" title="Notes">Notes</a> for details. </p><p> See <a class="xref" href="sql-createsubscription.html#SQL-CREATESUBSCRIPTION-NOTES" title="Notes">Notes</a> for details of how <code class="literal">copy_data = true</code> can interact with the <code class="literal">origin</code> parameter. </p></dd><dt id="SQL-CREATESUBSCRIPTION-WITH-STREAMING"><span class="term"><code class="literal">streaming</code> (<code class="type">enum</code>)</span> <a href="#SQL-CREATESUBSCRIPTION-WITH-STREAMING" class="id_link">#</a></dt><dd><p> Specifies whether to enable streaming of in-progress transactions for this subscription. The default value is <code class="literal">off</code>, meaning all transactions are fully decoded on the publisher and only then sent to the subscriber as a whole. </p><p> If set to <code class="literal">on</code>, 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. </p><p> If set to <code class="literal">parallel</code>, 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. </p></dd><dt id="SQL-CREATESUBSCRIPTION-WITH-SYNCHRONOUS-COMMIT"><span class="term"><code class="literal">synchronous_commit</code> (<code class="type">enum</code>)</span> <a href="#SQL-CREATESUBSCRIPTION-WITH-SYNCHRONOUS-COMMIT" class="id_link">#</a></dt><dd><p> The value of this parameter overrides the <a class="xref" href="runtime-config-wal.html#GUC-SYNCHRONOUS-COMMIT">synchronous_commit</a> setting within this subscription's apply worker processes. The default value is <code class="literal">off</code>. </p><p> It is safe to use <code class="literal">off</code> for logical replication: If the subscriber loses transactions because of missing synchronization, the data will be sent again from the publisher. </p><p> A different setting might be appropriate when doing synchronous logical replication. The logical replication workers report the positions of writes and flushes to the publisher, and when using synchronous replication, the publisher will wait for the actual flush. This means that setting <code class="literal">synchronous_commit</code> for the subscriber to <code class="literal">off</code> when the subscription is used for synchronous replication might increase the latency for <code class="command">COMMIT</code> on the publisher. In this scenario, it can be advantageous to set <code class="literal">synchronous_commit</code> to <code class="literal">local</code> or higher. </p></dd><dt id="SQL-CREATESUBSCRIPTION-WITH-TWO-PHASE"><span class="term"><code class="literal">two_phase</code> (<code class="type">boolean</code>)</span> <a href="#SQL-CREATESUBSCRIPTION-WITH-TWO-PHASE" class="id_link">#</a></dt><dd><p> Specifies whether two-phase commit is enabled for this subscription. The default is <code class="literal">false</code>. </p><p> When two-phase commit is enabled, prepared transactions are sent to the subscriber at the time of <code class="command">PREPARE TRANSACTION</code>, 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. </p><p> The implementation of two-phase commit requires that replication has successfully finished the initial table synchronization phase. So even when <code class="literal">two_phase</code> is enabled for a subscription, the internal two-phase state remains temporarily <span class="quote">“<span class="quote">pending</span>”</span> until the initialization phase completes. See column <code class="structfield">subtwophasestate</code> of <a class="link" href="catalog-pg-subscription.html" title="53.54. pg_subscription"><code class="structname">pg_subscription</code></a> to know the actual two-phase state. </p></dd><dt id="SQL-CREATESUBSCRIPTION-WITH-DISABLE-ON-ERROR"><span class="term"><code class="literal">disable_on_error</code> (<code class="type">boolean</code>)</span> <a href="#SQL-CREATESUBSCRIPTION-WITH-DISABLE-ON-ERROR" class="id_link">#</a></dt><dd><p> Specifies whether the subscription should be automatically disabled if any errors are detected by subscription workers during data replication from the publisher. The default is <code class="literal">false</code>. </p></dd><dt id="SQL-CREATESUBSCRIPTION-WITH-PASSWORD-REQUIRED"><span class="term"><code class="literal">password_required</code> (<code class="type">boolean</code>)</span> <a href="#SQL-CREATESUBSCRIPTION-WITH-PASSWORD-REQUIRED" class="id_link">#</a></dt><dd><p> If set to <code class="literal">true</code>, 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 <code class="literal">true</code>. Only superusers can set this value to <code class="literal">false</code>. </p></dd><dt id="SQL-CREATESUBSCRIPTION-WITH-RUN-AS-OWNER"><span class="term"><code class="literal">run_as_owner</code> (<code class="type">boolean</code>)</span> <a href="#SQL-CREATESUBSCRIPTION-WITH-RUN-AS-OWNER" class="id_link">#</a></dt><dd><p> If true, all replication actions are performed as the subscription owner. If false, replication workers will perform actions on each table as the owner of that table. The latter configuration is generally much more secure; for details, see <a class="xref" href="logical-replication-security.html" title="31.9. Security">Section 31.9</a>. The default is <code class="literal">false</code>. </p></dd><dt id="SQL-CREATESUBSCRIPTION-WITH-ORIGIN"><span class="term"><code class="literal">origin</code> (<code class="type">string</code>)</span> <a href="#SQL-CREATESUBSCRIPTION-WITH-ORIGIN" class="id_link">#</a></dt><dd><p> Specifies whether the subscription will request the publisher to only send changes that don't have an origin or send changes regardless of origin. Setting <code class="literal">origin</code> to <code class="literal">none</code> means that the subscription will request the publisher to only send changes that don't have an origin. Setting <code class="literal">origin</code> to <code class="literal">any</code> means that the publisher sends changes regardless of their origin. The default is <code class="literal">any</code>. </p><p> See <a class="xref" href="sql-createsubscription.html#SQL-CREATESUBSCRIPTION-NOTES" title="Notes">Notes</a> for details of how <code class="literal">copy_data = true</code> can interact with the <code class="literal">origin</code> parameter. </p></dd></dl></div></dd></dl></div><p> When specifying a parameter of type <code class="type">boolean</code>, the <code class="literal">=</code> <em class="replaceable"><code>value</code></em> part can be omitted, which is equivalent to specifying <code class="literal">TRUE</code>. </p></div><div class="refsect1" id="SQL-CREATESUBSCRIPTION-NOTES"><h2>Notes</h2><p> See <a class="xref" href="logical-replication-security.html" title="31.9. Security">Section 31.9</a> for details on how to configure access control between the subscription and the publication instance. </p><p> When creating a replication slot (the default behavior), <code class="command">CREATE SUBSCRIPTION</code> cannot be executed inside a transaction block. </p><p> Creating a subscription that connects to the same database cluster (for example, to replicate between databases in the same cluster or to replicate within the same database) will only succeed if the replication slot is not created as part of the same command. Otherwise, the <code class="command">CREATE SUBSCRIPTION</code> call will hang. To make this work, create the replication slot separately (using the function <code class="function">pg_create_logical_replication_slot</code> with the plugin name <code class="literal">pgoutput</code>) and create the subscription using the parameter <code class="literal">create_slot = false</code>. See <a class="xref" href="logical-replication-subscription.html#LOGICAL-REPLICATION-SUBSCRIPTION-EXAMPLES-DEFERRED-SLOT" title="31.2.3. Examples: Deferred Replication Slot Creation">Section 31.2.3</a> for examples. This is an implementation restriction that might be lifted in a future release. </p><p> If any table in the publication has a <code class="literal">WHERE</code> clause, rows for which the <em class="replaceable"><code>expression</code></em> evaluates to false or null will not be published. If the subscription has several publications in which the same table has been published with different <code class="literal">WHERE</code> clauses, a row will be published if any of the expressions (referring to that publish operation) are satisfied. In the case of different <code class="literal">WHERE</code> clauses, if one of the publications has no <code class="literal">WHERE</code> clause (referring to that publish operation) or the publication is declared as <a class="link" href="sql-createpublication.html#SQL-CREATEPUBLICATION-FOR-ALL-TABLES"><code class="literal">FOR ALL TABLES</code></a> or <a class="link" href="sql-createpublication.html#SQL-CREATEPUBLICATION-FOR-TABLES-IN-SCHEMA"><code class="literal">FOR TABLES IN SCHEMA</code></a>, rows are always published regardless of the definition of the other expressions. If the subscriber is a <span class="productname">PostgreSQL</span> 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 <a class="link" href="sql-createpublication.html#SQL-CREATEPUBLICATION-WITH-PUBLISH"><code class="literal">publish</code></a> parameter when copying existing table data, some rows may be copied that would not be replicated using DML. See <a class="xref" href="logical-replication-subscription.html#LOGICAL-REPLICATION-SUBSCRIPTION-EXAMPLES" title="31.2.2. Examples: Set Up Logical Replication">Section 31.2.2</a> for examples. </p><p> Subscriptions having several publications in which the same table has been published with different column lists are not supported. </p><p> We allow non-existent publications to be specified so that users can add those later. This means <a class="link" href="catalog-pg-subscription.html" title="53.54. pg_subscription"><code class="structname">pg_subscription</code></a> can have non-existent publications. </p><p> When using a subscription parameter combination of <code class="literal">copy_data = true</code> and <code class="literal">origin = NONE</code>, 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. </p><p> To find which tables might potentially include non-local origins (due to other subscriptions created on the publisher) try this SQL query: </p><pre class="programlisting"> # substitute <pub-names> below with your publication name(s) to be queried SELECT DISTINCT PT.schemaname, PT.tablename FROM pg_publication_tables PT, pg_subscription_rel PS JOIN pg_class C ON (C.oid = PS.srrelid) JOIN pg_namespace N ON (N.oid = C.relnamespace) WHERE N.nspname = PT.schemaname AND C.relname = PT.tablename AND PT.pubname IN (<pub-names>); </pre></div><div class="refsect1" id="id-1.9.3.84.8"><h2>Examples</h2><p> Create a subscription to a remote server that replicates tables in the publications <code class="literal">mypublication</code> and <code class="literal">insert_only</code> and starts replicating immediately on commit: </p><pre class="programlisting"> CREATE SUBSCRIPTION mysub CONNECTION 'host=192.168.1.50 port=5432 user=foo dbname=foodb' PUBLICATION mypublication, insert_only; </pre><p> </p><p> Create a subscription to a remote server that replicates tables in the <code class="literal">insert_only</code> publication and does not start replicating until enabled at a later time. </p><pre class="programlisting"> CREATE SUBSCRIPTION mysub CONNECTION 'host=192.168.1.50 port=5432 user=foo dbname=foodb' PUBLICATION insert_only WITH (enabled = false); </pre></div><div class="refsect1" id="id-1.9.3.84.9"><h2>Compatibility</h2><p> <code class="command">CREATE SUBSCRIPTION</code> is a <span class="productname">PostgreSQL</span> extension. </p></div><div class="refsect1" id="id-1.9.3.84.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-altersubscription.html" title="ALTER SUBSCRIPTION"><span class="refentrytitle">ALTER SUBSCRIPTION</span></a>, <a class="xref" href="sql-dropsubscription.html" title="DROP SUBSCRIPTION"><span class="refentrytitle">DROP SUBSCRIPTION</span></a>, <a class="xref" href="sql-createpublication.html" title="CREATE PUBLICATION"><span class="refentrytitle">CREATE PUBLICATION</span></a>, <a class="xref" href="sql-alterpublication.html" title="ALTER PUBLICATION"><span class="refentrytitle">ALTER PUBLICATION</span></a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-createstatistics.html" title="CREATE STATISTICS">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-createtable.html" title="CREATE TABLE">Next</a></td></tr><tr><td width="40%" align="left" valign="top">CREATE STATISTICS </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> CREATE TABLE</td></tr></table></div></body></html>