============================================================
*** src/backend/utils/mb/mbutils.c	dce11533700ce21aa39b9602dd5dad1a623c2909
--- src/backend/utils/mb/mbutils.c	b04e88249835b6a3a5fcf09bee9f18390bde98ed
***************
*** 9,20 ****
  #include "postgres.h"
  
  #include "access/xact.h"
  #include "miscadmin.h"
  #include "mb/pg_wchar.h"
  #include "utils/builtins.h"
  #include "utils/memutils.h"
  #include "utils/syscache.h"
- #include "catalog/namespace.h"
  
  /*
   * We handle for actual FE and BE encoding setting encoding-identificator
--- 9,20 ----
  #include "postgres.h"
  
  #include "access/xact.h"
+ #include "catalog/namespace.h"
  #include "miscadmin.h"
  #include "mb/pg_wchar.h"
  #include "utils/builtins.h"
  #include "utils/memutils.h"
  #include "utils/syscache.h"
  
  /*
   * We handle for actual FE and BE encoding setting encoding-identificator
***************
*** 25,34 ****
  static pg_enc2name *DatabaseEncoding = &pg_enc2name_tbl[PG_SQL_ASCII];
  
  /*
!  * Caches for conversion function info. Note that these values are
!  * allocated in TopMemoryContext so that they survive across
!  * transactions. See SetClientEncoding() for more details.
   */
  static FmgrInfo *ToServerConvProc = NULL;
  static FmgrInfo *ToClientConvProc = NULL;
  
--- 25,36 ----
  static pg_enc2name *DatabaseEncoding = &pg_enc2name_tbl[PG_SQL_ASCII];
  
  /*
!  * Caches for conversion function info. These values are allocated in
!  * MbProcContext. That context is a child of TopMemoryContext,
!  * which allows these values to survive across transactions. See
!  * SetClientEncoding() for more details.
   */
+ static MemoryContext MbProcContext = NULL;
  static FmgrInfo *ToServerConvProc = NULL;
  static FmgrInfo *ToClientConvProc = NULL;
  
***************
*** 86,98 ****
  		if (doit)
  		{
  			ClientEncoding = &pg_enc2name_tbl[encoding];
- 
- 			if (ToServerConvProc != NULL)
- 			{
- 				if (ToServerConvProc->fn_extra)
- 					pfree(ToServerConvProc->fn_extra);
- 				pfree(ToServerConvProc);
- 			}
  			ToServerConvProc = NULL;
  
  			if (ToClientConvProc != NULL)
--- 88,93 ----
***************
*** 94,106 ****
  				pfree(ToServerConvProc);
  			}
  			ToServerConvProc = NULL;
- 
- 			if (ToClientConvProc != NULL)
- 			{
- 				if (ToClientConvProc->fn_extra)
- 					pfree(ToClientConvProc->fn_extra);
- 				pfree(ToClientConvProc);
- 			}
  			ToClientConvProc = NULL;
  		}
  		return 0;
--- 89,94 ----
***************
*** 102,107 ****
--- 90,97 ----
  				pfree(ToClientConvProc);
  			}
  			ToClientConvProc = NULL;
+ 			if (MbProcContext)
+ 				MemoryContextReset(MbProcContext);
  		}
  		return 0;
  	}
***************
*** 134,144 ****
  	if (!doit)
  		return 0;
  
! 	/*
! 	 * load the fmgr info into TopMemoryContext so that it survives outside
! 	 * transaction.
! 	 */
! 	oldcontext = MemoryContextSwitchTo(TopMemoryContext);
  	to_server = palloc(sizeof(FmgrInfo));
  	to_client = palloc(sizeof(FmgrInfo));
  	fmgr_info(to_server_proc, to_server);
--- 124,152 ----
  	if (!doit)
  		return 0;
  
! 	/* Before loading the new fmgr info, remove the old info, if any */
! 	ToServerConvProc = NULL;
! 	ToClientConvProc = NULL;
! 	if (MbProcContext != NULL)
! 	{
! 		MemoryContextReset(MbProcContext);
! 	}
! 	else
! 	{
! 		/*
! 		 * This is the first time through, so create the context. Make
! 		 * it a child of TopMemoryContext so that these values survive
! 		 * across transactions.
! 		 */
! 		MbProcContext = AllocSetContextCreate(TopMemoryContext,
! 											  "",
! 											  ALLOCSET_SMALL_MINSIZE,
! 											  ALLOCSET_SMALL_INITSIZE,
! 											  ALLOCSET_SMALL_MAXSIZE);
! 	}
! 
! 	/* Load the fmgr info into MbProcContext */
! 	oldcontext = MemoryContextSwitchTo(MbProcContext);
  	to_server = palloc(sizeof(FmgrInfo));
  	to_client = palloc(sizeof(FmgrInfo));
  	fmgr_info(to_server_proc, to_server);
***************
*** 146,158 ****
  	MemoryContextSwitchTo(oldcontext);
  
  	ClientEncoding = &pg_enc2name_tbl[encoding];
- 
- 	if (ToServerConvProc != NULL)
- 	{
- 		if (ToServerConvProc->fn_extra)
- 			pfree(ToServerConvProc->fn_extra);
- 		pfree(ToServerConvProc);
- 	}
  	ToServerConvProc = to_server;
  
  	if (ToClientConvProc != NULL)
--- 154,159 ----
***************
*** 154,166 ****
  		pfree(ToServerConvProc);
  	}
  	ToServerConvProc = to_server;
- 
- 	if (ToClientConvProc != NULL)
- 	{
- 		if (ToClientConvProc->fn_extra)
- 			pfree(ToClientConvProc->fn_extra);
- 		pfree(ToClientConvProc);
- 	}
  	ToClientConvProc = to_client;
  
  	return 0;
--- 155,160 ----
