mirror of
https://github.com/postgres/postgres.git
synced 2025-07-28 23:42:10 +03:00
Support connection load balancing in libpq
This adds support for load balancing connections with libpq using a connection parameter: load_balance_hosts=<string>. When setting the param to random, hosts and addresses will be connected to in random order. This then results in load balancing across these addresses and hosts when multiple clients or frequent connection setups are used. The randomization employed performs two levels of shuffling: 1. The given hosts are randomly shuffled, before resolving them one-by-one. 2. Once a host its addresses get resolved, the returned addresses are shuffled, before trying to connect to them one-by-one. Author: Jelte Fennema <postgres@jeltef.nl> Reviewed-by: Aleksander Alekseev <aleksander@timescale.com> Reviewed-by: Michael Banck <mbanck@gmx.net> Reviewed-by: Andrey Borodin <amborodin86@gmail.com> Discussion: https://postgr.es/m/PR3PR83MB04768E2FF04818EEB2179949F7A69@PR3PR83MB0476.EURPRD83.prod.outlook.
This commit is contained in:
@ -2115,6 +2115,67 @@ postgresql://%2Fvar%2Flib%2Fpostgresql/dbname
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
|
||||
<varlistentry id="libpq-connect-load-balance-hosts" xreflabel="load_balance_hosts">
|
||||
<term><literal>load_balance_hosts</literal></term>
|
||||
<listitem>
|
||||
<para>
|
||||
Controls the order in which the client tries to connect to the available
|
||||
hosts and addresses. Once a connection attempt is successful no other
|
||||
hosts and addresses will be tried. This parameter is typically used in
|
||||
combination with multiple host names or a DNS record that returns
|
||||
multiple IPs. This parameter can be used in combination with
|
||||
<xref linkend="libpq-connect-target-session-attrs"/>
|
||||
to, for example, load balance over standby servers only. Once successfully
|
||||
connected, subsequent queries on the returned connection will all be
|
||||
sent to the same server. There are currently two modes:
|
||||
<variablelist>
|
||||
<varlistentry>
|
||||
<term><literal>disable</literal> (default)</term>
|
||||
<listitem>
|
||||
<para>
|
||||
No load balancing across hosts is performed. Hosts are tried in
|
||||
the order in which they are provided and addresses are tried in
|
||||
the order they are received from DNS or a hosts file.
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
|
||||
<varlistentry>
|
||||
<term><literal>random</literal></term>
|
||||
<listitem>
|
||||
<para>
|
||||
Hosts and addresses are tried in random order. This value is mostly
|
||||
useful when opening multiple connections at the same time, possibly
|
||||
from different machines. This way connections can be load balanced
|
||||
across multiple <productname>PostgreSQL</productname> servers.
|
||||
</para>
|
||||
<para>
|
||||
While random load balancing, due to its random nature, will almost
|
||||
never result in a completely uniform distribution, it statistically
|
||||
gets quite close. One important aspect here is that this algorithm
|
||||
uses two levels of random choices: First the hosts
|
||||
will be resolved in random order. Then secondly, before resolving
|
||||
the next host, all resolved addresses for the current host will be
|
||||
tried in random order. This behaviour can skew the amount of
|
||||
connections each node gets greatly in certain cases, for instance
|
||||
when some hosts resolve to more addresses than others. But such a
|
||||
skew can also be used on purpose, e.g. to increase the number of
|
||||
connections a larger server gets by providing its hostname multiple
|
||||
times in the host string.
|
||||
</para>
|
||||
<para>
|
||||
When using this value it's recommended to also configure a reasonable
|
||||
value for <xref linkend="libpq-connect-connect-timeout"/>. Because then,
|
||||
if one of the nodes that are used for load balancing is not responding,
|
||||
a new node will be tried.
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
</variablelist>
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
</variablelist>
|
||||
</para>
|
||||
</sect2>
|
||||
|
@ -256,7 +256,7 @@ make check-world -j8 >/dev/null
|
||||
<varname>PG_TEST_EXTRA</varname> to a whitespace-separated list, for
|
||||
example:
|
||||
<programlisting>
|
||||
make check-world PG_TEST_EXTRA='kerberos ldap ssl'
|
||||
make check-world PG_TEST_EXTRA='kerberos ldap ssl load_balance'
|
||||
</programlisting>
|
||||
The following values are currently supported:
|
||||
<variablelist>
|
||||
@ -290,6 +290,17 @@ make check-world PG_TEST_EXTRA='kerberos ldap ssl'
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
|
||||
<varlistentry>
|
||||
<term><literal>load_balance</literal></term>
|
||||
<listitem>
|
||||
<para>
|
||||
Runs the test <filename>src/interfaces/libpq/t/004_load_balance_dns.pl</filename>.
|
||||
This requires editing the system <filename>hosts</filename> file and
|
||||
opens TCP/IP listen sockets.
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
|
||||
<varlistentry>
|
||||
<term><literal>wal_consistency_checking</literal></term>
|
||||
<listitem>
|
||||
|
Reference in New Issue
Block a user