psycopg / psycopg2

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

This script causes a hang issue #1654

Closed cuteDen-ECNU closed 10 months ago

cuteDen-ECNU commented 11 months ago

This is a bug tracker If you have a question, such has "how do you do X with Python/PostgreSQL/psycopg2" please write to the mailing list or open a question instead.

Please complete the following information:

Describe the bug Please let us know:

Executing the following script will cause a hang issue:

$ cat test.py
import psycopg2
db_params = {
            'host': "localhost",
            'port': "5432",
            'database': "nyc",
            'user': "postgres",
        }
conn1 = psycopg2.connect(**db_params)
cursor1 = conn1.cursor()

conn = psycopg2.connect(**db_params)

cursor = conn.cursor()
cursor.execute('DROP TABLE IF EXISTS tableB;' )
cursor.execute('CREATE TABLE tableB (id int);' )
cursor.execute('DROP TABLE IF EXISTS tableB;' )
cursor1.execute('DROP TABLE IF EXISTS tableB;' )

$ timeout 30 python3 test.py

$ echo $?
124

124 is a timeout error code. It executed without returning results, which seems a bug.

dvarrazzo commented 10 months ago

It's definitely not a bug. Most likely you have database locks that stops these operations to complete. You can look at the pg_locks table to figure out what is locking your statement.