Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a56a016

Browse files
committed
Repair some REINDEX problems per recent discussions. The relcache is
now able to cope with assigning new relfilenode values to nailed-in-cache indexes, so they can be reindexed using the fully crash-safe method. This leaves only shared system indexes as special cases. Remove the 'index deactivation' code, since it provides no useful protection in the shared- index case. Require reindexing of shared indexes to be done in standalone mode, but remove other restrictions on REINDEX. -P (IgnoreSystemIndexes) now prevents using indexes for lookups, but does not disable index updates. It is therefore safe to allow from PGOPTIONS. Upshot: reindexing system catalogs can be done without a standalone backend for all cases except shared catalogs.
1 parent 5f78c6a commit a56a016

File tree

22 files changed

+618
-632
lines changed

22 files changed

+618
-632
lines changed

doc/src/sgml/ref/postgres-ref.sgml

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/ref/postgres-ref.sgml,v 1.36 2003/09/18 20:30:15 tgl Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/ref/postgres-ref.sgml,v 1.37 2003/09/24 18:54:01 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -177,9 +177,9 @@ PostgreSQL documentation
177177
<term><option>-P</option></term>
178178
<listitem>
179179
<para>
180-
Ignore system indexes while scanning/updating system tables. The
181-
<command>REINDEX</command> command for system tables/indexes
182-
requires this option to be used.
180+
Ignore system indexes when reading system tables (but still update
181+
the indexes when modifying the tables). This is useful when
182+
recovering from damaged system indexes.
183183
</para>
184184
</listitem>
185185
</varlistentry>

doc/src/sgml/ref/reindex.sgml

+93-48
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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 $
33
PostgreSQL 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>

src/backend/access/index/genam.c

+20-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/access/index/genam.c,v 1.40 2003/08/04 02:39:57 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/access/index/genam.c,v 1.41 2003/09/24 18:54:01 tgl Exp $
1212
*
1313
* NOTES
1414
* many of the old access method routines have been turned into
@@ -184,21 +184,32 @@ systable_beginscan(Relation heapRelation,
184184
int nkeys, ScanKey key)
185185
{
186186
SysScanDesc sysscan;
187+
Relation irel;
188+
189+
if (indexOK && !IsIgnoringSystemIndexes())
190+
{
191+
/* We assume it's a system index, so index_openr is OK */
192+
irel = index_openr(indexRelname);
193+
194+
if (ReindexIsProcessingIndex(RelationGetRelid(irel)))
195+
{
196+
/* oops, can't use index that's being rebuilt */
197+
index_close(irel);
198+
irel = NULL;
199+
}
200+
}
201+
else
202+
irel = NULL;
187203

188204
sysscan = (SysScanDesc) palloc(sizeof(SysScanDescData));
189205

190206
sysscan->heap_rel = heapRelation;
207+
sysscan->irel = irel;
191208

192-
if (indexOK &&
193-
heapRelation->rd_rel->relhasindex &&
194-
!IsIgnoringSystemIndexes())
209+
if (irel)
195210
{
196-
Relation irel;
197211
int i;
198212

199-
/* We assume it's a system index, so index_openr is OK */
200-
sysscan->irel = irel = index_openr(indexRelname);
201-
202213
/*
203214
* Change attribute numbers to be index column numbers.
204215
*
@@ -210,13 +221,13 @@ systable_beginscan(Relation heapRelation,
210221
Assert(key[i].sk_attno == irel->rd_index->indkey[i]);
211222
key[i].sk_attno = i + 1;
212223
}
224+
213225
sysscan->iscan = index_beginscan(heapRelation, irel, snapshot,
214226
nkeys, key);
215227
sysscan->scan = NULL;
216228
}
217229
else
218230
{
219-
sysscan->irel = NULL;
220231
sysscan->scan = heap_beginscan(heapRelation, snapshot, nkeys, key);
221232
sysscan->iscan = NULL;
222233
}

src/backend/access/transam/xact.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/access/transam/xact.c,v 1.152 2003/08/08 21:41:28 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/access/transam/xact.c,v 1.153 2003/09/24 18:54:01 tgl Exp $
1212
*
1313
* NOTES
1414
* Transaction aborts can now occur two ways:
@@ -834,8 +834,6 @@ StartTransaction(void)
834834
*/
835835
s->state = TRANS_START;
836836

837-
SetReindexProcessing(false);
838-
839837
/*
840838
* generate a new transaction id
841839
*/
@@ -1085,6 +1083,7 @@ AbortTransaction(void)
10851083
AtEOXact_Namespace(false);
10861084
AtEOXact_CatCache(false);
10871085
AtEOXact_Files();
1086+
SetReindexProcessing(InvalidOid, InvalidOid);
10881087
pgstat_count_xact_rollback();
10891088

10901089
/*

0 commit comments

Comments
 (0)