Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f59b05c

Browse files
committed
Ensure that any memory leaked during an error inside the bgwriter is
recovered. I did not see any actual leak while testing this in CVS tip, but 8.0 definitely has a problem with leaking the space temporarily palloc'd by BufferSync(). In any case this seems a good idea to forestall similar problems in future. Per report from Arjen van der Meijden.
1 parent 9813fc1 commit f59b05c

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/backend/postmaster/bgwriter.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*
3838
*
3939
* 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 $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -160,6 +160,7 @@ void
160160
BackgroundWriterMain(void)
161161
{
162162
sigjmp_buf local_sigjmp_buf;
163+
MemoryContext bgwriter_context;
163164

164165
Assert(BgWriterShmem != NULL);
165166
BgWriterShmem->bgwriter_pid = MyProcPid;
@@ -207,6 +208,19 @@ BackgroundWriterMain(void)
207208
*/
208209
last_checkpoint_time = time(NULL);
209210

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+
210224
/*
211225
* If an exception is encountered, processing resumes here.
212226
*
@@ -247,9 +261,12 @@ BackgroundWriterMain(void)
247261
* Now return to normal top-level context and clear ErrorContext
248262
* for next time.
249263
*/
250-
MemoryContextSwitchTo(TopMemoryContext);
264+
MemoryContextSwitchTo(bgwriter_context);
251265
FlushErrorState();
252266

267+
/* Flush any leaked data in the top-level context */
268+
MemoryContextResetAndDeleteChildren(bgwriter_context);
269+
253270
/* Now we can allow interrupts again */
254271
RESUME_INTERRUPTS();
255272

0 commit comments

Comments
 (0)