Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get lazy but reusable cursor with Psycopg2

I saw in the docs that you need to use server-side ("named") cursors to avoid loading all results into memory at once. But then I found that server-side cursors aren't reusable. You can only do a single .execute on them. (Otherwise you get ProgrammingError: can't call .execute() on named cursors more than once.)

Is there any way to get a cursor that I can re-use, but that also will not load all the data into memory at once?

I'm open to using a different Postgres driver if it will make things easier and more performant. I looked at PyGreSQL but it seems noticeably slower than a Postgres named cursor.

like image 344
BrenBarn Avatar asked Sep 12 '25 07:09

BrenBarn


1 Answers

Just don't reuse the cursors. Reuse the connections.

like image 105
piro Avatar answered Sep 14 '25 20:09

piro