]> begriffs open source - ai-pg/blob - full-docs/man7/REINDEX.7
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / man7 / REINDEX.7
1 '\" t
2 .\"     Title: REINDEX
3 .\"    Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
5 .\"      Date: 2025
6 .\"    Manual: PostgreSQL 18.0 Documentation
7 .\"    Source: PostgreSQL 18.0
8 .\"  Language: English
9 .\"
10 .TH "REINDEX" "7" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 .ie \n(.g .ds Aq \(aq
19 .el       .ds Aq '
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
24 .nh
25 .\" disable justification (adjust text to left margin only)
26 .ad l
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
30 .SH "NAME"
31 REINDEX \- rebuild indexes
32 .SH "SYNOPSIS"
33 .sp
34 .nf
35 REINDEX [ ( \fIoption\fR [, \&.\&.\&.] ) ] { INDEX | TABLE | SCHEMA } [ CONCURRENTLY ] \fIname\fR
36 REINDEX [ ( \fIoption\fR [, \&.\&.\&.] ) ] { DATABASE | SYSTEM } [ CONCURRENTLY ] [ \fIname\fR ]
37
38 where \fIoption\fR can be one of:
39
40     CONCURRENTLY [ \fIboolean\fR ]
41     TABLESPACE \fInew_tablespace\fR
42     VERBOSE [ \fIboolean\fR ]
43 .fi
44 .SH "DESCRIPTION"
45 .PP
46 \fBREINDEX\fR
47 rebuilds an index using the data stored in the index\*(Aqs table, replacing the old copy of the index\&. There are several scenarios in which to use
48 \fBREINDEX\fR:
49 .sp
50 .RS 4
51 .ie n \{\
52 \h'-04'\(bu\h'+03'\c
53 .\}
54 .el \{\
55 .sp -1
56 .IP \(bu 2.3
57 .\}
58 An index has become corrupted, and no longer contains valid data\&. Although in theory this should never happen, in practice indexes can become corrupted due to software bugs or hardware failures\&.
59 \fBREINDEX\fR
60 provides a recovery method\&.
61 .RE
62 .sp
63 .RS 4
64 .ie n \{\
65 \h'-04'\(bu\h'+03'\c
66 .\}
67 .el \{\
68 .sp -1
69 .IP \(bu 2.3
70 .\}
71 An index has become
72 \(lqbloated\(rq, that is it contains many empty or nearly\-empty pages\&. This can occur with B\-tree indexes in
73 PostgreSQL
74 under certain uncommon access patterns\&.
75 \fBREINDEX\fR
76 provides a way to reduce the space consumption of the index by writing a new version of the index without the dead pages\&. See
77 Section\ \&24.2
78 for more information\&.
79 .RE
80 .sp
81 .RS 4
82 .ie n \{\
83 \h'-04'\(bu\h'+03'\c
84 .\}
85 .el \{\
86 .sp -1
87 .IP \(bu 2.3
88 .\}
89 You have altered a storage parameter (such as fillfactor) for an index, and wish to ensure that the change has taken full effect\&.
90 .RE
91 .sp
92 .RS 4
93 .ie n \{\
94 \h'-04'\(bu\h'+03'\c
95 .\}
96 .el \{\
97 .sp -1
98 .IP \(bu 2.3
99 .\}
100 If an index build fails with the
101 CONCURRENTLY
102 option, this index is left as
103 \(lqinvalid\(rq\&. Such indexes are useless but it can be convenient to use
104 \fBREINDEX\fR
105 to rebuild them\&. Note that only
106 \fBREINDEX INDEX\fR
107 is able to perform a concurrent build on an invalid index\&.
108 .RE
109 .SH "PARAMETERS"
110 .PP
111 INDEX
112 .RS 4
113 Recreate the specified index\&. This form of
114 \fBREINDEX\fR
115 cannot be executed inside a transaction block when used with a partitioned index\&.
116 .RE
117 .PP
118 TABLE
119 .RS 4
120 Recreate all indexes of the specified table\&. If the table has a secondary
121 \(lqTOAST\(rq
122 table, that is reindexed as well\&. This form of
123 \fBREINDEX\fR
124 cannot be executed inside a transaction block when used with a partitioned table\&.
125 .RE
126 .PP
127 SCHEMA
128 .RS 4
129 Recreate all indexes of the specified schema\&. If a table of this schema has a secondary
130 \(lqTOAST\(rq
131 table, that is reindexed as well\&. Indexes on shared system catalogs are also processed\&. This form of
132 \fBREINDEX\fR
133 cannot be executed inside a transaction block\&.
134 .RE
135 .PP
136 DATABASE
137 .RS 4
138 Recreate all indexes within the current database, except system catalogs\&. Indexes on system catalogs are not processed\&. This form of
139 \fBREINDEX\fR
140 cannot be executed inside a transaction block\&.
141 .RE
142 .PP
143 SYSTEM
144 .RS 4
145 Recreate all indexes on system catalogs within the current database\&. Indexes on shared system catalogs are included\&. Indexes on user tables are not processed\&. This form of
146 \fBREINDEX\fR
147 cannot be executed inside a transaction block\&.
148 .RE
149 .PP
150 \fIname\fR
151 .RS 4
152 The name of the specific index, table, or database to be reindexed\&. Index and table names can be schema\-qualified\&. Presently,
153 \fBREINDEX DATABASE\fR
154 and
155 \fBREINDEX SYSTEM\fR
156 can only reindex the current database\&. Their parameter is optional, and it must match the current database\*(Aqs name\&.
157 .RE
158 .PP
159 CONCURRENTLY
160 .RS 4
161 When this option is used,
162 PostgreSQL
163 will rebuild the index without taking any locks that prevent concurrent inserts, updates, or deletes on the table; whereas a standard index rebuild locks out writes (but not reads) on the table until it\*(Aqs done\&. There are several caveats to be aware of when using this option \(em see
164 Rebuilding Indexes Concurrently
165 below\&.
166 .sp
167 For temporary tables,
168 \fBREINDEX\fR
169 is always non\-concurrent, as no other session can access them, and non\-concurrent reindex is cheaper\&.
170 .RE
171 .PP
172 TABLESPACE
173 .RS 4
174 Specifies that indexes will be rebuilt on a new tablespace\&.
175 .RE
176 .PP
177 VERBOSE
178 .RS 4
179 Prints a progress report as each index is reindexed at
180 INFO
181 level\&.
182 .RE
183 .PP
184 \fIboolean\fR
185 .RS 4
186 Specifies whether the selected option should be turned on or off\&. You can write
187 TRUE,
188 ON, or
189 1
190 to enable the option, and
191 FALSE,
192 OFF, or
193 0
194 to disable it\&. The
195 \fIboolean\fR
196 value can also be omitted, in which case
197 TRUE
198 is assumed\&.
199 .RE
200 .PP
201 \fInew_tablespace\fR
202 .RS 4
203 The tablespace where indexes will be rebuilt\&.
204 .RE
205 .SH "NOTES"
206 .PP
207 If you suspect corruption of an index on a user table, you can simply rebuild that index, or all indexes on the table, using
208 \fBREINDEX INDEX\fR
209 or
210 \fBREINDEX TABLE\fR\&.
211 .PP
212 Things are more difficult if you need to recover from corruption of an index on a system table\&. In this case it\*(Aqs important for the system to not have used any of the suspect indexes itself\&. (Indeed, in this sort of scenario you might find that server processes are crashing immediately at start\-up, due to reliance on the corrupted indexes\&.) To recover safely, the server must be started with the
213 \fB\-P\fR
214 option, which prevents it from using indexes for system catalog lookups\&.
215 .PP
216 One way to do this is to shut down the server and start a single\-user
217 PostgreSQL
218 server with the
219 \fB\-P\fR
220 option included on its command line\&. Then,
221 \fBREINDEX DATABASE\fR,
222 \fBREINDEX SYSTEM\fR,
223 \fBREINDEX TABLE\fR, or
224 \fBREINDEX INDEX\fR
225 can be issued, depending on how much you want to reconstruct\&. If in doubt, use
226 \fBREINDEX SYSTEM\fR
227 to select reconstruction of all system indexes in the database\&. Then quit the single\-user server session and restart the regular server\&. See the
228 \fBpostgres\fR(1)
229 reference page for more information about how to interact with the single\-user server interface\&.
230 .PP
231 Alternatively, a regular server session can be started with
232 \fB\-P\fR
233 included in its command line options\&. The method for doing this varies across clients, but in all
234 libpq\-based clients, it is possible to set the
235 \fBPGOPTIONS\fR
236 environment variable to
237 \-P
238 before starting the client\&. Note that while this method does not require locking out other clients, it might still be wise to prevent other users from connecting to the damaged database until repairs have been completed\&.
239 .PP
240 \fBREINDEX\fR
241 is similar to a drop and recreate of the index in that the index contents are rebuilt from scratch\&. However, the locking considerations are rather different\&.
242 \fBREINDEX\fR
243 locks out writes but not reads of the index\*(Aqs parent table\&. It also takes an
244 ACCESS EXCLUSIVE
245 lock on the specific index being processed, which will block reads that attempt to use that index\&. In particular, the query planner tries to take an
246 ACCESS SHARE
247 lock on every index of the table, regardless of the query, and so
248 \fBREINDEX\fR
249 blocks virtually any queries except for some prepared queries whose plan has been cached and which don\*(Aqt use this very index\&. In contrast,
250 \fBDROP INDEX\fR
251 momentarily takes an
252 ACCESS EXCLUSIVE
253 lock on the parent table, blocking both writes and reads\&. The subsequent
254 \fBCREATE INDEX\fR
255 locks out writes but not reads; since the index is not there, no read will attempt to use it, meaning that there will be no blocking but reads might be forced into expensive sequential scans\&.
256 .PP
257 While
258 \fBREINDEX\fR
259 is running, the
260 search_path
261 is temporarily changed to
262 pg_catalog, pg_temp\&.
263 .PP
264 Reindexing a single index or table requires having the
265 MAINTAIN
266 privilege on the table\&. Note that while
267 \fBREINDEX\fR
268 on a partitioned index or table requires having the
269 MAINTAIN
270 privilege on the partitioned table, such commands skip the privilege checks when processing the individual partitions\&. Reindexing a schema or database requires being the owner of that schema or database or having privileges of the
271 pg_maintain
272 role\&. Note specifically that it\*(Aqs thus possible for non\-superusers to rebuild indexes of tables owned by other users\&. However, as a special exception,
273 \fBREINDEX DATABASE\fR,
274 \fBREINDEX SCHEMA\fR, and
275 \fBREINDEX SYSTEM\fR
276 will skip indexes on shared catalogs unless the user has the
277 MAINTAIN
278 privilege on the catalog\&.
279 .PP
280 Reindexing partitioned indexes or partitioned tables is supported with
281 \fBREINDEX INDEX\fR
282 or
283 \fBREINDEX TABLE\fR, respectively\&. Each partition of the specified partitioned relation is reindexed in a separate transaction\&. Those commands cannot be used inside a transaction block when working on a partitioned table or index\&.
284 .PP
285 When using the
286 TABLESPACE
287 clause with
288 \fBREINDEX\fR
289 on a partitioned index or table, only the tablespace references of the leaf partitions are updated\&. As partitioned indexes are not updated, it is recommended to separately use
290 \fBALTER TABLE ONLY\fR
291 on them so as any new partitions attached inherit the new tablespace\&. On failure, it may not have moved all the indexes to the new tablespace\&. Re\-running the command will rebuild all the leaf partitions and move previously\-unprocessed indexes to the new tablespace\&.
292 .PP
293 If
294 SCHEMA,
295 DATABASE
296 or
297 SYSTEM
298 is used with
299 TABLESPACE, system relations are skipped and a single
300 WARNING
301 will be generated\&. Indexes on TOAST tables are rebuilt, but not moved to the new tablespace\&.
302 .SS "Rebuilding Indexes Concurrently"
303 .PP
304 Rebuilding an index can interfere with regular operation of a database\&. Normally
305 PostgreSQL
306 locks the table whose index is rebuilt against writes and performs the entire index build with a single scan of the table\&. Other transactions can still read the table, but if they try to insert, update, or delete rows in the table they will block until the index rebuild is finished\&. This could have a severe effect if the system is a live production database\&. Very large tables can take many hours to be indexed, and even for smaller tables, an index rebuild can lock out writers for periods that are unacceptably long for a production system\&.
307 .PP
308 PostgreSQL
309 supports rebuilding indexes with minimum locking of writes\&. This method is invoked by specifying the
310 CONCURRENTLY
311 option of
312 \fBREINDEX\fR\&. When this option is used,
313 PostgreSQL
314 must perform two scans of the table for each index that needs to be rebuilt and wait for termination of all existing transactions that could potentially use the index\&. This method requires more total work than a standard index rebuild and takes significantly longer to complete as it needs to wait for unfinished transactions that might modify the index\&. However, since it allows normal operations to continue while the index is being rebuilt, this method is useful for rebuilding indexes in a production environment\&. Of course, the extra CPU, memory and I/O load imposed by the index rebuild may slow down other operations\&.
315 .PP
316 The following steps occur in a concurrent reindex\&. Each step is run in a separate transaction\&. If there are multiple indexes to be rebuilt, then each step loops through all the indexes before moving to the next step\&.
317 .sp
318 .RS 4
319 .ie n \{\
320 \h'-04' 1.\h'+01'\c
321 .\}
322 .el \{\
323 .sp -1
324 .IP "  1." 4.2
325 .\}
326 A new transient index definition is added to the catalog
327 pg_index\&. This definition will be used to replace the old index\&. A
328 SHARE UPDATE EXCLUSIVE
329 lock at session level is taken on the indexes being reindexed as well as their associated tables to prevent any schema modification while processing\&.
330 .RE
331 .sp
332 .RS 4
333 .ie n \{\
334 \h'-04' 2.\h'+01'\c
335 .\}
336 .el \{\
337 .sp -1
338 .IP "  2." 4.2
339 .\}
340 A first pass to build the index is done for each new index\&. Once the index is built, its flag
341 pg_index\&.indisready
342 is switched to
343 \(lqtrue\(rq
344 to make it ready for inserts, making it visible to other sessions once the transaction that performed the build is finished\&. This step is done in a separate transaction for each index\&.
345 .RE
346 .sp
347 .RS 4
348 .ie n \{\
349 \h'-04' 3.\h'+01'\c
350 .\}
351 .el \{\
352 .sp -1
353 .IP "  3." 4.2
354 .\}
355 Then a second pass is performed to add tuples that were added while the first pass was running\&. This step is also done in a separate transaction for each index\&.
356 .RE
357 .sp
358 .RS 4
359 .ie n \{\
360 \h'-04' 4.\h'+01'\c
361 .\}
362 .el \{\
363 .sp -1
364 .IP "  4." 4.2
365 .\}
366 All the constraints that refer to the index are changed to refer to the new index definition, and the names of the indexes are changed\&. At this point,
367 pg_index\&.indisvalid
368 is switched to
369 \(lqtrue\(rq
370 for the new index and to
371 \(lqfalse\(rq
372 for the old, and a cache invalidation is done causing all sessions that referenced the old index to be invalidated\&.
373 .RE
374 .sp
375 .RS 4
376 .ie n \{\
377 \h'-04' 5.\h'+01'\c
378 .\}
379 .el \{\
380 .sp -1
381 .IP "  5." 4.2
382 .\}
383 The old indexes have
384 pg_index\&.indisready
385 switched to
386 \(lqfalse\(rq
387 to prevent any new tuple insertions, after waiting for running queries that might reference the old index to complete\&.
388 .RE
389 .sp
390 .RS 4
391 .ie n \{\
392 \h'-04' 6.\h'+01'\c
393 .\}
394 .el \{\
395 .sp -1
396 .IP "  6." 4.2
397 .\}
398 The old indexes are dropped\&. The
399 SHARE UPDATE EXCLUSIVE
400 session locks for the indexes and the table are released\&.
401 .RE
402 .PP
403 If a problem arises while rebuilding the indexes, such as a uniqueness violation in a unique index, the
404 \fBREINDEX\fR
405 command will fail but leave behind an
406 \(lqinvalid\(rq
407 new index in addition to the pre\-existing one\&. This index will be ignored for querying purposes because it might be incomplete; however it will still consume update overhead\&. The
408 psql
409 \fB\ed\fR
410 command will report such an index as
411 INVALID:
412 .sp
413 .if n \{\
414 .RS 4
415 .\}
416 .nf
417 postgres=# \ed tab
418        Table "public\&.tab"
419  Column |  Type   | Modifiers
420 \-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-
421  col    | integer |
422 Indexes:
423     "idx" btree (col)
424     "idx_ccnew" btree (col) INVALID
425 .fi
426 .if n \{\
427 .RE
428 .\}
429 .sp
430 If the index marked
431 INVALID
432 is suffixed
433 _ccnew, then it corresponds to the transient index created during the concurrent operation, and the recommended recovery method is to drop it using
434 DROP INDEX, then attempt
435 \fBREINDEX CONCURRENTLY\fR
436 again\&. If the invalid index is instead suffixed
437 _ccold, it corresponds to the original index which could not be dropped; the recommended recovery method is to just drop said index, since the rebuild proper has been successful\&. A nonzero number may be appended to the suffix of the invalid index names to keep them unique, like
438 _ccnew1,
439 _ccold2, etc\&.
440 .PP
441 Regular index builds permit other regular index builds on the same table to occur simultaneously, but only one concurrent index build can occur on a table at a time\&. In both cases, no other types of schema modification on the table are allowed meanwhile\&. Another difference is that a regular
442 \fBREINDEX TABLE\fR
443 or
444 \fBREINDEX INDEX\fR
445 command can be performed within a transaction block, but
446 \fBREINDEX CONCURRENTLY\fR
447 cannot\&.
448 .PP
449 Like any long\-running transaction,
450 \fBREINDEX\fR
451 on a table can affect which tuples can be removed by concurrent
452 \fBVACUUM\fR
453 on any other table\&.
454 .PP
455 \fBREINDEX SYSTEM\fR
456 does not support
457 \fBCONCURRENTLY\fR
458 since system catalogs cannot be reindexed concurrently\&.
459 .PP
460 Furthermore, indexes for exclusion constraints cannot be reindexed concurrently\&. If such an index is named directly in this command, an error is raised\&. If a table or database with exclusion constraint indexes is reindexed concurrently, those indexes will be skipped\&. (It is possible to reindex such indexes without the
461 \fBCONCURRENTLY\fR
462 option\&.)
463 .PP
464 Each backend running
465 \fBREINDEX\fR
466 will report its progress in the
467 pg_stat_progress_create_index
468 view\&. See
469 Section\ \&27.4.4
470 for details\&.
471 .SH "EXAMPLES"
472 .PP
473 Rebuild a single index:
474 .sp
475 .if n \{\
476 .RS 4
477 .\}
478 .nf
479 REINDEX INDEX my_index;
480 .fi
481 .if n \{\
482 .RE
483 .\}
484 .PP
485 Rebuild all the indexes on the table
486 my_table:
487 .sp
488 .if n \{\
489 .RS 4
490 .\}
491 .nf
492 REINDEX TABLE my_table;
493 .fi
494 .if n \{\
495 .RE
496 .\}
497 .PP
498 Rebuild all indexes in a particular database, without trusting the system indexes to be valid already:
499 .sp
500 .if n \{\
501 .RS 4
502 .\}
503 .nf
504 $ \fBexport PGOPTIONS="\-P"\fR
505 $ \fBpsql broken_db\fR
506 \&.\&.\&.
507 broken_db=> REINDEX DATABASE broken_db;
508 broken_db=> \eq
509 .fi
510 .if n \{\
511 .RE
512 .\}
513 .PP
514 Rebuild indexes for a table, without blocking read and write operations on involved relations while reindexing is in progress:
515 .sp
516 .if n \{\
517 .RS 4
518 .\}
519 .nf
520 REINDEX TABLE CONCURRENTLY my_broken_table;
521 .fi
522 .if n \{\
523 .RE
524 .\}
525 .SH "COMPATIBILITY"
526 .PP
527 There is no
528 \fBREINDEX\fR
529 command in the SQL standard\&.
530 .SH "SEE ALSO"
531 CREATE INDEX (\fBCREATE_INDEX\fR(7)), DROP INDEX (\fBDROP_INDEX\fR(7)), \fBreindexdb\fR(1), Section\ \&27.4.4