56 int strategy,
bool nulls_first)
64 elog(
ERROR,
"too soon to build canonical pathkeys");
73 if (
eclass == pk->pk_eclass &&
167 foreach(lc, pathkeys)
171 if (new_ec == old_pathkey->pk_eclass)
225 elog(
ERROR,
"missing operator %d(%u,%u) in opfamily %u",
229 elog(
ERROR,
"could not find opfamilies for equality operator %u",
234 opfamilies, opcintype, collation,
235 sortref, rel, create_it);
243 strategy, nulls_first);
268 &opfamily, &opcintype, &strategy))
269 elog(
ERROR,
"operator %u is not a valid ordering operator",
314 forboth(key1, keys1, key2, keys2)
319 if (pathkey1 != pathkey2)
374 else if (keys1 ==
NIL)
379 else if (keys2 ==
NIL)
389 forboth(key1, keys1, key2, keys2)
394 if (pathkey1 != pathkey2)
404 return (key1 == NULL);
423 bool require_parallel_safe)
425 Path *matched_path = NULL;
436 if (matched_path != NULL &&
470 Path *matched_path = NULL;
481 if (matched_path != NULL &&
547 if (
index->sortopfamily == NULL)
551 foreach(lc,
index->indextlist)
563 if (
i >=
index->nkeycolumns)
567 indexkey = indextle->
expr;
571 reverse_sort = !
index->reverse_sort[
i];
572 nulls_first = !
index->nulls_first[
i];
576 reverse_sort =
index->reverse_sort[
i];
577 nulls_first =
index->nulls_first[
i];
587 index->indexcollations[
i],
601 retval =
lappend(retval, cpathkey);
654 if (!IsBuiltinBooleanOpfamily(partscheme->
partopfamily[partkeycol]))
663 if (rinfo->pseudoconstant)
690 if (
equal(partexpr, clause))
725 Assert(partscheme != NULL);
761 retval =
lappend(retval, cpathkey);
783 *partialkeys =
false;
813 &opfamily, &opcintype, &strategy))
814 elog(
ERROR,
"operator %u is not a valid ordering operator",
854 List *subquery_pathkeys,
855 List *subquery_tlist)
862 foreach(
i, subquery_pathkeys)
879 elog(
ERROR,
"volatile EquivalenceClass has no sortref");
956 foreach(k, subquery_tlist)
979 if (!
equal(tle_expr, sub_expr))
1007 if (retvallen < outer_query_keys &&
1010 if (score > best_score)
1012 best_pathkey = outer_pk;
1032 retval =
lappend(retval, best_pathkey);
1096 List *outer_pathkeys)
1169 bool remove_redundant,
1176 foreach(l, *sortclauses)
1197 pathkeys =
lappend(pathkeys, pathkey);
1198 else if (remove_redundant)
1235 Assert(restrictinfo->mergeopfamilies !=
NIL);
1237 Assert(restrictinfo->left_ec == NULL);
1238 Assert(restrictinfo->right_ec == NULL);
1244 restrictinfo->left_ec =
1247 restrictinfo->mergeopfamilies,
1249 ((
OpExpr *) clause)->inputcollid,
1253 restrictinfo->right_ec =
1256 restrictinfo->mergeopfamilies,
1258 ((
OpExpr *) clause)->inputcollid,
1278 Assert(restrictinfo->mergeopfamilies !=
NIL);
1280 Assert(restrictinfo->left_ec != NULL);
1281 Assert(restrictinfo->right_ec != NULL);
1284 while (restrictinfo->left_ec->ec_merged)
1285 restrictinfo->left_ec = restrictinfo->left_ec->ec_merged;
1286 while (restrictinfo->right_ec->ec_merged)
1287 restrictinfo->right_ec = restrictinfo->right_ec->ec_merged;
1311 List *restrictinfos)
1317 foreach(
i, restrictinfos)
1324 foreach(
i, pathkeys)
1328 List *matched_restrictinfos =
NIL;
1368 foreach(
j, restrictinfos)
1373 clause_ec = rinfo->outer_is_left ?
1374 rinfo->left_ec : rinfo->right_ec;
1375 if (clause_ec == pathkey_ec)
1376 matched_restrictinfos =
lappend(matched_restrictinfos, rinfo);
1384 if (matched_restrictinfos ==
NIL)
1391 mergeclauses =
list_concat(mergeclauses, matched_restrictinfos);
1394 return mergeclauses;
1445 scores = (
int *)
palloc(nClauses *
sizeof(
int));
1448 foreach(lc, mergeclauses)
1458 if (rinfo->outer_is_left)
1459 oeclass = rinfo->left_ec;
1461 oeclass = rinfo->right_ec;
1464 for (
j = 0;
j < necs;
j++)
1466 if (ecs[
j] == oeclass)
1484 ecs[necs] = oeclass;
1485 scores[necs] = score;
1506 for (
j = 0;
j < necs;
j++)
1508 if (ecs[
j] == query_ec)
1527 for (
j = 0;
j < necs;
j++)
1529 if (ecs[
j] == query_ec)
1544 else if (matches == nClauses)
1569 best_score = scores[0];
1570 for (
j = 1;
j < necs;
j++)
1572 if (scores[
j] > best_score)
1575 best_score = scores[
j];
1581 scores[best_j] = -1;
1589 pathkeys =
lappend(pathkeys, pathkey);
1622 List *outer_pathkeys)
1634 foreach(lc, mergeclauses)
1643 if (rinfo->outer_is_left)
1645 oeclass = rinfo->left_ec;
1646 ieclass = rinfo->right_ec;
1650 oeclass = rinfo->right_ec;
1651 ieclass = rinfo->left_ec;
1656 if (oeclass != lastoeclass)
1659 elog(
ERROR,
"too few pathkeys for mergeclauses");
1661 lop =
lnext(outer_pathkeys, lop);
1662 lastoeclass = opathkey->pk_eclass;
1663 if (oeclass != lastoeclass)
1664 elog(
ERROR,
"outer pathkeys do not match mergeclause");
1672 if (ieclass == oeclass)
1691 pathkeys =
lappend(pathkeys, pathkey);
1730 bool matched_pathkey;
1735 if (pathkeys ==
NIL)
1740 pathkey_ec = pathkey->pk_eclass;
1741 lip =
lnext(pathkeys, lip);
1742 matched_pathkey =
false;
1745 foreach(
i, mergeclauses)
1753 clause_ec = rinfo->outer_is_left ?
1754 rinfo->right_ec : rinfo->left_ec;
1757 if (clause_ec != pathkey_ec)
1760 if (!matched_pathkey)
1767 pathkey_ec = pathkey->pk_eclass;
1768 lip =
lnext(pathkeys, lip);
1769 matched_pathkey =
false;
1773 if (clause_ec == pathkey_ec)
1775 new_mergeclauses =
lappend(new_mergeclauses, rinfo);
1776 matched_pathkey =
true;
1785 return new_mergeclauses;
1823 foreach(
i, pathkeys)
1826 bool matched =
false;
1852 if (restrictinfo->mergeopfamilies ==
NIL)
1856 if (pathkey->pk_eclass == restrictinfo->left_ec ||
1857 pathkey->pk_eclass == restrictinfo->right_ec)
1893 if (pathkey->pk_eclass == query_pathkey->pk_eclass &&
1924 int n_common_pathkeys;
1929 if (pathkeys ==
NIL)
1933 &n_common_pathkeys);
1935 return n_common_pathkeys;
1952 if (nuseful2 > nuseful)
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
#define OidIsValid(objectId)
elog(ERROR, "%s: %s", p2, msg)
bool equal(const void *a, const void *b)
Expr * canonicalize_ec_expression(Expr *expr, Oid req_type, Oid req_collation)
EquivalenceClass * get_eclass_for_sort_expr(PlannerInfo *root, Expr *expr, List *opfamilies, Oid opcintype, Oid collation, Index sortref, Relids rel, bool create_it)
bool eclass_useful_for_merging(PlannerInfo *root, EquivalenceClass *eclass, RelOptInfo *rel)
bool indexcol_is_bool_constant_for_query(PlannerInfo *root, IndexOptInfo *index, int indexcol)
Assert(fmt[strlen(fmt) - 1] !='\n')
List * lappend(List *list, void *datum)
List * list_copy_head(const List *oldlist, int len)
List * list_copy(const List *oldlist)
List * list_concat(List *list1, const List *list2)
List * get_mergejoin_opfamilies(Oid opno)
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
bool get_ordering_op_properties(Oid opno, Oid *opfamily, Oid *opcintype, int16 *strategy)
void op_input_types(Oid opno, Oid *lefttype, Oid *righttype)
void pfree(void *pointer)
Oid exprCollation(const Node *expr)
static Expr * get_notclausearg(const void *notclause)
static Node * get_rightop(const void *clause)
static bool is_notclause(const void *clause)
static Node * get_leftop(const void *clause)
#define IsA(nodeptr, _type_)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
bool partitions_are_ordered(PartitionBoundInfo boundinfo, Bitmapset *live_parts)
static bool matches_boolean_partition_clause(RestrictInfo *rinfo, RelOptInfo *partrel, int partkeycol)
List * build_join_pathkeys(PlannerInfo *root, RelOptInfo *joinrel, JoinType jointype, List *outer_pathkeys)
List * build_expression_pathkey(PlannerInfo *root, Expr *expr, Oid opno, Relids rel, bool create_it)
static bool right_merge_direction(PlannerInfo *root, PathKey *pathkey)
List * make_inner_pathkeys_for_merge(PlannerInfo *root, List *mergeclauses, List *outer_pathkeys)
Path * get_cheapest_path_for_pathkeys(List *paths, List *pathkeys, Relids required_outer, CostSelector cost_criterion, bool require_parallel_safe)
bool pathkeys_count_contained_in(List *keys1, List *keys2, int *n_common)
List * find_mergeclauses_for_outer_pathkeys(PlannerInfo *root, List *pathkeys, List *restrictinfos)
static PathKey * make_pathkey_from_sortop(PlannerInfo *root, Expr *expr, Oid ordering_op, bool nulls_first, Index sortref, bool create_it)
bool has_useful_pathkeys(PlannerInfo *root, RelOptInfo *rel)
List * append_pathkeys(List *target, List *source)
List * truncate_useless_pathkeys(PlannerInfo *root, RelOptInfo *rel, List *pathkeys)
static int pathkeys_useful_for_ordering(PlannerInfo *root, List *pathkeys)
List * trim_mergeclauses_for_inner_pathkeys(PlannerInfo *root, List *mergeclauses, List *pathkeys)
List * select_outer_pathkeys_for_merge(PlannerInfo *root, List *mergeclauses, RelOptInfo *joinrel)
void update_mergeclause_eclasses(PlannerInfo *root, RestrictInfo *restrictinfo)
static Var * find_var_for_subquery_tle(RelOptInfo *rel, TargetEntry *tle)
List * build_index_pathkeys(PlannerInfo *root, IndexOptInfo *index, ScanDirection scandir)
static bool partkey_is_bool_constant_for_query(RelOptInfo *partrel, int partkeycol)
static bool pathkey_is_redundant(PathKey *new_pathkey, List *pathkeys)
List * make_pathkeys_for_sortclauses_extended(PlannerInfo *root, List **sortclauses, List *tlist, bool remove_redundant, bool *sortable)
PathKey * make_canonical_pathkey(PlannerInfo *root, EquivalenceClass *eclass, Oid opfamily, int strategy, bool nulls_first)
Path * get_cheapest_parallel_safe_total_inner(List *paths)
List * make_pathkeys_for_sortclauses(PlannerInfo *root, List *sortclauses, List *tlist)
static int pathkeys_useful_for_merging(PlannerInfo *root, RelOptInfo *rel, List *pathkeys)
List * convert_subquery_pathkeys(PlannerInfo *root, RelOptInfo *rel, List *subquery_pathkeys, List *subquery_tlist)
static PathKey * make_pathkey_from_sortinfo(PlannerInfo *root, Expr *expr, Oid opfamily, Oid opcintype, Oid collation, bool reverse_sort, bool nulls_first, Index sortref, Relids rel, bool create_it)
void initialize_mergeclause_eclasses(PlannerInfo *root, RestrictInfo *restrictinfo)
Path * get_cheapest_fractional_path_for_pathkeys(List *paths, List *pathkeys, Relids required_outer, double fraction)
bool pathkeys_contained_in(List *keys1, List *keys2)
PathKeysComparison compare_pathkeys(List *keys1, List *keys2)
List * build_partition_pathkeys(PlannerInfo *root, RelOptInfo *partrel, ScanDirection scandir, bool *partialkeys)
int compare_fractional_path_costs(Path *path1, Path *path2, double fraction)
int compare_path_costs(Path *path1, Path *path2, CostSelector criterion)
#define EC_MUST_BE_REDUNDANT(eclass)
#define IS_SIMPLE_REL(rel)
#define PATH_REQ_OUTER(path)
#define lfirst_node(type, lc)
static int list_length(const List *l)
#define forboth(cell1, list1, cell2, list2)
static ListCell * list_head(const List *l)
static void * list_nth(const List *list, int n)
static ListCell * lnext(const List *l, const ListCell *c)
#define foreach_delete_current(lst, cell)
static rewind_source * source
static struct cvec * eclass(struct vars *v, chr c, int cases)
#define ScanDirectionIsBackward(direction)
#define BTGreaterStrategyNumber
#define BTLessStrategyNumber
#define BTEqualStrategyNumber
struct PathTarget * reltarget
TargetEntry * get_sortgroupref_tle(Index sortref, List *targetList)
Node * get_sortgroupclause_expr(SortGroupClause *sgClause, List *targetList)