作者
digoal
日期
2016-03-17
标签
PostgreSQL , 临时文件 , 清理
背景
在使用数据库时,跑某些SQL的时候,如果work_mem内存不足会涉及一些临时空间的使用,比如排序,聚合,group by。
如果数据库突然crash了,或者某些原因未清除temp file。
数据库在重启的时候,会自动清理。
PostmasterMain(int argc, char *argv[])
call
RemovePgTempFiles(void)
call
RemovePgTempFilesInDir(const char *tmpdirname)
代码如下:
src/backend/storage/file/fd.c
```
/ Process one pgsql_tmp directory for RemovePgTempFiles /
static void
RemovePgTempFilesInDir(const char tmpdirname)
{
DIR temp_dir;
struct dirent *temp_de;
char rm_path[MAXPGPATH];
temp_dir = AllocateDir(tmpdirname);
if (temp_dir == NULL)
{
/* anything except ENOENT is fishy */
if (errno != ENOENT)
elog(LOG,
"could not open temporary-files directory \"%s\": %m",
tmpdirname);
return;
}
while ((temp_de = ReadDir(temp_dir, tmpdirname)) != NULL)
{
if (strcmp(temp_de->d_name, ".") == 0 ||
strcmp(temp_de->d_name, "..") == 0)
continue;
snprintf(rm_path, sizeof(rm_path), "%s/%s",
tmpdirname, temp_de->d_name);
if (strncmp(temp_de->d_name,
PG_TEMP_FILE_PREFIX,
strlen(PG_TEMP_FILE_PREFIX)) == 0)
unlink(rm_path); /* note we ignore any error */
else
elog(LOG,
"unexpected file found in temporary-files directory: \"%s\"",
rm_path);
}
FreeDir(temp_dir);
}
```
PostgreSQL 许愿链接
您的愿望将传达给PG kernel hacker、数据库厂商等, 帮助提高数据库产品质量和功能, 说不定下一个PG版本就有您提出的功能点. 针对非常好的提议,奖励限量版PG文化衫、纪念品、贴纸、PG热门书籍等,奖品丰富,快来许愿。开不开森.
9.9元购买3个月阿里云RDS PostgreSQL实例
PostgreSQL 解决方案集合
德哥 / digoal's github - 公益是一辈子的事.





