head	1.13;
access;
symbols
	REL7_3_21:1.8.2.1
	REL7_3_20:1.8.2.1
	REL7_3_19:1.8.2.1
	REL7_3_18:1.8.2.1
	REL7_3_17:1.8.2.1
	REL7_3_16:1.8.2.1
	REL7_3_15:1.8.2.1
	REL7_3_14:1.8.2.1
	REL7_3_13:1.8.2.1
	REL7_3_12:1.8.2.1
	REL7_3_11:1.8.2.1
	REL7_3_10:1.8.2.1
	REL7_3_9:1.8.2.1
	REL7_3_8:1.8.2.1
	REL7_3_7:1.8.2.1
	REL7_3_6:1.8.2.1
	REL7_3_5:1.8.2.1
	REL7_3_4:1.8
	REL7_3_2:1.8
	REL7_3_STABLE:1.8.0.2;
locks; strict;
comment	@# @;


1.13
date	2003.06.18.12.19.11;	author petere;	state dead;
branches;
next	1.12;

1.12
date	2003.06.11.05.13.12;	author momjian;	state Exp;
branches;
next	1.11;

1.11
date	2003.05.14.03.26.03;	author tgl;	state Exp;
branches;
next	1.10;

1.10
date	2003.03.20.18.53.18;	author momjian;	state Exp;
branches;
next	1.9;

1.9
date	2003.02.13.05.37.44;	author momjian;	state Exp;
branches;
next	1.8;

1.8
date	2002.10.21.20.32.33;	author momjian;	state Exp;
branches
	1.8.2.1;
next	1.7;

1.7
date	2002.10.18.22.05.36;	author petere;	state Exp;
branches;
next	1.6;

1.6
date	2002.10.16.03.44.28;	author momjian;	state Exp;
branches;
next	1.5;

1.5
date	2002.10.16.03.24.09;	author momjian;	state Exp;
branches;
next	1.4;

1.4
date	2002.09.27.17.51.10;	author momjian;	state Exp;
branches;
next	1.3;

1.3
date	2002.09.12.00.18.14;	author momjian;	state Exp;
branches;
next	1.2;

1.2
date	2002.09.07.16.12.27;	author petere;	state Exp;
branches;
next	1.1;

1.1
date	2002.08.27.03.38.27;	author momjian;	state Exp;
branches;
next	;

1.8.2.1
date	2003.09.29.18.53.08;	author momjian;	state Exp;
branches;
next	;


desc
@@


1.13
log
@Change clusterdb and vacuumdb into C programs.
@
text
@#!/bin/sh
#-------------------------------------------------------------------------
#
# clusterdb--
#    cluster a postgres database
#
#    This script runs psql with the "-c" option to cluster
#    the requested database.
#
# Copyright (c) 2002, PostgreSQL Global Development Group
#
#
# IDENTIFICATION
#    $Header: /cvsroot/pgsql-server/src/bin/scripts/clusterdb,v 1.12 2003/06/11 05:13:12 momjian Exp $
#
#-------------------------------------------------------------------------

CMDNAME=`basename "$0"`
PATHNAME=`echo "$0" | sed "s,$CMDNAME\$,,"`

PSQLOPT=
table=
dbname=
alldb=
quiet=0

while [ "$#" -gt 0 ]
do
	case "$1" in
	--help|-\?)
		usage=t
		break
		;;
# options passed on to psql
	--host|-h)
		PSQLOPT="$PSQLOPT -h $2"
		shift;;
	-h*)
		PSQLOPT="$PSQLOPT $1"
		;;
	--host=*)
		PSQLOPT="$PSQLOPT -h `echo \"$1\" | sed 's/^--host=//'`"
		;;
	--port|-p)
		PSQLOPT="$PSQLOPT -p $2"
		shift;;
	-p*)
		PSQLOPT="$PSQLOPT $1"
		;;
	--port=*)
		PSQLOPT="$PSQLOPT -p `echo \"$1\" | sed 's/^--port=//'`"
		;;
	--username|-U)
		PSQLOPT="$PSQLOPT -U $2"
		shift;;
	-U*)
		PSQLOPT="$PSQLOPT $1"
		;;
	--username=*)
		PSQLOPT="$PSQLOPT -U `echo \"$1\" | sed 's/^--username=//'`"
		;;
	--password|-W)
		PSQLOPT="$PSQLOPT -W"
		;;
	--echo|-e)
		ECHOOPT="-e"
		;;
	--quiet|-q)
		ECHOOPT="$ECHOOPT -o /dev/null"
		quiet=1
		;;
	--dbname|-d)
		dbname="$2"
		shift;;
	-d*)
		dbname=`echo $1 | sed 's/^-d//'`
		;;
	--dbname=*)
		dbname=`echo $1 | sed 's/^--dbname=//'`
		;;
	-a|--alldb)
		alldb=1
		;;
# options converted into SQL command
	--table|-t)
		table="$2"
		shift;;
	-t*)
		table=`echo $1 | sed 's/^-t//'`
		;;
	--table=*)
		table=`echo $1 | sed 's/^--table=//'`
		;;
	-*)
		echo "$CMDNAME: invalid option: $1" 1>&2
		echo "Try '$CMDNAME --help' for more information." 1>&2
		exit 1
		;;
	*)
		dbname="$1"
		if [ "$#" -ne 1 ]; then
			echo "$CMDNAME: invalid option: $2" 1>&2
			echo "Try '$CMDNAME --help' for more information." 1>&2
			exit 1
		fi
		;;
	esac
	shift
done

if [ "$usage" ]; then	
	echo "$CMDNAME cluster all previously clustered tables in a database."
	echo
	echo "Usage:"
	echo "  $CMDNAME [OPTION]... [DBNAME]"
	echo
	echo "Options:"
	echo "  -a, --all                 cluster all databases"
	echo "  -d, --dbname=DBNAME       database to cluster"
	echo "  -t, --table='TABLE'       cluster specific table only"
	echo "  -e, --echo                show the commands sent to the backend"
	echo "  -q, --quiet               don't write any output"
	echo "  --help                    show this help, then exit"
	echo
	echo "Connection options:"
	echo "  -h, --host=HOSTNAME       database server host or socket directory"
	echo "  -p, --port=PORT           database server port"
	echo "  -U, --username=USERNAME   user name to connect as"
	echo "  -W, --password            prompt for password"
	echo
	echo "Read the description of the SQL command CLUSTER for details."
	echo
	echo "Report bugs to <pgsql-bugs@@postgresql.org>."
	exit 0
fi

if [ "$alldb" ]; then
	if [ "$dbname" -o "$table" ]; then
		echo "$CMDNAME: cannot cluster all databases and a specific one at the same time" 1>&2
		exit 1
	fi
	dbname=`${PATHNAME}psql $PSQLOPT -q -t -A -d template1 -c 'SELECT datname FROM pg_database WHERE datallowconn'`
	[ "$?" -ne 0 ] && exit 1

elif [ -z "$dbname" ]; then
	if [ "$PGDATABASE" ]; then
		dbname="$PGDATABASE"
	elif [ "$PGUSER" ]; then
		dbname="$PGUSER"
	else
		dbname=`${PATHNAME}pg_id -u -n`
	fi
	[ "$?" -ne 0 ] && exit 1
fi

for db in $dbname
do
	[ "$alldb" ] && echo "Clustering $db"
	if [ -z "$table" ]; then
		${PATHNAME}psql $PSQLOPT $ECHOOPT -c "CLUSTER" -d $db
		[ "$?" -ne 0 ] && exit 1
	else
		${PATHNAME}psql $PSQLOPT $ECHOOPT -c "CLUSTER $table" -d $db
		[ "$?" -ne 0 ] && exit 1
	fi
done

exit 0
@


1.12
log
@Document the -h client flag can use a socket directory as well as a host
name.
@
text
@d14 1
a14 1
#    $Header: /cvsroot/pgsql-server/src/bin/scripts/clusterdb,v 1.11 2003/05/14 03:26:03 tgl Exp $
@


1.11
log
@Backend support for autocommit removed, per recent discussions.  The
only remnant of this failed experiment is that the server will take
SET AUTOCOMMIT TO ON.  Still TODO: provide some client-side autocommit
logic in libpq.
@
text
@d14 1
a14 1
#    $Header: /cvsroot/pgsql-server/src/bin/scripts/clusterdb,v 1.10 2003/03/20 18:53:18 momjian Exp $
d126 1
a126 1
	echo "  -h, --host=HOSTNAME       database server host"
@


1.10
log
@Now that the CLUSTER ALL machinery is in place, the clusterdb script can
be simplified (I'd thought that it can even be removed).  This patch
does that.

Alvaro Herrera
@
text
@d14 1
a14 1
#    $Header: /cvsroot/pgsql-server/src/bin/scripts/clusterdb,v 1.9 2003/02/13 05:37:44 momjian Exp $
d160 1
a160 1
		${PATHNAME}psql $PSQLOPT $ECHOOPT -c "SET autocommit TO 'on';CLUSTER" -d $db
d163 1
a163 1
		${PATHNAME}psql $PSQLOPT $ECHOOPT -c "SET autocommit TO 'on';CLUSTER $table" -d $db
@


1.9
log
@This patch fixes an error in the usage message for 'clusterdb', and
makes a few editorial changes to the documentation.


Neil Conway
@
text
@d14 1
a14 1
#    $Header: /cvsroot/pgsql-server/src/bin/scripts/clusterdb,v 1.8 2002/10/21 20:32:33 momjian Exp $
a158 1
	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 AND pg_class.relowner=(SELECT usesysid FROM pg_user WHERE usename=current_user)"
d160 1
a160 1
		tables=`${PATHNAME}psql $db $PSQLOPT -F: -P format=unaligned -t -c "$query"`
d163 2
a164 23
		# 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
		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 "SET autocommit TO 'on';$query" -d $db
	if [ "$?" -ne 0 ]
	then
		echo "$CMDNAME: While clustering $db, the following failed: $query" 1>&2
@


1.8
log
@Cleanup for CLUSTERDB failure:

On Sat, Oct 19, 2002 at 12:11:32AM +0200, Peter Eisentraut wrote:
> $ ./clusterdb
> psql: could not connect to server: No such file or directory
>         Is the server running locally and accepting
>         connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
> psql: could not connect to server: No such file or directory
>         Is the server running locally and accepting
>         connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
> clusterdb: While clustering peter, the following failed:
> $
>
> This could probably handled a little more gracefully.

Yes, sorry.  A patch for this is attached.  Please apply.

Alvaro Herrera
@
text
@d14 1
a14 1
#    $Header: /cvsroot/pgsql-server/src/bin/scripts/clusterdb,v 1.7 2002/10/18 22:05:36 petere Exp $
d121 1
a121 1
	echo "  -e, --echo                show the command being sent to the backend"
d123 1
a123 2
	echo "  -v, --verbose             write a lot of output"
	echo " --help                     show this help, then exit"
@


1.8.2.1
log
@
[ Patch applied only to 7.3.X.]

Hi There's a bug in the clusterdb script where it looks like the arguments
to the psql command are being passed in the wrong order, so it fails when
you run it on a database that is not on localhost.
Here's the output from the command:

133 anands-Computer:bin/scripts> clusterdb -h wooster -U rr granada
psql: warning: extra option wooster ignored
psql: warning: extra option -U ignored
psql: warning: extra option rr ignored
psql: warning: extra option -F: ignored
psql: warning: extra option -P ignored
psql: warning: extra option format=unaligned ignored
psql: warning: extra option -t ignored
psql: warning: extra option -c ignored
psql: warning: extra option 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 AND pg_class.relowner=(SELECT usesysid FROM
pg_user WHERE usename=current_user) ignored
psql: FATAL:  user "-h" does not exist



I'm attaching a patch that fixes the problem. The diff was run on
postgresql 7.3.4

Thanks a lot.
Anand Ranganathan
@
text
@d14 1
a14 1
#    $Header: /cvsroot/pgsql-server/src/bin/scripts/clusterdb,v 1.8 2002/10/21 20:32:33 momjian Exp $
a158 1

a160 1

d162 1
a162 1
		tables=`${PATHNAME}psql $PSQLOPT -F: -P format=unaligned -t -c "$query" $db`
d170 1
a170 1
			tables=`${PATHNAME}psql $PSQLOPT -F: -P format=unaligned -t -c "$query AND pg_class.relname='$tbl' AND nspname='$nspc'" $db`
d173 1
a173 1
			tables=`${PATHNAME}psql $PSQLOPT -F: -P format=unaligned -t -c "$query AND pg_class.relname='$table'" $db`
@


1.7
log
@Improve formatting of --help output.
@
text
@d14 1
a14 1
#    $Header: /cvsroot/pgsql-server/src/bin/scripts/clusterdb,v 1.6 2002/10/16 03:44:28 momjian Exp $
d144 1
d163 1
@


1.6
log
@Improve appearance of SET command.
@
text
@d14 1
a14 1
#    $Header: /cvsroot/pgsql-server/src/bin/scripts/clusterdb,v 1.5 2002/10/16 03:24:09 momjian Exp $
d112 1
a112 1
	echo "$CMDNAME cluster all previously clustered tables in a database"
d115 1
a115 1
	echo "  $CMDNAME [options] [dbname]"
d118 13
a130 10
	echo "  -h, --host=HOSTNAME             Database server host"
	echo "  -p, --port=PORT                 Database server port"
	echo "  -U, --username=USERNAME         Username to connect as"
	echo "  -W, --password                  Prompt for password"
	echo "  -d, --dbname=DBNAME             Database to cluster"
	echo "  -a, --all                       Cluster all databases"
	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"
@


1.5
log
@Fix script to handle autocommit = 'off' by prepending autocommit 'on' to
the start of the psql commandline.  This is better than adding BEGIN/END
because it handles multiple queries well, and allows the return code for
psql to return the proper value.
@
text
@d14 1
a14 1
#    $Header: /cvsroot/pgsql-server/src/bin/scripts/clusterdb,v 1.4 2002/09/27 17:51:10 momjian Exp $
d179 1
a179 1
	${PATHNAME}psql $PSQLOPT $ECHOOPT -c "SET autocommit = 'on';$query" -d $db
@


1.4
log
@> Fix clusterdb to be schema-aware

Please apply the patch attached and this should be solved.

Alvaro Herrera
@
text
@d14 1
a14 1
#    $Header: /cvsroot/pgsql-server/src/bin/scripts/clusterdb,v 1.3 2002/09/12 00:18:14 momjian Exp $
d179 1
a179 1
	${PATHNAME}psql $PSQLOPT $ECHOOPT -c "$query" -d $db
@


1.3
log
@> 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
@
text
@d14 1
a14 1
#    $Header: /cvsroot/pgsql-server/src/bin/scripts/clusterdb,v 1.2 2002/09/07 16:12:27 petere Exp $
d156 1
a156 1
	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"
@


1.2
log
@Fix help output.
@
text
@d14 1
a14 1
#    $Header: /cvsroot/pgsql-server/src/bin/scripts/clusterdb,v 1.1 2002/08/27 03:38:27 momjian Exp $
d124 1
a124 1
	echo "  -t, --table='TABLE[(columns)]'  Cluster specific table only"
d155 2
a156 4
	[ "$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"
d160 10
a169 2
		tables=`${PATHNAME}psql $db $PSQLOPT -F: -P format=unaligned -t -c \
		"$query AND pg_class.relname='$table'"`
d171 1
d174 4
a177 6
		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
d179 5
@


1.1
log
@I attach a little patch to make CLUSTER set and reset the indisclustered
bit on the indexes.

I also attach clusterdb and clusterdb.sgml; both of them are blatant
rips of vacuumdb and vacuumdb.sgml, but get the job done.  Please review
them, as I'm probably making a lot of mistakes with SGML and I can't
compile it here.

vacuumdb itself is not very comfortable to use when the databases have
passwords, because it has to connect once for each table (I can probably
make it connect only once for each database; should I?).  Because of
this I added a mention of PGPASSWORDFILE in the documentation, but I
don't know if that is the correct place for that.

Alvaro Herrera
@
text
@d14 1
a14 1
#    $Header$
d129 1
a129 1
	echo "Read the description of the SQL command VACUUM for details."
@
