Opportunistic pruning is lost under direct io, and nothing shows it
Thread notes
1 note#feature#moderate#enhancement
hackorum-bot#feature#moderate#enhancement
@global #feature #moderate #enhancement
Hi hackers,
On access pruning gives up without a word when it cannot get a cleanup lock
on a page. That happens when some other session holds a pin on it. The dead
tuples stay, vacuum has to deal with them later, and not one counter moves.
I think we should be able to see this. The attached patch makes it visible.
I started looking because on access pruning now does more than it used to.
It can set a page all visible and it keeps the free space map up to date. So
when a scan walks away from a page, we lose more than a few dead tuples.
Here is what I measured. One table of 42 MB, shared_buffers of 8 MB,
autovacuum off, five clients, four of them counting rows in the table and
one doing single row updates, twenty seconds per run.
With direct io off, the share of prune attempts lost to a pin was
4.1%
2.6%
4.5%
With debug_io_direct set to data, same workload and about the same rate of
work done, it was
35.0%
44.0%
70.2%
With direct io still on and effective_io_concurrency raised to 64, it was
82.3%
83.0%
So under direct io most opportunistic pruning simply does not happen, and
right now there is no way to find that out. The reason is plain once you
look at it. Without direct io a buffer miss is often just a copy out of the
kernel cache, so the pin is held for a moment. With direct io the pin is
held across a real read from the device, and any other scan that wanted to
prune that page gives up.
Please treat these numbers with care. This is a laptop running macOS, where
debug_io_direct uses fcntl with F_NOCACHE, which is only advice to the
kernel and is not the same thing as O_DIRECT. io_method was worker. The
ratio of table size to shared_buffers is small on purpose. I would like
someone to repeat this on Linux with io_uring.
The patch adds four columns to pg_stat_all_tables.
prune_onaccess counts pages that a scan pruned.
prune_onaccess_missed counts prune attempts dropped because the page was
pinned. This is the number that goes up under direct io.
pages_all_visible_onaccess counts pages that on access pruning marked all
visible.
vacuum_missed_dead_pages counts pages vacuum could not clean up for the same
reason. Vacuum already counts these, it just prints them in the log and
forgets them, so you cannot follow the number over time.
Thanks,
Shihao
Rebase the patch as it based on old master
On Sun, Sep 13, 2026 at 10:14 PM shihao zhong <zhong950419@gmail.com> wrote:
On access pruning gives up without a word when it cannot get a cleanup lock
on a page. That happens when some other session holds a pin on it. The dead
tuples stay, vacuum has to deal with them later, and not one counter moves.
I think we should be able to see this. The attached patch makes it visible.I started looking because on access pruning now does more than it used to.
It can set a page all visible and it keeps the free space map up to date. So
when a scan walks away from a page, we lose more than a few dead tuples.Here is what I measured. One table of 42 MB, shared_buffers of 8 MB,
autovacuum off, five clients, four of them counting rows in the table and
one doing single row updates, twenty seconds per run.
This setup is contrived enough that I do not anticipate seeing
anything like this in real workloads. Though the ratio of table to
shared_buffers may be a real-world scenario, space for 1000 blocks in
shared_buffers is highly unrealistic. The contention this produces is
going to give you all kinds of problems beyond missed on-access
pruning.
The patch adds four columns to pg_stat_all_tables.
prune_onaccess counts pages that a scan pruned.
prune_onaccess_missed counts prune attempts dropped because the page was
pinned. This is the number that goes up under direct io.pages_all_visible_onaccess counts pages that on access pruning marked all
visible.vacuum_missed_dead_pages counts pages vacuum could not clean up for the same
reason. Vacuum already counts these, it just prints them in the log and
forgets them, so you cannot follow the number over time.
Adding four columns to pg_stat_all_tables is not worth it for this.
There are other vacuum-related stats that might be worth persisting in
a cumulative way outside of the logs, but I don't think this is one of
them. For your scenario, the user's only remediation would be to
increase shared_buffers. And this stat cumulatively won't tell you
much over time. For bursty workloads, what does a sum of missed pages
tell you? The log is much more useful to see at specific times when
vacuuming wasn't able to clean up a table. It's true that on-access
pruning doesn't have stats like this, but I don't feel convinced yet
that this should be added to pg_stat_all_tables.
- Melanie
Hi Melanie,
This setup is contrived enough that I do not anticipate seeing
anything like this in real workloads.
You were right, that first setup was not worth anything. With 1024 buffers,
MaxProportionalPins leaves each backend about 9 pins, so the read stream
never looked ahead and my reading of the numbers was wrong. I shrank the
buffer pool to force reads instead of growing the data. That was the wrong
trade.
Here it is again. Stock pgbench at scale 1000, so a 13 GB table.
shared_buffers of 2 GB. autovacuum on. 8 clients on the built in pgbench
script and 4 clients running one aggregate over the whole table. 120 seconds
per run.
Prune attempts dropped because the page was pinned.
direct io off, 4.6% and 3.2%
direct io on, 66.7% and 54.1%
Then I ran the same thing with the 4 scanning clients taken out, so plain
pgbench and nothing else. 4 missed prunes in 120 seconds. That was true with
direct io on and with it off.
So there are two conditions. The table has to be getting scans and updates
at
the same time, and direct io has to be on. Plain pgbench with no scans shows
nothing at all.
I would not call that a corner case. This is what we are running into in our
benchmark environment, and it is why I started looking.
Adding four columns to pg_stat_all_tables is not worth it for this.
You are right about the shape too. A cumulative sum does not tell you when
it
happened. I would rather find the right shape than push four columns. Vacuum
at least has a log line for the pages it could not clean up. On access
pruning has no counter and no log line, so when it stops working there is
nothing to find. I would like to know whether something there is worth
doing,
or whether you think this does not need to be visible at all.
Thanks,
Shihao