postgresql - 从另一个 PL/Python block 调用 postgres PL/Python 存储函数

标签 postgresql plpython postgresql-9.5 plpy

是否可以从其他 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/

相关文章:

python - PostgreSQL PL/Python : call stored procedure in virtualenv

postgresql - 如何在 IF-THEN-ENDIF 条件下使用存储过程返回 bool 值?

linux - 安装露天社区版时 initdb 出现权限被拒绝错误

sql - 如何编写 postgresql 查询以仅从表中获取时间戳字段的日期部分

POSTGRESQL 无法比较空列

python - 在 plpython3u 过程中导入 Python 包时出现 "Module not found"

postgresql - 在函数中返回多个值

macos - 使用 python 2.7 在 mac 上安装 plpython

sql - 在大表上使用 OFFSET 优化查询

mysql - PostgreSQL SERIAL 与 MySQL AUTO INCREMENT?