Hello, I have often seen inquiries about an log message from
PostgreSQL server.

> LOG:  could not create IPv6 socket: Address family not supported by protocol

This is emitted on ipv6-disabled environment which was available
for me by the following steps on CentOS 6.5,

 - Add 'NETWORKING_IPV6=no' into /etc/sysconfig/network

 - Create /etc/modprobe.d/disable-ipv6.conf with the following

   > options net-pf-10 off
   > install ipv6 /bin/true

 - Comment out the entry for ::1 in /etc/hosts

 - Reboot.

 - [Confirm that "ip a" and "ifconfig -a" don't show ipv6 addrs]

The cause is that the server collects available listen addresses
by getaddrinfo with hint.ai_flags not having AI_ADDRCONFIG set.

pqcomm.c:299 in function StreamServerPort
 >      hint.ai_family = family;
!>      hint.ai_flags = AI_PASSIVE;
 >      hint.ai_socktype = SOCK_STREAM;

The man page of getaddrinfo says that AI_ADDRCONFIG would help
and I saw it actually did.

Well, I excavated some discussions about this option from ancient
pgsql-hackers, a decade ago.

> This looks a little broken behaviour to me.  My guess is that
> it returns an AF_INET6 socket but doesn't support it in the
> kernel.  In that case with the AI_ADDRCONFIG option it
> shouldn't have returned that address.  The question is wether
> your getaddrinfo() supports that option, and wether it's
> working or not.

I suppose AI_ADDRCONFIG is a quite obscure option at the time,
but the time seems to have come when we can use this option.

man getaddrinfo
>       getaddrinfo() supports the address%scope-id notation for
>       specifying the IPv6 scope-ID.
>       AI_ADDRCONFIG, AI_ALL, and AI_V4MAPPED are available
>       since glibc 2.3.3.  AI_NUMERICSERV is available since
>       glibc 2.3.4.

RHEL/CentOS 4.9 and after seems satisfies the condition. I don't
know about other platforms but it is enough to define it as 0 if
not defined. In addition, it would not be a problem even if the
option doesn't work as expected because the error handling code
added at the time the above conversation took place would act as
the last resort. There would be no way it can work destructively.

Attached patch does this.

Any suggestions ?


Kyotaro Horiguchi
NTT Open Source Software Center
diff --git a/src/backend/libpq/pqcomm.c b/src/backend/libpq/pqcomm.c
index 2b24793..121f70b 100644
--- a/src/backend/libpq/pqcomm.c
+++ b/src/backend/libpq/pqcomm.c
@@ -297,7 +297,7 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
 	/* Initialize hint structure */
 	MemSet(&hint, 0, sizeof(hint));
 	hint.ai_family = family;
-	hint.ai_flags = AI_PASSIVE;
+	hint.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
 	hint.ai_socktype = SOCK_STREAM;
diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c
index 305d126..1dd7f2a 100644
--- a/src/backend/postmaster/pgstat.c
+++ b/src/backend/postmaster/pgstat.c
@@ -344,7 +344,7 @@ pgstat_init(void)
 	 * Create the UDP socket for sending and receiving statistic messages
-	hints.ai_flags = AI_PASSIVE;
+	hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
 	hints.ai_family = PF_UNSPEC;
 	hints.ai_socktype = SOCK_DGRAM;
 	hints.ai_protocol = 0;
diff --git a/src/include/getaddrinfo.h b/src/include/getaddrinfo.h
index 6192d1f..685d922 100644
--- a/src/include/getaddrinfo.h
+++ b/src/include/getaddrinfo.h
@@ -64,6 +64,11 @@
 #define AI_PASSIVE		0x0001
+/* Works as NOP if AI_ADDRCONFIG is not defined */
+#define AI_ADDRCONFIG	0x0000
  * some platforms don't support AI_NUMERICHOST; define as zero if using
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:

Reply via email to