head	1.7;
access;
symbols
	REL7_4_29:1.5
	REL8_0_25:1.5
	REL7_4_28:1.5
	REL8_0_24:1.5
	REL7_4_27:1.5
	REL8_0_23:1.5
	REL7_4_26:1.5
	REL8_0_22:1.5
	REL7_4_25:1.5
	REL8_0_21:1.5
	REL7_4_24:1.5
	REL8_0_20:1.5
	REL7_4_23:1.5
	REL8_0_19:1.5
	REL7_4_22:1.5
	REL8_0_18:1.5
	REL7_4_21:1.5
	REL8_0_17:1.5
	REL7_4_20:1.5
	REL8_0_16:1.5
	REL7_3_21:1.3.2.1
	REL7_4_19:1.5
	REL8_0_15:1.5
	REL7_3_20:1.3.2.1
	REL7_4_18:1.5
	REL8_0_14:1.5
	REL7_3_19:1.3.2.1
	REL7_4_17:1.5
	REL8_0_13:1.5
	REL8_0_12:1.5
	REL7_3_18:1.3.2.1
	REL7_4_16:1.5
	REL8_0_11:1.5
	REL8_0_10:1.5
	REL7_4_15:1.5
	REL7_3_17:1.3.2.1
	REL8_0_9:1.5
	REL7_4_14:1.5
	REL7_3_16:1.3.2.1
	REL7_3_15:1.3.2.1
	REL7_4_13:1.5
	REL8_0_8:1.5
	REL7_3_14:1.3.2.1
	REL7_4_12:1.5
	REL8_0_7:1.5
	REL7_3_13:1.3.2.1
	REL7_4_11:1.5
	REL8_0_6:1.5
	REL7_3_12:1.3.2.1
	REL7_4_10:1.5
	REL8_0_5:1.5
	REL7_3_11:1.3.2.1
	REL7_4_9:1.5
	REL8_0_4:1.5
	REL7_3_10:1.3.2.1
	REL7_4_8:1.5
	REL8_0_3:1.5
	REL8_0_2:1.5
	REL7_3_9:1.3.2.1
	REL7_4_7:1.5
	REL8_0_1:1.5
	REL8_0_STABLE:1.5.0.8
	REL8_0_0:1.5.0.6
	REL8_0_0RC5:1.5
	REL8_0_0RC4:1.5
	REL8_0_0RC3:1.5
	REL8_0_0RC2:1.5
	REL8_0_0RC1:1.5
	REL8_0_0BETA5:1.5
	REL8_0_0BETA4:1.5
	REL7_4_6:1.5
	REL7_3_8:1.3.2.1
	REL8_0_0BETA3:1.5
	REL8_0_0BETA2:1.5
	REL7_4_5:1.5
	REL7_3_7:1.3.2.1
	REL7_4_4:1.5
	REL8_0_0BETA1:1.5
	REL7_4_3:1.5
	REL7_4_2:1.5
	REL7_3_6:1.3.2.1
	REL7_4_1:1.5
	REL7_3_5:1.3.2.1
	REL7_4:1.5
	REL7_4_RC2:1.5
	REL7_4_STABLE:1.5.0.4
	REL7_4_RC1:1.5
	REL7_4_BETA5:1.5
	REL7_4_BETA4:1.5
	REL7_4_BETA3:1.5
	REL7_4_BETA2:1.5
	WIN32_DEV:1.5.0.2
	REL7_4_BETA1:1.5
	REL7_3_4:1.3.2.1
	REL7_3_2:1.3
	REL7_3_STABLE:1.3.0.2;
locks; strict;
comment	@# @;


1.7
date	2005.07.29.15.13.10;	author momjian;	state dead;
branches;
next	1.6;

1.6
date	2005.06.21.04.02.28;	author tgl;	state Exp;
branches;
next	1.5;

1.5
date	2003.05.19.01.00.12;	author tgl;	state Exp;
branches;
next	1.4;

1.4
date	2003.05.14.03.25.57;	author tgl;	state Exp;
branches;
next	1.3;

1.3
date	2002.11.03.01.20.06;	author momjian;	state Exp;
branches
	1.3.2.1;
next	1.2;

1.2
date	2002.10.18.18.41.20;	author momjian;	state Exp;
branches;
next	1.1;

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

1.3.2.1
date	2003.05.19.01.00.18;	author tgl;	state Exp;
branches;
next	;


desc
@@


1.7
log
@Move reindexdb from /contrib to /bin.

Euler Taveira de Oliveira
@
text
@#!/bin/sh
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- #
# Package : reindexdb                 Version : $Revision: 1.6 $
# Date    : 05/08/2002                Author  : Shaun Thomas
# Req     : psql, sh, perl, sed         Type  : Utility
#
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- #

# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- #
# Function Definitions
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- #

usage()
{
  echo "$CMDNAME reindexes a PostgreSQL database."
  echo
  echo "Usage:"
  echo "  $CMDNAME [options] [dbname]"
  echo
  echo "Options:"
  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 reindex"
  echo "  -a, --all                       Reindex all databases"
  echo "  -t, --table=TABLE               Reindex specific table only"
  echo "  -i, --index=INDEX               Reindex specific index only"
  echo "  -e, --echo                      Show the command(s) sent to the backend"
  echo "  -q, --quiet                     Don't write any output"
  echo
  echo "Read the description of the SQL command REINDEX for details."
  echo
  exit 0
}

# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- #
# Program Body
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- #

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

# Try valiantly to get the location of psql, since you can't ever
# really know where it has been placed.  We'll start by trying the
# path.  If that fails, we'll try the directory where this script
# resides.  Then on to whereis, and finally locate.  Wish us luck.

if x=`psql -V 2>/dev/null | grep psql`; then
  PSQL='psql'
elif [ -f ${PATHNAME}psql ]; then
  PSQL=${PATHNAME}psql;
elif x=`whereis -b psql 2>/dev/null | sed 's/.* //'`; then
  PSQL=$x
elif x=`locate -r bin/psql$ -n 1 2>/dev/null`; then
  PSQL=$x
else
  echo "$CMDNAME: Could not find psql to talk to postgres installation."
  echo "Please make sure psql is in your path, or that this script is in"
  echo "the same directory as psql was installed."
  exit 1
fi

# Now, go through all of our command-line options and get each operation
# we said we'd accept in the usage listing.

while [ "$#" -gt 0 ]
do
  # Show help.
  case "$1" in
    --help|-\?)
      usage
      exit 0
      ;;

  # All of the following are postgres options.  We can pass them on
  # directly, without interpreting them in any way.  We don't care.
  # Anything that allows a space, we'll get the next *two* arguments
  # and make sure to pass those along.

    --host|-h|-p|--port|-U|--username)
      PSQLOPT="$PSQLOPT $1 $2"
      shift
      ;;
    -h*|--host=*|-p*|--port=*|-U*|--username=*|-W|--password)
      PSQLOPT="$PSQLOPT $1"
      ;;

  # From this point on, we're setting options that are required for
  # or only valid in This script.  This includes which database(s) to
  # reindex, which tables, or which indexes, and so on.

    # Echoing.  We'll *not* use this in queries we use to get lists.
    --echo|-e)
      ECHOOPT="-e"
      ;;

    # Do not echo messages.
    --quiet|-q)
      ECHOOPT="-q"
      quiet=1
      ;;

    # Reindex all databases, all tables, all applicable indexes.
    --all|-a)
      alldb=1
      ;;

    # Database to connect to, if not all of them.
    --dbname|-d)
      dbname="$2"
      shift
      ;;
    -d*)
      dbname=`echo "$1" | sed 's/^-d/'`
      ;;
    --dbname=*)
      dbname=`echo "$1" | sed 's/^--dbname=//'`
      ;;

    # Reindex specific Table.  Disables index reindexing.
    --table|-t)
      table="$2"
      shift
      ;;
    -t*)
      table=`echo "$1" | sed 's/^-t//'`
      ;;
    --table=*)
      table=`echo "$1" | sed 's/^--table=//'`
      ;;

    # Reindex specific index.  Disables table reindexing.
    --index|-i)
      index="$2"
      shift
      ;;
    -i*)
      index=`echo "$1" | sed 's/^-i//'`
      ;;
    --index=*)
      index=`echo "$1" | sed 's/^--index=//'`
      ;;

    # Yeah, no options?  Whine, and show usage.
    -*)
      echo "$CMDNAME: invalid option: $1" 1>&2
      usage;
      exit 1
      ;;

    # Finally, it's possible that the database name was just the last
    # unlabeled option.  So, let's get that.
    *)
      dbname="$1"
      ;;
  esac

  shift # Shift off each argument as we loop.

done

# Get a list of all databases we'll be using.  This first case is if we
# were asked to do all databases.
if [ "$alldb" ]; then

  if [ "$dbname" ] || [ "$index" ] || [ "$table" ]; then
    echo "$CMDNAME: cannot reindex all databases and a specific database," 1>&2
    echo " table, or index at the same time." 1>&2
    exit 1
  fi

  # Execute a command to pull back all databases the user specified can
  # connect to.  That's the list we'll be using.  It's also why it's
  # a good idea for this to be run as a super-user.
  sql='SELECT datname FROM pg_database WHERE datallowconn'
  dbname=`$PSQL $PSQLOPT -q -t -A -d postgres -c "$sql"`

# Ok, if it's not all databases, make sure at least one database is
# specified before continuing.
elif [ -z "$dbname" ]; then
  echo "$CMDNAME: missing required argument: database name" 1>&2
  usage;
  exit 1
fi

# No.  We can't reindex a specific index and table at the same time.
# Complain about this, and move on.
if [ "$table" ] && [ "$index" ]; then
  echo "$CMDNAME: cannot reindex a specific table and a specific index" 1>&2
  echo "at the same time." 1>&2
  exit 1
fi

# If index was selected, reindex that index.
if [ "$index" ]; then
  $PSQL $PSQLOPT $ECHOOPT -c "REINDEX INDEX \"$index\"" -d "$dbname"
  if [ "$?" -ne 0 ]; then
    echo "$CMDNAME: reindex index \"$index\" failed" 1>&2
    exit 1
  fi

# Ok, no index.  Is there a specific table to reindex?
elif [ "$table" ]; then
  $PSQL $PSQLOPT $ECHOOPT -c "REINDEX TABLE \"$table\"" -d "$dbname"
  if [ "$?" -ne 0 ]; then
    echo "$CMDNAME: reindex table \"$table\" failed" 1>&2
    exit 1
  fi

# No specific table, no specific index, either we have a specific database,
# or were told to do all databases.  Do it!
else

  # We set IFS to newline only so that the for-loops won't misinterpret
  # spaces in the lists we retrieved via psql.  Note also the use of
  # regclass to handle spaces, mixed-case names, and schema awareness.
  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\\\\_%'"

  IFS='
'
  for db in $dbname; do

    # Only print which database we're currently reindexing if not in
    # quiet mode, and we're doing more than one database.
    [ "$alldb" ] && [ -z "$quiet" ] && echo "Reindexing $db"

    IFS=' 	
'
    # Get a list of non-system tables that have indexes.
    tables=`$PSQL $PSQLOPT -q -t -A -d "$db" -c "$sql"`

    # Ok, reindex every table in the database.
    IFS='
'
    for tab in $tables; do
	IFS=' 	
'
        $PSQL $PSQLOPT $ECHOOPT -c "REINDEX TABLE $tab" -d "$db"
        if [ "$?" -ne 0 ]; then
          echo "$CMDNAME: reindex table $tab failed" 1>&2
          exit 1
        fi
	IFS='
'
    done

  done

fi

exit 0
@


1.6
log
@Cause initdb to create a third standard database "postgres", which
unlike template0 and template1 does not have any special status in
terms of backend functionality.  However, all external utilities such
as createuser and createdb now connect to "postgres" instead of
template1, and the documentation is changed to encourage people to use
"postgres" instead of template1 as a play area.  This should fix some
longstanding gotchas involving unexpected propagation of database
objects by createdb (when you used template1 without understanding
the implications), as well as ameliorating the problem that CREATE
DATABASE is unhappy if anyone else is connected to template1.
Patch by Dave Page, minor editing by Tom Lane.  All per recent
pghackers discussions.
@
text
@d3 1
a3 1
# Package : reindexdb                 Version : $Revision: 1.5 $
@


1.5
log
@Repair quoting sloppiness, lack of schema awareness in reindexdb.
@
text
@d3 1
a3 1
# Package : reindexdb                 Version : $Revision: 1.4 $
d177 1
a177 1
  dbname=`$PSQL $PSQLOPT -q -t -A -d template1 -c "$sql"`
@


1.4
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
@d3 1
a3 1
# Package : reindexdb                 Version : $Revision: 1.3 $
d29 1
a29 1
  echo "  -e, --echo                      Show the command being sent to the backend"
d44 1
a44 1
# Try valliantly to get the location of psql, since you can't ever
d98 1
a98 1
    # Do not echo messages.  We'll direct all output to /dev/null.
d100 1
a100 1
      ECHOOPT="$ECHOOPT -o /dev/null"
d175 3
a177 2
  # a good idea for this to be a super-user.
  dbname=`$PSQL $PSQLOPT -q -t -A -d template1 -c 'SELECT datname FROM pg_database WHERE datallowconn'`
d195 1
a195 1
# If index was set, reindex that index.
d197 5
a201 1
  $PSQL $PSQLOPT $ECHOOPT -c "REINDEX INDEX $index" -d $dbname
d205 5
a209 1
    $PSQL $PSQLOPT $ECHOOPT -c "REINDEX TABLE \"$table\"" -d $dbname
d215 7
a221 1
  sql="SELECT distinct tablename FROM pg_indexes WHERE tablename NOT LIKE 'pg_%'"
d228 8
a235 4
    # Ok, reindex every table in the database.  Use the same method
    # we used to get a list of databases, and get a list of tables in this
    # database that we may reindex.
    tables=`$PSQL $PSQLOPT -q -t -A -d $db -c "$sql"`
d237 9
a245 1
        $PSQL $PSQLOPT $ECHOOPT -c "REINDEX TABLE \"$tab\"" -d $db
a249 7
fi

# If any of the commands we've executed above failed in any way, bail
# out with an error.
if [ "$?" -ne 0 ]; then
  echo "$CMDNAME: reindex $index $table $dbname failed" 1>&2
  exit 1
@


1.3
log
@Use sed rather than perl for reindexdb.
@
text
@d3 1
a3 1
# Package : reindexdb                 Version : $Revision: 1.2 $
d196 1
a196 1
  $PSQL $PSQLOPT $ECHOOPT -c "SET autocommit TO 'on';REINDEX INDEX $index" -d $dbname
d200 1
a200 1
    $PSQL $PSQLOPT $ECHOOPT -c "SET autocommit TO 'on';REINDEX TABLE \"$table\"" -d $dbname
d216 1
a216 1
    tables=`$PSQL $PSQLOPT -q -t -A -d $db -c "SET autocommit TO 'on';$sql"`
d218 1
a218 1
        $PSQL $PSQLOPT $ECHOOPT -c "SET autocommit TO 'on';REINDEX TABLE \"$tab\"" -d $db
@


1.3.2.1
log
@Repair quoting sloppiness, lack of schema awareness in reindexdb.
@
text
@d3 1
a3 1
# Package : reindexdb                 Version : $Revision: 1.4 $
d29 1
a29 1
  echo "  -e, --echo                      Show the command(s) sent to the backend"
d44 1
a44 1
# Try valiantly to get the location of psql, since you can't ever
d98 1
a98 1
    # Do not echo messages.
d100 1
a100 1
      ECHOOPT="-q"
d175 2
a176 3
  # a good idea for this to be run as a super-user.
  sql='SELECT datname FROM pg_database WHERE datallowconn'
  dbname=`$PSQL $PSQLOPT -q -t -A -d template1 -c "$sql"`
d194 1
a194 1
# If index was selected, reindex that index.
d196 1
a196 5
  $PSQL $PSQLOPT $ECHOOPT -c "SET autocommit TO 'on';REINDEX INDEX \"$index\"" -d "$dbname"
  if [ "$?" -ne 0 ]; then
    echo "$CMDNAME: reindex index \"$index\" failed" 1>&2
    exit 1
  fi
d200 1
a200 5
  $PSQL $PSQLOPT $ECHOOPT -c "SET autocommit TO 'on';REINDEX TABLE \"$table\"" -d "$dbname"
  if [ "$?" -ne 0 ]; then
    echo "$CMDNAME: reindex table \"$table\" failed" 1>&2
    exit 1
  fi
d206 1
a206 7
  # We set IFS to newline only so that the for-loops won't misinterpret
  # spaces in the lists we retrieved via psql.  Note also the use of
  # regclass to handle spaces, mixed-case names, and schema awareness.
  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\\\\_%'"

  IFS='
'
d213 4
a216 8
    IFS=' 	
'
    # Get a list of non-system tables that have indexes.
    tables=`$PSQL $PSQLOPT -q -t -A -d "$db" -c "$sql"`

    # Ok, reindex every table in the database.
    IFS='
'
d218 1
a218 9
	IFS=' 	
'
        $PSQL $PSQLOPT $ECHOOPT -c "SET autocommit TO 'on';REINDEX TABLE $tab" -d "$db"
        if [ "$?" -ne 0 ]; then
          echo "$CMDNAME: reindex table $tab failed" 1>&2
          exit 1
        fi
	IFS='
'
d223 7
@


1.2
log
@Update /contrib for "autocommit TO 'on'".

Create objects in public schema.

Make spacing/capitalization consistent.

Remove transaction block use for object creation.

Remove unneeded function GRANTs.
@
text
@d3 1
a3 1
# Package : reindexdb                 Version : $Revision: 1.1 $
d114 5
a118 2
    -d*|--dbname=*)
      dbname=`echo $1 | perl -pn -e 's/^--?d(bname=)?//'`
d126 5
a130 2
    -t*|--table=*)
      table=`echo $1 | perl -pn -e 's/^--?t(able=)?//'`
d138 5
a142 2
    -i*|--index=*)
      index=`echo $1 | perl -pn -e 's/^--?i(ndex=)?//'`
@


1.1
log
@Rename command to reindexdb, for consistency.
@
text
@d3 1
a3 1
# Package : reindexdb                 Version : $Revision: 1.3 $
d187 1
a187 1
  $PSQL $PSQLOPT $ECHOOPT -c "REINDEX INDEX $index" -d $dbname
d191 1
a191 1
    $PSQL $PSQLOPT $ECHOOPT -c "REINDEX TABLE \"$table\"" -d $dbname
d207 1
a207 1
    tables=`$PSQL $PSQLOPT -q -t -A -d $db -c "$sql"`
d209 1
a209 1
        $PSQL $PSQLOPT $ECHOOPT -c "REINDEX TABLE \"$tab\"" -d $db
@
