diff --git a/cdr/cdr_adaptive_odbc.c b/cdr/cdr_adaptive_odbc.c
index 8bd9d0da6455a150f0fa09eea7f16250c5e34402..0da8c01422d67d800bd21ba6b04118bf95146a54 100644
--- a/cdr/cdr_adaptive_odbc.c
+++ b/cdr/cdr_adaptive_odbc.c
@@ -358,11 +358,11 @@ static SQLHSTMT generic_prepare(struct odbc_obj *obj, void *data)
 	return stmt;
 }
 
-#define LENGTHEN_BUF1(size)														\
+#define LENGTHEN_BUF(size, var_sql)														\
 			do {																\
 				/* Lengthen buffer, if necessary */								\
-				if (ast_str_strlen(sql) + size + 1 > ast_str_size(sql)) {		\
-					if (ast_str_make_space(&sql, ((ast_str_size(sql) + size + 1) / 512 + 1) * 512) != 0) { \
+				if (ast_str_strlen(var_sql) + size + 1 > ast_str_size(var_sql)) {		\
+					if (ast_str_make_space(&var_sql, ((ast_str_size(var_sql) + size + 1) / 512 + 1) * 512) != 0) { \
 						ast_log(LOG_ERROR, "Unable to allocate sufficient memory.  Insert CDR '%s:%s' failed.\n", tableptr->connection, tableptr->table); \
 						ast_free(sql);											\
 						ast_free(sql2);											\
@@ -372,18 +372,10 @@ static SQLHSTMT generic_prepare(struct odbc_obj *obj, void *data)
 				}																\
 			} while (0)
 
-#define LENGTHEN_BUF2(size)														\
-			do {																\
-				if (ast_str_strlen(sql2) + size + 1 > ast_str_size(sql2)) {		\
-					if (ast_str_make_space(&sql2, ((ast_str_size(sql2) + size + 3) / 512 + 1) * 512) != 0) { \
-						ast_log(LOG_ERROR, "Unable to allocate sufficient memory.  Insert CDR '%s:%s' failed.\n", tableptr->connection, tableptr->table); \
-						ast_free(sql);											\
-						ast_free(sql2);											\
-						AST_RWLIST_UNLOCK(&odbc_tables);						\
-						return -1;												\
-					}															\
-				}																\
-			} while (0)
+#define LENGTHEN_BUF1(size)	\
+	LENGTHEN_BUF(size, sql);
+#define LENGTHEN_BUF2(size)	\
+	LENGTHEN_BUF(size, sql2);
 
 static int odbc_log(struct ast_cdr *cdr)
 {
diff --git a/cel/cel_odbc.c b/cel/cel_odbc.c
index 2d8408baa5cce184ead62f71ba90f697457c45fd..3c0be04e848aba53a3b55c0a98f7fcfe837f0845 100644
--- a/cel/cel_odbc.c
+++ b/cel/cel_odbc.c
@@ -341,11 +341,11 @@ static SQLHSTMT generic_prepare(struct odbc_obj *obj, void *data)
 	return stmt;
 }
 
-#define LENGTHEN_BUF1(size)														\
+#define LENGTHEN_BUF(size, var_sql)														\
 			do {																\
 				/* Lengthen buffer, if necessary */								\
-				if (ast_str_strlen(sql) + size + 1 > ast_str_size(sql)) {		\
-					if (ast_str_make_space(&sql, ((ast_str_size(sql) + size + 1) / 512 + 1) * 512) != 0) { \
+				if (ast_str_strlen(var_sql) + size + 1 > ast_str_size(var_sql)) {		\
+					if (ast_str_make_space(&var_sql, ((ast_str_size(var_sql) + size + 1) / 512 + 1) * 512) != 0) { \
 						ast_log(LOG_ERROR, "Unable to allocate sufficient memory.  Insert CEL '%s:%s' failed.\n", tableptr->connection, tableptr->table); \
 						ast_free(sql);											\
 						ast_free(sql2);											\
@@ -355,18 +355,11 @@ static SQLHSTMT generic_prepare(struct odbc_obj *obj, void *data)
 				}																\
 			} while (0)
 
-#define LENGTHEN_BUF2(size)														\
-			do {																\
-				if (ast_str_strlen(sql2) + size + 1 > ast_str_size(sql2)) {		\
-					if (ast_str_make_space(&sql2, ((ast_str_size(sql2) + size + 3) / 512 + 1) * 512) != 0) { \
-						ast_log(LOG_ERROR, "Unable to allocate sufficient memory.  Insert CEL '%s:%s' failed.\n", tableptr->connection, tableptr->table); \
-						ast_free(sql);											\
-						ast_free(sql2);											\
-						AST_RWLIST_UNLOCK(&odbc_tables);						\
-						return;													\
-					}															\
-				}																\
-			} while (0)
+#define LENGTHEN_BUF1(size) \
+	LENGTHEN_BUF(size, sql);
+
+#define LENGTHEN_BUF2(size) \
+	LENGTHEN_BUF(size, sql2);
 
 static void odbc_log(struct ast_event *event)
 {
diff --git a/cel/cel_pgsql.c b/cel/cel_pgsql.c
index f4959474a0a46b11b298c338aca99d64799167e2..e4e280e41c7a9375c442acc836dcbcfa21277a26 100644
--- a/cel/cel_pgsql.c
+++ b/cel/cel_pgsql.c
@@ -100,11 +100,11 @@ struct columns {
 
 static AST_RWLIST_HEAD_STATIC(psql_columns, columns);
 
-#define LENGTHEN_BUF1(size) \
+#define LENGTHEN_BUF(size, var_sql) \
 	do { \
 		/* Lengthen buffer, if necessary */ \
-		if (ast_str_strlen(sql) + size + 1 > ast_str_size(sql)) { \
-			if (ast_str_make_space(&sql, ((ast_str_size(sql) + size + 3) / 512 + 1) * 512) != 0) { \
+		if (ast_str_strlen(var_sql) + size + 1 > ast_str_size(var_sql)) { \
+			if (ast_str_make_space(&var_sql, ((ast_str_size(var_sql) + size + 3) / 512 + 1) * 512) != 0) { \
 				ast_log(LOG_ERROR, "Unable to allocate sufficient memory.  Insert CEL '%s:%s' failed.\n", pghostname, table); \
 				ast_free(sql); \
 				ast_free(sql2); \
@@ -114,18 +114,10 @@ static AST_RWLIST_HEAD_STATIC(psql_columns, columns);
 		} \
 	} while (0)
 
+#define LENGTHEN_BUF1(size) \
+	LENGTHEN_BUF(size, sql);
 #define LENGTHEN_BUF2(size) \
-	do { \
-		if (ast_str_strlen(sql2) + size + 1 > ast_str_size(sql2)) { \
-			if (ast_str_make_space(&sql2, ((ast_str_size(sql2) + size + 3) / 512 + 1) * 512) != 0) { \
-				ast_log(LOG_ERROR, "Unable to allocate sufficient memory.  Insert CEL '%s:%s' failed.\n", pghostname, table); \
-				ast_free(sql); \
-				ast_free(sql2); \
-				AST_RWLIST_UNLOCK(&psql_columns); \
-				return; \
-			} \
-		} \
-	} while (0)
+	LENGTHEN_BUF(size, sql2);
 
 static void pgsql_reconnect(void)
 {