New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue with cursor.fetchone() raising an exception after doing 'SELECT' on a non-existent row #346
Comments
I assume your second example is wrong. However yes, that's right: psycopg differentiates between a command returning tuples (even if it returns no tuple) from one returning no tuples (such as INSERT without RETURNING or an ALTER TABLE). This is consistent with what the dbapi requires. |
I can confirm that a SELECT statement that returns no results, the original poster's 'How it is:' example, does in fact throw a "ProgrammingError: no results to fetch" error. In my case I get the above error when calling cursor.fetchall() dvarrazzo's answer doesn't appear to apply to this situation. Unfortunately filoleg's example is not wrong. Can you confirm this is a bug? If so, any idea when it might get fixed? Thanks Running: |
I don't know what are you talking about.
Please provide a complete test. |
dvarrazzo thanks for responding. Your example isn't indicative of the reported problem, in both his example, and my previous experience, it happened on a WHERE clause against a table filled with records (or at least in my case it did). I have a not insubstantial program littered with try/catch blocks to prevent this from crashing the program again. Unfortunately I can't repeat this behavior consistently. The try/catch blocks are remaining as I can't risk this bug crashing the program. If/when I can repeat this behavior constantly I will post that block of code and a test table to run it against. Until then...... |
I have never seen the behaviour you report. Please add a print and showing cur.statusmessage when |
I have never seen this before also. Thanks for |
I have also observed this bug. I reuse my connection heavily among multiple threads, including to update rows and insert new ones. I have also multiple processes reading and modifying the same table simultaneously. I never reuse cursors. In fact, I create a new cursor locally every time I execute a query. So I am certain the cursor throwing the error just executed a My experience is similar to that reported by @coleman-rik above: Most of the time, the code below (when run in parallel with a lot of other code running in other threads and other processes) returns
|
I am seeing this as well. Same scenario:
I am on RHEL 7, Python 2.7.13, psycopg2 2.7.3.2. I have not seen issues at 2, 4, 8, 16, 32 concurrent processes. |
Couple more details: Printing out The |
Dug in a bit more, these errors start when there are more than 40 concurrent connections. Not sure if this is hitting a connection limit on postgres (which I am doubtful of as I have max connections in postgres set to 256) or some other connection related issue. Given that it occurs at the certain number of concurrent connections, it has to be a postgres or OS related limit and not a client limitation. However, psycopg2 is misinterpreting the actual issue as no results. |
I have also observed this issue with a high commit load but only 2 connections. The connection that is having the isue only uses select statements. |
I've seen this happening but it was totally my fault. In case it helps someone, here was my scenario: I had a helper function so my queries would be less verbose (probably not one of my brightest ideas). But the thing is, in some very specific cases (running a query without any argument), there was a flaw in my function that skipped Obviously "no results to fetch (execute didn't run)" |
I would also request a more helpful error message. In my case, I was running cur.execute(), but I was not awaiting the call (I was porting code from gevent). As a result I saw "no results to fetch" on any query that I ran |
Any solution? I have the same error |
Possible cause of the issue reported in #346 (in concurrent environments).
Possible cause of the issue reported in #346 (in concurrent environments).
@b-l-a-c-k-b-e-a-r-d @hi117 @jmoraleda @coleman-rik I have fixed some suspicious management of the cursor state that may lead to the problems you describe. I have released psycopg 2.7.7 with the correction: please test with this package and let me know if the problem disappear. This is only valid for the people who have experienced problems in multithread environments. If you just run |
I think the rollbars we've been seeing on the model endpoints are the result of this issue: psycopg/psycopg2#346
* Upgrade psycopg2 I think the rollbars we've been seeing on the model endpoints are the result of this issue: psycopg/psycopg2#346 * Don't pin to 2.7.7 * Increment jardin version
I get this error too (some times!) code example:
|
I have the same problem.
will produce None, and it does when I run this query alone interactively. I think I was able to reproduce it interactively too. If I run:
I get the Update: yes indeed there was a |
According to pretty much everything I read about psycopg2, doing cursor.fetchone() on a result of a 'SELECT' command that finds nothing should return a 'None' object (which makes sense and makes it easy to work with). Instead, however, it throws a "psycopg2.ProgrammingError: no results to fetch," as if a cursor.fetchone() was used on a result of a command that's not supposed to return something by default (e.g. DELETE, where it is totally understandable) instead of SELECT.
Code samples.
How it is supposed to be:
How it is:
Please, confirm whether it is an actual bug or how it is actually supposed to function.
The text was updated successfully, but these errors were encountered: