PostgreSQL在执行逻辑优化中相关的数据结构是什么

50次阅读
没有评论

共计 4392 个字符,预计需要花费 11 分钟才能阅读完成。

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

一、数据结构

FromExpr
表示 FROM … WHERE 结构

/*----------
 * FromExpr - represents a FROM ... WHERE ... construct
 *  表示 FROM ... WHERE 结构
 *
 * This is both more flexible than a JoinExpr (it can have any number of
 * children, including zero) and less so --- we don t need to deal with
 * aliases and so on. The output column set is implicitly just the union
 * of the outputs of the children.
 *  该结构比 JoinExpr(有 0..n 个子节点) 更为灵活  --  不需要处理别名等.
 *  输出列集合是子集输出的汇总.
 *----------
 */
typedef struct FromExpr
 NodeTag type;
 // 连接子树链表
 List *fromlist; /* List of join subtrees */
 //join 中的表达式
 Node *quals; /* qualifiers on join, if any */
} FromExpr;

JoinExpr
用于 SQL JOIN 表达式.

/*----------
 * JoinExpr - for SQL JOIN expressions
 *  用于 SQL JOIN 表达式.
 *
 * isNatural, usingClause, and quals are interdependent. The user can write
 * only one of NATURAL, USING(), or ON() (this is enforced by the grammar).
 * If he writes NATURAL then parse analysis generates the equivalent USING()
 * list, and from that fills in  quals  with the right equality comparisons.
 * If he writes USING() then  quals  is filled with equality comparisons.
 * If he writes ON() then only  quals  is set. Note that NATURAL/USING
 * are not equivalent to ON() since they also affect the output column list.
 * isNatural, usingClause, and quals 是相互依赖的.
 *  用户只可以使用 NATURAL, USING(), or ON()(语法限制).
 *  如果是 NATURAL, 则解析器会产生相应的 USING() 链表, 并使用正确的等值比较表达式填充 quals.
 *  如果是 USING(), 则使用正确的等值比较表达式填充 quals.
 *  如果是 ON(), 则只设置 quals 字段.
 *  注意 NATURAL/USING 与 ON() 并不相同, 因为它们同时影响了输出列链表.
 *
 * alias is an Alias node representing the AS alias-clause attached to the
 * join expression, or NULL if no clause. NB: presence or absence of the
 * alias has a critical impact on semantics, because a join with an alias
 * restricts visibility of the tables/columns inside it.
 * alias 表示与 join 表达式相关的 AS 别名子句, 如无则为 NULL.
 *  注意: 别名的存在与否对语义有很大影响, 因此有别名的 join 限制了其中表 / 列的可见性.
 *
 * During parse analysis, an RTE is created for the Join, and its index
 * is filled into rtindex. This RTE is present mainly so that Vars can
 * be created that refer to the outputs of the join. The planner sometimes
 * generates JoinExprs internally; these can have rtindex = 0 if there are
 * no join alias variables referencing such joins.
 *  在解析时,RTE 在参与 Join 时解析, 编号填充到 rtindex 中.
 *  该 RTE 存在的目的主要是可以创建引用 join 输出的 Vars.
 *  计划器有时候会在内部生成 JoinExprs; 如没有 join 别名变量参考这样的连接, 那么 rtindex = 0
 *----------
 */
typedef struct JoinExpr
 NodeTag type;
 //join 类型
 JoinType jointype; /* type of join */
 // 自然连接?
 bool isNatural; /* Natural join? Will need to shape table */
 // 左树
 Node *larg; /* left subtree */
 // 右树
 Node *rarg; /* right subtree */
 //USING 语句 (String 链表)
 List *usingClause; /* USING clause, if any (list of String) */
 //join 限定符
 Node *quals; /* qualifiers on join, if any */
 // 别名语句
 Alias *alias; /* user-written alias clause, if any */
 // 分配给 join 的 RT 编号, 或者为 0
 int rtindex; /* RT index assigned for join, or 0 */
} JoinExpr;

二、源码解读

N/A

三、跟踪分析

...
(gdb) p *(FromExpr *)($rte_sq_rte- subquery- jointree)
$44 = {type = T_FromExpr, fromlist = 0x16fda18, quals = 0x16fe0f0}
(gdb) set $rtesq2_jointree=(FromExpr *)($rte_sq_rte- subquery- jointree)
(gdb) p *$rtesq2_jointree- fromlist
$48 = {type = T_List, length = 1, head = 0x16fd9f8, tail = 0x16fd9f8}
(gdb) p *(Node *)$rtesq2_jointree- fromlist- head- data.ptr_value
$49 = {type = T_JoinExpr}
(gdb) set $tmpvar=(JoinExpr *)$rtesq2_jointree- fromlist- head- data.ptr_value
(gdb) p *$tmpvar
$3 = {type = T_JoinExpr, jointype = JOIN_INNER, isNatural = false, larg = 0x2b68730, rarg = 0x2c215e8, usingClause = 0x0, quals = 0x2c28130, alias = 0x0, rtindex = 5}
(gdb) p *$tmpvar- larg
$4 = {type = T_JoinExpr}
(gdb) p *(JoinExpr *)$tmpvar- larg
$5 = {type = T_JoinExpr, jointype = JOIN_INNER, isNatural = false, larg = 0x2c1e848, rarg = 0x2c1ebd8, 
 usingClause = 0x0, quals = 0x2c20c48, alias = 0x0, rtindex = 3}
(gdb) p *(JoinExpr *)$tmpvar- rarg
$6 = {type = T_RangeTblRef, jointype = JOIN_SEMI, isNatural = 8, larg = 0x2b66de0, rarg = 0x636d7764, 
 usingClause = 0x10, quals = 0x2b66de0, alias = 0xda, rtindex = 46274048}
(gdb) p *(JoinExpr *)$tmpvar- quals
$7 = {type = T_OpExpr, jointype = 98, isNatural = 67, larg = 0x0, rarg = 0x64, usingClause = 0x2c27fb8, 
 quals = 0xa9, alias = 0x0, rtindex = 0}
...

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

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-24发表,共计4392字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)