Skip to content

Commit 868a293

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 96c49f0 commit 868a293

File tree

2 files changed

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

2 files changed

+34
-0
lines changed

src/backend/storage/file/fd.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ bool data_sync_retry = false;
163163
/* How SyncDataDirectory() should do its job. */
164164
int recovery_init_sync_method = RECOVERY_INIT_SYNC_METHOD_FSYNC;
165165

166+
/* Which kinds of files should be opened with PG_O_DIRECT. */
167+
int io_direct_flags;
168+
169+
CheckTempFileSize_hook_type CheckTempFileSize_hook = NULL;
170+
166171
/* Debugging.... */
167172

168173
#ifdef FDDEBUG
@@ -1907,6 +1912,10 @@ FileClose(File file)
19071912
{
19081913
/* Subtract its size from current usage (do first in case of error) */
19091914
temporary_files_size -= vfdP->fileSize;
1915+
if (CheckTempFileSize_hook != NULL)
1916+
{
1917+
CheckTempFileSize_hook(vfdP->fileSize, PG_TEMP_FILES_SIZE_DEC);
1918+
}
19101919
vfdP->fileSize = 0;
19111920
}
19121921

@@ -2120,6 +2129,10 @@ FileWrite(File file, char *buffer, int amount, off_t offset,
21202129
(errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
21212130
errmsg("temporary file size exceeds temp_file_limit (%dkB)",
21222131
temp_file_limit)));
2132+
if (CheckTempFileSize_hook != NULL)
2133+
{
2134+
CheckTempFileSize_hook(past_write - vfdP->fileSize, PG_TEMP_FILES_SIZE_CHECK);
2135+
}
21232136
}
21242137
}
21252138

@@ -2145,6 +2158,10 @@ FileWrite(File file, char *buffer, int amount, off_t offset,
21452158
if (past_write > vfdP->fileSize)
21462159
{
21472160
temporary_files_size += past_write - vfdP->fileSize;
2161+
if (CheckTempFileSize_hook != NULL)
2162+
{
2163+
CheckTempFileSize_hook(past_write - vfdP->fileSize, PG_TEMP_FILES_SIZE_INC);
2164+
}
21482165
vfdP->fileSize = past_write;
21492166
}
21502167
}
@@ -2237,6 +2254,10 @@ FileTruncate(File file, off_t offset, uint32 wait_event_info)
22372254
/* adjust our state for truncation of a temp file */
22382255
Assert(VfdCache[file].fdstate & FD_TEMP_FILE_LIMIT);
22392256
temporary_files_size -= VfdCache[file].fileSize - offset;
2257+
if (CheckTempFileSize_hook != NULL)
2258+
{
2259+
CheckTempFileSize_hook(VfdCache[file].fileSize - offset, PG_TEMP_FILES_SIZE_DEC);
2260+
}
22402261
VfdCache[file].fileSize = offset;
22412262
}
22422263

@@ -2971,6 +2992,11 @@ static void
29712992
AtProcExit_Files(int code, Datum arg)
29722993
{
29732994
CleanupTempFiles(false, true);
2995+
2996+
if (CheckTempFileSize_hook != NULL)
2997+
{
2998+
CheckTempFileSize_hook(temporary_files_size, PG_TEMP_FILES_SIZE_DEC);
2999+
}
29743000
}
29753001

29763002
/*

src/include/storage/fd.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,14 @@ extern int durable_rename_excl(const char *oldfile, const char *newfile, int log
174174
extern void SyncDataDirectory(void);
175175
extern int data_sync_elevel(int elevel);
176176

177+
/* BEGIN_HADRON */
178+
#define PG_TEMP_FILES_SIZE_INC 0
179+
#define PG_TEMP_FILES_SIZE_DEC 1
180+
#define PG_TEMP_FILES_SIZE_CHECK 2
181+
typedef void (*CheckTempFileSize_hook_type) (off_t size, int action);
182+
extern PGDLLIMPORT CheckTempFileSize_hook_type CheckTempFileSize_hook;
183+
/* END_HADRON */
184+
177185
/* Filename components */
178186
#define PG_TEMP_FILES_DIR "pgsql_tmp"
179187
#define PG_TEMP_FILE_PREFIX "pgsql_tmp"

0 commit comments

Comments
 (0)