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