/* * ConnectToNode opens a connection to a remote PostgreSQL server. The function * configures the connection's fallback application name to 'pg_shard' and sets * the remote encoding to match the local one. This function requires that the * port be specified as a string for easier use with libpq functions. * * We attempt to connect up to MAX_CONNECT_ATTEMPT times. After that we give up * and return NULL. */ static PGconn * ConnectToNode(char *nodeName, char *nodePort) { PGconn *connection = NULL; const char *clientEncoding = GetDatabaseEncodingName(); const char *dbname = get_database_name(MyDatabaseId); const char *keywordArray[] = { "host", "port", "fallback_application_name", "client_encoding", "connect_timeout", "dbname", NULL }; const char *valueArray[] = { nodeName, nodePort, "pg_shard", clientEncoding, CLIENT_CONNECT_TIMEOUT_SECONDS, dbname, NULL }; Assert(sizeof(keywordArray) == sizeof(valueArray)); for (int attemptIndex = 0; attemptIndex < MAX_CONNECT_ATTEMPTS; attemptIndex++) { connection = PQconnectdbParams(keywordArray, valueArray, false); if (PQstatus(connection) == CONNECTION_OK) { break; } else { /* warn if still erroring on final attempt */ if (attemptIndex == MAX_CONNECT_ATTEMPTS - 1) { ReportRemoteError(connection, NULL); } PQfinish(connection); connection = NULL; } } return connection; }
/* * ReraiseRemoteError retrieves error fields from a remote result and re-raises * the error after amending it with a CONTEXT field containing the remote node * host and port information. */ void ReraiseRemoteError(PGconn *connection, PGresult *result) { ReportRemoteError(connection, result, true); }
/* * WarnRemoteError retrieves error fields from a remote result and produces an * error report at the WARNING level after amending the error with a CONTEXT * field containing the remote node host and port information. */ void WarnRemoteError(PGconn *connection, PGresult *result) { ReportRemoteError(connection, result, false); }