Re: Bug in plpython's Python Generators

Lists: pgsql-hackers
From: Jean-Baptiste Quenot <jbq(at)caraldi(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Bug in plpython's Python Generators
Date: 2010-10-21 12:20:16
Message-ID: AANLkTimLMr-u89f7vf4AckB6xH=V3uf+PxraOg4XB4=o@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hi there,

I can't make Python Generators to work reliably. According to the
documentation, this should work:

CREATE OR REPLACE FUNCTION foobar()
RETURNS SETOF text AS
$$
for s in ('Hello', 'World'):
plpy.execute('select 1')
yield s
$$
LANGUAGE 'plpythonu';

I get this error when calling the function:

test=# select foobar();
ERROR: error fetching next item from iterator
CONTEXT: PL/Python function "foobar"

When I remove the dummy plpy.execute() call, it works:

CREATE OR REPLACE FUNCTION foobar()
RETURNS SETOF text AS
$$
for s in ('Hello', 'World'):
yield s
$$
LANGUAGE 'plpythonu';

test=# select foobar();
foobar
--------
Hello
World
(2 rows)

Seems like calls to plpy.execute() conflict with generators. This is
the case both on versions 8.4.4 and 9.0.1.

All the best,
--
Jean-Baptiste Quenot


From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: Jean-Baptiste Quenot <jbq(at)caraldi(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Bug in plpython's Python Generators
Date: 2010-10-21 18:32:53
Message-ID: 1287685918-sup-456@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Excerpts from Jean-Baptiste Quenot's message of jue oct 21 09:20:16 -0300 2010:

> I get this error when calling the function:
>
> test=# select foobar();
> ERROR: error fetching next item from iterator

I can reproduce this here. The first bug to solve is, I think, getting
a more meaningful error report.

--
Álvaro Herrera <alvherre(at)commandprompt(dot)com>
The PostgreSQL Company - Command Prompt, Inc.
PostgreSQL Replication, Consulting, Custom Development, 24x7 support


From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
Cc: Jean-Baptiste Quenot <jbq(at)caraldi(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Bug in plpython's Python Generators
Date: 2010-10-21 18:48:57
Message-ID: 1287686694-sup-8959@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Excerpts from Alvaro Herrera's message of jue oct 21 15:32:53 -0300 2010:
> Excerpts from Jean-Baptiste Quenot's message of jue oct 21 09:20:16 -0300 2010:
>
> > I get this error when calling the function:
> >
> > test=# select foobar();
> > ERROR: error fetching next item from iterator
>
> I can reproduce this here. The first bug to solve is, I think, getting
> a more meaningful error report.

Something like this. Somebody that really knows their way around Python
has to clean this up.

alvherre=# select * from foobar();
ERROR: error extrayendo el próximo elemento del iterador
CONTEXTO: falló SPI_execute: SPI_ERROR_UNCONNECTED
función PL/Python «foobar»

I think all error cases in plpython need some improvement so that they
show the error message from Python. Right now they are ignored.

... and presumably somebody can fix the real bug that Jean-Baptiste hit,
too.

--
Álvaro Herrera <alvherre(at)commandprompt(dot)com>
The PostgreSQL Company - Command Prompt, Inc.
PostgreSQL Replication, Consulting, Custom Development, 24x7 support

Attachment Content-Type Size
plpy.patch application/octet-stream 908 bytes

From: Jan Urbański <wulczer(at)wulczer(dot)org>
To: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
Cc: Jean-Baptiste Quenot <jbq(at)caraldi(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Bug in plpython's Python Generators
Date: 2010-10-23 22:32:45
Message-ID: 4CC3628D.8070405@wulczer.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 21/10/10 20:48, Alvaro Herrera wrote:
> Excerpts from Alvaro Herrera's message of jue oct 21 15:32:53 -0300 2010:
>> Excerpts from Jean-Baptiste Quenot's message of jue oct 21 09:20:16 -0300 2010:
>>
>>> I get this error when calling the function:
>>>
>>> test=# select foobar();
>>> ERROR: error fetching next item from iterator

> I think all error cases in plpython need some improvement so that they
> show the error message from Python. Right now they are ignored.
>
> ... and presumably somebody can fix the real bug that Jean-Baptiste hit,
> too.

AFAICS the error comes from PLy_function_handler disconnecting from SPI
after calling into the Python code and then going ahead and reading the
result from the iterator. The problem is that after calling PyIter_Next
to resume the iterator and fetch the next value, it tries to do
plpy.execute("select 1"), which fails because SPI is already
disconnected. This probably means that a SRF should not disconnect from
SPI until it has finished reading the result.

The error handling in plpython is well-known to be a mess, hence the
confusing error message that OP got. Another annoying thing is that SPI
calls are not done in a subtransaction, which means you can't trap
errors with a try/catch Python construct. I'm currently trying to get
try/catch to work and might end up cleaning up error handling as well...
but this can take me some time, so maybe someone should plug this
particular hole right away.

Cheers,
Jan


From: Jan Urbański <wulczer(at)wulczer(dot)org>
To: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
Cc: Jean-Baptiste Quenot <jbq(at)caraldi(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>, Oleg Bartunov <oleg(at)sai(dot)msu(dot)su>
Subject: Re: Bug in plpython's Python Generators
Date: 2010-11-14 16:44:04
Message-ID: 4CE011D4.3070604@wulczer.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 24/10/10 00:32, Jan Urbański wrote:
> On 21/10/10 20:48, Alvaro Herrera wrote:
>> ... and presumably somebody can fix the real bug that Jean-Baptiste hit,
>> too.
>
> AFAICS the error comes from PLy_function_handler disconnecting from SPI
> after calling into the Python code and then going ahead and reading the
> result from the iterator.

Here's a patch with a fix for that bug.

Cheers,
Jan

Attachment Content-Type Size
plpython-generator-fix.diff text/x-patch 3.2 KB

From: Marti Raudsepp <marti(at)juffo(dot)org>
To: Jan Urbański <wulczer(at)wulczer(dot)org>
Cc: Alvaro Herrera <alvherre(at)commandprompt(dot)com>, Jean-Baptiste Quenot <jbq(at)caraldi(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Bug in plpython's Python Generators
Date: 2010-11-14 18:07:44
Message-ID: AANLkTi=Ai6dioMPWaBcZCoRWkMgF4Gq=uQCaX9NL1UrG@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Sun, Oct 24, 2010 at 01:32, Jan Urbański <wulczer(at)wulczer(dot)org> wrote:
> The error handling in plpython is well-known to be a mess, hence the
> confusing error message that OP got. Another annoying thing is that SPI
> calls are not done in a subtransaction, which means you can't trap
> errors with a try/catch Python construct. I'm currently trying to get
> try/catch to work and might end up cleaning up error handling as well...
> but this can take me some time, so maybe someone should plug this
> particular hole right away.

While on this topic, maybe you can chime in on the pgsql-docs list for
my documentation patches to document the current (broken) behavior?

Regards,
Marti


From: Oleg Bartunov <oleg(at)sai(dot)msu(dot)su>
To: Jan Urbański <wulczer(at)wulczer(dot)org>
Cc: Alvaro Herrera <alvherre(at)commandprompt(dot)com>, Jean-Baptiste Quenot <jbq(at)caraldi(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Bug in plpython's Python Generators
Date: 2010-11-14 21:04:41
Message-ID: Pine.LNX.4.64.1011150004180.12632@sn.sai.msu.ru
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Thanks, Jan.

I tested the patch and it looks fine.

Oleg
On Sun, 14 Nov 2010, Jan Urbaski wrote:

> On 24/10/10 00:32, Jan UrbaЪЪski wrote:
>> On 21/10/10 20:48, Alvaro Herrera wrote:
>>> ... and presumably somebody can fix the real bug that Jean-Baptiste hit,
>>> too.
>>
>> AFAICS the error comes from PLy_function_handler disconnecting from SPI
>> after calling into the Python code and then going ahead and reading the
>> result from the iterator.
>
> Here's a patch with a fix for that bug.
>
> Cheers,
> Jan
>

Regards,
Oleg
_____________________________________________________________
Oleg Bartunov, Research Scientist, Head of AstroNet (www.astronet.ru),
Sternberg Astronomical Institute, Moscow University, Russia
Internet: oleg(at)sai(dot)msu(dot)su, http://www.sai.msu.su/~megera/
phone: +007(495)939-16-83, +007(495)939-23-83


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Jan Urbański <wulczer(at)wulczer(dot)org>
Cc: Alvaro Herrera <alvherre(at)commandprompt(dot)com>, Jean-Baptiste Quenot <jbq(at)caraldi(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>, Oleg Bartunov <oleg(at)sai(dot)msu(dot)su>
Subject: Re: Bug in plpython's Python Generators
Date: 2010-11-15 19:29:21
Message-ID: 4845.1289849361@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

=?UTF-8?B?SmFuIFVyYmHFhHNraQ==?= <wulczer(at)wulczer(dot)org> writes:
> On 24/10/10 00:32, Jan Urbaski wrote:
>> On 21/10/10 20:48, Alvaro Herrera wrote:
>>> ... and presumably somebody can fix the real bug that Jean-Baptiste hit,
>>> too.

>> AFAICS the error comes from PLy_function_handler disconnecting from SPI
>> after calling into the Python code and then going ahead and reading the
>> result from the iterator.

> Here's a patch with a fix for that bug.

Applied back to 8.2. Thanks for the patch.

regards, tom lane