且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

对PostgreSQL中bufmgr.c 中 num_to_scan 的初步理解

更新时间:2022-09-12 22:16:35

开始

把BgBufferSync 的代码内容简略化,得到:

对PostgreSQL中bufmgr.c 中 num_to_scan 的初步理解
bool                    
BgBufferSync(void)                    
{                    
    ……                
                    
    /* Used to compute how far we scan ahead */                
    long        strategy_delta;        
    int            bufs_to_lap;    
    ……                
    /* Variables for the scanning loop proper */                
    int            num_to_scan;    
    int            num_written;    
    int            reusable_buffers;    
    ……                
    if (saved_info_valid)                
    {                
        ……            
                    
        if ((int32) (next_passes - strategy_passes) > 0)            
        {            
                    
            /* we're one pass ahead of the strategy point */        
            bufs_to_lap = strategy_buf_id - next_to_clean;        
            ……        
        }            
        else if (next_passes == strategy_passes &&            
                 next_to_clean >= strategy_buf_id)    
        {            
                    
            /* on same pass, but ahead or at least not behind */        
            bufs_to_lap = NBuffers - (next_to_clean - strategy_buf_id);        
            ……        
        }            
        else            
        {            
            ……        
            bufs_to_lap = NBuffers;        
        }            
                    
    }                
    else                
    {                
        ……            
        bufs_to_lap = NBuffers;            
    }                
    ……                
                    
    /*                
     * 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.                
     */                
                    
    ……                
    num_to_scan = bufs_to_lap;                
    ……                
    /* Execute the LRU scan */                
    while (num_to_scan > 0 && reusable_buffers < upcoming_alloc_est)                
    {                
                    
        //added by gaojian            
        fprintf(stderr,"num_to_scan is: %d \n",num_to_scan);            
                    
        int    buffer_state = SyncOneBuffer(next_to_clean, true);        
                    
        if (++next_to_clean >= NBuffers)            
        {            
            next_to_clean = 0;        
                    
            elog(INFO,"------------------next_passes++.\n");        
            next_passes++;        
        }            
        num_to_scan--;            
        ……            
    }                
                    
    ……                
    /* Return true if OK to hibernate */                
    return (bufs_to_lap == 0 && recent_alloc == 0);                
}                    
对PostgreSQL中bufmgr.c 中 num_to_scan 的初步理解

一开始的时候,bufs_to_lap =4096, 就是 4096*8K=32MB ,相当于 Shared_buffers 的数量。

然后如果对代码进行跟踪,可以发现 bufs_to_lap 一开始就是 4096,然后逐次减1。






本文转自健哥的数据花园博客园博客,原文链接:http://www.cnblogs.com/gaojian/archive/2012/11/02/2750961.html,如需转载请自行联系原作者