---------- * connectDBStart - * Begin the process of making a connection to the backend. * * Returns 1 if successful, 0 if not. * ---------- */
| 2111 | * ---------- |
| 2112 | */ |
| 2113 | static int |
| 2114 | connectDBStart(PGconn *conn) |
| 2115 | { |
| 2116 | if (!conn) |
| 2117 | return 0; |
| 2118 | |
| 2119 | if (!conn->options_valid) |
| 2120 | goto connect_errReturn; |
| 2121 | |
| 2122 | /* |
| 2123 | * Check for bad linking to backend-internal versions of src/common |
| 2124 | * functions (see comments in link-canary.c for the reason we need this). |
| 2125 | * Nobody but developers should see this message, so we don't bother |
| 2126 | * translating it. |
| 2127 | */ |
| 2128 | #ifdef FRONTEND |
| 2129 | if (!pg_link_canary_is_frontend()) |
| 2130 | { |
| 2131 | appendPQExpBufferStr(&conn->errorMessage, |
| 2132 | "libpq is incorrectly linked to backend functions\n"); |
| 2133 | goto connect_errReturn; |
| 2134 | } |
| 2135 | #endif |
| 2136 | |
| 2137 | /* Ensure our buffers are empty */ |
| 2138 | conn->inStart = conn->inCursor = conn->inEnd = 0; |
| 2139 | conn->outCount = 0; |
| 2140 | |
| 2141 | /* |
| 2142 | * Set up to try to connect to the first host. (Setting whichhost = -1 is |
| 2143 | * a bit of a cheat, but PQconnectPoll will advance it to 0 before |
| 2144 | * anything else looks at it.) |
| 2145 | */ |
| 2146 | conn->whichhost = -1; |
| 2147 | conn->try_next_addr = false; |
| 2148 | conn->try_next_host = true; |
| 2149 | conn->status = CONNECTION_NEEDED; |
| 2150 | |
| 2151 | /* Also reset the target_server_type state if needed */ |
| 2152 | if (conn->target_server_type == SERVER_TYPE_PREFER_STANDBY_PASS2) |
| 2153 | conn->target_server_type = SERVER_TYPE_PREFER_STANDBY; |
| 2154 | |
| 2155 | /* |
| 2156 | * The code for processing CONNECTION_NEEDED state is in PQconnectPoll(), |
| 2157 | * so that it can easily be re-executed if needed again during the |
| 2158 | * asynchronous startup process. However, we must run it once here, |
| 2159 | * because callers expect a success return from this routine to mean that |
| 2160 | * we are in PGRES_POLLING_WRITING connection state. |
| 2161 | */ |
| 2162 | if (PQconnectPoll(conn) == PGRES_POLLING_WRITING) |
| 2163 | return 1; |
| 2164 | |
| 2165 | connect_errReturn: |
| 2166 | |
| 2167 | /* |
| 2168 | * If we managed to open a socket, close it immediately rather than |
| 2169 | * waiting till PQfinish. (The application cannot have gotten the socket |
| 2170 | * from PQsocket yet, so this doesn't risk breaking anything.) |
no test coverage detected