Database Reference
In-Depth Information
What you see here is that the PL/Python arguments are passed to the Python code
after converting them to appropriate types, and the result is passed back and con-
verted to the appropriate PostgreSQL type for the return value.
Functions returning a record
To return a record from a Python function, you can use:
• A sequence or list of values in the same order as the fields in the return
record
• A dictionary with keys matching the fields in the return record
• A class or type instance with attributes matching the fields in the return
record
Here are samples of the three ways to return a record.
First, using an instance:
CREATE OR REPLACE FUNCTION userinfo(
INOUT username name,
OUT user_id oid,
OUT is_superuser boolean)
AS $$
class PGUser:
def
__init__(self,username,user_id,is_superuser):
self.username = username
self.user_id = user_id
self.is_superuser = is_superuser
u = plpy.execute("""\
select usename,usesysid,usesuper
from pg_user
where usename = '%s'""" %
username)[0]
user = PGUser(u['usename'], u['usesysid'],
u['usesuper'])
return user
$$ LANGUAGE plpythonu;
Search WWH ::




Custom Search