]> granicus.if.org Git - postgresql/commitdiff
Improve postmaster's logging of listen socket creation.
authorTom Lane <tgl@sss.pgh.pa.us>
Fri, 10 Mar 2017 21:32:18 +0000 (16:32 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 10 Mar 2017 21:32:25 +0000 (16:32 -0500)
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

doc/src/sgml/runtime.sgml
src/backend/libpq/pqcomm.c

index 5e3d783c6a6a6fca3fcc2b702a17d577897cf0e8..01153f9a3770a04309e50e9d85297bc1e99b13c4 100644 (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>
 
index 7939b1f544f857ea91306a4ed72e18765d2ec4cb..aa934569da33d1eccd36986dc4d55d7d8551dcb3 100644 (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++;
        }