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 1fahcY-000100-F8 for pgsql-hackers@arkaria.postgresql.org; Wed, 04 Jul 2018 13:14:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fahcW-0001e4-2l for pgsql-hackers@arkaria.postgresql.org; Wed, 04 Jul 2018 13:14:56 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fahcV-0001dx-Oi for pgsql-hackers@lists.postgresql.org; Wed, 04 Jul 2018 13:14:55 +0000 Received: from mx2.mailbox.org ([80.241.60.215]) by makus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fahcN-00077o-KW for pgsql-hackers@postgresql.org; Wed, 04 Jul 2018 13:14:54 +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 mx2.mailbox.org (Postfix) with ESMTPS id ED271413B1; Wed, 4 Jul 2018 15:14:42 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp1.mailbox.org ([80.241.60.240]) by spamfilter01.heinlein-hosting.de (spamfilter01.heinlein-hosting.de [80.241.56.115]) (amavisd-new, port 10030) with ESMTP id mdUP06dlGMmw; Wed, 4 Jul 2018 15:14:40 +0200 (CEST) Message-ID: <1530710078.2411.29.camel@cybertec.at> Subject: Re: Libpq support to connect to standby server as priority From: Laurenz Albe To: Haribabu Kommi , jingwangian@gmail.com Cc: Pg Hackers , "Tsunakawa, Takayuki" Date: Wed, 04 Jul 2018 15:14:38 +0200 In-Reply-To: References: 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 Wed, Jan 24, 2018 at 9:01 AM Jing Wang wrote: > > Hi All, > > > > Recently I put a proposal to support 'prefer-read' parameter in target_session_attrs in libpq. Now I updated the patch with adding content in the sgml and regression test case. > > > > Some people may have noticed there is already another patch (https://commitfest.postgresql.org/15/1148/ ) which looks similar with this. But I would say this patch is more complex than my proposal. > > > > It is better separate these 2 patches to consider. > > I also feel prefer-read and read-only options needs to take as two different options. > prefer-read is simple to support than read-only. > > Here I attached an updated patch that is rebased to the latest master and also > fixed some of the corner scenarios. The patch applies, builds and passes "make check-world". I think the "prefer-read" functionality is desirable: It is exactly what you need if you want to use replication for load balancing, and your application supports different database connections for reading and writing queries. "read-only" does not have a clear use case in my opinion. With the patch, PostgreSQL behaves as expected if I have a primary and a standby and run: psql "host=/tmp,/tmp port=5433,5434 target_session_attrs=prefer-read" But if I stop the standby (port 5434), libpq goes into an endless loop. Concerning the code: - The documentation needs some attention. Suggestion: If this parameter is set to prefer-read, connections where SHOW transaction_read_only returns off are preferred. If no such connection can be found, a connection that allows read-write transactions will be accepted. - I think the construction with "read_write_host_index" makes the code even more complicated than it already is. 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. Yours, Laurenz Albe