Re: need a method to ping a running database

From: Dmitry Tkach <dmitry(at)openratings(dot)com>
To: Steve Crawford <scrawford(at)pinpointresearch(dot)com>
Cc: Travis Hume <travis(dot)hume(at)tenzing(dot)com>, pgsql-general(at)postgresql(dot)org, Breck Thomas <Breck(dot)Thomas(at)tenzing(dot)com>
Subject: Re: need a method to ping a running database
Date: 2003-06-12 17:29:19
Message-ID: 3EE8B86F.3080505@openratings.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Steve Crawford wrote:

>So try:
>`psql -d database -h server -c "select 'It is running'" 2>/dev/null | grep -c
>"It is running"`
>
>
It doesn't make any difference - if it is 'starting up', you won't get
that 'it is running string' anyway...
BTW, 2>/dev/null redirect is pointless - the ERROR: stuff goes to stdout
anyway :-)

>Though I don't think the "ERROR: database is staring up" is an incorrect error
>as it still means that the database is not ready to handle queries.
>
Sure. I did not say it was "incorrect"...
But it is *different* from most if not all other errors, because it
requires a different item...
For example, if the server is dead, you are probably going to want to
restart it.... If it is 'starting up' though, you just need to wait a
little bit.

> Still, I
>think it's safer to look for the existence of expected results that the
>absence of specific error messages.
>
I don't know what is "safer" about it... :-)
It seems to be more like a question of taste...
You must be one of those guys who prefers to write something like
if (condition == FALSE) instead of if (!condition)

:-)

Dima

>
>Cheers,
>Steve
>
>On Wednesday 11 June 2003 6:42 pm, Dima Tkach wrote:
>
>
>>What about
>>
>>down= \
>>`psql -d database -h server -c 'select 1;' 2>&1 | grep -c '^ERROR'`
>>
>>?
>>
>>Dima.
>>
>>P.S. One thing to be cautious about is that if it has just been started,
>>you'll get something like 'ERROR: The database is starting up.', that
>>will get counted as an error (probably incorrectly)... So, the real
>>implementation has to be a little more elaborate then that to be able to
>>catch and handle that situation...
>>
>>Travis Hume wrote:
>>
>>
>>>I need a shell scriptable method to determine if a postgresql database
>>>is running and accepting connections.
>>>
>>>I've written a script to start out postgresql database and I was using:
>>>"pg_ctl status"
>>>
>>>to determine if the database had been started. There are a couple of
>>>problems with this method. It seems that if you start postgresql and
>>>then issue a "kill -9" on the postmaster processes a .pid file is left
>>>behind (understandable), but then if you run "pg_ctl status" it will
>>>immediately report that the database is running and give you the pid
>>>number and whatever else. This is a bit misleading to say the least.
>>>To solve this problem I changed my db_ping() function to be more like
>>>"ps ax |grep -q postmaster && pg_ctl status". This works better, but
>>>there is still a problem.
>>>
>>>The .pid file is being written immediately, not when the database is
>>>actually up and accepting connections. This causes my java app to try
>>>and open the database and setup a connection pool before it should.
>>>
>>>I need a shell scriptable method to determine if the database is started
>>>and ready to accept connections.
>>>
>>>
>>>
>>>---------------------------(end of broadcast)---------------------------
>>>TIP 1: subscribe and unsubscribe commands go to majordomo(at)postgresql(dot)org
>>>
>>>
>>---------------------------(end of broadcast)---------------------------
>>TIP 5: Have you checked our extensive FAQ?
>>
>>http://www.postgresql.org/docs/faqs/FAQ.html
>>
>>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Justin Clift 2003-06-12 17:29:37 Re: [HACKERS] SAP and MySQL ... [and Benchmark]
Previous Message Steve Crawford 2003-06-12 17:18:27 Re: need a method to ping a running database