Skip to content

Commit 1b53132

Browse files
Haoyu Huangtristan957
authored andcommitted
Add global temp file size limit
Ingestion is causing PG to run out of disk space due to temp files. Maintain a global temp file size and error out the statement if size limit is reached.
1 parent e7cdfe2 commit 1b53132

File tree

2 files changed

+35
-0
lines changed
  • src
    • backend/storage/file
    • include/storage

2 files changed

+35
-0
lines changed

src/backend/storage/file/fd.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ int recovery_init_sync_method = DATA_DIR_SYNC_METHOD_FSYNC;
167167
/* Which kinds of files should be opened with PG_O_DIRECT. */
168168
int io_direct_flags;
169169

170+
CheckTempFileSize_hook_type CheckTempFileSize_hook = NULL;
171+
170172
/* Debugging.... */
171173

172174
#ifdef FDDEBUG
@@ -2010,6 +2012,10 @@ FileClose(File file)
20102012
{
20112013
/* Subtract its size from current usage (do first in case of error) */
20122014
temporary_files_size -= vfdP->fileSize;
2015+
if (CheckTempFileSize_hook != NULL)
2016+
{
2017+
CheckTempFileSize_hook(vfdP->fileSize, PG_TEMP_FILES_SIZE_DEC);
2018+
}
20132019
vfdP->fileSize = 0;
20142020
}
20152021

@@ -2233,6 +2239,10 @@ FileWriteV(File file, const struct iovec *iov, int iovcnt, off_t offset,
22332239
(errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
22342240
errmsg("temporary file size exceeds temp_file_limit (%dkB)",
22352241
temp_file_limit)));
2242+
if (CheckTempFileSize_hook != NULL)
2243+
{
2244+
CheckTempFileSize_hook(past_write - vfdP->fileSize, PG_TEMP_FILES_SIZE_CHECK);
2245+
}
22362246
}
22372247
}
22382248

@@ -2262,6 +2272,10 @@ FileWriteV(File file, const struct iovec *iov, int iovcnt, off_t offset,
22622272
if (past_write > vfdP->fileSize)
22632273
{
22642274
temporary_files_size += past_write - vfdP->fileSize;
2275+
if (CheckTempFileSize_hook != NULL)
2276+
{
2277+
CheckTempFileSize_hook(past_write - vfdP->fileSize, PG_TEMP_FILES_SIZE_INC);
2278+
}
22652279
vfdP->fileSize = past_write;
22662280
}
22672281
}
@@ -2445,6 +2459,10 @@ FileTruncate(File file, off_t offset, uint32 wait_event_info)
24452459
/* adjust our state for truncation of a temp file */
24462460
Assert(VfdCache[file].fdstate & FD_TEMP_FILE_LIMIT);
24472461
temporary_files_size -= VfdCache[file].fileSize - offset;
2462+
if (CheckTempFileSize_hook != NULL)
2463+
{
2464+
CheckTempFileSize_hook(VfdCache[file].fileSize - offset, PG_TEMP_FILES_SIZE_DEC);
2465+
}
24482466
VfdCache[file].fileSize = offset;
24492467
}
24502468

@@ -3184,6 +3202,11 @@ BeforeShmemExit_Files(int code, Datum arg)
31843202
#ifdef USE_ASSERT_CHECKING
31853203
temporary_files_allowed = false;
31863204
#endif
3205+
3206+
if (CheckTempFileSize_hook != NULL)
3207+
{
3208+
CheckTempFileSize_hook(temporary_files_size, PG_TEMP_FILES_SIZE_DEC);
3209+
}
31873210
}
31883211

31893212
/*

src/include/storage/fd.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,16 @@ FileWrite(File file, const void *buffer, size_t amount, off_t offset,
216216
return FileWriteV(file, &iov, 1, offset, wait_event_info);
217217
}
218218

219+
/* BEGIN_HADRON */
220+
#define PG_TEMP_FILES_SIZE_INC 0
221+
#define PG_TEMP_FILES_SIZE_DEC 1
222+
#define PG_TEMP_FILES_SIZE_CHECK 2
223+
typedef void (*CheckTempFileSize_hook_type) (off_t size, int action);
224+
extern PGDLLIMPORT CheckTempFileSize_hook_type CheckTempFileSize_hook;
225+
/* END_HADRON */
226+
227+
/* Filename components */
228+
#define PG_TEMP_FILES_DIR "pgsql_tmp"
229+
#define PG_TEMP_FILE_PREFIX "pgsql_tmp"
230+
219231
#endif /* FD_H */

0 commit comments

Comments
 (0)