Use postmaster_is_alive() check in pg_ctl restart as well as pg_ctl status,

so that restart doesn't fail when old postmaster died unbetimes.
This commit is contained in:
Tom Lane 2005-05-04 22:35:15 +00:00
parent a5ed98ba5a
commit 13f75e378e

View File

@ -4,7 +4,7 @@
* *
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/pg_ctl/pg_ctl.c,v 1.56 2005/04/20 23:10:16 tgl Exp $ * $PostgreSQL: pgsql/src/bin/pg_ctl/pg_ctl.c,v 1.57 2005/05/04 22:35:15 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -116,6 +116,7 @@ static pgpid_t get_pgpid(void);
static char **readfile(const char *path); static char **readfile(const char *path);
static int start_postmaster(void); static int start_postmaster(void);
static bool test_postmaster_connection(void); static bool test_postmaster_connection(void);
static bool postmaster_is_alive(pid_t pid);
static char def_postopts_file[MAXPGPATH]; static char def_postopts_file[MAXPGPATH];
static char postopts_file[MAXPGPATH]; static char postopts_file[MAXPGPATH];
@ -683,7 +684,8 @@ do_restart(void)
if (pid == 0) /* no pid file */ if (pid == 0) /* no pid file */
{ {
write_stderr(_("%s: PID file \"%s\" does not exist\n"), progname, pid_file); write_stderr(_("%s: PID file \"%s\" does not exist\n"),
progname, pid_file);
write_stderr(_("Is postmaster running?\n")); write_stderr(_("Is postmaster running?\n"));
write_stderr(_("starting postmaster anyway\n")); write_stderr(_("starting postmaster anyway\n"));
do_start(); do_start();
@ -692,45 +694,58 @@ do_restart(void)
else if (pid < 0) /* standalone backend, not postmaster */ else if (pid < 0) /* standalone backend, not postmaster */
{ {
pid = -pid; pid = -pid;
write_stderr(_("%s: cannot restart postmaster; " if (postmaster_is_alive((pid_t) pid))
"postgres is running (PID: %ld)\n"), {
progname, pid); write_stderr(_("%s: cannot restart postmaster; "
write_stderr(_("Please terminate postgres and try again.\n")); "postgres is running (PID: %ld)\n"),
exit(1); progname, pid);
write_stderr(_("Please terminate postgres and try again.\n"));
exit(1);
}
} }
if (kill((pid_t) pid, sig) != 0) if (postmaster_is_alive((pid_t) pid))
{ {
write_stderr(_("%s: could not send stop signal (PID: %ld): %s\n"), progname, pid, if (kill((pid_t) pid, sig) != 0)
strerror(errno)); {
exit(1); write_stderr(_("%s: could not send stop signal (PID: %ld): %s\n"), progname, pid,
} strerror(errno));
exit(1);
}
print_msg(_("waiting for postmaster to shut down...")); print_msg(_("waiting for postmaster to shut down..."));
/* always wait for restart */ /* always wait for restart */
for (cnt = 0; cnt < wait_seconds; cnt++) for (cnt = 0; cnt < wait_seconds; cnt++)
{
if ((pid = get_pgpid()) != 0)
{ {
print_msg("."); if ((pid = get_pgpid()) != 0)
pg_usleep(1000000); /* 1 sec */ {
print_msg(".");
pg_usleep(1000000); /* 1 sec */
}
else
break;
} }
else
break;
}
if (pid != 0) /* pid file still exists */ if (pid != 0) /* pid file still exists */
{
print_msg(_(" failed\n"));
write_stderr(_("%s: postmaster does not shut down\n"), progname);
exit(1);
}
print_msg(_(" done\n"));
printf(_("postmaster stopped\n"));
}
else
{ {
print_msg(_(" failed\n")); write_stderr(_("%s: old postmaster process (PID: %ld) seems to be gone\n"),
progname, pid);
write_stderr(_("%s: postmaster does not shut down\n"), progname); write_stderr(_("starting postmaster anyway\n"));
exit(1);
} }
print_msg(_(" done\n"));
printf(_("postmaster stopped\n"));
do_start(); do_start();
} }
@ -841,8 +856,8 @@ do_kill(pgpid_t pid)
{ {
if (kill((pid_t) pid, sig) != 0) if (kill((pid_t) pid, sig) != 0)
{ {
write_stderr(_("%s: could not send signal %d (PID: %ld): %s\n"), progname, sig, pid, write_stderr(_("%s: could not send signal %d (PID: %ld): %s\n"),
strerror(errno)); progname, sig, pid, strerror(errno));
exit(1); exit(1);
} }
} }