Skip site navigation (1) Skip section navigation (2)

Re: how to returns set of records in PL/python

From: Gerardo Herzig <gherzig(at)fmed(dot)uba(dot)ar>
To: Anoop G <anoopmadavoor(at)gmail(dot)com>, "pgsql-sql(at)postgresql(dot)org" <pgsql-sql(at)postgresql(dot)org>
Subject: Re: how to returns set of records in PL/python
Date: 2008-04-07 13:03:38
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-sql
Anoop G wrote:

>Hi ,
>  Iam new to plpython,how can I return a  recordset from a plpython
>  Is there is any way give me an example;
>plpgsql function
>CREATE OR REPLACE FUNCTION function_to_get_all_countries() RETURNS SETOF
>    r RECORD;
>    FOR r IN SELECT  pk_bint_country_id,vchr_country FROM tbl_country LOOP
>        RETURN NEXT r;
>        END LOOP;
>    RETURN;
>$BODY$ LANGUAGE 'plpgsql';
>How i can get the same result using plpythonu and how I can call the
>function (is there any user defined type required like in plpgsql)?
>Thanks in advance
Yes, plpython can return setofs. You may need to define a new type for that.
After that you can return a list, tuple, dictionary, set, generator 
object, or any

You may also check the plpython version, as i recall, its kind of *new* 

This works on 8.2.5, python 2.5,

For a simple case, something like that would work
regression=# \d countries
           Table "public.countries"
    Column    |       Type        | Modifiers
 country_id   | integer           |
 country_name | character varying |

returns setof countries
security definer
return plpy.execute("select * from countries")

$$ language plpythonu;

Hope that helps.

In response to

pgsql-sql by date

Next:From: Gerardo HerzigDate: 2008-04-07 13:04:31
Subject: Re: how to returns set of records in PL/python
Previous:From: A. KretschmerDate: 2008-04-07 13:01:40
Subject: Re: Commit every processed record

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group