A创建了哈希表"PREDICATELOCKTARGET hash"。
B在上面的哈希表里增加了ScratchTargetTag结构的索引
C创建了哈希表"PREDICATELOCK hash"。
D初始化了"PredXactList"相关结构
E创建了哈希表"SERIALIZABLEXID hash"。
F初始化了RWConflictPool相关结构
H初始化了"FinishedSerializableTransactions"相关结构
I初始化了"OldSerXid SLRU Ctl"相关结构
J初始化了"OldSerXidControlData"相关结构
2初始化PredicateLocks相关结构
话说main()->…->PostmasterMain()->…->reset_shared() ->CreateSharedMemoryAndSemaphores()>…->InitPredicateLocks(),做了下面相关结构的内存分配和初始化:
A创建了哈希表"PREDICATELOCKTARGEThash"。
B在上面的哈希表里增加了ScratchTargetTag结构的索引
C创建了哈希表"PREDICATELOCKhash"。
D初始化了"PredXactList"相关结构
E创建了哈希表"SERIALIZABLEXID hash"。
F初始化了RWConflictPool相关结构,用于处理读写冲突
H初始化了"FinishedSerializableTransactions"相关结构
I初始化了"OldSerXid SLRU Ctl"相关结构
J初始化了"OldSerXidControlData"相关结构
初始化上面这些结构都会在共享内存/shmem哈希表索引shmemIndex中增加索引项,下面把初始化这些结构后的shmemIndex图放到下边,就不加一个索引一个图了,减小篇幅。不过这样就看不出先后顺序了,好在这些消失的信息对理解这些过程没什么影响。图中黄色的索引项就是本节新增加的索引项。
初始化完PredicateLocks相关结构的共享内存结构图
为了精简上图,把创建shmem的哈希表索引"ShmemIndex"时创建的HCTL结构删掉了,这个结构的作用是记录创建可扩展哈希表的相关信息,不过这个结构在"ShmemIndex"创建完成后也会由于出了对象作用域而消失。增加了左边灰色底的部分,描述共享内存/shmem里各变量物理布局概览,由下往上,由低地址到高地址。图中黄色的索引项就是本节新增加的索引项。
A创建了哈希表"PREDICATELOCKTARGEThash"。
InitPredicateLocks()->ShmemInitHash()->ShmemInitStruct(),在其中调用hash_search()在哈希表索引"ShmemIndex"中查找"PREDICATELOCKTARGET hash",如果没有,就在shmemIndex中给"PREDICATELOCKTARGET hash"分一个HashElement和ShmemIndexEnt(entry),在其中的Entry中写上"PREDICATELOCKTARGEThash"。返回ShmemInitStruct(),再调用ShmemAlloc()在共享内存上给"PREDICATELOCKTARGEThash"相关结构(见下面"PREDICATELOCKTARGET hash"相关结构图)分配空间,设置entry(在这儿即ShmemIndexEnt类型变量)的成员location指向该空间,size成员记录该空间大小,然后返回ShmemInitHash(),调用hash_create(),创建哈希表"PREDICATELOCKTARGET hash",最后返回ShmemInitHash(),让HTAB *类型静态全局变量PredicateLockTargetHash指向哈希表"PREDICATELOCKTARGET hash"。
相关结构定义见下面:
typedef struct PREDICATELOCKTARGETTAG
{
uint32 locktag_field1; /* a 32-bit ID field */
uint32 locktag_field2; /* a 32-bit ID field */
uint32 locktag_field3; /* a 32-bit ID field */
uint32 locktag_field4; /* a 32-bit ID field */
uint32 locktag_field5; /* a 32-bit ID field */
} PREDICATELOCKTARGETTAG;
typedef struct PREDICATELOCKTARGET
{
/* hash key */
PREDICATELOCKTARGETTAG tag; /* unique identifierof lockable object */
/* data */
SHM_QUEUE predicateLocks; /* list of PREDICATELOCK objects assoc. with
*predicate lock target */
} PREDICATELOCKTARGET;
"PREDICATELOCKTARGET hash"相关结构图
B在上面的哈希表里增加了ScratchTargetTag结构的索引
InitPredicateLocks()调用ShmemInitStruct(),在其中调用hash_search()在哈希表索引"PREDICATELOCKTARGEThash"中查找ScratchTargetTag(是PREDICATELOCKTARGET类型全局静态变量),如果没有,就把ScratchTargetTag作为哈希表"PREDICATELOCKTARGEThash"(该哈希表索引的类型是PREDICATELOCKTARGET)的第一个索引项(参见"PREDICATELOCKTARGET hash"相关结构图)。
C创建了哈希表"PREDICATELOCK hash"。
接着InitPredicateLocks()->ShmemInitHash()->ShmemInitStruct(),在其中调用hash_search()在哈希表索引"ShmemIndex"中查找"PREDICATELOCK hash",如果没有,就在shmemIndex中给"PREDICATELOCK hash"分一个HashElement和ShmemIndexEnt(entry),在其中的Entry中写上"PREDICATELOCKhash"。返回ShmemInitStruct(),再调用ShmemAlloc()在共享内存上给"PREDICATELOCKhash"相关结构(见下面"PREDICATELOCK hash"相关结构图)分配空间,设置entry(在这儿即ShmemIndexEnt类型变量)的成员location指向该空间,size成员记录该空间大小,然后返回ShmemInitHash(),调用hash_create(),创建哈希表"PREDICATELOCK hash",最后返回ShmemInitHash(),让HTAB *类型静态全局变量PredicateLockHash指向哈希表"PREDICATELOCK hash"。
相关结构定义见下面:
typedef struct PREDICATELOCKTAG
{
PREDICATELOCKTARGET *myTarget;
SERIALIZABLEXACT *myXact;
} PREDICATELOCKTAG;
typedef struct PREDICATELOCK
{
/* hash key */
PREDICATELOCKTAG tag; /* unique identifier of lock */
/* data */
SHM_QUEUE targetLink; /* list link inPREDICATELOCKTARGET's list of
*predicate locks */
SHM_QUEUE xactLink; /* list link in SERIALIZABLEXACT's list of
* predicate locks */
SerCommitSeqNo commitSeqNo; /* only used for summarized predicate locks*/
} PREDICATELOCK;
"PREDICATELOCK hash"相关结构图
D初始化了"PredXactList"相关结构
InitPredicateLocks()调用ShmemInitStruct(),在其中调用hash_search()在哈希表索引"ShmemIndex"中查找"PredXactList",如果没有,就在shmemIndex中给"PredXactList"分一个HashElement和ShmemIndexEnt(entry),在其中的Entry中写上"PredXactList"。返回ShmemInitStruct(),再调用ShmemAlloc()在共享内存上给"PredXactList"相关结构(见下面“PredXactList相关结构图”)分配空间,设置entry(在这儿及ShmemIndexEnt类型变量)的成员location指向该空间,size成员记录该空间大小,最后返回InitPredicateLocks(),让PredXactList *类型全局变量PredXact指向所分配内存,设置PredXactList结构类型的成员值。
相关结构定义见下面:
typedef struct PredXactListData
{
SHM_QUEUE availableList;
SHM_QUEUE activeList;
/*
* These global variablesare maintained when registering and cleaning up
* serializabletransactions. They must be global acrossall backends,
* but are not neededoutside the predicate.c source file. Protected by
*SerializableXactHashLock.
*/
TransactionId SxactGlobalXmin; /* global xmin for active serializable
* transactions */
int SxactGlobalXminCount; /* how many active serializable
* transactions have this xmin */
int WritableSxactCount; /* how many non-read-only serializable
* transactions are active */
SerCommitSeqNo LastSxactCommitSeqNo; /* a strictly monotonically
* increasing number for
* commits of serializable
* transactions */
/* Protected by SerializableXactHashLock.*/
SerCommitSeqNo CanPartialClearThrough; /* can clear predicate locks
* and inConflicts for
* committed transactions
* through this seq no */
/* Protected bySerializableFinishedListLock. */
SerCommitSeqNo HavePartialClearedThrough; /* have cleared through this
* seq no */
SERIALIZABLEXACT *OldCommittedSxact; /* shared copy of dummy sxact */
PredXactListElement element;
} PredXactListData;
typedef struct PredXactListData*PredXactList;
typedef struct SERIALIZABLEXACT
{
VirtualTransactionId vxid; /* The executing process always has one of
*these. */
/*
* We use two numbers totrack the order that transactions commit. Before
* commit,a transaction ismarked as prepared,and prepareSeqNo is set.
* Shortly after commit,it's marked as committed,and commitSeqNo is set.
* This doesn't give astrict commit order,but these two values together
* are good enough for us,aswe can always err on the safe side and
* assume that there's aconflict,if we can't be sure of the exact
* ordering of two commits.
*
* Note that a transactionis marked as prepared for a short period during
* commit processing,evenif two-phase commit is not used. But with
* two-phase commit,atransaction can stay in prepared state for some
* time.
*/
SerCommitSeqNo prepareSeqNo;
SerCommitSeqNo commitSeqNo;
/* these values are not both interesting atthe same time */
union
{
SerCommitSeqNo earliestOutConflictCommit; /* when committed with
* conflict out */
SerCommitSeqNo lastCommitBeforeSnapshot; /* when not committed or
* no conflict out */
} SeqNo;
SHM_QUEUE outConflicts; /* list of write transactions whose data we
*couldn't read. */
SHM_QUEUE inConflicts; /* list of read transactions which couldn't
*see our write. */
SHM_QUEUE predicateLocks; /* list of associated PREDICATELOCK objects */
SHM_QUEUE finishedLink; /* list link in
*FinishedSerializableTransactions */
/*
* for r/o transactions:list of concurrent r/w transactions that we could
* potentially haveconflicts with,and vice versa for r/w transactions
*/
SHM_QUEUE possibleUnsafeConflicts;
TransactionId topXid; /* top level xid for the transaction,if one
*exists; else invalid */
TransactionId finishedBefore; /* invalid means still running; else
* the struct expires when no
* serializable xids are before this.*/
TransactionId xmin; /* the transaction's snapshot xmin */
uint32 flags; /* OR'd combination of values defined below */
int pid; /* pid of associated process */
} SERIALIZABLEXACT;
PredXactList相关结构图
E创建了哈希表"SERIALIZABLEXIDhash"。
接着InitPredicateLocks()调用ShmemInitHash ()->ShmemInitStruct(),在其中调用hash_search()在哈希表索引"ShmemIndex"中查找"SERIALIZABLEXID hash",如果没有,就在shmemIndex中给"SERIALIZABLEXID hash"分一个HashElement和ShmemIndexEnt(entry),在其中的Entry中写上"SERIALIZABLEXIDhash"。返回ShmemInitStruct(),再调用ShmemAlloc()在共享内存上给"SERIALIZABLEXIDhash"相关结构(见下面"SERIALIZABLEXID hash"相关结构图)分配空间,设置entry(在这儿即ShmemIndexEnt类型变量)的成员location指向该空间,size成员记录该空间大小,然后返回ShmemInitHash(),调用hash_create(),创建哈希表"SERIALIZABLEXID hash",最后返回ShmemInitHash(),让HTAB *类型静态全局变量SerializableXidHash指向哈希表"SERIALIZABLEXID hash"。
相关结构定义见下面:
typedef struct SERIALIZABLEXIDTAG
{
TransactionId xid;
} SERIALIZABLEXIDTAG;
typedef struct SERIALIZABLEXID
{
/* hash key */
SERIALIZABLEXIDTAG tag;
/* data */
SERIALIZABLEXACT *myXact; /* pointer to the top level transaction data */
} SERIALIZABLEXID;
"SERIALIZABLEXID hash"相关结构图
F初始化了RWConflictPool相关结构,用于处理读写冲突
InitPredicateLocks()调用ShmemInitStruct(),在其中调用hash_search()在哈希表索引"ShmemIndex"中查找"RWConflictPool",如果没有,就在shmemIndex中给"RWConflictPool"分一个HashElement和ShmemIndexEnt(entry),在其中的Entry中写上"RWConflictPool"。返回ShmemInitStruct(),再调用ShmemAlloc()在共享内存上给"RWConflictPool"相关结构(见下面“RWConflictPool和FinishedSerializableTransactions相关结构图相关结构图”)分配空间,设置entry(在这儿及ShmemIndexEnt类型变量)的成员location指向该空间,size成员记录该空间大小,最后返回InitPredicateLocks(),让RWConflictPoolHeaderData*类型全局变量RWConflictPool指向所分内存,设置其中RWConflictPoolHeaderData结构类型的成员值。
相关结构定义见下面:
typedef struct RWConflictData
{
SHM_QUEUE outLink; /* link for list of conflicts out from a sxact */
SHM_QUEUE inLink; /* link for list of conflicts in to a sxact */
SERIALIZABLEXACT *sxactOut;
SERIALIZABLEXACT *sxactIn;
} RWConflictData;
typedef struct RWConflictPoolHeaderData
{
SHM_QUEUE availableList;
RWConflict element;
} RWConflictPoolHeaderData;
RWConflictPool和FinishedSerializableTransactions相关结构图
H初始化了"FinishedSerializableTransactions"相关结构
InitPredicateLocks()调用ShmemInitStruct(),在其中调用hash_search()在哈希表索引"ShmemIndex"中查找"FinishedSerializableTransactions",如果没有,就在shmemIndex中给"FinishedSerializableTransactions"分一个HashElement和ShmemIndexEnt(entry),在其中的Entry中写上"FinishedSerializableTransactions"。返回ShmemInitStruct(),再调用ShmemAlloc()在共享内存上给"FinishedSerializableTransactions"相关结构(见下面“RWConflictPool和FinishedSerializableTransactions相关结构图相关结构图”)分配空间,设置entry(在这儿及ShmemIndexEnt类型变量)的成员location指向该空间,size成员记录该空间大小,最后返回InitPredicateLocks(),让SHM_QUEUE*类型全局变量FinishedSerializableTransactions指向所分内存,设置其中SubTransCtlData结构类型的成员值。
I初始化了"OldSerXid SLRUCtl"相关结构
InitPredicateLocks()->OldSerXidInit()->SimpleLruInit()->ShmemInitStruct(),在其中调用hash_search()在哈希表索引"ShmemIndex"中查找"OldSerXidSLRU Ctl",如果没有,就在shmemIndex中给"OldSerXid SLRU Ctl"分一个HashElement和ShmemIndexEnt(entry),在其中的Entry中写上"OldSerXid SLRU Ctl"。返回ShmemInitStruct(),再调用ShmemAlloc()在共享内存上给"OldSerXid SLRU Ctl"相关结构(见下面“OldSerXid SLRU Ctl和OldSerXidControlData相关结构图”)分配空间,设置entry(在这儿及ShmemIndexEnt类型变量)的成员location指向该空间,size成员记录该空间大小,最后返回SimpleLruInit(),让SlruCtlData *类型全局变量OldSerXidSlruCtl指向给"OldSerXid SLRU Ctl"相关结构分配的内存起始地址,设置其中SlruCtlData结构类型的成员值。
J初始化了"OldSerXidControlData"相关结构
InitPredicateLocks()->OldSerXidInit()调用ShmemInitStruct(),在其中调用hash_search()在哈希表索引"ShmemIndex"中查找"OldSerXidControlData",如果没有,就在shmemIndex中给"OldSerXidControlData"分一个HashElement和ShmemIndexEnt(entry),在其中的Entry中写上"OldSerXidControlData"。返回ShmemInitStruct(),再调用ShmemAlloc()在共享内存上给"OldSerXidControlData"相关结构(见下面“OldSerXid SLRU Ctl和OldSerXidControlData相关结构图”)分配空间,设置entry(在这儿及ShmemIndexEnt类型变量)的成员location指向该空间,size成员记录该空间大小,最后返回OldSerXidInit(),让OldSerXidControlData*类型全局变量oldSerXidControl指向给"OldSerXidControlData"相关结构分配的内存地址,设置其中OldSerXidControlDat结构类型的成员值。
相关结构定义见下面:
typedef struct OldSerXidControlData
{
int headPage; /* newestinitialized page */
TransactionId headXid; /* newest valid Xid in the SLRU */
TransactionId tailXid; /* oldest xmin we might be interested in */
bool warningIssued; /* have we issued SLRU wrap-around warning?*/
} OldSerXidControlData;
OldSerXid SLRU Ctl和OldSerXidControlData相关结构图