Postgresql源码(112)plpgsql执行sql时变量何时替换为值

相关
《Postgresql源码(41)plpgsql函数编译执行流程分析》
《Postgresql源码(46)plpgsql中的变量类型及对应关系》
《Postgresql源码(49)plpgsql函数编译执行流程分析总结》
《Postgresql源码(53)plpgsql语法解析关键流程、函数分析》
《Postgresql源码(112)plpgsql执行sql时变量何时替换为值》

0 用例和问题

drop table d1;
create table d1(a varchar(32));do $$
declarekk varchar(32);
beginkk := 'abcd';insert into d1 values (kk);
end;
$$;select * from d1;

请问:insert执行时,kk变量的值是在哪里换成具体的字符串的。

下文总结:

  1. 在语义分析阶段,走钩子函数plpgsql_post_column_ref确认变量存在,并在Query树上挂Param节点记录变量在PL变量数组中的位置和类型。
  2. 在优化器中,走钩子函数plpgsql_param_fetch拿变量具体的值,然后用Const常量节点替换Param变量节点。

1 _SPI_prepare_plan→语义分析:transform阶段回调plpgsql_post_column_ref得到指向kk的Param

#0  make_datum_param (expr=0x2c508f0, dno=1, location=23) at pl_comp.c:1362
#1  0x00007fbb4f3d4499 in resolve_column_ref (pstate=0x2c56130, expr=0x2c508f0, cref=0x2c55e10, error_if_no_field=true) at pl_comp.c:1279
#2  0x00007fbb4f3d4048 in plpgsql_post_column_ref (pstate=0x2c56130, cref=0x2c55e10, var=0x0) at pl_comp.c:1125
#3  0x000000000063244f in transformColumnRef (pstate=0x2c56130, cref=0x2c55e10) at parse_expr.c:804
#4  0x0000000000631121 in transformExprRecurse (pstate=0x2c56130, expr=0x2c55e10) at parse_expr.c:137
#5  0x00000000006310b3 in transformExpr (pstate=0x2c56130, expr=0x2c55e10, exprKind=EXPR_KIND_VALUES_SINGLE) at parse_expr.c:116
#6  0x000000000064a231 in transformExpressionList (pstate=0x2c56130, exprlist=0x2c55eb0, exprKind=EXPR_KIND_VALUES_SINGLE, allowDefault=true) at parse_target.c:272
#7  0x00000000005e88db in transformInsertStmt (pstate=0x2c56130, stmt=0x2c56060) at analyze.c:889
#8  0x00000000005e79be in transformStmt (pstate=0x2c56130, parseTree=0x2c56060) at analyze.c:344
#9  0x00000000005e792f in transformOptionalSelectInto (pstate=0x2c56130, parseTree=0x2c56060) at analyze.c:306
#10 0x00000000005e77f3 in transformTopLevelStmt (pstate=0x2c56130, parseTree=0x2c560b0) at analyze.c:256
#11 0x00000000005e76de in parse_analyze_withcb (parseTree=0x2c560b0, sourceText=0x2c50980 "insert into d1 values (kk)", parserSetup=0x7fbb4f3d3f1d <plpgsql_parser_setup>, parserSetupArg=0x2c508f0, queryEnv=0x0) at analyze.c:203
#12 0x00000000009b71b6 in pg_analyze_and_rewrite_withcb (parsetree=0x2c560b0, query_string=0x2c50980 "insert into d1 values (kk)", parserSetup=0x7fbb4f3d3f1d <plpgsql_parser_setup>, parserSetupArg=0x2c508f0, queryEnv=0x0) at postgres.c:781
#13 0x000000000079906a in _SPI_prepare_plan (src=0x2c50980 "insert into d1 values (kk)", plan=0x7ffe8928dc90) at spi.c:2265
#14 0x0000000000796df8 in SPI_prepare_extended (src=0x2c50980 "insert into d1 values (kk)", options=0x7ffe8928dd10) at spi.c:925
#15 0x00007fbb4f3de778 in exec_prepare_plan (estate=0x7ffe8928e060, expr=0x2c508f0, cursorOptions=2048) at pl_exec.c:4193
#16 0x00007fbb4f3de898 in exec_stmt_execsql (estate=0x7ffe8928e060, stmt=0x2c509b0) at pl_exec.c:4233
#17 0x00007fbb4f3da092 in exec_stmts (estate=0x7ffe8928e060, stmts=0x2c50840) at pl_exec.c:2091
#18 0x00007fbb4f3d9c68 in exec_stmt_block (estate=0x7ffe8928e060, block=0x2c50a00) at pl_exec.c:1942
#19 0x00007fbb4f3d946d in exec_toplevel_block (estate=0x7ffe8928e060, block=0x2c50a00) at pl_exec.c:1633
#20 0x00007fbb4f3d7415 in plpgsql_exec_function (func=0x2c53de0, fcinfo=0x7ffe8928e2a0, simple_eval_estate=0x2c4b748, simple_eval_resowner=0x2b40478, procedure_resowner=0x2b40478, atomic=false) at pl_exec.c:622
#21 0x00007fbb4f3f1dae in plpgsql_inline_handler (fcinfo=0x7ffe8928e390) at pl_handler.c:368
#22 0x0000000000b80adb in FunctionCall1Coll (flinfo=0x7ffe8928e3f0, collation=0, arg1=46500088) at fmgr.c:1110
#23 0x0000000000b816c1 in OidFunctionCall1Coll (functionId=14272, collation=0, arg1=46500088) at fmgr.c:1388
#24 0x00000000006a6c87 in ExecuteDoStmt (pstate=0x2c587e8, stmt=0x2b45a48, atomic=false) at functioncmds.c:2144
#25 0x00000000009bff91 in standard_ProcessUtility (pstmt=0x2b45ae8, queryString=0x2b44e78 "do $$\ndeclare\n kk varchar(32);\nbegin\n  kk := 'abcd';\n  insert into d1 values (kk);\nend;\n$$;", readOnlyTree=false, context=PROCESS_UTILITY_TOPLEVEL, params=0x0, queryEnv=0x0, dest=0x2b45da8, qc=0x7ffe8928e8a0) at utility.c:714
#26 0x00000000009bfaa8 in ProcessUtility (pstmt=0x2b45ae8, queryString=0x2b44e78 "do $$\ndeclare\n kk varchar(32);\nbegin\n  kk := 'abcd';\n  insert into d1 values (kk);\nend;\n$$;", readOnlyTree=false, context=PROCESS_UTILITY_TOPLEVEL, params=0x0, queryEnv=0x0, dest=0x2b45da8, qc=0x7ffe8928e8a0) at utility.c:530
#27 0x00000000009be6e9 in PortalRunUtility (portal=0x2bf0388, pstmt=0x2b45ae8, isTopLevel=true, setHoldSnapshot=false, dest=0x2b45da8, qc=0x7ffe8928e8a0) at pquery.c:1158
#28 0x00000000009be943 in PortalRunMulti (portal=0x2bf0388, isTopLevel=true, setHoldSnapshot=false, dest=0x2b45da8, altdest=0x2b45da8, qc=0x7ffe8928e8a0) at pquery.c:1315
#29 0x00000000009bde7b in PortalRun (portal=0x2bf0388, count=9223372036854775807, isTopLevel=true, run_once=true, dest=0x2b45da8, altdest=0x2b45da8, qc=0x7ffe8928e8a0) at pquery.c:791
#30 0x00000000009b7962 in exec_simple_query (query_string=0x2b44e78 "do $$\ndeclare\n kk varchar(32);\nbegin\n  kk := 'abcd';\n  insert into d1 values (kk);\nend;\n$$;") at postgres.c:1274
#31 0x00000000009bbfc5 in PostgresMain (dbname=0x2b7c310 "postgres", username=0x2b7c2f8 "mingjie") at postgres.c:4632
#32 0x00000000008f31f6 in BackendRun (port=0x2b70670) at postmaster.c:4461
#33 0x00000000008f2b8f in BackendStartup (port=0x2b70670) at postmaster.c:4189
#34 0x00000000008ef45a in ServerLoop () at postmaster.c:1779
#35 0x00000000008eee2a in PostmasterMain (argc=1, argv=0x2b3ea80) at postmaster.c:1463
#36 0x00000000007b988e in main (argc=1, argv=0x2b3ea80) at main.c:198

注意Param只是一个指针,指向var,并没有存放具体的值:
在这里插入图片描述

2 _SPI_prepare_plan→语义分析:transformColumnRef拿到hook返回的Param

拿到Pl返回的Param

transformColumnRef......if (pstate->p_post_columnref_hook != NULL){Node	   *hookresult;hookresult = pstate->p_post_columnref_hook(pstate, cref, node);if (node == NULL)node = hookresult;else if (hookresult != NULL)ereport(ERROR,(errcode(ERRCODE_AMBIGUOUS_COLUMN),errmsg("column reference \"%s\" is ambiguous",NameListToString(cref->fields)),parser_errposition(pstate, cref->location)));}...

transformInsertStmt流程

transformInsertStmt......exprList = transformExpressionListtransformExprtransformExprRecursetransformColumnRef <- plpgsql_post_column_ref <- resolve_column_ref <- make_datum_param......result = lappend(result, e);exprList = transformInsertRow...transformAssignedExpr...type_id = exprType((Node *) expr);   // 1043coerce_to_target_type                // 类型转换,当前不需要

在这里插入图片描述

transformInsertStmt最后结果:
在这里插入图片描述

3 _SPI_execute_plan→优化器:preprocess_expression根据Param记录的位置走钩子paramFetch→plpgsql_param_fetch拿值

进入优化器:

#0  pg_plan_queries (querytrees=0x2c55798, query_string=0x2c625a0 "insert into d1 values (kk)", cursorOptions=2048, boundParams=0x2c62dc8) at postgres.c:975
#1  0x0000000000b5f6b3 in BuildCachedPlan (plansource=0x2c654d8, qlist=0x2c55798, boundParams=0x2c62dc8, queryEnv=0x0) at plancache.c:937
#2  0x0000000000b5fd69 in GetCachedPlan (plansource=0x2c654d8, boundParams=0x2c62dc8, owner=0x2b7dc00, queryEnv=0x0) at plancache.c:1219
#3  0x00000000007996a4 in _SPI_execute_plan (plan=0x2b6cfb8, options=0x7ffe8928dd00, snapshot=0x0, crosscheck_snapshot=0x0, fire_triggers=true) at spi.c:2555
#4  0x0000000000796997 in SPI_execute_plan_with_paramlist (plan=0x2b6cfb8, params=0x2c62dc8, read_only=false, tcount=0) at spi.c:749
#5  0x00007fbb4f3dea13 in exec_stmt_execsql (estate=0x7ffe8928e060, stmt=0x2c509b0) at pl_exec.c:4292
#6  0x00007fbb4f3da092 in exec_stmts (estate=0x7ffe8928e060, stmts=0x2c50840) at pl_exec.c:2091
#7  0x00007fbb4f3d9c68 in exec_stmt_block (estate=0x7ffe8928e060, block=0x2c50a00) at pl_exec.c:1942
#8  0x00007fbb4f3d946d in exec_toplevel_block (estate=0x7ffe8928e060, block=0x2c50a00) at pl_exec.c:1633
#9  0x00007fbb4f3d7415 in plpgsql_exec_function (func=0x2c53de0, fcinfo=0x7ffe8928e2a0, simple_eval_estate=0x2c4b748, simple_eval_resowner=0x2b40478, procedure_resowner=0x2b40478, atomic=false) at pl_exec.c:622
#10 0x00007fbb4f3f1dae in plpgsql_inline_handler (fcinfo=0x7ffe8928e390) at pl_handler.c:368
#11 0x0000000000b80adb in FunctionCall1Coll (flinfo=0x7ffe8928e3f0, collation=0, arg1=46500088) at fmgr.c:1110
#12 0x0000000000b816c1 in OidFunctionCall1Coll (functionId=14272, collation=0, arg1=46500088) at fmgr.c:1388
#13 0x00000000006a6c87 in ExecuteDoStmt (pstate=0x2c587e8, stmt=0x2b45a48, atomic=false) at functioncmds.c:2144
#14 0x00000000009bff91 in standard_ProcessUtility (pstmt=0x2b45ae8, queryString=0x2b44e78 "do $$\ndeclare\n kk varchar(32);\nbegin\n  kk := 'abcd';\n  insert into d1 values (kk);\nend;\n$$;", readOnlyTree=false, context=PROCESS_UTILITY_TOPLEVEL, params=0x0, queryEnv=0x0, dest=0x2b45da8, qc=0x7ffe8928e8a0) at utility.c:714
#15 0x00000000009bfaa8 in ProcessUtility (pstmt=0x2b45ae8, queryString=0x2b44e78 "do $$\ndeclare\n kk varchar(32);\nbegin\n  kk := 'abcd';\n  insert into d1 values (kk);\nend;\n$$;", readOnlyTree=false, context=PROCESS_UTILITY_TOPLEVEL, params=0x0, queryEnv=0x0, dest=0x2b45da8, qc=0x7ffe8928e8a0) at utility.c:530
#16 0x00000000009be6e9 in PortalRunUtility (portal=0x2bf0388, pstmt=0x2b45ae8, isTopLevel=true, setHoldSnapshot=false, dest=0x2b45da8, qc=0x7ffe8928e8a0) at pquery.c:1158
#17 0x00000000009be943 in PortalRunMulti (portal=0x2bf0388, isTopLevel=true, setHoldSnapshot=false, dest=0x2b45da8, altdest=0x2b45da8, qc=0x7ffe8928e8a0) at pquery.c:1315
#18 0x00000000009bde7b in PortalRun (portal=0x2bf0388, count=9223372036854775807, isTopLevel=true, run_once=true, dest=0x2b45da8, altdest=0x2b45da8, qc=0x7ffe8928e8a0) at pquery.c:791
#19 0x00000000009b7962 in exec_simple_query (query_string=0x2b44e78 "do $$\ndeclare\n kk varchar(32);\nbegin\n  kk := 'abcd';\n  insert into d1 values (kk);\nend;\n$$;") at postgres.c:1274
#20 0x00000000009bbfc5 in PostgresMain (dbname=0x2b7c310 "postgres", username=0x2b7c2f8 "mingjie") at postgres.c:4632
#21 0x00000000008f31f6 in BackendRun (port=0x2b70670) at postmaster.c:4461
#22 0x00000000008f2b8f in BackendStartup (port=0x2b70670) at postmaster.c:4189
#23 0x00000000008ef45a in ServerLoop () at postmaster.c:1779
#24 0x00000000008eee2a in PostmasterMain (argc=1, argv=0x2b3ea80) at postmaster.c:1463
#25 0x00000000007b988e in main (argc=1, argv=0x2b3ea80) at main.c:198

进入时的query树:

优化器preprocess_expression函数执行转换:

pg_plan_queries→pg_plan_query→planner→standard_planner→subquery_planner→preprocess_expression
在这里插入图片描述

preprocess_expressioneval_const_expressionseval_const_expressions_mutator层层递归遍历表达式,因为kk可以写成表达式kk || 'ddd'等等eval_const_expressions_mutatorcase T_Param:钩子拿值prm = paramLI->paramFetch(paramLI, param->paramid,true, &prmdata);进入PL堆栈plpgsql_param_fetch

拿到值后构造const常量,执行时看到的就是Const了。

执行时

plan中的expr已经变成const常量了。代表’abcd’字符串。
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/92127.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

word 应用 打不开 显示一直是正在启动中

word打开来显示一直正在启动中&#xff0c;其他调用word的应用也打不开&#xff0c;网上查了下以后进程关闭spoolsv.exe,就可以正常打开word了

OpenCV-Python中的图像处理-傅里叶变换

OpenCV-Python中的图像处理-傅里叶变换 傅里叶变换Numpy中的傅里叶变换Numpy中的傅里叶逆变换OpenCV中的傅里叶变换OpenCV中的傅里叶逆变换 DFT的性能优化不同滤波算子傅里叶变换对比 傅里叶变换 傅里叶变换经常被用来分析不同滤波器的频率特性。我们可以使用 2D 离散傅里叶变…

Mac RN环境搭建

RN ios android原生环境搭建有时候是真恶心&#xff0c;电脑环境不一样配置也有差异。 我已经安装官网的文档配置了ios环境 执行 npx react-nativelatest init AwesomeProject 报错 然后自己百度查呀执行 gem update --system 说是没有权限&#xff0c;执行失败。因为Mac…

Qt 7. 在自定义类TcpClient类中使用信号槽功能

1. 因为只有QObject类及其子类派生的类才能使用信号和槽机制。 使用信号和槽还必须在类声明的最开始处添加Q_OBJECT宏&#xff0c;在这个程序中&#xff0c;类的声明是自动生成的&#xff0c;已经添加了这个宏。UI类继承自QDialog&#xff0c;QDialog类又继承自QWidget类&…

数据链路层

数据链路层和网络层的对比 如果说网络层实现的是路由的功能&#xff0c;那么数据链路层就是实打实的实现具体的传输。 就像导航&#xff0c;网络层告诉我们下一步该去哪个主机&#xff0c;而数据链路层则是实现去下一个主机的方法。 网络层的IP地址告诉我们目的地在哪里&#x…

如何使用CSS实现一个纯CSS的滚动条样式?

聚沙成塔每天进步一点点 ⭐ 专栏简介⭐ 使用CSS实现自定义滚动条样式⭐ 写在最后 ⭐ 专栏简介 前端入门之旅&#xff1a;探索Web开发的奇妙世界 记得点击上方或者右侧链接订阅本专栏哦 几何带你启航前端之旅 欢迎来到前端入门之旅&#xff01;这个专栏是为那些对Web开发感兴趣…

每天一道leetcode:797. 所有可能的路径(图论中等深度优先遍历)

今日份题目&#xff1a; 给你一个有 n 个节点的 有向无环图&#xff08;DAG&#xff09;&#xff0c;请你找出所有从节点 0 到节点 n-1 的路径并输出&#xff08;不要求按特定顺序&#xff09; graph[i] 是一个从节点 i 可以访问的所有节点的列表&#xff08;即从节点 i 到节…

VBA manual

VBA MACRO Debug.Print()设置Macros安全修复乱码打开VBAAlt F11File/Options/Customize Ribbon Debug.Print() How to Use Excel VBA Debug. Print? 设置Macros安全 或者 File /Options 如果还是Block&#xff0c;右键文件属性 修复乱码 Tools / Options Control Pann…

大数据Flink(六十一):Flink流处理程序流程和项目准备

文章目录 Flink流处理程序流程和项目准备 一、Flink流处理程序的一般流程

java.lang.NoClassDefFoundError: org/apache/tez/dag/api/TezConfiguration

错误&#xff1a; java.lang.NoClassDefFoundError: org/apache/tez/dag/api/TezConfigurationat org.apache.hadoop.hive.ql.exec.tez.TezSessionPoolSession$AbstractTriggerValidator.startTriggerValidator(TezSessionPoolSession.java:74)at org.apache.hadoop.hive.ql.e…

day 0815

计算文件有多少行&#xff1f; 2.文件的拷贝

21.0 CSS 介绍

1. CSS层叠样式表 1.1 CSS简介 CSS(层叠样式表): 是一种用于描述网页上元素外观和布局的样式标记语言. 它可以与HTML结合使用, 通过为HTML元素添加样式来改变其外观. CSS使用选择器来选择需要应用样式的元素, 并使用属性-值对来定义这些样式.1.2 CSS版本 CSS有多个版本, 每个…

髋关节 弹响

评估测试 https://www.bilibili.com/video/BV1A44y1j71Y/?spm_id_from333.880.my_history.page.click&vd_source3535bfaa5db8443d107998d15e88dc44 根据此视频整理所得 托马斯测试 第一种情况 如果你难于将膝关节拉到胸前&#xff0c;并感觉前面有骨性的挤压 说明你股…

leetcode 面试题 02.05 链表求和

⭐️ 题目描述 &#x1f31f; leetcode链接&#xff1a;面试题 02.05 链表求和 ps&#xff1a; 首先定义一个头尾指针 head 、tail&#xff0c;这里的 tail 是方便我们尾插&#xff0c;每次不需要遍历找尾&#xff0c;由于这些数是反向存在的&#xff0c;所以我们直接加起来若…

分布式图数据库 NebulaGraph v3.6.0 正式发布,强化全文索引能力

本次 v3.6.0 版本&#xff0c;主要强化全文索引能力&#xff0c;以及优化部分场景下的 MATCH 性能。 强化 强化增强全文索引功能&#xff0c;具体 pr 参见&#xff1a;#5567、#5575、#5577、#5580、#5584、#5587 优化 支持使用 MATCH 子句检索 VID 或属性索引时使用变量&am…

概述、搭建Redis服务器、部署LNP+Redis、创建Redis集群、连接集群、集群工作原理

Top NSD DBA DAY09 案例1&#xff1a;搭建redis服务器案例2&#xff1a;常用命令限案例3&#xff1a;部署LNPRedis案例4&#xff1a;创建redis集群 1 案例1&#xff1a;搭建redis服务器 1.1 具体要求如下 在主机redis64运行redis服务修改服务运行参数 ip 地址192.168.88.6…

四张图片道清AI大模型的发展史(1943-2023)

四张图片道清AI大模型的发展史(1943-2023) 现在最火的莫过于GPT了&#xff0c;也就是大规模语言模型(LLM)。“LLM” 是 “Large Language Model”&#xff08;大语言模型&#xff09;的简称&#xff0c;通常用来指代具有巨大规模参数和复杂架构的自然语言处理模型&#xff0c;…

从零开始,外贸邮件营销如何做?

邮件营销是外贸企业开发新用户和维系老客户非常有效的方法之一&#xff0c;因其操作方便快捷、成本低廉且精准投放的特性&#xff0c;已成为外贸行业的必备营销手段。但如何才能利用好邮件营销&#xff0c;让邮件营销的作用发挥到最大呢&#xff1f;今天U-Mail李工就跟大家分享…

Python Flask+Echarts+sklearn+MySQL(评论情感分析、用户推荐、BI报表)项目分享

Python FlaskEchartssklearnMySQL(评论情感分析、用户推荐、BI报表)项目分享 项目背景&#xff1a; 随着互联网的快速发展和智能手机的普及&#xff0c;人们越来越倾向于在网上查找餐厅、购物中心、酒店和旅游景点等商户的点评和评分信息&#xff0c;以便做出更好的消费决策。…

vue3+ts使用antv/x6 + 自定义节点

使用 2.x 版本 x6.antv 新官网: 安装 npm install antv/x6 //"antv/x6": "^2.1.6",项目结构 1、初始化画布 index.vue <template><div id"container"></div> </template><script setup langts> import { onM…