﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
29760	Cursors are closing explicitly in autocommit mode	Ali Teoman Unay	nobody	"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 [http://initd.org/psycopg/docs/usage.html#server-side-cursors psycopg documentation], server-side cursors should not be closed explicitly if it is in autocommit mode.

According to [https://docs.djangoproject.com/en/2.1/topics/db/transactions/#autocommit 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().
 

"	Bug	new	Database layer (models, ORM)	2.1	Normal		cursor, sql, database, autocommit, transactions, postrgresql, psycopg		Unreviewed	0	0	0	0	0	0
