Skip to content

Commit 1786cda

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 7a15fe5 commit 1786cda

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
@@ -2629,11 +2629,11 @@ IncrBufferRefCount(Buffer buffer)
26292629
{
26302630
Assert(BufferIsPinned(buffer));
26312631
ResourceOwnerEnlargeBuffers(CurrentResourceOwner);
2632-
ResourceOwnerRememberBuffer(CurrentResourceOwner, buffer);
26332632
if (BufferIsLocal(buffer))
26342633
LocalRefCount[-buffer - 1]++;
26352634
else
26362635
PrivateRefCount[buffer - 1]++;
2636+
ResourceOwnerRememberBuffer(CurrentResourceOwner, buffer);
26372637
}
26382638

26392639
/*

src/backend/storage/file/fd.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,13 @@ OpenTemporaryFile(bool interXact)
11931193
{
11941194
File file = 0;
11951195

1196+
/*
1197+
* Make sure the current resource owner has space for this File before we
1198+
* open it, if we'll be registering it below.
1199+
*/
1200+
if (!interXact)
1201+
ResourceOwnerEnlargeFiles(CurrentResourceOwner);
1202+
11961203
/*
11971204
* If some temp tablespace(s) have been given to us, try to use the next
11981205
* one. If a given tablespace can't be found, we silently fall back to
@@ -1229,9 +1236,8 @@ OpenTemporaryFile(bool interXact)
12291236
{
12301237
VfdCache[file].fdstate |= FD_XACT_TEMPORARY;
12311238

1232-
ResourceOwnerEnlargeFiles(CurrentResourceOwner);
1233-
ResourceOwnerRememberFile(CurrentResourceOwner, file);
12341239
VfdCache[file].resowner = CurrentResourceOwner;
1240+
ResourceOwnerRememberFile(CurrentResourceOwner, file);
12351241

12361242
/* ensure cleanup happens at eoxact */
12371243
have_xact_temporary_files = true;

0 commit comments

Comments
 (0)