3333#include "fmgr.h"
3434#include "miscadmin.h"
3535#include "optimizer/clauses.h"
36+ #include "optimizer/plancat.h"
3637#include "optimizer/prep.h"
3738#include "optimizer/restrictinfo.h"
3839#include "optimizer/cost.h"
@@ -276,6 +277,8 @@ append_child_relation(PlannerInfo *root, Relation parent_relation,
276277Index childRTindex ;
277278PlanRowMark * parent_rowmark ,
278279* child_rowmark ;
280+ Node * childqual ;
281+ List * childquals ;
279282ListCell * lc ,
280283* lc2 ;
281284
@@ -323,10 +326,9 @@ append_child_relation(PlannerInfo *root, Relation parent_relation,
323326 * Copy restrictions. If it's not the parent table, copy only
324327 * those restrictions that are related to this partition.
325328 */
326- child_rel -> baserestrictinfo = NIL ;
327329if (parent_rte -> relid != child_oid )
328330{
329- List * childquals = NIL ;
331+ childquals = NIL ;
330332
331333forboth (lc ,wrappers ,lc2 ,parent_rel -> baserestrictinfo )
332334{
@@ -345,24 +347,39 @@ append_child_relation(PlannerInfo *root, Relation parent_relation,
345347Assert (new_clause );
346348childquals = lappend (childquals ,new_clause );
347349}
348-
349- childquals = (List * )adjust_appendrel_attrs (root ,
350- (Node * )childquals ,
351- appinfo );
352- childquals = make_restrictinfos_from_actual_clauses (root ,childquals );
353- child_rel -> baserestrictinfo = childquals ;
354350}
355351/* If it's the parent table, copy all restrictions */
356- else
352+ else childquals = get_all_actual_clauses (parent_rel -> baserestrictinfo );
353+
354+ /* Now it's time to change varnos and rebuld quals */
355+ childquals = (List * )adjust_appendrel_attrs (root ,
356+ (Node * )childquals ,
357+ appinfo );
358+ childqual = eval_const_expressions (root , (Node * )
359+ make_ands_explicit (childquals ));
360+ if (childqual && IsA (childqual ,Const )&&
361+ (((Const * )childqual )-> constisnull ||
362+ !DatumGetBool (((Const * )childqual )-> constvalue )))
357363{
358- List * childquals = NIL ;
359-
360- childquals = get_all_actual_clauses (parent_rel -> baserestrictinfo );
361- childquals = (List * )adjust_appendrel_attrs (root ,
362- (Node * )childquals ,
363- appinfo );
364- childquals = make_restrictinfos_from_actual_clauses (root ,childquals );
365- child_rel -> baserestrictinfo = childquals ;
364+ /*
365+ * Restriction reduces to constant FALSE or constant NULL after
366+ * substitution, so this child need not be scanned.
367+ */
368+ set_dummy_rel_pathlist (child_rel );
369+ }
370+ childquals = make_ands_implicit ((Expr * )childqual );
371+ childquals = make_restrictinfos_from_actual_clauses (root ,childquals );
372+
373+ /* Set new shiny childquals */
374+ child_rel -> baserestrictinfo = childquals ;
375+
376+ if (relation_excluded_by_constraints (root ,child_rel ,child_rte ))
377+ {
378+ /*
379+ * This child need not be scanned, so we can omit it from the
380+ * appendrel.
381+ */
382+ set_dummy_rel_pathlist (child_rel );
366383}
367384
368385/*
@@ -373,9 +390,6 @@ append_child_relation(PlannerInfo *root, Relation parent_relation,
373390add_child_rel_equivalences (root ,appinfo ,parent_rel ,child_rel );
374391child_rel -> has_eclass_joins = parent_rel -> has_eclass_joins ;
375392
376- /* Recalc parent relation tuples count */
377- parent_rel -> tuples += child_rel -> tuples ;
378-
379393/* Close child relations, but keep locks */
380394heap_close (child_relation ,NoLock );
381395
@@ -1666,8 +1680,8 @@ set_plain_rel_size(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
16661680static void
16671681set_plain_rel_pathlist (PlannerInfo * root ,RelOptInfo * rel ,RangeTblEntry * rte )
16681682{
1669- Relids required_outer ;
1670- Path * path ;
1683+ Relids required_outer ;
1684+ Path * path ;
16711685
16721686/*
16731687 * We don't support pushing join clauses into the quals of a seqscan, but
@@ -1753,11 +1767,11 @@ set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, Index rti,
17531767 */
17541768foreach (l ,root -> append_rel_list )
17551769{
1756- AppendRelInfo * appinfo = (AppendRelInfo * )lfirst (l );
1757- Index childRTindex ;
1758- RangeTblEntry * childRTE ;
1759- RelOptInfo * childrel ;
1760- ListCell * lcp ;
1770+ AppendRelInfo * appinfo = (AppendRelInfo * )lfirst (l );
1771+ Index childRTindex ;
1772+ RangeTblEntry * childRTE ;
1773+ RelOptInfo * childrel ;
1774+ ListCell * lcp ;
17611775
17621776/* append_rel_list contains all append rels; ignore others */
17631777if (appinfo -> parent_relid != parentRTindex )
@@ -1780,24 +1794,34 @@ set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, Index rti,
17801794set_rel_consider_parallel_compat (root ,childrel ,childRTE );
17811795#endif
17821796
1783- /*
1784- * Compute the child's access paths.
1785- */
1797+ /* Compute child's access paths & sizes */
17861798if (childRTE -> relkind == RELKIND_FOREIGN_TABLE )
17871799{
1800+ /* childrel->rows should be >= 1 */
17881801set_foreign_size (root ,childrel ,childRTE );
1802+
1803+ /* If child IS dummy, ignore it */
1804+ if (IS_DUMMY_REL (childrel ))
1805+ continue ;
1806+
17891807set_foreign_pathlist (root ,childrel ,childRTE );
17901808}
17911809else
17921810{
1811+ /* childrel->rows should be >= 1 */
17931812set_plain_rel_size (root ,childrel ,childRTE );
1813+
1814+ /* If child IS dummy, ignore it */
1815+ if (IS_DUMMY_REL (childrel ))
1816+ continue ;
1817+
17941818set_plain_rel_pathlist (root ,childrel ,childRTE );
17951819}
1820+
1821+ /* Set cheapest path for child */
17961822set_cheapest (childrel );
17971823
1798- /*
1799- * If child is dummy, ignore it.
1800- */
1824+ /* If child BECAME dummy, ignore it */
18011825if (IS_DUMMY_REL (childrel ))
18021826continue ;
18031827