如何在django和psycopg2中使用服务器端游标?

drs*_*drs 8 django postgresql transactions psycopg2

我正在尝试在psycop2中使用服务器端光标,详见本博文.从本质上讲,这是通过实现的

from django.db import connection

if connection.connection is None:
    cursor = connection.cursor()
    # This is required to populate the connection object properly

cursor = connection.connection.cursor(name='gigantic_cursor')
Run Code Online (Sandbox Code Playgroud)

当我执行查询时:

cursor.execute('SELECT * FROM %s WHERE foreign_id=%s' % (table_name, id))
Run Code Online (Sandbox Code Playgroud)

我得到一个ProgrammingError:

psycopg2.ProgrammingError: can't use a named cursor outside of transactions
Run Code Online (Sandbox Code Playgroud)

我天真地尝试使用创建一个事务

cursor.execute('BEGIN')
Run Code Online (Sandbox Code Playgroud)

在执行SELECT声明之前.但是,这会导致从cursor.execute('BEGIN')线路生成相同的错误.

我也试过用

cursor.execute('OPEN gigantic_cursor FOR SELECT * FROM %s WHERE foreign_id=%s' % (table_name, id))
Run Code Online (Sandbox Code Playgroud)

但我得到了相同的结果.

如何在django中进行交易?

Dav*_*ver 7

正如你在问题中提到的那样,我将在此重申未来的读者:也可以使用明确命名的游标而不绕过Django的公共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)
Run Code Online (Sandbox Code Playgroud)