]> begriffs open source - ai-pg/blob - full-docs/html/sql-alterindex.html
Include links to all subsection html pages, with shorter paths too
[ai-pg] / full-docs / html / sql-alterindex.html
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 18.0 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">
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> [ NO ] 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
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 (either
23       <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 and
31       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 have
33       <code class="literal">CREATE</code> privilege on the new tablespace.
34       All indexes in the current database in a tablespace can be moved by using
35       the <code class="literal">ALL IN TABLESPACE</code> form, which will lock all
36       indexes to be moved and then move each one.  This form also supports
37       <code class="literal">OWNED BY</code>, which will only move indexes owned by the
38       roles specified.  If the <code class="literal">NOWAIT</code> option is specified
39       then the command will fail if it is unable to acquire all of the locks
40       required immediately.  Note that system catalogs will not be moved by
41       this command, use <code class="command">ALTER DATABASE</code> or explicit
42       <code class="command">ALTER INDEX</code> invocations instead if desired.
43       See also
44       <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 <em class="replaceable"><code>index_name</code></em></code></span></dt><dd><p>
46       Causes the named index (possibly schema-qualified) to become attached
47       to the altered index.
48       The named index must be on a partition of the table containing the
49       index being altered, and have an equivalent definition.  An attached
50       index cannot be dropped by itself, and will automatically be dropped
51       if its parent index is dropped.
52      </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>
53       This form marks the index as dependent on the extension, or no longer
54       dependent on that extension if <code class="literal">NO</code> is specified.
55       An index that's marked as dependent on an extension is automatically
56       dropped when the extension is dropped.
57      </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>
58       This form changes one or more index-method-specific storage parameters
59       for the index.  See
60       <a class="link" href="sql-createindex.html" title="CREATE INDEX"><code class="command">CREATE INDEX</code></a>
61       for details on the available parameters.  Note that the index contents
62       will not be modified immediately by this command; depending on the
63       parameter you might need to rebuild the index with
64       <a class="link" href="sql-reindex.html" title="REINDEX"><code class="command">REINDEX</code></a>
65       to get the desired effects.
66      </p></dd><dt><span class="term"><code class="literal">RESET ( <em class="replaceable"><code>storage_parameter</code></em> [, ... ] )</code></span></dt><dd><p>
67       This form resets one or more index-method-specific storage parameters to
68       their defaults.  As with <code class="literal">SET</code>, a <code class="literal">REINDEX</code>
69       might be needed to update the index entirely.
70      </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>
71       This form sets the per-column statistics-gathering target for
72       subsequent <a class="link" href="sql-analyze.html" title="ANALYZE"><code class="command">ANALYZE</code></a> operations, though can
73       be used only on index columns that are defined as an expression.
74       Since expressions lack a unique name, we refer to them using the
75       ordinal number of the index column.
76       The target can be set in the range 0 to 10000; alternatively, set it
77       to -1 to revert to using the system default statistics
78       target (<a class="xref" href="runtime-config-query.html#GUC-DEFAULT-STATISTICS-TARGET">default_statistics_target</a>).
79       For more information on the use of statistics by the
80       <span class="productname">PostgreSQL</span> query planner, refer to
81       <a class="xref" href="planner-stats.html" title="14.2. Statistics Used by the Planner">Section 14.2</a>.
82      </p></dd></dl></div><p>
83   </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>
84         Do not throw an error if the index does not exist. A notice is issued
85         in this case.
86        </p></dd><dt><span class="term"><em class="replaceable"><code>column_number</code></em></span></dt><dd><p>
87         The ordinal number refers to the ordinal (left-to-right) position
88         of the index column.
89        </p></dd><dt><span class="term"><em class="replaceable"><code>name</code></em></span></dt><dd><p>
90         The name (possibly schema-qualified) of an existing index to
91         alter.
92        </p></dd><dt><span class="term"><em class="replaceable"><code>new_name</code></em></span></dt><dd><p>
93         The new name for the index.
94        </p></dd><dt><span class="term"><em class="replaceable"><code>tablespace_name</code></em></span></dt><dd><p>
95         The tablespace to which the index will be moved.
96        </p></dd><dt><span class="term"><em class="replaceable"><code>extension_name</code></em></span></dt><dd><p>
97         The name of the extension that the index is to depend on.
98        </p></dd><dt><span class="term"><em class="replaceable"><code>storage_parameter</code></em></span></dt><dd><p>
99         The name of an index-method-specific storage parameter.
100        </p></dd><dt><span class="term"><em class="replaceable"><code>value</code></em></span></dt><dd><p>
101         The new value for an index-method-specific storage parameter.
102         This might be a number or a word depending on the parameter.
103        </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.16.7"><h2>Notes</h2><p>
104     These operations are also possible using
105     <a class="link" href="sql-altertable.html" title="ALTER TABLE"><code class="command">ALTER TABLE</code></a>.
106     <code class="command">ALTER INDEX</code> is in fact just an alias for the forms
107     of <code class="command">ALTER TABLE</code> that apply to indexes.
108    </p><p>
109     There was formerly an <code class="command">ALTER INDEX OWNER</code> variant, but
110     this is now ignored (with a warning).  An index cannot have an owner
111     different from its table's owner.  Changing the table's owner
112     automatically changes the index as well.
113    </p><p>
114     Changing any part of a system catalog index is not permitted.
115    </p></div><div class="refsect1" id="id-1.9.3.16.8"><h2>Examples</h2><p>
116    To rename an existing index:
117 </p><pre class="programlisting">
118 ALTER INDEX distributors RENAME TO suppliers;
119 </pre><p>
120   </p><p>
121    To move an index to a different tablespace:
122 </p><pre class="programlisting">
123 ALTER INDEX distributors SET TABLESPACE fasttablespace;
124 </pre><p>
125   </p><p>
126    To change an index's fill factor (assuming that the index method
127    supports it):
128 </p><pre class="programlisting">
129 ALTER INDEX distributors SET (fillfactor = 75);
130 REINDEX INDEX distributors;
131 </pre><p>
132    Set the statistics-gathering target for an expression index:
133 </p><pre class="programlisting">
134 CREATE INDEX coord_idx ON measured (x, y, (z + t));
135 ALTER INDEX coord_idx ALTER COLUMN 3 SET STATISTICS 1000;
136 </pre></div><div class="refsect1" id="id-1.9.3.16.9"><h2>Compatibility</h2><p>
137    <code class="command">ALTER INDEX</code> is a <span class="productname">PostgreSQL</span>
138    extension.
139   </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 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> ALTER LANGUAGE</td></tr></table></div></body></html>