ソースコードからPostgreSQLのbgwriter_を理解するlru_maxpages

7798 ワード

コード:srcbackendstoragebufferbufmgr.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 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も、再書き込みを停止します.