Skip to content

Commit 5ac3406

Browse files
committed
Account for optimized MinMax aggregates during SS_finalize_plan.
We are capable of optimizing MIN() and MAX() aggregates on indexed columns into subqueries that exploit the index, rather than the normal thing of scanning the whole table. When we do this, we replace the Aggref node(s) with Params referencing subquery outputs. Such Params really ought to be included in the per-plan-node extParam/allParam sets computed by SS_finalize_plan. However, we've never done so up to now because of an ancient implementation choice to perform that substitution during set_plan_references, which runs after SS_finalize_plan, so that SS_finalize_plan never sees these Params. The cleanest fix would be to perform a separate tree walk to do these substitutions before SS_finalize_plan runs. That seems unattractive, first because a whole-tree mutation pass is expensive, and second because we lack infrastructure for visiting expression subtrees in a Plan tree, so that we'd need a new function knowing as much as SS_finalize_plan knows about that. I also considered swapping the order of SS_finalize_plan and set_plan_references, but that fell foul of various assumptions that seem tricky to fix. So the approach adopted here is to teach SS_finalize_plan itself to check for such Aggrefs. I refactored things a bit in setrefs.c to avoid having three copies of the code that does that. Back-patch of v17 commits d0d4404 and 779ac2c. When d0d4404 went in, there was no evidence that it was fixing a reachable bug, so I refrained from back-patching. Now we have such evidence. Per bug #18465 from Hal Takahara. Back-patch to all supported branches. Discussion: https://postgr.es/m/18465-2fae927718976b22@postgresql.org Discussion: https://postgr.es/m/2391880.1689025003@sss.pgh.pa.us
1 parent ccf3408 commit 5ac3406

File tree

5 files changed

+107
-29
lines changed

5 files changed

+107
-29
lines changed

src/backend/optimizer/plan/setrefs.c

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1974,22 +1974,14 @@ fix_scan_expr_mutator(Node *node, fix_scan_expr_context *context)
19741974
if (IsA(node, Aggref))
19751975
{
19761976
Aggref *aggref = (Aggref *) node;
1977+
Param *aggparam;
19771978

19781979
/* See if the Aggref should be replaced by a Param */
1979-
if (context->root->minmax_aggs != NIL &&
1980-
list_length(aggref->args) == 1)
1980+
aggparam = find_minmax_agg_replacement_param(context->root, aggref);
1981+
if (aggparam != NULL)
19811982
{
1982-
TargetEntry *curTarget = (TargetEntry *) linitial(aggref->args);
1983-
ListCell *lc;
1984-
1985-
foreach(lc, context->root->minmax_aggs)
1986-
{
1987-
MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
1988-
1989-
if (mminfo->aggfnoid == aggref->aggfnoid &&
1990-
equal(mminfo->target, curTarget->expr))
1991-
return (Node *) copyObject(mminfo->param);
1992-
}
1983+
/* Make a copy of the Param for paranoia's sake */
1984+
return (Node *) copyObject(aggparam);
19931985
}
19941986
/* If no match, just fall through to process it normally */
19951987
}
@@ -2913,22 +2905,14 @@ fix_upper_expr_mutator(Node *node, fix_upper_expr_context *context)
29132905
if (IsA(node, Aggref))
29142906
{
29152907
Aggref *aggref = (Aggref *) node;
2908+
Param *aggparam;
29162909

29172910
/* See if the Aggref should be replaced by a Param */
2918-
if (context->root->minmax_aggs != NIL &&
2919-
list_length(aggref->args) == 1)
2911+
aggparam = find_minmax_agg_replacement_param(context->root, aggref);
2912+
if (aggparam != NULL)
29202913
{
2921-
TargetEntry *curTarget = (TargetEntry *) linitial(aggref->args);
2922-
ListCell *lc;
2923-
2924-
foreach(lc, context->root->minmax_aggs)
2925-
{
2926-
MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
2927-
2928-
if (mminfo->aggfnoid == aggref->aggfnoid &&
2929-
equal(mminfo->target, curTarget->expr))
2930-
return (Node *) copyObject(mminfo->param);
2931-
}
2914+
/* Make a copy of the Param for paranoia's sake */
2915+
return (Node *) copyObject(aggparam);
29322916
}
29332917
/* If no match, just fall through to process it normally */
29342918
}
@@ -3010,6 +2994,38 @@ set_returning_clause_references(PlannerInfo *root,
30102994
}
30112995

30122996

2997+
/*
2998+
* find_minmax_agg_replacement_param
2999+
* If the given Aggref is one that we are optimizing into a subquery
3000+
* (cf. planagg.c), then return the Param that should replace it.
3001+
* Else return NULL.
3002+
*
3003+
* This is exported so that SS_finalize_plan can use it before setrefs.c runs.
3004+
* Note that it will not find anything until we have built a Plan from a
3005+
* MinMaxAggPath, as root->minmax_aggs will never be filled otherwise.
3006+
*/
3007+
Param *
3008+
find_minmax_agg_replacement_param(PlannerInfo *root, Aggref *aggref)
3009+
{
3010+
if (root->minmax_aggs != NIL &&
3011+
list_length(aggref->args) == 1)
3012+
{
3013+
TargetEntry *curTarget = (TargetEntry *) linitial(aggref->args);
3014+
ListCell *lc;
3015+
3016+
foreach(lc, root->minmax_aggs)
3017+
{
3018+
MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
3019+
3020+
if (mminfo->aggfnoid == aggref->aggfnoid &&
3021+
equal(mminfo->target, curTarget->expr))
3022+
return mminfo->param;
3023+
}
3024+
}
3025+
return NULL;
3026+
}
3027+
3028+
30133029
/*****************************************************************************
30143030
* QUERY DEPENDENCY MANAGEMENT
30153031
*****************************************************************************/

src/backend/optimizer/plan/subselect.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2849,8 +2849,8 @@ finalize_plan(PlannerInfo *root, Plan *plan,
28492849
}
28502850

28512851
/*
2852-
* finalize_primnode: add IDs of all PARAM_EXEC params appearing in the given
2853-
* expression tree to the result set.
2852+
* finalize_primnode: add IDs of all PARAM_EXEC params that appear (or will
2853+
* appear) in the given expression tree to the result set.
28542854
*/
28552855
static bool
28562856
finalize_primnode(Node *node, finalize_primnode_context *context)
@@ -2867,7 +2867,26 @@ finalize_primnode(Node *node, finalize_primnode_context *context)
28672867
}
28682868
return false; /* no more to do here */
28692869
}
2870-
if (IsA(node, SubPlan))
2870+
else if (IsA(node, Aggref))
2871+
{
2872+
/*
2873+
* Check to see if the aggregate will be replaced by a Param
2874+
* referencing a subquery output during setrefs.c. If so, we must
2875+
* account for that Param here. (For various reasons, it's not
2876+
* convenient to perform that substitution earlier than setrefs.c, nor
2877+
* to perform this processing after setrefs.c. Thus we need a wart
2878+
* here.)
2879+
*/
2880+
Aggref *aggref = (Aggref *) node;
2881+
Param *aggparam;
2882+
2883+
aggparam = find_minmax_agg_replacement_param(context->root, aggref);
2884+
if (aggparam != NULL)
2885+
context->paramids = bms_add_member(context->paramids,
2886+
aggparam->paramid);
2887+
/* Fall through to examine the agg's arguments */
2888+
}
2889+
else if (IsA(node, SubPlan))
28712890
{
28722891
SubPlan *subplan = (SubPlan *) node;
28732892
Plan *plan = planner_subplan_get_plan(context->root, subplan);

src/include/optimizer/planmain.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ extern bool innerrel_is_unique(PlannerInfo *root,
112112
* prototypes for plan/setrefs.c
113113
*/
114114
extern Plan *set_plan_references(PlannerInfo *root, Plan *plan);
115+
extern Param *find_minmax_agg_replacement_param(PlannerInfo *root,
116+
Aggref *aggref);
115117
extern void record_plan_function_dependency(PlannerInfo *root, Oid funcid);
116118
extern void record_plan_type_dependency(PlannerInfo *root, Oid typid);
117119
extern bool extract_query_dependencies_walker(Node *node, PlannerInfo *root);

src/test/regress/expected/aggregates.out

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,37 @@ NOTICE: drop cascades to 3 other objects
12391239
DETAIL: drop cascades to table minmaxtest1
12401240
drop cascades to table minmaxtest2
12411241
drop cascades to table minmaxtest3
1242+
-- DISTINCT can also trigger wrong answers with hash aggregation (bug #18465)
1243+
begin;
1244+
set local enable_sort = off;
1245+
explain (costs off)
1246+
select f1, (select distinct min(t1.f1) from int4_tbl t1 where t1.f1 = t0.f1)
1247+
from int4_tbl t0;
1248+
QUERY PLAN
1249+
---------------------------------------------------------------------
1250+
Seq Scan on int4_tbl t0
1251+
SubPlan 2
1252+
-> HashAggregate
1253+
Group Key: $1
1254+
InitPlan 1 (returns $1)
1255+
-> Limit
1256+
-> Seq Scan on int4_tbl t1
1257+
Filter: ((f1 IS NOT NULL) AND (f1 = t0.f1))
1258+
-> Result
1259+
(9 rows)
1260+
1261+
select f1, (select distinct min(t1.f1) from int4_tbl t1 where t1.f1 = t0.f1)
1262+
from int4_tbl t0;
1263+
f1 | min
1264+
-------------+-------------
1265+
0 | 0
1266+
123456 | 123456
1267+
-123456 | -123456
1268+
2147483647 | 2147483647
1269+
-2147483647 | -2147483647
1270+
(5 rows)
1271+
1272+
rollback;
12421273
-- check for correct detection of nested-aggregate errors
12431274
select max(min(unique1)) from tenk1;
12441275
ERROR: aggregate function calls cannot be nested

src/test/regress/sql/aggregates.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,16 @@ select distinct min(f1), max(f1) from minmaxtest;
416416

417417
drop table minmaxtest cascade;
418418

419+
-- DISTINCT can also trigger wrong answers with hash aggregation (bug #18465)
420+
begin;
421+
set local enable_sort = off;
422+
explain (costs off)
423+
select f1, (select distinct min(t1.f1) from int4_tbl t1 where t1.f1 = t0.f1)
424+
from int4_tbl t0;
425+
select f1, (select distinct min(t1.f1) from int4_tbl t1 where t1.f1 = t0.f1)
426+
from int4_tbl t0;
427+
rollback;
428+
419429
-- check for correct detection of nested-aggregate errors
420430
select max(min(unique1)) from tenk1;
421431
select (select max(min(unique1)) from int8_tbl) from tenk1;

0 commit comments

Comments
 (0)