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 1kfQr1-0000dt-2J for pgsql-hackers@arkaria.postgresql.org; Wed, 18 Nov 2020 17:02:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kfQqz-0000ZL-TN for pgsql-hackers@arkaria.postgresql.org; Wed, 18 Nov 2020 17:02:45 +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 1kfQqz-0000ZE-Gu for pgsql-hackers@lists.postgresql.org; Wed, 18 Nov 2020 17:02:45 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by magus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kfQqw-0007wB-Ru for pgsql-hackers@lists.postgresql.org; Wed, 18 Nov 2020 17:02:45 +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 32E8C21C48EF; Wed, 18 Nov 2020 20:02:41 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1605718961; bh=8jteFPRQkxx/rkGGjCRcGiBTmTnFGjnmT4BYshsDegA=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=eWAb9oFkQKdWnMzDtTG15f2/k1HRWxkASQ2odzco7VJZ9B6fMnEzQkYGGgi6fnPgS KnLLue45oA2gW1pby+lrJB5hb/IWm8IQ5lOGAE6NZNw2L8lJ7g45meckHj8eDRfY6n 7SXdUXZ6/A83pzY1rCq3GaOR8t5xrVdEQWro6Cik= MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit Date: Wed, 18 Nov 2020 20:02:41 +0300 From: Alexey Kondratov To: Bharath Rupireddy Cc: PostgreSQL Hackers Subject: Re: [PATCH] postgres_fdw connection caching - cause remote sessions linger till the local session exit In-Reply-To: References: <160467818521.7362.11186359886835925531.pgcf@coridan.postgresql.org> <6df6525ca7a4b54a4a39f55e4dd6b3e9@postgrespro.ru> User-Agent: Roundcube Webmail/1.4.0 Message-ID: X-Sender: a.kondratov@postgrespro.ru List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2020-11-18 16:39, Bharath Rupireddy wrote: > Thanks for the interest shown! > > On Wed, Nov 18, 2020 at 1:07 AM Alexey Kondratov > wrote: >> >> Regarding the initial issue I prefer point #3, i.e. foreign server >> option. It has a couple of benefits IMO: 1) it may be set separately >> on >> per foreign server basis, 2) it will live only in the postgres_fdw >> contrib without any need to touch core. I would only supplement this >> postgres_fdw foreign server option with a GUC, e.g. >> postgres_fdw.keep_connections, so one could easily define such >> behavior >> for all foreign servers at once or override server-level option by >> setting this GUC on per session basis. >> > > Below is what I have in my mind, mostly inline with yours: > > a) Have a server level option (keep_connetion true/false, with the > default being true), when set to false the connection that's made with > this foreign server is closed and cached entry from the connection > cache is deleted at the end of txn in pgfdw_xact_callback. > b) Have postgres_fdw level GUC postgres_fdw.keep_connections default > being true. When set to false by the user, the connections, that are > used after this, are closed and removed from the cache at the end of > respective txns. If we don't use a connection that was cached prior to > the user setting the GUC as false, then we may not be able to clear > it. We can avoid this problem by recommending users either to set the > GUC to false right after the CREATE EXTENSION postgres_fdw; or else > use the function specified in (c). > c) Have a new function that gets defined as part of CREATE EXTENSION > postgres_fdw;, say postgres_fdw_discard_connections(), similar to > dblink's dblink_disconnect(), which discards all the remote > connections and clears connection cache. And we can also have server > name as input to postgres_fdw_discard_connections() to discard > selectively. > > Thoughts? If okay with the approach, I will start working on the patch. > This approach looks solid enough from my perspective to give it a try. I would only make it as three separate patches for an ease of further review. >> >> Attached is a small POC patch, which implements this contrib-level >> postgres_fdw.keep_connections GUC. What do you think? >> > > I see two problems with your patch: 1) It just disconnects the remote > connection at the end of txn if the GUC is set to false, but it > doesn't remove the connection cache entry from ConnectionHash. Yes, and this looks like a valid state for postgres_fdw and it can get into the same state even without my patch. Next time GetConnection() will find this cache entry, figure out that entry->conn is NULL and establish a fresh connection. It is not clear for me right now, what benefits we will get from clearing also this cache entry, except just doing this for sanity. > 2) What > happens if there are some cached connections, user set the GUC to > false and not run any foreign queries or not use those connections > thereafter, so only the new connections will not be cached? Will the > existing unused connections still remain in the connection cache? See > (b) above for a solution. > Yes, they will. This could be solved with that additional disconnect function as you proposed in c). Regards -- Alexey Kondratov Postgres Professional https://www.postgrespro.com Russian Postgres Company