从另一个PL/Python块调用postgres PL/Python存储函数
问题描述:
是否可以从其他PL/Python块的PL/Python函数调用Python函数作为普通Python函数。从另一个PL/Python块调用postgres PL/Python存储函数
例如,我有一个函数F1:
create or replace function f1() returns text as $$
return "hello"
$$ language 'plpython3u';
我想调用其他函数或块这个功能,例如这个匿名块:使用t = plpy.execute("select f1()")
do $$
begin
...
t = f1()
...
end;
$$ language 'plpython3u';
这是可以做到,但我想,如果可能的话,将其称为正常的Python函数以避免类型转换(例如jsonb等)。
(我正在使用plpython3u〜Python 3)。
答
更详细的答案在这里:Reusing pure Python functions between PL/Python functions
我的方法,这是通过使用GD和SD字典是PG为您提供,more here的方式。
我通常有一个函数可以准备我的环境,而且我可以使用纯粹的python函数而不需要开销。在你的情况下,这看起来像:
create or replace function _meta() returns bool as $$
def f1():
return "hello"
GD["f1"] = f1
return True
$$ language 'plpython3u';
你会比在每个数据库会话开始打电话_meta,和你的Python功能将能够进入F1功能GD["f1"]()
:
do $$
begin
...
t = GD["f1"]()
...
end;
$$ language 'plpython3u';