Re: After 10 -> 15 upgrade getting "cannot commit while a portal is pinned" on one python function

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Jeff Ross <jross(at)openvistas(dot)net>
Cc: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: After 10 -> 15 upgrade getting "cannot commit while a portal is pinned" on one python function
Date: 2024-03-27 21:44:37
Message-ID: 3446817.1711575877@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Jeff Ross <jross(at)openvistas(dot)net> writes:
> I only use one plpython3u cursor in that function.  The plpython docs say:

> "Cursors are automatically disposed of. But if you want to explicitly
> release all resources held by a cursor, use the |close| method. Once
> closed, a cursor cannot be fetched from anymore."

> https://www.postgresql.org/docs/15/plpython-database.html#id-1.8.11.14.3

> Perhaps "pinned" in the error message means "open"?

No, it means "pinned" ... but I see that plpython pins the portal
underlying any PLyCursor object it creates. Most of our PLs do
that too, to prevent a portal from disappearing under them (e.g.
if you were to try to close the portal directly from SQL rather
than via whatever mechanism the PL wants you to use).

> I added a cursor.close() as the last line called in that function and it
> works again.

It looks to me like PLy_cursor_close does pretty much exactly the same
cleanup as PLy_cursor_dealloc, including unpinning and closing the
underlying portal. I'm far from a Python expert, but I suspect that
the docs you quote intend to say "cursors are disposed of when Python
garbage-collects them", and that the reason your code is failing is
that there's still a reference to the PLyCursor somewhere after the
plpython function exits, perhaps in a Python global variable.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jeff Ross 2024-03-27 23:05:21 Re: After 10 -> 15 upgrade getting "cannot commit while a portal is pinned" on one python function
Previous Message Jeff Ross 2024-03-27 16:02:11 Re: After 10 -> 15 upgrade getting "cannot commit while a portal is pinned" on one python function