[Server-devel] [PATCH] pgsql-xs: Wait for postmaster to complete startup -- solves race w Moodle

martin.langhoff at gmail.com martin.langhoff at gmail.com
Fri May 15 11:10:24 EDT 2009


From: Martin Langhoff <martin at laptop.org>

This is a function stolen from the old pg_ctl startup script taht shipped
with postgres 7.4. It defers returning success until it can connect to
the postmaster process and get it to list its DBs.

With this, Moodle DB initialisation now works, even on slow IO machines
such as the XO.

This is a highly local patch -- it works with assumptions we can make
on the XS, but are not general to a wide-use distro (see the warnings
comment block).

---

This solves the last niggle in the Moodle automagic startup and the
last major blocker with XS-on-XO. Yay!

---
 altfiles/etc/init.d/pgsql-xs |   40 +++++++++++++++++++++++++++++++++++++++-
 1 files changed, 39 insertions(+), 1 deletions(-)

diff --git a/altfiles/etc/init.d/pgsql-xs b/altfiles/etc/init.d/pgsql-xs
index 3446667..11b2d9d 100755
--- a/altfiles/etc/init.d/pgsql-xs
+++ b/altfiles/etc/init.d/pgsql-xs
@@ -181,7 +181,7 @@ start(){
 	$SU -l postgres -c "$PGENGINE/postmaster -p '$PGPORT' -D '$PGDATA' ${PGOPTS} &" >> "$PGLOG" 2>&1 < /dev/null
 	sleep 2
 	pid=`pidof -s "$PGENGINE/postmaster"`
-	if [ $pid ] && [ -f "$PGDATA/postmaster.pid" ]
+	if [ $pid ] && [ -f "$PGDATA/postmaster.pid" ] && test_pg_connection
 	then
 		success "$PSQL_START"
 		touch /var/lock/subsys/${NAME}
@@ -262,6 +262,44 @@ initdb(){
     fi
 }
 
+###
+### taken from the old pg_ctl.sh
+### -- this carries its original big bold warning
+# FIXME:  This is horribly misconceived.
+# 1) If password authentication is set up, the connection will fail.
+# 2) If a virtual host is set up, the connection may fail.
+# 3) If network traffic filters are set up tight enough, the connection
+#    may fail.
+# 4) When no Unix domain sockets are available, the connection will
+#    fail.  (Using TCP/IP by default ain't better.)
+# 5) When a different port is configured, the connection will fail
+#    or go to the wrong server.
+# 6) If the dynamic loader is not set up correctly (for this user/at
+#    this time), psql will fail (to find libpq).
+# 7) If psql is misconfigured, this may fail.
+
+test_pg_connection(){
+    cnt=0
+    wait_seconds=60
+    while :
+    do
+	$SU -l postgres -c "$PGENGINE/psql -l" >/dev/null 2>&1
+	if [ $? -eq 0 ]
+	then
+	    break;
+	else
+	    cnt=`expr $cnt + 1`
+	    echo -n '.'
+	    if [ "$cnt" -gt "$wait_seconds" ];then
+		$silence_echo echo "failed"
+		echo "$CMDNAME: postmaster does not start" 1>&2
+		exit 1
+	    fi
+	    sleep 1
+	fi
+    done
+}
+
 # This script is slightly unusual in that the name of the daemon (postmaster)
 # is not the same as the name of the subsystem (postgresql)
 
-- 
1.6.0.6



More information about the Server-devel mailing list