#!/usr/bin/env bash LOGGING=true curdir=`pwd` cd /tmp psql=`which psql` grep=`which grep` awk=`which awk` DATABASES=`/bin/su postgres -c "$psql -lt" | $grep -v -e"^$" | $grep -v -e "^\s*[:|]" | $awk '{print$1}'` for db in $DATABASES ; do if [ "$db" == "template0" ]; then continue fi if $LOGGING ;then echo "Database...: $db" fi TABLES=`/bin/su postgres -c "$psql -lt -q -c \"\dt \" $db" | awk '{print$3}'` if $LOGGING ;then echo fi for tbl in $TABLES ; do if $LOGGING ;then echo -e "\tvacuum analyze table $tbl .." fi /bin/su postgres -c "$psql -q -c \"VACUUM ANALYZE $tbl\" $db" [[ $? -gt 0 ]] && echo "[ERROR]: vacuun/analyzing table \"${tbl}\" of database \"$db\" failed !!" if $LOGGING ;then echo -e "\treindex table $tbl ..\n" fi /bin/su postgres -c "$psql -q -c \"REINDEX TABLE $tbl\" $db" > /dev/null 2>&1 [[ $? -gt 0 ]] && echo "[ERROR]: reindexing table \"${tbl}\" of database \"$db\" failed !!" done if $LOGGING ;then echo fi done cd $curdir exit