From 0bae3bc9be4a025df089f0a0c2f547fa538a97bc Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Fri, 27 May 2011 14:13:38 -0400 Subject: [PATCH] Improve corner cases in pg_ctl's new wait-for-postmaster-startup code. 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 | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c index 98bbaefd8c..7af3b913ff 100644 --- a/src/bin/pg_ctl/pg_ctl.c +++ b/src/bin/pg_ctl/pg_ctl.c @@ -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) -- 2.11.0