From 8c8ad69c937a0dfdf328896c217c91499bf3e219 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Thu, 25 Mar 2010 18:49:48 +0100 Subject: [PATCH] postgresql plugin: Connect to the database in the read function. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit … rather than the init function. This way, we don't need access to some global list of databases in the init function and it's done in preparation to using the "complex read" system. --- src/postgresql.c | 120 ++++++++++++++++++++++++----------------------- 1 file changed, 61 insertions(+), 59 deletions(-) diff --git a/src/postgresql.c b/src/postgresql.c index e6527b95..6a54fc0a 100644 --- a/src/postgresql.c +++ b/src/postgresql.c @@ -196,8 +196,49 @@ static void c_psql_database_delete (c_psql_database_t *db) return; } /* c_psql_database_delete */ +static int c_psql_connect (c_psql_database_t *db) +{ + char conninfo[4096]; + char *buf = conninfo; + int buf_len = sizeof (conninfo); + int status; + + if (! db) + return -1; + + status = ssnprintf (buf, buf_len, "dbname = '%s'", db->database); + if (0 < status) { + buf += status; + buf_len -= status; + } + + C_PSQL_PAR_APPEND (buf, buf_len, "host", db->host); + C_PSQL_PAR_APPEND (buf, buf_len, "port", db->port); + C_PSQL_PAR_APPEND (buf, buf_len, "user", db->user); + C_PSQL_PAR_APPEND (buf, buf_len, "password", db->password); + C_PSQL_PAR_APPEND (buf, buf_len, "sslmode", db->sslmode); + C_PSQL_PAR_APPEND (buf, buf_len, "krbsrvname", db->krbsrvname); + C_PSQL_PAR_APPEND (buf, buf_len, "service", db->service); + + db->conn = PQconnectdb (conninfo); + db->proto_version = PQprotocolVersion (db->conn); + return 0; +} /* c_psql_connect */ + static int c_psql_check_connection (c_psql_database_t *db) { + _Bool init = 0; + + if (! db->conn) { + init = 1; + + /* trigger c_release() */ + if (0 == db->conn_complaint.interval) + db->conn_complaint.interval = 1; + + c_psql_connect (db); + } + /* "ping" */ PQclear (PQexec (db->conn, "SELECT 42;")); @@ -216,15 +257,30 @@ static int c_psql_check_connection (c_psql_database_t *db) } db->proto_version = PQprotocolVersion (db->conn); - if (3 > db->proto_version) - log_warn ("Protocol version %d does not support parameters.", - db->proto_version); } db->server_version = PQserverVersion (db->conn); - c_release (LOG_INFO, &db->conn_complaint, - "Successfully reconnected to database %s", PQdb (db->conn)); + if (c_would_release (&db->conn_complaint)) { + char *server_host; + int server_version; + + server_host = PQhost (db->conn); + server_version = PQserverVersion (db->conn); + + c_do_release (LOG_INFO, &db->conn_complaint, + "Successfully %sconnected to database %s (user %s) " + "at server %s%s%s (server version: %d.%d.%d, " + "protocol version: %d, pid: %d)", init ? "" : "re", + PQdb (db->conn), PQuser (db->conn), + C_PSQL_SOCKET3 (server_host, PQport (db->conn)), + C_PSQL_SERVER_VERSION3 (server_version), + db->proto_version, PQbackendPID (db->conn)); + + if (3 > db->proto_version) + log_warn ("Protocol version %d does not support parameters.", + db->proto_version); + } return 0; } /* c_psql_check_connection */ @@ -452,63 +508,9 @@ static int c_psql_shutdown (void) static int c_psql_init (void) { - int i; - if ((NULL == databases) || (0 == databases_num)) return 0; - for (i = 0; i < databases_num; ++i) { - c_psql_database_t *db = databases + i; - - char conninfo[4096]; - char *buf = conninfo; - int buf_len = sizeof (conninfo); - int status; - - char *server_host; - int server_version; - - /* this will happen during reinitialization */ - if (NULL != db->conn) { - c_psql_check_connection (db); - continue; - } - - status = ssnprintf (buf, buf_len, "dbname = '%s'", db->database); - if (0 < status) { - buf += status; - buf_len -= status; - } - - C_PSQL_PAR_APPEND (buf, buf_len, "host", db->host); - C_PSQL_PAR_APPEND (buf, buf_len, "port", db->port); - C_PSQL_PAR_APPEND (buf, buf_len, "user", db->user); - C_PSQL_PAR_APPEND (buf, buf_len, "password", db->password); - C_PSQL_PAR_APPEND (buf, buf_len, "sslmode", db->sslmode); - C_PSQL_PAR_APPEND (buf, buf_len, "krbsrvname", db->krbsrvname); - C_PSQL_PAR_APPEND (buf, buf_len, "service", db->service); - - db->conn = PQconnectdb (conninfo); - if (0 != c_psql_check_connection (db)) - continue; - - db->proto_version = PQprotocolVersion (db->conn); - - server_host = PQhost (db->conn); - server_version = PQserverVersion (db->conn); - log_info ("Successfully connected to database %s (user %s) " - "at server %s%s%s (server version: %d.%d.%d, " - "protocol version: %d, pid: %d)", - PQdb (db->conn), PQuser (db->conn), - C_PSQL_SOCKET3 (server_host, PQport (db->conn)), - C_PSQL_SERVER_VERSION3 (server_version), - db->proto_version, PQbackendPID (db->conn)); - - if (3 > db->proto_version) - log_warn ("Protocol version %d does not support parameters.", - db->proto_version); - } - plugin_register_read ("postgresql", c_psql_read); plugin_register_shutdown ("postgresql", c_psql_shutdown); return 0; -- 2.30.2