↑↓ 选择 ↵ 打开 ⌫ 改范围 完整检索页

pgsql.cc 提供对 postgresql.org 官网内容的中文翻译,由 Pigsty 团队维护。

百科 / 连接参数 / 连接目标

load_balance_hosts

控制客户端尝试连接可用主机和地址的顺序。一旦某次连接尝试成功,就不会再尝试其他主机和地址。该参数通常与多个主机名或返回多个 IP 地址的 DNS 记录一起使用。它还可以与 target_session_attrs 组合使用,例如只在备库之间进行负载均衡。连接一旦成功建立,随后在返回的连接上发出的所有查询都会发送到同一台服务器。目前有两种模式:

当前查看 PostgreSQL 18.6。

说明

控制客户端尝试连接可用主机和地址的顺序。一旦某次连接尝试成功,就不会再尝试其他主机和地址。该参数通常与多个主机名或返回多个 IP 地址的 DNS 记录一起使用。它还可以与 target_session_attrs 组合使用,例如只在备库之间进行负载均衡。连接一旦成功建立,随后在返回的连接上发出的所有查询都会发送到同一台服务器。目前有两种模式:

客户端库
libpq 18.6
手册定义
手册已记载
源码中的环境变量回退
PGLOADBALANCEHOSTS
编译时回退表达式
DefaultLoadBalanceHosts

用法

load_balance_hosts

默认值解析与服务文件优先级

以下环境变量可用于选择连接参数的默认值,供 PQconnectdb 、 PQsetdbLogin 和 PQsetdb 在调用代码未直接指定参数值时使用。例如,这样可以避免在简单的客户端应用程序中硬编码数据库连接信息。

服务名称可以在每个用户的服务文件或系统范围的文件中定义。如果同一个服务名称存在于用户文件和系统文件中,则用户文件优先。默认情况下,每个用户的服务文件名为 ~/.pg_service.conf 。在Microsoft Windows上,它的名称为 %APPDATA%\postgresql\.pg_service.conf (其中 %APPDATA% 指用户配置文件夹中的应用数据子目录)。可以通过设置环境变量 PGSERVICEFILE 来指定不同的文件名。系统范围的文件名为 pg_service.conf 。默认情况下,在 PostgreSQL 安装的 etc 目录中寻找(使用 pg_config --sysconfdir 来准确识别此目录)。可以通过设置环境变量 PGSYSCONFDIR 来指定另一个目录,但不能指定不同的文件名。

从服务文件中获取的连接参数会与其他来源的参数合并。服务文件中的设置会覆盖相应的环境变量,而连接字符串中直接给出的值又会覆盖服务文件中的设置。例如,使用上述服务文件时,连接字符串 service=mydb port=5434 将使用主机 somehost 、端口 5434 、用户 admin ,以及由环境变量或内置默认值设置的其他参数。

环境变量证据

PGLOADBALANCEHOSTS 的行为与 load_balance_hosts 连接参数相同。

环境变量回退

变量手册记载的行为
PGLOADBALANCEHOSTSPGLOADBALANCEHOSTS 的行为与 load_balance_hosts 连接参数相同。

手册定义

load_balance_hosts

控制客户端尝试连接可用主机和地址的顺序。一旦某次连接尝试成功,就不会再尝试其他主机和地址。该参数通常与多个主机名或返回多个 IP 地址的 DNS 记录一起使用。它还可以与target_session_attrs组合使用,例如只在备库之间进行负载均衡。连接一旦成功建立,随后在返回的连接上发出的所有查询都会发送到同一台服务器。目前有两种模式:

disable(默认)

不在主机之间执行负载均衡。主机会按提供的顺序进行尝试,地址会按从 DNS 或 hosts 文件获得的顺序进行尝试。

random

按随机顺序尝试主机和地址。这个值主要适用于同时打开多个连接的场景,甚至这些连接来自不同机器。这样就可以把连接负载分散到多个PostgreSQL服务器上。

虽然随机负载均衡由于其随机性几乎不会得到完全均匀的分布,但统计上会相当接近。这里有一个重要点:该算法使用两级随机选择。首先,主机会按随机顺序解析。其次,在解析下一个主机之前,会按随机顺序尝试当前主机解析得到的全部地址。在某些情况下,这种行为会使各节点获得的连接数量明显倾斜,例如某些主机解析出的地址比其他主机更多时。但这种倾斜也可以被有意利用,例如通过在主机字符串中多次提供某台更大服务器的主机名,来增加它获得的连接数量。

使用这个值时,建议同时为connect_timeout配置一个合理的值。这样,如果某个参与负载均衡的节点没有响应,就会继续尝试新的节点。

相关条目

文档与源码

来源构建
版本
18.6
构建
https://ftp.postgresql.org/pub/source/v18.6/postgresql-18.6.tar.bz2
来源指纹
ee8d1a3612338fd9adf250730cb640fcc5233b5491337cc00a316a44e3a0b9f8

版本比较

PostgreSQL 15 → 16: 新增收录。

以下差异保留原始字段名与英文源描述。

--- PostgreSQL 15
+++ PostgreSQL 16
@@ -1 +1,8 @@
-该版未收录
+{
+  "compiled_default_expression": "DefaultLoadBalanceHosts",
+  "default_evidence": [],
+  "definition": "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 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: disable (default) 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. random 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 PostgreSQL servers. 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. When using this value it's recommended to also configure a reasonable value for connect_timeout . Because then, if one of the nodes that are used for load balancing is not responding, a new node will be tried.",
+  "documented": true,
+  "environment": "PGLOADBALANCEHOSTS",
+  "keyword": "load_balance_hosts"
+}

比较已记录的接口与属性,排除来源指纹和构建元数据。某个样本中没有记录,不能据此判断实际引入或移除的版本。

相关条目

导出 JSON · 返回连接参数 · 收录范围为 PostgreSQL 16 至 20;最早采样版本不一定是实际引入版本。