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 1fYuvp-00061J-KG for pgsql-hackers@arkaria.postgresql.org; Fri, 29 Jun 2018 15:03:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fYuvn-0005Ul-1z for pgsql-hackers@arkaria.postgresql.org; Fri, 29 Jun 2018 15:03:27 +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 1fYuvm-0005UX-RR for pgsql-hackers@lists.postgresql.org; Fri, 29 Jun 2018 15:03:26 +0000 Received: from mail-wr0-x244.google.com ([2a00:1450:400c:c0c::244]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fYuvi-00086G-Fp for pgsql-hackers@postgresql.org; Fri, 29 Jun 2018 15:03:25 +0000 Received: by mail-wr0-x244.google.com with SMTP id l2-v6so8584827wro.7 for ; Fri, 29 Jun 2018 08:03:22 -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=1Yzo9TQvVz5ZIBT1wpYcRfHz0Lv9T946Uqog/52uBDQ=; b=reyOvuIGdsLRuZgoMBTKR+GDZTSfKAXmmgp0FbGXEzCg6O+Su28EvQeidYJolyE9iu qO7oMNjFUfuaHsHutK12SdNzXOybKsc8ysCDBPGoCRot8rqR8VaJtdYMtV31TF0JP4WF NuuzjCEG2X/0eBklOa1PnoFVyJKKto++rjrZGn3ae1Yf7U/rW6MBDzRB04bLEdPMAIdc ZqVBLXJfYmIAQOLEeVcJr2hG5OOZr4OW80O3FLoAOKEVMJRjlEIt0m6OmmwiqZERTeq8 Kj9Gmyas8qqroG7S08kMbriXqHxvr0ncbf6oaaZcJtOabcDA65Hb1oBVNatDt5Mansvo ZnKQ== 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=1Yzo9TQvVz5ZIBT1wpYcRfHz0Lv9T946Uqog/52uBDQ=; b=XFSfH8lokt8pt0uv7rgy8T6mBtvCVdnI/vKMmEj+1Jr0n2tF/puU42l9LyRdY5D4ps kbKqX6W8YQ49zqkQ3S3HJo6Uo1qgMZyNFLSGYfqfTITPmEDCzGKPahXwfkfdIX+TQRqi l7zxriBqIV82RbR4vvX2wOnwhgCkT44jfraPdeeu7q6IiWMEItTBPgR5SLf8vELwhbs6 MSfbcd3IJUmTm20PmLr1nJXfmRR3smCabeIn3398KAKyOVO8b0P/Y35ToY4op9OM0gvh LrD3aFoOOxG0wDlp9cqCBEEL0lPkQ5CXsFzMviBeI1WeaumfPCJMdqVYTFr2oqYmh0nE z+ow== X-Gm-Message-State: APt69E3fchTAUhHB/6h4zquIsfM9MiWf/JX+6DPpdQSM71WxYZVcHkPZ 7BrMa2L3MpAo6Q6YFkV6RY0v0oe7Ex5DsfAZVw9NKpo/ZiT/w3b951mx8Jifu6jXN5b6HmjR3ku 7AFEZb95XgkM6Rq4QUTP/QhjWej8of7ctKzSHAXwWwjQyOwBs5kBlvJ9qi/Vjw5s1ihb8MyL1V2 shgD/G3Yg6 X-Google-Smtp-Source: AAOMgpfWBoGEtFsiq+5MtMMXRwLdOfd5exFmQSWrVSMxvAl5QyxSV1+OLEi12opOSYSXbi6Q8BoMcw== X-Received: by 2002:adf:99ea:: with SMTP id y97-v6mr12233459wrb.215.1530284601228; Fri, 29 Jun 2018 08:03:21 -0700 (PDT) Received: from [10.137.2.21] (static-84-42-175-93.net.upcbroadband.cz. [84.42.175.93]) by smtp.gmail.com with ESMTPSA id f81-v6sm2709732wmh.32.2018.06.29.08.03.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 29 Jun 2018 08:03:18 -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> <623f5e50-eeb2-1279-d30b-dc43611fb063@2ndquadrant.com> <1913e082-0d8d-5eb8-c8ca-053af828076a@2ndquadrant.com> <3bbdd1ee-544d-a549-b0c2-30485d9f6a16@sigaev.ru> From: Tomas Vondra Message-ID: <83b0d707-405d-c2be-0d98-01267e68d671@2ndquadrant.com> Date: Fri, 29 Jun 2018 17:03:13 +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: <3bbdd1ee-544d-a549-b0c2-30485d9f6a16@sigaev.ru> Content-Type: text/plain; charset=koi8-r; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 06/29/2018 04:51 PM, Teodor Sigaev wrote: > >>> I tried to attack the cost_sort() issues and hope on that basis we >>> can solve problems with 0002 patch and improve incremental sort patch. >>> >> >> OK, will do. Thanks for working on this! > > I hope, now we have a better cost_sort(). The obvious way is a try all > combination of pathkeys in get_cheapest_group_keys_order() and choose > cheapest one by cost_sort(). > But it requires N! operations and potentially could be very > expensive in case of large number of pathkeys and doesn't solve the > issue with user-knows-what-he-does pathkeys. Not sure. There are N! combinations, but this seems like a good candidate for backtracking [1]. You don't have to enumerate and evaluate all N! combinations, just construct one and then abandon whole classes of combinations as soon as they get more expensive than the currently best one. That's thanks to additive nature of the comparison costing, because appending a column to the sort key can only make it more expensive. My guess is this will make this a non-issue. [1] https://en.wikipedia.org/wiki/Backtracking > > We could suggest an order of pathkeys as patch suggests now and if > cost_sort() estimates cost is less than 80% (arbitrary chosen) cost > of user-suggested pathkeys then it use our else user pathkeys. > I really despise such arbitrary thresholds. I'd much rather use a more reliable heuristics by default, even if it gets it wrong in some cases (which it will, but that's natural). regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services