pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael Paquier <michael@paquier.xyz>
To: Haribabu Kommi <kommi.haribabu@gmail.com>
Cc: laurenz.albe@cybertec.at, Tom Lane <tgl@sss.pgh.pa.us>
Cc: Robert Haas <robertmhaas@gmail.com>
Cc: Jing Wang <jingwangian@gmail.com>
Cc: Pg Hackers <pgsql-hackers@postgresql.org>
Cc: Tsunakawa, Takayuki <tsunakawa.takay@jp.fujitsu.com>
Subject: Re: Libpq support to connect to standby server as priority
Date: Thu, 15 Nov 2018 17:14:33 +0900
Message-ID: <20181115081433.GF1374@paquier.xyz> (raw)
In-Reply-To: <CAJrrPGe0JPWn0T-SSBr+Xb9KGENXoLtQHo93m8CsdE2s4rCr-w@mail.gmail.com>
References: <CAJrrPGfzSD4kmedadcDx+3vckj_kJ_jAuufgi+NsemTkWSU5Tw@mail.gmail.com>
	<1530710078.2411.29.camel@cybertec.at>
	<CA+TgmobfBaNaMrT5Qx+nQ=JKYYy-VOZ2xxXAhAGUSSWZ=fkm4g@mail.gmail.com>
	<CAJrrPGea1hX5HmZyVzhJ3HbT8x3+2zoNp48HhPVFzQUYy5PU_w@mail.gmail.com>
	<CAJrrPGcMq9YgG-aMzLK051EndUgjdnpp8_dJwJaNDZbu=sS0qQ@mail.gmail.com>
	<CAJrrPGcwt0LLLzDF9c3-zWrESJCJko-HovDOrMobkaWQaJ5YUw@mail.gmail.com>
	<67be09458f276c0c95a4b2a2a383dca9775d19cd.camel@cybertec.at>
	<26890.1542047012@sss.pgh.pa.us>
	<096a642cff0dd68b8157a5f8838e1762d0050d18.camel@cybertec.at>
	<CAJrrPGe0JPWn0T-SSBr+Xb9KGENXoLtQHo93m8CsdE2s4rCr-w@mail.gmail.com>

On Tue, Nov 13, 2018 at 05:54:17PM +1100, Haribabu Kommi wrote:
> On Tue, Nov 13, 2018 at 7:26 AM Laurenz Albe <laurenz.albe@cybertec.at>
> wrote:
>> As it is now, the patch doesn't keep two connections open.  It remembers
>> the index of the host of the first successful writable connection, but
>> closes the connection, and opens another one to that host if no read-only
>> host can be found.

That's commented in the patch as follows:
+    /*
+     * Requested type is prefer-read, then record this host index
+     * and try the other before considering it later
+     */
+    if ((conn->target_session_attrs != NULL) &&
+        (strcmp(conn->target_session_attrs, "prefer-read") == 0) &&
+        (conn->read_write_host_index != -2))

>> If the read-only connection turns writable after it has been tested,
>> but before it is returned, that can hardly be avoided.
>> I don't think that's so bad - after all, you asked for a read-only
>> connection *if possible*.

Yeah, it is difficult to guarantee that except that checking from time
to time that the connection is still read-only after establishing it.
It is in my opinion mostly a matter of documentation, meaning that the
selection is done when the connection is attempted from the defined
set.

> I also have the same opinion of Laurenz, that this option is letting the
> application to connect to read-only server if possible, otherwise let it
> connect to read-write server.
> 
> I feel that any of the state changes during the connection and after
> connection, needs not to be reflected on the existing connection for
> these type of connections.

+      /*
+       * Reset the host index value to avoid recursion during the
+       * second connection attempt.
+       */
+      conn->read_write_host_index = -2;

Okay, this gets ugly.  I am pretty sure that we should use instead a
status flag and actually avoid any kind of recursion risk in the logic.
Or else it would get hard to track to which value what needs to be set
where in the code.

From purely the code point of view, it seems to me that it is actually
more simple to implement a "read-only" mode as this way there is no need
to mix between CONNECTION_CHECK_READONLY and CONNECTION_CHECK_WRITABLE,
remembering the past index of a connection which may be needed later on
if the next ones don't meet with the wanted conditions.

Each time I have heard about load balancing, applications did not really
care about whether only standbys were used for a set of queries and
accepted that the primary also shared some of the read-only load, be it
for analytics or OLTP, in which case "any" covers already everything
needed.  And if you really want a standby, "read-only" would also be
useful so as an application layer can properly fail if there is only a
primary available.

JDBC has its own set of options with targetServerType: master, slave,
secondary, preferSlave and preferSecondary.  What's proposed here is
preferSlave and what we would miss is slave at the end.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

view thread (149+ messages)  latest in thread

Message-ID: <20181115081433.GF1374@paquier.xyz>
Permalink:  ../20181115081433.GF1374@paquier.xyz/
Also on:    postgresql.org/message-id/20181115081433.GF1374@paquier.xyz

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-hackers@postgresql.org
  Cc: michael@paquier.xyz, kommi.haribabu@gmail.com, tgl@sss.pgh.pa.us, robertmhaas@gmail.com, jingwangian@gmail.com, tsunakawa.takay@jp.fujitsu.com
  Subject: Re: Libpq support to connect to standby server as priority
  In-Reply-To: <20181115081433.GF1374@paquier.xyz>

* 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