gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

195 行
16KB

  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_standby</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="contrib-prog-server.html" title="G.2. Server Applications" /><link rel="next" href="external-projects.html" title="Appendix H. External Projects" /></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_standby</span></th></tr><tr><td width="10%" align="left"><a accesskey="p" href="contrib-prog-server.html" title="G.2. Server Applications">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="contrib-prog-server.html" title="G.2. Server Applications">Up</a></td><th width="60%" align="center">G.2. 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="external-projects.html" title="Appendix H. External Projects">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="PGSTANDBY"><div class="titlepage"></div><a id="id-1.11.8.5.3.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle"><span class="application">pg_standby</span></span></h2><p>pg_standby — supports the creation of a <span class="productname">PostgreSQL</span> warm standby server</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><div class="cmdsynopsis"><p id="id-1.11.8.5.3.4.1"><code class="command">pg_standby</code> [<em class="replaceable"><code>option</code></em>...] <em class="replaceable"><code>archivelocation</code></em> <em class="replaceable"><code>nextwalfile</code></em> <em class="replaceable"><code>walfilepath</code></em> [<em class="replaceable"><code>restartwalfile</code></em>]</p></div></div><div class="refsect1" id="id-1.11.8.5.3.5"><h2>Description</h2><p>
  3. <span class="application">pg_standby</span> supports creation of a <span class="quote">“<span class="quote">warm standby</span>”</span>
  4. database server. It is designed to be a production-ready program, as well
  5. as a customizable template should you require specific modifications.
  6. </p><p>
  7. <span class="application">pg_standby</span> is designed to be a waiting
  8. <code class="varname">restore_command</code>, which is needed to turn a standard
  9. archive recovery into a warm standby operation. Other
  10. configuration is required as well, all of which is described in the main
  11. server manual (see <a class="xref" href="warm-standby.html" title="26.2. Log-Shipping Standby Servers">Section 26.2</a>).
  12. </p><p>
  13. To configure a standby
  14. server to use <span class="application">pg_standby</span>, put this into its
  15. <code class="filename">postgresql.conf</code> configuration file:
  16. </p><pre class="programlisting">
  17. restore_command = 'pg_standby <em class="replaceable"><code>archiveDir</code></em> %f %p %r'
  18. </pre><p>
  19. where <em class="replaceable"><code>archiveDir</code></em> is the directory from which WAL segment
  20. files should be restored.
  21. </p><p>
  22. If <em class="replaceable"><code>restartwalfile</code></em> is specified, normally by using the
  23. <code class="literal">%r</code> macro, then all WAL files logically preceding this
  24. file will be removed from <em class="replaceable"><code>archivelocation</code></em>. This minimizes
  25. the number of files that need to be retained, while preserving
  26. crash-restart capability. Use of this parameter is appropriate if the
  27. <em class="replaceable"><code>archivelocation</code></em> is a transient staging area for this
  28. particular standby server, but <span class="emphasis"><em>not</em></span> when the
  29. <em class="replaceable"><code>archivelocation</code></em> is intended as a long-term WAL archive area.
  30. </p><p>
  31. <span class="application">pg_standby</span> assumes that
  32. <em class="replaceable"><code>archivelocation</code></em> is a directory readable by the
  33. server-owning user. If <em class="replaceable"><code>restartwalfile</code></em> (or <code class="literal">-k</code>)
  34. is specified,
  35. the <em class="replaceable"><code>archivelocation</code></em> directory must be writable too.
  36. </p><p>
  37. There are two ways to fail over to a <span class="quote">“<span class="quote">warm standby</span>”</span> database server
  38. when the master server fails:
  39. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term">Smart Failover</span></dt><dd><p>
  40. In smart failover, the server is brought up after applying all WAL
  41. files available in the archive. This results in zero data loss, even if
  42. the standby server has fallen behind, but if there is a lot of
  43. unapplied WAL it can be a long time before the standby server becomes
  44. ready. To trigger a smart failover, create a trigger file containing
  45. the word <code class="literal">smart</code>, or just create it and leave it empty.
  46. </p></dd><dt><span class="term">Fast Failover</span></dt><dd><p>
  47. In fast failover, the server is brought up immediately. Any WAL files
  48. in the archive that have not yet been applied will be ignored, and
  49. all transactions in those files are lost. To trigger a fast failover,
  50. create a trigger file and write the word <code class="literal">fast</code> into it.
  51. <span class="application">pg_standby</span> can also be configured to execute a fast
  52. failover automatically if no new WAL file appears within a defined
  53. interval.
  54. </p></dd></dl></div><p>
  55. </p></div><div class="refsect1" id="id-1.11.8.5.3.6"><h2>Options</h2><p>
  56. <span class="application">pg_standby</span> accepts the following command-line arguments:
  57. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="option">-c</code></span></dt><dd><p>
  58. Use <code class="literal">cp</code> or <code class="literal">copy</code> command to restore WAL files
  59. from archive. This is the only supported behavior so this option is useless.
  60. </p></dd><dt><span class="term"><code class="option">-d</code></span></dt><dd><p>
  61. Print lots of debug logging output on <code class="filename">stderr</code>.
  62. </p></dd><dt><span class="term"><code class="option">-k</code></span></dt><dd><p>
  63. Remove files from <em class="replaceable"><code>archivelocation</code></em> so that
  64. no more than this many WAL files before the current one are kept in the
  65. archive. Zero (the default) means not to remove any files from
  66. <em class="replaceable"><code>archivelocation</code></em>.
  67. This parameter will be silently ignored if
  68. <em class="replaceable"><code>restartwalfile</code></em> is specified, since that
  69. specification method is more accurate in determining the correct
  70. archive cut-off point.
  71. Use of this parameter is <span class="emphasis"><em>deprecated</em></span> as of
  72. <span class="productname">PostgreSQL</span> 8.3; it is safer and more efficient to
  73. specify a <em class="replaceable"><code>restartwalfile</code></em> parameter. A too
  74. small setting could result in removal of files that are still needed
  75. for a restart of the standby server, while a too large setting wastes
  76. archive space.
  77. </p></dd><dt><span class="term"><code class="option">-r</code> <em class="replaceable"><code>maxretries</code></em></span></dt><dd><p>
  78. Set the maximum number of times to retry the copy command if
  79. it fails (default 3). After each failure, we wait for
  80. <em class="replaceable"><code>sleeptime</code></em> * <em class="replaceable"><code>num_retries</code></em>
  81. so that the wait time increases progressively. So by default,
  82. we will wait 5 secs, 10 secs, then 15 secs before reporting
  83. the failure back to the standby server. This will be
  84. interpreted as end of recovery and the standby will come
  85. up fully as a result.
  86. </p></dd><dt><span class="term"><code class="option">-s</code> <em class="replaceable"><code>sleeptime</code></em></span></dt><dd><p>
  87. Set the number of seconds (up to 60, default 5) to sleep between
  88. tests to see if the WAL file to be restored is available in
  89. the archive yet. The default setting is not necessarily
  90. recommended; consult <a class="xref" href="warm-standby.html" title="26.2. Log-Shipping Standby Servers">Section 26.2</a> for discussion.
  91. </p></dd><dt><span class="term"><code class="option">-t</code> <em class="replaceable"><code>triggerfile</code></em></span></dt><dd><p>
  92. Specify a trigger file whose presence should cause failover.
  93. It is recommended that you use a structured file name to
  94. avoid confusion as to which server is being triggered
  95. when multiple servers exist on the same system; for example
  96. <code class="filename">/tmp/pgsql.trigger.5432</code>.
  97. </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>
  98. Print the <span class="application">pg_standby</span> version and exit.
  99. </p></dd><dt><span class="term"><code class="option">-w</code> <em class="replaceable"><code>maxwaittime</code></em></span></dt><dd><p>
  100. Set the maximum number of seconds to wait for the next WAL file,
  101. after which a fast failover will be performed.
  102. A setting of zero (the default) means wait forever.
  103. The default setting is not necessarily recommended;
  104. consult <a class="xref" href="warm-standby.html" title="26.2. Log-Shipping Standby Servers">Section 26.2</a> for discussion.
  105. </p></dd><dt><span class="term"><code class="option">-?</code><br /></span><span class="term"><code class="option">--help</code></span></dt><dd><p>
  106. Show help about <span class="application">pg_standby</span> command line
  107. arguments, and exit.
  108. </p></dd></dl></div><p>
  109. </p></div><div class="refsect1" id="id-1.11.8.5.3.7"><h2>Notes</h2><p>
  110. <span class="application">pg_standby</span> is designed to work with
  111. <span class="productname">PostgreSQL</span> 8.2 and later.
  112. </p><p>
  113. <span class="productname">PostgreSQL</span> 8.3 provides the <code class="literal">%r</code> macro,
  114. which is designed to let <span class="application">pg_standby</span> know the
  115. last file it needs to keep. With <span class="productname">PostgreSQL</span> 8.2, the
  116. <code class="literal">-k</code> option must be used if archive cleanup is
  117. required. This option remains available in 8.3, but its use is deprecated.
  118. </p><p>
  119. <span class="productname">PostgreSQL</span> 8.4 provides the
  120. <code class="varname">recovery_end_command</code> option. Without this option
  121. a leftover trigger file can be hazardous.
  122. </p><p>
  123. <span class="application">pg_standby</span> is written in C and has an
  124. easy-to-modify source code, with specifically designated sections to modify
  125. for your own needs
  126. </p></div><div class="refsect1" id="id-1.11.8.5.3.8"><h2>Examples</h2><p>On Linux or Unix systems, you might use:
  127. </p><pre class="programlisting">
  128. archive_command = 'cp %p .../archive/%f'
  129. restore_command = 'pg_standby -d -s 2 -t /tmp/pgsql.trigger.5442 .../archive %f %p %r 2&gt;&gt;standby.log'
  130. recovery_end_command = 'rm -f /tmp/pgsql.trigger.5442'
  131. </pre><p>
  132. where the archive directory is physically located on the standby server,
  133. so that the <code class="varname">archive_command</code> is accessing it across NFS,
  134. but the files are local to the standby (enabling use of <code class="literal">ln</code>).
  135. This will:
  136. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  137. produce debugging output in <code class="filename">standby.log</code>
  138. </p></li><li class="listitem"><p>
  139. sleep for 2 seconds between checks for next WAL file availability
  140. </p></li><li class="listitem"><p>
  141. stop waiting only when a trigger file called
  142. <code class="filename">/tmp/pgsql.trigger.5442</code> appears,
  143. and perform failover according to its content
  144. </p></li><li class="listitem"><p>
  145. remove the trigger file when recovery ends
  146. </p></li><li class="listitem"><p>
  147. remove no-longer-needed files from the archive directory
  148. </p></li></ul></div><p>
  149. </p><p>On Windows, you might use:
  150. </p><pre class="programlisting">
  151. archive_command = 'copy %p ...\\archive\\%f'
  152. restore_command = 'pg_standby -d -s 5 -t C:\pgsql.trigger.5442 ...\archive %f %p %r 2&gt;&gt;standby.log'
  153. recovery_end_command = 'del C:\pgsql.trigger.5442'
  154. </pre><p>
  155. Note that backslashes need to be doubled in the
  156. <code class="varname">archive_command</code>, but <span class="emphasis"><em>not</em></span> in the
  157. <code class="varname">restore_command</code> or <code class="varname">recovery_end_command</code>.
  158. This will:
  159. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  160. use the <code class="literal">copy</code> command to restore WAL files from archive
  161. </p></li><li class="listitem"><p>
  162. produce debugging output in <code class="filename">standby.log</code>
  163. </p></li><li class="listitem"><p>
  164. sleep for 5 seconds between checks for next WAL file availability
  165. </p></li><li class="listitem"><p>
  166. stop waiting only when a trigger file called
  167. <code class="filename">C:\pgsql.trigger.5442</code> appears,
  168. and perform failover according to its content
  169. </p></li><li class="listitem"><p>
  170. remove the trigger file when recovery ends
  171. </p></li><li class="listitem"><p>
  172. remove no-longer-needed files from the archive directory
  173. </p></li></ul></div><p>
  174. </p><p>
  175. The <code class="literal">copy</code> command on Windows sets the final file size
  176. before the file is completely copied, which would ordinarily confuse
  177. <span class="application">pg_standby</span>. Therefore
  178. <span class="application">pg_standby</span> waits <em class="replaceable"><code>sleeptime</code></em>
  179. seconds once it sees the proper file size. GNUWin32's <code class="literal">cp</code>
  180. sets the file size only after the file copy is complete.
  181. </p><p>
  182. Since the Windows example uses <code class="literal">copy</code> at both ends, either
  183. or both servers might be accessing the archive directory across the
  184. network.
  185. </p></div><div class="refsect1" id="id-1.11.8.5.3.9"><h2>Author</h2><p>
  186. Simon Riggs <code class="email">&lt;<a class="email" href="mailto:simon@2ndquadrant.com">simon@2ndquadrant.com</a>&gt;</code>
  187. </p></div><div class="refsect1" id="id-1.11.8.5.3.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="pgarchivecleanup.html" title="pg_archivecleanup"><span class="refentrytitle"><span class="application">pg_archivecleanup</span></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="contrib-prog-server.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="contrib-prog-server.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="external-projects.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">G.2. Server Applications </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Appendix H. External Projects</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1