0
Traceback (most recent call last):
  File "/app/main.py", line 324, in that_function
    links = cur.fetchall()[0]
  File "/app/.heroku/python/lib/python3.10/site-packages/psycopg2/extras.py", line 104, in fetchall
    res = super().fetchall()
psycopg2.ProgrammingError: no results to fetch

I don't understand why I get this exception in this code:

cur.execute(
    "SELECT EXISTS(SELECT 1 FROM images where link = %s)", (original_url,)
)
links = cur.fetchall()[0]

What is the logical error in this? What's weird is in my infinite looping Python script, this happens randomly. The database is updated throughout the script however, so I'm not sure what is causing it.

4
  • 1
    This is a common cause of such problems. Commented Dec 10, 2022 at 14:57
  • @snakecharmerb Thank you, my script is indeed multithreaded and all the concurrent scheduled functions use the same cursor. Commented Dec 10, 2022 at 15:12
  • The same issue, psycopg2 + postgresql, when performance testing is run using Postman (50req/s), API occasionally gives 500 error with the same description, no results to fetch. Can't find the reason. Commented Jan 16, 2024 at 9:51
  • 1
    @VladVolkov I never came across the issue again after recoding my API to where every endpoint takes a connection from the connection pool, creates a cursor from it, does the necessary queries, and then closes the cursor and puts back the connection. Every endpoint does this separately so no cursor is used more than once at a time. Commented Jan 17, 2024 at 10:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.