diff --git a/src/backend/utils/mmgr/aset.c b/src/backend/utils/mmgr/aset.c
index d26991e..65a60b2 100644
*** a/src/backend/utils/mmgr/aset.c
--- b/src/backend/utils/mmgr/aset.c
***************
*** 86,91 ****
--- 86,93 ----
  
  #include "postgres.h"
  
+ #include <execinfo.h>
+ 
  #include "utils/memdebug.h"
  #include "utils/memutils.h"
  
*************** AllocSetAlloc(MemoryContext context, Siz
*** 667,672 ****
--- 669,703 ----
  
  	AssertArg(AllocSetIsValid(set));
  
+ 	if (strcmp(context->name, "SPI Proc") == 0)
+ 	{
+ 		void	   *bt[10];
+ 		int			n,
+ 					j;
+ 		char	  **strings;
+ 
+ 		n = backtrace(bt, lengthof(bt));
+ 		if (n > 0)
+ 		{
+ 			strings = backtrace_symbols(bt, n);
+ 			if (strings == NULL)
+ 			{
+ 				perror("backtrace_symbols");
+ 				exit(EXIT_FAILURE);
+ 			}
+ 			for (j = 0; j < n; j++)
+ 			{
+ 				if (strstr(strings[j], "plpgsql.so"))
+ 				{
+ 					fflush(NULL);
+ 					fprintf(stderr, "alloc from: %s\n", strings[j]);
+ 					break;
+ 				}
+ 			}
+ 			free(strings);
+ 		}
+ 	}
+ 
  	/*
  	 * If requested size exceeds maximum for chunks, allocate an entire block
  	 * for this request.
*************** AllocSetFree(MemoryContext context, void
*** 943,948 ****
--- 974,1008 ----
  				 set->header.name, chunk);
  #endif
  
+ 	if (strcmp(context->name, "SPI Proc") == 0)
+ 	{
+ 		void	   *bt[10];
+ 		int			n,
+ 					j;
+ 		char	  **strings;
+ 
+ 		n = backtrace(bt, lengthof(bt));
+ 		if (n > 0)
+ 		{
+ 			strings = backtrace_symbols(bt, n);
+ 			if (strings == NULL)
+ 			{
+ 				perror("backtrace_symbols");
+ 				exit(EXIT_FAILURE);
+ 			}
+ 			for (j = 0; j < n; j++)
+ 			{
+ 				if (strstr(strings[j], "plpgsql.so"))
+ 				{
+ 					fflush(NULL);
+ 					fprintf(stderr, "free from: %s\n", strings[j]);
+ 					break;
+ 				}
+ 			}
+ 			free(strings);
+ 		}
+ 	}
+ 
  	if (chunk->size > set->allocChunkLimit)
  	{
  		/*
diff --git a/src/pl/plpgsql/src/pl_comp.c b/src/pl/plpgsql/src/pl_comp.c
index b628c28..1cdb82a 100644
*** a/src/pl/plpgsql/src/pl_comp.c
--- b/src/pl/plpgsql/src/pl_comp.c
*************** do_compile(FunctionCallInfo fcinfo,
*** 288,293 ****
--- 288,294 ----
  	int		   *in_arg_varnos = NULL;
  	PLpgSQL_variable **out_arg_variables;
  	MemoryContext func_cxt;
+ 	MemoryContext oldcontext = CurrentMemoryContext;
  
  	/*
  	 * Setup the scanner input and error info.  We assume that this function
*************** do_compile(FunctionCallInfo fcinfo,
*** 334,339 ****
--- 335,347 ----
  	}
  	plpgsql_curr_compile = function;
  
+ 	plpgsql_compile_tmp_cxt =
+ 		AllocSetContextCreate(CurrentMemoryContext,
+ 							  "PL/pgSQL compile tmp context",
+ 							  ALLOCSET_DEFAULT_MINSIZE,
+ 							  ALLOCSET_DEFAULT_INITSIZE,
+ 							  ALLOCSET_DEFAULT_MAXSIZE);
+ 
  	/*
  	 * All the permanent output of compilation (e.g. parse tree) is kept in a
  	 * per-function memory context, so it can be reclaimed easily.
*************** do_compile(FunctionCallInfo fcinfo,
*** 343,349 ****
  									 ALLOCSET_DEFAULT_MINSIZE,
  									 ALLOCSET_DEFAULT_INITSIZE,
  									 ALLOCSET_DEFAULT_MAXSIZE);
! 	plpgsql_compile_tmp_cxt = MemoryContextSwitchTo(func_cxt);
  
  	function->fn_signature = format_procedure(fcinfo->flinfo->fn_oid);
  	function->fn_oid = fcinfo->flinfo->fn_oid;
--- 351,357 ----
  									 ALLOCSET_DEFAULT_MINSIZE,
  									 ALLOCSET_DEFAULT_INITSIZE,
  									 ALLOCSET_DEFAULT_MAXSIZE);
! 	MemoryContextSwitchTo(func_cxt);
  
  	function->fn_signature = format_procedure(fcinfo->flinfo->fn_oid);
  	function->fn_oid = fcinfo->flinfo->fn_oid;
*************** do_compile(FunctionCallInfo fcinfo,
*** 774,781 ****
  
  	plpgsql_check_syntax = false;
  
- 	MemoryContextSwitchTo(plpgsql_compile_tmp_cxt);
  	plpgsql_compile_tmp_cxt = NULL;
  	return function;
  }
  
--- 782,790 ----
  
  	plpgsql_check_syntax = false;
  
  	plpgsql_compile_tmp_cxt = NULL;
+ 
+ 	MemoryContextSwitchTo(oldcontext);
  	return function;
  }
  
*************** plpgsql_compile_inline(char *proc_source
*** 798,803 ****
--- 807,813 ----
  	PLpgSQL_variable *var;
  	int			parse_rc;
  	MemoryContext func_cxt;
+ 	MemoryContext oldcontext = CurrentMemoryContext;
  
  	/*
  	 * Setup the scanner input and error info.  We assume that this function
*************** plpgsql_compile_inline(char *proc_source
*** 824,829 ****
--- 834,846 ----
  
  	plpgsql_curr_compile = function;
  
+ 	plpgsql_compile_tmp_cxt =
+ 		AllocSetContextCreate(CurrentMemoryContext,
+ 							  "PL/pgSQL compile tmp context",
+ 							  ALLOCSET_DEFAULT_MINSIZE,
+ 							  ALLOCSET_DEFAULT_INITSIZE,
+ 							  ALLOCSET_DEFAULT_MAXSIZE);
+ 
  	/*
  	 * All the rest of the compile-time storage (e.g. parse tree) is kept in
  	 * its own memory context, so it can be reclaimed easily.
*************** plpgsql_compile_inline(char *proc_source
*** 833,839 ****
  									 ALLOCSET_DEFAULT_MINSIZE,
  									 ALLOCSET_DEFAULT_INITSIZE,
  									 ALLOCSET_DEFAULT_MAXSIZE);
! 	plpgsql_compile_tmp_cxt = MemoryContextSwitchTo(func_cxt);
  
  	function->fn_signature = pstrdup(func_name);
  	function->fn_is_trigger = PLPGSQL_NOT_TRIGGER;
--- 850,856 ----
  									 ALLOCSET_DEFAULT_MINSIZE,
  									 ALLOCSET_DEFAULT_INITSIZE,
  									 ALLOCSET_DEFAULT_MAXSIZE);
! 	MemoryContextSwitchTo(func_cxt);
  
  	function->fn_signature = pstrdup(func_name);
  	function->fn_is_trigger = PLPGSQL_NOT_TRIGGER;
*************** plpgsql_compile_inline(char *proc_source
*** 911,918 ****
  
  	plpgsql_check_syntax = false;
  
- 	MemoryContextSwitchTo(plpgsql_compile_tmp_cxt);
  	plpgsql_compile_tmp_cxt = NULL;
  	return function;
  }
  
--- 928,936 ----
  
  	plpgsql_check_syntax = false;
  
  	plpgsql_compile_tmp_cxt = NULL;
+ 
+ 	MemoryContextSwitchTo(oldcontext);
  	return function;
  }
  
