]> granicus.if.org Git - postgresql/commitdiff
Fix two violations of the ResourceOwnerEnlarge/Remember protocol.
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 8 Nov 2017 21:50:12 +0000 (16:50 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 8 Nov 2017 21:50:12 +0000 (16:50 -0500)
The point of having separate ResourceOwnerEnlargeFoo and
ResourceOwnerRememberFoo functions is so that resource allocation
can happen in between.  Doing it in some other order is just wrong.

OpenTemporaryFile() did open(), enlarge, remember, which would leak the
open file if the enlarge step ran out of memory.  Because fd.c has its own
layer of resource-remembering, the consequences look like they'd be limited
to an intratransaction FD leak, but it's still not good.

IncrBufferRefCount() did enlarge, remember, incr-refcount, which would blow
up if the incr-refcount step ever failed.  It was safe enough when written,
but since the introduction of PrivateRefCountHash, I think the assumption
that no error could happen there is pretty shaky.

The odds of real problems from either bug are probably small, but still,
back-patch to supported branches.

Thomas Munro and Tom Lane, per a comment from Andres Freund

src/backend/storage/buffer/bufmgr.c
src/backend/storage/file/fd.c

index 572f413d6e49e08aaf23e2b7254827d7bbb521a3..26df7cb38fe6580aed00a7112e27231836208091 100644 (file)
@@ -3348,7 +3348,6 @@ IncrBufferRefCount(Buffer buffer)
 {
        Assert(BufferIsPinned(buffer));
        ResourceOwnerEnlargeBuffers(CurrentResourceOwner);
-       ResourceOwnerRememberBuffer(CurrentResourceOwner, buffer);
        if (BufferIsLocal(buffer))
                LocalRefCount[-buffer - 1]++;
        else
@@ -3359,6 +3358,7 @@ IncrBufferRefCount(Buffer buffer)
                Assert(ref != NULL);
                ref->refcount++;
        }
+       ResourceOwnerRememberBuffer(CurrentResourceOwner, buffer);
 }
 
 /*
index 3849bfb15d93160386e5d69bd34f28350fbcf725..aa2fe2c6c04d6c86c19f0607f0ef053f2291b8dd 100644 (file)
@@ -1397,6 +1397,13 @@ OpenTemporaryFile(bool interXact)
 {
        File            file = 0;
 
+       /*
+        * Make sure the current resource owner has space for this File before we
+        * open it, if we'll be registering it below.
+        */
+       if (!interXact)
+               ResourceOwnerEnlargeFiles(CurrentResourceOwner);
+
        /*
         * If some temp tablespace(s) have been given to us, try to use the next
         * one.  If a given tablespace can't be found, we silently fall back to
@@ -1433,9 +1440,8 @@ OpenTemporaryFile(bool interXact)
        {
                VfdCache[file].fdstate |= FD_XACT_TEMPORARY;
 
-               ResourceOwnerEnlargeFiles(CurrentResourceOwner);
-               ResourceOwnerRememberFile(CurrentResourceOwner, file);
                VfdCache[file].resowner = CurrentResourceOwner;
+               ResourceOwnerRememberFile(CurrentResourceOwner, file);
 
                /* ensure cleanup happens at eoxact */
                have_xact_temporary_files = true;