Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZIaJF-0000ye-3e for pgsql-bugs@arkaria.postgresql.org; Fri, 24 Jul 2015 10:34:33 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZIaJD-0006v6-Kx for pgsql-bugs@arkaria.postgresql.org; Fri, 24 Jul 2015 10:34:31 +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) (envelope-from ) id 1ZIaJC-0006tx-Ab; Fri, 24 Jul 2015 10:34:30 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by makus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1ZIaJ9-0003wl-8x; Fri, 24 Jul 2015 10:34:28 +0000 Received: from localhost ([127.0.0.1]:31964 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtp (Exim 4.84 (FreeBSD)) (envelope-from ) id 1ZIaJ4-0009UK-Uj; Fri, 24 Jul 2015 10:34:23 +0000 From: Andrew Gierth To: Jeevan Chalke , andres@anarazel.de Cc: Atri Sharma , PostgreSQL Hackers , Postgres-Bugs Subject: Re: [HACKERS] GSets: Fix bug involving GROUPING and HAVING together In-Reply-To: <87si8rqba3.fsf@news-spur.riddles.org.uk> (Andrew Gierth's message of "Tue, 14 Jul 2015 11:53:12 +0100") Message-ID: <874mkt3l59.fsf@news-spur.riddles.org.uk> References: <87si8rqba3.fsf@news-spur.riddles.org.uk> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (berkeley-unix) Date: Fri, 24 Jul 2015 11:34:22 +0100 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Pg-Spam-Score: -3.1 (---) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --=-=-= Content-Type: text/plain >>>>> "Andrew" == Andrew Gierth writes: Andrew> The other is that in subquery_planner, the optimization of Andrew> converting HAVING clauses to WHERE clauses is suppressed if Andrew> parse->groupingSets isn't empty. (It is empty if there's either Andrew> no group by clause at all, or if there's exactly one grouping Andrew> set, which must not be empty, however derived.) This is costing Andrew> us some optimizations, especially in the case of an explicit Andrew> GROUP BY () clause; I'll look into this. I'm inclined to go with the simplest approach here, which copies the quals if there are grouping sets. The only way we could safely move a qual without copying is if we can show that none of the grouping sets is empty, that is (), and at this point the grouping set list has not been expanded so it's not trivial to determine that. Patch attached. -- Andrew (irc:RhodiumToad) --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=gsets_having_pushdown.patch Content-Description: grouping sets HAVING qual pushdown diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index a6ce96e..2866176 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -574,13 +574,12 @@ subquery_planner(PlannerGlobal *glob, Query *parse, if (contain_agg_clause(havingclause) || contain_volatile_functions(havingclause) || - contain_subplans(havingclause) || - parse->groupingSets) + contain_subplans(havingclause)) { /* keep it in HAVING */ newHaving = lappend(newHaving, havingclause); } - else if (parse->groupClause) + else if (parse->groupClause && !parse->groupingSets) { /* move it to WHERE */ parse->jointree->quals = (Node *) --=-=-= Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --=-=-=--