PL / pgSQL:如何获取变量/参数的名称? [PostgreSQL]

问题描述

@H_502_1@CREATE OR REPLACE FUNCTION public.test_function(test_parameter varchar default 'test_value_1')
    RETURNS void AS
$func$
DECLARE
    test_variable varchar = 'test_value_2';
BEGIN
    raise notice '% = %',(???),test_parameter; -- expected result: test_parameter = test_value_1
    raise notice '% = %',test_variable;  -- expected result: test_variable = test_value_2
    return;
END
$func$ LANGUAGE plpgsql;

SELECT * FROM public.test_function();

如何在Postgresql(PL / pgsql)中获得变量的/参数名称

解决方法

将变量名作为模板的一部分或将其作为字符串传递:

CREATE OR REPLACE FUNCTION public.test_function(test_parameter varchar default 'test_value_1')
    RETURNS void AS
$func$
DECLARE
    test_variable varchar = 'test_value_2';
BEGIN
    raise notice '% = %','test_parameter',test_parameter; -- expected result: test_parameter = test_value_1
    raise notice 'test_variable = %',test_variable;  -- expected result: test_variable = test_value_2
    return;
END
$func$ LANGUAGE plpgsql;

SELECT * FROM public.test_function();
,

某些将函数参数作为字典对象传递的语言(例如Python)。 Postgres环境中的参数以值数组的形式传递(并且不能从PLpgSQL访问此数组)。可以从系统表pg_proc中获取名称,但是在plpgsql中读取这些数据很慢并且很简单。在PLpgSQL中使用参数或变量的原理与静态编译语言C,C ++,Pascal,Ada等相同。...您无法在运行时获取函数内部的变量或参数列表。此信息在已编译的代码中不存在,或者无法从语言(PLpgSQL)访问。