* internal_ping * Determine if a server is running and if we can connect to it. * * The argument is a connection that's been started, but not completed. */
| 3972 | * The argument is a connection that's been started, but not completed. |
| 3973 | */ |
| 3974 | static PGPing |
| 3975 | internal_ping(PGconn *conn) |
| 3976 | { |
| 3977 | /* Say "no attempt" if we never got to PQconnectPoll */ |
| 3978 | if (!conn || !conn->options_valid) |
| 3979 | return PQPING_NO_ATTEMPT; |
| 3980 | |
| 3981 | /* Attempt to complete the connection */ |
| 3982 | if (conn->status != CONNECTION_BAD) |
| 3983 | (void) connectDBComplete(conn); |
| 3984 | |
| 3985 | /* Definitely OK if we succeeded */ |
| 3986 | if (conn->status != CONNECTION_BAD) |
| 3987 | return PQPING_OK; |
| 3988 | |
| 3989 | /* |
| 3990 | * Here begins the interesting part of "ping": determine the cause of the |
| 3991 | * failure in sufficient detail to decide what to return. We do not want |
| 3992 | * to report that the server is not up just because we didn't have a valid |
| 3993 | * password, for example. In fact, any sort of authentication request |
| 3994 | * implies the server is up. (We need this check since the libpq side of |
| 3995 | * things might have pulled the plug on the connection before getting an |
| 3996 | * error as such from the postmaster.) |
| 3997 | */ |
| 3998 | if (conn->auth_req_received) |
| 3999 | return PQPING_OK; |
| 4000 | |
| 4001 | /* |
| 4002 | * If we failed to get any ERROR response from the postmaster, report |
| 4003 | * PQPING_NO_RESPONSE. This result could be somewhat misleading for a |
| 4004 | * pre-7.4 server, since it won't send back a SQLSTATE, but those are long |
| 4005 | * out of support. Another corner case where the server could return a |
| 4006 | * failure without a SQLSTATE is fork failure, but PQPING_NO_RESPONSE |
| 4007 | * isn't totally unreasonable for that anyway. We expect that every other |
| 4008 | * failure case in a modern server will produce a report with a SQLSTATE. |
| 4009 | * |
| 4010 | * NOTE: whenever we get around to making libpq generate SQLSTATEs for |
| 4011 | * client-side errors, we should either not store those into |
| 4012 | * last_sqlstate, or add an extra flag so we can tell client-side errors |
| 4013 | * apart from server-side ones. |
| 4014 | */ |
| 4015 | if (strlen(conn->last_sqlstate) != 5) |
| 4016 | return PQPING_NO_RESPONSE; |
| 4017 | |
| 4018 | if (strcmp(conn->last_sqlstate, ERRCODE_MIRROR_READY) == 0) |
| 4019 | return PQPING_MIRROR_READY; |
| 4020 | |
| 4021 | /* |
| 4022 | * Report PQPING_REJECT if server says it's not accepting connections. (We |
| 4023 | * distinguish this case mainly for the convenience of pg_ctl.) |
| 4024 | */ |
| 4025 | if (strcmp(conn->last_sqlstate, ERRCODE_CANNOT_CONNECT_NOW) == 0) |
| 4026 | return PQPING_REJECT; |
| 4027 | |
| 4028 | /* |
| 4029 | * Any other SQLSTATE can be taken to indicate that the server is up. |
| 4030 | * Presumably it didn't like our username, password, or database name; or |
| 4031 | * perhaps it had some transient failure, but that should not be taken as |
no test coverage detected