gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 line
12KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!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 V1.79.1" /><link rel="prev" href="sql-createstatistics.html" title="CREATE STATISTICS" /><link rel="next" href="sql-createtable.html" title="CREATE TABLE" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" 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 12.4 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></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">
  3. CREATE SUBSCRIPTION <em class="replaceable"><code>subscription_name</code></em>
  4. CONNECTION '<em class="replaceable"><code>conninfo</code></em>'
  5. PUBLICATION <em class="replaceable"><code>publication_name</code></em> [, ...]
  6. [ WITH ( <em class="replaceable"><code>subscription_parameter</code></em> [= <em class="replaceable"><code>value</code></em>] [, ... ] ) ]
  7. </pre></div><div class="refsect1" id="id-1.9.3.84.5"><h2>Description</h2><p>
  8. <code class="command">CREATE SUBSCRIPTION</code> adds a new subscription for the
  9. current database. The subscription name must be distinct from the name of
  10. any existing subscription in the database.
  11. </p><p>
  12. The subscription represents a replication connection to the publisher. As
  13. such this command does not only add definitions in the local catalogs but
  14. also creates a replication slot on the publisher.
  15. </p><p>
  16. A logical replication worker will be started to replicate data for the new
  17. subscription at the commit of the transaction where this command is run.
  18. </p><p>
  19. Additional information about subscriptions and logical replication as a
  20. whole is available at <a class="xref" href="logical-replication-subscription.html" title="30.2. Subscription">Section 30.2</a> and
  21. <a class="xref" href="logical-replication.html" title="Chapter 30. Logical Replication">Chapter 30</a>.
  22. </p></div><div class="refsect1" id="id-1.9.3.84.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>subscription_name</code></em></span></dt><dd><p>
  23. The name of the new subscription.
  24. </p></dd><dt><span class="term"><code class="literal">CONNECTION '<em class="replaceable"><code>conninfo</code></em>'</code></span></dt><dd><p>
  25. The connection string to the publisher. For details
  26. see <a class="xref" href="libpq-connect.html#LIBPQ-CONNSTRING" title="33.1.1. Connection Strings">Section 33.1.1</a>.
  27. </p></dd><dt><span class="term"><code class="literal">PUBLICATION <em class="replaceable"><code>publication_name</code></em></code></span></dt><dd><p>
  28. Names of the publications on the publisher to subscribe to.
  29. </p></dd><dt><span class="term"><code class="literal">WITH ( <em class="replaceable"><code>subscription_parameter</code></em> [= <em class="replaceable"><code>value</code></em>] [, ... ] )</code></span></dt><dd><p>
  30. This clause specifies optional parameters for a subscription. The
  31. following parameters are supported:
  32. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">copy_data</code> (<code class="type">boolean</code>)</span></dt><dd><p>
  33. Specifies whether the existing data in the publications that are
  34. being subscribed to should be copied once the replication starts.
  35. The default is <code class="literal">true</code>.
  36. </p></dd><dt><span class="term"><code class="literal">create_slot</code> (<code class="type">boolean</code>)</span></dt><dd><p>
  37. Specifies whether the command should create the replication slot on
  38. the publisher. The default is <code class="literal">true</code>.
  39. </p></dd><dt><span class="term"><code class="literal">enabled</code> (<code class="type">boolean</code>)</span></dt><dd><p>
  40. Specifies whether the subscription should be actively replicating,
  41. or whether it should be just setup but not started yet. The default
  42. is <code class="literal">true</code>.
  43. </p></dd><dt><span class="term"><code class="literal">slot_name</code> (<code class="type">string</code>)</span></dt><dd><p>
  44. Name of the replication slot to use. The default behavior is to
  45. use the name of the subscription for the slot name.
  46. </p><p>
  47. When <code class="literal">slot_name</code> is set to
  48. <code class="literal">NONE</code>, there will be no replication slot
  49. associated with the subscription. This can be used if the
  50. replication slot will be created later manually. Such
  51. subscriptions must also have both <code class="literal">enabled</code> and
  52. <code class="literal">create_slot</code> set to <code class="literal">false</code>.
  53. </p></dd><dt><span class="term"><code class="literal">synchronous_commit</code> (<code class="type">enum</code>)</span></dt><dd><p>
  54. The value of this parameter overrides the
  55. <a class="xref" href="runtime-config-wal.html#GUC-SYNCHRONOUS-COMMIT">synchronous_commit</a> setting. The default
  56. value is <code class="literal">off</code>.
  57. </p><p>
  58. It is safe to use <code class="literal">off</code> for logical replication:
  59. If the subscriber loses transactions because of missing
  60. synchronization, the data will be resent from the publisher.
  61. </p><p>
  62. A different setting might be appropriate when doing synchronous
  63. logical replication. The logical replication workers report the
  64. positions of writes and flushes to the publisher, and when using
  65. synchronous replication, the publisher will wait for the actual
  66. flush. This means that setting
  67. <code class="literal">synchronous_commit</code> for the subscriber to
  68. <code class="literal">off</code> when the subscription is used for
  69. synchronous replication might increase the latency for
  70. <code class="command">COMMIT</code> on the publisher. In this scenario, it
  71. can be advantageous to set <code class="literal">synchronous_commit</code>
  72. to <code class="literal">local</code> or higher.
  73. </p></dd><dt><span class="term"><code class="literal">connect</code> (<code class="type">boolean</code>)</span></dt><dd><p>
  74. Specifies whether the <code class="command">CREATE SUBSCRIPTION</code>
  75. should connect to the publisher at all. Setting this to
  76. <code class="literal">false</code> will change default values of
  77. <code class="literal">enabled</code>, <code class="literal">create_slot</code> and
  78. <code class="literal">copy_data</code> to <code class="literal">false</code>.
  79. </p><p>
  80. It is not allowed to combine <code class="literal">connect</code> set to
  81. <code class="literal">false</code> and <code class="literal">enabled</code>,
  82. <code class="literal">create_slot</code>, or <code class="literal">copy_data</code>
  83. set to <code class="literal">true</code>.
  84. </p><p>
  85. Since no connection is made when this option is set
  86. to <code class="literal">false</code>, the tables are not subscribed, and so
  87. after you enable the subscription nothing will be replicated.
  88. It is required to run
  89. <code class="literal">ALTER SUBSCRIPTION ... REFRESH PUBLICATION</code> in order
  90. for tables to be subscribed.
  91. </p></dd></dl></div><p>
  92. </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.84.7"><h2>Notes</h2><p>
  93. See <a class="xref" href="logical-replication-security.html" title="30.7. Security">Section 30.7</a> for details on
  94. how to configure access control between the subscription and the
  95. publication instance.
  96. </p><p>
  97. When creating a replication slot (the default behavior), <code class="command">CREATE
  98. SUBSCRIPTION</code> cannot be executed inside a transaction block.
  99. </p><p>
  100. Creating a subscription that connects to the same database cluster (for
  101. example, to replicate between databases in the same cluster or to replicate
  102. within the same database) will only succeed if the replication slot is not
  103. created as part of the same command. Otherwise, the <code class="command">CREATE
  104. SUBSCRIPTION</code> call will hang. To make this work, create the
  105. replication slot separately (using the
  106. function <code class="function">pg_create_logical_replication_slot</code> with the
  107. plugin name <code class="literal">pgoutput</code>) and create the subscription using
  108. the parameter <code class="literal">create_slot = false</code>. This is an
  109. implementation restriction that might be lifted in a future release.
  110. </p></div><div class="refsect1" id="id-1.9.3.84.8"><h2>Examples</h2><p>
  111. Create a subscription to a remote server that replicates tables in
  112. the publications <code class="literal">mypublication</code> and
  113. <code class="literal">insert_only</code> and starts replicating immediately on
  114. commit:
  115. </p><pre class="programlisting">
  116. CREATE SUBSCRIPTION mysub
  117. CONNECTION 'host=192.168.1.50 port=5432 user=foo dbname=foodb'
  118. PUBLICATION mypublication, insert_only;
  119. </pre><p>
  120. </p><p>
  121. Create a subscription to a remote server that replicates tables in
  122. the <code class="literal">insert_only</code> publication and does not start replicating
  123. until enabled at a later time.
  124. </p><pre class="programlisting">
  125. CREATE SUBSCRIPTION mysub
  126. CONNECTION 'host=192.168.1.50 port=5432 user=foo dbname=foodb'
  127. PUBLICATION insert_only
  128. WITH (enabled = false);
  129. </pre></div><div class="refsect1" id="id-1.9.3.84.9"><h2>Compatibility</h2><p>
  130. <code class="command">CREATE SUBSCRIPTION</code> is a <span class="productname">PostgreSQL</span>
  131. extension.
  132. </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">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-createtable.html">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">Home</a></td><td width="40%" align="right" valign="top"> CREATE TABLE</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1