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