本篇内容介绍了“PostgreSQL中StrategyGetBuffer函数有什么作用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

一、数据结构

BufferDesc
共享缓冲区的共享描述符(状态)数据

/**Flagsforbufferdescriptors*buffer描述器标记**Note:TAG_VALIDessentiallymeansthatthereisabufferhashtable*entryassociatedwiththebuffer'stag.*注意:TAG_VALID本质上意味着有一个与缓冲区的标记相关联的缓冲区散列表条目。*///bufferheader锁定#defineBM_LOCKED(1U<<22)/*bufferheaderislocked*///数据需要写入(标记为DIRTY)#defineBM_DIRTY(1U<<23)/*dataneedswriting*///数据是有效的#defineBM_VALID(1U<<24)/*dataisvalid*///已分配buffertag#defineBM_TAG_VALID(1U<<25)/*tagisassigned*///正在R/W#defineBM_IO_IN_PROGRESS(1U<<26)/*readorwriteinprogress*///上一个I/O出现错误#defineBM_IO_ERROR(1U<<27)/*previousI/Ofailed*///开始写则变DIRTY#defineBM_JUST_DIRTIED(1U<<28)/*dirtiedsincewritestarted*///存在等待solepin的其他进程#defineBM_PIN_COUNT_WAITER(1U<<29)/*havewaiterforsolepin*///checkpoint发生,必须刷到磁盘上#defineBM_CHECKPOINT_NEEDED(1U<<30)/*mustwriteforcheckpoint*///持久化buffer(不是unlogged或者初始化fork)#defineBM_PERMANENT(1U<<31)/*permanentbuffer(notunlogged,*orinitfork)*//**BufferDesc--shareddescriptor/statedataforasinglesharedbuffer.*BufferDesc--共享缓冲区的共享描述符(状态)数据**Note:Bufferheaderlock(BM_LOCKEDflag)mustbeheldtoexamineorchange*thetag,stateorwait_backend_pidfields.Ingeneral,bufferheaderlock*isaspinlockwhichiscombinedwithflags,refcountandusagecountinto*singleatomicvariable.Thislayoutallowustodosomeoperationsina*singleatomicoperation,withoutactuallyacquiringandreleasingspinlock;*forinstance,increaseordecreaserefcount.buf_idfieldneverchanges*afterinitialization,sodoesnotneedlocking.freeNextisprotectedby*thebuffer_strategy_locknotbufferheaderlock.TheLWLockcantakecare*ofitself.Thebufferheaderlockis*not*usedtocontrolaccesstothe*datainthebuffer!*注意:必须持有Bufferheader锁(BM_LOCKED标记)才能检查或修改tag/state/wait_backend_pid字段.*通常来说,bufferheaderlock是spinlock,它与标记位/参考计数/使用计数组合到单个原子变量中.*这个布局设计允许我们执行原子操作,而不需要实际获得或者释放spinlock(比如,增加或者减少参考计数).*buf_id字段在初始化后不会出现变化,因此不需要锁定.*freeNext通过buffer_strategy_lock锁而不是bufferheaderlock保护.*LWLock可以很好的处理自己的状态.*务请注意的是:bufferheaderlock不用于控制buffer中的数据访问!**It'sassumedthatnobodychangesthestatefieldwhilebufferheaderlock*isheld.Thusbufferheaderlockholdercandocomplexupdatesofthe*statevariableinsinglewrite,simultaneouslywithlockrelease(cleaning*BM_LOCKEDflag).Ontheotherhand,updatingofstatewithoutholding*bufferheaderlockisrestrictedtoCAS,whichinsurethatBM_LOCKEDflag*isnotset.Atomicincrement/decrement,OR/ANDetc.arenotallowed.*假定在持有bufferheaderlock的情况下,没有人改变状态字段.*持有bufferheaderlock的进程可以执行在单个写操作中执行复杂的状态变量更新,*同步的释放锁(清除BM_LOCKED标记).*换句话说,如果没有持有bufferheaderlock的状态更新,会受限于CAS,*这种情况下确保BM_LOCKED没有被设置.*比如原子的增加/减少(AND/OR)等操作是不允许的.**Anexceptionisthatifwehavethebufferpinned,itstagcan'tchange*underneathus,sowecanexaminethetagwithoutlockingthebufferheader.*Also,inplaceswedoone-timereadsoftheflagswithoutbotheringto*lockthebufferheader;thisisgenerallyforsituationswherewedon't*expecttheflagbitbeingtestedtobechanging.*一种例外情况是如果我们已有bufferpinned,该buffer的tag不能改变(在本进程之下),*因此不需要锁定bufferheader就可以检查tag了.*同时,在执行一次性的flags读取时不需要锁定bufferheader.*这种情况通常用于我们不希望正在测试的flagbit将被改变.**Wecan'tphysicallyremoveitemsfromadiskpageifanotherbackendhas*thebufferpinned.Hence,abackendmayneedtowaitforallotherpins*togoaway.ThisissignaledbystoringitsownPIDinto*wait_backend_pidandsettingflagbitBM_PIN_COUNT_WAITER.Atpresent,*therecanbeonlyonesuchwaiterperbuffer.*如果其他进程有bufferpinned,那么进程不能物理的从磁盘页面中删除items.*因此,后台进程需要等待其他pins清除.这可以通过存储它自己的PID到wait_backend_pid中,*并设置标记位BM_PIN_COUNT_WAITER.*目前,每个缓冲区只能由一个等待进程.**Weusethissamestructforlocalbufferheaders,butthelocksarenot*usedandnotalloftheflagbitsareusefuleither.Toavoidunnecessary*overhead,manipulationsofthestatefieldshouldbedonewithoutactual*atomicoperations(i.e.onlypg_atomic_read_u32()and*pg_atomic_unlocked_write_u32()).*本地缓冲头部使用同样的结构,但并不需要使用locks,而且并不是所有的标记位都使用.*为了避免不必要的负载,状态域的维护不需要实际的原子操作*(比如只有pg_atomic_read_u32()andpg_atomic_unlocked_write_u32())**Becarefultoavoidincreasingthesizeofthestructwhenaddingor*reorderingmembers.Keepingitbelow64bytes(themostcommonCPU*cachelinesize)isfairlyimportantforperformance.*在增加或者记录成员变量时,小心避免增加结构体的大小.*保持结构体大小在64字节内(通常的CPU缓存线大小)对于性能是非常重要的.*/typedefstructBufferDesc{//buffertagBufferTagtag;/*IDofpagecontainedinbuffer*///buffer索引编号(0开始)intbuf_id;/*buffer'sindexnumber(from0)*//*stateofthetag,containingflags,refcountandusagecount*///tag状态,包括flags/refcount和usagecountpg_atomic_uint32state;//pin-count等待进程IDintwait_backend_pid;/*backendPIDofpin-countwaiter*///空闲链表链中下一个空闲的bufferintfreeNext;/*linkinfreelistchain*///缓冲区内容锁LWLockcontent_lock;/*tolockaccesstobuffercontents*/}BufferDesc;

BufferTag
Buffer tag标记了buffer存储的是磁盘中哪个block

/**Buffertagidentifieswhichdiskblockthebuffercontains.*Buffertag标记了buffer存储的是磁盘中哪个block**Note:theBufferTagdatamustbesufficienttodeterminewheretowritethe*block,withoutreferencetopg_classorpg_tablespaceentries.It's*possiblethatthebackendflushingthebufferdoesn'tevenbelievethe*relationisvisibleyet(itsxactmayhavestartedbeforethexactthat*createdtherel).Thestoragemanagermustbeabletocopeanyway.*注意:BufferTag必须足以确定如何写block而不需要参照pg_class或者pg_tablespace数据字典信息.*有可能后台进程在刷新缓冲区的时候深圳不相信关系是可见的(事务可能在创建rel的事务之前).*存储管理器必须可以处理这些事情.**Note:ifthere'sanypadbytesinthestruct,INIT_BUFFERTAGwillhave*tobefixedtozerothem,sincethisstructisusedasahashkey.*注意:如果在结构体中有填充的字节,INIT_BUFFERTAG必须将它们固定为零,因为这个结构体用作散列键.*/typedefstructbuftag{//物理relation标识符RelFileNodernode;/*physicalrelationidentifier*/ForkNumberforkNum;//相对于relation起始的块号BlockNumberblockNum;/*blknumrelativetobeginofreln*/}BufferTag;

SMgrRelation
smgr.c维护一个包含SMgrRelation对象的hash表,SMgrRelation对象本质上是缓存的文件句柄.

/**smgr.cmaintainsatableofSMgrRelationobjects,whichareessentially*cachedfilehandles.AnSMgrRelationiscreated(ifnotalreadypresent)*bysmgropen(),anddestroyedbysmgrclose().Notethatneitherofthese*operationsimplyI/O,theyjustcreateordestroyahashtableentry.*(Butsmgrclose()mayreleaseassociatedresources,suchasOS-levelfile*descriptors.)*smgr.c维护一个包含SMgrRelation对象的hash表,SMgrRelation对象本质上是缓存的文件句柄.*SMgrRelation对象(如非现成)通过smgropen()方法创建,通过smgrclose()方法销毁.*注意:这些操作都不会执行I/O操作,只会创建或者销毁哈希表条目.*(但是smgrclose()方法可能会释放相关的资源,比如OS基本的文件描述符)**AnSMgrRelationmayhavean"owner",whichisjustapointertoitfrom*somewhereelse;smgr.cwillclearthispointeriftheSMgrRelationis*closed.Weusethistoavoiddanglingpointersfromrelcachetosmgr*withouthavingtomakethesmgrexplicitlyawareofrelcache.There*can'tbemorethanone"owner"pointerperSMgrRelation,butthat's*allweneed.*SMgrRelation可能会有"宿主",这个宿主可能只是从某个地方指向它的指针而已;*如SMgrRelationsmgr.c会清除该指针.这样做可以避免从relcache到smgr的悬空指针,*而不必要让smgr显式的感知relcache(也就是隔离了smgr了relcache).*每个SMgrRelation不能跟多个"owner"指针关联,但这就是我们所需要的.**SMgrRelationsthatdonothavean"owner"areconsideredtobetransient,*andaredeletedatendoftransaction.*SMgrRelations如无owner指针,则被视为临时对象,在事务的最后被删除.*/typedefstructSMgrRelationData{/*rnodeisthehashtablelookupkey,soitmustbefirst!*///--------rnode是哈希表的搜索键,因此在结构体的首位//关系物理定义IDRelFileNodeBackendsmgr_rnode;/*relationphysicalidentifier*//*pointertoowningpointer,orNULLifnone*///---------指向拥有的指针,如无则为NULLstructSMgrRelationData**smgr_owner;/**Thesenextthreefieldsarenotactuallyusedormanipulatedbysmgr,*exceptthattheyareresettoInvalidBlockNumberuponacacheflush*event(inparticular,upontruncationoftherelation).Higherlevels*storecachedstateheresothatitwillberesetwhentruncation*happens.Inallthreecases,InvalidBlockNumbermeans"unknown".*接下来的3个字段实际上并不用于或者由smgr管理,*除非这些表里在cacheflushevent发生时被重置为InvalidBlockNumber*(特别是在关系被截断时).*在这里,更高层的存储缓存了状态因此在截断发生时会被重置.*在这3种情况下,InvalidBlockNumber都意味着"unknown".*///当前插入的目标blocBlockNumbersmgr_targblock;/*currentinsertiontargetblock*///最后已知的fsmfork大小BlockNumbersmgr_fsm_nblocks;/*lastknownsizeoffsmfork*///最后已知的vmfork大小BlockNumbersmgr_vm_nblocks;/*lastknownsizeofvmfork*//*additionalpublicfieldsmaysomedayexisthere*///-------未来可能新增的公共域/**Fieldsbelowhereareintendedtobeprivatetosmgr.candits*submodules.Donottouchthemfromelsewhere.*下面的字段是smgr.c及其子模块私有的,不要从其他模块接触这些字段.*///存储管理器选择器intsmgr_which;/*storagemanagerselector*//**formd.c;per-forkarraysofthenumberofopensegments*(md_num_open_segs)andthesegmentsthemselves(md_seg_fds).*用于md.c,打开段(md_num_open_segs)和段自身(md_seg_fds)的数组(每个fork一个)*/intmd_num_open_segs[MAX_FORKNUM+1];struct_MdfdVec*md_seg_fds[MAX_FORKNUM+1];/*ifunowned,listlinkinlistofallunownedSMgrRelations*///如没有宿主,未宿主的SMgrRelations链表的链表链接.structSMgrRelationData*next_unowned_reln;}SMgrRelationData;typedefSMgrRelationData*SMgrRelation;

RelFileNodeBackend
组合relfilenode和后台进程ID,用于提供需要定位物理存储的所有信息.

/**AugmentingarelfilenodewiththebackendIDprovidesalltheinformation*weneedtolocatethephysicalstorage.ThebackendIDisInvalidBackendId*forregularrelations(thoseaccessibletomorethanonebackend),orthe*owningbackend'sIDforbackend-localrelations.Backend-localrelations*arealwaystransientandremovedincaseofadatabasecrash;theyare*neverWAL-loggedorfsync'd.*组合relfilenode和后台进程ID,用于提供需要定位物理存储的所有信息.*对于普通的关系(可通过多个后台进程访问),后台进程ID是InvalidBackendId;*如为临时表,则为自己的后台进程ID.*临时表(backend-localrelations)通常是临时存在的,在数据库崩溃时删除,无需WAL-logged或者fsync.*/typedefstructRelFileNodeBackend{RelFileNodenode;//节点BackendIdbackend;//后台进程}RelFileNodeBackend;

StrategyControl
共享的空闲链表控制信息

/**Thesharedfreelistcontrolinformation.*共享的空闲链表控制信息.*/typedefstruct{/*Spinlock:protectsthevaluesbelow*///自旋锁,用于保护下面的值slock_tbuffer_strategy_lock;/**Clocksweephand:indexofnextbuffertoconsidergrabbing.Notethat*thisisn'taconcretebuffer-weonlyeverincreasethevalue.So,to*getanactualbuffer,itneedstobeusedmoduloNBuffers.*Clocksweephand:下一个考虑交换出去的buffer索引.*注意这并不是一个精确的buffer-我们只是曾经增加值而已.*因此,获得一个实际的buffer,需要取模(使用NBuffers).*/pg_atomic_uint32nextVictimBuffer;//未使用的buffers链表头部intfirstFreeBuffer;/*Headoflistofunusedbuffers*///未使用的buffers链表尾部intlastFreeBuffer;/*Tailoflistofunusedbuffers*//**NOTE:lastFreeBufferisundefinedwhenfirstFreeBufferis-1(thatis,*whenthelistisempty)*注意:如firstFreeBuffer是-1,则lastFreeBuffer是未定义的.*(这意味着,当链表是空的时候会出现这种情况)*//**Statistics.Thesecountersshouldbewideenoughthattheycan't*overflowduringasinglebgwritercycle.*统计信息.这些计数器需要足够大,以确保在单个bgwriter循环时不会溢出.*///完成一轮clocksweep循环,进行计数uint32completePasses;/*Completecyclesoftheclocksweep*///自上次重置后分配的bufferspg_atomic_uint32numBufferAllocs;/*Buffersallocatedsincelastreset*//**Bgworkerprocesstobenotifieduponactivityor-1ifnone.See*StrategyNotifyBgWriter.*活动时通知Bgworker进程,否则该值为-1.详细参见StrategyNotifyBgWriter.*/intbgwprocno;}BufferStrategyControl;/*Pointerstosharedstate*///指向BufferStrategyControl结构体的指针staticBufferStrategyControl*StrategyControl=NULL;二、源码解读

StrategyGetBuffer在BufferAlloc()中,由bufmgr调用,用于获得下一个候选的buffer.
其主要的处理逻辑如下:
1.初始化相关变量
2.如策略对象不为空,则从环形缓冲区中获取buffer,如成功则返回buf
3.如需要,则唤醒后台进程bgwriter,从共享内存中读取一次,然后根据该值设置latch
4.计算buffer分配请求,这样bgwriter可以估算buffer消耗的比例.
5.检查freelist中是否存在buffer
5.1如存在,则执行相关判断逻辑,如成功,则返回buf
5.2如不存在
5.2.1则使用clock sweep算法,选择buffer,执行相关判断,如成功,则返回buf
5.2.2如无法获取,在尝试过trycounter次后,报错

/**StrategyGetBuffer**Calledbythebufmgrtogetthenextcandidatebuffertousein*BufferAlloc().TheonlyhardrequirementBufferAlloc()hasisthat*theselectedbuffermustnotcurrentlybepinnedbyanyone.*在BufferAlloc()中,由bufmgr调用,用于获得下一个候选的buffer.*BufferAlloc()中唯一稍微困难的需求是选择的buffer不能被其他后台进程pinned.**strategyisaBufferAccessStrategyobject,orNULLfordefaultstrategy.*strategy是BufferAccessStrategy对象,如为默认策略,则为NULL.**Toensurethatnooneelsecanpinthebufferbeforewedo,wemust*returnthebufferwiththebufferheaderspinlockstillheld.*为了确保没有其他后台进程在我们完成之前pinbuffer,必须返回仍持有bufferheader自旋锁的buffer.*/BufferDesc*StrategyGetBuffer(BufferAccessStrategystrategy,uint32*buf_state){BufferDesc*buf;//buffer描述符intbgwprocno;inttrycounter;//尝试次数//避免重复的依赖和解依赖uint32local_buf_state;/*toavoidrepeated(de-)referencing*//**Ifgivenastrategyobject,seewhetheritcanselectabuffer.We*assumestrategyobjectsdon'tneedbuffer_strategy_lock.*如果给定了一个策略对象,看看是否可以选择一个buffer.*我们假定策略对象不需要buffer_strategy_lock锁.*/if(strategy!=NULL){//从环形缓冲区中获取buffer,如获取成功,则返回该bufferbuf=GetBufferFromRing(strategy,buf_state);if(buf!=NULL)returnbuf;}/**Ifasked,weneedtowakenthebgwriter.Sincewedon'twanttorelyon*aspinlockforthisweforceareadfromsharedmemoryonce,andthen*setthelatchbasedonthatvalue.Weneedtogothroughthatlength*becauseotherwisebgprocnomightberesetwhile/afterwecheckbecause*thecompilermightjustrereadfrommemory.*如需要,则唤醒后台进程bgwriter.*我们不希望依赖自旋锁实现这一点,所以强制从共享内存中读取一次,然后根据该值设置latch.*我们需要走完这一步,否则的话bgprocno在检查期间或之后被重置,因为编译器可能重新从内存中读取数据.**Thiscanpossiblysetthelatchofthewrongprocessifthebgwriter*diesinthewrongmoment.ButsincePGPROC->procLatchisnever*deallocatedtheworstconsequenceofthatisthatwesetthelatchof*somearbitraryprocess.*如果bgwriter出现异常宕机,可能会出现latch被设置为错误的进程.*但是由于PGPROC->procLatch从来没有被释放过,最坏的结果是我们设置了一些任意进程的latch。*/bgwprocno=INT_ACCESS_ONCE(StrategyControl->bgwprocno);if(bgwprocno!=-1){//---如bgwprocno不是-1/*resetbgwprocnofirst,beforesettingthelatch*///在设置latch前,首先重置bgwprocno为-1StrategyControl->bgwprocno=-1;/**NotacquiringProcArrayLockherewhichisslightlyicky.It's*actuallyfinebecauseprocLatchisn'teverfreed,sowejustcan*potentiallysetthewrongprocess'(ornoprocess')latch.*在这里不需要请求"令人生厌"的ProcArrayLock.*因为procLatch未曾释放过,因此实际上是没有问题的,*所以我们可能会设置错误的进程(或没有进程)latch。*/SetLatch(&ProcGlobal->allProcs[bgwprocno].procLatch);}/**Wecountbufferallocationrequestssothatthebgwritercanestimate*therateofbufferconsumption.Notethatbuffersrecycledbya*strategyobjectareintentionallynotcountedhere.*计算buffer分配请求,这样bgwriter可以估算buffer消耗的比例.*注意通过策略对象进行的buffer回收不会在这里计算.*/pg_atomic_fetch_add_u32(&StrategyControl->numBufferAllocs,1);/**Firstcheck,withoutacquiringthelock,whetherthere'sbuffersinthe*freelist.Sinceweotherwisedon'trequirethespinlockinevery*StrategyGetBuffer()invocation,it'dbesadtoacquireithere-*uselesslyinmostcases.Thatobviouslyleavesaracewhereabufferis*putonthefreelistbutwedon'tseethestoreyet-butthat'spretty*harmless,it'lljustgetusedduringthenextbufferacquisition.*不需要请求锁,首次检查在freelist中是否存在buffer.*因为我们不需要在每次StrategyGetBuffer()调用时都使用自旋锁,*在这里请求自旋锁有点郁闷--因为大多数情况下都没有用.*这显然存在一个竞争,其中缓冲区被放在空闲列表中,但进程却看不到存储*--但这是无害的,在下次buffer申请期间使用.**Ifthere'sbuffersonthefreelist,acquirethespinlocktopopone*bufferofthefreelist.Thencheckwhetherthatbufferisusableand*repeatifnot.*如果在空闲列表中有buffer存在,请求自旋锁,从空闲列表中弹出一个可用的buffer.*然后检查该buffer是否可用,如不可用则继续处理.**NotethatthefreeNextfieldsareconsideredtobeprotectedbythe*buffer_strategy_locknottheindividualbufferspinlocks,soit'sOKto*manipulatethemwithoutholdingthespinlock.*注意freeNext字段通过buffer_strategy_lock锁来保护,而不是使用独立的缓冲区自旋锁保护,*因此不需要持有自旋锁就可以维护这些字段.*/if(StrategyControl->firstFreeBuffer>=0){while(true){/*Acquirethespinlocktoremoveelementfromthefreelist*///请求自旋锁,删除空闲链表中的元素SpinLockAcquire(&StrategyControl->buffer_strategy_lock);if(StrategyControl->firstFreeBuffer<0){//如无空闲空间,则马上跳出循环SpinLockRelease(&StrategyControl->buffer_strategy_lock);break;}//获取缓冲描述符buf=GetBufferDescriptor(StrategyControl->firstFreeBuffer);Assert(buf->freeNext!=FREENEXT_NOT_IN_LIST);/*Unconditionallyremovebufferfromfreelist*///无条件的清除空闲链表中的bufferStrategyControl->firstFreeBuffer=buf->freeNext;buf->freeNext=FREENEXT_NOT_IN_LIST;/**Releasethelocksosomeoneelsecanaccessthefreelistwhile*wecheckoutthisbuffer.*释放锁,这样其他进程在我们检查该缓冲的时候可以访问空闲链表*/SpinLockRelease(&StrategyControl->buffer_strategy_lock);/**Ifthebufferispinnedorhasanonzerousage_count,wecannot*useit;discarditandretry.(ThiscanonlyhappenifVACUUM*putavalidbufferinthefreelistandthensomeoneelseused*itbeforewegottoit.It'sprobablyimpossiblealtogetheras*of8.3,butwe'dbettercheckanyway.)*如果缓冲pinned或者usage_count非0,则不能使用该buffer,丢弃并重试.*(这种情况发生在VACUUM把一个有效的buffer放在空闲链表中,然后其他进程提前获得了这个buffer.*在8.3中是完全不可能的,但最好执行该检查)*///锁定缓冲头部local_buf_state=LockBufHdr(buf);if(BUF_STATE_GET_REFCOUNT(local_buf_state)==0&&BUF_STATE_GET_USAGECOUNT(local_buf_state)==0){//refcount==0&&usagecount==0if(strategy!=NULL)//非默认策略,则添加到环形缓冲区中AddBufferToRing(strategy,buf);//设置输出参数*buf_state=local_buf_state;//返回bufreturnbuf;}//不满足条件,解锁bufferheaderUnlockBufHdr(buf,local_buf_state);}}/*Nothingonthefreelist,sorunthe"clocksweep"algorithm*///空闲链表中找不到或者满足不了条件,则执行"clocksweep"算法//intNBuffers=1000;trycounter=NBuffers;//尝试次数for(;;){//-------循环//获取buffer描述符buf=GetBufferDescriptor(ClockSweepTick());/**Ifthebufferispinnedorhasanonzerousage_count,wecannotuse*it;decrementtheusage_count(unlesspinned)andkeepscanning.*如果buffer已pinned,或者有一个非零值的usage_count,不能使用这个buffer.*减少usage_count(除非已pinned)继续扫描.*/local_buf_state=LockBufHdr(buf);if(BUF_STATE_GET_REFCOUNT(local_buf_state)==0){//-----refcount==0if(BUF_STATE_GET_USAGECOUNT(local_buf_state)!=0){//usage_count<>0//usage_count-1local_buf_state-=BUF_USAGECOUNT_ONE;//重置尝试次数trycounter=NBuffers;}else{//usage_count=0/*Foundausablebuffer*///发现一个可用的bufferif(strategy!=NULL)//添加到该策略的环形缓冲区中AddBufferToRing(strategy,buf);//输出参数赋值*buf_state=local_buf_state;//返回bufreturnbuf;}}elseif(--trycounter==0){//-----refcount<>0&&--trycounter==0/**We'vescannedallthebufferswithoutmakinganystatechanges,*soallthebuffersarepinned(orwerewhenwelookedatthem).*Wecouldhopethatsomeonewillfreeoneeventually,butit's*probablybettertofailthantoriskgettingstuckinan*infiniteloop.*在没有改变任何状态的情况,我们已经完成了所有buffers的遍历,*因此所有的buffers已pinned(或者在搜索的时候pinned).*我们希望某些进程会周期性的释放buffer,但如果实在拿不到,那报错总比傻傻的死循环要好.*/UnlockBufHdr(buf,local_buf_state);elog(ERROR,"nounpinnedbuffersavailable");}//解锁bufferheaderUnlockBufHdr(buf,local_buf_state);}}三、跟踪分析

测试脚本,查询数据表:

10:01:54(xdb@[local]:5432)testdb=#select*fromt1limit10;

启动gdb,设置断点

(gdb)Continuing.Breakpoint1,StrategyGetBuffer(strategy=0x0,buf_state=0x7ffcc97fb4ec)atfreelist.c:212212if(strategy!=NULL)(gdb)

输入参数
strategy=NULL,策略对象,使用默认策略

(gdb)p*buf_state$1=0

1.初始化相关变量
2.如策略对象不为空,则从环形缓冲区中获取buffer,如成功则返回buf
3.如需要,则唤醒后台进程bgwriter,从共享内存中读取一次,然后根据该值设置latch

(gdb)n231bgwprocno=INT_ACCESS_ONCE(StrategyControl->bgwprocno);(gdb)232if(bgwprocno!=-1)(gdb)235StrategyControl->bgwprocno=-1;(gdb)pbgwprocno$2=112(gdb)pStrategyControl$3=(BufferStrategyControl*)0x7f8607b21700(gdb)p*StrategyControl$4={buffer_strategy_lock=0'\000',nextVictimBuffer={value=0},firstFreeBuffer=134,lastFreeBuffer=65535,completePasses=0,numBufferAllocs={value=0},bgwprocno=112}(gdb)n242SetLatch(&ProcGlobal->allProcs[bgwprocno].procLatch);(gdb)

4.计算buffer分配请求,这样bgwriter可以估算buffer消耗的比例.

(gdb)250pg_atomic_fetch_add_u32(&StrategyControl->numBufferAllocs,1);

5.检查freelist中是否存在buffer

(gdb)268if(StrategyControl->firstFreeBuffer>=0)

5.1如存在,则执行相关判断逻辑,如成功,则返回buf

(gdb)n273SpinLockAcquire(&StrategyControl->buffer_strategy_lock);(gdb)275if(StrategyControl->firstFreeBuffer<0)(gdb)281buf=GetBufferDescriptor(StrategyControl->firstFreeBuffer);(gdb)282Assert(buf->freeNext!=FREENEXT_NOT_IN_LIST);(gdb)p*buf$5={tag={rnode={spcNode=0,dbNode=0,relNode=0},forkNum=InvalidForkNumber,blockNum=4294967295},buf_id=134,state={value=0},wait_backend_pid=0,freeNext=135,content_lock={tranche=54,state={value=536870912},waiters={head=2147483647,tail=2147483647}}}(gdb)n285StrategyControl->firstFreeBuffer=buf->freeNext;(gdb)286buf->freeNext=FREENEXT_NOT_IN_LIST;(gdb)292SpinLockRelease(&StrategyControl->buffer_strategy_lock);(gdb)301local_buf_state=LockBufHdr(buf);(gdb)302if(BUF_STATE_GET_REFCOUNT(local_buf_state)==0(gdb)303&&BUF_STATE_GET_USAGECOUNT(local_buf_state)==0)(gdb)305if(strategy!=NULL)(gdb)307*buf_state=local_buf_state;(gdb)308returnbuf;(gdb)p*buf_state$6=4194304(gdb)p*buf$7={tag={rnode={spcNode=0,dbNode=0,relNode=0},forkNum=InvalidForkNumber,blockNum=4294967295},buf_id=134,state={value=4194304},wait_backend_pid=0,freeNext=-2,content_lock={tranche=54,state={value=536870912},waiters={head=2147483647,tail=2147483647}}}(gdb)

返回结果,回到BufferAlloc

(gdb)n358}(gdb)BufferAlloc(smgr=0x22a38a0,relpersistence=112'p',forkNum=MAIN_FORKNUM,blockNum=0,strategy=0x0,foundPtr=0x7ffcc97fb5c3)atbufmgr.c:10731073Assert(BUF_STATE_GET_REFCOUNT(buf_state)==0);(gdb)

“PostgreSQL中StrategyGetBuffer函数有什么作用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!