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 1kzzlu-0005X3-3Q for pgsql-hackers@arkaria.postgresql.org; Thu, 14 Jan 2021 10:22:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kzzlr-00046q-CV for pgsql-hackers@arkaria.postgresql.org; Thu, 14 Jan 2021 10:22:27 +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 1kzzlr-00046j-4o for pgsql-hackers@lists.postgresql.org; Thu, 14 Jan 2021 10:22:27 +0000 Received: from oss.nttdata.com ([49.212.34.109]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kzzln-0005FK-In for pgsql-hackers@lists.postgresql.org; Thu, 14 Jan 2021 10:22:26 +0000 Received: from hnk.local (p2695249-ipbf2908funabasi.chiba.ocn.ne.jp [60.33.94.249]) by oss.nttdata.com (Postfix) with ESMTPSA id CF0EC62732; Thu, 14 Jan 2021 19:22:19 +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: Zhihong Yu , Alexey Kondratov , "Hou, Zhijie" , PostgreSQL Hackers References: <9b90712303dbecddd8691b95cee93b3a@postgrespro.ru> <82debcd7-b236-0fc5-5ff1-5e6b99c59d64@oss.nttdata.com> From: Fujii Masao Message-ID: <55bd82c5-d133-6fbb-f1f4-e25c1509c96e@oss.nttdata.com> Date: Thu, 14 Jan 2021 19:22:19 +0900 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:78.0) Gecko/20100101 Thunderbird/78.6.1 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/mixed; boundary="------------5DF4DBB49E8C097A5677EC08" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------5DF4DBB49E8C097A5677EC08 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit On 2021/01/09 10:12, Bharath Rupireddy wrote: > On Fri, Jan 8, 2021 at 9:55 AM Bharath Rupireddy > wrote: >> I will make the changes and post a new patch set soon. > > Attaching v9 patch set that has addressed the review comments on the > disconnect function returning setof records, documentation changes, > and postgres_fdw--1.0-1.1.sql changes. > > Please consider the v9 patch set for further review. Thanks for updating the patch! I reviewed only 0001 patch. + /* + * Quick exit if the cache has been destroyed in + * disconnect_cached_connections. + */ + if (!ConnectionHash) + return; This code is not necessary at least in pgfdw_xact_callback() and pgfdw_subxact_callback()? Because those functions check "if (!xact_got_connection)" before checking the above condition. - if (!HeapTupleIsValid(tup)) - elog(ERROR, "cache lookup failed for user mapping %u", entry->key); - umform = (Form_pg_user_mapping) GETSTRUCT(tup); - server = GetForeignServer(umform->umserver); - ReleaseSysCache(tup); + server = GetForeignServer(entry->serverid); What about applying only the change about serverid, as a separate patch at first? This change itself is helpful to get rid of error "cache lookup failed" in pgfdw_reject_incomplete_xact_state_change(). Patch attached. + server = GetForeignServerExtended(entry->serverid, true); Since the type of second argument in GetForeignServerExtended() is bits16, it's invalid to specify "true" there? + if (no_server_conn_cnt > 0) + { + ereport(WARNING, + (errmsg_plural("found an active connection for which the foreign server would have been dropped", + "found some active connections for which the foreign servers would have been dropped", + no_server_conn_cnt), + no_server_conn_cnt > 1 ? + errdetail("Such connections are discarded at the end of remote transaction.") + : errdetail("Such connection is discarded at the end of remote transaction."))); At least for me, I like returning such connections with "NULL" in server_name column and "false" in valid column, rather than emitting a warning. Because which would enable us to count the number of actual foreign connections easily by using SQL, for example. + * During the first call, we initialize the function context, get the list + * of active connections using get_connections and store this in the + * function's memory context so that it can live multiple calls. + */ + if (SRF_IS_FIRSTCALL()) I guess that you used value-per-call mode to make the function return a set result since you refered to dblink_get_pkey(). But isn't it better to use materialize mode like dblink_get_notify() does rather than value-per-call because this function returns not so many records? ISTM that we can simplify postgres_fdw_get_connections() by using materialize mode. + hash_destroy(ConnectionHash); + ConnectionHash = NULL; If GetConnection() is called after ConnectionHash is destroyed, it initialize the hashtable and registers some callback functions again even though the same function have already been registered. This causes same function to be registered as a callback more than once. This is a bug. +CREATE FUNCTION postgres_fdw_disconnect () Do we really want postgres_fdw_disconnect() with no argument? IMO postgres_fdw_disconnect() with the server name specified is enough. But I'd like to hear the opinion about that. Regards, -- Fujii Masao Advanced Computing Technology Center Research and Development Headquarters NTT DATA CORPORATION --------------5DF4DBB49E8C097A5677EC08 Content-Type: text/plain; charset=UTF-8; x-mac-type="0"; x-mac-creator="0"; name="serverid_v1.patch" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="serverid_v1.patch" ZGlmZiAtLWdpdCBhL2NvbnRyaWIvcG9zdGdyZXNfZmR3L2Nvbm5lY3Rpb24uYyBiL2NvbnRy aWIvcG9zdGdyZXNfZmR3L2Nvbm5lY3Rpb24uYwppbmRleCAyNjZmNjZjYzYyLi5lYWVkZmVh OWYyIDEwMDY0NAotLS0gYS9jb250cmliL3Bvc3RncmVzX2Zkdy9jb25uZWN0aW9uLmMKKysr IGIvY29udHJpYi9wb3N0Z3Jlc19mZHcvY29ubmVjdGlvbi5jCkBAIC01Nyw2ICs1Nyw3IEBA IHR5cGVkZWYgc3RydWN0IENvbm5DYWNoZUVudHJ5CiAJYm9vbAkJaGF2ZV9lcnJvcjsJCS8q IGhhdmUgYW55IHN1YnhhY3RzIGFib3J0ZWQgaW4gdGhpcyB4YWN0PyAqLwogCWJvb2wJCWNo YW5naW5nX3hhY3Rfc3RhdGU7CS8qIHhhY3Qgc3RhdGUgY2hhbmdlIGluIHByb2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