Index: pool_proto_modules.c
===================================================================
RCS file: /cvsroot/pgpool/pgpool-II/pool_proto_modules.c,v
retrieving revision 1.89
diff -u -r1.89 pool_proto_modules.c
--- pool_proto_modules.c	20 Oct 2010 00:34:22 -0000	1.89
+++ pool_proto_modules.c	13 Jan 2011 03:16:22 -0000
@@ -470,38 +470,51 @@
 
 	pool_debug("Execute: portal name <%s>", contents);
 
-	portal = pool_get_portal_by_portal_name(contents);
-	if (portal == NULL)
+	if (RAW_MODE)
 	{
-		pool_error("Execute: cannot get portal");
-		return POOL_END;
-	}
-	if (portal->pstmt == NULL)
-	{
-		pool_error("Execute: cannot get prepared statement");
-		return POOL_END;
-	}
-	if (portal->pstmt->qctxt == NULL)
-	{
-		pool_error("Execute: cannot get query context");
-		return POOL_END;
+		query_context = session_context->query_context;
+		node = query_context->parse_tree;
+		query = query_context->original_query;
+
+		session_context->query_context = query_context;
 	}
-	if (portal->pstmt->qctxt->parse_tree== NULL)
+	else
 	{
-		pool_error("Execute: cannot get parse tree");
-		return POOL_END;
+		portal = pool_get_portal_by_portal_name(contents);
+		if (portal == NULL)
+		{
+			pool_error("Execute: cannot get portal");
+			return POOL_END;
+		}
+		if (portal->pstmt == NULL)
+		{
+			pool_error("Execute: cannot get prepared statement");
+			return POOL_END;
+		}
+		if (portal->pstmt->qctxt == NULL)
+		{
+			pool_error("Execute: cannot get query context");
+			return POOL_END;
+		}
+		if (portal->pstmt->qctxt->parse_tree== NULL)
+		{
+			pool_error("Execute: cannot get parse tree");
+			return POOL_END;
+		}
+
+		query_context = portal->pstmt->qctxt;
+		node = query_context->parse_tree;
+		query = portal->pstmt->qctxt->original_query;
+
+		session_context->query_context = query_context;
 	}
 
-	query_context = portal->pstmt->qctxt;
-	node = query_context->parse_tree;
-	query = portal->pstmt->qctxt->original_query;
 	pool_debug("Execute: query: %s", query);
 	strncpy(query_string_buffer, query, sizeof(query_string_buffer));
 
 	/*
 	 * Decide where to send query
 	 */
-	session_context->query_context = query_context;
 	pool_where_to_send(query_context, query, node);
 
 
@@ -645,8 +658,11 @@
 		 */
 		pool_start_query(query_context, pstrdup(stmt), node);
 
-		ps = pool_create_prepared_statement(name, 0, len, contents, query_context);
-		session_context->pending_pstmt = ps;
+		if (!RAW_MODE)
+		{
+			ps = pool_create_prepared_statement(name, 0, len, contents, query_context);
+			session_context->pending_pstmt = ps;
+		}
 
 		/*
 		 * Decide where to send query
@@ -842,41 +858,49 @@
 	/*
 	 * Rewrite message
 	 */
-	portal_name = contents;
-	pstmt_name = contents + strlen(portal_name) + 1;
-
-	pstmt = pool_get_prepared_statement_by_pstmt_name(pstmt_name);
-	if (pstmt == NULL)
+	if (RAW_MODE)
 	{
-		pool_error("Bind: cannot get prepared statement \"%s\"", pstmt_name);
-		return POOL_END;
+		query_context = session_context->query_context;
 	}
-
-	portal = pool_create_portal(portal_name, pstmt->num_tsparams, pstmt);
-	if (portal == NULL)
+	else
 	{
-		pool_error("Bind: cannot create portal: %s", strerror(errno));
-		return POOL_END;
-	}
+		portal_name = contents;
+		pstmt_name = contents + strlen(portal_name) + 1;
 
-	query_context = pstmt->qctxt;
-	if (query_context == NULL)
-	{
-		pool_error("Bind: cannot get query context");
-		return POOL_END;
-	}
+		pstmt = pool_get_prepared_statement_by_pstmt_name(pstmt_name);
+		if (pstmt == NULL)
+		{
+			pool_error("Bind: cannot get prepared statement \"%s\"", pstmt_name);
+			return POOL_END;
+		}
 
-	session_context->pending_portal = portal;
+		portal = pool_create_portal(portal_name, pstmt->num_tsparams, pstmt);
+		if (portal == NULL)
+		{
+			pool_error("Bind: cannot create portal: %s", strerror(errno));
+			return POOL_END;
+		}
 
-	/* rewrite bind message */
-	if (REPLICATION && portal->num_tsparams > 0)
-	{
-		rewrite_msg = bind_rewrite_timestamp(backend, portal, contents, &len);
-		if (rewrite_msg != NULL)
-			contents = rewrite_msg;
+		query_context = pstmt->qctxt;
+		if (query_context == NULL)
+		{
+			pool_error("Bind: cannot get query context");
+			return POOL_END;
+		}
+
+		session_context->pending_portal = portal;
+
+		/* rewrite bind message */
+		if (REPLICATION && portal->num_tsparams > 0)
+		{
+			rewrite_msg = bind_rewrite_timestamp(backend, portal, contents, &len);
+			if (rewrite_msg != NULL)
+				contents = rewrite_msg;
+		}
+
+		session_context->query_context = query_context;
 	}
 
-	session_context->query_context = query_context;
 	pool_where_to_send(query_context, query_context->original_query,
 					   query_context->parse_tree);
 
@@ -928,38 +952,46 @@
 		return POOL_END;
 	}
 
-	/* Prepared Statement */
-	if (*contents == 'S')
+	if (RAW_MODE)
 	{
-		pstmt = pool_get_prepared_statement_by_pstmt_name(contents+1);
+		query_context = session_context->query_context;
 	}
-	/* Portal */
 	else
 	{
-		portal = pool_get_portal_by_portal_name(contents+1);
-		if (portal == NULL)
+		/* Prepared Statement */
+		if (*contents == 'S')
 		{
-			pool_error("Describe: cannot get portal \"%s\"", contents+1);
-			return POOL_END;
+			pstmt = pool_get_prepared_statement_by_pstmt_name(contents+1);
 		}
+		/* Portal */
+		else
+		{
+			portal = pool_get_portal_by_portal_name(contents+1);
+			if (portal == NULL)
+			{
+				pool_error("Describe: cannot get portal \"%s\"", contents+1);
+				return POOL_END;
+			}
 
-		pstmt = portal->pstmt;
-	}
+			pstmt = portal->pstmt;
+		}
 
-	if (pstmt == NULL)
-	{
-		pool_error("Describe: cannot get prepared statement");
-		return POOL_END;
-	}
+		if (pstmt == NULL)
+		{
+			pool_error("Describe: cannot get prepared statement");
+			return POOL_END;
+		}
 
-	query_context = pstmt->qctxt;
-	if (query_context == NULL)
-	{
-		pool_error("Describe: cannot get query context");
-		return POOL_END;
+		query_context = pstmt->qctxt;
+		if (query_context == NULL)
+		{
+			pool_error("Describe: cannot get query context");
+			return POOL_END;
+		}
+
+		session_context->query_context = query_context;
 	}
 
-	session_context->query_context = query_context;
 	pool_where_to_send(query_context, query_context->original_query,
 					   query_context->parse_tree);
 
@@ -995,45 +1027,53 @@
 		return POOL_END;
 	}
 
-	/* Prepared Statement */
-	if (*contents == 'S')
+	if (RAW_MODE)
 	{
-		pstmt = pool_get_prepared_statement_by_pstmt_name(contents+1);
-		if (pstmt == NULL)
+		query_context = session_context->query_context;
+	}
+	else
+	{
+		/* Prepared Statement */
+		if (*contents == 'S')
 		{
-			pool_error("Close: cannot get prepared statement");
-			return POOL_END;
+			pstmt = pool_get_prepared_statement_by_pstmt_name(contents+1);
+			if (pstmt == NULL)
+			{
+				pool_error("Close: cannot get prepared statement");
+				return POOL_END;
+			}
+
+			session_context->pending_pstmt = pstmt;
+			query_context = pstmt->qctxt;
 		}
+		/* Portal */
+		else if (*contents == 'P')
+		{
+			portal = pool_get_portal_by_portal_name(contents+1);
+			if (portal == NULL)
+			{
+				pool_error("Close: cannot get portal");
+				return POOL_END;
+			}
 
-		session_context->pending_pstmt = pstmt;
-		query_context = pstmt->qctxt;
-	}
-	/* Portal */
-	else if (*contents == 'P')
-	{
-		portal = pool_get_portal_by_portal_name(contents+1);
-		if (portal == NULL)
+			session_context->pending_portal = portal;
+			query_context = portal->qctxt;
+		}
+		else
 		{
-			pool_error("Close: cannot get portal");
+			pool_error("Close: invalid message");
 			return POOL_END;
 		}
 
-		session_context->pending_portal = portal;
-		query_context = portal->qctxt;
-	}
-	else
-	{
-		pool_error("Close: invalid message");
-		return POOL_END;
-	}
+		if (query_context == NULL)
+		{
+			pool_error("Close: cannot get query context");
+			return POOL_END;
+		}
 
-	if (query_context == NULL)
-	{
-		pool_error("Close: cannot get query context");
-		return POOL_END;
+		session_context->query_context = query_context;
 	}
 
-	session_context->query_context = query_context;
 	/* pool_where_to_send(query_context, query_context->original_query, query_context->parse_tree); */
 
 	pool_debug("Close: waiting for master completing the query");
@@ -1465,26 +1505,6 @@
 			pool_add_prepared_statement();
 			session_context->pending_pstmt = NULL;
 		}
-		else if (IsA(node, DeallocateStmt))
-		{
-			char *name;
-			
-			name = ((DeallocateStmt *)node)->name;
-			if (name == NULL)
-				pool_clear_prepared_statement_list();
-			else
-				pool_remove_prepared_statement();
-			session_context->pending_pstmt = NULL;
-		}
-		else if (IsA(node, DiscardStmt))
-		{
-			DiscardStmt *stmt = (DiscardStmt *)node;
-			if (stmt->target == DISCARD_ALL || stmt->target == DISCARD_PLANS)
-			{
-				pool_remove_pending_objects();
-				pool_clear_prepared_statement_list();
-			}
-		}
 		/*
 		 * JDBC driver sends "BEGIN" query internally if
 		 * setAutoCommit(false).  But it does not send Sync message
@@ -1675,6 +1695,32 @@
 
 	free(p1);
 
+	if (session_context->query_context != NULL)
+	{
+		Node *node = session_context->query_context->parse_tree;
+
+		if (IsA(node, DeallocateStmt))
+		{
+			char *name;
+			
+			name = ((DeallocateStmt *)node)->name;
+			if (name == NULL)
+				pool_clear_prepared_statement_list();
+			else
+				pool_remove_prepared_statement();
+			session_context->pending_pstmt = NULL;
+		}
+		else if (IsA(node, DiscardStmt))
+		{
+			DiscardStmt *stmt = (DiscardStmt *)node;
+			if (stmt->target == DISCARD_ALL || stmt->target == DISCARD_PLANS)
+			{
+				pool_remove_pending_objects();
+				pool_clear_prepared_statement_list();
+			}
+		}
+	}
+
 	return POOL_CONTINUE;
 }
 
Index: pool_query_context.c
===================================================================
RCS file: /cvsroot/pgpool/pgpool-II/pool_query_context.c,v
retrieving revision 1.32
diff -u -r1.32 pool_query_context.c
--- pool_query_context.c	20 Oct 2010 01:08:55 -0000	1.32
+++ pool_query_context.c	13 Jan 2011 03:16:22 -0000
@@ -972,6 +972,7 @@
 static
 void where_to_send_deallocate(POOL_QUERY_CONTEXT *query_context, Node *node)
 {
+	POOL_SESSION_CONTEXT *session_context;
 	DeallocateStmt *d = (DeallocateStmt *)node;
 	bool *wts;
 
@@ -997,6 +998,8 @@
 			ps = pool_get_prepared_statement_by_pstmt_name(d->name);
 			if (ps && ps->qctxt)
 			{
+				session_context = pool_get_session_context();
+				session_context->pending_pstmt = ps;
 				pool_copy_prep_where(ps->qctxt->where_to_send, query_context->where_to_send);
 				return;
 			}
Index: pool_session_context.c
===================================================================
RCS file: /cvsroot/pgpool/pgpool-II/pool_session_context.c,v
retrieving revision 1.25
diff -u -r1.25 pool_session_context.c
--- pool_session_context.c	19 Oct 2010 08:57:18 -0000	1.25
+++ pool_session_context.c	13 Jan 2011 03:16:23 -0000
@@ -1177,6 +1177,9 @@
 		}
 	}
 
+    if (!pool_is_doing_extended_query_message())
+		return false;
+
 	return true;
 }
 
