88 *
99 *
1010 * IDENTIFICATION
11- * $PostgreSQL: pgsql/src/backend/optimizer/path/allpaths.c,v 1.162 2007/04/2106:18:52 tgl Exp $
11+ * $PostgreSQL: pgsql/src/backend/optimizer/path/allpaths.c,v 1.163 2007/04/2121:01:44 tgl Exp $
1212 *
1313 *-------------------------------------------------------------------------
1414 */
@@ -39,7 +39,8 @@ intgeqo_threshold;
3939
4040
4141static void set_base_rel_pathlists (PlannerInfo * root );
42- static void set_rel_pathlist (PlannerInfo * root ,RelOptInfo * rel ,Index rti );
42+ static void set_rel_pathlist (PlannerInfo * root ,RelOptInfo * rel ,
43+ Index rti ,RangeTblEntry * rte );
4344static void set_plain_rel_pathlist (PlannerInfo * root ,RelOptInfo * rel ,
4445RangeTblEntry * rte );
4546static void set_append_rel_pathlist (PlannerInfo * root ,RelOptInfo * rel ,
@@ -144,7 +145,7 @@ set_base_rel_pathlists(PlannerInfo *root)
144145if (rel -> reloptkind != RELOPT_BASEREL )
145146continue ;
146147
147- set_rel_pathlist (root ,rel ,rti );
148+ set_rel_pathlist (root ,rel ,rti , root -> simple_rte_array [ rti ] );
148149}
149150}
150151
@@ -153,10 +154,9 @@ set_base_rel_pathlists(PlannerInfo *root)
153154 * Build access paths for a base relation
154155 */
155156static void
156- set_rel_pathlist (PlannerInfo * root ,RelOptInfo * rel ,Index rti )
157+ set_rel_pathlist (PlannerInfo * root ,RelOptInfo * rel ,
158+ Index rti ,RangeTblEntry * rte )
157159{
158- RangeTblEntry * rte = rt_fetch (rti ,root -> parse -> rtable );
159-
160160if (rte -> inh )
161161{
162162/* It's an "append relation", process accordingly */
@@ -200,8 +200,11 @@ set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
200200 * If we can prove we don't need to scan the rel via constraint exclusion,
201201 * set up a single dummy path for it. (Rather than inventing a special
202202 * "dummy" path type, we represent this as an AppendPath with no members.)
203+ * We only need to check for regular baserels; if it's an otherrel, CE
204+ * was already checked in set_append_rel_pathlist().
203205 */
204- if (relation_excluded_by_constraints (rel ,rte ))
206+ if (rel -> reloptkind == RELOPT_BASEREL &&
207+ relation_excluded_by_constraints (rel ,rte ))
205208{
206209/* Set dummy size estimates --- we leave attr_widths[] as zeroes */
207210rel -> rows = 0 ;
@@ -294,6 +297,7 @@ set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
294297{
295298AppendRelInfo * appinfo = (AppendRelInfo * )lfirst (l );
296299int childRTindex ;
300+ RangeTblEntry * childRTE ;
297301RelOptInfo * childrel ;
298302Path * childpath ;
299303ListCell * parentvars ;
@@ -304,6 +308,7 @@ set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
304308continue ;
305309
306310childRTindex = appinfo -> child_relid ;
311+ childRTE = root -> simple_rte_array [childRTindex ];
307312
308313/*
309314 * The child rel's RelOptInfo was already created during
@@ -313,18 +318,29 @@ set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
313318Assert (childrel -> reloptkind == RELOPT_OTHER_MEMBER_REL );
314319
315320/*
316- * Copy the parent's targetlist and quals to the child, with
317- * appropriate substitution of variables.
321+ * We have to copy the parent's targetlist and quals to the child,
322+ * with appropriate substitution of variables. However, only the
323+ * baserestrictinfo quals are needed before we can check for
324+ * constraint exclusion; so do that first and then check to see
325+ * if we can disregard this child.
318326 */
319- childrel -> reltargetlist = (List * )
320- adjust_appendrel_attrs ((Node * )rel -> reltargetlist ,
321- appinfo );
322327childrel -> baserestrictinfo = (List * )
323328adjust_appendrel_attrs ((Node * )rel -> baserestrictinfo ,
324329appinfo );
330+
331+ if (relation_excluded_by_constraints (childrel ,childRTE ))
332+ {
333+ /* this child need not be scanned, so just disregard it */
334+ continue ;
335+ }
336+
337+ /* CE failed, so finish copying targetlist and join quals */
325338childrel -> joininfo = (List * )
326339adjust_appendrel_attrs ((Node * )rel -> joininfo ,
327340appinfo );
341+ childrel -> reltargetlist = (List * )
342+ adjust_appendrel_attrs ((Node * )rel -> reltargetlist ,
343+ appinfo );
328344
329345/*
330346 * We have to make child entries in the EquivalenceClass data
@@ -353,12 +369,9 @@ set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
353369 * It's possible that the child is itself an appendrel, in which case
354370 * we can "cut out the middleman" and just add its child paths to our
355371 * own list. (We don't try to do this earlier because we need to
356- * apply both levels of transformation to the quals.) This test also
357- * handles the case where the child rel need not be scanned because of
358- * constraint exclusion: it'll have an Append path with no subpaths,
359- * and will vanish from our list.
372+ * apply both levels of transformation to the quals.)
360373 */
361- set_rel_pathlist (root ,childrel ,childRTindex );
374+ set_rel_pathlist (root ,childrel ,childRTindex , childRTE );
362375
363376childpath = childrel -> cheapest_total_path ;
364377if (IsA (childpath ,AppendPath ))