PL/Python

From: Tom Jenkins <tjenkins(at)devis(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: PL/Python
Date: 2003-01-21 17:05:44
Message-ID: 200301211205.44124.tjenkins@devis.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hey all,
In 7.2 we found a bug in PL/Python (i don't know if its still there in 7.3 - i
haven't downloaded the sources yet).

When we create a function like so:

CREATE OR REPLACE FUNCTION jojo (timestamp) RETURNS varchar AS '
pln = plpy.prepare("UPDATE jtab SET jfld = $1",["timestamp"])
rcs = plpy.execute(pln,[args[0]])
return "YES"' LANGUAGE 'PLPYTHON';

SELECT jojo(now());
works fine but

SELECT jojo(NULL);
errors out with
NOTICE: plpython: in function __plpython_procedure_jojo_49818349:
plpy.Error: Unknown error in PLy_spi_execute_plan
ERROR: Bad timestamp external representation 'None'

in plpython.c the function PLy_spi_execute_plan has the following snippet:
elem = PySequence_GetItem(list, i);
so = PyObject_Str(elem);
sv = PyString_AsString(so);
/*
* FIXME -- if this can elog, we have leak
*/
plan->values[i] = FunctionCall3(&(plan->args[i].out.d.typfunc),
CStringGetDatum(sv),
ObjectIdGetDatum(plan->args[i].out.d.typelem),
Int32GetDatum(-1));

this looks to me that the None object is getting converted into 'None'
(through PyString_AsString call).

I tried a quick fix but it crashes the backend:
elem = PySequence_GetItem(list, i);
if ( elem != Py_None )
{
so = PyObject_Str(elem);
sv = PyString_AsString(so);
/*
* FIXME -- if this can elog, we have leak
*/
plan->values[i] = FunctionCall3(&(plan->args[i].out.d.typfunc),
CStringGetDatum(sv),
ObjectIdGetDatum(plan->args[i].out.d.typelem),
Int32GetDatum(-1));

Py_DECREF(so);
}
else
{
plan->values[i] = FunctionCall3(&(plan->args[i].out.d.typfunc),
PointerGetDatum(NULL),
ObjectIdGetDatum(plan->args[i].out.d.typelem),
Int32GetDatum(-1));
}

Py_DECREF(elem);

I believe the logic is correct (if the python object is None then send to the
typfunc a NULL) but I don't know how to code it correctly.

Thanks for any help,

Tom

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2003-01-21 17:42:23 Re: LWLockAcquire
Previous Message Dennis Gearon 2003-01-21 16:59:07 Re: repost of how to do select in a constraint