diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2017-11-08 16:50:12 -0500 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2017-11-08 16:50:12 -0500 |
commit | c5269472ea9bb4a6fbb8a0510f7d676d725933ab (patch) | |
tree | 8b6ca7067c88b8b033e7cbc5b5e6b893303dce6a /src | |
parent | 2eb4a831e5fb5d8fc17e13aea56e04af3efe27b4 (diff) | |
download | postgresql-c5269472ea9bb4a6fbb8a0510f7d676d725933ab.tar.gz postgresql-c5269472ea9bb4a6fbb8a0510f7d676d725933ab.zip |
Fix two violations of the ResourceOwnerEnlarge/Remember protocol.
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
Diffstat (limited to 'src')
-rw-r--r-- | src/backend/storage/buffer/bufmgr.c | 2 | ||||
-rw-r--r-- | src/backend/storage/file/fd.c | 10 |
2 files changed, 9 insertions, 3 deletions
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 572f413d6e4..26df7cb38fe 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -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); } /* diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c index 3849bfb15d9..aa2fe2c6c04 100644 --- a/src/backend/storage/file/fd.c +++ b/src/backend/storage/file/fd.c @@ -1398,6 +1398,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 * the database's default tablespace. @@ -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; |