Skip to content

Commit b2711a0

Browse files
committed
> BTW, clusterdb is not schema-aware and will surely fail in any database
> where more than one schema is in use, because it doesn't trouble to > schema-qualify table names. Ok, the following patch should solve this concern. It also tries to connect as little times as possible (the previous one would connect one time per table plus one per database; this one connects two times per database). Alvaro Herrera
1 parent 5dd74c0 commit b2711a0

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

src/bin/scripts/clusterdb

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#
1212
#
1313
# IDENTIFICATION
14-
# $Header: /cvsroot/pgsql/src/bin/scripts/Attic/clusterdb,v 1.2 2002/09/07 16:12:27 petere Exp $
14+
# $Header: /cvsroot/pgsql/src/bin/scripts/Attic/clusterdb,v 1.3 2002/09/12 00:18:14 momjian Exp $
1515
#
1616
#-------------------------------------------------------------------------
1717

@@ -121,7 +121,7 @@ if [ "$usage" ]; then
121121
echo " -W, --password Prompt for password"
122122
echo " -d, --dbname=DBNAME Database to cluster"
123123
echo " -a, --all Cluster all databases"
124-
echo " -t, --table='TABLE[(columns)]' Cluster specific table only"
124+
echo " -t, --table='TABLE' Cluster specific table only"
125125
echo " -v, --verbose Write a lot of output"
126126
echo " -e, --echo Show the command being sent to the backend"
127127
echo " -q, --quiet Don't write any output"
@@ -152,25 +152,35 @@ fi
152152

153153
for db in $dbname
154154
do
155-
[ "$alldb" -a "$quiet" -ne 1 ] && echo "Clustering $db"
156-
query="SELECT pg_class.relname, pg_class_2.relname FROM pg_class, \
157-
pg_class AS pg_class_2, pg_index WHERE pg_class.oid=pg_index.indrelid\
158-
AND pg_class_2.oid=pg_index.indexrelid AND pg_index.indisclustered"
155+
[ "$alldb" ] && echo "Clustering $db"
156+
query="SELECT nspname, pg_class.relname, pg_class_2.relname FROM pg_class, pg_class AS pg_class_2 JOIN pg_namespace ON (pg_namespace.oid=relnamespace), pg_index WHERE pg_class.oid=pg_index.indrelid AND pg_class_2.oid=pg_index.indexrelid AND pg_index.indisclustered"
159157
if [ -z "$table" ]; then
160158
tables=`${PATHNAME}psql $db $PSQLOPT -F: -P format=unaligned -t -c "$query"`
161159
else
162-
tables=`${PATHNAME}psql $db $PSQLOPT -F: -P format=unaligned -t -c \
163-
"$query AND pg_class.relname='$table'"`
160+
# if tablename has a dot, use it as namespace separator
161+
if echo $table | grep -s '\.' 2>&1 >/dev/null
162+
then
163+
tbl=`echo $table | cut -d. -f2`
164+
nspc=`echo $table | cut -d. -f1`
165+
tables=`${PATHNAME}psql $db $PSQLOPT -F: -P format=unaligned -t -c "$query AND pg_class.relname='$tbl' AND nspname='$nspc'"`
166+
echo $tables
167+
else
168+
tables=`${PATHNAME}psql $db $PSQLOPT -F: -P format=unaligned -t -c "$query AND pg_class.relname='$table'"`
169+
fi
164170
fi
171+
query=
165172
for tabs in $tables
166173
do
167-
tab=`echo $tabs | cut -d: -f1`
168-
idx=`echo $tabs | cut -d: -f2`
169-
${PATHNAME}psql $PSQLOPT $ECHOOPT -c "CLUSTER $idx on $tab" -d $db
170-
if [ "$?" -ne 0 ]; then
171-
echo "$CMDNAME: cluster $table $db failed" 1>&2
172-
fi
174+
nspc=`echo $tabs | cut -d: -f1`
175+
tab=`echo $tabs | cut -d: -f2`
176+
idx=`echo $tabs | cut -d: -f3`
177+
query="$query CLUSTER $idx ON $nspc.$tab;"
173178
done
179+
${PATHNAME}psql $PSQLOPT $ECHOOPT -c "$query" -d $db
180+
if [ "$?" -ne 0 ]
181+
then
182+
echo "$CMDNAME: While clustering $db, the following failed: $query" 1>&2
183+
fi
174184
done
175185

176186
exit 0

0 commit comments

Comments
 (0)