Fix bgwriter's failure to release buffer pins and open files after an
authorTom Lane <[email protected]>
Thu, 8 Dec 2005 19:19:45 +0000 (19:19 +0000)
committerTom Lane <[email protected]>
Thu, 8 Dec 2005 19:19:45 +0000 (19:19 +0000)
error.  This probably explains bug #2099 and could also account for
mysterious VACUUM hangups.

src/backend/postmaster/bgwriter.c
src/backend/utils/resowner/resowner.c

index 568058a8861b73bea7e5c7f862de72c24c1015d7..a8c0a679ca70ededbabc580f809fc8c3b773ac90 100644 (file)
@@ -51,6 +51,7 @@
 #include "miscadmin.h"
 #include "postmaster/bgwriter.h"
 #include "storage/bufmgr.h"
+#include "storage/fd.h"
 #include "storage/freespace.h"
 #include "storage/ipc.h"
 #include "storage/pmsignal.h"
@@ -58,6 +59,7 @@
 #include "tcop/tcopprot.h"
 #include "utils/guc.h"
 #include "utils/memutils.h"
+#include "utils/resowner.h"
 
 
 /*----------
@@ -203,6 +205,12 @@ BackgroundWriterMain(void)
         */
        last_checkpoint_time = time(NULL);
 
+       /*
+        * Create a resource owner to keep track of our resources (currently
+        * only buffer pins).
+        */
+       CurrentResourceOwner = ResourceOwnerCreate(NULL, "Background Writer");
+
        /*
         * 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
@@ -235,11 +243,18 @@ BackgroundWriterMain(void)
                /*
                 * These operations are really just a minimal subset of
                 * AbortTransaction().  We don't have very many resources to worry
-                * about in bgwriter, but we do have LWLocks and buffers.
+                * about in bgwriter, but we do have LWLocks, buffers, and temp files.
                 */
                LWLockReleaseAll();
                AbortBufferIO();
                UnlockBuffers();
+               /* buffer pins are released here: */
+               ResourceOwnerRelease(CurrentResourceOwner,
+                                                        RESOURCE_RELEASE_BEFORE_LOCKS,
+                                                        false, true);
+               /* we needn't bother with the other ResourceOwnerRelease phases */
+               AtEOXact_Buffers(false);
+               AtEOXact_Files();
 
                /* Warn any waiting backends that the checkpoint failed. */
                if (ckpt_active)
index 13e90d6e7bf4227e4b6e5f910f6849e50cb91721..5b2ae5515a9ab3bb07833f780ce5f3400c4052f0 100644 (file)
@@ -455,7 +455,7 @@ UnregisterResourceReleaseCallback(ResourceReleaseCallback callback, void *arg)
  * of memory, it's critical to do so *before* acquiring the resource.
  *
  * We allow the case owner == NULL because the bufmgr is sometimes invoked
- * outside any transaction (for example, in the bgwriter).
+ * outside any transaction (for example, during WAL recovery).
  */
 void
 ResourceOwnerEnlargeBuffers(ResourceOwner owner)
@@ -488,7 +488,7 @@ ResourceOwnerEnlargeBuffers(ResourceOwner owner)
  * Caller must have previously done ResourceOwnerEnlargeBuffers()
  *
  * We allow the case owner == NULL because the bufmgr is sometimes invoked
- * outside any transaction (for example, in the bgwriter).
+ * outside any transaction (for example, during WAL recovery).
  */
 void
 ResourceOwnerRememberBuffer(ResourceOwner owner, Buffer buffer)
@@ -505,7 +505,7 @@ ResourceOwnerRememberBuffer(ResourceOwner owner, Buffer buffer)
  * Forget that a buffer pin is owned by a ResourceOwner
  *
  * We allow the case owner == NULL because the bufmgr is sometimes invoked
- * outside any transaction (for example, in the bgwriter).
+ * outside any transaction (for example, during WAL recovery).
  */
 void
 ResourceOwnerForgetBuffer(ResourceOwner owner, Buffer buffer)