Improve postmaster's logging of listen socket creation.

When one of the kernel calls in the socket()/bind()/listen() sequence
fails, include the specific address we're trying to bind to in the log
message.  This greatly eases debugging of network misconfigurations.

Also, after successfully setting up a listen socket, report its address
in the log, to ease verification that the expected addresses were bound.
There was some debate about whether to print this message at LOG level or
only DEBUG1, but the majority of votes were for the former.

Discussion: https://postgr.es/m/9564.1489091245@sss.pgh.pa.us
This commit is contained in:
Tom Lane 2017-03-10 16:32:18 -05:00
parent de75281637
commit f9dfa5c977
2 changed files with 43 additions and 16 deletions

View File

@ -464,9 +464,9 @@ su - postgres -c "/usr/local/pgsql/bin/pg_ctl start -l logfile -D /usr/local/pgs
<para>
<screen>
LOG: could not bind IPv4 socket: Address already in use
LOG: could not bind IPv4 address "127.0.0.1": Address already in use
HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
FATAL: could not create TCP/IP listen socket
FATAL: could not create any TCP/IP sockets
</screen>
This usually means just what it suggests: you tried to start
another server on the same port where one is already running.
@ -476,9 +476,9 @@ FATAL: could not create TCP/IP listen socket
on a reserved port number might draw something like:
<screen>
$ <userinput>postgres -p 666</userinput>
LOG: could not bind IPv4 socket: Permission denied
LOG: could not bind IPv4 address "127.0.0.1": Permission denied
HINT: Is another postmaster already running on port 666? If not, wait a few seconds and retry.
FATAL: could not create TCP/IP listen socket
FATAL: could not create any TCP/IP sockets
</screen>
</para>

View File

@ -319,6 +319,8 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
char portNumberStr[32];
const char *familyDesc;
char familyDescBuf[64];
const char *addrDesc;
char addrBuf[NI_MAXHOST];
char *service;
struct addrinfo *addrs = NULL,
*addr;
@ -407,7 +409,7 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
break;
}
/* set up family name for possible error messages */
/* set up address family name for log messages */
switch (addr->ai_family)
{
case AF_INET:
@ -431,13 +433,28 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
break;
}
/* set up text form of address for log messages */
#ifdef HAVE_UNIX_SOCKETS
if (addr->ai_family == AF_UNIX)
addrDesc = unixSocketPath;
else
#endif
{
pg_getnameinfo_all((const struct sockaddr_storage *) addr->ai_addr,
addr->ai_addrlen,
addrBuf, sizeof(addrBuf),
NULL, 0,
NI_NUMERICHOST);
addrDesc = addrBuf;
}
if ((fd = socket(addr->ai_family, SOCK_STREAM, 0)) == PGINVALID_SOCKET)
{
ereport(LOG,
(errcode_for_socket_access(),
/* translator: %s is IPv4, IPv6, or Unix */
errmsg("could not create %s socket: %m",
familyDesc)));
/* translator: first %s is IPv4, IPv6, or Unix */
errmsg("could not create %s socket for address \"%s\": %m",
familyDesc, addrDesc)));
continue;
}
@ -461,7 +478,9 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
{
ereport(LOG,
(errcode_for_socket_access(),
errmsg("setsockopt(SO_REUSEADDR) failed: %m")));
/* translator: first %s is IPv4, IPv6, or Unix */
errmsg("setsockopt(SO_REUSEADDR) failed for %s address \"%s\": %m",
familyDesc, addrDesc)));
closesocket(fd);
continue;
}
@ -476,7 +495,9 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
{
ereport(LOG,
(errcode_for_socket_access(),
errmsg("setsockopt(IPV6_V6ONLY) failed: %m")));
/* translator: first %s is IPv4, IPv6, or Unix */
errmsg("setsockopt(IPV6_V6ONLY) failed for %s address \"%s\": %m",
familyDesc, addrDesc)));
closesocket(fd);
continue;
}
@ -494,9 +515,9 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
{
ereport(LOG,
(errcode_for_socket_access(),
/* translator: %s is IPv4, IPv6, or Unix */
errmsg("could not bind %s socket: %m",
familyDesc),
/* translator: first %s is IPv4, IPv6, or Unix */
errmsg("could not bind %s address \"%s\": %m",
familyDesc, addrDesc),
(IS_AF_UNIX(addr->ai_family)) ?
errhint("Is another postmaster already running on port %d?"
" If not, remove socket file \"%s\" and retry.",
@ -533,12 +554,18 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
{
ereport(LOG,
(errcode_for_socket_access(),
/* translator: %s is IPv4, IPv6, or Unix */
errmsg("could not listen on %s socket: %m",
familyDesc)));
/* translator: first %s is IPv4, IPv6, or Unix */
errmsg("could not listen on %s address \"%s\": %m",
familyDesc, addrDesc)));
closesocket(fd);
continue;
}
ereport(LOG,
/* translator: first %s is IPv4, IPv6, or Unix */
(errmsg("listening on %s address \"%s\"",
familyDesc, addrDesc)));
ListenSocket[listen_index] = fd;
added++;
}