Skip to content

Commit c526947

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 2eb4a83 commit c526947

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
@@ -1397,6 +1397,13 @@ OpenTemporaryFile(bool interXact)
13971397
{
13981398
File file = 0;
13991399

1400+
/*
1401+
* Make sure the current resource owner has space for this File before we
1402+
* open it, if we'll be registering it below.
1403+
*/
1404+
if (!interXact)
1405+
ResourceOwnerEnlargeFiles(CurrentResourceOwner);
1406+
14001407
/*
14011408
* If some temp tablespace(s) have been given to us, try to use the next
14021409
* one. If a given tablespace can't be found, we silently fall back to
@@ -1433,9 +1440,8 @@ OpenTemporaryFile(bool interXact)
14331440
{
14341441
VfdCache[file].fdstate |= FD_XACT_TEMPORARY;
14351442

1436-
ResourceOwnerEnlargeFiles(CurrentResourceOwner);
1437-
ResourceOwnerRememberFile(CurrentResourceOwner, file);
14381443
VfdCache[file].resowner = CurrentResourceOwner;
1444+
ResourceOwnerRememberFile(CurrentResourceOwner, file);
14391445

14401446
/* ensure cleanup happens at eoxact */
14411447
have_xact_temporary_files = true;

0 commit comments

Comments
 (0)