# io_workers

> io_workers：设置 io_method=worker 时使用的 I/O 工作进程数。实测在档范围为 PG18；最后在档的 PG18 启动默认值为 3，context 为 sighup。它在 PG19 Beta 3 被移除。
---

> [!NOTE]
> **Fact — 官方简述译文**：设置 io_method=worker 时使用的 I/O 工作进程数。

## 身份 {#identity}

| 字段 | 值 | 含义 |
| --- | --- | --- |
| 类型 | `integer` | 上游 pg_settings 类型 |
| Context | `sighup` | 配置 reload 后生效 |
| 单位 | — | 原始单位 |
| 范围 | `1` – `32` | 最后在档版本的原始上下限 |
| 枚举值 | — | 非枚举类型记为 — |
| 分类 | Resource Usage / I/O | 上游分类 |
| 最后 boot 值 | `3` | 3 |
{.fields meta="-"}

## 生命周期 {#lifecycle}

| Fact | 值 |
| --- | --- |
| 首次观测 | PG18 |
| 在档版本 | PG18 |
| 移除版本 | PG19 Beta 3 |
| 引入提交 | [`55b454d0e140`](https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=55b454d0e14084c841a034073abbf1a0ea937a45) — aio: Infrastructure for io_method=worker |
| 提交日期 | 2025-03-18 |
| Discussion | [讨论 1](https://postgr.es/m/uvrtrknj4kdytuboidbhwclo4gxhswwcpgadptsjvjqcluzmah%40brqs62irg4dt) · [讨论 2](https://postgr.es/m/20210223100344.llw5an2aklengrmn@alap3.anarazel.de) · [讨论 3](https://postgr.es/m/stj36ea6yyhoxtqkhpieia2z4krnam7qyetc57rfezgk4zgapf@gcnactj4z56m) |

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

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

## 机制详解 {#mechanism}

io_workers 设置 PostgreSQL 18 在 io_method=worker 时使用的专用 I/O 工作进程数；对 io_uring 或 sync 无效。

I/O worker 代表数据库进程执行异步请求；这是执行池，与并行查询 worker 及通用后台 worker 名额不同。

数量可重载修改，但有效能力仍受 io_max_concurrency、负载队列深度、存储延迟与 CPU 限制。更多 worker 不保证设备吞吐更高。其 SIGHUP 上下文允许通过重载配置生效，无需重启服务器。

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

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

| 场景 | 建议 |
| --- | --- |
| OLTP | 只有 io_method=worker 时才调 io_workers。从 PG18 默认 3 开始，测量 worker 饱和、CPU 与尾延迟；只有符合条件的 I/O 在等待 worker 执行而不是等待设备时才扩大池。 |
| OLAP | 持续的符合条件扫描需要足够 I/O worker 才能喂满存储，但额外进程会增加调度开销，也不能突破单进程或设备并发瓶颈。应比较吞吐与 worker 利用率；请求尺寸由其他参数控制。 |
| 小规格 | 进程与 CPU 预算紧张时保留 3，或使用实测更小值。io_uring、sync 下 io_workers 无效，因此确认 io_method=worker 前不要调它。 |
{.full-width}

## Pigsty 取值 {#pigsty}

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

| 模板 | 有效值 | 与上游 boot 比较 | 源表达式 |
| --- | --- | --- | --- |
| OLTP | `4` | 不同于 boot 值 | `{{ pg_io_workers }}` |
| OLAP | `4` | 不同于 boot 值 | `{{ pg_io_workers }}` |
| CRIT | `4` | 不同于 boot 值 | `{{ pg_io_workers }}` |
| TINY | `3` | 等于 boot 值 | `{{ pg_io_workers }}` |
{.full-width}

> [!CAUTION]
> **Advice — 待人工复核。** 当前 Pigsty 模板投影事实：OLTP: PG18 = 4 (dcs)；OLAP: PG18 = 4 (dcs)；CRIT: PG18 = 4 (dcs)；TINY: PG18 = 3 (dcs)。 建议（待人工复核）——编辑推断（待维护者复核）：该 worker 池解析值让 PG18 worker AIO 方法在非 tiny 模板拥有更多执行进程，同时 tiny 保持上游启动数量。

## 常见坑 {#common-pitfalls}

- io_method 为 io_uring 或 sync 时修改 io_workers，此时它没有效果。
- 把 worker 进程数误当成每次请求字节数或单进程 I/O 上限。
- 存储设备已经饱和时仍增加 worker。
- 忘记可重载的 worker 池仍会消耗进程、CPU 与调度容量。

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

[`io_method`](/zh/parameters/io-method/) · [`io_max_concurrency`](/zh/parameters/io-max-concurrency/) · [`effective_io_concurrency`](/zh/parameters/effective-io-concurrency/) · [`maintenance_io_concurrency`](/zh/parameters/maintenance-io-concurrency/) · [`max_worker_processes`](/zh/parameters/max-worker-processes/)

## 参考资料 {#references}

- [PostgreSQL 18 — io_workers](https://www.postgresql.org/docs/18/runtime-config-resource.html#GUC-IO-WORKERS)
- [机器可读 GUC 全量导出](/data/guc.jsonl)
