|
37 | 37 | *
|
38 | 38 | *
|
39 | 39 | * IDENTIFICATION
|
40 |
| - * $PostgreSQL: pgsql/src/backend/postmaster/bgwriter.c,v 1.19 2005/08/20 23:26:17 tgl Exp $ |
| 40 | + * $PostgreSQL: pgsql/src/backend/postmaster/bgwriter.c,v 1.20 2005/09/12 22:20:16 tgl Exp $ |
41 | 41 | *
|
42 | 42 | *-------------------------------------------------------------------------
|
43 | 43 | */
|
|
160 | 160 | BackgroundWriterMain(void)
|
161 | 161 | {
|
162 | 162 | sigjmp_buf local_sigjmp_buf;
|
| 163 | + MemoryContext bgwriter_context; |
163 | 164 |
|
164 | 165 | Assert(BgWriterShmem != NULL);
|
165 | 166 | BgWriterShmem->bgwriter_pid = MyProcPid;
|
@@ -207,6 +208,19 @@ BackgroundWriterMain(void)
|
207 | 208 | */
|
208 | 209 | last_checkpoint_time = time(NULL);
|
209 | 210 |
|
| 211 | + /* |
| 212 | + * Create a memory context that we will do all our work in. We do this |
| 213 | + * so that we can reset the context during error recovery and thereby |
| 214 | + * avoid possible memory leaks. Formerly this code just ran in |
| 215 | + * TopMemoryContext, but resetting that would be a really bad idea. |
| 216 | + */ |
| 217 | + bgwriter_context = AllocSetContextCreate(TopMemoryContext, |
| 218 | + "Background Writer", |
| 219 | + ALLOCSET_DEFAULT_MINSIZE, |
| 220 | + ALLOCSET_DEFAULT_INITSIZE, |
| 221 | + ALLOCSET_DEFAULT_MAXSIZE); |
| 222 | + MemoryContextSwitchTo(bgwriter_context); |
| 223 | + |
210 | 224 | /*
|
211 | 225 | * If an exception is encountered, processing resumes here.
|
212 | 226 | *
|
@@ -247,9 +261,12 @@ BackgroundWriterMain(void)
|
247 | 261 | * Now return to normal top-level context and clear ErrorContext
|
248 | 262 | * for next time.
|
249 | 263 | */
|
250 |
| - MemoryContextSwitchTo(TopMemoryContext); |
| 264 | + MemoryContextSwitchTo(bgwriter_context); |
251 | 265 | FlushErrorState();
|
252 | 266 |
|
| 267 | + /* Flush any leaked data in the top-level context */ |
| 268 | + MemoryContextResetAndDeleteChildren(bgwriter_context); |
| 269 | + |
253 | 270 | /* Now we can allow interrupts again */
|
254 | 271 | RESUME_INTERRUPTS();
|
255 | 272 |
|
|
0 commit comments