是否可以从其他 PL/Python block 调用 PL/Python 函数作为普通 Python 函数。
例如,我有一个函数 f1:
create or replace function f1() returns text as $$
return "hello"
$$ language 'plpython3u';
我想从其他函数或 block 调用此函数,例如这个匿名 block :
do $$
begin
...
t = f1()
...
end;
$$ language 'plpython3u';
这可以使用 t = plpy.execute("select f1()")
来完成,但我希望,如果可能的话,将其作为普通的Python函数调用以避免类型转换(例如jsonb等)。
(我正在使用 plpython3u ~ Python 3)。
最佳答案
更详细的答案在这里: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';
您可以在每个数据库 session 开始时调用 _meta,并且您的 python 函数将能够以 GD["f1"]()
的形式访问 f1 函数:
do $$
begin
...
t = GD["f1"]()
...
end;
$$ language 'plpython3u';
关于postgresql - 从另一个 PL/Python block 调用 postgres PL/Python 存储函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34764665/