网站开发的实训报告,网站系统维护一般多久,怎么寻找做有益做网站的客户,免费企业名录数据相关 《Postgresql源码#xff08;41#xff09;plpgsql函数编译执行流程分析》 《Postgresql源码#xff08;46#xff09;plpgsql中的变量类型及对应关系》 《Postgresql源码#xff08;49#xff09;plpgsql函数编译执行流程分析总结》 《Postgresql源码#xff08;5… 相关 《Postgresql源码41plpgsql函数编译执行流程分析》 《Postgresql源码46plpgsql中的变量类型及对应关系》 《Postgresql源码49plpgsql函数编译执行流程分析总结》 《Postgresql源码53plpgsql语法解析关键流程、函数分析》 《Postgresql源码112plpgsql执行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变量的值是在哪里换成具体的字符串的。 下文总结 在语义分析阶段走钩子函数plpgsql_post_column_ref确认变量存在并在Query树上挂Param节点记录变量在PL变量数组中的位置和类型。在优化器中走钩子函数plpgsql_param_fetch拿变量具体的值然后用Const常量节点替换Param变量节点。 1 _SPI_prepare_plan→语义分析transform阶段回调plpgsql_post_column_ref得到指向kk的Param
#0 make_datum_param (expr0x2c508f0, dno1, location23) at pl_comp.c:1362
#1 0x00007fbb4f3d4499 in resolve_column_ref (pstate0x2c56130, expr0x2c508f0, cref0x2c55e10, error_if_no_fieldtrue) at pl_comp.c:1279
#2 0x00007fbb4f3d4048 in plpgsql_post_column_ref (pstate0x2c56130, cref0x2c55e10, var0x0) at pl_comp.c:1125
#3 0x000000000063244f in transformColumnRef (pstate0x2c56130, cref0x2c55e10) at parse_expr.c:804
#4 0x0000000000631121 in transformExprRecurse (pstate0x2c56130, expr0x2c55e10) at parse_expr.c:137
#5 0x00000000006310b3 in transformExpr (pstate0x2c56130, expr0x2c55e10, exprKindEXPR_KIND_VALUES_SINGLE) at parse_expr.c:116
#6 0x000000000064a231 in transformExpressionList (pstate0x2c56130, exprlist0x2c55eb0, exprKindEXPR_KIND_VALUES_SINGLE, allowDefaulttrue) at parse_target.c:272
#7 0x00000000005e88db in transformInsertStmt (pstate0x2c56130, stmt0x2c56060) at analyze.c:889
#8 0x00000000005e79be in transformStmt (pstate0x2c56130, parseTree0x2c56060) at analyze.c:344
#9 0x00000000005e792f in transformOptionalSelectInto (pstate0x2c56130, parseTree0x2c56060) at analyze.c:306
#10 0x00000000005e77f3 in transformTopLevelStmt (pstate0x2c56130, parseTree0x2c560b0) at analyze.c:256
#11 0x00000000005e76de in parse_analyze_withcb (parseTree0x2c560b0, sourceText0x2c50980 insert into d1 values (kk), parserSetup0x7fbb4f3d3f1d plpgsql_parser_setup, parserSetupArg0x2c508f0, queryEnv0x0) at analyze.c:203
#12 0x00000000009b71b6 in pg_analyze_and_rewrite_withcb (parsetree0x2c560b0, query_string0x2c50980 insert into d1 values (kk), parserSetup0x7fbb4f3d3f1d plpgsql_parser_setup, parserSetupArg0x2c508f0, queryEnv0x0) at postgres.c:781
#13 0x000000000079906a in _SPI_prepare_plan (src0x2c50980 insert into d1 values (kk), plan0x7ffe8928dc90) at spi.c:2265
#14 0x0000000000796df8 in SPI_prepare_extended (src0x2c50980 insert into d1 values (kk), options0x7ffe8928dd10) at spi.c:925
#15 0x00007fbb4f3de778 in exec_prepare_plan (estate0x7ffe8928e060, expr0x2c508f0, cursorOptions2048) at pl_exec.c:4193
#16 0x00007fbb4f3de898 in exec_stmt_execsql (estate0x7ffe8928e060, stmt0x2c509b0) at pl_exec.c:4233
#17 0x00007fbb4f3da092 in exec_stmts (estate0x7ffe8928e060, stmts0x2c50840) at pl_exec.c:2091
#18 0x00007fbb4f3d9c68 in exec_stmt_block (estate0x7ffe8928e060, block0x2c50a00) at pl_exec.c:1942
#19 0x00007fbb4f3d946d in exec_toplevel_block (estate0x7ffe8928e060, block0x2c50a00) at pl_exec.c:1633
#20 0x00007fbb4f3d7415 in plpgsql_exec_function (func0x2c53de0, fcinfo0x7ffe8928e2a0, simple_eval_estate0x2c4b748, simple_eval_resowner0x2b40478, procedure_resowner0x2b40478, atomicfalse) at pl_exec.c:622
#21 0x00007fbb4f3f1dae in plpgsql_inline_handler (fcinfo0x7ffe8928e390) at pl_handler.c:368
#22 0x0000000000b80adb in FunctionCall1Coll (flinfo0x7ffe8928e3f0, collation0, arg146500088) at fmgr.c:1110
#23 0x0000000000b816c1 in OidFunctionCall1Coll (functionId14272, collation0, arg146500088) at fmgr.c:1388
#24 0x00000000006a6c87 in ExecuteDoStmt (pstate0x2c587e8, stmt0x2b45a48, atomicfalse) at functioncmds.c:2144
#25 0x00000000009bff91 in standard_ProcessUtility (pstmt0x2b45ae8, queryString0x2b44e78 do $$\ndeclare\n kk varchar(32);\nbegin\n kk : abcd;\n insert into d1 values (kk);\nend;\n$$;, readOnlyTreefalse, contextPROCESS_UTILITY_TOPLEVEL, params0x0, queryEnv0x0, dest0x2b45da8, qc0x7ffe8928e8a0) at utility.c:714
#26 0x00000000009bfaa8 in ProcessUtility (pstmt0x2b45ae8, queryString0x2b44e78 do $$\ndeclare\n kk varchar(32);\nbegin\n kk : abcd;\n insert into d1 values (kk);\nend;\n$$;, readOnlyTreefalse, contextPROCESS_UTILITY_TOPLEVEL, params0x0, queryEnv0x0, dest0x2b45da8, qc0x7ffe8928e8a0) at utility.c:530
#27 0x00000000009be6e9 in PortalRunUtility (portal0x2bf0388, pstmt0x2b45ae8, isTopLeveltrue, setHoldSnapshotfalse, dest0x2b45da8, qc0x7ffe8928e8a0) at pquery.c:1158
#28 0x00000000009be943 in PortalRunMulti (portal0x2bf0388, isTopLeveltrue, setHoldSnapshotfalse, dest0x2b45da8, altdest0x2b45da8, qc0x7ffe8928e8a0) at pquery.c:1315
#29 0x00000000009bde7b in PortalRun (portal0x2bf0388, count9223372036854775807, isTopLeveltrue, run_oncetrue, dest0x2b45da8, altdest0x2b45da8, qc0x7ffe8928e8a0) at pquery.c:791
#30 0x00000000009b7962 in exec_simple_query (query_string0x2b44e78 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 (dbname0x2b7c310 postgres, username0x2b7c2f8 mingjie) at postgres.c:4632
#32 0x00000000008f31f6 in BackendRun (port0x2b70670) at postmaster.c:4461
#33 0x00000000008f2b8f in BackendStartup (port0x2b70670) at postmaster.c:4189
#34 0x00000000008ef45a in ServerLoop () at postmaster.c:1779
#35 0x00000000008eee2a in PostmasterMain (argc1, argv0x2b3ea80) at postmaster.c:1463
#36 0x00000000007b988e in main (argc1, argv0x2b3ea80) 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 (querytrees0x2c55798, query_string0x2c625a0 insert into d1 values (kk), cursorOptions2048, boundParams0x2c62dc8) at postgres.c:975
#1 0x0000000000b5f6b3 in BuildCachedPlan (plansource0x2c654d8, qlist0x2c55798, boundParams0x2c62dc8, queryEnv0x0) at plancache.c:937
#2 0x0000000000b5fd69 in GetCachedPlan (plansource0x2c654d8, boundParams0x2c62dc8, owner0x2b7dc00, queryEnv0x0) at plancache.c:1219
#3 0x00000000007996a4 in _SPI_execute_plan (plan0x2b6cfb8, options0x7ffe8928dd00, snapshot0x0, crosscheck_snapshot0x0, fire_triggerstrue) at spi.c:2555
#4 0x0000000000796997 in SPI_execute_plan_with_paramlist (plan0x2b6cfb8, params0x2c62dc8, read_onlyfalse, tcount0) at spi.c:749
#5 0x00007fbb4f3dea13 in exec_stmt_execsql (estate0x7ffe8928e060, stmt0x2c509b0) at pl_exec.c:4292
#6 0x00007fbb4f3da092 in exec_stmts (estate0x7ffe8928e060, stmts0x2c50840) at pl_exec.c:2091
#7 0x00007fbb4f3d9c68 in exec_stmt_block (estate0x7ffe8928e060, block0x2c50a00) at pl_exec.c:1942
#8 0x00007fbb4f3d946d in exec_toplevel_block (estate0x7ffe8928e060, block0x2c50a00) at pl_exec.c:1633
#9 0x00007fbb4f3d7415 in plpgsql_exec_function (func0x2c53de0, fcinfo0x7ffe8928e2a0, simple_eval_estate0x2c4b748, simple_eval_resowner0x2b40478, procedure_resowner0x2b40478, atomicfalse) at pl_exec.c:622
#10 0x00007fbb4f3f1dae in plpgsql_inline_handler (fcinfo0x7ffe8928e390) at pl_handler.c:368
#11 0x0000000000b80adb in FunctionCall1Coll (flinfo0x7ffe8928e3f0, collation0, arg146500088) at fmgr.c:1110
#12 0x0000000000b816c1 in OidFunctionCall1Coll (functionId14272, collation0, arg146500088) at fmgr.c:1388
#13 0x00000000006a6c87 in ExecuteDoStmt (pstate0x2c587e8, stmt0x2b45a48, atomicfalse) at functioncmds.c:2144
#14 0x00000000009bff91 in standard_ProcessUtility (pstmt0x2b45ae8, queryString0x2b44e78 do $$\ndeclare\n kk varchar(32);\nbegin\n kk : abcd;\n insert into d1 values (kk);\nend;\n$$;, readOnlyTreefalse, contextPROCESS_UTILITY_TOPLEVEL, params0x0, queryEnv0x0, dest0x2b45da8, qc0x7ffe8928e8a0) at utility.c:714
#15 0x00000000009bfaa8 in ProcessUtility (pstmt0x2b45ae8, queryString0x2b44e78 do $$\ndeclare\n kk varchar(32);\nbegin\n kk : abcd;\n insert into d1 values (kk);\nend;\n$$;, readOnlyTreefalse, contextPROCESS_UTILITY_TOPLEVEL, params0x0, queryEnv0x0, dest0x2b45da8, qc0x7ffe8928e8a0) at utility.c:530
#16 0x00000000009be6e9 in PortalRunUtility (portal0x2bf0388, pstmt0x2b45ae8, isTopLeveltrue, setHoldSnapshotfalse, dest0x2b45da8, qc0x7ffe8928e8a0) at pquery.c:1158
#17 0x00000000009be943 in PortalRunMulti (portal0x2bf0388, isTopLeveltrue, setHoldSnapshotfalse, dest0x2b45da8, altdest0x2b45da8, qc0x7ffe8928e8a0) at pquery.c:1315
#18 0x00000000009bde7b in PortalRun (portal0x2bf0388, count9223372036854775807, isTopLeveltrue, run_oncetrue, dest0x2b45da8, altdest0x2b45da8, qc0x7ffe8928e8a0) at pquery.c:791
#19 0x00000000009b7962 in exec_simple_query (query_string0x2b44e78 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 (dbname0x2b7c310 postgres, username0x2b7c2f8 mingjie) at postgres.c:4632
#21 0x00000000008f31f6 in BackendRun (port0x2b70670) at postmaster.c:4461
#22 0x00000000008f2b8f in BackendStartup (port0x2b70670) at postmaster.c:4189
#23 0x00000000008ef45a in ServerLoop () at postmaster.c:1779
#24 0x00000000008eee2a in PostmasterMain (argc1, argv0x2b3ea80) at postmaster.c:1463
#25 0x00000000007b988e in main (argc1, argv0x2b3ea80) 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’字符串。