gooderp18绿色标准版
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

271 lignes
20KB

  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>REINDEX</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="sql-refreshmaterializedview.html" title="REFRESH MATERIALIZED VIEW" /><link rel="next" href="sql-release-savepoint.html" title="RELEASE SAVEPOINT" /></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">REINDEX</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-refreshmaterializedview.html" title="REFRESH MATERIALIZED VIEW">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-release-savepoint.html" title="RELEASE SAVEPOINT">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-REINDEX"><div class="titlepage"></div><a id="id-1.9.3.162.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">REINDEX</span></h2><p>REINDEX — rebuild indexes</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. REINDEX [ ( VERBOSE ) ] { INDEX | TABLE | SCHEMA | DATABASE | SYSTEM } [ CONCURRENTLY ] <em class="replaceable"><code>name</code></em>
  4. </pre></div><div class="refsect1" id="id-1.9.3.162.5"><h2>Description</h2><p>
  5. <code class="command">REINDEX</code> rebuilds an index using the data
  6. stored in the index's table, replacing the old copy of the index. There are
  7. several scenarios in which to use <code class="command">REINDEX</code>:
  8. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  9. An index has become corrupted, and no longer contains valid
  10. data. Although in theory this should never happen, in
  11. practice indexes can become corrupted due to software bugs or
  12. hardware failures. <code class="command">REINDEX</code> provides a
  13. recovery method.
  14. </p></li><li class="listitem"><p>
  15. An index has become <span class="quote">“<span class="quote">bloated</span>”</span>, that is it contains many
  16. empty or nearly-empty pages. This can occur with B-tree indexes in
  17. <span class="productname">PostgreSQL</span> under certain uncommon access
  18. patterns. <code class="command">REINDEX</code> provides a way to reduce
  19. the space consumption of the index by writing a new version of
  20. the index without the dead pages. See <a class="xref" href="routine-reindex.html" title="24.2. Routine Reindexing">Section 24.2</a> for more information.
  21. </p></li><li class="listitem"><p>
  22. You have altered a storage parameter (such as fillfactor)
  23. for an index, and wish to ensure that the change has taken full effect.
  24. </p></li><li class="listitem"><p>
  25. If an index build fails with the <code class="literal">CONCURRENTLY</code> option,
  26. this index is left as <span class="quote">“<span class="quote">invalid</span>”</span>. Such indexes are useless
  27. but it can be convenient to use <code class="command">REINDEX</code> to rebuild
  28. them. Note that only <code class="command">REINDEX INDEX</code> is able
  29. to perform a concurrent build on an invalid index.
  30. </p></li></ul></div></div><div class="refsect1" id="id-1.9.3.162.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">INDEX</code></span></dt><dd><p>
  31. Recreate the specified index.
  32. </p></dd><dt><span class="term"><code class="literal">TABLE</code></span></dt><dd><p>
  33. Recreate all indexes of the specified table. If the table has a
  34. secondary <span class="quote">“<span class="quote">TOAST</span>”</span> table, that is reindexed as well.
  35. </p></dd><dt><span class="term"><code class="literal">SCHEMA</code></span></dt><dd><p>
  36. Recreate all indexes of the specified schema. If a table of this
  37. schema has a secondary <span class="quote">“<span class="quote">TOAST</span>”</span> table, that is reindexed as
  38. well. Indexes on shared system catalogs are also processed.
  39. This form of <code class="command">REINDEX</code> cannot be executed inside a
  40. transaction block.
  41. </p></dd><dt><span class="term"><code class="literal">DATABASE</code></span></dt><dd><p>
  42. Recreate all indexes within the current database.
  43. Indexes on shared system catalogs are also processed.
  44. This form of <code class="command">REINDEX</code> cannot be executed inside a
  45. transaction block.
  46. </p></dd><dt><span class="term"><code class="literal">SYSTEM</code></span></dt><dd><p>
  47. Recreate all indexes on system catalogs within the current database.
  48. Indexes on shared system catalogs are included.
  49. Indexes on user tables are not processed.
  50. This form of <code class="command">REINDEX</code> cannot be executed inside a
  51. transaction block.
  52. </p></dd><dt><span class="term"><em class="replaceable"><code>name</code></em></span></dt><dd><p>
  53. The name of the specific index, table, or database to be
  54. reindexed. Index and table names can be schema-qualified.
  55. Presently, <code class="command">REINDEX DATABASE</code> and <code class="command">REINDEX SYSTEM</code>
  56. can only reindex the current database, so their parameter must match
  57. the current database's name.
  58. </p></dd><dt><span class="term"><code class="literal">CONCURRENTLY</code></span></dt><dd><p>
  59. When this option is used, <span class="productname">PostgreSQL</span> will rebuild the
  60. index without taking any locks that prevent concurrent inserts,
  61. updates, or deletes on the table; whereas a standard index rebuild
  62. locks out writes (but not reads) on the table until it's done.
  63. There are several caveats to be aware of when using this option
  64. — see <a class="xref" href="sql-reindex.html#SQL-REINDEX-CONCURRENTLY" title="Rebuilding Indexes Concurrently">Rebuilding Indexes Concurrently</a>.
  65. </p><p>
  66. For temporary tables, <code class="command">REINDEX</code> is always
  67. non-concurrent, as no other session can access them, and
  68. non-concurrent reindex is cheaper.
  69. </p></dd><dt><span class="term"><code class="literal">VERBOSE</code></span></dt><dd><p>
  70. Prints a progress report as each index is reindexed.
  71. </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.162.7"><h2>Notes</h2><p>
  72. If you suspect corruption of an index on a user table, you can
  73. simply rebuild that index, or all indexes on the table, using
  74. <code class="command">REINDEX INDEX</code> or <code class="command">REINDEX TABLE</code>.
  75. </p><p>
  76. Things are more difficult if you need to recover from corruption of
  77. an index on a system table. In this case it's important for the
  78. system to not have used any of the suspect indexes itself.
  79. (Indeed, in this sort of scenario you might find that server
  80. processes are crashing immediately at start-up, due to reliance on
  81. the corrupted indexes.) To recover safely, the server must be started
  82. with the <code class="option">-P</code> option, which prevents it from using
  83. indexes for system catalog lookups.
  84. </p><p>
  85. One way to do this is to shut down the server and start a single-user
  86. <span class="productname">PostgreSQL</span> server
  87. with the <code class="option">-P</code> option included on its command line.
  88. Then, <code class="command">REINDEX DATABASE</code>, <code class="command">REINDEX SYSTEM</code>,
  89. <code class="command">REINDEX TABLE</code>, or <code class="command">REINDEX INDEX</code> can be
  90. issued, depending on how much you want to reconstruct. If in
  91. doubt, use <code class="command">REINDEX SYSTEM</code> to select
  92. reconstruction of all system indexes in the database. Then quit
  93. the single-user server session and restart the regular server.
  94. See the <a class="xref" href="app-postgres.html" title="postgres"><span class="refentrytitle"><span class="application">postgres</span></span></a> reference page for more
  95. information about how to interact with the single-user server
  96. interface.
  97. </p><p>
  98. Alternatively, a regular server session can be started with
  99. <code class="option">-P</code> included in its command line options.
  100. The method for doing this varies across clients, but in all
  101. <span class="application">libpq</span>-based clients, it is possible to set
  102. the <code class="envar">PGOPTIONS</code> environment variable to <code class="literal">-P</code>
  103. before starting the client. Note that while this method does not
  104. require locking out other clients, it might still be wise to prevent
  105. other users from connecting to the damaged database until repairs
  106. have been completed.
  107. </p><p>
  108. <code class="command">REINDEX</code> is similar to a drop and recreate of the index
  109. in that the index contents are rebuilt from scratch. However, the locking
  110. considerations are rather different. <code class="command">REINDEX</code> locks out writes
  111. but not reads of the index's parent table. It also takes an exclusive lock
  112. on the specific index being processed, which will block reads that attempt
  113. to use that index. In contrast, <code class="command">DROP INDEX</code> momentarily takes
  114. an exclusive lock on the parent table, blocking both writes and reads. The
  115. subsequent <code class="command">CREATE INDEX</code> locks out writes but not reads; since
  116. the index is not there, no read will attempt to use it, meaning that there
  117. will be no blocking but reads might be forced into expensive sequential
  118. scans.
  119. </p><p>
  120. Reindexing a single index or table requires being the owner of that
  121. index or table. Reindexing a schema or database requires being the
  122. owner of that schema or database. Note that is therefore sometimes
  123. possible for non-superusers to rebuild indexes of tables owned by
  124. other users. However, as a special exception, when
  125. <code class="command">REINDEX DATABASE</code>, <code class="command">REINDEX SCHEMA</code>
  126. or <code class="command">REINDEX SYSTEM</code> is issued by a non-superuser,
  127. indexes on shared catalogs will be skipped unless the user owns the
  128. catalog (which typically won't be the case). Of course, superusers
  129. can always reindex anything.
  130. </p><p>
  131. Reindexing partitioned tables or partitioned indexes is not supported.
  132. Each individual partition can be reindexed separately instead.
  133. </p><div class="refsect2" id="SQL-REINDEX-CONCURRENTLY"><h3>Rebuilding Indexes Concurrently</h3><a id="id-1.9.3.162.7.9.2" class="indexterm"></a><p>
  134. Rebuilding an index can interfere with regular operation of a database.
  135. Normally <span class="productname">PostgreSQL</span> locks the table whose index is rebuilt
  136. against writes and performs the entire index build with a single scan of the
  137. table. Other transactions can still read the table, but if they try to
  138. insert, update, or delete rows in the table they will block until the
  139. index rebuild is finished. This could have a severe effect if the system is
  140. a live production database. Very large tables can take many hours to be
  141. indexed, and even for smaller tables, an index rebuild can lock out writers
  142. for periods that are unacceptably long for a production system.
  143. </p><p>
  144. <span class="productname">PostgreSQL</span> supports rebuilding indexes with minimum locking
  145. of writes. This method is invoked by specifying the
  146. <code class="literal">CONCURRENTLY</code> option of <code class="command">REINDEX</code>. When this option
  147. is used, <span class="productname">PostgreSQL</span> must perform two scans of the table
  148. for each index that needs to be rebuilt and wait for termination of
  149. all existing transactions that could potentially use the index.
  150. This method requires more total work than a standard index
  151. rebuild and takes significantly longer to complete as it needs to wait
  152. for unfinished transactions that might modify the index. However, since
  153. it allows normal operations to continue while the index is being rebuilt, this
  154. method is useful for rebuilding indexes in a production environment. Of
  155. course, the extra CPU, memory and I/O load imposed by the index rebuild
  156. may slow down other operations.
  157. </p><p>
  158. The following steps occur in a concurrent reindex. Each step is run in a
  159. separate transaction. If there are multiple indexes to be rebuilt, then
  160. each step loops through all the indexes before moving to the next step.
  161. </p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"><p>
  162. A new temporary index definition is added to the catalog
  163. <code class="literal">pg_index</code>. This definition will be used to replace
  164. the old index. A <code class="literal">SHARE UPDATE EXCLUSIVE</code> lock at
  165. session level is taken on the indexes being reindexed as well as their
  166. associated tables to prevent any schema modification while processing.
  167. </p></li><li class="listitem"><p>
  168. A first pass to build the index is done for each new index. Once the
  169. index is built, its flag <code class="literal">pg_index.indisready</code> is
  170. switched to <span class="quote">“<span class="quote">true</span>”</span> to make it ready for inserts, making it
  171. visible to other sessions once the transaction that performed the build
  172. is finished. This step is done in a separate transaction for each
  173. index.
  174. </p></li><li class="listitem"><p>
  175. Then a second pass is performed to add tuples that were added while the
  176. first pass was running. This step is also done in a separate
  177. transaction for each index.
  178. </p></li><li class="listitem"><p>
  179. All the constraints that refer to the index are changed to refer to the
  180. new index definition, and the names of the indexes are changed. At
  181. this point, <code class="literal">pg_index.indisvalid</code> is switched to
  182. <span class="quote">“<span class="quote">true</span>”</span> for the new index and to <span class="quote">“<span class="quote">false</span>”</span> for
  183. the old, and a cache invalidation is done causing all sessions that
  184. referenced the old index to be invalidated.
  185. </p></li><li class="listitem"><p>
  186. The old indexes have <code class="literal">pg_index.indisready</code> switched to
  187. <span class="quote">“<span class="quote">false</span>”</span> to prevent any new tuple insertions, after waiting
  188. for running queries that might reference the old index to complete.
  189. </p></li><li class="listitem"><p>
  190. The old indexes are dropped. The <code class="literal">SHARE UPDATE
  191. EXCLUSIVE</code> session locks for the indexes and the table are
  192. released.
  193. </p></li></ol></div><p>
  194. </p><p>
  195. If a problem arises while rebuilding the indexes, such as a
  196. uniqueness violation in a unique index, the <code class="command">REINDEX</code>
  197. command will fail but leave behind an <span class="quote">“<span class="quote">invalid</span>”</span> new index in addition to
  198. the pre-existing one. This index will be ignored for querying purposes
  199. because it might be incomplete; however it will still consume update
  200. overhead. The <span class="application">psql</span> <code class="command">\d</code> command will report
  201. such an index as <code class="literal">INVALID</code>:
  202. </p><pre class="programlisting">
  203. postgres=# \d tab
  204. Table "public.tab"
  205. Column | Type | Modifiers
  206. --------+---------+-----------
  207. col | integer |
  208. Indexes:
  209. "idx" btree (col)
  210. "idx_ccnew" btree (col) INVALID
  211. </pre><p>
  212. The recommended recovery method in such cases is to drop the invalid index
  213. and try again to perform <code class="command">REINDEX CONCURRENTLY</code>. The
  214. concurrent index created during the processing has a name ending in the
  215. suffix <code class="literal">ccnew</code>, or <code class="literal">ccold</code> if it is an
  216. old index definition which we failed to drop. Invalid indexes can be
  217. dropped using <code class="literal">DROP INDEX</code>, including invalid toast
  218. indexes.
  219. </p><p>
  220. Regular index builds permit other regular index builds on the same table
  221. to occur simultaneously, but only one concurrent index build can occur on a
  222. table at a time. In both cases, no other types of schema modification on
  223. the table are allowed meanwhile. Another difference is that a regular
  224. <code class="command">REINDEX TABLE</code> or <code class="command">REINDEX INDEX</code>
  225. command can be performed within a transaction block, but <code class="command">REINDEX
  226. CONCURRENTLY</code> cannot.
  227. </p><p>
  228. <code class="command">REINDEX SYSTEM</code> does not support
  229. <code class="command">CONCURRENTLY</code> since system catalogs cannot be reindexed
  230. concurrently.
  231. </p><p>
  232. Furthermore, indexes for exclusion constraints cannot be reindexed
  233. concurrently. If such an index is named directly in this command, an
  234. error is raised. If a table or database with exclusion constraint indexes
  235. is reindexed concurrently, those indexes will be skipped. (It is possible
  236. to reindex such indexes without the <code class="command">CONCURRENTLY</code> option.)
  237. </p></div></div><div class="refsect1" id="id-1.9.3.162.8"><h2>Examples</h2><p>
  238. Rebuild a single index:
  239. </p><pre class="programlisting">
  240. REINDEX INDEX my_index;
  241. </pre><p>
  242. </p><p>
  243. Rebuild all the indexes on the table <code class="literal">my_table</code>:
  244. </p><pre class="programlisting">
  245. REINDEX TABLE my_table;
  246. </pre><p>
  247. </p><p>
  248. Rebuild all indexes in a particular database, without trusting the
  249. system indexes to be valid already:
  250. </p><pre class="programlisting">
  251. $ <strong class="userinput"><code>export PGOPTIONS="-P"</code></strong>
  252. $ <strong class="userinput"><code>psql broken_db</code></strong>
  253. ...
  254. broken_db=&gt; REINDEX DATABASE broken_db;
  255. broken_db=&gt; \q
  256. </pre><p>
  257. Rebuild indexes for a table, without blocking read and write operations
  258. on involved relations while reindexing is in progress:
  259. </p><pre class="programlisting">
  260. REINDEX TABLE CONCURRENTLY my_broken_table;
  261. </pre></div><div class="refsect1" id="id-1.9.3.162.9"><h2>Compatibility</h2><p>
  262. There is no <code class="command">REINDEX</code> command in the SQL standard.
  263. </p></div><div class="refsect1" id="id-1.9.3.162.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-createindex.html" title="CREATE INDEX"><span class="refentrytitle">CREATE INDEX</span></a>, <a class="xref" href="sql-dropindex.html" title="DROP INDEX"><span class="refentrytitle">DROP INDEX</span></a>, <a class="xref" href="app-reindexdb.html" title="reindexdb"><span class="refentrytitle"><span class="application">reindexdb</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="sql-refreshmaterializedview.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-release-savepoint.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">REFRESH MATERIALIZED VIEW </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> RELEASE SAVEPOINT</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1