smgr管理器核心数据结构,需要配合md.c来使用。
代码语言:javascript复制 /*
* for md.c; per-fork arrays of the number of open segments
* (md_num_open_segs) and the segments themselves (md_seg_fds).
*/
int md_num_open_segs[MAX_FORKNUM 1];
struct _MdfdVec *md_seg_fds[MAX_FORKNUM 1];
- md_num_open_segs记录当前表打开文件数量
- md_seg_fds记录当前表打开文件段指针,PG文件超过1G需要切分,例如文件名为23456的表:
- 切分为一个文件
- 23456信息记录在
md_seg_fds[MAIN_FORKNUM][0]
md_num_open_segs[MAIN_FORKNUM]=1
- 23456信息记录在
- 切分为两个文件
- 23456信息记录在
md_seg_fds[MAIN_FORKNUM][0]
- 23456.0信息记录在
md_seg_fds[MAIN_FORKNUM][1]
md_num_open_segs[MAIN_FORKNUM]=2
- 23456信息记录在
- 切分为一个文件
注意: md_seg_fds是一个指针数组,每一个指针指向当前表存FD的一块空间,当前表有多个文件的话,这块空间就会有多个_MdfdVec,每个_MdfdVec记录当前文件的vfd和当前文件的segments号(segments表示切割后第几个文件)
- 每张表对应一个SMgrRelation
- 每个SMgrRelation维护自己已经打开的vfd
- vfd在md.c中使用,在fd.c中实现
- 实现新的文件系统接口需要在smgr.c中添加,并指向自己的实现(比如md.c)
- 实现新的文件系统大概率无需使用vfd,文件句柄需要自己管理
/*
* smgr.c maintains a table of SMgrRelation objects, which are essentially
* cached file handles. An SMgrRelation is created (if not already present)
* by smgropen(), and destroyed by smgrclose(). Note that neither of these
* operations imply I/O, they just create or destroy a hashtable entry.
* (But smgrclose() may release associated resources, such as OS-level file
* descriptors.)
*
* An SMgrRelation may have an "owner", which is just a pointer to it from
* somewhere else; smgr.c will clear this pointer if the SMgrRelation is
* closed. We use this to avoid dangling pointers from relcache to smgr
* without having to make the smgr explicitly aware of relcache. There
* can't be more than one "owner" pointer per SMgrRelation, but that's
* all we need.
*
* SMgrRelations that do not have an "owner" are considered to be transient,
* and are deleted at end of transaction.
*/
typedef struct SMgrRelationData
{
/* rnode is the hashtable lookup key, so it must be first! */
RelFileNodeBackend smgr_rnode; /* relation physical identifier */
/* pointer to owning pointer, or NULL if none */
struct SMgrRelationData **smgr_owner;
/*
* These next three fields are not actually used or manipulated by smgr,
* except that they are reset to InvalidBlockNumber upon a cache flush
* event (in particular, upon truncation of the relation). Higher levels
* store cached state here so that it will be reset when truncation
* happens. In all three cases, InvalidBlockNumber means "unknown".
*/
BlockNumber smgr_targblock; /* current insertion target block */
BlockNumber smgr_fsm_nblocks; /* last known size of fsm fork */
BlockNumber smgr_vm_nblocks; /* last known size of vm fork */
/* additional public fields may someday exist here */
/*
* Fields below here are intended to be private to smgr.c and its
* submodules. Do not touch them from elsewhere.
*/
int smgr_which; /* storage manager selector */
/*
* for md.c; per-fork arrays of the number of open segments
* (md_num_open_segs) and the segments themselves (md_seg_fds).
*/
int md_num_open_segs[MAX_FORKNUM 1];
struct _MdfdVec *md_seg_fds[MAX_FORKNUM 1];
/* if unowned, list link in list of all unowned SMgrRelations */
dlist_node node;
} SMgrRelationData;
typedef SMgrRelationData *SMgrRelation