OSDN Git Service

Remove the restriction originally coded into optimize_minmax_aggregates() that
authorTom Lane <tgl@sss.pgh.pa.us>
Fri, 28 Apr 2006 20:57:49 +0000 (20:57 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 28 Apr 2006 20:57:49 +0000 (20:57 +0000)
MIN/MAX not be converted to use an index if the query WHERE clause contains
any volatile functions or subplans.

I had originally feared that the conversion might alter the behavior of such a
query with respect to a volatile function.  Well, so it might, but only in the
sense that the function would get evaluated at a subset of the table rows
rather than all of them --- and we have never made any such guarantee anyway.
(For instance, we don't refuse to use an index for an ordinary non-aggregate
query when one of the non-indexable filter conditions contains a volatile
function.)

The prohibition against subplans was because of worry that that case wasn't
adequately tested, which it wasn't, but it turns out to be possible to make
8.1 fail anyway:

regression=# select o.ten, (select max(unique2) from tenk1 i where ten = o.ten
or ten = (select f1 from int4_tbl limit 1)) from tenk1 o;
ERROR:  direct correlated subquery unsupported as initplan

This is due to bogus code in SS_make_initplan_from_plan (it's an initplan,
ergo it can't have any parParams).  Having fixed that, we might as well allow
subplans as well as initplans.

src/backend/optimizer/plan/planagg.c
src/backend/optimizer/plan/subselect.c

index b1a5c78..c2a1199 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/optimizer/plan/planagg.c,v 1.13 2006/03/05 15:58:29 momjian Exp $
+ *       $PostgreSQL: pgsql/src/backend/optimizer/plan/planagg.c,v 1.14 2006/04/28 20:57:49 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -118,15 +118,6 @@ optimize_minmax_aggregates(PlannerInfo *root, List *tlist, Path *best_path)
        rel = find_base_rel(root, rtr->rtindex);
 
        /*
-        * Also reject cases with subplans or volatile functions in WHERE. This
-        * may be overly paranoid, but it's not entirely clear if the
-        * transformation is safe then.
-        */
-       if (contain_subplans(parse->jointree->quals) ||
-               contain_volatile_functions(parse->jointree->quals))
-               return NULL;
-
-       /*
         * Since this optimization is not applicable all that often, we want to
         * fall out before doing very much work if possible.  Therefore we do the
         * work in several passes.      The first pass scans the tlist and HAVING qual
@@ -509,7 +500,7 @@ make_agg_subplan(PlannerInfo *root, MinMaxAggInfo *info, List *constant_quals)
        ntest->nulltesttype = IS_NOT_NULL;
        ntest->arg = copyObject(info->target);
 
-       plan->qual = lappend(plan->qual, ntest);
+       plan->qual = lcons(ntest, plan->qual);
 
        if (constant_quals)
                plan = (Plan *) make_result(copyObject(plan->targetlist),
index 5296bb0..b9e123d 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/optimizer/plan/subselect.c,v 1.105 2006/04/22 01:25:59 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/optimizer/plan/subselect.c,v 1.106 2006/04/28 20:57:49 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1248,8 +1248,6 @@ SS_make_initplan_from_plan(PlannerInfo *root, Plan *plan,
        List       *saved_initplan = PlannerInitPlan;
        SubPlan    *node;
        Param      *prm;
-       Bitmapset  *tmpset;
-       int                     paramid;
 
        /*
         * Set up for a new level of subquery.  This is just to keep
@@ -1280,18 +1278,9 @@ SS_make_initplan_from_plan(PlannerInfo *root, Plan *plan,
        PlannerInitPlan = lappend(PlannerInitPlan, node);
 
        /*
-        * Make parParam list of params that current query level will pass to this
-        * child plan.  (In current usage there probably aren't any.)
+        * The node can't have any inputs (since it's an initplan), so the
+        * parParam and args lists remain empty.
         */
-       tmpset = bms_copy(plan->extParam);
-       while ((paramid = bms_first_member(tmpset)) >= 0)
-       {
-               PlannerParamItem *pitem = list_nth(PlannerParamList, paramid);
-
-               if (pitem->abslevel == PlannerQueryLevel)
-                       node->parParam = lappend_int(node->parParam, paramid);
-       }
-       bms_free(tmpset);
 
        /*
         * Make a Param that will be the subplan's output.