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