[Fixed]-How can I use server-side cursors with django and psycopg2?

13๐Ÿ‘

โœ…

As you mention in your question but Iโ€™ll reiterate here for future readers: itโ€™s also possible to use explicitly named cursors without bypassing Djangoโ€™s public API:

from django.db import connection, transaction

with transaction.atomic(), connection.cursor() as cur:
    cur.execute("""
        DECLARE mycursor CURSOR FOR
        SELECT *
        FROM giant_table
    """)
    while True:
        cur.execute("FETCH 1000 FROM mycursor")
        chunk = cur.fetchall()
        if not chunk:
            break
        for row in chunk:
            process_row(row)
๐Ÿ‘คDavid Wolever

0๐Ÿ‘

Cursors should be used inside transactions. You need to define a transaction and to use the cursor inside it.

โ€” need to be in a transaction to use cursors.

Taken from here.

๐Ÿ‘คLajos Arpad

0๐Ÿ‘

I was getting this due to isolation_level='AUTOCOMMIT' in my sqlalchemy.create_engine.

๐Ÿ‘คdavetapley

Leave a comment