From 64917ccb561e52aac4efa07eabc6f653d296b1dc Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Sun, 29 Feb 2004 17:36:05 +0000 Subject: [PATCH] make_sort_from_pathkeys()'s method for choosing which of several equivalent sort expressions to use was broken: you can't just look at the relation membership, you have to actually grovel over the individual Vars in each expression. I think this did work when it was written, but it was broken by subsequent optimizations that made join relations not propagate every single input variable upward. Must find the Var that got propagated, not choose one at random. Per bug report from Daniel O'Neill. --- src/backend/optimizer/plan/createplan.c | 26 +++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c index ffb34e4be5..5b52a63f02 100644 --- a/src/backend/optimizer/plan/createplan.c +++ b/src/backend/optimizer/plan/createplan.c @@ -10,7 +10,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/optimizer/plan/createplan.c,v 1.167 2004/01/18 00:50:02 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/optimizer/plan/createplan.c,v 1.168 2004/02/29 17:36:05 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -106,7 +106,7 @@ static MergeJoin *make_mergejoin(List *tlist, static Sort *make_sort(Query *root, Plan *lefttree, int numCols, AttrNumber *sortColIdx, Oid *sortOperators); static Sort *make_sort_from_pathkeys(Query *root, Plan *lefttree, - Relids relids, List *pathkeys); + List *pathkeys); /* @@ -1057,7 +1057,6 @@ create_mergejoin_plan(Query *root, outer_plan = (Plan *) make_sort_from_pathkeys(root, outer_plan, - best_path->jpath.outerjoinpath->parent->relids, best_path->outersortkeys); } @@ -1067,7 +1066,6 @@ create_mergejoin_plan(Query *root, inner_plan = (Plan *) make_sort_from_pathkeys(root, inner_plan, - best_path->jpath.innerjoinpath->parent->relids, best_path->innersortkeys); } @@ -1837,7 +1835,6 @@ add_sort_column(AttrNumber colIdx, Oid sortOp, * Create sort plan to sort according to given pathkeys * * 'lefttree' is the node which yields input tuples - * 'relids' is the set of relids represented by the input node * 'pathkeys' is the list of pathkeys by which the result is to be sorted * * We must convert the pathkey information into arrays of sort key column @@ -1850,8 +1847,7 @@ add_sort_column(AttrNumber colIdx, Oid sortOp, * adding a Result node just to do the projection. */ static Sort * -make_sort_from_pathkeys(Query *root, Plan *lefttree, - Relids relids, List *pathkeys) +make_sort_from_pathkeys(Query *root, Plan *lefttree, List *pathkeys) { List *tlist = lefttree->targetlist; List *i; @@ -1895,12 +1891,22 @@ make_sort_from_pathkeys(Query *root, Plan *lefttree, } if (!resdom) { - /* No matching Var; look for an expression */ + /* No matching Var; look for a computable expression */ foreach(j, keysublist) { + List *exprvars; + List *k; + pathkey = lfirst(j); - if (bms_is_subset(pull_varnos(pathkey->key), relids)) - break; + exprvars = pull_var_clause(pathkey->key, false); + foreach(k, exprvars) + { + if (!tlist_member(lfirst(k), tlist)) + break; + } + freeList(exprvars); + if (!k) + break; /* found usable expression */ } if (!j) elog(ERROR, "could not find pathkey item to sort"); -- 2.40.0