use radix tree for bitmap heap scan
This is not in reviewable state, but I wanted to get a sketch of a PoC
out there and highlight some issues that will need to be tackled. The
index scan successfully populates the radix tree and the contents are
verified to match the hash table for the queries present in the
regression tests. (Now that I think of it, this patch set might not be
buildable as is, since my local tree has two copies of the radix tree
template so TID store doesn't break while working on this)
First issue: Different memory management needs from vacuum. I split
RT_SET into two functions, the lower of which is similar to simple
hash's "insert" method.
Second issue: TBM intersection
1) Radix tree iteration assumes that no one will delete keys out from
under it. That includes the caller itself! This is not sufficient for
intersecting bitmaps. I kluged around it by saving
blocks-to-be-deleted in a stack array, but a proper solution could be
to (vigorous handwaving)
1) save per-leaf-node keys in the iterator
2) delete the keys when iteration leaves that node, possibly with a
bulk-delete mechanism
3) restart iteration from the top, at the next part of the key space
The above is enough to get rid of the qsort call, but next we'll want
to use variable-length bitmaps like vacuum does. This is both to
reduce memory use and to allow the length of the line pointer array to
be sized independently from MaxHeapTuplesPerPage. That will require
getting rid of the iterator array and doing shared iteration over the
tree itself. Ideally, this will be done in a way compatible with
vacuum, but resolving the differences into new abstractions will be
the final step.
--
John Naylor
Amazon Web Services
Attachments:
v1-0001-Get-rid-of-TBMStatus.patchtext/x-patch; charset=US-ASCII; name=v1-0001-Get-rid-of-TBMStatus.patchDownload+5-126
v1-0005-Populate-radix-tree-at-the-same-time-as-hash-tabl.patchtext/x-patch; charset=US-ASCII; name=v1-0005-Populate-radix-tree-at-the-same-time-as-hash-tabl.patchDownload+506-8
v1-0002-TEMP-disable-tbm_lossify.patchtext/x-patch; charset=US-ASCII; name=v1-0002-TEMP-disable-tbm_lossify.patchDownload+3-1
v1-0003-Turn-RT_SET-into-wrapper-that-calls-to-RT_GET_SLO.patchtext/x-patch; charset=US-ASCII; name=v1-0003-Turn-RT_SET-into-wrapper-that-calls-to-RT_GET_SLO.patchDownload+46-16
v1-0004-Get-rid-of-RT_CHILD_PTR.patchtext/x-patch; charset=US-ASCII; name=v1-0004-Get-rid-of-RT_CHILD_PTR.patchDownload+225-222
Attached is PoC v2, which fully replaces the hash table with radix
tree and passes regression tests.
The architecture is still one of tidbitmap.c having it's own tree of
(fixed length) PagetableEntries. The eventual goal is for vacuum and
tidbitmap to both use TID store in some form, but there are some
architectural issues to work out first.
0001 temporarily gets rid of the tidbitmap one-page cache so I could
always count on the hash table and radix tree having matching contents
during development, barring bugs. I'm curious how important it is, so
it doesn't yet get put back in this patch set.
0002 temporarily gets turns off page lossification for the same reason
of matching contents. (Still not reimplemented)
0003 makes some radix tree APIs more similar to simplehash, in a
backward compatible way.
0004 is a step towards putting allocation of tree values in the
caller's hands. This will be even more necessary when doing unions and
intersections with variable-length bitmaps (not yet implemented).
0005 make it easier to add new tree APIs that aren't used everywhere.
0006 RT_BEGIN_ITERATE_AT, similar to simplehash. This is a
prerequisite for 0007, and could also be used for shared iteration.
This implementation is naive and may slow down iteration, but that can
easily be fixed.
On Thu, Jul 3, 2025 at 3:43 PM I wrote:
Second issue: TBM intersection
1) Radix tree iteration assumes that no one will delete keys out from
under it. That includes the caller itself! This is not sufficient for
intersecting bitmaps. I kluged around it by saving
blocks-to-be-deleted in a stack array, but a proper solution could be
to (vigorous handwaving)
1) save per-leaf-node keys in the iterator
2) delete the keys when iteration leaves that node, possibly with a
bulk-delete mechanism
3) restart iteration from the top, at the next part of the key space
0007 implements the above (but no bulk deletion). It's so far only
used for intersection, but will also be the basis for page
lossification. Because latter requires inserts as well as deletes, it
could be tricky to do nicely. Compared to the hash table, retail
deletion is likely slower in general, but since they're batched per
leaf node it might be okay. 0006 and 0007 were tricky to get right,
and may contain bugs.
0008 is the main event. It's sloppy about memory management and
contains a kluge for the shared memory case: I was unable to quickly
adapt the existing relative pointer logic so I gave up and and
allocated a new DSA array, since it works and gets the point across.
There are new radix tree tests (separately, some of the existing ones
should be simplified to look more like the new bitmapset testing
module), but no new bitmap heap scan tests.
With some cleanups, restoring lossification, and maybe performance
work, the above could be the basis of a viable commit, since it has a
fairly small blast radius and gets rid of the qsort steps. However, I
believe it won't quite be enough on its own for a major release. For
that, we'd want
* variable-length keys to save memory, which would add some complexity
to union and intersection steps
* probably direct iteration over the tree, rather than via reference
arrays, which add memory and are no longer as useful because we don't
need a separate sorting step
* integration with TID store in some form
* adjustments to costing?
--
John Naylor
Amazon Web Services
Attachments:
t51851_2v2-0004-Decouple-RT_ALLOC_LEAF-from-node-types.patchtext/x-patch; charset=US-ASCII; name=v2-0004-Decouple-RT_ALLOC_LEAF-from-node-types.patchDownload+30-17
v2-0006-Add-RT_BEGIN_ITERATE_AT.patchtext/x-patch; charset=US-ASCII; name=v2-0006-Add-RT_BEGIN_ITERATE_AT.patchDownload+92-23
v2-0005-Get-rid-of-RT_USE_DELETE.patchtext/x-patch; charset=US-ASCII; name=v2-0005-Get-rid-of-RT_USE_DELETE.patchDownload+2-18
v2-0007-RT_ITER_STAGE_DELETE.patchtext/x-patch; charset=US-ASCII; name=v2-0007-RT_ITER_STAGE_DELETE.patchDownload+141-1
v2-0008-Replace-tidbitmap.c-s-hash-table-with-a-radix-tre.patchtext/x-patch; charset=US-ASCII; name=v2-0008-Replace-tidbitmap.c-s-hash-table-with-a-radix-tre.patchDownload+77-158
v2-0002-TEMP-disable-tbm_lossify.patchtext/x-patch; charset=US-ASCII; name=v2-0002-TEMP-disable-tbm_lossify.patchDownload+3-1
v2-0001-TEMP-Get-rid-of-TBMStatus.patchtext/x-patch; charset=US-ASCII; name=v2-0001-TEMP-Get-rid-of-TBMStatus.patchDownload+5-126
v2-0003-Split-RT_SET.patchtext/x-patch; charset=US-ASCII; name=v2-0003-Split-RT_SET.patchDownload+46-16
On 9/24/25 11:30, John Naylor wrote:
Attached is PoC v2, which fully replaces the hash table with radix
tree and passes regression tests.
Cool!
The architecture is still one of tidbitmap.c having it's own tree of
(fixed length) PagetableEntries. The eventual goal is for vacuum and
tidbitmap to both use TID store in some form, but there are some
architectural issues to work out first.
That sounds reasonable.
0001 temporarily gets rid of the tidbitmap one-page cache so I could
always count on the hash table and radix tree having matching contents
during development, barring bugs. I'm curious how important it is, so
it doesn't yet get put back in this patch set.
0002 temporarily gets turns off page lossification for the same reason
of matching contents. (Still not reimplemented)
0003 makes some radix tree APIs more similar to simplehash, in a
backward compatible way.
0004 is a step towards putting allocation of tree values in the
caller's hands. This will be even more necessary when doing unions and
intersections with variable-length bitmaps (not yet implemented).
0005 make it easier to add new tree APIs that aren't used everywhere.
0006 RT_BEGIN_ITERATE_AT, similar to simplehash. This is a
prerequisite for 0007, and could also be used for shared iteration.
This implementation is naive and may slow down iteration, but that can
easily be fixed.On Thu, Jul 3, 2025 at 3:43 PM I wrote:
Second issue: TBM intersection
1) Radix tree iteration assumes that no one will delete keys out from
under it. That includes the caller itself! This is not sufficient for
intersecting bitmaps. I kluged around it by saving
blocks-to-be-deleted in a stack array, but a proper solution could be
to (vigorous handwaving)
I'm not familiar with the radix tree code, but why does specifically
intersection need deletion? You could also create a new radix tree and
only enter TIDs present in both inputs?
I guess lossification will need deletions though?
With some cleanups, restoring lossification, and maybe performance
work, the above could be the basis of a viable commit, since it has a
fairly small blast radius and gets rid of the qsort steps. However, I
believe it won't quite be enough on its own for a major release. For
that, we'd want
* variable-length keys to save memory, which would add some complexity
to union and intersection steps
* probably direct iteration over the tree, rather than via reference
arrays, which add memory and are no longer as useful because we don't
need a separate sorting step
* integration with TID store in some form
* adjustments to costing?
Before spending more time on getting the details right, I would be
interested in some performance numbers about raw insert / construction
performance without lossification. Saving the sort (which is parallel
with [1]/messages/by-id/CAPsAnrn5yWsoWs8GhqwbwAJx1SeLxLntV54Biq0Z-J_E86Fnng@mail.gmail.com) and being better in the lossy-case is great but only as long
as we don't regress the common case.
One more property of the hash map based TIDBitmap to consider is its
incremental growth:
So I'm wondering: what is the (re-)allocation pattern of the radix tree?
Is it incrementally resized, e.g. node by node? If so, the amortized
insertion cost might be lower than we think with increasing number of TIDs.
This is because today, a major cost of large BitmapIndexScans is
repeatedly growing the simplehash.h-based hash table backing TIDBitmap
from initially 128 elements to its final size.
It would be great if we could overcome this bottleneck as well with the
radix tree approach. Together with [1]/messages/by-id/CAPsAnrn5yWsoWs8GhqwbwAJx1SeLxLntV54Biq0Z-J_E86Fnng@mail.gmail.com and [2]/messages/by-id/29f27b97-b3d7-476b-acae-832b3443b0c4@gmail.com this would further close
the gap compared to plain vanilla index scan.
--
David Geier
[1]: /messages/by-id/CAPsAnrn5yWsoWs8GhqwbwAJx1SeLxLntV54Biq0Z-J_E86Fnng@mail.gmail.com
/messages/by-id/CAPsAnrn5yWsoWs8GhqwbwAJx1SeLxLntV54Biq0Z-J_E86Fnng@mail.gmail.com
[2]: /messages/by-id/29f27b97-b3d7-476b-acae-832b3443b0c4@gmail.com
/messages/by-id/29f27b97-b3d7-476b-acae-832b3443b0c4@gmail.com
It would be great if we could overcome this bottleneck as well with the
radix tree approach. Together with [1] and [2] this would further close
the gap compared to plain vanilla index scan.[1]
/messages/by-id/CAPsAnrn5yWsoWs8GhqwbwAJx1SeLxLntV54Biq0Z-J_E86Fnng@mail.gmail.com
[2]
/messages/by-id/29f27b97-b3d7-476b-acae-832b3443b0c4@gmail.com
I missed [3]/messages/by-id/CAEEBa3U7QDChb=PjzQGHoie-tOnfr7dumhO12Tp=5o2iT4t_ug@mail.gmail.com which is yet another improvement to reduce the overhead of
BIS. For overview purposes we have:
1. Parallelizing Bitmap Index Scan and Bitmap And/Or
2. Optimizing the block sort via sort_template.h
3. Improving TID insertion performance by batching
4. Using radix tree instead of simplehash.h in TIDBitmap
--
David Geier
[3]: /messages/by-id/CAEEBa3U7QDChb=PjzQGHoie-tOnfr7dumhO12Tp=5o2iT4t_ug@mail.gmail.com
/messages/by-id/CAEEBa3U7QDChb=PjzQGHoie-tOnfr7dumhO12Tp=5o2iT4t_ug@mail.gmail.com
On Wed, Oct 14, 2026 at 6:14 AM David Geier <geidav.pg@gmail.com> wrote:
On 9/24/25 11:30, John Naylor wrote:
On Thu, Jul 3, 2025 at 3:43 PM I wrote:
Second issue: TBM intersection
1) Radix tree iteration assumes that no one will delete keys out from
under it. That includes the caller itself! This is not sufficient for
intersecting bitmaps. I kluged around it by saving
blocks-to-be-deleted in a stack array, but a proper solution could be
to (vigorous handwaving)I'm not familiar with the radix tree code, but why does specifically
intersection need deletion?
Currently, tbm_intersect_page() already reports that a page can be
deleted so that the caller can delete it, so I was just continuing
that practice.
BTW, my local branch has deletion-during-iteration working, and it's
not too ugly.
You could also create a new radix tree and
only enter TIDs present in both inputs?I guess lossification will need deletions though?
Right.
Before spending more time on getting the details right, I would be
interested in some performance numbers about raw insert / construction
performance without lossification. Saving the sort (which is parallel
with [1]) and being better in the lossy-case is great but only as long
as we don't regress the common case.
Already on my get-back-to list, maybe in November. (note for would-be
volunteers: the latest patch in this thread is _not_ ready for
testing.)
One more property of the hash map based TIDBitmap to consider is its
incremental growth:So I'm wondering: what is the (re-)allocation pattern of the radix tree?
Is it incrementally resized, e.g. node by node? If so, the amortized
insertion cost might be lower than we think with increasing number of TIDs.This is because today, a major cost of large BitmapIndexScans is
repeatedly growing the simplehash.h-based hash table backing TIDBitmap
from initially 128 elements to its final size.
How much of a major cost?
Related: I do suspect the lossification threshold is broken: With 4MB
work_mem, the hash table grows from 3MB to 6MB, but the max entries
number is computed from 4MB (accounting for the iteration arrays), so
I believe (but haven't verified) that it starts lossifying when the
hash table is not even close to full. But if we tried to account for
that somehow, then we still risk the table growing from 6MB to 12MB
because of the linear probe length threshold. That is to say, yes,
there are multiple reasons to switch to radix tree if the challenges
can be overcome, and I think they can.
--
John Naylor
Amazon Web Services
I'm not familiar with the radix tree code, but why does specifically
intersection need deletion?Currently, tbm_intersect_page() already reports that a page can be
deleted so that the caller can delete it, so I was just continuing
that practice.
It seems like some of the TIDBitmap operations might be better
implemented differently on top of a radix tree. For example union and
intersection could profit from iterating both trees to compute the
intersection / union in the spirit of a merge sort.
But I can see the appeal to make TIDBitmap first work as is on top of
the radix tree, as long as it doesn't put hard-to-implement requirements
on the radix tree.
BTW, my local branch has deletion-during-iteration working, and it's
not too ugly.
Nice!
Before spending more time on getting the details right, I would be
interested in some performance numbers about raw insert / construction
performance without lossification. Saving the sort (which is parallel
with [1]) and being better in the lossy-case is great but only as long
as we don't regress the common case.Already on my get-back-to list, maybe in November. (note for would-be
volunteers: the latest patch in this thread is _not_ ready for
testing.)
I'm ready to help, once you have a patch that is ready for testing. Let
me know.
One more property of the hash map based TIDBitmap to consider is its
incremental growth:So I'm wondering: what is the (re-)allocation pattern of the radix tree?
Is it incrementally resized, e.g. node by node? If so, the amortized
insertion cost might be lower than we think with increasing number of TIDs.This is because today, a major cost of large BitmapIndexScans is
repeatedly growing the simplehash.h-based hash table backing TIDBitmap
from initially 128 elements to its final size.How much of a major cost?
The "actual time" of the BIS in my test query dropped from 193 ms to 139
ms when reserving all memory of the final hash table upfront by passing
its final size to pagetable_create() in tbm_create_pagetable().
Part of that time is spent on page faults because the allocations are
big enough for ASET to call malloc() and for malloc() to call sbrk(),
and then in the other direction for free() to call systrim() which calls
sbrk() to shrink the heap again.
This can be seen also from the perf stat output:
3,034 page-faults # 5114.8 faults/sec page_faults_per_second
vs when reserving enough memory to avoid resizing
0 page-faults # 0.0 faults/sec page_faults_per_second
I'm not entirely sure why it has 0 page faults for successive calls when
I reserve a big hash table. It should still have to fault when writing
to the pages of the initially reserved hash map. It seems to cache the
allocation somehow as I no longer see calls to sbrk().
My take away beyond the radix tree effort is that it would be useful if
we could allocate anonymous and shared memory backed by 2 MiB huge
pages. That would help for TIDBitmap but also for large sort buffers or
large hash join hash tables. Shared allocations can end up in the main
memory segment if dynamic_shared_memory_size is picked appropriately
but, FWIR, anonymous memory is always 4k-backed.
Related: I do suspect the lossification threshold is broken: With 4MB
work_mem, the hash table grows from 3MB to 6MB, but the max entries
number is computed from 4MB (accounting for the iteration arrays), so
I believe (but haven't verified) that it starts lossifying when the
hash table is not even close to full. But if we tried to account for
that somehow, then we still risk the table growing from 6MB to 12MB
because of the linear probe length threshold. That is to say, yes,
there are multiple reasons to switch to radix tree if the challenges
can be overcome, and I think they can.
Ahh, that's a good one. It makes sense because max entries is not
updated when simplehash.h grows. Then the lossification will start too
early.
What would be useful is a list of shortcomings of simplehash.h-based
TIDBitmap and how the radix tree would overcome those. It doesn't have
to complete but I feel like I'm not having the whole picture. We could
then see if there are more areas where a radix tree would help (e.g. how
certain TBMBitmap operations are implemented, etc.).
--
David Geier
This is because today, a major cost of large BitmapIndexScans is
repeatedly growing the simplehash.h-based hash table backing TIDBitmap
from initially 128 elements to its final size.How much of a major cost?
The "actual time" of the BIS in my test query dropped from 193 ms to 139
ms when reserving all memory of the final hash table upfront by passing
its final size to pagetable_create() in tbm_create_pagetable().Part of that time is spent on page faults because the allocations are
big enough for ASET to call malloc() and for malloc() to call sbrk(),
and then in the other direction for free() to call systrim() which calls
sbrk() to shrink the heap again.This can be seen also from the perf stat output:
3,034 page-faults # 5114.8 faults/sec page_faults_per_second
vs when reserving enough memory to avoid resizing
0 page-faults # 0.0 faults/sec page_faults_per_second
I'm not entirely sure why it has 0 page faults for successive calls when
I reserve a big hash table. It should still have to fault when writing
to the pages of the initially reserved hash map. It seems to cache the
allocation somehow as I no longer see calls to sbrk().My take away beyond the radix tree effort is that it would be useful if
we could allocate anonymous and shared memory backed by 2 MiB huge
pages. That would help for TIDBitmap but also for large sort buffers or
large hash join hash tables. Shared allocations can end up in the main
memory segment if dynamic_shared_memory_size is picked appropriately
but, FWIR, anonymous memory is always 4k-backed.
Closer inspection revealed that all allocation functions that return
zeroed memory, such as palloc0(), palloc_extended(MCXT_ALLOC_ZERO),
repalloc0(), MemoryContextAllocZero(), etc., unconditionally call
memset() in the wrapper around the memory context, rather than leaving
that to the memory context.
Leaving the zeroing to the memory context would have the advantage of
the memory context being able to skip it, if it knows that the memory
it's going to return is already zeroed.
It would know that, for example, if we used calloc() to serve big zeroed
allocations. On Linux, calloc() skips zeroing the memory if it comes
fresh from sbrk or mmap (via the zero-fill-on-demand technique using a
zero page).
Performance is only one aspect here. The other aspect is memory
consumption: large zeroed allocations that is only sparsely written to
will unnecessarily get all pages faulted in which makes a difference
with vm.memory_overcommit = 0/1.
It could also prevent OOM situations: say we create a large simplehash.h
map, created based on estimated row count, but actual row count is much
lower.
--
David Geier
On Fri, Oct 16, 2026, John Naylor wrote:
On Wed, Oct 14, 2026, David Geier wrote:
This is because today, a major cost of large BitmapIndexScans is
repeatedly growing the simplehash.h-based hash table backing TIDBitmap
from initially 128 elements to its final size.How much of a major cost?
I measured this while chasing a many-client slowdown, and have posted a
patch for it in a new thread [1]/messages/by-id/tidbitmap-cache-v1.20261108.b957201f@burd.me.
The short answer: on its own the growth is a few percent, and the page
faults it causes are the rest. The test was a count(*) whose bitmap
covers ~17k heap pages, glibc defaults, data cached. Removing most of
the growth and its page faults was worth +13.5% at 1 client and +56.8%
at 96 clients on a 96-thread host. Under jemalloc, where hardly
anything faults, only the rehash and copy of the doubling is left, and
removing it was worth +4.9% at 1 client and +6.3% at 96.
On Tue, Oct 20, 2026, David Geier wrote:
I'm not entirely sure why it has 0 page faults for successive calls
when I reserve a big hash table. It should still have to fault when
writing to the pages of the initially reserved hash map. It seems to
cache the allocation somehow as I no longer see calls to sbrk().
I saw the same. Unpresized, each grow from 256 slots up is a fresh
malloc that glibc carves from the top of the heap with brk, and at
ExecutorEnd free() trims the heap back with brk, so the next query
faults the same pages in again. I counted about 5.7 brk calls and ~730
page faults per query. With the table presized there were no brk calls
at all, so glibc kept that memory between queries. I haven't worked out
from the glibc source why the one big allocation stays and the doubling
chain doesn't.
Under concurrency those faults are the expensive part: at 96 clients,
27% of all cycles were in native_queued_spin_lock_slowpath under
folio_lruvec_lock_irqsave. Presizing removes them only when the
estimate is right (4 rows estimated against 17k for "@>" on two int4[]
elements, in my test), and under musl, or glibc with a low mmap
threshold, it only roughly halved them. So the patch keeps the
pagetable memory per backend instead, bounded by work_mem. Details and
numbers are in the new thread.
On Tue, Oct 20, 2026, David Geier wrote:
Leaving the zeroing to the memory context would have the advantage of
the memory context being able to skip it, if it knows that the memory
it's going to return is already zeroed.
I tried that in a prototype last month. Allocating without
MCXT_ALLOC_ZERO and setting only each slot's status byte made no
difference to the faults (732 vs 732 per query): with 48-byte slots, one
status byte per slot touches every page anyway. The memset costs CPU,
not faults. calloc() was worse, 1,161 faults per query, which I did not
track down.
How this relates to the radix tree: the patch only changes how
tidbitmap.c allocates and frees the simplehash arrays and the sorted
page list, so the radix tree would replace most of it.
The question it might help answer is whether the tree's memory survives
from one query to the next. If the nodes come from a memory context
that is deleted at ExecutorEnd, whether glibc keeps or returns those
blocks depends on their sizes, so it needs measuring. If it does return
them, the same approach would carry over. Keep the tree's context per
backend, bounded by work_mem, and give it back with a reset callback. I
can run the same reproducer against a radix-tree patch once one is ready
for testing.
On the lossify threshold: the patch doesn't change it. At 4 MB
work_mem maxentries is 65,536, and a 65,536-slot table grows at 58,982
members, so the table still reaches 131,072 slots (6 MB) before lossify
starts. The patch never caches an array larger than work_mem, so a
table like that finishes the old way.
best.
-greg
[1]: /messages/by-id/tidbitmap-cache-v1.20261108.b957201f@burd.me
Hi Greg!
The short answer: on its own the growth is a few percent, and the page
faults it causes are the rest. The test was a count(*) whose bitmap
covers ~17k heap pages, glibc defaults, data cached. Removing most of
the growth and its page faults was worth +13.5% at 1 client and +56.8%
at 96 clients on a 96-thread host. Under jemalloc, where hardly
anything faults, only the rehash and copy of the doubling is left, and
removing it was worth +4.9% at 1 client and +6.3% at 96.
Hah, I knew I saw it show up in a production instance. Hadn't realized
it came from running many queries concurrently. Cool you found that.
On Tue, Oct 20, 2026, David Geier wrote:
I'm not entirely sure why it has 0 page faults for successive calls
when I reserve a big hash table. It should still have to fault when
writing to the pages of the initially reserved hash map. It seems to
cache the allocation somehow as I no longer see calls to sbrk().I saw the same. Unpresized, each grow from 256 slots up is a fresh
malloc that glibc carves from the top of the heap with brk, and at
ExecutorEnd free() trims the heap back with brk, so the next query
faults the same pages in again. I counted about 5.7 brk calls and ~730
page faults per query. With the table presized there were no brk calls
at all, so glibc kept that memory between queries. I haven't worked out
from the glibc source why the one big allocation stays and the doubling
chain doesn't.
A shot into the dark: sbrk() can only shrink if there's no allocation
sticking around after the freed memory. Maybe that causes the virtual
memory to not be returned in some cases?
Under concurrency those faults are the expensive part: at 96 clients,
27% of all cycles were in native_queued_spin_lock_slowpath under
folio_lruvec_lock_irqsave. Presizing removes them only when the
estimate is right (4 rows estimated against 17k for "@>" on two int4[]
elements, in my test), and under musl, or glibc with a low mmap
threshold, it only roughly halved them. So the patch keeps the
pagetable memory per backend instead, bounded by work_mem. Details and
numbers are in the new thread.
Replying to that part in the other thread.
Leaving the zeroing to the memory context would have the advantage of
the memory context being able to skip it, if it knows that the memory
it's going to return is already zeroed.I tried that in a prototype last month. Allocating without
MCXT_ALLOC_ZERO and setting only each slot's status byte made no
difference to the faults (732 vs 732 per query): with 48-byte slots, one
status byte per slot touches every page anyway. The memset costs CPU,
not faults. calloc() was worse, 1,161 faults per query, which I did not
track down.
Yeah, writing just the status byte vs zeroing everything shouldn't make
much of a difference. The majority of the cost is the page fault. But
it's curious that calloc() didn't help. Did you remove writing to the
status byte after changing to calloc()?
How this relates to the radix tree: the patch only changes how
tidbitmap.c allocates and frees the simplehash arrays and the sorted
page list, so the radix tree would replace most of it.The question it might help answer is whether the tree's memory survives
from one query to the next. If the nodes come from a memory context
that is deleted at ExecutorEnd, whether glibc keeps or returns those
blocks depends on their sizes, so it needs measuring. If it does return
them, the same approach would carry over. Keep the tree's context per
backend, bounded by work_mem, and give it back with a reset callback. I
can run the same reproducer against a radix-tree patch once one is ready
for testing.
The more important part is that the radix tree grows incrementally while
the hash map needs to double in size and then be rehashed. That lowers
the number of page faults considerably.
--
David Geier
On Tuesday, November 10th, 2026 at 11:38 AM, David Geier <geidav.pg@gmail.com> wrote:
Hi Greg!
Hey David. :)
The short answer: on its own the growth is a few percent, and the page
faults it causes are the rest. The test was a count(*) whose bitmap
covers ~17k heap pages, glibc defaults, data cached. Removing most of
the growth and its page faults was worth +13.5% at 1 client and +56.8%
at 96 clients on a 96-thread host. Under jemalloc, where hardly
anything faults, only the rehash and copy of the doubling is left, and
removing it was worth +4.9% at 1 client and +6.3% at 96.Hah, I knew I saw it show up in a production instance. Hadn't realized
it came from running many queries concurrently. Cool you found that.
Glad I could solve that mystery for you.
On Tue, Oct 20, 2026, David Geier wrote:
I'm not entirely sure why it has 0 page faults for successive calls
when I reserve a big hash table. It should still have to fault when
writing to the pages of the initially reserved hash map. It seems to
cache the allocation somehow as I no longer see calls to sbrk().I saw the same. Unpresized, each grow from 256 slots up is a fresh
malloc that glibc carves from the top of the heap with brk, and at
ExecutorEnd free() trims the heap back with brk, so the next query
faults the same pages in again. I counted about 5.7 brk calls and ~730
page faults per query. With the table presized there were no brk calls
at all, so glibc kept that memory between queries. I haven't worked out
from the glibc source why the one big allocation stays and the doubling
chain doesn't.A shot into the dark: sbrk() can only shrink if there's no allocation
sticking around after the freed memory. Maybe that causes the virtual
memory to not be returned in some cases?
That's as good a guess as any at this point. I could keep digging, but is
there a point to it now? I do like allocators [1]https://codeberg.org/gregburd/libumem so it's not like I'm
not interested...
Under concurrency those faults are the expensive part: at 96 clients,
27% of all cycles were in native_queued_spin_lock_slowpath under
folio_lruvec_lock_irqsave. Presizing removes them only when the
estimate is right (4 rows estimated against 17k for "@>" on two int4[]
elements, in my test), and under musl, or glibc with a low mmap
threshold, it only roughly halved them. So the patch keeps the
pagetable memory per backend instead, bounded by work_mem. Details and
numbers are in the new thread.Replying to that part in the other thread.
Saw that, thanks!
Leaving the zeroing to the memory context would have the advantage of
the memory context being able to skip it, if it knows that the memory
it's going to return is already zeroed.I tried that in a prototype last month. Allocating without
MCXT_ALLOC_ZERO and setting only each slot's status byte made no
difference to the faults (732 vs 732 per query): with 48-byte slots, one
status byte per slot touches every page anyway. The memset costs CPU,
not faults. calloc() was worse, 1,161 faults per query, which I did not
track down.Yeah, writing just the status byte vs zeroing everything shouldn't make
much of a difference. The majority of the cost is the page fault. But
it's curious that calloc() didn't help. Did you remove writing to the
status byte after changing to calloc()?
I'll have to go back and look, I don't recall off hand. I agree about the
page fault.
How this relates to the radix tree: the patch only changes how
tidbitmap.c allocates and frees the simplehash arrays and the sorted
page list, so the radix tree would replace most of it.The question it might help answer is whether the tree's memory survives
from one query to the next. If the nodes come from a memory context
that is deleted at ExecutorEnd, whether glibc keeps or returns those
blocks depends on their sizes, so it needs measuring. If it does return
them, the same approach would carry over. Keep the tree's context per
backend, bounded by work_mem, and give it back with a reset callback. I
can run the same reproducer against a radix-tree patch once one is ready
for testing.The more important part is that the radix tree grows incrementally while
the hash map needs to double in size and then be rehashed. That lowers
the number of page faults considerably.
That is true, and should help (or hide?) the issue.
--
David Geier
best.
-greg