Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1UzaUV-0006mE-Cr for pgsql-performance@arkaria.postgresql.org; Wed, 17 Jul 2013 22:46:35 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1UzaUU-00071f-Ob for pgsql-performance@arkaria.postgresql.org; Wed, 17 Jul 2013 22:46:34 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1UzaUT-00070H-BZ for pgsql-performance@postgresql.org; Wed, 17 Jul 2013 22:46:33 +0000 Received: from smtp.01.com ([199.36.142.181] helo=smtp-out-1.01.com) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1UzaUO-0005v9-RA for pgsql-performance@postgresql.org; Wed, 17 Jul 2013 22:46:32 +0000 Received: from localhost (localhost [127.0.0.1]) by smtp-out-1.01.com (Postfix) with ESMTP id AAE332940AA; Wed, 17 Jul 2013 17:46:27 -0500 (CDT) X-Virus-Scanned: amavisd-new at smtp-out-1.01.com Received: from smtp-out-1.01.com ([127.0.0.1]) by localhost (smtp-out-1.01.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Hw_wj9HIUnue; Wed, 17 Jul 2013 17:46:27 -0500 (CDT) Received: from smtp-out-1.01.com (localhost [127.0.0.1]) by smtp-out-1.01.com (Postfix) with ESMTP id 886042941BF; Wed, 17 Jul 2013 17:46:27 -0500 (CDT) Received: from localhost (localhost [127.0.0.1]) by smtp-out-1.01.com (Postfix) with ESMTP id 795912940AA; Wed, 17 Jul 2013 17:46:27 -0500 (CDT) X-Virus-Scanned: amavisd-new at smtp-out-1.01.com Received: from smtp-out-1.01.com ([127.0.0.1]) by localhost (smtp-out-1.01.com [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id lDoPoPx4PHJD; Wed, 17 Jul 2013 17:46:27 -0500 (CDT) Received: from [172.47.23.103] (70-36-143-10.dsl.dynamic.sonic.net [70.36.143.10]) by smtp-out-1.01.com (Postfix) with ESMTPSA id 126132941B3; Wed, 17 Jul 2013 17:46:26 -0500 (CDT) Message-ID: <51E71EBF.1030702@agliodbs.com> Date: Wed, 17 Jul 2013 15:46:23 -0700 From: Josh Berkus Organization: PostgreSQL Experts Inc. User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130623 Thunderbird/17.0.7 MIME-Version: 1.0 To: postgres performance list , Greg Smith Subject: bgwriter autotuning might be unnecessarily penalizing bursty workloads X-Enigmail-Version: 1.5.1 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org Greg, All: So, I've been running some stats over some customer workloads who are having issues with checkpoint spikes, and I'm finding that the bgwriter is almost entirely ineffective for them: pct_checkpoints_req | 33.0 avg_frequency_min | 2.30 avg_write_time_s | 112.17 avg_sync_time_s | 1.33 mb_written | 2387369.6 mb_written_per_min | 61.01 mb_per_checkpoint | 82.27 pct_checkpoint_buffers | 58.6 pct_bgwriter_buffers | 0.3 pct_backend_buffers | 41.1 bgwriter_halt_freq | 0.06 bgwriter_halt_potential | 70.11 buffer_allocation_ratio | 1.466 (query for the above is below) The key metric I'm looking at is that the bgwriter only took care of 0.3% of buffers. Yet average write throughput is around 1mb/s, and the bgwriter is capable of flushing 4mb/s, if it's waking up every 200ms. Of course, our first conclusion is that the writes are very bursty and the bgwriter is frequently hitting lru_maxpages. In fact, that seems to be the case, per bgwriter_halt_potential above (this is a measurement of the % of the time the bgwriter halted vs if all buffer writes were done in one continuous session where it couldn't keep up). And from the raw pg_stat_bgwriter: maxwritten_clean | 6950 So we're halting because we hit lru_maxpages a *lot*, which is keeping the bgwriter from keeping up. What gives? Well, digging into the docs, one thing I noticed was this note: "It then sleeps for bgwriter_delay milliseconds, and repeats. When there are no dirty buffers in the buffer pool, though, it goes into a longer sleep regardless of bgwriter_delay." combined with: "The number of dirty buffers written in each round is based on the number of new buffers that have been needed by server processes during recent rounds" ... so Greg built in the bgwriter autotuner with ramp-up/down behavior, where it sleeps longer and writes less if it hasn't been busy lately. But given the stats I'm looking at, I'm wondering if that isn't too much of a double-whammy for people with bursty workloads. That is, if you have several seconds of inactivity followed by a big write, then the bgwriter will wake up slowly (which, btw, is not manually tunable), and then write very little when it does wake up, at least in the first round. Of course, I may be misinterpreting the data in front of me ... I'm currently running a week-long test of raising lru_maxpages and decreasing bgwriter_delay to see how it affects things ... but I wanted to discuss it on-list. Bgwriter stats query follows: with bgstats as ( select checkpoints_timed, checkpoints_req, checkpoints_timed + checkpoints_req as checkpoints, checkpoint_sync_time, checkpoint_write_time, buffers_checkpoint, buffers_clean, maxwritten_clean, buffers_backend, buffers_backend_fsync, buffers_alloc, buffers_checkpoint + buffers_clean + buffers_backend as total_buffers, round(extract('epoch' from now() - stats_reset)/60)::numeric as min_since_reset, lru.setting::numeric as bgwriter_maxpages, delay.setting::numeric as bgwriter_delay from pg_stat_bgwriter cross join pg_settings as lru cross join pg_settings as delay where lru.name = 'bgwriter_lru_maxpages' and delay.name = 'bgwriter_delay' ) select round(checkpoints_req*100/checkpoints,1) as pct_checkpoints_req, round(min_since_reset/checkpoints,2) as avg_frequency_min, round(checkpoint_write_time::numeric/(checkpoints*1000),2) as avg_write_time_s, round(checkpoint_sync_time::numeric/(checkpoints*1000),2) as avg_sync_time_s, round(total_buffers/128::numeric,1) as mb_written, round(total_buffers/(128 * min_since_reset),2) as mb_written_per_min, round(buffers_checkpoint/(checkpoints*128::numeric),2) as mb_per_checkpoint, round(buffers_checkpoint*100/total_buffers::numeric,1) as pct_checkpoint_buffers, round(buffers_clean*100/total_buffers::numeric,1) as pct_bgwriter_buffers, round(buffers_backend*100/total_buffers::numeric,1) as pct_backend_buffers, round(maxwritten_clean*100::numeric/(min_since_reset*60000/bgwriter_delay),2) as bgwriter_halt_freq, round(maxwritten_clean*100::numeric/(buffers_clean/bgwriter_maxpages),2) as bgwriter_halt_potential, round(buffers_alloc::numeric/total_buffers,3) as buffer_allocation_ratio from bgstats; -- Josh Berkus PostgreSQL Experts Inc. http://pgexperts.com -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance