Index: pool_proto_modules.c
===================================================================
RCS file: /cvsroot/pgpool/pgpool-II/pool_proto_modules.c,v
retrieving revision 1.108
diff -c -r1.108 pool_proto_modules.c
*** pool_proto_modules.c	17 Aug 2011 07:44:38 -0000	1.108
--- pool_proto_modules.c	3 Oct 2011 06:40:06 -0000
***************
*** 115,120 ****
--- 115,121 ----
  
  	POOL_SESSION_CONTEXT *session_context;
  	POOL_QUERY_CONTEXT *query_context;
+ 	POOL_MEMORY_POOL *old_context;
  
  	/* Get session context */
  	session_context = pool_get_session_context();
***************
*** 148,153 ****
--- 149,158 ----
  		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);
  
***************
*** 270,276 ****
  					 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;
--- 275,280 ----
***************
*** 335,341 ****
  					status = insert_lock(frontend, backend, contents, (InsertStmt *)node, lock_kind);
  					if (status != POOL_CONTINUE)
  					{
! 						free_parser();
  						return status;
  					}
  				}
--- 339,345 ----
  					status = insert_lock(frontend, backend, contents, (InsertStmt *)node, lock_kind);
  					if (status != POOL_CONTINUE)
  					{
! 						pool_query_context_destroy(query_context);
  						return status;
  					}
  				}
***************
*** 343,349 ****
  		}
  		else if (REPLICATION && contents == NULL && start_internal_transaction(frontend, backend, node))
  		{
! 			free_parser();
  			return POOL_ERROR;
  		}
  	}
--- 347,353 ----
  		}
  		else if (REPLICATION && contents == NULL && start_internal_transaction(frontend, backend, node))
  		{
! 			pool_query_context_destroy(query_context);
  			return POOL_ERROR;
  		}
  	}
***************
*** 438,453 ****
  			{
  				/* Send query to all DB nodes at once */
  				status = pool_send_and_wait(query_context, 0, 0);
! 				/*
! 				free_parser();
! 				*/
  				return status;
  			}
  
  			/* Send the query to master node */
  			if (pool_send_and_wait(query_context, 1, MASTER_NODE_ID) != POOL_CONTINUE)
  			{
! 				free_parser();
  				return POOL_END;
  			}
  		}
--- 442,455 ----
  			{
  				/* Send query to all DB nodes at once */
  				status = pool_send_and_wait(query_context, 0, 0);
! 				/* free_parser(); */
  				return status;
  			}
  
  			/* Send the query to master node */
  			if (pool_send_and_wait(query_context, 1, MASTER_NODE_ID) != POOL_CONTINUE)
  			{
! 				pool_query_context_destroy(query_context);
  				return POOL_END;
  			}
  		}
***************
*** 457,463 ****
  		 */
  		if (pool_send_and_wait(query_context, -1, MASTER_NODE_ID) != POOL_CONTINUE)
  		{
! 			free_parser();
  			return POOL_END;
  		}
  
--- 459,465 ----
  		 */
  		if (pool_send_and_wait(query_context, -1, MASTER_NODE_ID) != POOL_CONTINUE)
  		{
! 			pool_query_context_destroy(query_context);
  			return POOL_END;
  		}
  
***************
*** 466,490 ****
  		{
  			if (pool_send_and_wait(query_context, 1, MASTER_NODE_ID) != POOL_CONTINUE)
  			{
! /*
! 				free_parser();
! */
  				return POOL_END;
  			}
  		}
! 		free_parser();
  	}
  	else
  	{
  		if (pool_send_and_wait(query_context, 1, MASTER_NODE_ID) != POOL_CONTINUE)
  		{
! 			free_parser();
  			return POOL_END;
  		}
! /*
! 		free_parser();
! */
  	}
  	return POOL_CONTINUE;
  }
  
--- 468,492 ----
  		{
  			if (pool_send_and_wait(query_context, 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, 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;
  }
  
***************
*** 785,797 ****
  			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;
  			}
  
--- 787,799 ----
  			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;
  			}
  
***************
*** 812,818 ****
  			status = insert_lock(frontend, backend, stmt, (InsertStmt *)query_context->parse_tree, insert_stmt_with_lock);
  			if (status != POOL_CONTINUE)
  			{
! 				/* free_parser(); */
  				return status;
  			}
  		}
--- 814,820 ----
  			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;
  			}
  		}
***************
*** 832,838 ****
  		pool_debug("Parse: waiting for master completing the query");
  		if (pool_extended_send_and_wait(query_context, "P", len, contents, 1, MASTER_NODE_ID) != POOL_CONTINUE)
  		{
! 			/* free_parser(); */
  			return POOL_END;
  		}
  
--- 834,840 ----
  		pool_debug("Parse: waiting for master completing the query");
  		if (pool_extended_send_and_wait(query_context, "P", len, contents, 1, MASTER_NODE_ID) != POOL_CONTINUE)
  		{
! 			pool_query_context_destroy(query_context);
  			return POOL_END;
  		}
  
***************
*** 845,851 ****
  		deadlock_detected = detect_deadlock_error(MASTER(backend), MAJOR(backend));
  		if (deadlock_detected < 0)
  		{
! 			/* free_parser(); */
  			return POOL_END;
  		}
  		else
--- 847,853 ----
  		deadlock_detected = detect_deadlock_error(MASTER(backend), MAJOR(backend));
  		if (deadlock_detected < 0)
  		{
! 			pool_query_context_destroy(query_context);
  			return POOL_END;
  		}
  		else
***************
*** 871,877 ****
  
  			if (pool_send_and_wait(error_qc, -1, MASTER_NODE_ID) != POOL_CONTINUE)
  			{
! 				/* free_parser(); */
  				return POOL_END;
  			}
  
--- 873,879 ----
  
  			if (pool_send_and_wait(error_qc, -1, MASTER_NODE_ID) != POOL_CONTINUE)
  			{
! 				pool_query_context_destroy(query_context);
  				return POOL_END;
  			}
  
***************
*** 883,889 ****
  		{
  			if (pool_extended_send_and_wait(query_context, "P", len, contents, -1, MASTER_NODE_ID) != POOL_CONTINUE)
  			{
! 				/* free_parser(); */
  				return POOL_END;
  			}
  		}
--- 885,891 ----
  		{
  			if (pool_extended_send_and_wait(query_context, "P", len, contents, -1, MASTER_NODE_ID) != POOL_CONTINUE)
  			{
! 				pool_query_context_destroy(query_context);
  				return POOL_END;
  			}
  		}
***************
*** 892,898 ****
  	{
  		if (pool_extended_send_and_wait(query_context, "P", len, contents, 1, MASTER_NODE_ID) != POOL_CONTINUE)
  		{
! 			/* free_parser(); */
  			return POOL_END;
  		}
  	}
--- 894,900 ----
  	{
  		if (pool_extended_send_and_wait(query_context, "P", len, contents, 1, MASTER_NODE_ID) != 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	3 Oct 2011 06:40:07 -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
diff -c -r1.20 pool_rewrite_query.c
*** pool_rewrite_query.c	17 Aug 2010 09:23:18 -0000	1.20
--- pool_rewrite_query.c	3 Oct 2011 06:40:07 -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 ----
