Unix Technical Forum

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

This is a discussion on How to 'register' functions, so they can be called (plpythonu) within the Pgsql General forums, part of the PostgreSQL category; --> I am defining some functions using plpythonu, through the standard means. Here I have one function (test1) which calls ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > Pgsql General

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-09-2008, 07:21 PM
blay bloo
 
Posts: n/a
Default How to 'register' functions, so they can be called (plpythonu)

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;

---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-09-2008, 07:21 PM
Alvaro Herrera
 
Posts: n/a
Default Re: How to 'register' functions, so they can be called(plpythonu)

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)

---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-09-2008, 07:21 PM
Richard Huxton
 
Posts: n/a
Default Re: How to 'register' functions, so they can be called(plpythonu)

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

---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 05:32 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com