11<!--
2- $Header: /cvsroot/pgsql/doc/src/sgml/ref/reindex.sgml,v 1.20 2003/09/11 21:42:20 momjian Exp $
2+ $Header: /cvsroot/pgsql/doc/src/sgml/ref/reindex.sgml,v 1.21 2003/09/24 18:54:01 tgl Exp $
33PostgreSQL documentation
44-->
55
@@ -56,43 +56,6 @@ REINDEX { DATABASE | TABLE | INDEX } <replaceable class="PARAMETER">name</replac
5656 </listitem>
5757 </itemizedlist>
5858 </para>
59-
60- <para>
61- If you suspect corruption of an index on a user table, you can
62- simply rebuild that index, or all indexes on the table, using
63- <command>REINDEX INDEX</command> or <command>REINDEX
64- TABLE</command>. Another approach to dealing with a corrupted
65- user-table index is just to drop and recreate it. This may in fact
66- be preferable if you would like to maintain some semblance of
67- normal operation on the table meanwhile. <command>REINDEX</>
68- acquires exclusive lock on the table, while <command>CREATE
69- INDEX</> only locks out writes not reads of the table.
70- </para>
71-
72- <para>
73- Things are more difficult if you need to recover from corruption of
74- an index on a system table. In this case it's important for the
75- system to not have used any of the suspect indexes itself.
76- (Indeed, in this sort of scenario you may find that server
77- processes are crashing immediately at start-up, due to reliance on
78- the corrupted indexes.) To recover safely, the server must be shut
79- down and a stand-alone <productname>PostgreSQL</productname> server
80- must be started instead with the command-line options
81- <option>-O</option> and <option>-P</option>. (These options allow
82- system table modifications and prevent use of system indexes,
83- respectively.) Then, <command>REINDEX DATABASE</>,
84- <command>REINDEX TABLE</>, or <command>REINDEX INDEX</> can be
85- issued, depending on how much you want to reconstruct. If in
86- doubt, use <command>REINDEX DATABASE FORCE</> to force
87- reconstruction of all system indexes in the database. Then quit
88- the standalone server session and restart the real server.
89- </para>
90-
91- <para>
92- See the <xref linkend="app-postgres"> reference page for more
93- information about how to interact with the stand-alone server
94- interface.
95- </para>
9659 </refsect1>
9760
9861 <refsect1>
@@ -104,8 +67,8 @@ REINDEX { DATABASE | TABLE | INDEX } <replaceable class="PARAMETER">name</replac
10467 <listitem>
10568 <para>
10669 Recreate all system indexes of a specified database. Indexes on
107- user tables are not included. This form of <command>REINDEX</>
108- can only be used in stand-alone mode (see above ).
70+ user tables are not processed. Also, indexes on shared system
71+ catalogs are skipped except in stand-alone mode (see below ).
10972 </para>
11073 </listitem>
11174 </varlistentry>
@@ -114,7 +77,8 @@ REINDEX { DATABASE | TABLE | INDEX } <replaceable class="PARAMETER">name</replac
11477 <term><literal>TABLE</literal></term>
11578 <listitem>
11679 <para>
117- Recreate all indexes of a specified table.
80+ Recreate all indexes of a specified table. If the table has a
81+ secondary <quote>TOAST</> table, that is reindexed as well.
11882 </para>
11983 </listitem>
12084 </varlistentry>
@@ -142,16 +106,93 @@ REINDEX { DATABASE | TABLE | INDEX } <replaceable class="PARAMETER">name</replac
142106 <term><literal>FORCE</literal></term>
143107 <listitem>
144108 <para>
145- Force rebuild of system indexes. Without this key word,
146- <command>REINDEX</> skips system indexes that are not marked
147- invalid. <literal>FORCE</> is irrelevant for <command>REINDEX
148- INDEX</> or when reindexing user indexes.
109+ This is an obsolete option; it is ignored if specified.
149110 </para>
150111 </listitem>
151112 </varlistentry>
152113 </variablelist>
153114 </refsect1>
154115
116+ <refsect1>
117+ <title>Notes</title>
118+
119+ <para>
120+ If you suspect corruption of an index on a user table, you can
121+ simply rebuild that index, or all indexes on the table, using
122+ <command>REINDEX INDEX</command> or <command>REINDEX
123+ TABLE</command>. Another approach to dealing with a corrupted
124+ user-table index is just to drop and recreate it. This may in fact
125+ be preferable if you would like to maintain some semblance of
126+ normal operation on the table meanwhile. <command>REINDEX</>
127+ acquires exclusive lock on the table, while <command>CREATE
128+ INDEX</> only locks out writes not reads of the table.
129+ </para>
130+
131+ <para>
132+ Things are more difficult if you need to recover from corruption of
133+ an index on a system table. In this case it's important for the
134+ system to not have used any of the suspect indexes itself.
135+ (Indeed, in this sort of scenario you may find that server
136+ processes are crashing immediately at start-up, due to reliance on
137+ the corrupted indexes.) To recover safely, the server must be started
138+ with the <option>-P</option> option, which prevents it from using
139+ indexes for system catalog lookups.
140+ </para>
141+
142+ <para>
143+ One way to do this is to shut down the postmaster and start a stand-alone
144+ <productname>PostgreSQL</productname> server
145+ with the <option>-P</option> option included on its command line.
146+ Then, <command>REINDEX DATABASE</>,
147+ <command>REINDEX TABLE</>, or <command>REINDEX INDEX</> can be
148+ issued, depending on how much you want to reconstruct. If in
149+ doubt, use <command>REINDEX DATABASE</> to select
150+ reconstruction of all system indexes in the database. Then quit
151+ the standalone server session and restart the regular server.
152+ See the <xref linkend="app-postgres"> reference page for more
153+ information about how to interact with the stand-alone server
154+ interface.
155+ </para>
156+
157+ <para>
158+ Alternatively, a regular server session can be started with
159+ <option>-P</option> included in its command line options.
160+ The method for doing this varies across clients, but in all
161+ <application>libpq</>-based clients, it is possible to set
162+ the <envar>PGOPTIONS</envar> environment variable to <literal>-P</>
163+ before starting the client. Note that while this method does not
164+ require locking out other clients, it may still be wise to prevent
165+ other users from connecting to the damaged database until repairs
166+ have been completed.
167+ </para>
168+
169+ <para>
170+ If corruption is suspected in the indexes of any of the shared
171+ system catalogs (<structname>pg_database</structname>,
172+ <structname>pg_group</structname>, or
173+ <structname>pg_shadow</structname>), then a standalone server
174+ must be used to repair it. <command>REINDEX</> will not process
175+ shared catalogs in multiuser mode.
176+ </para>
177+
178+ <para>
179+ For all indexes except the shared system catalogs, <command>REINDEX</>
180+ is crash-safe and transaction-safe. <command>REINDEX</> is not
181+ crash-safe for shared indexes, which is why this case is disallowed
182+ during normal operation. If a failure occurs while reindexing one
183+ of these catalogs in standalone mode, it is important that the failure
184+ be rectified and the <command>REINDEX</> operation redone
185+ before attempting to restart the regular server.
186+ </para>
187+
188+ <para>
189+ Prior to <productname>PostgreSQL</productname> 7.4, <command>REINDEX
190+ TABLE</> did not automatically process TOAST tables, and so those had
191+ to be reindexed by separate commands. This is still possible, but
192+ redundant.
193+ </para>
194+ </refsect1>
195+
155196 <refsect1>
156197 <title>Examples</title>
157198
@@ -172,11 +213,15 @@ REINDEX INDEX my_index;
172213 </para>
173214
174215 <para>
175- Rebuild all system indexes (this will only work in a stand-alone
176- server session) :
216+ Rebuild all system indexes in a particular database, without trusting them
217+ to be valid already :
177218
178219<programlisting>
179- REINDEX DATABASE my_database FORCE;
220+ $ <userinput>export PGOPTIONS="-P"</userinput>
221+ $ <userinput>psql broken_db</userinput>
222+ ...
223+ broken_db=> REINDEX DATABASE broken_db;
224+ broken_db=> \q
180225</programlisting>
181226 </para>
182227 </refsect1>
0 commit comments