#! /bin/sh
# Online recovery 2nd stage script
#
PGDATA=$1		# master dabatase cluster
DEST=$2			# hostname of the DB node to be recovered
DESTDIR=$3		# database cluster of the DB node to be recovered
PGSQL_HOME="$(dirname $PGDATA)"
port=5432		# PostgreSQL port number
archdir="$PGSQL_HOME/archive"	# archive log directory

ARCHIVING_LOCK="${PGDATA}/archiving_enabled"
if [ ! -f "$ARCHIVING_LOCK" ]; then
    echo No "$ARCHIVING_LOCK", giving up
    exit 1
fi

# Force to flush current value of sequences to xlog 
psql -p $port -t -c 'SELECT datname FROM pg_database WHERE NOT datistemplate AND datallowconn' template1|
while read i
do
  if [ "$i" != "" ];then
    psql -p $port -c "SELECT setval(oid, nextval(oid)) FROM pg_class WHERE relkind = 'S'" $i
  fi
done

psql -p $port -c "SELECT pgpool_switch_xlog('$archdir')" template1

rm -f ${ARCHIVING_LOCK}
