Merge "res_pjsip: Ignore empty TLS configuration"
[asterisk/asterisk.git] / res / res_config_pgsql.c
index 214d9ee..e436e2f 100644 (file)
@@ -1,8 +1,8 @@
 /*
- * Asterisk -- A telephony toolkit for Linux.
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 1999 - 2017, Digium, Inc.
  *
- * Copyright (C) 1999-2005, Digium, Inc.
- * 
  * Manuel Guesdon <mguesdon@oxymium.net> - PostgreSQL RealTime Driver Author/Adaptor
  * Mark Spencer <markster@digium.com>  - Asterisk Author
  * Matthew Boehm <mboehm@cytelcom.com> - MySQL RealTime Driver Author
  * \author Mark Spencer <markster@digium.com>
  * \author Manuel Guesdon <mguesdon@oxymium.net> - PostgreSQL RealTime Driver Author/Adaptor
  *
- * \arg http://www.postgresql.org
+ * PostgreSQL http://www.postgresql.org
  */
 
 /*** MODULEINFO
        <depend>pgsql</depend>
+       <support_level>extended</support_level>
  ***/
 
 #include "asterisk.h"
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
-
 #include <libpq-fe.h>                  /* PostgreSQL */
 
 #include "asterisk/file.h"
@@ -42,10 +41,18 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/cli.h"
 
 AST_MUTEX_DEFINE_STATIC(pgsql_lock);
+AST_THREADSTORAGE(sql_buf);
+AST_THREADSTORAGE(findtable_buf);
+AST_THREADSTORAGE(where_buf);
+AST_THREADSTORAGE(escapebuf_buf);
+AST_THREADSTORAGE(semibuf_buf);
 
 #define RES_CONFIG_PGSQL_CONF "res_pgsql.conf"
 
-PGconn *pgsqlConn = NULL;
+static PGconn *pgsqlConn = NULL;
+static int version;
+#define has_schema_support     (version > 70300 ? 1 : 0)
+#define USE_BACKSLASH_AS_STRING        (version >= 90100 ? 1 : 0)
 
 #define MAX_DB_OPTION_SIZE 64
 
@@ -59,7 +66,7 @@ struct columns {
 };
 
 struct tables {
-       ast_mutex_t lock;
+       ast_rwlock_t lock;
        AST_LIST_HEAD_NOLOCK(psql_columns, columns) columns;
        AST_LIST_ENTRY(tables) list;
        char name[0];
@@ -71,6 +78,7 @@ static char dbhost[MAX_DB_OPTION_SIZE] = "";
 static char dbuser[MAX_DB_OPTION_SIZE] = "";
 static char dbpass[MAX_DB_OPTION_SIZE] = "";
 static char dbname[MAX_DB_OPTION_SIZE] = "";
+static char dbappname[MAX_DB_OPTION_SIZE] = "";
 static char dbsock[MAX_DB_OPTION_SIZE] = "";
 static int dbport = 5432;
 static time_t connect_time = 0;
@@ -80,69 +88,240 @@ static int pgsql_reconnect(const char *database);
 static char *handle_cli_realtime_pgsql_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
 static char *handle_cli_realtime_pgsql_cache(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
 
-enum { RQ_WARN, RQ_CREATECLOSE, RQ_CREATECHAR } requirements;
+static enum { RQ_WARN, RQ_CREATECLOSE, RQ_CREATECHAR } requirements;
 
 static struct ast_cli_entry cli_realtime[] = {
        AST_CLI_DEFINE(handle_cli_realtime_pgsql_status, "Shows connection information for the PostgreSQL RealTime driver"),
        AST_CLI_DEFINE(handle_cli_realtime_pgsql_cache, "Shows cached tables within the PostgreSQL realtime driver"),
 };
 
+#define ESCAPE_STRING(buffer, stringname) \
+       do { \
+               int len = strlen(stringname); \
+               struct ast_str *semi = ast_str_thread_get(&semibuf_buf, len * 3 + 1); \
+               const char *chunk = stringname; \
+               ast_str_reset(semi); \
+               for (; *chunk; chunk++) { \
+                       if (strchr(";^", *chunk)) { \
+                               ast_str_append(&semi, 0, "^%02hhX", *chunk); \
+                       } else { \
+                               ast_str_append(&semi, 0, "%c", *chunk); \
+                       } \
+               } \
+               if (ast_str_strlen(semi) > (ast_str_size(buffer) - 1) / 2) { \
+                       ast_str_make_space(&buffer, ast_str_strlen(semi) * 2 + 1); \
+               } \
+               PQescapeStringConn(pgsqlConn, ast_str_buffer(buffer), ast_str_buffer(semi), ast_str_size(buffer), &pgresult); \
+       } while (0)
+
 static void destroy_table(struct tables *table)
 {
        struct columns *column;
-       ast_mutex_lock(&table->lock);
+       ast_rwlock_wrlock(&table->lock);
        while ((column = AST_LIST_REMOVE_HEAD(&table->columns, list))) {
                ast_free(column);
        }
-       ast_mutex_unlock(&table->lock);
-       ast_mutex_destroy(&table->lock);
+       ast_rwlock_unlock(&table->lock);
+       ast_rwlock_destroy(&table->lock);
        ast_free(table);
 }
 
-static struct tables *find_table(const char *tablename)
+/*! \brief Helper function for pgsql_exec.  For running querys, use pgsql_exec()
+ *
+ *  Connect if not currently connected.  Run the given query.
+ *
+ *  \param database   database name we are connected to (used for error logging)
+ *  \param tablename  table  name we are connected to (used for error logging)
+ *  \param sql        sql query string to execute
+ *  \param result     pointer for where to store the result handle
+ *
+ *  \return -1 on fatal query error
+ *  \return -2 on query failure that resulted in disconnection
+ *  \return 0 on success
+ *
+ *  \note see pgsql_exec for full example
+ */
+static int _pgsql_exec(const char *database, const char *tablename, const char *sql, PGresult **result)
+{
+       ExecStatusType result_status;
+
+       if (!pgsqlConn) {
+               ast_debug(1, "PostgreSQL connection not defined, connecting\n");
+
+               if (pgsql_reconnect(database) != 1) {
+                       ast_log(LOG_NOTICE, "reconnect failed\n");
+                       *result = NULL;
+                       return -1;
+               }
+
+               ast_debug(1, "PostgreSQL connection successful\n");
+       }
+
+       *result = PQexec(pgsqlConn, sql);
+       result_status = PQresultStatus(*result);
+       if (result_status != PGRES_COMMAND_OK
+               && result_status != PGRES_TUPLES_OK
+               && result_status != PGRES_NONFATAL_ERROR) {
+
+               ast_log(LOG_ERROR, "PostgreSQL RealTime: Failed to query '%s@%s'.\n", tablename, database);
+               ast_log(LOG_ERROR, "PostgreSQL RealTime: Query Failed: %s\n", sql);
+               ast_log(LOG_ERROR, "PostgreSQL RealTime: Query Failed because: %s (%s)\n",
+                       PQresultErrorMessage(*result),
+                       PQresStatus(result_status));
+
+               /* we may have tried to run a command on a disconnected/disconnecting handle */
+               /* are we no longer connected to the database... if not try again */
+               if (PQstatus(pgsqlConn) != CONNECTION_OK) {
+                       PQfinish(pgsqlConn);
+                       pgsqlConn = NULL;
+                       return -2;
+               }
+
+               /* connection still okay, which means the query is just plain bad */
+               return -1;
+       }
+
+       ast_debug(1, "PostgreSQL query successful: %s\n", sql);
+       return 0;
+}
+
+/*! \brief Do a postgres query, with reconnection support
+ *
+ *  Connect if not currently connected.  Run the given query
+ *  and if we're disconnected afterwards, reconnect and query again.
+ *
+ *  \param database   database name we are connected to (used for error logging)
+ *  \param tablename  table  name we are connected to (used for error logging)
+ *  \param sql        sql query string to execute
+ *  \param result     pointer for where to store the result handle
+ *
+ *  \return -1 on query failure
+ *  \return 0 on success
+ *
+ *  \code
+ *     int i, rows;
+ *     PGresult *result;
+ *     char *field_name, *field_type, *field_len, *field_notnull, *field_default;
+ *
+ *     pgsql_exec("db", "table", "SELECT 1", &result)
+ *
+ *     rows = PQntuples(result);
+ *     for (i = 0; i < rows; i++) {
+ *             field_name    = PQgetvalue(result, i, 0);
+ *             field_type    = PQgetvalue(result, i, 1);
+ *             field_len     = PQgetvalue(result, i, 2);
+ *             field_notnull = PQgetvalue(result, i, 3);
+ *             field_default = PQgetvalue(result, i, 4);
+ *     }
+ *  \endcode
+ */
+static int pgsql_exec(const char *database, const char *tablename, const char *sql, PGresult **result)
+{
+       int attempts = 0;
+       int res;
+
+       /* Try the query, note failure if any */
+       /* On first failure, reconnect and try again (_pgsql_exec handles reconnect) */
+       /* On second failure, treat as fatal query error */
+
+       while (attempts++ < 2) {
+               ast_debug(1, "PostgreSQL query attempt %d\n", attempts);
+               res = _pgsql_exec(database, tablename, sql, result);
+
+               if (res == 0) {
+                       if (attempts > 1) {
+                               ast_log(LOG_NOTICE, "PostgreSQL RealTime: Query finally succeeded: %s\n", sql);
+                       }
+
+                       return 0;
+               }
+
+               if (res == -1) {
+                       return -1; /* Still connected to db, but could not process query (fatal error) */
+               }
+
+               /* res == -2 (query on a disconnected handle) */
+               ast_debug(1, "PostgreSQL query attempt %d failed, trying again\n", attempts);
+       }
+
+       return -1;
+}
+
+static struct tables *find_table(const char *database, const char *orig_tablename)
 {
        struct columns *column;
        struct tables *table;
-       struct ast_str *sql = ast_str_create(330);
-       char *pgerror;
-       PGresult *result;
+       struct ast_str *sql = ast_str_thread_get(&findtable_buf, 330);
+       RAII_VAR(PGresult *, result, NULL, PQclear);
+       int exec_result;
        char *fname, *ftype, *flen, *fnotnull, *fdef;
        int i, rows;
 
        AST_LIST_LOCK(&psql_tables);
        AST_LIST_TRAVERSE(&psql_tables, table, list) {
-               if (!strcasecmp(table->name, tablename)) {
+               if (!strcasecmp(table->name, orig_tablename)) {
                        ast_debug(1, "Found table in cache; now locking\n");
-                       ast_mutex_lock(&table->lock);
+                       ast_rwlock_rdlock(&table->lock);
                        ast_debug(1, "Lock cached table; now returning\n");
                        AST_LIST_UNLOCK(&psql_tables);
                        return table;
                }
        }
 
-       ast_debug(1, "Table '%s' not found in cache, querying now\n", tablename);
+       if (database == NULL) {
+               AST_LIST_UNLOCK(&psql_tables);
+               return NULL;
+       }
+
+       ast_debug(1, "Table '%s' not found in cache, querying now\n", orig_tablename);
 
        /* Not found, scan the table */
-       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", tablename);
-       result = PQexec(pgsqlConn, sql->str);
+       if (has_schema_support) {
+               char *schemaname, *tablename, *tmp_schemaname, *tmp_tablename;
+               if (strchr(orig_tablename, '.')) {
+                       tmp_schemaname = ast_strdupa(orig_tablename);
+                       tmp_tablename = strchr(tmp_schemaname, '.');
+                       *tmp_tablename++ = '\0';
+               } else {
+                       tmp_schemaname = "";
+                       tmp_tablename = ast_strdupa(orig_tablename);
+               }
+
+               tablename = ast_alloca(strlen(tmp_tablename) * 2 + 1);
+               PQescapeStringConn(pgsqlConn, tablename, tmp_tablename, strlen(tmp_tablename), NULL);
+               schemaname = ast_alloca(strlen(tmp_schemaname) * 2 + 1);
+               PQescapeStringConn(pgsqlConn, schemaname, tmp_schemaname, strlen(tmp_schemaname), NULL);
+
+               ast_str_set(&sql, 0, "SELECT a.attname, t.typname, a.attlen, a.attnotnull, d.adsrc, a.atttypmod FROM (((pg_catalog.pg_class c INNER JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace AND c.relname = '%s' AND n.nspname = %s%s%s) INNER JOIN pg_catalog.pg_attribute a ON (NOT a.attisdropped) AND a.attnum > 0 AND a.attrelid = c.oid) INNER JOIN pg_catalog.pg_type t ON t.oid = a.atttypid) LEFT OUTER JOIN pg_attrdef d ON a.atthasdef AND d.adrelid = a.attrelid AND d.adnum = a.attnum ORDER BY n.nspname, c.relname, attnum",
+                       tablename,
+                       ast_strlen_zero(schemaname) ? "" : "'", ast_strlen_zero(schemaname) ? "current_schema()" : schemaname, ast_strlen_zero(schemaname) ? "" : "'");
+       } else {
+               char *tablename;
+               tablename = ast_alloca(strlen(orig_tablename) * 2 + 1);
+               PQescapeStringConn(pgsqlConn, tablename, orig_tablename, strlen(orig_tablename), NULL);
+
+               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", tablename);
+       }
+
+       ast_mutex_lock(&pgsql_lock);
+       exec_result = pgsql_exec(database, orig_tablename, ast_str_buffer(sql), &result);
+       ast_mutex_unlock(&pgsql_lock);
        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);
-               PQclear(result);
+       if (exec_result != 0) {
+               ast_log(LOG_ERROR, "Failed to query database columns for table %s\n", orig_tablename);
                AST_LIST_UNLOCK(&psql_tables);
                return NULL;
        }
 
-       if (!(table = ast_calloc(1, sizeof(*table) + strlen(tablename) + 1))) {
+       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_LIST_UNLOCK(&psql_tables);
                return NULL;
        }
-       strcpy(table->name, tablename); /* SAFE */
-       ast_mutex_init(&table->lock);
+       strcpy(table->name, orig_tablename); /* SAFE */
+       ast_rwlock_init(&table->lock);
        AST_LIST_HEAD_INIT_NOLOCK(&table->columns);
-       
+
        rows = PQntuples(result);
        for (i = 0; i < rows; i++) {
                fname = PQgetvalue(result, i, 0);
@@ -153,7 +332,7 @@ static struct tables *find_table(const char *tablename)
                ast_verb(4, "Found column '%s' of type '%s'\n", fname, ftype);
 
                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", tablename, fname);
+                       ast_log(LOG_ERROR, "Unable to allocate column element for %s, %s\n", orig_tablename, fname);
                        destroy_table(table);
                        AST_LIST_UNLOCK(&psql_tables);
                        return NULL;
@@ -162,10 +341,10 @@ static struct tables *find_table(const char *tablename)
                if (strcmp(flen, "-1") == 0) {
                        /* Some types, like chars, have the length stored in a different field */
                        flen = PQgetvalue(result, i, 5);
-                       sscanf(flen, "%d", &column->len);
+                       sscanf(flen, "%30d", &column->len);
                        column->len -= 4;
                } else {
-                       sscanf(flen, "%d", &column->len);
+                       sscanf(flen, "%30d", &column->len);
                }
                column->name = (char *)column + sizeof(*column);
                column->type = (char *)column + sizeof(*column) + strlen(fname) + 1;
@@ -183,105 +362,124 @@ static struct tables *find_table(const char *tablename)
                }
                AST_LIST_INSERT_TAIL(&table->columns, column, list);
        }
-       PQclear(result);
 
        AST_LIST_INSERT_TAIL(&psql_tables, table, list);
-       ast_mutex_lock(&table->lock);
+       ast_rwlock_rdlock(&table->lock);
        AST_LIST_UNLOCK(&psql_tables);
        return table;
 }
 
-static struct ast_variable *realtime_pgsql(const char *database, const char *table, va_list ap)
+#define release_table(table) ast_rwlock_unlock(&(table)->lock);
+
+static struct columns *find_column(struct tables *t, const char *colname)
+{
+       struct columns *column;
+
+       /* Check that the column exists in the table */
+       AST_LIST_TRAVERSE(&t->columns, column, list) {
+               if (strcmp(column->name, colname) == 0) {
+                       return column;
+               }
+       }
+       return NULL;
+}
+
+#define IS_SQL_LIKE_CLAUSE(x) ((x) && ast_ends_with(x, " LIKE"))
+#define ESCAPE_CLAUSE (USE_BACKSLASH_AS_STRING ? " ESCAPE '\\'" : " ESCAPE '\\\\'")
+
+static struct ast_variable *realtime_pgsql(const char *database, const char *tablename, const struct ast_variable *fields)
 {
-       PGresult *result = NULL;
-       int num_rows = 0, pgerror;
-       char sql[256], escapebuf[513];
+       RAII_VAR(PGresult *, result, NULL, PQclear);
+       int num_rows = 0, pgresult;
+       struct ast_str *sql = ast_str_thread_get(&sql_buf, 100);
+       struct ast_str *escapebuf = ast_str_thread_get(&escapebuf_buf, 100);
        char *stringp;
        char *chunk;
        char *op;
-       const char *newparam, *newval;
+       char *escape = "";
+       const struct ast_variable *field = fields;
        struct ast_variable *var = NULL, *prev = NULL;
 
-       if (!table) {
+       /*
+        * Ignore database from the extconfig.conf since it was
+        * configured by res_pgsql.conf.
+        */
+       database = dbname;
+
+       if (!tablename) {
                ast_log(LOG_WARNING, "PostgreSQL RealTime: No table specified.\n");
                return NULL;
        }
 
+       /*
+        * Must connect to the server before anything else as ESCAPE_STRING()
+        * uses pgsqlConn
+        */
+       ast_mutex_lock(&pgsql_lock);
+       if (!pgsql_reconnect(database)) {
+               ast_mutex_unlock(&pgsql_lock);
+               return NULL;
+       }
+
        /* Get the first parameter and first value in our list of passed paramater/value pairs */
-       newparam = va_arg(ap, const char *);
-       newval = va_arg(ap, const char *);
-       if (!newparam || !newval) {
+       if (!field) {
                ast_log(LOG_WARNING,
                                "PostgreSQL RealTime: Realtime retrieval requires at least 1 parameter and 1 value to search on.\n");
                if (pgsqlConn) {
                        PQfinish(pgsqlConn);
                        pgsqlConn = NULL;
-               };
+               }
+               ast_mutex_unlock(&pgsql_lock);
                return NULL;
        }
 
        /* Create the first part of the query using the first parameter/value pairs we just extracted
           If there is only 1 set, then we have our query. Otherwise, loop thru the list and concat */
-       op = strchr(newparam, ' ') ? "" : " =";
+       if (!strchr(field->name, ' ')) {
+               op = " =";
+       } else {
+               op = "";
+               if (IS_SQL_LIKE_CLAUSE(field->name)) {
+                       escape = ESCAPE_CLAUSE;
+               }
+       }
 
-       PQescapeStringConn(pgsqlConn, escapebuf, newval, (sizeof(escapebuf) - 1) / 2, &pgerror);
-       if (pgerror) {
-               ast_log(LOG_ERROR, "Postgres detected invalid input: '%s'\n", newval);
-               va_end(ap);
+       ESCAPE_STRING(escapebuf, field->value);
+       if (pgresult) {
+               ast_log(LOG_ERROR, "PostgreSQL RealTime: detected invalid input: '%s'\n", field->value);
+               ast_mutex_unlock(&pgsql_lock);
                return NULL;
        }
 
-       snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE %s%s '%s'", table, newparam, op,
-                        escapebuf);
-       while ((newparam = va_arg(ap, const char *))) {
-               newval = va_arg(ap, const char *);
-               if (!strchr(newparam, ' '))
+       ast_str_set(&sql, 0, "SELECT * FROM %s WHERE %s%s '%s'%s", tablename, field->name, op, ast_str_buffer(escapebuf), escape);
+       while ((field = field->next)) {
+               escape = "";
+               if (!strchr(field->name, ' ')) {
                        op = " =";
-               else
+               } else {
                        op = "";
+                       if (IS_SQL_LIKE_CLAUSE(field->name)) {
+                               escape = ESCAPE_CLAUSE;
+                       }
+               }
 
-               PQescapeStringConn(pgsqlConn, escapebuf, newval, (sizeof(escapebuf) - 1) / 2, &pgerror);
-               if (pgerror) {
-                       ast_log(LOG_ERROR, "Postgres detected invalid input: '%s'\n", newval);
-                       va_end(ap);
+               ESCAPE_STRING(escapebuf, field->value);
+               if (pgresult) {
+                       ast_log(LOG_ERROR, "PostgreSQL RealTime: detected invalid input: '%s'\n", field->value);
+                       ast_mutex_unlock(&pgsql_lock);
                        return NULL;
                }
 
-               snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " AND %s%s '%s'", newparam,
-                                op, escapebuf);
+               ast_str_append(&sql, 0, " AND %s%s '%s'%s", field->name, op, ast_str_buffer(escapebuf), escape);
        }
-       va_end(ap);
 
        /* We now have our complete statement; Lets connect to the server and execute it. */
-       ast_mutex_lock(&pgsql_lock);
-       if (!pgsql_reconnect(database)) {
-               ast_mutex_unlock(&pgsql_lock);
-               return NULL;
-       }
-
-       if (!(result = PQexec(pgsqlConn, sql))) {
-               ast_log(LOG_WARNING,
-                               "PostgreSQL RealTime: Failed to query database. Check debug for more info.\n");
-               ast_debug(1, "PostgreSQL RealTime: Query: %s\n", sql);
-               ast_debug(1, "PostgreSQL RealTime: Query Failed because: %s\n", PQerrorMessage(pgsqlConn));
+        if (pgsql_exec(database, tablename, ast_str_buffer(sql), &result) != 0) {
                ast_mutex_unlock(&pgsql_lock);
                return NULL;
-       } else {
-               ExecStatusType result_status = PQresultStatus(result);
-               if (result_status != PGRES_COMMAND_OK
-                       && result_status != PGRES_TUPLES_OK
-                       && result_status != PGRES_NONFATAL_ERROR) {
-                       ast_log(LOG_WARNING,
-                                       "PostgreSQL RealTime: Failed to query database. Check debug for more info.\n");
-                       ast_debug(1, "PostgreSQL RealTime: Query: %s\n", sql);
-                       ast_debug(1, "PostgreSQL RealTime: Query Failed because: %s (%s)\n",
-                                               PQresultErrorMessage(result), PQresStatus(result_status));
-                       ast_mutex_unlock(&pgsql_lock);
-                       return NULL;
-               }
-       }
+        }
 
-       ast_debug(1, "PostgreSQL RealTime: Result=%p Query: %s\n", result, sql);
+       ast_debug(1, "PostgreSQL RealTime: Result=%p Query: %s\n", result, ast_str_buffer(sql));
 
        if ((num_rows = PQntuples(result)) > 0) {
                int i = 0;
@@ -293,7 +491,6 @@ static struct ast_variable *realtime_pgsql(const char *database, const char *tab
 
                if (!(fieldnames = ast_calloc(1, numFields * sizeof(char *)))) {
                        ast_mutex_unlock(&pgsql_lock);
-                       PQclear(result);
                        return NULL;
                }
                for (i = 0; i < numFields; i++)
@@ -303,7 +500,7 @@ static struct ast_variable *realtime_pgsql(const char *database, const char *tab
                                stringp = PQgetvalue(result, rowIndex, i);
                                while (stringp) {
                                        chunk = strsep(&stringp, ";");
-                                       if (!ast_strlen_zero(ast_strip(chunk))) {
+                                       if (chunk && !ast_strlen_zero(ast_realtime_decode_chunk(ast_strip(chunk)))) {
                                                if (prev) {
                                                        prev->next = ast_variable_new(fieldnames[i], chunk, "");
                                                        if (prev->next) {
@@ -318,29 +515,36 @@ static struct ast_variable *realtime_pgsql(const char *database, const char *tab
                }
                ast_free(fieldnames);
        } else {
-               ast_debug(1, "Postgresql RealTime: Could not find any rows in table %s.\n", table);
+               ast_debug(1, "Postgresql RealTime: Could not find any rows in table %s@%s.\n", tablename, database);
        }
 
        ast_mutex_unlock(&pgsql_lock);
-       PQclear(result);
 
        return var;
 }
 
-static struct ast_config *realtime_multi_pgsql(const char *database, const char *table, va_list ap)
+static struct ast_config *realtime_multi_pgsql(const char *database, const char *table, const struct ast_variable *fields)
 {
-       PGresult *result = NULL;
-       int num_rows = 0, pgerror;
-       char sql[256], escapebuf[513];
+       RAII_VAR(PGresult *, result, NULL, PQclear);
+       int num_rows = 0, pgresult;
+       struct ast_str *sql = ast_str_thread_get(&sql_buf, 100);
+       struct ast_str *escapebuf = ast_str_thread_get(&escapebuf_buf, 100);
+       const struct ast_variable *field = fields;
        const char *initfield = NULL;
        char *stringp;
        char *chunk;
        char *op;
-       const char *newparam, *newval;
+       char *escape = "";
        struct ast_variable *var = NULL;
        struct ast_config *cfg = NULL;
        struct ast_category *cat = NULL;
 
+       /*
+        * Ignore database from the extconfig.conf since it was
+        * configured by res_pgsql.conf.
+        */
+       database = dbname;
+
        if (!table) {
                ast_log(LOG_WARNING, "PostgreSQL RealTime: No table specified.\n");
                return NULL;
@@ -349,20 +553,30 @@ static struct ast_config *realtime_multi_pgsql(const char *database, const char
        if (!(cfg = ast_config_new()))
                return NULL;
 
+       /*
+        * Must connect to the server before anything else as ESCAPE_STRING()
+        * uses pgsqlConn
+        */
+       ast_mutex_lock(&pgsql_lock);
+       if (!pgsql_reconnect(database)) {
+               ast_mutex_unlock(&pgsql_lock);
+               return NULL;
+       }
+
        /* Get the first parameter and first value in our list of passed paramater/value pairs */
-       newparam = va_arg(ap, const char *);
-       newval = va_arg(ap, const char *);
-       if (!newparam || !newval) {
+       if (!field) {
                ast_log(LOG_WARNING,
                                "PostgreSQL RealTime: Realtime retrieval requires at least 1 parameter and 1 value to search on.\n");
                if (pgsqlConn) {
                        PQfinish(pgsqlConn);
                        pgsqlConn = NULL;
-               };
+               }
+               ast_mutex_unlock(&pgsql_lock);
+               ast_config_destroy(cfg);
                return NULL;
        }
 
-       initfield = ast_strdupa(newparam);
+       initfield = ast_strdupa(field->name);
        if ((op = strchr(initfield, ' '))) {
                *op = '\0';
        }
@@ -370,57 +584,56 @@ static struct ast_config *realtime_multi_pgsql(const char *database, const char
        /* Create the first part of the query using the first parameter/value pairs we just extracted
           If there is only 1 set, then we have our query. Otherwise, loop thru the list and concat */
 
-       if (!strchr(newparam, ' '))
+       if (!strchr(field->name, ' ')) {
                op = " =";
-       else
+               escape = "";
+       } else {
                op = "";
+               if (IS_SQL_LIKE_CLAUSE(field->name)) {
+                       escape = ESCAPE_CLAUSE;
+               }
+       }
 
-       PQescapeStringConn(pgsqlConn, escapebuf, newval, (sizeof(escapebuf) - 1) / 2, &pgerror);
-       if (pgerror) {
-               ast_log(LOG_ERROR, "Postgres detected invalid input: '%s'\n", newval);
-               va_end(ap);
+       ESCAPE_STRING(escapebuf, field->value);
+       if (pgresult) {
+               ast_log(LOG_ERROR, "PostgreSQL RealTime: detected invalid input: '%s'\n", field->value);
+               ast_mutex_unlock(&pgsql_lock);
+               ast_config_destroy(cfg);
                return NULL;
        }
 
-       snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE %s%s '%s'", table, newparam, op,
-                        escapebuf);
-       while ((newparam = va_arg(ap, const char *))) {
-               newval = va_arg(ap, const char *);
-               if (!strchr(newparam, ' '))
+       ast_str_set(&sql, 0, "SELECT * FROM %s WHERE %s%s '%s'%s", table, field->name, op, ast_str_buffer(escapebuf), escape);
+       while ((field = field->next)) {
+               escape = "";
+               if (!strchr(field->name, ' ')) {
                        op = " =";
-               else
+                       escape = "";
+               } else {
                        op = "";
+                       if (IS_SQL_LIKE_CLAUSE(field->name)) {
+                               escape = ESCAPE_CLAUSE;
+                       }
+               }
 
-               PQescapeStringConn(pgsqlConn, escapebuf, newval, (sizeof(escapebuf) - 1) / 2, &pgerror);
-               if (pgerror) {
-                       ast_log(LOG_ERROR, "Postgres detected invalid input: '%s'\n", newval);
-                       va_end(ap);
+               ESCAPE_STRING(escapebuf, field->value);
+               if (pgresult) {
+                       ast_log(LOG_ERROR, "PostgreSQL RealTime: detected invalid input: '%s'\n", field->value);
+                       ast_mutex_unlock(&pgsql_lock);
+                       ast_config_destroy(cfg);
                        return NULL;
                }
 
-               snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " AND %s%s '%s'", newparam,
-                                op, escapebuf);
+               ast_str_append(&sql, 0, " AND %s%s '%s'%s", field->name, op, ast_str_buffer(escapebuf), escape);
        }
 
        if (initfield) {
-               snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " ORDER BY %s", initfield);
+               ast_str_append(&sql, 0, " ORDER BY %s", initfield);
        }
 
-       va_end(ap);
-
        /* We now have our complete statement; Lets connect to the server and execute it. */
-       ast_mutex_lock(&pgsql_lock);
-       if (!pgsql_reconnect(database)) {
-               ast_mutex_unlock(&pgsql_lock);
-               return NULL;
-       }
-
-       if (!(result = PQexec(pgsqlConn, sql))) {
-               ast_log(LOG_WARNING,
-                               "PostgreSQL RealTime: Failed to query database. Check debug for more info.\n");
-               ast_debug(1, "PostgreSQL RealTime: Query: %s\n", sql);
-               ast_debug(1, "PostgreSQL RealTime: Query Failed because: %s\n", PQerrorMessage(pgsqlConn));
+       if (pgsql_exec(database, table, ast_str_buffer(sql), &result) != 0) {
                ast_mutex_unlock(&pgsql_lock);
+               ast_config_destroy(cfg);
                return NULL;
        } else {
                ExecStatusType result_status = PQresultStatus(result);
@@ -428,16 +641,17 @@ static struct ast_config *realtime_multi_pgsql(const char *database, const char
                        && result_status != PGRES_TUPLES_OK
                        && result_status != PGRES_NONFATAL_ERROR) {
                        ast_log(LOG_WARNING,
-                                       "PostgreSQL RealTime: Failed to query database. Check debug for more info.\n");
-                       ast_debug(1, "PostgreSQL RealTime: Query: %s\n", sql);
+                                       "PostgreSQL RealTime: Failed to query %s@%s. Check debug for more info.\n", table, database);
+                       ast_debug(1, "PostgreSQL RealTime: Query: %s\n", ast_str_buffer(sql));
                        ast_debug(1, "PostgreSQL RealTime: Query Failed because: %s (%s)\n",
                                                PQresultErrorMessage(result), PQresStatus(result_status));
                        ast_mutex_unlock(&pgsql_lock);
+                       ast_config_destroy(cfg);
                        return NULL;
                }
        }
 
-       ast_debug(1, "PostgreSQL RealTime: Result=%p Query: %s\n", result, sql);
+       ast_debug(1, "PostgreSQL RealTime: Result=%p Query: %s\n", result, ast_str_buffer(sql));
 
        if ((num_rows = PQntuples(result)) > 0) {
                int numFields = PQnfields(result);
@@ -449,7 +663,7 @@ static struct ast_config *realtime_multi_pgsql(const char *database, const char
 
                if (!(fieldnames = ast_calloc(1, numFields * sizeof(char *)))) {
                        ast_mutex_unlock(&pgsql_lock);
-                       PQclear(result);
+                       ast_config_destroy(cfg);
                        return NULL;
                }
                for (i = 0; i < numFields; i++)
@@ -457,13 +671,15 @@ static struct ast_config *realtime_multi_pgsql(const char *database, const char
 
                for (rowIndex = 0; rowIndex < num_rows; rowIndex++) {
                        var = NULL;
-                       if (!(cat = ast_category_new("","",99999)))
+                       cat = ast_category_new_anonymous();
+                       if (!cat) {
                                continue;
+                       }
                        for (i = 0; i < numFields; i++) {
                                stringp = PQgetvalue(result, rowIndex, i);
                                while (stringp) {
                                        chunk = strsep(&stringp, ";");
-                                       if (!ast_strlen_zero(ast_strip(chunk))) {
+                                       if (chunk && !ast_strlen_zero(ast_realtime_decode_chunk(ast_strip(chunk)))) {
                                                if (initfield && !strcmp(initfield, fieldnames[i])) {
                                                        ast_category_rename(cat, chunk);
                                                }
@@ -476,137 +692,123 @@ static struct ast_config *realtime_multi_pgsql(const char *database, const char
                }
                ast_free(fieldnames);
        } else {
-               ast_log(LOG_WARNING,
-                               "PostgreSQL RealTime: Could not find any rows in table %s.\n", table);
+               ast_debug(1, "PostgreSQL RealTime: Could not find any rows in table %s.\n", table);
        }
 
        ast_mutex_unlock(&pgsql_lock);
-       PQclear(result);
 
        return cfg;
 }
 
 static int update_pgsql(const char *database, const char *tablename, const char *keyfield,
-                                               const char *lookup, va_list ap)
+                                               const char *lookup, const struct ast_variable *fields)
 {
-       PGresult *result = NULL;
-       int numrows = 0, pgerror;
-       char escapebuf[513];
-       const char *newparam, *newval;
-       struct ast_str *sql = ast_str_create(100);
+       RAII_VAR(PGresult *, result, NULL, PQclear);
+       int numrows = 0, pgresult;
+       const struct ast_variable *field = fields;
+       struct ast_str *sql = ast_str_thread_get(&sql_buf, 100);
+       struct ast_str *escapebuf = ast_str_thread_get(&escapebuf_buf, 100);
        struct tables *table;
        struct columns *column = NULL;
 
+       /*
+        * Ignore database from the extconfig.conf since it was
+        * configured by res_pgsql.conf.
+        */
+       database = dbname;
+
        if (!tablename) {
                ast_log(LOG_WARNING, "PostgreSQL RealTime: No table specified.\n");
-               ast_free(sql);
                return -1;
        }
 
-       if (!(table = find_table(tablename))) {
+       if (!(table = find_table(database, tablename))) {
                ast_log(LOG_ERROR, "Table '%s' does not exist!!\n", tablename);
-               ast_free(sql);
+               return -1;
+       }
+
+       /*
+        * Must connect to the server before anything else as ESCAPE_STRING()
+        * uses pgsqlConn
+        */
+       ast_mutex_lock(&pgsql_lock);
+       if (!pgsql_reconnect(database)) {
+               ast_mutex_unlock(&pgsql_lock);
+               release_table(table);
                return -1;
        }
 
        /* Get the first parameter and first value in our list of passed paramater/value pairs */
-       newparam = va_arg(ap, const char *);
-       newval = va_arg(ap, const char *);
-       if (!newparam || !newval) {
+       if (!field) {
                ast_log(LOG_WARNING,
                                "PostgreSQL RealTime: Realtime retrieval requires at least 1 parameter and 1 value to search on.\n");
                if (pgsqlConn) {
                        PQfinish(pgsqlConn);
                        pgsqlConn = NULL;
-               };
-               ast_mutex_unlock(&table->lock);
-               ast_free(sql);
+               }
+               ast_mutex_unlock(&pgsql_lock);
+               release_table(table);
                return -1;
        }
 
        /* Check that the column exists in the table */
        AST_LIST_TRAVERSE(&table->columns, column, list) {
-               if (strcmp(column->name, newparam) == 0) {
+               if (strcmp(column->name, field->name) == 0) {
                        break;
                }
        }
 
        if (!column) {
-               ast_log(LOG_ERROR, "PostgreSQL RealTime: Updating on column '%s', but that column does not exist within the table '%s'!\n", newparam, tablename);
-               ast_mutex_unlock(&table->lock);
-               ast_free(sql);
+               ast_log(LOG_ERROR, "PostgreSQL RealTime: Updating on column '%s', but that column does not exist within the table '%s'!\n", field->name, tablename);
+               ast_mutex_unlock(&pgsql_lock);
+               release_table(table);
                return -1;
        }
 
        /* Create the first part of the query using the first parameter/value pairs we just extracted
           If there is only 1 set, then we have our query. Otherwise, loop thru the list and concat */
 
-       PQescapeStringConn(pgsqlConn, escapebuf, newval, (sizeof(escapebuf) - 1) / 2, &pgerror);
-       if (pgerror) {
-               ast_log(LOG_ERROR, "Postgres detected invalid input: '%s'\n", newval);
-               va_end(ap);
-               ast_mutex_unlock(&table->lock);
-               ast_free(sql);
+       ESCAPE_STRING(escapebuf, field->value);
+       if (pgresult) {
+               ast_log(LOG_ERROR, "PostgreSQL RealTime: detected invalid input: '%s'\n", field->value);
+               ast_mutex_unlock(&pgsql_lock);
+               release_table(table);
                return -1;
        }
-       ast_str_set(&sql, 0, "UPDATE %s SET %s = '%s'", tablename, newparam, escapebuf);
-
-       while ((newparam = va_arg(ap, const char *))) {
-               newval = va_arg(ap, const char *);
+       ast_str_set(&sql, 0, "UPDATE %s SET %s = '%s'", tablename, field->name, ast_str_buffer(escapebuf));
 
-               /* If the column is not within the table, then skip it */
-               AST_LIST_TRAVERSE(&table->columns, column, list) {
-                       if (strcmp(column->name, newparam) == 0) {
-                               break;
-                       }
-               }
-
-               if (!column) {
-                       ast_log(LOG_WARNING, "Attempted to update column '%s' in table '%s', but column does not exist!\n", newparam, tablename);
+       while ((field = field->next)) {
+               if (!find_column(table, field->name)) {
+                       ast_log(LOG_NOTICE, "Attempted to update column '%s' in table '%s', but column does not exist!\n", field->name, tablename);
                        continue;
                }
 
-               PQescapeStringConn(pgsqlConn, escapebuf, newval, (sizeof(escapebuf) - 1) / 2, &pgerror);
-               if (pgerror) {
-                       ast_log(LOG_ERROR, "Postgres detected invalid input: '%s'\n", newval);
-                       va_end(ap);
-                       ast_mutex_unlock(&table->lock);
-                       ast_free(sql);
+               ESCAPE_STRING(escapebuf, field->value);
+               if (pgresult) {
+                       ast_log(LOG_ERROR, "PostgreSQL RealTime: detected invalid input: '%s'\n", field->value);
+                       ast_mutex_unlock(&pgsql_lock);
+                       release_table(table);
                        return -1;
                }
 
-               ast_str_append(&sql, 0, ", %s = '%s'", newparam, escapebuf);
+               ast_str_append(&sql, 0, ", %s = '%s'", field->name, ast_str_buffer(escapebuf));
        }
-       va_end(ap);
-       ast_mutex_unlock(&table->lock);
+       release_table(table);
 
-       PQescapeStringConn(pgsqlConn, escapebuf, lookup, (sizeof(escapebuf) - 1) / 2, &pgerror);
-       if (pgerror) {
-               ast_log(LOG_ERROR, "Postgres detected invalid input: '%s'\n", lookup);
-               va_end(ap);
-               ast_free(sql);
+       ESCAPE_STRING(escapebuf, lookup);
+       if (pgresult) {
+               ast_log(LOG_ERROR, "PostgreSQL RealTime: detected invalid input: '%s'\n", lookup);
+               ast_mutex_unlock(&pgsql_lock);
                return -1;
        }
 
-       ast_str_append(&sql, 0, " WHERE %s = '%s'", keyfield, escapebuf);
+       ast_str_append(&sql, 0, " WHERE %s = '%s'", keyfield, ast_str_buffer(escapebuf));
 
-       ast_debug(1, "PostgreSQL RealTime: Update SQL: %s\n", sql->str);
+       ast_debug(1, "PostgreSQL RealTime: Update SQL: %s\n", ast_str_buffer(sql));
 
        /* We now have our complete statement; Lets connect to the server and execute it. */
-       ast_mutex_lock(&pgsql_lock);
-       if (!pgsql_reconnect(database)) {
-               ast_mutex_unlock(&pgsql_lock);
-               ast_free(sql);
-               return -1;
-       }
-
-       if (!(result = PQexec(pgsqlConn, sql->str))) {
-               ast_log(LOG_WARNING,
-                               "PostgreSQL RealTime: Failed to query database. Check debug for more info.\n");
-               ast_debug(1, "PostgreSQL RealTime: Query: %s\n", sql->str);
-               ast_debug(1, "PostgreSQL RealTime: Query Failed because: %s\n", PQerrorMessage(pgsqlConn));
+       if (pgsql_exec(database, tablename, ast_str_buffer(sql), &result) != 0) {
                ast_mutex_unlock(&pgsql_lock);
-               ast_free(sql);
                return -1;
        } else {
                ExecStatusType result_status = PQresultStatus(result);
@@ -615,18 +817,16 @@ static int update_pgsql(const char *database, const char *tablename, const char
                        && result_status != PGRES_NONFATAL_ERROR) {
                        ast_log(LOG_WARNING,
                                        "PostgreSQL RealTime: Failed to query database. Check debug for more info.\n");
-                       ast_debug(1, "PostgreSQL RealTime: Query: %s\n", sql->str);
+                       ast_debug(1, "PostgreSQL RealTime: Query: %s\n", ast_str_buffer(sql));
                        ast_debug(1, "PostgreSQL RealTime: Query Failed because: %s (%s)\n",
                                                PQresultErrorMessage(result), PQresStatus(result_status));
                        ast_mutex_unlock(&pgsql_lock);
-                       ast_free(sql);
                        return -1;
                }
        }
 
        numrows = atoi(PQcmdTuples(result));
        ast_mutex_unlock(&pgsql_lock);
-       ast_free(sql);
 
        ast_debug(1, "PostgreSQL RealTime: Updated %d rows on table: %s\n", numrows, tablename);
 
@@ -642,86 +842,118 @@ static int update_pgsql(const char *database, const char *tablename, const char
        return -1;
 }
 
-static int store_pgsql(const char *database, const char *table, va_list ap)
+static int update2_pgsql(const char *database, const char *tablename, const struct ast_variable *lookup_fields, const struct ast_variable *update_fields)
 {
-       PGresult *result = NULL;
-       Oid insertid;
-       char sql[256];
-       char params[256];
-       char vals[256];
-       char buf[256];
-       int pgresult;
-       const char *newparam, *newval;
+       RAII_VAR(PGresult *, result, NULL, PQclear);
+       int numrows = 0, pgresult, first = 1;
+       struct ast_str *escapebuf = ast_str_thread_get(&escapebuf_buf, 16);
+       const struct ast_variable *field;
+       struct ast_str *sql = ast_str_thread_get(&sql_buf, 100);
+       struct ast_str *where = ast_str_thread_get(&where_buf, 100);
+       struct tables *table;
 
-       if (!table) {
+       /*
+        * Ignore database from the extconfig.conf since it was
+        * configured by res_pgsql.conf.
+        */
+       database = dbname;
+
+       if (!tablename) {
                ast_log(LOG_WARNING, "PostgreSQL RealTime: No table specified.\n");
                return -1;
        }
 
-       /* Get the first parameter and first value in our list of passed paramater/value pairs */
-       newparam = va_arg(ap, const char *);
-       newval = va_arg(ap, const char *);
-       if (!newparam || !newval) {
-               ast_log(LOG_WARNING,
-                               "PostgreSQL RealTime: Realtime storage requires at least 1 parameter and 1 value to store.\n");
-               if (pgsqlConn) {
-                       PQfinish(pgsqlConn);
-                       pgsqlConn = NULL;
-               };
+       if (!escapebuf || !sql || !where) {
+               /* Memory error, already handled */
+               return -1;
+       }
+
+       if (!(table = find_table(database, tablename))) {
+               ast_log(LOG_ERROR, "Table '%s' does not exist!!\n", tablename);
                return -1;
        }
 
-       /* Must connect to the server before anything else, as the escape function requires the connection handle.. */
+       /*
+        * Must connect to the server before anything else as ESCAPE_STRING()
+        * uses pgsqlConn
+        */
        ast_mutex_lock(&pgsql_lock);
        if (!pgsql_reconnect(database)) {
                ast_mutex_unlock(&pgsql_lock);
+               release_table(table);
                return -1;
        }
 
-       /* Create the first part of the query using the first parameter/value pairs we just extracted
-          If there is only 1 set, then we have our query. Otherwise, loop thru the list and concat */
-       PQescapeStringConn(pgsqlConn, buf, newparam, sizeof(newparam), &pgresult);
-       snprintf(params, sizeof(params), "%s", buf);
-       PQescapeStringConn(pgsqlConn, buf, newval, sizeof(newval), &pgresult);
-       snprintf(vals, sizeof(vals), "'%s'", buf);
-       while ((newparam = va_arg(ap, const char *))) {
-               newval = va_arg(ap, const char *);
-               PQescapeStringConn(pgsqlConn, buf, newparam, sizeof(newparam), &pgresult);
-               snprintf(params + strlen(params), sizeof(params) - strlen(params), ", %s", buf);
-               PQescapeStringConn(pgsqlConn, buf, newval, sizeof(newval), &pgresult);
-               snprintf(vals + strlen(vals), sizeof(vals) - strlen(vals), ", '%s'", buf);
-       }
-       va_end(ap);
-       snprintf(sql, sizeof(sql), "INSERT INTO (%s) VALUES (%s)", params, vals);
-
-       ast_debug(1, "PostgreSQL RealTime: Insert SQL: %s\n", sql);
-
-       if (!(result = PQexec(pgsqlConn, sql))) {
+       ast_str_set(&sql, 0, "UPDATE %s SET", tablename);
+       ast_str_set(&where, 0, " WHERE");
+
+       for (field = lookup_fields; field; field = field->next) {
+               if (!find_column(table, field->name)) {
+                       ast_log(LOG_ERROR, "Attempted to update based on criteria column '%s' (%s@%s), but that column does not exist!\n", field->name, tablename, database);
+                       ast_mutex_unlock(&pgsql_lock);
+                       release_table(table);
+                       return -1;
+               }
+
+               ESCAPE_STRING(escapebuf, field->value);
+               if (pgresult) {
+                       ast_log(LOG_ERROR, "PostgreSQL RealTime: detected invalid input: '%s'\n", field->value);
+                       ast_mutex_unlock(&pgsql_lock);
+                       release_table(table);
+                       return -1;
+               }
+               ast_str_append(&where, 0, "%s %s='%s'", first ? "" : " AND", field->name, ast_str_buffer(escapebuf));
+               first = 0;
+       }
+
+       if (first) {
                ast_log(LOG_WARNING,
-                               "PostgreSQL RealTime: Failed to query database. Check debug for more info.\n");
-               ast_debug(1, "PostgreSQL RealTime: Query: %s\n", sql);
-               ast_debug(1, "PostgreSQL RealTime: Query Failed because: %s\n", PQerrorMessage(pgsqlConn));
+                               "PostgreSQL RealTime: Realtime update requires at least 1 parameter and 1 value to search on.\n");
+               if (pgsqlConn) {
+                       PQfinish(pgsqlConn);
+                       pgsqlConn = NULL;
+               }
                ast_mutex_unlock(&pgsql_lock);
+               release_table(table);
                return -1;
-       } else {
-               ExecStatusType result_status = PQresultStatus(result);
-               if (result_status != PGRES_COMMAND_OK
-                       && result_status != PGRES_TUPLES_OK
-                       && result_status != PGRES_NONFATAL_ERROR) {
-                       ast_log(LOG_WARNING,
-                                       "PostgreSQL RealTime: Failed to query database. Check debug for more info.\n");
-                       ast_debug(1, "PostgreSQL RealTime: Query: %s\n", sql);
-                       ast_debug(1, "PostgreSQL RealTime: Query Failed because: %s (%s)\n",
-                                               PQresultErrorMessage(result), PQresStatus(result_status));
+       }
+
+       /* Now retrieve the columns to update */
+       first = 1;
+       for (field = update_fields; field; field = field->next) {
+               /* If the column is not within the table, then skip it */
+               if (!find_column(table, field->name)) {
+                       ast_log(LOG_NOTICE, "Attempted to update column '%s' in table '%s@%s', but column does not exist!\n", field->name, tablename, database);
+                       continue;
+               }
+
+               ESCAPE_STRING(escapebuf, field->value);
+               if (pgresult) {
+                       ast_log(LOG_ERROR, "PostgreSQL RealTime: detected invalid input: '%s'\n", field->value);
                        ast_mutex_unlock(&pgsql_lock);
+                       release_table(table);
                        return -1;
                }
+
+               ast_str_append(&sql, 0, "%s %s='%s'", first ? "" : ",", field->name, ast_str_buffer(escapebuf));
+               first = 0;
        }
+       release_table(table);
+
+       ast_str_append(&sql, 0, "%s", ast_str_buffer(where));
+
+       ast_debug(1, "PostgreSQL RealTime: Update SQL: %s\n", ast_str_buffer(sql));
+
+       /* We now have our complete statement; Lets connect to the server and execute it. */
+        if (pgsql_exec(database, tablename, ast_str_buffer(sql), &result) != 0) {
+               ast_mutex_unlock(&pgsql_lock);
+               return -1;
+        }
 
-       insertid = PQoidValue(result);
+       numrows = atoi(PQcmdTuples(result));
        ast_mutex_unlock(&pgsql_lock);
 
-       ast_debug(1, "PostgreSQL RealTime: row inserted on table: %s, id: %u\n", table, insertid);
+       ast_debug(1, "PostgreSQL RealTime: Updated %d rows on table: %s\n", numrows, tablename);
 
        /* From http://dev.pgsql.com/doc/pgsql/en/pgsql-affected-rows.html
         * An integer greater than zero indicates the number of rows affected
@@ -729,84 +961,156 @@ static int store_pgsql(const char *database, const char *table, va_list ap)
         * -1 indicates that the query returned an error (although, if the query failed, it should have been caught above.)
         */
 
-       if (insertid >= 0)
-               return (int) insertid;
+       if (numrows >= 0) {
+               return (int) numrows;
+       }
 
        return -1;
 }
 
-static int destroy_pgsql(const char *database, const char *table, const char *keyfield, const char *lookup, va_list ap)
+static int store_pgsql(const char *database, const char *table, const struct ast_variable *fields)
 {
-       PGresult *result = NULL;
-       int numrows = 0;
+       RAII_VAR(PGresult *, result, NULL, PQclear);
+       int numrows;
+       struct ast_str *buf = ast_str_thread_get(&escapebuf_buf, 256);
+       struct ast_str *sql1 = ast_str_thread_get(&sql_buf, 256);
+       struct ast_str *sql2 = ast_str_thread_get(&where_buf, 256);
        int pgresult;
-       char sql[256];
-       char buf[256], buf2[256];
-       const char *newparam, *newval;
+       const struct ast_variable *field = fields;
+
+       /*
+        * Ignore database from the extconfig.conf since it was
+        * configured by res_pgsql.conf.
+        */
+       database = dbname;
 
        if (!table) {
                ast_log(LOG_WARNING, "PostgreSQL RealTime: No table specified.\n");
                return -1;
        }
 
+       /*
+        * Must connect to the server before anything else as ESCAPE_STRING()
+        * uses pgsqlConn
+        */
+       ast_mutex_lock(&pgsql_lock);
+       if (!pgsql_reconnect(database)) {
+               ast_mutex_unlock(&pgsql_lock);
+               return -1;
+       }
+
        /* Get the first parameter and first value in our list of passed paramater/value pairs */
-       /*newparam = va_arg(ap, const char *);
-       newval = va_arg(ap, const char *);
-       if (!newparam || !newval) {*/
-       if (ast_strlen_zero(keyfield) || ast_strlen_zero(lookup))  {
+       if (!field) {
                ast_log(LOG_WARNING,
-                               "PostgreSQL RealTime: Realtime destroy requires at least 1 parameter and 1 value to search on.\n");
+                               "PostgreSQL RealTime: Realtime storage requires at least 1 parameter and 1 value to store.\n");
                if (pgsqlConn) {
                        PQfinish(pgsqlConn);
                        pgsqlConn = NULL;
-               };
+               }
+               ast_mutex_unlock(&pgsql_lock);
+               return -1;
+       }
+
+       /* Create the first part of the query using the first parameter/value pairs we just extracted
+          If there is only 1 set, then we have our query. Otherwise, loop thru the list and concat */
+       ESCAPE_STRING(buf, field->name);
+       ast_str_set(&sql1, 0, "INSERT INTO %s (%s", table, ast_str_buffer(buf));
+       ESCAPE_STRING(buf, field->value);
+       ast_str_set(&sql2, 0, ") VALUES ('%s'", ast_str_buffer(buf));
+       while ((field = field->next)) {
+               ESCAPE_STRING(buf, field->name);
+               ast_str_append(&sql1, 0, ", %s", ast_str_buffer(buf));
+               ESCAPE_STRING(buf, field->value);
+               ast_str_append(&sql2, 0, ", '%s'", ast_str_buffer(buf));
+       }
+       ast_str_append(&sql1, 0, "%s)", ast_str_buffer(sql2));
+
+       ast_debug(1, "PostgreSQL RealTime: Insert SQL: %s\n", ast_str_buffer(sql1));
+
+       /* We now have our complete statement; Lets connect to the server and execute it. */
+        if (pgsql_exec(database, table, ast_str_buffer(sql1), &result) != 0) {
+               ast_mutex_unlock(&pgsql_lock);
+               return -1;
+        }
+
+       numrows = atoi(PQcmdTuples(result));
+       ast_mutex_unlock(&pgsql_lock);
+
+       ast_debug(1, "PostgreSQL RealTime: row inserted on table: %s.\n", table);
+
+       /* From http://dev.pgsql.com/doc/pgsql/en/pgsql-affected-rows.html
+        * An integer greater than zero indicates the number of rows affected
+        * Zero indicates that no records were updated
+        * -1 indicates that the query returned an error (although, if the query failed, it should have been caught above.)
+        */
+
+       if (numrows >= 0) {
+               return numrows;
+       }
+
+       return -1;
+}
+
+static int destroy_pgsql(const char *database, const char *table, const char *keyfield, const char *lookup, const struct ast_variable *fields)
+{
+       RAII_VAR(PGresult *, result, NULL, PQclear);
+       int numrows = 0;
+       int pgresult;
+       struct ast_str *sql = ast_str_thread_get(&sql_buf, 256);
+       struct ast_str *buf1 = ast_str_thread_get(&where_buf, 60), *buf2 = ast_str_thread_get(&escapebuf_buf, 60);
+       const struct ast_variable *field;
+
+       /*
+        * Ignore database from the extconfig.conf since it was
+        * configured by res_pgsql.conf.
+        */
+       database = dbname;
+
+       if (!table) {
+               ast_log(LOG_WARNING, "PostgreSQL RealTime: No table specified.\n");
                return -1;
        }
 
-       /* Must connect to the server before anything else, as the escape function requires the connection handle.. */
+       /*
+        * Must connect to the server before anything else as ESCAPE_STRING()
+        * uses pgsqlConn
+        */
        ast_mutex_lock(&pgsql_lock);
        if (!pgsql_reconnect(database)) {
                ast_mutex_unlock(&pgsql_lock);
                return -1;
        }
 
+       /* Get the first parameter and first value in our list of passed paramater/value pairs */
+       if (ast_strlen_zero(keyfield) || ast_strlen_zero(lookup))  {
+               ast_log(LOG_WARNING,
+                               "PostgreSQL RealTime: Realtime destroy requires at least 1 parameter and 1 value to search on.\n");
+               if (pgsqlConn) {
+                       PQfinish(pgsqlConn);
+                       pgsqlConn = NULL;
+               }
+               ast_mutex_unlock(&pgsql_lock);
+               return -1;
+       }
 
        /* Create the first part of the query using the first parameter/value pairs we just extracted
           If there is only 1 set, then we have our query. Otherwise, loop thru the list and concat */
 
-       PQescapeStringConn(pgsqlConn, buf, keyfield, sizeof(keyfield), &pgresult);
-       PQescapeStringConn(pgsqlConn, buf2, lookup, sizeof(lookup), &pgresult);
-       snprintf(sql, sizeof(sql), "DELETE FROM %s WHERE %s = '%s'", table, buf, buf2);
-       while ((newparam = va_arg(ap, const char *))) {
-               newval = va_arg(ap, const char *);
-               PQescapeStringConn(pgsqlConn, buf, newparam, sizeof(newparam), &pgresult);
-               PQescapeStringConn(pgsqlConn, buf2, newval, sizeof(newval), &pgresult);
-               snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " AND %s = '%s'", buf, buf2);
+       ESCAPE_STRING(buf1, keyfield);
+       ESCAPE_STRING(buf2, lookup);
+       ast_str_set(&sql, 0, "DELETE FROM %s WHERE %s = '%s'", table, ast_str_buffer(buf1), ast_str_buffer(buf2));
+       for (field = fields; field; field = field->next) {
+               ESCAPE_STRING(buf1, field->name);
+               ESCAPE_STRING(buf2, field->value);
+               ast_str_append(&sql, 0, " AND %s = '%s'", ast_str_buffer(buf1), ast_str_buffer(buf2));
        }
-       va_end(ap);
 
-       ast_debug(1, "PostgreSQL RealTime: Delete SQL: %s\n", sql);
+       ast_debug(1, "PostgreSQL RealTime: Delete SQL: %s\n", ast_str_buffer(sql));
 
-       if (!(result = PQexec(pgsqlConn, sql))) {
-               ast_log(LOG_WARNING,
-                               "PostgreSQL RealTime: Failed to query database. Check debug for more info.\n");
-               ast_debug(1, "PostgreSQL RealTime: Query: %s\n", sql);
-               ast_debug(1, "PostgreSQL RealTime: Query Failed because: %s\n", PQerrorMessage(pgsqlConn));
+       /* We now have our complete statement; Lets connect to the server and execute it. */
+       if (pgsql_exec(database, table, ast_str_buffer(sql), &result) != 0) {
                ast_mutex_unlock(&pgsql_lock);
                return -1;
-       } else {
-               ExecStatusType result_status = PQresultStatus(result);
-               if (result_status != PGRES_COMMAND_OK
-                       && result_status != PGRES_TUPLES_OK
-                       && result_status != PGRES_NONFATAL_ERROR) {
-                       ast_log(LOG_WARNING,
-                                       "PostgreSQL RealTime: Failed to query database. Check debug for more info.\n");
-                       ast_debug(1, "PostgreSQL RealTime: Query: %s\n", sql);
-                       ast_debug(1, "PostgreSQL RealTime: Query Failed because: %s (%s)\n",
-                                               PQresultErrorMessage(result), PQresStatus(result_status));
-                       ast_mutex_unlock(&pgsql_lock);
-                       return -1;
-               }
        }
 
        numrows = atoi(PQcmdTuples(result));
@@ -831,57 +1135,40 @@ static struct ast_config *config_pgsql(const char *database, const char *table,
                                                                           const char *file, struct ast_config *cfg,
                                                                           struct ast_flags flags, const char *suggested_incl, const char *who_asked)
 {
-       PGresult *result = NULL;
+       RAII_VAR(PGresult *, result, NULL, PQclear);
        long num_rows;
        struct ast_variable *new_v;
        struct ast_category *cur_cat = NULL;
-       char sqlbuf[1024] = "";
-       char *sql = sqlbuf;
-       size_t sqlleft = sizeof(sqlbuf);
-       char last[80] = "";
+       struct ast_str *sql = ast_str_thread_get(&sql_buf, 100);
+       char last[80];
        int last_cat_metric = 0;
 
        last[0] = '\0';
 
+       /*
+        * Ignore database from the extconfig.conf since it is
+        * configured by res_pgsql.conf.
+        */
+       database = dbname;
+
        if (!file || !strcmp(file, RES_CONFIG_PGSQL_CONF)) {
                ast_log(LOG_WARNING, "PostgreSQL RealTime: Cannot configure myself.\n");
                return NULL;
        }
 
-       ast_build_string(&sql, &sqlleft, "SELECT category, var_name, var_val, cat_metric FROM %s ", table);
-       ast_build_string(&sql, &sqlleft, "WHERE filename='%s' and commented=0", file);
-       ast_build_string(&sql, &sqlleft, "ORDER BY cat_metric DESC, var_metric ASC, category, var_name ");
+       ast_str_set(&sql, 0, "SELECT category, var_name, var_val, cat_metric FROM %s "
+                       "WHERE filename='%s' and commented=0 "
+                       "ORDER BY cat_metric DESC, var_metric ASC, category, var_name ", table, file);
 
-       ast_debug(1, "PostgreSQL RealTime: Static SQL: %s\n", sqlbuf);
+       ast_debug(1, "PostgreSQL RealTime: Static SQL: %s\n", ast_str_buffer(sql));
 
-       /* We now have our complete statement; Lets connect to the server and execute it. */
        ast_mutex_lock(&pgsql_lock);
-       if (!pgsql_reconnect(database)) {
-               ast_mutex_unlock(&pgsql_lock);
-               return NULL;
-       }
 
-       if (!(result = PQexec(pgsqlConn, sqlbuf))) {
-               ast_log(LOG_WARNING,
-                               "PostgreSQL RealTime: Failed to query database. Check debug for more info.\n");
-               ast_debug(1, "PostgreSQL RealTime: Query: %s\n", sql);
-               ast_debug(1, "PostgreSQL RealTime: Query Failed because: %s\n", PQerrorMessage(pgsqlConn));
+       /* We now have our complete statement; Lets connect to the server and execute it. */
+        if (pgsql_exec(database, table, ast_str_buffer(sql), &result) != 0) {
                ast_mutex_unlock(&pgsql_lock);
-               return NULL;
-       } else {
-               ExecStatusType result_status = PQresultStatus(result);
-               if (result_status != PGRES_COMMAND_OK
-                       && result_status != PGRES_TUPLES_OK
-                       && result_status != PGRES_NONFATAL_ERROR) {
-                       ast_log(LOG_WARNING,
-                                       "PostgreSQL RealTime: Failed to query database. Check debug for more info.\n");
-                       ast_debug(1, "PostgreSQL RealTime: Query: %s\n", sql);
-                       ast_debug(1, "PostgreSQL RealTime: Query Failed because: %s (%s)\n",
-                                               PQresultErrorMessage(result), PQresStatus(result_status));
-                       ast_mutex_unlock(&pgsql_lock);
-                       return NULL;
-               }
-       }
+               return NULL;
+        }
 
        if ((num_rows = PQntuples(result)) > 0) {
                int rowIndex = 0;
@@ -895,7 +1182,6 @@ static struct ast_config *config_pgsql(const char *database, const char *table,
                        char *field_cat_metric = PQgetvalue(result, rowIndex, 3);
                        if (!strcmp(field_var_name, "#include")) {
                                if (!ast_config_internal_load(field_var_val, cfg, flags, "", who_asked)) {
-                                       PQclear(result);
                                        ast_mutex_unlock(&pgsql_lock);
                                        return NULL;
                                }
@@ -903,10 +1189,11 @@ static struct ast_config *config_pgsql(const char *database, const char *table,
                        }
 
                        if (strcmp(last, field_category) || last_cat_metric != atoi(field_cat_metric)) {
-                               cur_cat = ast_category_new(field_category, "", 99999);
-                               if (!cur_cat)
+                               cur_cat = ast_category_new_dynamic(field_category);
+                               if (!cur_cat) {
                                        break;
-                               strcpy(last, field_category);
+                               }
+                               ast_copy_string(last, field_category, sizeof(last));
                                last_cat_metric = atoi(field_cat_metric);
                                ast_category_append(cfg, cur_cat);
                        }
@@ -918,7 +1205,6 @@ static struct ast_config *config_pgsql(const char *database, const char *table,
                                "PostgreSQL RealTime: Could not find config '%s' in database.\n", file);
        }
 
-       PQclear(result);
        ast_mutex_unlock(&pgsql_lock);
 
        return cfg;
@@ -927,10 +1213,17 @@ static struct ast_config *config_pgsql(const char *database, const char *table,
 static int require_pgsql(const char *database, const char *tablename, va_list ap)
 {
        struct columns *column;
-       struct tables *table = find_table(tablename);
+       struct tables *table;
        char *elm;
        int type, size, res = 0;
 
+       /*
+        * Ignore database from the extconfig.conf since it was
+        * configured by res_pgsql.conf.
+        */
+       database = dbname;
+
+       table = find_table(database, tablename);
        if (!table) {
                ast_log(LOG_WARNING, "Table %s not found in database.  This table should exist if you're using realtime.\n", tablename);
                return -1;
@@ -971,9 +1264,16 @@ static int require_pgsql(const char *database, const char *tablename, va_list ap
                                                        size, column->type);
                                                res = -1;
                                        }
-                               } else if (strncmp(column->type, "float", 5) == 0 && !ast_rq_is_int(type) && type != RQ_FLOAT) {
-                                       ast_log(LOG_WARNING, "Column %s cannot be a %s\n", column->name, column->type);
-                                       res = -1;
+                               } else if (strncmp(column->type, "float", 5) == 0) {
+                                       if (!ast_rq_is_int(type) && type != RQ_FLOAT) {
+                                               ast_log(LOG_WARNING, "Column %s cannot be a %s\n", column->name, column->type);
+                                               res = -1;
+                                       }
+                               } else if (strncmp(column->type, "timestamp", 9) == 0) {
+                                       if (type != RQ_DATETIME && type != RQ_DATE) {
+                                               ast_log(LOG_WARNING, "Column %s cannot be a %s\n", column->name, column->type);
+                                               res = -1;
+                                       }
                                } else { /* There are other types that no module implements yet */
                                        ast_log(LOG_WARNING, "Possibly unsupported column type '%s' on column '%s'\n", column->type, column->name);
                                        res = -1;
@@ -985,6 +1285,7 @@ static int require_pgsql(const char *database, const char *tablename, va_list ap
                if (!column) {
                        if (requirements == RQ_WARN) {
                                ast_log(LOG_WARNING, "Table %s requires a column '%s' of size '%d', but no such column exists.\n", tablename, elm, size);
+                               res = -1;
                        } else {
                                struct ast_str *sql = ast_str_create(100);
                                char fieldtype[15];
@@ -994,7 +1295,7 @@ static int require_pgsql(const char *database, const char *tablename, va_list ap
                                        /* Size is minimum length; make it at least 50% greater,
                                         * just to be sure, because PostgreSQL doesn't support
                                         * resizing columns. */
-                                       snprintf(fieldtype, sizeof(fieldtype), "CHAR(%d)",
+                                       snprintf(fieldtype, sizeof(fieldtype), "CHAR(%hhu)",
                                                size < 15 ? size * 2 :
                                                (size * 3 / 2 > 255) ? 255 : size * 3 / 2);
                                } else if (type == RQ_INTEGER1 || type == RQ_UINTEGER1 || type == RQ_INTEGER2) {
@@ -1021,18 +1322,17 @@ static int require_pgsql(const char *database, const char *tablename, va_list ap
                                ast_debug(1, "About to lock pgsql_lock (running alter on table '%s' to add column '%s')\n", tablename, elm);
 
                                ast_mutex_lock(&pgsql_lock);
-                               if (!pgsql_reconnect(database)) {
+                               ast_debug(1, "About to run ALTER query on table '%s' to add column '%s'\n", tablename, elm);
+
+                               if (pgsql_exec(database, tablename, ast_str_buffer(sql), &result) != 0) {
                                        ast_mutex_unlock(&pgsql_lock);
-                                       ast_log(LOG_ERROR, "Unable to add column: %s\n", sql->str);
-                                       ast_free(sql);
-                                       continue;
-                               }
+                                       release_table(table);
+                                       return -1;
+                               }
 
-                               ast_debug(1, "About to run ALTER query on table '%s' to add column '%s'\n", tablename, elm);
-                               result = PQexec(pgsqlConn, sql->str);
                                ast_debug(1, "Finished running ALTER query on table '%s'\n", tablename);
                                if (PQresultStatus(result) != PGRES_COMMAND_OK) {
-                                       ast_log(LOG_ERROR, "Unable to add column: %s\n", sql->str);
+                                       ast_log(LOG_ERROR, "Unable to add column: %s\n", ast_str_buffer(sql));
                                }
                                PQclear(result);
                                ast_mutex_unlock(&pgsql_lock);
@@ -1041,13 +1341,20 @@ static int require_pgsql(const char *database, const char *tablename, va_list ap
                        }
                }
        }
-       ast_mutex_unlock(&table->lock);
+       release_table(table);
        return res;
 }
 
 static int unload_pgsql(const char *database, const char *tablename)
 {
        struct tables *cur;
+
+       /*
+        * Ignore database from the extconfig.conf since it was
+        * configured by res_pgsql.conf.
+        */
+       database = dbname;
+
        ast_debug(2, "About to lock table cache list\n");
        AST_LIST_LOCK(&psql_tables);
        ast_debug(2, "About to traverse table cache list\n");
@@ -1075,6 +1382,7 @@ static struct ast_config_engine pgsql_engine = {
        .store_func = store_pgsql,
        .destroy_func = destroy_pgsql,
        .update_func = update_pgsql,
+       .update2_func = update2_pgsql,
        .require_func = require_pgsql,
        .unload_func = unload_pgsql,
 };
@@ -1085,8 +1393,8 @@ static int load_module(void)
                return AST_MODULE_LOAD_DECLINE;
 
        ast_config_engine_register(&pgsql_engine);
-       ast_verb(1, "PostgreSQL RealTime driver loaded.\n");
-       ast_cli_register_multiple(cli_realtime, sizeof(cli_realtime) / sizeof(struct ast_cli_entry));
+
+       ast_cli_register_multiple(cli_realtime, ARRAY_LEN(cli_realtime));
 
        return 0;
 }
@@ -1101,9 +1409,11 @@ static int unload_module(void)
                PQfinish(pgsqlConn);
                pgsqlConn = NULL;
        }
-       ast_cli_unregister_multiple(cli_realtime, sizeof(cli_realtime) / sizeof(struct ast_cli_entry));
+       ast_cli_unregister_multiple(cli_realtime, ARRAY_LEN(cli_realtime));
        ast_config_engine_deregister(&pgsql_engine);
-       ast_verb(1, "PostgreSQL RealTime unloaded.\n");
+
+       /* Unlock so something else can destroy the lock. */
+       ast_mutex_unlock(&pgsql_lock);
 
        /* Destroy cached table info */
        AST_LIST_LOCK(&psql_tables);
@@ -1112,9 +1422,6 @@ static int unload_module(void)
        }
        AST_LIST_UNLOCK(&psql_tables);
 
-       /* Unlock so something else can destroy the lock. */
-       ast_mutex_unlock(&pgsql_lock);
-
        return 0;
 }
 
@@ -1131,16 +1438,22 @@ static int parse_config(int is_reload)
        const char *s;
        struct ast_flags config_flags = { is_reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
 
-       if ((config = ast_config_load(RES_CONFIG_PGSQL_CONF, config_flags)) == CONFIG_STATUS_FILEUNCHANGED)
+       config = ast_config_load(RES_CONFIG_PGSQL_CONF, config_flags);
+       if (config == CONFIG_STATUS_FILEUNCHANGED) {
+               if (is_reload && pgsqlConn && PQstatus(pgsqlConn) != CONNECTION_OK) {
+                       ast_log(LOG_WARNING,  "PostgreSQL RealTime: Not connected\n");
+               }
                return 0;
+       }
 
-       if (!config) {
+       if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEINVALID) {
                ast_log(LOG_WARNING, "Unable to load config %s\n", RES_CONFIG_PGSQL_CONF);
                return 0;
        }
 
        ast_mutex_lock(&pgsql_lock);
 
+       /* XXX: Why would we do this before we're ready to establish a new connection? */
        if (pgsqlConn) {
                PQfinish(pgsqlConn);
                pgsqlConn = NULL;
@@ -1186,12 +1499,18 @@ static int parse_config(int is_reload)
                dbport = atoi(s);
        }
 
+       if (!(s = ast_variable_retrieve(config, "general", "dbappname"))) {
+               dbappname[0] = '\0';
+       } else {
+               ast_copy_string(dbappname, s, sizeof(dbappname));
+       }
+
        if (!ast_strlen_zero(dbhost)) {
                /* No socket needed */
        } else if (!(s = ast_variable_retrieve(config, "general", "dbsock"))) {
                ast_log(LOG_WARNING,
-                               "PostgreSQL RealTime: No database socket found, using '/tmp/pgsql.sock' as default.\n");
-               strcpy(dbsock, "/tmp/pgsql.sock");
+                               "PostgreSQL RealTime: No database socket found, using '/tmp/.s.PGSQL.%d' as default.\n", dbport);
+               strcpy(dbsock, "/tmp");
        } else {
                ast_copy_string(dbsock, s, sizeof(dbsock));
        }
@@ -1242,35 +1561,50 @@ static int pgsql_reconnect(const char *database)
 
        /* mutex lock should have been locked before calling this function. */
 
-       if (pgsqlConn && PQstatus(pgsqlConn) != CONNECTION_OK) {
+       if (pgsqlConn) {
+               if (PQstatus(pgsqlConn) == CONNECTION_OK) {
+                       /* We're good? */
+                       return 1;
+               }
+
                PQfinish(pgsqlConn);
                pgsqlConn = NULL;
        }
 
        /* DB password can legitimately be 0-length */
-       if ((!pgsqlConn) && (!ast_strlen_zero(dbhost) || !ast_strlen_zero(dbsock)) && !ast_strlen_zero(dbuser) && !ast_strlen_zero(my_database)) {
-               struct ast_str *connInfo = ast_str_create(32);
+       if ((!ast_strlen_zero(dbhost) || !ast_strlen_zero(dbsock)) && !ast_strlen_zero(dbuser) && !ast_strlen_zero(my_database)) {
+               struct ast_str *conn_info = ast_str_create(128);
 
-               ast_str_set(&connInfo, 0, "host=%s port=%d dbname=%s user=%s",
-                       dbhost, dbport, my_database, dbuser);
-               if (!ast_strlen_zero(dbpass))
-                       ast_str_append(&connInfo, 0, " password=%s", dbpass);
+               if (!conn_info) {
+                       ast_log(LOG_ERROR, "PostgreSQL RealTime: Failed to allocate memory for connection string.\n");
+                       return 0;
+               }
+
+               ast_str_set(&conn_info, 0, "host=%s port=%d dbname=%s user=%s",
+                       S_OR(dbhost, dbsock), dbport, my_database, dbuser);
+
+               if (!ast_strlen_zero(dbappname)) {
+                       ast_str_append(&conn_info, 0, " application_name=%s", dbappname);
+               }
+
+               if (!ast_strlen_zero(dbpass)) {
+                       ast_str_append(&conn_info, 0, " password=%s", dbpass);
+               }
 
-               ast_debug(1, "%u connInfo=%s\n", (unsigned int)connInfo->len, connInfo->str);
-               pgsqlConn = PQconnectdb(connInfo->str);
-               ast_debug(1, "%u connInfo=%s\n", (unsigned int)connInfo->len, connInfo->str);
-               ast_free(connInfo);
-               connInfo = NULL;
+               pgsqlConn = PQconnectdb(ast_str_buffer(conn_info));
+               ast_free(conn_info);
+               conn_info = NULL;
 
                ast_debug(1, "pgsqlConn=%p\n", pgsqlConn);
                if (pgsqlConn && PQstatus(pgsqlConn) == CONNECTION_OK) {
                        ast_debug(1, "PostgreSQL RealTime: Successfully connected to database.\n");
                        connect_time = time(NULL);
+                       version = PQserverVersion(pgsqlConn);
                        return 1;
                } else {
                        ast_log(LOG_ERROR,
                                        "PostgreSQL RealTime: Failed to connect database %s on %s: %s\n",
-                                       dbname, dbhost, PQresultErrorMessage(NULL));
+                                       my_database, dbhost, PQresultErrorMessage(NULL));
                        return 0;
                }
        } else {
@@ -1287,13 +1621,13 @@ static char *handle_cli_realtime_pgsql_cache(struct ast_cli_entry *e, int cmd, s
 
        switch (cmd) {
        case CLI_INIT:
-               e->command = "realtime pgsql cache";
+               e->command = "realtime show pgsql cache";
                e->usage =
-                       "Usage: realtime pgsql cache [<table>]\n"
+                       "Usage: realtime show pgsql cache [<table>]\n"
                        "       Shows table cache for the PostgreSQL RealTime driver\n";
                return NULL;
        case CLI_GENERATE:
-               if (a->argc != 3) {
+               if (a->argc != 4) {
                        return NULL;
                }
                l = strlen(a->word);
@@ -1309,25 +1643,25 @@ static char *handle_cli_realtime_pgsql_cache(struct ast_cli_entry *e, int cmd, s
                return ret;
        }
 
-       if (a->argc == 3) {
+       if (a->argc == 4) {
                /* List of tables */
                AST_LIST_LOCK(&psql_tables);
                AST_LIST_TRAVERSE(&psql_tables, cur, list) {
                        ast_cli(a->fd, "%s\n", cur->name);
                }
                AST_LIST_UNLOCK(&psql_tables);
-       } else if (a->argc == 4) {
+       } else if (a->argc == 5) {
                /* List of columns */
-               if ((cur = find_table(a->argv[3]))) {
+               if ((cur = find_table(NULL, a->argv[4]))) {
                        struct columns *col;
-                       ast_cli(a->fd, "Columns for Table Cache '%s':\n", a->argv[3]);
+                       ast_cli(a->fd, "Columns for Table Cache '%s':\n", a->argv[4]);
                        ast_cli(a->fd, "%-20.20s %-20.20s %-3.3s %-8.8s\n", "Name", "Type", "Len", "Nullable");
                        AST_LIST_TRAVERSE(&cur->columns, col, list) {
                                ast_cli(a->fd, "%-20.20s %-20.20s %3d %-8.8s\n", col->name, col->type, col->len, col->notnull ? "NOT NULL" : "");
                        }
-                       ast_mutex_unlock(&cur->lock);
+                       release_table(cur);
                } else {
-                       ast_cli(a->fd, "No such table '%s'\n", a->argv[3]);
+                       ast_cli(a->fd, "No such table '%s'\n", a->argv[4]);
                }
        }
        return 0;
@@ -1335,60 +1669,54 @@ static char *handle_cli_realtime_pgsql_cache(struct ast_cli_entry *e, int cmd, s
 
 static char *handle_cli_realtime_pgsql_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
-       char status[256], credentials[100] = "";
-       int ctimesec = time(NULL) - connect_time;
+       char connection_info[256];
+       char credentials[100] = "";
+       char buf[376]; /* 256+100+"Connected to "+" for "+NULL */
+       int is_connected = 0, ctimesec = time(NULL) - connect_time;
 
        switch (cmd) {
        case CLI_INIT:
-               e->command = "realtime pgsql status";
+               e->command = "realtime show pgsql status";
                e->usage =
-                       "Usage: realtime pgsql status\n"
+                       "Usage: realtime show pgsql status\n"
                        "       Shows connection information for the PostgreSQL RealTime driver\n";
                return NULL;
        case CLI_GENERATE:
                return NULL;
        }
 
-       if (a->argc != 3)
+       if (a->argc != 4)
                return CLI_SHOWUSAGE;
 
-       if (pgsqlConn && PQstatus(pgsqlConn) == CONNECTION_OK) {
-               if (!ast_strlen_zero(dbhost))
-                       snprintf(status, sizeof(status), "Connected to %s@%s, port %d", dbname, dbhost, dbport);
-               else if (!ast_strlen_zero(dbsock))
-                       snprintf(status, sizeof(status), "Connected to %s on socket file %s", dbname, dbsock);
-               else
-                       snprintf(status, sizeof(status), "Connected to %s@%s", dbname, dbhost);
-
-               if (!ast_strlen_zero(dbuser))
-                       snprintf(credentials, sizeof(credentials), " with username %s", dbuser);
-
-               if (ctimesec > 31536000)
-                       ast_cli(a->fd, "%s%s for %d years, %d days, %d hours, %d minutes, %d seconds.\n",
-                                       status, credentials, ctimesec / 31536000, (ctimesec % 31536000) / 86400,
-                                       (ctimesec % 86400) / 3600, (ctimesec % 3600) / 60, ctimesec % 60);
-               else if (ctimesec > 86400)
-                       ast_cli(a->fd, "%s%s for %d days, %d hours, %d minutes, %d seconds.\n", status,
-                                       credentials, ctimesec / 86400, (ctimesec % 86400) / 3600, (ctimesec % 3600) / 60,
-                                       ctimesec % 60);
-               else if (ctimesec > 3600)
-                       ast_cli(a->fd, "%s%s for %d hours, %d minutes, %d seconds.\n", status, credentials,
-                                       ctimesec / 3600, (ctimesec % 3600) / 60, ctimesec % 60);
-               else if (ctimesec > 60)
-                       ast_cli(a->fd, "%s%s for %d minutes, %d seconds.\n", status, credentials, ctimesec / 60,
-                                       ctimesec % 60);
-               else
-                       ast_cli(a->fd, "%s%s for %d seconds.\n", status, credentials, ctimesec);
+       if (!ast_strlen_zero(dbhost))
+               snprintf(connection_info, sizeof(connection_info), "%s@%s, port %d", dbname, dbhost, dbport);
+       else if (!ast_strlen_zero(dbsock))
+               snprintf(connection_info, sizeof(connection_info), "%s on socket file %s", dbname, dbsock);
+       else
+               snprintf(connection_info, sizeof(connection_info), "%s@%s", dbname, dbhost);
+
+       if (!ast_strlen_zero(dbuser))
+               snprintf(credentials, sizeof(credentials), " with username %s", dbuser);
+
+       ast_mutex_lock(&pgsql_lock);
+       is_connected = (pgsqlConn && PQstatus(pgsqlConn) == CONNECTION_OK);
+       ast_mutex_unlock(&pgsql_lock);
 
+       if (is_connected) {
+               snprintf(buf, sizeof(buf), "Connected to %s%s for ", connection_info, credentials);
+               ast_cli_print_timestr_fromseconds(a->fd, ctimesec, buf);
                return CLI_SUCCESS;
        } else {
+               ast_cli(a->fd, "Unable to connect %s%s\n", connection_info, credentials);
                return CLI_FAILURE;
        }
 }
 
 /* needs usecount semantics defined */
-AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "PostgreSQL RealTime Configuration Driver",
-               .load = load_module,
-               .unload = unload_module,
-               .reload = reload
-              );
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PostgreSQL RealTime Configuration Driver",
+       .support_level = AST_MODULE_SUPPORT_EXTENDED,
+       .load = load_module,
+       .unload = unload_module,
+       .reload = reload,
+       .load_pri = AST_MODPRI_REALTIME_DRIVER,
+);