zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

Postgresql源码(78)plpgsql中调用call proc()时的参数传递和赋值(pl参数)

postgresql源码 参数 调用 赋值 call PL 参数传递
2023-06-13 09:11:07 时间

《Postgresql源码(77)plpgsql中参数传递和赋值(pl参数)》 《Postgresql源码(78)plpgsql中调用call proc()时的参数传递和赋值(pl参数)》

总结

  • 调用者在exec_stmt_call中拼接ParamListInfo传给SPI去执行call xxxx命令。
  • ParamListInfo记录了PL的一些回调函数,在SPI会走到:ExecuteCallStmt
  • ExecuteCallStmt核心流程两步:
    1. 拼参数列表:会拿到所有入参
    • 假设第一个入参是Param类型,会回调PL的plpgsql_param_fetch函数,从PL的Datums中拿变量的值赋值给fcinfo->args[0]
    • 假设第二个入参是Const类型常量,则会直接在执行器内赋值给fcinfo->args[1]
    1. 走FunctionCallInvoke进入plpgsql_exec_functions开始执行被调用函数。

实验用例

create or replace procedure p1(p_a in int, p_b in int, p_c out int)
language plpgsql
as $$
begin
  p_c := 30000;
  raise notice '% % %', p_a, p_b, p_c;
end;
$$;


do $$                          
declare 
  a1 int;
  a3 int;
begin 
  a1 := 10;
  call p1(a1, 20, a3);
  raise notice 'a3: %', a3;
end;
$$;

进入exec_stmt_call时

  • a1:有值,value = 10,isnull = false,freeval = false
  • a3:无值,value = 0, isnull = true, freeval = false
(gdb) p *(PLpgSQL_var*)estate->datums[0]
$9 = {dtype = PLPGSQL_DTYPE_VAR, dno = 0, refname = 0x17107c0 "found", lineno = 0, 
	isconst = false, notnull = false, default_val = 0x0, datatype = 0x17106b0,
  cursor_explicit_expr = 0x0, cursor_explicit_argrow = 0, cursor_options = 0,
  value = 0, isnull = false, freeval = false, promise = PLPGSQL_PROMISE_NONE}

(gdb) p *(PLpgSQL_var*)estate->datums[1]
$10 = {dtype = PLPGSQL_DTYPE_VAR, dno = 1, refname = 0x1711518 "a1", lineno = 3, 
	isconst = false, notnull = false, default_val = 0x0, datatype = 0x1711408,
  cursor_explicit_expr = 0x0, cursor_explicit_argrow = 0, cursor_options = 0, 
  value = 10, isnull = false, freeval = false, promise = PLPGSQL_PROMISE_NONE}

(gdb) p *(PLpgSQL_var*)estate->datums[2]
$11 = {dtype = PLPGSQL_DTYPE_VAR, dno = 2, refname = 0x1711e20 "a3", lineno = 4, 
	isconst = false, notnull = false, default_val = 0x0, datatype = 0x1711d10,
  cursor_explicit_expr = 0x0, cursor_explicit_argrow = 0, cursor_options = 0, 
  value = 0, isnull = true, freeval = false, promise = PLPGSQL_PROMISE_NONE}
  
(gdb) p *expr
$13 = {query = 0x1712178 "call p1(a1, 20, a3)", parseMode = RAW_PARSE_DEFAULT, 
	plan = 0x0, paramnos = 0x0, func = 0x0, ns = 0x1711e40, expr_simple_expr = 0x0,
  expr_simple_type = 0, expr_simple_typmod = 0, expr_simple_mutable = false, 
  target_param = -1, expr_rw_param = 0x0, expr_simple_plansource = 0x0,
  expr_simple_plan = 0x0, expr_simple_plan_lxid = 0, expr_simple_state = 0x0, 
  expr_simple_in_use = false, expr_simple_lxid = 0}