Re: Re: [BUGS] BUG #13611: test_postmaster_connection failed (Windows, listen_addresses = '0.0.0.0' or '::')

From: Tatsuo Ishii <ishii(at)postgresql(dot)org>
To: noah(at)leadboat(dot)com
Cc: tgl(at)sss(dot)pgh(dot)pa(dot)us, robertmhaas(at)gmail(dot)com, kondo(at)sraoss(dot)co(dot)jp, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Re: [BUGS] BUG #13611: test_postmaster_connection failed (Windows, listen_addresses = '0.0.0.0' or '::')
Date: 2015-10-26 00:54:03
Message-ID: 20151026.095403.804552198762060224.t-ishii@sraoss.co.jp
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-hackers

> As I stated upthread, PQping("host='0.0.0.0'") is _not portable_. It works on
> GNU/Linux, which I used for that demo. It fails on OpenBSD and Windows.
>
>> I'd be inclined to suggest fixing it like this:
>>
>> /* If postmaster is listening on "*", use localhost */
>> - if (strcmp(host_str, "*") == 0)
>> + if (strcmp(host_str, "*") == 0 ||
>> + strcmp(host_str, "0.0.0.0") == 0 ||
>> + strcmp(host_str, "::") == 0)
>> strcpy(host_str, "localhost");
>>
>> which covers the cases we document as supported.
>
> On RHEL 5 and some other "active adult" systems, "localhost" does not reach a
> listen_addresses='::' server. IPv6 is available, but "localhost" resolves to
> 127.0.0.1 only.
>
> The latest systems resolve "localhost" to both 127.0.0.1 and ::1, in which
> case PQping("host='localhost'") will attempt both addresses in an unspecified
> order. Given a postmaster with listen_addresses='0.0.0.0', contacting ::1
> first will fail (fine) or reach a different postmaster (not fine).
>
> Kondo's design is correct.

So more proper fix looks like this?
diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c
index dacdfef..23d5a3c 100644
--- a/src/bin/pg_ctl/pg_ctl.c
+++ b/src/bin/pg_ctl/pg_ctl.c
@@ -646,9 +646,11 @@ test_postmaster_connection(pgpid_t pm_pid, bool do_checkpoint)
return PQPING_NO_ATTEMPT;
}

- /* If postmaster is listening on "*", use localhost */
- if (strcmp(host_str, "*") == 0)
- strcpy(host_str, "localhost");
+ /* If postmaster is listening on "*", "0.0.0.0" or "::", use 127.0.0.1 */
+ if (strcmp(host_str, "*") == 0 ||
+ strcmp(host_str, "0.0.0.0") == 0 ||
+ strcmp(host_str, "::") == 0)
+ strcpy(host_str, "127.0.0.1");

/*
* We need to set connect_timeout otherwise on Windows

Best regards,
--
Tatsuo Ishii
SRA OSS, Inc. Japan
English: http://www.sraoss.co.jp/index_en.php
Japanese:http://www.sraoss.co.jp

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Michael Paquier 2015-10-26 04:52:38 Re: BUG #13657: Some kind of undetected deadlock between query and "startup process" on replica.
Previous Message zkronion 2015-10-25 21:54:51 BUG #13727: pg_dump with a connection string fails when performing parallel backup

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2015-10-26 00:59:00 About BoringSSL, an OpenSSL fork
Previous Message Zeus Kronion 2015-10-25 21:55:43 WIP: Fix parallel workers connection bug in pg_dump (Bug #13727)