Index: res/res_config_pgsql.c =================================================================== --- res/res_config_pgsql.c (revision 310878) +++ res/res_config_pgsql.c (working copy) @@ -209,11 +209,18 @@ ast_str_set(&sql, 0, "SELECT a.attname, t.typname, a.attlen, a.attnotnull, d.adsrc, a.atttypmod FROM pg_class c, pg_type t, pg_attribute a LEFT OUTER JOIN pg_attrdef d ON a.atthasdef AND d.adrelid = a.attrelid AND d.adnum = a.attnum WHERE c.oid = a.attrelid AND a.atttypid = t.oid AND (a.attnum > 0) AND c.relname = '%s' ORDER BY c.relname, attnum", orig_tablename); } + ast_mutex_lock(&pgsql_lock); + if (!pgsql_reconnect(NULL)) { + ast_mutex_unlock(&pgsql_lock); + return NULL; + } + result = PQexec(pgsqlConn, ast_str_buffer(sql)); ast_debug(1, "Query of table structure complete. Now retrieving results.\n"); if (PQresultStatus(result) != PGRES_TUPLES_OK) { pgerror = PQresultErrorMessage(result); ast_log(LOG_ERROR, "Failed to query database columns: %s\n", pgerror); + ast_mutex_unlock(&pgsql_lock); PQclear(result); AST_LIST_UNLOCK(&psql_tables); return NULL; @@ -221,6 +228,8 @@ if (!(table = ast_calloc(1, sizeof(*table) + strlen(orig_tablename) + 1))) { ast_log(LOG_ERROR, "Unable to allocate memory for new table structure\n"); + ast_mutex_unlock(&pgsql_lock); + PQclear(result); AST_LIST_UNLOCK(&psql_tables); return NULL; } @@ -240,6 +249,8 @@ if (!(column = ast_calloc(1, sizeof(*column) + strlen(fname) + strlen(ftype) + 2))) { ast_log(LOG_ERROR, "Unable to allocate column element for %s, %s\n", orig_tablename, fname); destroy_table(table); + ast_mutex_unlock(&pgsql_lock); + PQclear(result); AST_LIST_UNLOCK(&psql_tables); return NULL; } @@ -268,6 +279,8 @@ } AST_LIST_INSERT_TAIL(&table->columns, column, list); } + + ast_mutex_unlock(&pgsql_lock); PQclear(result); AST_LIST_INSERT_TAIL(&psql_tables, table, list);