Re: using server side cursor

From: thomas veymont <thomas(dot)veymont(at)gmail(dot)com>
To: psycopg(at)postgresql(dot)org
Subject: Re: using server side cursor
Date: 2011-10-14 13:22:23
Message-ID: CAHcTkqqaZTdWRkhmbM00OW5dGOo_m9HvAmD-BY7csV-jSobXNw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

thanks for your helpful answers.

>> mycursor.execute ( "fetch mycursor" )
is working okay.

you are right : using the psycopg native support for cursors makes
clearer code. But, yes indeed, the pgsql function is somewhat a
mandatory API to the database, so the Python code doesn't have to know
the inner query structure.

Daniele, beside the hack you are providing, you say : "all cursors
from the same connections live in the same transaction"
=> is it something specific that is true today but may change in the
future ? I mean, may I rely on this for a long-living code ?

thanks again
Tom

2011/10/14 Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com>:
> On Fri, Oct 14, 2011 at 12:41 PM, Federico Di Gregorio <fog(at)dndg(dot)it> wrote:
>> On 14/10/11 12:32, thomas veymont wrote:
>> [snip]
>>> I guess I'm doing it the wrong way. There's a doc in psycopg
>>> about named cursors but I don't understand exactly how I should
>>> follow it to tie to my problem. (that is : using my existing pgpsql function
>>> that returns a cursor, then iterate on that cursor).
>>>
>>> any suggestion ?
>>
>> Yes, use the native support for server-side cursors in psycopg. First,
>> don't use a procedure to setup the cursor but just pass to execute()
>> your SQL:
>
> He has a point though: what if somebody has a database function call
> to be used as interface?
>
> Oh, there's a nice hack that can be done :P because all cursors from
> the same connections live in the same transaction, one can use a
> regular cursor to create the postgres refcursor and a named cursor to
> iterate it:
>
>>>> cur1 = cnn.cursor()
>>>> cur1.callproc('myfunction', ['mycursor'])
> ['mycursor']
>
>>>> cur2 = cnn.cursor('mycursor')
>>>> cur2.fetchone()
> (1,)
>>>> cur2.fetchmany(2)
> [(2,), (3,)]
>
> Nasty :D
>
>
> -- Daniele
>

In response to

Responses

Browse psycopg by date

  From Date Subject
Next Message Federico Di Gregorio 2011-10-14 13:41:42 Re: using server side cursor
Previous Message Daniele Varrazzo 2011-10-14 12:18:40 Re: using server side cursor