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 1fQw0N-0005YL-OI for pgsql-hackers@arkaria.postgresql.org; Thu, 07 Jun 2018 14:35:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fQw0M-0007eO-Ep for pgsql-hackers@arkaria.postgresql.org; Thu, 07 Jun 2018 14:35:10 +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.89) (envelope-from ) id 1fQw0L-0007dZ-Ro for pgsql-hackers@lists.postgresql.org; Thu, 07 Jun 2018 14:35:10 +0000 Received: from mail-wr0-x22a.google.com ([2a00:1450:400c:c0c::22a]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fQw0I-0004hd-I4 for pgsql-hackers@postgresql.org; Thu, 07 Jun 2018 14:35:08 +0000 Received: by mail-wr0-x22a.google.com with SMTP id k16-v6so10125459wro.0 for ; Thu, 07 Jun 2018 07:35:06 -0700 (PDT) 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=Tbtr2cPE/DU57XPUWK6Ky/Ov0f1LkclAkFHqiNy/XLk=; b=hi8xpXFOBYlfsmClZ0/e6Rmwgbu86SOcMvDEB212krmBbv1d5yPZsNy074RJ96K6zt B1P4B2tBEQCysbTNb5sBPtbcXPBtYU8VYPFiKthWVY4d5uqcOwwqPGDeZ+a+c/BE/rAa btIZSbE5hT9FWsFlNOXLbRlSywbWppfcDWvFvj77tVd8eGNrYFZ0LJ0h2sojR3EjYo5t pfUHGzFjBmz06r+GuQcADdDosbsBkbpwgIXrmg+k2LR5m/NbbHJiA79AscgdZu+09hg8 ElbzLnema1mzXfX3p4XEnGcJIKIh8GoxheFCNV4KtTu1QOgQa4Ct2pLdXYG9PcK/UMXl 6kEw== 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=Tbtr2cPE/DU57XPUWK6Ky/Ov0f1LkclAkFHqiNy/XLk=; b=tWGe15TW5SP09s0WyKGX+DbPYx3ji395PWx+Dft1ghCYoLuHPNi/foKsnaiCxUQBjP WM9tRLjMVC7AF/eN1U/PhozmyQHZqC1HeFrZSlJstu2dPZYKWZFjTx1S1HRBODWx4joJ sfdSnWN0rvggMgzS0huvcCrez6EbdKzB0yATiDIQHc8Xro8BOU+a5qPhV6dT3HtE3s1l i2ZQ7RVBKj2F0MTo7ngQv4/zuy+lrvInQtOj4LZFAbF65Coqd77QKa4RyZBd6HIzc8JN HJ47hpOueQNZU4ucHkDgaosuscTpDr8yoEpPi3xi46VWGt5Lb22vxATCW6WraakxnFcA BHlg== X-Gm-Message-State: APt69E0IgHps+YoZxQNj7UAIjBEocuK3Xs7zmthUEmg/rb7M+6cLSyeY 97aHJ8G+OzReHys0y2FghXpjlN1T+pEqc0MHhtsp4CzvTxA9ldFJjPfevU7Fr1738NQMKghE3TL Y0b7vjd1c+HbuZdq44PjvGL9oQps3DCFnjQRbYoY9MaIHMoqHUfVsM/BCeBqjYZHl+tyR/NZuiX bhmM2mJOWX X-Google-Smtp-Source: ADUXVKKImPWtH0ENco8dSD1hI3UOUhLtd2+SXMT7KtG5Y34ijGsdHwTEqWic1tl3BZJd1AJlSSDt8A== X-Received: by 2002:adf:a949:: with SMTP id u67-v6mr1883401wrc.71.1528382103995; Thu, 07 Jun 2018 07:35:03 -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 h65-v6sm2290996wmf.7.2018.06.07.07.35.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 07 Jun 2018 07:35:03 -0700 (PDT) Subject: Re: POC: GROUP BY optimization To: Teodor Sigaev , Claudio Freire Cc: PostgreSQL-Dev 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> <4a76e22a-045f-a083-1bc2-84ee90fdb662@2ndquadrant.com> <736c5ece-7ac4-241b-4a11-52797691238f@sigaev.ru> From: Tomas Vondra Message-ID: <8260d372-8826-31ef-68e6-a7ff8a7265d9@2ndquadrant.com> Date: Thu, 7 Jun 2018 16:34:59 +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: <736c5ece-7ac4-241b-4a11-52797691238f@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/07/2018 03:41 PM, Teodor Sigaev wrote: >> >> ... snip ... >> >>> Priorization of the user-provided order can be as simple as giving >>> that comparison_cost a small handicap. >> >> I see no point in doing that, and I don't recall a single place in the >> planner where we do that. If the user specified ORDER BY, we'll slap an >> explicit Sort on top when needed (which acts as the handicap, but in a >> clear way). Otherwise we don't do such things - it'd be just plain >> confusing (consider "ORDER BY a,b" vs. "ORDER BY b,c" with same data >> types, ndistinct etc. but unexpectedly different costs). Also, what >> would be a good value for the handicap? > > Again agree. If we have fixed order of columns (ORDER BY) then we should > not try to reorder it. Current patch follows that if I didn't a mistake. > This part seems to be more a misunderstanding between me and Claudio. I believe Claudio was referring to the column order in a GROUP BY, not ORDER BY. In which case we don't add any Sort, of course. I'm still opposed to adding arbitrary handicap to prioritize the order specified by user, for the reasons I explained before. We should aim to make the heuristics/costing reliable enough to make this unnecessary. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services