searchusermenu
点赞
收藏
评论
分享
原创

pg_pool连接池参数详解

2024-07-22 08:51:16
498
0

1.PG-Pool连接池参数

PG-Pool支持的连接池参数比较多,可以根据以下说明进行配置。具体含义可以参考下图:

  1. num_init_children:该参数定义了 pgpool 启动时创建的子进程数量,默认值是 32。该参数也是客户端到 pgpool 的并发连接限制,大于该参数的客户端连接在 backlog 监听队列 (listen queue) 中阻塞等待,该队列大小为 listen_backlog_multiplier * num_init_children。如果监听队列大小大于操作系统默认值 net.core.somaxconn 需要修改该值。
  2. listen_backlog_multiplier:该参数用于调整从客户端到 pgpool 的监听队列大小,默认值是 2,监听队列大小为 listen_backlog_multiplier * num_init_children。
  3. reserved_connections:该参数用于限制从客户端到 pgpool 的监听队列过长,避免在客户端连接规模很大时的响应时间过长导致系统不稳定情况,默认值是 0。该参数设置为 0 时表示不拒绝任何客户端连接;当该参数值大于 0 时,pgpool 不会将超过并发连接限制(num_init_children - reserved_connections)的客户端连接加入到监听队列,而是直接拒绝该连接。
  4. connection_cache:该参数用于指定是否开启到后端数据库的连接缓存,默认为开启 (on)。即使该参数设置为 on,到 template0, template1, postgres 和 regression 这些数据库的连接也不进行缓存。
  5. max_pool【后端】:该参数用于指定每个 pgpool 子进程可以缓存到后端的连接数量,默认值是 4。如果到后端的连接与缓存中的连接 user, database 以及运行时参数相同时,直接复用该缓存的连接;没有命中缓存时,则需要新创建到后端的连接,并在缓存数量大于 max_pool 时剔除最旧的连接。

到每个后端数据库节点最多连接数为 max_pool*num_init_children,所以还需要关注 PG 的两个配置参数

  • max_connections :该参数用于指定 PG 数据库的最大并发连接数,默认值是 100, 表示最多可以同时处理100 个连接。
  • superuser_reserved_connections:该参数用于指定为 PG 超级用户连接而保留的连接槽数,默认值是 3 ,且该值必须小于 max_connections。PG 活跃的并发连接数最多为 max_connections - superuser_reserved_connections,如果超过该值,新的连接就只能由超级用户发起,并且不会有新的复制连接被接受。
  1. child_life_time:该参数用于指定子进程的生命周期,生命周期开始于上一次断开客户端连接,以秒为单位,默认值是 300 即 5 分钟,如果该值为 0 则表示禁用该功能。当子进程的生命周期超过这个值时,pgpool 将终止该子进程并创建一个新的子进程,防止内存泄漏。
  2. client_idle_limit:该参数指定了客户端连接的空闲时间限制,以秒为单位,默认值是 0,表示不启用空闲连接关闭。如果一个客户端连接在指定的时间内没有活动,pgpool 将关闭该连接以释放资源
  3. child_max_connections:该参数用于指定每个子进程在生命周期内可以处理的最大客户端连接数,默认值是 0,表示没有限制。当一个子进程的连接数达到这个值时,pgpool 将关闭这个子进程,并且创建一个新的子进程来替换它。这个参数通常使用在 child_life_time 和 connection_life_time 几乎不会被触发的繁忙场景,在生命周期内使用连接数而非时间来提前结束子进程生命周期。
  4. connection_life_time【后端】:该参数用于指定到后端数据库连接的最大生命周期,即连接缓存过期时间,以秒为单位,默认值是 0,表示不启用连接的生命周期限制。如果一个后端连接的生命周期超过这个值,pgpool 将关闭该连接。

2.PG-Pool连接模式

PG-Pool支持静态模式和动态模式两种进程管理模式 (process_management_mode)。

  • static(静态模式):pgpool 的子进程数固定且由 num_init_children 参数指定,所有子进程在 pgpool 启动时预先创建。
  • dynamic(动态模式):pgpool 的子进程数根据 min_spare_children 和 max_spare_children 两个参数动态调整,pgpool 启动时预先创建 max_spare_children 指定的子进程数,子进程数最高可达 num_init_children。

由于静态模式配置完成后连接数就是固定的,可能会有进程浪费和连接个数不够等问题,本文下面主要介绍动态连接模式。

dynamic 模式中 min_spare_children 和 max_spare_children 用于管理 WAIT_FOR_CONNECT 状态的子进程。启动时,根据 max_spare_children 预先创建空闲子进程数(而非 static 模式中根据 num_init_children 创建)。处理客户端连接过程中,当空闲子进程数少于 min_spare_children 时 fork 新的空闲子进程以满足最小空闲子进程数要求;当空闲子进程数大于 max_spare_children 时 kill 空闲子进程以满足最大空闲子进程数要求。

  1. process_management_strategy(进程管理策略):该参数用于指定 pgpool 在动态模式下如何管理子进程,用户管理调整频率它有以下几个可选值,默认值是 gentle
  • lazy:空闲子进程数保持超过 max_spare_children 时间大于 5 分钟,触发逐步执行缩减。
  • gentle:空闲子进程数保持超过 max_spare_children 时间大于 2 分钟,触发逐步执行缩减。
  • aggressive:空闲子进程数保持超过 max_spare_children 时间大于 50 秒,更频繁地触发执行缩减,且在选择被关闭子进程时不考虑子进程到后端的连接数(后端连接越少的越优先关闭)。
  1. min_spare_children(最小空闲子进程数):该参数用于指定在动态模式下,pgpool 应该保持的最小空闲子进程数。
  • 当空闲子进程数低于该值时,pgpool 会 fork 新的子进程以满足 min_spare_children;每次触发 fork 的空闲子进程数为: new_spawn_no = min_spare_children - idle_children + max_spare_children/4,其中 idle_children 表示当前空闲子进程数。
  • 如果当前总子进程数已经达到 num_init_children,则不再创建新的子进程;
  • 如果开启了 child_life_time 功能,所有子进程生命周期结束后仍会保留 min_spare_children 所指定的空闲子进程数。
  1. max_spare_children(最大空闲子进程数):该参数用于指定在动态模式下,pgpool 允许保持的最大空闲子进程数。
  • 空闲子进程数高于该值时,pgpool 会 kill 多余的空闲子进程以达到 max_spare_children 所指定的空闲子进程数;每次触发 kill 的空闲子进程数为:kill_count = idle_children - max_spare_children,其中 idle_children 表示当前空闲子进程数。
  • max_spare_children 大于 num_init_children 时,该参数失效。

动态调整模式下,连接池数位参数关系如下图:


3.PG-Pool配置和PG参数关系

在为Postgresql配置PG-Pool时,连接数配置要考虑后端Postgresql,具体连接数关系如下:

Pool的配置max_pool,num_init_children

PG的配置max_connections 和 superuser_reserved_connections 必须符合以下规则:

max_pool*num_init_children <= (max_connections - superuser_reserved_connections) (不需要取消查询) 
max_pool*num_init_children*2 <= (max_connections - superuser_reserved_connections) (需要取消查询) 

修改pg和Pool的参数都需要重启

4.PG-Pool配置实践

配置示例

num_init_children = 3000
max_pool = 4
listen_backlog_multiplier = 2
reserved_connections = 8
connection_cache = 'on'
child_life_time = 300
child_max_connections = 0
connection_life_time = 0
client_idle_limit = 30min
process_management_mode = dynamic
process_management_strategy = gentle
min_spare_children = 32
max_spare_children = 64

reference:

https://www.pgpool.net/docs/44/en/html/runtime-config-connection.html#RUNTIME-CONFIG-CONNECTION-SETTINGS

https://www.pgpool.net/docs/latest/en/html/runtime-config-connection-pooling.html

https://www.pgpool.net/docs/latest/en/html/runtime-config-process-management.html

https://www.sraoss.co.jp/wp-content/uploads/files/event_seminar/material/2022/pgpool-4.4.pdf

0条评论
作者已关闭评论
胡****研
1文章数
0粉丝数
胡****研
1 文章 | 0 粉丝
胡****研
1文章数
0粉丝数
胡****研
1 文章 | 0 粉丝
原创

pg_pool连接池参数详解

2024-07-22 08:51:16
498
0

1.PG-Pool连接池参数

PG-Pool支持的连接池参数比较多,可以根据以下说明进行配置。具体含义可以参考下图:

  1. num_init_children:该参数定义了 pgpool 启动时创建的子进程数量,默认值是 32。该参数也是客户端到 pgpool 的并发连接限制,大于该参数的客户端连接在 backlog 监听队列 (listen queue) 中阻塞等待,该队列大小为 listen_backlog_multiplier * num_init_children。如果监听队列大小大于操作系统默认值 net.core.somaxconn 需要修改该值。
  2. listen_backlog_multiplier:该参数用于调整从客户端到 pgpool 的监听队列大小,默认值是 2,监听队列大小为 listen_backlog_multiplier * num_init_children。
  3. reserved_connections:该参数用于限制从客户端到 pgpool 的监听队列过长,避免在客户端连接规模很大时的响应时间过长导致系统不稳定情况,默认值是 0。该参数设置为 0 时表示不拒绝任何客户端连接;当该参数值大于 0 时,pgpool 不会将超过并发连接限制(num_init_children - reserved_connections)的客户端连接加入到监听队列,而是直接拒绝该连接。
  4. connection_cache:该参数用于指定是否开启到后端数据库的连接缓存,默认为开启 (on)。即使该参数设置为 on,到 template0, template1, postgres 和 regression 这些数据库的连接也不进行缓存。
  5. max_pool【后端】:该参数用于指定每个 pgpool 子进程可以缓存到后端的连接数量,默认值是 4。如果到后端的连接与缓存中的连接 user, database 以及运行时参数相同时,直接复用该缓存的连接;没有命中缓存时,则需要新创建到后端的连接,并在缓存数量大于 max_pool 时剔除最旧的连接。

到每个后端数据库节点最多连接数为 max_pool*num_init_children,所以还需要关注 PG 的两个配置参数

  • max_connections :该参数用于指定 PG 数据库的最大并发连接数,默认值是 100, 表示最多可以同时处理100 个连接。
  • superuser_reserved_connections:该参数用于指定为 PG 超级用户连接而保留的连接槽数,默认值是 3 ,且该值必须小于 max_connections。PG 活跃的并发连接数最多为 max_connections - superuser_reserved_connections,如果超过该值,新的连接就只能由超级用户发起,并且不会有新的复制连接被接受。
  1. child_life_time:该参数用于指定子进程的生命周期,生命周期开始于上一次断开客户端连接,以秒为单位,默认值是 300 即 5 分钟,如果该值为 0 则表示禁用该功能。当子进程的生命周期超过这个值时,pgpool 将终止该子进程并创建一个新的子进程,防止内存泄漏。
  2. client_idle_limit:该参数指定了客户端连接的空闲时间限制,以秒为单位,默认值是 0,表示不启用空闲连接关闭。如果一个客户端连接在指定的时间内没有活动,pgpool 将关闭该连接以释放资源
  3. child_max_connections:该参数用于指定每个子进程在生命周期内可以处理的最大客户端连接数,默认值是 0,表示没有限制。当一个子进程的连接数达到这个值时,pgpool 将关闭这个子进程,并且创建一个新的子进程来替换它。这个参数通常使用在 child_life_time 和 connection_life_time 几乎不会被触发的繁忙场景,在生命周期内使用连接数而非时间来提前结束子进程生命周期。
  4. connection_life_time【后端】:该参数用于指定到后端数据库连接的最大生命周期,即连接缓存过期时间,以秒为单位,默认值是 0,表示不启用连接的生命周期限制。如果一个后端连接的生命周期超过这个值,pgpool 将关闭该连接。

2.PG-Pool连接模式

PG-Pool支持静态模式和动态模式两种进程管理模式 (process_management_mode)。

  • static(静态模式):pgpool 的子进程数固定且由 num_init_children 参数指定,所有子进程在 pgpool 启动时预先创建。
  • dynamic(动态模式):pgpool 的子进程数根据 min_spare_children 和 max_spare_children 两个参数动态调整,pgpool 启动时预先创建 max_spare_children 指定的子进程数,子进程数最高可达 num_init_children。

由于静态模式配置完成后连接数就是固定的,可能会有进程浪费和连接个数不够等问题,本文下面主要介绍动态连接模式。

dynamic 模式中 min_spare_children 和 max_spare_children 用于管理 WAIT_FOR_CONNECT 状态的子进程。启动时,根据 max_spare_children 预先创建空闲子进程数(而非 static 模式中根据 num_init_children 创建)。处理客户端连接过程中,当空闲子进程数少于 min_spare_children 时 fork 新的空闲子进程以满足最小空闲子进程数要求;当空闲子进程数大于 max_spare_children 时 kill 空闲子进程以满足最大空闲子进程数要求。

  1. process_management_strategy(进程管理策略):该参数用于指定 pgpool 在动态模式下如何管理子进程,用户管理调整频率它有以下几个可选值,默认值是 gentle
  • lazy:空闲子进程数保持超过 max_spare_children 时间大于 5 分钟,触发逐步执行缩减。
  • gentle:空闲子进程数保持超过 max_spare_children 时间大于 2 分钟,触发逐步执行缩减。
  • aggressive:空闲子进程数保持超过 max_spare_children 时间大于 50 秒,更频繁地触发执行缩减,且在选择被关闭子进程时不考虑子进程到后端的连接数(后端连接越少的越优先关闭)。
  1. min_spare_children(最小空闲子进程数):该参数用于指定在动态模式下,pgpool 应该保持的最小空闲子进程数。
  • 当空闲子进程数低于该值时,pgpool 会 fork 新的子进程以满足 min_spare_children;每次触发 fork 的空闲子进程数为: new_spawn_no = min_spare_children - idle_children + max_spare_children/4,其中 idle_children 表示当前空闲子进程数。
  • 如果当前总子进程数已经达到 num_init_children,则不再创建新的子进程;
  • 如果开启了 child_life_time 功能,所有子进程生命周期结束后仍会保留 min_spare_children 所指定的空闲子进程数。
  1. max_spare_children(最大空闲子进程数):该参数用于指定在动态模式下,pgpool 允许保持的最大空闲子进程数。
  • 空闲子进程数高于该值时,pgpool 会 kill 多余的空闲子进程以达到 max_spare_children 所指定的空闲子进程数;每次触发 kill 的空闲子进程数为:kill_count = idle_children - max_spare_children,其中 idle_children 表示当前空闲子进程数。
  • max_spare_children 大于 num_init_children 时,该参数失效。

动态调整模式下,连接池数位参数关系如下图:


3.PG-Pool配置和PG参数关系

在为Postgresql配置PG-Pool时,连接数配置要考虑后端Postgresql,具体连接数关系如下:

Pool的配置max_pool,num_init_children

PG的配置max_connections 和 superuser_reserved_connections 必须符合以下规则:

max_pool*num_init_children <= (max_connections - superuser_reserved_connections) (不需要取消查询) 
max_pool*num_init_children*2 <= (max_connections - superuser_reserved_connections) (需要取消查询) 

修改pg和Pool的参数都需要重启

4.PG-Pool配置实践

配置示例

num_init_children = 3000
max_pool = 4
listen_backlog_multiplier = 2
reserved_connections = 8
connection_cache = 'on'
child_life_time = 300
child_max_connections = 0
connection_life_time = 0
client_idle_limit = 30min
process_management_mode = dynamic
process_management_strategy = gentle
min_spare_children = 32
max_spare_children = 64

reference:

https://www.pgpool.net/docs/44/en/html/runtime-config-connection.html#RUNTIME-CONFIG-CONNECTION-SETTINGS

https://www.pgpool.net/docs/latest/en/html/runtime-config-connection-pooling.html

https://www.pgpool.net/docs/latest/en/html/runtime-config-process-management.html

https://www.sraoss.co.jp/wp-content/uploads/files/event_seminar/material/2022/pgpool-4.4.pdf

文章来自个人专栏
文章 | 订阅
0条评论
作者已关闭评论
作者已关闭评论
0
0