OSDN Git Service

Improve corner cases in pg_ctl's new wait-for-postmaster-startup code.
authorTom Lane <tgl@sss.pgh.pa.us>
Fri, 27 May 2011 18:13:38 +0000 (14:13 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 27 May 2011 18:13:38 +0000 (14:13 -0400)
With "-w -t 0", we should report "still starting up", not "ok".  If we
fall out of the loop without ever being able to call PQping (because we
were never able to construct a connection string), report "no response",
not "ok".  This gets rid of corner cases in which we'd claim the server
had started even though it had not.

Also, if the postmaster.pid file is not there at any point after we've
waited 5 seconds, assume the postmaster has failed and report that, rather
than almost-certainly-fruitlessly continuing to wait.  The pidfile should
appear almost instantly even when there is extensive startup work to do,
so 5 seconds is already a very conservative figure.  This part is per a
gripe from MauMau --- there might be better ways to do it, but nothing
simple enough to get done for 9.1.

src/bin/pg_ctl/pg_ctl.c

index 98bbaef..7af3b91 100644 (file)
@@ -411,10 +411,14 @@ start_postmaster(void)
 static PGPing
 test_postmaster_connection(bool do_checkpoint)
 {
-       PGPing          ret = PQPING_OK;        /* assume success for wait == zero */
+       PGPing          ret = PQPING_NO_RESPONSE;
        char            connstr[MAXPGPATH * 2 + 256];
        int                     i;
 
+       /* if requested wait time is zero, return "still starting up" code */
+       if (wait_seconds <= 0)
+               return PQPING_REJECT;
+
        connstr[0] = '\0';
 
        for (i = 0; i < wait_seconds; i++)
@@ -538,6 +542,19 @@ test_postmaster_connection(bool do_checkpoint)
                                break;
                }
 
+               /*
+                * The postmaster should create postmaster.pid very soon after being
+                * started.  If it's not there after we've waited 5 or more seconds,
+                * assume startup failed and give up waiting.
+                */
+               if (i >= 5)
+               {
+                       struct stat statbuf;
+
+                       if (stat(pid_file, &statbuf) != 0)
+                               return PQPING_NO_RESPONSE;
+               }
+
                /* No response, or startup still in process; wait */
 #if defined(WIN32)
                if (do_checkpoint)