For the record, from IRC:
[11:14am] timbunce_: G_SabinoMullane: why even consider the value of pg_db_txn_status at all? The semantics of ping are unrelated to transactions.
[11:14am] timbunce_: (but maybe I'm missing something as I'm not familiar with libpq)
[11:42am] G_SabinoMullane: timbunce_: Well we know we can't do anything if it returns active (query already running) or in a failed transaction.
[11:43am] G_SabinoMullane: But while we can't run a ping in those circumstances, we can return a little more information (well, pg_ping does: while ping is boolean, pg_ping provides more information via integer return code)
[3:42pm] timbunce_: G_SabinoMullane: it's vital that ping returns false on failure of the server/connection regardless of the state of the txn/client.
[3:42pm] timbunce_: It sounds like you're saying that in certain txn/client states the state of server/connection can't be determined. Is that right? If so, please give more details.
[3:45pm] G_SabinoMullane: If we are, for example, in a failed transaction, we cannot send the 'SELECT 1' to the backend.
[3:45pm] G_SabinoMullane: However..
[3:45pm] G_SabinoMullane: I suppose we could still send it and swallow the error, in the theory that the ping server-still-there response trumps everything else.
[3:46pm] timbunce_: G_SabinoMullane: yes, that would be fine.
[3:47pm] G_SabinoMullane: I suppose the same could be said for the other states too. We can always *send* the SELECT 1, but we cannot promise we will get back a valid answer.
[3:47pm] G_SabinoMullane: But in this context, a valid answer could be an ERROR. As long as it came from the server, ping can reply true.
[3:48pm] timbunce_: Yes, exactly. Thanks.
[3:48pm] G_SabinoMullane: Okay. I will whip up some tests when I get some tuits.
[3:50pm] timbunce_: Thanks. Hopefully there's some way to cartegories errors-from-server vs errors-from-libpq.
[4:07pm] timbunce_: pgbouncer is another case that needs considering - it might be up but the pg backend be down.