Skip to content

Commit d74eaf3

Browse files
committed
Ensure all files created for a single BufFile have the same resource owner.
Callers expect that they only have to set the right resource owner when creating a BufFile, not during subsequent operations on it. While we could insist this be fixed at the caller level, it seems more sensible for the BufFile to take care of it. Without this, some temp files belonging to a BufFile can go away too soon, eg at the end of a subtransaction, leading to errors or crashes. Reported and fixed by Andres Freund. Back-patch to all active branches.
1 parent 01499ea commit d74eaf3

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/backend/storage/file/buffile.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
* will go away automatically at transaction end. If the underlying
2424
* virtual File is made with OpenTemporaryFile, then all resources for
2525
* the file are certain to be cleaned up even if processing is aborted
26-
* by ereport(ERROR). To avoid confusion, the caller should take care that
27-
* all calls for a single BufFile are made in the same palloc context.
26+
* by ereport(ERROR). The data structures required are made in the
27+
* palloc context that was current when the BufFile was created, and
28+
* any external resources such as temp files are owned by the ResourceOwner
29+
* that was current at that time.
2830
*
2931
* BufFile also supports temporary files that exceed the OS file size limit
3032
* (by opening multiple fd.c temporary files). This is an essential feature
@@ -37,6 +39,7 @@
3739
#include "storage/fd.h"
3840
#include "storage/buffile.h"
3941
#include "storage/buf_internals.h"
42+
#include "utils/resowner.h"
4043

4144
/*
4245
* We break BufFiles into gigabyte-sized segments, regardless of RELSEG_SIZE.
@@ -67,6 +70,13 @@ struct BufFile
6770
bool isInterXact; /* keep open over transactions? */
6871
bool dirty; /* does buffer need to be written? */
6972

73+
/*
74+
* resowner is the ResourceOwner to use for underlying temp files. (We
75+
* don't need to remember the memory context we're using explicitly,
76+
* because after creation we only repalloc our arrays larger.)
77+
*/
78+
ResourceOwner resowner;
79+
7080
/*
7181
* "current pos" is position of start of buffer within the logical file.
7282
* Position as seen by user of BufFile is (curFile, curOffset + pos).
@@ -102,6 +112,7 @@ makeBufFile(File firstfile)
102112
file->isTemp = false;
103113
file->isInterXact = false;
104114
file->dirty = false;
115+
file->resowner = CurrentResourceOwner;
105116
file->curFile = 0;
106117
file->curOffset = 0L;
107118
file->pos = 0;
@@ -117,11 +128,18 @@ static void
117128
extendBufFile(BufFile *file)
118129
{
119130
File pfile;
131+
ResourceOwner oldowner;
132+
133+
/* Be sure to associate the file with the BufFile's resource owner */
134+
oldowner = CurrentResourceOwner;
135+
CurrentResourceOwner = file->resowner;
120136

121137
Assert(file->isTemp);
122138
pfile = OpenTemporaryFile(file->isInterXact);
123139
Assert(pfile >= 0);
124140

141+
CurrentResourceOwner = oldowner;
142+
125143
file->files = (File *) repalloc(file->files,
126144
(file->numFiles + 1) * sizeof(File));
127145
file->offsets = (off_t *) repalloc(file->offsets,
@@ -140,7 +158,8 @@ extendBufFile(BufFile *file)
140158
* at end of transaction.
141159
*
142160
* Note: if interXact is true, the caller had better be calling us in a
143-
* memory context that will survive across transaction boundaries.
161+
* memory context, and with a resource owner, that will survive across
162+
* transaction boundaries.
144163
*/
145164
BufFile *
146165
BufFileCreateTemp(bool interXact)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy