codekingpro/portable-devtools
115k
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 Vsnapshot" /><link rel="prev" href="sql-altergroup.html" title="ALTER GROUP" /><link rel="next" href="sql-alterlanguage.html" title="ALTER LANGUAGE" /></head><body id="docContent" class="container-fluid col-10"><div 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 16.3 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 /></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">3ALTER INDEX [ IF EXISTS ] <em class="replaceable"><code>name</code></em> RENAME TO <em class="replaceable"><code>new_name</code></em>4ALTER INDEX [ IF EXISTS ] <em class="replaceable"><code>name</code></em> SET TABLESPACE <em class="replaceable"><code>tablespace_name</code></em>5ALTER INDEX <em class="replaceable"><code>name</code></em> ATTACH PARTITION <em class="replaceable"><code>index_name</code></em>6ALTER INDEX <em class="replaceable"><code>name</code></em> [ NO ] DEPENDS ON EXTENSION <em class="replaceable"><code>extension_name</code></em>7ALTER 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>] [, ... ] )8ALTER INDEX [ IF EXISTS ] <em class="replaceable"><code>name</code></em> RESET ( <em class="replaceable"><code>storage_parameter</code></em> [, ... ] )9ALTER 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>11ALTER 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 required16 may differ for each subform. An <code class="literal">ACCESS EXCLUSIVE</code> lock is held17 unless explicitly noted. When multiple subcommands are listed, the lock18 held will be the strictest one required from any subcommand.19 20 </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">RENAME</code></span></dt><dd><p>21 The <code class="literal">RENAME</code> form changes the name of the index.22 If the index is associated with a table constraint (either23 <code class="literal">UNIQUE</code>, <code class="literal">PRIMARY KEY</code>,24 or <code class="literal">EXCLUDE</code>), the constraint is renamed as well.25 There is no effect on the stored data.26 </p><p>27 Renaming an index acquires a <code class="literal">SHARE UPDATE EXCLUSIVE</code>28 lock.29 </p></dd><dt><span class="term"><code class="literal">SET TABLESPACE</code></span></dt><dd><p>30 This form changes the index's tablespace to the specified tablespace and31 moves the data file(s) associated with the index to the new tablespace.32 To change the tablespace of an index, you must own the index and have33 <code class="literal">CREATE</code> privilege on the new tablespace.34 All indexes in the current database in a tablespace can be moved by using35 the <code class="literal">ALL IN TABLESPACE</code> form, which will lock all36 indexes to be moved and then move each one. This form also supports37 <code class="literal">OWNED BY</code>, which will only move indexes owned by the38 roles specified. If the <code class="literal">NOWAIT</code> option is specified39 then the command will fail if it is unable to acquire all of the locks40 required immediately. Note that system catalogs will not be moved by41 this command, use <code class="command">ALTER DATABASE</code> or explicit42 <code class="command">ALTER INDEX</code> invocations instead if desired.43 See also44 <a class="link" href="sql-createtablespace.html" title="CREATE TABLESPACE"><code class="command">CREATE TABLESPACE</code></a>.45 </p></dd><dt><span class="term"><code class="literal">ATTACH PARTITION</code></span></dt><dd><p>46 Causes the named index to become attached to the altered index.47 The named index must be on a partition of the table containing the48 index being altered, and have an equivalent definition. An attached49 index cannot be dropped by itself, and will automatically be dropped50 if its parent index is dropped.51 </p></dd><dt><span class="term"><code class="literal">DEPENDS ON EXTENSION <em class="replaceable"><code>extension_name</code></em></code><br /></span><span class="term"><code class="literal">NO DEPENDS ON EXTENSION <em class="replaceable"><code>extension_name</code></em></code></span></dt><dd><p>52 This form marks the index as dependent on the extension, or no longer53 dependent on that extension if <code class="literal">NO</code> is specified.54 An index that's marked as dependent on an extension is automatically55 dropped when the extension is dropped.56 </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>57 This form changes one or more index-method-specific storage parameters58 for the index. See59 <a class="link" href="sql-createindex.html" title="CREATE INDEX"><code class="command">CREATE INDEX</code></a>60 for details on the available parameters. Note that the index contents61 will not be modified immediately by this command; depending on the62 parameter you might need to rebuild the index with63 <a class="link" href="sql-reindex.html" title="REINDEX"><code class="command">REINDEX</code></a>64 to get the desired effects.65 </p></dd><dt><span class="term"><code class="literal">RESET ( <em class="replaceable"><code>storage_parameter</code></em> [, ... ] )</code></span></dt><dd><p>66 This form resets one or more index-method-specific storage parameters to67 their defaults. As with <code class="literal">SET</code>, a <code class="literal">REINDEX</code>68 might be needed to update the index entirely.69 </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>70 This form sets the per-column statistics-gathering target for71 subsequent <a class="link" href="sql-analyze.html" title="ANALYZE"><code class="command">ANALYZE</code></a> operations, though can72 be used only on index columns that are defined as an expression.73 Since expressions lack a unique name, we refer to them using the74 ordinal number of the index column.75 The target can be set in the range 0 to 10000; alternatively, set it76 to -1 to revert to using the system default statistics77 target (<a class="xref" href="runtime-config-query.html#GUC-DEFAULT-STATISTICS-TARGET">default_statistics_target</a>).78 For more information on the use of statistics by the79 <span class="productname">PostgreSQL</span> query planner, refer to80 <a class="xref" href="planner-stats.html" title="14.2. Statistics Used by the Planner">Section 14.2</a>.81 </p></dd></dl></div><p>82 </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>83 Do not throw an error if the index does not exist. A notice is issued84 in this case.85 </p></dd><dt><span class="term"><em class="replaceable"><code>column_number</code></em></span></dt><dd><p>86 The ordinal number refers to the ordinal (left-to-right) position87 of the index column.88 </p></dd><dt><span class="term"><em class="replaceable"><code>name</code></em></span></dt><dd><p>89 The name (possibly schema-qualified) of an existing index to90 alter.91 </p></dd><dt><span class="term"><em class="replaceable"><code>new_name</code></em></span></dt><dd><p>92 The new name for the index.93 </p></dd><dt><span class="term"><em class="replaceable"><code>tablespace_name</code></em></span></dt><dd><p>94 The tablespace to which the index will be moved.95 </p></dd><dt><span class="term"><em class="replaceable"><code>extension_name</code></em></span></dt><dd><p>96 The name of the extension that the index is to depend on.97 </p></dd><dt><span class="term"><em class="replaceable"><code>storage_parameter</code></em></span></dt><dd><p>98 The name of an index-method-specific storage parameter.99 </p></dd><dt><span class="term"><em class="replaceable"><code>value</code></em></span></dt><dd><p>100 The new value for an index-method-specific storage parameter.101 This might be a number or a word depending on the parameter.102 </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.16.7"><h2>Notes</h2><p>103 These operations are also possible using104 <a class="link" href="sql-altertable.html" title="ALTER TABLE"><code class="command">ALTER TABLE</code></a>.105 <code class="command">ALTER INDEX</code> is in fact just an alias for the forms106 of <code class="command">ALTER TABLE</code> that apply to indexes.107 </p><p>108 There was formerly an <code class="command">ALTER INDEX OWNER</code> variant, but109 this is now ignored (with a warning). An index cannot have an owner110 different from its table's owner. Changing the table's owner111 automatically changes the index as well.112 </p><p>113 Changing any part of a system catalog index is not permitted.114 </p></div><div class="refsect1" id="id-1.9.3.16.8"><h2>Examples</h2><p>115 To rename an existing index:116</p><pre class="programlisting">117ALTER INDEX distributors RENAME TO suppliers;118</pre><p>119 </p><p>120 To move an index to a different tablespace:121</p><pre class="programlisting">122ALTER INDEX distributors SET TABLESPACE fasttablespace;123</pre><p>124 </p><p>125 To change an index's fill factor (assuming that the index method126 supports it):127</p><pre class="programlisting">128ALTER INDEX distributors SET (fillfactor = 75);129REINDEX INDEX distributors;130</pre><p>131 Set the statistics-gathering target for an expression index:132</p><pre class="programlisting">133CREATE INDEX coord_idx ON measured (x, y, (z + t));134ALTER INDEX coord_idx ALTER COLUMN 3 SET STATISTICS 1000;135</pre></div><div class="refsect1" id="id-1.9.3.16.9"><h2>Compatibility</h2><p>136 <code class="command">ALTER INDEX</code> is a <span class="productname">PostgreSQL</span>137 extension.138 </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" title="ALTER GROUP">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-alterlanguage.html" title="ALTER LANGUAGE">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" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> ALTER LANGUAGE</td></tr></table></div></body></html>