I ran into an interesting issue trying to pass an array to a plpythonu function (Postgresql 8.03).
When I googled the issue I found a number of people asking similar questions and they haven't found
an answer. The problem is that there is no type mapped from a postgresql array to a python list.
These conversion functions will map between a postgresql array and a python list and back. Is there
a way to stick this in the integration code somewhere, so that every time an array is passed to/from
a python function it converts it transparently?
In the python function itself this type of code will work, what I don't know is how to change the
internals so that when an array variable is passed in it automatically puts it through this code and
hands off a python list variable.
The second function would work as is, and return a postgresql array.
Of course both would have to work with any type of array and not just text[]
create or replace function pgarray_to_pylist(text[]) returns {python list} as
$$
parm=args[0]
parm=parm.replace("{","[").replace("}","]")
pylist=eval(parm)
return pylist
$$ language 'plpythonu'
create or replace function pylist_to_pgarray({python list}) return text[] as
$$
parm=`args[0]`
parm=parm.replace("[","{").replace("]","}")
return parm
$$ language 'plpythonu'