Skip to content

Commit 13f75e3

Browse files
committed
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.
1 parent a5ed98b commit 13f75e3

File tree

1 file changed

+45
-30
lines changed

1 file changed

+45
-30
lines changed

src/bin/pg_ctl/pg_ctl.c

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
66
*
7-
* $PostgreSQL: pgsql/src/bin/pg_ctl/pg_ctl.c,v 1.56 2005/04/20 23:10:16 tgl Exp $
7+
* $PostgreSQL: pgsql/src/bin/pg_ctl/pg_ctl.c,v 1.57 2005/05/04 22:35:15 tgl Exp $
88
*
99
*-------------------------------------------------------------------------
1010
*/
@@ -116,6 +116,7 @@ static pgpid_t get_pgpid(void);
116116
static char **readfile(const char *path);
117117
static int start_postmaster(void);
118118
static bool test_postmaster_connection(void);
119+
static bool postmaster_is_alive(pid_t pid);
119120

120121
static char def_postopts_file[MAXPGPATH];
121122
static char postopts_file[MAXPGPATH];
@@ -683,7 +684,8 @@ do_restart(void)
683684

684685
if (pid == 0) /* no pid file */
685686
{
686-
write_stderr(_("%s: PID file \"%s\" does not exist\n"), progname, pid_file);
687+
write_stderr(_("%s: PID file \"%s\" does not exist\n"),
688+
progname, pid_file);
687689
write_stderr(_("Is postmaster running?\n"));
688690
write_stderr(_("starting postmaster anyway\n"));
689691
do_start();
@@ -692,45 +694,58 @@ do_restart(void)
692694
else if (pid < 0) /* standalone backend, not postmaster */
693695
{
694696
pid = -pid;
695-
write_stderr(_("%s: cannot restart postmaster; "
696-
"postgres is running (PID: %ld)\n"),
697-
progname, pid);
698-
write_stderr(_("Please terminate postgres and try again.\n"));
699-
exit(1);
697+
if (postmaster_is_alive((pid_t) pid))
698+
{
699+
write_stderr(_("%s: cannot restart postmaster; "
700+
"postgres is running (PID: %ld)\n"),
701+
progname, pid);
702+
write_stderr(_("Please terminate postgres and try again.\n"));
703+
exit(1);
704+
}
700705
}
701706

702-
if (kill((pid_t) pid, sig) != 0)
707+
if (postmaster_is_alive((pid_t) pid))
703708
{
704-
write_stderr(_("%s: could not send stop signal (PID: %ld): %s\n"), progname, pid,
705-
strerror(errno));
706-
exit(1);
707-
}
709+
if (kill((pid_t) pid, sig) != 0)
710+
{
711+
write_stderr(_("%s: could not send stop signal (PID: %ld): %s\n"), progname, pid,
712+
strerror(errno));
713+
exit(1);
714+
}
708715

709-
print_msg(_("waiting for postmaster to shut down..."));
716+
print_msg(_("waiting for postmaster to shut down..."));
710717

711718
/* always wait for restart */
712719

713-
for (cnt = 0; cnt < wait_seconds; cnt++)
714-
{
715-
if ((pid = get_pgpid()) != 0)
720+
for (cnt = 0; cnt < wait_seconds; cnt++)
716721
{
717-
print_msg(".");
718-
pg_usleep(1000000); /* 1 sec */
722+
if ((pid = get_pgpid()) != 0)
723+
{
724+
print_msg(".");
725+
pg_usleep(1000000); /* 1 sec */
726+
}
727+
else
728+
break;
719729
}
720-
else
721-
break;
722-
}
723730

724-
if (pid != 0) /* pid file still exists */
725-
{
726-
print_msg(_(" failed\n"));
731+
if (pid != 0) /* pid file still exists */
732+
{
733+
print_msg(_(" failed\n"));
727734

728-
write_stderr(_("%s: postmaster does not shut down\n"), progname);
729-
exit(1);
735+
write_stderr(_("%s: postmaster does not shut down\n"), progname);
736+
exit(1);
737+
}
738+
739+
print_msg(_(" done\n"));
740+
printf(_("postmaster stopped\n"));
741+
}
742+
else
743+
{
744+
write_stderr(_("%s: old postmaster process (PID: %ld) seems to be gone\n"),
745+
progname, pid);
746+
write_stderr(_("starting postmaster anyway\n"));
730747
}
731748

732-
print_msg(_(" done\n"));
733-
printf(_("postmaster stopped\n"));
734749
do_start();
735750
}
736751

@@ -841,8 +856,8 @@ do_kill(pgpid_t pid)
841856
{
842857
if (kill((pid_t) pid, sig) != 0)
843858
{
844-
write_stderr(_("%s: could not send signal %d (PID: %ld): %s\n"), progname, sig, pid,
845-
strerror(errno));
859+
write_stderr(_("%s: could not send signal %d (PID: %ld): %s\n"),
860+
progname, sig, pid, strerror(errno));
846861
exit(1);
847862
}
848863
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy