Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Possible bug in cursor iterator

#126

Submitted by: Pietro Delsante <pietro.delsante at gmail.com>

Hello,

I have a script that uses psycopg2 2.4.5 (OS is Ubuntu 12.04, psycopg2 was installed from Ubuntu repository with apt-get) to open a connection to a local PostgreSQL database and another one to a remote PostgreSQL database, like that:


logger.info("Connecting to local database...")
try:

localconn = psycopg2.connect(host='localhost', dbname='somedb', user='someuser')
localcur = localconn.cursor(cursor_factory=psycopg2.extras.DictCursor)

except:

logger.critical("Could not connect to local database, aborting!")
sys.exit(0)

logger.info("Connecting to remote database...")
try:

remoteconn = psycopg2.connect(host='somehost', dbname='somedb', user='someuser', password='somepassword')
remotecur = remoteconn.cursor(cursor_factory=psycopg2.extras.DictCursor)

except:

logger.critical("Could not connect to remote database, aborting!")
sys.exit(0)

Then, I try to execute a SELECT query with one of the two cursors, then I start a cycle iterating on the results, like that:


remotecur.execute("SELECT * FROM sometable;")
for remoteresult in remotecur:

# Do something...

When I try to do that with the cursor pointing to the REMOTE database, everything seems to work pretty well; however, if I do the very same with the LOCAL database, like that;


localcur.execute("SELECT * FROM sometable;")
for localresult in localcur:

# Do something...

... I get this exception:


me@myhost:/path/to/# ./myscript.py
Traceback (most recent call last):
File "./myscript.py", line 37, in

for localsample in localcur:

File "/usr/lib/python2.7/dist-packages/psycopg2/extras.py", line 101, in iter

yield res.next()

psycopg2.ProgrammingError: no results to fetch


Of course, I checked that, in the LOCAL database, the query I am trying to execute does have a lot of results. In fact, if I modify my code to use fetchall() instead of using the iterator, like that:


localcur.execute("SELECT * FROM sometable;")
for localresult in localcur.fetchall():

# Do something...

... everything works pretty well, except that, of course, it's way slower.

Could this be a bug?

Thanks,
Pietro
pietro.delsante at gmail.com

Reported by Psycopg website · September 19th, 2012 @ 03:41 PM

State: invalid
Milestone: none
Assigned to: nobody

Activity

  1. Federico Di Gregorio
    Federico Di Gregorio

    It could be a bug but without the full script it's difficult to tell. Can you post it instead of just copy and pasting some lines, please?

    September 19th, 2012 @ 03:46 PM

  2. Daniele Varrazzo
    Daniele Varrazzo

    It is more likely that you are calling execute on one cursor and iterating on another one.

    September 19th, 2012 @ 03:49 PM

  3. Daniele Varrazzo
    Daniele Varrazzo
    • State changed from new to invalid

    Talked with the guy: he was using the cursor for other queries too, clearing the internal state.

    September 19th, 2012 @ 05:47 PM