对PostgreSQL中bufmgr.c 中 num_to_scan 的初步理解
开始
把BgBufferSync 的代码内容简略化,得到:
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); }
一开始的时候,bufs_to_lap =4096, 就是 4096*8K=32MB ,相当于 Shared_buffers 的数量。
然后如果对代码进行跟踪,可以发现 bufs_to_lap 一开始就是 4096,然后逐次减1。
[作者:技术者高健@博客园 mail: luckyjackgao@gmail.com ]
结束