# io_method

> io_method：选择 PostgreSQL 执行异步 I/O 操作的方法。实测在档范围为 PG18–19 Beta 3；最后在档的 PG19 Beta 3 启动默认值为 worker，context 为 postmaster。这是测试版快照事实，PostgreSQL 19 正式发布前仍可能变化。
---

> [!NOTE]
> **Fact — 官方简述译文**：选择 PostgreSQL 执行异步 I/O 操作的方法。

## 身份 {#identity}

| 字段 | 值 | 含义 |
| --- | --- | --- |
| 类型 | `enum` | 上游 pg_settings 类型 |
| Context | `postmaster` | 修改后需要重启数据库 |
| 单位 | — | 原始单位 |
| 范围 | — | 最后在档版本的原始上下限 |
| 枚举值 | `sync`, `worker`, `io_uring` | 非枚举类型记为 — |
| 分类 | Resource Usage / I/O | 上游分类 |
| 最后 boot 值 | `worker` | worker |
{.fields meta="-"}

## 生命周期 {#lifecycle}

| Fact | 值 |
| --- | --- |
| 首次观测 | PG18 |
| 在档版本 | PG18–19 Beta 3 |
| 移除版本 | 否 |
| 引入提交 | [`02844012b304`](https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=02844012b304ba80d1c48d51f6fe10bb622490cc) — aio: Basic subsystem initialization |
| 提交日期 | 2025-03-17 |
| Discussion | [讨论 1](https://postgr.es/m/uvrtrknj4kdytuboidbhwclo4gxhswwcpgadptsjvjqcluzmah%40brqs62irg4dt) |

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

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

## 机制详解 {#mechanism}

worker 把符合条件的 I/O 交给专用 I/O worker 进程；io_uring 使用 Linux io_uring，要求构建时包含 liburing 支持；sync 则同步执行原本可异步的操作。上游默认是 worker。

PG18 AIO 子系统允许后端排队多个读取请求，可改善顺序扫描、位图堆扫描、vacuum 等受支持操作；它不会让 PostgreSQL 的所有 I/O 路径都自动异步化。

io_workers 仅在选择 worker 时有效。io_max_concurrency 与 I/O combine limit 控制不同维度的队列深度和请求大小，因此方法选择必须结合这些参数及完整存储栈评估。

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

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

| 场景 | 建议 |
| --- | --- |
| OLTP | 把 worker 作为兼容性基线。只有在确认构建与内核支持后才测试 io_uring，并以代表性并发延迟改善且存储队列稳定作为保留依据。 |
| OLAP | 顺序扫描和位图访问较多的分析负载是 AIO 的有力候选；应在真实扫描并发下比较 worker 与 io_uring，而不是只看单次冷扫描。 |
| 小规格 | worker 是安全默认值；当 worker 开销或平台限制成为问题时，sync 可作为诊断回退。不要让过多 io_workers 占用稀缺进程资源。 |
{.full-width}

## Pigsty 取值 {#pigsty}

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

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

> [!CAUTION]
> **Advice — 待人工复核。** 当前 Pigsty 模板投影事实：OLTP: PG18–19 Beta 3 = worker (dcs)；OLAP: PG18–19 Beta 3 = worker (dcs)；CRIT: PG18–19 Beta 3 = worker (dcs)；TINY: PG18–19 Beta 3 = worker (dcs)。 建议（待人工复核）——编辑推断（待维护者复核）：Pigsty 选择可移植的 PG18 AIO 基线，并把 io_uring 保留为运维人员的显式选择。

## 常见坑 {#common-pitfalls}

- PostgreSQL 18 之前不存在该参数。
- 修改后必须重启服务器。
- io_uring 需要操作系统和构建支持，仅写入枚举值不能创造这些能力。
- 除非 io_method=worker，否则 io_workers 不生效。
- AIO 只改善符合条件的路径，无法弥补过载或配置不当的存储层。

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

[`io_workers`](/zh/parameters/io-workers/) · [`io_max_concurrency`](/zh/parameters/io-max-concurrency/) · [`io_combine_limit`](/zh/parameters/io-combine-limit/) · [`io_max_combine_limit`](/zh/parameters/io-max-combine-limit/) · [`effective_io_concurrency`](/zh/parameters/effective-io-concurrency/) · [`maintenance_io_concurrency`](/zh/parameters/maintenance-io-concurrency/)

## 参考资料 {#references}

- [PostgreSQL 19 Beta 3 — io_method](https://www.postgresql.org/docs/19/runtime-config-resource.html#GUC-IO-METHOD)
- [PostgreSQL 18 发行说明：异步 I/O 子系统](https://www.postgresql.org/docs/18/release-18.html)
- [PostgreSQL 19 发行说明](https://www.postgresql.org/docs/19/release-19.html)
- [机器可读 GUC 全量导出](/data/guc.jsonl)
