Skip to content

Commit a6a6317

Browse files
authored
Merge pull request #1052 from psycopg/connection-log
Add connection log info
2 parents cd7145b + 62cd04d commit a6a6317

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

psycopg/psycopg/generators.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
READY_W = Ready.W
5757
READY_RW = Ready.RW
5858

59-
logger = logging.getLogger(__name__)
59+
logger = logging.getLogger("psycopg")
6060

6161

6262
def _connect(conninfo: str, *, timeout: float = 0.0) -> PQGenConn[PGconn]:
@@ -65,7 +65,15 @@ def _connect(conninfo: str, *, timeout: float = 0.0) -> PQGenConn[PGconn]:
6565
"""
6666
deadline = monotonic() + timeout if timeout else 0.0
6767

68+
# To debug slowdown during connection:
69+
#
70+
# $ PSYCOPG_IMPL=python python
71+
# >>> import logging
72+
# >>> logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(message)s')
73+
# >>> logging.getLogger("psycopg").setLevel(logging.DEBUG)
74+
6875
conn = pq.PGconn.connect_start(conninfo.encode())
76+
logger.debug("connection started: %s", conn)
6977
while True:
7078
if conn.status == BAD:
7179
encoding = conninfo_encoding(conninfo)
@@ -74,6 +82,7 @@ def _connect(conninfo: str, *, timeout: float = 0.0) -> PQGenConn[PGconn]:
7482
)
7583

7684
status = conn.connect_poll()
85+
logger.debug("connection polled: %s", conn)
7786

7887
if status == POLL_READING or status == POLL_WRITING:
7988
wait = WAIT_R if status == POLL_READING else WAIT_W

psycopg/psycopg/pq/misc.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,12 @@ def connection_summary(pgconn: abc.PGconn) -> str:
162162
parts.append(("database", pgconn.db.decode()))
163163

164164
else:
165-
status = ConnStatus(pgconn.status).name
165+
try:
166+
status = ConnStatus(pgconn.status).name
167+
except ValueError:
168+
# It might happen if a new status on connection appears
169+
# before upgrading the ConnStatus enum.
170+
status = f"status={pgconn.status} (unkndown)"
166171

167172
if sparts := " ".join(("%s=%s" % part for part in parts)):
168173
sparts = f" ({sparts})"

psycopg_c/psycopg_c/_psycopg/generators.pyx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def connect(conninfo: str, *, timeout: float = 0.0) -> PQGenConn[abc.PGconn]:
4242
if timeout:
4343
deadline = monotonic() + timeout
4444

45+
logger.debug("connection started: %s", conn)
4546
while True:
4647
if conn_status == libpq.CONNECTION_BAD:
4748
encoding = conninfo_encoding(conninfo)
@@ -52,6 +53,7 @@ def connect(conninfo: str, *, timeout: float = 0.0) -> PQGenConn[abc.PGconn]:
5253

5354
with nogil:
5455
poll_status = libpq.PQconnectPoll(pgconn_ptr)
56+
logger.debug("connection polled: %s", conn)
5557

5658
if poll_status == libpq.PGRES_POLLING_READING \
5759
or poll_status == libpq.PGRES_POLLING_WRITING:

0 commit comments

Comments
 (0)