Opened 6 years ago

Last modified 5 years ago

#29760 closed Bug

Cursors are being closed explicitly in autocommit mode — at Version 3

Reported by: Ali Teoman Unay Owned by: nobody
Component: Database layer (models, ORM) Version: 1.11
Severity: Normal Keywords: cursor, sql, database, autocommit, transactions, postrgresql, psycopg
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Ali Teoman Unay)

Recently we started to get this exception time to time:

OperationalError: cursor “_django_curs_<id>” does not exist

especially when our traffic is higher than usual. Each time the error is in a different line of the code so it is not easy to follow but appearantly it is a synchronisation issue; the cursor is being closed before the transaction ended.

In django.db.models.sql.compiler.SQLAggregateCompiler:

def cursor_iter(cursor, sentinel, col_count, itersize):
    """
    Yield blocks of rows from a cursor and ensure the cursor is closed when
    done.
    """
    try:
        for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
            yield rows if col_count is None else [r[:col_count] for r in rows]
    finally:
        cursor.close()

According to psycopg documentation, server-side cursors should not be closed explicitly if it is in autocommit mode.

According to Django documentation, Django sets autocommit mode true in default settings.

Of course it is possible to set withhold setting to false. In this case, cursor.close() must be called at the end but otherwise, according to the documentation, it should not be called at all. So if I am not mistaken there should be a conditional statement before calling cursor.close().

Change History (3)

comment:1 by Ali Teoman Unay, 6 years ago

Summary: Cursors are closing explicitly in autocommit modeCursors are being closed explicitly in autocommit mode

comment:2 by Tim Graham, 6 years ago

Duplicate of #29257?

comment:3 by Ali Teoman Unay, 6 years ago

Description: modified (diff)
Version: 2.11.11
Note: See TracTickets for help on using tickets.
Back to Top