psycopg / psycopg2

PostgreSQL database adapter for the Python programming language
https://www.psycopg.org/
Other
3.35k stars 506 forks source link

Using psycopg.connect hangs for around 90 seconds on the latest versions of psycopg2-binary #1723

Closed bvanelli closed 2 months ago

bvanelli commented 2 months ago

This is a bug tracker

Please complete the following information:

Describe the bug

I noticed the hanging of psycopg2-binary starting from version 2.9.6. Here is the script I used (pg must be started via docker):

import time
import psycopg2

# run docker with
# docker run --name postgres -e POSTGRES_PASSWORD=postgres -p 5432:5432 -d postgres
t = time.perf_counter()
conn = psycopg2.connect("dbname=postgres user=postgres password=postgres host=localhost")
t_elapsed = time.perf_counter() - t
print(f"Elapsed time on version {psycopg2.__version__} was  {t_elapsed:.2f}")

Seems like this bug was introduced in version 2.9.7, here are the outputs:

psycopg2 version Output
2.9.6 Elapsed time on version 2.9.6 (dt dec pq3 ext lo64) was 0.07
2.9.7 Elapsed time on version 2.9.7 (dt dec pq3 ext lo64) was 90.19
2.9.8 Elapsed time on version 2.9.8 (dt dec pq3 ext lo64) was 90.24
2.9.9 Elapsed time on version 2.9.9 (dt dec pq3 ext lo64) was 90.20

With debug I could go as far as the Python wrapper goes, up to the call:

    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)

Could this somehow be unrelated to the python wrapper, but rather to the libpg under it?

bvanelli commented 2 months ago

Here is the diff between the versions: https://github.com/psycopg/psycopg2/compare/2.9.6..2.9.7

Possible causes:

dvarrazzo commented 2 months ago

Usually on macOS it is some exotic connection method timing out, such as GSS. See #1645 for example.

bvanelli commented 2 months ago

Thanks @dvarrazzo , I tried looking for a related issue yesterday but I could not find anything, this issue you mentioned is exactly what was needed.

For anyone that may com to this thread, the solution was to go to Ticket Viewer and remove all entries. It had some expired tickets:

image

Not it connects as expected 👍