Skip to content

Commit 005bee1

Browse files
hlinnakahackorum
authored andcommitted
Make RegisterTemporaryFile() set the 'fdstate' field, and rename it
In preparation for the next commit, which will move functions related to temporary file management to a separate source file, make RegisterTemporaryFile() set the 'tempFlags' (previously 'fdstate') and 'resowner' fields in the virtual File, instead of having the callers do it. That way, the callers don't need to know about the layout of the File struct. The 'fdstate' field was only used for flags related to temporary files. Rename it to 'tempFlags' for clarity.
1 parent 1cea429 commit 005bee1

1 file changed

Lines changed: 44 additions & 33 deletions

File tree

  • src/backend/storage/file

src/backend/storage/file/fd.c

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,14 @@ int io_direct_flags;
177177

178178
#define FileIsNotOpen(file) (VfdCache[file].fd == VFD_CLOSED)
179179

180-
/* these are the assigned bits in fdstate below: */
180+
/* these are the assigned bits in tempFlags below: */
181181
#define FD_DELETE_AT_CLOSE (1 << 0) /* T = delete when closed */
182182
#define FD_TEMP_FILE_LIMIT (1 << 1) /* T = respect temp_file_limit */
183183

184184
typedef struct vfd
185185
{
186186
int fd; /* current FD, or VFD_CLOSED if none */
187-
unsigned short fdstate; /* bitflags for VFD's state */
187+
unsigned short tempFlags; /* bitflags for temporary file VFDs */
188188
ResourceOwner resowner; /* owner, for automatic cleanup */
189189
File nextFree; /* link to next free VFD, if in freelist */
190190
File lruMoreRecently; /* doubly linked recency-of-use list */
@@ -746,7 +746,7 @@ LruDelete(File file)
746746
* to leak the FD than to mess up our internal state.
747747
*/
748748
if (close(vfdP->fd) != 0)
749-
elog(vfdP->fdstate & FD_TEMP_FILE_LIMIT ? LOG : data_sync_elevel(LOG),
749+
elog(vfdP->tempFlags & FD_TEMP_FILE_LIMIT ? LOG : data_sync_elevel(LOG),
750750
"could not close file \"%s\": %m", vfdP->fileName);
751751
vfdP->fd = VFD_CLOSED;
752752
--nfile;
@@ -927,7 +927,7 @@ FreeVfd(File file)
927927
free(vfdP->fileName);
928928
vfdP->fileName = NULL;
929929
}
930-
vfdP->fdstate = 0x0;
930+
vfdP->tempFlags = 0x0;
931931

932932
vfdP->nextFree = VfdCache[0].nextFree;
933933
VfdCache[0].nextFree = file;
@@ -985,15 +985,25 @@ ReportTemporaryFileUsage(const char *path, pgoff_t size)
985985
}
986986

987987
/*
988-
* Called to register a temporary file for automatic close.
989-
* ResourceOwnerEnlarge(CurrentResourceOwner) must have been called
990-
* before the file was opened.
988+
* Register a file as a temporary file.
989+
*
990+
* If 'resowner' is given, the file is registered to be automatically closed
991+
* by the resource owner. ResourceOwnerEnlarge(resowner) must have been
992+
* called before the file was opened.
993+
*
994+
* 'flags' indicate whether the file is to be deleted on close
995+
* (FD_DELETE_AT_CLOSE) and whether it counts towards the temp_file_limit
996+
* (FD_TEMP_FILE_LIMIT)
991997
*/
992998
static void
993-
RegisterTemporaryFile(File file)
999+
RegisterTemporaryFile(File file, ResourceOwner resowner, unsigned short flags)
9941000
{
995-
ResourceOwnerRememberFile(CurrentResourceOwner, file);
996-
VfdCache[file].resowner = CurrentResourceOwner;
1001+
if (resowner)
1002+
{
1003+
ResourceOwnerRememberFile(resowner, file);
1004+
VfdCache[file].resowner = resowner;
1005+
}
1006+
VfdCache[file].tempFlags |= flags;
9971007
}
9981008

9991009
/*
@@ -1079,7 +1089,7 @@ PathNameOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
10791089
vfdP->fileFlags = fileFlags & ~(O_CREAT | O_TRUNC | O_EXCL);
10801090
vfdP->fileMode = fileMode;
10811091
vfdP->fileSize = 0;
1082-
vfdP->fdstate = 0x0;
1092+
vfdP->tempFlags = 0x0;
10831093
vfdP->resowner = NULL;
10841094

10851095
Insert(file);
@@ -1143,12 +1153,13 @@ OpenTemporaryFile(bool interXact)
11431153
DEFAULTTABLESPACE_OID,
11441154
true);
11451155

1146-
/* Mark it for deletion at close and temporary file size limit */
1147-
VfdCache[file].fdstate |= FD_DELETE_AT_CLOSE | FD_TEMP_FILE_LIMIT;
1148-
1149-
/* Register it with the current resource owner */
1150-
if (!interXact)
1151-
RegisterTemporaryFile(file);
1156+
/*
1157+
* Register it with the current resource owner (unless it's a cross-xact
1158+
* temp file), and mark for deletion at close and temp_file_limit
1159+
* accounting.
1160+
*/
1161+
RegisterTemporaryFile(file, interXact ? NULL : CurrentResourceOwner,
1162+
FD_DELETE_AT_CLOSE | FD_TEMP_FILE_LIMIT);
11521163

11531164
return file;
11541165
}
@@ -1263,11 +1274,8 @@ PathNameCreateTemporaryFile(const char *path, bool error_on_failure)
12631274
return file;
12641275
}
12651276

1266-
/* Mark it for temp_file_limit accounting. */
1267-
VfdCache[file].fdstate |= FD_TEMP_FILE_LIMIT;
1268-
1269-
/* Register it for automatic close. */
1270-
RegisterTemporaryFile(file);
1277+
/* Register it for automatic close and temp_file_limit accounting */
1278+
RegisterTemporaryFile(file, CurrentResourceOwner, FD_TEMP_FILE_LIMIT);
12711279

12721280
return file;
12731281
}
@@ -1298,8 +1306,11 @@ PathNameOpenTemporaryFile(const char *path, int mode)
12981306

12991307
if (file > 0)
13001308
{
1301-
/* Register it for automatic close. */
1302-
RegisterTemporaryFile(file);
1309+
/*
1310+
* Register it for automatic close (but no delete-at-close or
1311+
* temp_file_limit accounting)
1312+
*/
1313+
RegisterTemporaryFile(file, CurrentResourceOwner, 0);
13031314
}
13041315

13051316
return file;
@@ -1378,7 +1389,7 @@ FileClose(File file)
13781389
* We may need to panic on failure to close non-temporary files;
13791390
* see LruDelete.
13801391
*/
1381-
elog(vfdP->fdstate & FD_TEMP_FILE_LIMIT ? LOG : data_sync_elevel(LOG),
1392+
elog(vfdP->tempFlags & FD_TEMP_FILE_LIMIT ? LOG : data_sync_elevel(LOG),
13821393
"could not close file \"%s\": %m", vfdP->fileName);
13831394
}
13841395

@@ -1389,7 +1400,7 @@ FileClose(File file)
13891400
Delete(file);
13901401
}
13911402

1392-
if (vfdP->fdstate & FD_TEMP_FILE_LIMIT)
1403+
if (vfdP->tempFlags & FD_TEMP_FILE_LIMIT)
13931404
{
13941405
/* Subtract its size from current usage (do first in case of error) */
13951406
temporary_files_size -= vfdP->fileSize;
@@ -1399,7 +1410,7 @@ FileClose(File file)
13991410
/*
14001411
* Delete the file if it was temporary, and make a log entry if wanted
14011412
*/
1402-
if (vfdP->fdstate & FD_DELETE_AT_CLOSE)
1413+
if (vfdP->tempFlags & FD_DELETE_AT_CLOSE)
14031414
{
14041415
struct stat filestats;
14051416
int stat_errno;
@@ -1411,7 +1422,7 @@ FileClose(File file)
14111422
* is arranged to ensure that the worst-case consequence is failing to
14121423
* emit log message(s), not failing to attempt the unlink.
14131424
*/
1414-
vfdP->fdstate &= ~FD_DELETE_AT_CLOSE;
1425+
vfdP->tempFlags &= ~FD_DELETE_AT_CLOSE;
14151426

14161427

14171428
/* first try the stat() */
@@ -1648,7 +1659,7 @@ FileWriteV(File file, const struct iovec *iov, int iovcnt, pgoff_t offset,
16481659
* message if we do that. All current callers would just throw error
16491660
* immediately anyway, so this is safe at present.
16501661
*/
1651-
if (temp_file_limit >= 0 && (vfdP->fdstate & FD_TEMP_FILE_LIMIT))
1662+
if (temp_file_limit >= 0 && (vfdP->tempFlags & FD_TEMP_FILE_LIMIT))
16521663
{
16531664
pgoff_t past_write = offset;
16541665

@@ -1687,7 +1698,7 @@ FileWriteV(File file, const struct iovec *iov, int iovcnt, pgoff_t offset,
16871698
/*
16881699
* Maintain fileSize and temporary_files_size if it's a temp file.
16891700
*/
1690-
if (vfdP->fdstate & FD_TEMP_FILE_LIMIT)
1701+
if (vfdP->tempFlags & FD_TEMP_FILE_LIMIT)
16911702
{
16921703
pgoff_t past_write = offset + returnCode;
16931704

@@ -1875,7 +1886,7 @@ FileTruncate(File file, pgoff_t offset, uint32 wait_event_info)
18751886
if (returnCode == 0 && VfdCache[file].fileSize > offset)
18761887
{
18771888
/* adjust our state for truncation of a temp file */
1878-
Assert(VfdCache[file].fdstate & FD_TEMP_FILE_LIMIT);
1889+
Assert(VfdCache[file].tempFlags & FD_TEMP_FILE_LIMIT);
18791890
temporary_files_size -= VfdCache[file].fileSize - offset;
18801891
VfdCache[file].fileSize = offset;
18811892
}
@@ -2648,9 +2659,9 @@ CleanupTempFiles(bool isCommit, bool isProcExit)
26482659
Assert(FileIsNotOpen(0)); /* Make sure ring not corrupted */
26492660
for (i = 1; i < SizeVfdCache; i++)
26502661
{
2651-
unsigned short fdstate = VfdCache[i].fdstate;
2662+
unsigned short tempFlags = VfdCache[i].tempFlags;
26522663

2653-
if ((fdstate & FD_DELETE_AT_CLOSE) && VfdCache[i].fileName != NULL)
2664+
if ((tempFlags & FD_DELETE_AT_CLOSE) && VfdCache[i].fileName != NULL)
26542665
FileClose(i);
26552666
}
26562667
}

0 commit comments

Comments
 (0)