]> granicus.if.org Git - postgresql/blob - src/backend/optimizer/path/allpaths.c
12081c2cda555d0670a12a3b17e911b4fe217191
[postgresql] / src / backend / optimizer / path / allpaths.c
1 /*-------------------------------------------------------------------------
2  *
3  * allpaths.c
4  *        Routines to find possible search paths for processing a query
5  *
6  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $PostgreSQL: pgsql/src/backend/optimizer/path/allpaths.c,v 1.123 2004/12/31 22:00:00 pgsql Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15
16 #include "postgres.h"
17
18 #ifdef OPTIMIZER_DEBUG
19 #include "nodes/print.h"
20 #endif
21 #include "optimizer/clauses.h"
22 #include "optimizer/cost.h"
23 #include "optimizer/geqo.h"
24 #include "optimizer/pathnode.h"
25 #include "optimizer/paths.h"
26 #include "optimizer/plancat.h"
27 #include "optimizer/planner.h"
28 #include "optimizer/prep.h"
29 #include "optimizer/var.h"
30 #include "parser/parsetree.h"
31 #include "parser/parse_clause.h"
32 #include "rewrite/rewriteManip.h"
33
34
35 /* These parameters are set by GUC */
36 bool            enable_geqo = false;    /* just in case GUC doesn't set it */
37 int                     geqo_threshold;
38
39
40 static void set_base_rel_pathlists(Query *root);
41 static void set_plain_rel_pathlist(Query *root, RelOptInfo *rel,
42                                            RangeTblEntry *rte);
43 static void set_inherited_rel_pathlist(Query *root, RelOptInfo *rel,
44                                                    Index rti, RangeTblEntry *rte,
45                                                    List *inheritlist);
46 static void set_subquery_pathlist(Query *root, RelOptInfo *rel,
47                                           Index rti, RangeTblEntry *rte);
48 static void set_function_pathlist(Query *root, RelOptInfo *rel,
49                                           RangeTblEntry *rte);
50 static RelOptInfo *make_one_rel_by_joins(Query *root, int levels_needed,
51                                           List *initial_rels);
52 static bool subquery_is_pushdown_safe(Query *subquery, Query *topquery,
53                                                   bool *differentTypes);
54 static bool recurse_pushdown_safe(Node *setOp, Query *topquery,
55                                           bool *differentTypes);
56 static void compare_tlist_datatypes(List *tlist, List *colTypes,
57                                                 bool *differentTypes);
58 static bool qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual,
59                                           bool *differentTypes);
60 static void subquery_push_qual(Query *subquery, List *rtable,
61                                    Index rti, Node *qual);
62 static void recurse_push_qual(Node *setOp, Query *topquery,
63                                   List *rtable, Index rti, Node *qual);
64
65
66 /*
67  * make_one_rel
68  *        Finds all possible access paths for executing a query, returning a
69  *        single rel that represents the join of all base rels in the query.
70  */
71 RelOptInfo *
72 make_one_rel(Query *root)
73 {
74         RelOptInfo *rel;
75
76         /*
77          * Generate access paths for the base rels.
78          */
79         set_base_rel_pathlists(root);
80
81         /*
82          * Generate access paths for the entire join tree.
83          */
84         Assert(root->jointree != NULL && IsA(root->jointree, FromExpr));
85
86         rel = make_fromexpr_rel(root, root->jointree);
87
88         /*
89          * The result should join all the query's base rels.
90          */
91         Assert(bms_num_members(rel->relids) == list_length(root->base_rel_list));
92
93         return rel;
94 }
95
96 /*
97  * set_base_rel_pathlists
98  *        Finds all paths available for scanning each base-relation entry.
99  *        Sequential scan and any available indices are considered.
100  *        Each useful path is attached to its relation's 'pathlist' field.
101  */
102 static void
103 set_base_rel_pathlists(Query *root)
104 {
105         ListCell   *l;
106
107         foreach(l, root->base_rel_list)
108         {
109                 RelOptInfo *rel = (RelOptInfo *) lfirst(l);
110                 Index           rti = rel->relid;
111                 RangeTblEntry *rte;
112                 List       *inheritlist;
113
114                 Assert(rti > 0);                /* better be base rel */
115                 rte = rt_fetch(rti, root->rtable);
116
117                 if (rel->rtekind == RTE_SUBQUERY)
118                 {
119                         /* Subquery --- generate a separate plan for it */
120                         set_subquery_pathlist(root, rel, rti, rte);
121                 }
122                 else if (rel->rtekind == RTE_FUNCTION)
123                 {
124                         /* RangeFunction --- generate a separate plan for it */
125                         set_function_pathlist(root, rel, rte);
126                 }
127                 else if ((inheritlist = expand_inherited_rtentry(root, rti)) != NIL)
128                 {
129                         /* Relation is root of an inheritance tree, process specially */
130                         set_inherited_rel_pathlist(root, rel, rti, rte, inheritlist);
131                 }
132                 else
133                 {
134                         /* Plain relation */
135                         set_plain_rel_pathlist(root, rel, rte);
136                 }
137
138 #ifdef OPTIMIZER_DEBUG
139                 debug_print_rel(root, rel);
140 #endif
141         }
142 }
143
144 /*
145  * set_plain_rel_pathlist
146  *        Build access paths for a plain relation (no subquery, no inheritance)
147  */
148 static void
149 set_plain_rel_pathlist(Query *root, RelOptInfo *rel, RangeTblEntry *rte)
150 {
151         /* Mark rel with estimated output rows, width, etc */
152         set_baserel_size_estimates(root, rel);
153
154         /* Test any partial indexes of rel for applicability */
155         check_partial_indexes(root, rel);
156
157         /*
158          * Check to see if we can extract any restriction conditions from join
159          * quals that are OR-of-AND structures.  If so, add them to the rel's
160          * restriction list, and recompute the size estimates.
161          */
162         if (create_or_index_quals(root, rel))
163                 set_baserel_size_estimates(root, rel);
164
165         /*
166          * Generate paths and add them to the rel's pathlist.
167          *
168          * Note: add_path() will discard any paths that are dominated by another
169          * available path, keeping only those paths that are superior along at
170          * least one dimension of cost or sortedness.
171          */
172
173         /* Consider sequential scan */
174         add_path(rel, create_seqscan_path(root, rel));
175
176         /* Consider TID scans */
177         create_tidscan_paths(root, rel);
178
179         /* Consider index paths for both simple and OR index clauses */
180         create_index_paths(root, rel);
181         create_or_index_paths(root, rel);
182
183         /* Now find the cheapest of the paths for this rel */
184         set_cheapest(rel);
185 }
186
187 /*
188  * set_inherited_rel_pathlist
189  *        Build access paths for a inheritance tree rooted at rel
190  *
191  * inheritlist is a list of RT indexes of all tables in the inheritance tree,
192  * including a duplicate of the parent itself.  Note we will not come here
193  * unless there's at least one child in addition to the parent.
194  *
195  * NOTE: the passed-in rel and RTE will henceforth represent the appended
196  * result of the whole inheritance tree.  The members of inheritlist represent
197  * the individual tables --- in particular, the inheritlist member that is a
198  * duplicate of the parent RTE represents the parent table alone.
199  * We will generate plans to scan the individual tables that refer to
200  * the inheritlist RTEs, whereas Vars elsewhere in the plan tree that
201  * refer to the original RTE are taken to refer to the append output.
202  * In particular, this means we have separate RelOptInfos for the parent
203  * table and for the append output, which is a good thing because they're
204  * not the same size.
205  */
206 static void
207 set_inherited_rel_pathlist(Query *root, RelOptInfo *rel,
208                                                    Index rti, RangeTblEntry *rte,
209                                                    List *inheritlist)
210 {
211         int                     parentRTindex = rti;
212         Oid                     parentOID = rte->relid;
213         List       *subpaths = NIL;
214         ListCell   *il;
215
216         /*
217          * XXX for now, can't handle inherited expansion of FOR UPDATE; can we
218          * do better?
219          */
220         if (list_member_int(root->rowMarks, parentRTindex))
221                 ereport(ERROR,
222                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
223                                  errmsg("SELECT FOR UPDATE is not supported for inheritance queries")));
224
225         /*
226          * Initialize to compute size estimates for whole inheritance tree
227          */
228         rel->rows = 0;
229         rel->width = 0;
230
231         /*
232          * Generate access paths for each table in the tree (parent AND
233          * children), and pick the cheapest path for each table.
234          */
235         foreach(il, inheritlist)
236         {
237                 int                     childRTindex = lfirst_int(il);
238                 RangeTblEntry *childrte;
239                 Oid                     childOID;
240                 RelOptInfo *childrel;
241                 ListCell   *parentvars;
242                 ListCell   *childvars;
243
244                 childrte = rt_fetch(childRTindex, root->rtable);
245                 childOID = childrte->relid;
246
247                 /*
248                  * Make a RelOptInfo for the child so we can do planning.  Do NOT
249                  * attach the RelOptInfo to the query's base_rel_list, however,
250                  * since the child is not part of the main join tree.  Instead,
251                  * the child RelOptInfo is added to other_rel_list.
252                  */
253                 childrel = build_other_rel(root, childRTindex);
254
255                 /*
256                  * Copy the parent's targetlist and restriction quals to the
257                  * child, with attribute-number adjustment as needed.  We don't
258                  * bother to copy the join quals, since we can't do any joining of
259                  * the individual tables.  Also, we just zap attr_needed rather
260                  * than trying to adjust it; it won't be looked at in the child.
261                  */
262                 childrel->reltargetlist = (List *)
263                         adjust_inherited_attrs((Node *) rel->reltargetlist,
264                                                                    parentRTindex,
265                                                                    parentOID,
266                                                                    childRTindex,
267                                                                    childOID);
268                 childrel->attr_needed = NULL;
269                 childrel->baserestrictinfo = (List *)
270                         adjust_inherited_attrs((Node *) rel->baserestrictinfo,
271                                                                    parentRTindex,
272                                                                    parentOID,
273                                                                    childRTindex,
274                                                                    childOID);
275
276                 /*
277                  * Now compute child access paths, and save the cheapest.
278                  */
279                 set_plain_rel_pathlist(root, childrel, childrte);
280
281                 subpaths = lappend(subpaths, childrel->cheapest_total_path);
282
283                 /*
284                  * Propagate size information from the child back to the parent.
285                  * For simplicity, we use the largest widths from any child as the
286                  * parent estimates.
287                  */
288                 rel->rows += childrel->rows;
289                 if (childrel->width > rel->width)
290                         rel->width = childrel->width;
291
292                 forboth(parentvars, rel->reltargetlist,
293                                 childvars, childrel->reltargetlist)
294                 {
295                         Var                *parentvar = (Var *) lfirst(parentvars);
296                         Var                *childvar = (Var *) lfirst(childvars);
297
298                         if (IsA(parentvar, Var) &&IsA(childvar, Var))
299                         {
300                                 int                     pndx = parentvar->varattno - rel->min_attr;
301                                 int                     cndx = childvar->varattno - childrel->min_attr;
302
303                                 if (childrel->attr_widths[cndx] > rel->attr_widths[pndx])
304                                         rel->attr_widths[pndx] = childrel->attr_widths[cndx];
305                         }
306                 }
307         }
308
309         /*
310          * Finally, build Append path and install it as the only access path
311          * for the parent rel.
312          */
313         add_path(rel, (Path *) create_append_path(rel, subpaths));
314
315         /* Select cheapest path (pretty easy in this case...) */
316         set_cheapest(rel);
317 }
318
319 /*
320  * set_subquery_pathlist
321  *              Build the (single) access path for a subquery RTE
322  */
323 static void
324 set_subquery_pathlist(Query *root, RelOptInfo *rel,
325                                           Index rti, RangeTblEntry *rte)
326 {
327         Query      *subquery = rte->subquery;
328         bool       *differentTypes;
329         List       *pathkeys;
330
331         /* We need a workspace for keeping track of set-op type coercions */
332         differentTypes = (bool *)
333                 palloc0((list_length(subquery->targetList) + 1) * sizeof(bool));
334
335         /*
336          * If there are any restriction clauses that have been attached to the
337          * subquery relation, consider pushing them down to become HAVING
338          * quals of the subquery itself.  (Not WHERE clauses, since they may
339          * refer to subquery outputs that are aggregate results.  But
340          * planner.c will transfer them into the subquery's WHERE if they do
341          * not.)  This transformation is useful because it may allow us to
342          * generate a better plan for the subquery than evaluating all the
343          * subquery output rows and then filtering them.
344          *
345          * There are several cases where we cannot push down clauses.
346          * Restrictions involving the subquery are checked by
347          * subquery_is_pushdown_safe().  Restrictions on individual clauses
348          * are checked by qual_is_pushdown_safe().
349          *
350          * Non-pushed-down clauses will get evaluated as qpquals of the
351          * SubqueryScan node.
352          *
353          * XXX Are there any cases where we want to make a policy decision not to
354          * push down a pushable qual, because it'd result in a worse plan?
355          */
356         if (rel->baserestrictinfo != NIL &&
357                 subquery_is_pushdown_safe(subquery, subquery, differentTypes))
358         {
359                 /* OK to consider pushing down individual quals */
360                 List       *upperrestrictlist = NIL;
361                 ListCell   *l;
362
363                 foreach(l, rel->baserestrictinfo)
364                 {
365                         RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
366                         Node       *clause = (Node *) rinfo->clause;
367
368                         if (qual_is_pushdown_safe(subquery, rti, clause, differentTypes))
369                         {
370                                 /* Push it down */
371                                 subquery_push_qual(subquery, root->rtable, rti, clause);
372                         }
373                         else
374                         {
375                                 /* Keep it in the upper query */
376                                 upperrestrictlist = lappend(upperrestrictlist, rinfo);
377                         }
378                 }
379                 rel->baserestrictinfo = upperrestrictlist;
380         }
381
382         pfree(differentTypes);
383
384         /* Generate the plan for the subquery */
385         rel->subplan = subquery_planner(subquery, 0.0 /* default case */ );
386
387         /* Copy number of output rows from subplan */
388         rel->tuples = rel->subplan->plan_rows;
389
390         /* Mark rel with estimated output rows, width, etc */
391         set_baserel_size_estimates(root, rel);
392
393         /* Convert subquery pathkeys to outer representation */
394         pathkeys = build_subquery_pathkeys(root, rel, subquery);
395
396         /* Generate appropriate path */
397         add_path(rel, create_subqueryscan_path(rel, pathkeys));
398
399         /* Select cheapest path (pretty easy in this case...) */
400         set_cheapest(rel);
401 }
402
403 /*
404  * set_function_pathlist
405  *              Build the (single) access path for a function RTE
406  */
407 static void
408 set_function_pathlist(Query *root, RelOptInfo *rel, RangeTblEntry *rte)
409 {
410         /* Mark rel with estimated output rows, width, etc */
411         set_function_size_estimates(root, rel);
412
413         /* Generate appropriate path */
414         add_path(rel, create_functionscan_path(root, rel));
415
416         /* Select cheapest path (pretty easy in this case...) */
417         set_cheapest(rel);
418 }
419
420 /*
421  * make_fromexpr_rel
422  *        Build access paths for a FromExpr jointree node.
423  */
424 RelOptInfo *
425 make_fromexpr_rel(Query *root, FromExpr *from)
426 {
427         int                     levels_needed;
428         List       *initial_rels = NIL;
429         ListCell   *jt;
430
431         /*
432          * Count the number of child jointree nodes.  This is the depth of the
433          * dynamic-programming algorithm we must employ to consider all ways
434          * of joining the child nodes.
435          */
436         levels_needed = list_length(from->fromlist);
437
438         if (levels_needed <= 0)
439                 return NULL;                    /* nothing to do? */
440
441         /*
442          * Construct a list of rels corresponding to the child jointree nodes.
443          * This may contain both base rels and rels constructed according to
444          * explicit JOIN directives.
445          */
446         foreach(jt, from->fromlist)
447         {
448                 Node       *jtnode = (Node *) lfirst(jt);
449
450                 initial_rels = lappend(initial_rels,
451                                                            make_jointree_rel(root, jtnode));
452         }
453
454         if (levels_needed == 1)
455         {
456                 /*
457                  * Single jointree node, so we're done.
458                  */
459                 return (RelOptInfo *) linitial(initial_rels);
460         }
461         else
462         {
463                 /*
464                  * Consider the different orders in which we could join the rels,
465                  * using either GEQO or regular optimizer.
466                  */
467                 if (enable_geqo && levels_needed >= geqo_threshold)
468                         return geqo(root, levels_needed, initial_rels);
469                 else
470                         return make_one_rel_by_joins(root, levels_needed, initial_rels);
471         }
472 }
473
474 /*
475  * make_one_rel_by_joins
476  *        Find all possible joinpaths for a query by successively finding ways
477  *        to join component relations into join relations.
478  *
479  * 'levels_needed' is the number of iterations needed, ie, the number of
480  *              independent jointree items in the query.  This is > 1.
481  *
482  * 'initial_rels' is a list of RelOptInfo nodes for each independent
483  *              jointree item.  These are the components to be joined together.
484  *
485  * Returns the final level of join relations, i.e., the relation that is
486  * the result of joining all the original relations together.
487  */
488 static RelOptInfo *
489 make_one_rel_by_joins(Query *root, int levels_needed, List *initial_rels)
490 {
491         List      **joinitems;
492         int                     lev;
493         RelOptInfo *rel;
494
495         /*
496          * We employ a simple "dynamic programming" algorithm: we first find
497          * all ways to build joins of two jointree items, then all ways to
498          * build joins of three items (from two-item joins and single items),
499          * then four-item joins, and so on until we have considered all ways
500          * to join all the items into one rel.
501          *
502          * joinitems[j] is a list of all the j-item rels.  Initially we set
503          * joinitems[1] to represent all the single-jointree-item relations.
504          */
505         joinitems = (List **) palloc0((levels_needed + 1) * sizeof(List *));
506
507         joinitems[1] = initial_rels;
508
509         for (lev = 2; lev <= levels_needed; lev++)
510         {
511                 ListCell   *x;
512
513                 /*
514                  * Determine all possible pairs of relations to be joined at this
515                  * level, and build paths for making each one from every available
516                  * pair of lower-level relations.
517                  */
518                 joinitems[lev] = make_rels_by_joins(root, lev, joinitems);
519
520                 /*
521                  * Do cleanup work on each just-processed rel.
522                  */
523                 foreach(x, joinitems[lev])
524                 {
525                         rel = (RelOptInfo *) lfirst(x);
526
527                         /* Find and save the cheapest paths for this rel */
528                         set_cheapest(rel);
529
530 #ifdef OPTIMIZER_DEBUG
531                         debug_print_rel(root, rel);
532 #endif
533                 }
534         }
535
536         /*
537          * We should have a single rel at the final level.
538          */
539         if (joinitems[levels_needed] == NIL)
540                 elog(ERROR, "failed to build any %d-way joins", levels_needed);
541         Assert(list_length(joinitems[levels_needed]) == 1);
542
543         rel = (RelOptInfo *) linitial(joinitems[levels_needed]);
544
545         return rel;
546 }
547
548 /*****************************************************************************
549  *                      PUSHING QUALS DOWN INTO SUBQUERIES
550  *****************************************************************************/
551
552 /*
553  * subquery_is_pushdown_safe - is a subquery safe for pushing down quals?
554  *
555  * subquery is the particular component query being checked.  topquery
556  * is the top component of a set-operations tree (the same Query if no
557  * set-op is involved).
558  *
559  * Conditions checked here:
560  *
561  * 1. If the subquery has a LIMIT clause, we must not push down any quals,
562  * since that could change the set of rows returned.
563  *
564  * 2. If the subquery contains EXCEPT or EXCEPT ALL set ops we cannot push
565  * quals into it, because that would change the results.
566  *
567  * 3. For subqueries using UNION/UNION ALL/INTERSECT/INTERSECT ALL, we can
568  * push quals into each component query, but the quals can only reference
569  * subquery columns that suffer no type coercions in the set operation.
570  * Otherwise there are possible semantic gotchas.  So, we check the
571  * component queries to see if any of them have different output types;
572  * differentTypes[k] is set true if column k has different type in any
573  * component.
574  */
575 static bool
576 subquery_is_pushdown_safe(Query *subquery, Query *topquery,
577                                                   bool *differentTypes)
578 {
579         SetOperationStmt *topop;
580
581         /* Check point 1 */
582         if (subquery->limitOffset != NULL || subquery->limitCount != NULL)
583                 return false;
584
585         /* Are we at top level, or looking at a setop component? */
586         if (subquery == topquery)
587         {
588                 /* Top level, so check any component queries */
589                 if (subquery->setOperations != NULL)
590                         if (!recurse_pushdown_safe(subquery->setOperations, topquery,
591                                                                            differentTypes))
592                                 return false;
593         }
594         else
595         {
596                 /* Setop component must not have more components (too weird) */
597                 if (subquery->setOperations != NULL)
598                         return false;
599                 /* Check whether setop component output types match top level */
600                 topop = (SetOperationStmt *) topquery->setOperations;
601                 Assert(topop && IsA(topop, SetOperationStmt));
602                 compare_tlist_datatypes(subquery->targetList,
603                                                                 topop->colTypes,
604                                                                 differentTypes);
605         }
606         return true;
607 }
608
609 /*
610  * Helper routine to recurse through setOperations tree
611  */
612 static bool
613 recurse_pushdown_safe(Node *setOp, Query *topquery,
614                                           bool *differentTypes)
615 {
616         if (IsA(setOp, RangeTblRef))
617         {
618                 RangeTblRef *rtr = (RangeTblRef *) setOp;
619                 RangeTblEntry *rte = rt_fetch(rtr->rtindex, topquery->rtable);
620                 Query      *subquery = rte->subquery;
621
622                 Assert(subquery != NULL);
623                 return subquery_is_pushdown_safe(subquery, topquery, differentTypes);
624         }
625         else if (IsA(setOp, SetOperationStmt))
626         {
627                 SetOperationStmt *op = (SetOperationStmt *) setOp;
628
629                 /* EXCEPT is no good */
630                 if (op->op == SETOP_EXCEPT)
631                         return false;
632                 /* Else recurse */
633                 if (!recurse_pushdown_safe(op->larg, topquery, differentTypes))
634                         return false;
635                 if (!recurse_pushdown_safe(op->rarg, topquery, differentTypes))
636                         return false;
637         }
638         else
639         {
640                 elog(ERROR, "unrecognized node type: %d",
641                          (int) nodeTag(setOp));
642         }
643         return true;
644 }
645
646 /*
647  * Compare tlist's datatypes against the list of set-operation result types.
648  * For any items that are different, mark the appropriate element of
649  * differentTypes[] to show that this column will have type conversions.
650  */
651 static void
652 compare_tlist_datatypes(List *tlist, List *colTypes,
653                                                 bool *differentTypes)
654 {
655         ListCell   *l;
656         ListCell   *colType = list_head(colTypes);
657
658         foreach(l, tlist)
659         {
660                 TargetEntry *tle = (TargetEntry *) lfirst(l);
661
662                 if (tle->resdom->resjunk)
663                         continue;                       /* ignore resjunk columns */
664                 if (colType == NULL)
665                         elog(ERROR, "wrong number of tlist entries");
666                 if (tle->resdom->restype != lfirst_oid(colType))
667                         differentTypes[tle->resdom->resno] = true;
668                 colType = lnext(colType);
669         }
670         if (colType != NULL)
671                 elog(ERROR, "wrong number of tlist entries");
672 }
673
674 /*
675  * qual_is_pushdown_safe - is a particular qual safe to push down?
676  *
677  * qual is a restriction clause applying to the given subquery (whose RTE
678  * has index rti in the parent query).
679  *
680  * Conditions checked here:
681  *
682  * 1. The qual must not contain any subselects (mainly because I'm not sure
683  * it will work correctly: sublinks will already have been transformed into
684  * subplans in the qual, but not in the subquery).
685  *
686  * 2. The qual must not refer to any subquery output columns that were
687  * found to have inconsistent types across a set operation tree by
688  * subquery_is_pushdown_safe().
689  *
690  * 3. If the subquery uses DISTINCT ON, we must not push down any quals that
691  * refer to non-DISTINCT output columns, because that could change the set
692  * of rows returned.  This condition is vacuous for DISTINCT, because then
693  * there are no non-DISTINCT output columns, but unfortunately it's fairly
694  * expensive to tell the difference between DISTINCT and DISTINCT ON in the
695  * parsetree representation.  It's cheaper to just make sure all the Vars
696  * in the qual refer to DISTINCT columns.
697  *
698  * 4. We must not push down any quals that refer to subselect outputs that
699  * return sets, else we'd introduce functions-returning-sets into the
700  * subquery's WHERE/HAVING quals.
701  */
702 static bool
703 qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual,
704                                           bool *differentTypes)
705 {
706         bool            safe = true;
707         List       *vars;
708         ListCell   *vl;
709         Bitmapset  *tested = NULL;
710
711         /* Refuse subselects (point 1) */
712         if (contain_subplans(qual))
713                 return false;
714
715         /*
716          * Examine all Vars used in clause; since it's a restriction clause,
717          * all such Vars must refer to subselect output columns.
718          */
719         vars = pull_var_clause(qual, false);
720         foreach(vl, vars)
721         {
722                 Var                *var = (Var *) lfirst(vl);
723                 TargetEntry *tle;
724
725                 Assert(var->varno == rti);
726
727                 /*
728                  * We use a bitmapset to avoid testing the same attno more than
729                  * once.  (NB: this only works because subquery outputs can't have
730                  * negative attnos.)
731                  */
732                 if (bms_is_member(var->varattno, tested))
733                         continue;
734                 tested = bms_add_member(tested, var->varattno);
735
736                 /* Check point 2 */
737                 if (differentTypes[var->varattno])
738                 {
739                         safe = false;
740                         break;
741                 }
742
743                 /* Must find the tlist element referenced by the Var */
744                 tle = get_tle_by_resno(subquery->targetList, var->varattno);
745                 Assert(tle != NULL);
746                 Assert(!tle->resdom->resjunk);
747
748                 /* If subquery uses DISTINCT or DISTINCT ON, check point 3 */
749                 if (subquery->distinctClause != NIL &&
750                         !targetIsInSortList(tle, subquery->distinctClause))
751                 {
752                         /* non-DISTINCT column, so fail */
753                         safe = false;
754                         break;
755                 }
756
757                 /* Refuse functions returning sets (point 4) */
758                 if (expression_returns_set((Node *) tle->expr))
759                 {
760                         safe = false;
761                         break;
762                 }
763         }
764
765         list_free(vars);
766         bms_free(tested);
767
768         return safe;
769 }
770
771 /*
772  * subquery_push_qual - push down a qual that we have determined is safe
773  */
774 static void
775 subquery_push_qual(Query *subquery, List *rtable, Index rti, Node *qual)
776 {
777         if (subquery->setOperations != NULL)
778         {
779                 /* Recurse to push it separately to each component query */
780                 recurse_push_qual(subquery->setOperations, subquery,
781                                                   rtable, rti, qual);
782         }
783         else
784         {
785                 /*
786                  * We need to replace Vars in the qual (which must refer to
787                  * outputs of the subquery) with copies of the subquery's
788                  * targetlist expressions.      Note that at this point, any uplevel
789                  * Vars in the qual should have been replaced with Params, so they
790                  * need no work.
791                  *
792                  * This step also ensures that when we are pushing into a setop tree,
793                  * each component query gets its own copy of the qual.
794                  */
795                 qual = ResolveNew(qual, rti, 0, rtable,
796                                                   subquery->targetList,
797                                                   CMD_SELECT, 0);
798                 subquery->havingQual = make_and_qual(subquery->havingQual,
799                                                                                          qual);
800
801                 /*
802                  * We need not change the subquery's hasAggs or hasSublinks flags,
803                  * since we can't be pushing down any aggregates that weren't
804                  * there before, and we don't push down subselects at all.
805                  */
806         }
807 }
808
809 /*
810  * Helper routine to recurse through setOperations tree
811  */
812 static void
813 recurse_push_qual(Node *setOp, Query *topquery,
814                                   List *rtable, Index rti, Node *qual)
815 {
816         if (IsA(setOp, RangeTblRef))
817         {
818                 RangeTblRef *rtr = (RangeTblRef *) setOp;
819                 RangeTblEntry *subrte = rt_fetch(rtr->rtindex, topquery->rtable);
820                 Query      *subquery = subrte->subquery;
821
822                 Assert(subquery != NULL);
823                 subquery_push_qual(subquery, rtable, rti, qual);
824         }
825         else if (IsA(setOp, SetOperationStmt))
826         {
827                 SetOperationStmt *op = (SetOperationStmt *) setOp;
828
829                 recurse_push_qual(op->larg, topquery, rtable, rti, qual);
830                 recurse_push_qual(op->rarg, topquery, rtable, rti, qual);
831         }
832         else
833         {
834                 elog(ERROR, "unrecognized node type: %d",
835                          (int) nodeTag(setOp));
836         }
837 }
838
839 /*****************************************************************************
840  *                      DEBUG SUPPORT
841  *****************************************************************************/
842
843 #ifdef OPTIMIZER_DEBUG
844
845 static void
846 print_relids(Relids relids)
847 {
848         Relids          tmprelids;
849         int                     x;
850         bool            first = true;
851
852         tmprelids = bms_copy(relids);
853         while ((x = bms_first_member(tmprelids)) >= 0)
854         {
855                 if (!first)
856                         printf(" ");
857                 printf("%d", x);
858                 first = false;
859         }
860         bms_free(tmprelids);
861 }
862
863 static void
864 print_restrictclauses(Query *root, List *clauses)
865 {
866         ListCell   *l;
867
868         foreach(l, clauses)
869         {
870                 RestrictInfo *c = lfirst(l);
871
872                 print_expr((Node *) c->clause, root->rtable);
873                 if (lnext(l))
874                         printf(", ");
875         }
876 }
877
878 static void
879 print_path(Query *root, Path *path, int indent)
880 {
881         const char *ptype;
882         bool            join = false;
883         Path       *subpath = NULL;
884         int                     i;
885
886         switch (nodeTag(path))
887         {
888                 case T_Path:
889                         ptype = "SeqScan";
890                         break;
891                 case T_IndexPath:
892                         ptype = "IdxScan";
893                         break;
894                 case T_TidPath:
895                         ptype = "TidScan";
896                         break;
897                 case T_AppendPath:
898                         ptype = "Append";
899                         break;
900                 case T_ResultPath:
901                         ptype = "Result";
902                         subpath = ((ResultPath *) path)->subpath;
903                         break;
904                 case T_MaterialPath:
905                         ptype = "Material";
906                         subpath = ((MaterialPath *) path)->subpath;
907                         break;
908                 case T_UniquePath:
909                         ptype = "Unique";
910                         subpath = ((UniquePath *) path)->subpath;
911                         break;
912                 case T_NestPath:
913                         ptype = "NestLoop";
914                         join = true;
915                         break;
916                 case T_MergePath:
917                         ptype = "MergeJoin";
918                         join = true;
919                         break;
920                 case T_HashPath:
921                         ptype = "HashJoin";
922                         join = true;
923                         break;
924                 default:
925                         ptype = "???Path";
926                         break;
927         }
928
929         for (i = 0; i < indent; i++)
930                 printf("\t");
931         printf("%s", ptype);
932
933         if (path->parent)
934         {
935                 printf("(");
936                 print_relids(path->parent->relids);
937                 printf(") rows=%.0f", path->parent->rows);
938         }
939         printf(" cost=%.2f..%.2f\n", path->startup_cost, path->total_cost);
940
941         if (path->pathkeys)
942         {
943                 for (i = 0; i < indent; i++)
944                         printf("\t");
945                 printf("  pathkeys: ");
946                 print_pathkeys(path->pathkeys, root->rtable);
947         }
948
949         if (join)
950         {
951                 JoinPath   *jp = (JoinPath *) path;
952
953                 for (i = 0; i < indent; i++)
954                         printf("\t");
955                 printf("  clauses: ");
956                 print_restrictclauses(root, jp->joinrestrictinfo);
957                 printf("\n");
958
959                 if (IsA(path, MergePath))
960                 {
961                         MergePath  *mp = (MergePath *) path;
962
963                         if (mp->outersortkeys || mp->innersortkeys)
964                         {
965                                 for (i = 0; i < indent; i++)
966                                         printf("\t");
967                                 printf("  sortouter=%d sortinner=%d\n",
968                                            ((mp->outersortkeys) ? 1 : 0),
969                                            ((mp->innersortkeys) ? 1 : 0));
970                         }
971                 }
972
973                 print_path(root, jp->outerjoinpath, indent + 1);
974                 print_path(root, jp->innerjoinpath, indent + 1);
975         }
976
977         if (subpath)
978                 print_path(root, subpath, indent + 1);
979 }
980
981 void
982 debug_print_rel(Query *root, RelOptInfo *rel)
983 {
984         ListCell   *l;
985
986         printf("RELOPTINFO (");
987         print_relids(rel->relids);
988         printf("): rows=%.0f width=%d\n", rel->rows, rel->width);
989
990         if (rel->baserestrictinfo)
991         {
992                 printf("\tbaserestrictinfo: ");
993                 print_restrictclauses(root, rel->baserestrictinfo);
994                 printf("\n");
995         }
996
997         foreach(l, rel->joininfo)
998         {
999                 JoinInfo   *j = (JoinInfo *) lfirst(l);
1000
1001                 printf("\tjoininfo (");
1002                 print_relids(j->unjoined_relids);
1003                 printf("): ");
1004                 print_restrictclauses(root, j->jinfo_restrictinfo);
1005                 printf("\n");
1006         }
1007
1008         printf("\tpath list:\n");
1009         foreach(l, rel->pathlist)
1010                 print_path(root, lfirst(l), 1);
1011         printf("\n\tcheapest startup path:\n");
1012         print_path(root, rel->cheapest_startup_path, 1);
1013         printf("\n\tcheapest total path:\n");
1014         print_path(root, rel->cheapest_total_path, 1);
1015         printf("\n");
1016         fflush(stdout);
1017 }
1018
1019 #endif   /* OPTIMIZER_DEBUG */