Re: Adding skip scan (including MDAM style range skip scan) to nbtree

From: Tomas Vondra <tomas(at)vondra(dot)me>
To: Peter Geoghegan <pg(at)bowt(dot)ie>
Cc: Mark Dilger <mark(dot)dilger(at)enterprisedb(dot)com>, Heikki Linnakangas <hlinnaka(at)iki(dot)fi>, pgsql-hackers(at)lists(dot)postgresql(dot)org, Matthias van de Meent <boekewurm+postgres(at)gmail(dot)com>
Subject: Re: Adding skip scan (including MDAM style range skip scan) to nbtree
Date: 2025-05-09 18:07:17
Message-ID: da5ba8c8-55d7-4e5c-9a1f-e0491d142d4d@vondra.me
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 5/9/25 19:30, Peter Geoghegan wrote:
> On Fri, May 9, 2025 at 8:58 AM Tomas Vondra <tomas(at)vondra(dot)me> wrote:
>> select count(*) from pgbench_accounts where bid = 0
>
> What kind of plan are you getting? Are you sure it's index-only scans?
>
> With 100 partitions, I get a parallel sequential scan when I run
> EXPLAIN ANALYZE with this query from psql -- though only with "bid =
> 1". With your original "bid = 0" query I do get index-only scans.
>
> What ends up happening (when index-only scans are used) is that we
> scan only one index leaf page per partition index scanned. The
> individual index-only scans don't need to scan too much (even when the
> "bid = 1" variant query is forced to use index-only similar scans), so
> I guess it's plausible that something like a regression in
> preprocessing could be to blame, after all. As I mentioned just now,
> these indexes each have only one index leaf page (the thing about 85
> leaf pages only applies when partitioning isn't in use).
>
> I find that the execution time for index-only scans with "bid = 0"
> with a warm cache are:
>
> Planning Time: 0.720 ms
> Serialization: time=0.001 ms output=1kB format=text
> Execution Time: 0.311 ms
>
> Whereas the execution times for index-only scans with "bid = 1" are:
>
> Planning Time: 0.713 ms
> Serialization: time=0.001 ms output=1kB format=text
> Execution Time: 16.491 ms
>
> So you can see why I'd find it so hard to believe that any underlying
> regression wouldn't at least be well hidden (by all of the other
> overhead) in the case of the "bid = 1" variant query. There's no
> reason to expect the absolute number of cycles added by some
> hypothetical regression in preprocessing to vary among these two
> variants of your count(*) query.
>

Yes, I'm sure it's doing index only scan - did you update "bid" or did
you leave it as generated by "pgbench -i"?. Because then there's only
one value "1", and it'd make sense to use seqscan. The exact steps I did
for the "bid = 1" case are:

update pgbench_accounts set bid = aid / 100;
vacuum full;
analyze;

and then I get the proper index-only scans, with pretty much the same
behavior as for bid=0.

Also, I did some profiling and the (attached) flamegraphs confirm this.
The "slow" is on master, "fast" is on 3ba2cdaa454. Both very clearly
show IndexOnlyScan callbacks, etc. And the "slow" flamegraph also shows
a lot of time spent in malloc(), unlike the fast one.

(AFAIK the profiles for bid=0 and bid=1 look exactly the same.)

In fact, all of the malloc() calls seem to happen in AllocSetAllocLarge,
which matches the guess that something tripped over allocChunkLimit. Not
sure what, though.

regards

--
Tomas Vondra

Attachment Content-Type Size
perf-fast.svg.gz application/gzip 80.9 KB
perf-slow.svg.gz application/gzip 104.1 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Bruce Momjian 2025-05-09 18:16:14 Re: PG 18 release notes draft committed
Previous Message Bruce Momjian 2025-05-09 18:03:10 Re: PG 18 release notes draft committed