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