Index: pool_proto_modules.c
===================================================================
RCS file: /cvsroot/pgpool/pgpool-II/pool_proto_modules.c,v
retrieving revision 1.89.2.13
diff -c -r1.89.2.13 pool_proto_modules.c
*** pool_proto_modules.c	17 Aug 2011 07:50:57 -0000	1.89.2.13
--- pool_proto_modules.c	22 Sep 2011 09:36:12 -0000
***************
*** 113,118 ****
--- 113,119 ----
  
  	POOL_SESSION_CONTEXT *session_context;
  	POOL_QUERY_CONTEXT *query_context;
+ 	POOL_MEMORY_POOL *old_context;
  
  	/* Get session context */
  	session_context = pool_get_session_context();
***************
*** 146,151 ****
--- 147,156 ----
  		return POOL_END;
  	}
  
+ 	/* switch memory context */
+ 	old_context = pool_memory;
+ 	pool_memory = query_context->memory_context;
+ 
  	/* parse SQL string */
  	parse_tree_list = raw_parser(contents);
  
***************
*** 268,274 ****
  					 sp->user, sp->database, remote_ps_data);
  			set_ps_display(psbuf, false);
  
- 			free_parser();
  			pool_query_context_destroy(query_context);
  			pool_set_skip_reading_from_backends();
  			return POOL_CONTINUE;
--- 273,278 ----
***************
*** 333,339 ****
  					status = insert_lock(frontend, backend, contents, (InsertStmt *)node, lock_kind);
  					if (status != POOL_CONTINUE)
  					{
! 						free_parser();
  						return status;
  					}
  				}
--- 337,343 ----
  					status = insert_lock(frontend, backend, contents, (InsertStmt *)node, lock_kind);
  					if (status != POOL_CONTINUE)
  					{
! 						pool_query_context_destroy(query_context);
  						return status;
  					}
  				}
***************
*** 341,347 ****
  		}
  		else if (REPLICATION && contents == NULL && start_internal_transaction(frontend, backend, node))
  		{
! 			free_parser();
  			return POOL_ERROR;
  		}
  	}
--- 345,351 ----
  		}
  		else if (REPLICATION && contents == NULL && start_internal_transaction(frontend, backend, node))
  		{
! 			pool_query_context_destroy(query_context);
  			return POOL_ERROR;
  		}
  	}
***************
*** 426,441 ****
  			{
  				/* Send query to all DB nodes at once */
  				status = pool_send_and_wait(query_context, string, len, 0, 0, "");
! 				/*
! 				free_parser();
! 				*/
  				return status;
  			}
  
  			/* Send the query to master node */
  			if (pool_send_and_wait(query_context, string, len, 1, MASTER_NODE_ID, "") != POOL_CONTINUE)
  			{
! 				free_parser();
  				return POOL_END;
  			}
  		}
--- 430,443 ----
  			{
  				/* Send query to all DB nodes at once */
  				status = pool_send_and_wait(query_context, string, len, 0, 0, "");
! 				/* free_parser(); */
  				return status;
  			}
  
  			/* Send the query to master node */
  			if (pool_send_and_wait(query_context, string, len, 1, MASTER_NODE_ID, "") != POOL_CONTINUE)
  			{
! 				pool_query_context_destroy(query_context);
  				return POOL_END;
  			}
  		}
***************
*** 445,451 ****
  		 */
  		if (pool_send_and_wait(query_context, string, len, -1, MASTER_NODE_ID, "") != POOL_CONTINUE)
  		{
! 			free_parser();
  			return POOL_END;
  		}
  
--- 447,453 ----
  		 */
  		if (pool_send_and_wait(query_context, string, len, -1, MASTER_NODE_ID, "") != POOL_CONTINUE)
  		{
! 			pool_query_context_destroy(query_context);
  			return POOL_END;
  		}
  
***************
*** 454,478 ****
  		{
  			if (pool_send_and_wait(query_context, string, len, 1, MASTER_NODE_ID, "") != POOL_CONTINUE)
  			{
! /*
! 				free_parser();
! */
  				return POOL_END;
  			}
  		}
! 		free_parser();
  	}
  	else
  	{
  		if (pool_send_and_wait(query_context, string, len, 1, MASTER_NODE_ID, "") != POOL_CONTINUE)
  		{
! 			free_parser();
  			return POOL_END;
  		}
! /*
! 		free_parser();
! */
  	}
  	return POOL_CONTINUE;
  }
  
--- 456,480 ----
  		{
  			if (pool_send_and_wait(query_context, string, len, 1, MASTER_NODE_ID, "") != POOL_CONTINUE)
  			{
! 				pool_query_context_destroy(query_context);
  				return POOL_END;
  			}
  		}
! 		/* free_parser(); */
  	}
  	else
  	{
  		if (pool_send_and_wait(query_context, string, len, 1, MASTER_NODE_ID, "") != POOL_CONTINUE)
  		{
! 			pool_query_context_destroy(query_context);
  			return POOL_END;
  		}
! 		/* free_parser(); */
  	}
+ 
+ 	/* switch memory context */
+ 	pool_memory = old_context;
+ 
  	return POOL_CONTINUE;
  }
  
***************
*** 760,772 ****
  			kind = pool_read_kind(backend);
  			if (kind != 'Z')
  			{
! 				/* free_parser(); */
  				return POOL_END;
  			}
  
  			if (ReadyForQuery(frontend, backend, 0) != POOL_CONTINUE)
  			{
! 				/* free_parser(); */
  				return POOL_END;
  			}
  
--- 762,774 ----
  			kind = pool_read_kind(backend);
  			if (kind != 'Z')
  			{
! 				pool_query_context_destroy(query_context);
  				return POOL_END;
  			}
  
  			if (ReadyForQuery(frontend, backend, 0) != POOL_CONTINUE)
  			{
! 				pool_query_context_destroy(query_context);
  				return POOL_END;
  			}
  
***************
*** 787,793 ****
  			status = insert_lock(frontend, backend, stmt, (InsertStmt *)query_context->parse_tree, insert_stmt_with_lock);
  			if (status != POOL_CONTINUE)
  			{
! 				/* free_parser(); */
  				return status;
  			}
  		}
--- 789,795 ----
  			status = insert_lock(frontend, backend, stmt, (InsertStmt *)query_context->parse_tree, insert_stmt_with_lock);
  			if (status != POOL_CONTINUE)
  			{
! 				pool_query_context_destroy(query_context);
  				return status;
  			}
  		}
***************
*** 807,813 ****
  		pool_debug("Parse: waiting for master completing the query");
  		if (pool_send_and_wait(query_context, contents, len, 1, MASTER_NODE_ID, "P") != POOL_CONTINUE)
  		{
! 			/* free_parser(); */
  			return POOL_END;
  		}
  
--- 809,815 ----
  		pool_debug("Parse: waiting for master completing the query");
  		if (pool_send_and_wait(query_context, contents, len, 1, MASTER_NODE_ID, "P") != POOL_CONTINUE)
  		{
! 			pool_query_context_destroy(query_context);
  			return POOL_END;
  		}
  
***************
*** 820,826 ****
  		deadlock_detected = detect_deadlock_error(MASTER(backend), MAJOR(backend));
  		if (deadlock_detected < 0)
  		{
! 			/* free_parser(); */
  			return POOL_END;
  		}
  		else
--- 822,828 ----
  		deadlock_detected = detect_deadlock_error(MASTER(backend), MAJOR(backend));
  		if (deadlock_detected < 0)
  		{
! 			pool_query_context_destroy(query_context);
  			return POOL_END;
  		}
  		else
***************
*** 841,847 ****
  								   strlen(POOL_ERROR_QUERY)+1, -1,
  								   MASTER_NODE_ID, "") != POOL_CONTINUE)
  			{
! 				/* free_parser(); */
  				return POOL_END;
  			}
  		}
--- 843,849 ----
  								   strlen(POOL_ERROR_QUERY)+1, -1,
  								   MASTER_NODE_ID, "") != POOL_CONTINUE)
  			{
! 				pool_query_context_destroy(query_context);
  				return POOL_END;
  			}
  		}
***************
*** 849,855 ****
  		{
  			if (pool_send_and_wait(query_context, contents, len, -1, MASTER_NODE_ID, "P") != POOL_CONTINUE)
  			{
! 				/* free_parser(); */
  				return POOL_END;
  			}
  		}
--- 851,857 ----
  		{
  			if (pool_send_and_wait(query_context, contents, len, -1, MASTER_NODE_ID, "P") != POOL_CONTINUE)
  			{
! 				pool_query_context_destroy(query_context);
  				return POOL_END;
  			}
  		}
***************
*** 858,864 ****
  	{
  		if (pool_send_and_wait(query_context, contents, len, 1, MASTER_NODE_ID, "P") != POOL_CONTINUE)
  		{
! 			/* free_parser(); */
  			return POOL_END;
  		}
  	}
--- 860,866 ----
  	{
  		if (pool_send_and_wait(query_context, contents, len, 1, MASTER_NODE_ID, "P") != POOL_CONTINUE)
  		{
! 			pool_query_context_destroy(query_context);
  			return POOL_END;
  		}
  	}
Index: pool_query_cache.c
===================================================================
RCS file: /cvsroot/pgpool/pgpool-II/pool_query_cache.c,v
retrieving revision 1.15
diff -c -r1.15 pool_query_cache.c
*** pool_query_cache.c	30 Aug 2010 03:55:58 -0000	1.15
--- pool_query_cache.c	22 Sep 2011 09:36:13 -0000
***************
*** 1013,1019 ****
  		{
  			free(parsed_query);
  			parsed_query = NULL;
- 			free_parser();
  		}
  	}
  
--- 1013,1018 ----
Index: pool_rewrite_query.c
===================================================================
RCS file: /cvsroot/pgpool/pgpool-II/pool_rewrite_query.c,v
retrieving revision 1.20.2.1
diff -c -r1.20.2.1 pool_rewrite_query.c
*** pool_rewrite_query.c	22 Sep 2011 01:43:43 -0000	1.20.2.1
--- pool_rewrite_query.c	22 Sep 2011 09:36:13 -0000
***************
*** 685,691 ****
  		 * Call parallel exe engine and return status to the upper layer.
  		 */
  		POOL_STATUS stats = pool_parallel_exec(frontend,backend,r_query->rewrite_query, node,true);
- 		free_parser();
  		pool_unset_query_in_progress();
  		return stats;
  	}
--- 685,690 ----
***************
*** 695,701 ****
  		r_query = rewrite_query_stmt(node,frontend,backend,r_query);
  		if(r_query->type == T_InsertStmt)
  		{
! 			free_parser();
  
  			if(r_query->r_code != INSERT_DIST_NO_RULE) {
  				pool_unset_query_in_progress();
--- 694,700 ----
  		r_query = rewrite_query_stmt(node,frontend,backend,r_query);
  		if(r_query->type == T_InsertStmt)
  		{
! 			/* free_parser(); */
  
  			if(r_query->r_code != INSERT_DIST_NO_RULE) {
  				pool_unset_query_in_progress();
***************
*** 705,711 ****
  		}
  		else if(r_query->type == T_SelectStmt)
  		{
- 			free_parser();
  			pool_unset_query_in_progress();
  			pool_set_skip_reading_from_backends();
  			return r_query->status;
--- 704,709 ----
