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 1khEgX-0000ZK-Sz for pgsql-hackers@arkaria.postgresql.org; Mon, 23 Nov 2020 16:27:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1khEgW-0007VV-S1 for pgsql-hackers@arkaria.postgresql.org; Mon, 23 Nov 2020 16:27:24 +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 1khEgW-0007VO-JM for pgsql-hackers@lists.postgresql.org; Mon, 23 Nov 2020 16:27:24 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by magus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1khEgT-0000mM-U0 for pgsql-hackers@lists.postgresql.org; Mon, 23 Nov 2020 16:27: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 E6B3B21C61AE; Mon, 23 Nov 2020 19:27:18 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1606148838; bh=xwPLAOPpqC8f7nm1j6D6H3u97avzC3bdPrV9eceEKZU=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=PJM79hSg6mAK5mK0rApRQetRlU1FhRobec/d04rNS0tgjlkr3hTfy63PZOsTNIvZ6 d/3eIqNqC1FqFP1PDG72fBNGmEAgTANuYqw73RKmGhXBo2EiVfiTbnnJJDp5IPz4VR gbASzFVSfgvbxCXUMBx+klCOy7XFO186a2BcOMJY= MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit Date: Mon, 23 Nov 2020 19:27:18 +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: <1f29ad3f1d8b969dfde6d500bbd9d82d@postgrespro.ru> X-Sender: a.kondratov@postgrespro.ru List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi, On 2020-11-23 09:48, Bharath Rupireddy wrote: >> >> Here is how I'm making 4 separate patches: >> >> 1. new function and it's documentation. >> 2. GUC and it's documentation. >> 3. server level option and it's documentation. >> 4. test cases for all of the above patches. >> > > Hi, I'm attaching the patches here. Note that, though the code changes > for this feature are small, I divided them up as separate patches to > make review easy. > > v1-0001-postgres_fdw-function-to-discard-cached-connections.patch > This patch looks pretty straightforward for me, but there are some things to be addressed IMO: + server = GetForeignServerByName(servername, true); + + if (server != NULL) + { Yes, you return a false if no server was found, but for me it worth throwing an error in this case as, for example, dblink does in the dblink_disconnect(). + result = disconnect_cached_connections(FOREIGNSERVEROID, + hashvalue, + false); + if (all || (!all && cacheid == FOREIGNSERVEROID && + entry->server_hashvalue == hashvalue)) + { + if (entry->conn != NULL && + !all && cacheid == FOREIGNSERVEROID && + entry->server_hashvalue == hashvalue) These conditions look bulky for me. First, you pass FOREIGNSERVEROID to disconnect_cached_connections(), but actually it just duplicates 'all' flag, since when it is 'FOREIGNSERVEROID', then 'all == false'; when it is '-1', then 'all == true'. That is all, there are only two calls of disconnect_cached_connections(). That way, it seems that we should keep only 'all' flag at least for now, doesn't it? Second, I think that we should just rewrite this if statement in order to simplify it and make more readable, e.g.: if ((all || entry->server_hashvalue == hashvalue) && entry->conn != NULL) { disconnect_pg_server(entry); result = true; } + if (all) + { + hash_destroy(ConnectionHash); + ConnectionHash = NULL; + result = true; + } Also, I am still not sure that it is a good idea to destroy the whole cache even in 'all' case, but maybe others will have a different opinion. > > v1-0002-postgres_fdw-add-keep_connections-GUC-to-not-cache-connections.patch > + entry->changing_xact_state) || + (entry->used_in_current_xact && + !keep_connections)) I am not sure, but I think, that instead of adding this additional flag into ConnCacheEntry structure we can look on entry->xact_depth and use local: bool used_in_current_xact = entry->xact_depth > 0; for exactly the same purpose. Since we set entry->xact_depth to zero at the end of xact, then it was used if it is not zero. It is set to 1 by begin_remote_xact() called by GetConnection(), so everything seems to be fine. Otherwise, both patches seem to be working as expected. I am going to have a look on the last two patches a bit later. Regards -- Alexey Kondratov Postgres Professional https://www.postgrespro.com Russian Postgres Company