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 1l5OVO-0002E4-Om for pgsql-hackers@arkaria.postgresql.org; Fri, 29 Jan 2021 07:47:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1l5OVM-0008Vl-Or for pgsql-hackers@arkaria.postgresql.org; Fri, 29 Jan 2021 07:47:44 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l5OVM-0008Uq-Hh for pgsql-hackers@lists.postgresql.org; Fri, 29 Jan 2021 07:47:44 +0000 Received: from oss.nttdata.com ([49.212.34.109]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l5OVJ-0002v4-2T for pgsql-hackers@lists.postgresql.org; Fri, 29 Jan 2021 07:47:42 +0000 Received: from hnk.local (p2112118-ipbf2507funabasi.chiba.ocn.ne.jp [123.216.63.118]) by oss.nttdata.com (Postfix) with ESMTPSA id 5BF4D62842; Fri, 29 Jan 2021 16:47:37 +0900 (JST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.102.3 at oss.nttdata.com Subject: Re: [PATCH] postgres_fdw connection caching - cause remote sessions linger till the local session exit To: Bharath Rupireddy Cc: Tom Lane , PostgreSQL Hackers References: <3c20e3e6-1127-17f4-7725-a860b394076c@oss.nttdata.com> <73fc5ae4-3c54-1262-4533-f8c547de2e60@oss.nttdata.com> <2688508.1611865371@sss.pgh.pa.us> <2724627.1611886184@sss.pgh.pa.us> <6e9c9d45-c612-639a-f741-7798013c0ae4@oss.nttdata.com> <7fa16ff3-5b37-0617-7a27-22e839695ef5@oss.nttdata.com> From: Fujii Masao Message-ID: <8a6d3620-11de-30c8-7647-375cf92f442a@oss.nttdata.com> Date: Fri, 29 Jan 2021 16:47:36 +0900 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:78.0) Gecko/20100101 Thunderbird/78.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2021/01/29 16:12, Bharath Rupireddy wrote: > On Fri, Jan 29, 2021 at 12:36 PM Fujii Masao > wrote: >> On 2021/01/29 15:44, Bharath Rupireddy wrote: >>> On Fri, Jan 29, 2021 at 11:54 AM Fujii Masao >>> wrote: >>>>>> IIRC, when we were finding a way to close the invalidated connections >>>>>> so that they don't leaked, we had two options: >>>>>> >>>>>> 1) let those connections (whether currently being used in the xact or >>>>>> not) get marked invalidated in pgfdw_inval_callback and closed in >>>>>> pgfdw_xact_callback at the main txn end as shown below >>>>>> >>>>>> if (PQstatus(entry->conn) != CONNECTION_OK || >>>>>> PQtransactionStatus(entry->conn) != PQTRANS_IDLE || >>>>>> entry->changing_xact_state || >>>>>> entry->invalidated). ----> by adding this >>>>>> { >>>>>> elog(DEBUG3, "discarding connection %p", entry->conn); >>>>>> disconnect_pg_server(entry); >>>>>> } >>>>>> >>>>>> 2) close the unused connections right away in pgfdw_inval_callback >>>>>> instead of marking them invalidated. Mark used connections as >>>>>> invalidated in pgfdw_inval_callback and close them in >>>>>> pgfdw_xact_callback at the main txn end. >>>>>> >>>>>> We went with option (2) because we thought this would ease some burden >>>>>> on pgfdw_xact_callback closing a lot of invalid connections at once. >>>>> >>>>> Also, see the original patch for the connection leak issue just does >>>>> option (1), see [1]. But in [2] and [3], we chose option (2). >>>>> >>>>> I feel, we can go for option (1), with the patch attached in [1] i.e. >>>>> having have_invalid_connections whenever any connection gets invalided >>>>> so that we don't quickly exit in pgfdw_xact_callback and the >>>>> invalidated connections get closed properly. Thoughts? >>>> >>>> Before going for (1) or something, I'd like to understand what the actual >>>> issue of (2), i.e., the current code is. Otherwise other approaches might >>>> have the same issue. >>> >>> The problem with option (2) is that because of CLOBBER_CACHE_ALWAYS, >>> pgfdw_inval_callback is getting called many times and the connections >>> that are not used i..e xact_depth == 0, are getting disconnected >>> there, so we are not seeing the consistent results for >>> postgres_fdw_get_connectionstest cases. If the connections are being >>> used within the xact, then the valid option for those connections are >>> being shown as false again making postgres_fdw_get_connections output >>> inconsistent. This is what happened on the build farm member with >>> CLOBBER_CACHE_ALWAYS build. >> >> But if the issue is only the inconsistency of test results, >> we can go with the option (2)? Even with (2), we can make the test >> stable by removing "valid" column and executing >> postgres_fdw_get_connections() within the transaction? > > Hmmm, and we should have the tests at the start of the file > postgres_fdw.sql before even we make any foreign server connections. We don't need to move the test if we always call postgres_fdw_disconnect_all() just before starting new transaction and calling postgres_fdw_get_connections() as follows? SELECT 1 FROM postgres_fdw_disconnect_all(); BEGIN; ... SELECT * FROM postgres_fdw_get_connections(); ... > > If okay, I can prepare the patch and run with clobber cache build locally. Many thanks! > >>> >>> So if we go with option (1), get rid of valid state from >>> postgres_fdw_get_connectionstest and having the test cases inside an >>> explicit xact block at the beginning of the postgres_fdw.sql test >>> file, we don't see CLOBBER_CACHE_ALWAYS inconsistencies. I'm not sure >>> if this is the correct way. >>> >>>> Regarding (1), as far as I understand correctly, even when the transaction >>>> doesn't use foreign tables at all, it needs to scan the connection cache >>>> entries if necessary. I was thinking to avoid this. I guess that this doesn't >>>> work with at least the postgres_fdw 2PC patch that Sawada-san is proposing >>>> because with the patch the commit/rollback callback is performed only >>>> for the connections used in the transaction. >>> >>> You mean to say, pgfdw_xact_callback will not get called when the xact >>> uses no foreign server connection or is it that pgfdw_xact_callback >>> gets called but exits quickly from it? I'm not sure what the 2PC patch >>> does. >> >> Maybe it's chance to review the patch! ;P >> >> BTW his patch tries to add new callback interfaces for commit/rollback of >> foreign transactions, and make postgres_fdw use them instead of >> XactCallback. And those new interfaces are executed only when >> the transaction has started the foreign transactions. > > IMHO, it's better to keep it as a separate discussion. Yes, of course! Regards, -- Fujii Masao Advanced Computing Technology Center Research and Development Headquarters NTT DATA CORPORATION