gooderp18绿色标准版
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

539 lines
39KB

  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>26.5. Hot 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="log-shipping-alternative.html" title="26.4. Alternative Method for Log Shipping" /><link rel="next" href="monitoring.html" title="Chapter 27. Monitoring Database Activity" /></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">26.5. Hot Standby</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="log-shipping-alternative.html" title="26.4. Alternative Method for Log Shipping">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="high-availability.html" title="Chapter 26. High Availability, Load Balancing, and Replication">Up</a></td><th width="60%" align="center">Chapter 26. High Availability, Load Balancing, and Replication</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="monitoring.html" title="Chapter 27. Monitoring Database Activity">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="HOT-STANDBY"><div class="titlepage"><div><div><h2 class="title" style="clear: both">26.5. Hot Standby</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="hot-standby.html#HOT-STANDBY-USERS">26.5.1. User's Overview</a></span></dt><dt><span class="sect2"><a href="hot-standby.html#HOT-STANDBY-CONFLICT">26.5.2. Handling Query Conflicts</a></span></dt><dt><span class="sect2"><a href="hot-standby.html#HOT-STANDBY-ADMIN">26.5.3. Administrator's Overview</a></span></dt><dt><span class="sect2"><a href="hot-standby.html#HOT-STANDBY-PARAMETERS">26.5.4. Hot Standby Parameter Reference</a></span></dt><dt><span class="sect2"><a href="hot-standby.html#HOT-STANDBY-CAVEATS">26.5.5. Caveats</a></span></dt></dl></div><a id="id-1.6.13.19.2" class="indexterm"></a><p>
  3. Hot Standby is the term used to describe the ability to connect to
  4. the server and run read-only queries while the server is in archive
  5. recovery or standby mode. This
  6. is useful both for replication purposes and for restoring a backup
  7. to a desired state with great precision.
  8. The term Hot Standby also refers to the ability of the server to move
  9. from recovery through to normal operation while users continue running
  10. queries and/or keep their connections open.
  11. </p><p>
  12. Running queries in hot standby mode is similar to normal query operation,
  13. though there are several usage and administrative differences
  14. explained below.
  15. </p><div class="sect2" id="HOT-STANDBY-USERS"><div class="titlepage"><div><div><h3 class="title">26.5.1. User's Overview</h3></div></div></div><p>
  16. When the <a class="xref" href="runtime-config-replication.html#GUC-HOT-STANDBY">hot_standby</a> parameter is set to true on a
  17. standby server, it will begin accepting connections once the recovery has
  18. brought the system to a consistent state. All such connections are
  19. strictly read-only; not even temporary tables may be written.
  20. </p><p>
  21. The data on the standby takes some time to arrive from the primary server
  22. so there will be a measurable delay between primary and standby. Running the
  23. same query nearly simultaneously on both primary and standby might therefore
  24. return differing results. We say that data on the standby is
  25. <em class="firstterm">eventually consistent</em> with the primary. Once the
  26. commit record for a transaction is replayed on the standby, the changes
  27. made by that transaction will be visible to any new snapshots taken on
  28. the standby. Snapshots may be taken at the start of each query or at the
  29. start of each transaction, depending on the current transaction isolation
  30. level. For more details, see <a class="xref" href="transaction-iso.html" title="13.2. Transaction Isolation">Section 13.2</a>.
  31. </p><p>
  32. Transactions started during hot standby may issue the following commands:
  33. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  34. Query access - <code class="command">SELECT</code>, <code class="command">COPY TO</code>
  35. </p></li><li class="listitem"><p>
  36. Cursor commands - <code class="command">DECLARE</code>, <code class="command">FETCH</code>, <code class="command">CLOSE</code>
  37. </p></li><li class="listitem"><p>
  38. Parameters - <code class="command">SHOW</code>, <code class="command">SET</code>, <code class="command">RESET</code>
  39. </p></li><li class="listitem"><p>
  40. Transaction management commands
  41. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"><p>
  42. <code class="command">BEGIN</code>, <code class="command">END</code>, <code class="command">ABORT</code>, <code class="command">START TRANSACTION</code>
  43. </p></li><li class="listitem"><p>
  44. <code class="command">SAVEPOINT</code>, <code class="command">RELEASE</code>, <code class="command">ROLLBACK TO SAVEPOINT</code>
  45. </p></li><li class="listitem"><p>
  46. <code class="command">EXCEPTION</code> blocks and other internal subtransactions
  47. </p></li></ul></div><p>
  48. </p></li><li class="listitem"><p>
  49. <code class="command">LOCK TABLE</code>, though only when explicitly in one of these modes:
  50. <code class="literal">ACCESS SHARE</code>, <code class="literal">ROW SHARE</code> or <code class="literal">ROW EXCLUSIVE</code>.
  51. </p></li><li class="listitem"><p>
  52. Plans and resources - <code class="command">PREPARE</code>, <code class="command">EXECUTE</code>,
  53. <code class="command">DEALLOCATE</code>, <code class="command">DISCARD</code>
  54. </p></li><li class="listitem"><p>
  55. Plugins and extensions - <code class="command">LOAD</code>
  56. </p></li><li class="listitem"><p>
  57. <code class="command">UNLISTEN</code>
  58. </p></li></ul></div><p>
  59. </p><p>
  60. Transactions started during hot standby will never be assigned a
  61. transaction ID and cannot write to the system write-ahead log.
  62. Therefore, the following actions will produce error messages:
  63. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  64. Data Manipulation Language (DML) - <code class="command">INSERT</code>,
  65. <code class="command">UPDATE</code>, <code class="command">DELETE</code>, <code class="command">COPY FROM</code>,
  66. <code class="command">TRUNCATE</code>.
  67. Note that there are no allowed actions that result in a trigger
  68. being executed during recovery. This restriction applies even to
  69. temporary tables, because table rows cannot be read or written without
  70. assigning a transaction ID, which is currently not possible in a
  71. Hot Standby environment.
  72. </p></li><li class="listitem"><p>
  73. Data Definition Language (DDL) - <code class="command">CREATE</code>,
  74. <code class="command">DROP</code>, <code class="command">ALTER</code>, <code class="command">COMMENT</code>.
  75. This restriction applies even to temporary tables, because carrying
  76. out these operations would require updating the system catalog tables.
  77. </p></li><li class="listitem"><p>
  78. <code class="command">SELECT ... FOR SHARE | UPDATE</code>, because row locks cannot be
  79. taken without updating the underlying data files.
  80. </p></li><li class="listitem"><p>
  81. Rules on <code class="command">SELECT</code> statements that generate DML commands.
  82. </p></li><li class="listitem"><p>
  83. <code class="command">LOCK</code> that explicitly requests a mode higher than <code class="literal">ROW EXCLUSIVE MODE</code>.
  84. </p></li><li class="listitem"><p>
  85. <code class="command">LOCK</code> in short default form, since it requests <code class="literal">ACCESS EXCLUSIVE MODE</code>.
  86. </p></li><li class="listitem"><p>
  87. Transaction management commands that explicitly set non-read-only state:
  88. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"><p>
  89. <code class="command">BEGIN READ WRITE</code>,
  90. <code class="command">START TRANSACTION READ WRITE</code>
  91. </p></li><li class="listitem"><p>
  92. <code class="command">SET TRANSACTION READ WRITE</code>,
  93. <code class="command">SET SESSION CHARACTERISTICS AS TRANSACTION READ WRITE</code>
  94. </p></li><li class="listitem"><p>
  95. <code class="command">SET transaction_read_only = off</code>
  96. </p></li></ul></div><p>
  97. </p></li><li class="listitem"><p>
  98. Two-phase commit commands - <code class="command">PREPARE TRANSACTION</code>,
  99. <code class="command">COMMIT PREPARED</code>, <code class="command">ROLLBACK PREPARED</code>
  100. because even read-only transactions need to write WAL in the
  101. prepare phase (the first phase of two phase commit).
  102. </p></li><li class="listitem"><p>
  103. Sequence updates - <code class="function">nextval()</code>, <code class="function">setval()</code>
  104. </p></li><li class="listitem"><p>
  105. <code class="command">LISTEN</code>, <code class="command">NOTIFY</code>
  106. </p></li></ul></div><p>
  107. </p><p>
  108. In normal operation, <span class="quote">“<span class="quote">read-only</span>”</span> transactions are allowed to
  109. use <code class="command">LISTEN</code> and <code class="command">NOTIFY</code>,
  110. so Hot Standby sessions operate under slightly tighter
  111. restrictions than ordinary read-only sessions. It is possible that some
  112. of these restrictions might be loosened in a future release.
  113. </p><p>
  114. During hot standby, the parameter <code class="varname">transaction_read_only</code> is always
  115. true and may not be changed. But as long as no attempt is made to modify
  116. the database, connections during hot standby will act much like any other
  117. database connection. If failover or switchover occurs, the database will
  118. switch to normal processing mode. Sessions will remain connected while the
  119. server changes mode. Once hot standby finishes, it will be possible to
  120. initiate read-write transactions (even from a session begun during
  121. hot standby).
  122. </p><p>
  123. Users will be able to tell whether their session is read-only by
  124. issuing <code class="command">SHOW transaction_read_only</code>. In addition, a set of
  125. functions (<a class="xref" href="functions-admin.html#FUNCTIONS-RECOVERY-INFO-TABLE" title="Table 9.85. Recovery Information Functions">Table 9.85</a>) allow users to
  126. access information about the standby server. These allow you to write
  127. programs that are aware of the current state of the database. These
  128. can be used to monitor the progress of recovery, or to allow you to
  129. write complex programs that restore the database to particular states.
  130. </p></div><div class="sect2" id="HOT-STANDBY-CONFLICT"><div class="titlepage"><div><div><h3 class="title">26.5.2. Handling Query Conflicts</h3></div></div></div><p>
  131. The primary and standby servers are in many ways loosely connected. Actions
  132. on the primary will have an effect on the standby. As a result, there is
  133. potential for negative interactions or conflicts between them. The easiest
  134. conflict to understand is performance: if a huge data load is taking place
  135. on the primary then this will generate a similar stream of WAL records on the
  136. standby, so standby queries may contend for system resources, such as I/O.
  137. </p><p>
  138. There are also additional types of conflict that can occur with Hot Standby.
  139. These conflicts are <span class="emphasis"><em>hard conflicts</em></span> in the sense that queries
  140. might need to be canceled and, in some cases, sessions disconnected to resolve them.
  141. The user is provided with several ways to handle these
  142. conflicts. Conflict cases include:
  143. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  144. Access Exclusive locks taken on the primary server, including both
  145. explicit <code class="command">LOCK</code> commands and various <acronym class="acronym">DDL</acronym>
  146. actions, conflict with table accesses in standby queries.
  147. </p></li><li class="listitem"><p>
  148. Dropping a tablespace on the primary conflicts with standby queries
  149. using that tablespace for temporary work files.
  150. </p></li><li class="listitem"><p>
  151. Dropping a database on the primary conflicts with sessions connected
  152. to that database on the standby.
  153. </p></li><li class="listitem"><p>
  154. Application of a vacuum cleanup record from WAL conflicts with
  155. standby transactions whose snapshots can still <span class="quote">“<span class="quote">see</span>”</span> any of
  156. the rows to be removed.
  157. </p></li><li class="listitem"><p>
  158. Application of a vacuum cleanup record from WAL conflicts with
  159. queries accessing the target page on the standby, whether or not
  160. the data to be removed is visible.
  161. </p></li></ul></div><p>
  162. </p><p>
  163. On the primary server, these cases simply result in waiting; and the
  164. user might choose to cancel either of the conflicting actions. However,
  165. on the standby there is no choice: the WAL-logged action already occurred
  166. on the primary so the standby must not fail to apply it. Furthermore,
  167. allowing WAL application to wait indefinitely may be very undesirable,
  168. because the standby's state will become increasingly far behind the
  169. primary's. Therefore, a mechanism is provided to forcibly cancel standby
  170. queries that conflict with to-be-applied WAL records.
  171. </p><p>
  172. An example of the problem situation is an administrator on the primary
  173. server running <code class="command">DROP TABLE</code> on a table that is currently being
  174. queried on the standby server. Clearly the standby query cannot continue
  175. if the <code class="command">DROP TABLE</code> is applied on the standby. If this situation
  176. occurred on the primary, the <code class="command">DROP TABLE</code> would wait until the
  177. other query had finished. But when <code class="command">DROP TABLE</code> is run on the
  178. primary, the primary doesn't have information about what queries are
  179. running on the standby, so it will not wait for any such standby
  180. queries. The WAL change records come through to the standby while the
  181. standby query is still running, causing a conflict. The standby server
  182. must either delay application of the WAL records (and everything after
  183. them, too) or else cancel the conflicting query so that the <code class="command">DROP
  184. TABLE</code> can be applied.
  185. </p><p>
  186. When a conflicting query is short, it's typically desirable to allow it to
  187. complete by delaying WAL application for a little bit; but a long delay in
  188. WAL application is usually not desirable. So the cancel mechanism has
  189. parameters, <a class="xref" href="runtime-config-replication.html#GUC-MAX-STANDBY-ARCHIVE-DELAY">max_standby_archive_delay</a> and <a class="xref" href="runtime-config-replication.html#GUC-MAX-STANDBY-STREAMING-DELAY">max_standby_streaming_delay</a>, that define the maximum
  190. allowed delay in WAL application. Conflicting queries will be canceled
  191. once it has taken longer than the relevant delay setting to apply any
  192. newly-received WAL data. There are two parameters so that different delay
  193. values can be specified for the case of reading WAL data from an archive
  194. (i.e., initial recovery from a base backup or <span class="quote">“<span class="quote">catching up</span>”</span> a
  195. standby server that has fallen far behind) versus reading WAL data via
  196. streaming replication.
  197. </p><p>
  198. In a standby server that exists primarily for high availability, it's
  199. best to set the delay parameters relatively short, so that the server
  200. cannot fall far behind the primary due to delays caused by standby
  201. queries. However, if the standby server is meant for executing
  202. long-running queries, then a high or even infinite delay value may be
  203. preferable. Keep in mind however that a long-running query could
  204. cause other sessions on the standby server to not see recent changes
  205. on the primary, if it delays application of WAL records.
  206. </p><p>
  207. Once the delay specified by <code class="varname">max_standby_archive_delay</code> or
  208. <code class="varname">max_standby_streaming_delay</code> has been exceeded, conflicting
  209. queries will be canceled. This usually results just in a cancellation
  210. error, although in the case of replaying a <code class="command">DROP DATABASE</code>
  211. the entire conflicting session will be terminated. Also, if the conflict
  212. is over a lock held by an idle transaction, the conflicting session is
  213. terminated (this behavior might change in the future).
  214. </p><p>
  215. Canceled queries may be retried immediately (after beginning a new
  216. transaction, of course). Since query cancellation depends on
  217. the nature of the WAL records being replayed, a query that was
  218. canceled may well succeed if it is executed again.
  219. </p><p>
  220. Keep in mind that the delay parameters are compared to the elapsed time
  221. since the WAL data was received by the standby server. Thus, the grace
  222. period allowed to any one query on the standby is never more than the
  223. delay parameter, and could be considerably less if the standby has already
  224. fallen behind as a result of waiting for previous queries to complete, or
  225. as a result of being unable to keep up with a heavy update load.
  226. </p><p>
  227. The most common reason for conflict between standby queries and WAL replay
  228. is <span class="quote">“<span class="quote">early cleanup</span>”</span>. Normally, <span class="productname">PostgreSQL</span> allows
  229. cleanup of old row versions when there are no transactions that need to
  230. see them to ensure correct visibility of data according to MVCC rules.
  231. However, this rule can only be applied for transactions executing on the
  232. master. So it is possible that cleanup on the master will remove row
  233. versions that are still visible to a transaction on the standby.
  234. </p><p>
  235. Experienced users should note that both row version cleanup and row version
  236. freezing will potentially conflict with standby queries. Running a manual
  237. <code class="command">VACUUM FREEZE</code> is likely to cause conflicts even on tables with
  238. no updated or deleted rows.
  239. </p><p>
  240. Users should be clear that tables that are regularly and heavily updated
  241. on the primary server will quickly cause cancellation of longer running
  242. queries on the standby. In such cases the setting of a finite value for
  243. <code class="varname">max_standby_archive_delay</code> or
  244. <code class="varname">max_standby_streaming_delay</code> can be considered similar to
  245. setting <code class="varname">statement_timeout</code>.
  246. </p><p>
  247. Remedial possibilities exist if the number of standby-query cancellations
  248. is found to be unacceptable. The first option is to set the parameter
  249. <code class="varname">hot_standby_feedback</code>, which prevents <code class="command">VACUUM</code> from
  250. removing recently-dead rows and so cleanup conflicts do not occur.
  251. If you do this, you
  252. should note that this will delay cleanup of dead rows on the primary,
  253. which may result in undesirable table bloat. However, the cleanup
  254. situation will be no worse than if the standby queries were running
  255. directly on the primary server, and you are still getting the benefit of
  256. off-loading execution onto the standby.
  257. If standby servers connect and disconnect frequently, you
  258. might want to make adjustments to handle the period when
  259. <code class="varname">hot_standby_feedback</code> feedback is not being provided.
  260. For example, consider increasing <code class="varname">max_standby_archive_delay</code>
  261. so that queries are not rapidly canceled by conflicts in WAL archive
  262. files during disconnected periods. You should also consider increasing
  263. <code class="varname">max_standby_streaming_delay</code> to avoid rapid cancellations
  264. by newly-arrived streaming WAL entries after reconnection.
  265. </p><p>
  266. Another option is to increase <a class="xref" href="runtime-config-replication.html#GUC-VACUUM-DEFER-CLEANUP-AGE">vacuum_defer_cleanup_age</a>
  267. on the primary server, so that dead rows will not be cleaned up as quickly
  268. as they normally would be. This will allow more time for queries to
  269. execute before they are canceled on the standby, without having to set
  270. a high <code class="varname">max_standby_streaming_delay</code>. However it is
  271. difficult to guarantee any specific execution-time window with this
  272. approach, since <code class="varname">vacuum_defer_cleanup_age</code> is measured in
  273. transactions executed on the primary server.
  274. </p><p>
  275. The number of query cancels and the reason for them can be viewed using
  276. the <code class="structname">pg_stat_database_conflicts</code> system view on the standby
  277. server. The <code class="structname">pg_stat_database</code> system view also contains
  278. summary information.
  279. </p></div><div class="sect2" id="HOT-STANDBY-ADMIN"><div class="titlepage"><div><div><h3 class="title">26.5.3. Administrator's Overview</h3></div></div></div><p>
  280. If <code class="varname">hot_standby</code> is <code class="literal">on</code> in <code class="filename">postgresql.conf</code>
  281. (the default value) and there is a <code class="filename">standby.signal</code>
  282. file present, the server will run in Hot Standby mode.
  283. However, it may take some time for Hot Standby connections to be allowed,
  284. because the server will not accept connections until it has completed
  285. sufficient recovery to provide a consistent state against which queries
  286. can run. During this period,
  287. clients that attempt to connect will be refused with an error message.
  288. To confirm the server has come up, either loop trying to connect from
  289. the application, or look for these messages in the server logs:
  290. </p><pre class="programlisting">
  291. LOG: entering standby mode
  292. ... then some time later ...
  293. LOG: consistent recovery state reached
  294. LOG: database system is ready to accept read only connections
  295. </pre><p>
  296. Consistency information is recorded once per checkpoint on the primary.
  297. It is not possible to enable hot standby when reading WAL
  298. written during a period when <code class="varname">wal_level</code> was not set to
  299. <code class="literal">replica</code> or <code class="literal">logical</code> on the primary. Reaching
  300. a consistent state can also be delayed in the presence of both of these
  301. conditions:
  302. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  303. A write transaction has more than 64 subtransactions
  304. </p></li><li class="listitem"><p>
  305. Very long-lived write transactions
  306. </p></li></ul></div><p>
  307. If you are running file-based log shipping ("warm standby"), you might need
  308. to wait until the next WAL file arrives, which could be as long as the
  309. <code class="varname">archive_timeout</code> setting on the primary.
  310. </p><p>
  311. The setting of some parameters on the standby will need reconfiguration
  312. if they have been changed on the primary. For these parameters,
  313. the value on the standby must
  314. be equal to or greater than the value on the primary.
  315. Therefore, if you want to increase these values, you should do so on all
  316. standby servers first, before applying the changes to the primary server.
  317. Conversely, if you want to decrease these values, you should do so on the
  318. primary server first, before applying the changes to all standby servers.
  319. If these parameters
  320. are not set high enough then the standby will refuse to start.
  321. Higher values can then be supplied and the server
  322. restarted to begin recovery again. These parameters are:
  323. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  324. <code class="varname">max_connections</code>
  325. </p></li><li class="listitem"><p>
  326. <code class="varname">max_prepared_transactions</code>
  327. </p></li><li class="listitem"><p>
  328. <code class="varname">max_locks_per_transaction</code>
  329. </p></li><li class="listitem"><p>
  330. <code class="varname">max_wal_senders</code>
  331. </p></li><li class="listitem"><p>
  332. <code class="varname">max_worker_processes</code>
  333. </p></li></ul></div><p>
  334. </p><p>
  335. It is important that the administrator select appropriate settings for
  336. <a class="xref" href="runtime-config-replication.html#GUC-MAX-STANDBY-ARCHIVE-DELAY">max_standby_archive_delay</a> and <a class="xref" href="runtime-config-replication.html#GUC-MAX-STANDBY-STREAMING-DELAY">max_standby_streaming_delay</a>. The best choices vary
  337. depending on business priorities. For example if the server is primarily
  338. tasked as a High Availability server, then you will want low delay
  339. settings, perhaps even zero, though that is a very aggressive setting. If
  340. the standby server is tasked as an additional server for decision support
  341. queries then it might be acceptable to set the maximum delay values to
  342. many hours, or even -1 which means wait forever for queries to complete.
  343. </p><p>
  344. Transaction status "hint bits" written on the primary are not WAL-logged,
  345. so data on the standby will likely re-write the hints again on the standby.
  346. Thus, the standby server will still perform disk writes even though
  347. all users are read-only; no changes occur to the data values
  348. themselves. Users will still write large sort temporary files and
  349. re-generate relcache info files, so no part of the database
  350. is truly read-only during hot standby mode.
  351. Note also that writes to remote databases using
  352. <span class="application">dblink</span> module, and other operations outside the
  353. database using PL functions will still be possible, even though the
  354. transaction is read-only locally.
  355. </p><p>
  356. The following types of administration commands are not accepted
  357. during recovery mode:
  358. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  359. Data Definition Language (DDL) - e.g. <code class="command">CREATE INDEX</code>
  360. </p></li><li class="listitem"><p>
  361. Privilege and Ownership - <code class="command">GRANT</code>, <code class="command">REVOKE</code>,
  362. <code class="command">REASSIGN</code>
  363. </p></li><li class="listitem"><p>
  364. Maintenance commands - <code class="command">ANALYZE</code>, <code class="command">VACUUM</code>,
  365. <code class="command">CLUSTER</code>, <code class="command">REINDEX</code>
  366. </p></li></ul></div><p>
  367. </p><p>
  368. Again, note that some of these commands are actually allowed during
  369. "read only" mode transactions on the primary.
  370. </p><p>
  371. As a result, you cannot create additional indexes that exist solely
  372. on the standby, nor statistics that exist solely on the standby.
  373. If these administration commands are needed, they should be executed
  374. on the primary, and eventually those changes will propagate to the
  375. standby.
  376. </p><p>
  377. <code class="function">pg_cancel_backend()</code>
  378. and <code class="function">pg_terminate_backend()</code> will work on user backends,
  379. but not the Startup process, which performs
  380. recovery. <code class="structname">pg_stat_activity</code> does not show
  381. recovering transactions as active. As a result,
  382. <code class="structname">pg_prepared_xacts</code> is always empty during
  383. recovery. If you wish to resolve in-doubt prepared transactions, view
  384. <code class="literal">pg_prepared_xacts</code> on the primary and issue commands to
  385. resolve transactions there or resolve them after the end of recovery.
  386. </p><p>
  387. <code class="structname">pg_locks</code> will show locks held by backends,
  388. as normal. <code class="structname">pg_locks</code> also shows
  389. a virtual transaction managed by the Startup process that owns all
  390. <code class="literal">AccessExclusiveLocks</code> held by transactions being replayed by recovery.
  391. Note that the Startup process does not acquire locks to
  392. make database changes, and thus locks other than <code class="literal">AccessExclusiveLocks</code>
  393. do not show in <code class="structname">pg_locks</code> for the Startup
  394. process; they are just presumed to exist.
  395. </p><p>
  396. The <span class="productname">Nagios</span> plugin <span class="productname">check_pgsql</span> will
  397. work, because the simple information it checks for exists.
  398. The <span class="productname">check_postgres</span> monitoring script will also work,
  399. though some reported values could give different or confusing results.
  400. For example, last vacuum time will not be maintained, since no
  401. vacuum occurs on the standby. Vacuums running on the primary
  402. do still send their changes to the standby.
  403. </p><p>
  404. WAL file control commands will not work during recovery,
  405. e.g. <code class="function">pg_start_backup</code>, <code class="function">pg_switch_wal</code> etc.
  406. </p><p>
  407. Dynamically loadable modules work, including <code class="structname">pg_stat_statements</code>.
  408. </p><p>
  409. Advisory locks work normally in recovery, including deadlock detection.
  410. Note that advisory locks are never WAL logged, so it is impossible for
  411. an advisory lock on either the primary or the standby to conflict with WAL
  412. replay. Nor is it possible to acquire an advisory lock on the primary
  413. and have it initiate a similar advisory lock on the standby. Advisory
  414. locks relate only to the server on which they are acquired.
  415. </p><p>
  416. Trigger-based replication systems such as <span class="productname">Slony</span>,
  417. <span class="productname">Londiste</span> and <span class="productname">Bucardo</span> won't run on the
  418. standby at all, though they will run happily on the primary server as
  419. long as the changes are not sent to standby servers to be applied.
  420. WAL replay is not trigger-based so you cannot relay from the
  421. standby to any system that requires additional database writes or
  422. relies on the use of triggers.
  423. </p><p>
  424. New OIDs cannot be assigned, though some <acronym class="acronym">UUID</acronym> generators may still
  425. work as long as they do not rely on writing new status to the database.
  426. </p><p>
  427. Currently, temporary table creation is not allowed during read only
  428. transactions, so in some cases existing scripts will not run correctly.
  429. This restriction might be relaxed in a later release. This is
  430. both a SQL Standard compliance issue and a technical issue.
  431. </p><p>
  432. <code class="command">DROP TABLESPACE</code> can only succeed if the tablespace is empty.
  433. Some standby users may be actively using the tablespace via their
  434. <code class="varname">temp_tablespaces</code> parameter. If there are temporary files in the
  435. tablespace, all active queries are canceled to ensure that temporary
  436. files are removed, so the tablespace can be removed and WAL replay
  437. can continue.
  438. </p><p>
  439. Running <code class="command">DROP DATABASE</code> or <code class="command">ALTER DATABASE ... SET
  440. TABLESPACE</code> on the primary
  441. will generate a WAL entry that will cause all users connected to that
  442. database on the standby to be forcibly disconnected. This action occurs
  443. immediately, whatever the setting of
  444. <code class="varname">max_standby_streaming_delay</code>. Note that
  445. <code class="command">ALTER DATABASE ... RENAME</code> does not disconnect users, which
  446. in most cases will go unnoticed, though might in some cases cause a
  447. program confusion if it depends in some way upon database name.
  448. </p><p>
  449. In normal (non-recovery) mode, if you issue <code class="command">DROP USER</code> or <code class="command">DROP ROLE</code>
  450. for a role with login capability while that user is still connected then
  451. nothing happens to the connected user - they remain connected. The user cannot
  452. reconnect however. This behavior applies in recovery also, so a
  453. <code class="command">DROP USER</code> on the primary does not disconnect that user on the standby.
  454. </p><p>
  455. The statistics collector is active during recovery. All scans, reads, blocks,
  456. index usage, etc., will be recorded normally on the standby. Replayed
  457. actions will not duplicate their effects on primary, so replaying an
  458. insert will not increment the Inserts column of pg_stat_user_tables.
  459. The stats file is deleted at the start of recovery, so stats from primary
  460. and standby will differ; this is considered a feature, not a bug.
  461. </p><p>
  462. Autovacuum is not active during recovery. It will start normally at the
  463. end of recovery.
  464. </p><p>
  465. The background writer is active during recovery and will perform
  466. restartpoints (similar to checkpoints on the primary) and normal block
  467. cleaning activities. This can include updates of the hint bit
  468. information stored on the standby server.
  469. The <code class="command">CHECKPOINT</code> command is accepted during recovery,
  470. though it performs a restartpoint rather than a new checkpoint.
  471. </p></div><div class="sect2" id="HOT-STANDBY-PARAMETERS"><div class="titlepage"><div><div><h3 class="title">26.5.4. Hot Standby Parameter Reference</h3></div></div></div><p>
  472. Various parameters have been mentioned above in
  473. <a class="xref" href="hot-standby.html#HOT-STANDBY-CONFLICT" title="26.5.2. Handling Query Conflicts">Section 26.5.2</a> and
  474. <a class="xref" href="hot-standby.html#HOT-STANDBY-ADMIN" title="26.5.3. Administrator's Overview">Section 26.5.3</a>.
  475. </p><p>
  476. On the primary, parameters <a class="xref" href="runtime-config-wal.html#GUC-WAL-LEVEL">wal_level</a> and
  477. <a class="xref" href="runtime-config-replication.html#GUC-VACUUM-DEFER-CLEANUP-AGE">vacuum_defer_cleanup_age</a> can be used.
  478. <a class="xref" href="runtime-config-replication.html#GUC-MAX-STANDBY-ARCHIVE-DELAY">max_standby_archive_delay</a> and
  479. <a class="xref" href="runtime-config-replication.html#GUC-MAX-STANDBY-STREAMING-DELAY">max_standby_streaming_delay</a> have no effect if set on
  480. the primary.
  481. </p><p>
  482. On the standby, parameters <a class="xref" href="runtime-config-replication.html#GUC-HOT-STANDBY">hot_standby</a>,
  483. <a class="xref" href="runtime-config-replication.html#GUC-MAX-STANDBY-ARCHIVE-DELAY">max_standby_archive_delay</a> and
  484. <a class="xref" href="runtime-config-replication.html#GUC-MAX-STANDBY-STREAMING-DELAY">max_standby_streaming_delay</a> can be used.
  485. <a class="xref" href="runtime-config-replication.html#GUC-VACUUM-DEFER-CLEANUP-AGE">vacuum_defer_cleanup_age</a> has no effect
  486. as long as the server remains in standby mode, though it will
  487. become relevant if the standby becomes primary.
  488. </p></div><div class="sect2" id="HOT-STANDBY-CAVEATS"><div class="titlepage"><div><div><h3 class="title">26.5.5. Caveats</h3></div></div></div><p>
  489. There are several limitations of Hot Standby.
  490. These can and probably will be fixed in future releases:
  491. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  492. Full knowledge of running transactions is required before snapshots
  493. can be taken. Transactions that use large numbers of subtransactions
  494. (currently greater than 64) will delay the start of read only
  495. connections until the completion of the longest running write transaction.
  496. If this situation occurs, explanatory messages will be sent to the server log.
  497. </p></li><li class="listitem"><p>
  498. Valid starting points for standby queries are generated at each
  499. checkpoint on the master. If the standby is shut down while the master
  500. is in a shutdown state, it might not be possible to re-enter Hot Standby
  501. until the primary is started up, so that it generates further starting
  502. points in the WAL logs. This situation isn't a problem in the most
  503. common situations where it might happen. Generally, if the primary is
  504. shut down and not available anymore, that's likely due to a serious
  505. failure that requires the standby being converted to operate as
  506. the new primary anyway. And in situations where the primary is
  507. being intentionally taken down, coordinating to make sure the standby
  508. becomes the new primary smoothly is also standard procedure.
  509. </p></li><li class="listitem"><p>
  510. At the end of recovery, <code class="literal">AccessExclusiveLocks</code> held by prepared transactions
  511. will require twice the normal number of lock table entries. If you plan
  512. on running either a large number of concurrent prepared transactions
  513. that normally take <code class="literal">AccessExclusiveLocks</code>, or you plan on having one
  514. large transaction that takes many <code class="literal">AccessExclusiveLocks</code>, you are
  515. advised to select a larger value of <code class="varname">max_locks_per_transaction</code>,
  516. perhaps as much as twice the value of the parameter on
  517. the primary server. You need not consider this at all if
  518. your setting of <code class="varname">max_prepared_transactions</code> is 0.
  519. </p></li><li class="listitem"><p>
  520. The Serializable transaction isolation level is not yet available in hot
  521. standby. (See <a class="xref" href="transaction-iso.html#XACT-SERIALIZABLE" title="13.2.3. Serializable Isolation Level">Section 13.2.3</a> and
  522. <a class="xref" href="applevel-consistency.html#SERIALIZABLE-CONSISTENCY" title="13.4.1. Enforcing Consistency with Serializable Transactions">Section 13.4.1</a> for details.)
  523. An attempt to set a transaction to the serializable isolation level in
  524. hot standby mode will generate an error.
  525. </p></li></ul></div><p>
  526. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="log-shipping-alternative.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="high-availability.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="monitoring.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">26.4. Alternative Method for Log Shipping </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 27. Monitoring Database Activity</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1