是否可以从其他PL / Python块调用PL / Python函数作为普通的Python函数。
例如,我有一个函数f1:
create or replace function f1() returns text as $$
return "hello"
$$ language 'plpython3u';
我想从其他函数或块调用此函数,例如此匿名块:
do $$
begin
...
t = f1()
...
end;
$$ language 'plpython3u';
这可以使用t = plpy.execute("select f1()")
完成,但是如果可能的话,我希望将其称为普通的Python函数以避免类型转换(例如jsonb等)。
(我正在使用plpython3u~Python 3)。
答案 0 :(得分:2)
更详细的答案:Reusing pure Python functions between PL/Python functions
我接近这个的方法是使用PG为你提供的GD和SD词典more here。
我通常有一个函数来准备我的环境,而且我可以使用纯python函数而不需要开销。在你的情况下,这将是:
create or replace function _meta() returns bool as $$
def f1():
return "hello"
GD["f1"] = f1
return True
$$ language 'plpython3u';
您可以在每个数据库会话开始时调用_meta,并且您的python函数将能够以GD["f1"]()
访问f1函数:
do $$
begin
...
t = GD["f1"]()
...
end;
$$ language 'plpython3u';