PL/Python - Mailing list pgsql-general

From Tom Jenkins
Subject PL/Python
Date
Msg-id 200301211205.44124.tjenkins@devis.com
Whole thread Raw
Responses Re: PL/Python
List 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



pgsql-general by date:

Previous
From: Peter Childs
Date:
Subject: Logging of queryies
Next
From: Tom Lane
Date:
Subject: Re: LWLockAcquire