博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
从源代码上理解 PostgreSQL 的 bgwriter_lru_maxpages
阅读量:7127 次
发布时间:2019-06-28

本文共 5054 字,大约阅读时间需要 16 分钟。

先看代码:src\backend\storage\buffer\bufmgr.c

/*                                     * BgBufferSync -- Write out some dirty buffers in the pool.                                     *                                     * This is called periodically by the background writer process.                                     *                                     * Returns true if it's appropriate for the bgwriter process to go into                                     * low-power hibernation mode.                (This happens if the strategy clock sweep                     * has been "lapped" and no buffer allocations have occurred recently,                                     * or if the bgwriter has been effectively disabled by setting                                     * bgwriter_lru_maxpages to 0.)                                     */                                    bool                                    BgBufferSync(void)                                    {                                        ……                                    /*                                     * If we're not running the LRU scan, just stop after doing the stats                                     * stuff.  We mark the saved state invalid so that we can recover sanely                                     * if LRU scan is turned back on later.                                     */                                    if (bgwriter_lru_maxpages <= 0)                                    {                                        saved_info_valid = false;                                    return true;                                }                                    ……                                                                        /*                                     * Now write out dirty reusable buffers, working forward from the                                     * next_to_clean point, until we have lapped the strategy scan, or cleaned                                     * enough buffers to match our estimate of the next cycle's allocation                                     * requirements, or hit the bgwriter_lru_maxpages limit.                                     */                                                                        /* Make sure we can handle the pin inside SyncOneBuffer */                                    ResourceOwnerEnlargeBuffers(CurrentResourceOwner);                                                                        num_to_scan = bufs_to_lap;                                    num_written = 0;                                    reusable_buffers = reusable_buffers_est;                                                                        /* Execute the LRU scan */                                    while (num_to_scan > 0 && reusable_buffers < upcoming_alloc_est)                                    {                                        int            buffer_state = SyncOneBuffer(next_to_clean, true);                                                            if (++next_to_clean >= NBuffers)                                    {                                        next_to_clean = 0;                                    next_passes++;                                }                                    num_to_scan--;                                                                        if (buffer_state & BUF_WRITTEN)                                    {                                        reusable_buffers++;                                    if (++num_written >= bgwriter_lru_maxpages)                                    {                                        BgWriterStats.m_maxwritten_clean++;                                    break;                                }                                }                                    else if (buffer_state & BUF_REUSABLE)                                        reusable_buffers++;                            }                                                                        BgWriterStats.m_buf_written_clean += num_written;                                                                        ……                                    /* Return true if OK to hibernate */                                    return (bufs_to_lap == 0 && recent_alloc == 0);                                }

从上述代码看出:

开宗明义,人家已经在注释中说了: This is called periodically by the background writer process.

而对于   bgwriter_lru_maxpages:

/*

* If we're not running the LRU scan, just stop after doing the stats
* stuff. We mark the saved state invalid so that we can recover sanely
* if LRU scan is turned back on later.
*/
if (bgwriter_lru_maxpages <= 0)
{
  saved_info_valid = false;
  return true;
}

如果  bgwriter_lru_maxpages <=0,则立即返回。根本不进行 脏数据读写。

再看:

while (num_to_scan > 0 && reusable_buffers < upcoming_alloc_est)

{
……
  if (buffer_state & BUF_WRITTEN)
  {
    reusable_buffers++;
    if (++num_written >= bgwriter_lru_maxpages)
    {
      BgWriterStats.m_maxwritten_clean++;
      break;
    }
  }
……
}

一旦超过 bgwriter_lru_maxpages,也将停止再写入。

 

转载地址:http://schel.baihongyu.com/

你可能感兴趣的文章
Linux多线程编程(不限Linux)
查看>>
Object中的方法
查看>>
swift -- 集合
查看>>
Oracle跟踪文件
查看>>
程序员学炒股(7) 股指期货收盘价对第二天开盘价有影响吗?
查看>>
关于离线缓存webView的新方法NSURLProtocol
查看>>
unittest详解(六) 断言
查看>>
F#与数学(I) – PowerPack中的数字类型
查看>>
Window Phone上的F# - 图形计算器
查看>>
lower_bound()返回值
查看>>
面向对象三大特性
查看>>
C语言调用Python
查看>>
scrapy 的框架的安装
查看>>
微信小程序 --- page.js文件
查看>>
Thinkphp --- 入口文件
查看>>
SQLServer2005创建定时作业任务
查看>>
linux服务器外网内网(双网络)搭建
查看>>
RAMCloud:内存云存储的内存分配机制
查看>>
android selector 用法
查看>>
[LintCode] Longest Increasing Subsequence
查看>>