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: Tue, 24 Nov 2020 21:43:01 +0300
Message-ID: <be224d0520df0594b0693c95bd19fe0a@postgrespro.ru> (raw)
In-Reply-To: <CALj2ACVq99r+s04tWrD8TBG6QOfa=r1j0JXvKbO6JFmrMgdWpg@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>
<1f29ad3f1d8b969dfde6d500bbd9d82d@postgrespro.ru>
<CALj2ACVq99r+s04tWrD8TBG6QOfa=r1j0JXvKbO6JFmrMgdWpg@mail.gmail.com>
On 2020-11-24 06:52, Bharath Rupireddy wrote:
> Thanks for the review comments.
>
> On Mon, Nov 23, 2020 at 9:57 PM Alexey Kondratov
> <a.kondratov@postgrespro.ru> wrote:
>>
>> > 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().
>>
>
> dblink_disconnect() "Returns status, which is always OK (since any
> error causes the function to throw an error instead of returning)."
> This behaviour doesn't seem okay to me.
>
> Since we throw true/false, I would prefer to throw a warning(with a
> reason) while returning false over an error.
>
I thought about something a bit more sophisticated:
1) Return 'true' if there were open connections and we successfully
closed them.
2) Return 'false' in the no-op case, i.e. there were no open
connections.
3) Rise an error if something went wrong. And non-existing server case
belongs to this last category, IMO.
That looks like a semantically correct behavior, but let us wait for any
other opinion.
>
>>
>> + 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?
>>
>
> I added cachid as an argument to disconnect_cached_connections() for
> reusability. Say, someone wants to use it with a user mapping then
> they can pass cacheid USERMAPPINGOID, hash value of user mapping. The
> cacheid == USERMAPPINGOID && entry->mapping_hashvalue == hashvalue can
> be added to disconnect_cached_connections().
>
Yeah, I have got your point and motivation to add this argument, but how
we can use it? To disconnect all connections belonging to some specific
user mapping? But any user mapping is hard bound to some foreign server,
AFAIK, so we can pass serverid-based hash in this case.
In the case of pgfdw_inval_callback() this argument makes sense, since
syscache callbacks work that way, but here I can hardly imagine a case
where we can use it. Thus, it still looks as a preliminary complication
for me, since we do not have plans to use it, do we? Anyway, everything
seems to be working fine, so it is up to you to keep this additional
argument.
>
> v1-0003-postgres_fdw-server-level-option-keep_connection.patch
> This patch adds a new server level option, keep_connection, default
> being on, when set to off, the local session doesn't cache the
> connections associated with the foreign server.
>
This patch looks good to me, except one note:
(entry->used_in_current_xact &&
- !keep_connections))
+ (!keep_connections || !entry->keep_connection)))
{
Following this logic:
1) If keep_connections == true, then per-server keep_connection has a
*higher* priority, so one can disable caching of a single foreign
server.
2) But if keep_connections == false, then it works like a global switch
off indifferently of per-server keep_connection's, i.e. they have a
*lower* priority.
It looks fine for me, at least I cannot propose anything better, but
maybe it should be documented in 0004?
>
> v1-0004-postgres_fdw-connection-cache-discard-tests-and-documentation.patch
> This patch adds the tests and documentation related to this feature.
>
I have not read all texts thoroughly, but what caught my eye:
+ A GUC, <varname>postgres_fdw.keep_connections</varname>, default
being
+ <literal>on</literal>, when set to <literal>off</literal>, the local
session
I think that GUC acronym is used widely only in the source code and
Postgres docs tend to do not use it at all, except from acronyms list
and a couple of 'GUC parameters' collocation usage. And it never used in
a singular form there, so I think that it should be rather:
A configuration parameter,
<varname>postgres_fdw.keep_connections</varname>, default being...
+ <para>
+ Note that when <varname>postgres_fdw.keep_connections</varname>
is set to
+ off, <filename>postgres_fdw</filename> discards either the
connections
+ that are made previously and will be used by the local session or
the
+ connections that will be made newly. But the connections that are
made
+ previously and kept, but not used after this parameter is set to
off, are
+ not discarded. To discard them, use
+ <function>postgres_fdw_disconnect</function> function.
+ </para>
The whole paragraph is really difficult to follow. It could be something
like that:
<para>
Note that setting <varname>postgres_fdw.keep_connections</varname>
to
off does not discard any previously made and still open
connections immediately.
They will be closed only at the end of a future transaction, which
operated on them.
To close all connections immediately use
<function>postgres_fdw_disconnect</function> function.
</para>
Regards
--
Alexey Kondratov
Postgres Professional https://www.postgrespro.com
Russian Postgres Company
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: <be224d0520df0594b0693c95bd19fe0a@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> <CALj2ACVq99r+s04tWrD8TBG6QOfa=r1j0JXvKbO6JFmrMgdWpg@mail.gmail.com> <be224d0520df0594b0693c95bd19fe0a@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