3 虚拟文件管理
PG的本项功能称为:a cache of 'virtual' file descriptors (VFDs)
操作系统,对于资源的分配,存在限制,比如,一个进程,可以拥有多少子线程、可以同时打开多少个文件、可以使用的堆栈大小是多少等等。
而不同的操作系统,其限制是不一样的。
作为数据库管理系统,PG能够做到跨平台,对文件的管理,有自己的一套规则以屏蔽操作系统在文件打开数上不同限制。
PG使用LRU算法,对文件句柄进行管理,调用的函数有:LruDelete(File file)、LruInsert(File file)。
一些函数调用关系如下:
Insert
LruInsert //LRU相关函数
FileAccess //文件操作的相关封装
FilePrefetch
FileRead //文件操作的相关封装
lo_import_internal
BufFileLoadBuffer
mdread //上一层操作的相关封装
FileWrite //文件操作的相关封装
FileSync //文件操作的相关封装
FileSeek //文件操作的相关封装
FileTruncate //文件操作的相关封装
PathNameOpenFile
另外,LRU相关结构如下:
typedef struct vfd
{
int fd; /* current FD, or VFD_CLOSED if none */
unsigned short fdstate; /* bitflags for VFD's state */
ResourceOwner resowner; /* owner, for automatic cleanup */
File nextFree; /* link to next free VFD, if in freelist */
File lruMoreRecently; /* doubly linked recency-of-use list */
File lruLessRecently;
off_t seekPos; /* current logical file position */
char *fileName; /* name of file, or NULL for unused VFD */
/* NB: fileName is malloc'd, and must be free'd when closing the VFD */
int fileFlags; /* open(2) flags for (re)opening the file */
int fileMode; /* mode to pass to open(2) */
} Vfd;