openGauss的SQL by pass框架

执行引擎一般负责查询的执行,执行引擎在sql执行栈中起到接收优化器生成的执行计划plan、并对通过存储引擎提供的数据读写接口,实现对数据进行计算得到查询的结果集。
在典型的oltp场景中,简单查询占了很大一部分比例。这种查询的特征是只涉及单表和简单表达式的查询,因此为了加速这类查询,opengauss提出了sql by pass框架,在parse层对这类查询做简单的模式判别后,进入到特殊的执行路径里,跳过经典的执行器执行框架,包括算子的初始化与执行、表达式与投影等经典框架,直接重写一套简洁的执行路径,并且直接调用存储接口,这样可以大大加速简单查询的执行速度。  
  01
  sql by pass
enable_opfusion用于控制是否对简单增删改查进行优化,简单insert语句在开启enable_opfusion时的执行计划如下: 由于开启sql by pass,从exec_simple_query过来的语句,会判断可以走sql by pass,否则进入createportal走经典执行流程。
                                                      static void exec_simple_query(const char* query_string, messagetype messagetype, stringinfo msg = null){ … /* sql bypass */ if (runopfusioncheck) { // 进入sql by pass (void)memorycontextswitchto(oldcontext); void* opfusionobj = opfusion::fusionfactory( opfusion::getfusiontype(null, null, plantree_list), oldcontext, null, plantree_list, null); if (opfusionobj != null) { ((opfusion*)opfusionobj)->setcurrentopfusionobj((opfusion*)opfusionobj); if (opfusion::process(fusion_execute, null, completiontag, istoplevel, null)) { commandcounterincrement(); finish_xact_command(); endcommand(completiontag, dest); memorycontextreset(optimizercontext); break; } assert(0); } (void)memorycontextswitchto(t_thrd.mem_cxt.msg_mem_cxt); } /* * create unnamed portal to run the query or queries in. if there * already is one, silently drop it. */ portal = createportal(, true, true); // 经典执行流程 … 进入insertfusion::execute完成数据插入操作。
                #0 insertfusion::execute (this=0x7fd93a4104f8, max_rows=9223372036854775807, completiontag=0x7fd933e67020 @p34663331177) at opfusion_insert.cpp:297#1 0x0000000001ac00d9 in opfusion::fusionexecute (this=0x7fd93a4104f8, msg=0x0, completiontag=0x7fd933e67020 @p34663331177, istoplevel=true, isquerycompleted=0x0) at opfusion.cpp:453#2 0x0000000001ac0389 in opfusion::process (op=0, msg=0x0, completiontag=0x7fd933e67020 @p34663331177, istoplevel=true, isquerycompleted=0x0) at opfusion.cpp:491#3 0x000000000193a910 in exec_simple_query (query_string=0x7fd966ad2060 insert into t1 values(1,200);, messagetype=query_message, msg=0x7fd933e67210) at postgres.cpp:2624  
sql by pass适应的场景有:
只支持indexscan和indexonlyscan,且全部where语句的过滤条件都在索引上。
只支持单表增删改查,不支持join、using。
只支持行存表,不支持分区表,表不支持有触发器。
不支持active sql、qps等信息统计特性。
不支持正在扩容和缩容的表。
不支持查询或者修改系统列。
只支持简单select语句,例如:  
  select c3 from t1 where c1 = ? and c2 =10; 仅可以查询目标表的列,c1和c2列为索引列,后边可以是常量或者参数,可以使用 for update。
只支持简单insert语句,例如:
  insert into t1 values (?,10,?); 仅支持一个values,values里面的类型可以是常量和参数,不支持returning。
只支持简单delete语句,例如:
  delete from t1 where c1 = ? and c2 = 10; c1和c2列为索引列,后边可以是常量或者参数。
只支持简单update语句,例如:
  update t1 set c3 = c3+? where c1 = ? and c2 = 10; c3列修改的值可以是常量和参数,也可以是一个简单的表达式,c1和c2列为索引列,后边可以是常量或者参数。
  02
  经典的执行器
关闭enable_opfusion,简单insert的执行计划是这样的: 在这种执行流程中portal是执行sql语句的载体,每一条sql对应唯一的portal,不同的查询类型对应的portal类型也有区别。
              typedef enum portalstrategy { portal_one_select, // sql语句包含单一的select查询 portal_one_returning, // insert/update/delete语句包含returning portal_one_mod_with, // 查询语句包含with portal_util_select, // 工具类型查询语句,如explain portal_multi_query // 所有其他类型查询语句} portalstrategy; portal的生命周期管理在exec_simple_query函数中实现,该函数负责portal创建、执行和清理。portal执行的主要执行流程包括portalstart函数、portalrun函数、portaldrop函数几个部分。其中portalstart函数负责进行portal结构体初始化工作,包括执行算子初始化、内存上下文分配等;portalrun函数负责真正的执行和运算,它是执行器的核心;portaldrop函数负责最后的清理工作,主要是数据结构、缓存的清理。 portalrun函数根据查询类型进入不同的处理函数:
                                  bool portalrun( portal portal, long count, bool istoplevel, destreceiver* dest, destreceiver* altdest, char* completiontag){ … switch (portal->strategy) { case portal_one_select: … case portal_multi_query: // insert从这里进入 portalrunmulti(portal, istoplevel, dest, altdest, completiontag); /* prevent portal's commands from being re-executed */ markportaldone(portal); /* always complete at end of runmulti */ result = true; break; …} 最终执行execinsertt完成数据插入。
                                        #0 execinsertt (state=0x7fdbf1836060, slot=0x7fdbf0c86460, planslot=0x7fdbf0c86460, estate=0x7fdbf0c74060, cansettag=true, options=0, partitionlist=0x7fdbf3125860) at nodemodifytable.cpp:800#1 0x0000000001a684cd in execmodifytable (node=0x7fdbf1836060) at nodemodifytable.cpp:3043#2 0x00000000019f3f93 in execmodifytablewrap (node=0x7fdbf1836060) at execprocnode.cpp:785#3 0x00000000019f43b5 in execprocnode (node=0x7fdbf1836060) at execprocnode.cpp:1038#4 0x00000000019ed9d5 in executeplan (estate=0x7fdbf0c74060, planstate=0x7fdbf1836060, operation=cmd_insert, sendtuples=false, numbertuples=0, direction=forwardscandirection, dest=0x7fdbf13bb9c8, motjitcontext=0x0) at execmain.cpp:2163#5 0x00000000019ea25a in standard_executorrun (querydesc=0x7fdbf1558060, direction=forwardscandirection, count=0) at execmain.cpp:608#6 0x000000000181d6ef in explain_executorrun (querydesc=0x7fdbf1558060, direction=forwardscandirection, count=0) at auto_explain.cpp:121#7 0x00000000019e9dee in executorrun (querydesc=0x7fdbf1558060, direction=forwardscandirection, count=0) at execmain.cpp:486#8 0x000000000194fed6 in processquery (plan=0x7fdbf0b7b2e0, sourcetext=0x7fdbf13ba060 insert into t1 values(1,200);, params=0x0, ismottable=false, motjitcontext=0x0, dest=0x7fdbf13bb9c8, completiontag=0x7fdbf3126020 ) at pquery.cpp:292#9 0x0000000001953fa1 in portalrunmulti (portal=0x7fdbf0c7a060, istoplevel=true, dest=0x7fdbf13bb9c8, altdest=0x7fdbf13bb9c8, completiontag=0x7fdbf3126020 ) at pquery.cpp:1889#10 0x00000000019525e0 in portalrun (portal=0x7fdbf0c7a060, count=9223372036854775807, istoplevel=true, dest=0x7fdbf13bb9c8, altdest=0x7fdbf13bb9c8, completiontag=0x7fdbf3126020 ) at pquery.cpp:1191#11 0x000000000193ac65 in exec_simple_query (query_string=0x7fdbf13ba060 insert into t1 values(1,200);, messagetype=query_message, msg=0x7fdbf3126210) at postgres.cpp:2720 以上分析了简单insert语句的两种执行流程,对于delete,update,select基本工作流程一致。  


LG宣布参展2019AWE,并将带来多款新品
vivo即将推出120W超快闪充技术13分钟就能充满4000mAh的手机
汽车线束设计流程及要点
紫光国芯未来有权对长江存储进行整合
石墨烯反点纳米带横向异质结带阶匹配及输运特性
openGauss的SQL by pass框架
旋进旋涡流量计的原理
AI的神话和显示分别是怎样的
为什么需要测量燃料电池的电阻值呢?如何测量呢?
手机屏触控在戴手套时为啥不管用
介绍一下2023年硬核的电池技术
我国LED封装行业竞争优势及定位的详细介绍和分析资料概述
从四个方面来详细聊聊泰捷盒子的真实使用情况
双极性电源是如何提供电流的
PI衰减器简介
便携汽车轮胎充气泵方案——鼎盛合电子方案
紧随时代脚步:中国人工智能发展程度居于世界前列
Zephyr中断服务类型及实际应用
东方风电成功签约国新集团辽宁鞍山融智新能源分散式项目 总装机容量达82.5MW
液晶拼接屏出现色差的原因