Re: Return SETOF or array from pl/python

From: Peter Fein <pfein(at)pobox(dot)com>
To: "Joshua D(dot) Drake" <jd(at)commandprompt(dot)com>
Cc: Postgresql-General list <pgsql-general(at)postgresql(dot)org>
Subject: Re: Return SETOF or array from pl/python
Date: 2005-07-15 16:49:49
Message-ID: 42D7E92D.8080001@pobox.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Joshua D. Drake wrote:
> Peter Fein wrote:
>
>> Is it possible to return a SETOF text or a text[] from pl/python?
>>
>> I've got the following test cases:
>>
>> CREATE OR REPLACE FUNCTION arf()
>> RETURNS text[] LANGUAGE plpythonu AS
>> $$return ["one", "two", "three"]$$;
>>
>> SELECT arf();
>>
>> ERROR: missing dimension value
>>
>> CREATE OR REPLACE FUNCTION arf2()
>> RETURNS text[] LANGUAGE plpythonu AS
>> $$return '{"one", "two", "three"}'$$;
>>
>> SELECT arf2();
>>
>> arf2
>> -----------------
>> {one,two,three}
>> (1 row)
>>
>> CREATE OR REPLACE FUNCTION srf()
>> RETURNS SETOF text LANGUAGE plpythonu AS
>> $$return ["one", "two", "three"]$$;
>>
>> SELECT * FROM srf();
>>
>> srf
>> -------------------------
>> ['one', 'two', 'three']
>> (1 row)
>>
>> SELECT srf();
>> Never returns.
>
>
> I am not an everyday python programmer but I am pretty sure that you are
> trying to return a list in arf(). You can't return a list you have to
> return the array type which is why arf2 works.

Ok. How does one convert a python list to a PGSql array then? Is there
a better way to do it than what I did in arf2?

> The reasons srf works is because you are just returning text in general
> regardless that it is formatting to a list.

How does one return a set then? I want as my output:

SELECT * FROM srf();

srf
-------------------------
'one'
'two'
'three'
(3 rows)

--
Peter Fein pfein(at)pobox(dot)com 773-575-0694

Basically, if you're not a utopianist, you're a schmuck. -J. Feldman

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Matt Miller 2005-07-15 16:50:30 Re: CVS - psql segfault
Previous Message Joshua D. Drake 2005-07-15 16:33:58 Re: Return SETOF or array from pl/python