summaryrefslogtreecommitdiffstats
path: root/doc/src/sgml/man7/REINDEX.7
diff options
context:
space:
mode:
Diffstat (limited to 'doc/src/sgml/man7/REINDEX.7')
-rw-r--r--doc/src/sgml/man7/REINDEX.7444
1 files changed, 444 insertions, 0 deletions
diff --git a/doc/src/sgml/man7/REINDEX.7 b/doc/src/sgml/man7/REINDEX.7
new file mode 100644
index 0000000..aa48a4e
--- /dev/null
+++ b/doc/src/sgml/man7/REINDEX.7
@@ -0,0 +1,444 @@
+'\" t
+.\" Title: REINDEX
+.\" Author: The PostgreSQL Global Development Group
+.\" Generator: DocBook XSL Stylesheets v1.79.1 <http://docbook.sf.net/>
+.\" Date: 2021
+.\" Manual: PostgreSQL 13.4 Documentation
+.\" Source: PostgreSQL 13.4
+.\" Language: English
+.\"
+.TH "REINDEX" "7" "2021" "PostgreSQL 13.4" "PostgreSQL 13.4 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"
+REINDEX \- rebuild indexes
+.SH "SYNOPSIS"
+.sp
+.nf
+REINDEX [ ( \fIoption\fR [, \&.\&.\&.] ) ] { INDEX | TABLE | SCHEMA | DATABASE | SYSTEM } [ CONCURRENTLY ] \fIname\fR
+
+where \fIoption\fR can be one of:
+
+ VERBOSE
+.fi
+.SH "DESCRIPTION"
+.PP
+\fBREINDEX\fR
+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
+\fBREINDEX\fR:
+.sp
+.RS 4
+.ie n \{\
+\h'-04'\(bu\h'+03'\c
+.\}
+.el \{\
+.sp -1
+.IP \(bu 2.3
+.\}
+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\&.
+\fBREINDEX\fR
+provides a recovery method\&.
+.RE
+.sp
+.RS 4
+.ie n \{\
+\h'-04'\(bu\h'+03'\c
+.\}
+.el \{\
+.sp -1
+.IP \(bu 2.3
+.\}
+An index has become
+\(lqbloated\(rq, that is it contains many empty or nearly\-empty pages\&. This can occur with B\-tree indexes in
+PostgreSQL
+under certain uncommon access patterns\&.
+\fBREINDEX\fR
+provides a way to reduce the space consumption of the index by writing a new version of the index without the dead pages\&. See
+Section\ \&24.2
+for more information\&.
+.RE
+.sp
+.RS 4
+.ie n \{\
+\h'-04'\(bu\h'+03'\c
+.\}
+.el \{\
+.sp -1
+.IP \(bu 2.3
+.\}
+You have altered a storage parameter (such as fillfactor) for an index, and wish to ensure that the change has taken full effect\&.
+.RE
+.sp
+.RS 4
+.ie n \{\
+\h'-04'\(bu\h'+03'\c
+.\}
+.el \{\
+.sp -1
+.IP \(bu 2.3
+.\}
+If an index build fails with the
+CONCURRENTLY
+option, this index is left as
+\(lqinvalid\(rq\&. Such indexes are useless but it can be convenient to use
+\fBREINDEX\fR
+to rebuild them\&. Note that only
+\fBREINDEX INDEX\fR
+is able to perform a concurrent build on an invalid index\&.
+.RE
+.SH "PARAMETERS"
+.PP
+INDEX
+.RS 4
+Recreate the specified index\&.
+.RE
+.PP
+TABLE
+.RS 4
+Recreate all indexes of the specified table\&. If the table has a secondary
+\(lqTOAST\(rq
+table, that is reindexed as well\&.
+.RE
+.PP
+SCHEMA
+.RS 4
+Recreate all indexes of the specified schema\&. If a table of this schema has a secondary
+\(lqTOAST\(rq
+table, that is reindexed as well\&. Indexes on shared system catalogs are also processed\&. This form of
+\fBREINDEX\fR
+cannot be executed inside a transaction block\&.
+.RE
+.PP
+DATABASE
+.RS 4
+Recreate all indexes within the current database\&. Indexes on shared system catalogs are also processed\&. This form of
+\fBREINDEX\fR
+cannot be executed inside a transaction block\&.
+.RE
+.PP
+SYSTEM
+.RS 4
+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
+\fBREINDEX\fR
+cannot be executed inside a transaction block\&.
+.RE
+.PP
+\fIname\fR
+.RS 4
+The name of the specific index, table, or database to be reindexed\&. Index and table names can be schema\-qualified\&. Presently,
+\fBREINDEX DATABASE\fR
+and
+\fBREINDEX SYSTEM\fR
+can only reindex the current database, so their parameter must match the current database\*(Aqs name\&.
+.RE
+.PP
+CONCURRENTLY
+.RS 4
+When this option is used,
+PostgreSQL
+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
+Rebuilding Indexes Concurrently
+below\&.
+.sp
+For temporary tables,
+\fBREINDEX\fR
+is always non\-concurrent, as no other session can access them, and non\-concurrent reindex is cheaper\&.
+.RE
+.PP
+VERBOSE
+.RS 4
+Prints a progress report as each index is reindexed\&.
+.RE
+.SH "NOTES"
+.PP
+If you suspect corruption of an index on a user table, you can simply rebuild that index, or all indexes on the table, using
+\fBREINDEX INDEX\fR
+or
+\fBREINDEX TABLE\fR\&.
+.PP
+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
+\fB\-P\fR
+option, which prevents it from using indexes for system catalog lookups\&.
+.PP
+One way to do this is to shut down the server and start a single\-user
+PostgreSQL
+server with the
+\fB\-P\fR
+option included on its command line\&. Then,
+\fBREINDEX DATABASE\fR,
+\fBREINDEX SYSTEM\fR,
+\fBREINDEX TABLE\fR, or
+\fBREINDEX INDEX\fR
+can be issued, depending on how much you want to reconstruct\&. If in doubt, use
+\fBREINDEX SYSTEM\fR
+to select reconstruction of all system indexes in the database\&. Then quit the single\-user server session and restart the regular server\&. See the
+\fBpostgres\fR(1)
+reference page for more information about how to interact with the single\-user server interface\&.
+.PP
+Alternatively, a regular server session can be started with
+\fB\-P\fR
+included in its command line options\&. The method for doing this varies across clients, but in all
+libpq\-based clients, it is possible to set the
+\fBPGOPTIONS\fR
+environment variable to
+\-P
+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\&.
+.PP
+\fBREINDEX\fR
+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\&.
+\fBREINDEX\fR
+locks out writes but not reads of the index\*(Aqs parent table\&. It also takes an
+ACCESS EXCLUSIVE
+lock on the specific index being processed, which will block reads that attempt to use that index\&. In contrast,
+\fBDROP INDEX\fR
+momentarily takes an
+ACCESS EXCLUSIVE
+lock on the parent table, blocking both writes and reads\&. The subsequent
+\fBCREATE INDEX\fR
+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\&.
+.PP
+Reindexing a single index or table requires being the owner of that index or table\&. Reindexing a schema or database requires being the owner of that schema or database\&. Note specifically that it\*(Aqs thus possible for non\-superusers to rebuild indexes of tables owned by other users\&. However, as a special exception, when
+\fBREINDEX DATABASE\fR,
+\fBREINDEX SCHEMA\fR
+or
+\fBREINDEX SYSTEM\fR
+is issued by a non\-superuser, indexes on shared catalogs will be skipped unless the user owns the catalog (which typically won\*(Aqt be the case)\&. Of course, superusers can always reindex anything\&.
+.PP
+Reindexing partitioned tables or partitioned indexes is not supported\&. Each individual partition can be reindexed separately instead\&.
+.SS "Rebuilding Indexes Concurrently"
+.PP
+Rebuilding an index can interfere with regular operation of a database\&. Normally
+PostgreSQL
+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\&.
+.PP
+PostgreSQL
+supports rebuilding indexes with minimum locking of writes\&. This method is invoked by specifying the
+CONCURRENTLY
+option of
+\fBREINDEX\fR\&. When this option is used,
+PostgreSQL
+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\&.
+.PP
+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\&.
+.sp
+.RS 4
+.ie n \{\
+\h'-04' 1.\h'+01'\c
+.\}
+.el \{\
+.sp -1
+.IP " 1." 4.2
+.\}
+A new transient index definition is added to the catalog
+pg_index\&. This definition will be used to replace the old index\&. A
+SHARE UPDATE EXCLUSIVE
+lock at session level is taken on the indexes being reindexed as well as their associated tables to prevent any schema modification while processing\&.
+.RE
+.sp
+.RS 4
+.ie n \{\
+\h'-04' 2.\h'+01'\c
+.\}
+.el \{\
+.sp -1
+.IP " 2." 4.2
+.\}
+A first pass to build the index is done for each new index\&. Once the index is built, its flag
+pg_index\&.indisready
+is switched to
+\(lqtrue\(rq
+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\&.
+.RE
+.sp
+.RS 4
+.ie n \{\
+\h'-04' 3.\h'+01'\c
+.\}
+.el \{\
+.sp -1
+.IP " 3." 4.2
+.\}
+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\&.
+.RE
+.sp
+.RS 4
+.ie n \{\
+\h'-04' 4.\h'+01'\c
+.\}
+.el \{\
+.sp -1
+.IP " 4." 4.2
+.\}
+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,
+pg_index\&.indisvalid
+is switched to
+\(lqtrue\(rq
+for the new index and to
+\(lqfalse\(rq
+for the old, and a cache invalidation is done causing all sessions that referenced the old index to be invalidated\&.
+.RE
+.sp
+.RS 4
+.ie n \{\
+\h'-04' 5.\h'+01'\c
+.\}
+.el \{\
+.sp -1
+.IP " 5." 4.2
+.\}
+The old indexes have
+pg_index\&.indisready
+switched to
+\(lqfalse\(rq
+to prevent any new tuple insertions, after waiting for running queries that might reference the old index to complete\&.
+.RE
+.sp
+.RS 4
+.ie n \{\
+\h'-04' 6.\h'+01'\c
+.\}
+.el \{\
+.sp -1
+.IP " 6." 4.2
+.\}
+The old indexes are dropped\&. The
+SHARE UPDATE EXCLUSIVE
+session locks for the indexes and the table are released\&.
+.RE
+.PP
+If a problem arises while rebuilding the indexes, such as a uniqueness violation in a unique index, the
+\fBREINDEX\fR
+command will fail but leave behind an
+\(lqinvalid\(rq
+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
+psql
+\fB\ed\fR
+command will report such an index as
+INVALID:
+.sp
+.if n \{\
+.RS 4
+.\}
+.nf
+postgres=# \ed tab
+ Table "public\&.tab"
+ Column | Type | Modifiers
+\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-
+ col | integer |
+Indexes:
+ "idx" btree (col)
+ "idx_ccnew" btree (col) INVALID
+.fi
+.if n \{\
+.RE
+.\}
+.sp
+If the index marked
+INVALID
+is suffixed
+ccnew, then it corresponds to the transient index created during the concurrent operation, and the recommended recovery method is to drop it using
+DROP INDEX, then attempt
+\fBREINDEX CONCURRENTLY\fR
+again\&. If the invalid index is instead suffixed
+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\&.
+.PP
+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
+\fBREINDEX TABLE\fR
+or
+\fBREINDEX INDEX\fR
+command can be performed within a transaction block, but
+\fBREINDEX CONCURRENTLY\fR
+cannot\&.
+.PP
+Like any long\-running transaction,
+\fBREINDEX\fR
+on a table can affect which tuples can be removed by concurrent
+\fBVACUUM\fR
+on any other table\&.
+.PP
+\fBREINDEX SYSTEM\fR
+does not support
+\fBCONCURRENTLY\fR
+since system catalogs cannot be reindexed concurrently\&.
+.PP
+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
+\fBCONCURRENTLY\fR
+option\&.)
+.SH "EXAMPLES"
+.PP
+Rebuild a single index:
+.sp
+.if n \{\
+.RS 4
+.\}
+.nf
+REINDEX INDEX my_index;
+.fi
+.if n \{\
+.RE
+.\}
+.PP
+Rebuild all the indexes on the table
+my_table:
+.sp
+.if n \{\
+.RS 4
+.\}
+.nf
+REINDEX TABLE my_table;
+.fi
+.if n \{\
+.RE
+.\}
+.PP
+Rebuild all indexes in a particular database, without trusting the system indexes to be valid already:
+.sp
+.if n \{\
+.RS 4
+.\}
+.nf
+$ \fBexport PGOPTIONS="\-P"\fR
+$ \fBpsql broken_db\fR
+\&.\&.\&.
+broken_db=> REINDEX DATABASE broken_db;
+broken_db=> \eq
+.fi
+.if n \{\
+.RE
+.\}
+.PP
+Rebuild indexes for a table, without blocking read and write operations on involved relations while reindexing is in progress:
+.sp
+.if n \{\
+.RS 4
+.\}
+.nf
+REINDEX TABLE CONCURRENTLY my_broken_table;
+.fi
+.if n \{\
+.RE
+.\}
+.SH "COMPATIBILITY"
+.PP
+There is no
+\fBREINDEX\fR
+command in the SQL standard\&.
+.SH "SEE ALSO"
+CREATE INDEX (\fBCREATE_INDEX\fR(7)), DROP INDEX (\fBDROP_INDEX\fR(7)), \fBreindexdb\fR(1)