OSDN Git Service

FIRST REPOSITORY
[eos/hostdependOTHERS.git] / I686LINUX / util / I686LINUX / doc / postgresql / html / sql-reindex.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >REINDEX</TITLE
6 ><META
7 NAME="GENERATOR"
8 CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
9 REV="MADE"
10 HREF="mailto:pgsql-docs@postgresql.org"><LINK
11 REL="HOME"
12 TITLE="PostgreSQL 7.4.1 Documentation"
13 HREF="index.html"><LINK
14 REL="UP"
15 TITLE="SQL Commands"
16 HREF="sql-commands.html"><LINK
17 REL="PREVIOUS"
18 TITLE="PREPARE"
19 HREF="sql-prepare.html"><LINK
20 REL="NEXT"
21 TITLE="RESET"
22 HREF="sql-reset.html"><LINK
23 REL="STYLESHEET"
24 TYPE="text/css"
25 HREF="stylesheet.css"><META
26 NAME="creation"
27 CONTENT="2003-12-22T03:48:47"></HEAD
28 ><BODY
29 CLASS="REFENTRY"
30 ><DIV
31 CLASS="NAVHEADER"
32 ><TABLE
33 SUMMARY="Header navigation table"
34 WIDTH="100%"
35 BORDER="0"
36 CELLPADDING="0"
37 CELLSPACING="0"
38 ><TR
39 ><TH
40 COLSPAN="5"
41 ALIGN="center"
42 VALIGN="bottom"
43 >PostgreSQL 7.4.1 Documentation</TH
44 ></TR
45 ><TR
46 ><TD
47 WIDTH="10%"
48 ALIGN="left"
49 VALIGN="top"
50 ><A
51 HREF="sql-prepare.html"
52 ACCESSKEY="P"
53 >Prev</A
54 ></TD
55 ><TD
56 WIDTH="10%"
57 ALIGN="left"
58 VALIGN="top"
59 ><A
60 HREF="sql-prepare.html"
61 >Fast Backward</A
62 ></TD
63 ><TD
64 WIDTH="60%"
65 ALIGN="center"
66 VALIGN="bottom"
67 ></TD
68 ><TD
69 WIDTH="10%"
70 ALIGN="right"
71 VALIGN="top"
72 ><A
73 HREF="sql-reset.html"
74 >Fast Forward</A
75 ></TD
76 ><TD
77 WIDTH="10%"
78 ALIGN="right"
79 VALIGN="top"
80 ><A
81 HREF="sql-reset.html"
82 ACCESSKEY="N"
83 >Next</A
84 ></TD
85 ></TR
86 ></TABLE
87 ><HR
88 ALIGN="LEFT"
89 WIDTH="100%"></DIV
90 ><H1
91 ><A
92 NAME="SQL-REINDEX"
93 ></A
94 >REINDEX</H1
95 ><DIV
96 CLASS="REFNAMEDIV"
97 ><A
98 NAME="AEN41376"
99 ></A
100 ><H2
101 >Name</H2
102 >REINDEX&nbsp;--&nbsp;rebuild indexes</DIV
103 ><A
104 NAME="AEN41379"
105 ></A
106 ><DIV
107 CLASS="REFSYNOPSISDIV"
108 ><A
109 NAME="AEN41381"
110 ></A
111 ><H2
112 >Synopsis</H2
113 ><PRE
114 CLASS="SYNOPSIS"
115 >REINDEX { DATABASE | TABLE | INDEX } <VAR
116 CLASS="REPLACEABLE"
117 >name</VAR
118 > [ FORCE ]</PRE
119 ></DIV
120 ><DIV
121 CLASS="REFSECT1"
122 ><A
123 NAME="AEN41384"
124 ></A
125 ><H2
126 >Description</H2
127 ><P
128 >   <TT
129 CLASS="COMMAND"
130 >REINDEX</TT
131 > rebuilds an index based on the data
132    stored in the table, replacing the old copy of the index. There are
133    two main reasons to use <TT
134 CLASS="COMMAND"
135 >REINDEX</TT
136 >:
137
138    <P
139 ></P
140 ></P><UL
141 ><LI
142 ><P
143 >      An index has become corrupted, and no longer contains valid
144       data. Although in theory this should never happen, in
145       practice indexes may become corrupted due to software bugs or
146       hardware failures.  <TT
147 CLASS="COMMAND"
148 >REINDEX</TT
149 > provides a
150       recovery method.
151      </P
152 ></LI
153 ><LI
154 ><P
155 >      The index in question contains a lot of dead index pages that
156       are not being reclaimed. This can occur with B-tree indexes in
157       <SPAN
158 CLASS="PRODUCTNAME"
159 >PostgreSQL</SPAN
160 > under certain access
161       patterns. <TT
162 CLASS="COMMAND"
163 >REINDEX</TT
164 > provides a way to reduce
165       the space consumption of the index by writing a new version of
166       the index without the dead pages. See <A
167 HREF="routine-reindex.html"
168 >Section 21.2</A
169 > for more information.
170      </P
171 ></LI
172 ></UL
173 ><P>
174   </P
175 ></DIV
176 ><DIV
177 CLASS="REFSECT1"
178 ><A
179 NAME="AEN41398"
180 ></A
181 ><H2
182 >Parameters</H2
183 ><P
184 ></P
185 ><DIV
186 CLASS="VARIABLELIST"
187 ><DL
188 ><DT
189 ><TT
190 CLASS="LITERAL"
191 >DATABASE</TT
192 ></DT
193 ><DD
194 ><P
195 >      Recreate all system indexes of a specified database. Indexes on
196       user tables are not processed.  Also, indexes on shared system
197       catalogs are skipped except in stand-alone mode (see below).
198      </P
199 ></DD
200 ><DT
201 ><TT
202 CLASS="LITERAL"
203 >TABLE</TT
204 ></DT
205 ><DD
206 ><P
207 >      Recreate all indexes of a specified table.  If the table has a
208       secondary <SPAN
209 CLASS="QUOTE"
210 >"TOAST"</SPAN
211 > table, that is reindexed as well.
212      </P
213 ></DD
214 ><DT
215 ><TT
216 CLASS="LITERAL"
217 >INDEX</TT
218 ></DT
219 ><DD
220 ><P
221 >      Recreate a specified index.
222      </P
223 ></DD
224 ><DT
225 ><VAR
226 CLASS="REPLACEABLE"
227 >name</VAR
228 ></DT
229 ><DD
230 ><P
231 >      The name of the specific database, table, or index to be
232       reindexed.  Table and index names may be schema-qualified.
233      </P
234 ></DD
235 ><DT
236 ><TT
237 CLASS="LITERAL"
238 >FORCE</TT
239 ></DT
240 ><DD
241 ><P
242 >      This is an obsolete option; it is ignored if specified.
243      </P
244 ></DD
245 ></DL
246 ></DIV
247 ></DIV
248 ><DIV
249 CLASS="REFSECT1"
250 ><A
251 NAME="AEN41427"
252 ></A
253 ><H2
254 >Notes</H2
255 ><P
256 >   If you suspect corruption of an index on a user table, you can
257    simply rebuild that index, or all indexes on the table, using
258    <TT
259 CLASS="COMMAND"
260 >REINDEX INDEX</TT
261 > or <TT
262 CLASS="COMMAND"
263 >REINDEX
264    TABLE</TT
265 >.  Another approach to dealing with a corrupted
266    user-table index is just to drop and recreate it.  This may in fact
267    be preferable if you would like to maintain some semblance of
268    normal operation on the table meanwhile.  <TT
269 CLASS="COMMAND"
270 >REINDEX</TT
271 >
272    acquires exclusive lock on the table, while <TT
273 CLASS="COMMAND"
274 >CREATE
275    INDEX</TT
276 > only locks out writes not reads of the table.
277   </P
278 ><P
279 >   Things are more difficult if you need to recover from corruption of
280    an index on a system table.  In this case it's important for the
281    system to not have used any of the suspect indexes itself.
282    (Indeed, in this sort of scenario you may find that server
283    processes are crashing immediately at start-up, due to reliance on
284    the corrupted indexes.)  To recover safely, the server must be started
285    with the <VAR
286 CLASS="OPTION"
287 >-P</VAR
288 > option, which prevents it from using
289    indexes for system catalog lookups.
290   </P
291 ><P
292 >   One way to do this is to shut down the postmaster and start a stand-alone
293    <SPAN
294 CLASS="PRODUCTNAME"
295 >PostgreSQL</SPAN
296 > server
297    with the <VAR
298 CLASS="OPTION"
299 >-P</VAR
300 > option included on its command line.
301    Then, <TT
302 CLASS="COMMAND"
303 >REINDEX DATABASE</TT
304 >,
305    <TT
306 CLASS="COMMAND"
307 >REINDEX TABLE</TT
308 >, or <TT
309 CLASS="COMMAND"
310 >REINDEX INDEX</TT
311 > can be
312    issued, depending on how much you want to reconstruct.  If in
313    doubt, use <TT
314 CLASS="COMMAND"
315 >REINDEX DATABASE</TT
316 > to select
317    reconstruction of all system indexes in the database.  Then quit
318    the standalone server session and restart the regular server.
319    See the <A
320 HREF="app-postgres.html"
321 ><SPAN
322 CLASS="APPLICATION"
323 >postgres</SPAN
324 ></A
325 > reference page for more
326    information about how to interact with the stand-alone server
327    interface.
328   </P
329 ><P
330 >   Alternatively, a regular server session can be started with
331    <VAR
332 CLASS="OPTION"
333 >-P</VAR
334 > included in its command line options.
335    The method for doing this varies across clients, but in all
336    <SPAN
337 CLASS="APPLICATION"
338 >libpq</SPAN
339 >-based clients, it is possible to set
340    the <TT
341 CLASS="ENVAR"
342 >PGOPTIONS</TT
343 > environment variable to <TT
344 CLASS="LITERAL"
345 >-P</TT
346 >
347    before starting the client.  Note that while this method does not
348    require locking out other clients, it may still be wise to prevent
349    other users from connecting to the damaged database until repairs
350    have been completed.
351   </P
352 ><P
353 >   If corruption is suspected in the indexes of any of the shared
354    system catalogs (<TT
355 CLASS="STRUCTNAME"
356 >pg_database</TT
357 >,
358    <TT
359 CLASS="STRUCTNAME"
360 >pg_group</TT
361 >, or
362    <TT
363 CLASS="STRUCTNAME"
364 >pg_shadow</TT
365 >), then a standalone server
366    must be used to repair it.  <TT
367 CLASS="COMMAND"
368 >REINDEX</TT
369 > will not process
370    shared catalogs in multiuser mode.
371   </P
372 ><P
373 >   For all indexes except the shared system catalogs, <TT
374 CLASS="COMMAND"
375 >REINDEX</TT
376 >
377    is crash-safe and transaction-safe.  <TT
378 CLASS="COMMAND"
379 >REINDEX</TT
380 > is not
381    crash-safe for shared indexes, which is why this case is disallowed
382    during normal operation.  If a failure occurs while reindexing one
383    of these catalogs in standalone mode, it will not be possible to
384    restart the regular server until the problem is rectified.  (The
385    typical symptom of a partially rebuilt shared index is <SPAN
386 CLASS="QUOTE"
387 >"index is not
388    a btree"</SPAN
389 > errors.)
390   </P
391 ><P
392 >   Prior to <SPAN
393 CLASS="PRODUCTNAME"
394 >PostgreSQL</SPAN
395 > 7.4, <TT
396 CLASS="COMMAND"
397 >REINDEX
398    TABLE</TT
399 > did not automatically process TOAST tables, and so those had
400    to be reindexed by separate commands.  This is still possible, but
401    redundant.
402   </P
403 ></DIV
404 ><DIV
405 CLASS="REFSECT1"
406 ><A
407 NAME="AEN41461"
408 ></A
409 ><H2
410 >Examples</H2
411 ><P
412 >   Recreate the indexes on the table <TT
413 CLASS="LITERAL"
414 >my_table</TT
415 >:
416
417 </P><PRE
418 CLASS="PROGRAMLISTING"
419 >REINDEX TABLE my_table;</PRE
420 ><P>
421   </P
422 ><P
423 >   Rebuild a single index:
424
425 </P><PRE
426 CLASS="PROGRAMLISTING"
427 >REINDEX INDEX my_index;</PRE
428 ><P>
429   </P
430 ><P
431 >   Rebuild all system indexes in a particular database, without trusting them
432    to be valid already:
433
434 </P><PRE
435 CLASS="PROGRAMLISTING"
436 >$ <KBD
437 CLASS="USERINPUT"
438 >export PGOPTIONS="-P"</KBD
439 >
440 $ <KBD
441 CLASS="USERINPUT"
442 >psql broken_db</KBD
443 >
444 ...
445 broken_db=&#62; REINDEX DATABASE broken_db;
446 broken_db=&#62; \q</PRE
447 ><P>
448   </P
449 ></DIV
450 ><DIV
451 CLASS="REFSECT1"
452 ><A
453 NAME="AEN41472"
454 ></A
455 ><H2
456 >Compatibility</H2
457 ><P
458 >   There is no <TT
459 CLASS="COMMAND"
460 >REINDEX</TT
461 > command in the SQL standard.
462   </P
463 ></DIV
464 ><DIV
465 CLASS="NAVFOOTER"
466 ><HR
467 ALIGN="LEFT"
468 WIDTH="100%"><TABLE
469 SUMMARY="Footer navigation table"
470 WIDTH="100%"
471 BORDER="0"
472 CELLPADDING="0"
473 CELLSPACING="0"
474 ><TR
475 ><TD
476 WIDTH="33%"
477 ALIGN="left"
478 VALIGN="top"
479 ><A
480 HREF="sql-prepare.html"
481 ACCESSKEY="P"
482 >Prev</A
483 ></TD
484 ><TD
485 WIDTH="34%"
486 ALIGN="center"
487 VALIGN="top"
488 ><A
489 HREF="index.html"
490 ACCESSKEY="H"
491 >Home</A
492 ></TD
493 ><TD
494 WIDTH="33%"
495 ALIGN="right"
496 VALIGN="top"
497 ><A
498 HREF="sql-reset.html"
499 ACCESSKEY="N"
500 >Next</A
501 ></TD
502 ></TR
503 ><TR
504 ><TD
505 WIDTH="33%"
506 ALIGN="left"
507 VALIGN="top"
508 >PREPARE</TD
509 ><TD
510 WIDTH="34%"
511 ALIGN="center"
512 VALIGN="top"
513 ><A
514 HREF="sql-commands.html"
515 ACCESSKEY="U"
516 >Up</A
517 ></TD
518 ><TD
519 WIDTH="33%"
520 ALIGN="right"
521 VALIGN="top"
522 >RESET</TD
523 ></TR
524 ></TABLE
525 ></DIV
526 ></BODY
527 ></HTML
528 >