*** a/src/pl/plpgsql/src/pl_exec.c
--- b/src/pl/plpgsql/src/pl_exec.c
***************
*** 5148,5154 **** exec_eval_simple_expr(PLpgSQL_execstate *estate,
  	 */
  	if (expr->expr_simple_lxid != curlxid)
  	{
! 		oldcontext = MemoryContextSwitchTo(simple_eval_estate->es_query_cxt);
  		expr->expr_simple_state = ExecInitExpr(expr->expr_simple_expr, NULL);
  		expr->expr_simple_in_use = false;
  		expr->expr_simple_lxid = curlxid;
--- 5148,5160 ----
  	 */
  	if (expr->expr_simple_lxid != curlxid)
  	{
! 		if (estate->func->simple_eval_estate)
! 			oldcontext = MemoryContextSwitchTo(
! 							estate->func->simple_eval_estate->es_query_cxt);
! 		else
! 			oldcontext
! 				= MemoryContextSwitchTo(simple_eval_estate->es_query_cxt);
! 
  		expr->expr_simple_state = ExecInitExpr(expr->expr_simple_expr, NULL);
  		expr->expr_simple_in_use = false;
  		expr->expr_simple_lxid = curlxid;
*** a/src/pl/plpgsql/src/pl_handler.c
--- b/src/pl/plpgsql/src/pl_handler.c
***************
*** 24,29 ****
--- 24,30 ----
  #include "utils/guc.h"
  #include "utils/lsyscache.h"
  #include "utils/syscache.h"
+ #include "utils/memutils.h"
  
  PG_MODULE_MAGIC;
  
***************
*** 177,182 **** plpgsql_inline_handler(PG_FUNCTION_ARGS)
--- 178,184 ----
  	FmgrInfo	flinfo;
  	Datum		retval;
  	int			rc;
+ 	MemoryContext oldcontext;
  
  	Assert(IsA(codeblock, InlineCodeBlock));
  
***************
*** 203,209 **** plpgsql_inline_handler(PG_FUNCTION_ARGS)
  	flinfo.fn_oid = InvalidOid;
  	flinfo.fn_mcxt = CurrentMemoryContext;
  
! 	retval = plpgsql_exec_function(func, &fake_fcinfo);
  
  	/* Function should now have no remaining use-counts ... */
  	func->use_count--;
--- 205,226 ----
  	flinfo.fn_oid = InvalidOid;
  	flinfo.fn_mcxt = CurrentMemoryContext;
  
! 	oldcontext = MemoryContextSwitchTo(TopTransactionContext);
! 	func->simple_eval_estate = CreateExecutorState();
! 	MemoryContextSwitchTo(oldcontext);
! 
! 	PG_TRY();
! 	{
! 		retval = plpgsql_exec_function(func, &fake_fcinfo);
! 	}
! 	PG_CATCH();
! 	{
! 		FreeExecutorState(func->simple_eval_estate);
! 		PG_RE_THROW();
! 	}
! 	PG_END_TRY();
! 
!     FreeExecutorState(func->simple_eval_estate);
  
  	/* Function should now have no remaining use-counts ... */
  	func->use_count--;
*** a/src/pl/plpgsql/src/plpgsql.h
--- b/src/pl/plpgsql/src/plpgsql.h
***************
*** 746,751 **** typedef struct PLpgSQL_function
--- 746,754 ----
  	/* these fields change when the function is used */
  	struct PLpgSQL_execstate *cur_estate;
  	unsigned long use_count;
+ 
+ 	/* this field will keep local estate for DO block to execute simple expr*/
+ 	EState *simple_eval_estate;
  } PLpgSQL_function;
  
  
