1先上个图,看一下函数调用过程梗概,中间略过部分细节
@H_403_1@
@H_403_1@
2初始化xlog相关结构@H_403_1@
话说main()->…->PostmasterMain()->…->reset_shared() ->CreateSharedMemoryAndSemaphores()>…->CreateSharedInvalidationState(),调用ShmemInitStruct(),在其中调用hash_search()在哈希表索引"ShmemIndex"中查找"shmInvalBuffer",如果没有,就在shmemIndex中给"shmInvalBuffer"分一个HashElement和ShmemIndexEnt(entry),在其中的Entry中写上"shmInvalBuffer"。返回ShmemInitStruct(),再调用ShmemAlloc()在共享内存上给"shmInvalBuffer"相关结构(见下面“shmInvalBuffer相关结构图”)分配空间,设置entry(在这儿及ShmemIndexEnt类型变量)的成员location指向该空间,size成员记录该空间大小,最后返回CreateSharedInvalidationState(),让SISeg *类型静态全局变量shmInvalBuffer指向所分配内存,设置SISeg和ProcState结构类型的成员值。@H_403_1@
相关结构定义见下面:@H_403_1@
/* Shared cache invalidationmemory segment */@H_403_1@
typedef struct SISeg@H_403_1@
{@H_403_1@
/*@H_403_1@
* General stateinformation@H_403_1@
*/@H_403_1@
int minMsgNum; /* oldestmessage still needed */@H_403_1@
int maxMsgNum; /* nextmessage number to be assigned */@H_403_1@
int nextThreshold; /* # ofmessages to call SICleanupQueue */@H_403_1@
int lastBackend; /* index of lastactive procState entry,+1 */@H_403_1@
int maxBackends; /* size ofprocState array */@H_403_1@
@H_403_1@
slock_t msgnumLock; /* spinlock protectingmaxMsgNum */@H_403_1@
@H_403_1@
/*@H_403_1@
* Circular buffer holdingshared-inval messages@H_403_1@
*/@H_403_1@
SharedInvalidationMessage buffer[MAXNUMMESSAGES];@H_403_1@
@H_403_1@
/*@H_403_1@
* Per-backend stateinfo.@H_403_1@
*@H_403_1@
* We declare procState as1 entry because C wants a fixed-size array,but@H_403_1@
* actually it ismaxBackends entries long.@H_403_1@
*/@H_403_1@
ProcState procState[1]; /* reflects the invalidation state */@H_403_1@
} SISeg;@H_403_1@
@H_403_1@
static SISeg *shmInvalBuffer; /* pointerto the shared inval buffer */@H_403_1@
@H_403_1@
初始化完shmInvalBuffer相关结构的共享内存结构图@H_403_1@
为了精简上图,把创建shmem的哈希表索引"ShmemIndex"时创建的HCTL结构删掉了,这个结构的作用是记录创建可扩展哈希表的相关信息,不过这个结构在"ShmemIndex"创建完成后也会由于出了对象作用域而消失。增加了左边灰色底的部分,描述共享内存/shmem里各变量物理布局概览,由下往上,由低地址到高地址。图中黄色的索引项就是本节新增加的索引项。@H_403_1@
@H_403_1@
@H_403_1@
shmInvalBuffer相关结构图@H_403_1@