reindex.sgml 10.1 KB
Newer Older
Hiroshi Inoue's avatar
Hiroshi Inoue committed
1
<!--
2
$PostgreSQL: pgsql/doc/src/sgml/ref/reindex.sgml,v 1.34 2006/10/31 01:52:31 neilc Exp $
3
PostgreSQL documentation
Hiroshi Inoue's avatar
Hiroshi Inoue committed
4 5 6 7
-->

<refentry id="SQL-REINDEX">
 <refmeta>
8
  <refentrytitle id="SQL-REINDEX-TITLE">REINDEX</refentrytitle>
Hiroshi Inoue's avatar
Hiroshi Inoue committed
9 10
  <refmiscinfo>SQL - Language Statements</refmiscinfo>
 </refmeta>
11

Hiroshi Inoue's avatar
Hiroshi Inoue committed
12
 <refnamediv>
13 14
  <refname>REINDEX</refname>
  <refpurpose>rebuild indexes</refpurpose>
Hiroshi Inoue's avatar
Hiroshi Inoue committed
15
 </refnamediv>
16

Peter Eisentraut's avatar
Peter Eisentraut committed
17 18 19 20
 <indexterm zone="sql-reindex">
  <primary>REINDEX</primary>
 </indexterm>

Hiroshi Inoue's avatar
Hiroshi Inoue committed
21
 <refsynopsisdiv>
22
<synopsis>
23
REINDEX { INDEX | TABLE | DATABASE | SYSTEM } <replaceable class="PARAMETER">name</replaceable> [ FORCE ]
24
</synopsis>
Hiroshi Inoue's avatar
Hiroshi Inoue committed
25 26
 </refsynopsisdiv>

27 28 29
 <refsect1>
  <title>Description</title>

Hiroshi Inoue's avatar
Hiroshi Inoue committed
30
  <para>
31
   <command>REINDEX</command> rebuilds an index using the data
32
   stored in the index's table, replacing the old copy of the index. There are
33
   several scenarios in which to use <command>REINDEX</command>:
34

35
   <itemizedlist>
36 37 38
    <listitem>
     <para>
      An index has become corrupted, and no longer contains valid
39
      data. Although in theory this should never happen, in
40 41 42 43 44 45 46 47
      practice indexes may become corrupted due to software bugs or
      hardware failures.  <command>REINDEX</command> provides a
      recovery method.
     </para>
    </listitem>

    <listitem>
     <para>
48 49 50
      An index has become <quote>bloated</>, that it is contains many
      empty or nearly-empty pages.  This can occur with B-tree indexes in
      <productname>PostgreSQL</productname> under certain uncommon access
51 52 53 54
      patterns. <command>REINDEX</command> provides a way to reduce
      the space consumption of the index by writing a new version of
      the index without the dead pages. See <xref
      linkend="routine-reindex"> for more information.
55 56
     </para>
    </listitem>
57 58 59 60 61 62 63

    <listitem>
     <para>
      You have altered a storage parameter (such as fillfactor)
      for an index, and wish to ensure that the change has taken full effect.
     </para>
    </listitem>
64 65 66 67 68 69 70 71 72 73 74 75

    <listitem>
     <para>
      An index build with the <literal>CONCURRENTLY</> option failed, leaving
      an <quote>invalid</> index. Such indexes are useless but it can be
      convenient to use <command>REINDEX</> to rebuild them. Note that
      <command>REINDEX</> will not perform a concurrent build. To build the
      index without interfering with production you should drop the index and
      reissue the <command>CREATE INDEX CONCURRENTLY</> command.
     </para>
    </listitem>

76
   </itemizedlist>
77
  </para>
78 79 80 81
 </refsect1>
  
 <refsect1>
  <title>Parameters</title>
82

83 84
  <variablelist>
   <varlistentry>
85
    <term><literal>INDEX</literal></term>
86 87
    <listitem>
     <para>
88
      Recreate the specified index.
89 90
     </para>
    </listitem>
91
   </varlistentry>
92

93 94
   <varlistentry>
    <term><literal>TABLE</literal></term>
95 96
    <listitem>
     <para>
97
      Recreate all indexes of the specified table.  If the table has a
98
      secondary <quote>TOAST</> table, that is reindexed as well.
99 100
     </para>
    </listitem>
101
   </varlistentry>
102

103
   <varlistentry>
104
    <term><literal>DATABASE</literal></term>
105 106
    <listitem>
     <para>
107 108
      Recreate all indexes within the current database.
      Indexes on shared system catalogs are skipped except in stand-alone mode
109 110
      (see below). This form of <command>REINDEX</command> cannot be executed 
      inside a transaction block.
111 112 113 114 115 116 117 118 119 120 121
     </para>
    </listitem>
   </varlistentry>

   <varlistentry>
    <term><literal>SYSTEM</literal></term>
    <listitem>
     <para>
      Recreate all indexes on system catalogs within the current database.
      Indexes on user tables are not processed.  Also, indexes on shared
      system catalogs are skipped except in stand-alone mode (see below).
122 123
      This form of <command>REINDEX</command> cannot be executed inside a
      transaction block.
124 125
     </para>
    </listitem>
126
   </varlistentry>
127

128 129
   <varlistentry>
    <term><replaceable class="PARAMETER">name</replaceable></term>
130 131
    <listitem>
     <para>
132 133 134 135 136
      The name of the specific index, table, or database to be
      reindexed.  Index and table names may be schema-qualified.
      Presently, <command>REINDEX DATABASE</> and <command>REINDEX SYSTEM</>
      can only reindex the current database, so their parameter must match
      the current database's name. 
137 138
     </para>
    </listitem>
139
   </varlistentry>
140

141 142 143 144
   <varlistentry>
    <term><literal>FORCE</literal></term>
    <listitem>
     <para>
145
      This is an obsolete option; it is ignored if specified.
146 147 148 149 150
     </para>
    </listitem>
   </varlistentry>
  </variablelist>
 </refsect1>
Hiroshi Inoue's avatar
Hiroshi Inoue committed
151

152 153 154 155 156 157
 <refsect1>
  <title>Notes</title>

  <para>
   If you suspect corruption of an index on a user table, you can
   simply rebuild that index, or all indexes on the table, using
158
   <command>REINDEX INDEX</command> or <command>REINDEX TABLE</command>.  
159 160 161 162 163 164 165 166 167 168 169 170 171 172
  </para>

  <para>
   Things are more difficult if you need to recover from corruption of
   an index on a system table.  In this case it's important for the
   system to not have used any of the suspect indexes itself.
   (Indeed, in this sort of scenario you may 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 <option>-P</option> option, which prevents it from using
   indexes for system catalog lookups.
  </para>

  <para>
173
   One way to do this is to shut down the server and start a single-user
174 175
   <productname>PostgreSQL</productname> server
   with the <option>-P</option> option included on its command line.
176
   Then, <command>REINDEX DATABASE</>, <command>REINDEX SYSTEM</>,
177 178
   <command>REINDEX TABLE</>, or <command>REINDEX INDEX</> can be
   issued, depending on how much you want to reconstruct.  If in
179
   doubt, use <command>REINDEX SYSTEM</> to select
180
   reconstruction of all system indexes in the database.  Then quit
181
   the single-user server session and restart the regular server.
182
   See the <xref linkend="app-postgres"> reference page for more
183
   information about how to interact with the single-user server
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
   interface.
  </para>

  <para>
   Alternatively, a regular server session can be started with
   <option>-P</option> included in its command line options.
   The method for doing this varies across clients, but in all
   <application>libpq</>-based clients, it is possible to set
   the <envar>PGOPTIONS</envar> environment variable to <literal>-P</>
   before starting the client.  Note that while this method does not
   require locking out other clients, it may still be wise to prevent
   other users from connecting to the damaged database until repairs
   have been completed.
  </para>

  <para>
   If corruption is suspected in the indexes of any of the shared
201
   system catalogs (which are <structname>pg_authid</structname>,
202 203
   <structname>pg_auth_members</structname>,
   <structname>pg_database</structname>,
204 205
   <structname>pg_pltemplate</structname>,
   <structname>pg_shdepend</structname>, and
206
   <structname>pg_tablespace</structname>), then a standalone server
207 208 209 210 211 212 213 214 215
   must be used to repair it.  <command>REINDEX</> will not process
   shared catalogs in multiuser mode.
  </para>

  <para>
   For all indexes except the shared system catalogs, <command>REINDEX</>
   is crash-safe and transaction-safe.  <command>REINDEX</> is not
   crash-safe for shared indexes, which is why this case is disallowed
   during normal operation.  If a failure occurs while reindexing one
216 217 218 219
   of these catalogs in standalone mode, it will not be possible to
   restart the regular server until the problem is rectified.  (The
   typical symptom of a partially rebuilt shared index is <quote>index is not
   a btree</> errors.)
220 221
  </para>

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
  <para>
   <command>REINDEX</command> 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.  <command>REINDEX</> locks out writes
   but not reads of the index's parent table.  It also takes an exclusive lock
   on the specific index being processed, which will block reads that attempt
   to use that index.  In contrast, <command>DROP INDEX</> momentarily takes
   exclusive lock on the parent table, blocking both writes and reads.  The
   subsequent <command>CREATE INDEX</> 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 may be forced into expensive sequential
   scans.  Another important point is that the drop/create approach
   invalidates any cached query plans that use the index, while
   <command>REINDEX</> does not.
  </para>

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
  <para>
   Reindexing a single index or table requires being the owner of that
   index or table.  Reindexing a database requires being the owner of
   the database (note that the owner can therefore rebuild indexes of
   tables owned by other users).  Of course, superusers can always
   reindex anything.
  </para>

  <para>
   Prior to <productname>PostgreSQL</productname> 8.1, <command>REINDEX
   DATABASE</> processed only system indexes, not all indexes as one would
   expect from the name.  This has been changed to reduce the surprise
   factor.  The old behavior is available as <command>REINDEX SYSTEM</>.
  </para>

253 254 255 256 257 258 259 260
  <para>
   Prior to <productname>PostgreSQL</productname> 7.4, <command>REINDEX
   TABLE</> did not automatically process TOAST tables, and so those had
   to be reindexed by separate commands.  This is still possible, but
   redundant.
  </para>
 </refsect1>

261 262 263
 <refsect1>
  <title>Examples</title>

Hiroshi Inoue's avatar
Hiroshi Inoue committed
264
  <para>
265
   Rebuild a single index:
Hiroshi Inoue's avatar
Hiroshi Inoue committed
266

267
<programlisting>
268
REINDEX INDEX my_index;
269
</programlisting>
Hiroshi Inoue's avatar
Hiroshi Inoue committed
270 271 272
  </para>

  <para>
273
   Rebuild all the indexes on the table <literal>my_table</literal>:
274

275
<programlisting>
276
REINDEX TABLE my_table;
277
</programlisting>
278 279 280
  </para>

  <para>
281 282
   Rebuild all indexes in a particular database, without trusting the
   system indexes to be valid already:
Hiroshi Inoue's avatar
Hiroshi Inoue committed
283

284
<programlisting>
285 286 287
$ <userinput>export PGOPTIONS="-P"</userinput>
$ <userinput>psql broken_db</userinput>
...
288 289
broken_db=&gt; REINDEX DATABASE broken_db;
broken_db=&gt; \q
290
</programlisting>
Hiroshi Inoue's avatar
Hiroshi Inoue committed
291 292 293
  </para>
 </refsect1>
 
294 295 296 297 298 299
 <refsect1>
  <title>Compatibility</title>

  <para>
   There is no <command>REINDEX</command> command in the SQL standard.
  </para>
Hiroshi Inoue's avatar
Hiroshi Inoue committed
300 301
 </refsect1>
</refentry>