Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fUDc8-0001dD-0n for pgsql-hackers@arkaria.postgresql.org; Sat, 16 Jun 2018 15:59:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fUDc4-0003Tx-LS for pgsql-hackers@arkaria.postgresql.org; Sat, 16 Jun 2018 15:59:40 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fUDc4-0003Tn-AJ for pgsql-hackers@lists.postgresql.org; Sat, 16 Jun 2018 15:59:40 +0000 Received: from mail-wm0-x235.google.com ([2a00:1450:400c:c09::235]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fUDbv-0000mD-PP for pgsql-hackers@postgresql.org; Sat, 16 Jun 2018 15:59:39 +0000 Received: by mail-wm0-x235.google.com with SMTP id v16-v6so7754410wmh.5 for ; Sat, 16 Jun 2018 08:59:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=bR5j9F2Jb53t7SsX+oJRSSHz65/n7+EkE3DDIohZlxs=; b=d2luYwzeLeZqEV9/tZ9TVGanjMu/KIZiIhnadLFmIw2LVfwzmCIhiiqkrH1tMQM5+L kPcEfpAfy9YOxqwpU2K2rEZftu8JaEy1hy4du3IQhJn3HZ9NfYDMPKT/gK2kQdRBCZdi wToVwFhOZDit5ECX6GD7aTus+Vla9YaJCT6ZgxOj2pitkuJVEIQkrDk5M7CmX31H4C8U BU7HxUdd/scBbWJryxL3h3XUQGbrOuI+4pn7kqgR/j01qwebMJ59d52kqWq3qlWpwogC j2E9DUm4rhRpskpl5aQeGlH5/xagUVfJS/ftLgjRByGfEBJTA77POoNxwqaOmG/mE/Yn csaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=bR5j9F2Jb53t7SsX+oJRSSHz65/n7+EkE3DDIohZlxs=; b=Gf0q+OqK7u4itstfQcYYTo3K1ioaz3mBnBd6aKyaU7yoSEMTZ0JuSlMzJnpAtl9kt6 AALBOKu7juyToKmPixZ6+JPRQqFi4uC3hfINQO8LaIwF7vue6TuOMNKg6z2rJizqY0kf S/tm6ZDo0m4kvYuu+Loy7wMEvNL+slHdDIN21YNXmjuAJCataHSpVLAq7+BNlRGkyp8R OTphynop3FciEgjBejkkykE3j70bJLXT8sTM2oh2L1dPBsDS5um14E/MSXEFdTNUTNTk 8LWo9cUYXtnAIk0nUuy0GuWjSIFmzNEYSk0Aa5Yo3F4JdqG0swJsDuqvrlMgyETQm/6H 90pg== X-Gm-Message-State: APt69E2AnMvAygkYOrLs1Revfux7ixFBChpt3LDhaaB7LgL+rwZSgh8Y 0bdjf/oTM+beX0bs3W67gc0yy65g/PM3JpOy1wrvVXTb6bM9AHsmins0ZOPy6YIOGDylavtCNrG f2QIbgwfn3UaioWs7lONwVPn0Lj7ebCac+uXNgaox7fQtUQSa/llPzL1tOQjjEWuPu2wFZES32o uhNJlpIDNi X-Google-Smtp-Source: ADUXVKK0801RvOzrytUZW3xxViGdBdp/1IrJggn4inPRUasBo0G7JYN28N8H4cb59d6c2kM4Y1PDBQ== X-Received: by 2002:a1c:71db:: with SMTP id d88-v6mr4413654wmi.117.1529164769488; Sat, 16 Jun 2018 08:59:29 -0700 (PDT) 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 u108-v6sm21360582wrc.40.2018.06.16.08.59.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 16 Jun 2018 08:59:28 -0700 (PDT) Subject: Re: POC: GROUP BY optimization To: Teodor Sigaev , Pgsql Hackers References: <7c79e6a5-8597-74e8-0671-1c39d124c9d6@sigaev.ru> <97d148b7-f3e7-1c39-07fd-74cbc66f6ec9@sigaev.ru> <6d1e0cdb-dde3-f62a-43e2-e90bbd9b0f42@2ndquadrant.com> <7edddb4f-9ddd-561d-2bde-47340f91d2a0@2ndquadrant.com> <2cb99655-7ae7-ec8d-2c74-3fea58987104@sigaev.ru> <6dddcce6-0d91-1868-794a-4d61dc5516f0@2ndquadrant.com> <523d1f60-c60c-5c8c-8cc5-c6c078970da6@2ndquadrant.com> <24c773b8-3a9a-8fe9-610d-01145758931b@sigaev.ru> From: Tomas Vondra Message-ID: <623f5e50-eeb2-1279-d30b-dc43611fb063@2ndquadrant.com> Date: Sat, 16 Jun 2018 17:59:24 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2 MIME-Version: 1.0 In-Reply-To: <24c773b8-3a9a-8fe9-610d-01145758931b@sigaev.ru> Content-Type: text/plain; charset=koi8-r Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 06/13/2018 06:56 PM, Teodor Sigaev wrote: >> I.e. we already do reorder the group clauses to match ORDER BY, to only >> require a single sort. This happens in preprocess_groupclause(), which >> also explains the reasoning behind that. > Huh. I missed that. That means group_keys_reorder_by_pathkeys() call > inside get_cheapest_group_keys_order() duplicates work of > preprocess_groupclause(). > šAnd so it's possible to replace it to simple counting number of the > same pathkeys in beginning of lists. Or remove most part of > preprocess_groupclause() - but it seems to me we should use first > option, preprocess_groupclause() is simpler, it doesn't operate with > pathkeys only withš SortGroupClause which is simpler. > > BTW, incremental sort path provides pathkeys_common(), exactly what we > need. > >> I wonder if some of the new code reordering group pathkeys could/should >> be moved here (not sure, maybe it's too early for those decisions). In >> any case, it might be appropriate to update some of the comments before >> preprocess_groupclause() which claim we don't do certain things added by >> the proposed patches. > > preprocess_groupclause() is too early step to use something like > group_keys_reorder_by_pathkeys() because pathkeys is not known here yet. > > >> This probably also somewhat refutes my claim that the order of >> grouping keys is currently fully determined by users (and so they >> may pick the most efficient order), while the reorder-by-ndistinct >> patch would make that impossible. Apparently when there's ORDER BY, >> we already mess with the order of group clauses - there are ways to >> get around it (subquery with OFFSET 0) but it's much less clear. > > I like a moment when objections go away :) > I'm afraid that's a misunderstanding - my objections did not really go away. I'm just saying my claim that we're not messing with order of grouping keys is not entirely accurate, because we do that in one particular case. I still think we need to be careful when introducing new optimizations in this area - reordering the grouping keys by ndistinct, ORDER BY or whatever. In particular I don't think we should commit these patches that may quite easily cause regressions, and then hope some hypothetical future patch fixes the costing. Those objections still stand. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services