1585 GistEntryVector *entryvec = (GistEntryVector *)PG_GETARG_POINTER(0);
1587 GIST_SPLITVEC *v = (GIST_SPLITVEC *)PG_GETARG_POINTER(1);
1592 OffsetNumber **list;
1595 GIDX *box_pageunion;
1598 bool all_entries_equal =
true;
1599 OffsetNumber max_offset;
1600 int nbytes, ndims_pageunion, d;
1601 int posmin = entryvec->n;
1603 POSTGIS_DEBUG(4,
"[GIST] 'picksplit' function called");
1609 max_offset = entryvec->n - 1;
1610 box_current = (GIDX *)PG_DETOAST_DATUM(entryvec->vector[FirstOffsetNumber].key);
1614 for (i = OffsetNumberNext(FirstOffsetNumber); i <= max_offset; i = OffsetNumberNext(i))
1616 box_current = (GIDX *)PG_DETOAST_DATUM(entryvec->vector[i].key);
1618 if (all_entries_equal && !
gidx_equals(box_pageunion, box_current))
1619 all_entries_equal =
false;
1624 POSTGIS_DEBUGF(3,
"[GIST] box_pageunion: %s", gidx_to_string(box_pageunion));
1627 if (all_entries_equal)
1629 POSTGIS_DEBUG(4,
"[GIST] picksplit finds all entries equal!");
1631 PG_RETURN_POINTER(v);
1635 nbytes = (max_offset + 2) *
sizeof(OffsetNumber);
1636 ndims_pageunion = GIDX_NDIMS(box_pageunion);
1637 POSTGIS_DEBUGF(4,
"[GIST] ndims_pageunion == %d", ndims_pageunion);
1638 pos = palloc(2 * ndims_pageunion *
sizeof(
int));
1639 list = palloc(2 * ndims_pageunion *
sizeof(OffsetNumber *));
1640 box_union = palloc(2 * ndims_pageunion *
sizeof(GIDX *));
1641 for (d = 0; d < ndims_pageunion; d++)
1643 list[
BELOW(d)] = (OffsetNumber *)palloc(nbytes);
1644 list[
ABOVE(d)] = (OffsetNumber *)palloc(nbytes);
1645 box_union[
BELOW(d)] = gidx_new(ndims_pageunion);
1646 box_union[
ABOVE(d)] = gidx_new(ndims_pageunion);
1656 POSTGIS_DEBUG(4,
"[GIST] 'picksplit' calculating best split axis");
1657 for (i = FirstOffsetNumber; i <= max_offset; i = OffsetNumberNext(i))
1659 box_current = (GIDX *)PG_DETOAST_DATUM(entryvec->vector[i].key);
1661 for (d = 0; d < ndims_pageunion; d++)
1663 if (GIDX_GET_MIN(box_current, d) - GIDX_GET_MIN(box_pageunion, d) <
1664 GIDX_GET_MAX(box_pageunion, d) - GIDX_GET_MAX(box_current, d))
1666 list[
BELOW(d)], &(box_union[
BELOW(d)]), box_current, &(pos[
BELOW(d)]), i);
1669 list[
ABOVE(d)], &(box_union[
ABOVE(d)]), box_current, &(pos[
ABOVE(d)]), i);
1686 double *avgCenter = palloc(ndims_pageunion *
sizeof(
double));
1688 for (d = 0; d < ndims_pageunion; d++)
1691 POSTGIS_DEBUG(4,
"[GIST] picksplit can't find good split axis, trying center point method");
1693 for (i = FirstOffsetNumber; i <= max_offset; i = OffsetNumberNext(i))
1695 box_current = (GIDX *)PG_DETOAST_DATUM(entryvec->vector[i].key);
1696 for (d = 0; d < ndims_pageunion; d++)
1697 avgCenter[d] += (GIDX_GET_MAX(box_current, d) + GIDX_GET_MIN(box_current, d)) / 2.0;
1699 for (d = 0; d < ndims_pageunion; d++)
1701 avgCenter[d] /= max_offset;
1703 POSTGIS_DEBUGF(4,
"[GIST] picksplit average center point[%d] = %.12g", d, avgCenter[d]);
1707 for (i = FirstOffsetNumber; i <= max_offset; i = OffsetNumberNext(i))
1710 box_current = (GIDX *)PG_DETOAST_DATUM(entryvec->vector[i].key);
1712 for (d = 0; d < ndims_pageunion; d++)
1714 center = (GIDX_GET_MIN(box_current, d) + GIDX_GET_MAX(box_current, d)) / 2.0;
1715 if (center < avgCenter[d])
1717 list[
BELOW(d)], &(box_union[
BELOW(d)]), box_current, &(pos[
BELOW(d)]), i);
1718 else if (FPeq(center, avgCenter[d]))
1721 &(box_union[
ABOVE(d)]),
1727 &(box_union[
BELOW(d)]),
1733 list[
ABOVE(d)], &(box_union[
ABOVE(d)]), box_current, &(pos[
ABOVE(d)]), i);
1741 "[GIST] picksplit still cannot find a good split! just cutting the node in half");
1743 PG_RETURN_POINTER(v);
1754 for (d = 0; d < ndims_pageunion; d++)
1756 int posd = Max(pos[
ABOVE(d)], pos[
BELOW(d)]);
1763 if (direction == -1 || posmin == entryvec->n)
1764 elog(ERROR,
"Error in building split, unable to determine split direction.");
1766 POSTGIS_DEBUGF(3,
"[GIST] 'picksplit' splitting on axis %d", direction);
1769 list[
BELOW(direction)],
1770 pos[
BELOW(direction)],
1771 &(box_union[
BELOW(direction)]),
1772 list[
ABOVE(direction)],
1773 pos[
ABOVE(direction)],
1774 &(box_union[
ABOVE(direction)]));
1776 POSTGIS_DEBUGF(4,
"[GIST] spl_ldatum: %s", gidx_to_string((GIDX *)v->spl_ldatum));
1777 POSTGIS_DEBUGF(4,
"[GIST] spl_rdatum: %s", gidx_to_string((GIDX *)v->spl_rdatum));
1781 "[GIST] axis %d: parent range (%.12g, %.12g) left range (%.12g, %.12g), right range (%.12g, %.12g)",
1783 GIDX_GET_MIN(box_pageunion, direction),
1784 GIDX_GET_MAX(box_pageunion, direction),
1785 GIDX_GET_MIN((GIDX *)v->spl_ldatum, direction),
1786 GIDX_GET_MAX((GIDX *)v->spl_ldatum, direction),
1787 GIDX_GET_MIN((GIDX *)v->spl_rdatum, direction),
1788 GIDX_GET_MAX((GIDX *)v->spl_rdatum, direction));
1790 PG_RETURN_POINTER(v);
static void gserialized_gist_picksplit_constructsplit(GIST_SPLITVEC *v, OffsetNumber *list1, int nlist1, GIDX **union1, OffsetNumber *list2, int nlist2, GIDX **union2)
static bool gserialized_gist_picksplit_badratios(int *pos, int dims)
static void gserialized_gist_picksplit_addlist(OffsetNumber *list, GIDX **box_union, GIDX *box_current, int *pos, int num)
GIDX * gidx_copy(GIDX *b)
bool gidx_equals(GIDX *a, GIDX *b)
static void gserialized_gist_picksplit_fallback(GistEntryVector *entryvec, GIST_SPLITVEC *v)
void gidx_merge(GIDX **b_union, GIDX *b_new)