MCPcopy Create free account
hub / github.com/apache/cloudberry / connectDBStart

Function connectDBStart

src/interfaces/libpq/fe-connect.c:2113–2175  ·  view source on GitHub ↗

---------- * connectDBStart - * Begin the process of making a connection to the backend. * * Returns 1 if successful, 0 if not. * ---------- */

Source from the content-addressed store, hash-verified

2111 * ----------
2112 */
2113static int
2114connectDBStart(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
2165connect_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.)

Callers 5

PQconnectStartParamsFunction · 0.85
PQconnectStartFunction · 0.85
PQsetdbLoginFunction · 0.85
PQresetFunction · 0.85
PQresetStartFunction · 0.85

Calls 4

appendPQExpBufferStrFunction · 0.85
PQconnectPollFunction · 0.85
pqDropConnectionFunction · 0.85

Tested by

no test coverage detected