Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ed47h-0003jA-HH for pgsql-bugs@arkaria.postgresql.org; Sun, 21 Jan 2018 01:08:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ed47g-0001nR-QS for pgsql-bugs@arkaria.postgresql.org; Sun, 21 Jan 2018 01:08:36 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1ed47g-0001nI-Hc for pgsql-bugs@lists.postgresql.org; Sun, 21 Jan 2018 01:08:36 +0000 Received: from mail-wr0-x241.google.com ([2a00:1450:400c:c0c::241]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ed47b-0004N9-1r for pgsql-bugs@lists.postgresql.org; Sun, 21 Jan 2018 01:08:34 +0000 Received: by mail-wr0-x241.google.com with SMTP id x1so4899607wrb.5 for ; Sat, 20 Jan 2018 17:08:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=DRtfiwMAM8yTR7sJf38tvVC8dO1OlTbKNgp5BHA/JIE=; b=gbxC8gftRB0diNa25vVQprjexV96koNhXLftsxvgjquUcTBrL4s+KbA5ypWzineLE6 iVbBUaangSYeVkh9ictwhEGt+gMXeEwGfAETKPKGSRHW5ADV9ShrBt+CZjwMEFoaQ18X ZptdjNcEh8Akx5iKvjGrLltgZaE+xuvEmUc/OXM3lJHjm859JdnnIGUnq6wG1Xh0QM+J yeTNrnlF9FPIfkvnZhkwunlUDFyxxatczzkpKqLDXCgWOu0UflMbcqivMT25hKmIkVht YQB2Hprn5fa+2+lQLpewx+bLi2/YhVKSQY7DkvZyUeW/z84vOOn86OSaPUSiT7uh9cu9 0xuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=DRtfiwMAM8yTR7sJf38tvVC8dO1OlTbKNgp5BHA/JIE=; b=BEHn/EglmHXE45+GhVX7fozimp+2u8uwrOdKQPwnrHCfHUOJigqiKQ5Jd0/hOv9zko AJ0NylDYwpuksvGVdMogWgpv/CVWfTEE4n+QYVGBLRlAu7zP88r3mL5nbKnr/ypQ7aue lLkE+mhutsOCewwnbr7koapiOQmzcwBHWbIiOjqTO42tMLZqPMqj1rPxWjOt8+xXuPsh lDUI54YI4lFN9hCKD0mni/oFsKbtDZwhjheiel8WCgZpneT/tKqjfknBfhvFZrw6eAlV xScGIJPOIMdAKvi8UAxUMwUpzdIAf1Z84BuH+KRR1Gm+fHA++8JawqS/zMcoHMThdLoJ +f0g== X-Gm-Message-State: AKwxytdOePgSq6RfinraWBhvSznFOgXRns/Rmh2ny5l+Tm6ezauXysia MacuO6Z1tvZXuaPA4vL8ze5g+EOQFWDBBlS/WTaGXsoXwHy1tq9CvPv13Jfr75yCxyEBoCX09Uo QMnCiz5779b7bDRtBHKol6gbDIscWmNqwkN6F2BNvVx6H2QEiKeFq/bs+zxu0+8skpV4F/Outq6 TE7UsXbbseFB4t X-Google-Smtp-Source: AH8x225uhooE2f0s/sPWgAwahS77wJmnQidvIqmykDJjYnhDeB4sQiiRbRMF0Q6RzOCw8VCVt89nyg== X-Received: by 10.223.185.92 with SMTP id b28mr2452408wrg.196.1516496908161; Sat, 20 Jan 2018 17:08:28 -0800 (PST) Received: from [10.137.2.19] (ip-78-102-97-226.net.upcbroadband.cz. [78.102.97.226]) by smtp.gmail.com with ESMTPSA id r1sm6374012wmg.22.2018.01.20.17.08.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 20 Jan 2018 17:08:27 -0800 (PST) Subject: Re: BUG #15001: planner cann't distinguish composite index? To: Amit Kapila Cc: =?UTF-8?B?5b635ZOl?= , pgsql-bugs@lists.postgresql.org References: <20180109112516.21776.93511@wrigleys.postgresql.org> <24a39222-8bd3-155c-9bd7-ee20b23da7cf@2ndquadrant.com> From: Tomas Vondra Message-ID: Date: Sun, 21 Jan 2018 02:08:25 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 01/20/2018 07:15 AM, Amit Kapila wrote: > On Thu, Jan 18, 2018 at 3:14 AM, Tomas Vondra > wrote: >> >> >> On 01/10/2018 04:35 AM, Amit Kapila wrote: >>> On Tue, Jan 9, 2018 at 4:55 PM, PG Bug reporting form >>> wrote: >>>> The following bug has been logged on the website: >>>> >>>> Bug reference: 15001 >>>> Logged by: Zhou Digoal >>>> Email address: digoal@126.com >>>> PostgreSQL version: 10.1 >>>> Operating system: centos 7.x x64 >>>> Description: >>>> >>>> ``` >>>> postgres=# create table tbl(c1 int, c2 int, c3 int); >>>> CREATE TABLE >>>> postgres=# create index idx_tbl on tbl (c1,c2); >>>> CREATE INDEX >>>> postgres=# insert into tbl select random()*100, random()*10 from >>>> generate_series(1,10000000); >>>> INSERT 0 10000000 >>>> >>>> postgres=# explain select c1,c2 , count(*) from tbl group by c2,c1; >>>> QUERY PLAN >>>> >>>> -------------------------------------------------------------------------------------------------------------------- >>>> Finalize GroupAggregate (cost=176259.41..176337.18 rows=1111 width=16) >>>> Group Key: c2, c1 >>>> -> Sort (cost=176259.41..176276.08 rows=6666 width=16) >>>> Sort Key: c2, c1 >>>> -> Gather (cost=175158.32..175836.03 rows=6666 width=16) >>>> Workers Planned: 6 >>>> -> Partial HashAggregate (cost=174158.32..174169.43 >>>> rows=1111 width=16) >>>> Group Key: c2, c1 >>>> -> Parallel Index Only Scan using idx_tbl on tbl >>>> (cost=0.43..161658.26 rows=1666675 width=8) >>>> (9 rows) >>>> >>>> postgres=# explain select c1,c2 , count(*) from tbl group by c1,c2; >>>> QUERY PLAN >>>> >>>> -------------------------------------------------------------------------------------------------------------- >>>> Finalize GroupAggregate (cost=1000.53..176040.80 rows=1111 width=16) >>>> Group Key: c1, c2 >>>> -> Gather Merge (cost=1000.53..175979.69 rows=6666 width=16) >>>> Workers Planned: 6 >>>> -> Partial GroupAggregate (cost=0.43..174169.43 rows=1111 >>>> width=16) >>>> Group Key: c1, c2 >>>> -> Parallel Index Only Scan using idx_tbl on tbl >>>> (cost=0.43..161658.26 rows=1666675 width=8) >>>> (7 rows) >>>> ``` >>>> >>>> i need to set enable_sort=off, so planner can choose the same planner with >>>> c1,c2 and c2,c1 group by. >>>> >>>> ``` >>>> postgres=# set enable_sort=off; >>>> SET >>>> postgres=# explain select c1,c2 , count(*) from tbl group by c2,c1; >>>> QUERY PLAN >>>> >>>> -------------------------------------------------------------------------------------------------------------- >>>> Finalize HashAggregate (cost=175886.03..175897.14 rows=1111 width=16) >>>> Group Key: c2, c1 >>>> -> Gather (cost=175158.32..175836.03 rows=6666 width=16) >>>> Workers Planned: 6 >>>> -> Partial HashAggregate (cost=174158.32..174169.43 rows=1111 >>>> width=16) >>>> Group Key: c2, c1 >>>> -> Parallel Index Only Scan using idx_tbl on tbl >>>> (cost=0.43..161658.26 rows=1666675 width=8) >>>> (7 rows) >>>> ``` >>>> >>> >>> I don't think the plan chosen is same. If you notice, for c1,c2 the >>> plan chosen is Finalize GroupAggregate -> Gather Merge whereas for >>> c2,c1 (with sort off) it is Finalize HashAggregate -> Gather. There is >>> a lot of difference between both the plans. The output by Gather >>> Merge is sorted, so you can directly use GroupAggregate whereas the >>> output of Gather is unsorted, so the final plan is HashAggregate. >>> >>> Now, here one can wonder why the planner hasn't chosen the path >>> without Sort for c2,c1 even when enable_sort=on as the cost of that >>> plan is less. If you see the cost difference of plans with sort >>> (total_cost - 176337.18) and without sort (total_cost - 175897.14), it >>> is marginal and planner thinks that they are fuzzily same. The same >>> is true for startup costs as well. Now, if both have same costs >>> (fuzzily), it gives preference to the sorted path. >>> >> >> The original report is quite unclear - it only shows some plans but does >> not explain why it's a bug or what's the expected behavior. >> >> The fuzzy comparison of costs certainly explains at least some of it, >> but my feeling is it's likely related to the fact that group by has to >> match a composite index including the order of columns. >> >> That is, an index defined on (c1,c2) is unusable for (GROUP BY c2,c1). >> Which is why GROUP BY c1,c2 uses GroupAggregate, while GROUP BY c2,c1 >> uses HashAggregate. >> > > Sure, but it can use sort node for c2,c1 to make use of GroupAggregate > if such a plan is cheap and that is what has happened in the first > case of this report. I have tried to explain the reason why planner > has chosen the specific plan in each of the three cases. I guess the > OP might have confused between second and third plan which appears to > be somewhat similar but are actually quite different. > Yes, I understand that, and I agree with your analysis regarding fuzzy cost comparisons. My point is the first query can't possibly use the same plan shown for the second one (I mean, the one with index only scan and group aggregate leveraging the sorted output directly) because the GROUP BY clause does not match the index definition. That being said, the more I look at the plans the more I'm confused about what the OP might have been asking about :-/ regrds -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services