Re: PL/pgSQL's "open cur for execute" provides workarouned... Re: cursors with prepared statements

From: Bryn Llewellyn <bryn(at)yugabyte(dot)com>
To: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
Cc: pgsql-general list <pgsql-general(at)lists(dot)postgresql(dot)org>, "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>, Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>
Subject: Re: PL/pgSQL's "open cur for execute" provides workarouned... Re: cursors with prepared statements
Date: 2023-04-16 21:41:31
Message-ID: 13318207-0E1A-4974-9B10-1ABD94804793@yugabyte.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> adrian(dot)klaver(at)aklaver(dot)com wrote:
>
>> bryn(at)yugabyte(dot)com wrote:
>>
>> It seems that there must be different underlying mechanisms at work and that this explains why creating a cursor using SQL to execute a prepared statement fails but doing this using PL/pgSQL succeeds. What's going on under the covers?
>
> Pretty sure:
>
> www.postgresql.org/docs/current/spi.html

I think that I see what you mean, Adrian. I had read the "PL/pgSQL Under the Hood" section to mean that, at run time, ordinary SQL calls were invariably made whenever the point of execution reached anything that implied SQL functionality (including, famously, expression evaluation). I'd assumed, therefore, that when the PL/pgSQL has an "open" statement, and when this is encountered at run time, the ordinary SQL "declare" statement was invoked.

But it seems, rather, that the SQL "declare" and the PL/pgSQL "open" each as its own implementation by lower-level prinitives—and that these differ in their details and in their power of expression. That would explain why the "pg_cursors.statement" text differs for cursors with identical properties (like scrollability), and the identically spelled subquery, like I showed in my earlier email.

It seems odd that the two approaches each has its own limitation(s).

— You can't create a cursor to execute a prepared statement using the SQL API; and you can't create a holdable cursor using the (static) PL/pgSQL API but can work around this with dynamic SQL.

—You have to dive down to a lower-level API like "Embedded SQL in C" (a.k.a. ECPG) to create a holdable cursor to execute a prepared statement.

But I appreciate that this comes with the territory and that anyone who feels strongly about this, and who knows how to do it, can develop their own patch and submit it for consideration.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message 黄宁 2023-04-17 02:25:46 Re: cursor with hold must be save to disk?
Previous Message FOUTE K. Jaurès 2023-04-16 19:22:27 Re: Call a Normal function inside a Trigger Function