changeset 8646:7c568270c8df HEAD

pgsql: Don't use BEGIN/COMMIT if transaction has only a single change.
author Timo Sirainen <tss@iki.fi>
date Sat, 17 Jan 2009 12:31:28 -0500
parents a90d1488bacf
children cc46e5822b96
files src/lib-sql/driver-pgsql.c
diffstat 1 files changed, 20 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-sql/driver-pgsql.c	Sat Jan 17 12:30:54 2009 -0500
+++ b/src/lib-sql/driver-pgsql.c	Sat Jan 17 12:31:28 2009 -0500
@@ -74,6 +74,7 @@
 	sql_commit_callback_t *callback;
 	void *context;
 
+	char *first_update;
 	const char *error;
 
 	unsigned int opened:1;
@@ -84,6 +85,8 @@
 extern struct sql_result driver_pgsql_result;
 
 static void
+transaction_update_callback(struct sql_result *result, void *context);
+static void
 driver_pgsql_query_full(struct sql_db *db, const char *query,
 			sql_query_callback_t *callback, void *context,
 			bool retry_query);
@@ -384,6 +387,7 @@
 	i_assert(!db->querying);
 	i_assert(db->connected);
 
+	i_warning("%s", query);
 	if (!PQsendQuery(db->pg, query)) {
 		db->connected = FALSE;
 		result_finish(result);
@@ -893,6 +897,12 @@
 		(struct pgsql_transaction_context *)_ctx;
 	struct sql_result *result;
 
+	if (ctx->first_update != NULL) {
+		sql_query(_ctx->db, ctx->first_update,
+			  transaction_update_callback, ctx);
+		i_free_and_null(ctx->first_update);
+	}
+
 	if (ctx->failed) {
 		*error_r = ctx->error;
 		if (ctx->opened)
@@ -922,6 +932,7 @@
 
 	if (ctx->opened)
 		sql_exec(_ctx->db, "ROLLBACK");
+	i_free(ctx->first_update);
 	i_free(ctx);
 }
 
@@ -946,8 +957,17 @@
 		return;
 
 	if (!ctx->opened) {
+		if (ctx->first_update == NULL) {
+			/* delay sending the first update in case there is
+			   only one to be sent and we don't need BEGIN/COMMIT */
+			ctx->first_update = i_strdup(query);
+			return;
+		}
 		ctx->opened = TRUE;
 		sql_query(_ctx->db, "BEGIN", transaction_update_callback, ctx);
+		sql_query(_ctx->db, ctx->first_update,
+			  transaction_update_callback, ctx);
+		i_free_and_null(ctx->first_update);
 	}
 
 	driver_pgsql_query_full(_ctx->db, query,