本篇内容主要讲解“PostgreSQL Locks中LOCK结构体是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“PostgreSQL Locks中LOCK结构体是什么”吧!

一、LOCK Struct

/**TheLOCKTAGstructisdefinedwithmaliceaforethoughttofitinto16*byteswithnopadding.Notethatthiswouldneedadjustmentifwewere*towidenOid,BlockNumber,orTransactionIdtomorethan32bits.**Weincludelockmethodidinthelocktagsothatasinglehashtablein*sharedmemorycanstorelocksofdifferentlockmethods.*/typedefstructLOCKTAG{uint32locktag_field1;/*a32-bitIDfield*/uint32locktag_field2;/*a32-bitIDfield*/uint32locktag_field3;/*a32-bitIDfield*/uint16locktag_field4;/*a16-bitIDfield*/uint8locktag_type;/*seeenumLockTagType*/uint8locktag_lockmethodid;/*lockmethodindicator*/}LOCKTAG;/**Per-locked-objectlockinformation:**tag--uniquelyidentifiestheobjectbeinglocked*grantMask--bitmaskforalllocktypescurrentlygrantedonthisobject.*waitMask--bitmaskforalllocktypescurrentlyawaitedonthisobject.*procLocks--listofPROCLOCKobjectsforthislock.*waitProcs--queueofprocesseswaitingforthislock.*requested--countofeachlocktypecurrentlyrequestedonthelock*(includesrequestsalreadygranted!!).*nRequested--totalrequestedlocksofalltypes.*granted--countofeachlocktypecurrentlygrantedonthelock.*nGranted--totalgrantedlocksofalltypes.**Note:thesecountscount1foreachbackend.Internallytoabackend,*theremaybemultiplegrabsonaparticularlock,butthisisnotreflected*intosharedmemory.*/typedefstructLOCK{/*hashkey*/LOCKTAGtag;/*uniqueidentifieroflockableobject*//*data*/LOCKMASKgrantMask;/*bitmaskforlocktypesalreadygranted*/LOCKMASKwaitMask;/*bitmaskforlocktypesawaited*/SHM_QUEUEprocLocks;/*listofPROCLOCKobjectsassoc.withlock*/PROC_QUEUEwaitProcs;/*listofPGPROCobjectswaitingonlock*/intrequested[MAX_LOCKMODES];/*countsofrequestedlocks*/intnRequested;/*totalofrequested[]array*/intgranted[MAX_LOCKMODES];/*countsofgrantedlocks*/intnGranted;/*totalofgranted[]array*/}LOCK;#defineLOCK_LOCKMETHOD(lock)((LOCKMETHODID)(lock).tag.locktag_lockmethodid)---------------------------------------------------------------------------Thelockmanager'sLOCKobjectscontain:LOCK结构体包括:tag-Thekeyfieldsthatareusedforhashinglocksinthesharedmemorylockhashtable.Thecontentsofthetagessentiallydefineanindividuallockableobject.Seeinclude/storage/lock.hfordetailsaboutthesupportedtypesoflockableobjects.Thisisdeclaredasaseparatestructtoensurethatwealwayszerooutthecorrectnumberofbytes.Itiscriticalthatanyalignment-paddingbytesthecompilermightinsertinthestructbezeroedout,elsethehashcomputationwillberandom.(Currently,wearecarefultodefinestructLOCKTAGsothattherearenopaddingbytes.)tag-该键域用于标记共享内存lock哈希表中的hashinglocks.标记tag的内容本质上定义了一个独立的可锁定对象.关于已支持的可锁定对象类型的详细信息可参考include/storage/lock.h.之所以定义为一个单独的结构是为了确保能够把归零正确的字节数.编译器可能插入到结构体中的所有对齐字节数正确被归零是很很重要的,否则的话哈希的计算会是随机的.(当前来看,定义结构体LOCKTAG以避免对齐字节)grantMask-Thisbitmaskindicateswhattypesoflocksarecurrentlyheldonthegivenlockableobject.Itisused(againstthelocktable'sconflicttable)todetermineifanewlockrequestwillconflictwithexistinglocktypesheld.ConflictsaredeterminedbybitwiseANDoperationsbetweenthegrantMaskandtheconflicttableentryfortherequestedlocktype.BitiofgrantMaskis1ifandonlyifgranted[i]>0.grantMask-该bitmask表示在给定的可锁定对象上持有了哪些类型的locks.该字段用于确定新申请的锁是否会与现存的锁存在冲突.冲突通过grantMask和请求锁类型的冲突表条目的bitwiseAND操作实现.当且仅当granted[i]>0,grantMask的第i位为1.waitMask-Thisbitmaskshowsthetypesoflocksbeingwaitedfor.BitiofwaitMaskis1ifandonlyifrequested[i]>granted[i].waitMask-该字段标记了正在等待的锁类型.当且仅当requested[i]>granted[i],waitMask中的第1位为1.procLocks-ThisisasharedmemoryqueueofallthePROCLOCKstructsassociatedwiththelockobject.NotethatbothgrantedandwaitingPROCLOCKsareinthislist(indeed,thesamePROCLOCKmighthavesomealready-grantedlocksandbewaitingformore!).procLocks-与lockobject相关的PROCLOCK结构体在共享内存中的队列.注意链表中存在granted和waitingPROCLOCKs.(实际上,同一个PROCLOCK可能有已授予的locks但正在等待更多的锁)waitProcs-ThisisasharedmemoryqueueofallPGPROCstructurescorrespondingtobackendsthatarewaiting(sleeping)untilanotherbackendreleasesthislock.Theprocessstructureholdstheinformationneededtodetermineifitshouldbewokenupwhenthelockisreleased.waitProcs-对应等待其他后台进程释放锁的后台进程的PGPROC结构体在共享内存中的队列.进程结构体保存了用于确定在锁释放时是否需要唤醒的相关信息.nRequested-Keepsacountofhowmanytimesthislockhasbeenattemptedtobeacquired.Thecountincludesattemptsbyprocesseswhichwereputtosleepduetoconflicts.Italsocountsthesamebackendtwiceif,forexample,abackendprocessfirstacquiresareadandthenacquiresawrite.(Butmultipleacquisitionsofthesamelock/lockmodewithinabackendarenotmultiplycountedhere;theyarerecordedonlyinthebackend'sLOCALLOCKstructure.)nRequested-该字段保存了尝试获取该锁的次数.计数包括因为冲突而处于休眠状态的次数.如果一个进程第一次请求读然后请求写时可能会导致该进程被多次统计.requested-Keepsacountofhowmanylocksofeachtypehavebeenattempted.Onlyelements1throughMAX_LOCKMODES-1areusedastheycorrespondtothelocktypedefinedconstants.Summingthevaluesofrequested[]shouldcomeoutequaltonRequested.requested-该字段保存了尝试获取多少种锁类型.只有1->MAX_LOCKMODES-1被使用,因为这对应了锁类型常量.计算requested数组的和应等于nRequested.nGranted-Keepscountofhowmanytimesthislockhasbeensuccessfullyacquired.Thiscountdoesnotincludeattemptsthatarewaitingduetoconflicts.OtherwisethecountingrulesarethesameasfornRequested.nGranted-成功获取该锁的次数.该计数不包括因为冲突而等待的次数.因此该计数规则与nRequested一样.granted-Keepscountofhowmanylocksofeachtypearecurrentlyheld.Onceagainonlyelements1throughMAX_LOCKMODES-1areused(0isnot).Also,likerequested[],summingthevaluesofgranted[]shouldtotaltothevalueofnGranted.granted-保存每种类型有多少锁.1->MAX_LOCKMODES-1是有用的.与requested类似,granted[]数组的和应等于nGranted.Weshouldalwayshave0<=nGranted<=nRequested,and0<=granted[i]<=requested[i]foreachi.Whenalltherequestcountsgotozero,theLOCKobjectisnolongerneededandcanbefreed.nGranted的的范围为[0,nRequested],对于每一个granted[i]范围为[0,requested[i]].如果所有请求变为0,那么LOCK对象不再需要,会通过free释放.

到此,相信大家对“PostgreSQL Locks中LOCK结构体是什么”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!