gooderp18绿色标准版
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

166 líneas
15KB

  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>pg_rewind</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="app-pgresetwal.html" title="pg_resetwal" /><link rel="next" href="pgtestfsync.html" title="pg_test_fsync" /></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"><span xmlns="http://www.w3.org/1999/xhtml" class="application">pg_rewind</span></th></tr><tr><td width="10%" align="left"><a accesskey="p" href="app-pgresetwal.html" title="pg_resetwal">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="reference-server.html" title="PostgreSQL Server Applications">Up</a></td><th width="60%" align="center">PostgreSQL Server Applications</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="pgtestfsync.html" title="pg_test_fsync">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="APP-PGREWIND"><div class="titlepage"></div><a id="id-1.9.5.9.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle"><span class="application">pg_rewind</span></span></h2><p>pg_rewind — synchronize a <span class="productname">PostgreSQL</span> data directory with another data directory that was forked from it</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><div class="cmdsynopsis"><p id="id-1.9.5.9.4.1"><code class="command">pg_rewind</code> [<em class="replaceable"><code>option</code></em>...] { <code class="option">-D </code> | <code class="option">--target-pgdata</code> }<em class="replaceable"><code> directory</code></em> { <code class="option">--source-pgdata=<em class="replaceable"><code>directory</code></em></code> | <code class="option">--source-server=<em class="replaceable"><code>connstr</code></em></code> } </p></div></div><div class="refsect1" id="id-1.9.5.9.5"><h2>Description</h2><p>
  3. <span class="application">pg_rewind</span> is a tool for synchronizing a PostgreSQL cluster
  4. with another copy of the same cluster, after the clusters' timelines have
  5. diverged. A typical scenario is to bring an old master server back online
  6. after failover as a standby that follows the new master.
  7. </p><p>
  8. The result is equivalent to replacing the target data directory with the
  9. source one. Only changed blocks from relation files are copied;
  10. all other files are copied in full, including configuration files. The
  11. advantage of <span class="application">pg_rewind</span> over taking a new base backup, or
  12. tools like <span class="application">rsync</span>, is that <span class="application">pg_rewind</span> does
  13. not require reading through unchanged blocks in the cluster. This makes
  14. it a lot faster when the database is large and only a small
  15. fraction of blocks differ between the clusters.
  16. </p><p>
  17. <span class="application">pg_rewind</span> examines the timeline histories of the source
  18. and target clusters to determine the point where they diverged, and
  19. expects to find WAL in the target cluster's <code class="filename">pg_wal</code> directory
  20. reaching all the way back to the point of divergence. The point of divergence
  21. can be found either on the target timeline, the source timeline, or their common
  22. ancestor. In the typical failover scenario where the target cluster was
  23. shut down soon after the divergence, this is not a problem, but if the
  24. target cluster ran for a long time after the divergence, the old WAL
  25. files might no longer be present. In that case, they can be manually
  26. copied from the WAL archive to the <code class="filename">pg_wal</code> directory, or
  27. fetched on startup by configuring <a class="xref" href="runtime-config-replication.html#GUC-PRIMARY-CONNINFO">primary_conninfo</a> or
  28. <a class="xref" href="runtime-config-wal.html#GUC-RESTORE-COMMAND">restore_command</a>. The use of
  29. <span class="application">pg_rewind</span> is not limited to failover, e.g. a standby
  30. server can be promoted, run some write transactions, and then rewinded
  31. to become a standby again.
  32. </p><p>
  33. When the target server is started for the first time after running
  34. <span class="application">pg_rewind</span>, it will go into recovery mode and replay all
  35. WAL generated in the source server after the point of divergence.
  36. If some of the WAL was no longer available in the source server when
  37. <span class="application">pg_rewind</span> was run, and therefore could not be copied by the
  38. <span class="application">pg_rewind</span> session, it must be made available when the
  39. target server is started. This can be done by creating a
  40. <code class="filename">recovery.signal</code> file in the target data directory
  41. and configuring suitable <a class="xref" href="runtime-config-wal.html#GUC-RESTORE-COMMAND">restore_command</a>
  42. in <code class="filename">postgresql.conf</code>.
  43. </p><p>
  44. <span class="application">pg_rewind</span> requires that the target server either has
  45. the <a class="xref" href="runtime-config-wal.html#GUC-WAL-LOG-HINTS">wal_log_hints</a> option enabled
  46. in <code class="filename">postgresql.conf</code> or data checksums enabled when
  47. the cluster was initialized with <span class="application">initdb</span>. Neither of these
  48. are currently on by default. <a class="xref" href="runtime-config-wal.html#GUC-FULL-PAGE-WRITES">full_page_writes</a>
  49. must also be set to <code class="literal">on</code>, but is enabled by default.
  50. </p><div class="warning"><h3 class="title">Warning</h3><p>
  51. If <span class="application">pg_rewind</span> fails while processing, then
  52. the data folder of the target is likely not in a state that can be
  53. recovered. In such a case, taking a new fresh backup is recommended.
  54. </p><p>
  55. <span class="application">pg_rewind</span> will fail immediately if it finds
  56. files it cannot write directly to. This can happen for example when
  57. the source and the target server use the same file mapping for read-only
  58. SSL keys and certificates. If such files are present on the target server
  59. it is recommended to remove them before running
  60. <span class="application">pg_rewind</span>. After doing the rewind, some of
  61. those files may have been copied from the source, in which case it may
  62. be necessary to remove the data copied and restore back the set of links
  63. used before the rewind.
  64. </p></div></div><div class="refsect1" id="id-1.9.5.9.6"><h2>Options</h2><p>
  65. <span class="application">pg_rewind</span> accepts the following command-line
  66. arguments:
  67. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="option">-D <em class="replaceable"><code>directory</code></em></code><br /></span><span class="term"><code class="option">--target-pgdata=<em class="replaceable"><code>directory</code></em></code></span></dt><dd><p>
  68. This option specifies the target data directory that is synchronized
  69. with the source. The target server must be shut down cleanly before
  70. running <span class="application">pg_rewind</span>
  71. </p></dd><dt><span class="term"><code class="option">--source-pgdata=<em class="replaceable"><code>directory</code></em></code></span></dt><dd><p>
  72. Specifies the file system path to the data directory of the source
  73. server to synchronize the target with. This option requires the
  74. source server to be cleanly shut down.
  75. </p></dd><dt><span class="term"><code class="option">--source-server=<em class="replaceable"><code>connstr</code></em></code></span></dt><dd><p>
  76. Specifies a libpq connection string to connect to the source
  77. <span class="productname">PostgreSQL</span> server to synchronize the target
  78. with. The connection must be a normal (non-replication) connection
  79. with a role having sufficient permissions to execute the functions
  80. used by <span class="application">pg_rewind</span> on the source server
  81. (see Notes section for details) or a superuser role. This option
  82. requires the source server to be running and not in recovery mode.
  83. </p></dd><dt><span class="term"><code class="option">-n</code><br /></span><span class="term"><code class="option">--dry-run</code></span></dt><dd><p>
  84. Do everything except actually modifying the target directory.
  85. </p></dd><dt><span class="term"><code class="option">-N</code><br /></span><span class="term"><code class="option">--no-sync</code></span></dt><dd><p>
  86. By default, <code class="command">pg_rewind</code> will wait for all files
  87. to be written safely to disk. This option causes
  88. <code class="command">pg_rewind</code> to return without waiting, which is
  89. faster, but means that a subsequent operating system crash can leave
  90. the synchronized data directory corrupt. Generally, this option is
  91. useful for testing but should not be used when creating a production
  92. installation.
  93. </p></dd><dt><span class="term"><code class="option">-P</code><br /></span><span class="term"><code class="option">--progress</code></span></dt><dd><p>
  94. Enables progress reporting. Turning this on will deliver an approximate
  95. progress report while copying data from the source cluster.
  96. </p></dd><dt><span class="term"><code class="option">--debug</code></span></dt><dd><p>
  97. Print verbose debugging output that is mostly useful for developers
  98. debugging <span class="application">pg_rewind</span>.
  99. </p></dd><dt><span class="term"><code class="option">-V</code><br /></span><span class="term"><code class="option">--version</code></span></dt><dd><p>Display version information, then exit.</p></dd><dt><span class="term"><code class="option">-?</code><br /></span><span class="term"><code class="option">--help</code></span></dt><dd><p>Show help, then exit.</p></dd></dl></div><p>
  100. </p></div><div class="refsect1" id="id-1.9.5.9.7"><h2>Environment</h2><p>
  101. When <code class="option">--source-server</code> option is used,
  102. <span class="application">pg_rewind</span> also uses the environment variables
  103. supported by <span class="application">libpq</span> (see <a class="xref" href="libpq-envars.html" title="33.14. Environment Variables">Section 33.14</a>).
  104. </p><p>
  105. The environment variable <code class="envar">PG_COLOR</code> specifies whether to use
  106. color in diagnostic messages. Possible values are
  107. <code class="literal">always</code>, <code class="literal">auto</code> and
  108. <code class="literal">never</code>.
  109. </p></div><div class="refsect1" id="id-1.9.5.9.8"><h2>Notes</h2><p>
  110. When executing <span class="application">pg_rewind</span> using an online
  111. cluster as source, a role having sufficient permissions to execute the
  112. functions used by <span class="application">pg_rewind</span> on the source
  113. cluster can be used instead of a superuser. Here is how to create such
  114. a role, named <code class="literal">rewind_user</code> here:
  115. </p><pre class="programlisting">
  116. CREATE USER rewind_user LOGIN;
  117. GRANT EXECUTE ON function pg_catalog.pg_ls_dir(text, boolean, boolean) TO rewind_user;
  118. GRANT EXECUTE ON function pg_catalog.pg_stat_file(text, boolean) TO rewind_user;
  119. GRANT EXECUTE ON function pg_catalog.pg_read_binary_file(text) TO rewind_user;
  120. GRANT EXECUTE ON function pg_catalog.pg_read_binary_file(text, bigint, bigint, boolean) TO rewind_user;
  121. </pre><p>
  122. </p><p>
  123. When executing <span class="application">pg_rewind</span> using an online
  124. cluster as source which has been recently promoted, it is necessary
  125. to execute a <code class="command">CHECKPOINT</code> after promotion so as its
  126. control file reflects up-to-date timeline information, which is used by
  127. <span class="application">pg_rewind</span> to check if the target cluster
  128. can be rewound using the designated source cluster.
  129. </p><div class="refsect2" id="id-1.9.5.9.8.4"><h3>How It Works</h3><p>
  130. The basic idea is to copy all file system-level changes from the source
  131. cluster to the target cluster:
  132. </p><div class="procedure"><ol class="procedure" type="1"><li class="step"><p>
  133. Scan the WAL log of the target cluster, starting from the last
  134. checkpoint before the point where the source cluster's timeline
  135. history forked off from the target cluster. For each WAL record,
  136. record each data block that was touched. This yields a list of all
  137. the data blocks that were changed in the target cluster, after the
  138. source cluster forked off.
  139. </p></li><li class="step"><p>
  140. Copy all those changed blocks from the source cluster to
  141. the target cluster, either using direct file system access
  142. (<code class="option">--source-pgdata</code>) or SQL (<code class="option">--source-server</code>).
  143. </p></li><li class="step"><p>
  144. Copy all other files such as <code class="filename">pg_xact</code> and
  145. configuration files from the source cluster to the target cluster
  146. (everything except the relation files). Similarly to base backups,
  147. the contents of the directories <code class="filename">pg_dynshmem/</code>,
  148. <code class="filename">pg_notify/</code>, <code class="filename">pg_replslot/</code>,
  149. <code class="filename">pg_serial/</code>, <code class="filename">pg_snapshots/</code>,
  150. <code class="filename">pg_stat_tmp/</code>, and
  151. <code class="filename">pg_subtrans/</code> are omitted from the data copied
  152. from the source cluster. Any file or directory beginning with
  153. <code class="filename">pgsql_tmp</code> is omitted, as well as are
  154. <code class="filename">backup_label</code>,
  155. <code class="filename">tablespace_map</code>,
  156. <code class="filename">pg_internal.init</code>,
  157. <code class="filename">postmaster.opts</code> and
  158. <code class="filename">postmaster.pid</code>.
  159. </p></li><li class="step"><p>
  160. Apply the WAL from the source cluster, starting from the checkpoint
  161. created at failover. (Strictly speaking, <span class="application">pg_rewind</span>
  162. doesn't apply the WAL, it just creates a backup label file that
  163. makes <span class="productname">PostgreSQL</span> start by replaying all WAL from
  164. that checkpoint forward.)
  165. </p></li></ol></div></div></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="app-pgresetwal.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="reference-server.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="pgtestfsync.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top"><span class="application">pg_resetwal</span> </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> <span class="application">pg_test_fsync</span></td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1