pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alexey Kondratov <a.kondratov@postgrespro.ru>
To: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Subject: Re: [PATCH] postgres_fdw connection caching - cause remote sessions linger till the local session exit
Date: Mon, 23 Nov 2020 19:27:18 +0300
Message-ID: <1f29ad3f1d8b969dfde6d500bbd9d82d@postgrespro.ru> (raw)
In-Reply-To: <CALj2ACU0gf_Xf2sAtrCRhvLc-G42UqLGthDfVC9G1jzUpBcQ1w@mail.gmail.com>
References: <CALj2ACVvrp5=AVp2PupEm+nAC8S4buqR3fJMmaCoc7ftT0aD2A@mail.gmail.com>
	<CALj2ACVKQeXJ2okDDDiSC5y6i1tfd3P+n7-5iwqnQ=JiOxUyMA@mail.gmail.com>
	<160467818521.7362.11186359886835925531.pgcf@coridan.postgresql.org>
	<6df6525ca7a4b54a4a39f55e4dd6b3e9@postgrespro.ru>
	<CALj2ACVODykNQQAuz3Veh5S9J3QfAi4D9CsExguMhYB4-+-YGw@mail.gmail.com>
	<f58d1df4ae58f6cf3bfa560f923462e0@postgrespro.ru>
	<CALj2ACX4B9sBTKPg70TzKfPfPJ=ndocU6hY5jii5wDvzW2X2DQ@mail.gmail.com>
	<c22ad9fcdba6ab768ec2ea86d33d0c18@postgrespro.ru>
	<CALj2ACW+PzzuqE6YOueNUi5Zek9XTv9P3FXdT4XF3-1JDk4RuA@mail.gmail.com>
	<CALj2ACU0gf_Xf2sAtrCRhvLc-G42UqLGthDfVC9G1jzUpBcQ1w@mail.gmail.com>

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





view thread (175+ messages)  latest in thread

Message-ID: <1f29ad3f1d8b969dfde6d500bbd9d82d@postgrespro.ru>
Permalink:  ../1f29ad3f1d8b969dfde6d500bbd9d82d@postgrespro.ru/
Also on:    postgresql.org/message-id/1f29ad3f1d8b969dfde6d500bbd9d82d@postgrespro.ru

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: a.kondratov@postgrespro.ru, bharath.rupireddyforpostgres@gmail.com
  Subject: Re: [PATCH] postgres_fdw connection caching - cause remote sessions linger till the local session exit
  In-Reply-To: <1f29ad3f1d8b969dfde6d500bbd9d82d@postgrespro.ru>
  References: <CALj2ACVvrp5=AVp2PupEm+nAC8S4buqR3fJMmaCoc7ftT0aD2A@mail.gmail.com> <CALj2ACVKQeXJ2okDDDiSC5y6i1tfd3P+n7-5iwqnQ=JiOxUyMA@mail.gmail.com> <160467818521.7362.11186359886835925531.pgcf@coridan.postgresql.org> <6df6525ca7a4b54a4a39f55e4dd6b3e9@postgrespro.ru> <CALj2ACVODykNQQAuz3Veh5S9J3QfAi4D9CsExguMhYB4-+-YGw@mail.gmail.com> <f58d1df4ae58f6cf3bfa560f923462e0@postgrespro.ru> <CALj2ACX4B9sBTKPg70TzKfPfPJ=ndocU6hY5jii5wDvzW2X2DQ@mail.gmail.com> <c22ad9fcdba6ab768ec2ea86d33d0c18@postgrespro.ru> <CALj2ACW+PzzuqE6YOueNUi5Zek9XTv9P3FXdT4XF3-1JDk4RuA@mail.gmail.com> <CALj2ACU0gf_Xf2sAtrCRhvLc-G42UqLGthDfVC9G1jzUpBcQ1w@mail.gmail.com> <1f29ad3f1d8b969dfde6d500bbd9d82d@postgrespro.ru>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox