Skip to content

Commit b89ad38

Browse files
committed
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
1 parent 1a93c25 commit b89ad38

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/backend/storage/buffer/bufmgr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3348,7 +3348,6 @@ IncrBufferRefCount(Buffer buffer)
33483348
{
33493349
Assert(BufferIsPinned(buffer));
33503350
ResourceOwnerEnlargeBuffers(CurrentResourceOwner);
3351-
ResourceOwnerRememberBuffer(CurrentResourceOwner, buffer);
33523351
if (BufferIsLocal(buffer))
33533352
LocalRefCount[-buffer - 1]++;
33543353
else
@@ -3359,6 +3358,7 @@ IncrBufferRefCount(Buffer buffer)
33593358
Assert(ref != NULL);
33603359
ref->refcount++;
33613360
}
3361+
ResourceOwnerRememberBuffer(CurrentResourceOwner, buffer);
33623362
}
33633363

33643364
/*

src/backend/storage/file/fd.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,13 @@ OpenTemporaryFile(bool interXact)
13351335
{
13361336
File file = 0;
13371337

1338+
/*
1339+
* Make sure the current resource owner has space for this File before we
1340+
* open it, if we'll be registering it below.
1341+
*/
1342+
if (!interXact)
1343+
ResourceOwnerEnlargeFiles(CurrentResourceOwner);
1344+
13381345
/*
13391346
* If some temp tablespace(s) have been given to us, try to use the next
13401347
* one. If a given tablespace can't be found, we silently fall back to
@@ -1371,9 +1378,8 @@ OpenTemporaryFile(bool interXact)
13711378
{
13721379
VfdCache[file].fdstate |= FD_XACT_TEMPORARY;
13731380

1374-
ResourceOwnerEnlargeFiles(CurrentResourceOwner);
1375-
ResourceOwnerRememberFile(CurrentResourceOwner, file);
13761381
VfdCache[file].resowner = CurrentResourceOwner;
1382+
ResourceOwnerRememberFile(CurrentResourceOwner, file);
13771383

13781384
/* ensure cleanup happens at eoxact */
13791385
have_xact_temporary_files = true;

0 commit comments

Comments
 (0)