I looked a bit more into the meaning of the port="" setting. The docs for the port parameter / PGPORT env var <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-PORT> say:
An empty string, or an empty item in a comma-separated list, specifies the default port number established when PostgreSQL was built Thus I understand that the return value of PQport wants to reflect this behaviour, therefore the value "" is legitimate and it's up to the client to figure out how the libpq was built (PQconndefaults may tell that). Please find attached a new patch that doesn't change the behaviour and just makes sure to not return NULL in case no info is available in 'conn->connhost'. Cheers -- Daniele
From ecd3063613a44309583e553db4485cfa55df34a9 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo <daniele.varra...@gmail.com> Date: Thu, 8 May 2025 18:34:56 +0200 Subject: [PATCH] Fix PQport to never return NULL unless the connection is NULL This is the documented behaviour, but, at the moment, if the port in the connection string is an empty string, the port is returned as NULL by this function. --- src/interfaces/libpq/fe-connect.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c index 2f87961a71e..454d2ea3fb7 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -7074,7 +7074,9 @@ PQport(const PGconn *conn) if (!conn) return NULL; - if (conn->connhost != NULL) + if (conn->connhost != NULL && + conn->connhost[conn->whichhost].port != NULL && + conn->connhost[conn->whichhost].port[0] != '\0') return conn->connhost[conn->whichhost].port; return ""; -- 2.34.1