asynchronous I/O
read streams, io_method = worker or io_uring, and what postgres 18 changed about reading.
- address
- everything.storage.async_io
- part
- part 12, pages and the buffer pool
- chapter
- chapter 12 of 12
- reading time
- 9 min
- version
- pg 18+
until postgres 17, a backend that needed a page asked for it, waited, and then asked for the next one. postgres 18 adds an asynchronous I/O subsystem. code that knows what it will read next, a read stream, asks for pages ahead of time, several at once, merged into bigger reads, and works on each page the moment it arrives.
io_method picks who actually does the reading: worker (the default: a few io worker processes), io_uring (linux, straight to the kernel), or sync (the backend itself, the old way). the browser engine runs sync.
the picture
you know how a seq scan of a table that isn't cached can be slow even on a fast SSD? the SSD isn't slow at reading. the backend is slow at asking: one page, wait, one page, wait.
think of a waiter in a restaurant. the old way, they walk to the kitchen for one dish, wait while it's plated, carry it out, walk back for the next. the new way, they hand the kitchen the whole order (the read stream knows the next pages), the kitchen works on several dishes at once (the io workers), and the waiter serves each dish the moment it's ready. the walk doesn't get shorter. there's just a lot less standing around.
here's that as a toy schedule, eight pages, with made-up units of time:
the point of the last setting is real, and measured below: asynchronous I/O makes waiting overlap with working. if there's no waiting, there's nothing to gain.
read streams: fewer, bigger reads
the part of this you can see in every engine, even the browser's, is the read stream merging neighbouring blocks into one read. postgres 17 added that (io_combine_limit, 128 kB by default pg 17+), and postgres 18 runs it through the new I/O subsystem. here's a cold seq scan of events, all 335 pages, on a native postgres 18.6:
24 reads of about 112 KB, against 335 reads of 8 KB. on a disk where every request has a fixed cost, that's most of the cost gone. (why pages exist measured that fixed cost on one SSD: a 64 KB read took about 1.6 times as long as an 8 KB one, not 8 times.) the browser engine does the same merging with io_method = sync:
SELECT count(*) FROM events;
SELECT buffers_evicted FROM pg_buffercache_evict_relation('events');
SELECT pg_stat_force_next_flush();
SELECT pg_stat_reset_shared('io');
SELECT sum(length(kind)) FROM events;
SELECT pg_stat_force_next_flush();
SELECT reads, read_bytes, round(read_bytes / reads / 1024.0, 1) AS kb_per_read
FROM pg_stat_io
WHERE backend_type = 'client backend' AND object = 'relation' AND context = 'normal';| countint8 |
|---|
| 25000 |
| buffers_evictedint4 |
|---|
| 335 |
| pg_stat_force_next_flushvoid |
|---|
| '' |
| pg_stat_reset_sharedvoid |
|---|
| '' |
| sumint8 |
|---|
| 132117 |
| pg_stat_force_next_flushvoid |
|---|
| '' |
| readsint8 | read_bytesnumeric | kb_per_readnumeric |
|---|---|---|
| 38 | 2859008 | 73.5 |
the first count warms up the catalog, the evict empties events out of the pool, and the rest counts the reads of one scan: 38 reads, 73.5 KB each on average, for events' 335 pages and a few catalog pages that came along. far fewer reads than pages, with no io workers anywhere.
io_method
the read stream decides what to read and when. io_method decides who does the reading:
- worker, the default. the backend puts its read requests in shared memory and carries on; one of
io_workers(3) background processes performs each read and marks it done. works everywhere. - io_uring, linux only. the backend hands its reads straight to the kernel through an io_uring queue and collects the results, no extra processes in between. needs a postgres built with liburing.
- sync. each read is performed right there in the backend, when it's issued: postgres 17 behaviour, read combining and all, without the overlap.
it's a server start setting. on this mac, Postgres.app 18 offers two of the three:
SELECT name, setting, unit, enumvals FROM pg_settings
WHERE name IN ('io_method', 'io_workers', 'io_combine_limit', 'io_max_combine_limit',
'effective_io_concurrency', 'maintenance_io_concurrency')
ORDER BY name;| nametext | settingtext | unittext | enumvalstext[] |
|---|---|---|---|
| effective_io_concurrency | 16 | null | null |
| io_combine_limit | 16 | 8kB | null |
| io_max_combine_limit | 16 | 8kB | null |
| io_method | worker | null | {sync,worker} |
| io_workers | 3 | null | null |
| maintenance_io_concurrency | 16 | null | null |
checked on a real postgres 18.6 server. the browser engine can't run this one.
no io_uring on macOS: it's a linux kernel interface. and effective_io_concurrency is now 16 by default (it was 1), which is how many reads a stream may keep in flight at once. the browser engine:
SHOW io_method;
SHOW effective_io_concurrency;| io_methodtext |
|---|
| sync |
| effective_io_concurrencytext |
|---|
| 16 |
sync, because PGlite is one process: there's nobody to hand a read to.
SELECT backend_type, reads, read_bytes
FROM pg_stat_io WHERE object = 'relation' AND reads > 0;what uses it
the postgres 18 release notes name sequential scans, bitmap heap scans and vacuums, "etc.": the operations that read through read streams, where the next blocks are known in advance. and they describe it as backends queueing multiple read requests. writing dirty pages back, by the checkpointer, the background writer or a backend, still goes the old way, one write() at a time (writing dirty pages back).
the new pg_aios view lists the I/O handles in flight at the moment you look, and is empty the rest of the time. pg_stat_io gained read_bytes, write_bytes and extend_bytes in 18, which is what made the KB-per-read column above possible.
does it make things faster?
where reads are slow, yes: network storage, cloud volumes with per-request latency, a cold SSD, a big seq scan or VACUUM that mostly waits. where reads are already fast, there's little to win. measured on this mac (an M4 Pro, Postgres.app 18.6): a 184 MB table, evicted from shared buffers but still in the OS page cache, SELECT count(*), median of 9 runs, on two fresh servers per setting:
no difference worth the name, because every "read" was a memory copy from the OS cache. the cold case is the one that matters, and it needs the OS cache dropped between runs, which macOS only allows as root. so there's no cold number here: the toy schedule above is the honest picture of why it helps, and the release notes are the honest source for that it does.
keylines
- read streams know what's next, so they ask for it early, several reads at a time.
- neighbouring blocks are merged into reads of up to 128 kB (io_combine_limit).
- io_method: worker (default, 3 io workers), io_uring (linux), sync (the old way, and the browser's).
- I/O is counted for the backend that asked for it, whoever performed it.
- it hides waiting. no waiting, no gain.