SPI_ERROR_UNCONNECTED in python callback function

Started by Ian Haywoodalmost 17 years ago2 messagesgeneral
Jump to latest
#1Ian Haywood
ihaywood@iinet.net.au

I am using PL/Python to make queries via SPI. This seems to work when the
query is made directly, but when I pass a function as a callback to an external
module (in this case, python's cheetah templating engine), the function fails thusly:

=# select common.template(8963,1);
ERROR: plpython: function "template" could not create return value
DETAIL: <class 'plpy.SPIError'>: SPI_execute failed: SPI_ERROR_UNCONNECTED
=#

The code is

from Cheetah.Template import Template
import os, tempfile

def sql(x):
return plpy.execute(x)[0] # this is the callback function
def row(t,f,v):
return sql("select * from %s where %s = %s" % (t,f,v))
q = row("common.lu_templates","pk",template)
templ = q["definition"]
result = Template(templ, searchList=[{'row':row,'key':key,'plpy':plpy,'GD':GD,'sql':sql}]) # this template is calling the sql function above.
return result

It seems as soon as execution leaves the main function access to SPI is lost. Is this correct or a bug?
Is there any way to re-enable access to SPI?

Postgresql is 8.3.3, Python 2.5.2

Ian

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Ian Haywood (#1)
Re: SPI_ERROR_UNCONNECTED in python callback function

Ian Haywood <ihaywood@iinet.net.au> writes:

It seems as soon as execution leaves the main function access to SPI is lost. Is this correct or a bug?

What this probably means is that plpython is failing to use
SPI_push/SPI_pop properly --- these should be called around
any operation that might invoke an independent SPI-using function:
http://www.postgresql.org/docs/8.3/static/spi-spi-push.html

I'm not enough of a plpython user to be excited about trying to fix it,
but if you are then please send a patch.

regards, tom lane