Skip to content

Commit 53ee9f5

Browse files
committed
Remove the restriction originally coded into optimize_minmax_aggregates() that
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.
1 parent 4c5eb2c commit 53ee9f5

File tree

2 files changed

+5
-25
lines changed

2 files changed

+5
-25
lines changed

src/backend/optimizer/plan/planagg.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/optimizer/plan/planagg.c,v 1.13 2006/03/05 15:58:29 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/optimizer/plan/planagg.c,v 1.14 2006/04/28 20:57:49 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -117,15 +117,6 @@ optimize_minmax_aggregates(PlannerInfo *root, List *tlist, Path *best_path)
117117
return NULL;
118118
rel = find_base_rel(root, rtr->rtindex);
119119

120-
/*
121-
* Also reject cases with subplans or volatile functions in WHERE. This
122-
* may be overly paranoid, but it's not entirely clear if the
123-
* transformation is safe then.
124-
*/
125-
if (contain_subplans(parse->jointree->quals) ||
126-
contain_volatile_functions(parse->jointree->quals))
127-
return NULL;
128-
129120
/*
130121
* Since this optimization is not applicable all that often, we want to
131122
* fall out before doing very much work if possible. Therefore we do the
@@ -509,7 +500,7 @@ make_agg_subplan(PlannerInfo *root, MinMaxAggInfo *info, List *constant_quals)
509500
ntest->nulltesttype = IS_NOT_NULL;
510501
ntest->arg = copyObject(info->target);
511502

512-
plan->qual = lappend(plan->qual, ntest);
503+
plan->qual = lcons(ntest, plan->qual);
513504

514505
if (constant_quals)
515506
plan = (Plan *) make_result(copyObject(plan->targetlist),

src/backend/optimizer/plan/subselect.c

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/backend/optimizer/plan/subselect.c,v 1.105 2006/04/22 01:25:59 tgl Exp $
10+
* $PostgreSQL: pgsql/src/backend/optimizer/plan/subselect.c,v 1.106 2006/04/28 20:57:49 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -1248,8 +1248,6 @@ SS_make_initplan_from_plan(PlannerInfo *root, Plan *plan,
12481248
List *saved_initplan = PlannerInitPlan;
12491249
SubPlan *node;
12501250
Param *prm;
1251-
Bitmapset *tmpset;
1252-
int paramid;
12531251

12541252
/*
12551253
* 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,
12801278
PlannerInitPlan = lappend(PlannerInitPlan, node);
12811279

12821280
/*
1283-
* Make parParam list of params that current query level will pass to this
1284-
* child plan. (In current usage there probably aren't any.)
1281+
* The node can't have any inputs (since it's an initplan), so the
1282+
* parParam and args lists remain empty.
12851283
*/
1286-
tmpset = bms_copy(plan->extParam);
1287-
while ((paramid = bms_first_member(tmpset)) >= 0)
1288-
{
1289-
PlannerParamItem *pitem = list_nth(PlannerParamList, paramid);
1290-
1291-
if (pitem->abslevel == PlannerQueryLevel)
1292-
node->parParam = lappend_int(node->parParam, paramid);
1293-
}
1294-
bms_free(tmpset);
12951284

12961285
/*
12971286
* Make a Param that will be the subplan's output.

0 commit comments

Comments
 (0)