Merge branch 'collectd-4.5'
[collectd.git] / src / postgresql.c
index 409a5de..a5bacf4 100644 (file)
@@ -217,8 +217,7 @@ static c_psql_database_t *c_psql_database_new (const char *name)
 
        db->conn = NULL;
 
-       db->conn_complaint.last     = 0;
-       db->conn_complaint.interval = 0;
+       C_COMPLAIN_INIT (&db->conn_complaint);
 
        db->proto_version = 0;
 
@@ -244,6 +243,7 @@ static c_psql_database_t *c_psql_database_new (const char *name)
 static void c_psql_database_delete (c_psql_database_t *db)
 {
        PQfinish (db->conn);
+       db->conn = NULL;
 
        sfree (db->queries);
        db->queries_num = 0;
@@ -417,14 +417,17 @@ static int c_psql_exec_query (c_psql_database_t *db, int idx)
        }
 
        rows = PQntuples (res);
-       if (1 > rows)
+       if (1 > rows) {
+               PQclear (res);
                return 0;
+       }
 
        cols = PQnfields (res);
        if (query->cols_num != cols) {
                log_err ("SQL query returned wrong number of fields "
                                "(expected: %i, got: %i)", query->cols_num, cols);
                log_info ("SQL query was: %s", query->query);
+               PQclear (res);
                return -1;
        }
 
@@ -442,6 +445,7 @@ static int c_psql_exec_query (c_psql_database_t *db, int idx)
                                submit_gauge (db, col.type, col.type_instance, value);
                }
        }
+       PQclear (res);
        return 0;
 } /* c_psql_exec_query */
 
@@ -546,6 +550,12 @@ static int c_psql_init (void)
 
                int j;
 
+               /* 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;
@@ -774,6 +784,20 @@ static int c_psql_config_query (oconfig_item_t *ci)
                        log_warn ("Ignoring unknown config key \"%s\".", c->key);
        }
 
+       for (i = 0; i < queries_num - 1; ++i) {
+               c_psql_query_t *q = queries + i;
+
+               if ((0 == strcasecmp (q->name, query->name))
+                               && (q->min_pg_version <= query->max_pg_version)
+                               && (query->min_pg_version <= q->max_pg_version)) {
+                       log_err ("Ignoring redefinition (with overlapping version ranges) "
+                                       "of query \"%s\".", query->name);
+                       c_psql_query_delete (query);
+                       --queries_num;
+                       return 1;
+               }
+       }
+
        if (query->min_pg_version > query->max_pg_version) {
                log_err ("Query \"%s\": MinPGVersion > MaxPGVersion.",
                                query->name);