Thread: How to 'register' functions, so they can be called (plpythonu)

How to 'register' functions, so they can be called (plpythonu)

From
"blay bloo"
Date:
I am defining some functions using plpythonu, through the standard means.

Here I have one function (test1) which calls another (testfunc). When
I excute this I get the following error:

ERROR:  plpython: function "test1" failed
DETAIL:  <type 'exceptions.NameError'>: global name 'testfunc' is not defined

However, from the console, select testfunc('test') works fine.

I was wondering how do I 'register'/'reference' these functions so
that one function can call another? I am guessing this is important,
i.e. how will I access the complete functionality of the general
python libs?

Cheers, Blay.

PS functions are defined in the usual way - e.g.

create or replace function test1(text) returns integer as $$
return 1
$$ language plpythonu;

Re: How to 'register' functions, so they can be called (plpythonu)

From
Alvaro Herrera
Date:
blay bloo wrote:
> I am defining some functions using plpythonu, through the standard means.
>
> Here I have one function (test1) which calls another (testfunc). When
> I excute this I get the following error:
>
> ERROR:  plpython: function "test1" failed
> DETAIL:  <type 'exceptions.NameError'>: global name 'testfunc' is not defined
>
> However, from the console, select testfunc('test') works fine.
>
> I was wondering how do I 'register'/'reference' these functions so
> that one function can call another?

You can't, because the name you give to the function lives in Postgres'
namespace, not Python's.  Therefore you can only call the other function
using the SPI interface that PL/Python offers you.

> I am guessing this is important,
> i.e. how will I access the complete functionality of the general
> python libs?

This is quite different -- I'm guessing the Python library can be
accessed via normal means.  What you cannot do is use Postgres' CREATE
FUNCTION to define a regular Python function (i.e. you cannot extend the
"library").

--
Alvaro Herrera                        http://www.advogato.org/person/alvherre
Bob [Floyd] used to say that he was planning to get a Ph.D. by the "green
stamp method," namely by saving envelopes addressed to him as 'Dr. Floyd'.
After collecting 500 such letters, he mused, a university somewhere in
Arizona would probably grant him a degree.              (Don Knuth)

Re: How to 'register' functions, so they can be called (plpythonu)

From
Richard Huxton
Date:
blay bloo wrote:
> I am defining some functions using plpythonu, through the standard means.
>
> Here I have one function (test1) which calls another (testfunc). When
> I excute this I get the following error:
>
> ERROR:  plpython: function "test1" failed
> DETAIL:  <type 'exceptions.NameError'>: global name 'testfunc' is not defined

You need to call it as an SQL statement "SELECT testfunc(...)"

> However, from the console, select testfunc('test') works fine.

I believe the python embedder mangles the function names when it loads
them into PG, so you can't call them directly.

--
   Richard Huxton
   Archonet Ltd

Re: How to 'register' functions, so they can be called (plpythonu)

From
"blay bloo"
Date:
> I believe the python embedder mangles the function names when it loads
> them into PG, so you can't call them directly.


do you think it possible to use the internal system catalogs to lookup
the 'mangled' names?