Without worrying about debugging your script, have you considered using pg_standby that's in contrib? It works well and you don't have to fuss with scripts unless you're doing something special. David
pgsql-admin by date:
Соглашаюсь с условиями обработки персональных данных