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 1kpwfb-0006x6-2x for pgsql-hackers@arkaria.postgresql.org; Thu, 17 Dec 2020 17:02:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kpwfZ-00074J-VW for pgsql-hackers@arkaria.postgresql.org; Thu, 17 Dec 2020 17:02:25 +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 1kpwfZ-000746-N3 for pgsql-hackers@lists.postgresql.org; Thu, 17 Dec 2020 17:02:25 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by makus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kpwfW-0007WH-DH for pgsql-hackers@lists.postgresql.org; Thu, 17 Dec 2020 17:02:24 +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 CABDE21C2A6A; Thu, 17 Dec 2020 20:02:19 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1608224539; bh=x43ieUv/FFtKTskX77YvirSsenPbNrrVAgoOZUFd4TQ=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=F7Pvh826zzW6Kkl4ncZzYva/hlfGguDDADFwYWvWdfxX7WCWKQdyAtNlj0tWaMPlL 7u7+Hbo/zQRcAseMotjoum+SZIOx2/xKd3D0E/y7Um+BygaOMvaRS5l7npnBv4R0yZ ZLjP1bJaaXkJrTe9rkPNVD4G8gAVyOkWCIf3u9QY= MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit Date: Thu, 17 Dec 2020 20:02:19 +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> User-Agent: Roundcube Webmail/1.4.0 Message-ID: <919942767cf9a3d4e413c63887aec679@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-17 18:02, Bharath Rupireddy wrote: > On Thu, Dec 17, 2020 at 5:38 PM Hou, Zhijie > wrote: >> I took a look into the patch, and have a little issue: >> >> +bool disconnect_cached_connections(uint32 hashvalue, bool all) >> + if (all) >> + { >> + hash_destroy(ConnectionHash); >> + ConnectionHash = NULL; >> + result = true; >> + } >> >> If disconnect_cached_connections is called to disconnect all the >> connections, >> should we reset the 'xact_got_connection' flag ? > > I think we must allow postgres_fdw_disconnect() to disconnect the > particular/all connections only when the corresponding entries have no > open txns or connections are not being used in that txn, otherwise > not. We may end up closing/disconnecting the connection that's still > being in use because entry->xact_dept can even go more than 1 for sub > txns. See use case [1]. > > + if ((all || entry->server_hashvalue == hashvalue) && > entry->xact_depth == 0 && > + entry->conn) > + { > + disconnect_pg_server(entry); > + result = true; > + } > > Thoughts? > I think that you are right. Actually, I was thinking about much more simple solution to this problem --- just restrict postgres_fdw_disconnect() to run only *outside* of explicit transaction block. This should protect everyone from closing its underlying connections, but seems to be a bit more restrictive than you propose. Just thought, that if we start closing fdw connections in the open xact block: 1) Close a couple of them. 2) Found one with xact_depth > 0 and error out. 3) End up in the mixed state: some of connections were closed, but some them not, and it cannot be rolled back with the xact. In other words, I have some doubts about allowing to call a non-transactional by its nature function in the transaction block. Regards -- Alexey Kondratov Postgres Professional https://www.postgrespro.com Russian Postgres Company