2016-01-13 96 views
2

是否可以從其他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)。

回答

2

更詳細的答案在這裏: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';