Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e2555f9

Browse files
committed
Add hostorder and failover_timeout parameters to libpq; also provide backward compatibility with PGPROEE 9.6
1 parent 7edbcce commit e2555f9

File tree

7 files changed

+877
-100
lines changed

7 files changed

+877
-100
lines changed

doc/src/sgml/libpq.sgml

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ postgresql://localhost/mydb
844844
postgresql://user@localhost
845845
postgresql://user:secret@localhost
846846
postgresql://other@localhost/otherdb?connect_timeout=10&application_name=myapp
847-
postgresql://host1:123,host2:456/somedb?target_session_attrs=any&application_name=myapp
847+
postgresql://host1:123,host2:456/somedb?hostorder=random&target_session_attrs=any&application_name=myapp
848848
</programlisting>
849849
Components of the hierarchical part of the <acronym>URI</acronym> can also
850850
be given as parameters. For example:
@@ -909,14 +909,15 @@ postgresql://%2Fvar%2Flib%2Fpostgresql/dbname
909909
<title>Specifying Multiple Hosts</title>
910910

911911
<para>
912-
It is possible to specify multiple hosts to connect to, so that they are
913-
tried in the given order. In the Keyword/Value format, the <literal>host</>,
914-
<literal>hostaddr</>, and <literal>port</> options accept a comma-separated
915-
list of values. The same number of elements must be given in each option, such
916-
that e.g. the first <literal>hostaddr</> corresponds to the first host name,
917-
the second <literal>hostaddr</> corresponds to the second host name, and so
918-
forth. As an exception, if only one <literal>port</literal> is specified, it
919-
applies to all the hosts.
912+
It is possible to specify multiple hosts to connect to. In the Keyword/Value
913+
format, the <literal>host</>, <literal>hostaddr</>, and <literal>port</>
914+
options accept a comma-separated list of values. The same number of
915+
elements must be given in each option, such that e.g. the first
916+
<literal>hostaddr</> corresponds to the first host name, the second
917+
<literal>hostaddr</> corresponds to the second host name, and so
918+
forth. As an exception, if only one <literal>port</literal> is specified,
919+
it applies to all the hosts. The order in which hosts are tried may be
920+
specified by <xref linkend="libpq-connect-hostorder"> parameter.
920921
</para>
921922

922923
<para>
@@ -967,8 +968,7 @@ postgresql://%2Fvar%2Flib%2Fpostgresql/dbname
967968
Unix-domain sockets, the default is to connect to <literal>localhost</>.
968969
</para>
969970
<para>
970-
A comma-separated list of host names is also accepted, in which case
971-
each host name in the list is tried in order. See
971+
A comma-separated list of host names is also accepted. See
972972
<xref linkend="libpq-multiple-hosts"> for details.
973973
</para>
974974
</listitem>
@@ -1038,6 +1038,30 @@ postgresql://%2Fvar%2Flib%2Fpostgresql/dbname
10381038
</listitem>
10391039
</varlistentry>
10401040

1041+
<varlistentry id="libpq-connect-hostorder" xreflabel="hostorder">
1042+
<term><literal>hostorder</literal></term>
1043+
<listitem>
1044+
<para>
1045+
Specifies the order in which hosts from the list of hosts
1046+
specified by the <xref linkend="libpq-connect-host"> parameter are
1047+
tried.
1048+
</para>
1049+
<para>
1050+
If the value of this argument is <literal>sequential</literal> (the
1051+
default) connections to the hosts will be attempted in the order in
1052+
which they are given.
1053+
</para>
1054+
<para>
1055+
If the value is <literal>random</literal>, the host to connect to
1056+
will be randomly picked from the list. It allows load balacing between
1057+
several cluster nodes. However, PostgreSQL doesn't currently support
1058+
multimaster clusters. So, without the use of third-party products,
1059+
only read-only connections can take advantage from load-balancing.
1060+
See <xref linkend="libpq-connect-target-session-attrs">
1061+
</para>
1062+
</listitem>
1063+
</varlistentry>
1064+
10411065
<varlistentry id="libpq-connect-port" xreflabel="port">
10421066
<term><literal>port</literal></term>
10431067
<listitem>
@@ -1114,6 +1138,29 @@ postgresql://%2Fvar%2Flib%2Fpostgresql/dbname
11141138
</listitem>
11151139
</varlistentry>
11161140

1141+
<varlistentry id="libpq-connect-falover-timeout" xreflabel="failover_timeout">
1142+
<term><literal>failover_timeout</literal></term>
1143+
<listitem>
1144+
<para>
1145+
Maximum time to cyclically retry all the hosts in the connection string.
1146+
(as decimal integer number of seconds). If not specified, then
1147+
hosts are tried just once.
1148+
</para>
1149+
<para>
1150+
If we have replicating cluster, and master node fails, it might
1151+
take some time to promote one of the standby nodes to the new master.
1152+
So clients which detect failure to connect to the master might
1153+
abandon attempts to reestablish a connection before the new master
1154+
becomes available.
1155+
</para>
1156+
<para>
1157+
Setting this parameter to a value that takes into account the amount of
1158+
time needed for failover to complete will ensure attempts to connect
1159+
to hosts continue to be made until the new master becomes available.
1160+
</para>
1161+
</listitem>
1162+
</varlistentry>
1163+
11171164
<varlistentry id="libpq-connect-client-encoding" xreflabel="client_encoding">
11181165
<term><literal>client_encoding</literal></term>
11191166
<listitem>
@@ -1498,13 +1545,14 @@ postgresql://%2Fvar%2Flib%2Fpostgresql/dbname
14981545

14991546
<varlistentry id="libpq-connect-target-session-attrs" xreflabel="target_session_attrs">
15001547
<term><literal>target_session_attrs</literal></term>
1548+
<term><literal>target_server_type (synonym)</literal></term>
15011549
<listitem>
15021550
<para>
1503-
If this parameter is set to <literal>read-write</literal>, only a
1504-
connection in which read-write transactions are accepted by default
1505-
is considered acceptable. The query
1506-
<literal>SHOW transaction_read_only</literal> will be sent upon any
1507-
successful connection; if it returns <literal>on</>, the connection
1551+
If this parameter is set to <literal>read-write</literal> (or its
1552+
equivalent <literal>master</literal>, only a connection in which
1553+
read-write transactions are accepted by default is considered acceptable.
1554+
The query <literal>SHOW transaction_read_only</literal> will be sent upon
1555+
any successful connection; if it returns <literal>on</>, the connection
15081556
will be closed. If multiple hosts were specified in the connection
15091557
string, any remaining servers will be tried just as if the connection
15101558
attempt had failed. The default value of this parameter,

src/interfaces/libpq/Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ OBJS= fe-auth.o fe-auth-scram.o fe-connect.o fe-exec.o fe-misc.o fe-print.o fe-l
3535
fe-protocol2.o fe-protocol3.o pqexpbuffer.o fe-secure.o \
3636
libpq-events.o
3737
# libpgport C files we always use
38-
OBJS += chklocale.o inet_net_ntop.o noblock.o pgstrcasecmp.o pqsignal.o \
39-
thread.o
38+
OBJS += chklocale.o inet_net_ntop.o noblock.o pgsleep.o pgstrcasecmp.o \
39+
pqsignal.o thread.o
4040
# libpgport C files that are needed if identified by configure
4141
OBJS += $(filter crypt.o getaddrinfo.o getpeereid.o inet_aton.o open.o system.o snprintf.o strerror.o strlcpy.o win32error.o win32setlocale.o, $(LIBOBJS))
4242

@@ -148,6 +148,9 @@ install: all installdirs install-lib
148148
installcheck:
149149
$(MAKE) -C test $@
150150

151+
check:
152+
$(prove_check)
153+
151154
installdirs: installdirs-lib
152155
$(MKDIR_P) '$(DESTDIR)$(includedir)' '$(DESTDIR)$(includedir_internal)' '$(DESTDIR)$(datadir)'
153156

src/interfaces/libpq/fe-auth.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
#include "fe-auth.h"
4646

4747

48+
#define CURRENT_HOST(conn) \
49+
((conn)->connhost[(conn)->connaddr[(conn)->whichaddr].hostidx])
50+
4851
#ifdef ENABLE_GSS
4952
/*
5053
* GSSAPI authentication system.
@@ -538,7 +541,7 @@ pg_SASL_init(PGconn *conn, int payloadlen)
538541
char *password;
539542

540543
conn->password_needed = true;
541-
password = conn->connhost[conn->whichhost].password;
544+
password = CURRENT_HOST(conn).password;
542545
if (password == NULL)
543546
password = conn->pgpass;
544547
if (password == NULL || password[0] == '\0')
@@ -949,7 +952,7 @@ pg_fe_sendauth(AuthRequest areq, int payloadlen, PGconn *conn)
949952
char *password;
950953

951954
conn->password_needed = true;
952-
password = conn->connhost[conn->whichhost].password;
955+
password = CURRENT_HOST(conn).password;
953956
if (password == NULL)
954957
password = conn->pgpass;
955958
if (password == NULL || password[0] == '\0')

0 commit comments

Comments
 (0)