58 int strategy,
bool nulls_first)
65 if (!
root->ec_merging_done)
66 elog(
ERROR,
"too soon to build canonical pathkeys");
72 foreach(lc,
root->canon_pathkeys)
75 if (
eclass == pk->pk_eclass &&
169 foreach(lc, pathkeys)
173 if (new_ec == old_pathkey->pk_eclass)
227 elog(
ERROR,
"missing operator %d(%u,%u) in opfamily %u",
231 elog(
ERROR,
"could not find opfamilies for equality operator %u",
236 opfamilies, opcintype, collation,
237 sortref, rel, create_it);
245 strategy, nulls_first);
271 &opfamily, &opcintype, &strategy))
272 elog(
ERROR,
"operator %u is not a valid ordering operator",
317 forboth(key1, keys1, key2, keys2)
322 if (pathkey1 != pathkey2)
371 List **group_clauses,
372 int num_groupby_pathkeys)
374 List *new_group_pathkeys =
NIL,
375 *new_group_clauses =
NIL;
376 List *grouping_pathkeys;
380 if (pathkeys ==
NIL || *group_pathkeys ==
NIL)
392 grouping_pathkeys =
list_copy_head(*group_pathkeys, num_groupby_pathkeys);
403 foreach(lc, pathkeys)
415 pathkey->pk_eclass->ec_sortref == 0)
435 new_group_pathkeys =
lappend(new_group_pathkeys, pathkey);
436 new_group_clauses =
lappend(new_group_clauses, sgc);
473 List *pathkeys =
root->group_pathkeys;
474 List *clauses =
root->processed_groupClause;
493 if (
parse->groupingSets)
507 root->num_groupby_pathkeys);
521#ifdef USE_ASSERT_CHECKING
573 else if (keys1 ==
NIL)
578 else if (keys2 ==
NIL)
588 forboth(key1, keys1, key2, keys2)
593 if (pathkey1 != pathkey2)
603 return (key1 == NULL);
623 bool require_parallel_safe)
625 Path *matched_path = NULL;
640 if (matched_path != NULL &&
671 Path *matched_path = NULL;
682 if (matched_path != NULL &&
748 if (
index->sortopfamily == NULL)
752 foreach(lc,
index->indextlist)
764 if (
i >=
index->nkeycolumns)
768 indexkey = indextle->
expr;
772 reverse_sort = !
index->reverse_sort[
i];
773 nulls_first = !
index->nulls_first[
i];
777 reverse_sort =
index->reverse_sort[
i];
778 nulls_first =
index->nulls_first[
i];
788 index->indexcollations[
i],
802 retval =
lappend(retval, cpathkey);
855 if (!IsBuiltinBooleanOpfamily(partscheme->
partopfamily[partkeycol]))
864 if (rinfo->pseudoconstant)
891 if (
equal(partexpr, clause))
926 Assert(partscheme != NULL);
962 retval =
lappend(retval, cpathkey);
984 *partialkeys =
false;
1014 &opfamily, &opcintype, &strategy))
1015 elog(
ERROR,
"operator %u is not a valid ordering operator",
1055 List *subquery_pathkeys,
1056 List *subquery_tlist)
1063 foreach(
i, subquery_pathkeys)
1080 elog(
ERROR,
"volatile EquivalenceClass has no sortref");
1143 int best_score = -1;
1157 foreach(k, subquery_tlist)
1180 if (!
equal(tle_expr, sub_expr))
1208 if (retvallen < outer_query_keys &&
1211 if (score > best_score)
1213 best_pathkey = outer_pk;
1233 retval =
lappend(retval, best_pathkey);
1297 List *outer_pathkeys)
1383 bool remove_redundant,
1384 bool remove_group_rtindex,
1386 bool set_ec_sortref)
1392 foreach(l, *sortclauses)
1404 if (remove_group_rtindex)
1419 if (pathkey->pk_eclass->ec_sortref == 0 && set_ec_sortref)
1431 pathkeys =
lappend(pathkeys, pathkey);
1432 else if (remove_redundant)
1469 Assert(restrictinfo->mergeopfamilies !=
NIL);
1471 Assert(restrictinfo->left_ec == NULL);
1472 Assert(restrictinfo->right_ec == NULL);
1478 restrictinfo->left_ec =
1481 restrictinfo->mergeopfamilies,
1483 ((
OpExpr *) clause)->inputcollid,
1487 restrictinfo->right_ec =
1490 restrictinfo->mergeopfamilies,
1492 ((
OpExpr *) clause)->inputcollid,
1512 Assert(restrictinfo->mergeopfamilies !=
NIL);
1514 Assert(restrictinfo->left_ec != NULL);
1515 Assert(restrictinfo->right_ec != NULL);
1518 while (restrictinfo->left_ec->ec_merged)
1519 restrictinfo->left_ec = restrictinfo->left_ec->ec_merged;
1520 while (restrictinfo->right_ec->ec_merged)
1521 restrictinfo->right_ec = restrictinfo->right_ec->ec_merged;
1545 List *restrictinfos)
1551 foreach(
i, restrictinfos)
1558 foreach(
i, pathkeys)
1562 List *matched_restrictinfos =
NIL;
1602 foreach(
j, restrictinfos)
1607 clause_ec = rinfo->outer_is_left ?
1608 rinfo->left_ec : rinfo->right_ec;
1609 if (clause_ec == pathkey_ec)
1610 matched_restrictinfos =
lappend(matched_restrictinfos, rinfo);
1618 if (matched_restrictinfos ==
NIL)
1625 mergeclauses =
list_concat(mergeclauses, matched_restrictinfos);
1628 return mergeclauses;
1679 scores = (
int *)
palloc(nClauses *
sizeof(
int));
1682 foreach(lc, mergeclauses)
1692 if (rinfo->outer_is_left)
1693 oeclass = rinfo->left_ec;
1695 oeclass = rinfo->right_ec;
1698 for (
j = 0;
j < necs;
j++)
1700 if (ecs[
j] == oeclass)
1718 ecs[necs] = oeclass;
1719 scores[necs] = score;
1731 if (
root->query_pathkeys)
1735 foreach(lc,
root->query_pathkeys)
1740 for (
j = 0;
j < necs;
j++)
1742 if (ecs[
j] == query_ec)
1756 foreach(lc,
root->query_pathkeys)
1761 for (
j = 0;
j < necs;
j++)
1763 if (ecs[
j] == query_ec)
1778 else if (matches == nClauses)
1803 best_score = scores[0];
1804 for (
j = 1;
j < necs;
j++)
1806 if (scores[
j] > best_score)
1809 best_score = scores[
j];
1815 scores[best_j] = -1;
1823 pathkeys =
lappend(pathkeys, pathkey);
1856 List *outer_pathkeys)
1868 foreach(lc, mergeclauses)
1877 if (rinfo->outer_is_left)
1879 oeclass = rinfo->left_ec;
1880 ieclass = rinfo->right_ec;
1884 oeclass = rinfo->right_ec;
1885 ieclass = rinfo->left_ec;
1890 if (oeclass != lastoeclass)
1893 elog(
ERROR,
"too few pathkeys for mergeclauses");
1895 lop =
lnext(outer_pathkeys, lop);
1896 lastoeclass = opathkey->pk_eclass;
1897 if (oeclass != lastoeclass)
1898 elog(
ERROR,
"outer pathkeys do not match mergeclause");
1906 if (ieclass == oeclass)
1925 pathkeys =
lappend(pathkeys, pathkey);
1964 bool matched_pathkey;
1969 if (pathkeys ==
NIL)
1974 pathkey_ec = pathkey->pk_eclass;
1975 lip =
lnext(pathkeys, lip);
1976 matched_pathkey =
false;
1979 foreach(
i, mergeclauses)
1987 clause_ec = rinfo->outer_is_left ?
1988 rinfo->right_ec : rinfo->left_ec;
1991 if (clause_ec != pathkey_ec)
1994 if (!matched_pathkey)
2001 pathkey_ec = pathkey->pk_eclass;
2002 lip =
lnext(pathkeys, lip);
2003 matched_pathkey =
false;
2007 if (clause_ec == pathkey_ec)
2009 new_mergeclauses =
lappend(new_mergeclauses, rinfo);
2010 matched_pathkey =
true;
2019 return new_mergeclauses;
2057 foreach(
i, pathkeys)
2060 bool matched =
false;
2086 if (restrictinfo->mergeopfamilies ==
NIL)
2090 if (pathkey->pk_eclass == restrictinfo->left_ec ||
2091 pathkey->pk_eclass == restrictinfo->right_ec)
2123 foreach(l,
root->query_pathkeys)
2127 if (pathkey->pk_eclass == query_pathkey->pk_eclass &&
2158 int n_common_pathkeys;
2161 &n_common_pathkeys);
2163 return n_common_pathkeys;
2192 if (
root->group_pathkeys ==
NIL)
2196 foreach(
key, pathkeys)
2222 int n_common_pathkeys;
2228 if (
root->distinct_pathkeys ==
NIL)
2232 n_common_pathkeys = 0;
2239 n_common_pathkeys++;
2242 return n_common_pathkeys;
2253 int n_common_pathkeys;
2256 &n_common_pathkeys);
2258 return n_common_pathkeys;
2275 if (nuseful2 > nuseful)
2278 if (nuseful2 > nuseful)
2281 if (nuseful2 > nuseful)
2284 if (nuseful2 > nuseful)
2319 if (
root->group_pathkeys !=
NIL)
2321 if (
root->query_pathkeys !=
NIL)
Bitmapset * bms_make_singleton(int x)
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
#define Assert(condition)
#define OidIsValid(objectId)
bool enable_incremental_sort
bool equal(const void *a, const void *b)
EquivalenceClass * get_eclass_for_sort_expr(PlannerInfo *root, Expr *expr, List *opfamilies, Oid opcintype, Oid collation, Index sortref, Relids rel, bool create_it)
Expr * canonicalize_ec_expression(Expr *expr, Oid req_type, Oid req_collation)
bool eclass_useful_for_merging(PlannerInfo *root, EquivalenceClass *eclass, RelOptInfo *rel)
bool indexcol_is_bool_constant_for_query(PlannerInfo *root, IndexOptInfo *index, int indexcol)
List * list_difference(const List *list1, const List *list2)
List * lappend(List *list, void *datum)
List * list_difference_ptr(const List *list1, const List *list2)
List * list_concat_unique_ptr(List *list1, const List *list2)
List * list_concat(List *list1, const List *list2)
List * list_copy(const List *oldlist)
bool list_member_ptr(const List *list, const void *datum)
void list_free(List *list)
List * list_copy_head(const List *oldlist, int len)
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)
List * get_mergejoin_opfamilies(Oid opno)
void op_input_types(Oid opno, Oid *lefttype, Oid *righttype)
void pfree(void *pointer)
Oid exprCollation(const Node *expr)
static Node * get_rightop(const void *clause)
static bool is_notclause(const void *clause)
static Expr * get_notclausearg(const void *notclause)
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 * truncate_useless_pathkeys(PlannerInfo *root, RelOptInfo *rel, List *pathkeys)
static int pathkeys_useful_for_setop(PlannerInfo *root, List *pathkeys)
Path * get_cheapest_fractional_path_for_pathkeys(List *paths, List *pathkeys, Relids required_outer, double fraction)
static bool right_merge_direction(PlannerInfo *root, PathKey *pathkey)
List * append_pathkeys(List *target, List *source)
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 * make_inner_pathkeys_for_merge(PlannerInfo *root, List *mergeclauses, List *outer_pathkeys)
static int pathkeys_useful_for_distinct(PlannerInfo *root, List *pathkeys)
static PathKey * make_pathkey_from_sortop(PlannerInfo *root, Expr *expr, Oid ordering_op, bool reverse_sort, bool nulls_first, Index sortref, bool create_it)
static int group_keys_reorder_by_pathkeys(List *pathkeys, List **group_pathkeys, List **group_clauses, int num_groupby_pathkeys)
bool has_useful_pathkeys(PlannerInfo *root, RelOptInfo *rel)
List * build_index_pathkeys(PlannerInfo *root, IndexOptInfo *index, ScanDirection scandir)
static int pathkeys_useful_for_ordering(PlannerInfo *root, List *pathkeys)
List * find_mergeclauses_for_outer_pathkeys(PlannerInfo *root, List *pathkeys, List *restrictinfos)
void update_mergeclause_eclasses(PlannerInfo *root, RestrictInfo *restrictinfo)
List * make_pathkeys_for_sortclauses(PlannerInfo *root, List *sortclauses, List *tlist)
static Var * find_var_for_subquery_tle(RelOptInfo *rel, TargetEntry *tle)
List * trim_mergeclauses_for_inner_pathkeys(PlannerInfo *root, List *mergeclauses, List *pathkeys)
static int pathkeys_useful_for_grouping(PlannerInfo *root, List *pathkeys)
static bool partkey_is_bool_constant_for_query(RelOptInfo *partrel, int partkeycol)
bool enable_group_by_reordering
List * build_expression_pathkey(PlannerInfo *root, Expr *expr, Oid opno, Relids rel, bool create_it)
static bool pathkey_is_redundant(PathKey *new_pathkey, List *pathkeys)
List * build_partition_pathkeys(PlannerInfo *root, RelOptInfo *partrel, ScanDirection scandir, bool *partialkeys)
List * select_outer_pathkeys_for_merge(PlannerInfo *root, List *mergeclauses, RelOptInfo *joinrel)
List * convert_subquery_pathkeys(PlannerInfo *root, RelOptInfo *rel, List *subquery_pathkeys, List *subquery_tlist)
static int pathkeys_useful_for_merging(PlannerInfo *root, RelOptInfo *rel, List *pathkeys)
List * make_pathkeys_for_sortclauses_extended(PlannerInfo *root, List **sortclauses, List *tlist, bool remove_redundant, bool remove_group_rtindex, bool *sortable, bool set_ec_sortref)
void initialize_mergeclause_eclasses(PlannerInfo *root, RestrictInfo *restrictinfo)
bool pathkeys_contained_in(List *keys1, List *keys2)
List * build_join_pathkeys(PlannerInfo *root, RelOptInfo *joinrel, JoinType jointype, List *outer_pathkeys)
PathKey * make_canonical_pathkey(PlannerInfo *root, EquivalenceClass *eclass, Oid opfamily, int strategy, bool nulls_first)
PathKeysComparison compare_pathkeys(List *keys1, List *keys2)
Path * get_cheapest_parallel_safe_total_inner(List *paths)
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)
List * get_useful_group_keys_orderings(PlannerInfo *root, Path *path)
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 linitial_node(type, l)
#define forboth(cell1, list1, cell2, list2)
#define foreach_current_index(var_or_cell)
#define foreach_delete_current(lst, var_or_cell)
#define for_each_from(cell, lst, N)
static void * list_nth(const List *list, int n)
#define foreach_node(type, var, lst)
static ListCell * list_head(const List *l)
static ListCell * lnext(const List *l, const ListCell *c)
static rewind_source * source
static struct cvec * eclass(struct vars *v, chr c, int cases)
static struct subre * parse(struct vars *v, int stopper, int type, struct state *init, struct state *final)
Node * remove_nulling_relids(Node *node, const Bitmapset *removable_relids, const Bitmapset *except_relids)
#define ScanDirectionIsBackward(direction)
#define BTGreaterStrategyNumber
#define BTLessStrategyNumber
#define BTEqualStrategyNumber
struct PathTarget * reltarget
SortGroupClause * get_sortgroupref_clause_noerr(Index sortref, List *clauses)
TargetEntry * get_sortgroupref_tle(Index sortref, List *targetList)
Node * get_sortgroupclause_expr(SortGroupClause *sgClause, List *targetList)