PostgreSQL PL/Python 和 PL/Postgres 函数互相调用

时间:2022-09-14 22:54:44
create or replace function hello(name text)
    returns text
as $$
#    str = name+',你吃饭了吗?';
#    return 'hello %s !' % str;
    return 'Hello %s!' % name
$$ language plpythonu;


create or replace function hello_invoker(name text)
    returns text
as $$
begin
    return public.hello('[plpgsql_invoker_prefix]' || name);
end;
$$ language plpgsql;

create or replace function hello_invoker_wrap(name text)
    returns text
as $$
    pyret = plpy.execute("select hello_invoker('" + name + "[python wrap suffix]') as ret")[0];
    return pyret['ret'];
$$ language plpythonu;

 

testdb=# select * from public.hello_invoker_wrap('吴xx');
                   hello_invoker_wrap                    
---------------------------------------------------------
 Hello [plpgsql_invoker_prefix]吴xx[python wrap suffix]!

 

plpy.execute返回的结果集为:

 <PLyResult status=5 nrows=1 rows=[{'ret': 'Hello [plpgsql_invoker_prefix]xxx[python wrap suffix]!'}]>
(1 行记录)