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.

590 line
45KB

  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>24.1. Routine Vacuuming</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="maintenance.html" title="Chapter 24. Routine Database Maintenance Tasks" /><link rel="next" href="routine-reindex.html" title="24.2. Routine Reindexing" /></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">24.1. Routine Vacuuming</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="maintenance.html" title="Chapter 24. Routine Database Maintenance Tasks">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="maintenance.html" title="Chapter 24. Routine Database Maintenance Tasks">Up</a></td><th width="60%" align="center">Chapter 24. Routine Database Maintenance Tasks</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="routine-reindex.html" title="24.2. Routine Reindexing">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="ROUTINE-VACUUMING"><div class="titlepage"><div><div><h2 class="title" style="clear: both">24.1. Routine Vacuuming</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="routine-vacuuming.html#VACUUM-BASICS">24.1.1. Vacuuming Basics</a></span></dt><dt><span class="sect2"><a href="routine-vacuuming.html#VACUUM-FOR-SPACE-RECOVERY">24.1.2. Recovering Disk Space</a></span></dt><dt><span class="sect2"><a href="routine-vacuuming.html#VACUUM-FOR-STATISTICS">24.1.3. Updating Planner Statistics</a></span></dt><dt><span class="sect2"><a href="routine-vacuuming.html#VACUUM-FOR-VISIBILITY-MAP">24.1.4. Updating the Visibility Map</a></span></dt><dt><span class="sect2"><a href="routine-vacuuming.html#VACUUM-FOR-WRAPAROUND">24.1.5. Preventing Transaction ID Wraparound Failures</a></span></dt><dt><span class="sect2"><a href="routine-vacuuming.html#AUTOVACUUM">24.1.6. The Autovacuum Daemon</a></span></dt></dl></div><a id="id-1.6.11.10.2" class="indexterm"></a><p>
  3. <span class="productname">PostgreSQL</span> databases require periodic
  4. maintenance known as <em class="firstterm">vacuuming</em>. For many installations, it
  5. is sufficient to let vacuuming be performed by the <em class="firstterm">autovacuum
  6. daemon</em>, which is described in <a class="xref" href="routine-vacuuming.html#AUTOVACUUM" title="24.1.6. The Autovacuum Daemon">Section 24.1.6</a>. You might
  7. need to adjust the autovacuuming parameters described there to obtain best
  8. results for your situation. Some database administrators will want to
  9. supplement or replace the daemon's activities with manually-managed
  10. <code class="command">VACUUM</code> commands, which typically are executed according to a
  11. schedule by <span class="application">cron</span> or <span class="application">Task
  12. Scheduler</span> scripts. To set up manually-managed vacuuming properly,
  13. it is essential to understand the issues discussed in the next few
  14. subsections. Administrators who rely on autovacuuming may still wish
  15. to skim this material to help them understand and adjust autovacuuming.
  16. </p><div class="sect2" id="VACUUM-BASICS"><div class="titlepage"><div><div><h3 class="title">24.1.1. Vacuuming Basics</h3></div></div></div><p>
  17. <span class="productname">PostgreSQL</span>'s
  18. <a class="xref" href="sql-vacuum.html" title="VACUUM"><span class="refentrytitle">VACUUM</span></a> command has to
  19. process each table on a regular basis for several reasons:
  20. </p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">To recover or reuse disk space occupied by updated or deleted
  21. rows.</li><li class="listitem">To update data statistics used by the
  22. <span class="productname">PostgreSQL</span> query planner.</li><li class="listitem">To update the visibility map, which speeds
  23. up <a class="link" href="indexes-index-only-scans.html" title="11.9. Index-Only Scans and Covering Indexes">index-only
  24. scans</a>.</li><li class="listitem">To protect against loss of very old data due to
  25. <em class="firstterm">transaction ID wraparound</em> or
  26. <em class="firstterm">multixact ID wraparound</em>.</li></ol></div><p>
  27. Each of these reasons dictates performing <code class="command">VACUUM</code> operations
  28. of varying frequency and scope, as explained in the following subsections.
  29. </p><p>
  30. There are two variants of <code class="command">VACUUM</code>: standard <code class="command">VACUUM</code>
  31. and <code class="command">VACUUM FULL</code>. <code class="command">VACUUM FULL</code> can reclaim more
  32. disk space but runs much more slowly. Also,
  33. the standard form of <code class="command">VACUUM</code> can run in parallel with production
  34. database operations. (Commands such as <code class="command">SELECT</code>,
  35. <code class="command">INSERT</code>, <code class="command">UPDATE</code>, and
  36. <code class="command">DELETE</code> will continue to function normally, though you
  37. will not be able to modify the definition of a table with commands such as
  38. <code class="command">ALTER TABLE</code> while it is being vacuumed.)
  39. <code class="command">VACUUM FULL</code> requires exclusive lock on the table it is
  40. working on, and therefore cannot be done in parallel with other use
  41. of the table. Generally, therefore,
  42. administrators should strive to use standard <code class="command">VACUUM</code> and
  43. avoid <code class="command">VACUUM FULL</code>.
  44. </p><p>
  45. <code class="command">VACUUM</code> creates a substantial amount of I/O
  46. traffic, which can cause poor performance for other active sessions.
  47. There are configuration parameters that can be adjusted to reduce the
  48. performance impact of background vacuuming — see
  49. <a class="xref" href="runtime-config-resource.html#RUNTIME-CONFIG-RESOURCE-VACUUM-COST" title="19.4.4. Cost-based Vacuum Delay">Section 19.4.4</a>.
  50. </p></div><div class="sect2" id="VACUUM-FOR-SPACE-RECOVERY"><div class="titlepage"><div><div><h3 class="title">24.1.2. Recovering Disk Space</h3></div></div></div><a id="id-1.6.11.10.5.2" class="indexterm"></a><p>
  51. In <span class="productname">PostgreSQL</span>, an
  52. <code class="command">UPDATE</code> or <code class="command">DELETE</code> of a row does not
  53. immediately remove the old version of the row.
  54. This approach is necessary to gain the benefits of multiversion
  55. concurrency control (<acronym class="acronym">MVCC</acronym>, see <a class="xref" href="mvcc.html" title="Chapter 13. Concurrency Control">Chapter 13</a>): the row version
  56. must not be deleted while it is still potentially visible to other
  57. transactions. But eventually, an outdated or deleted row version is no
  58. longer of interest to any transaction. The space it occupies must then be
  59. reclaimed for reuse by new rows, to avoid unbounded growth of disk
  60. space requirements. This is done by running <code class="command">VACUUM</code>.
  61. </p><p>
  62. The standard form of <code class="command">VACUUM</code> removes dead row
  63. versions in tables and indexes and marks the space available for
  64. future reuse. However, it will not return the space to the operating
  65. system, except in the special case where one or more pages at the
  66. end of a table become entirely free and an exclusive table lock can be
  67. easily obtained. In contrast, <code class="command">VACUUM FULL</code> actively compacts
  68. tables by writing a complete new version of the table file with no dead
  69. space. This minimizes the size of the table, but can take a long time.
  70. It also requires extra disk space for the new copy of the table, until
  71. the operation completes.
  72. </p><p>
  73. The usual goal of routine vacuuming is to do standard <code class="command">VACUUM</code>s
  74. often enough to avoid needing <code class="command">VACUUM FULL</code>. The
  75. autovacuum daemon attempts to work this way, and in fact will
  76. never issue <code class="command">VACUUM FULL</code>. In this approach, the idea
  77. is not to keep tables at their minimum size, but to maintain steady-state
  78. usage of disk space: each table occupies space equivalent to its
  79. minimum size plus however much space gets used up between vacuumings.
  80. Although <code class="command">VACUUM FULL</code> can be used to shrink a table back
  81. to its minimum size and return the disk space to the operating system,
  82. there is not much point in this if the table will just grow again in the
  83. future. Thus, moderately-frequent standard <code class="command">VACUUM</code> runs are a
  84. better approach than infrequent <code class="command">VACUUM FULL</code> runs for
  85. maintaining heavily-updated tables.
  86. </p><p>
  87. Some administrators prefer to schedule vacuuming themselves, for example
  88. doing all the work at night when load is low.
  89. The difficulty with doing vacuuming according to a fixed schedule
  90. is that if a table has an unexpected spike in update activity, it may
  91. get bloated to the point that <code class="command">VACUUM FULL</code> is really necessary
  92. to reclaim space. Using the autovacuum daemon alleviates this problem,
  93. since the daemon schedules vacuuming dynamically in response to update
  94. activity. It is unwise to disable the daemon completely unless you
  95. have an extremely predictable workload. One possible compromise is
  96. to set the daemon's parameters so that it will only react to unusually
  97. heavy update activity, thus keeping things from getting out of hand,
  98. while scheduled <code class="command">VACUUM</code>s are expected to do the bulk of the
  99. work when the load is typical.
  100. </p><p>
  101. For those not using autovacuum, a typical approach is to schedule a
  102. database-wide <code class="command">VACUUM</code> once a day during a low-usage period,
  103. supplemented by more frequent vacuuming of heavily-updated tables as
  104. necessary. (Some installations with extremely high update rates vacuum
  105. their busiest tables as often as once every few minutes.) If you have
  106. multiple databases in a cluster, don't forget to
  107. <code class="command">VACUUM</code> each one; the program <a class="xref" href="app-vacuumdb.html" title="vacuumdb"><span class="refentrytitle"><span class="application">vacuumdb</span></span></a> might be helpful.
  108. </p><div class="tip"><h3 class="title">Tip</h3><p>
  109. Plain <code class="command">VACUUM</code> may not be satisfactory when
  110. a table contains large numbers of dead row versions as a result of
  111. massive update or delete activity. If you have such a table and
  112. you need to reclaim the excess disk space it occupies, you will need
  113. to use <code class="command">VACUUM FULL</code>, or alternatively
  114. <a class="xref" href="sql-cluster.html" title="CLUSTER"><span class="refentrytitle">CLUSTER</span></a>
  115. or one of the table-rewriting variants of
  116. <a class="xref" href="sql-altertable.html" title="ALTER TABLE"><span class="refentrytitle">ALTER TABLE</span></a>.
  117. These commands rewrite an entire new copy of the table and build
  118. new indexes for it. All these options require exclusive lock. Note that
  119. they also temporarily use extra disk space approximately equal to the size
  120. of the table, since the old copies of the table and indexes can't be
  121. released until the new ones are complete.
  122. </p></div><div class="tip"><h3 class="title">Tip</h3><p>
  123. If you have a table whose entire contents are deleted on a periodic
  124. basis, consider doing it with
  125. <a class="xref" href="sql-truncate.html" title="TRUNCATE"><span class="refentrytitle">TRUNCATE</span></a> rather
  126. than using <code class="command">DELETE</code> followed by
  127. <code class="command">VACUUM</code>. <code class="command">TRUNCATE</code> removes the
  128. entire content of the table immediately, without requiring a
  129. subsequent <code class="command">VACUUM</code> or <code class="command">VACUUM
  130. FULL</code> to reclaim the now-unused disk space.
  131. The disadvantage is that strict MVCC semantics are violated.
  132. </p></div></div><div class="sect2" id="VACUUM-FOR-STATISTICS"><div class="titlepage"><div><div><h3 class="title">24.1.3. Updating Planner Statistics</h3></div></div></div><a id="id-1.6.11.10.6.2" class="indexterm"></a><a id="id-1.6.11.10.6.3" class="indexterm"></a><p>
  133. The <span class="productname">PostgreSQL</span> query planner relies on
  134. statistical information about the contents of tables in order to
  135. generate good plans for queries. These statistics are gathered by
  136. the <a class="xref" href="sql-analyze.html" title="ANALYZE"><span class="refentrytitle">ANALYZE</span></a> command,
  137. which can be invoked by itself or
  138. as an optional step in <code class="command">VACUUM</code>. It is important to have
  139. reasonably accurate statistics, otherwise poor choices of plans might
  140. degrade database performance.
  141. </p><p>
  142. The autovacuum daemon, if enabled, will automatically issue
  143. <code class="command">ANALYZE</code> commands whenever the content of a table has
  144. changed sufficiently. However, administrators might prefer to rely
  145. on manually-scheduled <code class="command">ANALYZE</code> operations, particularly
  146. if it is known that update activity on a table will not affect the
  147. statistics of <span class="quote">“<span class="quote">interesting</span>”</span> columns. The daemon schedules
  148. <code class="command">ANALYZE</code> strictly as a function of the number of rows
  149. inserted or updated; it has no knowledge of whether that will lead
  150. to meaningful statistical changes.
  151. </p><p>
  152. As with vacuuming for space recovery, frequent updates of statistics
  153. are more useful for heavily-updated tables than for seldom-updated
  154. ones. But even for a heavily-updated table, there might be no need for
  155. statistics updates if the statistical distribution of the data is
  156. not changing much. A simple rule of thumb is to think about how much
  157. the minimum and maximum values of the columns in the table change.
  158. For example, a <code class="type">timestamp</code> column that contains the time
  159. of row update will have a constantly-increasing maximum value as
  160. rows are added and updated; such a column will probably need more
  161. frequent statistics updates than, say, a column containing URLs for
  162. pages accessed on a website. The URL column might receive changes just
  163. as often, but the statistical distribution of its values probably
  164. changes relatively slowly.
  165. </p><p>
  166. It is possible to run <code class="command">ANALYZE</code> on specific tables and even
  167. just specific columns of a table, so the flexibility exists to update some
  168. statistics more frequently than others if your application requires it.
  169. In practice, however, it is usually best to just analyze the entire
  170. database, because it is a fast operation. <code class="command">ANALYZE</code> uses a
  171. statistically random sampling of the rows of a table rather than reading
  172. every single row.
  173. </p><div class="tip"><h3 class="title">Tip</h3><p>
  174. Although per-column tweaking of <code class="command">ANALYZE</code> frequency might not be
  175. very productive, you might find it worthwhile to do per-column
  176. adjustment of the level of detail of the statistics collected by
  177. <code class="command">ANALYZE</code>. Columns that are heavily used in <code class="literal">WHERE</code>
  178. clauses and have highly irregular data distributions might require a
  179. finer-grain data histogram than other columns. See <code class="command">ALTER TABLE
  180. SET STATISTICS</code>, or change the database-wide default using the <a class="xref" href="runtime-config-query.html#GUC-DEFAULT-STATISTICS-TARGET">default_statistics_target</a> configuration parameter.
  181. </p><p>
  182. Also, by default there is limited information available about
  183. the selectivity of functions. However, if you create an expression
  184. index that uses a function call, useful statistics will be
  185. gathered about the function, which can greatly improve query
  186. plans that use the expression index.
  187. </p></div><div class="tip"><h3 class="title">Tip</h3><p>
  188. The autovacuum daemon does not issue <code class="command">ANALYZE</code> commands for
  189. foreign tables, since it has no means of determining how often that
  190. might be useful. If your queries require statistics on foreign tables
  191. for proper planning, it's a good idea to run manually-managed
  192. <code class="command">ANALYZE</code> commands on those tables on a suitable schedule.
  193. </p></div></div><div class="sect2" id="VACUUM-FOR-VISIBILITY-MAP"><div class="titlepage"><div><div><h3 class="title">24.1.4. Updating the Visibility Map</h3></div></div></div><p>
  194. Vacuum maintains a <a class="link" href="storage-vm.html" title="68.4. Visibility Map">visibility map</a> for each
  195. table to keep track of which pages contain only tuples that are known to be
  196. visible to all active transactions (and all future transactions, until the
  197. page is again modified). This has two purposes. First, vacuum
  198. itself can skip such pages on the next run, since there is nothing to
  199. clean up.
  200. </p><p>
  201. Second, it allows <span class="productname">PostgreSQL</span> to answer some
  202. queries using only the index, without reference to the underlying table.
  203. Since <span class="productname">PostgreSQL</span> indexes don't contain tuple
  204. visibility information, a normal index scan fetches the heap tuple for each
  205. matching index entry, to check whether it should be seen by the current
  206. transaction.
  207. An <a class="link" href="indexes-index-only-scans.html" title="11.9. Index-Only Scans and Covering Indexes"><em class="firstterm">index-only
  208. scan</em></a>, on the other hand, checks the visibility map first.
  209. If it's known that all tuples on the page are
  210. visible, the heap fetch can be skipped. This is most useful on
  211. large data sets where the visibility map can prevent disk accesses.
  212. The visibility map is vastly smaller than the heap, so it can easily be
  213. cached even when the heap is very large.
  214. </p></div><div class="sect2" id="VACUUM-FOR-WRAPAROUND"><div class="titlepage"><div><div><h3 class="title">24.1.5. Preventing Transaction ID Wraparound Failures</h3></div></div></div><a id="id-1.6.11.10.8.2" class="indexterm"></a><a id="id-1.6.11.10.8.3" class="indexterm"></a><p>
  215. <span class="productname">PostgreSQL</span>'s
  216. <a class="link" href="mvcc-intro.html" title="13.1. Introduction">MVCC</a> transaction semantics
  217. depend on being able to compare transaction ID (<acronym class="acronym">XID</acronym>)
  218. numbers: a row version with an insertion XID greater than the current
  219. transaction's XID is <span class="quote">“<span class="quote">in the future</span>”</span> and should not be visible
  220. to the current transaction. But since transaction IDs have limited size
  221. (32 bits) a cluster that runs for a long time (more
  222. than 4 billion transactions) would suffer <em class="firstterm">transaction ID
  223. wraparound</em>: the XID counter wraps around to zero, and all of a sudden
  224. transactions that were in the past appear to be in the future — which
  225. means their output become invisible. In short, catastrophic data loss.
  226. (Actually the data is still there, but that's cold comfort if you cannot
  227. get at it.) To avoid this, it is necessary to vacuum every table
  228. in every database at least once every two billion transactions.
  229. </p><p>
  230. The reason that periodic vacuuming solves the problem is that
  231. <code class="command">VACUUM</code> will mark rows as <span class="emphasis"><em>frozen</em></span>, indicating that
  232. they were inserted by a transaction that committed sufficiently far in
  233. the past that the effects of the inserting transaction are certain to be
  234. visible to all current and future transactions.
  235. Normal XIDs are
  236. compared using modulo-2<sup>32</sup> arithmetic. This means
  237. that for every normal XID, there are two billion XIDs that are
  238. <span class="quote">“<span class="quote">older</span>”</span> and two billion that are <span class="quote">“<span class="quote">newer</span>”</span>; another
  239. way to say it is that the normal XID space is circular with no
  240. endpoint. Therefore, once a row version has been created with a particular
  241. normal XID, the row version will appear to be <span class="quote">“<span class="quote">in the past</span>”</span> for
  242. the next two billion transactions, no matter which normal XID we are
  243. talking about. If the row version still exists after more than two billion
  244. transactions, it will suddenly appear to be in the future. To
  245. prevent this, <span class="productname">PostgreSQL</span> reserves a special XID,
  246. <code class="literal">FrozenTransactionId</code>, which does not follow the normal XID
  247. comparison rules and is always considered older
  248. than every normal XID.
  249. Frozen row versions are treated as if the inserting XID were
  250. <code class="literal">FrozenTransactionId</code>, so that they will appear to be
  251. <span class="quote">“<span class="quote">in the past</span>”</span> to all normal transactions regardless of wraparound
  252. issues, and so such row versions will be valid until deleted, no matter
  253. how long that is.
  254. </p><div class="note"><h3 class="title">Note</h3><p>
  255. In <span class="productname">PostgreSQL</span> versions before 9.4, freezing was
  256. implemented by actually replacing a row's insertion XID
  257. with <code class="literal">FrozenTransactionId</code>, which was visible in the
  258. row's <code class="structname">xmin</code> system column. Newer versions just set a flag
  259. bit, preserving the row's original <code class="structname">xmin</code> for possible
  260. forensic use. However, rows with <code class="structname">xmin</code> equal
  261. to <code class="literal">FrozenTransactionId</code> (2) may still be found
  262. in databases <span class="application">pg_upgrade</span>'d from pre-9.4 versions.
  263. </p><p>
  264. Also, system catalogs may contain rows with <code class="structname">xmin</code> equal
  265. to <code class="literal">BootstrapTransactionId</code> (1), indicating that they were
  266. inserted during the first phase of <span class="application">initdb</span>.
  267. Like <code class="literal">FrozenTransactionId</code>, this special XID is treated as
  268. older than every normal XID.
  269. </p></div><p>
  270. <a class="xref" href="runtime-config-client.html#GUC-VACUUM-FREEZE-MIN-AGE">vacuum_freeze_min_age</a>
  271. controls how old an XID value has to be before rows bearing that XID will be
  272. frozen. Increasing this setting may avoid unnecessary work if the
  273. rows that would otherwise be frozen will soon be modified again,
  274. but decreasing this setting increases
  275. the number of transactions that can elapse before the table must be
  276. vacuumed again.
  277. </p><p>
  278. <code class="command">VACUUM</code> uses the <a class="link" href="storage-vm.html" title="68.4. Visibility Map">visibility map</a>
  279. to determine which pages of a table must be scanned. Normally, it
  280. will skip pages that don't have any dead row versions even if those pages
  281. might still have row versions with old XID values. Therefore, normal
  282. <code class="command">VACUUM</code>s won't always freeze every old row version in the table.
  283. Periodically, <code class="command">VACUUM</code> will perform an <em class="firstterm">aggressive
  284. vacuum</em>, skipping only those pages which contain neither dead rows nor
  285. any unfrozen XID or MXID values.
  286. <a class="xref" href="runtime-config-client.html#GUC-VACUUM-FREEZE-TABLE-AGE">vacuum_freeze_table_age</a>
  287. controls when <code class="command">VACUUM</code> does that: all-visible but not all-frozen
  288. pages are scanned if the number of transactions that have passed since the
  289. last such scan is greater than <code class="varname">vacuum_freeze_table_age</code> minus
  290. <code class="varname">vacuum_freeze_min_age</code>. Setting
  291. <code class="varname">vacuum_freeze_table_age</code> to 0 forces <code class="command">VACUUM</code> to
  292. use this more aggressive strategy for all scans.
  293. </p><p>
  294. The maximum time that a table can go unvacuumed is two billion
  295. transactions minus the <code class="varname">vacuum_freeze_min_age</code> value at
  296. the time of the last aggressive vacuum. If it were to go
  297. unvacuumed for longer than
  298. that, data loss could result. To ensure that this does not happen,
  299. autovacuum is invoked on any table that might contain unfrozen rows with
  300. XIDs older than the age specified by the configuration parameter <a class="xref" href="runtime-config-autovacuum.html#GUC-AUTOVACUUM-FREEZE-MAX-AGE">autovacuum_freeze_max_age</a>. (This will happen even if
  301. autovacuum is disabled.)
  302. </p><p>
  303. This implies that if a table is not otherwise vacuumed,
  304. autovacuum will be invoked on it approximately once every
  305. <code class="varname">autovacuum_freeze_max_age</code> minus
  306. <code class="varname">vacuum_freeze_min_age</code> transactions.
  307. For tables that are regularly vacuumed for space reclamation purposes,
  308. this is of little importance. However, for static tables
  309. (including tables that receive inserts, but no updates or deletes),
  310. there is no need to vacuum for space reclamation, so it can
  311. be useful to try to maximize the interval between forced autovacuums
  312. on very large static tables. Obviously one can do this either by
  313. increasing <code class="varname">autovacuum_freeze_max_age</code> or decreasing
  314. <code class="varname">vacuum_freeze_min_age</code>.
  315. </p><p>
  316. The effective maximum for <code class="varname">vacuum_freeze_table_age</code> is 0.95 *
  317. <code class="varname">autovacuum_freeze_max_age</code>; a setting higher than that will be
  318. capped to the maximum. A value higher than
  319. <code class="varname">autovacuum_freeze_max_age</code> wouldn't make sense because an
  320. anti-wraparound autovacuum would be triggered at that point anyway, and
  321. the 0.95 multiplier leaves some breathing room to run a manual
  322. <code class="command">VACUUM</code> before that happens. As a rule of thumb,
  323. <code class="command">vacuum_freeze_table_age</code> should be set to a value somewhat
  324. below <code class="varname">autovacuum_freeze_max_age</code>, leaving enough gap so that
  325. a regularly scheduled <code class="command">VACUUM</code> or an autovacuum triggered by
  326. normal delete and update activity is run in that window. Setting it too
  327. close could lead to anti-wraparound autovacuums, even though the table
  328. was recently vacuumed to reclaim space, whereas lower values lead to more
  329. frequent aggressive vacuuming.
  330. </p><p>
  331. The sole disadvantage of increasing <code class="varname">autovacuum_freeze_max_age</code>
  332. (and <code class="varname">vacuum_freeze_table_age</code> along with it) is that
  333. the <code class="filename">pg_xact</code> and <code class="filename">pg_commit_ts</code>
  334. subdirectories of the database cluster will take more space, because it
  335. must store the commit status and (if <code class="varname">track_commit_timestamp</code> is
  336. enabled) timestamp of all transactions back to
  337. the <code class="varname">autovacuum_freeze_max_age</code> horizon. The commit status uses
  338. two bits per transaction, so if
  339. <code class="varname">autovacuum_freeze_max_age</code> is set to its maximum allowed value
  340. of two billion, <code class="filename">pg_xact</code> can be expected to grow to about half
  341. a gigabyte and <code class="filename">pg_commit_ts</code> to about 20GB. If this
  342. is trivial compared to your total database size,
  343. setting <code class="varname">autovacuum_freeze_max_age</code> to its maximum allowed value
  344. is recommended. Otherwise, set it depending on what you are willing to
  345. allow for <code class="filename">pg_xact</code> and <code class="filename">pg_commit_ts</code> storage.
  346. (The default, 200 million transactions, translates to about 50MB
  347. of <code class="filename">pg_xact</code> storage and about 2GB of <code class="filename">pg_commit_ts</code>
  348. storage.)
  349. </p><p>
  350. One disadvantage of decreasing <code class="varname">vacuum_freeze_min_age</code> is that
  351. it might cause <code class="command">VACUUM</code> to do useless work: freezing a row
  352. version is a waste of time if the row is modified
  353. soon thereafter (causing it to acquire a new XID). So the setting should
  354. be large enough that rows are not frozen until they are unlikely to change
  355. any more.
  356. </p><p>
  357. To track the age of the oldest unfrozen XIDs in a database,
  358. <code class="command">VACUUM</code> stores XID
  359. statistics in the system tables <code class="structname">pg_class</code> and
  360. <code class="structname">pg_database</code>. In particular,
  361. the <code class="structfield">relfrozenxid</code> column of a table's
  362. <code class="structname">pg_class</code> row contains the freeze cutoff XID that was used
  363. by the last aggressive <code class="command">VACUUM</code> for that table. All rows
  364. inserted by transactions with XIDs older than this cutoff XID are
  365. guaranteed to have been frozen. Similarly,
  366. the <code class="structfield">datfrozenxid</code> column of a database's
  367. <code class="structname">pg_database</code> row is a lower bound on the unfrozen XIDs
  368. appearing in that database — it is just the minimum of the
  369. per-table <code class="structfield">relfrozenxid</code> values within the database.
  370. A convenient way to
  371. examine this information is to execute queries such as:
  372. </p><pre class="programlisting">
  373. SELECT c.oid::regclass as table_name,
  374. greatest(age(c.relfrozenxid),age(t.relfrozenxid)) as age
  375. FROM pg_class c
  376. LEFT JOIN pg_class t ON c.reltoastrelid = t.oid
  377. WHERE c.relkind IN ('r', 'm');
  378. SELECT datname, age(datfrozenxid) FROM pg_database;
  379. </pre><p>
  380. The <code class="literal">age</code> column measures the number of transactions from the
  381. cutoff XID to the current transaction's XID.
  382. </p><p>
  383. <code class="command">VACUUM</code> normally only scans pages that have been modified
  384. since the last vacuum, but <code class="structfield">relfrozenxid</code> can only be
  385. advanced when every page of the table
  386. that might contain unfrozen XIDs is scanned. This happens when
  387. <code class="structfield">relfrozenxid</code> is more than
  388. <code class="varname">vacuum_freeze_table_age</code> transactions old, when
  389. <code class="command">VACUUM</code>'s <code class="literal">FREEZE</code> option is used, or when all
  390. pages that are not already all-frozen happen to
  391. require vacuuming to remove dead row versions. When <code class="command">VACUUM</code>
  392. scans every page in the table that is not already all-frozen, it should
  393. set <code class="literal">age(relfrozenxid)</code> to a value just a little more than the
  394. <code class="varname">vacuum_freeze_min_age</code> setting
  395. that was used (more by the number of transactions started since the
  396. <code class="command">VACUUM</code> started). If no <code class="structfield">relfrozenxid</code>-advancing
  397. <code class="command">VACUUM</code> is issued on the table until
  398. <code class="varname">autovacuum_freeze_max_age</code> is reached, an autovacuum will soon
  399. be forced for the table.
  400. </p><p>
  401. If for some reason autovacuum fails to clear old XIDs from a table, the
  402. system will begin to emit warning messages like this when the database's
  403. oldest XIDs reach eleven million transactions from the wraparound point:
  404. </p><pre class="programlisting">
  405. WARNING: database "mydb" must be vacuumed within 10985967 transactions
  406. HINT: To avoid a database shutdown, execute a database-wide VACUUM in that database.
  407. </pre><p>
  408. (A manual <code class="command">VACUUM</code> should fix the problem, as suggested by the
  409. hint; but note that the <code class="command">VACUUM</code> must be performed by a
  410. superuser, else it will fail to process system catalogs and thus not
  411. be able to advance the database's <code class="structfield">datfrozenxid</code>.)
  412. If these warnings are
  413. ignored, the system will shut down and refuse to start any new
  414. transactions once there are fewer than 1 million transactions left
  415. until wraparound:
  416. </p><pre class="programlisting">
  417. ERROR: database is not accepting commands to avoid wraparound data loss in database "mydb"
  418. HINT: Stop the postmaster and vacuum that database in single-user mode.
  419. </pre><p>
  420. The 1-million-transaction safety margin exists to let the
  421. administrator recover without data loss, by manually executing the
  422. required <code class="command">VACUUM</code> commands. However, since the system will not
  423. execute commands once it has gone into the safety shutdown mode,
  424. the only way to do this is to stop the server and start the server in single-user
  425. mode to execute <code class="command">VACUUM</code>. The shutdown mode is not enforced
  426. in single-user mode. See the <a class="xref" href="app-postgres.html" title="postgres"><span class="refentrytitle"><span class="application">postgres</span></span></a> reference
  427. page for details about using single-user mode.
  428. </p><div class="sect3" id="VACUUM-FOR-MULTIXACT-WRAPAROUND"><div class="titlepage"><div><div><h4 class="title">24.1.5.1. Multixacts and Wraparound</h4></div></div></div><a id="id-1.6.11.10.8.17.2" class="indexterm"></a><a id="id-1.6.11.10.8.17.3" class="indexterm"></a><p>
  429. <em class="firstterm">Multixact IDs</em> are used to support row locking by
  430. multiple transactions. Since there is only limited space in a tuple
  431. header to store lock information, that information is encoded as
  432. a <span class="quote">“<span class="quote">multiple transaction ID</span>”</span>, or multixact ID for short,
  433. whenever there is more than one transaction concurrently locking a
  434. row. Information about which transaction IDs are included in any
  435. particular multixact ID is stored separately in
  436. the <code class="filename">pg_multixact</code> subdirectory, and only the multixact ID
  437. appears in the <code class="structfield">xmax</code> field in the tuple header.
  438. Like transaction IDs, multixact IDs are implemented as a
  439. 32-bit counter and corresponding storage, all of which requires
  440. careful aging management, storage cleanup, and wraparound handling.
  441. There is a separate storage area which holds the list of members in
  442. each multixact, which also uses a 32-bit counter and which must also
  443. be managed.
  444. </p><p>
  445. Whenever <code class="command">VACUUM</code> scans any part of a table, it will replace
  446. any multixact ID it encounters which is older than
  447. <a class="xref" href="runtime-config-client.html#GUC-VACUUM-MULTIXACT-FREEZE-MIN-AGE">vacuum_multixact_freeze_min_age</a>
  448. by a different value, which can be the zero value, a single
  449. transaction ID, or a newer multixact ID. For each table,
  450. <code class="structname">pg_class</code>.<code class="structfield">relminmxid</code> stores the oldest
  451. possible multixact ID still appearing in any tuple of that table.
  452. If this value is older than
  453. <a class="xref" href="runtime-config-client.html#GUC-VACUUM-MULTIXACT-FREEZE-TABLE-AGE">vacuum_multixact_freeze_table_age</a>, an aggressive
  454. vacuum is forced. As discussed in the previous section, an aggressive
  455. vacuum means that only those pages which are known to be all-frozen will
  456. be skipped. <code class="function">mxid_age()</code> can be used on
  457. <code class="structname">pg_class</code>.<code class="structfield">relminmxid</code> to find its age.
  458. </p><p>
  459. Aggressive <code class="command">VACUUM</code> scans, regardless of
  460. what causes them, enable advancing the value for that table.
  461. Eventually, as all tables in all databases are scanned and their
  462. oldest multixact values are advanced, on-disk storage for older
  463. multixacts can be removed.
  464. </p><p>
  465. As a safety device, an aggressive vacuum scan will occur for any table
  466. whose multixact-age is greater than
  467. <a class="xref" href="runtime-config-autovacuum.html#GUC-AUTOVACUUM-MULTIXACT-FREEZE-MAX-AGE">autovacuum_multixact_freeze_max_age</a>. Aggressive
  468. vacuum scans will also occur progressively for all tables, starting with
  469. those that have the oldest multixact-age, if the amount of used member
  470. storage space exceeds the amount 50% of the addressable storage space.
  471. Both of these kinds of aggressive scans will occur even if autovacuum is
  472. nominally disabled.
  473. </p></div></div><div class="sect2" id="AUTOVACUUM"><div class="titlepage"><div><div><h3 class="title">24.1.6. The Autovacuum Daemon</h3></div></div></div><a id="id-1.6.11.10.9.2" class="indexterm"></a><p>
  474. <span class="productname">PostgreSQL</span> has an optional but highly
  475. recommended feature called <em class="firstterm">autovacuum</em>,
  476. whose purpose is to automate the execution of
  477. <code class="command">VACUUM</code> and <code class="command">ANALYZE </code> commands.
  478. When enabled, autovacuum checks for
  479. tables that have had a large number of inserted, updated or deleted
  480. tuples. These checks use the statistics collection facility;
  481. therefore, autovacuum cannot be used unless <a class="xref" href="runtime-config-statistics.html#GUC-TRACK-COUNTS">track_counts</a> is set to <code class="literal">true</code>.
  482. In the default configuration, autovacuuming is enabled and the related
  483. configuration parameters are appropriately set.
  484. </p><p>
  485. The <span class="quote">“<span class="quote">autovacuum daemon</span>”</span> actually consists of multiple processes.
  486. There is a persistent daemon process, called the
  487. <em class="firstterm">autovacuum launcher</em>, which is in charge of starting
  488. <em class="firstterm">autovacuum worker</em> processes for all databases. The
  489. launcher will distribute the work across time, attempting to start one
  490. worker within each database every <a class="xref" href="runtime-config-autovacuum.html#GUC-AUTOVACUUM-NAPTIME">autovacuum_naptime</a>
  491. seconds. (Therefore, if the installation has <em class="replaceable"><code>N</code></em> databases,
  492. a new worker will be launched every
  493. <code class="varname">autovacuum_naptime</code>/<em class="replaceable"><code>N</code></em> seconds.)
  494. A maximum of <a class="xref" href="runtime-config-autovacuum.html#GUC-AUTOVACUUM-MAX-WORKERS">autovacuum_max_workers</a> worker processes
  495. are allowed to run at the same time. If there are more than
  496. <code class="varname">autovacuum_max_workers</code> databases to be processed,
  497. the next database will be processed as soon as the first worker finishes.
  498. Each worker process will check each table within its database and
  499. execute <code class="command">VACUUM</code> and/or <code class="command">ANALYZE</code> as needed.
  500. <a class="xref" href="runtime-config-autovacuum.html#GUC-LOG-AUTOVACUUM-MIN-DURATION">log_autovacuum_min_duration</a> can be set to monitor
  501. autovacuum workers' activity.
  502. </p><p>
  503. If several large tables all become eligible for vacuuming in a short
  504. amount of time, all autovacuum workers might become occupied with
  505. vacuuming those tables for a long period. This would result
  506. in other tables and databases not being vacuumed until a worker becomes
  507. available. There is no limit on how many workers might be in a
  508. single database, but workers do try to avoid repeating work that has
  509. already been done by other workers. Note that the number of running
  510. workers does not count towards <a class="xref" href="runtime-config-connection.html#GUC-MAX-CONNECTIONS">max_connections</a> or
  511. <a class="xref" href="runtime-config-connection.html#GUC-SUPERUSER-RESERVED-CONNECTIONS">superuser_reserved_connections</a> limits.
  512. </p><p>
  513. Tables whose <code class="structfield">relfrozenxid</code> value is more than
  514. <a class="xref" href="runtime-config-autovacuum.html#GUC-AUTOVACUUM-FREEZE-MAX-AGE">autovacuum_freeze_max_age</a> transactions old are always
  515. vacuumed (this also applies to those tables whose freeze max age has
  516. been modified via storage parameters; see below). Otherwise, if the
  517. number of tuples obsoleted since the last
  518. <code class="command">VACUUM</code> exceeds the <span class="quote">“<span class="quote">vacuum threshold</span>”</span>, the
  519. table is vacuumed. The vacuum threshold is defined as:
  520. </p><pre class="programlisting">
  521. vacuum threshold = vacuum base threshold + vacuum scale factor * number of tuples
  522. </pre><p>
  523. where the vacuum base threshold is
  524. <a class="xref" href="runtime-config-autovacuum.html#GUC-AUTOVACUUM-VACUUM-THRESHOLD">autovacuum_vacuum_threshold</a>,
  525. the vacuum scale factor is
  526. <a class="xref" href="runtime-config-autovacuum.html#GUC-AUTOVACUUM-VACUUM-SCALE-FACTOR">autovacuum_vacuum_scale_factor</a>,
  527. and the number of tuples is
  528. <code class="structname">pg_class</code>.<code class="structfield">reltuples</code>.
  529. The number of obsolete tuples is obtained from the statistics
  530. collector; it is a semi-accurate count updated by each
  531. <code class="command">UPDATE</code> and <code class="command">DELETE</code> operation. (It
  532. is only semi-accurate because some information might be lost under heavy
  533. load.) If the <code class="structfield">relfrozenxid</code> value of the table is more
  534. than <code class="varname">vacuum_freeze_table_age</code> transactions old, an aggressive
  535. vacuum is performed to freeze old tuples and advance
  536. <code class="structfield">relfrozenxid</code>; otherwise, only pages that have been modified
  537. since the last vacuum are scanned.
  538. </p><p>
  539. For analyze, a similar condition is used: the threshold, defined as:
  540. </p><pre class="programlisting">
  541. analyze threshold = analyze base threshold + analyze scale factor * number of tuples
  542. </pre><p>
  543. is compared to the total number of tuples inserted, updated, or deleted
  544. since the last <code class="command">ANALYZE</code>.
  545. </p><p>
  546. Temporary tables cannot be accessed by autovacuum. Therefore,
  547. appropriate vacuum and analyze operations should be performed via
  548. session SQL commands.
  549. </p><p>
  550. The default thresholds and scale factors are taken from
  551. <code class="filename">postgresql.conf</code>, but it is possible to override them
  552. (and many other autovacuum control parameters) on a per-table basis; see
  553. <a class="xref" href="sql-createtable.html#SQL-CREATETABLE-STORAGE-PARAMETERS" title="Storage Parameters">Storage Parameters</a> for more information.
  554. If a setting has been changed via a table's storage parameters, that value
  555. is used when processing that table; otherwise the global settings are
  556. used. See <a class="xref" href="runtime-config-autovacuum.html" title="19.10. Automatic Vacuuming">Section 19.10</a> for more details on
  557. the global settings.
  558. </p><p>
  559. When multiple workers are running, the autovacuum cost delay parameters
  560. (see <a class="xref" href="runtime-config-resource.html#RUNTIME-CONFIG-RESOURCE-VACUUM-COST" title="19.4.4. Cost-based Vacuum Delay">Section 19.4.4</a>) are
  561. <span class="quote">“<span class="quote">balanced</span>”</span> among all the running workers, so that the
  562. total I/O impact on the system is the same regardless of the number
  563. of workers actually running. However, any workers processing tables whose
  564. per-table <code class="literal">autovacuum_vacuum_cost_delay</code> or
  565. <code class="literal">autovacuum_vacuum_cost_limit</code> storage parameters have been set
  566. are not considered in the balancing algorithm.
  567. </p><p>
  568. Autovacuum workers generally don't block other commands. If a process
  569. attempts to acquire a lock that conflicts with the
  570. <code class="literal">SHARE UPDATE EXCLUSIVE</code> lock held by autovacuum, lock
  571. acquisition will interrupt the autovacuum. For conflicting lock modes,
  572. see <a class="xref" href="explicit-locking.html#TABLE-LOCK-COMPATIBILITY" title="Table 13.2.  Conflicting Lock Modes">Table 13.2</a>. However, if the autovacuum
  573. is running to prevent transaction ID wraparound (i.e., the autovacuum query
  574. name in the <code class="structname">pg_stat_activity</code> view ends with
  575. <code class="literal">(to prevent wraparound)</code>), the autovacuum is not
  576. automatically interrupted.
  577. </p><div class="warning"><h3 class="title">Warning</h3><p>
  578. Regularly running commands that acquire locks conflicting with a
  579. <code class="literal">SHARE UPDATE EXCLUSIVE</code> lock (e.g., ANALYZE) can
  580. effectively prevent autovacuums from ever completing.
  581. </p></div></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="maintenance.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="maintenance.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="routine-reindex.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 24. Routine Database Maintenance Tasks </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 24.2. Routine Reindexing</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1