From b2711a0aee509af9420a2e6e005739e8da3ce606 Mon Sep 17 00:00:00 2001
From: Bruce Momjian <bruce@momjian.us>
Date: Thu, 12 Sep 2002 00:18:14 +0000
Subject: [PATCH] > 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
---
 src/bin/scripts/clusterdb | 38 ++++++++++++++++++++++++--------------
 1 file changed, 24 insertions(+), 14 deletions(-)

diff --git a/src/bin/scripts/clusterdb b/src/bin/scripts/clusterdb
index 33d5967dde..dd3021ae7b 100644
--- a/src/bin/scripts/clusterdb
+++ b/src/bin/scripts/clusterdb
@@ -11,7 +11,7 @@
 #
 #
 # IDENTIFICATION
-#    $Header: /cvsroot/pgsql/src/bin/scripts/Attic/clusterdb,v 1.2 2002/09/07 16:12:27 petere Exp $
+#    $Header: /cvsroot/pgsql/src/bin/scripts/Attic/clusterdb,v 1.3 2002/09/12 00:18:14 momjian Exp $
 #
 #-------------------------------------------------------------------------
 
@@ -121,7 +121,7 @@ if [ "$usage" ]; then
 	echo "  -W, --password                  Prompt for password"
 	echo "  -d, --dbname=DBNAME             Database to cluster"
 	echo "  -a, --all                       Cluster all databases"
-	echo "  -t, --table='TABLE[(columns)]'  Cluster specific table only"
+	echo "  -t, --table='TABLE'             Cluster specific table only"
 	echo "  -v, --verbose                   Write a lot of output"
 	echo "  -e, --echo                      Show the command being sent to the backend"
 	echo "  -q, --quiet                     Don't write any output"
@@ -152,25 +152,35 @@ fi
 
 for db in $dbname
 do
-	[ "$alldb" -a "$quiet" -ne 1 ] && echo "Clustering $db"
-	query="SELECT pg_class.relname, pg_class_2.relname FROM pg_class, \
-		pg_class AS pg_class_2, pg_index WHERE pg_class.oid=pg_index.indrelid\
-		AND pg_class_2.oid=pg_index.indexrelid AND pg_index.indisclustered"
+	[ "$alldb" ] && echo "Clustering $db"
+	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"
 	if [ -z "$table" ]; then
 		tables=`${PATHNAME}psql $db $PSQLOPT -F: -P format=unaligned -t -c "$query"`
 	else
-		tables=`${PATHNAME}psql $db $PSQLOPT -F: -P format=unaligned -t -c \
-		"$query AND pg_class.relname='$table'"`
+		# if tablename has a dot, use it as namespace separator
+		if echo $table | grep -s '\.' 2>&1 >/dev/null
+		then
+			tbl=`echo $table | cut -d. -f2`
+			nspc=`echo $table | cut -d. -f1`
+			tables=`${PATHNAME}psql $db $PSQLOPT -F: -P format=unaligned -t -c "$query AND pg_class.relname='$tbl' AND nspname='$nspc'"`
+			echo $tables
+		else
+			tables=`${PATHNAME}psql $db $PSQLOPT -F: -P format=unaligned -t -c "$query AND pg_class.relname='$table'"`
+		fi
 	fi
+	query=
 	for tabs in $tables
 	do
-		tab=`echo $tabs | cut -d: -f1`
-		idx=`echo $tabs | cut -d: -f2`
-		${PATHNAME}psql $PSQLOPT $ECHOOPT -c "CLUSTER $idx on $tab" -d $db
-		if [ "$?" -ne 0 ]; then
-			echo "$CMDNAME: cluster $table $db failed" 1>&2
-		fi
+		nspc=`echo $tabs | cut -d: -f1`
+		tab=`echo $tabs | cut -d: -f2`
+		idx=`echo $tabs | cut -d: -f3`
+		query="$query CLUSTER $idx ON $nspc.$tab;"
 	done
+	${PATHNAME}psql $PSQLOPT $ECHOOPT -c "$query" -d $db
+	if [ "$?" -ne 0 ]
+	then
+		echo "$CMDNAME: While clustering $db, the following failed: $query" 1>&2
+	fi
 done
 
 exit 0
-- 
2.24.1