Opened 6 years ago

Last modified 4 years ago

#29760 closed Bug

Cursors are closing explicitly in autocommit mode — at Initial Version

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

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 (0)

Note: See TracTickets for help on using tickets.
Back to Top