当前位置:数据库 > SQLServer >>

PostgreSQL启动过程中的那些事七:初始化共享内存和信号八:shmem中初始化常规锁管理器

这一节pg初始化锁管理器,通过InitLocks例程实现,主要是创建了三个哈希表。第一个哈希表"LOCKhash"用于管理锁,第二个哈希表"PROCLOCKhash"用于管理进程锁,第三个"LOCALLOCKhash"用于管理本地锁信息。其中第一个和第二个哈希表都是共享哈希表,第三个是非关系哈希表。初始化第三个哈希表"LOCALLOCK hash"时在共享内存哈希表索引"ShmemIndex"里没有创建索引,因为这个哈希表不在共享内存里,而是在MemoryContext "LOCALLOCK hash" 里分配的内存。

        pg中的锁有三种类型:自旋锁(spinlock)、轻量锁(LWLock)、常规锁(Lock),作为一个主题另行讨论。

1先上个图,看一下函数调用过程梗概,中间略过部分细节

 

\  

 

初始化Lockmgr方法调用流程图

2初始化xlog相关结构

        话说main()->…->PostmasterMain()->…->reset_shared() ->CreateSharedMemoryAndSemaphores()>…->InitLocks(),在shmem里分配了三个哈希表。第一个哈希表"LOCK hash"用于管理锁,第二个哈希表"PROCLOCK hash"用于管理进程锁,第三个"LOCALLOCK hash"用于管理本地锁信息。其中第一个和第二个哈希表都是共享哈希表,第三个是非关系哈希表。初始化第三个哈希表"LOCALLOCK hash"时在共享内存哈希表索引"ShmemIndex"里没有创建索引,因为这个哈希表不在共享内存里,而是在MemoryContext "LOCALLOCK hash" 里分配的内存。

        InitLocks()->ShmemInitHash()->ShmemInitStruct(),在其中调用hash_search()在哈希表索引"ShmemIndex"中查找"LOCK hash",如果没有,就在shmemIndex中给"LOCK hash"分一个HashElement和ShmemIndexEnt(entry),在其中的Entry中写上"LOCKhash"。返回ShmemInitStruct(),再调用ShmemAlloc()在共享内存上给"LOCKhash"相关结构(见下面"LOCK hash"相关结构图)分配空间,设置entry(在这儿即ShmemIndexEnt类型变量)的成员location指向该空间,size成员记录该空间大小,然后返回ShmemInitHash(),调用hash_create(),创建哈希表"LOCK hash",最后返回ShmemInitHash(),让HTAB *类型静态全局变量LockMethodLockHash指向哈希表"LOCK hash"。

        接着InitLocks()->ShmemInitHash()->ShmemInitStruct(),在其中调用hash_search()在哈希表索引"ShmemIndex"中查找"PROCLOCK hash",如果没有,就在shmemIndex中给"PROCLOCK hash"分一个HashElement和ShmemIndexEnt(entry),在其中的Entry中写上"PROCLOCKhash"。返回ShmemInitStruct(),再调用ShmemAlloc()在共享内存上给"PROCLOCKhash"相关结构(见下面"PROCLOCK hash"相关结构图)分配空间,设置entry(在这儿即ShmemIndexEnt类型变量)的成员location指向该空间,size成员记录该空间大小,然后返回ShmemInitHash(),调用hash_create(),创建哈希表"PROCLOCK hash",最后返回ShmemInitHash(),让HTAB *类型静态全局变量LockMethodProcLockHash指向哈希表"PROCLOCK hash"。

        接着InitLocks()->hash_create(),在其中调用AllocSetContextCreate(),创建MemoryContext "LOCALLOCK hash"(做个回顾,当前MemoryContext见下面的当前MemoryContext结构图),调用DynaHashAlloc(),在MemoryContext "LOCALLOCK hash"上分配空间,创建哈希表索引"LOCALLOCKhash"(见下面"LOCALLOCK hash"相关结构图),最后返回InitLocks(),让HTAB *类型静态全局变量LockMethodLocalHash指向哈希表"LOCALLOCK hash"。

        相关变量、结构定义和初始化完成后数据结构图在下面。

LOCKTAG结构被定义用于填充16字节。请注意,如果pg要扩大OID,BlockNumber,或TransactionId超过32位,这将需要调整。

       LOCKTAG包含lockmethodid是为了共享内存里一个哈希表能够存储不同lockmemthods的锁。

typedef struct LOCKTAG

{

    uint32      locktag_field1; /*a 32-bit ID field */

    uint32      locktag_field2; /*a 32-bit ID field */

    uint32      locktag_field3; /*a 32-bit ID field */

    uint16      locktag_field4; /*a 16-bit ID field */

    uint8       locktag_type;   /* see enum LockTagType */

    uint8       locktag_lockmethodid;   /* lockmethodindicator */

} LOCKTAG;

 

每个被锁对象的锁信息:

tag:可锁对象的唯一标识符

grantMask:目前授予该对象的所有类型锁的位掩码

/*

 *Per-locked-object lock information:

 *

 * tag -- uniquelyidentifies the object being locked

 * grantMask --bitmask for all lock types currently granted on this object.

 * waitMask --bitmask for all lock types currently awaited on this object.

 * procLocks --list of PROCLOCK objects for this lock.

 * waitProcs --queue of processes waiting for this lock.

 * requested --count of each lock type currently requested on the lock

 *      (includes requests already granted!!).

 * nRequested --total requested locks of all types.

 * granted -- countof each lock type currently granted on the lock.

 * nGranted --total granted locks of all types.

 *

 * Note: thesecounts count 1 for each backend. Internally to a backend,

 * there may bemultiple grabs on a particular lock, but this is not reflected

 * into sharedmemory.

 */

typedef struct LOCK

{

    /* hash key */

    LOCKTAG     tag;            /* unique identifier of lockable object */

 

    /* data */

    LOCKMASK    grantMask;      /* bitmask for lock types already granted */

    LOCKMASK    waitMask;       /* bitmask for lock types awaited */

    SHM_QUEUE   procLocks;      /* list of PROCLOCK objects assoc. with lock */

    PROC_QUEUE  waitProcs;      /* list of PGPROC objects waiting on lock */

    int         requested[MAX_LOCKMODES];       /* counts ofrequested locks */

    int         nRequested;     /* total ofrequested[] array */

    int         granted[MAX_LOCKMODES];/* counts of granted locks */

    int         nGranted;       /* total ofgranted[] array */

} LOCK;

 

        pg可以有多个不同的backend进程在同一个开锁对象上持有或等待锁。pg需要为每个持有者/等待着存储一些信息。这些保存在结构PROCLOCK里。

        PROCLOCKTAG是在proclock哈希表里查找一个PROCLOCK项的关键信息。一个PROCLOCKTAG值唯一的标识一个可锁对象和一个持有者/等待着的组合。(这儿pg能使用指针,因为PROCLOCKTAG仅需要在PROCLOCK的生命周期里唯一,且不会在lock和proc生存期以为)

        为了不同的目的,backend进程可以持有同一个锁:独立于会话锁,backend进程跟踪事务锁。但是,这个在共享内存状态中没有反映出来:pg仅跟踪持有锁的backend进程。这是可以的,因为backend进程不能阻塞自己。

        holdMask字段显示已经授予

Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,