Re: using server side cursor

From: Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com>
To: Federico Di Gregorio <fog(at)dndg(dot)it>
Cc: thomas veymont <thomas(dot)veymont(at)gmail(dot)com>, psycopg(at)postgresql(dot)org
Subject: Re: using server side cursor
Date: 2011-10-14 12:18:40
Message-ID: CA+mi_8YXS-LFit5++nDn2=ZA+quhh+MujJDgW9=cyHvdizCH1Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

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 thomas veymont 2011-10-14 13:22:23 Re: using server side cursor
Previous Message Daniele Varrazzo 2011-10-14 12:07:42 Re: using server side cursor