10000 Fix two violations of the ResourceOwnerEnlarge/Remember protocol. · koderP/postgres@d7f5934 · GitHub
[go: up one dir, main page]

Skip to content

Commit d7f5934

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 941602d commit d7f5934

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
@@ -2999,7 +2999,6 @@ IncrBufferRefCount(Buffer buffer)
29992999
{
30003000
Assert(BufferIsPinned(buffer));
30013001
ResourceOwnerEnlargeBuffers(CurrentResourceOwner);
3002-
ResourceOwnerRememberBuffer(CurrentResourceOwner, buffer);
30033002
if (BufferIsLocal(buffer))
30043003
LocalRefCount[-buffer - 1]++;
30053004
else
@@ -3010,6 +3009,7 @@ IncrBufferRefCount(Buffer buffer)
30103009
Assert(ref != NULL);
30113010
ref->refcount++;
30123011
}
3012+
ResourceOwnerRememberBuffer(CurrentResourceOwner, buffer);
30133013
}
30143014

30153015
/*

src/backend/storage/file/fd.c

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

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

1233-
ResourceOwnerEnlargeFiles(CurrentResourceOwner);
1234-
ResourceOwnerRememberFile(CurrentResourceOwner, file);
12351240
VfdCache[file].resowner = CurrentResourceOwner;
1241+
ResourceOwnerRememberFile(CurrentResourceOwner, file);
12361242

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

0 commit comments

Comments
 (0)
0