|
31 | 31 | #include "parser/analyze.h"
|
32 | 32 | #include "rewrite/rewriteManip.h"
|
33 | 33 | #include "utils/lsyscache.h"
|
| 34 | +#include "utils/rel.h" |
34 | 35 | #include "utils/typcache.h"
|
35 | 36 |
|
36 | 37 | /* These parameters are set by GUC */
|
@@ -2629,32 +2630,51 @@ add_base_clause_to_rel(PlannerInfo *root, Index relid,
|
2629 | 2630 | RestrictInfo *restrictinfo)
|
2630 | 2631 | {
|
2631 | 2632 | RelOptInfo *rel = find_base_rel(root, relid);
|
| 2633 | + RangeTblEntry *rte = root->simple_rte_array[relid]; |
2632 | 2634 |
|
2633 | 2635 | Assert(bms_membership(restrictinfo->required_relids) == BMS_SINGLETON);
|
2634 | 2636 |
|
2635 |
| - /* Don't add the clause if it is always true */ |
2636 |
| - if (restriction_is_always_true(root, restrictinfo)) |
2637 |
| - return; |
2638 |
| - |
2639 | 2637 | /*
|
2640 |
| - * Substitute the origin qual with constant-FALSE if it is provably always |
2641 |
| - * false. Note that we keep the same rinfo_serial. |
| 2638 | + * For inheritance parent tables, we must always record the RestrictInfo |
| 2639 | + * in baserestrictinfo as is. If we were to transform or skip adding it, |
| 2640 | + * then the original wouldn't be available in apply_child_basequals. Since |
| 2641 | + * there are two RangeTblEntries for inheritance parents, one with |
| 2642 | + * inh==true and the other with inh==false, we're still able to apply this |
| 2643 | + * optimization to the inh==false one. The inh==true one is what |
| 2644 | + * apply_child_basequals() sees, whereas the inh==false one is what's used |
| 2645 | + * for the scan node in the final plan. |
| 2646 | + * |
| 2647 | + * We make an exception to this is for partitioned tables. For these, we |
| 2648 | + * always apply the constant-TRUE and constant-FALSE transformations. A |
| 2649 | + * qual which is either of these for a partitioned table must also be that |
| 2650 | + * for all of its child partitions. |
2642 | 2651 | */
|
2643 |
| - if (restriction_is_always_false(root, restrictinfo)) |
| 2652 | + if (!rte->inh || rte->relkind == RELKIND_PARTITIONED_TABLE) |
2644 | 2653 | {
|
2645 |
| - int save_rinfo_serial = restrictinfo->rinfo_serial; |
2646 |
| - |
2647 |
| - restrictinfo = make_restrictinfo(root, |
2648 |
| - (Expr *) makeBoolConst(false, false), |
2649 |
| - restrictinfo->is_pushed_down, |
2650 |
| - restrictinfo->has_clone, |
2651 |
| - restrictinfo->is_clone, |
2652 |
| - restrictinfo->pseudoconstant, |
2653 |
| - 0, /* security_level */ |
2654 |
| - restrictinfo->required_relids, |
2655 |
| - restrictinfo->incompatible_relids, |
2656 |
| - restrictinfo->outer_relids); |
2657 |
| - restrictinfo->rinfo_serial = save_rinfo_serial; |
| 2654 | + /* Don't add the clause if it is always true */ |
| 2655 | + if (restriction_is_always_true(root, restrictinfo)) |
| 2656 | + return; |
| 2657 | + |
| 2658 | + /* |
| 2659 | + * Substitute the origin qual with constant-FALSE if it is provably |
| 2660 | + * always false. Note that we keep the same rinfo_serial. |
| 2661 | + */ |
| 2662 | + if (restriction_is_always_false(root, restrictinfo)) |
| 2663 | + { |
| 2664 | + int save_rinfo_serial = restrictinfo->rinfo_serial; |
| 2665 | + |
| 2666 | + restrictinfo = make_restrictinfo(root, |
| 2667 | + (Expr *) makeBoolConst(false, false), |
| 2668 | + restrictinfo->is_pushed_down, |
| 2669 | + restrictinfo->has_clone, |
| 2670 | + restrictinfo->is_clone, |
| 2671 | + restrictinfo->pseudoconstant, |
| 2672 | + 0, /* security_level */ |
| 2673 | + restrictinfo->required_relids, |
| 2674 | + restrictinfo->incompatible_relids, |
| 2675 | + restrictinfo->outer_relids); |
| 2676 | + restrictinfo->rinfo_serial = save_rinfo_serial; |
| 2677 | + } |
2658 | 2678 | }
|
2659 | 2679 |
|
2660 | 2680 | /* Add clause to rel's restriction list */
|
|
0 commit comments