Re: How to check for server availability? [v9.3.1, Linux]

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Vincenzo Romano <vincenzo(dot)romano(at)notorand(dot)it>
Cc: PostgreSQL General <pgsql-general(at)postgresql(dot)org>
Subject: Re: How to check for server availability? [v9.3.1, Linux]
Date: 2012-05-18 12:34:52
Message-ID: 20279.1337344492@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Vincenzo Romano <vincenzo(dot)romano(at)notorand(dot)it> writes:
> I'd need to check from a C language program whether the server is
> running or not.
> I was planning to use either "PGPing PQping(const char *conninfo)" or
> "ConnStatusType PQstatus(const PGconn *conn)".
> I have my program running and checking from time to time whether the
> connection is kicking and alive.
> In order to test the things I've done the following: while the program
> is waiting for input I simply restart the postgresql server.
> The issue is thayt both functions return PQPING_OK and CONNECTION_OK
> respectively.
> When I forward the request, I get an error: "FATAL: terminating
> connection due to administrator command".
> How can I reliably check the server status?

You are not actually interested in checking the server status; you
evidently want to know about the health of an existing connection.
PQping will not help that because it is meant to see if the server
would accept a new connection. PQstatus is not tremendously helpful
either since it doesn't issue any new server traffic; it's just for
seeing if the operations so far were OK. I think issuing a new
dummy query (an empty string would do) is your best bet. But why
bother? Just send your next command, whatever it is. If you don't
have another useful command to issue, you're just wasting bandwidth
and server cycles.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message yxj 2012-05-18 13:47:13 Re: How to write a script to analyze and vacuum all the tables in the system catalog?Thanks.
Previous Message John Watts 2012-05-18 11:59:44 Re: difference in query plan when db is restored