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.

136 lines
12KB

  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>ALTER INDEX</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-altergroup.html" title="ALTER GROUP" /><link rel="next" href="sql-alterlanguage.html" title="ALTER LANGUAGE" /></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">ALTER INDEX</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-altergroup.html" title="ALTER GROUP">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-alterlanguage.html" title="ALTER LANGUAGE">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-ALTERINDEX"><div class="titlepage"></div><a id="id-1.9.3.16.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">ALTER INDEX</span></h2><p>ALTER INDEX — change the definition of an index</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. ALTER INDEX [ IF EXISTS ] <em class="replaceable"><code>name</code></em> RENAME TO <em class="replaceable"><code>new_name</code></em>
  4. ALTER INDEX [ IF EXISTS ] <em class="replaceable"><code>name</code></em> SET TABLESPACE <em class="replaceable"><code>tablespace_name</code></em>
  5. ALTER INDEX <em class="replaceable"><code>name</code></em> ATTACH PARTITION <em class="replaceable"><code>index_name</code></em>
  6. ALTER INDEX <em class="replaceable"><code>name</code></em> DEPENDS ON EXTENSION <em class="replaceable"><code>extension_name</code></em>
  7. ALTER INDEX [ IF EXISTS ] <em class="replaceable"><code>name</code></em> SET ( <em class="replaceable"><code>storage_parameter</code></em> [= <em class="replaceable"><code>value</code></em>] [, ... ] )
  8. ALTER INDEX [ IF EXISTS ] <em class="replaceable"><code>name</code></em> RESET ( <em class="replaceable"><code>storage_parameter</code></em> [, ... ] )
  9. ALTER INDEX [ IF EXISTS ] <em class="replaceable"><code>name</code></em> ALTER [ COLUMN ] <em class="replaceable"><code>column_number</code></em>
  10. SET STATISTICS <em class="replaceable"><code>integer</code></em>
  11. ALTER INDEX ALL IN TABLESPACE <em class="replaceable"><code>name</code></em> [ OWNED BY <em class="replaceable"><code>role_name</code></em> [, ... ] ]
  12. SET TABLESPACE <em class="replaceable"><code>new_tablespace</code></em> [ NOWAIT ]
  13. </pre></div><div class="refsect1" id="id-1.9.3.16.5"><h2>Description</h2><p>
  14. <code class="command">ALTER INDEX</code> changes the definition of an existing index.
  15. There are several subforms described below. Note that the lock level required
  16. may differ for each subform. An <code class="literal">ACCESS EXCLUSIVE</code> lock is held
  17. unless explicitly noted. When multiple subcommands are listed, the lock
  18. held will be the strictest one required from any subcommand.
  19. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">RENAME</code></span></dt><dd><p>
  20. The <code class="literal">RENAME</code> form changes the name of the index.
  21. If the index is associated with a table constraint (either
  22. <code class="literal">UNIQUE</code>, <code class="literal">PRIMARY KEY</code>,
  23. or <code class="literal">EXCLUDE</code>), the constraint is renamed as well.
  24. There is no effect on the stored data.
  25. </p><p>
  26. Renaming an index acquires a <code class="literal">SHARE UPDATE EXCLUSIVE</code>
  27. lock.
  28. </p></dd><dt><span class="term"><code class="literal">SET TABLESPACE</code></span></dt><dd><p>
  29. This form changes the index's tablespace to the specified tablespace and
  30. moves the data file(s) associated with the index to the new tablespace.
  31. To change the tablespace of an index, you must own the index and have
  32. <code class="literal">CREATE</code> privilege on the new tablespace.
  33. All indexes in the current database in a tablespace can be moved by using
  34. the <code class="literal">ALL IN TABLESPACE</code> form, which will lock all
  35. indexes to be moved and then move each one. This form also supports
  36. <code class="literal">OWNED BY</code>, which will only move indexes owned by the
  37. roles specified. If the <code class="literal">NOWAIT</code> option is specified
  38. then the command will fail if it is unable to acquire all of the locks
  39. required immediately. Note that system catalogs will not be moved by
  40. this command, use <code class="command">ALTER DATABASE</code> or explicit
  41. <code class="command">ALTER INDEX</code> invocations instead if desired.
  42. See also
  43. <a class="xref" href="sql-createtablespace.html" title="CREATE TABLESPACE"><span class="refentrytitle">CREATE TABLESPACE</span></a>.
  44. </p></dd><dt><span class="term"><code class="literal">ATTACH PARTITION</code></span></dt><dd><p>
  45. Causes the named index to become attached to the altered index.
  46. The named index must be on a partition of the table containing the
  47. index being altered, and have an equivalent definition. An attached
  48. index cannot be dropped by itself, and will automatically be dropped
  49. if its parent index is dropped.
  50. </p></dd><dt><span class="term"><code class="literal">DEPENDS ON EXTENSION</code></span></dt><dd><p>
  51. This form marks the index as dependent on the extension, such that if the
  52. extension is dropped, the index will automatically be dropped as well.
  53. </p></dd><dt><span class="term"><code class="literal">SET ( <em class="replaceable"><code>storage_parameter</code></em> [= <em class="replaceable"><code>value</code></em>] [, ... ] )</code></span></dt><dd><p>
  54. This form changes one or more index-method-specific storage parameters
  55. for the index. See
  56. <a class="xref" href="sql-createindex.html" title="CREATE INDEX"><span class="refentrytitle">CREATE INDEX</span></a>
  57. for details on the available parameters. Note that the index contents
  58. will not be modified immediately by this command; depending on the
  59. parameter you might need to rebuild the index with
  60. <a class="xref" href="sql-reindex.html" title="REINDEX"><span class="refentrytitle">REINDEX</span></a>
  61. to get the desired effects.
  62. </p></dd><dt><span class="term"><code class="literal">RESET ( <em class="replaceable"><code>storage_parameter</code></em> [, ... ] )</code></span></dt><dd><p>
  63. This form resets one or more index-method-specific storage parameters to
  64. their defaults. As with <code class="literal">SET</code>, a <code class="literal">REINDEX</code>
  65. might be needed to update the index entirely.
  66. </p></dd><dt><span class="term"><code class="literal">ALTER [ COLUMN ] <em class="replaceable"><code>column_number</code></em> SET STATISTICS <em class="replaceable"><code>integer</code></em></code></span></dt><dd><p>
  67. This form sets the per-column statistics-gathering target for
  68. subsequent <a class="xref" href="sql-analyze.html" title="ANALYZE"><span class="refentrytitle">ANALYZE</span></a> operations, though can
  69. be used only on index columns that are defined as an expression.
  70. Since expressions lack a unique name, we refer to them using the
  71. ordinal number of the index column.
  72. The target can be set in the range 0 to 10000; alternatively, set it
  73. to -1 to revert to using the system default statistics
  74. target (<a class="xref" href="runtime-config-query.html#GUC-DEFAULT-STATISTICS-TARGET">default_statistics_target</a>).
  75. For more information on the use of statistics by the
  76. <span class="productname">PostgreSQL</span> query planner, refer to
  77. <a class="xref" href="planner-stats.html" title="14.2. Statistics Used by the Planner">Section 14.2</a>.
  78. </p></dd></dl></div><p>
  79. </p></div><div class="refsect1" id="id-1.9.3.16.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">IF EXISTS</code></span></dt><dd><p>
  80. Do not throw an error if the index does not exist. A notice is issued
  81. in this case.
  82. </p></dd><dt><span class="term"><em class="replaceable"><code>column_number</code></em></span></dt><dd><p>
  83. The ordinal number refers to the ordinal (left-to-right) position
  84. of the index column.
  85. </p></dd><dt><span class="term"><em class="replaceable"><code>name</code></em></span></dt><dd><p>
  86. The name (possibly schema-qualified) of an existing index to
  87. alter.
  88. </p></dd><dt><span class="term"><em class="replaceable"><code>new_name</code></em></span></dt><dd><p>
  89. The new name for the index.
  90. </p></dd><dt><span class="term"><em class="replaceable"><code>tablespace_name</code></em></span></dt><dd><p>
  91. The tablespace to which the index will be moved.
  92. </p></dd><dt><span class="term"><em class="replaceable"><code>extension_name</code></em></span></dt><dd><p>
  93. The name of the extension that the index is to depend on.
  94. </p></dd><dt><span class="term"><em class="replaceable"><code>storage_parameter</code></em></span></dt><dd><p>
  95. The name of an index-method-specific storage parameter.
  96. </p></dd><dt><span class="term"><em class="replaceable"><code>value</code></em></span></dt><dd><p>
  97. The new value for an index-method-specific storage parameter.
  98. This might be a number or a word depending on the parameter.
  99. </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.16.7"><h2>Notes</h2><p>
  100. These operations are also possible using
  101. <a class="xref" href="sql-altertable.html" title="ALTER TABLE"><span class="refentrytitle">ALTER TABLE</span></a>.
  102. <code class="command">ALTER INDEX</code> is in fact just an alias for the forms
  103. of <code class="command">ALTER TABLE</code> that apply to indexes.
  104. </p><p>
  105. There was formerly an <code class="command">ALTER INDEX OWNER</code> variant, but
  106. this is now ignored (with a warning). An index cannot have an owner
  107. different from its table's owner. Changing the table's owner
  108. automatically changes the index as well.
  109. </p><p>
  110. Changing any part of a system catalog index is not permitted.
  111. </p></div><div class="refsect1" id="id-1.9.3.16.8"><h2>Examples</h2><p>
  112. To rename an existing index:
  113. </p><pre class="programlisting">
  114. ALTER INDEX distributors RENAME TO suppliers;
  115. </pre><p>
  116. </p><p>
  117. To move an index to a different tablespace:
  118. </p><pre class="programlisting">
  119. ALTER INDEX distributors SET TABLESPACE fasttablespace;
  120. </pre><p>
  121. </p><p>
  122. To change an index's fill factor (assuming that the index method
  123. supports it):
  124. </p><pre class="programlisting">
  125. ALTER INDEX distributors SET (fillfactor = 75);
  126. REINDEX INDEX distributors;
  127. </pre><p>
  128. Set the statistics-gathering target for an expression index:
  129. </p><pre class="programlisting">
  130. CREATE INDEX coord_idx ON measured (x, y, (z + t));
  131. ALTER INDEX coord_idx ALTER COLUMN 3 SET STATISTICS 1000;
  132. </pre></div><div class="refsect1" id="id-1.9.3.16.9"><h2>Compatibility</h2><p>
  133. <code class="command">ALTER INDEX</code> is a <span class="productname">PostgreSQL</span>
  134. extension.
  135. </p></div><div class="refsect1" id="id-1.9.3.16.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-reindex.html" title="REINDEX"><span class="refentrytitle">REINDEX</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-altergroup.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-alterlanguage.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">ALTER GROUP </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> ALTER LANGUAGE</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1