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 1kufTa-00027K-CL for pgsql-hackers@arkaria.postgresql.org; Wed, 30 Dec 2020 17:41:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kufTZ-0008Sv-Bt for pgsql-hackers@arkaria.postgresql.org; Wed, 30 Dec 2020 17:41:33 +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 1kufTZ-0008So-3F for pgsql-hackers@lists.postgresql.org; Wed, 30 Dec 2020 17:41:33 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by makus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kufTW-0008Iq-0O for pgsql-hackers@lists.postgresql.org; Wed, 30 Dec 2020 17:41:32 +0000 Received: from mail.postgrespro.ru (cyclops.postgrespro.ru [93.174.131.138]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id 39A8121C37F7; Wed, 30 Dec 2020 20:41:27 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1609350087; bh=TU7/eiZkNzgbcsJfdTcuxK6RDRF8Y2zGJBUem7g5rPg=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=AbnmCEVXWu6+wG1jKBvPqZuDz8yvK45OQYXin1fQHenmaz/H1W8A5+EUSraSkAUyn wcVTe6XtCKO3XaAp8dHNzqJDUfjcJ6TBJ319RDAjSwaJs/ykhdOA6duI43WI2KY1FK O77tE/YhA+SH14F07R80KqnW29iQBbyXtkfuuFEg= MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit Date: Wed, 30 Dec 2020 20:41:27 +0300 From: Alexey Kondratov To: Bharath Rupireddy Cc: "Hou, Zhijie" , PostgreSQL Hackers , Fujii Masao Subject: Re: [PATCH] postgres_fdw connection caching - cause remote sessions linger till the local session exit In-Reply-To: References: <3595a221-d845-c97c-96fe-5d4f739edaad@oss.nttdata.com> <2a0d7305-1cf6-db59-7fc6-241c55c8fc8b@oss.nttdata.com> <6da38393-6ae5-4d87-2690-11c932123403@oss.nttdata.com> <0b13f28d-de46-fb06-e946-4c0c00cea591@oss.nttdata.com> <617e9887b74c4332a5d1e5fb2c56ddfd@G08CNEXMBPEKD05.g08.fujitsu.local> <919942767cf9a3d4e413c63887aec679@postgrespro.ru> User-Agent: Roundcube Webmail/1.4.0 Message-ID: <9b90712303dbecddd8691b95cee93b3a@postgrespro.ru> X-Sender: a.kondratov@postgrespro.ru List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2020-12-30 17:59, Bharath Rupireddy wrote: > On Wed, Dec 30, 2020 at 5:20 PM Alexey Kondratov > wrote: >> >> On 2020-12-30 09:10, Bharath Rupireddy wrote: >> I still have some doubts that it is worth of allowing to call >> postgres_fdw_disconnect() in the explicit transaction block, since it >> adds a lot of things to care and check for. But otherwise current >> logic >> looks solid. >> >> + errdetail("Such connections get >> closed either in the next use or >> at the end of the current transaction.") >> + : errdetail("Such connection gets >> closed either in the next use or >> at the end of the current transaction."))); >> >> Does it really have a chance to get closed on the next use? If foreign >> server is dropped then user mapping should be dropped as well (either >> with CASCADE or manually), but we do need user mapping for a local >> cache >> lookup. That way, if I understand all the discussion up-thread >> correctly, we can only close such connections at the end of xact, do >> we? > > The next use of such a connection in the following query whose foreign > server would have been dropped fails because of the cascading that can > happen to drop the user mapping and the foreign table as well. During > the start of the next query such connection will be marked as > invalidated because xact_depth of that connection is > 1 and when the > fail happens, txn gets aborted due to which pgfdw_xact_callback gets > called and in that the connection gets closed. To make it more clear, > please have a look at the scenarios [1]. > In my understanding 'connection gets closed either in the next use' means that connection will be closed next time someone will try to use it, i.e. GetConnection() will be called and it closes this connection because of a bad state. However, if foreign server is dropped GetConnection() cannot lookup the connection because it needs a user mapping oid as a key. I had a look on your scenarios. IIUC, under **next use** you mean a select attempt from a table belonging to the same foreign server, which leads to a transaction abort and connection gets closed in the xact callback. Sorry, maybe I am missing something, but this just confirms that such connections only get closed in the xact callback (taking into account your recently committed patch [1]), so 'next use' looks misleading. [1] https://www.postgresql.org/message-id/8b2aa1aa-c638-12a8-cb56-ea0f0a5019cf%40oss.nttdata.com Regards -- Alexey Kondratov Postgres Professional https://www.postgrespro.com Russian Postgres Company