Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7ace43e

Browse files
committed
Fix oversight in MIN/MAX optimization: must not return NULL entries
from index, since the aggregates ignore NULLs.
1 parent2e7a688 commit7ace43e

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

‎src/backend/optimizer/plan/createplan.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/optimizer/plan/createplan.c,v 1.178 2005/04/06 16:34:05 tgl Exp $
13+
* $PostgreSQL: pgsql/src/backend/optimizer/plan/createplan.c,v 1.179 2005/04/12 05:11:28 tgl Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -73,7 +73,6 @@ static void fix_indxqual_sublist(List *indexqual, IndexOptInfo *index,
7373
staticNode*fix_indxqual_operand(Node*node,IndexOptInfo*index,
7474
Oid*opclass);
7575
staticList*get_switched_clauses(List*clauses,Relidsouterrelids);
76-
staticList*order_qual_clauses(Query*root,List*clauses);
7776
staticvoidcopy_path_costsize(Plan*dest,Path*src);
7877
staticvoidcopy_plan_costsize(Plan*dest,Plan*src);
7978
staticSeqScan*make_seqscan(List*qptlist,List*qpqual,Indexscanrelid);
@@ -1417,7 +1416,7 @@ get_switched_clauses(List *clauses, Relids outerrelids)
14171416
* For now, we just move any quals that contain SubPlan references (but not
14181417
* InitPlan references) to the end of the list.
14191418
*/
1420-
staticList*
1419+
List*
14211420
order_qual_clauses(Query*root,List*clauses)
14221421
{
14231422
List*nosubplans;

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

Lines changed: 24 additions & 10 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.2 2005/04/1204:26:24 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/optimizer/plan/planagg.c,v 1.3 2005/04/1205:11:28 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -185,6 +185,8 @@ optimize_minmax_aggregates(Query *root, List *tlist, Path *best_path)
185185
{
186186
Assert(((ResultPath*)best_path)->subpath!=NULL);
187187
constant_quals= ((ResultPath*)best_path)->constantqual;
188+
/* no need to do this more than once: */
189+
constant_quals=order_qual_clauses(root,constant_quals);
188190
}
189191
else
190192
constant_quals=NIL;
@@ -438,10 +440,10 @@ static void
438440
make_agg_subplan(Query*root,MinMaxAggInfo*info,List*constant_quals)
439441
{
440442
Query*subquery;
441-
Path*path;
442443
Plan*plan;
443444
TargetEntry*tle;
444445
SortClause*sortcl;
446+
NullTest*ntest;
445447

446448
/*
447449
* Generate a suitably modified Query node. Much of the work here is
@@ -482,18 +484,30 @@ make_agg_subplan(Query *root, MinMaxAggInfo *info, List *constant_quals)
482484
* Generate the plan for the subquery. We already have a Path for
483485
* the basic indexscan, but we have to convert it to a Plan and
484486
* attach a LIMIT node above it. We might need a gating Result, too,
485-
* which is most easily added at the Path stage.
487+
* to handle any non-variable qual clauses.
488+
*
489+
* Also we must add a "WHERE foo IS NOT NULL" restriction to the
490+
* indexscan, to be sure we don't return a NULL, which'd be contrary
491+
* to the standard behavior of MIN/MAX. XXX ideally this should be
492+
* done earlier, so that the selectivity of the restriction could be
493+
* included in our cost estimates. But that looks painful, and in
494+
* most cases the fraction of NULLs isn't high enough to change the
495+
* decision.
486496
*/
487-
path=(Path*)info->path;
497+
plan=create_plan(subquery, (Path*)info->path);
488498

489-
if (constant_quals)
490-
path= (Path*)create_result_path(NULL,
491-
path,
492-
copyObject(constant_quals));
499+
plan->targetlist=copyObject(subquery->targetList);
493500

494-
plan=create_plan(subquery,path);
501+
ntest=makeNode(NullTest);
502+
ntest->nulltesttype=IS_NOT_NULL;
503+
ntest->arg=copyObject(info->target);
495504

496-
plan->targetlist=copyObject(subquery->targetList);
505+
plan->qual=lappend(plan->qual,ntest);
506+
507+
if (constant_quals)
508+
plan= (Plan*)make_result(copyObject(plan->targetlist),
509+
copyObject(constant_quals),
510+
plan);
497511

498512
plan= (Plan*)make_limit(plan,
499513
subquery->limitOffset,

‎src/include/optimizer/planmain.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/optimizer/planmain.h,v 1.81 2005/04/11 23:06:56 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/optimizer/planmain.h,v 1.82 2005/04/12 05:11:28 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -40,6 +40,7 @@ extern Sort *make_sort_from_sortclauses(Query *root, List *sortcls,
4040
Plan*lefttree);
4141
externSort*make_sort_from_groupcols(Query*root,List*groupcls,
4242
AttrNumber*grpColIdx,Plan*lefttree);
43+
externList*order_qual_clauses(Query*root,List*clauses);
4344
externAgg*make_agg(Query*root,List*tlist,List*qual,
4445
AggStrategyaggstrategy,
4546
intnumGroupCols,AttrNumber*grpColIdx,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp