15
Building the B-Tree
Sorting is employed
All rows are sorted first.
Then the rows are gathered into leaf index pages
The pointers are collected into next-level pages.
and so on until the root is reached
Can run in parallel
max_parallel_maintenance_workers
Constraint
maintenance_work_mem, as the operation is not frequent
When building an index (specifically a B-tree), the server would add records
to an empty index one at a time, processing the table's rows in sequence.
However, this approach is highly inefficient.
Therefore, sorting is used when creating or rebuilding indexes: all table rows
are sorted and then organized into leaf index pages. The upper levels of the
tree, consisting of references to elements from lower-level pages, are then
constructed until only one page remains — this becomes the root of the tree.
Sorting is done in the same way as described earlier. However, the memory
limit is not determined by work_mem but by maintenance_work_mem, as
the index creation process isn't too frequent and it's beneficial to allocate
more memory for it.
Index building can be done in parallel. Number of worker processes the
number of worker processes is determined similarly to parallel queries
(depending on the table size), but is limited by the
max_parallel_maintenance_worker parameter.