Re: doc: explain pgstatindex fragmentation

From: Ants Aasma <ants(dot)aasma(at)cybertec(dot)at>
To: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
Cc: Benoit Lobréau <benoit(dot)lobreau(at)dalibo(dot)com>, Frédéric Yhuel <frederic(dot)yhuel(at)dalibo(dot)com>, Bertrand Drouvot <bertranddrouvot(dot)pg(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: doc: explain pgstatindex fragmentation
Date: 2025-01-27 19:56:52
Message-ID: CANwKhkOf_PLp11aTP-hXXmQ71uDu4Z+5PhfjHR80-pL4kpeeyQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Mon, 27 Jan 2025 at 11:21, Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at> wrote:
> > He reported to our team, that he did a test with two indexes on the same
> > data. They had the same density but one had no fragmentation while the
> > other had 100%. He got an execution time of ~90ms (0 frag) vs ~340ms
> > 100% frag).
> >
> > I get similar result with my laptor (except my disk is significantly
> > worse: ~152ms vs ~833ms).
>
> Thanks for checking.
> I'll set the patch "ready for committer".
> I personally would still like to know how fragmentation slows down performance.

Probable reason is that scanning an unfragmented index results in
sequential I/O patterns that the kernel read-ahead mechanism detects
and does prefetching for us. Fragmented index looks like random I/O
and gets to wait for the full disk latency for every index page.

This is one of the tricky parts to fix for AIO, as directIO will also
bypass this mechanism. PostgreSQL would need to start issuing those
prefetches itself to not have a regression there.

In a theoretical world, where we would be able to drive prefetches
from an inner B-tree page, the difference between fragmented and
unfragmented indexes would be much less.

--
Ants Aasma

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Vladimir Vasilenko 2025-01-27 20:22:55 How to build Postgres Pro on FreeBSD?
Previous Message Corey Huinker 2025-01-27 19:52:15 Re: Extended Statistics set/restore/clear functions.