Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1courI-0000Fp-9z for pgsql-hackers@arkaria.postgresql.org; Fri, 17 Mar 2017 16:36:08 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1courH-0007Ld-T1 for pgsql-hackers@arkaria.postgresql.org; Fri, 17 Mar 2017 16:36:07 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1courG-0007KI-C8 for pgsql-hackers@postgresql.org; Fri, 17 Mar 2017 16:36:06 +0000 Received: from mail-wr0-x234.google.com ([2a00:1450:400c:c0c::234]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1courD-0000Eq-Aj for pgsql-hackers@postgresql.org; Fri, 17 Mar 2017 16:36:05 +0000 Received: by mail-wr0-x234.google.com with SMTP id u48so55490891wrc.0 for ; Fri, 17 Mar 2017 09:36:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:cc:from:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding; bh=gTKJNNsU8A4jTlKO6XAZtPFbky5nb7XTqtM3zpv74y0=; b=cQ8/ROMmqHtmUaqHuPbal/U0Bt3/OWVAtBlh23v4ANyBxPRIyG8BO10Yi9Bq3EeXhr LWlyUfGe8JhHNvAbRB4p7b8BM/ZweasxOgfhmqQeA4sDQT+fuhIkrGEFuhSBMRfB0TpK oApyLP0ABECWGThfZc2WD53oA58u6tiOYgGvDFZD3jb2It0ouh+NV8tAi2mzH/RqO50G 5kGRC1prRKsMDaErjZ1oOym3y2CW5/ib+/5Bno7JkPl8jYt5wL3Gv8AFr5C6vBPxwLEv UDnrjYwcUkKZNMfROquQzwzqWEmav6OmYDGXsPpqhZk4v8jS/UsELpL2cczVW2rTgLRe 5VKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:cc:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=gTKJNNsU8A4jTlKO6XAZtPFbky5nb7XTqtM3zpv74y0=; b=dby+BHh2lZlp3EqPJyHv9S/4NtWldqBacemwgzFVe1nCYlKR/KkpBTgKFEk94xvVNH jN8JxJQ2HvFu7iGfdlhuMC0OwY0LjRLXghlwPV+IlYNA/ThVpYhJt+7wylMtjOE9LevW RpResYSmsswQbEnvuUg1N6QjrY21NuZh+r6aBwrX10fc5D5/3ldDXvdgJSiIxlGac+Kz C2LPf1fR/WdQZhFbpMnDK6GryyGWR0nU+LXKxQztaw2WJ98Xdcpw0SKrXVroktMq6Hj7 Tz8rIiqBNMnWIvYr4F/nT+Exe2uRQ33e+4UCRkssnKmiZdtEmdqwZf3PgE4yIzaYFVED jJEw== X-Gm-Message-State: AFeK/H0tiLnYFLWxEp/ilsn14MG2pA8uFz9c67kpBKq0VlG8SxBTWrC8xccfV+AFiBvrkxjRsx2zMaRgt5z/nxuXcgw270jAnp4G8DYjJe6FGxgbPJqO1oSsT/xb7Cy7z+FtblevGE/+xFrIMzbTC+IG6hjfQcolnjeumchSSKdHMNP3LWY90DB5t6KnPGSe5U9/ZDreo332 X-Received: by 10.223.155.129 with SMTP id d1mr13931578wrc.67.1489768561649; Fri, 17 Mar 2017 09:36:01 -0700 (PDT) Received: from [192.168.0.105] (150.123.broadband11.iol.cz. [90.178.123.150]) by smtp.gmail.com with ESMTPSA id v186sm3225927wmv.2.2017.03.17.09.36.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 17 Mar 2017 09:36:00 -0700 (PDT) Subject: Re: [COMMITTERS] pgsql: Use asynchronous connect API in libpqwalreceiver To: Tom Lane References: <19403.1488586564@sss.pgh.pa.us> <34d98fbf-9195-04d2-b85a-634d6952ffdf@2ndquadrant.com> <19815.1488600693@sss.pgh.pa.us> <4144256b-dc4f-892c-a720-71f6e7b9ac87@2ndquadrant.com> <3ffe98f7-d2ed-65c6-51f3-4d97ffd6e312@2ndquadrant.com> <34591606-976a-bbee-8eda-778c1e1fc52f@2ndQuadrant.com> <10458.1489763266@sss.pgh.pa.us> <5b6a6d6d-fb45-0afb-2e95-5600063c3dbd@2ndquadrant.com> <13398.1489768097@sss.pgh.pa.us> Cc: Andrew Dunstan , Peter Eisentraut , pgsql-hackers@postgresql.org From: Petr Jelinek Message-ID: Date: Fri, 17 Mar 2017 17:36:00 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.7.0 MIME-Version: 1.0 In-Reply-To: <13398.1489768097@sss.pgh.pa.us> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 8bit X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org On 17/03/17 17:28, Tom Lane wrote: > Petr Jelinek writes: >> Now the documentation for WSAEventSelect says "The FD_WRITE network >> event is handled slightly differently. An FD_WRITE network event is >> recorded when a socket is first connected with a call to the connect, >> ConnectEx, WSAConnect, WSAConnectByList, or WSAConnectByName function or >> when a socket is accepted with accept, AcceptEx, or WSAAccept function >> and then after a send fails with WSAEWOULDBLOCK and buffer space becomes >> available. Therefore, an application can assume that sends are possible >> starting from the first FD_WRITE network event setting and lasting until >> a send returns WSAEWOULDBLOCK. After such a failure the application will >> find out that sends are again possible when an FD_WRITE network event is >> recorded and the associated event object is set." > >> But while PQconnectPoll does connect() before setting connection status >> to CONNECTION_STARTED and returns PGRES_POLLING_WRITING so the FD_WRITE >> eventually happens, it does not do any writes in the code block that >> switches to CONNECTION_MADE and PGRES_POLLING_WRITING. That means >> FD_WRITE event is never recorded as per quoted documentation. > > Ah-hah! Great detective work. > >> Then what remains is why it works in libpq. If you look at >> pgwin32_select which is eventually called by libpq code, it actually >> handles the situation by trying empty WSASend on any socket that is >> supposed to wait for FD_WRITE and only calling the >> WaitForMultipleObjectsEx when WSASend failed with WSAEWOULDBLOCK, when >> the WSASend succeeds it immediately returns ok. > >> So I did something similar in attached for WaitEventSetWaitBlock() and >> it indeed solves the issue my windows test machine. Now the >> coding/placement probably isn't the best (and there are no comments), >> but maybe somebody will find proper place for this now that we know the >> cause. > > Yeah, I'm afraid we had better do something more or less like this. > It's interesting to speculate about whether WaitEventSet could keep > additional state that would avoid the need for a dummy send() every > time, but that seems like material for new development not a bug fix. > > In any case, a quick review of the code suggests that there are no > performance-critical places where we wait for WL_SOCKET_WRITEABLE > unless we've already detected that we have to wait for the network; > so the dummy send() isn't going to do anything except consume a > few more CPU cycles before we get to the point of blocking. It may > not be worth worrying about. > Thanks, now that I look at it again I think we might need to do cycle with the occurred_events and returned_events and not return on first find since theoretically there can be multiple sockets if this gets called directly and not via WaitLatchOrSocket(). I don't have mental power to finish it up right now though, sorry for that. > I'll add some comments and push this. Does everyone agree that > this had better get back-patched, too? > +1 -- Petr Jelinek http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers