]> granicus.if.org Git - postgresql/blob - src/backend/optimizer/path/pathkeys.c
3e4bcffe2e85ab6062ae4856de5e001ad0fa04ad
[postgresql] / src / backend / optimizer / path / pathkeys.c
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-2005, PostgreSQL Global Development Group
11  * Portions Copyright (c) 1994, Regents of the University of California
12  *
13  * IDENTIFICATION
14  *        $PostgreSQL: pgsql/src/backend/optimizer/path/pathkeys.c,v 1.71 2005/07/28 22:27:00 tgl Exp $
15  *
16  *-------------------------------------------------------------------------
17  */
18 #include "postgres.h"
19
20 #include "nodes/makefuncs.h"
21 #include "optimizer/clauses.h"
22 #include "optimizer/pathnode.h"
23 #include "optimizer/paths.h"
24 #include "optimizer/planmain.h"
25 #include "optimizer/tlist.h"
26 #include "optimizer/var.h"
27 #include "parser/parsetree.h"
28 #include "parser/parse_expr.h"
29 #include "parser/parse_func.h"
30 #include "utils/lsyscache.h"
31 #include "utils/memutils.h"
32
33
34 static PathKeyItem *makePathKeyItem(Node *key, Oid sortop, bool checkType);
35 static void generate_outer_join_implications(PlannerInfo *root,
36                                                                                          List *equi_key_set,
37                                                                                          Relids *relids);
38 static void sub_generate_join_implications(PlannerInfo *root,
39                                                                                    List *equi_key_set, Relids *relids,
40                                                                                    Node *item1, Oid sortop1,
41                                                                                    Relids item1_relids);
42 static void process_implied_const_eq(PlannerInfo *root,
43                                                                          List *equi_key_set, Relids *relids,
44                                                                          Node *item1, Oid sortop1,
45                                                                          Relids item1_relids,
46                                                                          bool delete_it);
47 static List *make_canonical_pathkey(PlannerInfo *root, PathKeyItem *item);
48 static Var *find_indexkey_var(PlannerInfo *root, RelOptInfo *rel,
49                                   AttrNumber varattno);
50
51
52 /*
53  * makePathKeyItem
54  *              create a PathKeyItem node
55  */
56 static PathKeyItem *
57 makePathKeyItem(Node *key, Oid sortop, bool checkType)
58 {
59         PathKeyItem *item = makeNode(PathKeyItem);
60
61         /*
62          * Some callers pass expressions that are not necessarily of the same
63          * type as the sort operator expects as input (for example when
64          * dealing with an index that uses binary-compatible operators).  We
65          * must relabel these with the correct type so that the key
66          * expressions will be seen as equal() to expressions that have been
67          * correctly labeled.
68          */
69         if (checkType)
70         {
71                 Oid                     lefttype,
72                                         righttype;
73
74                 op_input_types(sortop, &lefttype, &righttype);
75                 if (exprType(key) != lefttype)
76                         key = (Node *) makeRelabelType((Expr *) key,
77                                                                                    lefttype, -1,
78                                                                                    COERCE_DONTCARE);
79         }
80
81         item->key = key;
82         item->sortop = sortop;
83         return item;
84 }
85
86 /*
87  * add_equijoined_keys
88  *        The given clause has a mergejoinable operator, so its two sides
89  *        can be considered equal after restriction clause application; in
90  *        particular, any pathkey mentioning one side (with the correct sortop)
91  *        can be expanded to include the other as well.  Record the exprs and
92  *        associated sortops in the query's equi_key_list for future use.
93  *
94  * The query's equi_key_list field points to a list of sublists of PathKeyItem
95  * nodes, where each sublist is a set of two or more exprs+sortops that have
96  * been identified as logically equivalent (and, therefore, we may consider
97  * any two in a set to be equal).  As described above, we will subsequently
98  * use direct pointers to one of these sublists to represent any pathkey
99  * that involves an equijoined variable.
100  */
101 void
102 add_equijoined_keys(PlannerInfo *root, RestrictInfo *restrictinfo)
103 {
104         Expr       *clause = restrictinfo->clause;
105         PathKeyItem *item1 = makePathKeyItem(get_leftop(clause),
106                                                                                  restrictinfo->left_sortop,
107                                                                                  false);
108         PathKeyItem *item2 = makePathKeyItem(get_rightop(clause),
109                                                                                  restrictinfo->right_sortop,
110                                                                                  false);
111         List       *newset;
112         ListCell   *cursetlink;
113
114         /* We might see a clause X=X; don't make a single-element list from it */
115         if (equal(item1, item2))
116                 return;
117
118         /*
119          * Our plan is to make a two-element set, then sweep through the
120          * existing equijoin sets looking for matches to item1 or item2.  When
121          * we find one, we remove that set from equi_key_list and union it
122          * into our new set. When done, we add the new set to the front of
123          * equi_key_list.
124          *
125          * It may well be that the two items we're given are already known to be
126          * equijoin-equivalent, in which case we don't need to change our data
127          * structure.  If we find both of them in the same equivalence set to
128          * start with, we can quit immediately.
129          *
130          * This is a standard UNION-FIND problem, for which there exist better
131          * data structures than simple lists.  If this code ever proves to be
132          * a bottleneck then it could be sped up --- but for now, simple is
133          * beautiful.
134          */
135         newset = NIL;
136
137         /* cannot use foreach here because of possible lremove */
138         cursetlink = list_head(root->equi_key_list);
139         while (cursetlink)
140         {
141                 List       *curset = (List *) lfirst(cursetlink);
142                 bool            item1here = list_member(curset, item1);
143                 bool            item2here = list_member(curset, item2);
144
145                 /* must advance cursetlink before lremove possibly pfree's it */
146                 cursetlink = lnext(cursetlink);
147
148                 if (item1here || item2here)
149                 {
150                         /*
151                          * If find both in same equivalence set, no need to do any
152                          * more
153                          */
154                         if (item1here && item2here)
155                         {
156                                 /* Better not have seen only one in an earlier set... */
157                                 Assert(newset == NIL);
158                                 return;
159                         }
160
161                         /* Build the new set only when we know we must */
162                         if (newset == NIL)
163                                 newset = list_make2(item1, item2);
164
165                         /* Found a set to merge into our new set */
166                         newset = list_concat_unique(newset, curset);
167
168                         /*
169                          * Remove old set from equi_key_list.
170                          */
171                         root->equi_key_list = list_delete_ptr(root->equi_key_list, curset);
172                         list_free(curset);      /* might as well recycle old cons cells */
173                 }
174         }
175
176         /* Build the new set only when we know we must */
177         if (newset == NIL)
178                 newset = list_make2(item1, item2);
179
180         root->equi_key_list = lcons(newset, root->equi_key_list);
181 }
182
183 /*
184  * generate_implied_equalities
185  *        Scan the completed equi_key_list for the query, and generate explicit
186  *        qualifications (WHERE clauses) for all the pairwise equalities not
187  *        already mentioned in the quals; or remove qualifications found to be
188  *        redundant.
189  *
190  * Adding deduced equalities is useful because the additional clauses help
191  * the selectivity-estimation code and may allow better joins to be chosen;
192  * and in fact it's *necessary* to ensure that sort keys we think are
193  * equivalent really are (see src/backend/optimizer/README for more info).
194  *
195  * If an equi_key_list set includes any constants then we adopt a different
196  * strategy: we record all the "var = const" deductions we can make, and
197  * actively remove all the "var = var" clauses that are implied by the set
198  * (including the clauses that originally gave rise to the set!).  The reason
199  * is that given input like "a = b AND b = 42", once we have deduced "a = 42"
200  * there is no longer any need to apply the clause "a = b"; not only is
201  * it a waste of time to check it, but we will misestimate selectivity if the
202  * clause is left in.  So we must remove it.  For this purpose, any pathkey
203  * item that mentions no Vars of the current level can be taken as a constant.
204  * (The only case where this would be risky is if the item contains volatile
205  * functions; but we will never consider such an expression to be a pathkey
206  * at all, because check_mergejoinable() will reject it.)
207  *
208  * Also, when we have constants in an equi_key_list we can try to propagate
209  * the constants into outer joins; see generate_outer_join_implications
210  * for discussion.
211  *
212  * This routine just walks the equi_key_list to find all pairwise equalities.
213  * We call process_implied_equality (in plan/initsplan.c) to adjust the
214  * restrictinfo datastructures for each pair.
215  */
216 void
217 generate_implied_equalities(PlannerInfo *root)
218 {
219         ListCell   *cursetlink;
220
221         foreach(cursetlink, root->equi_key_list)
222         {
223                 List       *curset = (List *) lfirst(cursetlink);
224                 int                     nitems = list_length(curset);
225                 Relids     *relids;
226                 bool            have_consts;
227                 ListCell   *ptr1;
228                 int                     i1;
229
230                 /*
231                  * A set containing only two items cannot imply any equalities
232                  * beyond the one that created the set, so we can skip it ---
233                  * unless outer joins appear in the query.
234                  */
235                 if (nitems < 3 && !root->hasOuterJoins)
236                         continue;
237
238                 /*
239                  * Collect info about relids mentioned in each item.  For this
240                  * routine we only really care whether there are any at all in
241                  * each item, but process_implied_equality() needs the exact sets,
242                  * so we may as well pull them here.
243                  */
244                 relids = (Relids *) palloc(nitems * sizeof(Relids));
245                 have_consts = false;
246                 i1 = 0;
247                 foreach(ptr1, curset)
248                 {
249                         PathKeyItem *item1 = (PathKeyItem *) lfirst(ptr1);
250
251                         relids[i1] = pull_varnos(item1->key);
252                         if (bms_is_empty(relids[i1]))
253                                 have_consts = true;
254                         i1++;
255                 }
256
257                 /*
258                  * Match each item in the set with all that appear after it (it's
259                  * sufficient to generate A=B, need not process B=A too).
260                  *
261                  * A set containing only two items cannot imply any equalities
262                  * beyond the one that created the set, so we can skip this
263                  * processing in that case.
264                  */
265                 if (nitems >= 3)
266                 {
267                         i1 = 0;
268                         foreach(ptr1, curset)
269                         {
270                                 PathKeyItem *item1 = (PathKeyItem *) lfirst(ptr1);
271                                 bool            i1_is_variable = !bms_is_empty(relids[i1]);
272                                 ListCell   *ptr2;
273                                 int                     i2 = i1 + 1;
274
275                                 for_each_cell(ptr2, lnext(ptr1))
276                                 {
277                                         PathKeyItem *item2 = (PathKeyItem *) lfirst(ptr2);
278                                         bool            i2_is_variable = !bms_is_empty(relids[i2]);
279
280                                         /*
281                                          * If it's "const = const" then just ignore it altogether.
282                                          * There is no place in the restrictinfo structure to
283                                          * store it.  (If the two consts are in fact unequal, then
284                                          * propagating the comparison to Vars will cause us to
285                                          * produce zero rows out, as expected.)
286                                          */
287                                         if (i1_is_variable || i2_is_variable)
288                                         {
289                                                 /*
290                                                  * Tell process_implied_equality to delete the clause,
291                                                  * not add it, if it's "var = var" and we have
292                                                  * constants present in the list.
293                                                  */
294                                                 bool            delete_it = (have_consts &&
295                                                                                                  i1_is_variable &&
296                                                                                                  i2_is_variable);
297
298                                                 process_implied_equality(root,
299                                                                                                  item1->key, item2->key,
300                                                                                                  item1->sortop, item2->sortop,
301                                                                                                  relids[i1], relids[i2],
302                                                                                                  delete_it);
303                                         }
304                                         i2++;
305                                 }
306                                 i1++;
307                         }
308                 }
309
310                 /*
311                  * If we have constant(s) and outer joins, try to propagate the
312                  * constants through outer-join quals.
313                  */
314                 if (have_consts && root->hasOuterJoins)
315                         generate_outer_join_implications(root, curset, relids);
316         }
317 }
318
319 /*
320  * generate_outer_join_implications
321  *        Generate clauses that can be deduced in outer-join situations.
322  *
323  * When we have mergejoinable clauses A = B that are outer-join clauses,
324  * we can't blindly combine them with other clauses A = C to deduce B = C,
325  * since in fact the "equality" A = B won't necessarily hold above the
326  * outer join (one of the variables might be NULL instead).  Nonetheless
327  * there are cases where we can add qual clauses using transitivity.
328  *
329  * One case that we look for here is an outer-join clause OUTERVAR = INNERVAR
330  * combined with a pushed-down (valid everywhere) clause OUTERVAR = CONSTANT.
331  * It is safe and useful to push a clause INNERVAR = CONSTANT into the
332  * evaluation of the inner (nullable) relation, because any inner rows not
333  * meeting this condition will not contribute to the outer-join result anyway.
334  * (Any outer rows they could join to will be eliminated by the pushed-down
335  * clause.)
336  *
337  * Note that the above rule does not work for full outer joins, nor for
338  * pushed-down restrictions on an inner-side variable; nor is it very
339  * interesting to consider cases where the pushed-down clause involves
340  * relations entirely outside the outer join, since such clauses couldn't
341  * be pushed into the inner side's scan anyway.  So the restriction to
342  * outervar = pseudoconstant is not really giving up anything.
343  *
344  * For full-join cases, we can only do something useful if it's a FULL JOIN
345  * USING and a merged column has a restriction MERGEDVAR = CONSTANT.  By
346  * the time it gets here, the restriction will look like
347  *              COALESCE(LEFTVAR, RIGHTVAR) = CONSTANT
348  * and we will have a join clause LEFTVAR = RIGHTVAR that we can match the
349  * COALESCE expression to.  In this situation we can push LEFTVAR = CONSTANT
350  * and RIGHTVAR = CONSTANT into the input relations, since any rows not
351  * meeting these conditions cannot contribute to the join result.
352  *
353  * Again, there isn't any traction to be gained by trying to deal with
354  * clauses comparing a mergedvar to a non-pseudoconstant.  So we can make
355  * use of the equi_key_lists to quickly find the interesting pushed-down
356  * clauses.  The interesting outer-join clauses were accumulated for us by
357  * distribute_qual_to_rels.
358  *
359  * equi_key_set: a list of PathKeyItems that are known globally equivalent,
360  * at least one of which is a pseudoconstant.
361  * relids: an array of Relids sets showing the relation membership of each
362  * PathKeyItem in equi_key_set.
363  */
364 static void
365 generate_outer_join_implications(PlannerInfo *root,
366                                                                  List *equi_key_set,
367                                                                  Relids *relids)
368 {
369         ListCell   *l;
370         int                     i = 0;
371
372         /* Process each non-constant element of equi_key_set */
373         foreach(l, equi_key_set)
374         {
375                 PathKeyItem *item1 = (PathKeyItem *) lfirst(l);
376
377                 if (!bms_is_empty(relids[i]))
378                 {
379                         sub_generate_join_implications(root, equi_key_set, relids,
380                                                                                    item1->key,
381                                                                                    item1->sortop,
382                                                                                    relids[i]);
383                 }
384                 i++;
385         }
386 }
387
388 /*
389  * sub_generate_join_implications
390  *        Propagate a constant equality through outer join clauses.
391  *
392  * The item described by item1/sortop1/item1_relids has been determined
393  * to be equal to the constant(s) listed in equi_key_set.  Recursively
394  * trace out the implications of this.
395  *
396  * equi_key_set and relids are as for generate_outer_join_implications.
397  */
398 static void
399 sub_generate_join_implications(PlannerInfo *root,
400                                                                 List *equi_key_set, Relids *relids,
401                                                                 Node *item1, Oid sortop1, Relids item1_relids)
402
403 {
404         ListCell   *l;
405
406         /*
407          * Examine each mergejoinable outer-join clause with OUTERVAR on left,
408          * looking for an OUTERVAR identical to item1
409          */
410         foreach(l, root->left_join_clauses)
411         {
412                 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
413                 Node   *leftop = get_leftop(rinfo->clause);
414
415                 if (equal(leftop, item1) && rinfo->left_sortop == sortop1)
416                 {
417                         /*
418                          * Match, so find constant member(s) of set and generate
419                          * implied INNERVAR = CONSTANT
420                          */
421                         Node   *rightop = get_rightop(rinfo->clause);
422
423                         process_implied_const_eq(root, equi_key_set, relids,
424                                                                          rightop,
425                                                                          rinfo->right_sortop,
426                                                                          rinfo->right_relids,
427                                                                          false);
428                         /*
429                          * We can remove explicit tests of this outer-join qual, too,
430                          * since we now have tests forcing each of its sides
431                          * to the same value.
432                          */
433                         process_implied_equality(root,
434                                                                          leftop, rightop,
435                                                                          rinfo->left_sortop, rinfo->right_sortop,
436                                                                          rinfo->left_relids, rinfo->right_relids,
437                                                                          true);
438                         /*
439                          * And recurse to see if we can deduce anything from
440                          * INNERVAR = CONSTANT
441                          */
442                         sub_generate_join_implications(root, equi_key_set, relids,
443                                                                                    rightop,
444                                                                                    rinfo->right_sortop,
445                                                                                    rinfo->right_relids);
446                 }
447         }
448
449         /* The same, looking at clauses with OUTERVAR on right */
450         foreach(l, root->right_join_clauses)
451         {
452                 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
453                 Node   *rightop = get_rightop(rinfo->clause);
454
455                 if (equal(rightop, item1) && rinfo->right_sortop == sortop1)
456                 {
457                         /*
458                          * Match, so find constant member(s) of set and generate
459                          * implied INNERVAR = CONSTANT
460                          */
461                         Node   *leftop = get_leftop(rinfo->clause);
462
463                         process_implied_const_eq(root, equi_key_set, relids,
464                                                                          leftop,
465                                                                          rinfo->left_sortop,
466                                                                          rinfo->left_relids,
467                                                                          false);
468                         /*
469                          * We can remove explicit tests of this outer-join qual, too,
470                          * since we now have tests forcing each of its sides
471                          * to the same value.
472                          */
473                         process_implied_equality(root,
474                                                                          leftop, rightop,
475                                                                          rinfo->left_sortop, rinfo->right_sortop,
476                                                                          rinfo->left_relids, rinfo->right_relids,
477                                                                          true);
478                         /*
479                          * And recurse to see if we can deduce anything from
480                          * INNERVAR = CONSTANT
481                          */
482                         sub_generate_join_implications(root, equi_key_set, relids,
483                                                                                    leftop,
484                                                                                    rinfo->left_sortop,
485                                                                                    rinfo->left_relids);
486                 }
487         }
488
489         /*
490          * Only COALESCE(x,y) items can possibly match full joins
491          */
492         if (IsA(item1, CoalesceExpr))
493         {
494                 CoalesceExpr *cexpr = (CoalesceExpr *) item1;
495                 Node   *cfirst;
496                 Node   *csecond;
497
498                 if (list_length(cexpr->args) != 2)
499                         return;
500                 cfirst = (Node *) linitial(cexpr->args);
501                 csecond = (Node *) lsecond(cexpr->args);
502
503                 /*
504                  * Examine each mergejoinable full-join clause, looking for a
505                  * clause of the form "x = y" matching the COALESCE(x,y) expression
506                  */
507                 foreach(l, root->full_join_clauses)
508                 {
509                         RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
510                         Node   *leftop = get_leftop(rinfo->clause);
511                         Node   *rightop = get_rightop(rinfo->clause);
512
513                         /*
514                          * We can assume the COALESCE() inputs are in the same order
515                          * as the join clause, since both were automatically generated
516                          * in the cases we care about.
517                          *
518                          * XXX currently this may fail to match in cross-type cases
519                          * because the COALESCE will contain typecast operations while
520                          * the join clause may not (if there is a cross-type mergejoin
521                          * operator available for the two column types).
522                          * Is it OK to strip implicit coercions from the COALESCE
523                          * arguments?  What of the sortops in such cases?
524                          */
525                         if (equal(leftop, cfirst) &&
526                                 equal(rightop, csecond) &&
527                                 rinfo->left_sortop == sortop1 &&
528                                 rinfo->right_sortop == sortop1)
529                         {
530                                 /*
531                                  * Match, so find constant member(s) of set and generate
532                                  * implied LEFTVAR = CONSTANT
533                                  */
534                                 process_implied_const_eq(root, equi_key_set, relids,
535                                                                                  leftop,
536                                                                                  rinfo->left_sortop,
537                                                                                  rinfo->left_relids,
538                                                                                  false);
539                                 /* ... and RIGHTVAR = CONSTANT */
540                                 process_implied_const_eq(root, equi_key_set, relids,
541                                                                                  rightop,
542                                                                                  rinfo->right_sortop,
543                                                                                  rinfo->right_relids,
544                                                                                  false);
545                                 /* ... and remove COALESCE() = CONSTANT */
546                                 process_implied_const_eq(root, equi_key_set, relids,
547                                                                                  item1,
548                                                                                  sortop1,
549                                                                                  item1_relids,
550                                                                                  true);
551                                 /*
552                                  * We can remove explicit tests of this outer-join qual, too,
553                                  * since we now have tests forcing each of its sides
554                                  * to the same value.
555                                  */
556                                 process_implied_equality(root,
557                                                                                  leftop, rightop,
558                                                                                  rinfo->left_sortop,
559                                                                                  rinfo->right_sortop,
560                                                                                  rinfo->left_relids,
561                                                                                  rinfo->right_relids,
562                                                                                  true);
563                                 /*
564                                  * And recurse to see if we can deduce anything from
565                                  * LEFTVAR = CONSTANT
566                                  */
567                                 sub_generate_join_implications(root, equi_key_set, relids,
568                                                                                            leftop,
569                                                                                            rinfo->left_sortop,
570                                                                                            rinfo->left_relids);
571                                 /* ... and RIGHTVAR = CONSTANT */
572                                 sub_generate_join_implications(root, equi_key_set, relids,
573                                                                                            rightop,
574                                                                                            rinfo->right_sortop,
575                                                                                            rinfo->right_relids);
576
577                         }
578                 }
579         }
580 }
581
582 /*
583  * process_implied_const_eq
584  *        Apply process_implied_equality with the given item and each
585  *        pseudoconstant member of equi_key_set.
586  *
587  * equi_key_set and relids are as for generate_outer_join_implications,
588  * the other parameters as for process_implied_equality.
589  */
590 static void
591 process_implied_const_eq(PlannerInfo *root, List *equi_key_set, Relids *relids,
592                                                  Node *item1, Oid sortop1, Relids item1_relids,
593                                                  bool delete_it)
594 {
595         ListCell   *l;
596         bool            found = false;
597         int                     i = 0;
598
599         foreach(l, equi_key_set)
600         {
601                 PathKeyItem *item2 = (PathKeyItem *) lfirst(l);
602
603                 if (bms_is_empty(relids[i]))
604                 {
605                         process_implied_equality(root,
606                                                                          item1, item2->key,
607                                                                          sortop1, item2->sortop,
608                                                                          item1_relids, NULL,
609                                                                          delete_it);
610                         found = true;
611                 }
612                 i++;
613         }
614         /* Caller screwed up if no constants in list */
615         Assert(found);
616 }
617
618 /*
619  * exprs_known_equal
620  *        Detect whether two expressions are known equal due to equijoin clauses.
621  *
622  * Note: does not bother to check for "equal(item1, item2)"; caller must
623  * check that case if it's possible to pass identical items.
624  */
625 bool
626 exprs_known_equal(PlannerInfo *root, Node *item1, Node *item2)
627 {
628         ListCell   *cursetlink;
629
630         foreach(cursetlink, root->equi_key_list)
631         {
632                 List       *curset = (List *) lfirst(cursetlink);
633                 bool            item1member = false;
634                 bool            item2member = false;
635                 ListCell   *ptr;
636
637                 foreach(ptr, curset)
638                 {
639                         PathKeyItem *pitem = (PathKeyItem *) lfirst(ptr);
640
641                         if (equal(item1, pitem->key))
642                                 item1member = true;
643                         else if (equal(item2, pitem->key))
644                                 item2member = true;
645                         /* Exit as soon as equality is proven */
646                         if (item1member && item2member)
647                                 return true;
648                 }
649         }
650         return false;
651 }
652
653
654 /*
655  * make_canonical_pathkey
656  *        Given a PathKeyItem, find the equi_key_list subset it is a member of,
657  *        if any.  If so, return a pointer to that sublist, which is the
658  *        canonical representation (for this query) of that PathKeyItem's
659  *        equivalence set.      If it is not found, add a singleton "equivalence set"
660  *        to the equi_key_list and return that --- see compare_pathkeys.
661  *
662  * Note that this function must not be used until after we have completed
663  * scanning the WHERE clause for equijoin operators.
664  */
665 static List *
666 make_canonical_pathkey(PlannerInfo *root, PathKeyItem *item)
667 {
668         List       *newset;
669         ListCell   *cursetlink;
670
671         foreach(cursetlink, root->equi_key_list)
672         {
673                 List       *curset = (List *) lfirst(cursetlink);
674
675                 if (list_member(curset, item))
676                         return curset;
677         }
678         newset = list_make1(item);
679         root->equi_key_list = lcons(newset, root->equi_key_list);
680         return newset;
681 }
682
683 /*
684  * canonicalize_pathkeys
685  *         Convert a not-necessarily-canonical pathkeys list to canonical form.
686  *
687  * Note that this function must not be used until after we have completed
688  * scanning the WHERE clause for equijoin operators.
689  */
690 List *
691 canonicalize_pathkeys(PlannerInfo *root, List *pathkeys)
692 {
693         List       *new_pathkeys = NIL;
694         ListCell   *l;
695
696         foreach(l, pathkeys)
697         {
698                 List       *pathkey = (List *) lfirst(l);
699                 PathKeyItem *item;
700                 List       *cpathkey;
701
702                 /*
703                  * It's sufficient to look at the first entry in the sublist; if
704                  * there are more entries, they're already part of an equivalence
705                  * set by definition.
706                  */
707                 Assert(pathkey != NIL);
708                 item = (PathKeyItem *) linitial(pathkey);
709                 cpathkey = make_canonical_pathkey(root, item);
710
711                 /*
712                  * Eliminate redundant ordering requests --- ORDER BY A,A is the
713                  * same as ORDER BY A.  We want to check this only after we have
714                  * canonicalized the keys, so that equivalent-key knowledge is
715                  * used when deciding if an item is redundant.
716                  */
717                 new_pathkeys = list_append_unique_ptr(new_pathkeys, cpathkey);
718         }
719         return new_pathkeys;
720 }
721
722
723 /*
724  * count_canonical_peers
725  *        Given a PathKeyItem, find the equi_key_list subset it is a member of,
726  *        if any.  If so, return the number of other members of the set.
727  *        If not, return 0 (without actually adding it to our equi_key_list).
728  *
729  * This is a hack to support the rather bogus heuristics in
730  * convert_subquery_pathkeys.
731  */
732 static int
733 count_canonical_peers(PlannerInfo *root, PathKeyItem *item)
734 {
735         ListCell   *cursetlink;
736
737         foreach(cursetlink, root->equi_key_list)
738         {
739                 List       *curset = (List *) lfirst(cursetlink);
740
741                 if (list_member(curset, item))
742                         return list_length(curset) - 1;
743         }
744         return 0;
745 }
746
747 /****************************************************************************
748  *              PATHKEY COMPARISONS
749  ****************************************************************************/
750
751 /*
752  * compare_pathkeys
753  *        Compare two pathkeys to see if they are equivalent, and if not whether
754  *        one is "better" than the other.
755  *
756  *        This function may only be applied to canonicalized pathkey lists.
757  *        In the canonical representation, sublists can be checked for equality
758  *        by simple pointer comparison.
759  */
760 PathKeysComparison
761 compare_pathkeys(List *keys1, List *keys2)
762 {
763         ListCell   *key1,
764                            *key2;
765
766         forboth(key1, keys1, key2, keys2)
767         {
768                 List       *subkey1 = (List *) lfirst(key1);
769                 List       *subkey2 = (List *) lfirst(key2);
770
771                 /*
772                  * XXX would like to check that we've been given canonicalized
773                  * input, but PlannerInfo not accessible here...
774                  */
775 #ifdef NOT_USED
776                 Assert(list_member_ptr(root->equi_key_list, subkey1));
777                 Assert(list_member_ptr(root->equi_key_list, subkey2));
778 #endif
779
780                 /*
781                  * We will never have two subkeys where one is a subset of the
782                  * other, because of the canonicalization process.      Either they
783                  * are equal or they ain't.  Furthermore, we only need pointer
784                  * comparison to detect equality.
785                  */
786                 if (subkey1 != subkey2)
787                         return PATHKEYS_DIFFERENT;      /* no need to keep looking */
788         }
789
790         /*
791          * If we reached the end of only one list, the other is longer and
792          * therefore not a subset.      (We assume the additional sublist(s) of
793          * the other list are not NIL --- no pathkey list should ever have a
794          * NIL sublist.)
795          */
796         if (key1 == NULL && key2 == NULL)
797                 return PATHKEYS_EQUAL;
798         if (key1 != NULL)
799                 return PATHKEYS_BETTER1;        /* key1 is longer */
800         return PATHKEYS_BETTER2;        /* key2 is longer */
801 }
802
803 /*
804  * compare_noncanonical_pathkeys
805  *        Compare two pathkeys to see if they are equivalent, and if not whether
806  *        one is "better" than the other.  This is used when we must compare
807  *        non-canonicalized pathkeys.
808  *
809  *        A pathkey can be considered better than another if it is a superset:
810  *        it contains all the keys of the other plus more.      For example, either
811  *        ((A) (B)) or ((A B)) is better than ((A)).
812  *
813  *        Currently, the only user of this routine is grouping_planner(),
814  *        and it will only pass single-element sublists (from
815  *        make_pathkeys_for_sortclauses).  Therefore we don't have to do the
816  *        full two-way-subset-inclusion test on each pair of sublists that is
817  *        implied by the above statement.  Instead we just verify they are
818  *        singleton lists and then do an equal().  This could be improved if
819  *        necessary.
820  */
821 PathKeysComparison
822 compare_noncanonical_pathkeys(List *keys1, List *keys2)
823 {
824         ListCell   *key1,
825                            *key2;
826
827         forboth(key1, keys1, key2, keys2)
828         {
829                 List       *subkey1 = (List *) lfirst(key1);
830                 List       *subkey2 = (List *) lfirst(key2);
831
832                 Assert(list_length(subkey1) == 1);
833                 Assert(list_length(subkey2) == 1);
834                 if (!equal(subkey1, subkey2))
835                         return PATHKEYS_DIFFERENT;      /* no need to keep looking */
836         }
837
838         /*
839          * If we reached the end of only one list, the other is longer and
840          * therefore not a subset.      (We assume the additional sublist(s) of
841          * the other list are not NIL --- no pathkey list should ever have a
842          * NIL sublist.)
843          */
844         if (key1 == NULL && key2 == NULL)
845                 return PATHKEYS_EQUAL;
846         if (key1 != NULL)
847                 return PATHKEYS_BETTER1;        /* key1 is longer */
848         return PATHKEYS_BETTER2;        /* key2 is longer */
849 }
850
851 /*
852  * pathkeys_contained_in
853  *        Common special case of compare_pathkeys: we just want to know
854  *        if keys2 are at least as well sorted as keys1.
855  */
856 bool
857 pathkeys_contained_in(List *keys1, List *keys2)
858 {
859         switch (compare_pathkeys(keys1, keys2))
860         {
861                 case PATHKEYS_EQUAL:
862                 case PATHKEYS_BETTER2:
863                         return true;
864                 default:
865                         break;
866         }
867         return false;
868 }
869
870 /*
871  * noncanonical_pathkeys_contained_in
872  *        The same, when we don't have canonical pathkeys.
873  */
874 bool
875 noncanonical_pathkeys_contained_in(List *keys1, List *keys2)
876 {
877         switch (compare_noncanonical_pathkeys(keys1, keys2))
878         {
879                 case PATHKEYS_EQUAL:
880                 case PATHKEYS_BETTER2:
881                         return true;
882                 default:
883                         break;
884         }
885         return false;
886 }
887
888 /*
889  * get_cheapest_path_for_pathkeys
890  *        Find the cheapest path (according to the specified criterion) that
891  *        satisfies the given pathkeys.  Return NULL if no such path.
892  *
893  * 'paths' is a list of possible paths that all generate the same relation
894  * 'pathkeys' represents a required ordering (already canonicalized!)
895  * 'cost_criterion' is STARTUP_COST or TOTAL_COST
896  */
897 Path *
898 get_cheapest_path_for_pathkeys(List *paths, List *pathkeys,
899                                                            CostSelector cost_criterion)
900 {
901         Path       *matched_path = NULL;
902         ListCell   *l;
903
904         foreach(l, paths)
905         {
906                 Path       *path = (Path *) lfirst(l);
907
908                 /*
909                  * Since cost comparison is a lot cheaper than pathkey comparison,
910                  * do that first.  (XXX is that still true?)
911                  */
912                 if (matched_path != NULL &&
913                         compare_path_costs(matched_path, path, cost_criterion) <= 0)
914                         continue;
915
916                 if (pathkeys_contained_in(pathkeys, path->pathkeys))
917                         matched_path = path;
918         }
919         return matched_path;
920 }
921
922 /*
923  * get_cheapest_fractional_path_for_pathkeys
924  *        Find the cheapest path (for retrieving a specified fraction of all
925  *        the tuples) that satisfies the given pathkeys.
926  *        Return NULL if no such path.
927  *
928  * See compare_fractional_path_costs() for the interpretation of the fraction
929  * parameter.
930  *
931  * 'paths' is a list of possible paths that all generate the same relation
932  * 'pathkeys' represents a required ordering (already canonicalized!)
933  * 'fraction' is the fraction of the total tuples expected to be retrieved
934  */
935 Path *
936 get_cheapest_fractional_path_for_pathkeys(List *paths,
937                                                                                   List *pathkeys,
938                                                                                   double fraction)
939 {
940         Path       *matched_path = NULL;
941         ListCell   *l;
942
943         foreach(l, paths)
944         {
945                 Path       *path = (Path *) lfirst(l);
946
947                 /*
948                  * Since cost comparison is a lot cheaper than pathkey comparison,
949                  * do that first.
950                  */
951                 if (matched_path != NULL &&
952                 compare_fractional_path_costs(matched_path, path, fraction) <= 0)
953                         continue;
954
955                 if (pathkeys_contained_in(pathkeys, path->pathkeys))
956                         matched_path = path;
957         }
958         return matched_path;
959 }
960
961 /****************************************************************************
962  *              NEW PATHKEY FORMATION
963  ****************************************************************************/
964
965 /*
966  * build_index_pathkeys
967  *        Build a pathkeys list that describes the ordering induced by an index
968  *        scan using the given index.  (Note that an unordered index doesn't
969  *        induce any ordering; such an index will have no sortop OIDS in
970  *        its "ordering" field, and we will return NIL.)
971  *
972  * If 'scandir' is BackwardScanDirection, attempt to build pathkeys
973  * representing a backwards scan of the index.  Return NIL if can't do it.
974  *
975  * We generate the full pathkeys list whether or not all are useful for the
976  * current query.  Caller should do truncate_useless_pathkeys().
977  */
978 List *
979 build_index_pathkeys(PlannerInfo *root,
980                                          IndexOptInfo *index,
981                                          ScanDirection scandir)
982 {
983         List       *retval = NIL;
984         int                *indexkeys = index->indexkeys;
985         Oid                *ordering = index->ordering;
986         ListCell   *indexprs_item = list_head(index->indexprs);
987
988         while (*ordering != InvalidOid)
989         {
990                 PathKeyItem *item;
991                 Oid                     sortop;
992                 Node       *indexkey;
993                 List       *cpathkey;
994
995                 sortop = *ordering;
996                 if (ScanDirectionIsBackward(scandir))
997                 {
998                         sortop = get_commutator(sortop);
999                         if (sortop == InvalidOid)
1000                                 break;                  /* oops, no reverse sort operator? */
1001                 }
1002
1003                 if (*indexkeys != 0)
1004                 {
1005                         /* simple index column */
1006                         indexkey = (Node *) find_indexkey_var(root, index->rel,
1007                                                                                                   *indexkeys);
1008                 }
1009                 else
1010                 {
1011                         /* expression --- assume we need not copy it */
1012                         if (indexprs_item == NULL)
1013                                 elog(ERROR, "wrong number of index expressions");
1014                         indexkey = (Node *) lfirst(indexprs_item);
1015                         indexprs_item = lnext(indexprs_item);
1016                 }
1017
1018                 /* OK, make a sublist for this sort key */
1019                 item = makePathKeyItem(indexkey, sortop, true);
1020                 cpathkey = make_canonical_pathkey(root, item);
1021
1022                 /*
1023                  * Eliminate redundant ordering info; could happen if query is
1024                  * such that index keys are equijoined...
1025                  */
1026                 retval = list_append_unique_ptr(retval, cpathkey);
1027
1028                 indexkeys++;
1029                 ordering++;
1030         }
1031
1032         return retval;
1033 }
1034
1035 /*
1036  * Find or make a Var node for the specified attribute of the rel.
1037  *
1038  * We first look for the var in the rel's target list, because that's
1039  * easy and fast.  But the var might not be there (this should normally
1040  * only happen for vars that are used in WHERE restriction clauses,
1041  * but not in join clauses or in the SELECT target list).  In that case,
1042  * gin up a Var node the hard way.
1043  */
1044 static Var *
1045 find_indexkey_var(PlannerInfo *root, RelOptInfo *rel, AttrNumber varattno)
1046 {
1047         ListCell   *temp;
1048         Index           relid;
1049         Oid                     reloid,
1050                                 vartypeid;
1051         int32           type_mod;
1052
1053         foreach(temp, rel->reltargetlist)
1054         {
1055                 Var                *var = (Var *) lfirst(temp);
1056
1057                 if (IsA(var, Var) &&
1058                         var->varattno == varattno)
1059                         return var;
1060         }
1061
1062         relid = rel->relid;
1063         reloid = getrelid(relid, root->parse->rtable);
1064         get_atttypetypmod(reloid, varattno, &vartypeid, &type_mod);
1065
1066         return makeVar(relid, varattno, vartypeid, type_mod, 0);
1067 }
1068
1069 /*
1070  * convert_subquery_pathkeys
1071  *        Build a pathkeys list that describes the ordering of a subquery's
1072  *        result, in the terms of the outer query.  This is essentially a
1073  *        task of conversion.
1074  *
1075  * 'rel': outer query's RelOptInfo for the subquery relation.
1076  * 'subquery_pathkeys': the subquery's output pathkeys, in its terms.
1077  *
1078  * It is not necessary for caller to do truncate_useless_pathkeys(),
1079  * because we select keys in a way that takes usefulness of the keys into
1080  * account.
1081  */
1082 List *
1083 convert_subquery_pathkeys(PlannerInfo *root, RelOptInfo *rel,
1084                                                   List *subquery_pathkeys)
1085 {
1086         List       *retval = NIL;
1087         int                     retvallen = 0;
1088         int                     outer_query_keys = list_length(root->query_pathkeys);
1089         List       *sub_tlist = rel->subplan->targetlist;
1090         ListCell   *i;
1091
1092         foreach(i, subquery_pathkeys)
1093         {
1094                 List       *sub_pathkey = (List *) lfirst(i);
1095                 ListCell   *j;
1096                 PathKeyItem *best_item = NULL;
1097                 int                     best_score = 0;
1098                 List       *cpathkey;
1099
1100                 /*
1101                  * The sub_pathkey could contain multiple elements (representing
1102                  * knowledge that multiple items are effectively equal).  Each
1103                  * element might match none, one, or more of the output columns
1104                  * that are visible to the outer query.  This means we may have
1105                  * multiple possible representations of the sub_pathkey in the
1106                  * context of the outer query.  Ideally we would generate them all
1107                  * and put them all into a pathkey list of the outer query,
1108                  * thereby propagating equality knowledge up to the outer query.
1109                  * Right now we cannot do so, because the outer query's canonical
1110                  * pathkey sets are already frozen when this is called.  Instead
1111                  * we prefer the one that has the highest "score" (number of
1112                  * canonical pathkey peers, plus one if it matches the outer
1113                  * query_pathkeys). This is the most likely to be useful in the
1114                  * outer query.
1115                  */
1116                 foreach(j, sub_pathkey)
1117                 {
1118                         PathKeyItem *sub_item = (PathKeyItem *) lfirst(j);
1119                         Node       *sub_key = sub_item->key;
1120                         ListCell   *k;
1121
1122                         foreach(k, sub_tlist)
1123                         {
1124                                 TargetEntry *tle = (TargetEntry *) lfirst(k);
1125
1126                                 if (!tle->resjunk &&
1127                                         equal(tle->expr, sub_key))
1128                                 {
1129                                         /* Found a representation for this sub_key */
1130                                         Var                *outer_var;
1131                                         PathKeyItem *outer_item;
1132                                         int                     score;
1133
1134                                         outer_var = makeVar(rel->relid,
1135                                                                                 tle->resno,
1136                                                                                 exprType((Node *) tle->expr),
1137                                                                                 exprTypmod((Node *) tle->expr),
1138                                                                                 0);
1139                                         outer_item = makePathKeyItem((Node *) outer_var,
1140                                                                                                  sub_item->sortop,
1141                                                                                                  true);
1142                                         /* score = # of mergejoin peers */
1143                                         score = count_canonical_peers(root, outer_item);
1144                                         /* +1 if it matches the proper query_pathkeys item */
1145                                         if (retvallen < outer_query_keys &&
1146                                                 list_member(list_nth(root->query_pathkeys, retvallen), outer_item))
1147                                                 score++;
1148                                         if (score > best_score)
1149                                         {
1150                                                 best_item = outer_item;
1151                                                 best_score = score;
1152                                         }
1153                                 }
1154                         }
1155                 }
1156
1157                 /*
1158                  * If we couldn't find a representation of this sub_pathkey, we're
1159                  * done (we can't use the ones to its right, either).
1160                  */
1161                 if (!best_item)
1162                         break;
1163
1164                 /* Canonicalize the chosen item (we did not before) */
1165                 cpathkey = make_canonical_pathkey(root, best_item);
1166
1167                 /*
1168                  * Eliminate redundant ordering info; could happen if outer query
1169                  * equijoins subquery keys...
1170                  */
1171                 if (!list_member_ptr(retval, cpathkey))
1172                 {
1173                         retval = lappend(retval, cpathkey);
1174                         retvallen++;
1175                 }
1176         }
1177
1178         return retval;
1179 }
1180
1181 /*
1182  * build_join_pathkeys
1183  *        Build the path keys for a join relation constructed by mergejoin or
1184  *        nestloop join.  These keys should include all the path key vars of the
1185  *        outer path (since the join will retain the ordering of the outer path)
1186  *        plus any vars of the inner path that are equijoined to the outer vars.
1187  *
1188  *        Per the discussion in backend/optimizer/README, equijoined inner vars
1189  *        can be considered path keys of the result, just the same as the outer
1190  *        vars they were joined with; furthermore, it doesn't matter what kind
1191  *        of join algorithm is actually used.
1192  *
1193  *        EXCEPTION: in a FULL or RIGHT join, we cannot treat the result as
1194  *        having the outer path's path keys, because null lefthand rows may be
1195  *        inserted at random points.  It must be treated as unsorted.
1196  *
1197  * 'joinrel' is the join relation that paths are being formed for
1198  * 'jointype' is the join type (inner, left, full, etc)
1199  * 'outer_pathkeys' is the list of the current outer path's path keys
1200  *
1201  * Returns the list of new path keys.
1202  */
1203 List *
1204 build_join_pathkeys(PlannerInfo *root,
1205                                         RelOptInfo *joinrel,
1206                                         JoinType jointype,
1207                                         List *outer_pathkeys)
1208 {
1209         if (jointype == JOIN_FULL || jointype == JOIN_RIGHT)
1210                 return NIL;
1211
1212         /*
1213          * This used to be quite a complex bit of code, but now that all
1214          * pathkey sublists start out life canonicalized, we don't have to do
1215          * a darn thing here!  The inner-rel vars we used to need to add are
1216          * *already* part of the outer pathkey!
1217          *
1218          * We do, however, need to truncate the pathkeys list, since it may
1219          * contain pathkeys that were useful for forming this joinrel but are
1220          * uninteresting to higher levels.
1221          */
1222         return truncate_useless_pathkeys(root, joinrel, outer_pathkeys);
1223 }
1224
1225 /****************************************************************************
1226  *              PATHKEYS AND SORT CLAUSES
1227  ****************************************************************************/
1228
1229 /*
1230  * make_pathkeys_for_sortclauses
1231  *              Generate a pathkeys list that represents the sort order specified
1232  *              by a list of SortClauses (GroupClauses will work too!)
1233  *
1234  * NB: the result is NOT in canonical form, but must be passed through
1235  * canonicalize_pathkeys() before it can be used for comparisons or
1236  * labeling relation sort orders.  (We do things this way because
1237  * grouping_planner needs to be able to construct requested pathkeys
1238  * before the pathkey equivalence sets have been created for the query.)
1239  *
1240  * 'sortclauses' is a list of SortClause or GroupClause nodes
1241  * 'tlist' is the targetlist to find the referenced tlist entries in
1242  */
1243 List *
1244 make_pathkeys_for_sortclauses(List *sortclauses,
1245                                                           List *tlist)
1246 {
1247         List       *pathkeys = NIL;
1248         ListCell   *l;
1249
1250         foreach(l, sortclauses)
1251         {
1252                 SortClause *sortcl = (SortClause *) lfirst(l);
1253                 Node       *sortkey;
1254                 PathKeyItem *pathkey;
1255
1256                 sortkey = get_sortgroupclause_expr(sortcl, tlist);
1257                 pathkey = makePathKeyItem(sortkey, sortcl->sortop, true);
1258
1259                 /*
1260                  * The pathkey becomes a one-element sublist, for now;
1261                  * canonicalize_pathkeys() might replace it with a longer sublist
1262                  * later.
1263                  */
1264                 pathkeys = lappend(pathkeys, list_make1(pathkey));
1265         }
1266         return pathkeys;
1267 }
1268
1269 /****************************************************************************
1270  *              PATHKEYS AND MERGECLAUSES
1271  ****************************************************************************/
1272
1273 /*
1274  * cache_mergeclause_pathkeys
1275  *              Make the cached pathkeys valid in a mergeclause restrictinfo.
1276  *
1277  * RestrictInfo contains fields in which we may cache the result
1278  * of looking up the canonical pathkeys for the left and right sides
1279  * of the mergeclause.  (Note that in normal cases they will be the
1280  * same, but not if the mergeclause appears above an OUTER JOIN.)
1281  * This is a worthwhile savings because these routines will be invoked
1282  * many times when dealing with a many-relation query.
1283  *
1284  * We have to be careful that the cached values are palloc'd in the same
1285  * context the RestrictInfo node itself is in.  This is not currently a
1286  * problem for normal planning, but it is an issue for GEQO planning.
1287  */
1288 void
1289 cache_mergeclause_pathkeys(PlannerInfo *root, RestrictInfo *restrictinfo)
1290 {
1291         Node       *key;
1292         PathKeyItem *item;
1293         MemoryContext oldcontext;
1294
1295         Assert(restrictinfo->mergejoinoperator != InvalidOid);
1296
1297         if (restrictinfo->left_pathkey == NIL)
1298         {
1299                 oldcontext = MemoryContextSwitchTo(GetMemoryChunkContext(restrictinfo));
1300                 key = get_leftop(restrictinfo->clause);
1301                 item = makePathKeyItem(key, restrictinfo->left_sortop, false);
1302                 restrictinfo->left_pathkey = make_canonical_pathkey(root, item);
1303                 MemoryContextSwitchTo(oldcontext);
1304         }
1305         if (restrictinfo->right_pathkey == NIL)
1306         {
1307                 oldcontext = MemoryContextSwitchTo(GetMemoryChunkContext(restrictinfo));
1308                 key = get_rightop(restrictinfo->clause);
1309                 item = makePathKeyItem(key, restrictinfo->right_sortop, false);
1310                 restrictinfo->right_pathkey = make_canonical_pathkey(root, item);
1311                 MemoryContextSwitchTo(oldcontext);
1312         }
1313 }
1314
1315 /*
1316  * find_mergeclauses_for_pathkeys
1317  *        This routine attempts to find a set of mergeclauses that can be
1318  *        used with a specified ordering for one of the input relations.
1319  *        If successful, it returns a list of mergeclauses.
1320  *
1321  * 'pathkeys' is a pathkeys list showing the ordering of an input path.
1322  *                      It doesn't matter whether it is for the inner or outer path.
1323  * 'restrictinfos' is a list of mergejoinable restriction clauses for the
1324  *                      join relation being formed.
1325  *
1326  * The result is NIL if no merge can be done, else a maximal list of
1327  * usable mergeclauses (represented as a list of their restrictinfo nodes).
1328  *
1329  * XXX Ideally we ought to be considering context, ie what path orderings
1330  * are available on the other side of the join, rather than just making
1331  * an arbitrary choice among the mergeclauses that will work for this side
1332  * of the join.
1333  */
1334 List *
1335 find_mergeclauses_for_pathkeys(PlannerInfo *root,
1336                                                            List *pathkeys,
1337                                                            List *restrictinfos)
1338 {
1339         List       *mergeclauses = NIL;
1340         ListCell   *i;
1341
1342         /* make sure we have pathkeys cached in the clauses */
1343         foreach(i, restrictinfos)
1344         {
1345                 RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(i);
1346
1347                 cache_mergeclause_pathkeys(root, restrictinfo);
1348         }
1349
1350         foreach(i, pathkeys)
1351         {
1352                 List       *pathkey = (List *) lfirst(i);
1353                 List       *matched_restrictinfos = NIL;
1354                 ListCell   *j;
1355
1356                 /*
1357                  * We can match a pathkey against either left or right side of any
1358                  * mergejoin clause.  (We examine both sides since we aren't told
1359                  * if the given pathkeys are for inner or outer input path; no
1360                  * confusion is possible.)      Furthermore, if there are multiple
1361                  * matching clauses, take them all.  In plain inner-join scenarios
1362                  * we expect only one match, because redundant-mergeclause
1363                  * elimination will have removed any redundant mergeclauses from
1364                  * the input list. However, in outer-join scenarios there might be
1365                  * multiple matches. An example is
1366                  *
1367                  * select * from a full join b on a.v1 = b.v1 and a.v2 = b.v2 and
1368                  * a.v1 = b.v2;
1369                  *
1370                  * Given the pathkeys ((a.v1), (a.v2)) it is okay to return all three
1371                  * clauses (in the order a.v1=b.v1, a.v1=b.v2, a.v2=b.v2) and
1372                  * indeed we *must* do so or we will be unable to form a valid
1373                  * plan.
1374                  */
1375                 foreach(j, restrictinfos)
1376                 {
1377                         RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(j);
1378
1379                         /*
1380                          * We can compare canonical pathkey sublists by simple pointer
1381                          * equality; see compare_pathkeys.
1382                          */
1383                         if ((pathkey == restrictinfo->left_pathkey ||
1384                                  pathkey == restrictinfo->right_pathkey) &&
1385                                 !list_member_ptr(mergeclauses, restrictinfo))
1386                         {
1387                                 matched_restrictinfos = lappend(matched_restrictinfos,
1388                                                                                                 restrictinfo);
1389                         }
1390                 }
1391
1392                 /*
1393                  * If we didn't find a mergeclause, we're done --- any additional
1394                  * sort-key positions in the pathkeys are useless.      (But we can
1395                  * still mergejoin if we found at least one mergeclause.)
1396                  */
1397                 if (matched_restrictinfos == NIL)
1398                         break;
1399
1400                 /*
1401                  * If we did find usable mergeclause(s) for this sort-key
1402                  * position, add them to result list.
1403                  */
1404                 mergeclauses = list_concat(mergeclauses, matched_restrictinfos);
1405         }
1406
1407         return mergeclauses;
1408 }
1409
1410 /*
1411  * make_pathkeys_for_mergeclauses
1412  *        Builds a pathkey list representing the explicit sort order that
1413  *        must be applied to a path in order to make it usable for the
1414  *        given mergeclauses.
1415  *
1416  * 'mergeclauses' is a list of RestrictInfos for mergejoin clauses
1417  *                      that will be used in a merge join.
1418  * 'rel' is the relation the pathkeys will apply to (ie, either the inner
1419  *                      or outer side of the proposed join rel).
1420  *
1421  * Returns a pathkeys list that can be applied to the indicated relation.
1422  *
1423  * Note that it is not this routine's job to decide whether sorting is
1424  * actually needed for a particular input path.  Assume a sort is necessary;
1425  * just make the keys, eh?
1426  */
1427 List *
1428 make_pathkeys_for_mergeclauses(PlannerInfo *root,
1429                                                            List *mergeclauses,
1430                                                            RelOptInfo *rel)
1431 {
1432         List       *pathkeys = NIL;
1433         ListCell   *l;
1434
1435         foreach(l, mergeclauses)
1436         {
1437                 RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(l);
1438                 List       *pathkey;
1439
1440                 cache_mergeclause_pathkeys(root, restrictinfo);
1441
1442                 if (bms_is_subset(restrictinfo->left_relids, rel->relids))
1443                 {
1444                         /* Rel is left side of mergeclause */
1445                         pathkey = restrictinfo->left_pathkey;
1446                 }
1447                 else if (bms_is_subset(restrictinfo->right_relids, rel->relids))
1448                 {
1449                         /* Rel is right side of mergeclause */
1450                         pathkey = restrictinfo->right_pathkey;
1451                 }
1452                 else
1453                 {
1454                         elog(ERROR, "could not identify which side of mergeclause to use");
1455                         pathkey = NIL;          /* keep compiler quiet */
1456                 }
1457
1458                 /*
1459                  * When we are given multiple merge clauses, it's possible that
1460                  * some clauses refer to the same vars as earlier clauses. There's
1461                  * no reason for us to specify sort keys like (A,B,A) when (A,B)
1462                  * will do --- and adding redundant sort keys makes add_path think
1463                  * that this sort order is different from ones that are really the
1464                  * same, so don't do it.  Since we now have a canonicalized
1465                  * pathkey, a simple ptrMember test is sufficient to detect
1466                  * redundant keys.
1467                  */
1468                 pathkeys = list_append_unique_ptr(pathkeys, pathkey);
1469         }
1470
1471         return pathkeys;
1472 }
1473
1474 /****************************************************************************
1475  *              PATHKEY USEFULNESS CHECKS
1476  *
1477  * We only want to remember as many of the pathkeys of a path as have some
1478  * potential use, either for subsequent mergejoins or for meeting the query's
1479  * requested output ordering.  This ensures that add_path() won't consider
1480  * a path to have a usefully different ordering unless it really is useful.
1481  * These routines check for usefulness of given pathkeys.
1482  ****************************************************************************/
1483
1484 /*
1485  * pathkeys_useful_for_merging
1486  *              Count the number of pathkeys that may be useful for mergejoins
1487  *              above the given relation (by looking at its joininfo list).
1488  *
1489  * We consider a pathkey potentially useful if it corresponds to the merge
1490  * ordering of either side of any joinclause for the rel.  This might be
1491  * overoptimistic, since joinclauses that require different other relations
1492  * might never be usable at the same time, but trying to be exact is likely
1493  * to be more trouble than it's worth.
1494  */
1495 int
1496 pathkeys_useful_for_merging(PlannerInfo *root, RelOptInfo *rel, List *pathkeys)
1497 {
1498         int                     useful = 0;
1499         ListCell   *i;
1500
1501         foreach(i, pathkeys)
1502         {
1503                 List       *pathkey = (List *) lfirst(i);
1504                 bool            matched = false;
1505                 ListCell   *j;
1506
1507                 foreach(j, rel->joininfo)
1508                 {
1509                         RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(j);
1510
1511                         if (restrictinfo->mergejoinoperator == InvalidOid)
1512                                 continue;
1513                         cache_mergeclause_pathkeys(root, restrictinfo);
1514
1515                         /*
1516                          * We can compare canonical pathkey sublists by simple
1517                          * pointer equality; see compare_pathkeys.
1518                          */
1519                         if (pathkey == restrictinfo->left_pathkey ||
1520                                 pathkey == restrictinfo->right_pathkey)
1521                         {
1522                                 matched = true;
1523                                 break;
1524                         }
1525                 }
1526
1527                 /*
1528                  * If we didn't find a mergeclause, we're done --- any additional
1529                  * sort-key positions in the pathkeys are useless.      (But we can
1530                  * still mergejoin if we found at least one mergeclause.)
1531                  */
1532                 if (matched)
1533                         useful++;
1534                 else
1535                         break;
1536         }
1537
1538         return useful;
1539 }
1540
1541 /*
1542  * pathkeys_useful_for_ordering
1543  *              Count the number of pathkeys that are useful for meeting the
1544  *              query's requested output ordering.
1545  *
1546  * Unlike merge pathkeys, this is an all-or-nothing affair: it does us
1547  * no good to order by just the first key(s) of the requested ordering.
1548  * So the result is always either 0 or list_length(root->query_pathkeys).
1549  */
1550 int
1551 pathkeys_useful_for_ordering(PlannerInfo *root, List *pathkeys)
1552 {
1553         if (root->query_pathkeys == NIL)
1554                 return 0;                               /* no special ordering requested */
1555
1556         if (pathkeys == NIL)
1557                 return 0;                               /* unordered path */
1558
1559         if (pathkeys_contained_in(root->query_pathkeys, pathkeys))
1560         {
1561                 /* It's useful ... or at least the first N keys are */
1562                 return list_length(root->query_pathkeys);
1563         }
1564
1565         return 0;                                       /* path ordering not useful */
1566 }
1567
1568 /*
1569  * truncate_useless_pathkeys
1570  *              Shorten the given pathkey list to just the useful pathkeys.
1571  */
1572 List *
1573 truncate_useless_pathkeys(PlannerInfo *root,
1574                                                   RelOptInfo *rel,
1575                                                   List *pathkeys)
1576 {
1577         int                     nuseful;
1578         int                     nuseful2;
1579
1580         nuseful = pathkeys_useful_for_merging(root, rel, pathkeys);
1581         nuseful2 = pathkeys_useful_for_ordering(root, pathkeys);
1582         if (nuseful2 > nuseful)
1583                 nuseful = nuseful2;
1584
1585         /*
1586          * Note: not safe to modify input list destructively, but we can avoid
1587          * copying the list if we're not actually going to change it
1588          */
1589         if (nuseful == list_length(pathkeys))
1590                 return pathkeys;
1591         else
1592                 return list_truncate(list_copy(pathkeys), nuseful);
1593 }