Hi,
This patch implements backward I/O combining mainly to help index
prefetching work [1]. Since there is no real user of this patch
without [1], this patch should be applied on top of it. It doesn't
cleanly apply on top of the master without it.
----------
High level design:
Instead of implementing backward I/O combining in all subsystems (read
stream, aio, bufmgr etc.), I decided to implement it in read stream
level only. So, only read stream code knows backward I/O combining is
happening and then it sends the reverse of these I/O requests to lower
levels; so lower levels will behave the same as forward I/O combining.
Then the read stream returns these buffers in the reverse order as
they reversed once while sending to lower levels.This decision is made
because of 2 main reasons:
1- Almost all of I/O related code is designed for forward I/O
combining, there would be too much duplicated code.
2- There is no actual way to do backward read with pread(), it does
forward read only so the I/O request needs to be reversed at some
point.
----------
Implementation details:
We have a new struct called ReverseRange, this struct saves the
information of 'the start index of backward I/O combining and how many
blocks it includes'. All of the blocks in this range are in descending
order. Then when the read stream encounters the start index of
ReverseRange, we reverse them and read all buffers in this range
before returning any buffer to the read stream user. That is important
because let's say our ReverseRange consists of 5 blocks and their
numbers are [8, 7, 6, 5, 4], keep in mind that the first block that
will be returned is 8. When we reverse this range (while sending the
lower levels), blocks are in ascending order: [4, 5, 6, 7, 8] so the
first block we return (8) will be the last block we read. Because of
that, we need to read all blocks in range before returning any of
them.
Also, because of the same problem above we can't have forwarded
buffers in backward I/O combining (i.e having short read and then
continuing to expand I/O combined blocks from remaining I/O). Let's
have the same example, our range is [8, 7, 6, 5, 4] and its reverse is
[4, 5, 6, 7, 8]. We had a short read because of some reason and we
read [4, 5, 6] first. [7, 8] are still remaining and we must finish
reading [7, 8] before returning 8. We can't expand (combine) [7, 8]
with any blocks.
----------
Benchmark:
I used a simple benchmark script generated by LLM. I checked the
script and it works correctly. This script uses
read_stream_for_blocks() to benchmark, by using that we directly see
the effect of backward I/O combining patch without any external
effect. This benchmark shows up to 4x speedups for the backward scans
when the I/O combining is available and there are no regressions. I
see similar speedups with the backward index scan when the I/O
combining is available.
You can run the benchmark script by: 'DATADIR=/tmp/pg_datadir
./bench_read_stream.sh' command. Some results from the benchmark
script:
Without backward I/O combining patch:
=== cold ===
io_combine_limit | forward(ms) | backward(ms) | bwd/fwd
-----------------+--------------+--------------+---------
1 | 115.448 | 136.790 | 1.18x
8 | 49.344 | 141.556 | 2.87x
16 | 40.519 | 141.847 | 3.50x
32 | 35.429 | 139.396 | 3.93x
With backward I/O combining patch:
=== cold ===
io_combine_limit | forward(ms) | backward(ms) | bwd/fwd
-----------------+--------------+--------------+---------
1 | 112.774 | 134.188 | 1.19x
8 | 42.958 | 47.918 | 1.12x
16 | 40.161 | 40.352 | 1.00x
32 | 36.299 | 36.284 | 1.00x
----------
v1 is attached. 0001 is the squashed patch of v31 index prefetching
patches [2]. 0002 is the actual patch which implements backward I/O
combining. 0003 has the benchmark script and the required code to run
the benchmark. Only 0002 is attached for a review / commit.
Any feedback would be appreciated.
[1] https://postgr.es/m/cf85f46f-b02f-05b2-5248-5000b894ebab%40enterprisedb.com
[2] https://postgr.es/m/CAH2-WzmpQV7wqbwE_bW%2B1Ja32chnh87i%3Dwx2Y6NkhFiuEVsBzw%40mail.gmail.com
--
Regards,
Nazir Bilal Yavuz
Microsoft
Hi,
Thanks for the patch. I got this patch a couple days/week ago, as part
of the work on index prefetching. So I got to do a bit of benchmarking.
Note: It's hard to generate the backward heap access without an index
scan, as all other ReadStream users generate forward pattern (with
increasing block number). So this is very index-scan specific patch.
I've been also interested if / how much this helps to mitigate the
annoying differences I described almost exactly a year ago:
https://vondra.me/posts/fun-and-weirdness-with-ssds/
The gist of that post is that both the kernel read-ahead and the
heuristics in SSD firmware are very helpful, but do not kick in for
backward access at all (just like "our" combining before the patch).
The blog post focuses on "raw" fio tests, showing massive differences
depending on the access direction. In most cases the backward access
performs about the same as random.
There's a couple queries with index scans, and the cases with backward
access are often ~30x slower, despite accessing exactly the same amount
of data. It's all just because the kernel/firmware does not recognize
the backward access pattern.
The blog post did not test the index prefetching patch, but that helps
quite a bit - it gets the regression to ~5x (so much better than 30x).
But it's still far worse than an equivalent forward case. Most likely
due to the I/O combining not kicking in, so still doing 8K reads (while
for forward access it gets combined into 128K chunks).
So I was wondering if this patch gets us closer to the forward access.
This may seem like an artificial case, but I think it's pretty common in
practice. Consider a table with a "creation time", and queries like:
SELECT * FROM my_table ORDER BY creation_time DESC
The table is likely correlated with creation_time (modulo some small
perturbations due to deletes / updates), and so the query will generate
a nice backward access pattern. I could construct more more examples.
Of course, the costing completely ignores this effect. That seems bad,
but it's not clear what to do about it.
So, I ran a benchmark compring timings for runs with a number of
parameters. You can find the scripts and results here:
https://github.com/tvondra/index-backward-tests
There's a "fio" folder with measurements for some low-level I/O tests
(some results are interesting/funny), but I'll focus on the "pg"
results, measuring timing of queries.
It runs tests for different combinations of a couple parameters:
- buffered vs. direct I/O
- io_method and io_workers
- 0 and 4 parallel workers
- branch: master vs. index-prefetch vs. index-prefetch-backward-combine
- effective_io_concurrency 16 and 128
- ASC / DESC query order (translates to forward vs. backward access)
- device (6 different SSD devices on 2 machines)
I'm attaching a PDF summarizing the results. The columns show timings
for the ASC/DESC direction, prefetch on/off and various branches. And
then the last group of columns show timings relative to master/ASC
(which is a baseline). green=good red=bad, as usual.
The pattern is pretty clear. The DESC (backward access) is much slower,
hence the huge area of red results for runs without index prefetch.
The index prefetch helps quite a bit, with ~5x speedup, which is nice.
But it's still ~5x slower than the ASC case. The combining gets us to
~1.5x compared to master/ASC for all the tested SSD devices etc.
So I think this works pretty well, and it largely addresses the issue
with backward access.
I haven't done that yet, but it might be interesting to run these tests
on non-local storage.
regards
--
Tomas Vondra