]> granicus.if.org Git - postgresql/blob - src/backend/optimizer/plan/initsplan.c
4c7e281866bc236d76b25790cbd8221a5bb1a7c6
[postgresql] / src / backend / optimizer / plan / initsplan.c
1 /*-------------------------------------------------------------------------
2  *
3  * initsplan.c
4  *        Target list, qualification, joininfo initialization routines
5  *
6  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $Header: /cvsroot/pgsql/src/backend/optimizer/plan/initsplan.c,v 1.65 2001/10/25 05:49:33 momjian Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16
17 #include <sys/types.h>
18
19 #include "catalog/pg_operator.h"
20 #include "catalog/pg_type.h"
21 #include "nodes/makefuncs.h"
22 #include "optimizer/clauses.h"
23 #include "optimizer/cost.h"
24 #include "optimizer/joininfo.h"
25 #include "optimizer/pathnode.h"
26 #include "optimizer/paths.h"
27 #include "optimizer/planmain.h"
28 #include "optimizer/tlist.h"
29 #include "optimizer/var.h"
30 #include "parser/parsetree.h"
31 #include "parser/parse_expr.h"
32 #include "parser/parse_oper.h"
33 #include "parser/parse_type.h"
34 #include "utils/lsyscache.h"
35 #include "utils/syscache.h"
36
37
38 static void mark_baserels_for_outer_join(Query *root, Relids rels,
39                                                          Relids outerrels);
40 static void distribute_qual_to_rels(Query *root, Node *clause,
41                                                 bool ispusheddown,
42                                                 bool isouterjoin,
43                                                 bool isdeduced,
44                                                 Relids qualscope);
45 static void add_join_info_to_rels(Query *root, RestrictInfo *restrictinfo,
46                                           Relids join_relids);
47 static void add_vars_to_targetlist(Query *root, List *vars);
48 static bool qual_is_redundant(Query *root, RestrictInfo *restrictinfo,
49                                   List *restrictlist);
50 static void check_mergejoinable(RestrictInfo *restrictinfo);
51 static void check_hashjoinable(RestrictInfo *restrictinfo);
52
53
54 /*****************************************************************************
55  *
56  *       TARGET LISTS
57  *
58  *****************************************************************************/
59
60 /*
61  * build_base_rel_tlists
62  *        Creates rel nodes for every relation mentioned in the target list
63  *        'tlist' (if a node hasn't already been created) and adds them to
64  *        root->base_rel_list.  Creates targetlist entries for each var seen
65  *        in 'tlist' and adds them to the tlist of the appropriate rel node.
66  */
67 void
68 build_base_rel_tlists(Query *root, List *tlist)
69 {
70         List       *tlist_vars = pull_var_clause((Node *) tlist, false);
71
72         add_vars_to_targetlist(root, tlist_vars);
73         freeList(tlist_vars);
74 }
75
76 /*
77  * add_vars_to_targetlist
78  *        For each variable appearing in the list, add it to the relation's
79  *        targetlist if not already present.  Corresponding base rel nodes
80  *        will be created if not already present.
81  */
82 static void
83 add_vars_to_targetlist(Query *root, List *vars)
84 {
85         List       *temp;
86
87         foreach(temp, vars)
88         {
89                 Var                *var = (Var *) lfirst(temp);
90                 RelOptInfo *rel = build_base_rel(root, var->varno);
91
92                 add_var_to_tlist(rel, var);
93         }
94 }
95
96 /*----------
97  * add_missing_rels_to_query
98  *
99  *        If we have a relation listed in the join tree that does not appear
100  *        in the target list nor qualifications, we must add it to the base
101  *        relation list so that it can be processed.  For instance,
102  *                      select count(*) from foo;
103  *        would fail to scan foo if this routine were not called.  More subtly,
104  *                      select f.x from foo f, foo f2
105  *        is a join of f and f2.  Note that if we have
106  *                      select foo.x from foo f
107  *        this also gets turned into a join (between foo as foo and foo as f).
108  *
109  *        Returns a list of all the base relations (RelOptInfo nodes) that appear
110  *        in the join tree.  This list can be used for cross-checking in the
111  *        reverse direction, ie, that we have a join tree entry for every
112  *        relation used in the query.
113  *----------
114  */
115 List *
116 add_missing_rels_to_query(Query *root, Node *jtnode)
117 {
118         List       *result = NIL;
119
120         if (jtnode == NULL)
121                 return NIL;
122         if (IsA(jtnode, RangeTblRef))
123         {
124                 int                     varno = ((RangeTblRef *) jtnode)->rtindex;
125
126                 /* This call to build_base_rel does the primary work... */
127                 RelOptInfo *rel = build_base_rel(root, varno);
128
129                 result = makeList1(rel);
130         }
131         else if (IsA(jtnode, FromExpr))
132         {
133                 FromExpr   *f = (FromExpr *) jtnode;
134                 List       *l;
135
136                 foreach(l, f->fromlist)
137                 {
138                         result = nconc(result,
139                                                    add_missing_rels_to_query(root, lfirst(l)));
140                 }
141         }
142         else if (IsA(jtnode, JoinExpr))
143         {
144                 JoinExpr   *j = (JoinExpr *) jtnode;
145
146                 result = add_missing_rels_to_query(root, j->larg);
147                 result = nconc(result,
148                                            add_missing_rels_to_query(root, j->rarg));
149         }
150         else
151                 elog(ERROR, "add_missing_rels_to_query: unexpected node type %d",
152                          nodeTag(jtnode));
153         return result;
154 }
155
156
157 /*****************************************************************************
158  *
159  *        QUALIFICATIONS
160  *
161  *****************************************************************************/
162
163
164 /*
165  * distribute_quals_to_rels
166  *        Recursively scan the query's join tree for WHERE and JOIN/ON qual
167  *        clauses, and add these to the appropriate RestrictInfo and JoinInfo
168  *        lists belonging to base RelOptInfos.  New base rel entries are created
169  *        as needed.  Also, base RelOptInfos are marked with outerjoinset
170  *        information, to aid in proper positioning of qual clauses that appear
171  *        above outer joins.
172  *
173  * NOTE: when dealing with inner joins, it is appropriate to let a qual clause
174  * be evaluated at the lowest level where all the variables it mentions are
175  * available.  However, we cannot push a qual down into the nullable side(s)
176  * of an outer join since the qual might eliminate matching rows and cause a
177  * NULL row to be incorrectly emitted by the join.      Therefore, rels appearing
178  * within the nullable side(s) of an outer join are marked with
179  * outerjoinset = list of Relids used at the outer join node.
180  * This list will be added to the list of rels referenced by quals using such
181  * a rel, thereby forcing them up the join tree to the right level.
182  *
183  * To ease the calculation of these values, distribute_quals_to_rels() returns
184  * the list of Relids involved in its own level of join.  This is just an
185  * internal convenience; no outside callers pay attention to the result.
186  */
187 Relids
188 distribute_quals_to_rels(Query *root, Node *jtnode)
189 {
190         Relids          result = NIL;
191
192         if (jtnode == NULL)
193                 return result;
194         if (IsA(jtnode, RangeTblRef))
195         {
196                 int                     varno = ((RangeTblRef *) jtnode)->rtindex;
197
198                 /* No quals to deal with, just return correct result */
199                 result = makeListi1(varno);
200         }
201         else if (IsA(jtnode, FromExpr))
202         {
203                 FromExpr   *f = (FromExpr *) jtnode;
204                 List       *l;
205                 List       *qual;
206
207                 /*
208                  * First, recurse to handle child joins.
209                  *
210                  * Note: we assume it's impossible to see same RT index from more
211                  * than one subtree, so nconc() is OK rather than set_unioni().
212                  */
213                 foreach(l, f->fromlist)
214                 {
215                         result = nconc(result,
216                                                    distribute_quals_to_rels(root, lfirst(l)));
217                 }
218
219                 /*
220                  * Now process the top-level quals.  These are always marked as
221                  * "pushed down", since they clearly didn't come from a JOIN expr.
222                  */
223                 foreach(qual, (List *) f->quals)
224                         distribute_qual_to_rels(root, (Node *) lfirst(qual),
225                                                                         true, false, false, result);
226         }
227         else if (IsA(jtnode, JoinExpr))
228         {
229                 JoinExpr   *j = (JoinExpr *) jtnode;
230                 Relids          leftids,
231                                         rightids;
232                 bool            isouterjoin;
233                 List       *qual;
234
235                 /*
236                  * Order of operations here is subtle and critical.  First we
237                  * recurse to handle sub-JOINs.  Their join quals will be placed
238                  * without regard for whether this level is an outer join, which
239                  * is correct. Then, if we are an outer join, we mark baserels
240                  * contained within the nullable side(s) with our own rel list;
241                  * this will restrict placement of subsequent quals using those
242                  * rels, including our own quals and quals above us in the join
243                  * tree. Finally we place our own join quals.
244                  */
245                 leftids = distribute_quals_to_rels(root, j->larg);
246                 rightids = distribute_quals_to_rels(root, j->rarg);
247
248                 result = nconc(listCopy(leftids), rightids);
249
250                 isouterjoin = false;
251                 switch (j->jointype)
252                 {
253                         case JOIN_INNER:
254                                 /* Inner join adds no restrictions for quals */
255                                 break;
256                         case JOIN_LEFT:
257                                 mark_baserels_for_outer_join(root, rightids, result);
258                                 isouterjoin = true;
259                                 break;
260                         case JOIN_FULL:
261                                 mark_baserels_for_outer_join(root, result, result);
262                                 isouterjoin = true;
263                                 break;
264                         case JOIN_RIGHT:
265                                 mark_baserels_for_outer_join(root, leftids, result);
266                                 isouterjoin = true;
267                                 break;
268                         case JOIN_UNION:
269
270                                 /*
271                                  * This is where we fail if upper levels of planner
272                                  * haven't rewritten UNION JOIN as an Append ...
273                                  */
274                                 elog(ERROR, "UNION JOIN is not implemented yet");
275                                 break;
276                         default:
277                                 elog(ERROR,
278                                          "distribute_quals_to_rels: unsupported join type %d",
279                                          (int) j->jointype);
280                                 break;
281                 }
282
283                 foreach(qual, (List *) j->quals)
284                         distribute_qual_to_rels(root, (Node *) lfirst(qual),
285                                                                         false, isouterjoin, false, result);
286         }
287         else
288                 elog(ERROR, "distribute_quals_to_rels: unexpected node type %d",
289                          nodeTag(jtnode));
290         return result;
291 }
292
293 /*
294  * mark_baserels_for_outer_join
295  *        Mark all base rels listed in 'rels' as having the given outerjoinset.
296  */
297 static void
298 mark_baserels_for_outer_join(Query *root, Relids rels, Relids outerrels)
299 {
300         List       *relid;
301
302         foreach(relid, rels)
303         {
304                 int                     relno = lfirsti(relid);
305                 RelOptInfo *rel = build_base_rel(root, relno);
306
307                 /*
308                  * Since we do this bottom-up, any outer-rels previously marked
309                  * should be within the new outer join set.
310                  */
311                 Assert(is_subseti(rel->outerjoinset, outerrels));
312
313                 /*
314                  * Presently the executor cannot support FOR UPDATE marking of
315                  * rels appearing on the nullable side of an outer join. (It's
316                  * somewhat unclear what that would mean, anyway: what should we
317                  * mark when a result row is generated from no element of the
318                  * nullable relation?)  So, complain if target rel is FOR UPDATE.
319                  * It's sufficient to make this check once per rel, so do it only
320                  * if rel wasn't already known nullable.
321                  */
322                 if (rel->outerjoinset == NIL)
323                 {
324                         if (intMember(relno, root->rowMarks))
325                                 elog(ERROR, "SELECT FOR UPDATE cannot be applied to the nullable side of an OUTER JOIN");
326                 }
327
328                 rel->outerjoinset = outerrels;
329         }
330 }
331
332 /*
333  * distribute_qual_to_rels
334  *        Add clause information to either the 'RestrictInfo' or 'JoinInfo' field
335  *        (depending on whether the clause is a join) of each base relation
336  *        mentioned in the clause.      A RestrictInfo node is created and added to
337  *        the appropriate list for each rel.  Also, if the clause uses a
338  *        mergejoinable operator and is not an outer-join qual, enter the left-
339  *        and right-side expressions into the query's lists of equijoined vars.
340  *
341  * 'clause': the qual clause to be distributed
342  * 'ispusheddown': if TRUE, force the clause to be marked 'ispusheddown'
343  *              (this indicates the clause came from a FromExpr, not a JoinExpr)
344  * 'isouterjoin': TRUE if the qual came from an OUTER JOIN's ON-clause
345  * 'isdeduced': TRUE if the qual came from implied-equality deduction
346  * 'qualscope': list of baserels the qual's syntactic scope covers
347  *
348  * 'qualscope' identifies what level of JOIN the qual came from.  For a top
349  * level qual (WHERE qual), qualscope lists all baserel ids and in addition
350  * 'ispusheddown' will be TRUE.
351  */
352 static void
353 distribute_qual_to_rels(Query *root, Node *clause,
354                                                 bool ispusheddown,
355                                                 bool isouterjoin,
356                                                 bool isdeduced,
357                                                 Relids qualscope)
358 {
359         RestrictInfo *restrictinfo = makeNode(RestrictInfo);
360         Relids          relids;
361         List       *vars;
362         bool            can_be_equijoin;
363
364         restrictinfo->clause = (Expr *) clause;
365         restrictinfo->subclauseindices = NIL;
366         restrictinfo->eval_cost = -1;           /* not computed until needed */
367         restrictinfo->this_selec = -1;          /* not computed until needed */
368         restrictinfo->mergejoinoperator = InvalidOid;
369         restrictinfo->left_sortop = InvalidOid;
370         restrictinfo->right_sortop = InvalidOid;
371         restrictinfo->left_pathkey = NIL;       /* not computable yet */
372         restrictinfo->right_pathkey = NIL;
373         restrictinfo->hashjoinoperator = InvalidOid;
374         restrictinfo->left_bucketsize = -1; /* not computed until needed */
375         restrictinfo->right_bucketsize = -1;
376
377         /*
378          * Retrieve all relids and vars contained within the clause.
379          */
380         clause_get_relids_vars(clause, &relids, &vars);
381
382         /*
383          * Cross-check: clause should contain no relids not within its scope.
384          * Otherwise the parser messed up.
385          */
386         if (!is_subseti(relids, qualscope))
387                 elog(ERROR, "JOIN qualification may not refer to other relations");
388
389         /*
390          * If the clause is variable-free, we force it to be evaluated at its
391          * original syntactic level.  Note that this should not happen for
392          * top-level clauses, because query_planner() special-cases them.  But
393          * it will happen for variable-free JOIN/ON clauses.  We don't have to
394          * be real smart about such a case, we just have to be correct.
395          */
396         if (relids == NIL)
397                 relids = qualscope;
398
399         /*
400          * For an outer-join qual, pretend that the clause references all rels
401          * appearing within its syntactic scope, even if it really doesn't.
402          * This ensures that the clause will be evaluated exactly at the level
403          * of joining corresponding to the outer join.
404          *
405          * For a non-outer-join qual, we can evaluate the qual as soon as (1) we
406          * have all the rels it mentions, and (2) we are at or above any outer
407          * joins that can null any of these rels and are below the syntactic
408          * location of the given qual.  To enforce the latter, scan the base
409          * rels listed in relids, and merge their outer-join lists into the
410          * clause's own reference list.  At the time we are called, the
411          * outerjoinset list of each baserel will show exactly those outer
412          * joins that are below the qual in the join tree.
413          *
414          * If the qual came from implied-equality deduction, we can evaluate the
415          * qual at its natural semantic level.
416          *
417          */
418         if (isdeduced)
419         {
420                 Assert(sameseti(relids, qualscope));
421                 can_be_equijoin = true;
422         }
423         else if (isouterjoin)
424         {
425                 relids = qualscope;
426                 can_be_equijoin = false;
427         }
428         else
429         {
430                 Relids          newrelids = relids;
431                 List       *relid;
432
433                 /*
434                  * We rely on set_unioni to be nondestructive of its input
435                  * lists...
436                  */
437                 can_be_equijoin = true;
438                 foreach(relid, relids)
439                 {
440                         RelOptInfo *rel = build_base_rel(root, lfirsti(relid));
441
442                         if (rel->outerjoinset &&
443                                 !is_subseti(rel->outerjoinset, relids))
444                         {
445                                 newrelids = set_unioni(newrelids, rel->outerjoinset);
446
447                                 /*
448                                  * Because application of the qual will be delayed by
449                                  * outer join, we mustn't assume its vars are equal
450                                  * everywhere.
451                                  */
452                                 can_be_equijoin = false;
453                         }
454                 }
455                 relids = newrelids;
456                 /* Should still be a subset of current scope ... */
457                 Assert(is_subseti(relids, qualscope));
458         }
459
460         /*
461          * Mark the qual as "pushed down" if it can be applied at a level
462          * below its original syntactic level.  This allows us to distinguish
463          * original JOIN/ON quals from higher-level quals pushed down to the
464          * same joinrel. A qual originating from WHERE is always considered
465          * "pushed down".
466          */
467         restrictinfo->ispusheddown = ispusheddown || !sameseti(relids,
468                                                                                                                    qualscope);
469
470         if (length(relids) == 1)
471         {
472                 /*
473                  * There is only one relation participating in 'clause', so
474                  * 'clause' is a restriction clause for that relation.
475                  */
476                 RelOptInfo *rel = build_base_rel(root, lfirsti(relids));
477
478                 /*
479                  * Check for a "mergejoinable" clause even though it's not a join
480                  * clause.      This is so that we can recognize that "a.x = a.y"
481                  * makes x and y eligible to be considered equal, even when they
482                  * belong to the same rel.      Without this, we would not recognize
483                  * that "a.x = a.y AND a.x = b.z AND a.y = c.q" allows us to
484                  * consider z and q equal after their rels are joined.
485                  */
486                 if (can_be_equijoin)
487                         check_mergejoinable(restrictinfo);
488
489                 /*
490                  * If the clause was deduced from implied equality, check to see
491                  * whether it is redundant with restriction clauses we already
492                  * have for this rel.  Note we cannot apply this check to
493                  * user-written clauses, since we haven't found the canonical
494                  * pathkey sets yet while processing user clauses.      (NB: no
495                  * comparable check is done in the join-clause case; redundancy
496                  * will be detected when the join clause is moved into a join
497                  * rel's restriction list.)
498                  */
499                 if (!isdeduced ||
500                         !qual_is_redundant(root, restrictinfo, rel->baserestrictinfo))
501                 {
502                         /* Add clause to rel's restriction list */
503                         rel->baserestrictinfo = lappend(rel->baserestrictinfo,
504                                                                                         restrictinfo);
505                 }
506         }
507         else if (relids != NIL)
508         {
509                 /*
510                  * 'clause' is a join clause, since there is more than one rel in
511                  * the relid list.      Set additional RestrictInfo fields for
512                  * joining.
513                  *
514                  * We don't bother setting the merge/hashjoin info if we're not going
515                  * to need it.  We do want to know about mergejoinable ops in any
516                  * potential equijoin clause (see later in this routine), and we
517                  * ignore enable_mergejoin if isouterjoin is true, because
518                  * mergejoin is the only implementation we have for full and right
519                  * outer joins.
520                  */
521                 if (enable_mergejoin || isouterjoin || can_be_equijoin)
522                         check_mergejoinable(restrictinfo);
523                 if (enable_hashjoin)
524                         check_hashjoinable(restrictinfo);
525
526                 /*
527                  * Add clause to the join lists of all the relevant relations.
528                  */
529                 add_join_info_to_rels(root, restrictinfo, relids);
530
531                 /*
532                  * Add vars used in the join clause to targetlists of their
533                  * relations, so that they will be emitted by the plan nodes that
534                  * scan those relations (else they won't be available at the join
535                  * node!).
536                  */
537                 add_vars_to_targetlist(root, vars);
538         }
539         else
540         {
541                 /*
542                  * 'clause' references no rels, and therefore we have no place to
543                  * attach it.  Shouldn't get here if callers are working properly.
544                  */
545                 elog(ERROR, "distribute_qual_to_rels: can't cope with variable-free clause");
546         }
547
548         /*
549          * If the clause has a mergejoinable operator, and is not an
550          * outer-join qualification nor bubbled up due to an outer join, then
551          * the two sides represent equivalent PathKeyItems for path keys: any
552          * path that is sorted by one side will also be sorted by the other
553          * (as soon as the two rels are joined, that is).  Record the key
554          * equivalence for future use.  (We can skip this for a deduced
555          * clause, since the keys are already known equivalent in that case.)
556          */
557         if (can_be_equijoin && restrictinfo->mergejoinoperator != InvalidOid &&
558                 !isdeduced)
559                 add_equijoined_keys(root, restrictinfo);
560 }
561
562 /*
563  * add_join_info_to_rels
564  *        For every relation participating in a join clause, add 'restrictinfo' to
565  *        the appropriate joininfo list (creating a new list and adding it to the
566  *        appropriate rel node if necessary).
567  *
568  * 'restrictinfo' describes the join clause
569  * 'join_relids' is the list of relations participating in the join clause
570  */
571 static void
572 add_join_info_to_rels(Query *root, RestrictInfo *restrictinfo,
573                                           Relids join_relids)
574 {
575         List       *join_relid;
576
577         /* For every relid, find the joininfo, and add the proper join entries */
578         foreach(join_relid, join_relids)
579         {
580                 int                     cur_relid = lfirsti(join_relid);
581                 Relids          unjoined_relids = NIL;
582                 JoinInfo   *joininfo;
583                 List       *otherrel;
584
585                 /* Get the relids not equal to the current relid */
586                 foreach(otherrel, join_relids)
587                 {
588                         if (lfirsti(otherrel) != cur_relid)
589                                 unjoined_relids = lappendi(unjoined_relids, lfirsti(otherrel));
590                 }
591
592                 /*
593                  * Find or make the joininfo node for this combination of rels,
594                  * and add the restrictinfo node to it.
595                  */
596                 joininfo = find_joininfo_node(build_base_rel(root, cur_relid),
597                                                                           unjoined_relids);
598                 joininfo->jinfo_restrictinfo = lappend(joininfo->jinfo_restrictinfo,
599                                                                                            restrictinfo);
600         }
601 }
602
603 /*
604  * process_implied_equality
605  *        Check to see whether we already have a restrictinfo item that says
606  *        item1 = item2, and create one if not.  This is a consequence of
607  *        transitivity of mergejoin equality: if we have mergejoinable
608  *        clauses A = B and B = C, we can deduce A = C (where = is an
609  *        appropriate mergejoinable operator).
610  */
611 void
612 process_implied_equality(Query *root, Node *item1, Node *item2,
613                                                  Oid sortop1, Oid sortop2)
614 {
615         Index           irel1;
616         Index           irel2;
617         RelOptInfo *rel1;
618         List       *restrictlist;
619         List       *itm;
620         Oid                     ltype,
621                                 rtype;
622         Operator        eq_operator;
623         Form_pg_operator pgopform;
624         Expr       *clause;
625
626         /*
627          * Currently, since check_mergejoinable only accepts Var = Var
628          * clauses, we should only see Var nodes here.  Would have to work a
629          * little harder to locate the right rel(s) if more-general mergejoin
630          * clauses were accepted.
631          */
632         Assert(IsA(item1, Var));
633         irel1 = ((Var *) item1)->varno;
634         Assert(IsA(item2, Var));
635         irel2 = ((Var *) item2)->varno;
636
637         /*
638          * If both vars belong to same rel, we need to look at that rel's
639          * baserestrictinfo list.  If different rels, each will have a
640          * joininfo node for the other, and we can scan either list.
641          *
642          * All baserel entries should already exist at this point, so use
643          * find_base_rel not build_base_rel.
644          */
645         rel1 = find_base_rel(root, irel1);
646         if (irel1 == irel2)
647                 restrictlist = rel1->baserestrictinfo;
648         else
649         {
650                 JoinInfo   *joininfo = find_joininfo_node(rel1,
651                                                                                                   makeListi1(irel2));
652
653                 restrictlist = joininfo->jinfo_restrictinfo;
654         }
655
656         /*
657          * Scan to see if equality is already known.
658          */
659         foreach(itm, restrictlist)
660         {
661                 RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(itm);
662                 Node       *left,
663                                    *right;
664
665                 if (restrictinfo->mergejoinoperator == InvalidOid)
666                         continue;                       /* ignore non-mergejoinable clauses */
667                 /* We now know the restrictinfo clause is a binary opclause */
668                 left = (Node *) get_leftop(restrictinfo->clause);
669                 right = (Node *) get_rightop(restrictinfo->clause);
670                 if ((equal(item1, left) && equal(item2, right)) ||
671                         (equal(item2, left) && equal(item1, right)))
672                         return;                         /* found a matching clause */
673         }
674
675         /*
676          * This equality is new information, so construct a clause
677          * representing it to add to the query data structures.
678          */
679         ltype = exprType(item1);
680         rtype = exprType(item2);
681         eq_operator = compatible_oper("=", ltype, rtype, true);
682         if (!HeapTupleIsValid(eq_operator))
683         {
684                 /*
685                  * Would it be safe to just not add the equality to the query if
686                  * we have no suitable equality operator for the combination of
687                  * datatypes?  NO, because sortkey selection may screw up anyway.
688                  */
689                 elog(ERROR, "Unable to identify an equality operator for types '%s' and '%s'",
690                          typeidTypeName(ltype), typeidTypeName(rtype));
691         }
692         pgopform = (Form_pg_operator) GETSTRUCT(eq_operator);
693
694         /*
695          * Let's just make sure this appears to be a compatible operator.
696          */
697         if (pgopform->oprlsortop != sortop1 ||
698                 pgopform->oprrsortop != sortop2 ||
699                 pgopform->oprresult != BOOLOID)
700                 elog(ERROR, "Equality operator for types '%s' and '%s' should be mergejoinable, but isn't",
701                          typeidTypeName(ltype), typeidTypeName(rtype));
702
703         clause = makeNode(Expr);
704         clause->typeOid = BOOLOID;
705         clause->opType = OP_EXPR;
706         clause->oper = (Node *) makeOper(oprid(eq_operator),            /* opno */
707                                                                          InvalidOid,            /* opid */
708                                                                          BOOLOID);      /* operator result type */
709         clause->args = makeList2(item1, item2);
710
711         ReleaseSysCache(eq_operator);
712
713         /*
714          * Note: we mark the qual "pushed down" to ensure that it can never be
715          * taken for an original JOIN/ON clause.
716          */
717         distribute_qual_to_rels(root, (Node *) clause,
718                                                         true, false, true,
719                                                         pull_varnos((Node *) clause));
720 }
721
722 /*
723  * qual_is_redundant
724  *        Detect whether an implied-equality qual that turns out to be a
725  *        restriction clause for a single base relation is redundant with
726  *        already-known restriction clauses for that rel.  This occurs with,
727  *        for example,
728  *                              SELECT * FROM tab WHERE f1 = f2 AND f2 = f3;
729  *        We need to suppress the redundant condition to avoid computing
730  *        too-small selectivity, not to mention wasting time at execution.
731  */
732 static bool
733 qual_is_redundant(Query *root,
734                                   RestrictInfo *restrictinfo,
735                                   List *restrictlist)
736 {
737         List       *oldquals;
738         List       *olditem;
739         Node       *newleft;
740         Node       *newright;
741         List       *equalvars;
742         bool            someadded;
743
744         /*
745          * Set cached pathkeys.  NB: it is okay to do this now because this
746          * routine is only invoked while we are generating implied equalities.
747          * Therefore, the equi_key_list is already complete and so we can
748          * correctly determine canonical pathkeys.
749          */
750         cache_mergeclause_pathkeys(root, restrictinfo);
751         /* If different, say "not redundant" (should never happen) */
752         if (restrictinfo->left_pathkey != restrictinfo->right_pathkey)
753                 return false;
754
755         /*
756          * Scan existing quals to find those referencing same pathkeys.
757          * Usually there will be few, if any, so build a list of just the
758          * interesting ones.
759          */
760         oldquals = NIL;
761         foreach(olditem, restrictlist)
762         {
763                 RestrictInfo *oldrinfo = (RestrictInfo *) lfirst(olditem);
764
765                 if (oldrinfo->mergejoinoperator != InvalidOid)
766                 {
767                         cache_mergeclause_pathkeys(root, oldrinfo);
768                         if (restrictinfo->left_pathkey == oldrinfo->left_pathkey &&
769                                 restrictinfo->right_pathkey == oldrinfo->right_pathkey)
770                                 oldquals = lcons(oldrinfo, oldquals);
771                 }
772         }
773         if (oldquals == NIL)
774                 return false;
775
776         /*
777          * Now, we want to develop a list of Vars that are known equal to the
778          * left side of the new qual.  We traverse the old-quals list
779          * repeatedly to transitively expand the Vars list.  If at any point
780          * we find we can reach the right-side Var of the new qual, we are
781          * done.  We give up when we can't expand the equalvars list any more.
782          */
783         newleft = (Node *) get_leftop(restrictinfo->clause);
784         newright = (Node *) get_rightop(restrictinfo->clause);
785         equalvars = makeList1(newleft);
786         do
787         {
788                 someadded = false;
789                 foreach(olditem, oldquals)
790                 {
791                         RestrictInfo *oldrinfo = (RestrictInfo *) lfirst(olditem);
792                         Node       *oldleft = (Node *) get_leftop(oldrinfo->clause);
793                         Node       *oldright = (Node *) get_rightop(oldrinfo->clause);
794                         Node       *newguy = NULL;
795
796                         if (member(oldleft, equalvars))
797                                 newguy = oldright;
798                         else if (member(oldright, equalvars))
799                                 newguy = oldleft;
800                         else
801                                 continue;
802                         if (equal(newguy, newright))
803                                 return true;    /* we proved new clause is redundant */
804                         equalvars = lcons(newguy, equalvars);
805                         someadded = true;
806
807                         /*
808                          * Remove this qual from list, since we don't need it anymore.
809                          * Note this doesn't break the foreach() loop, since lremove
810                          * doesn't touch the next-link of the removed cons cell.
811                          */
812                         oldquals = lremove(oldrinfo, oldquals);
813                 }
814         } while (someadded);
815
816         return false;                           /* it's not redundant */
817 }
818
819
820 /*****************************************************************************
821  *
822  *       CHECKS FOR MERGEJOINABLE AND HASHJOINABLE CLAUSES
823  *
824  *****************************************************************************/
825
826 /*
827  * check_mergejoinable
828  *        If the restrictinfo's clause is mergejoinable, set the mergejoin
829  *        info fields in the restrictinfo.
830  *
831  *        Currently, we support mergejoin for binary opclauses where
832  *        both operands are simple Vars and the operator is a mergejoinable
833  *        operator.
834  */
835 static void
836 check_mergejoinable(RestrictInfo *restrictinfo)
837 {
838         Expr       *clause = restrictinfo->clause;
839         Var                *left,
840                            *right;
841         Oid                     opno,
842                                 leftOp,
843                                 rightOp;
844
845         if (!is_opclause((Node *) clause))
846                 return;
847
848         left = get_leftop(clause);
849         right = get_rightop(clause);
850
851         /* caution: is_opclause accepts more than I do, so check it */
852         if (!right)
853                 return;                                 /* unary opclauses need not apply */
854         if (!IsA(left, Var) ||!IsA(right, Var))
855                 return;
856
857         opno = ((Oper *) clause->oper)->opno;
858
859         if (op_mergejoinable(opno,
860                                                  left->vartype,
861                                                  right->vartype,
862                                                  &leftOp,
863                                                  &rightOp))
864         {
865                 restrictinfo->mergejoinoperator = opno;
866                 restrictinfo->left_sortop = leftOp;
867                 restrictinfo->right_sortop = rightOp;
868         }
869 }
870
871 /*
872  * check_hashjoinable
873  *        If the restrictinfo's clause is hashjoinable, set the hashjoin
874  *        info fields in the restrictinfo.
875  *
876  *        Currently, we support hashjoin for binary opclauses where
877  *        both operands are simple Vars and the operator is a hashjoinable
878  *        operator.
879  */
880 static void
881 check_hashjoinable(RestrictInfo *restrictinfo)
882 {
883         Expr       *clause = restrictinfo->clause;
884         Var                *left,
885                            *right;
886         Oid                     opno;
887
888         if (!is_opclause((Node *) clause))
889                 return;
890
891         left = get_leftop(clause);
892         right = get_rightop(clause);
893
894         /* caution: is_opclause accepts more than I do, so check it */
895         if (!right)
896                 return;                                 /* unary opclauses need not apply */
897         if (!IsA(left, Var) ||!IsA(right, Var))
898                 return;
899
900         opno = ((Oper *) clause->oper)->opno;
901
902         if (op_hashjoinable(opno,
903                                                 left->vartype,
904                                                 right->vartype))
905                 restrictinfo->hashjoinoperator = opno;
906 }