Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f9dfa5c

Browse files
committed
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
1 parent de75281 commit f9dfa5c

File tree

2 files changed

+43
-16
lines changed

2 files changed

+43
-16
lines changed

doc/src/sgml/runtime.sgml

+4-4
Original file line numberDiff line numberDiff line change
@@ -464,9 +464,9 @@ su - postgres -c "/usr/local/pgsql/bin/pg_ctl start -l logfile -D /usr/local/pgs
464464

465465
<para>
466466
<screen>
467-
LOG: could not bind IPv4 socket: Address already in use
467+
LOG: could not bind IPv4 address "127.0.0.1": Address already in use
468468
HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
469-
FATAL: could not create TCP/IP listen socket
469+
FATAL: could not create any TCP/IP sockets
470470
</screen>
471471
This usually means just what it suggests: you tried to start
472472
another server on the same port where one is already running.
@@ -476,9 +476,9 @@ FATAL: could not create TCP/IP listen socket
476476
on a reserved port number might draw something like:
477477
<screen>
478478
$ <userinput>postgres -p 666</userinput>
479-
LOG: could not bind IPv4 socket: Permission denied
479+
LOG: could not bind IPv4 address "127.0.0.1": Permission denied
480480
HINT: Is another postmaster already running on port 666? If not, wait a few seconds and retry.
481-
FATAL: could not create TCP/IP listen socket
481+
FATAL: could not create any TCP/IP sockets
482482
</screen>
483483
</para>
484484

src/backend/libpq/pqcomm.c

+39-12
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,8 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
319319
char portNumberStr[32];
320320
const char *familyDesc;
321321
char familyDescBuf[64];
322+
const char *addrDesc;
323+
char addrBuf[NI_MAXHOST];
322324
char *service;
323325
struct addrinfo *addrs = NULL,
324326
*addr;
@@ -407,7 +409,7 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
407409
break;
408410
}
409411

410-
/* set up family name for possible error messages */
412+
/* set up address family name for log messages */
411413
switch (addr->ai_family)
412414
{
413415
case AF_INET:
@@ -431,13 +433,28 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
431433
break;
432434
}
433435

436+
/* set up text form of address for log messages */
437+
#ifdef HAVE_UNIX_SOCKETS
438+
if (addr->ai_family == AF_UNIX)
439+
addrDesc = unixSocketPath;
440+
else
441+
#endif
442+
{
443+
pg_getnameinfo_all((const struct sockaddr_storage *) addr->ai_addr,
444+
addr->ai_addrlen,
445+
addrBuf, sizeof(addrBuf),
446+
NULL, 0,
447+
NI_NUMERICHOST);
448+
addrDesc = addrBuf;
449+
}
450+
434451
if ((fd = socket(addr->ai_family, SOCK_STREAM, 0)) == PGINVALID_SOCKET)
435452
{
436453
ereport(LOG,
437454
(errcode_for_socket_access(),
438-
/* translator: %s is IPv4, IPv6, or Unix */
439-
errmsg("could not create %s socket: %m",
440-
familyDesc)));
455+
/* translator: first %s is IPv4, IPv6, or Unix */
456+
errmsg("could not create %s socket for address \"%s\": %m",
457+
familyDesc, addrDesc)));
441458
continue;
442459
}
443460

@@ -461,7 +478,9 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
461478
{
462479
ereport(LOG,
463480
(errcode_for_socket_access(),
464-
errmsg("setsockopt(SO_REUSEADDR) failed: %m")));
481+
/* translator: first %s is IPv4, IPv6, or Unix */
482+
errmsg("setsockopt(SO_REUSEADDR) failed for %s address \"%s\": %m",
483+
familyDesc, addrDesc)));
465484
closesocket(fd);
466485
continue;
467486
}
@@ -476,7 +495,9 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
476495
{
477496
ereport(LOG,
478497
(errcode_for_socket_access(),
479-
errmsg("setsockopt(IPV6_V6ONLY) failed: %m")));
498+
/* translator: first %s is IPv4, IPv6, or Unix */
499+
errmsg("setsockopt(IPV6_V6ONLY) failed for %s address \"%s\": %m",
500+
familyDesc, addrDesc)));
480501
closesocket(fd);
481502
continue;
482503
}
@@ -494,9 +515,9 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
494515
{
495516
ereport(LOG,
496517
(errcode_for_socket_access(),
497-
/* translator: %s is IPv4, IPv6, or Unix */
498-
errmsg("could not bind %s socket: %m",
499-
familyDesc),
518+
/* translator: first %s is IPv4, IPv6, or Unix */
519+
errmsg("could not bind %s address \"%s\": %m",
520+
familyDesc, addrDesc),
500521
(IS_AF_UNIX(addr->ai_family)) ?
501522
errhint("Is another postmaster already running on port %d?"
502523
" If not, remove socket file \"%s\" and retry.",
@@ -533,12 +554,18 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
533554
{
534555
ereport(LOG,
535556
(errcode_for_socket_access(),
536-
/* translator: %s is IPv4, IPv6, or Unix */
537-
errmsg("could not listen on %s socket: %m",
538-
familyDesc)));
557+
/* translator: first %s is IPv4, IPv6, or Unix */
558+
errmsg("could not listen on %s address \"%s\": %m",
559+
familyDesc, addrDesc)));
539560
closesocket(fd);
540561
continue;
541562
}
563+
564+
ereport(LOG,
565+
/* translator: first %s is IPv4, IPv6, or Unix */
566+
(errmsg("listening on %s address \"%s\"",
567+
familyDesc, addrDesc)));
568+
542569
ListenSocket[listen_index] = fd;
543570
added++;
544571
}

0 commit comments

Comments
 (0)