# reserved_connections

> reserved_connections 定义为具有 pg_use_reserved_connections 权限的角色保留的连接槽数量。
---

> [!NOTE]
> **Fact — 官方简述译文**：设置为具有 pg_use_reserved_connections 权限的角色保留的连接槽数量。

## 身份 {#identity}

| 字段 | 值 | 含义 |
| --- | --- | --- |
| 类型 | `integer` | 上游 pg_settings 类型 |
| Context | `postmaster` | 修改后需要重启数据库 |
| 单位 | — | 原始单位 |
| 范围 | `0` – `262143` | 最后在档版本的原始上下限 |
| 枚举值 | — | 非枚举类型记为 — |
| 分类 | Connections and Authentication / Connection Settings | 上游分类 |
| 最后 boot 值 | `0` | 0 |
{.fields meta="-"}

## 生命周期 {#lifecycle}

| Fact | 值 |
| --- | --- |
| 首次观测 | PG16 |
| 在档版本 | PG16–19 Beta 3 |
| 移除版本 | 否 |
| 引入提交 | [`6e2775e4d4e4`](https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=6e2775e4d4e47775f0d933e4a93c148024a3bc63) — Add new GUC reserved_connections. |
| 提交日期 | 2023-01-20 |
| Discussion | [讨论 1](https://postgr.es/m/20230119194601.GA4105788@nathanxps13) |

## 默认值变迁 {#default-history}

| 版本 | 原始 `boot_val` | 单位 | 人类可读值 |
| --- | --- | --- | --- |
| PG16–19 Beta 3 | `0` | — | 0 |
{.full-width caption="PG9.0–19 Beta 3 实测 boot 默认值"}

## 机制详解 {#mechanism}

reserved_connections：设置为具有 pg_use_reserved_connections 权限的角色保留的连接槽数量。这些槽位只向获授 pg_use_reserved_connections 的角色开放，位于普通容量之上、超级用户专用预留之下。

reserved_connections 属于 POSTMASTER 上下文：PostgreSQL 在服务器启动时读取它，重新加载配置或会话 SET 都不能使新值生效。

reserved_connections 与 superuser_reserved_connections 从 max_connections 中划出特权层；连接池、监控、复制、维护与故障转移必须共同纳入同一后端总预算。

## 调优建议 {#tuning-advice}

> [!TIP]
> **Advice。** 以下建议是工作负载起点，必须用真实测量验证。

| 场景 | 建议 |
| --- | --- |
| OLTP | 在 max_connections 总量内，按独立应急主体数量、连接池行为与故障转移操作确定 reserved_connections；测试普通连接饱和后管理入口仍可用。 |
| OLAP | 为重型分析会话之外的控制、监控与取消预留足够槽位，但不要让预留吞噬本就较小后端池的过高比例。 |
| 小规格 | 让 reserved_connections 相对 max_connections 保持适度，同时至少保留一条经过测试的应急路径；饱和时预留槽不会供普通客户端使用。 |
{.full-width}

## Pigsty 取值 {#pigsty}

以下值来自固定 8 核、32 GiB、100 GiB SSD 夹具，并按 PG19 Beta 3 渲染当前 Pigsty 模板；这不表示 Pigsty 当前支持该历史或测试版本。

| 模板 | 有效值 | 与上游 boot 比较 | 源表达式 |
| --- | --- | --- | --- |
| OLTP | 未修改 | — | — |
| OLAP | 未修改 | — | — |
| CRIT | 未修改 | — | — |
| TINY | 未修改 | — | — |
{.full-width}

> [!CAUTION]
> **Advice — 待人工复核。** 当前 Pigsty 模板投影事实：OLTP: PG16–19 Beta 3 未修改；OLAP: PG16–19 Beta 3 未修改；CRIT: PG16–19 Beta 3 未修改；TINY: PG16–19 Beta 3 未修改。 未发现覆盖值，因此不推断 Pigsty 专属理由。

## 常见坑 {#common-pitfalls}

- 期待重新加载或 SET 激活 reserved_connections，而它实际需要受控重启服务器。
- 把预留槽计算在 max_connections 之外，而所有层级实际共享同一总上限。
- 应急响应预留过少，或预留过多以致饱和时普通应用容量骤降。
- 规划普通、预留与超级用户预留槽时，没有确保总量纳入 max_connections。

## 关联参数 {#related-parameters}

[`listen_addresses`](/zh/parameters/listen-addresses/) · [`port`](/zh/parameters/port/) · [`max_connections`](/zh/parameters/max-connections/) · [`superuser_reserved_connections`](/zh/parameters/superuser-reserved-connections/) · [`unix_socket_directories`](/zh/parameters/unix-socket-directories/)

## 参考资料 {#references}

- [PostgreSQL 19 Beta 3 — reserved_connections](https://www.postgresql.org/docs/19/runtime-config-connection.html#GUC-RESERVED-CONNECTIONS)
- [PostgreSQL 19 发行说明](https://www.postgresql.org/docs/19/release-19.html)
- [机器可读 GUC 全量导出](/data/guc.jsonl)
