pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Fujii Masao <masao.fujii@oss.nttdata.com>
To: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Cc: Zhihong Yu <zyu@yugabyte.com>
Cc: Alexey Kondratov <a.kondratov@postgrespro.ru>
Cc: Hou, Zhijie <houzj.fnst@cn.fujitsu.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: Thu, 21 Jan 2021 13:36:46 +0900
Message-ID: <0e61fbcc-fca2-18f8-758c-4ed61c664fd7@oss.nttdata.com> (raw)
In-Reply-To: <CALj2ACWVy_PGERjsZW+nTKBNKoEWPnRVoc=Bw0M7TorYuoquFQ@mail.gmail.com>
References: <CALj2ACVvrp5=AVp2PupEm+nAC8S4buqR3fJMmaCoc7ftT0aD2A@mail.gmail.com>
	<CALNJ-vTHJBFBVnxJ2soHhAg6UrCAD9RgMF9RF6yj5AhTuJM-mg@mail.gmail.com>
	<CALj2ACVEadzaB88ZRVF499nFxdO5pBTMCDJ0xG9kvLVhea0eYg@mail.gmail.com>
	<c2a00f21-a80d-8bce-e2ea-8cd8e16c4da9@oss.nttdata.com>
	<CALj2ACURjjg8wVpfb-s8GE+4ycQcx6z+QGQOM3TC_Cjz+=Lq6w@mail.gmail.com>
	<422884a8-c863-a4d4-cebd-562855c29977@oss.nttdata.com>
	<CALj2ACUv_+qaoYvsy674Aw8Myo-Ydte7b6vRKi3Q8ZmfG+S5Bg@mail.gmail.com>
	<0bc2b366-76d4-9480-5812-6f0de2b3f526@oss.nttdata.com>
	<CALj2ACXMB4Z4ewCeXWx7oOSxzkhqQ4KLeNVZ=NB6=fjhN89n3w@mail.gmail.com>
	<ba9c0824-77b7-16bd-e425-7b1893725eee@oss.nttdata.com>
	<CALj2ACW3p_TLLH_On3Q=EWYoGKLYzi9vxH_isRXYksw5j1mkTQ@mail.gmail.com>
	<0c53c3da-9bfe-82f0-1190-5f029e92a816@oss.nttdata.com>
	<CALj2ACUdk3YJSSW9z7hNbnp53ase4yasNO5UaubUx7WeQtdKuQ@mail.gmail.com>
	<394e56d4-5b8c-57cf-52e2-ea331436882d@oss.nttdata.com>
	<CALj2ACWVy_PGERjsZW+nTKBNKoEWPnRVoc=Bw0M7TorYuoquFQ@mail.gmail.com>



On 2021/01/21 12:00, Bharath Rupireddy wrote:
> On Wed, Jan 20, 2021 at 6:58 PM Fujii Masao <masao.fujii@oss.nttdata.com> wrote:
>> + * It checks if the cache has a connection for the given foreign server that's
>> + * not being used within current transaction, then returns true. If the
>> + * connection is in use, then it emits a warning and returns false.
>>
>> The comment also should mention the case where no open connection
>> for the given server is found? What about rewriting this to the following?
>>
>> ---------------------
>> If the cached connection for the given foreign server is found and has not
>> been used within current transaction yet, close the connection and return
>> true. Even when it's found, if it's already used, keep the connection, emit
>> a warning and return false. If it's not found, return false.
>> ---------------------
> 
> Done.
> 
>> + * It returns true, if it closes at least one connection, otherwise false.
>> + *
>> + * It returns false, if the cache doesn't exit.
>>
>> The above second comment looks redundant.
> 
> Yes. "otherwise false" means it.
> 
>> +       if (ConnectionHash)
>> +               result = disconnect_cached_connections(0, true);
>>
>> Isn't it smarter to make disconnect_cached_connections() check
>> ConnectionHash and return false if it's NULL? If we do that, we can
>> simplify the code of postgres_fdw_disconnect() and _all().
> 
> Done.
> 
>> + * current transaction are disconnected. Otherwise, the unused entries with the
>> + * given hashvalue are disconnected.
>>
>> In the above second comment, a singular form should be used instead?
>> Because there must be no multiple entries with the given hashvalue.
> 
> Rephrased the function comment a bit. Mentioned the _disconnect and
> _disconnect_all in comments because we have said enough what each of
> those two functions do.
> 
> +/*
> + * Workhorse to disconnect cached connections.
> + *
> + * This function disconnects either all unused connections when called from
> + * postgres_fdw_disconnect_all or a given foreign server unused connection when
> + * called from postgres_fdw_disconnect.
> + *
> + * This function returns true if at least one connection is disconnected,
> + * otherwise false.
> + */
> 
>> +                               server = GetForeignServer(entry->serverid);
>>
>> This seems to cause an error "cache lookup failed"
>> if postgres_fdw_disconnect_all() is called when there is
>> a connection in use but its server is dropped. To avoid this error,
>> GetForeignServerExtended() with FSV_MISSING_OK should be used
>> instead, like postgres_fdw_get_connections() does?
> 
> +1.  So, I changed it to GetForeignServerExtended, added an assertion
> for invalidation  just like postgres_fdw_get_connections. I also added
> a test case for this, we now emit a slightly different warning for
> this case alone that is (errmsg("cannot close dropped server
> connection because it is still in use")));. This warning looks okay as
> we cannot show any other server name in the ouput and we know that
> this rare case can exist when someone drops the server in an explicit
> transaction.
> 
>> +                       if (entry->server_hashvalue == hashvalue &&
>> +                               (entry->xact_depth > 0 || result))
>> +                       {
>> +                               hash_seq_term(&scan);
>> +                               break;
>>
>> entry->server_hashvalue can be 0? If yes, since postgres_fdw_disconnect_all()
>> specifies 0 as hashvalue, ISTM that the above condition can be true
>> unexpectedly. Can we replace this condition with just "if (!all)"?
> 
> I don't think so entry->server_hashvalue can be zero, because
> GetSysCacheHashValue1/CatalogCacheComputeHashValue will not return 0
> as hash value. I have not seen someone comparing hashvalue with an
> expectation that it has 0 value, for instance see if (hashvalue == 0
> || riinfo->oidHashValue == hashvalue).
> 
>   Having if(!all) something like below there doesn't suffice because we
> might call hash_seq_term, when some connection other than the given
> foreign server connection is in use.

No because we check the following condition before reaching that code. No?

+		if ((all || entry->server_hashvalue == hashvalue) &&


I was thinking that "(entry->xact_depth > 0 || result))" condition is not
necessary because "result" is set to true when xact_depth <= 0 and that
condition always indicates true.

Regards,

-- 
Fujii Masao
Advanced Computing Technology Center
Research and Development Headquarters
NTT DATA CORPORATION





view thread (175+ messages)  latest in thread

Message-ID: <0e61fbcc-fca2-18f8-758c-4ed61c664fd7@oss.nttdata.com>
Permalink:  ../0e61fbcc-fca2-18f8-758c-4ed61c664fd7@oss.nttdata.com/
Also on:    postgresql.org/message-id/0e61fbcc-fca2-18f8-758c-4ed61c664fd7@oss.nttdata.com

 · 

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: masao.fujii@oss.nttdata.com, bharath.rupireddyforpostgres@gmail.com, zyu@yugabyte.com, a.kondratov@postgrespro.ru, houzj.fnst@cn.fujitsu.com
  Subject: Re: [PATCH] postgres_fdw connection caching - cause remote sessions linger till the local session exit
  In-Reply-To: <0e61fbcc-fca2-18f8-758c-4ed61c664fd7@oss.nttdata.com>
  References: <CALj2ACVvrp5=AVp2PupEm+nAC8S4buqR3fJMmaCoc7ftT0aD2A@mail.gmail.com> <CALj2ACVEadzaB88ZRVF499nFxdO5pBTMCDJ0xG9kvLVhea0eYg@mail.gmail.com> <c2a00f21-a80d-8bce-e2ea-8cd8e16c4da9@oss.nttdata.com> <CALj2ACURjjg8wVpfb-s8GE+4ycQcx6z+QGQOM3TC_Cjz+=Lq6w@mail.gmail.com> <422884a8-c863-a4d4-cebd-562855c29977@oss.nttdata.com> <CALj2ACUv_+qaoYvsy674Aw8Myo-Ydte7b6vRKi3Q8ZmfG+S5Bg@mail.gmail.com> <0bc2b366-76d4-9480-5812-6f0de2b3f526@oss.nttdata.com> <CALj2ACXMB4Z4ewCeXWx7oOSxzkhqQ4KLeNVZ=NB6=fjhN89n3w@mail.gmail.com> <ba9c0824-77b7-16bd-e425-7b1893725eee@oss.nttdata.com> <CALj2ACW3p_TLLH_On3Q=EWYoGKLYzi9vxH_isRXYksw5j1mkTQ@mail.gmail.com> <0c53c3da-9bfe-82f0-1190-5f029e92a816@oss.nttdata.com> <CALj2ACUdk3YJSSW9z7hNbnp53ase4yasNO5UaubUx7WeQtdKuQ@mail.gmail.com> <394e56d4-5b8c-57cf-52e2-ea331436882d@oss.nttdata.com> <CALj2ACWVy_PGERjsZW+nTKBNKoEWPnRVoc=Bw0M7TorYuoquFQ@mail.gmail.com> <0e61fbcc-fca2-18f8-758c-4ed61c664fd7@oss.nttdata.com>

* 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