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