Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g8RBi-0004k0-VQ for pgsql-hackers@arkaria.postgresql.org; Fri, 05 Oct 2018 14:34:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g8RBh-0002Wc-7E for pgsql-hackers@arkaria.postgresql.org; Fri, 05 Oct 2018 14:34:41 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g8RBg-0002WV-Vx for pgsql-hackers@lists.postgresql.org; Fri, 05 Oct 2018 14:34:41 +0000 Received: from mx1.mailbox.org ([80.241.60.212]) by magus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1g8RBe-0006sm-U4 for pgsql-hackers@postgresql.org; Fri, 05 Oct 2018 14:34:40 +0000 Received: from smtp1.mailbox.org (smtp1.mailbox.org [80.241.60.240]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.mailbox.org (Postfix) with ESMTPS id 3CD9A498B7; Fri, 5 Oct 2018 16:34:37 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp1.mailbox.org ([80.241.60.240]) by spamfilter03.heinlein-hosting.de (spamfilter03.heinlein-hosting.de [80.241.56.117]) (amavisd-new, port 10030) with ESMTP id 2c0r4EnwZmWc; Fri, 5 Oct 2018 16:34:36 +0200 (CEST) Message-ID: <360dcd4eff43a302e04148abca0c94f56a233df9.camel@cybertec.at> Subject: Re: Libpq support to connect to standby server as priority From: Laurenz Albe To: Haribabu Kommi , Robert Haas Cc: Jing Wang , Pg Hackers , "Tsunakawa, Takayuki" Date: Fri, 05 Oct 2018 16:34:34 +0200 In-Reply-To: References: <1530710078.2411.29.camel@cybertec.at> Organization: Cybertec Content-Type: text/plain; charset="UTF-8" Mime-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Haribabu Kommi wrote: > On Thu, Jul 19, 2018 at 10:59 PM Haribabu Kommi wrote: > > On Wed, Jul 18, 2018 at 10:53 PM Robert Haas wrote: > > > On Wed, Jul 4, 2018 at 9:14 AM, Laurenz Albe wrote: > > > > What about keeping the first successful connection open and storing it in a > > > > variable if we are in "prefer-read" mode. > > > > If we get the read-only connection we desire, close that cached connection, > > > > otherwise use it. > > > > > > I like this idea. If I recall correctly, the logic in this area is > > > getting pretty complex, so we might need to refactor it for better > > > readability and maintainability. > > > > OK. I will work on the code refactoring first and then provide the > > prefer-read option on top it. > > commits d1c6a14bacf and 5ca00774194 have refactored the logic > of handling the different connection states. > > Attached is a rebased patch after further refactoring the new option > code for easier maintenance. The code is much more readable now, thanks. --- a/src/interfaces/libpq/libpq-int.h +++ b/src/interfaces/libpq/libpq-int.h @@ -397,6 +397,7 @@ struct pg_conn int nconnhost; /* # of hosts named in conn string */ int whichhost; /* host we're currently trying/connected to */ pg_conn_host *connhost; /* details about each named host */ + int read_write_host_index; /* index for first read-write host in connhost */ /* Connection data */ pgsocket sock; /* FD for socket, PGINVALID_SOCKET if I think the comment could use more love. This would be the place to document the logic: Initial value is -1, then then index of the first working server we found, and -2 for the second attempt to connect to that server. I notice that you don't keep the first connection open, but close and reopen it. I guess that is a matter of taste, but it would be easier on resources (and reduce connection time) if the connection were kept open. Admittedly, it would be more difficult and might further complicate code that is not very clear as it is. If you work on some more on the comment above, I will mark it as ready for committer. Yours, Laurenz Albe