深入理解缓冲区(十一)

来源:互联网 发布:淘宝网男款休闲鞋 编辑:程序博客网 时间:2024/06/06 09:10

4.1.6        Buf置换管理算法

Clock sweep算法总结如下:

1.         PG维护一个“freelist”,将空闲的块,置于其上,当有空闲块时,直接从空闲块中取;当有块不再使用,可以归入到空闲块中。

2.         当空闲块已经用完,则执行淘汰策略,如下:

3.         缓冲块使用频率较高,不将其替换,只是将它的使用计数减一

4.         如果缓冲块的引用计数是0,使用计数也是0,就是说当前使用这个缓冲块,而且这个缓冲块最近无人使用,那么就可以重新使用了,这时,淘汰这个块上写的信息(脏页,刷出信息到外存),然后供重新使用

5.         如果已经遍历了所有的缓冲块,发现所有缓冲块都被别人占用,则报错,因为无法找到可用的缓冲区块了

 

 

PostgreSQL对于缓冲区管理算法有如下描述(src/backend/storage/buffer/README):

Normal Buffer Replacement Strategy

----------------------------------

There is a "free list" of buffers that are prime candidates for replacement.

In particular, buffers that are completely free (contain no valid page) are

always in this list.  We could also throw buffers into this list if we

consider their pages unlikely to be needed soon; however, the current

algorithm never does that.  The list is singly-linked using fields in the

buffer headers; we maintain head and tail pointers in global variables.

(Note: although the list links are in the buffer headers, they are

considered to be protected by the BufFreelistLock, not the buffer-header

spinlocks.)  To choose a victim buffer to recycle when there are no free

buffers available, we use a simple clock-sweep algorithm, which avoids the

need to take system-wide locks during common operations.

 

这段话表明,缓冲区置换调度策略,首先是buf维护一个空闲链表,如果空闲列表有空闲块,则优先使用空闲块;否则,则从缓冲区的数据库缓存块区根据置换策略淘汰缓存块,刷出脏数据,然后把这个以刷出数据的缓存块分配出去。

而置换,则是简单的(蓝色粗体字)“clock-sweep”(时钟置换)算法。

 

另外,除了“clock-sweep”算法,PostgreSQL还在其他场景下使用了其他算法,如下(非本文主要讨论之处,不在详述):

Buffer Ring Replacement Strategy

---------------------------------

When running a query that needs to access a large number of pages just once,

such as VACUUM or a large sequential scan, a different strategy is used.


原创粉丝点击