Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YbG7k-000379-H5 for pgsql-performance@arkaria.postgresql.org; Thu, 26 Mar 2015 22:19:36 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YbG7j-0006cq-VH for pgsql-performance@arkaria.postgresql.org; Thu, 26 Mar 2015 22:19:36 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YbG7i-0006b3-MA for pgsql-performance@postgresql.org; Thu, 26 Mar 2015 22:19:34 +0000 Received: from mail-wg0-f49.google.com ([74.125.82.49]) by magus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1YbG7e-0002jG-3X for pgsql-performance@postgresql.org; Thu, 26 Mar 2015 22:19:33 +0000 Received: by wgra20 with SMTP id a20so79824007wgr.3 for ; Thu, 26 Mar 2015 15:19:28 -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:cc:subject:references:in-reply-to:content-type :content-transfer-encoding; bh=jzdhq/LTMw/L6XOep9Sjh/lDh3GejfP/dcykVMwVOtI=; b=gPwg2o7VbZ6m8RZWhR/QRDwBWnDS3BldI3ZkdTzox4QLQ6HA/MOUjSH4J8S06mnQCp nlX3t9NCVtBc/fol88U6g+Ymd6/oimwvN+iugUcGfN3nc+VvlfvTjcBpwVBPyj/5PIn0 zQyPeLACOLGIT0wbawFzWb0x4GxRzEZ96QbRlJcDOiATypdcFz0wim5QRSbMNwlDuSNI gFQlRmwt6l09W43kJ0nS15VU6AwwKbt6CwaqWak/XR/g9E1p+8O2bIhMGW1wZ6/bj+A+ MOx4tj0ibtM7fIN8Y+JK2yLrg8XiwvFHUmzYLhigvKQHpV0UpsZasRkD4E+64PiyiRa9 0zaQ== X-Gm-Message-State: ALoCoQmcQPLN3tWN951PHa5imNXWKvbpI5omoGkmtkJsY7v6bl3BpSEOBzrWsmvr4s1Gf0YjsxBtebxUWj+2Kt5VSvf91QpDw5gcjyR2gM9GdtRHTbUJIDxW3dcYFYHNTTBmEo1StBMnuU+pOGFvwZRoFnB2TfHT7vW2i0zYaeb65bQdmf0wHdytDnYSBZ9+KtWWJXVHmcnf X-Received: by 10.180.96.37 with SMTP id dp5mr51912258wib.41.1427408368285; Thu, 26 Mar 2015 15:19:28 -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 ei8sm773803wib.10.2015.03.26.15.19.26 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 26 Mar 2015 15:19:27 -0700 (PDT) Message-ID: <551485DB.8060901@2ndquadrant.com> Date: Thu, 26 Mar 2015 23:19:07 +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: Jeff Janes CC: "pgsql-performance@postgresql.org" Subject: Re: Index only scan sometimes switches to sequential scan for small amount of rows References: <5512ADD2.7080402@2ndquadrant.com> <7439.1427304380@sss.pgh.pa.us> <5513FF23.8090603@2ndquadrant.com> 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 26.3.2015 17:35, Jeff Janes wrote: > On Thu, Mar 26, 2015 at 5:44 AM, Tomas Vondra > > wrote: > >> That might work IMO, but maybe we should increase the coefficient a >> bit (say, from 1.25 to 2), not to produce needlessly long MCV >> lists. > > That wouldn't work here, because at the point of decision the value > present 99 times contributes half the average, so the average is 50, > and of course it can't possibly be twice of that. Oh, right. How could I miss that? ;-) > I have a patch, but is there a way to determine how it affects a > wide variety of situations? I guess run `make installcheck`, then > analyze, then dump pg_stats, with the patch and without the patch, > and then compare the dumpsj? I doubt there's such way. I'd argue that if you can show this always generates longer MCV lists, we can assume the stats are probably more accurate, and thus the plans should be better. Of course, there's always the possibility that the plan was good by luck, and improving the estimates will result in a worse plan. But I don't think we can really fix that :-( >>> It is also grossly inconsistent with the other behavior. If they >>> are "29900; 98; 2" then all three go into the MCV. >> >> Isn't the mincount still 12500? How could all three get into the >> MCV? > > If all observed values are observed at least twice, it takes a > different path through the code. It just keeps them all in the MCV > list. That is what is causing the instability for the OP. If the 3rd > most common is seen twice, then all three are kept. If it is seen > once, then only the most common is kept. See if statements at 2494 > and 2585 > > else if (toowide_cnt == 0 && nmultiple == ndistinct) > > if (track_cnt == ndistinct .... Aha, I think I see it now. I've been concentrating on this code: avgcount = (double) samplerows / ndistinct; /* set minimum threshold count to store a value */ mincount = avgcount * 1.25; if (mincount < 2) mincount = 2; but this is actually too late, because first we do this: else if (toowide_cnt == 0 && nmultiple == ndistinct) { stats->stadistinct = ndistinct; } and that only happens if each item is observed at least 2x in the sample (and the actual Haas and Stokes estimator it not used). And then we do this: if (track_cnt == ndistinct && toowide_cnt == 0 && stats->stadistinct > 0 && track_cnt <= num_mcv) { num_mcv = track_cnt; } so that we track everything. If at least one value is seen only 1x, it works differently, and we use the code with (1.25*avgcount) threshold. I wonder where the 1.25x threshold comes from - whether it's something we came up with, or if it comes from some paper. I guess the former. -- 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