11#! /bin/sh
22# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- #
3- # Package : reindexdb Version : $Revision: 1.4 $
3+ # Package : reindexdb Version : $Revision: 1.5 $
44# Date : 05/08/2002 Author : Shaun Thomas
55# Req : psql, sh, perl, sed Type : Utility
66#
@@ -26,7 +26,7 @@ usage()
2626 echo " -a, --all Reindex all databases"
2727 echo " -t, --table=TABLE Reindex specific table only"
2828 echo " -i, --index=INDEX Reindex specific index only"
29- echo " -e, --echo Show the command being sent to the backend"
29+ echo " -e, --echo Show the command(s) sent to the backend"
3030 echo " -q, --quiet Don't write any output"
3131 echo
3232 echo " Read the description of the SQL command REINDEX for details."
@@ -41,7 +41,7 @@ usage()
4141CMDNAME=` basename " $0 " `
4242PATHNAME=` echo $0 | sed " s,$CMDNAME \$ ,," `
4343
44- # Try valliantly to get the location of psql, since you can't ever
44+ # Try valiantly to get the location of psql, since you can't ever
4545# really know where it has been placed. We'll start by trying the
4646# path. If that fails, we'll try the directory where this script
4747# resides. Then on to whereis, and finally locate. Wish us luck.
9595 ECHOOPT=" -e"
9696 ;;
9797
98- # Do not echo messages. We'll direct all output to /dev/null.
98+ # Do not echo messages.
9999 --quiet|-q)
100- ECHOOPT=" $ECHOOPT -o /dev/null "
100+ ECHOOPT=" -q "
101101 quiet=1
102102 ;;
103103
@@ -172,8 +172,9 @@ if [ "$alldb" ]; then
172172
173173 # Execute a command to pull back all databases the user specified can
174174 # connect to. That's the list we'll be using. It's also why it's
175- # a good idea for this to be a super-user.
176- dbname=` $PSQL $PSQLOPT -q -t -A -d template1 -c ' SELECT datname FROM pg_database WHERE datallowconn' `
175+ # a good idea for this to be run as a super-user.
176+ sql=' SELECT datname FROM pg_database WHERE datallowconn'
177+ dbname=` $PSQL $PSQLOPT -q -t -A -d template1 -c " $sql " `
177178
178179# Ok, if it's not all databases, make sure at least one database is
179180# specified before continuing.
@@ -191,42 +192,61 @@ if [ "$table" ] && [ "$index" ]; then
191192 exit 1
192193fi
193194
194- # If index was set , reindex that index.
195+ # If index was selected , reindex that index.
195196if [ " $index " ]; then
196- $PSQL $PSQLOPT $ECHOOPT -c " REINDEX INDEX $index " -d $dbname
197+ $PSQL $PSQLOPT $ECHOOPT -c " REINDEX INDEX \" $index \" " -d " $dbname "
198+ if [ " $? " -ne 0 ]; then
199+ echo " $CMDNAME : reindex index \" $index \" failed" 1>&2
200+ exit 1
201+ fi
197202
198203# Ok, no index. Is there a specific table to reindex?
199204elif [ " $table " ]; then
200- $PSQL $PSQLOPT $ECHOOPT -c " REINDEX TABLE \" $table \" " -d $dbname
205+ $PSQL $PSQLOPT $ECHOOPT -c " REINDEX TABLE \" $table \" " -d " $dbname "
206+ if [ " $? " -ne 0 ]; then
207+ echo " $CMDNAME : reindex table \" $table \" failed" 1>&2
208+ exit 1
209+ fi
201210
202211# No specific table, no specific index, either we have a specific database,
203212# or were told to do all databases. Do it!
204213else
205214
206- sql=" SELECT distinct tablename FROM pg_indexes WHERE tablename NOT LIKE 'pg_%'"
215+ # We set IFS to newline only so that the for-loops won't misinterpret
216+ # spaces in the lists we retrieved via psql. Note also the use of
217+ # regclass to handle spaces, mixed-case names, and schema awareness.
218+ sql=" SELECT DISTINCT c.oid::pg_catalog.regclass FROM pg_catalog.pg_index x JOIN pg_catalog.pg_class c ON c.oid = x.indrelid JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid WHERE nspname NOT LIKE 'pg\\\\ _%'"
219+
220+ IFS='
221+ '
207222 for db in $dbname ; do
208223
209224 # Only print which database we're currently reindexing if not in
210225 # quiet mode, and we're doing more than one database.
211226 [ " $alldb " ] && [ -z " $quiet " ] && echo " Reindexing $db "
212227
213- # Ok, reindex every table in the database. Use the same method
214- # we used to get a list of databases, and get a list of tables in this
215- # database that we may reindex.
216- tables=` $PSQL $PSQLOPT -q -t -A -d $db -c " $sql " `
228+ IFS='
229+ '
230+ # Get a list of non-system tables that have indexes.
231+ tables=` $PSQL $PSQLOPT -q -t -A -d " $db " -c " $sql " `
232+
233+ # Ok, reindex every table in the database.
234+ IFS='
235+ '
217236 for tab in $tables ; do
218- $PSQL $PSQLOPT $ECHOOPT -c " REINDEX TABLE \" $tab \" " -d $db
237+ IFS='
238+ '
239+ $PSQL $PSQLOPT $ECHOOPT -c " REINDEX TABLE $tab " -d " $db "
240+ if [ " $? " -ne 0 ]; then
241+ echo " $CMDNAME : reindex table $tab failed" 1>&2
242+ exit 1
243+ fi
244+ IFS='
245+ '
219246 done
220247
221248 done
222249
223250fi
224251
225- # If any of the commands we've executed above failed in any way, bail
226- # out with an error.
227- if [ " $? " -ne 0 ]; then
228- echo " $CMDNAME : reindex $index $table $dbname failed" 1>&2
229- exit 1
230- fi
231-
232252exit 0
0 commit comments