这篇文章主要介绍“PostgreSQL在执行逻辑优化中相关的数据结构有哪些”,在日常操作中,相信很多人在PostgreSQL在执行逻辑优化中相关的数据结构有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”PostgreSQL在执行逻辑优化中相关的数据结构有哪些”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

PostgreSQL在执行逻辑优化中相关的数据结构包括RangeSubselect/Alias/RangeVar/ResTarget/ColumnRef.

一、数据结构

RangeSubselect
出现在FROM语句中的子查询

/**RangeSubselect-subqueryappearinginaFROMclause*出现在FROM语句中的子查询*/typedefstructRangeSubselect{NodeTagtype;//是否有LATERAL?boollateral;/*doesithaveLATERALprefix?*///未转换的子查询语句Node*subquery;/*theuntransformedsub-selectclause*///表别名和可选的列别名Alias*alias;/*tablealias&optionalcolumnaliases*/}RangeSubselect;

Alias
为RangeVar指定别名.别名可能同时重命名了表列.

/**Alias-*specifiesanaliasforarangevariable;thealiasmightalso*specifyrenamingofcolumnswithinthetable.*为RangeVar指定别名.别名可能同时重命名了表列.**Note:colnamesisalistofValuenodes(alwaysstrings).InAliasstructs*associatedwithRTEs,theremaybeentriescorrespondingtodropped*columns;thesearenormallyemptystrings("").Seeparsenodes.hforinfo.*注意:colnames是Value节点(通常是字符串)链表.*在与RTEs相关的Alias结构体中,可能有跟已删除的列对应的条目.*这些通常是空字符串("").详细可参考parsenodes.h*/typedefstructAlias{NodeTagtype;//别名char*aliasname;/*aliasedrelname(neverqualified)*///列别名链表List*colnames;/*optionallistofcolumnaliases*/}Alias;

RangeVar
range variable,用于FROM语句中.

/**RangeVar-rangevariable,usedinFROMclauses*rangevariable,用于FROM语句中.**Alsousedtorepresenttablenamesinutilitystatements;there,thealias*fieldisnotused,andinhtellswhethertoapplytheoperation*recursivelytochildtables.Insomecontextsitisalsousefultocarry*aTEMPtableindicationhere.*在工具语句中,用于表示表名,这时alias字段没有使用.*inh用于判断是否在子表中递归应用相关的操作.*/typedefstructRangeVar{NodeTagtype;char*catalogname;/*thecatalog(database)name,orNULL*/char*schemaname;/*theschemaname,orNULL*/char*relname;/*therelation/sequencename*/boolinh;/*expandrelbyinheritance?recursivelyact*onchildren?*/charrelpersistence;/*seeRELPERSISTENCE_*inpg_class.h*/Alias*alias;/*tablealias&optionalcolumnaliases*/intlocation;/*tokenlocation,or-1ifunknown*/}RangeVar;

ResTarget
结果目标列(用于先前已转换的解析树的目标链表中)

/**ResTarget-*resulttarget(usedintargetlistofpre-transformedparsetrees)*结果目标列(用于先前已转换的解析树的目标链表中)**InaSELECTtargetlist,'name'isthecolumnlabelfroman*'ASColumnLabel'clause,orNULLiftherewasnone,and'val'isthe*valueexpressionitself.The'indirection'fieldisnotused.*在SELECT的目标链表中,*'name'是'ASColumnLabel'语句中的列标签,如无则为NULL,*'val'是value表达式本身.*'indirection'未使用.**INSERTusesResTargetinitstarget-column-nameslist.Here,'name'is*thenameofthedestinationcolumn,'indirection'storesanysubscripts*attachedtothedestination,and'val'isnotused.*INSERT在target-column-names链表中使用ResTarget.*这里'name'是目标列名称,'indirection'存储了所有与目标相关的子脚本,'val'未使用.**InanUPDATEtargetlist,'name'isthenameofthedestinationcolumn,*'indirection'storesanysubscriptsattachedtothedestination,and*'val'istheexpressiontoassign.*在UPDATE目标链表中,'name'是目标列名称,'indirection'存储了所有与目标相关的子脚本,*'val'是与赋值相关的表达式.**SeeA_Indirectionformoreinfoaboutwhatcanappearin'indirection'.*详细参见A_Indirection('indirection')*/typedefstructResTarget{NodeTagtype;//列名或NULLchar*name;/*columnnameorNULL*///子脚本,字段名称,'*'或NILList*indirection;/*subscripts,fieldnames,and'*',orNIL*///需要计算或赋值的值表达式Node*val;/*thevalueexpressiontocomputeorassign*///token的位置,-1表示未知intlocation;/*tokenlocation,or-1ifunknown*/}ResTarget;

ColumnRef
指定对列或整个元组的引用

/**ColumnRef-specifiesareferencetoacolumn,orpossiblyawholetuple*指定对列或整个元组的引用**The"fields"listmustbenonempty.ItcancontainstringValuenodes*(representingnames)andA_Starnodes(representingoccurrenceofa'*').*Currently,A_Starmustappearonlyasthelastlistelement---thegrammar*isresponsibleforenforcingthis!*"fields"链表不能为空.可能包含字符串Value节点和A_Star节点(表示出现了*).*截止到目前为止,A_Star必须出现在最后一个链表元素中.**Note:anycontainersubscriptingorselectionoffieldsfromcompositecolumns*isrepresentedbyanA_IndirectionnodeabovetheColumnRef.However,*forsimplicityinthenormalcase,initialfieldselectionfromatable*nameisrepresentedwithinColumnRefandnotbyaddingA_Indirection.*/typedefstructColumnRef{NodeTagtype;//字段名称(字符串值)链表或A_StarList*fields;/*fieldnames(Valuestrings)orA_Star*///token位置intlocation;/*tokenlocation,or-1ifunknown*/}ColumnRef;二、源码解读

N/A

三、跟踪分析

RangeSubselect/Alias

(gdb)p*(Node*)($stmt->fromClause->head.data->ptr_value)$15={type=T_RangeSubselect}#实际类型是范围子查询RangeSubselect(gdb)set$fromclause=(RangeSubselect*)($stmt->fromClause->head.data->ptr_value)(gdb)p*$fromclause$16={type=T_RangeSubselect,lateral=false,subquery=0x1666c18,alias=0x1666d40}(gdb)p*($fromclause->subquery)#subquery,子查询是SelectStmt类型的节点$17={type=T_SelectStmt}(gdb)p*($fromclause->alias)#alias,别名,实际值是字符串ret$18={type=T_Alias,aliasname=0x1666d28"ret",colnames=0x0}

RangeVar

...$43={type=T_RangeVar}(gdb)p*(RangeVar*)((JoinExpr*)($joinexpr->larg))->larg$44={type=T_RangeVar,catalogname=0x0,schemaname=0x0,relname=0x1643380"t_dwxx",inh=true,relpersistence=112'p',alias=0x0,location=82}...

ResTarget

...(gdb)p*(Node*)($subquerylarg->targetList->head.data->ptr_value)$26={type=T_ResTarget}(gdb)set$subvar=(ResTarget*)($subquerylarg->targetList->head.data->ptr_value)(gdb)p*$subvar{type=T_ResTarget,name=0x0,indirection=0x0,val=0x1642c70,location=23}...

ColumnRef

...(gdb)p*$restarget->val$25={type=T_ColumnRef}(gdb)p*(ColumnRef*)$restarget->val$26={type=T_ColumnRef,fields=0x1a47a08,location=7}(gdb)p*((ColumnRef*)$restarget->val)->fields$27={type=T_List,length=2,head=0x1a47a88,tail=0x1a479e8}(gdb)p*(Node*)(((ColumnRef*)$restarget->val)->fields)->head.data->ptr_value$32={type=T_String}#fields链表的第1个元素是数据表,第2个元素是数据列(gdb)p*(Value*)(((ColumnRef*)$restarget->val)->fields)->head.data->ptr_value$37={type=T_String,val={ival=27556248,str=0x1a47998"t_dwxx"}}(gdb)p*(Value*)(((ColumnRef*)$restarget->val)->fields)->tail.data->ptr_value$38={type=T_String,val={ival=27556272,str=0x1a479b0"dwmc"}}...

到此,关于“PostgreSQL在执行逻辑优化中相关的数据结构有哪些”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!