PostgreSQL Source Code git master
pathkeys.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * pathkeys.c
4 * Utilities for matching and building path keys
5 *
6 * See src/backend/optimizer/README for a great deal of information about
7 * the nature and use of path keys.
8 *
9 *
10 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
11 * Portions Copyright (c) 1994, Regents of the University of California
12 *
13 * IDENTIFICATION
14 * src/backend/optimizer/path/pathkeys.c
15 *
16 *-------------------------------------------------------------------------
17 */
18#include "postgres.h"
19
20#include "access/stratnum.h"
21#include "catalog/pg_opfamily.h"
22#include "nodes/nodeFuncs.h"
23#include "optimizer/cost.h"
24#include "optimizer/optimizer.h"
25#include "optimizer/pathnode.h"
26#include "optimizer/paths.h"
29#include "utils/lsyscache.h"
30
31/* Consider reordering of GROUP BY keys? */
33
34static bool pathkey_is_redundant(PathKey *new_pathkey, List *pathkeys);
36 RelOptInfo *partrel,
37 int partkeycol);
39static bool right_merge_direction(PlannerInfo *root, PathKey *pathkey);
40
41
42/****************************************************************************
43 * PATHKEY CONSTRUCTION AND REDUNDANCY TESTING
44 ****************************************************************************/
45
46/*
47 * make_canonical_pathkey
48 * Given the parameters for a PathKey, find any pre-existing matching
49 * pathkey in the query's list of "canonical" pathkeys. Make a new
50 * entry if there's not one already.
51 *
52 * Note that this function must not be used until after we have completed
53 * merging EquivalenceClasses.
54 */
55PathKey *
57 EquivalenceClass *eclass, Oid opfamily,
58 int strategy, bool nulls_first)
59{
60 PathKey *pk;
61 ListCell *lc;
62 MemoryContext oldcontext;
63
64 /* Can't make canonical pathkeys if the set of ECs might still change */
65 if (!root->ec_merging_done)
66 elog(ERROR, "too soon to build canonical pathkeys");
67
68 /* The passed eclass might be non-canonical, so chase up to the top */
69 while (eclass->ec_merged)
70 eclass = eclass->ec_merged;
71
72 foreach(lc, root->canon_pathkeys)
73 {
74 pk = (PathKey *) lfirst(lc);
75 if (eclass == pk->pk_eclass &&
76 opfamily == pk->pk_opfamily &&
77 strategy == pk->pk_strategy &&
78 nulls_first == pk->pk_nulls_first)
79 return pk;
80 }
81
82 /*
83 * Be sure canonical pathkeys are allocated in the main planning context.
84 * Not an issue in normal planning, but it is for GEQO.
85 */
86 oldcontext = MemoryContextSwitchTo(root->planner_cxt);
87
88 pk = makeNode(PathKey);
89 pk->pk_eclass = eclass;
90 pk->pk_opfamily = opfamily;
91 pk->pk_strategy = strategy;
92 pk->pk_nulls_first = nulls_first;
93
94 root->canon_pathkeys = lappend(root->canon_pathkeys, pk);
95
96 MemoryContextSwitchTo(oldcontext);
97
98 return pk;
99}
100
101/*
102 * append_pathkeys
103 * Append all non-redundant PathKeys in 'source' onto 'target' and
104 * returns the updated 'target' list.
105 */
106List *
108{
109 ListCell *lc;
110
111 Assert(target != NIL);
112
113 foreach(lc, source)
114 {
115 PathKey *pk = lfirst_node(PathKey, lc);
116
117 if (!pathkey_is_redundant(pk, target))
118 target = lappend(target, pk);
119 }
120 return target;
121}
122
123/*
124 * pathkey_is_redundant
125 * Is a pathkey redundant with one already in the given list?
126 *
127 * We detect two cases:
128 *
129 * 1. If the new pathkey's equivalence class contains a constant, and isn't
130 * below an outer join, then we can disregard it as a sort key. An example:
131 * SELECT ... WHERE x = 42 ORDER BY x, y;
132 * We may as well just sort by y. Note that because of opfamily matching,
133 * this is semantically correct: we know that the equality constraint is one
134 * that actually binds the variable to a single value in the terms of any
135 * ordering operator that might go with the eclass. This rule not only lets
136 * us simplify (or even skip) explicit sorts, but also allows matching index
137 * sort orders to a query when there are don't-care index columns.
138 *
139 * 2. If the new pathkey's equivalence class is the same as that of any
140 * existing member of the pathkey list, then it is redundant. Some examples:
141 * SELECT ... ORDER BY x, x;
142 * SELECT ... ORDER BY x, x DESC;
143 * SELECT ... WHERE x = y ORDER BY x, y;
144 * In all these cases the second sort key cannot distinguish values that are
145 * considered equal by the first, and so there's no point in using it.
146 * Note in particular that we need not compare opfamily (all the opfamilies
147 * of the EC have the same notion of equality) nor sort direction.
148 *
149 * Both the given pathkey and the list members must be canonical for this
150 * to work properly, but that's okay since we no longer ever construct any
151 * non-canonical pathkeys. (Note: the notion of a pathkey *list* being
152 * canonical includes the additional requirement of no redundant entries,
153 * which is exactly what we are checking for here.)
154 *
155 * Because the equivclass.c machinery forms only one copy of any EC per query,
156 * pointer comparison is enough to decide whether canonical ECs are the same.
157 */
158static bool
159pathkey_is_redundant(PathKey *new_pathkey, List *pathkeys)
160{
161 EquivalenceClass *new_ec = new_pathkey->pk_eclass;
162 ListCell *lc;
163
164 /* Check for EC containing a constant --- unconditionally redundant */
165 if (EC_MUST_BE_REDUNDANT(new_ec))
166 return true;
167
168 /* If same EC already used in list, then redundant */
169 foreach(lc, pathkeys)
170 {
171 PathKey *old_pathkey = (PathKey *) lfirst(lc);
172
173 if (new_ec == old_pathkey->pk_eclass)
174 return true;
175 }
176
177 return false;
178}
179
180/*
181 * make_pathkey_from_sortinfo
182 * Given an expression and sort-order information, create a PathKey.
183 * The result is always a "canonical" PathKey, but it might be redundant.
184 *
185 * If the PathKey is being generated from a SortGroupClause, sortref should be
186 * the SortGroupClause's SortGroupRef; otherwise zero.
187 *
188 * If rel is not NULL, it identifies a specific relation we're considering
189 * a path for, and indicates that child EC members for that relation can be
190 * considered. Otherwise child members are ignored. (See the comments for
191 * get_eclass_for_sort_expr.)
192 *
193 * create_it is true if we should create any missing EquivalenceClass
194 * needed to represent the sort key. If it's false, we return NULL if the
195 * sort key isn't already present in any EquivalenceClass.
196 */
197static PathKey *
199 Expr *expr,
200 Oid opfamily,
201 Oid opcintype,
202 Oid collation,
203 bool reverse_sort,
204 bool nulls_first,
205 Index sortref,
206 Relids rel,
207 bool create_it)
208{
209 int16 strategy;
210 Oid equality_op;
211 List *opfamilies;
213
214 strategy = reverse_sort ? BTGreaterStrategyNumber : BTLessStrategyNumber;
215
216 /*
217 * EquivalenceClasses need to contain opfamily lists based on the family
218 * membership of mergejoinable equality operators, which could belong to
219 * more than one opfamily. So we have to look up the opfamily's equality
220 * operator and get its membership.
221 */
222 equality_op = get_opfamily_member(opfamily,
223 opcintype,
224 opcintype,
226 if (!OidIsValid(equality_op)) /* shouldn't happen */
227 elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
228 BTEqualStrategyNumber, opcintype, opcintype, opfamily);
229 opfamilies = get_mergejoin_opfamilies(equality_op);
230 if (!opfamilies) /* certainly should find some */
231 elog(ERROR, "could not find opfamilies for equality operator %u",
232 equality_op);
233
234 /* Now find or (optionally) create a matching EquivalenceClass */
236 opfamilies, opcintype, collation,
237 sortref, rel, create_it);
238
239 /* Fail if no EC and !create_it */
240 if (!eclass)
241 return NULL;
242
243 /* And finally we can find or create a PathKey node */
244 return make_canonical_pathkey(root, eclass, opfamily,
245 strategy, nulls_first);
246}
247
248/*
249 * make_pathkey_from_sortop
250 * Like make_pathkey_from_sortinfo, but work from a sort operator.
251 *
252 * This should eventually go away, but we need to restructure SortGroupClause
253 * first.
254 */
255static PathKey *
257 Expr *expr,
258 Oid ordering_op,
259 bool reverse_sort,
260 bool nulls_first,
261 Index sortref,
262 bool create_it)
263{
264 Oid opfamily,
265 opcintype,
266 collation;
267 int16 strategy;
268
269 /* Find the operator in pg_amop --- failure shouldn't happen */
270 if (!get_ordering_op_properties(ordering_op,
271 &opfamily, &opcintype, &strategy))
272 elog(ERROR, "operator %u is not a valid ordering operator",
273 ordering_op);
274
275 /* Because SortGroupClause doesn't carry collation, consult the expr */
276 collation = exprCollation((Node *) expr);
277
279 expr,
280 opfamily,
281 opcintype,
282 collation,
283 reverse_sort,
284 nulls_first,
285 sortref,
286 NULL,
287 create_it);
288}
289
290
291/****************************************************************************
292 * PATHKEY COMPARISONS
293 ****************************************************************************/
294
295/*
296 * compare_pathkeys
297 * Compare two pathkeys to see if they are equivalent, and if not whether
298 * one is "better" than the other.
299 *
300 * We assume the pathkeys are canonical, and so they can be checked for
301 * equality by simple pointer comparison.
302 */
305{
306 ListCell *key1,
307 *key2;
308
309 /*
310 * Fall out quickly if we are passed two identical lists. This mostly
311 * catches the case where both are NIL, but that's common enough to
312 * warrant the test.
313 */
314 if (keys1 == keys2)
315 return PATHKEYS_EQUAL;
316
317 forboth(key1, keys1, key2, keys2)
318 {
319 PathKey *pathkey1 = (PathKey *) lfirst(key1);
320 PathKey *pathkey2 = (PathKey *) lfirst(key2);
321
322 if (pathkey1 != pathkey2)
323 return PATHKEYS_DIFFERENT; /* no need to keep looking */
324 }
325
326 /*
327 * If we reached the end of only one list, the other is longer and
328 * therefore not a subset.
329 */
330 if (key1 != NULL)
331 return PATHKEYS_BETTER1; /* key1 is longer */
332 if (key2 != NULL)
333 return PATHKEYS_BETTER2; /* key2 is longer */
334 return PATHKEYS_EQUAL;
335}
336
337/*
338 * pathkeys_contained_in
339 * Common special case of compare_pathkeys: we just want to know
340 * if keys2 are at least as well sorted as keys1.
341 */
342bool
344{
345 switch (compare_pathkeys(keys1, keys2))
346 {
347 case PATHKEYS_EQUAL:
348 case PATHKEYS_BETTER2:
349 return true;
350 default:
351 break;
352 }
353 return false;
354}
355
356/*
357 * group_keys_reorder_by_pathkeys
358 * Reorder GROUP BY pathkeys and clauses to match the input pathkeys.
359 *
360 * 'pathkeys' is an input list of pathkeys
361 * '*group_pathkeys' and '*group_clauses' are pathkeys and clauses lists to
362 * reorder. The pointers are redirected to new lists, original lists
363 * stay untouched.
364 * 'num_groupby_pathkeys' is the number of first '*group_pathkeys' items to
365 * search matching pathkeys.
366 *
367 * Returns the number of GROUP BY keys with a matching pathkey.
368 */
369static int
370group_keys_reorder_by_pathkeys(List *pathkeys, List **group_pathkeys,
371 List **group_clauses,
372 int num_groupby_pathkeys)
373{
374 List *new_group_pathkeys = NIL,
375 *new_group_clauses = NIL;
376 List *grouping_pathkeys;
377 ListCell *lc;
378 int n;
379
380 if (pathkeys == NIL || *group_pathkeys == NIL)
381 return 0;
382
383 /*
384 * We're going to search within just the first num_groupby_pathkeys of
385 * *group_pathkeys. The thing is that root->group_pathkeys is passed as
386 * *group_pathkeys containing grouping pathkeys altogether with aggregate
387 * pathkeys. If we process aggregate pathkeys we could get an invalid
388 * result of get_sortgroupref_clause_noerr(), because their
389 * pathkey->pk_eclass->ec_sortref doesn't reference query targetlist. So,
390 * we allocate a separate list of pathkeys for lookups.
391 */
392 grouping_pathkeys = list_copy_head(*group_pathkeys, num_groupby_pathkeys);
393
394 /*
395 * Walk the pathkeys (determining ordering of the input path) and see if
396 * there's a matching GROUP BY key. If we find one, we append it to the
397 * list, and do the same for the clauses.
398 *
399 * Once we find the first pathkey without a matching GROUP BY key, the
400 * rest of the pathkeys are useless and can't be used to evaluate the
401 * grouping, so we abort the loop and ignore the remaining pathkeys.
402 */
403 foreach(lc, pathkeys)
404 {
405 PathKey *pathkey = (PathKey *) lfirst(lc);
406 SortGroupClause *sgc;
407
408 /*
409 * Pathkeys are built in a way that allows simply comparing pointers.
410 * Give up if we can't find the matching pointer. Also give up if
411 * there is no sortclause reference for some reason.
412 */
413 if (foreach_current_index(lc) >= num_groupby_pathkeys ||
414 !list_member_ptr(grouping_pathkeys, pathkey) ||
415 pathkey->pk_eclass->ec_sortref == 0)
416 break;
417
418 /*
419 * Since 1349d27 pathkey coming from underlying node can be in the
420 * root->group_pathkeys but not in the processed_groupClause. So, we
421 * should be careful here.
422 */
423 sgc = get_sortgroupref_clause_noerr(pathkey->pk_eclass->ec_sortref,
424 *group_clauses);
425 if (!sgc)
426 /* The grouping clause does not cover this pathkey */
427 break;
428
429 /*
430 * Sort group clause should have an ordering operator as long as there
431 * is an associated pathkey.
432 */
433 Assert(OidIsValid(sgc->sortop));
434
435 new_group_pathkeys = lappend(new_group_pathkeys, pathkey);
436 new_group_clauses = lappend(new_group_clauses, sgc);
437 }
438
439 /* remember the number of pathkeys with a matching GROUP BY key */
440 n = list_length(new_group_pathkeys);
441
442 /* append the remaining group pathkeys (will be treated as not sorted) */
443 *group_pathkeys = list_concat_unique_ptr(new_group_pathkeys,
444 *group_pathkeys);
445 *group_clauses = list_concat_unique_ptr(new_group_clauses,
446 *group_clauses);
447
448 list_free(grouping_pathkeys);
449 return n;
450}
451
452/*
453 * get_useful_group_keys_orderings
454 * Determine which orderings of GROUP BY keys are potentially interesting.
455 *
456 * Returns a list of GroupByOrdering items, each representing an interesting
457 * ordering of GROUP BY keys. Each item stores pathkeys and clauses in the
458 * matching order.
459 *
460 * The function considers (and keeps) following GROUP BY orderings:
461 *
462 * - GROUP BY keys as ordered by preprocess_groupclause() to match target
463 * ORDER BY clause (as much as possible),
464 * - GROUP BY keys reordered to match 'path' ordering (as much as possible).
465 */
466List *
468{
469 Query *parse = root->parse;
470 List *infos = NIL;
471 GroupByOrdering *info;
472
473 List *pathkeys = root->group_pathkeys;
474 List *clauses = root->processed_groupClause;
475
476 /* always return at least the original pathkeys/clauses */
478 info->pathkeys = pathkeys;
479 info->clauses = clauses;
480 infos = lappend(infos, info);
481
482 /*
483 * Should we try generating alternative orderings of the group keys? If
484 * not, we produce only the order specified in the query, i.e. the
485 * optimization is effectively disabled.
486 */
488 return infos;
489
490 /*
491 * Grouping sets have own and more complex logic to decide the ordering.
492 */
493 if (parse->groupingSets)
494 return infos;
495
496 /*
497 * If the path is sorted in some way, try reordering the group keys to
498 * match the path as much of the ordering as possible. Then thanks to
499 * incremental sort we would get this sort as cheap as possible.
500 */
501 if (path->pathkeys &&
502 !pathkeys_contained_in(path->pathkeys, root->group_pathkeys))
503 {
504 int n;
505
506 n = group_keys_reorder_by_pathkeys(path->pathkeys, &pathkeys, &clauses,
507 root->num_groupby_pathkeys);
508
509 if (n > 0 &&
510 (enable_incremental_sort || n == root->num_groupby_pathkeys) &&
511 compare_pathkeys(pathkeys, root->group_pathkeys) != PATHKEYS_EQUAL)
512 {
514 info->pathkeys = pathkeys;
515 info->clauses = clauses;
516
517 infos = lappend(infos, info);
518 }
519 }
520
521#ifdef USE_ASSERT_CHECKING
522 {
524 ListCell *lc;
525
526 /* Test consistency of info structures */
527 for_each_from(lc, infos, 1)
528 {
529 ListCell *lc1,
530 *lc2;
531
532 info = lfirst_node(GroupByOrdering, lc);
533
534 Assert(list_length(info->clauses) == list_length(pinfo->clauses));
536 Assert(list_difference(info->clauses, pinfo->clauses) == NIL);
538
539 forboth(lc1, info->clauses, lc2, info->pathkeys)
540 {
542 PathKey *pk = lfirst_node(PathKey, lc2);
543
544 Assert(pk->pk_eclass->ec_sortref == sgc->tleSortGroupRef);
545 }
546 }
547 }
548#endif
549 return infos;
550}
551
552/*
553 * pathkeys_count_contained_in
554 * Same as pathkeys_contained_in, but also sets length of longest
555 * common prefix of keys1 and keys2.
556 */
557bool
558pathkeys_count_contained_in(List *keys1, List *keys2, int *n_common)
559{
560 int n = 0;
561 ListCell *key1,
562 *key2;
563
564 /*
565 * See if we can avoiding looping through both lists. This optimization
566 * gains us several percent in planning time in a worst-case test.
567 */
568 if (keys1 == keys2)
569 {
570 *n_common = list_length(keys1);
571 return true;
572 }
573 else if (keys1 == NIL)
574 {
575 *n_common = 0;
576 return true;
577 }
578 else if (keys2 == NIL)
579 {
580 *n_common = 0;
581 return false;
582 }
583
584 /*
585 * If both lists are non-empty, iterate through both to find out how many
586 * items are shared.
587 */
588 forboth(key1, keys1, key2, keys2)
589 {
590 PathKey *pathkey1 = (PathKey *) lfirst(key1);
591 PathKey *pathkey2 = (PathKey *) lfirst(key2);
592
593 if (pathkey1 != pathkey2)
594 {
595 *n_common = n;
596 return false;
597 }
598 n++;
599 }
600
601 /* If we ended with a null value, then we've processed the whole list. */
602 *n_common = n;
603 return (key1 == NULL);
604}
605
606/*
607 * get_cheapest_path_for_pathkeys
608 * Find the cheapest path (according to the specified criterion) that
609 * satisfies the given pathkeys and parameterization, and is parallel-safe
610 * if required.
611 * Return NULL if no such path.
612 *
613 * 'paths' is a list of possible paths that all generate the same relation
614 * 'pathkeys' represents a required ordering (in canonical form!)
615 * 'required_outer' denotes allowable outer relations for parameterized paths
616 * 'cost_criterion' is STARTUP_COST or TOTAL_COST
617 * 'require_parallel_safe' causes us to consider only parallel-safe paths
618 */
619Path *
621 Relids required_outer,
622 CostSelector cost_criterion,
623 bool require_parallel_safe)
624{
625 Path *matched_path = NULL;
626 ListCell *l;
627
628 foreach(l, paths)
629 {
630 Path *path = (Path *) lfirst(l);
631
632 /* If required, reject paths that are not parallel-safe */
633 if (require_parallel_safe && !path->parallel_safe)
634 continue;
635
636 /*
637 * Since cost comparison is a lot cheaper than pathkey comparison, do
638 * that first. (XXX is that still true?)
639 */
640 if (matched_path != NULL &&
641 compare_path_costs(matched_path, path, cost_criterion) <= 0)
642 continue;
643
644 if (pathkeys_contained_in(pathkeys, path->pathkeys) &&
645 bms_is_subset(PATH_REQ_OUTER(path), required_outer))
646 matched_path = path;
647 }
648 return matched_path;
649}
650
651/*
652 * get_cheapest_fractional_path_for_pathkeys
653 * Find the cheapest path (for retrieving a specified fraction of all
654 * the tuples) that satisfies the given pathkeys and parameterization.
655 * Return NULL if no such path.
656 *
657 * See compare_fractional_path_costs() for the interpretation of the fraction
658 * parameter.
659 *
660 * 'paths' is a list of possible paths that all generate the same relation
661 * 'pathkeys' represents a required ordering (in canonical form!)
662 * 'required_outer' denotes allowable outer relations for parameterized paths
663 * 'fraction' is the fraction of the total tuples expected to be retrieved
664 */
665Path *
667 List *pathkeys,
668 Relids required_outer,
669 double fraction)
670{
671 Path *matched_path = NULL;
672 ListCell *l;
673
674 foreach(l, paths)
675 {
676 Path *path = (Path *) lfirst(l);
677
678 /*
679 * Since cost comparison is a lot cheaper than pathkey comparison, do
680 * that first. (XXX is that still true?)
681 */
682 if (matched_path != NULL &&
683 compare_fractional_path_costs(matched_path, path, fraction) <= 0)
684 continue;
685
686 if (pathkeys_contained_in(pathkeys, path->pathkeys) &&
687 bms_is_subset(PATH_REQ_OUTER(path), required_outer))
688 matched_path = path;
689 }
690 return matched_path;
691}
692
693
694/*
695 * get_cheapest_parallel_safe_total_inner
696 * Find the unparameterized parallel-safe path with the least total cost.
697 */
698Path *
700{
701 ListCell *l;
702
703 foreach(l, paths)
704 {
705 Path *innerpath = (Path *) lfirst(l);
706
707 if (innerpath->parallel_safe &&
708 bms_is_empty(PATH_REQ_OUTER(innerpath)))
709 return innerpath;
710 }
711
712 return NULL;
713}
714
715/****************************************************************************
716 * NEW PATHKEY FORMATION
717 ****************************************************************************/
718
719/*
720 * build_index_pathkeys
721 * Build a pathkeys list that describes the ordering induced by an index
722 * scan using the given index. (Note that an unordered index doesn't
723 * induce any ordering, so we return NIL.)
724 *
725 * If 'scandir' is BackwardScanDirection, build pathkeys representing a
726 * backwards scan of the index.
727 *
728 * We iterate only key columns of covering indexes, since non-key columns
729 * don't influence index ordering. The result is canonical, meaning that
730 * redundant pathkeys are removed; it may therefore have fewer entries than
731 * there are key columns in the index.
732 *
733 * Another reason for stopping early is that we may be able to tell that
734 * an index column's sort order is uninteresting for this query. However,
735 * that test is just based on the existence of an EquivalenceClass and not
736 * on position in pathkey lists, so it's not complete. Caller should call
737 * truncate_useless_pathkeys() to possibly remove more pathkeys.
738 */
739List *
742 ScanDirection scandir)
743{
744 List *retval = NIL;
745 ListCell *lc;
746 int i;
747
748 if (index->sortopfamily == NULL)
749 return NIL; /* non-orderable index */
750
751 i = 0;
752 foreach(lc, index->indextlist)
753 {
754 TargetEntry *indextle = (TargetEntry *) lfirst(lc);
755 Expr *indexkey;
756 bool reverse_sort;
757 bool nulls_first;
758 PathKey *cpathkey;
759
760 /*
761 * INCLUDE columns are stored in index unordered, so they don't
762 * support ordered index scan.
763 */
764 if (i >= index->nkeycolumns)
765 break;
766
767 /* We assume we don't need to make a copy of the tlist item */
768 indexkey = indextle->expr;
769
770 if (ScanDirectionIsBackward(scandir))
771 {
772 reverse_sort = !index->reverse_sort[i];
773 nulls_first = !index->nulls_first[i];
774 }
775 else
776 {
777 reverse_sort = index->reverse_sort[i];
778 nulls_first = index->nulls_first[i];
779 }
780
781 /*
782 * OK, try to make a canonical pathkey for this sort key.
783 */
785 indexkey,
786 index->sortopfamily[i],
787 index->opcintype[i],
788 index->indexcollations[i],
789 reverse_sort,
790 nulls_first,
791 0,
792 index->rel->relids,
793 false);
794
795 if (cpathkey)
796 {
797 /*
798 * We found the sort key in an EquivalenceClass, so it's relevant
799 * for this query. Add it to list, unless it's redundant.
800 */
801 if (!pathkey_is_redundant(cpathkey, retval))
802 retval = lappend(retval, cpathkey);
803 }
804 else
805 {
806 /*
807 * Boolean index keys might be redundant even if they do not
808 * appear in an EquivalenceClass, because of our special treatment
809 * of boolean equality conditions --- see the comment for
810 * indexcol_is_bool_constant_for_query(). If that applies, we can
811 * continue to examine lower-order index columns. Otherwise, the
812 * sort key is not an interesting sort order for this query, so we
813 * should stop considering index columns; any lower-order sort
814 * keys won't be useful either.
815 */
817 break;
818 }
819
820 i++;
821 }
822
823 return retval;
824}
825
826/*
827 * partkey_is_bool_constant_for_query
828 *
829 * If a partition key column is constrained to have a constant value by the
830 * query's WHERE conditions, then it's irrelevant for sort-order
831 * considerations. Usually that means we have a restriction clause
832 * WHERE partkeycol = constant, which gets turned into an EquivalenceClass
833 * containing a constant, which is recognized as redundant by
834 * build_partition_pathkeys(). But if the partition key column is a
835 * boolean variable (or expression), then we are not going to see such a
836 * WHERE clause, because expression preprocessing will have simplified it
837 * to "WHERE partkeycol" or "WHERE NOT partkeycol". So we are not going
838 * to have a matching EquivalenceClass (unless the query also contains
839 * "ORDER BY partkeycol"). To allow such cases to work the same as they would
840 * for non-boolean values, this function is provided to detect whether the
841 * specified partition key column matches a boolean restriction clause.
842 */
843static bool
845{
846 PartitionScheme partscheme = partrel->part_scheme;
847 ListCell *lc;
848
849 /*
850 * If the partkey isn't boolean, we can't possibly get a match.
851 *
852 * Partitioning currently can only use built-in AMs, so checking for
853 * built-in boolean opfamilies is good enough.
854 */
855 if (!IsBuiltinBooleanOpfamily(partscheme->partopfamily[partkeycol]))
856 return false;
857
858 /* Check each restriction clause for the partitioned rel */
859 foreach(lc, partrel->baserestrictinfo)
860 {
861 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
862
863 /* Ignore pseudoconstant quals, they won't match */
864 if (rinfo->pseudoconstant)
865 continue;
866
867 /* See if we can match the clause's expression to the partkey column */
868 if (matches_boolean_partition_clause(rinfo, partrel, partkeycol))
869 return true;
870 }
871
872 return false;
873}
874
875/*
876 * matches_boolean_partition_clause
877 * Determine if the boolean clause described by rinfo matches
878 * partrel's partkeycol-th partition key column.
879 *
880 * "Matches" can be either an exact match (equivalent to partkey = true),
881 * or a NOT above an exact match (equivalent to partkey = false).
882 */
883static bool
885 RelOptInfo *partrel, int partkeycol)
886{
887 Node *clause = (Node *) rinfo->clause;
888 Node *partexpr = (Node *) linitial(partrel->partexprs[partkeycol]);
889
890 /* Direct match? */
891 if (equal(partexpr, clause))
892 return true;
893 /* NOT clause? */
894 else if (is_notclause(clause))
895 {
896 Node *arg = (Node *) get_notclausearg((Expr *) clause);
897
898 if (equal(partexpr, arg))
899 return true;
900 }
901
902 return false;
903}
904
905/*
906 * build_partition_pathkeys
907 * Build a pathkeys list that describes the ordering induced by the
908 * partitions of partrel, under either forward or backward scan
909 * as per scandir.
910 *
911 * Caller must have checked that the partitions are properly ordered,
912 * as detected by partitions_are_ordered().
913 *
914 * Sets *partialkeys to true if pathkeys were only built for a prefix of the
915 * partition key, or false if the pathkeys include all columns of the
916 * partition key.
917 */
918List *
920 ScanDirection scandir, bool *partialkeys)
921{
922 List *retval = NIL;
923 PartitionScheme partscheme = partrel->part_scheme;
924 int i;
925
926 Assert(partscheme != NULL);
927 Assert(partitions_are_ordered(partrel->boundinfo, partrel->live_parts));
928 /* For now, we can only cope with baserels */
929 Assert(IS_SIMPLE_REL(partrel));
930
931 for (i = 0; i < partscheme->partnatts; i++)
932 {
933 PathKey *cpathkey;
934 Expr *keyCol = (Expr *) linitial(partrel->partexprs[i]);
935
936 /*
937 * Try to make a canonical pathkey for this partkey.
938 *
939 * We assume the PartitionDesc lists any NULL partition last, so we
940 * treat the scan like a NULLS LAST index: we have nulls_first for
941 * backwards scan only.
942 */
944 keyCol,
945 partscheme->partopfamily[i],
946 partscheme->partopcintype[i],
947 partscheme->partcollation[i],
950 0,
951 partrel->relids,
952 false);
953
954
955 if (cpathkey)
956 {
957 /*
958 * We found the sort key in an EquivalenceClass, so it's relevant
959 * for this query. Add it to list, unless it's redundant.
960 */
961 if (!pathkey_is_redundant(cpathkey, retval))
962 retval = lappend(retval, cpathkey);
963 }
964 else
965 {
966 /*
967 * Boolean partition keys might be redundant even if they do not
968 * appear in an EquivalenceClass, because of our special treatment
969 * of boolean equality conditions --- see the comment for
970 * partkey_is_bool_constant_for_query(). If that applies, we can
971 * continue to examine lower-order partition keys. Otherwise, the
972 * sort key is not an interesting sort order for this query, so we
973 * should stop considering partition columns; any lower-order sort
974 * keys won't be useful either.
975 */
977 {
978 *partialkeys = true;
979 return retval;
980 }
981 }
982 }
983
984 *partialkeys = false;
985 return retval;
986}
987
988/*
989 * build_expression_pathkey
990 * Build a pathkeys list that describes an ordering by a single expression
991 * using the given sort operator.
992 *
993 * expr and rel are as for make_pathkey_from_sortinfo.
994 * We induce the other arguments assuming default sort order for the operator.
995 *
996 * Similarly to make_pathkey_from_sortinfo, the result is NIL if create_it
997 * is false and the expression isn't already in some EquivalenceClass.
998 */
999List *
1001 Expr *expr,
1002 Oid opno,
1003 Relids rel,
1004 bool create_it)
1005{
1006 List *pathkeys;
1007 Oid opfamily,
1008 opcintype;
1009 int16 strategy;
1010 PathKey *cpathkey;
1011
1012 /* Find the operator in pg_amop --- failure shouldn't happen */
1014 &opfamily, &opcintype, &strategy))
1015 elog(ERROR, "operator %u is not a valid ordering operator",
1016 opno);
1017
1019 expr,
1020 opfamily,
1021 opcintype,
1022 exprCollation((Node *) expr),
1023 (strategy == BTGreaterStrategyNumber),
1024 (strategy == BTGreaterStrategyNumber),
1025 0,
1026 rel,
1027 create_it);
1028
1029 if (cpathkey)
1030 pathkeys = list_make1(cpathkey);
1031 else
1032 pathkeys = NIL;
1033
1034 return pathkeys;
1035}
1036
1037/*
1038 * convert_subquery_pathkeys
1039 * Build a pathkeys list that describes the ordering of a subquery's
1040 * result, in the terms of the outer query. This is essentially a
1041 * task of conversion.
1042 *
1043 * 'rel': outer query's RelOptInfo for the subquery relation.
1044 * 'subquery_pathkeys': the subquery's output pathkeys, in its terms.
1045 * 'subquery_tlist': the subquery's output targetlist, in its terms.
1046 *
1047 * We intentionally don't do truncate_useless_pathkeys() here, because there
1048 * are situations where seeing the raw ordering of the subquery is helpful.
1049 * For example, if it returns ORDER BY x DESC, that may prompt us to
1050 * construct a mergejoin using DESC order rather than ASC order; but the
1051 * right_merge_direction heuristic would have us throw the knowledge away.
1052 */
1053List *
1055 List *subquery_pathkeys,
1056 List *subquery_tlist)
1057{
1058 List *retval = NIL;
1059 int retvallen = 0;
1060 int outer_query_keys = list_length(root->query_pathkeys);
1061 ListCell *i;
1062
1063 foreach(i, subquery_pathkeys)
1064 {
1065 PathKey *sub_pathkey = (PathKey *) lfirst(i);
1066 EquivalenceClass *sub_eclass = sub_pathkey->pk_eclass;
1067 PathKey *best_pathkey = NULL;
1068
1069 if (sub_eclass->ec_has_volatile)
1070 {
1071 /*
1072 * If the sub_pathkey's EquivalenceClass is volatile, then it must
1073 * have come from an ORDER BY clause, and we have to match it to
1074 * that same targetlist entry.
1075 */
1076 TargetEntry *tle;
1077 Var *outer_var;
1078
1079 if (sub_eclass->ec_sortref == 0) /* can't happen */
1080 elog(ERROR, "volatile EquivalenceClass has no sortref");
1081 tle = get_sortgroupref_tle(sub_eclass->ec_sortref, subquery_tlist);
1082 Assert(tle);
1083 /* Is TLE actually available to the outer query? */
1084 outer_var = find_var_for_subquery_tle(rel, tle);
1085 if (outer_var)
1086 {
1087 /* We can represent this sub_pathkey */
1088 EquivalenceMember *sub_member;
1089 EquivalenceClass *outer_ec;
1090
1091 Assert(list_length(sub_eclass->ec_members) == 1);
1092 sub_member = (EquivalenceMember *) linitial(sub_eclass->ec_members);
1093
1094 /*
1095 * Note: it might look funny to be setting sortref = 0 for a
1096 * reference to a volatile sub_eclass. However, the
1097 * expression is *not* volatile in the outer query: it's just
1098 * a Var referencing whatever the subquery emitted. (IOW, the
1099 * outer query isn't going to re-execute the volatile
1100 * expression itself.) So this is okay.
1101 */
1102 outer_ec =
1104 (Expr *) outer_var,
1105 sub_eclass->ec_opfamilies,
1106 sub_member->em_datatype,
1107 sub_eclass->ec_collation,
1108 0,
1109 rel->relids,
1110 false);
1111
1112 /*
1113 * If we don't find a matching EC, sub-pathkey isn't
1114 * interesting to the outer query
1115 */
1116 if (outer_ec)
1117 best_pathkey =
1119 outer_ec,
1120 sub_pathkey->pk_opfamily,
1121 sub_pathkey->pk_strategy,
1122 sub_pathkey->pk_nulls_first);
1123 }
1124 }
1125 else
1126 {
1127 /*
1128 * Otherwise, the sub_pathkey's EquivalenceClass could contain
1129 * multiple elements (representing knowledge that multiple items
1130 * are effectively equal). Each element might match none, one, or
1131 * more of the output columns that are visible to the outer query.
1132 * This means we may have multiple possible representations of the
1133 * sub_pathkey in the context of the outer query. Ideally we
1134 * would generate them all and put them all into an EC of the
1135 * outer query, thereby propagating equality knowledge up to the
1136 * outer query. Right now we cannot do so, because the outer
1137 * query's EquivalenceClasses are already frozen when this is
1138 * called. Instead we prefer the one that has the highest "score"
1139 * (number of EC peers, plus one if it matches the outer
1140 * query_pathkeys). This is the most likely to be useful in the
1141 * outer query.
1142 */
1143 int best_score = -1;
1144 ListCell *j;
1145
1146 foreach(j, sub_eclass->ec_members)
1147 {
1148 EquivalenceMember *sub_member = (EquivalenceMember *) lfirst(j);
1149 Expr *sub_expr = sub_member->em_expr;
1150 Oid sub_expr_type = sub_member->em_datatype;
1151 Oid sub_expr_coll = sub_eclass->ec_collation;
1152 ListCell *k;
1153
1154 if (sub_member->em_is_child)
1155 continue; /* ignore children here */
1156
1157 foreach(k, subquery_tlist)
1158 {
1159 TargetEntry *tle = (TargetEntry *) lfirst(k);
1160 Var *outer_var;
1161 Expr *tle_expr;
1162 EquivalenceClass *outer_ec;
1163 PathKey *outer_pk;
1164 int score;
1165
1166 /* Is TLE actually available to the outer query? */
1167 outer_var = find_var_for_subquery_tle(rel, tle);
1168 if (!outer_var)
1169 continue;
1170
1171 /*
1172 * The targetlist entry is considered to match if it
1173 * matches after sort-key canonicalization. That is
1174 * needed since the sub_expr has been through the same
1175 * process.
1176 */
1177 tle_expr = canonicalize_ec_expression(tle->expr,
1178 sub_expr_type,
1179 sub_expr_coll);
1180 if (!equal(tle_expr, sub_expr))
1181 continue;
1182
1183 /* See if we have a matching EC for the TLE */
1184 outer_ec = get_eclass_for_sort_expr(root,
1185 (Expr *) outer_var,
1186 sub_eclass->ec_opfamilies,
1187 sub_expr_type,
1188 sub_expr_coll,
1189 0,
1190 rel->relids,
1191 false);
1192
1193 /*
1194 * If we don't find a matching EC, this sub-pathkey isn't
1195 * interesting to the outer query
1196 */
1197 if (!outer_ec)
1198 continue;
1199
1200 outer_pk = make_canonical_pathkey(root,
1201 outer_ec,
1202 sub_pathkey->pk_opfamily,
1203 sub_pathkey->pk_strategy,
1204 sub_pathkey->pk_nulls_first);
1205 /* score = # of equivalence peers */
1206 score = list_length(outer_ec->ec_members) - 1;
1207 /* +1 if it matches the proper query_pathkeys item */
1208 if (retvallen < outer_query_keys &&
1209 list_nth(root->query_pathkeys, retvallen) == outer_pk)
1210 score++;
1211 if (score > best_score)
1212 {
1213 best_pathkey = outer_pk;
1214 best_score = score;
1215 }
1216 }
1217 }
1218 }
1219
1220 /*
1221 * If we couldn't find a representation of this sub_pathkey, we're
1222 * done (we can't use the ones to its right, either).
1223 */
1224 if (!best_pathkey)
1225 break;
1226
1227 /*
1228 * Eliminate redundant ordering info; could happen if outer query
1229 * equivalences subquery keys...
1230 */
1231 if (!pathkey_is_redundant(best_pathkey, retval))
1232 {
1233 retval = lappend(retval, best_pathkey);
1234 retvallen++;
1235 }
1236 }
1237
1238 return retval;
1239}
1240
1241/*
1242 * find_var_for_subquery_tle
1243 *
1244 * If the given subquery tlist entry is due to be emitted by the subquery's
1245 * scan node, return a Var for it, else return NULL.
1246 *
1247 * We need this to ensure that we don't return pathkeys describing values
1248 * that are unavailable above the level of the subquery scan.
1249 */
1250static Var *
1252{
1253 ListCell *lc;
1254
1255 /* If the TLE is resjunk, it's certainly not visible to the outer query */
1256 if (tle->resjunk)
1257 return NULL;
1258
1259 /* Search the rel's targetlist to see what it will return */
1260 foreach(lc, rel->reltarget->exprs)
1261 {
1262 Var *var = (Var *) lfirst(lc);
1263
1264 /* Ignore placeholders */
1265 if (!IsA(var, Var))
1266 continue;
1267 Assert(var->varno == rel->relid);
1268
1269 /* If we find a Var referencing this TLE, we're good */
1270 if (var->varattno == tle->resno)
1271 return copyObject(var); /* Make a copy for safety */
1272 }
1273 return NULL;
1274}
1275
1276/*
1277 * build_join_pathkeys
1278 * Build the path keys for a join relation constructed by mergejoin or
1279 * nestloop join. This is normally the same as the outer path's keys.
1280 *
1281 * EXCEPTION: in a FULL, RIGHT or RIGHT_ANTI join, we cannot treat the
1282 * result as having the outer path's path keys, because null lefthand rows
1283 * may be inserted at random points. It must be treated as unsorted.
1284 *
1285 * We truncate away any pathkeys that are uninteresting for higher joins.
1286 *
1287 * 'joinrel' is the join relation that paths are being formed for
1288 * 'jointype' is the join type (inner, left, full, etc)
1289 * 'outer_pathkeys' is the list of the current outer path's path keys
1290 *
1291 * Returns the list of new path keys.
1292 */
1293List *
1295 RelOptInfo *joinrel,
1296 JoinType jointype,
1297 List *outer_pathkeys)
1298{
1299 /* RIGHT_SEMI should not come here */
1300 Assert(jointype != JOIN_RIGHT_SEMI);
1301
1302 if (jointype == JOIN_FULL ||
1303 jointype == JOIN_RIGHT ||
1304 jointype == JOIN_RIGHT_ANTI)
1305 return NIL;
1306
1307 /*
1308 * This used to be quite a complex bit of code, but now that all pathkey
1309 * sublists start out life canonicalized, we don't have to do a darn thing
1310 * here!
1311 *
1312 * We do, however, need to truncate the pathkeys list, since it may
1313 * contain pathkeys that were useful for forming this joinrel but are
1314 * uninteresting to higher levels.
1315 */
1316 return truncate_useless_pathkeys(root, joinrel, outer_pathkeys);
1317}
1318
1319/****************************************************************************
1320 * PATHKEYS AND SORT CLAUSES
1321 ****************************************************************************/
1322
1323/*
1324 * make_pathkeys_for_sortclauses
1325 * Generate a pathkeys list that represents the sort order specified
1326 * by a list of SortGroupClauses
1327 *
1328 * The resulting PathKeys are always in canonical form. (Actually, there
1329 * is no longer any code anywhere that creates non-canonical PathKeys.)
1330 *
1331 * 'sortclauses' is a list of SortGroupClause nodes
1332 * 'tlist' is the targetlist to find the referenced tlist entries in
1333 */
1334List *
1336 List *sortclauses,
1337 List *tlist)
1338{
1339 List *result;
1340 bool sortable;
1341
1343 &sortclauses,
1344 tlist,
1345 false,
1346 false,
1347 &sortable,
1348 false);
1349 /* It's caller error if not all clauses were sortable */
1350 Assert(sortable);
1351 return result;
1352}
1353
1354/*
1355 * make_pathkeys_for_sortclauses_extended
1356 * Generate a pathkeys list that represents the sort order specified
1357 * by a list of SortGroupClauses
1358 *
1359 * The comments for make_pathkeys_for_sortclauses apply here too. In addition:
1360 *
1361 * If remove_redundant is true, then any sort clauses that are found to
1362 * give rise to redundant pathkeys are removed from the sortclauses list
1363 * (which therefore must be pass-by-reference in this version).
1364 *
1365 * If remove_group_rtindex is true, then we need to remove the RT index of the
1366 * grouping step from the sort expressions before we make PathKeys for them.
1367 *
1368 * *sortable is set to true if all the sort clauses are in fact sortable.
1369 * If any are not, they are ignored except for setting *sortable false.
1370 * (In that case, the output pathkey list isn't really useful. However,
1371 * we process the whole sortclauses list anyway, because it's still valid
1372 * to remove any clauses that can be proven redundant via the eclass logic.
1373 * Even though we'll have to hash in that case, we might as well not hash
1374 * redundant columns.)
1375 *
1376 * If set_ec_sortref is true then sets the value of the pathkey's
1377 * EquivalenceClass unless it's already initialized.
1378 */
1379List *
1381 List **sortclauses,
1382 List *tlist,
1383 bool remove_redundant,
1384 bool remove_group_rtindex,
1385 bool *sortable,
1386 bool set_ec_sortref)
1387{
1388 List *pathkeys = NIL;
1389 ListCell *l;
1390
1391 *sortable = true;
1392 foreach(l, *sortclauses)
1393 {
1394 SortGroupClause *sortcl = (SortGroupClause *) lfirst(l);
1395 Expr *sortkey;
1396 PathKey *pathkey;
1397
1398 sortkey = (Expr *) get_sortgroupclause_expr(sortcl, tlist);
1399 if (!OidIsValid(sortcl->sortop))
1400 {
1401 *sortable = false;
1402 continue;
1403 }
1404 if (remove_group_rtindex)
1405 {
1406 Assert(root->group_rtindex > 0);
1407 sortkey = (Expr *)
1408 remove_nulling_relids((Node *) sortkey,
1409 bms_make_singleton(root->group_rtindex),
1410 NULL);
1411 }
1413 sortkey,
1414 sortcl->sortop,
1415 sortcl->reverse_sort,
1416 sortcl->nulls_first,
1417 sortcl->tleSortGroupRef,
1418 true);
1419 if (pathkey->pk_eclass->ec_sortref == 0 && set_ec_sortref)
1420 {
1421 /*
1422 * Copy the sortref if it hasn't been set yet. That may happen if
1423 * the EquivalenceClass was constructed from a WHERE clause, i.e.
1424 * it doesn't have a target reference at all.
1425 */
1426 pathkey->pk_eclass->ec_sortref = sortcl->tleSortGroupRef;
1427 }
1428
1429 /* Canonical form eliminates redundant ordering keys */
1430 if (!pathkey_is_redundant(pathkey, pathkeys))
1431 pathkeys = lappend(pathkeys, pathkey);
1432 else if (remove_redundant)
1433 *sortclauses = foreach_delete_current(*sortclauses, l);
1434 }
1435 return pathkeys;
1436}
1437
1438/****************************************************************************
1439 * PATHKEYS AND MERGECLAUSES
1440 ****************************************************************************/
1441
1442/*
1443 * initialize_mergeclause_eclasses
1444 * Set the EquivalenceClass links in a mergeclause restrictinfo.
1445 *
1446 * RestrictInfo contains fields in which we may cache pointers to
1447 * EquivalenceClasses for the left and right inputs of the mergeclause.
1448 * (If the mergeclause is a true equivalence clause these will be the
1449 * same EquivalenceClass, otherwise not.) If the mergeclause is either
1450 * used to generate an EquivalenceClass, or derived from an EquivalenceClass,
1451 * then it's easy to set up the left_ec and right_ec members --- otherwise,
1452 * this function should be called to set them up. We will generate new
1453 * EquivalenceClauses if necessary to represent the mergeclause's left and
1454 * right sides.
1455 *
1456 * Note this is called before EC merging is complete, so the links won't
1457 * necessarily point to canonical ECs. Before they are actually used for
1458 * anything, update_mergeclause_eclasses must be called to ensure that
1459 * they've been updated to point to canonical ECs.
1460 */
1461void
1463{
1464 Expr *clause = restrictinfo->clause;
1465 Oid lefttype,
1466 righttype;
1467
1468 /* Should be a mergeclause ... */
1469 Assert(restrictinfo->mergeopfamilies != NIL);
1470 /* ... with links not yet set */
1471 Assert(restrictinfo->left_ec == NULL);
1472 Assert(restrictinfo->right_ec == NULL);
1473
1474 /* Need the declared input types of the operator */
1475 op_input_types(((OpExpr *) clause)->opno, &lefttype, &righttype);
1476
1477 /* Find or create a matching EquivalenceClass for each side */
1478 restrictinfo->left_ec =
1480 (Expr *) get_leftop(clause),
1481 restrictinfo->mergeopfamilies,
1482 lefttype,
1483 ((OpExpr *) clause)->inputcollid,
1484 0,
1485 NULL,
1486 true);
1487 restrictinfo->right_ec =
1489 (Expr *) get_rightop(clause),
1490 restrictinfo->mergeopfamilies,
1491 righttype,
1492 ((OpExpr *) clause)->inputcollid,
1493 0,
1494 NULL,
1495 true);
1496}
1497
1498/*
1499 * update_mergeclause_eclasses
1500 * Make the cached EquivalenceClass links valid in a mergeclause
1501 * restrictinfo.
1502 *
1503 * These pointers should have been set by process_equivalence or
1504 * initialize_mergeclause_eclasses, but they might have been set to
1505 * non-canonical ECs that got merged later. Chase up to the canonical
1506 * merged parent if so.
1507 */
1508void
1510{
1511 /* Should be a merge clause ... */
1512 Assert(restrictinfo->mergeopfamilies != NIL);
1513 /* ... with pointers already set */
1514 Assert(restrictinfo->left_ec != NULL);
1515 Assert(restrictinfo->right_ec != NULL);
1516
1517 /* Chase up to the top as needed */
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;
1522}
1523
1524/*
1525 * find_mergeclauses_for_outer_pathkeys
1526 * This routine attempts to find a list of mergeclauses that can be
1527 * used with a specified ordering for the join's outer relation.
1528 * If successful, it returns a list of mergeclauses.
1529 *
1530 * 'pathkeys' is a pathkeys list showing the ordering of an outer-rel path.
1531 * 'restrictinfos' is a list of mergejoinable restriction clauses for the
1532 * join relation being formed, in no particular order.
1533 *
1534 * The restrictinfos must be marked (via outer_is_left) to show which side
1535 * of each clause is associated with the current outer path. (See
1536 * select_mergejoin_clauses())
1537 *
1538 * The result is NIL if no merge can be done, else a maximal list of
1539 * usable mergeclauses (represented as a list of their restrictinfo nodes).
1540 * The list is ordered to match the pathkeys, as required for execution.
1541 */
1542List *
1544 List *pathkeys,
1545 List *restrictinfos)
1546{
1547 List *mergeclauses = NIL;
1548 ListCell *i;
1549
1550 /* make sure we have eclasses cached in the clauses */
1551 foreach(i, restrictinfos)
1552 {
1553 RestrictInfo *rinfo = (RestrictInfo *) lfirst(i);
1554
1556 }
1557
1558 foreach(i, pathkeys)
1559 {
1560 PathKey *pathkey = (PathKey *) lfirst(i);
1561 EquivalenceClass *pathkey_ec = pathkey->pk_eclass;
1562 List *matched_restrictinfos = NIL;
1563 ListCell *j;
1564
1565 /*----------
1566 * A mergejoin clause matches a pathkey if it has the same EC.
1567 * If there are multiple matching clauses, take them all. In plain
1568 * inner-join scenarios we expect only one match, because
1569 * equivalence-class processing will have removed any redundant
1570 * mergeclauses. However, in outer-join scenarios there might be
1571 * multiple matches. An example is
1572 *
1573 * select * from a full join b
1574 * on a.v1 = b.v1 and a.v2 = b.v2 and a.v1 = b.v2;
1575 *
1576 * Given the pathkeys ({a.v1}, {a.v2}) it is okay to return all three
1577 * clauses (in the order a.v1=b.v1, a.v1=b.v2, a.v2=b.v2) and indeed
1578 * we *must* do so or we will be unable to form a valid plan.
1579 *
1580 * We expect that the given pathkeys list is canonical, which means
1581 * no two members have the same EC, so it's not possible for this
1582 * code to enter the same mergeclause into the result list twice.
1583 *
1584 * It's possible that multiple matching clauses might have different
1585 * ECs on the other side, in which case the order we put them into our
1586 * result makes a difference in the pathkeys required for the inner
1587 * input rel. However this routine hasn't got any info about which
1588 * order would be best, so we don't worry about that.
1589 *
1590 * It's also possible that the selected mergejoin clauses produce
1591 * a noncanonical ordering of pathkeys for the inner side, ie, we
1592 * might select clauses that reference b.v1, b.v2, b.v1 in that
1593 * order. This is not harmful in itself, though it suggests that
1594 * the clauses are partially redundant. Since the alternative is
1595 * to omit mergejoin clauses and thereby possibly fail to generate a
1596 * plan altogether, we live with it. make_inner_pathkeys_for_merge()
1597 * has to delete duplicates when it constructs the inner pathkeys
1598 * list, and we also have to deal with such cases specially in
1599 * create_mergejoin_plan().
1600 *----------
1601 */
1602 foreach(j, restrictinfos)
1603 {
1604 RestrictInfo *rinfo = (RestrictInfo *) lfirst(j);
1605 EquivalenceClass *clause_ec;
1606
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);
1611 }
1612
1613 /*
1614 * If we didn't find a mergeclause, we're done --- any additional
1615 * sort-key positions in the pathkeys are useless. (But we can still
1616 * mergejoin if we found at least one mergeclause.)
1617 */
1618 if (matched_restrictinfos == NIL)
1619 break;
1620
1621 /*
1622 * If we did find usable mergeclause(s) for this sort-key position,
1623 * add them to result list.
1624 */
1625 mergeclauses = list_concat(mergeclauses, matched_restrictinfos);
1626 }
1627
1628 return mergeclauses;
1629}
1630
1631/*
1632 * select_outer_pathkeys_for_merge
1633 * Builds a pathkey list representing a possible sort ordering
1634 * that can be used with the given mergeclauses.
1635 *
1636 * 'mergeclauses' is a list of RestrictInfos for mergejoin clauses
1637 * that will be used in a merge join.
1638 * 'joinrel' is the join relation we are trying to construct.
1639 *
1640 * The restrictinfos must be marked (via outer_is_left) to show which side
1641 * of each clause is associated with the current outer path. (See
1642 * select_mergejoin_clauses())
1643 *
1644 * Returns a pathkeys list that can be applied to the outer relation.
1645 *
1646 * Since we assume here that a sort is required, there is no particular use
1647 * in matching any available ordering of the outerrel. (joinpath.c has an
1648 * entirely separate code path for considering sort-free mergejoins.) Rather,
1649 * it's interesting to try to match, or match a prefix of the requested
1650 * query_pathkeys so that a second output sort may be avoided or an
1651 * incremental sort may be done instead. We can get away with just a prefix
1652 * of the query_pathkeys when that prefix covers the entire join condition.
1653 * Failing that, we try to list "more popular" keys (those with the most
1654 * unmatched EquivalenceClass peers) earlier, in hopes of making the resulting
1655 * ordering useful for as many higher-level mergejoins as possible.
1656 */
1657List *
1659 List *mergeclauses,
1660 RelOptInfo *joinrel)
1661{
1662 List *pathkeys = NIL;
1663 int nClauses = list_length(mergeclauses);
1664 EquivalenceClass **ecs;
1665 int *scores;
1666 int necs;
1667 ListCell *lc;
1668 int j;
1669
1670 /* Might have no mergeclauses */
1671 if (nClauses == 0)
1672 return NIL;
1673
1674 /*
1675 * Make arrays of the ECs used by the mergeclauses (dropping any
1676 * duplicates) and their "popularity" scores.
1677 */
1678 ecs = (EquivalenceClass **) palloc(nClauses * sizeof(EquivalenceClass *));
1679 scores = (int *) palloc(nClauses * sizeof(int));
1680 necs = 0;
1681
1682 foreach(lc, mergeclauses)
1683 {
1684 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1685 EquivalenceClass *oeclass;
1686 int score;
1687 ListCell *lc2;
1688
1689 /* get the outer eclass */
1691
1692 if (rinfo->outer_is_left)
1693 oeclass = rinfo->left_ec;
1694 else
1695 oeclass = rinfo->right_ec;
1696
1697 /* reject duplicates */
1698 for (j = 0; j < necs; j++)
1699 {
1700 if (ecs[j] == oeclass)
1701 break;
1702 }
1703 if (j < necs)
1704 continue;
1705
1706 /* compute score */
1707 score = 0;
1708 foreach(lc2, oeclass->ec_members)
1709 {
1711
1712 /* Potential future join partner? */
1713 if (!em->em_is_const && !em->em_is_child &&
1714 !bms_overlap(em->em_relids, joinrel->relids))
1715 score++;
1716 }
1717
1718 ecs[necs] = oeclass;
1719 scores[necs] = score;
1720 necs++;
1721 }
1722
1723 /*
1724 * Find out if we have all the ECs mentioned in query_pathkeys; if so we
1725 * can generate a sort order that's also useful for final output. If we
1726 * only have a prefix of the query_pathkeys, and that prefix is the entire
1727 * join condition, then it's useful to use the prefix as the pathkeys as
1728 * this increases the chances that an incremental sort will be able to be
1729 * used by the upper planner.
1730 */
1731 if (root->query_pathkeys)
1732 {
1733 int matches = 0;
1734
1735 foreach(lc, root->query_pathkeys)
1736 {
1737 PathKey *query_pathkey = (PathKey *) lfirst(lc);
1738 EquivalenceClass *query_ec = query_pathkey->pk_eclass;
1739
1740 for (j = 0; j < necs; j++)
1741 {
1742 if (ecs[j] == query_ec)
1743 break; /* found match */
1744 }
1745 if (j >= necs)
1746 break; /* didn't find match */
1747
1748 matches++;
1749 }
1750 /* if we got to the end of the list, we have them all */
1751 if (lc == NULL)
1752 {
1753 /* copy query_pathkeys as starting point for our output */
1754 pathkeys = list_copy(root->query_pathkeys);
1755 /* mark their ECs as already-emitted */
1756 foreach(lc, root->query_pathkeys)
1757 {
1758 PathKey *query_pathkey = (PathKey *) lfirst(lc);
1759 EquivalenceClass *query_ec = query_pathkey->pk_eclass;
1760
1761 for (j = 0; j < necs; j++)
1762 {
1763 if (ecs[j] == query_ec)
1764 {
1765 scores[j] = -1;
1766 break;
1767 }
1768 }
1769 }
1770 }
1771
1772 /*
1773 * If we didn't match to all of the query_pathkeys, but did match to
1774 * all of the join clauses then we'll make use of these as partially
1775 * sorted input is better than nothing for the upper planner as it may
1776 * lead to incremental sorts instead of full sorts.
1777 */
1778 else if (matches == nClauses)
1779 {
1780 pathkeys = list_copy_head(root->query_pathkeys, matches);
1781
1782 /* we have all of the join pathkeys, so nothing more to do */
1783 pfree(ecs);
1784 pfree(scores);
1785
1786 return pathkeys;
1787 }
1788 }
1789
1790 /*
1791 * Add remaining ECs to the list in popularity order, using a default sort
1792 * ordering. (We could use qsort() here, but the list length is usually
1793 * so small it's not worth it.)
1794 */
1795 for (;;)
1796 {
1797 int best_j;
1798 int best_score;
1799 EquivalenceClass *ec;
1800 PathKey *pathkey;
1801
1802 best_j = 0;
1803 best_score = scores[0];
1804 for (j = 1; j < necs; j++)
1805 {
1806 if (scores[j] > best_score)
1807 {
1808 best_j = j;
1809 best_score = scores[j];
1810 }
1811 }
1812 if (best_score < 0)
1813 break; /* all done */
1814 ec = ecs[best_j];
1815 scores[best_j] = -1;
1816 pathkey = make_canonical_pathkey(root,
1817 ec,
1820 false);
1821 /* can't be redundant because no duplicate ECs */
1822 Assert(!pathkey_is_redundant(pathkey, pathkeys));
1823 pathkeys = lappend(pathkeys, pathkey);
1824 }
1825
1826 pfree(ecs);
1827 pfree(scores);
1828
1829 return pathkeys;
1830}
1831
1832/*
1833 * make_inner_pathkeys_for_merge
1834 * Builds a pathkey list representing the explicit sort order that
1835 * must be applied to an inner path to make it usable with the
1836 * given mergeclauses.
1837 *
1838 * 'mergeclauses' is a list of RestrictInfos for the mergejoin clauses
1839 * that will be used in a merge join, in order.
1840 * 'outer_pathkeys' are the already-known canonical pathkeys for the outer
1841 * side of the join.
1842 *
1843 * The restrictinfos must be marked (via outer_is_left) to show which side
1844 * of each clause is associated with the current outer path. (See
1845 * select_mergejoin_clauses())
1846 *
1847 * Returns a pathkeys list that can be applied to the inner relation.
1848 *
1849 * Note that it is not this routine's job to decide whether sorting is
1850 * actually needed for a particular input path. Assume a sort is necessary;
1851 * just make the keys, eh?
1852 */
1853List *
1855 List *mergeclauses,
1856 List *outer_pathkeys)
1857{
1858 List *pathkeys = NIL;
1859 EquivalenceClass *lastoeclass;
1860 PathKey *opathkey;
1861 ListCell *lc;
1862 ListCell *lop;
1863
1864 lastoeclass = NULL;
1865 opathkey = NULL;
1866 lop = list_head(outer_pathkeys);
1867
1868 foreach(lc, mergeclauses)
1869 {
1870 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1871 EquivalenceClass *oeclass;
1872 EquivalenceClass *ieclass;
1873 PathKey *pathkey;
1874
1876
1877 if (rinfo->outer_is_left)
1878 {
1879 oeclass = rinfo->left_ec;
1880 ieclass = rinfo->right_ec;
1881 }
1882 else
1883 {
1884 oeclass = rinfo->right_ec;
1885 ieclass = rinfo->left_ec;
1886 }
1887
1888 /* outer eclass should match current or next pathkeys */
1889 /* we check this carefully for debugging reasons */
1890 if (oeclass != lastoeclass)
1891 {
1892 if (!lop)
1893 elog(ERROR, "too few pathkeys for mergeclauses");
1894 opathkey = (PathKey *) lfirst(lop);
1895 lop = lnext(outer_pathkeys, lop);
1896 lastoeclass = opathkey->pk_eclass;
1897 if (oeclass != lastoeclass)
1898 elog(ERROR, "outer pathkeys do not match mergeclause");
1899 }
1900
1901 /*
1902 * Often, we'll have same EC on both sides, in which case the outer
1903 * pathkey is also canonical for the inner side, and we can skip a
1904 * useless search.
1905 */
1906 if (ieclass == oeclass)
1907 pathkey = opathkey;
1908 else
1909 pathkey = make_canonical_pathkey(root,
1910 ieclass,
1911 opathkey->pk_opfamily,
1912 opathkey->pk_strategy,
1913 opathkey->pk_nulls_first);
1914
1915 /*
1916 * Don't generate redundant pathkeys (which can happen if multiple
1917 * mergeclauses refer to the same EC). Because we do this, the output
1918 * pathkey list isn't necessarily ordered like the mergeclauses, which
1919 * complicates life for create_mergejoin_plan(). But if we didn't,
1920 * we'd have a noncanonical sort key list, which would be bad; for one
1921 * reason, it certainly wouldn't match any available sort order for
1922 * the input relation.
1923 */
1924 if (!pathkey_is_redundant(pathkey, pathkeys))
1925 pathkeys = lappend(pathkeys, pathkey);
1926 }
1927
1928 return pathkeys;
1929}
1930
1931/*
1932 * trim_mergeclauses_for_inner_pathkeys
1933 * This routine trims a list of mergeclauses to include just those that
1934 * work with a specified ordering for the join's inner relation.
1935 *
1936 * 'mergeclauses' is a list of RestrictInfos for mergejoin clauses for the
1937 * join relation being formed, in an order known to work for the
1938 * currently-considered sort ordering of the join's outer rel.
1939 * 'pathkeys' is a pathkeys list showing the ordering of an inner-rel path;
1940 * it should be equal to, or a truncation of, the result of
1941 * make_inner_pathkeys_for_merge for these mergeclauses.
1942 *
1943 * What we return will be a prefix of the given mergeclauses list.
1944 *
1945 * We need this logic because make_inner_pathkeys_for_merge's result isn't
1946 * necessarily in the same order as the mergeclauses. That means that if we
1947 * consider an inner-rel pathkey list that is a truncation of that result,
1948 * we might need to drop mergeclauses even though they match a surviving inner
1949 * pathkey. This happens when they are to the right of a mergeclause that
1950 * matches a removed inner pathkey.
1951 *
1952 * The mergeclauses must be marked (via outer_is_left) to show which side
1953 * of each clause is associated with the current outer path. (See
1954 * select_mergejoin_clauses())
1955 */
1956List *
1958 List *mergeclauses,
1959 List *pathkeys)
1960{
1961 List *new_mergeclauses = NIL;
1962 PathKey *pathkey;
1963 EquivalenceClass *pathkey_ec;
1964 bool matched_pathkey;
1965 ListCell *lip;
1966 ListCell *i;
1967
1968 /* No pathkeys => no mergeclauses (though we don't expect this case) */
1969 if (pathkeys == NIL)
1970 return NIL;
1971 /* Initialize to consider first pathkey */
1972 lip = list_head(pathkeys);
1973 pathkey = (PathKey *) lfirst(lip);
1974 pathkey_ec = pathkey->pk_eclass;
1975 lip = lnext(pathkeys, lip);
1976 matched_pathkey = false;
1977
1978 /* Scan mergeclauses to see how many we can use */
1979 foreach(i, mergeclauses)
1980 {
1981 RestrictInfo *rinfo = (RestrictInfo *) lfirst(i);
1982 EquivalenceClass *clause_ec;
1983
1984 /* Assume we needn't do update_mergeclause_eclasses again here */
1985
1986 /* Check clause's inner-rel EC against current pathkey */
1987 clause_ec = rinfo->outer_is_left ?
1988 rinfo->right_ec : rinfo->left_ec;
1989
1990 /* If we don't have a match, attempt to advance to next pathkey */
1991 if (clause_ec != pathkey_ec)
1992 {
1993 /* If we had no clauses matching this inner pathkey, must stop */
1994 if (!matched_pathkey)
1995 break;
1996
1997 /* Advance to next inner pathkey, if any */
1998 if (lip == NULL)
1999 break;
2000 pathkey = (PathKey *) lfirst(lip);
2001 pathkey_ec = pathkey->pk_eclass;
2002 lip = lnext(pathkeys, lip);
2003 matched_pathkey = false;
2004 }
2005
2006 /* If mergeclause matches current inner pathkey, we can use it */
2007 if (clause_ec == pathkey_ec)
2008 {
2009 new_mergeclauses = lappend(new_mergeclauses, rinfo);
2010 matched_pathkey = true;
2011 }
2012 else
2013 {
2014 /* Else, no hope of adding any more mergeclauses */
2015 break;
2016 }
2017 }
2018
2019 return new_mergeclauses;
2020}
2021
2022
2023/****************************************************************************
2024 * PATHKEY USEFULNESS CHECKS
2025 *
2026 * We only want to remember as many of the pathkeys of a path as have some
2027 * potential use, either for subsequent mergejoins or for meeting the query's
2028 * requested output ordering. This ensures that add_path() won't consider
2029 * a path to have a usefully different ordering unless it really is useful.
2030 * These routines check for usefulness of given pathkeys.
2031 ****************************************************************************/
2032
2033/*
2034 * pathkeys_useful_for_merging
2035 * Count the number of pathkeys that may be useful for mergejoins
2036 * above the given relation.
2037 *
2038 * We consider a pathkey potentially useful if it corresponds to the merge
2039 * ordering of either side of any joinclause for the rel. This might be
2040 * overoptimistic, since joinclauses that require different other relations
2041 * might never be usable at the same time, but trying to be exact is likely
2042 * to be more trouble than it's worth.
2043 *
2044 * To avoid doubling the number of mergejoin paths considered, we would like
2045 * to consider only one of the two scan directions (ASC or DESC) as useful
2046 * for merging for any given target column. The choice is arbitrary unless
2047 * one of the directions happens to match an ORDER BY key, in which case
2048 * that direction should be preferred, in hopes of avoiding a final sort step.
2049 * right_merge_direction() implements this heuristic.
2050 */
2051static int
2053{
2054 int useful = 0;
2055 ListCell *i;
2056
2057 foreach(i, pathkeys)
2058 {
2059 PathKey *pathkey = (PathKey *) lfirst(i);
2060 bool matched = false;
2061 ListCell *j;
2062
2063 /* If "wrong" direction, not useful for merging */
2064 if (!right_merge_direction(root, pathkey))
2065 break;
2066
2067 /*
2068 * First look into the EquivalenceClass of the pathkey, to see if
2069 * there are any members not yet joined to the rel. If so, it's
2070 * surely possible to generate a mergejoin clause using them.
2071 */
2072 if (rel->has_eclass_joins &&
2073 eclass_useful_for_merging(root, pathkey->pk_eclass, rel))
2074 matched = true;
2075 else
2076 {
2077 /*
2078 * Otherwise search the rel's joininfo list, which contains
2079 * non-EquivalenceClass-derivable join clauses that might
2080 * nonetheless be mergejoinable.
2081 */
2082 foreach(j, rel->joininfo)
2083 {
2084 RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(j);
2085
2086 if (restrictinfo->mergeopfamilies == NIL)
2087 continue;
2088 update_mergeclause_eclasses(root, restrictinfo);
2089
2090 if (pathkey->pk_eclass == restrictinfo->left_ec ||
2091 pathkey->pk_eclass == restrictinfo->right_ec)
2092 {
2093 matched = true;
2094 break;
2095 }
2096 }
2097 }
2098
2099 /*
2100 * If we didn't find a mergeclause, we're done --- any additional
2101 * sort-key positions in the pathkeys are useless. (But we can still
2102 * mergejoin if we found at least one mergeclause.)
2103 */
2104 if (matched)
2105 useful++;
2106 else
2107 break;
2108 }
2109
2110 return useful;
2111}
2112
2113/*
2114 * right_merge_direction
2115 * Check whether the pathkey embodies the preferred sort direction
2116 * for merging its target column.
2117 */
2118static bool
2120{
2121 ListCell *l;
2122
2123 foreach(l, root->query_pathkeys)
2124 {
2125 PathKey *query_pathkey = (PathKey *) lfirst(l);
2126
2127 if (pathkey->pk_eclass == query_pathkey->pk_eclass &&
2128 pathkey->pk_opfamily == query_pathkey->pk_opfamily)
2129 {
2130 /*
2131 * Found a matching query sort column. Prefer this pathkey's
2132 * direction iff it matches. Note that we ignore pk_nulls_first,
2133 * which means that a sort might be needed anyway ... but we still
2134 * want to prefer only one of the two possible directions, and we
2135 * might as well use this one.
2136 */
2137 return (pathkey->pk_strategy == query_pathkey->pk_strategy);
2138 }
2139 }
2140
2141 /* If no matching ORDER BY request, prefer the ASC direction */
2142 return (pathkey->pk_strategy == BTLessStrategyNumber);
2143}
2144
2145/*
2146 * pathkeys_useful_for_ordering
2147 * Count the number of pathkeys that are useful for meeting the
2148 * query's requested output ordering.
2149 *
2150 * Because we the have the possibility of incremental sort, a prefix list of
2151 * keys is potentially useful for improving the performance of the requested
2152 * ordering. Thus we return 0, if no valuable keys are found, or the number
2153 * of leading keys shared by the list and the requested ordering..
2154 */
2155static int
2157{
2158 int n_common_pathkeys;
2159
2160 (void) pathkeys_count_contained_in(root->query_pathkeys, pathkeys,
2161 &n_common_pathkeys);
2162
2163 return n_common_pathkeys;
2164}
2165
2166/*
2167 * pathkeys_useful_for_grouping
2168 * Count the number of pathkeys that are useful for grouping (instead of
2169 * explicit sort)
2170 *
2171 * Group pathkeys could be reordered to benefit from the ordering. The
2172 * ordering may not be "complete" and may require incremental sort, but that's
2173 * fine. So we simply count prefix pathkeys with a matching group key, and
2174 * stop once we find the first pathkey without a match.
2175 *
2176 * So e.g. with pathkeys (a,b,c) and group keys (a,b,e) this determines (a,b)
2177 * pathkeys are useful for grouping, and we might do incremental sort to get
2178 * path ordered by (a,b,e).
2179 *
2180 * This logic is necessary to retain paths with ordering not matching grouping
2181 * keys directly, without the reordering.
2182 *
2183 * Returns the length of pathkey prefix with matching group keys.
2184 */
2185static int
2187{
2188 ListCell *key;
2189 int n = 0;
2190
2191 /* no special ordering requested for grouping */
2192 if (root->group_pathkeys == NIL)
2193 return 0;
2194
2195 /* walk the pathkeys and search for matching group key */
2196 foreach(key, pathkeys)
2197 {
2198 PathKey *pathkey = (PathKey *) lfirst(key);
2199
2200 /* no matching group key, we're done */
2201 if (!list_member_ptr(root->group_pathkeys, pathkey))
2202 break;
2203
2204 n++;
2205 }
2206
2207 return n;
2208}
2209
2210/*
2211 * pathkeys_useful_for_distinct
2212 * Count the number of pathkeys that are useful for DISTINCT or DISTINCT
2213 * ON clause.
2214 *
2215 * DISTINCT keys could be reordered to benefit from the given pathkey list. As
2216 * with pathkeys_useful_for_grouping, we return the number of leading keys in
2217 * the list that are shared by the distinctClause pathkeys.
2218 */
2219static int
2221{
2222 int n_common_pathkeys;
2223
2224 /*
2225 * distinct_pathkeys may have become empty if all of the pathkeys were
2226 * determined to be redundant. Return 0 in this case.
2227 */
2228 if (root->distinct_pathkeys == NIL)
2229 return 0;
2230
2231 /* walk the pathkeys and search for matching DISTINCT key */
2232 n_common_pathkeys = 0;
2233 foreach_node(PathKey, pathkey, pathkeys)
2234 {
2235 /* no matching DISTINCT key, we're done */
2236 if (!list_member_ptr(root->distinct_pathkeys, pathkey))
2237 break;
2238
2239 n_common_pathkeys++;
2240 }
2241
2242 return n_common_pathkeys;
2243}
2244
2245/*
2246 * pathkeys_useful_for_setop
2247 * Count the number of leading common pathkeys root's 'setop_pathkeys' in
2248 * 'pathkeys'.
2249 */
2250static int
2252{
2253 int n_common_pathkeys;
2254
2255 (void) pathkeys_count_contained_in(root->setop_pathkeys, pathkeys,
2256 &n_common_pathkeys);
2257
2258 return n_common_pathkeys;
2259}
2260
2261/*
2262 * truncate_useless_pathkeys
2263 * Shorten the given pathkey list to just the useful pathkeys.
2264 */
2265List *
2267 RelOptInfo *rel,
2268 List *pathkeys)
2269{
2270 int nuseful;
2271 int nuseful2;
2272
2273 nuseful = pathkeys_useful_for_merging(root, rel, pathkeys);
2274 nuseful2 = pathkeys_useful_for_ordering(root, pathkeys);
2275 if (nuseful2 > nuseful)
2276 nuseful = nuseful2;
2277 nuseful2 = pathkeys_useful_for_grouping(root, pathkeys);
2278 if (nuseful2 > nuseful)
2279 nuseful = nuseful2;
2280 nuseful2 = pathkeys_useful_for_distinct(root, pathkeys);
2281 if (nuseful2 > nuseful)
2282 nuseful = nuseful2;
2283 nuseful2 = pathkeys_useful_for_setop(root, pathkeys);
2284 if (nuseful2 > nuseful)
2285 nuseful = nuseful2;
2286
2287 /*
2288 * Note: not safe to modify input list destructively, but we can avoid
2289 * copying the list if we're not actually going to change it
2290 */
2291 if (nuseful == 0)
2292 return NIL;
2293 else if (nuseful == list_length(pathkeys))
2294 return pathkeys;
2295 else
2296 return list_copy_head(pathkeys, nuseful);
2297}
2298
2299/*
2300 * has_useful_pathkeys
2301 * Detect whether the specified rel could have any pathkeys that are
2302 * useful according to truncate_useless_pathkeys().
2303 *
2304 * This is a cheap test that lets us skip building pathkeys at all in very
2305 * simple queries. It's OK to err in the direction of returning "true" when
2306 * there really aren't any usable pathkeys, but erring in the other direction
2307 * is bad --- so keep this in sync with the routines above!
2308 *
2309 * We could make the test more complex, for example checking to see if any of
2310 * the joinclauses are really mergejoinable, but that likely wouldn't win
2311 * often enough to repay the extra cycles. Queries with neither a join nor
2312 * a sort are reasonably common, though, so this much work seems worthwhile.
2313 */
2314bool
2316{
2317 if (rel->joininfo != NIL || rel->has_eclass_joins)
2318 return true; /* might be able to use pathkeys for merging */
2319 if (root->group_pathkeys != NIL)
2320 return true; /* might be able to use pathkeys for grouping */
2321 if (root->query_pathkeys != NIL)
2322 return true; /* might be able to use them for ordering */
2323 return false; /* definitely useless */
2324}
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:216
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:412
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:582
#define bms_is_empty(a)
Definition: bitmapset.h:118
#define Assert(condition)
Definition: c.h:815
int16_t int16
Definition: c.h:483
unsigned int Index
Definition: c.h:571
#define OidIsValid(objectId)
Definition: c.h:732
bool enable_incremental_sort
Definition: costsize.c:151
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:223
EquivalenceClass * get_eclass_for_sort_expr(PlannerInfo *root, Expr *expr, List *opfamilies, Oid opcintype, Oid collation, Index sortref, Relids rel, bool create_it)
Definition: equivclass.c:586
Expr * canonicalize_ec_expression(Expr *expr, Oid req_type, Oid req_collation)
Definition: equivclass.c:471
bool eclass_useful_for_merging(PlannerInfo *root, EquivalenceClass *eclass, RelOptInfo *rel)
Definition: equivclass.c:3266
bool indexcol_is_bool_constant_for_query(PlannerInfo *root, IndexOptInfo *index, int indexcol)
Definition: indxpath.c:4323
int j
Definition: isn.c:73
int i
Definition: isn.c:72
List * list_difference(const List *list1, const List *list2)
Definition: list.c:1237
List * lappend(List *list, void *datum)
Definition: list.c:339
List * list_difference_ptr(const List *list1, const List *list2)
Definition: list.c:1263
List * list_concat_unique_ptr(List *list1, const List *list2)
Definition: list.c:1427
List * list_concat(List *list1, const List *list2)
Definition: list.c:561
List * list_copy(const List *oldlist)
Definition: list.c:1573
bool list_member_ptr(const List *list, const void *datum)
Definition: list.c:682
void list_free(List *list)
Definition: list.c:1546
List * list_copy_head(const List *oldlist, int len)
Definition: list.c:1593
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:166
bool get_ordering_op_properties(Oid opno, Oid *opfamily, Oid *opcintype, int16 *strategy)
Definition: lsyscache.c:207
List * get_mergejoin_opfamilies(Oid opno)
Definition: lsyscache.c:366
void op_input_types(Oid opno, Oid *lefttype, Oid *righttype)
Definition: lsyscache.c:1358
void pfree(void *pointer)
Definition: mcxt.c:1521
void * palloc(Size size)
Definition: mcxt.c:1317
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:821
static Node * get_rightop(const void *clause)
Definition: nodeFuncs.h:95
static bool is_notclause(const void *clause)
Definition: nodeFuncs.h:125
static Expr * get_notclausearg(const void *notclause)
Definition: nodeFuncs.h:134
static Node * get_leftop(const void *clause)
Definition: nodeFuncs.h:83
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
#define copyObject(obj)
Definition: nodes.h:224
#define makeNode(_type_)
Definition: nodes.h:155
JoinType
Definition: nodes.h:288
@ JOIN_FULL
Definition: nodes.h:295
@ JOIN_RIGHT
Definition: nodes.h:296
@ JOIN_RIGHT_SEMI
Definition: nodes.h:309
@ JOIN_RIGHT_ANTI
Definition: nodes.h:310
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
bool partitions_are_ordered(PartitionBoundInfo boundinfo, Bitmapset *live_parts)
Definition: partbounds.c:2852
static bool matches_boolean_partition_clause(RestrictInfo *rinfo, RelOptInfo *partrel, int partkeycol)
Definition: pathkeys.c:884
List * truncate_useless_pathkeys(PlannerInfo *root, RelOptInfo *rel, List *pathkeys)
Definition: pathkeys.c:2266
static int pathkeys_useful_for_setop(PlannerInfo *root, List *pathkeys)
Definition: pathkeys.c:2251
Path * get_cheapest_fractional_path_for_pathkeys(List *paths, List *pathkeys, Relids required_outer, double fraction)
Definition: pathkeys.c:666
static bool right_merge_direction(PlannerInfo *root, PathKey *pathkey)
Definition: pathkeys.c:2119
List * append_pathkeys(List *target, List *source)
Definition: pathkeys.c:107
Path * get_cheapest_path_for_pathkeys(List *paths, List *pathkeys, Relids required_outer, CostSelector cost_criterion, bool require_parallel_safe)
Definition: pathkeys.c:620
bool pathkeys_count_contained_in(List *keys1, List *keys2, int *n_common)
Definition: pathkeys.c:558
List * make_inner_pathkeys_for_merge(PlannerInfo *root, List *mergeclauses, List *outer_pathkeys)
Definition: pathkeys.c:1854
static int pathkeys_useful_for_distinct(PlannerInfo *root, List *pathkeys)
Definition: pathkeys.c:2220
static PathKey * make_pathkey_from_sortop(PlannerInfo *root, Expr *expr, Oid ordering_op, bool reverse_sort, bool nulls_first, Index sortref, bool create_it)
Definition: pathkeys.c:256
static int group_keys_reorder_by_pathkeys(List *pathkeys, List **group_pathkeys, List **group_clauses, int num_groupby_pathkeys)
Definition: pathkeys.c:370
bool has_useful_pathkeys(PlannerInfo *root, RelOptInfo *rel)
Definition: pathkeys.c:2315
List * build_index_pathkeys(PlannerInfo *root, IndexOptInfo *index, ScanDirection scandir)
Definition: pathkeys.c:740
static int pathkeys_useful_for_ordering(PlannerInfo *root, List *pathkeys)
Definition: pathkeys.c:2156
List * find_mergeclauses_for_outer_pathkeys(PlannerInfo *root, List *pathkeys, List *restrictinfos)
Definition: pathkeys.c:1543
void update_mergeclause_eclasses(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: pathkeys.c:1509
List * make_pathkeys_for_sortclauses(PlannerInfo *root, List *sortclauses, List *tlist)
Definition: pathkeys.c:1335
static Var * find_var_for_subquery_tle(RelOptInfo *rel, TargetEntry *tle)
Definition: pathkeys.c:1251
List * trim_mergeclauses_for_inner_pathkeys(PlannerInfo *root, List *mergeclauses, List *pathkeys)
Definition: pathkeys.c:1957
static int pathkeys_useful_for_grouping(PlannerInfo *root, List *pathkeys)
Definition: pathkeys.c:2186
static bool partkey_is_bool_constant_for_query(RelOptInfo *partrel, int partkeycol)
Definition: pathkeys.c:844
bool enable_group_by_reordering
Definition: pathkeys.c:32
List * build_expression_pathkey(PlannerInfo *root, Expr *expr, Oid opno, Relids rel, bool create_it)
Definition: pathkeys.c:1000
static bool pathkey_is_redundant(PathKey *new_pathkey, List *pathkeys)
Definition: pathkeys.c:159
List * build_partition_pathkeys(PlannerInfo *root, RelOptInfo *partrel, ScanDirection scandir, bool *partialkeys)
Definition: pathkeys.c:919
List * select_outer_pathkeys_for_merge(PlannerInfo *root, List *mergeclauses, RelOptInfo *joinrel)
Definition: pathkeys.c:1658
List * convert_subquery_pathkeys(PlannerInfo *root, RelOptInfo *rel, List *subquery_pathkeys, List *subquery_tlist)
Definition: pathkeys.c:1054
static int pathkeys_useful_for_merging(PlannerInfo *root, RelOptInfo *rel, List *pathkeys)
Definition: pathkeys.c:2052
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)
Definition: pathkeys.c:1380
void initialize_mergeclause_eclasses(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: pathkeys.c:1462
bool pathkeys_contained_in(List *keys1, List *keys2)
Definition: pathkeys.c:343
List * build_join_pathkeys(PlannerInfo *root, RelOptInfo *joinrel, JoinType jointype, List *outer_pathkeys)
Definition: pathkeys.c:1294
PathKey * make_canonical_pathkey(PlannerInfo *root, EquivalenceClass *eclass, Oid opfamily, int strategy, bool nulls_first)
Definition: pathkeys.c:56
PathKeysComparison compare_pathkeys(List *keys1, List *keys2)
Definition: pathkeys.c:304
Path * get_cheapest_parallel_safe_total_inner(List *paths)
Definition: pathkeys.c:699
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)
Definition: pathkeys.c:198
List * get_useful_group_keys_orderings(PlannerInfo *root, Path *path)
Definition: pathkeys.c:467
int compare_fractional_path_costs(Path *path1, Path *path2, double fraction)
Definition: pathnode.c:124
int compare_path_costs(Path *path1, Path *path2, CostSelector criterion)
Definition: pathnode.c:69
#define EC_MUST_BE_REDUNDANT(eclass)
Definition: pathnodes.h:1414
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:839
CostSelector
Definition: pathnodes.h:37
#define PATH_REQ_OUTER(path)
Definition: pathnodes.h:1681
PathKeysComparison
Definition: paths.h:203
@ PATHKEYS_BETTER2
Definition: paths.h:206
@ PATHKEYS_BETTER1
Definition: paths.h:205
@ PATHKEYS_DIFFERENT
Definition: paths.h:207
@ PATHKEYS_EQUAL
Definition: paths.h:204
void * arg
#define lfirst(lc)
Definition: pg_list.h:172
#define lfirst_node(type, lc)
Definition: pg_list.h:176
static int list_length(const List *l)
Definition: pg_list.h:152
#define linitial_node(type, l)
Definition: pg_list.h:181
#define NIL
Definition: pg_list.h:68
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:518
#define foreach_current_index(var_or_cell)
Definition: pg_list.h:403
#define foreach_delete_current(lst, var_or_cell)
Definition: pg_list.h:391
#define list_make1(x1)
Definition: pg_list.h:212
#define for_each_from(cell, lst, N)
Definition: pg_list.h:414
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
#define linitial(l)
Definition: pg_list.h:178
#define foreach_node(type, var, lst)
Definition: pg_list.h:496
static ListCell * list_head(const List *l)
Definition: pg_list.h:128
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:343
#define linitial_oid(l)
Definition: pg_list.h:180
static rewind_source * source
Definition: pg_rewind.c:89
unsigned int Oid
Definition: postgres_ext.h:32
tree ctl root
Definition: radixtree.h:1857
static struct cvec * eclass(struct vars *v, chr c, int cases)
Definition: regc_locale.c:500
static struct subre * parse(struct vars *v, int stopper, int type, struct state *init, struct state *final)
Definition: regcomp.c:717
Node * remove_nulling_relids(Node *node, const Bitmapset *removable_relids, const Bitmapset *except_relids)
#define ScanDirectionIsBackward(direction)
Definition: sdir.h:50
ScanDirection
Definition: sdir.h:25
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
#define BTLessStrategyNumber
Definition: stratnum.h:29
#define BTEqualStrategyNumber
Definition: stratnum.h:31
List * ec_opfamilies
Definition: pathnodes.h:1394
Definition: pg_list.h:54
Definition: nodes.h:129
bool pk_nulls_first
Definition: pathnodes.h:1482
int pk_strategy
Definition: pathnodes.h:1481
Oid pk_opfamily
Definition: pathnodes.h:1480
List * exprs
Definition: pathnodes.h:1544
List * pathkeys
Definition: pathnodes.h:1677
bool parallel_safe
Definition: pathnodes.h:1666
List * baserestrictinfo
Definition: pathnodes.h:985
List * joininfo
Definition: pathnodes.h:991
Relids relids
Definition: pathnodes.h:871
struct PathTarget * reltarget
Definition: pathnodes.h:893
Index relid
Definition: pathnodes.h:918
bool has_eclass_joins
Definition: pathnodes.h:993
Bitmapset * live_parts
Definition: pathnodes.h:1039
Expr * clause
Definition: pathnodes.h:2575
Index tleSortGroupRef
Definition: parsenodes.h:1447
Expr * expr
Definition: primnodes.h:2245
AttrNumber resno
Definition: primnodes.h:2247
Definition: primnodes.h:261
AttrNumber varattno
Definition: primnodes.h:273
int varno
Definition: primnodes.h:268
Definition: type.h:96
SortGroupClause * get_sortgroupref_clause_noerr(Index sortref, List *clauses)
Definition: tlist.c:443
TargetEntry * get_sortgroupref_tle(Index sortref, List *targetList)
Definition: tlist.c:345
Node * get_sortgroupclause_expr(SortGroupClause *sgClause, List *targetList)
Definition: tlist.c:379