zoukankan      html  css  js  c++  java
  • 从源代码上理解 PostgreSQL 的 bgwriter_lru_maxpages

    先看代码: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,也将停止再写入。

  • 相关阅读:
    【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)
    【LeetCode】151. Reverse Words in a String
    【LeetCode】152. Maximum Product Subarray
    【LeetCode】154. Find Minimum in Rotated Sorted Array II (3 solutions)
    如何使用 Chrome 浏览器调试动态加载的 Javascript 脚本
    如何对数据库中的表以及表中的字段进行重命名
    关于 sql server 数据库权限乱七八糟的一些东西
    对 sql server 数据库的备份进行加密
    使用vs的查找功能,简单大概的统计vs中的代码行数
    在 sql server 中,查询 数据库的大小 和 数据库中各表的大小
  • 原文地址:https://www.cnblogs.com/gaojian/p/2737250.html
Copyright © 2011-2022 走看看