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.pswp.cn/news/43541.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/43541.shtml
英文地址,請注明出處:http://en.pswp.cn/news/43541.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

PyTorch從零開始實現ResNet

文章目錄 代碼實現參考 代碼實現 本文實現 ResNet原論文 Deep Residual Learning for Image Recognition 中的50層&#xff0c;101層和152層殘差連接。 代碼中使用基礎殘差塊這個概念&#xff0c;這里的基礎殘差塊指的是上圖中紅色矩形圈出的內容&#xff1a;從上到下分別使用…

感覺和身邊其他人有差距怎么辦?

雖然清楚知識需要靠時間沉淀&#xff0c;但在看到自己做不出來的題別人會做&#xff0c;自己寫不出的代碼別人會寫時還是會感到焦慮怎么辦&#xff1f; 你是否也因為自身跟周圍人的差距而產生過迷茫&#xff0c;這份迷茫如今是被你克服了還是仍舊讓你感到困擾&#xff1f; 下…

LabVIEW開發最小化5G系統測試平臺

LabVIEW開發最小化5G系統測試平臺 由于具有大量存儲能力和數據的應用程序的智能手機的激增&#xff0c;當前一代產品被迫提高其吞吐效率。正交頻分復用由于其卓越的品質&#xff0c;如單抽頭均衡和具有成本效益的實施&#xff0c;現在被廣泛用作物理層技術。這些好處是以嚴格的…

ElasticSearch索引庫、文檔、RestClient操作

文章目錄 一、索引庫1、mapping屬性2、索引庫的crud 二、文檔的crud三、RestClient 一、索引庫 es中的索引是指相同類型的文檔集合&#xff0c;即mysql中表的概念 映射&#xff1a;索引中文檔字段的約束&#xff0c;比如名稱、類型 1、mapping屬性 mapping映射是對索引庫中文…

Elasticsearch在部署時,對Linux的設置有哪些優化方法?

部署Elasticsearch時&#xff0c;可以通過優化Linux系統的設置來提升性能和穩定性。以下是一些常見的優化方法&#xff1a; 1.文件描述符限制 Elasticsearch需要大量的文件描述符來處理數據和連接&#xff0c;所以確保調整系統的文件描述符限制。可以通過修改 /etc/security/…

Docker-compose搭建Git私服

1. 新建個專用的目錄&#xff0c;然后在里面新建個docker-compose.yml文件&#xff1a; &#xff08;gitlab-ce是社區版&#xff0c;當然還有ee&#xff0c;是商業版&#xff09; version: 3.6 …

es自定義分詞器支持數字字母分詞,中文分詞器jieba支持添加禁用詞和擴展詞典

自定義分析器&#xff0c;分詞器 PUT http://xxx.xxx.xxx.xxx:9200/test_index/ {"settings": {"analysis": {"analyzer": {"char_test_analyzer": {"tokenizer": "char_test_tokenizer","filter": [&…

公網遠程連接Redis數據庫詳解

文章目錄 1. Linux(centos8)安裝redis數據庫2. 配置redis數據庫3. 內網穿透3.1 安裝cpolar內網穿透3.2 創建隧道映射本地端口 4. 配置固定TCP端口地址4.1 保留一個固定tcp地址4.2 配置固定TCP地址4.3 使用固定的tcp地址連接 前言 潔潔的個人主頁 我就問你有沒有發揮&#xff0…

ssh免密登陸報錯ERROR: @ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!

問題描述&#xff1a; 在日常的運維中需要做ssh的免密登陸有提示如下的報錯內容&#xff1a; [rootpaas-harbor01 cce-v5.2.3]# ssh-copy-id 192.45.66.14 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" /usr/bin/ssh-c…

通訊錄實現【C語言】

目錄 前言 一、整體邏輯分析 二、實現步驟 1、創建菜單和多次操作問題 2、創建通訊錄 3、初始化通訊錄 4、添加聯系人 5、顯示聯系人 6、刪除指定聯系人 ?7、查找指定聯系人 8、修改聯系人信息 9、排序聯系人信息 三、全部源碼 前言 我們上期已經詳細的介紹了自定…

Java SpringBoot Vue ERP系統

系統介紹 該ERP系統基于SpringBoot框架和SaaS模式&#xff0c;支持多租戶&#xff0c;專注進銷存財務生產功能。主要模塊有零售管理、采購管理、銷售管理、倉庫管理、財務管理、報表查詢、系統管理等。支持預付款、收入支出、倉庫調撥、組裝拆卸、訂單等特色功能。擁有商品庫存…

ubuntu設置共享文件夾成功后卻不顯示找不到(已解決)

1.首先輸下面命令查看是否真的設置成功共享文件夾 vmware-hgfsclient如果確實已經設置過共享文件夾將輸出window下共享文件夾名字 2.確認自己已設置共享文件夾后輸入下面的命令 //如果之前沒有命令包則先執行sudo apt-get install open-vm-tools sudo vmhgfs-fuse .host:/ /mn…

十六、Spring Cloud Sleuth 分布式請求鏈路追蹤

目錄 一、概述1、為什么出出現這個技術&#xff1f;需要解決哪些問題2、是什么&#xff1f;3、解決 二、搭建鏈路監控步驟1、下載運行zipkin2、服務提供者3、服務調用者4、測試 一、概述 1、為什么出出現這個技術&#xff1f;需要解決哪些問題 2、是什么&#xff1f; 官網&am…

spss---如何使用信度分析以及案例分析

信度分析 問卷調查法是教育研究中廣泛采用的一種調查方法&#xff0c;根據調查目的設計的調查問卷是問卷調查法獲取信息的工具&#xff0c;其質量高低對調查結果的真實性、適用性等具有決定性的作用。 為了保證問卷具有較高的可靠性和有效性&#xff0c;在形成正式問卷之 前&…

CLion:最好用的c/c++編寫工具(最詳細安裝教程)

目錄 一.前言介紹 1.下載安裝 1.1右上角點擊下載 1.2選擇自己操作系統&#xff0c;然后點擊下載 1.3選擇next 1.4 更改路徑 1.5D盤最好 1.6 按照我的選擇配置環境 1.7install安裝 1.8 安裝完成 2、mingw64安裝 2.1下載資源壓縮包 2.2mingw64放入到合適的位置&#xff0c;…

Redis五大基本數據類型及其使用場景

文章目錄 **一 什么是NoSQL&#xff1f;****二 redis是什么&#xff1f;****三 redis五大基本類型**1 String&#xff08;字符串&#xff09;**應用場景** 2 List&#xff08;列表&#xff09;**應用場景** 3 Set&#xff08;集合&#xff09;4 sorted set&#xff08;有序集合…

高級藝術二維碼制作教程

最近不少關于二維碼制作的&#xff0c;而且都是付費。大概就是一個好看的二維碼&#xff0c;掃描后跳轉網址。本篇文章使用Python來實現&#xff0c;這么簡單花啥錢呢&#xff1f;學會&#xff0c;拿去賣便宜點吧。 文章目錄 高級二維碼制作環境安裝普通二維碼藝術二維碼動態 …

【LVS】2、部署LVS-DR群集

LVS-DR數據包的流向分析 1.客戶端發送請求到負載均衡器&#xff0c;請求的數據報文到達內核空間&#xff1b; 2.負載均衡服務器和正式服務器在同一個網絡中&#xff0c;數據通過二層數據鏈路層來傳輸&#xff1b; 3.內核空間判斷數據包的目標IP是本機VIP&#xff0c;此時IP虛…

批量將Excel中的第二列內容從拼音轉換為漢字

要批量將Excel中的第二列內容從拼音轉換為漢字&#xff0c;您可以使用Python的openpyxl庫來實現。下面是一個示例代碼&#xff0c;演示如何讀取Excel文件并將第二列內容進行拼音轉漢字&#xff1a; from openpyxl import load_workbook from xpinyin import Pinyin # 打開Exce…

Android kotlin系列講解(入門篇)使用Intent在Activity之間穿梭

<<返回總目錄 上一篇:Android kotlin系列講解(入門篇)Activity的理解與基本用法 文章目錄 1、使用顯式Intent2、使用隱式Intent3、更多隱式Intent的用法4、向下一個Activity傳遞數據5、返回數據給上一個Activity1、使用顯式Intent 你應該已經對創建Activity的流程比較…