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-16 00:53:26
Message-ID: 42D85A86.7020308@pobox.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Joshua D. Drake wrote:
> Somebody may know of a better way but what I would think would happen is
> this:
>
> Break up list, insert each value of list into a temp table as a row,
> return set of temp table.

Ok. I tried this & ran in to some trouble:

CREATE OR REPLACE FUNCTION setret(text)
RETURNS SETOF record AS
$BODY$plpy.execute("""
CREATE TEMP TABLE my_temp
(
clean_text text NOT NULL
) WITHOUT OIDS
ON COMMIT DROP;
""")

for i in text.split():
plpy.execute("INSERT INTO my_temp VALUES (%s)"%i.lower())

# Do SQL stuff with my_temp - JOIN it to permanent tables, etc..

return plpy.execute("SELECT * FROM my_temp")$BODY$
LANGUAGE 'plpythonu' STABLE STRICT;
ALTER FUNCTION setret(text) OWNER TO postgres;

SELECT setret('foo BAR baz Quux');
ERROR: plpython functions cannot return type record

SELECT * FROM setret('foo BAR baz Quux');
ERROR: a column definition list is required for functions returning
"record"

After much googling, I found
http://archives.postgresql.org/pgsql-general/2005-03/msg01488.php
which indicates it can't be done. Any suggestions?

I suppose I could have my python function return an array of its
processed values and then write a plsql function that loops over it,
returning records (unless there's a builtin to do that?). I need to do
further SQL operations on the output of the python function (JOINs,
etc.). This seems ugly/slow. The whole mess eventually goes in to a
trigger, so doing it client-side isn't an option.

--
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

Browse pgsql-general by date

  From Date Subject
Next Message Berend Tober 2005-07-16 02:04:07 Re: How to create unique constraint on NULL columns
Previous Message Thomas F. O'Connell 2005-07-15 23:38:30 Re: ERROR: could not open relation