From 0175b4a333bc426133dac1bfa8cf14d352a495ed Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Thu, 25 Mar 2010 19:11:08 +0100 Subject: [PATCH] postgresql plugin: Use the "complex read" mechanism. This way, multiple databases are queried in parallel (depending on the number of read threads). --- src/postgresql.c | 93 ++++++++++++++++++++---------------------------- 1 file changed, 38 insertions(+), 55 deletions(-) diff --git a/src/postgresql.c b/src/postgresql.c index 6a54fc0a..4f140b60 100644 --- a/src/postgresql.c +++ b/src/postgresql.c @@ -132,22 +132,16 @@ static int def_queries_num = STATIC_ARRAY_SIZE (def_queries); static udb_query_t **queries = NULL; static size_t queries_num = 0; -static c_psql_database_t *databases = NULL; -static int databases_num = 0; - static c_psql_database_t *c_psql_database_new (const char *name) { c_psql_database_t *db; - ++databases_num; - if (NULL == (databases = (c_psql_database_t *)realloc (databases, - databases_num * sizeof (*databases)))) { + db = (c_psql_database_t *)malloc (sizeof (*db)); + if (NULL == db) { log_err ("Out of memory."); exit (5); } - db = databases + (databases_num - 1); - db->conn = NULL; C_COMPLAIN_INIT (&db->conn_complaint); @@ -174,8 +168,10 @@ static c_psql_database_t *c_psql_database_new (const char *name) return db; } /* c_psql_database_new */ -static void c_psql_database_delete (c_psql_database_t *db) +static void c_psql_database_delete (void *data) { + c_psql_database_t *db = data; + PQfinish (db->conn); db->conn = NULL; @@ -447,57 +443,41 @@ static int c_psql_exec_query (c_psql_database_t *db, udb_query_t *q) #undef BAIL_OUT } /* c_psql_exec_query */ -static int c_psql_read (void) +static int c_psql_read (user_data_t *ud) { - int success = 0; + c_psql_database_t *db; int i; - for (i = 0; i < databases_num; ++i) { - c_psql_database_t *db = databases + i; - - int j; + if ((ud == NULL) || (ud->data == NULL)) { + log_err ("c_psql_read: Invalid user data."); + return -1; + } - assert (NULL != db->database); + db = ud->data; - if (0 != c_psql_check_connection (db)) - continue; + assert (NULL != db->database); - for (j = 0; j < db->queries_num; ++j) - { - udb_query_t *q; + if (0 != c_psql_check_connection (db)) + return -1; - q = db->queries[j]; + for (i = 0; i < db->queries_num; ++i) + { + udb_query_t *q; - if ((0 != db->server_version) - && (udb_query_check_version (q, db->server_version) <= 0)) - continue; + q = db->queries[i]; - c_psql_exec_query (db, q); - } + if ((0 != db->server_version) + && (udb_query_check_version (q, db->server_version) <= 0)) + return -1; - ++success; + c_psql_exec_query (db, q); } - - if (! success) - return -1; return 0; } /* c_psql_read */ static int c_psql_shutdown (void) { - int i; - - if ((NULL == databases) || (0 == databases_num)) - return 0; - - plugin_unregister_read ("postgresql"); - plugin_unregister_shutdown ("postgresql"); - - for (i = 0; i < databases_num; ++i) - c_psql_database_delete (databases + i); - - sfree (databases); - databases_num = 0; + plugin_unregister_read_group ("postgresql"); udb_query_free (queries, queries_num); queries = NULL; @@ -506,16 +486,6 @@ static int c_psql_shutdown (void) return 0; } /* c_psql_shutdown */ -static int c_psql_init (void) -{ - if ((NULL == databases) || (0 == databases_num)) - return 0; - - plugin_register_read ("postgresql", c_psql_read); - plugin_register_shutdown ("postgresql", c_psql_shutdown); - return 0; -} /* c_psql_init */ - static int config_set_s (char *name, char **var, const oconfig_item_t *ci) { if ((0 != ci->children_num) || (1 != ci->values_num) @@ -589,6 +559,9 @@ static int c_psql_config_database (oconfig_item_t *ci) { c_psql_database_t *db; + char cb_name[DATA_MAX_NAME_LEN]; + user_data_t ud; + int i; if ((1 != ci->values_num) @@ -597,6 +570,8 @@ static int c_psql_config_database (oconfig_item_t *ci) return 1; } + memset (&ud, 0, sizeof (ud)); + db = c_psql_database_new (ci->values[0].value.string); for (i = 0; i < ci->children_num; ++i) { @@ -638,6 +613,14 @@ static int c_psql_config_database (oconfig_item_t *ci) if ((data != NULL) && (data->params_num > db->max_params_num)) db->max_params_num = data->params_num; } + + ud.data = db; + ud.free_func = c_psql_database_delete; + + ssnprintf (cb_name, sizeof (cb_name), "postgresql-%s", db->database); + + plugin_register_complex_read ("postgresql", cb_name, c_psql_read, + /* interval = */ NULL, &ud); return 0; } /* c_psql_config_database */ @@ -681,7 +664,7 @@ static int c_psql_config (oconfig_item_t *ci) void module_register (void) { plugin_register_complex_config ("postgresql", c_psql_config); - plugin_register_init ("postgresql", c_psql_init); + plugin_register_shutdown ("postgresql", c_psql_shutdown); } /* module_register */ /* vim: set sw=4 ts=4 tw=78 noexpandtab : */ -- 2.30.2