Randomize B-Tree page split location to avoid oscillating patterns
Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.
You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:
docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t139501psql -h localhost -U postgresBuilt from patchset v14 (message #14), October 22, 2026 at 09:37 AM.
Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:
git clone --branch t139501_14 https://github.com/hackorum-dev/postgres.gitIn a checkout you already have, add the fork once:
git remote add hackorum https://github.com/hackorum-dev/postgres.gitthen, for this patchset and every later one:
git fetch hackorum t139501_14 && git checkout t139501_14Patchset v14 (message #14) is on t139501_14
TL;DR There seems to be a known phenomenon, where a data ingestion into a
B-Tree produces page splits following an oscillating pattern, which in turn
affects IO and buffer contention, impacting the performance. It turns out that
PostgreSQL is not an exception, but it should be possible to randomize a split
location a bit to mitigate the issue.
Hi,
Some time ago while working on models for PostgreSQL performance [1]https://zenodo.org/records/15786156 I've
stumbled upon an interesting oscillating patters around various B-Tree metrics
(number of page splits, index size, etc). This turned out to be a known thing
described in a catchy way as "Waves of misery" [2]Glombiewski N., Seeger B., Graefe G. (2019). Waves of Misery After Index Creation. BTW 2019. Gesellschaft für Informatik. doi:10.18420/btw2019-06, and boils down to the fact
that a fixed split location is usually chosen for a page split -- in this case
probabilities of page split lead to oscillating solutions under certain
workloads. Looks like the only pre-condition is that the data to be ingested
has the same distribution as the data already existing in the tree, in
particular UUIDs are in a bad position for this. And of course it's possible to
reproduce this with PostgreSQL.
Such an oscillation can lead to variability in IO and buffer contention,
negatively impacting performance. The fillfactor only shifts the waves, but do
not cancel them. One of the proposed remediation for this is to do suffix
truncation, which will "spread" split locations across some range. While we do
column suffix truncation, it turns out to be not enough for many workloads.
Another option is to randomize the split location, e.g. pick the actual
location from a range of 20% around the best one. In our case it's easy to do
randomization based on the split state, as all the possible split locations are
sorted by delta -- and all what's needed is to add a shift to the lowsplit from
a range, based on the number of split locations.
I've done few experiments with this, here is how it looks like:
* The first one is synthetic: a single column table with integer values, a
B-Tree index over it with the fillfactor=100, inserting new values one by one
from a uniform distribution via PGBench. In the graph "split.png" you can see
the number of page splits over time for the main branch and the patch (named
"Main" and "Rand" correspondingly), and the oscillating is clear for the
former one.
* Another one is following a data schema from one real projects out there, with
UUIDs as index values and very large records, with the default fillfactor=90.
The data ingestion is happening in large batches. The graph "split_batch.png"
represent the data for this case, with the main branch oscillating much more
than the randomized.
The unfortunate part is that I couldn't get clear numbers for the performance
impact. Turns out the disk in my experimental setup is not good enough to get a
sufficient number of inserts to trigger the issue, and to get nice graphs I was
running everything either on a RAM disk or on an unlogged table -- in both
cases it's easy to observe oscillations of page splits, but their impact is not
large enough since only so much IO is happening.
But anyway, any thoughts / commentaries on that?
[1]: https://zenodo.org/records/15786156
[2]: Glombiewski N., Seeger B., Graefe G. (2019). Waves of Misery After Index Creation. BTW 2019. Gesellschaft für Informatik. doi:10.18420/btw2019-06
Creation. BTW 2019. Gesellschaft für Informatik. doi:10.18420/btw2019-06
On Mon, Apr 27, 2026 at 12:24 PM Dmitry Dolgov <9erthalion6@gmail.com> wrote:
Some time ago while working on models for PostgreSQL performance [1] I've
stumbled upon an interesting oscillating patters around various B-Tree metrics
(number of page splits, index size, etc). This turned out to be a known thing
described in a catchy way as "Waves of misery" [2], and boils down to the fact
that a fixed split location is usually chosen for a page split -- in this case
probabilities of page split lead to oscillating solutions under certain
workloads.
I'm quite familiar with the paper, and find its argument convincing.
I'm not sure of the overall importance of the issues that they
describe, but the phenomenon is obviously real.
Such an oscillation can lead to variability in IO and buffer contention,
negatively impacting performance. The fillfactor only shifts the waves, but do
not cancel them. One of the proposed remediation for this is to do suffix
truncation, which will "spread" split locations across some range. While we do
column suffix truncation, it turns out to be not enough for many workloads.
Another option is to randomize the split location, e.g. pick the actual
location from a range of 20% around the best one. In our case it's easy to do
randomization based on the split state, as all the possible split locations are
sorted by delta -- and all what's needed is to add a shift to the lowsplit from
a range, based on the number of split locations.
My interpretation is that randomization can be combined with the usual
suffix truncation split point choosing heuristics; the paper even
recommends this at one point. That is, you randomly pick from a small
number of candidate split points that are all approximately equally
good according to the traditional criteria. It looks like your patch
doesn't account for suffix truncation at all, though.
Your patch should initially look for several split points that are
approximately equal in quality according to the current criteria -- a
separate, initial pass. You'd then randomly pick a final split point
from those gathered during this initial pass -- not from the original
fillfactormult-sorted list of split points. What you have in v1 will
make suffix truncation much less effective, which seems unacceptable.
Another issue is that nbtsort.c doesn't have any ability to pick among
split points; it focuses entirely on keeping free space balanced. To
get much benefit from this, I think you'd have to teach nbtsort.c to
also pick split points using approximately the same algorithm as
nbtsplitloc.c would (assuming retail inserts in ascending key space
order). I've been meaning to add proper suffix truncation to the
CREATE INDEX case.
I've done few experiments with this, here is how it looks like:
* The first one is synthetic: a single column table with integer values, a
B-Tree index over it with the fillfactor=100, inserting new values one by one
from a uniform distribution via PGBench. In the graph "split.png" you can see
the number of page splits over time for the main branch and the patch (named
"Main" and "Rand" correspondingly), and the oscillating is clear for the
former one.
I don't think that fillfactor=100 is very interesting in general.
The unfortunate part is that I couldn't get clear numbers for the performance
impact. Turns out the disk in my experimental setup is not good enough to get a
sufficient number of inserts to trigger the issue, and to get nice graphs I was
running everything either on a RAM disk or on an unlogged table -- in both
cases it's easy to observe oscillations of page splits, but their impact is not
large enough since only so much IO is happening.But anyway, any thoughts / commentaries on that?
I wouldn't expect the patch to increase absolute throughput
significantly, if at all. Its value comes from making the *rate* of
splits over time more consistent, for a given fixed workload. You
might notice a more interesting effect if you look at latency,
particularly worst case latency.
--
Peter Geoghegan
Hi,
On 2026-04-27 14:07:14 -0400, Peter Geoghegan wrote:
On Mon, Apr 27, 2026 at 12:24 PM Dmitry Dolgov <9erthalion6@gmail.com> wrote:
The unfortunate part is that I couldn't get clear numbers for the performance
impact. Turns out the disk in my experimental setup is not good enough to get a
sufficient number of inserts to trigger the issue, and to get nice graphs I was
running everything either on a RAM disk or on an unlogged table -- in both
cases it's easy to observe oscillations of page splits, but their impact is not
large enough since only so much IO is happening.But anyway, any thoughts / commentaries on that?
I wouldn't expect the patch to increase absolute throughput
significantly, if at all. Its value comes from making the *rate* of
splits over time more consistent, for a given fixed workload. You
might notice a more interesting effect if you look at latency,
particularly worst case latency.
Possibly somewhat orthogonal, but the worst case latency point reminded me of
something around this:
It seems like it's not great for contention avoidance / concurrency that right
now btree splits will often do IO - to write out a victim buffer and then to
extend the relation - while holding an exclusive lock on a page.
Couldn't we instead release the lock on the page, acquire an empty page,
reacquire the lock, recheck that the split is still needed and, if so, split
the page without needing to do IO while holding the lock?
Greetings,
Andres Freund
On Mon, Apr 27, 2026 at 02:07:14PM -0400, Peter Geoghegan wrote:
Sorry for the delay.
My interpretation is that randomization can be combined with the usual
suffix truncation split point choosing heuristics; the paper even
recommends this at one point. That is, you randomly pick from a small
number of candidate split points that are all approximately equally
good according to the traditional criteria. It looks like your patch
doesn't account for suffix truncation at all, though.Your patch should initially look for several split points that are
approximately equal in quality according to the current criteria -- a
separate, initial pass. You'd then randomly pick a final split point
from those gathered during this initial pass -- not from the original
fillfactormult-sorted list of split points. What you have in v1 will
make suffix truncation much less effective, which seems unacceptable.
The paper suggest combining randomization and suffix truncation via
randomly shifting the interval for truncation. I'm not sure a separate
pass is needed for that, looks like it should be enough to add random
shift to lowsplit / highsplit in _bt_bestsplitloc, when the penalty is
calculated.
The interesting part here is that it seems the current split interval
might be too small for such randomization to make a significant impact
-- few experiments show that with the current value the result looks
more like changing the fillfactor, waves are just shifting to the right.
Increasing the split interval helps in this case.
Such approach (randomized suffix truncation interval) produces the same
effect in a test with single column index as in the patch above. In a
test with a wider index and significant impact of truncation, there is
no visible degradation (and no oscillations either, as expected suffix
truncation introduces some randomness on it's own).
Another issue is that nbtsort.c doesn't have any ability to pick among
split points; it focuses entirely on keeping free space balanced. To
get much benefit from this, I think you'd have to teach nbtsort.c to
also pick split points using approximately the same algorithm as
nbtsplitloc.c would (assuming retail inserts in ascending key space
order). I've been meaning to add proper suffix truncation to the
CREATE INDEX case.
Good point, I need to look into this.
The unfortunate part is that I couldn't get clear numbers for the performance
impact. Turns out the disk in my experimental setup is not good enough to get a
sufficient number of inserts to trigger the issue, and to get nice graphs I was
running everything either on a RAM disk or on an unlogged table -- in both
cases it's easy to observe oscillations of page splits, but their impact is not
large enough since only so much IO is happening.But anyway, any thoughts / commentaries on that?
I wouldn't expect the patch to increase absolute throughput
significantly, if at all. Its value comes from making the *rate* of
splits over time more consistent, for a given fixed workload. You
might notice a more interesting effect if you look at latency,
particularly worst case latency.
That's exactly what I'm talking about. In those experiments I was trying
to get any visible changes in latency variability, but in this
particular setup I could spot nothing beyond regular noise, while the
number of page splits was obviously heavily oscillating.
On Wed, May 6, 2026 at 1:10 PM Dmitry Dolgov <9erthalion6@gmail.com> wrote:
On Mon, Apr 27, 2026 at 02:07:14PM -0400, Peter Geoghegan wrote:
Your patch should initially look for several split points that are
approximately equal in quality according to the current criteria -- a
separate, initial pass. You'd then randomly pick a final split point
from those gathered during this initial pass -- not from the original
fillfactormult-sorted list of split points. What you have in v1 will
make suffix truncation much less effective, which seems unacceptable.The paper suggest combining randomization and suffix truncation via
randomly shifting the interval for truncation. I'm not sure a separate
pass is needed for that, looks like it should be enough to add random
shift to lowsplit / highsplit in _bt_bestsplitloc, when the penalty is
calculated.
I can't see why that would make much difference. Increasing the split
interval usually won't affect the final chosen split point at all.
Decreasing is more likely to change the split point, but that's
*precisely* because it makes suffix truncation less effective.
In general it's quite unlikely that an expanded interval (e.g.,
doubling LEAF_SPLIT_DISTANCE) will include a split point that
truncates additional index attributes compared to the best split point
available within the current calculated split interval/current
LEAF_SPLIT_DISTANCE. For example, with the TPC-C indexes, I'd expect
only a tiny minority of all page splits to be affected by doubling
LEAF_SPLIT_DISTANCE to make nbtsplitloc.c consider ~20% of all
possible split points around the middle of the page. Whereas *halving*
LEAF_SPLIT_DISTANCE will indeed make suffix truncation worse.
This asymmetry matters. I believe that it'll make it impossible for
space utilization to average out to the target leaffillfactor% over
time. After all, this approach doesn't actually target space
utilization. This is also why it will make literally no difference at
all with a single column unique index.
The interesting part here is that it seems the current split interval
might be too small for such randomization to make a significant impact
-- few experiments show that with the current value the result looks
more like changing the fillfactor, waves are just shifting to the right.
Increasing the split interval helps in this case.
The point of gathering a list of "equally good" split points in an
initial pass is that it removes the danger of making the split
interval less effective in terms of final split penalty. The random
choice becomes a choice among split points that all truncate away the
same number of suffix attributes (all of which must still be
sufficiently close to the space-optimal split location to ensure that
no split is ever wildly unbalanced). This prevents the variable/random
choice from affecting the suffix truncation/split penalty.
With a single column unique index, all split points will have an equal
penalty under this scheme (implying that suffix truncation will be
equally effective). The initial list gathered in the first pass is
exactly all of the split points within the split interval in that
case. Whereas with other types of indexes the initial list gathered is
some subset of all the split points within the interval, without
regard for how balanced the post-split space utilization will be (all
splits within the interval are assumed to be "good enough" from a
space utilization perspective). Over time, space utilization should
reach fillfactor% -- without it negatively affecting suffix truncation.
It's possible that increasing the split interval would also make
sense. That seems like a follow-up question to me.
--
Peter Geoghegan
On Wed, May 06, 2026 at 01:49:10PM -0400, Peter Geoghegan wrote:
On Wed, May 6, 2026 at 1:10 PM Dmitry Dolgov <9erthalion6@gmail.com> wrote:On Mon, Apr 27, 2026 at 02:07:14PM -0400, Peter Geoghegan wrote:
Your patch should initially look for several split points that are
approximately equal in quality according to the current criteria -- a
separate, initial pass. You'd then randomly pick a final split point
from those gathered during this initial pass -- not from the original
fillfactormult-sorted list of split points. What you have in v1 will
make suffix truncation much less effective, which seems unacceptable.The paper suggest combining randomization and suffix truncation via
randomly shifting the interval for truncation. I'm not sure a separate
pass is needed for that, looks like it should be enough to add random
shift to lowsplit / highsplit in _bt_bestsplitloc, when the penalty is
calculated.I can't see why that would make much difference. Increasing the split
interval usually won't affect the final chosen split point at all.
Decreasing is more likely to change the split point, but that's
*precisely* because it makes suffix truncation less effective.
I assume by increasing/decreasing the split interval you mean increase/decrease
around the split point, but I was talking about shifting the interval to the
right. More about this in the next part.
The point of gathering a list of "equally good" split points in an
initial pass is that it removes the danger of making the split
interval less effective in terms of final split penalty. The random
choice becomes a choice among split points that all truncate away the
same number of suffix attributes (all of which must still be
sufficiently close to the space-optimal split location to ensure that
no split is ever wildly unbalanced). This prevents the variable/random
choice from affecting the suffix truncation/split penalty.
I see what you mean, but I'm concerned about the resulting overhead. Here is my
understanding of how it would work, let me know if something is missing in the
chain of thoughts:
* What happens currently is we collect all possible split points, sort them by
delta, calculate the split interval. Then we iterate over the possible split
points in the interval [0, split interval), trying to find the one causing
the lowest penalty. If what we found is the best possible penalty, we stop --
for unique indexes it means that virtually all the time everything is over
after a single iteration, since the first attribute is enough to
distinguish the split.
* If we're going to pick at random from a list of "equally good" split points,
everything up to the split interval is the same. Then we iterate over the
possible split points in the interval [0, split interval), and search for all
the points with penalty equal or lower than the current lowest value (the
list is reset if we found a lower value). We search this way until we found
enough for the chosen randomization interval (those 20% from the paper) or
until we're out of split points. In the end we've got a list of split points,
each has equally low penalty, and we choose a split point at random from this
list. For a unique index it means we have to do at least as many iterations
as the size of randomization interval, instead of one iteration as before --
this is the overhead I'm concerned about.
* If we're going to shift the split interval, everything is also the same up to
the split interval calculation. Now we introduce a
delta = random from (0, randomization interval)
and consider two intervals of available split points:
[0, delta) and [delta, split interval + delta)
We first search in [0, delta) as before for lowest possible penalty, just to
make sure we're not missing it, if it's not present in the second interval.
The main part is search in [delta, split interval + delta) for a split point
with the same or lower penalty as found in the first interval and use it as
the final result. There are few possible outcomes:
1. There are one or more lowest penalty split point in [0, delta) and one or
more in [delta, split interval + delta). Since delta is randomized, we pick
one of the "equally good" split points at random.
2. There are one or more lowest penalty split points in [0, delta) and for
some reason none in [delta, split interval + delta). In this case we forgo
randomization and go with the lowest penalty split point, using the point
from the [0, delta).
3. There are none lowest penalty split points in [0, delta) and one or more
in [delta, split interval + delta). In this case we still pick up one of the
"equally good" split points, still with some randomization.
4. There are none lowest penalty split points in [0, randomization interval)
and one or more in [randomization interval, split interval + randomization
interval). In this case we still pick up one of the "equally good" split
points, but without randomization, since the best point lays beyond the
allowed randomization range, and we hit it all the time.
Ultimately we would like to apply randomization of split point for mostly
unique indexes, since otherwise suffix truncation already does the job. With
this approach the randomization interval serves as a threshold to distinguish
such indexes -- if the index has mostly unique values, the optimal split
point would be located close to the start of the interval and multiple
"equally good" points will fall within the randomization interval. If on the
other hand the index has many duplicating values, the optimal split point may
be located outside the randomization interval and we would choose it all
the time -- but thanks to suffix truncation it doesn't matter.
From the overhead perspective, with this approach we do:
- one search in [0, delta), which will end after one iteration for unique
indexes.
- then do a "jump" of random distance and do one search in [delta, split
interval + delta) on the same conditions. It will end after one iteration
for unique index, and will process a similar number of iteration otherwise.
To summarize, in comparison with the second approach the third one seems to
have less overhead, the same guarantees regarding penalty, and more complex
implementation. I'm fine going with the second approach, but first would like
to discuss the alternatives and make sure we're on the same page.
On Thu, May 07, 2026 at 08:10:30PM +0200, Dmitry Dolgov wrote:
To summarize, in comparison with the second approach the third one seems to
have less overhead, the same guarantees regarding penalty, and more complex
implementation. I'm fine going with the second approach, but first would like
to discuss the alternatives and make sure we're on the same page.
Here is a sketch of what I had in mind. First two patches are metrics
only (adding USDT, I think 0001 makes sense to have in general, 0002 is
purely for benchmarking). The third patch implements strategy of
collecting equally good split points and choosing from them, and an
alternative version does the same via shifting the search interval
(attached with the txt suffix to not confuse the CF bot, whatever it
does nowadays).
I've managed to perform the same test as before (a best case scenario,
single column unique index) with a bit more throughput, and both
versions showed the same improvement over the main branch (see the
splits.png, where the patch versions are marked as "Rand (best loc)" and
"Rand (shifted)" for searching equally good split points and shifted
search interval correspondingly). It was also enough to produce visible
improvement for latency stddev as reported by pgbench (see stddev.png).
Measuring latency between newly introduced USDT inside _bt_findsplitloc
in this test shows that the latter version (shifted interval) is marginally
faster, probably due to the former needing to remember new data (equally good
split points):
// Time spent in _bt_findsplitloc for "shifted" version
@nsecs:
[4, 8) 2 | |
[8, 16) 9433 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
[16, 32) 11365 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|
[32, 64) 3940 |@@@@@@@@@@@@@@@@@@ |
[64, 128) 1255 |@@@@@ |
[128, 256) 595 |@@ |
[256, 512) 72 | |
[512, 1K) 0 | |
[1K, 2K) 0 | |
[2K, 4K) 1 | |
// Time spent in _bt_findsplitloc for "equally good points" version
@nsecs:
[4, 8) 1 | |
[8, 16) 8265 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
[16, 32) 10391 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|
[32, 64) 4982 |@@@@@@@@@@@@@@@@@@@@@@@@ |
[64, 128) 1155 |@@@@@ |
[128, 256) 698 |@@@ |
[256, 512) 79 | |
[512, 1K) 2 | |
[1K, 2K) 0 | |
[2K, 4K) 10 | |
Attachments:
v2-0001-Add-USDT-for-nbtree-page-splits-and-suffix-trunca.patchtext/plain; charset=us-asciiDownload+8-1
v2-0002-Add-USDT-for-split-location-benchmarking.patchtext/plain; charset=us-asciiDownload+8-1
v2-0003-Randomize-nbtree-split-location-to-avoid-oscillat.patchtext/plain; charset=iso-8859-1Download+49-5
v2-0003-Randomize-nbtree-split-location-to-avoid-oscillat.patch.txttext/plain; charset=iso-8859-1Download+48-3
splits.pngimage/pngDownload+2-0
stddev.pngimage/pngDownload
Hi Dmitry, Peter, Andres,
On Tue, Jun 16, 2026 at 10:13 AM Dmitry Dolgov wrote:
We search either until all the split points are evaluated, or we've
collected 20% of all possible locations in the list of equally good
split points.
I like Peter's formulation that the random choice should only be among
split points that are equally good according to the existing criteria.
I do not think v2 quite guarantees that yet.
state->splits is ordered by distance from the desired space balance, not
by _bt_split_penalty(). The loop can collect 20% of locations with the
best penalty seen so far and stop before reaching a lower penalty later
in the interval. The existing early exit is safe because it only stops
at perfectpenalty, which is a known lower bound.
There is also a special case in SPLIT_MANY_DUPLICATES. That strategy
widens the interval to the whole page, but still relies on delta order to
pick the nearest location with the required penalty. Randomizing among
equal-penalty locations changes that policy and might also avoid the
existing protection against repeatedly leaving unusable free space.
Would it be simpler to initially restrict randomization to
SPLIT_DEFAULT leaf splits, examine the whole existing interval, and use
reservoir sampling among locations with the true minimum penalty? That
would need no extra array and would preserve suffix truncation exactly.
The existing default interval is deliberately narrow, so I would first
measure the cost of scanning it completely before adding an early exit.
On Mon, Apr 27, 2026 at 7:49 PM Andres Freund wrote:
Couldn't we instead release the lock on the page, acquire an empty page,
reacquire the lock, recheck that the split is still needed and, if so,
split the page without needing to do IO while holding the lock?
Releasing and reacquiring the page lock sounds somewhat risky to me. A
concurrent inserter may change the page or split it, so this turns into a
restart protocol, with an acquired page that may no longer be needed.
Could we instead keep a small reserve of pages that can be acquired
without victim writeback or relation extension, replenished in the
background or in batches? Heap already uses ExtendBufferedRelBy() to
extend by multiple blocks and makes the extra pages available through a
BulkInsertState or the FSM, while _bt_allocbuf() requests exactly one
page. I am not sure whether a reserve belongs in bufmgr, the FSM, or the
access method, but it might avoid changing the B-tree locking protocol.
Thank you!
Best regards, Andrey Borodin.
On Mon, Aug 24, 2026 at 02:22:46PM +0300, Andrey Borodin wrote:
Thanks for looking into it.
state->splits is ordered by distance from the desired space balance, not
by _bt_split_penalty(). The loop can collect 20% of locations with the
best penalty seen so far and stop before reaching a lower penalty later
in the interval. The existing early exit is safe because it only stops
at perfectpenalty, which is a known lower bound.
v2 does not rely on the split points being sorted anymore, but reaching
20% before finding even better split location is indeed a bug in the
algorithm. I have to adjust the last condition to be something like
"we've reached 20% and it's a perfectpenalty", which will be equivalent
to the original implementation. Btw, I think the second approach,
nicknamed "shifted interval", is not affected by this since it uses very
same logic as in the original loop.
There is also a special case in SPLIT_MANY_DUPLICATES. That strategy
widens the interval to the whole page, but still relies on delta order to
pick the nearest location with the required penalty. Randomizing among
equal-penalty locations changes that policy and might also avoid the
existing protection against repeatedly leaving unusable free space.Would it be simpler to initially restrict randomization to
SPLIT_DEFAULT leaf splits,
Yeah, I've actually concentrated my efforts on SPLIT_DEFAULT and haven't
tested other strategies, so it sounds reasonable to apply randomization
only to this strategy.
examine the whole existing interval, and use
reservoir sampling among locations with the true minimum penalty? That
would need no extra array and would preserve suffix truncation exactly.
If I got you correct, we still would need to keep locations with the
true minimum penalty, so this part sounds similar to what already
happens in the v2.
Hi Dmitry,
On Mon, Aug 24, 2026 at 11:03 PM Dmitry Dolgov wrote:
Btw, I think the second approach, nicknamed "shifted interval", is not
affected by this since it uses very same logic as in the original loop.
Right, the early-stop problem does not apply to the shifted-interval
variant. Its two loops together scan
[0, rand_offset)
[rand_offset, highsplit + rand_offset)
rather than the original [0, highsplit) interval. The new upper bound is
not capped at state->nsplits and, even when capped, extends the search
beyond the original balance interval.
If I got you correct, we still would need to keep locations with the
true minimum penalty, so this part sounds similar to what already
happens in the v2.
We only need to keep one selected location and the number of equally
good locations seen so far. In pseudocode:
if (penalty < bestpenalty)
bestpenalty = penalty, selected = i, nmatches = 1;
else if (penalty == bestpenalty)
if (random(++nmatches) == 0)
selected = i;
After a full scan, this produces the same random choice among locations
with the true minimum penalty as collecting those locations in an array
and choosing an element at the end, without storing the array.
Separately, I followed up on my suggestion in response to Andres and
tried extending B-tree indexes in batches. PFA a prototype.
When _bt_allocbuf() exhausts the FSM, it extends the index by several
pages, returns the first page to the split, and records the rest in the
FSM. The batch grows geometrically for small indexes and is capped at
16 pages. This amortizes relation extension without changing the
B-tree locking protocol.
The act of extending a relation is not WAL-logged directly.
FSM updates are hints, but MarkBufferDirtyHint() can emit an
XLOG_FPI_FOR_HINT record, so an FSM page can reach a standby. The standby
can therefore have an FSM entry past the end of its main fork. This is
the issue exposed by heap bulk extension [0]/messages/by-id/20221029025420.eplyow6k7tgu6he3@awork3.anarazel.de. The prototype relies on
the later FSM fix [1]/messages/by-id/1878547.tdWV9SEqCh@aivenlaptop, which rejects such entries. Its test covers the
length difference and continued index use after promotion.
In a custom fixed-work pgbench test, 32 clients each performed 50,000
prepared single-row inserts with increasing int8 keys into an unlogged
table with a 3-million-entry B-tree. Batches of 16 reduced extension
calls from about 5,635 to 354 and extension time from about 96 ms to 9
ms. The three paired TPS changes were +4.5%, +1.9%, and +1.3%; p99
latency improved by about 12%. One client showed no throughput change,
while its p99.99 became worse, as expected for a rarer but larger
extension.
Best regards, Andrey Borodin.
[0]: /messages/by-id/20221029025420.eplyow6k7tgu6he3@awork3.anarazel.de
[1]: /messages/by-id/1878547.tdWV9SEqCh@aivenlaptop
Attachments:
t139501_100001-Extend-B-tree-indexes-in-batches.patchapplication/octet-stream; name=0001-Extend-B-tree-indexes-in-batches.patch; x-unix-mode=0644Download+115-3
On Tue, Aug 25, 2026 at 04:27:25PM +0500, Andrey Borodin wrote:
If I got you correct, we still would need to keep locations with the
true minimum penalty, so this part sounds similar to what already
happens in the v2.We only need to keep one selected location and the number of equally
good locations seen so far. In pseudocode:if (penalty < bestpenalty)
bestpenalty = penalty, selected = i, nmatches = 1;
else if (penalty == bestpenalty)
if (random(++nmatches) == 0)
selected = i;After a full scan, this produces the same random choice among locations
with the true minimum penalty as collecting those locations in an array
and choosing an element at the end, without storing the array.
I see, but it will also require more calls of random number generation,
and it's not obvious to me that this would have less overhead. Let me
experiment with this part, but otherwise I assume you find the patch
idea sound?
Hi Dmitry,
I see, but it will also require more calls of random number generation,
and it's not obvious to me that this would have less overhead.
Right. Reservoir sampling avoids the candidate array, but does not
necessarily use less CPU. My guess is that a xoroshiro128** call is cheaper
than another _bt_split_penalty() calculation, although the difference is
probably too small to matter here. Keeping the array is also fine once the
whole interval is examined and the candidates are reset whenever a lower
penalty is found.
Let me experiment with this part, but otherwise I assume you find the
patch idea sound?
Yes. The idea makes sense to me in general, with the initial scope restricted
to SPLIT_DEFAULT leaf splits. Picking randomly only among locations with the
true minimum penalty preserves the existing suffix-truncation criterion,
while the existing balance interval keeps every candidate reasonably close
to the target occupancy.
I would leave SPLIT_MANY_DUPLICATES and internal pages unchanged until we
have evidence that randomization helps them without weakening their current
policies. I think the important cost here is not a few random calls or a
short second pass, but additional complexity in nbtree, which already has
many interacting policies. From a large set of small possible improvements,
we should prefer a small set that are simple and orthogonal, particularly to
avoid bugs where their policies interact. So I would choose whichever
implementation keeps both this patch and its interaction with the existing
strategies simplest. The remaining question for the initial patch is
empirical. Randomization deliberately lets some pages reach their next split
earlier, spreading that work over time. The reduction in tail latency should
be statistically significant, while the resulting reduction in average page
occupancy should be statistically insignificant. The idea makes sense to me.
Best regards, Andrey Borodin.
On Sat, Aug 29, 2026 at 1:38 PM Andrey Borodin <x4mmm@yandex-team.ru> wrote:
Yes. The idea makes sense to me in general, with the initial scope restricted
to SPLIT_DEFAULT leaf splits. Picking randomly only among locations with the
true minimum penalty preserves the existing suffix-truncation criterion,
while the existing balance interval keeps every candidate reasonably close
to the target occupancy.
+1
I would leave SPLIT_MANY_DUPLICATES and internal pages unchanged until we
have evidence that randomization helps them without weakening their current
policies.
+1. I doubt that SPLIT_MANY_DUPLICATES is a good candidate for any
kind of randomized choice. It's only used when we have no choice but
to accept a lopsided split to keep a large group of duplicates
together on the same leaf page. Using an *even more* lopsided split
point is very risky because there's no reason to believe we'll
converge on fillfactor% utilization over time.
The remaining question for the initial patch is
empirical. Randomization deliberately lets some pages reach their next split
earlier, spreading that work over time. The reduction in tail latency should
be statistically significant, while the resulting reduction in average page
occupancy should be statistically insignificant. The idea makes sense to me.
When I developed the parts of nbtsplitloc.c that deal with suffix
truncation (including the related handling of large groups of
duplicates), I found it useful to mostly ignore fixed CPU costs
earlier on. It was more important to develop the right general
approach, based on a set of realistic-ish benchmarks. The test results
were perfectly deterministic, and I only cared about space
utilization, so iterating was fairly easy. I was able to deal with
added fixed costs later on (actually, I don't think that they were
ever really a problem).
My guess is that a similar approach will work best here. The only
notable difference is that the primary goal here is to make the rate
of page splits steady over time (where the unit of time is each
inserted tuple). Space utilization will still matter, and it is
something to keep an eye on early, but only to avoid regressions;
there's no reason to expect it to improve.
--
Peter Geoghegan
On Sat, Aug 29, 2026 at 01:57:04PM -0400, Peter Geoghegan wrote:
On Sat, Aug 29, 2026 at 1:38 PM Andrey Borodin <x4mmm@yandex-team.ru> wrote:Yes. The idea makes sense to me in general, with the initial scope restricted
to SPLIT_DEFAULT leaf splits. Picking randomly only among locations with the
true minimum penalty preserves the existing suffix-truncation criterion,
while the existing balance interval keeps every candidate reasonably close
to the target occupancy.+1
Here is the new versions. I've compared the reservoir sampling approach
with keeping an array of split locations, and in terms of performance in
my tests it looks like the former is only marginally faster than the
latter one (the test was a single-column integer index under insert-only
workload, where the size of the locations array gew up to ~30). But
eventually I've decided to stick to it, since it felt a bit more
naturally fitting to the code flow around.
I've also added a simple test to it. Originally I was already preparing
a new injection point and more elaborate test, only to realize that it
would be silly to verify that the split point is more variable now.
Instead the decision was to verify the end result -- more variable free
space distribution across index pages via building a histogram for it
and checking how many buckets we end up with.