Обсуждение: Call postgres PL/Python stored function from another PL/Python block.

Поиск
Список
Период
Сортировка

Call postgres PL/Python stored function from another PL/Python block.

От
lodopidolo
Дата:
Hello. It is possible to call al PL/Python stored function natively from another PL/Python function?

Something like:

create or replace function f1() returns text as $$   return "hello"
$$ language 'plpython3u';
do $$
begin   ...   t = f1()   ...
end;
$$ language 'plpython3u';

Regards.

Re: Call postgres PL/Python stored function from another PL/Python block.

От
Pavel Stehule
Дата:
Hi

2016-01-13 16:47 GMT+01:00 lodopidolo <dir.postgresql@orencio.org>:
Hello. It is possible to call al PL/Python stored function natively from another PL/Python function?

The function created by CREATE OR REPLACE FUNCTION statement can be called only via special interface, or via SQL executor, so you cannot to call this function directly from Python

Pavel

 

Something like:

create or replace function f1() returns text as $$   return "hello"
$$ language 'plpython3u';
do $$
begin   ...   t = f1()   ...
end;
$$ language 'plpython3u';

Regards.


Re: Call postgres PL/Python stored function from another PL/Python block.

От
Jim Nasby
Дата:
On 1/13/16 9:47 AM, lodopidolo wrote:
> Hello. It is possible to call al PL/Python stored function natively from
> another PL/Python function?

Stackoverflow is stupid and won't let me post there, but here's what you
want:

There is no special capability to call other plpython functions. You
need to call them as you would any other Postgres function, ie:

     do $$
     begin
     ...
     rv = plpy.execute("SELECT f1()")
     t = rv[1]["f1]
     ...
     end;
     $$ language 'plpython3u';

See
http://www.postgresql.org/docs/9.5/static/plpython-database.html#AEN65599 for
more information.

--
Jim Nasby, Data Architect, Blue Treble Consulting, Austin TX
Experts in Analytics, Data Architecture and PostgreSQL
Data in Trouble? Get it in Treble! http://BlueTreble.com


Re: Call postgres PL/Python stored function from another PL/Python block.

От
Adrian Klaver
Дата:
On 01/13/2016 07:47 AM, lodopidolo wrote:
> Hello. It is possible to call al PL/Python stored function natively from
> another PL/Python function?
>
> Something like:
>
> |createorreplace functionf1()returns text as$$return"hello"$$language
> 'plpython3u';|
>
> |do $$begin...t =f1()...end;$$language 'plpython3u';|
>
>
> Regards.
>
> (this question has been made in
> http://stackoverflow.com/questions/34764665/call-postgres-pl-python-stored-function-from-another-pl-python-block
> too).

In addition to the other answers there is the option of just creating
the function in a Python module outside Postgres and doing:

from some_module import f

Which would seem to address this(from the SO post):

"This can be done using t = plpy.execute("select f1()"), but I want, if
it is possible, call it as a normal Python function to avoid type
conversions (for example jsonb, etc)."

The downside being you have a dependency outside the database.

--
Adrian Klaver
adrian.klaver@aklaver.com


Re: Call postgres PL/Python stored function from another PL/Python block.

От
lodopidolo
Дата:
Ok, thank you very much.

2016-01-13 19:04 GMT+01:00 Adrian Klaver <adrian.klaver@aklaver.com>:
On 01/13/2016 07:47 AM, lodopidolo wrote:
Hello. It is possible to call al PL/Python stored function natively from
another PL/Python function?

Something like:

|createorreplace functionf1()returns text as$$return"hello"$$language
'plpython3u';|

|do $$begin...t =f1()...end;$$language 'plpython3u';|


Regards.

(this question has been made in
http://stackoverflow.com/questions/34764665/call-postgres-pl-python-stored-function-from-another-pl-python-block
too).

In addition to the other answers there is the option of just creating the function in a Python module outside Postgres and doing:

from some_module import f

Which would seem to address this(from the SO post):

"This can be done using t = plpy.execute("select f1()"), but I want, if it is possible, call it as a normal Python function to avoid type conversions (for example jsonb, etc)."

The downside being you have a dependency outside the database.

--
Adrian Klaver
adrian.klaver@aklaver.com