无法在 postgres 函数中获取输入参数的值

Unable to get value for input parameter inside postgres function

在下面的 postgres 函数中,我将 'sample'(整数)作为输入参数传递,但是当我尝试在函数体内使用 raise notice 打印它的值时,出现以下错误

ERROR: column "sample" does not exist LINE 1: SELECT sample

CREATE OR REPLACE FUNCTION public.test_function(
        sample integer)
        RETURNS json
        LANGUAGE 'sql'
        COST 100
        VOLATILE PARALLEL UNSAFE
    AS $BODY$
    DO  $$
    BEGIN
      raise notice 'test: %',sample;
    END
    $$;
    select json_agg(1);
    $BODY$;

select "test_function"(10);

匿名 pl/pgsql DO 块没有 'see' sample 参数。这是 pl/pgsql 中的重写 'see' 它。

CREATE OR REPLACE function test_function(sample integer)
RETURNS json LANGUAGE plpgsql COST 100 VOLATILE PARALLEL UNSAFE AS
$BODY$
  BEGIN
    raise notice 'test: %',sample;
    return json_agg(1 + sample);
  END
$BODY$;

pl/pgsql DO 块或多或少被封装,也不能 return 任何东西。