summaryrefslogtreecommitdiff
path: root/src/backend/optimizer/prep
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>1999-08-21 03:49:17 +0000
committerTom Lane <tgl@sss.pgh.pa.us>1999-08-21 03:49:17 +0000
commitdb436adf761bd5cb7990745ceba2959ac4bfca7c (patch)
tree8878ce970fd9b3ac480f3c5ef953fbc85827e685 /src/backend/optimizer/prep
parent5588c559e6e21fae6ba1f162616f4fb4f680fb31 (diff)
Major revision of sort-node handling: push knowledge of query
sort order down into planner, instead of handling it only at the very top level of the planner. This fixes many things. An explicit sort is now avoided if there is a cheaper alternative (typically an indexscan) not only for ORDER BY, but also for the internal sort of GROUP BY. It works even when there is no other reason (such as a WHERE condition) to consider the indexscan. It works for indexes on functions. It works for indexes on functions, backwards. It's just so cool... CAUTION: I have changed the representation of SortClause nodes, therefore THIS UPDATE BREAKS STORED RULES. You will need to initdb.
Diffstat (limited to 'src/backend/optimizer/prep')
-rw-r--r--src/backend/optimizer/prep/preptlist.c47
-rw-r--r--src/backend/optimizer/prep/prepunion.c15
2 files changed, 33 insertions, 29 deletions
diff --git a/src/backend/optimizer/prep/preptlist.c b/src/backend/optimizer/prep/preptlist.c
index 0cc4780807e..2a9ddfc716a 100644
--- a/src/backend/optimizer/prep/preptlist.c
+++ b/src/backend/optimizer/prep/preptlist.c
@@ -7,7 +7,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/optimizer/prep/preptlist.c,v 1.29 1999/08/09 03:13:31 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/optimizer/prep/preptlist.c,v 1.30 1999/08/21 03:49:05 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -163,7 +163,6 @@ replace_matching_resname(List *new_tlist, List *old_tlist)
{
TargetEntry *old_tle = (TargetEntry *) lfirst(temp);
- old_tle = lfirst(temp);
if (!strcmp(old_tle->resdom->resname,
new_tle->resdom->resname))
{
@@ -174,6 +173,7 @@ replace_matching_resname(List *new_tlist, List *old_tlist)
if (matching_old_tl)
{
+ /* XXX safe to modify old resdom? */
matching_old_tl->resdom->resno = new_tle->resdom->resno;
t_list = lappend(t_list, matching_old_tl);
}
@@ -197,45 +197,42 @@ replace_matching_resname(List *new_tlist, List *old_tlist)
{
TargetEntry *old_tle,
*new_tl;
- Resdom *newresno;
old_tle = lfirst(temp);
if (old_tle->resdom->resno < 0)
{
- newresno = (Resdom *) copyObject((Node *) old_tle->resdom);
- newresno->resno = length(t_list) + 1;
- newresno->resjunk = true;
- new_tl = makeTargetEntry(newresno, old_tle->expr);
+ Resdom *newresdom;
+
+ newresdom = (Resdom *) copyObject((Node *) old_tle->resdom);
+ newresdom->resno = length(t_list) + 1;
+ newresdom->resjunk = true;
+ new_tl = makeTargetEntry(newresdom, old_tle->expr);
t_list = lappend(t_list, new_tl);
}
-
/*
* Also it is possible that the parser or rewriter added some junk
- * attributes to hold GROUP BY expressions which are not part of
+ * attributes to hold ORDER/GROUP BY expressions which are not part of
* the result attributes. We can simply identify them by looking
- * at the resgroupref in the TLE's resdom, which is a unique
- * number telling which TLE belongs to which GroupClause.
+ * at the ressortgroupref in the TLE's resdom, which is a unique
+ * number telling which TLE belongs to which Sort/GroupClause.
*/
- if (old_tle->resdom->resgroupref > 0)
+ else if (old_tle->resdom->ressortgroupref > 0)
{
- bool already_there = FALSE;
- TargetEntry *new_tle;
- Resdom *newresno;
+ bool already_there = false;
/*
* Check if the tle is already in the new list
*/
foreach(i, t_list)
{
- new_tle = (TargetEntry *) lfirst(i);
+ TargetEntry *new_tle = (TargetEntry *) lfirst(i);
- if (new_tle->resdom->resgroupref ==
- old_tle->resdom->resgroupref)
+ if (new_tle->resdom->ressortgroupref ==
+ old_tle->resdom->ressortgroupref)
{
- already_there = TRUE;
+ already_there = true;
break;
}
-
}
/*
@@ -243,10 +240,12 @@ replace_matching_resname(List *new_tlist, List *old_tlist)
*/
if (!already_there)
{
- newresno = (Resdom *) copyObject((Node *) old_tle->resdom);
- newresno->resno = length(t_list) + 1;
- newresno->resjunk = true;
- new_tl = makeTargetEntry(newresno, old_tle->expr);
+ Resdom *newresdom;
+
+ newresdom = (Resdom *) copyObject((Node *) old_tle->resdom);
+ newresdom->resno = length(t_list) + 1;
+ newresdom->resjunk = true;
+ new_tl = makeTargetEntry(newresdom, old_tle->expr);
t_list = lappend(t_list, new_tl);
}
}
diff --git a/src/backend/optimizer/prep/prepunion.c b/src/backend/optimizer/prep/prepunion.c
index 21afad92b82..54e84739d24 100644
--- a/src/backend/optimizer/prep/prepunion.c
+++ b/src/backend/optimizer/prep/prepunion.c
@@ -7,7 +7,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepunion.c,v 1.38 1999/08/16 02:17:55 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepunion.c,v 1.39 1999/08/21 03:49:05 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -159,7 +159,7 @@ plan_union_queries(Query *parse)
union_plans = lcons(union_planner(parse), NIL);
union_rts = lcons(parse->rtable, NIL);
- /* Append the remainging UNION ALLs */
+ /* Append the remaining UNION ALLs */
foreach(ulist, union_all_queries)
{
Query *union_all_query = lfirst(ulist);
@@ -172,14 +172,19 @@ plan_union_queries(Query *parse)
/* We have already split UNION and UNION ALL and we made it consistent */
if (!last_union_all_flag)
{
+ /* Need SELECT DISTINCT behavior to implement UNION.
+ * Set uniqueFlag properly, put back the held sortClause,
+ * and add any missing columns to the sort clause.
+ */
parse->uniqueFlag = "*";
- parse->sortClause = transformSortClause(NULL, NIL,
- hold_sortClause,
- parse->targetList, "*");
+ parse->sortClause = addAllTargetsToSortList(hold_sortClause,
+ parse->targetList);
}
else
+ {
/* needed so we don't take the flag from the first query */
parse->uniqueFlag = NULL;
+ }
/* Make sure we don't try to apply the first query's grouping stuff
* to the Append node, either. Basically we don't want union_planner