summaryrefslogtreecommitdiffstats
path: root/doc/src/sgml/man7/ALTER_INDEX.7
blob: 79b3923b7c7e437a091376754eb893ea3b110b7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
'\" t
.\"     Title: ALTER INDEX
.\"    Author: The PostgreSQL Global Development Group
.\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
.\"      Date: 2023
.\"    Manual: PostgreSQL 15.5 Documentation
.\"    Source: PostgreSQL 15.5
.\"  Language: English
.\"
.TH "ALTER INDEX" "7" "2023" "PostgreSQL 15.5" "PostgreSQL 15.5 Documentation"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.\" http://bugs.debian.org/507673
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\" -----------------------------------------------------------------
.\" * set default formatting
.\" -----------------------------------------------------------------
.\" disable hyphenation
.nh
.\" disable justification (adjust text to left margin only)
.ad l
.\" -----------------------------------------------------------------
.\" * MAIN CONTENT STARTS HERE *
.\" -----------------------------------------------------------------
.SH "NAME"
ALTER_INDEX \- change the definition of an index
.SH "SYNOPSIS"
.sp
.nf
ALTER INDEX [ IF EXISTS ] \fIname\fR RENAME TO \fInew_name\fR
ALTER INDEX [ IF EXISTS ] \fIname\fR SET TABLESPACE \fItablespace_name\fR
ALTER INDEX \fIname\fR ATTACH PARTITION \fIindex_name\fR
ALTER INDEX \fIname\fR [ NO ] DEPENDS ON EXTENSION \fIextension_name\fR
ALTER INDEX [ IF EXISTS ] \fIname\fR SET ( \fIstorage_parameter\fR [= \fIvalue\fR] [, \&.\&.\&. ] )
ALTER INDEX [ IF EXISTS ] \fIname\fR RESET ( \fIstorage_parameter\fR [, \&.\&.\&. ] )
ALTER INDEX [ IF EXISTS ] \fIname\fR ALTER [ COLUMN ] \fIcolumn_number\fR
    SET STATISTICS \fIinteger\fR
ALTER INDEX ALL IN TABLESPACE \fIname\fR [ OWNED BY \fIrole_name\fR [, \&.\&.\&. ] ]
    SET TABLESPACE \fInew_tablespace\fR [ NOWAIT ]
.fi
.SH "DESCRIPTION"
.PP
\fBALTER INDEX\fR
changes the definition of an existing index\&. There are several subforms described below\&. Note that the lock level required may differ for each subform\&. An
ACCESS EXCLUSIVE
lock is held unless explicitly noted\&. When multiple subcommands are listed, the lock held will be the strictest one required from any subcommand\&.
.PP
RENAME
.RS 4
The
RENAME
form changes the name of the index\&. If the index is associated with a table constraint (either
UNIQUE,
PRIMARY KEY, or
EXCLUDE), the constraint is renamed as well\&. There is no effect on the stored data\&.
.sp
Renaming an index acquires a
SHARE UPDATE EXCLUSIVE
lock\&.
.RE
.PP
SET TABLESPACE
.RS 4
This form changes the index\*(Aqs tablespace to the specified tablespace and moves the data file(s) associated with the index to the new tablespace\&. To change the tablespace of an index, you must own the index and have
CREATE
privilege on the new tablespace\&. All indexes in the current database in a tablespace can be moved by using the
ALL IN TABLESPACE
form, which will lock all indexes to be moved and then move each one\&. This form also supports
OWNED BY, which will only move indexes owned by the roles specified\&. If the
NOWAIT
option is specified then the command will fail if it is unable to acquire all of the locks required immediately\&. Note that system catalogs will not be moved by this command, use
\fBALTER DATABASE\fR
or explicit
\fBALTER INDEX\fR
invocations instead if desired\&. See also
\fBCREATE TABLESPACE\fR\&.
.RE
.PP
ATTACH PARTITION
.RS 4
Causes the named index to become attached to the altered index\&. The named index must be on a partition of the table containing the index being altered, and have an equivalent definition\&. An attached index cannot be dropped by itself, and will automatically be dropped if its parent index is dropped\&.
.RE
.PP
DEPENDS ON EXTENSION \fIextension_name\fR
.br
NO DEPENDS ON EXTENSION \fIextension_name\fR
.RS 4
This form marks the index as dependent on the extension, or no longer dependent on that extension if
NO
is specified\&. An index that\*(Aqs marked as dependent on an extension is automatically dropped when the extension is dropped\&.
.RE
.PP
SET ( \fIstorage_parameter\fR [= \fIvalue\fR] [, \&.\&.\&. ] )
.RS 4
This form changes one or more index\-method\-specific storage parameters for the index\&. See
\fBCREATE INDEX\fR
for details on the available parameters\&. Note that the index contents will not be modified immediately by this command; depending on the parameter you might need to rebuild the index with
\fBREINDEX\fR
to get the desired effects\&.
.RE
.PP
RESET ( \fIstorage_parameter\fR [, \&.\&.\&. ] )
.RS 4
This form resets one or more index\-method\-specific storage parameters to their defaults\&. As with
SET, a
REINDEX
might be needed to update the index entirely\&.
.RE
.PP
ALTER [ COLUMN ] \fIcolumn_number\fR SET STATISTICS \fIinteger\fR
.RS 4
This form sets the per\-column statistics\-gathering target for subsequent
\fBANALYZE\fR
operations, though can be used only on index columns that are defined as an expression\&. Since expressions lack a unique name, we refer to them using the ordinal number of the index column\&. The target can be set in the range 0 to 10000; alternatively, set it to \-1 to revert to using the system default statistics target (default_statistics_target)\&. For more information on the use of statistics by the
PostgreSQL
query planner, refer to
Section\ \&14.2\&.
.RE
.SH "PARAMETERS"
.PP
IF EXISTS
.RS 4
Do not throw an error if the index does not exist\&. A notice is issued in this case\&.
.RE
.PP
\fIcolumn_number\fR
.RS 4
The ordinal number refers to the ordinal (left\-to\-right) position of the index column\&.
.RE
.PP
\fIname\fR
.RS 4
The name (possibly schema\-qualified) of an existing index to alter\&.
.RE
.PP
\fInew_name\fR
.RS 4
The new name for the index\&.
.RE
.PP
\fItablespace_name\fR
.RS 4
The tablespace to which the index will be moved\&.
.RE
.PP
\fIextension_name\fR
.RS 4
The name of the extension that the index is to depend on\&.
.RE
.PP
\fIstorage_parameter\fR
.RS 4
The name of an index\-method\-specific storage parameter\&.
.RE
.PP
\fIvalue\fR
.RS 4
The new value for an index\-method\-specific storage parameter\&. This might be a number or a word depending on the parameter\&.
.RE
.SH "NOTES"
.PP
These operations are also possible using
\fBALTER TABLE\fR\&.
\fBALTER INDEX\fR
is in fact just an alias for the forms of
\fBALTER TABLE\fR
that apply to indexes\&.
.PP
There was formerly an
\fBALTER INDEX OWNER\fR
variant, but this is now ignored (with a warning)\&. An index cannot have an owner different from its table\*(Aqs owner\&. Changing the table\*(Aqs owner automatically changes the index as well\&.
.PP
Changing any part of a system catalog index is not permitted\&.
.SH "EXAMPLES"
.PP
To rename an existing index:
.sp
.if n \{\
.RS 4
.\}
.nf
ALTER INDEX distributors RENAME TO suppliers;
.fi
.if n \{\
.RE
.\}
.PP
To move an index to a different tablespace:
.sp
.if n \{\
.RS 4
.\}
.nf
ALTER INDEX distributors SET TABLESPACE fasttablespace;
.fi
.if n \{\
.RE
.\}
.PP
To change an index\*(Aqs fill factor (assuming that the index method supports it):
.sp
.if n \{\
.RS 4
.\}
.nf
ALTER INDEX distributors SET (fillfactor = 75);
REINDEX INDEX distributors;
.fi
.if n \{\
.RE
.\}
.PP
Set the statistics\-gathering target for an expression index:
.sp
.if n \{\
.RS 4
.\}
.nf
CREATE INDEX coord_idx ON measured (x, y, (z + t));
ALTER INDEX coord_idx ALTER COLUMN 3 SET STATISTICS 1000;
.fi
.if n \{\
.RE
.\}
.SH "COMPATIBILITY"
.PP
\fBALTER INDEX\fR
is a
PostgreSQL
extension\&.
.SH "SEE ALSO"
CREATE INDEX (\fBCREATE_INDEX\fR(7)), \fBREINDEX\fR(7)