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

Commit25e46a5

Browse files
committed
Fix a couple of oversights associated with the "physical tlist" optimization:
we had several code paths where a physical tlist could be used for the inputto a Sort node, which is a dumb idea because any unneeded table columns willincrease the volume of data the sort has to push around.(Unfortunately the easy-looking fix of calling disuse_physical_tlist duringmake_sort_xxx doesn't work because in most cases we're already committed tothe current input tlist --- it's been marked with sort column numbers, orwe've built grouping column numbers using it, etc. The tlist has to beselected properly at the calling level before we start constructing sort-colinformation. This is easy enough to do, we were just failing to take thepoint into consideration.)Back-patch to 8.3. I believe the problem probably exists clear back to 7.4when the physical tlist optimization was added, but I'm afraid to back-patchfurther than 8.3 without a great deal more study than I want to put into it.The code in this area has drifted a lot over time. The real-world importanceof these code paths is uncertain anyway --- I think in many cases we'dprobably prefer hash-based methods.
1 parentc5c7ba1 commit25e46a5

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

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

Lines changed: 9 additions & 6 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.239 2008/04/13 20:51:20 tgl Exp $
13+
* $PostgreSQL: pgsql/src/backend/optimizer/plan/createplan.c,v 1.240 2008/04/17 21:22:14 tgl Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -115,6 +115,7 @@ static MergeJoin *make_mergejoin(List *tlist,
115115
staticSort*make_sort(PlannerInfo*root,Plan*lefttree,intnumCols,
116116
AttrNumber*sortColIdx,Oid*sortOperators,bool*nullsFirst,
117117
doublelimit_tuples);
118+
staticMaterial*make_material(Plan*lefttree);
118119

119120

120121
/*
@@ -616,12 +617,14 @@ create_unique_plan(PlannerInfo *root, UniquePath *best_path)
616617
* add any such expressions to the subplan's tlist.
617618
*
618619
* The subplan may have a "physical" tlist if it is a simple scan plan.
619-
* This should be left as-is if we don't need to add any expressions;
620+
* If we're going to sort, this should be reduced to the regular tlist,
621+
* so that we don't sort more data than we need to. For hashing, the
622+
* tlist should be left as-is if we don't need to add any expressions;
620623
* but if we do have to add expressions, then a projection step will be
621-
* needed at runtime anyway,andso we may as well remove unneeded items.
624+
* needed at runtime anyway, so we may as well remove unneeded items.
622625
* Therefore newtlist starts from build_relation_tlist() not just a
623626
* copy of the subplan's tlist; and we don't install it into the subplan
624-
* unless stuff has to be added.
627+
* unlesswe are sorting orstuff has to be added.
625628
*
626629
* To find the correct list of values to unique-ify, we look in the
627630
* information saved for IN expressions. If this code is ever used in
@@ -669,7 +672,7 @@ create_unique_plan(PlannerInfo *root, UniquePath *best_path)
669672
}
670673
}
671674

672-
if (newitems)
675+
if (newitems||best_path->umethod==UNIQUE_PATH_SORT)
673676
{
674677
/*
675678
* If the top plan node can't do projections, we need to add a Result
@@ -2850,7 +2853,7 @@ make_sort_from_groupcols(PlannerInfo *root,
28502853
sortColIdx,sortOperators,nullsFirst,-1.0);
28512854
}
28522855

2853-
Material*
2856+
staticMaterial*
28542857
make_material(Plan*lefttree)
28552858
{
28562859
Material*node=makeNode(Material);

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/optimizer/plan/planner.c,v 1.231 2008/04/01 00:48:33 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/optimizer/plan/planner.c,v 1.232 2008/04/17 21:22:14 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -957,9 +957,23 @@ grouping_planner(PlannerInfo *root, double tuple_fraction)
957957
* Normal case --- create a plan according to query_planner's
958958
* results.
959959
*/
960+
boolneed_sort_for_grouping= false;
961+
960962
result_plan=create_plan(root,best_path);
961963
current_pathkeys=best_path->pathkeys;
962964

965+
/* Detect if we'll need an explicit sort for grouping */
966+
if (parse->groupClause&& !use_hashed_grouping&&
967+
!pathkeys_contained_in(group_pathkeys,current_pathkeys))
968+
{
969+
need_sort_for_grouping= true;
970+
/*
971+
* Always override query_planner's tlist, so that we don't
972+
* sort useless data from a "physical" tlist.
973+
*/
974+
need_tlist_eval= true;
975+
}
976+
963977
/*
964978
* create_plan() returns a plan with just a "flat" tlist of
965979
* required Vars. Usually we need to insert the sub_tlist as the
@@ -1054,8 +1068,7 @@ grouping_planner(PlannerInfo *root, double tuple_fraction)
10541068

10551069
if (parse->groupClause)
10561070
{
1057-
if (!pathkeys_contained_in(group_pathkeys,
1058-
current_pathkeys))
1071+
if (need_sort_for_grouping)
10591072
{
10601073
result_plan= (Plan*)
10611074
make_sort_from_groupcols(root,
@@ -1098,7 +1111,7 @@ grouping_planner(PlannerInfo *root, double tuple_fraction)
10981111
* Add an explicit sort if we couldn't make the path come out
10991112
* the way the GROUP node needs it.
11001113
*/
1101-
if (!pathkeys_contained_in(group_pathkeys,current_pathkeys))
1114+
if (need_sort_for_grouping)
11021115
{
11031116
result_plan= (Plan*)
11041117
make_sort_from_groupcols(root,

‎src/include/optimizer/planmain.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2008, 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.106 2008/01/01 19:45:58 momjian Exp $
10+
* $PostgreSQL: pgsql/src/include/optimizer/planmain.h,v 1.107 2008/04/17 21:22:14 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -53,7 +53,6 @@ extern Group *make_group(PlannerInfo *root, List *tlist, List *qual,
5353
intnumGroupCols,AttrNumber*grpColIdx,Oid*grpOperators,
5454
doublenumGroups,
5555
Plan*lefttree);
56-
externMaterial*make_material(Plan*lefttree);
5756
externPlan*materialize_finished_plan(Plan*subplan);
5857
externUnique*make_unique(Plan*lefttree,List*distinctList);
5958
externLimit*make_limit(Plan*lefttree,Node*limitOffset,Node*limitCount,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp