4
4
* (currently mule internal code (mic) is used)
5
5
* Tatsuo Ishii
6
6
*
7
- * $PostgreSQL: pgsql/src/backend/utils/mb/mbutils.c,v 1.54 2006/01/11 08:43:12 neilc Exp $
7
+ * $PostgreSQL: pgsql/src/backend/utils/mb/mbutils.c,v 1.55 2006/01/12 22:04:02 neilc Exp $
8
8
*/
9
9
#include "postgres.h"
10
10
11
11
#include "access/xact.h"
12
+ #include "catalog/namespace.h"
12
13
#include "miscadmin.h"
13
14
#include "mb/pg_wchar.h"
14
15
#include "utils/builtins.h"
15
16
#include "utils/memutils.h"
16
17
#include "utils/syscache.h"
17
- #include "catalog/namespace.h"
18
18
19
19
/*
20
20
* We handle for actual FE and BE encoding setting encoding-identificator
@@ -25,10 +25,12 @@ static pg_enc2name *ClientEncoding = &pg_enc2name_tbl[PG_SQL_ASCII];
25
25
static pg_enc2name * DatabaseEncoding = & pg_enc2name_tbl [PG_SQL_ASCII ];
26
26
27
27
/*
28
- * Caches for conversion function info. Note that these values are
29
- * allocated in TopMemoryContext so that they survive across
30
- * transactions. See SetClientEncoding() for more details.
28
+ * Caches for conversion function info. These values are allocated in
29
+ * MbProcContext. That context is a child of TopMemoryContext,
30
+ * which allows these values to survive across transactions. See
31
+ * SetClientEncoding() for more details.
31
32
*/
33
+ static MemoryContext MbProcContext = NULL ;
32
34
static FmgrInfo * ToServerConvProc = NULL ;
33
35
static FmgrInfo * ToClientConvProc = NULL ;
34
36
@@ -86,22 +88,10 @@ SetClientEncoding(int encoding, bool doit)
86
88
if (doit )
87
89
{
88
90
ClientEncoding = & pg_enc2name_tbl [encoding ];
89
-
90
- if (ToServerConvProc != NULL )
91
- {
92
- if (ToServerConvProc -> fn_extra )
93
- pfree (ToServerConvProc -> fn_extra );
94
- pfree (ToServerConvProc );
95
- }
96
91
ToServerConvProc = NULL ;
97
-
98
- if (ToClientConvProc != NULL )
99
- {
100
- if (ToClientConvProc -> fn_extra )
101
- pfree (ToClientConvProc -> fn_extra );
102
- pfree (ToClientConvProc );
103
- }
104
92
ToClientConvProc = NULL ;
93
+ if (MbProcContext )
94
+ MemoryContextReset (MbProcContext );
105
95
}
106
96
return 0 ;
107
97
}
@@ -134,33 +124,37 @@ SetClientEncoding(int encoding, bool doit)
134
124
if (!doit )
135
125
return 0 ;
136
126
137
- /*
138
- * load the fmgr info into TopMemoryContext so that it survives outside
139
- * transaction.
140
- */
141
- oldcontext = MemoryContextSwitchTo (TopMemoryContext );
127
+ /* Before loading the new fmgr info, remove the old info, if any */
128
+ ToServerConvProc = NULL ;
129
+ ToClientConvProc = NULL ;
130
+ if (MbProcContext != NULL )
131
+ {
132
+ MemoryContextReset (MbProcContext );
133
+ }
134
+ else
135
+ {
136
+ /*
137
+ * This is the first time through, so create the context. Make
138
+ * it a child of TopMemoryContext so that these values survive
139
+ * across transactions.
140
+ */
141
+ MbProcContext = AllocSetContextCreate (TopMemoryContext ,
142
+ "MbProcContext" ,
143
+ ALLOCSET_SMALL_MINSIZE ,
144
+ ALLOCSET_SMALL_INITSIZE ,
145
+ ALLOCSET_SMALL_MAXSIZE );
146
+ }
147
+
148
+ /* Load the fmgr info into MbProcContext */
149
+ oldcontext = MemoryContextSwitchTo (MbProcContext );
142
150
to_server = palloc (sizeof (FmgrInfo ));
143
151
to_client = palloc (sizeof (FmgrInfo ));
144
152
fmgr_info (to_server_proc , to_server );
145
153
fmgr_info (to_client_proc , to_client );
146
154
MemoryContextSwitchTo (oldcontext );
147
155
148
156
ClientEncoding = & pg_enc2name_tbl [encoding ];
149
-
150
- if (ToServerConvProc != NULL )
151
- {
152
- if (ToServerConvProc -> fn_extra )
153
- pfree (ToServerConvProc -> fn_extra );
154
- pfree (ToServerConvProc );
155
- }
156
157
ToServerConvProc = to_server ;
157
-
158
- if (ToClientConvProc != NULL )
159
- {
160
- if (ToClientConvProc -> fn_extra )
161
- pfree (ToClientConvProc -> fn_extra );
162
- pfree (ToClientConvProc );
163
- }
164
158
ToClientConvProc = to_client ;
165
159
166
160
return 0 ;
0 commit comments