summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane2005-09-12 22:20:30 +0000
committerTom Lane2005-09-12 22:20:30 +0000
commit8080e8e8cea37507394a2bff43c1dbd9b0a437a4 (patch)
tree8831235abf19b21fd85d744ad9fc8482c1da98cd
parent3926b6e9c31ab247cb8a2f2efcd1733da6d96bea (diff)
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.
-rw-r--r--src/backend/postmaster/bgwriter.c21
1 files changed, 19 insertions, 2 deletions
diff --git a/src/backend/postmaster/bgwriter.c b/src/backend/postmaster/bgwriter.c
index 85c63960a24..353d43b8eba 100644
--- a/src/backend/postmaster/bgwriter.c
+++ b/src/backend/postmaster/bgwriter.c
@@ -37,7 +37,7 @@
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/postmaster/bgwriter.c,v 1.13.4.1 2005/02/19 23:16:27 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/postmaster/bgwriter.c,v 1.13.4.2 2005/09/12 22:20:30 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -155,6 +155,7 @@ void
BackgroundWriterMain(void)
{
sigjmp_buf local_sigjmp_buf;
+ MemoryContext bgwriter_context;
Assert(BgWriterShmem != NULL);
BgWriterShmem->bgwriter_pid = MyProcPid;
@@ -203,6 +204,19 @@ BackgroundWriterMain(void)
last_checkpoint_time = time(NULL);
/*
+ * Create a memory context that we will do all our work in. We do this
+ * so that we can reset the context during error recovery and thereby
+ * avoid possible memory leaks. Formerly this code just ran in
+ * TopMemoryContext, but resetting that would be a really bad idea.
+ */
+ bgwriter_context = AllocSetContextCreate(TopMemoryContext,
+ "Background Writer",
+ ALLOCSET_DEFAULT_MINSIZE,
+ ALLOCSET_DEFAULT_INITSIZE,
+ ALLOCSET_DEFAULT_MAXSIZE);
+ MemoryContextSwitchTo(bgwriter_context);
+
+ /*
* If an exception is encountered, processing resumes here.
*
* See notes in postgres.c about the design of this coding.
@@ -242,9 +256,12 @@ BackgroundWriterMain(void)
* Now return to normal top-level context and clear ErrorContext
* for next time.
*/
- MemoryContextSwitchTo(TopMemoryContext);
+ MemoryContextSwitchTo(bgwriter_context);
FlushErrorState();
+ /* Flush any leaked data in the top-level context */
+ MemoryContextResetAndDeleteChildren(bgwriter_context);
+
/* Now we can allow interrupts again */
RESUME_INTERRUPTS();