]> granicus.if.org Git - postgresql/commitdiff
Allow to push down clauses from HAVING to WHERE when grouping sets are used.
authorAndres Freund <andres@anarazel.de>
Sun, 26 Jul 2015 13:56:26 +0000 (15:56 +0200)
committerAndres Freund <andres@anarazel.de>
Sun, 26 Jul 2015 14:50:20 +0000 (16:50 +0200)
Previously we disallowed pushing down quals to WHERE in the presence of
grouping sets. That's overly restrictive.

We now instead copy quals to WHERE if applicable, leaving the
one in HAVING in place. That's because, at that stage of the planning
process, it's nontrivial to determine if it's safe to remove the one in
HAVING.

Author: Andrew Gierth
Discussion: 874mkt3l59.fsf@news-spur.riddles.org.uk
Backpatch: 9.5, where grouping sets were introduced. This isn't exactly
    a bugfix, but it seems better to keep the branches in sync at this point.

src/backend/optimizer/plan/planner.c

index 11678388fab7cda4067b1b4354854840275793a2..6ee411eec870a95a3c42a198f79af15b52cf3464 100644 (file)
@@ -570,13 +570,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 *)