PostgreSQL Source Code  git master
gistsplit.c File Reference
#include "postgres.h"
#include "access/gist_private.h"
#include "utils/rel.h"
Include dependency graph for gistsplit.c:

Go to the source code of this file.

Data Structures

struct  GistSplitUnion
 

Macros

#define SWAPVAR(s, d, t)
 

Functions

static void gistunionsubkeyvec (GISTSTATE *giststate, IndexTuple *itvec, GistSplitUnion *gsvp)
 
static void gistunionsubkey (GISTSTATE *giststate, IndexTuple *itvec, GistSplitVector *spl)
 
static int findDontCares (Relation r, GISTSTATE *giststate, GISTENTRY *valvec, GistSplitVector *spl, int attno)
 
static void removeDontCares (OffsetNumber *a, int *len, const bool *dontcare)
 
static void placeOne (Relation r, GISTSTATE *giststate, GistSplitVector *v, IndexTuple itup, OffsetNumber off, int attno)
 
static void supportSecondarySplit (Relation r, GISTSTATE *giststate, int attno, GIST_SPLITVEC *sv, Datum oldL, Datum oldR)
 
static void genericPickSplit (GISTSTATE *giststate, GistEntryVector *entryvec, GIST_SPLITVEC *v, int attno)
 
static bool gistUserPicksplit (Relation r, GistEntryVector *entryvec, int attno, GistSplitVector *v, IndexTuple *itup, int len, GISTSTATE *giststate)
 
static void gistSplitHalf (GIST_SPLITVEC *v, int len)
 
void gistSplitByKey (Relation r, Page page, IndexTuple *itup, int len, GISTSTATE *giststate, GistSplitVector *v, int attno)
 

Macro Definition Documentation

◆ SWAPVAR

#define SWAPVAR (   s,
  d,
 
)
Value:
do { \
(t) = (s); \
(s) = (d); \
(d) = (t); \
} while(0)

Definition at line 237 of file gistsplit.c.

Function Documentation

◆ findDontCares()

static int findDontCares ( Relation  r,
GISTSTATE giststate,
GISTENTRY valvec,
GistSplitVector spl,
int  attno 
)
static

Definition at line 113 of file gistsplit.c.

115 {
116  int i;
117  GISTENTRY entry;
118  int NumDontCare = 0;
119 
120  /*
121  * First, search the left-side tuples to see if any have zero penalty to
122  * be added to the right-side union key.
123  *
124  * attno column is known all-not-null (see gistSplitByKey), so we need not
125  * check for nulls
126  */
127  gistentryinit(entry, spl->splitVector.spl_rdatum, r, NULL,
128  (OffsetNumber) 0, false);
129  for (i = 0; i < spl->splitVector.spl_nleft; i++)
130  {
131  int j = spl->splitVector.spl_left[i];
132  float penalty = gistpenalty(giststate, attno, &entry, false,
133  &valvec[j], false);
134 
135  if (penalty == 0.0)
136  {
137  spl->spl_dontcare[j] = true;
138  NumDontCare++;
139  }
140  }
141 
142  /* And conversely for the right-side tuples */
143  gistentryinit(entry, spl->splitVector.spl_ldatum, r, NULL,
144  (OffsetNumber) 0, false);
145  for (i = 0; i < spl->splitVector.spl_nright; i++)
146  {
147  int j = spl->splitVector.spl_right[i];
148  float penalty = gistpenalty(giststate, attno, &entry, false,
149  &valvec[j], false);
150 
151  if (penalty == 0.0)
152  {
153  spl->spl_dontcare[j] = true;
154  NumDontCare++;
155  }
156  }
157 
158  return NumDontCare;
159 }
#define gistentryinit(e, k, r, pg, o, l)
Definition: gist.h:244
float gistpenalty(GISTSTATE *giststate, int attno, GISTENTRY *orig, bool isNullOrig, GISTENTRY *add, bool isNullAdd)
Definition: gistutil.c:724
int j
Definition: isn.c:74
int i
Definition: isn.c:73
uint16 OffsetNumber
Definition: off.h:24
int spl_nleft
Definition: gist.h:143
OffsetNumber * spl_right
Definition: gist.h:147
Datum spl_ldatum
Definition: gist.h:144
Datum spl_rdatum
Definition: gist.h:149
int spl_nright
Definition: gist.h:148
OffsetNumber * spl_left
Definition: gist.h:142
GIST_SPLITVEC splitVector
Definition: gist_private.h:237

References gistentryinit, gistpenalty(), i, j, GistSplitVector::spl_dontcare, GIST_SPLITVEC::spl_ldatum, GIST_SPLITVEC::spl_left, GIST_SPLITVEC::spl_nleft, GIST_SPLITVEC::spl_nright, GIST_SPLITVEC::spl_rdatum, GIST_SPLITVEC::spl_right, and GistSplitVector::splitVector.

Referenced by gistUserPicksplit().

◆ genericPickSplit()

static void genericPickSplit ( GISTSTATE giststate,
GistEntryVector entryvec,
GIST_SPLITVEC v,
int  attno 
)
static

Definition at line 344 of file gistsplit.c.

345 {
346  OffsetNumber i,
347  maxoff;
348  int nbytes;
349  GistEntryVector *evec;
350 
351  maxoff = entryvec->n - 1;
352 
353  nbytes = (maxoff + 2) * sizeof(OffsetNumber);
354 
355  v->spl_left = (OffsetNumber *) palloc(nbytes);
356  v->spl_right = (OffsetNumber *) palloc(nbytes);
357  v->spl_nleft = v->spl_nright = 0;
358 
359  for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
360  {
361  if (i <= (maxoff - FirstOffsetNumber + 1) / 2)
362  {
363  v->spl_left[v->spl_nleft] = i;
364  v->spl_nleft++;
365  }
366  else
367  {
368  v->spl_right[v->spl_nright] = i;
369  v->spl_nright++;
370  }
371  }
372 
373  /*
374  * Form union datums for each side
375  */
376  evec = palloc(sizeof(GISTENTRY) * entryvec->n + GEVHDRSZ);
377 
378  evec->n = v->spl_nleft;
379  memcpy(evec->vector, entryvec->vector + FirstOffsetNumber,
380  sizeof(GISTENTRY) * evec->n);
381  v->spl_ldatum = FunctionCall2Coll(&giststate->unionFn[attno],
382  giststate->supportCollation[attno],
383  PointerGetDatum(evec),
384  PointerGetDatum(&nbytes));
385 
386  evec->n = v->spl_nright;
387  memcpy(evec->vector, entryvec->vector + FirstOffsetNumber + v->spl_nleft,
388  sizeof(GISTENTRY) * evec->n);
389  v->spl_rdatum = FunctionCall2Coll(&giststate->unionFn[attno],
390  giststate->supportCollation[attno],
391  PointerGetDatum(evec),
392  PointerGetDatum(&nbytes));
393 }
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1149
#define GEVHDRSZ
Definition: gist.h:239
void * palloc(Size size)
Definition: mcxt.c:1317
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
#define FirstOffsetNumber
Definition: off.h:27
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
Oid supportCollation[INDEX_MAX_KEYS]
Definition: gist_private.h:97
FmgrInfo unionFn[INDEX_MAX_KEYS]
Definition: gist_private.h:87
GISTENTRY vector[FLEXIBLE_ARRAY_MEMBER]
Definition: gist.h:236
int32 n
Definition: gist.h:235

References FirstOffsetNumber, FunctionCall2Coll(), GEVHDRSZ, i, GistEntryVector::n, OffsetNumberNext, palloc(), PointerGetDatum(), GIST_SPLITVEC::spl_ldatum, GIST_SPLITVEC::spl_left, GIST_SPLITVEC::spl_nleft, GIST_SPLITVEC::spl_nright, GIST_SPLITVEC::spl_rdatum, GIST_SPLITVEC::spl_right, GISTSTATE::supportCollation, GISTSTATE::unionFn, and GistEntryVector::vector.

Referenced by gistUserPicksplit().

◆ gistSplitByKey()

void gistSplitByKey ( Relation  r,
Page  page,
IndexTuple itup,
int  len,
GISTSTATE giststate,
GistSplitVector v,
int  attno 
)

Definition at line 623 of file gistsplit.c.

625 {
626  GistEntryVector *entryvec;
627  OffsetNumber *offNullTuples;
628  int nOffNullTuples = 0;
629  int i;
630 
631  /* generate the item array, and identify tuples with null keys */
632  /* note that entryvec->vector[0] goes unused in this code */
633  entryvec = palloc(GEVHDRSZ + (len + 1) * sizeof(GISTENTRY));
634  entryvec->n = len + 1;
635  offNullTuples = (OffsetNumber *) palloc(len * sizeof(OffsetNumber));
636 
637  for (i = 1; i <= len; i++)
638  {
639  Datum datum;
640  bool IsNull;
641 
642  datum = index_getattr(itup[i - 1], attno + 1, giststate->leafTupdesc,
643  &IsNull);
644  gistdentryinit(giststate, attno, &(entryvec->vector[i]),
645  datum, r, page, i,
646  false, IsNull);
647  if (IsNull)
648  offNullTuples[nOffNullTuples++] = i;
649  }
650 
651  if (nOffNullTuples == len)
652  {
653  /*
654  * Corner case: All keys in attno column are null, so just transfer
655  * our attention to the next column. If there's no next column, just
656  * split page in half.
657  */
658  v->spl_risnull[attno] = v->spl_lisnull[attno] = true;
659 
660  if (attno + 1 < giststate->nonLeafTupdesc->natts)
661  gistSplitByKey(r, page, itup, len, giststate, v, attno + 1);
662  else
664  }
665  else if (nOffNullTuples > 0)
666  {
667  int j = 0;
668 
669  /*
670  * We don't want to mix NULL and not-NULL keys on one page, so split
671  * nulls to right page and not-nulls to left.
672  */
673  v->splitVector.spl_right = offNullTuples;
674  v->splitVector.spl_nright = nOffNullTuples;
675  v->spl_risnull[attno] = true;
676 
678  v->splitVector.spl_nleft = 0;
679  for (i = 1; i <= len; i++)
680  if (j < v->splitVector.spl_nright && offNullTuples[j] == i)
681  j++;
682  else
684 
685  /* Compute union keys, unless outer recursion level will handle it */
686  if (attno == 0 && giststate->nonLeafTupdesc->natts == 1)
687  {
688  v->spl_dontcare = NULL;
689  gistunionsubkey(giststate, itup, v);
690  }
691  }
692  else
693  {
694  /*
695  * All keys are not-null, so apply user-defined PickSplit method
696  */
697  if (gistUserPicksplit(r, entryvec, attno, v, itup, len, giststate))
698  {
699  /*
700  * Splitting on attno column is not optimal, so consider
701  * redistributing don't-care tuples according to the next column
702  */
703  Assert(attno + 1 < giststate->nonLeafTupdesc->natts);
704 
705  if (v->spl_dontcare == NULL)
706  {
707  /*
708  * This split was actually degenerate, so ignore it altogether
709  * and just split according to the next column.
710  */
711  gistSplitByKey(r, page, itup, len, giststate, v, attno + 1);
712  }
713  else
714  {
715  /*
716  * Form an array of just the don't-care tuples to pass to a
717  * recursive invocation of this function for the next column.
718  */
719  IndexTuple *newitup = (IndexTuple *) palloc(len * sizeof(IndexTuple));
720  OffsetNumber *map = (OffsetNumber *) palloc(len * sizeof(OffsetNumber));
721  int newlen = 0;
722  GIST_SPLITVEC backupSplit;
723 
724  for (i = 0; i < len; i++)
725  {
726  if (v->spl_dontcare[i + 1])
727  {
728  newitup[newlen] = itup[i];
729  map[newlen] = i + 1;
730  newlen++;
731  }
732  }
733 
734  Assert(newlen > 0);
735 
736  /*
737  * Make a backup copy of v->splitVector, since the recursive
738  * call will overwrite that with its own result.
739  */
740  backupSplit = v->splitVector;
741  backupSplit.spl_left = (OffsetNumber *) palloc(sizeof(OffsetNumber) * len);
742  memcpy(backupSplit.spl_left, v->splitVector.spl_left, sizeof(OffsetNumber) * v->splitVector.spl_nleft);
743  backupSplit.spl_right = (OffsetNumber *) palloc(sizeof(OffsetNumber) * len);
744  memcpy(backupSplit.spl_right, v->splitVector.spl_right, sizeof(OffsetNumber) * v->splitVector.spl_nright);
745 
746  /* Recursively decide how to split the don't-care tuples */
747  gistSplitByKey(r, page, newitup, newlen, giststate, v, attno + 1);
748 
749  /* Merge result of subsplit with non-don't-care tuples */
750  for (i = 0; i < v->splitVector.spl_nleft; i++)
751  backupSplit.spl_left[backupSplit.spl_nleft++] = map[v->splitVector.spl_left[i] - 1];
752  for (i = 0; i < v->splitVector.spl_nright; i++)
753  backupSplit.spl_right[backupSplit.spl_nright++] = map[v->splitVector.spl_right[i] - 1];
754 
755  v->splitVector = backupSplit;
756  }
757  }
758  }
759 
760  /*
761  * If we're handling a multicolumn index, at the end of the recursion
762  * recompute the left and right union datums for all index columns. This
763  * makes sure we hand back correct union datums in all corner cases,
764  * including when we haven't processed all columns to start with, or when
765  * a secondary split moved "don't care" tuples from one side to the other
766  * (we really shouldn't assume that that didn't change the union datums).
767  *
768  * Note: when we're in an internal recursion (attno > 0), we do not worry
769  * about whether the union datums we return with are sensible, since
770  * calling levels won't care. Also, in a single-column index, we expect
771  * that PickSplit (or the special cases above) produced correct union
772  * datums.
773  */
774  if (attno == 0 && giststate->nonLeafTupdesc->natts > 1)
775  {
776  v->spl_dontcare = NULL;
777  gistunionsubkey(giststate, itup, v);
778  }
779 }
#define Assert(condition)
Definition: c.h:861
static void gistunionsubkey(GISTSTATE *giststate, IndexTuple *itvec, GistSplitVector *spl)
Definition: gistsplit.c:80
void gistSplitByKey(Relation r, Page page, IndexTuple *itup, int len, GISTSTATE *giststate, GistSplitVector *v, int attno)
Definition: gistsplit.c:623
static bool gistUserPicksplit(Relation r, GistEntryVector *entryvec, int attno, GistSplitVector *v, IndexTuple *itup, int len, GISTSTATE *giststate)
Definition: gistsplit.c:415
static void gistSplitHalf(GIST_SPLITVEC *v, int len)
Definition: gistsplit.c:585
void gistdentryinit(GISTSTATE *giststate, int nkey, GISTENTRY *e, Datum k, Relation r, Page pg, OffsetNumber o, bool l, bool isNull)
Definition: gistutil.c:547
for(;;)
static Datum index_getattr(IndexTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
Definition: itup.h:117
const void size_t len
uintptr_t Datum
Definition: postgres.h:64
TupleDesc leafTupdesc
Definition: gist_private.h:80
TupleDesc nonLeafTupdesc
Definition: gist_private.h:81
bool spl_lisnull[INDEX_MAX_KEYS]
Definition: gist_private.h:241
bool spl_risnull[INDEX_MAX_KEYS]
Definition: gist_private.h:245

References Assert, for(), GEVHDRSZ, gistdentryinit(), gistSplitHalf(), gistunionsubkey(), gistUserPicksplit(), i, index_getattr(), j, GISTSTATE::leafTupdesc, len, GistEntryVector::n, TupleDescData::natts, GISTSTATE::nonLeafTupdesc, palloc(), GistSplitVector::spl_dontcare, GIST_SPLITVEC::spl_left, GistSplitVector::spl_lisnull, GIST_SPLITVEC::spl_nleft, GIST_SPLITVEC::spl_nright, GIST_SPLITVEC::spl_right, GistSplitVector::spl_risnull, GistSplitVector::splitVector, and GistEntryVector::vector.

Referenced by gistSplit().

◆ gistSplitHalf()

static void gistSplitHalf ( GIST_SPLITVEC v,
int  len 
)
static

Definition at line 585 of file gistsplit.c.

586 {
587  int i;
588 
589  v->spl_nright = v->spl_nleft = 0;
590  v->spl_left = (OffsetNumber *) palloc(len * sizeof(OffsetNumber));
591  v->spl_right = (OffsetNumber *) palloc(len * sizeof(OffsetNumber));
592  for (i = 1; i <= len; i++)
593  if (i < len / 2)
594  v->spl_right[v->spl_nright++] = i;
595  else
596  v->spl_left[v->spl_nleft++] = i;
597 
598  /* we need not compute union keys, caller took care of it */
599 }

References i, len, palloc(), GIST_SPLITVEC::spl_left, GIST_SPLITVEC::spl_nleft, GIST_SPLITVEC::spl_nright, and GIST_SPLITVEC::spl_right.

Referenced by gistSplitByKey().

◆ gistunionsubkey()

static void gistunionsubkey ( GISTSTATE giststate,
IndexTuple itvec,
GistSplitVector spl 
)
static

Definition at line 80 of file gistsplit.c.

81 {
82  GistSplitUnion gsvp;
83 
84  gsvp.dontcare = spl->spl_dontcare;
85 
86  gsvp.entries = spl->splitVector.spl_left;
87  gsvp.len = spl->splitVector.spl_nleft;
88  gsvp.attr = spl->spl_lattr;
89  gsvp.isnull = spl->spl_lisnull;
90 
91  gistunionsubkeyvec(giststate, itvec, &gsvp);
92 
93  gsvp.entries = spl->splitVector.spl_right;
94  gsvp.len = spl->splitVector.spl_nright;
95  gsvp.attr = spl->spl_rattr;
96  gsvp.isnull = spl->spl_risnull;
97 
98  gistunionsubkeyvec(giststate, itvec, &gsvp);
99 }
static void gistunionsubkeyvec(GISTSTATE *giststate, IndexTuple *itvec, GistSplitUnion *gsvp)
Definition: gistsplit.c:47
OffsetNumber * entries
Definition: gistsplit.c:33
bool * isnull
Definition: gistsplit.c:36
bool * dontcare
Definition: gistsplit.c:37
Datum * attr
Definition: gistsplit.c:35
Datum spl_lattr[INDEX_MAX_KEYS]
Definition: gist_private.h:239
Datum spl_rattr[INDEX_MAX_KEYS]
Definition: gist_private.h:243

References GistSplitUnion::attr, GistSplitUnion::dontcare, GistSplitUnion::entries, gistunionsubkeyvec(), GistSplitUnion::isnull, GistSplitUnion::len, GistSplitVector::spl_dontcare, GistSplitVector::spl_lattr, GIST_SPLITVEC::spl_left, GistSplitVector::spl_lisnull, GIST_SPLITVEC::spl_nleft, GIST_SPLITVEC::spl_nright, GistSplitVector::spl_rattr, GIST_SPLITVEC::spl_right, GistSplitVector::spl_risnull, and GistSplitVector::splitVector.

Referenced by gistSplitByKey(), and gistUserPicksplit().

◆ gistunionsubkeyvec()

static void gistunionsubkeyvec ( GISTSTATE giststate,
IndexTuple itvec,
GistSplitUnion gsvp 
)
static

Definition at line 47 of file gistsplit.c.

49 {
50  IndexTuple *cleanedItVec;
51  int i,
52  cleanedLen = 0;
53 
54  cleanedItVec = (IndexTuple *) palloc(sizeof(IndexTuple) * gsvp->len);
55 
56  for (i = 0; i < gsvp->len; i++)
57  {
58  if (gsvp->dontcare && gsvp->dontcare[gsvp->entries[i]])
59  continue;
60 
61  cleanedItVec[cleanedLen++] = itvec[gsvp->entries[i] - 1];
62  }
63 
64  gistMakeUnionItVec(giststate, cleanedItVec, cleanedLen,
65  gsvp->attr, gsvp->isnull);
66 
67  pfree(cleanedItVec);
68 }
void gistMakeUnionItVec(GISTSTATE *giststate, IndexTuple *itvec, int len, Datum *attr, bool *isnull)
Definition: gistutil.c:155
void pfree(void *pointer)
Definition: mcxt.c:1521

References GistSplitUnion::attr, GistSplitUnion::dontcare, GistSplitUnion::entries, gistMakeUnionItVec(), i, GistSplitUnion::isnull, GistSplitUnion::len, palloc(), and pfree().

Referenced by gistunionsubkey().

◆ gistUserPicksplit()

static bool gistUserPicksplit ( Relation  r,
GistEntryVector entryvec,
int  attno,
GistSplitVector v,
IndexTuple itup,
int  len,
GISTSTATE giststate 
)
static

Definition at line 415 of file gistsplit.c.

417 {
418  GIST_SPLITVEC *sv = &v->splitVector;
419 
420  /*
421  * Prepare spl_ldatum/spl_rdatum/spl_ldatum_exists/spl_rdatum_exists in
422  * case we are doing a secondary split (see comments in gist.h).
423  */
424  sv->spl_ldatum_exists = !(v->spl_lisnull[attno]);
425  sv->spl_rdatum_exists = !(v->spl_risnull[attno]);
426  sv->spl_ldatum = v->spl_lattr[attno];
427  sv->spl_rdatum = v->spl_rattr[attno];
428 
429  /*
430  * Let the opclass-specific PickSplit method do its thing. Note that at
431  * this point we know there are no null keys in the entryvec.
432  */
433  FunctionCall2Coll(&giststate->picksplitFn[attno],
434  giststate->supportCollation[attno],
435  PointerGetDatum(entryvec),
436  PointerGetDatum(sv));
437 
438  if (sv->spl_nleft == 0 || sv->spl_nright == 0)
439  {
440  /*
441  * User-defined picksplit failed to create an actual split, ie it put
442  * everything on the same side. Complain but cope.
443  */
444  ereport(DEBUG1,
445  (errcode(ERRCODE_INTERNAL_ERROR),
446  errmsg("picksplit method for column %d of index \"%s\" failed",
447  attno + 1, RelationGetRelationName(r)),
448  errhint("The index is not optimal. To optimize it, contact a developer, or try to use the column as the second one in the CREATE INDEX command.")));
449 
450  /*
451  * Reinit GIST_SPLITVEC. Although these fields are not used by
452  * genericPickSplit(), set them up for further processing
453  */
454  sv->spl_ldatum_exists = !(v->spl_lisnull[attno]);
455  sv->spl_rdatum_exists = !(v->spl_risnull[attno]);
456  sv->spl_ldatum = v->spl_lattr[attno];
457  sv->spl_rdatum = v->spl_rattr[attno];
458 
459  /* Do a generic split */
460  genericPickSplit(giststate, entryvec, sv, attno);
461  }
462  else
463  {
464  /* hack for compatibility with old picksplit API */
465  if (sv->spl_left[sv->spl_nleft - 1] == InvalidOffsetNumber)
466  sv->spl_left[sv->spl_nleft - 1] = (OffsetNumber) (entryvec->n - 1);
467  if (sv->spl_right[sv->spl_nright - 1] == InvalidOffsetNumber)
468  sv->spl_right[sv->spl_nright - 1] = (OffsetNumber) (entryvec->n - 1);
469  }
470 
471  /* Clean up if PickSplit didn't take care of a secondary split */
472  if (sv->spl_ldatum_exists || sv->spl_rdatum_exists)
473  supportSecondarySplit(r, giststate, attno, sv,
474  v->spl_lattr[attno], v->spl_rattr[attno]);
475 
476  /* emit union datums computed by PickSplit back to v arrays */
477  v->spl_lattr[attno] = sv->spl_ldatum;
478  v->spl_rattr[attno] = sv->spl_rdatum;
479  v->spl_lisnull[attno] = false;
480  v->spl_risnull[attno] = false;
481 
482  /*
483  * If index columns remain, then consider whether we can improve the split
484  * by using them.
485  */
486  v->spl_dontcare = NULL;
487 
488  if (attno + 1 < giststate->nonLeafTupdesc->natts)
489  {
490  int NumDontCare;
491 
492  /*
493  * Make a quick check to see if left and right union keys are equal;
494  * if so, the split is certainly degenerate, so tell caller to
495  * re-split with the next column.
496  */
497  if (gistKeyIsEQ(giststate, attno, sv->spl_ldatum, sv->spl_rdatum))
498  return true;
499 
500  /*
501  * Locate don't-care tuples, if any. If there are none, the split is
502  * optimal, so just fall out and return false.
503  */
504  v->spl_dontcare = (bool *) palloc0(sizeof(bool) * (entryvec->n + 1));
505 
506  NumDontCare = findDontCares(r, giststate, entryvec->vector, v, attno);
507 
508  if (NumDontCare > 0)
509  {
510  /*
511  * Remove don't-cares from spl_left[] and spl_right[].
512  */
515 
516  /*
517  * If all tuples on either side were don't-cares, the split is
518  * degenerate, and we're best off to ignore it and split on the
519  * next column. (We used to try to press on with a secondary
520  * split by forcing a random tuple on each side to be treated as
521  * non-don't-care, but it seems unlikely that that technique
522  * really gives a better result. Note that we don't want to try a
523  * secondary split with empty left or right primary split sides,
524  * because then there is no union key on that side for the
525  * PickSplit function to try to expand, so it can have no good
526  * figure of merit for what it's doing. Also note that this check
527  * ensures we can't produce a bogus one-side-only split in the
528  * NumDontCare == 1 special case below.)
529  */
530  if (sv->spl_nleft == 0 || sv->spl_nright == 0)
531  {
532  v->spl_dontcare = NULL;
533  return true;
534  }
535 
536  /*
537  * Recompute union keys, considering only non-don't-care tuples.
538  * NOTE: this will set union keys for remaining index columns,
539  * which will cause later calls of gistUserPicksplit to pass those
540  * values down to user-defined PickSplit methods with
541  * spl_ldatum_exists/spl_rdatum_exists set true.
542  */
543  gistunionsubkey(giststate, itup, v);
544 
545  if (NumDontCare == 1)
546  {
547  /*
548  * If there's only one don't-care tuple then we can't do a
549  * PickSplit on it, so just choose whether to send it left or
550  * right by comparing penalties. We needed the
551  * gistunionsubkey step anyway so that we have appropriate
552  * union keys for figuring the penalties.
553  */
554  OffsetNumber toMove;
555 
556  /* find it ... */
557  for (toMove = FirstOffsetNumber; toMove < entryvec->n; toMove++)
558  {
559  if (v->spl_dontcare[toMove])
560  break;
561  }
562  Assert(toMove < entryvec->n);
563 
564  /* ... and assign it to cheaper side */
565  placeOne(r, giststate, v, itup[toMove - 1], toMove, attno + 1);
566 
567  /*
568  * At this point the union keys are wrong, but we don't care
569  * because we're done splitting. The outermost recursion
570  * level of gistSplitByKey will fix things before returning.
571  */
572  }
573  else
574  return true;
575  }
576  }
577 
578  return false;
579 }
int errhint(const char *fmt,...)
Definition: elog.c:1317
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define DEBUG1
Definition: elog.h:30
#define ereport(elevel,...)
Definition: elog.h:149
static void removeDontCares(OffsetNumber *a, int *len, const bool *dontcare)
Definition: gistsplit.c:167
static void supportSecondarySplit(Relation r, GISTSTATE *giststate, int attno, GIST_SPLITVEC *sv, Datum oldL, Datum oldR)
Definition: gistsplit.c:258
static void genericPickSplit(GISTSTATE *giststate, GistEntryVector *entryvec, GIST_SPLITVEC *v, int attno)
Definition: gistsplit.c:344
static void placeOne(Relation r, GISTSTATE *giststate, GistSplitVector *v, IndexTuple itup, OffsetNumber off, int attno)
Definition: gistsplit.c:200
static int findDontCares(Relation r, GISTSTATE *giststate, GISTENTRY *valvec, GistSplitVector *spl, int attno)
Definition: gistsplit.c:113
bool gistKeyIsEQ(GISTSTATE *giststate, int attno, Datum a, Datum b)
Definition: gistutil.c:281
void * palloc0(Size size)
Definition: mcxt.c:1347
#define InvalidOffsetNumber
Definition: off.h:26
#define RelationGetRelationName(relation)
Definition: rel.h:539
FmgrInfo picksplitFn[INDEX_MAX_KEYS]
Definition: gist_private.h:91
bool spl_ldatum_exists
Definition: gist.h:145
bool spl_rdatum_exists
Definition: gist.h:150

References Assert, DEBUG1, ereport, errcode(), errhint(), errmsg(), findDontCares(), FirstOffsetNumber, FunctionCall2Coll(), genericPickSplit(), gistKeyIsEQ(), gistunionsubkey(), InvalidOffsetNumber, GistEntryVector::n, TupleDescData::natts, GISTSTATE::nonLeafTupdesc, palloc0(), GISTSTATE::picksplitFn, placeOne(), PointerGetDatum(), RelationGetRelationName, removeDontCares(), GistSplitVector::spl_dontcare, GistSplitVector::spl_lattr, GIST_SPLITVEC::spl_ldatum, GIST_SPLITVEC::spl_ldatum_exists, GIST_SPLITVEC::spl_left, GistSplitVector::spl_lisnull, GIST_SPLITVEC::spl_nleft, GIST_SPLITVEC::spl_nright, GistSplitVector::spl_rattr, GIST_SPLITVEC::spl_rdatum, GIST_SPLITVEC::spl_rdatum_exists, GIST_SPLITVEC::spl_right, GistSplitVector::spl_risnull, GistSplitVector::splitVector, GISTSTATE::supportCollation, supportSecondarySplit(), and GistEntryVector::vector.

Referenced by gistSplitByKey().

◆ placeOne()

static void placeOne ( Relation  r,
GISTSTATE giststate,
GistSplitVector v,
IndexTuple  itup,
OffsetNumber  off,
int  attno 
)
static

Definition at line 200 of file gistsplit.c.

202 {
203  GISTENTRY identry[INDEX_MAX_KEYS];
204  bool isnull[INDEX_MAX_KEYS];
205  bool toLeft = true;
206 
207  gistDeCompressAtt(giststate, r, itup, NULL, (OffsetNumber) 0,
208  identry, isnull);
209 
210  for (; attno < giststate->nonLeafTupdesc->natts; attno++)
211  {
212  float lpenalty,
213  rpenalty;
214  GISTENTRY entry;
215 
216  gistentryinit(entry, v->spl_lattr[attno], r, NULL, 0, false);
217  lpenalty = gistpenalty(giststate, attno, &entry, v->spl_lisnull[attno],
218  identry + attno, isnull[attno]);
219  gistentryinit(entry, v->spl_rattr[attno], r, NULL, 0, false);
220  rpenalty = gistpenalty(giststate, attno, &entry, v->spl_risnull[attno],
221  identry + attno, isnull[attno]);
222 
223  if (lpenalty != rpenalty)
224  {
225  if (lpenalty > rpenalty)
226  toLeft = false;
227  break;
228  }
229  }
230 
231  if (toLeft)
232  v->splitVector.spl_left[v->splitVector.spl_nleft++] = off;
233  else
235 }
void gistDeCompressAtt(GISTSTATE *giststate, Relation r, IndexTuple tuple, Page p, OffsetNumber o, GISTENTRY *attdata, bool *isnull)
Definition: gistutil.c:296
#define INDEX_MAX_KEYS

References gistDeCompressAtt(), gistentryinit, gistpenalty(), INDEX_MAX_KEYS, TupleDescData::natts, GISTSTATE::nonLeafTupdesc, GistSplitVector::spl_lattr, GIST_SPLITVEC::spl_left, GistSplitVector::spl_lisnull, GIST_SPLITVEC::spl_nleft, GIST_SPLITVEC::spl_nright, GistSplitVector::spl_rattr, GIST_SPLITVEC::spl_right, GistSplitVector::spl_risnull, and GistSplitVector::splitVector.

Referenced by gistUserPicksplit().

◆ removeDontCares()

static void removeDontCares ( OffsetNumber a,
int *  len,
const bool dontcare 
)
static

Definition at line 167 of file gistsplit.c.

168 {
169  int origlen,
170  newlen,
171  i;
172  OffsetNumber *curwpos;
173 
174  origlen = newlen = *len;
175  curwpos = a;
176  for (i = 0; i < origlen; i++)
177  {
178  OffsetNumber ai = a[i];
179 
180  if (dontcare[ai] == false)
181  {
182  /* re-emit item into a[] */
183  *curwpos = ai;
184  curwpos++;
185  }
186  else
187  newlen--;
188  }
189 
190  *len = newlen;
191 }
int a
Definition: isn.c:69

References a, i, and len.

Referenced by gistUserPicksplit().

◆ supportSecondarySplit()

static void supportSecondarySplit ( Relation  r,
GISTSTATE giststate,
int  attno,
GIST_SPLITVEC sv,
Datum  oldL,
Datum  oldR 
)
static

Definition at line 258 of file gistsplit.c.

260 {
261  bool leaveOnLeft = true,
262  tmpBool;
263  GISTENTRY entryL,
264  entryR,
265  entrySL,
266  entrySR;
267 
268  gistentryinit(entryL, oldL, r, NULL, 0, false);
269  gistentryinit(entryR, oldR, r, NULL, 0, false);
270  gistentryinit(entrySL, sv->spl_ldatum, r, NULL, 0, false);
271  gistentryinit(entrySR, sv->spl_rdatum, r, NULL, 0, false);
272 
273  if (sv->spl_ldatum_exists && sv->spl_rdatum_exists)
274  {
275  float penalty1,
276  penalty2;
277 
278  penalty1 = gistpenalty(giststate, attno, &entryL, false, &entrySL, false) +
279  gistpenalty(giststate, attno, &entryR, false, &entrySR, false);
280  penalty2 = gistpenalty(giststate, attno, &entryL, false, &entrySR, false) +
281  gistpenalty(giststate, attno, &entryR, false, &entrySL, false);
282 
283  if (penalty1 > penalty2)
284  leaveOnLeft = false;
285  }
286  else
287  {
288  GISTENTRY *entry1 = (sv->spl_ldatum_exists) ? &entryL : &entryR;
289  float penalty1,
290  penalty2;
291 
292  /*
293  * There is only one previously defined union, so we just choose swap
294  * or not by lowest penalty for that side. We can only get here if a
295  * secondary split happened to have all NULLs in its column in the
296  * tuples that the outer recursion level had assigned to one side.
297  * (Note that the null checks in gistSplitByKey don't prevent the
298  * case, because they'll only be checking tuples that were considered
299  * don't-cares at the outer recursion level, not the tuples that went
300  * into determining the passed-down left and right union keys.)
301  */
302  penalty1 = gistpenalty(giststate, attno, entry1, false, &entrySL, false);
303  penalty2 = gistpenalty(giststate, attno, entry1, false, &entrySR, false);
304 
305  if (penalty1 < penalty2)
306  leaveOnLeft = sv->spl_ldatum_exists;
307  else
308  leaveOnLeft = sv->spl_rdatum_exists;
309  }
310 
311  if (leaveOnLeft == false)
312  {
313  /*
314  * swap left and right
315  */
316  OffsetNumber *off,
317  noff;
318  Datum datum;
319 
320  SWAPVAR(sv->spl_left, sv->spl_right, off);
321  SWAPVAR(sv->spl_nleft, sv->spl_nright, noff);
322  SWAPVAR(sv->spl_ldatum, sv->spl_rdatum, datum);
323  gistentryinit(entrySL, sv->spl_ldatum, r, NULL, 0, false);
324  gistentryinit(entrySR, sv->spl_rdatum, r, NULL, 0, false);
325  }
326 
327  if (sv->spl_ldatum_exists)
328  gistMakeUnionKey(giststate, attno, &entryL, false, &entrySL, false,
329  &sv->spl_ldatum, &tmpBool);
330 
331  if (sv->spl_rdatum_exists)
332  gistMakeUnionKey(giststate, attno, &entryR, false, &entrySR, false,
333  &sv->spl_rdatum, &tmpBool);
334 
335  sv->spl_ldatum_exists = sv->spl_rdatum_exists = false;
336 }
#define SWAPVAR(s, d, t)
Definition: gistsplit.c:237
void gistMakeUnionKey(GISTSTATE *giststate, int attno, GISTENTRY *entry1, bool isnull1, GISTENTRY *entry2, bool isnull2, Datum *dst, bool *dstisnull)
Definition: gistutil.c:233

References gistentryinit, gistMakeUnionKey(), gistpenalty(), GIST_SPLITVEC::spl_ldatum, GIST_SPLITVEC::spl_ldatum_exists, GIST_SPLITVEC::spl_left, GIST_SPLITVEC::spl_nleft, GIST_SPLITVEC::spl_nright, GIST_SPLITVEC::spl_rdatum, GIST_SPLITVEC::spl_rdatum_exists, GIST_SPLITVEC::spl_right, and SWAPVAR.

Referenced by gistUserPicksplit().