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 1khdHR-0003v3-00 for pgsql-hackers@arkaria.postgresql.org; Tue, 24 Nov 2020 18:43:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1khdHP-0003Bs-U0 for pgsql-hackers@arkaria.postgresql.org; Tue, 24 Nov 2020 18:43:07 +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 1khdHP-0003Bl-KH for pgsql-hackers@lists.postgresql.org; Tue, 24 Nov 2020 18:43:07 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by magus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1khdHM-0005yH-W8 for pgsql-hackers@lists.postgresql.org; Tue, 24 Nov 2020 18:43:07 +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 17CAB21C50E7; Tue, 24 Nov 2020 21:43:02 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1606243382; bh=XtuiggfCOXgTCFZ57M0U4pM+UKDlLORKVcVDFWbsDTA=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=RI5W02HzoyXEfLId/98h0cf/vfETgpFOEvQYoJgX1zshQOdJcADxDozgUSqqQAC0p TVp/TEab8HGVWX5jFRWH3IeWn1eZ6YqlkDGOXz94OJ06xyWF8Dou7imGC98m983g6R TglJYW8G//kaqfhYjw5cE3oISgcuZ/tEf2tkQrR4= MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit Date: Tue, 24 Nov 2020 21:43:01 +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> <1f29ad3f1d8b969dfde6d500bbd9d82d@postgrespro.ru> User-Agent: Roundcube Webmail/1.4.0 Message-ID: X-Sender: a.kondratov@postgrespro.ru List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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 > 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, postgres_fdw.keep_connections, default being + on, when set to off, 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, postgres_fdw.keep_connections, default being... + + Note that when postgres_fdw.keep_connections is set to + off, postgres_fdw 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 + postgres_fdw_disconnect function. + The whole paragraph is really difficult to follow. It could be something like that: Note that setting postgres_fdw.keep_connections 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 postgres_fdw_disconnect function. Regards -- Alexey Kondratov Postgres Professional https://www.postgrespro.com Russian Postgres Company