Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit bffd1ce

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 45f64f1 commit bffd1ce

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/backend/storage/file/buffile.c

+22-3
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
@@ -38,6 +40,7 @@
3840
#include "storage/fd.h"
3941
#include "storage/buffile.h"
4042
#include "storage/buf_internals.h"
43+
#include "utils/resowner.h"
4144

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

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

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

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

0 commit comments

Comments
 (0)