reindex.7
来自「PostgreSQL 8.2中增加了很多企业用户所需要的功能和性能上的提高,其开」· 7 代码 · 共 193 行
7
193 行
.\\" auto-generated by docbook2man-spec $Revision: 1.1.1.1 $.TH "REINDEX" "" "2008-01-03" "SQL - Language Statements" "SQL Commands".SH NAMEREINDEX \- rebuild indexes.SH SYNOPSIS.sp.nfREINDEX { INDEX | TABLE | DATABASE | SYSTEM } \fIname\fR [ FORCE ].sp.fi.SH "DESCRIPTION".PP\fBREINDEX\fR rebuilds an index using the datastored in the index's table, replacing the old copy of the index. There areseveral scenarios in which to use \fBREINDEX\fR:.TP 0.2i\(buAn index has become corrupted, and no longer contains validdata. Although in theory this should never happen, inpractice indexes may become corrupted due to software bugs orhardware failures. \fBREINDEX\fR provides arecovery method..TP 0.2i\(buAn index has become ``bloated'', that it is contains manyempty or nearly-empty pages. This can occur with B-tree indexes inPostgreSQL under certain uncommon accesspatterns. \fBREINDEX\fR provides a way to reducethe space consumption of the index by writing a new version ofthe index without the dead pages. See in the documentation for more information..TP 0.2i\(buYou have altered a storage parameter (such as fillfactor)for an index, and wish to ensure that the change has taken full effect..TP 0.2i\(buAn index build with the CONCURRENTLY option failed, leavingan ``invalid'' index. Such indexes are useless but it can beconvenient to use \fBREINDEX\fR to rebuild them. Note that\fBREINDEX\fR will not perform a concurrent build. To build theindex without interfering with production you should drop the index andreissue the \fBCREATE INDEX CONCURRENTLY\fR command..PP.SH "PARAMETERS".TP\fBINDEX\fRRecreate the specified index..TP\fBTABLE\fRRecreate all indexes of the specified table. If the table has asecondary ``TOAST'' table, that is reindexed as well..TP\fBDATABASE\fRRecreate all indexes within the current database.Indexes on shared system catalogs are skipped except in stand-alone mode(see below). This form of \fBREINDEX\fR cannot be executed inside a transaction block..TP\fBSYSTEM\fRRecreate all indexes on system catalogs within the current database.Indexes on user tables are not processed. Also, indexes on sharedsystem catalogs are skipped except in stand-alone mode (see below).This form of \fBREINDEX\fR cannot be executed inside atransaction block..TP\fB\fIname\fB\fRThe name of the specific index, table, or database to bereindexed. Index and table names may be schema-qualified.Presently, \fBREINDEX DATABASE\fR and \fBREINDEX SYSTEM\fRcan only reindex the current database, so their parameter must matchthe current database's name. .TP\fBFORCE\fRThis is an obsolete option; it is ignored if specified..SH "NOTES".PPIf you suspect corruption of an index on a user table, you cansimply rebuild that index, or all indexes on the table, using\fBREINDEX INDEX\fR or \fBREINDEX TABLE\fR. .PPThings are more difficult if you need to recover from corruption ofan index on a system table. In this case it's important for thesystem to not have used any of the suspect indexes itself.(Indeed, in this sort of scenario you may find that serverprocesses are crashing immediately at start-up, due to reliance onthe corrupted indexes.) To recover safely, the server must be startedwith the \fB-P\fR option, which prevents it from usingindexes for system catalog lookups..PPOne way to do this is to shut down the server and start a single-userPostgreSQL serverwith 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 beissued, depending on how much you want to reconstruct. If indoubt, use \fBREINDEX SYSTEM\fR to selectreconstruction of all system indexes in the database. Then quitthe single-user server session and restart the regular server.See the \fBpostgres\fR(1) reference page for moreinformation about how to interact with the single-user serverinterface..PPAlternatively, 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\fBlibpq\fR-based clients, it is possible to setthe \fBPGOPTIONS\fR environment variable to -Pbefore starting the client. Note that while this method does notrequire locking out other clients, it may still be wise to preventother users from connecting to the damaged database until repairshave been completed..PPIf corruption is suspected in the indexes of any of the sharedsystem catalogs (which are \fBpg_authid\fR,\fBpg_auth_members\fR,\fBpg_database\fR,\fBpg_pltemplate\fR,\fBpg_shdepend\fR,\fBpg_shdescription\fR, and\fBpg_tablespace\fR), then a standalone servermust be used to repair it. \fBREINDEX\fR will not processshared catalogs in multiuser mode..PPFor all indexes except the shared system catalogs, \fBREINDEX\fRis crash-safe and transaction-safe. \fBREINDEX\fR is notcrash-safe for shared indexes, which is why this case is disallowedduring normal operation. If a failure occurs while reindexing oneof these catalogs in standalone mode, it will not be possible torestart the regular server until the problem is rectified. (Thetypical symptom of a partially rebuilt shared index is ``index is nota btree'' errors.).PP\fBREINDEX\fR is similar to a drop and recreate of the indexin that the index contents are rebuilt from scratch. However, the lockingconsiderations are rather different. \fBREINDEX\fR locks out writesbut not reads of the index's parent table. It also takes an exclusive lockon the specific index being processed, which will block reads that attemptto use that index. In contrast, \fBDROP INDEX\fR momentarily takesexclusive lock on the parent table, blocking both writes and reads. Thesubsequent \fBCREATE INDEX\fR locks out writes but not reads; sincethe index is not there, no read will attempt to use it, meaning that therewill be no blocking but reads may be forced into expensive sequentialscans. Another important point is that the drop/create approachinvalidates any cached query plans that use the index, while\fBREINDEX\fR does not..PPReindexing a single index or table requires being the owner of thatindex or table. Reindexing a database requires being the owner ofthe database (note that the owner can therefore rebuild indexes oftables owned by other users). Of course, superusers can alwaysreindex anything..PPPrior to PostgreSQL 8.1, \fBREINDEXDATABASE\fR processed only system indexes, not all indexes as one wouldexpect from the name. This has been changed to reduce the surprisefactor. The old behavior is available as \fBREINDEX SYSTEM\fR..PPPrior to PostgreSQL 7.4, \fBREINDEXTABLE\fR did not automatically process TOAST tables, and so those hadto be reindexed by separate commands. This is still possible, butredundant..SH "EXAMPLES".PPRebuild a single index:.sp.nfREINDEX INDEX my_index;.sp.fi.PPRebuild all the indexes on the table my_table:.sp.nfREINDEX TABLE my_table;.sp.fi.PPRebuild all indexes in a particular database, without trusting thesystem indexes to be valid already:.sp.nf$ \fBexport PGOPTIONS="-P"\fR$ \fBpsql broken_db\fR...broken_db=> REINDEX DATABASE broken_db;broken_db=> \\q.sp.fi.SH "COMPATIBILITY".PPThere is no \fBREINDEX\fR command in the SQL standard.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?