Re: Libpq support to connect to standby server as priority

Started by Jing Wangabout 8 years ago145 messageshackers
Jump to latest
#1Jing Wang
jingwangian@gmail.com

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.

Regards,
Jing Wang
Fujitsu Australia

Attachments:

libpq_support_perfer-read_002.patchapplication/octet-stream; name=libpq_support_perfer-read_002.patchDownload+175-50
#2Haribabu Kommi
kommi.haribabu@gmail.com
In reply to: Jing Wang (#1)

On Wed, Jan 24, 2018 at 9:01 AM Jing Wang <jingwangian@gmail.com> 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.

Regards,
Haribabu Kommi
Fujitsu Australia

Attachments:

0001-Allow-target-session-attrs-to-accept-prefer-read-opti.patchapplication/octet-stream; name=0001-Allow-target-session-attrs-to-accept-prefer-read-opti.patchDownload+189-50
#3Laurenz Albe
laurenz.albe@cybertec.at
In reply to: Haribabu Kommi (#2)

Haribabu Kommi wrote:

On Wed, Jan 24, 2018 at 9:01 AM Jing Wang <jingwangian@gmail.com> 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 <literal>prefer-read</literal>, connections
where <literal>SHOW transaction_read_only</literal> 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

#4Haribabu Kommi
kommi.haribabu@gmail.com
In reply to: Laurenz Albe (#3)

On Wed, Jul 4, 2018 at 11:14 PM Laurenz Albe <laurenz.albe@cybertec.at>
wrote:

Haribabu Kommi wrote:

On Wed, Jan 24, 2018 at 9:01 AM Jing Wang <jingwangian@gmail.com> 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.

Thanks for the review.

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.

There was a problem in reusing the primary host index and it leads to loop.
Attached patch fixed the issue.

Concerning the code:

- The documentation needs some attention. Suggestion:

If this parameter is set to <literal>prefer-read</literal>, connections
where <literal>SHOW transaction_read_only</literal> returns off are
preferred.
If no such connection can be found, a connection that allows read-write
transactions will be accepted.

updated as per you comment.

- 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.

Even if we add a variable to cache the connection, I don't think the logic
of checking
the next host for the read-only host logic may not change, but the extra
connection
request to the read-write host again will be removed.

Regards,
Haribabu Kommi
Fujitsu Australia

Attachments:

0001-Allow-taget-session-attrs-to-accept-prefer-read-opti_v2.patchapplication/octet-stream; name=0001-Allow-taget-session-attrs-to-accept-prefer-read-opti_v2.patchDownload+190-50
#5Haribabu Kommi
kommi.haribabu@gmail.com
In reply to: Haribabu Kommi (#4)

On Wed, Jul 11, 2018 at 6:00 PM Haribabu Kommi <kommi.haribabu@gmail.com>
wrote:

On Wed, Jul 4, 2018 at 11:14 PM Laurenz Albe <laurenz.albe@cybertec.at>
wrote:

Haribabu Kommi wrote:

- 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.

Even if we add a variable to cache the connection, I don't think the logic
of checking
the next host for the read-only host logic may not change, but the extra
connection
request to the read-write host again will be removed.

I evaluated your suggestion of caching the connection and reuse it when
there is no
read only server doesn't find, but I am thinking that it will add more
complexity and also
the connection to the other servers delays, the cached connection may be
closed by
the server also because of timeout.

I feel the extra time during connection may be fine, if user is preferring
the prefer-read
mode, instead of adding more complexity in handling the cached connection?

comments?

Regards,
Haribabu Kommi
Fujitsu Australia

#6Laurenz Albe
laurenz.albe@cybertec.at
In reply to: Haribabu Kommi (#5)

Haribabu Kommi wrote:

On Wed, Jul 4, 2018 at 11:14 PM Laurenz Albe <laurenz.albe@cybertec.at> wrote:

Haribabu Kommi wrote:

- 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.

Even if we add a variable to cache the connection, I don't think the logic of checking
the next host for the read-only host logic may not change, but the extra connection
request to the read-write host again will be removed.

I evaluated your suggestion of caching the connection and reuse it when there is no
read only server doesn't find, but I am thinking that it will add more complexity and also
the connection to the other servers delays, the cached connection may be closed by
the server also because of timeout.

I feel the extra time during connection may be fine, if user is preferring the prefer-read
mode, instead of adding more complexity in handling the cached connection?

comments?

I tested the new patch, and it works as expected.

I don't think that time-out of the cached session is a valid concern, because that
would have to be a really short timeout.
On the other hand, establishing the connection twice (first to check if it is read-only,
then again because no read-only connection is found) can be quite costly.

But that is a matter of debate, as is the readability of the code.

Since I don't think I can contribute more to this patch, I'll mark it as
ready for committer.

Yours,
Laurenz Albe

#7Haribabu Kommi
kommi.haribabu@gmail.com
In reply to: Laurenz Albe (#6)

On Tue, Jul 17, 2018 at 12:42 AM Laurenz Albe <laurenz.albe@cybertec.at>
wrote:

Haribabu Kommi wrote:

On Wed, Jul 4, 2018 at 11:14 PM Laurenz Albe <laurenz.albe@cybertec.at>

wrote:

Haribabu Kommi wrote:

- 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.

Even if we add a variable to cache the connection, I don't think the

logic of checking

the next host for the read-only host logic may not change, but the

extra connection

request to the read-write host again will be removed.

I evaluated your suggestion of caching the connection and reuse it when

there is no

read only server doesn't find, but I am thinking that it will add more

complexity and also

the connection to the other servers delays, the cached connection may be

closed by

the server also because of timeout.

I feel the extra time during connection may be fine, if user is

preferring the prefer-read

mode, instead of adding more complexity in handling the cached

connection?

comments?

I tested the new patch, and it works as expected.

Thanks for the confirmation.

I don't think that time-out of the cached session is a valid concern,
because that
would have to be a really short timeout.
On the other hand, establishing the connection twice (first to check if it
is read-only,
then again because no read-only connection is found) can be quite costly.

But that is a matter of debate, as is the readability of the code.

Thanks for your opinion, let's wait for opinion from others also.
I can go for the modification, if others also find it useful.

Regards,
Haribabu Kommi
Fujitsu Australia

#8Robert Haas
robertmhaas@gmail.com
In reply to: Laurenz Albe (#3)

On Wed, Jul 4, 2018 at 9:14 AM, Laurenz Albe <laurenz.albe@cybertec.at> 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.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

#9Haribabu Kommi
kommi.haribabu@gmail.com
In reply to: Robert Haas (#8)

On Wed, Jul 18, 2018 at 10:53 PM Robert Haas <robertmhaas@gmail.com> wrote:

On Wed, Jul 4, 2018 at 9:14 AM, Laurenz Albe <laurenz.albe@cybertec.at>
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.

Regards,
Haribabu Kommi
Fujitsu Australia

#10Haribabu Kommi
kommi.haribabu@gmail.com
In reply to: Haribabu Kommi (#9)

On Thu, Jul 19, 2018 at 10:59 PM Haribabu Kommi <kommi.haribabu@gmail.com>
wrote:

On Wed, Jul 18, 2018 at 10:53 PM Robert Haas <robertmhaas@gmail.com>
wrote:

On Wed, Jul 4, 2018 at 9:14 AM, Laurenz Albe <laurenz.albe@cybertec.at>
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.

Regards,
Haribabu Kommi
Fujitsu Australia

Attachments:

0001-Allow-taget-session-attrs-to-accept-prefer-read-opti_v3.patchapplication/octet-stream; name=0001-Allow-taget-session-attrs-to-accept-prefer-read-opti_v3.patchDownload+152-27
#11Laurenz Albe
laurenz.albe@cybertec.at
In reply to: Haribabu Kommi (#10)

Haribabu Kommi wrote:

On Thu, Jul 19, 2018 at 10:59 PM Haribabu Kommi <kommi.haribabu@gmail.com> wrote:

On Wed, Jul 18, 2018 at 10:53 PM Robert Haas <robertmhaas@gmail.com> wrote:

On Wed, Jul 4, 2018 at 9:14 AM, Laurenz Albe <laurenz.albe@cybertec.at> 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

#12Haribabu Kommi
kommi.haribabu@gmail.com
In reply to: Haribabu Kommi (#10)

On Fri, Sep 28, 2018 at 5:31 PM Haribabu Kommi <kommi.haribabu@gmail.com>
wrote:

On Thu, Jul 19, 2018 at 10:59 PM Haribabu Kommi <kommi.haribabu@gmail.com>
wrote:

On Wed, Jul 18, 2018 at 10:53 PM Robert Haas <robertmhaas@gmail.com>
wrote:

On Wed, Jul 4, 2018 at 9:14 AM, Laurenz Albe <laurenz.albe@cybertec.at>
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.

[some how i didn't receive this mail, copy pasted from mailing list ]

The code is much more readable now, thanks.

Thanks for the review.

--- 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.

Added comments along the lines that you mentioned. And also try
to update some more comments.

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.

Yes, I didn't add that logic of keeping the first connection open, Currently
I feel that adds more complexity in supporting the same. If everyone feels
that is required, I will add that logic.

Updated patch attached.

Regards,
Haribabu Kommi
Fujitsu Australia

Attachments:

0001-Allow-taget-session-attrs-to-accept-prefer-read-opti_v4.patchapplication/octet-stream; name=0001-Allow-taget-session-attrs-to-accept-prefer-read-opti_v4.patchDownload+163-27
#13Laurenz Albe
laurenz.albe@cybertec.at
In reply to: Haribabu Kommi (#12)

Haribabu Kommi wrote:

Added comments along the lines that you mentioned. And also try
to update some more comments.

Looks ok to me, I'll mark it as "ready for committer".

Yours,
Laurenz Albe

#14Tom Lane
tgl@sss.pgh.pa.us
In reply to: Laurenz Albe (#13)

Laurenz Albe <laurenz.albe@cybertec.at> writes:

Haribabu Kommi wrote:

Added comments along the lines that you mentioned. And also try
to update some more comments.

Looks ok to me, I'll mark it as "ready for committer".

I don't like this patch at all: the business with keeping two connections
open seems impossibly fragile and full of race conditions. (For instance,
by the time you return the read-only session to the application, it might
not be read-only any more. I also wonder what inquiry functions like
PQsocket ought to return while in this state.) I think the feature
definition needs to be re-thought to make that unnecessary.

Also, we really need to consider the interaction between this and the
feature(s) being discussed in the thread at

/messages/by-id/1700970.cRWpxnom9y@hammer.magicstack.net

regards, tom lane

#15Laurenz Albe
laurenz.albe@cybertec.at
In reply to: Tom Lane (#14)

Tom Lane wrote:

Laurenz Albe <laurenz.albe@cybertec.at> writes:

Haribabu Kommi wrote:

Added comments along the lines that you mentioned. And also try
to update some more comments.

Looks ok to me, I'll mark it as "ready for committer".

I don't like this patch at all: the business with keeping two connections
open seems impossibly fragile and full of race conditions. (For instance,
by the time you return the read-only session to the application, it might
not be read-only any more. I also wonder what inquiry functions like
PQsocket ought to return while in this state.) I think the feature
definition needs to be re-thought to make that unnecessary.

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.

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*.
If you demand that the server be not promoted until the connection has
been returned to the client, you'd somehow have to block the server
from being promoted, right?

Also, we really need to consider the interaction between this and the
feature(s) being discussed in the thread at

/messages/by-id/1700970.cRWpxnom9y@hammer.magicstack.net

That's a good point.

Yours,
Laurenz Albe

#16Haribabu Kommi
kommi.haribabu@gmail.com
In reply to: Laurenz Albe (#15)

On Tue, Nov 13, 2018 at 7:26 AM Laurenz Albe <laurenz.albe@cybertec.at>
wrote:

Tom Lane wrote:

Laurenz Albe <laurenz.albe@cybertec.at> writes:

Haribabu Kommi wrote:

Added comments along the lines that you mentioned. And also try
to update some more comments.

Looks ok to me, I'll mark it as "ready for committer".

I don't like this patch at all: the business with keeping two connections
open seems impossibly fragile and full of race conditions. (For

instance,

by the time you return the read-only session to the application, it might
not be read-only any more. I also wonder what inquiry functions like
PQsocket ought to return while in this state.) I think the feature
definition needs to be re-thought to make that unnecessary.

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.

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*.
If you demand that the server be not promoted until the connection has
been returned to the client, you'd somehow have to block the server
from being promoted, right?

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.

Also, we really need to consider the interaction between this and the
feature(s) being discussed in the thread at

/messages/by-id/1700970.cRWpxnom9y@hammer.magicstack.net

That's a good point.

Thanks for the link. Based on the conclusion on the other thread of
GUC_REPORT,
this patch also can use that logic, but that is limited only till the
connection establishment
for these connection types.

Regards,
Haribabu Kommi
Fujitsu Australia

#17Michael Paquier
michael@paquier.xyz
In reply to: Haribabu Kommi (#16)

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

#18Michael Paquier
michael@paquier.xyz
In reply to: Michael Paquier (#17)

On Thu, Nov 15, 2018 at 05:14:33PM +0900, Michael Paquier wrote:

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.

So thinking a couple of extra minutes on this one, I am wondering if it
would be better to close completely the gap with two patches:
1) Get "read-only" done first, which uses most of the existing
infrastructure. That seems simple enough.
2) Get "prefer-read" and "prefer-write", which need some new
infrastructure to track the last preferred connection depending on what
the client wants.
--
Michael

#19Tom Lane
tgl@sss.pgh.pa.us
In reply to: Laurenz Albe (#15)

Laurenz Albe <laurenz.albe@cybertec.at> writes:

Tom Lane wrote:

I don't like this patch at all: the business with keeping two connections
open seems impossibly fragile and full of race conditions.

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.

Oh! The reason I assumed it wasn't doing that is that such a behavior
seems completely insane. If the point is to keep down the load on your
master server, then connecting only to immediately disconnect is not
a friendly way to do that --- even without counting the fact that you
might later come back and connect again.

If that's the best we can do, we should forget the whole feature and
just recommend putting slave servers first in your hosts list when
you want prefer-slave.

regards, tom lane

#20Laurenz Albe
laurenz.albe@cybertec.at
In reply to: Tom Lane (#19)

Tom Lane 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.

Oh! The reason I assumed it wasn't doing that is that such a behavior
seems completely insane. If the point is to keep down the load on your
master server, then connecting only to immediately disconnect is not
a friendly way to do that --- even without counting the fact that you
might later come back and connect again.

That's why I had argued initially to keep the session open, but you
seem to dislike that idea as well.

If that's the best we can do, we should forget the whole feature and
just recommend putting slave servers first in your hosts list when
you want prefer-slave.

If you know which is which, certainly.
But in a setup with automated failover you cannot be certain which is which.
That's what the proposed feature targets.

Yours,
Laurenz Albe

#21Haribabu Kommi
kommi.haribabu@gmail.com
In reply to: Laurenz Albe (#20)
#22Michael Paquier
michael@paquier.xyz
In reply to: Haribabu Kommi (#21)
#23Vladimir Sitnikov
sitnikov.vladimir@gmail.com
In reply to: Michael Paquier (#22)
#24Dave Cramer
pg@fastcrypt.com
In reply to: Vladimir Sitnikov (#23)
#25Robert Haas
robertmhaas@gmail.com
In reply to: Tom Lane (#19)
#26Dave Cramer
pg@fastcrypt.com
In reply to: Robert Haas (#25)
#27Tom Lane
tgl@sss.pgh.pa.us
In reply to: Dave Cramer (#26)
#28Tsunakawa, Takayuki
tsunakawa.takay@jp.fujitsu.com
In reply to: Tom Lane (#27)
#29Michael Paquier
michael@paquier.xyz
In reply to: Tsunakawa, Takayuki (#28)
#30Tom Lane
tgl@sss.pgh.pa.us
In reply to: Michael Paquier (#29)
#31Dave Cramer
pg@fastcrypt.com
In reply to: Tsunakawa, Takayuki (#28)
#32Robert Haas
robertmhaas@gmail.com
In reply to: Tom Lane (#27)
#33Tsunakawa, Takayuki
tsunakawa.takay@jp.fujitsu.com
In reply to: Tom Lane (#30)
#34Tsunakawa, Takayuki
tsunakawa.takay@jp.fujitsu.com
In reply to: Dave Cramer (#31)
#35Tatsuo Ishii
t-ishii@sra.co.jp
In reply to: Tsunakawa, Takayuki (#34)
#36Tsunakawa, Takayuki
tsunakawa.takay@jp.fujitsu.com
In reply to: Tatsuo Ishii (#35)
#37Tatsuo Ishii
t-ishii@sra.co.jp
In reply to: Tsunakawa, Takayuki (#36)
#38Tsunakawa, Takayuki
tsunakawa.takay@jp.fujitsu.com
In reply to: Tatsuo Ishii (#37)
#39Laurenz Albe
laurenz.albe@cybertec.at
In reply to: Tsunakawa, Takayuki (#28)
#40Dave Cramer
pg@fastcrypt.com
In reply to: Tsunakawa, Takayuki (#34)
#41Dave Cramer
pg@fastcrypt.com
In reply to: Tatsuo Ishii (#37)
#42Dave Cramer
pg@fastcrypt.com
In reply to: Laurenz Albe (#39)
#43Tatsuo Ishii
t-ishii@sra.co.jp
In reply to: Dave Cramer (#41)
#44Dave Cramer
pg@fastcrypt.com
In reply to: Tatsuo Ishii (#43)
#45Tsunakawa, Takayuki
tsunakawa.takay@jp.fujitsu.com
In reply to: Dave Cramer (#44)
#46Tatsuo Ishii
t-ishii@sra.co.jp
In reply to: Dave Cramer (#44)
#47Dave Cramer
pg@fastcrypt.com
In reply to: Tatsuo Ishii (#46)
#48Dave Cramer
pg@fastcrypt.com
In reply to: Tsunakawa, Takayuki (#45)
#49Tatsuo Ishii
t-ishii@sra.co.jp
In reply to: Dave Cramer (#47)
#50Dave Cramer
pg@fastcrypt.com
In reply to: Tatsuo Ishii (#49)
#51Tatsuo Ishii
t-ishii@sra.co.jp
In reply to: Dave Cramer (#50)
#52Dave Cramer
pg@fastcrypt.com
In reply to: Tatsuo Ishii (#51)
#53Tsunakawa, Takayuki
tsunakawa.takay@jp.fujitsu.com
In reply to: Laurenz Albe (#39)
#54Haribabu Kommi
kommi.haribabu@gmail.com
In reply to: Tsunakawa, Takayuki (#53)
#55Tsunakawa, Takayuki
tsunakawa.takay@jp.fujitsu.com
In reply to: Haribabu Kommi (#54)
#56Tatsuo Ishii
t-ishii@sra.co.jp
In reply to: Dave Cramer (#52)
#57Tsunakawa, Takayuki
tsunakawa.takay@jp.fujitsu.com
In reply to: Tsunakawa, Takayuki (#55)
#58Laurenz Albe
laurenz.albe@cybertec.at
In reply to: Tsunakawa, Takayuki (#53)
#59Haribabu Kommi
kommi.haribabu@gmail.com
In reply to: Tsunakawa, Takayuki (#57)
#60Tsunakawa, Takayuki
tsunakawa.takay@jp.fujitsu.com
In reply to: Laurenz Albe (#58)
#61Tsunakawa, Takayuki
tsunakawa.takay@jp.fujitsu.com
In reply to: Haribabu Kommi (#59)
#62Michael Paquier
michael@paquier.xyz
In reply to: Tsunakawa, Takayuki (#61)
#63Haribabu Kommi
kommi.haribabu@gmail.com
In reply to: Tsunakawa, Takayuki (#61)
#64Tsunakawa, Takayuki
tsunakawa.takay@jp.fujitsu.com
In reply to: Haribabu Kommi (#63)
#65Haribabu Kommi
kommi.haribabu@gmail.com
In reply to: Tsunakawa, Takayuki (#64)
#66Tsunakawa, Takayuki
tsunakawa.takay@jp.fujitsu.com
In reply to: Haribabu Kommi (#65)
#67Haribabu Kommi
kommi.haribabu@gmail.com
In reply to: Tsunakawa, Takayuki (#66)
#68Tsunakawa, Takayuki
tsunakawa.takay@jp.fujitsu.com
In reply to: Haribabu Kommi (#67)
#69Haribabu Kommi
kommi.haribabu@gmail.com
In reply to: Tsunakawa, Takayuki (#68)
#70Tsunakawa, Takayuki
tsunakawa.takay@jp.fujitsu.com
In reply to: Haribabu Kommi (#67)
#71Haribabu Kommi
kommi.haribabu@gmail.com
In reply to: Tsunakawa, Takayuki (#70)
#72Tsunakawa, Takayuki
tsunakawa.takay@jp.fujitsu.com
In reply to: Haribabu Kommi (#71)
#73Dave Cramer
pg@fastcrypt.com
In reply to: Haribabu Kommi (#67)
#74Haribabu Kommi
kommi.haribabu@gmail.com
In reply to: Tsunakawa, Takayuki (#72)
#75Tsunakawa, Takayuki
tsunakawa.takay@jp.fujitsu.com
In reply to: Haribabu Kommi (#74)
#76Robert Haas
robertmhaas@gmail.com
In reply to: Haribabu Kommi (#74)
#77Tsunakawa, Takayuki
tsunakawa.takay@jp.fujitsu.com
In reply to: Robert Haas (#76)
#78Haribabu Kommi
kommi.haribabu@gmail.com
In reply to: Tsunakawa, Takayuki (#77)
#79Robert Haas
robertmhaas@gmail.com
In reply to: Haribabu Kommi (#78)
#80Haribabu Kommi
kommi.haribabu@gmail.com
In reply to: Robert Haas (#79)
#81Haribabu Kommi
kommi.haribabu@gmail.com
In reply to: Haribabu Kommi (#80)
#82Haribabu Kommi
kommi.haribabu@gmail.com
In reply to: Haribabu Kommi (#81)
#83Tsunakawa, Takayuki
tsunakawa.takay@jp.fujitsu.com
In reply to: Haribabu Kommi (#82)
#84Tsunakawa, Takayuki
tsunakawa.takay@jp.fujitsu.com
In reply to: Tsunakawa, Takayuki (#83)
#85Haribabu Kommi
kommi.haribabu@gmail.com
In reply to: Tsunakawa, Takayuki (#84)
#86Tsunakawa, Takayuki
tsunakawa.takay@jp.fujitsu.com
In reply to: Haribabu Kommi (#85)
#87Haribabu Kommi
kommi.haribabu@gmail.com
In reply to: Tsunakawa, Takayuki (#86)
#88Haribabu Kommi
kommi.haribabu@gmail.com
In reply to: Haribabu Kommi (#87)
#89Haribabu Kommi
kommi.haribabu@gmail.com
In reply to: Haribabu Kommi (#88)
#90Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Haribabu Kommi (#89)
#91Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Alvaro Herrera (#90)
#92Tsunakawa, Takayuki
tsunakawa.takay@jp.fujitsu.com
In reply to: Alvaro Herrera (#91)
#93Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Tsunakawa, Takayuki (#92)
#94Greg Nancarrow
gregn4422@gmail.com
In reply to: Alvaro Herrera (#93)
#95Greg Nancarrow
gregn4422@gmail.com
In reply to: Alvaro Herrera (#93)
#96tsunakawa.takay@fujitsu.com
tsunakawa.takay@fujitsu.com
In reply to: Greg Nancarrow (#95)
#97Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Greg Nancarrow (#95)
#98Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Alvaro Herrera (#97)
#99Dave Cramer
pg@fastcrypt.com
In reply to: Alvaro Herrera (#97)
#100Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Dave Cramer (#99)
#101tsunakawa.takay@fujitsu.com
tsunakawa.takay@fujitsu.com
In reply to: Alvaro Herrera (#97)
#102Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: tsunakawa.takay@fujitsu.com (#101)
#103tsunakawa.takay@fujitsu.com
tsunakawa.takay@fujitsu.com
In reply to: Alvaro Herrera (#102)
#104Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: tsunakawa.takay@fujitsu.com (#103)
#105Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Alvaro Herrera (#104)
#106David Steele
david@pgmasters.net
In reply to: Alvaro Herrera (#105)
#107Greg Nancarrow
gregn4422@gmail.com
In reply to: David Steele (#106)
#108Daniel Gustafsson
daniel@yesql.se
In reply to: Greg Nancarrow (#107)
#109Greg Nancarrow
gregn4422@gmail.com
In reply to: Daniel Gustafsson (#108)
#110Daniel Gustafsson
daniel@yesql.se
In reply to: Greg Nancarrow (#109)
#111Greg Nancarrow
gregn4422@gmail.com
In reply to: Daniel Gustafsson (#110)
#112Smith, Peter
peters@fast.au.fujitsu.com
In reply to: Greg Nancarrow (#111)
#113Smith, Peter
peters@fast.au.fujitsu.com
In reply to: Greg Nancarrow (#111)
#114Robert Haas
robertmhaas@gmail.com
In reply to: Alvaro Herrera (#102)
#115tsunakawa.takay@fujitsu.com
tsunakawa.takay@fujitsu.com
In reply to: Robert Haas (#114)
#116Greg Nancarrow
gregn4422@gmail.com
In reply to: Smith, Peter (#113)
#117Peter Smith
smithpb2250@gmail.com
In reply to: Greg Nancarrow (#116)
#118Greg Nancarrow
gregn4422@gmail.com
In reply to: Peter Smith (#117)
#119Peter Smith
smithpb2250@gmail.com
In reply to: Greg Nancarrow (#118)
#120Tom Lane
tgl@sss.pgh.pa.us
In reply to: Greg Nancarrow (#118)
#121Tom Lane
tgl@sss.pgh.pa.us
In reply to: Tom Lane (#120)
#122Greg Nancarrow
gregn4422@gmail.com
In reply to: Tom Lane (#121)
#123Anastasia Lubennikova
a.lubennikova@postgrespro.ru
In reply to: Greg Nancarrow (#122)
#124Tom Lane
tgl@sss.pgh.pa.us
In reply to: Anastasia Lubennikova (#123)
#125Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Tom Lane (#124)
#126Tom Lane
tgl@sss.pgh.pa.us
In reply to: Alvaro Herrera (#125)
#127Tom Lane
tgl@sss.pgh.pa.us
In reply to: Tom Lane (#126)
#128Greg Nancarrow
gregn4422@gmail.com
In reply to: Tom Lane (#127)
#129Tom Lane
tgl@sss.pgh.pa.us
In reply to: Greg Nancarrow (#128)
#130Greg Nancarrow
gregn4422@gmail.com
In reply to: Tom Lane (#129)
#131Greg Nancarrow
gregn4422@gmail.com
In reply to: Greg Nancarrow (#130)
#132Tom Lane
tgl@sss.pgh.pa.us
In reply to: Greg Nancarrow (#131)
#133vignesh C
vignesh21@gmail.com
In reply to: Tom Lane (#132)
#134Greg Nancarrow
gregn4422@gmail.com
In reply to: vignesh C (#133)
#135Greg Nancarrow
gregn4422@gmail.com
In reply to: vignesh C (#133)
#136vignesh C
vignesh21@gmail.com
In reply to: Greg Nancarrow (#134)
#137vignesh C
vignesh21@gmail.com
In reply to: Greg Nancarrow (#135)
#138Greg Nancarrow
gregn4422@gmail.com
In reply to: vignesh C (#137)
#139vignesh C
vignesh21@gmail.com
In reply to: Greg Nancarrow (#138)
#140Greg Nancarrow
gregn4422@gmail.com
In reply to: vignesh C (#139)
#141Tom Lane
tgl@sss.pgh.pa.us
In reply to: Greg Nancarrow (#140)
#142vignesh C
vignesh21@gmail.com
In reply to: Tom Lane (#141)
#143Greg Nancarrow
gregn4422@gmail.com
In reply to: vignesh C (#142)
#144Tom Lane
tgl@sss.pgh.pa.us
In reply to: vignesh C (#142)
#145Tom Lane
tgl@sss.pgh.pa.us
In reply to: Tom Lane (#144)