Re: PL/Python: How do I use result methods?

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Oliver Elphick <olly(at)lfix(dot)co(dot)uk>
Cc: pgsql-interfaces(at)postgresql(dot)org
Subject: Re: PL/Python: How do I use result methods?
Date: 2004-10-16 16:56:00
Message-ID: 20041016165600.GA71873@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-interfaces

On Sat, Oct 16, 2004 at 03:34:29PM +0100, Oliver Elphick wrote:
>
> According to the docs (8.0):
>
> Calling plpy.execute with a query string and an optional limit
> argument causes that query to be run and the result to be
> returned in a result object. The result object emulates a list
> or dictionary object. The result object can be accessed by row
> number and column name. It has these additional methods: nrows
> which returns the number of rows returned by the query, and
> status which is the SPI_execute() return value.
>
> What exactly is the syntax for using the nrows and status methods?
>
> I am trying to use it like this:
>
> ...
> arec = plpy.execute(plan, [ addrno ], 1)
> plpy.info("Executed plan")
> n = plpy.nrows(arec)

The documentation describes nrows as being a method of the result
object, so I'd guess it should be used thusly:

n = arec.nrows()

I tried that with the following function:

CREATE OR REPLACE FUNCTION test() RETURNS INTEGER AS $$
rv = plpy.execute('SELECT usename FROM pg_user')
n = rv.nrows()
return 1
$$ LANGUAGE plpythonu;

The function failed:

test=> SELECT test();
ERROR: plpython: function "test" failed
DETAIL: exceptions.SystemError: error return without exception set

Digging through plpython.c I discovered the following:

#ifdef NOT_USED
/* Appear to be unused */
static PyMethodDef PLy_result_methods[] = {
{"fetch", (PyCFunction) PLy_result_fetch, METH_VARARGS, NULL,},
{"nrows", (PyCFunction) PLy_result_nrows, METH_VARARGS, NULL},
{"status", (PyCFunction) PLy_result_status, METH_VARARGS, NULL},
{NULL, NULL, 0, NULL}
};
#endif

It looks like nrows(), status(), and fetch() aren't available as
result object methods. I don't know what would happen if you
un-#ifdef-ed that block and the two others that are related;
perhaps one of the developers can tell us what's going on.

If the methods aren't available then they probably shouldn't be
mentioned in the doc.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andrew Dunstan 2004-10-16 20:22:30 win32 pg_autovacuum make error
Previous Message Tom Lane 2004-10-16 16:54:17 Getting rid of AtEOXact_Buffers (was Re: [Testperf-general] Re: [PERFORM] First set of OSDL Shared Memscalability results, some wierdness ...)

Browse pgsql-interfaces by date

  From Date Subject
Next Message K. Richard Pixley 2004-10-19 07:12:38 Er... what's up and what's current?
Previous Message Oliver Elphick 2004-10-16 14:34:29 PL/Python: How do I use result methods?