Thread: PQhost() Returns NULL on Connection

PQhost() Returns NULL on Connection

From
Keary Suska
Date:
Why would PQhost() return NULL on a valid connection? I would assume that it
would return either a host name or Unix domain socket name/path regardless
of whether the "host" parameter was passed at connect time. Is it because it
won't return a value when a domain socket is used? If so, is there a way to
get the domain socket used?

Best,

Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"



Re: PQhost() Returns NULL on Connection

From
Tom Lane
Date:
Keary Suska <hierophant@pcisys.net> writes:
> Why would PQhost() return NULL on a valid connection?

One reason why would be if you'd selected the server by hostaddr rather
than host.  Returning a host name in this case would require a DNS
lookup, and since the entire point of the hostaddr feature is to avoid
DNS lookups, I don't think that's a particularly great idea.

These functions are really defined as "give me back the connection
options that were specified", anyway, not "peek inside to see what
values are actually in use".  So a NULL for an unspecified option
seems reasonable to me.
        regards, tom lane


Re: PQhost() Returns NULL on Connection

From
Volkan YAZICI
Date:
On Apr 26 02:08, Keary Suska wrote:
> If so, is there a way to get the domain socket used?

Below is some source code snippet I thought can help you.

src/interfaces/libpq/fe-connect.c
777 #ifdef HAVE_UNIX_SOCKETS
778         /* pghostaddr and pghost are NULL, so use Unix domain socket */
779         node = NULL;
780         hint.ai_family = AF_UNIX;
781         UNIXSOCK_PATH(portstr, portnum, conn->pgunixsocket);
782 #else

src/include/libpq/pqcomm.h
77 /* Configure the UNIX socket location for the well known port. */
78 
79 #define UNIXSOCK_PATH(path,port,defpath) \
80         snprintf(path, sizeof(path), "%s/.s.PGSQL.%d", \
81                 ((defpath) && *(defpath) != '\0') ? (defpath) : \
82                 DEFAULT_PGSOCKET_DIR, \
83                 (port))


Regards.


Re: PQhost() Returns NULL on Connection

From
Keary Suska
Date:
on 4/26/06 5:59 PM, tgl@sss.pgh.pa.us purportedly said:

>> Why would PQhost() return NULL on a valid connection?
> 
> One reason why would be if you'd selected the server by hostaddr rather
> than host.  Returning a host name in this case would require a DNS
> lookup, and since the entire point of the hostaddr feature is to avoid
> DNS lookups, I don't think that's a particularly great idea.
> 
> These functions are really defined as "give me back the connection
> options that were specified", anyway, not "peek inside to see what
> values are actually in use".  So a NULL for an unspecified option
> seems reasonable to me.

I wasn't sure, because the behavior is not consistent. For instance,
PQport() will return a value regardless of the connection type and
parameters passed to connect.

Also, there doesn't appear to be a way to retrieve hostaddr even if
specified, so I thought maybe the PQhost() was more generic. E.g. the lib
could return an address if specified and not have to do a wasteful DNS
lookup.

Considering that the connection status functions exist to retrieve parameter
values, it would at least be good to have access to all of them, and not
just a selection of them.

Best,

Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"