Re: Improving scalability of Parallel Bitmap Heap/Index Scan

From: David Geier <geidav(dot)pg(at)gmail(dot)com>
To: John Naylor <johncnaylorls(at)gmail(dot)com>
Cc: PostgreSQL Developers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Improving scalability of Parallel Bitmap Heap/Index Scan
Date: 2026-10-14 14:39:47
Message-ID: 58db4547-0a79-498b-be51-47ab6b6362bc@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> +/* Needed for sort template to work with pointers to PagetableEntry */
> +typedef PagetableEntry *PagetableEntryPtr;
>
> This is a workaround; let's fix it properly, like this:
>
> ST_POINTER_TYPE *a = (ST_POINTER_TYPE *) data;
> ST_POINTER_TYPE *pa;
> ST_POINTER_TYPE *pb;
> ...
>
> I see that Thomas already encountered that problem in the 0001 patch
> of that series:
>
> https://www.postgresql.org/message-id/attachment/120554/0001-Add-bsearch-and-unique-templates-to-sort_template.h.patch

Makes sense. I should have done that right away. Done in patch v2-0001.

> +#define ST_COMPARE(a, b) pg_cmp_u32((*(a))->blockno, (*(b))->blockno)
> ...
> +#define ST_COMPARE(a, b, base) pg_cmp_u32(base[*(a)].blockno,
> base[*(b)].blockno)
>
> There's a comment in common/int.h that says:
>
> * NB: If the comparator function is inlined, some compilers may produce
> * worse code with these helper functions than with code with the
> * following form:
> *
> * if (a < b)
> * return -1;
> * if (a > b)
> * return 1;
> * return 0;
>
> ...and that's been true every time it's tried. It should be fine to
> instead make the current comparators static inline and make both match
> the above pattern. You might get a further speed-up.
Oh, right. The qsort partitioning code does:

return DO_COMPARE(a, b) < 0 ?
(DO_COMPARE(b, c) < 0 ? b : (DO_COMPARE(a, c) < 0 ? c : a))
: (DO_COMPARE(b, c) > 0 ? b : (DO_COMPARE(a, c) < 0 ? a : c));

With the if-cascade the compiler can better optimize the expression in
DO_COMPARE().

I revived the original comparators with some minor modifications.
Runtimes of just the sorting now are:

master: 189 ms
patched v1: 136 ms
patched v2: 106 ms

Nice additional speedup!

--
David Geier

Attachment Content-Type Size
v2-0002-TIDBitmap-uses-sort_template.h-for-pagetable-sort.patch text/x-patch 4.6 KB
v2-0001-sort_template.h-supports-pointer-element-types.patch text/x-patch 1.1 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Nisha Moond 2026-10-14 14:46:22 Re: Proposal: Conflict log history table for Logical Replication
Previous Message Matthias van de Meent 2026-10-14 14:25:33 Re: SQL-level pg_datum_image_equal