Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Yakgf-0007zk-8b for pgsql-performance@arkaria.postgresql.org; Wed, 25 Mar 2015 12:45:33 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Yakge-0002TN-M9 for pgsql-performance@arkaria.postgresql.org; Wed, 25 Mar 2015 12:45:32 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Yakgc-0002QW-T1 for pgsql-performance@postgresql.org; Wed, 25 Mar 2015 12:45:31 +0000 Received: from mail-wg0-f50.google.com ([74.125.82.50]) by makus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1YakgZ-0003kI-8U for pgsql-performance@postgresql.org; Wed, 25 Mar 2015 12:45:29 +0000 Received: by wgra20 with SMTP id a20so25955185wgr.3 for ; Wed, 25 Mar 2015 05:45:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:message-id:date:from:organization:user-agent :mime-version:to:subject:references:in-reply-to:content-type :content-transfer-encoding; bh=xDr0G4ZoqK2iL9LRvBKLFthQmA4uYs4+HZCS4btZPmE=; b=Z1YEBmuuNyZnByT560HUvGQMnZa7bMLAWqkF4G8KKZg8lal8szYUFpVyCAQ4s8IW8/ rf5QWhGkV8ExMmMIal/CkZ1KdGRnyZ3eXuBgXW6i/K+xS3V1jIIdfaHI94N+ZqcuQSbP OinP81mf8aQhpe1fSUGwhYlaid+LbSHzh7SgYuuUZa21UDc0Ay+EhYisgiitccQJk8vw v7lTPZoFTLlQpvl3iwhtDzpi+0qj0Op9RlSjw8rREeEcO8mqdvAigSbwQ+zMerSTPKoy +wS7wbtgFTkGvgUNCbzzNjbK84+GYI0QUAvPV/ZN4nqQ5rD3Gff03au4trEilA/QhWZJ 054w== X-Gm-Message-State: ALoCoQmB0lwq3JwjsTKWqqVtb7iVA9SCfnfOxB9BtlgbahUAkwpLspSUaQfcK6tovD48f8T879hqUgA8uNx755ZO/GwfNomQYjkq/tKRz4qixlVRoAo7jrAJ/C+jWx5PIHQnmuoVuuS0QDQdlKSGA9IXhxDV87Q/R1D+FK3R3r/hrDnSAJFdXvubJhDt/TlhPjKorKeeiRVK X-Received: by 10.180.79.170 with SMTP id k10mr37228475wix.1.1427287524973; Wed, 25 Mar 2015 05:45:24 -0700 (PDT) Received: from [192.168.1.163] (ip-78-45-139-138.net.upcbroadband.cz. [78.45.139.138]) by mx.google.com with ESMTPSA id fm10sm4152229wib.7.2015.03.25.05.45.23 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 25 Mar 2015 05:45:24 -0700 (PDT) Message-ID: <5512ADD2.7080402@2ndquadrant.com> Date: Wed, 25 Mar 2015 13:45:06 +0100 From: Tomas Vondra Organization: 2ndQuadrant User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Re: Index only scan sometimes switches to sequential scan for small amount of rows References: In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.6 (--) 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 On 25.3.2015 13:04, Feike Steenbergen wrote: ... > When analyzing pg_stats we have sometimes have the following: (Note: > 'NOT_YET_PRINTED' has not been found during this analyze, these are > real values) > > attname | status > inherited | f > null_frac | 0 > avg_width | 4 > n_distinct | 3 > most_common_vals | {PRINTED} > most_common_freqs | {0.996567} > histogram_bounds | {PREPARED,ERROR} > correlation | 0.980644 > > A question about this specific entry, which some of you may be able to > shed some light on: > > most_common_vals contains only 1 entry, why is this? I would expect to > see 3 entries, as it has n_distinct=3 To be included in the MCV list, the value has to actually appear in the random sample at least twice, IIRC. If the values are very rare (e.g. if you only have such 10 rows out of 3.5M), that may not happen. You may try increasing the statistics target for this column, which should make the sample larger and stats more detailed (max is 10000, which should use sample ~3M rows, i.e. almost the whole table). > When looking at > http://www.postgresql.org/docs/current/static/row-estimation-examples.html > we can see that an estimate > 5000 is what is to be expected for these > statistics: > > # select ( (1 - 0.996567)/2 * 3500000 )::int; > int4 > ------ > 6008 > (1 row) > > But why does it not record the frequency of 'PREPARED' and 'ERROR' > in most_common_*? Can you post results for this query? SELECT stats, COUNT(*) FROM print_list group by 1 I'd like to know how frequent the other values are. > > Our current strategies in mitigating this problem is decreasing the > autovacuum_*_scale_factor for this specific table, therefore > triggering more analyses and vacuums. I'm not sure this is a good solution. The problem is elsewhere, IMHO. > This is helping somewhat, as if the problem occurs it often solved > automatically if autoanalyze analyzes this table, it is analyzed > many times an hour currently. > > We can also increase the 'Stats target' for this table, which will > cause the statistics to contain information about 'NOT_YET_PRINTED' > more often, but even then, it may not find any of these records, as > they sometimes do not exist. This is a better solution, IMHO. > > Could you help us to find a strategy to troubleshoot this issue > further? You might also make the index scans cheaper, so that the switch to sequential scan happens later (when more rows are estimated). Try to decreasing random_page_cost from 4 (default) to 1.5 or something like that. It may hurt other queries, though, depending on the dataset size etc. -- Tomas Vondra http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance