文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

PostgreSQL 源码解读(43)- 查询语句#28(query_planner函数#5)

2024-04-02 19:55

关注

上一小节介绍了函数query_planner中子函数build_base_rel_tlists/find_placeholders_in_jointree/find_lateral_references的实现逻辑,本节介绍deconstruct_jointree函数的主要实现逻辑。

query_planner代码片段:

     //...
     
     build_base_rel_tlists(root, tlist);//构建"base rels"的投影列
 
     find_placeholders_in_jointree(root);//处理jointree中的PHI
 
     find_lateral_references(root);//处理jointree中Lateral依赖
 
     joinlist = deconstruct_jointree(root);//分解jointree

     
     reconsider_outer_join_clauses(root);//已创建等价类,那么需要重新考虑被下推后处理的外连接表达式
 
     
     generate_base_implied_equalities(root);//等价类构建后,生成因此外加的约束语句
 
     //...

一、重要的数据结构

RelOptInfo
与上节一样,RelOptInfo结构体贯彻逻辑优化和物理优化过程的始终,需不时Review.

 typedef struct RelOptInfo
 {
     NodeTag     type;//节点标识
 
     RelOptKind  reloptkind;//RelOpt类型
 
     
     Relids      relids;         
 
     
     double      rows;           
 
     
     bool        consider_startup;   
     bool        consider_param_startup; 
     bool        consider_parallel;  
 
     
     struct PathTarget *reltarget;   
 
     
     List       *pathlist;       
     List       *ppilist;        
     List       *partial_pathlist;   
     struct Path *cheapest_startup_path;//代价最低的启动路径
     struct Path *cheapest_total_path;//代价最低的整体路径
     struct Path *cheapest_unique_path;//代价最低的获取唯一值的路径
     List       *cheapest_parameterized_paths;//代价最低的参数化?路径链表
 
     
     
     Relids      direct_lateral_relids;  
     Relids      lateral_relids; 
 
     
     //reloptkind=RELOPT_BASEREL时使用的数据结构
     Index       relid;          
     Oid         reltablespace;  
     RTEKind     rtekind;        
     AttrNumber  min_attr;       
     AttrNumber  max_attr;       
     Relids     *attr_needed;    
     int32      *attr_widths;    
     List       *lateral_vars;   
     Relids      lateral_referencers;    
     List       *indexlist;      
     List       *statlist;       
     BlockNumber pages;          
     double      tuples;         
     double      allvisfrac;     
     PlannerInfo *subroot;       
     List       *subplan_params; 
     int         rel_parallel_workers;   
 
     
     //FWD相关信息
     Oid         serverid;       
     Oid         userid;         
     bool        useridiscurrent;    
     
     struct FdwRoutine *fdwroutine;
     void       *fdw_private;
 
     
     //已知的,可保证唯一的Relids链表
     List       *unique_for_rels;    
     List       *non_unique_for_rels;    
 
     
     List       *baserestrictinfo;   
     QualCost    baserestrictcost;   
     Index       baserestrict_min_security;  
     List       *joininfo;       
     bool        has_eclass_joins;   
 
     
     bool        consider_partitionwise_join;    
     Relids      top_parent_relids;  
 
     
     //分区表使用
     PartitionScheme part_scheme;    
     int         nparts;         
     struct PartitionBoundInfoData *boundinfo;   
     List       *partition_qual; 
     struct RelOptInfo **part_rels;  
     List      **partexprs;      
     List      **nullable_partexprs; 
     List       *partitioned_child_rels; 
 } RelOptInfo;

PostponedQual

 
 typedef struct PostponedQual
 {
     Node       *qual;           
     Relids      relids;         
 } PostponedQual;

二、源码解读

deconstruct_jointree函数:

 


 
 List *
 deconstruct_jointree(PlannerInfo *root)
 {
     List       *result;
     Relids      qualscope;
     Relids      inner_join_rels;
     List       *postponed_qual_list = NIL;
 
     
     Assert(root->parse->jointree != NULL &&
            IsA(root->parse->jointree, FromExpr));
 
     
     root->nullable_baserels = NULL;
 
     result = deconstruct_recurse(root, (Node *) root->parse->jointree, false,
                                  &qualscope, &inner_join_rels,
                                  &postponed_qual_list);
 
     
     Assert(postponed_qual_list == NIL);
 
     return result;
 }
 
 
 static List *
 deconstruct_recurse(PlannerInfo *root, Node *jtnode, bool below_outer_join,
                     Relids *qualscope, Relids *inner_join_rels,
                     List **postponed_qual_list)
 {
     List       *joinlist;
 
     if (jtnode == NULL)
     {
         *qualscope = NULL;
         *inner_join_rels = NULL;
         return NIL;
     }
     if (IsA(jtnode, RangeTblRef))//RTR
     {
         int         varno = ((RangeTblRef *) jtnode)->rtindex;
 
         
         *qualscope = bms_make_singleton(varno);//添加到qualscope中
         
         if (root->qual_security_level > 0)
             process_security_barrier_quals(root,
                                            varno,
                                            *qualscope,
                                            below_outer_join);
         
         *inner_join_rels = NULL;//inner_join_rels设置为NULL
         joinlist = list_make1(jtnode);//添加到joinlist中
     }
     else if (IsA(jtnode, FromExpr))//FromExpr
     {
         FromExpr   *f = (FromExpr *) jtnode;
         List       *child_postponed_quals = NIL;
         int         remaining;
         ListCell   *l;
 
         
         *qualscope = NULL;
         *inner_join_rels = NULL;//初始化
         joinlist = NIL;//初始化
         remaining = list_length(f->fromlist);
         foreach(l, f->fromlist)
         {
             Relids      sub_qualscope;
             List       *sub_joinlist;
             int         sub_members;
 
             sub_joinlist = deconstruct_recurse(root, lfirst(l),
                                                below_outer_join,
                                                &sub_qualscope,
                                                inner_join_rels,
                                                &child_postponed_quals);//递归调用
             *qualscope = bms_add_members(*qualscope, sub_qualscope);//添加到qualscope中
             sub_members = list_length(sub_joinlist);//sub-joinlist中的元素个数
             remaining--;//计数
             if (sub_members <= 1 ||
                 list_length(joinlist) + sub_members + remaining <= from_collapse_limit)
                 joinlist = list_concat(joinlist, sub_joinlist);//
             else
                 joinlist = lappend(joinlist, sub_joinlist);//
         }
 
         
         if (list_length(f->fromlist) > 1)
             *inner_join_rels = *qualscope;//JOIN
 
         
         foreach(l, child_postponed_quals)
         {
             PostponedQual *pq = (PostponedQual *) lfirst(l);
 
             if (bms_is_subset(pq->relids, *qualscope))//pq依赖的relids是qualscope的子集
                 distribute_qual_to_rels(root, pq->qual,
                                         false, below_outer_join, JOIN_INNER,
                                         root->qual_security_level,
                                         *qualscope, NULL, NULL, NULL,
                                         NULL);//可以分发到Rels中,构建约束条件等
             else
                 *postponed_qual_list = lappend(*postponed_qual_list, pq);//添加到postponed_qual_list链表
         }
 
         
         foreach(l, (List *) f->quals)//处理表达式
         {
             Node       *qual = (Node *) lfirst(l);
 
             distribute_qual_to_rels(root, qual,
                                     false, below_outer_join, JOIN_INNER,
                                     root->qual_security_level,
                                     *qualscope, NULL, NULL, NULL,
                                     postponed_qual_list);//分发到Rels中,构建约束条件等
         }
     }
     else if (IsA(jtnode, JoinExpr))//JoinExpr
     {
         JoinExpr   *j = (JoinExpr *) jtnode;
         List       *child_postponed_quals = NIL;
         Relids      leftids,
                     rightids,
                     left_inners,
                     right_inners,
                     nonnullable_rels,
                     nullable_rels,
                     ojscope;
         List       *leftjoinlist,
                    *rightjoinlist;
         List       *my_quals;
         SpecialJoinInfo *sjinfo;//特殊连接信息
         ListCell   *l;
 
         
         switch (j->jointype)
         {
             case JOIN_INNER://内连接
                 leftjoinlist = deconstruct_recurse(root, j->larg,
                                                    below_outer_join,
                                                    &leftids, &left_inners,
                                                    &child_postponed_quals);//递归调用
                 rightjoinlist = deconstruct_recurse(root, j->rarg,
                                                     below_outer_join,
                                                     &rightids, &right_inners,
                                                     &child_postponed_quals);
                 *qualscope = bms_union(leftids, rightids);
                 *inner_join_rels = *qualscope;
                 
                 nonnullable_rels = NULL;
                 
                 nullable_rels = NULL;
                 break;
             case JOIN_LEFT:
             case JOIN_ANTI://左连接或者反连接
                 leftjoinlist = deconstruct_recurse(root, j->larg,
                                                    below_outer_join,
                                                    &leftids, &left_inners,
                                                    &child_postponed_quals);
                 rightjoinlist = deconstruct_recurse(root, j->rarg,
                                                     true,
                                                     &rightids, &right_inners,
                                                     &child_postponed_quals);
                 *qualscope = bms_union(leftids, rightids);
                 *inner_join_rels = bms_union(left_inners, right_inners);
                 nonnullable_rels = leftids;
                 nullable_rels = rightids;
                 break;
             case JOIN_SEMI://半连接
                 leftjoinlist = deconstruct_recurse(root, j->larg,
                                                    below_outer_join,
                                                    &leftids, &left_inners,
                                                    &child_postponed_quals);
                 rightjoinlist = deconstruct_recurse(root, j->rarg,
                                                     below_outer_join,
                                                     &rightids, &right_inners,
                                                     &child_postponed_quals);
                 *qualscope = bms_union(leftids, rightids);
                 *inner_join_rels = bms_union(left_inners, right_inners);
                 
                 nonnullable_rels = NULL;
 
                 
                 nullable_rels = NULL;
                 break;
             case JOIN_FULL://全连接
                 leftjoinlist = deconstruct_recurse(root, j->larg,
                                                    true,
                                                    &leftids, &left_inners,
                                                    &child_postponed_quals);
                 rightjoinlist = deconstruct_recurse(root, j->rarg,
                                                     true,
                                                     &rightids, &right_inners,
                                                     &child_postponed_quals);
                 *qualscope = bms_union(leftids, rightids);
                 *inner_join_rels = bms_union(left_inners, right_inners);
                 
                 nonnullable_rels = *qualscope;
                 nullable_rels = *qualscope;
                 break;
             default:
                 
                 elog(ERROR, "unrecognized join type: %d",
                      (int) j->jointype);
                 nonnullable_rels = NULL;    
                 nullable_rels = NULL;
                 leftjoinlist = rightjoinlist = NIL;
                 break;
         }
 
         
         root->nullable_baserels = bms_add_members(root->nullable_baserels,
                                                   nullable_rels);//nullable-side rels
 
         
         my_quals = NIL;//添加到表达式链表中
         foreach(l, child_postponed_quals)
         {
             PostponedQual *pq = (PostponedQual *) lfirst(l);
 
             if (bms_is_subset(pq->relids, *qualscope))
                 my_quals = lappend(my_quals, pq->qual);
             else
             {
                 
                 Assert(j->jointype == JOIN_INNER);
                 *postponed_qual_list = lappend(*postponed_qual_list, pq);
             }
         }
         
         my_quals = list_concat(my_quals, (List *) j->quals);
 
         
         if (j->jointype != JOIN_INNER)//非内连接
         {
             sjinfo = make_outerjoininfo(root,
                                         leftids, rightids,
                                         *inner_join_rels,
                                         j->jointype,
                                         my_quals);//构建特殊连接信息
             if (j->jointype == JOIN_SEMI)
                 ojscope = NULL;//半连接
             else
                 ojscope = bms_union(sjinfo->min_lefthand,
                                     sjinfo->min_righthand);
         }
         else
         {
             sjinfo = NULL;//内连接,设置为NULL
             ojscope = NULL;
         }
 
         
         foreach(l, my_quals)//处理JOIN中的qual表达式
         {
             Node       *qual = (Node *) lfirst(l);
 
             distribute_qual_to_rels(root, qual,
                                     false, below_outer_join, j->jointype,
                                     root->qual_security_level,
                                     *qualscope,
                                     ojscope, nonnullable_rels, NULL,
                                     postponed_qual_list);//处理表达式
         }
 
         
         if (sjinfo)//特殊连接信息
         {
             root->join_info_list = lappend(root->join_info_list, sjinfo);
             
             update_placeholder_eval_levels(root, sjinfo);
         }
 
         
         if (j->jointype == JOIN_FULL)
         {
             
             joinlist = list_make1(list_make2(leftjoinlist, rightjoinlist));
         }
         else if (list_length(leftjoinlist) + list_length(rightjoinlist) <=
                  join_collapse_limit)
         {
             
             joinlist = list_concat(leftjoinlist, rightjoinlist);
         }
         else
         {
             
             Node       *leftpart,
                        *rightpart;
 
             
             if (list_length(leftjoinlist) == 1)
                 leftpart = (Node *) linitial(leftjoinlist);
             else
                 leftpart = (Node *) leftjoinlist;
             if (list_length(rightjoinlist) == 1)
                 rightpart = (Node *) linitial(rightjoinlist);
             else
                 rightpart = (Node *) rightjoinlist;
             joinlist = list_make2(leftpart, rightpart);
         }
     }
     else
     {
         elog(ERROR, "unrecognized node type: %d",
              (int) nodeTag(jtnode));
         joinlist = NIL;         
     }
     return joinlist;
 }

 
 static void
 distribute_qual_to_rels(PlannerInfo *root, Node *clause,
                         bool is_deduced,
                         bool below_outer_join,
                         JoinType jointype,
                         Index security_level,
                         Relids qualscope,
                         Relids ojscope,
                         Relids outerjoin_nonnullable,
                         Relids deduced_nullable_relids,
                         List **postponed_qual_list)
 {
     Relids      relids;
     bool        is_pushed_down;
     bool        outerjoin_delayed;
     bool        pseudoconstant = false;
     bool        maybe_equivalence;
     bool        maybe_outer_join;
     Relids      nullable_relids;
     RestrictInfo *restrictinfo;
 
     
     relids = pull_varnos(clause);//遍历,获取该节点中的所有relids
 
     
     if (!bms_is_subset(relids, qualscope))//不是qualscope的子集
     {
         PostponedQual *pq = (PostponedQual *) palloc(sizeof(PostponedQual));
 
         Assert(root->hasLateralRTEs);   
         Assert(jointype == JOIN_INNER); 
         Assert(!is_deduced);    
         pq->qual = clause;
         pq->relids = relids;
         *postponed_qual_list = lappend(*postponed_qual_list, pq);//添加到postponed_qual_list中,返回
         return;
     }
 
     
     if (ojscope && !bms_is_subset(relids, ojscope))
         elog(ERROR, "JOIN qualification cannot refer to other relations");
 
     
     if (bms_is_empty(relids))//空的relids
     {
         if (ojscope)//外连接
         {
             
             relids = bms_copy(ojscope);
             
         }
         else//非外连接
         {
             
             relids = bms_copy(qualscope);
             if (!contain_volatile_functions(clause))//不存在易变函数
             {
                 
                 pseudoconstant = true;
                 
                 root->hasPseudoConstantQuals = true;
                 
                 if (!below_outer_join)
                 {
                     relids =
                         get_relids_in_jointree((Node *) root->parse->jointree,
                                                false);
                     qualscope = bms_copy(relids);
                 }
             }
         }
     }
 
     
     if (is_deduced)//推导出来?
     {
         
         Assert(!ojscope);//非外连接
         is_pushed_down = true;//可以被下推
         outerjoin_delayed = false;//无需外连接延迟
         nullable_relids = deduced_nullable_relids;//nullable端的relids
         
         maybe_equivalence = false;//不需要反馈更多的推导
         maybe_outer_join = false;
     }
     else if (bms_overlap(relids, outerjoin_nonnullable))//与外连接nonnullable端有交集
     {
         
         is_pushed_down = false;//不能被下推
         maybe_equivalence = false;
         maybe_outer_join = true;//可能是外连接
 
         
         outerjoin_delayed = check_outerjoin_delay(root,
                                                   &relids,
                                                   &nullable_relids,
                                                   false);//检查外连接延迟
 
         
         Assert(ojscope);
         relids = ojscope;
         Assert(!pseudoconstant);
     }
     else//常规的情况
     {
         
         is_pushed_down = true;//可以下推
 
         
         outerjoin_delayed = check_outerjoin_delay(root,
                                                   &relids,
                                                   &nullable_relids,
                                                   true);//检查是否被下层的外连接所延迟
 
         if (outerjoin_delayed)//需延迟
         {
             
             Assert(root->hasLateralRTEs || bms_is_subset(relids, qualscope));
             Assert(ojscope == NULL || bms_is_subset(relids, ojscope));
 
             
             maybe_equivalence = false;
 
             
             if (check_redundant_nullability_qual(root, clause))
                 return;
         }
         else//无需延迟
         {
             
             maybe_equivalence = true;//可能会出现等价类
             if (outerjoin_nonnullable != NULL)
                 below_outer_join = true;
         }
 
         
         maybe_outer_join = false;//不会是外连接
     }
 
     
     restrictinfo = make_restrictinfo((Expr *) clause,
                                      is_pushed_down,
                                      outerjoin_delayed,
                                      pseudoconstant,
                                      security_level,
                                      relids,
                                      outerjoin_nonnullable,
                                      nullable_relids);//构造约束条件
 
     
     if (bms_membership(relids) == BMS_MULTIPLE)//存在多个relids
     {
         List       *vars = pull_var_clause(clause,
                                            PVC_RECURSE_AGGREGATES |
                                            PVC_RECURSE_WINDOWFUNCS |
                                            PVC_INCLUDE_PLACEHOLDERS);//遍历获取Vars
 
         add_vars_to_targetlist(root, vars, relids, false);//添加到相应的投影列中
         list_free(vars);
     }
 
     
     check_mergejoinable(restrictinfo);//检查是否可以合并
 
     
     if (restrictinfo->mergeopfamilies)//可以
     {
         if (maybe_equivalence)//存在合并的可能
         {
             if (check_equivalence_delay(root, restrictinfo) &&
                 process_equivalence(root, &restrictinfo, below_outer_join))
                 return;
             
             if (restrictinfo->mergeopfamilies)  
                 initialize_mergeclause_eclasses(root, restrictinfo);
             
         }
         else if (maybe_outer_join && restrictinfo->can_join)//可能是外连接而且约束条件can_join?
         {
             
             initialize_mergeclause_eclasses(root, restrictinfo);
             
             if (bms_is_subset(restrictinfo->left_relids,
                               outerjoin_nonnullable) &&
                 !bms_overlap(restrictinfo->right_relids,
                              outerjoin_nonnullable))
             {
                 
                 root->left_join_clauses = lappend(root->left_join_clauses,
                                                   restrictinfo);
                 return;
             }
             if (bms_is_subset(restrictinfo->right_relids,
                               outerjoin_nonnullable) &&
                 !bms_overlap(restrictinfo->left_relids,
                              outerjoin_nonnullable))
             {
                 
                 root->right_join_clauses = lappend(root->right_join_clauses,
                                                    restrictinfo);
                 return;
             }
             if (jointype == JOIN_FULL)
             {
                 
                 root->full_join_clauses = lappend(root->full_join_clauses,
                                                   restrictinfo);
                 return;
             }
             
         }
         else
         {
             
             initialize_mergeclause_eclasses(root, restrictinfo);//初始化合并语句的等价类
         }
     }
 
     
     distribute_restrictinfo_to_rels(root, restrictinfo);//分发到PlannerInfo中的子句中
 }
 
 
 
 void
 distribute_restrictinfo_to_rels(PlannerInfo *root,
                                 RestrictInfo *restrictinfo)
 {
     Relids      relids = restrictinfo->required_relids;
     RelOptInfo *rel;
 
     switch (bms_membership(relids))
     {
         case BMS_SINGLETON:
 
             
             rel = find_base_rel(root, bms_singleton_member(relids));
 
             
             rel->baserestrictinfo = lappend(rel->baserestrictinfo,
                                             restrictinfo);
             
             rel->baserestrict_min_security = Min(rel->baserestrict_min_security,
                                                  restrictinfo->security_level);
             break;
         case BMS_MULTIPLE:
 
             
 
             
             check_hashjoinable(restrictinfo);
 
             
             add_join_clause_to_rels(root, restrictinfo, relids);
             break;
         default:
 
             
             elog(ERROR, "cannot cope with variable-free clause");
             break;
     }
 }
 

三、跟踪分析

测试脚本:

testdb=# explain verbose select a.*,b.grbh,b.je 
testdb-# from t_dwxx a,lateral (select t1.dwbh,t1.grbh,t2.je from t_grxx t1 inner join t_jfxx t2 on t1.dwbh = a.dwbh and t1.grbh = t2.grbh) b
testdb-# where a.dwbh = '1001'
testdb-# order by b.dwbh;
                                     QUERY PLAN                                     
------------------------------------------------------------------------------------
 Nested Loop  (cost=15.03..36.10 rows=7 width=558)
   Output: a.dwmc, a.dwbh, a.dwdz, t1.grbh, t2.je, t1.dwbh
   ->  Seq Scan on public.t_dwxx a  (cost=0.00..1.04 rows=1 width=474)
         Output: a.dwmc, a.dwbh, a.dwdz
         Filter: ((a.dwbh)::text = '1001'::text)
   ->  Hash Join  (cost=15.03..34.99 rows=7 width=84)
         Output: t1.grbh, t1.dwbh, t2.je
         Hash Cond: ((t2.grbh)::text = (t1.grbh)::text)
         ->  Seq Scan on public.t_jfxx t2  (cost=0.00..17.20 rows=720 width=46)
               Output: t2.grbh, t2.ny, t2.je
         ->  Hash  (cost=15.00..15.00 rows=2 width=76)
               Output: t1.grbh, t1.dwbh
               ->  Seq Scan on public.t_grxx t1  (cost=0.00..15.00 rows=2 width=76)
                     Output: t1.grbh, t1.dwbh
                     Filter: ((t1.dwbh)::text = '1001'::text) -- 谓词下推
(15 rows)

在deconstruct_jointree上设置断点,启动gdb跟踪:

(gdb) b deconstruct_jointree
Breakpoint 1 at 0x7660e3: file initsplan.c, line 718.
(gdb) c
Continuing.

Breakpoint 1, deconstruct_jointree (root=0x1a498f0) at initsplan.c:718
718   List     *postponed_qual_list = NIL;

进入函数deconstruct_jointree

718   List     *postponed_qual_list = NIL;
(gdb) n
725   root->nullable_baserels = NULL;
(gdb) 
#递归调用deconstruct_recurse
727   result = deconstruct_recurse(root, (Node *) root->parse->jointree, false,
(gdb) 

进入deconstruct_recurse函数

(gdb) step
deconstruct_recurse (root=0x1a498f0, jtnode=0x1a473e0, below_outer_join=false, qualscope=0x7ffe02efe0a0, 
    inner_join_rels=0x7ffe02efe098, postponed_qual_list=0x7ffe02efe090) at initsplan.c:765
765   if (jtnode == NULL)
(gdb) p *jtnode
$1 = {type = T_FromExpr}

处理逻辑进入FromExpr节点

...
804     foreach(l, f->fromlist)
(gdb) 
#递归调用deconstruct_recurse
810       sub_joinlist = deconstruct_recurse(root, lfirst(l),
(gdb) 
#进入deconstruct_recurse
(gdb) step
deconstruct_recurse (root=0x1a498f0, jtnode=0x19bb600, below_outer_join=false, qualscope=0x7ffe02efdfa0, 
    inner_join_rels=0x7ffe02efe098, postponed_qual_list=0x7ffe02efdfa8) at initsplan.c:765
765   if (jtnode == NULL)
(gdb) p *jtnode
#FromExpr->fromlist->head的类型是FromExpr
$2 = {type = T_RangeTblRef}
...
(gdb) n
#返回joinlist(varno=1)
1094    return joinlist;
...
回到FromExpr处理逻辑
815       *qualscope = bms_add_members(*qualscope, sub_qualscope);
(gdb) p *qualscope
$8 = (Relids) 0x0
(gdb) p *sub_qualscope
$9 = {nwords = 1, words = 0x1a920c4}
(gdb) p *sub_qualscope->words
$10 = 2
...
#循环继续处理FromExpr->fromlist
804     foreach(l, f->fromlist)
...
#FromExpr#1->fromlist.2的类型是FromExpr
(gdb) p *jtnode
$15 = {type = T_FromExpr}
...

第2个FromExpr中的fromlist,第1个元素为JoinExpr

#即:FromExpr#2->fromlist.1的类型是JoinExpr
...
(gdb)
810       sub_joinlist = deconstruct_recurse(root, lfirst(l),
#直接执行,处理JoinExpr,返回结果
#24=8+16(即3/4号rtindex)
(gdb) p sub_qualscope->words[0]
$22 = 24
815       *qualscope = bms_add_members(*qualscope, sub_qualscope);
...
#处理完JoinExpr后
...
843       if (bms_is_subset(pq->relids, *qualscope))
(gdb) p *qualscope->words
$38 = 24
(gdb) p *pq->relids->words
$41 = 10
#不是子集,添加到postponed_qual_list,由上层负责处理
(gdb) n
850         *postponed_qual_list = lappend(*postponed_qual_list, pq);
856     foreach(l, (List *) f->quals)
(gdb) 
#第2个FromExpr没有qual,直接返回
1094    return joinlist;
...

回到第1个FromExpr的处理逻辑

(gdb) 
815       *qualscope = bms_add_members(*qualscope, sub_qualscope);
(gdb) p *qualscope->words
$42 = 2
(gdb) p *sub_qualscope->words
$44 = 24
(gdb) n
816       sub_members = list_length(sub_joinlist);
#拼入到qualscope中,26=2+8+16(1/3/4号rte)
(gdb)  p *qualscope->words
$45 = 26
...
843       if (bms_is_subset(pq->relids, *qualscope))
#这时候10是26的子集,因此可以调用distribute_qual_to_rels了
(gdb) n
844         distribute_qual_to_rels(root, pq->qual,
(gdb) step
distribute_qual_to_rels (root=0x1a498f0, clause=0x1a56ab0, is_deduced=false, below_outer_join=false, jointype=JOIN_INNER, 
    security_level=0, qualscope=0x1a920d8, ojscope=0x0, outerjoin_nonnullable=0x0, deduced_nullable_relids=0x0, 
    postponed_qual_list=0x0) at initsplan.c:1656
1656    bool    pseudoconstant = false;
#进入distribute_qual_to_rels
#clause是t_dwxx.dwbh = t_grxx.dwbh
...
(gdb) n

构造约束条件

1897    restrictinfo = make_restrictinfo((Expr *) clause,
...
(gdb) p *restrictinfo
$63 = {type = T_RestrictInfo, clause = 0x1a56ab0, is_pushed_down = true, outerjoin_delayed = false, can_join = true, 
  pseudoconstant = false, leakproof = false, security_level = 0, clause_relids = 0x1a92840, required_relids = 0x1a926e8, 
  outer_relids = 0x0, nullable_relids = 0x0, left_relids = 0x1a92810, right_relids = 0x1a92828, orclause = 0x0, 
  parent_ec = 0x0, eval_cost = {startup = -1, per_tuple = 0}, norm_selec = -1, outer_selec = -1, 
  mergeopfamilies = 0x1a92878, left_ec = 0x0, right_ec = 0x0, left_em = 0x0, right_em = 0x0, scansel_cache = 0x0, 
  outer_is_left = false, hashjoinoperator = 0, left_bucketsize = -1, right_bucketsize = -1, left_mcvfreq = -1, 
  right_mcvfreq = -1}
(gdb) n
1971        if (check_equivalence_delay(root, restrictinfo) &&
(gdb) 
1972          process_equivalence(root, &restrictinfo, below_outer_join))
(gdb) 
1971        if (check_equivalence_delay(root, restrictinfo) &&
(gdb)

检查&处理等价类,如OK,则返回

1973          return;
(gdb) 
deconstruct_recurse (root=0x1a498f0, jtnode=0x1a473e0, below_outer_join=false, qualscope=0x7ffe02efe0a0, 
    inner_join_rels=0x7ffe02efe098, postponed_qual_list=0x7ffe02efe090) at initsplan.c:839
839     foreach(l, child_postponed_quals)
(gdb) 
856     foreach(l, (List *) f->quals)
#处理第1个FromExpr的quals(即dwbh='1001')
(gdb) n
858       Node     *qual = (Node *) lfirst(l);
(gdb) 
860       distribute_qual_to_rels(root, qual,
(gdb) 
856     foreach(l, (List *) f->quals)
(gdb) 
#返回joinlist(3个RTR)
1094    return joinlist;
(gdb) n
1095  }
(gdb) 
deconstruct_jointree (root=0x1a498f0) at initsplan.c:734
734   return result;

执行完毕,在PlannerInfo中产生了两个等价类

(gdb) p *root
$85 = {type = T_PlannerInfo, parse = 0x19bb1a0, glob = 0x1a53ee8, query_level = 1, parent_root = 0x0, plan_params = 0x0, 
  outer_params = 0x0, simple_rel_array = 0x1a90568, simple_rel_array_size = 6, simple_rte_array = 0x1a905b8, 
  all_baserels = 0x0, nullable_baserels = 0x0, join_rel_list = 0x0, join_rel_hash = 0x0, join_rel_level = 0x0, 
  join_cur_level = 0, init_plans = 0x0, cte_plan_ids = 0x0, multiexpr_params = 0x0, eq_classes = 0x1a92650, 
  canon_pathkeys = 0x0, left_join_clauses = 0x0, right_join_clauses = 0x0, full_join_clauses = 0x0, join_info_list = 0x0, 
  append_rel_list = 0x0, rowMarks = 0x0, placeholder_list = 0x0, fkey_list = 0x0, query_pathkeys = 0x0, 
  group_pathkeys = 0x0, window_pathkeys = 0x0, distinct_pathkeys = 0x0, sort_pathkeys = 0x0, part_schemes = 0x0, 
  initial_rels = 0x0, upper_rels = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, upper_targets = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 
    0x0}, processed_tlist = 0x1a56160, grouping_map = 0x0, minmax_aggs = 0x0, planner_cxt = 0x1997040, 
  total_table_pages = 0, tuple_fraction = 0, limit_tuples = -1, qual_security_level = 0, inhTargetKind = INHKIND_NONE, 
  hasJoinRTEs = true, hasLateralRTEs = true, hasDeletedRTEs = false, hasHavingQual = false, hasPseudoConstantQuals = false, 
  hasRecursion = false, wt_param_id = -1, non_recursive_path = 0x0, curOuterRels = 0x0, curOuterParams = 0x0, 
  join_search_private = 0x0, partColsUpdated = false}
(gdb) p *root->eq_classes
$86 = {type = T_List, length = 2, head = 0x1a92630, tail = 0x1a92ad0}
(gdb) p *(Node *)root->eq_classes->head->data.ptr_value
$87 = {type = T_EquivalenceClass}
(gdb) p *(EquivalenceClass *)root->eq_classes->head->data.ptr_value
$88 = {type = T_EquivalenceClass, ec_opfamilies = 0x1a92350, ec_collation = 100, ec_members = 0x1a92590, 
  ec_sources = 0x1a924d8, ec_derives = 0x0, ec_relids = 0x1a92558, ec_has_const = false, ec_has_volatile = false, 
  ec_below_outer_join = false, ec_broken = false, ec_sortref = 0, ec_min_security = 0, ec_max_security = 0, ec_merged = 0x0}
(gdb) p *(EquivalenceClass *)root->eq_classes->head->next->data.ptr_value
$89 = {type = T_EquivalenceClass, ec_opfamilies = 0x1a92878, ec_collation = 100, ec_members = 0x1a92a30, 
  ec_sources = 0x1a92978, ec_derives = 0x0, ec_relids = 0x1a929f8, ec_has_const = true, ec_has_volatile = false, 
  ec_below_outer_join = false, ec_broken = false, ec_sortref = 0, ec_min_security = 0, ec_max_security = 0, ec_merged = 0x0}
(gdb) 

第1个等价类有2个Member

(gdb) p *((EquivalenceClass *)root->eq_classes->head->data.ptr_value)->ec_members
$91 = {type = T_List, length = 2, head = 0x1a92570, tail = 0x1a92610}

第2个等价类有3个Member

(gdb) p *((EquivalenceClass *)root->eq_classes->head->next->data.ptr_value)->ec_members
$97 = {type = T_List, length = 3, head = 0x1a92a10, tail = 0x1a92d08}

等价类的解释下节再行介绍.

四、参考资料

initsplan.c

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 资料下载
  • 历年真题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯