Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kQMpz-00041I-QP for pgsql-committers@arkaria.postgresql.org; Thu, 08 Oct 2020 03:43:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kQMpy-00062Z-El for pgsql-committers@arkaria.postgresql.org; Thu, 08 Oct 2020 03:43:26 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kQMpy-00062S-93 for pgsql-committers@lists.postgresql.org; Thu, 08 Oct 2020 03:43:26 +0000 Received: from oss.nttdata.com ([49.212.34.109]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kQMpu-0001SH-Ds for pgsql-committers@lists.postgresql.org; Thu, 08 Oct 2020 03:43:25 +0000 Received: from hnk.local (p1295030-ipngn4902funabasi.chiba.ocn.ne.jp [153.166.85.30]) by oss.nttdata.com (Postfix) with ESMTPSA id 8B20E6017B; Thu, 8 Oct 2020 12:43:17 +0900 (JST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.102.3 at oss.nttdata.com Subject: Re: pgsql: postgres_fdw: reestablish new connection if cached one is detect From: Fujii Masao To: Michael Paquier , Fujii Masao Cc: pgsql-committers@lists.postgresql.org References: <20201007021347.GD2256@paquier.xyz> <14cd32c8-9532-47b8-77d4-b81eb3b192ea@oss.nttdata.com> <78d509fe-f87a-7046-672a-f86cb6f6957d@oss.nttdata.com> Message-ID: Date: Thu, 8 Oct 2020 12:43:17 +0900 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:68.0) Gecko/20100101 Thunderbird/68.12.1 MIME-Version: 1.0 In-Reply-To: <78d509fe-f87a-7046-672a-f86cb6f6957d@oss.nttdata.com> Content-Type: multipart/mixed; boundary="------------87ACD2F46EF2C7AF0D2CE04D" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------87ACD2F46EF2C7AF0D2CE04D Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit On 2020/10/08 0:48, Fujii Masao wrote: > > > On 2020/10/07 22:25, Fujii Masao wrote: >> >> >> On 2020/10/07 12:54, Fujii Masao wrote: >>> >>> >>> On 2020/10/07 11:13, Michael Paquier wrote: >>>> Hi Fujii-san, >>>> >>>> On Tue, Oct 06, 2020 at 01:52:55AM +0000, Fujii Masao wrote: >>>>> postgres_fdw: reestablish new connection if cached one is detected as broken. >>>>> >>>>> In postgres_fdw, once remote connections are established, they are cached >>>>> and re-used for subsequent queries and transactions. There can be some >>>>> cases where those cached connections are unavaiable, for example, >>>>> by the restart of remote server. In these cases, previously an error was >>>>> reported and the query accessing to remote server failed if new remote >>>>> transaction failed to start because the cached connection was broken. >>>>> >>>>> This commit improves postgres_fdw so that new connection is remade >>>>> if broken connection is detected when starting new remote transaction. >>>>> This is useful to avoid unnecessary failure of queries when connection is >>>>> broken but can be reestablished. >>>> >>>> lorikeet is telling that the test introduced by this commit is >>>> unstable: >>>> https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=lorikeet&dt=2020-10-06%2008%3A28%3A36 >>> >>> Thanks for letting me know this! >>> >>>> >>>> Some details: >>>>   BEGIN; >>>>   SELECT 1 FROM ft1 LIMIT 1; >>>> - ?column? >>>> ----------- >>>> -        1 >>>> -(1 row) >>>> - >>>> +ERROR:  could not receive data from server: Software caused connection abort >>>> +CONTEXT:  remote SQL command: START TRANSACTION ISOLATION LEVEL REPEATABLE READ >>> >>> This error means that new connection was successfully reestablished >>> after the cached connection was terminated, and then the above connection >>> error occurred when issuing "START TRANSACTION" command on that >>> new connection. There seems no suspicious relevant log messages in the >>> logfile. So I'm not sure why this error happened, yet. >>> >>> Per the previous discusson at [1], lorikeet sometimes seems to cause >>> connection-relation failure in the regression test. So the cause of error >>> that we faced today also may be lorikeet itself. >> >> Since it's not good to keep the buildfarm member red, I will revert >> the commit unless I come up with something even after further >> investigation. >> >> My current just guess is that PQstatus(conn) doesn't indicate >> CONNECTION_BAD when the above error occurs, and which >> prevents new connection from being reestablished because of >> the following check. >> >> +        if (PQstatus(entry->conn) != CONNECTION_BAD || >> +            entry->xact_depth > 0 || >> +            retry_conn) >> +            PG_RE_THROW(); > > The error message in discussion is reported when recv() fails and > errno=ECONNABORTED. As far as I read the code, pqReadData() marks > the connection as CONNECTION_BAD when errno=ECONNRESET, > but not when errno=ECONNABORTED. So since PQstatus(entry->conn) > doesn't indicate CONNECTION_BAD in ECONNABORTED case, > the above check is passed through, an error is re-thrown and > new connection is not reestablished. > > Therefore, the easy fix is to make libpq mark the connection as > CONNECTION_BAD even in ECONNABORTED, like we do in ECONNRESET. Patch attached. This patch also changes errcode_for_socket_access() so that it uses ERRCODE_CONNECTION_FAILURE rather than ERRCODE_INTERNAL_ERROR as sqlerrorcode in ECONNABORTED case like ECONNRESET. Is this sane? Regards, -- Fujii Masao Advanced Computing Technology Center Research and Development Headquarters NTT DATA CORPORATION --------------87ACD2F46EF2C7AF0D2CE04D Content-Type: text/plain; charset=UTF-8; x-mac-type="0"; x-mac-creator="0"; name="econnaborted_as_conn_error.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="econnaborted_as_conn_error.patch" diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c index d0b368530e..6ca814224f 100644 --- a/src/backend/utils/error/elog.c +++ b/src/backend/utils/error/elog.c @@ -714,6 +714,9 @@ errcode_for_socket_access(void) case EPIPE: #ifdef ECONNRESET case ECONNRESET: +#endif +#ifdef ECONNABORTED + case ECONNABORTED: #endif edata->sqlerrcode = ERRCODE_CONNECTION_FAILURE; break; diff --git a/src/interfaces/libpq/fe-misc.c b/src/interfaces/libpq/fe-misc.c index ff840b7730..657310f22c 100644 --- a/src/interfaces/libpq/fe-misc.c +++ b/src/interfaces/libpq/fe-misc.c @@ -683,6 +683,11 @@ retry3: #ifdef ECONNRESET if (SOCK_ERRNO == ECONNRESET) goto definitelyFailed; +#endif + /* We might get ECONNABORTED here if using TCP and backend died */ +#ifdef ECONNABORTED + if (SOCK_ERRNO == ECONNABORTED) + goto definitelyFailed; #endif /* pqsecure_read set the error message for us */ return -1; @@ -773,6 +778,11 @@ retry4: #ifdef ECONNRESET if (SOCK_ERRNO == ECONNRESET) goto definitelyFailed; +#endif + /* We might get ECONNABORTED here if using TCP and backend died */ +#ifdef ECONNABORTED + if (SOCK_ERRNO == ECONNABORTED) + goto definitelyFailed; #endif /* pqsecure_read set the error message for us */ return -1; diff --git a/src/interfaces/libpq/win32.h b/src/interfaces/libpq/win32.h index c42d7abfe3..40cff28f3a 100644 --- a/src/interfaces/libpq/win32.h +++ b/src/interfaces/libpq/win32.h @@ -22,6 +22,9 @@ #ifndef ECONNRESET #define ECONNRESET WSAECONNRESET #endif +#ifndef ECONNABORTED +#define ECONNABORTED WSAECONNABORTED +#endif #ifndef EINPROGRESS #define EINPROGRESS WSAEINPROGRESS #endif --------------87ACD2F46EF2C7AF0D2CE04D--