1 /*-------------------------------------------------------------------------
4 * interface routines for the postgres GiST index access method.
9 * $Header: /cvsroot/pgsql/src/backend/access/gist/gist.c,v 1.52 2000/03/17 02:36:00 tgl Exp $
11 *-------------------------------------------------------------------------
16 #include "access/genam.h"
17 #include "access/gist.h"
18 #include "access/gistscan.h"
19 #include "access/heapam.h"
20 #include "catalog/index.h"
21 #include "catalog/pg_index.h"
22 #include "executor/executor.h"
23 #include "miscadmin.h"
24 #include "utils/syscache.h"
27 /* non-export function prototypes */
28 static InsertIndexResult gistdoinsert(Relation r, IndexTuple itup,
29 GISTSTATE *GISTstate);
30 static InsertIndexResult gistentryinsert(Relation r, GISTSTACK *stk,
32 GISTSTATE *giststate);
33 static void gistentryinserttwo(Relation r, GISTSTACK *stk, IndexTuple ltup,
34 IndexTuple rtup, GISTSTATE *giststate);
35 static void gistAdjustKeys(Relation r, GISTSTACK *stk, BlockNumber blk,
36 char *datum, int att_size, GISTSTATE *giststate);
37 static void gistintinsert(Relation r, GISTSTACK *stk, IndexTuple ltup,
38 IndexTuple rtup, GISTSTATE *giststate);
39 static InsertIndexResult gistSplit(Relation r, Buffer buffer,
40 GISTSTACK *stack, IndexTuple itup,
41 GISTSTATE *giststate);
42 static void gistnewroot(GISTSTATE *giststate, Relation r, IndexTuple lt,
44 static void GISTInitBuffer(Buffer b, uint32 f);
45 static BlockNumber gistChooseSubtree(Relation r, IndexTuple itup, int level,
47 GISTSTACK **retstack, Buffer *leafbuf);
48 static OffsetNumber gistchoose(Relation r, Page p, IndexTuple it,
49 GISTSTATE *giststate);
50 static int gistnospace(Page p, IndexTuple it);
51 void gistdelete(Relation r, ItemPointer tid);
52 static IndexTuple gist_tuple_replacekey(Relation r, GISTENTRY entry, IndexTuple t);
53 static void gistcentryinit(GISTSTATE *giststate, GISTENTRY *e, char *pr,
54 Relation r, Page pg, OffsetNumber o, int b, bool l);
56 static char *int_range_out(INTRANGE *r);
60 ** routine to build an index. Basically calls insert over and over
63 gistbuild(Relation heap,
79 InsertIndexResult res;
86 #ifndef OMIT_PARTIAL_INDEX
87 ExprContext *econtext;
88 TupleTable tupleTable;
96 Buffer buffer = InvalidBuffer;
99 /* no locking is needed */
101 CommandCounterIncrement(); /* so we can see the new pg_index tuple */
103 initGISTstate(&giststate, index);
105 pred = predInfo->pred;
106 oldPred = predInfo->oldPred;
109 * We expect to be called exactly once for any index relation. If
110 * that's not the case, big trouble's what we have.
113 if (oldPred == NULL && (nb = RelationGetNumberOfBlocks(index)) != 0)
114 elog(ERROR, "%s already contains data", RelationGetRelationName(index));
116 /* initialize the root page (if this is a new index) */
119 buffer = ReadBuffer(index, P_NEW);
120 GISTInitBuffer(buffer, F_LEAF);
124 /* init the tuple descriptors and get set for a heap scan */
125 hd = RelationGetDescr(heap);
126 id = RelationGetDescr(index);
127 d = (Datum *) palloc(natts * sizeof(*d));
128 nulls = (bool *) palloc(natts * sizeof(*nulls));
131 * If this is a predicate (partial) index, we will need to evaluate
132 * the predicate using ExecQual, which requires the current tuple to
133 * be in a slot of a TupleTable. In addition, ExecQual must have an
134 * ExprContext referring to that slot. Here, we initialize dummy
135 * TupleTable and ExprContext objects for this purpose. --Nels, Feb
138 #ifndef OMIT_PARTIAL_INDEX
139 if (pred != NULL || oldPred != NULL)
141 tupleTable = ExecCreateTupleTable(1);
142 slot = ExecAllocTableSlot(tupleTable);
143 econtext = makeNode(ExprContext);
144 FillDummyExprContext(econtext, slot, hd, InvalidBuffer);
147 /* shut the compiler up */
153 #endif /* OMIT_PARTIAL_INDEX */
154 /* int the tuples as we insert them */
157 scan = heap_beginscan(heap, 0, SnapshotNow, 0, (ScanKey) NULL);
159 while (HeapTupleIsValid(htup = heap_getnext(scan, 0)))
164 * If oldPred != NULL, this is an EXTEND INDEX command, so skip
165 * this tuple if it was already in the existing partial index
169 #ifndef OMIT_PARTIAL_INDEX
170 /* SetSlotContents(slot, htup); */
172 if (ExecQual((List *) oldPred, econtext, false))
177 #endif /* OMIT_PARTIAL_INDEX */
181 * Skip this tuple if it doesn't satisfy the partial-index
186 #ifndef OMIT_PARTIAL_INDEX
187 /* SetSlotContents(slot, htup); */
189 if (! ExecQual((List *) pred, econtext, false))
191 #endif /* OMIT_PARTIAL_INDEX */
197 * For the current heap tuple, extract all the attributes we use
198 * in this index, and note which are null.
201 for (i = 1; i <= natts; i++)
207 * Offsets are from the start of the tuple, and are
208 * zero-based; indices are one-based. The next call returns i
209 * - 1. That's data hiding for you.
212 attoff = AttrNumberGetAttrOffset(i);
215 * d[attoff] = HeapTupleGetAttributeValue(htup, buffer,
217 d[attoff] = GetIndexValue(htup,
223 nulls[attoff] = (attnull ? 'n' : ' ');
226 /* immediately compress keys to normalize */
227 compvec = (bool *) palloc(sizeof(bool) * natts);
228 for (i = 0; i < natts; i++)
230 gistcentryinit(&giststate, &tmpcentry, (char *) d[i],
231 (Relation) NULL, (Page) NULL, (OffsetNumber) 0,
232 -1 /* size is currently bogus */ , TRUE);
233 if (d[i] != (Datum) tmpcentry.pred && !(giststate.keytypbyval))
237 d[i] = (Datum) tmpcentry.pred;
240 /* form an index tuple and point it at the heap tuple */
241 itup = index_formtuple(id, &d[0], nulls);
242 itup->t_tid = htup->t_self;
245 * Since we already have the index relation locked, we call
246 * gistdoinsert directly. Normal access method calls dispatch
247 * through gistinsert, which locks the relation for write. This
248 * is the right thing to do if you're inserting single tups, but
249 * not when you're initializing the whole index at once.
252 res = gistdoinsert(index, itup, &giststate);
253 for (i = 0; i < natts; i++)
254 if (compvec[i] == TRUE)
255 pfree((char *) d[i]);
261 /* okay, all heap tuples are indexed */
264 if (pred != NULL || oldPred != NULL)
266 #ifndef OMIT_PARTIAL_INDEX
267 ExecDropTupleTable(tupleTable, true);
269 #endif /* OMIT_PARTIAL_INDEX */
273 * Since we just counted the tuples in the heap, we update its stats
274 * in pg_class to guarantee that the planner takes advantage of the
275 * index we just created. But, only update statistics during
276 * normal index definitions, not for indices on system catalogs
277 * created during bootstrap processing. We must close the relations
278 * before updating statistics to guarantee that the relcache entries
279 * are flushed when we increment the command counter in UpdateStats().
280 * But we do not release any locks on the relations; those will be
281 * held until end of transaction.
283 if (IsNormalProcessingMode())
285 Oid hrelid = RelationGetRelid(heap);
286 Oid irelid = RelationGetRelid(index);
287 bool inplace = IsReindexProcessing();
289 heap_close(heap, NoLock);
291 UpdateStats(hrelid, nh, inplace);
292 UpdateStats(irelid, ni, inplace);
293 if (oldPred != NULL && !inplace)
297 UpdateIndexPredicate(irelid, oldPred, pred);
307 * gistinsert -- wrapper for GiST tuple insertion.
309 * This is the public interface routine for tuple insertion in GiSTs.
310 * It doesn't do any work; just locks the relation and passes the buck.
313 gistinsert(Relation r, Datum *datum, char *nulls, ItemPointer ht_ctid, Relation heapRel)
315 InsertIndexResult res;
322 initGISTstate(&giststate, r);
324 /* immediately compress keys to normalize */
325 compvec = (bool *) palloc(sizeof(bool) * r->rd_att->natts);
326 for (i = 0; i < r->rd_att->natts; i++)
328 gistcentryinit(&giststate, &tmpentry, (char *) datum[i],
329 (Relation) NULL, (Page) NULL, (OffsetNumber) 0,
330 -1 /* size is currently bogus */ , TRUE);
331 if (datum[i] != (Datum) tmpentry.pred && !(giststate.keytypbyval))
335 datum[i] = (Datum) tmpentry.pred;
337 itup = index_formtuple(RelationGetDescr(r), datum, nulls);
338 itup->t_tid = *ht_ctid;
341 * Notes in ExecUtils:ExecOpenIndices()
343 * RelationSetLockForWrite(r);
346 res = gistdoinsert(r, itup, &giststate);
347 for (i = 0; i < r->rd_att->natts; i++)
348 if (compvec[i] == TRUE)
349 pfree((char *) datum[i]);
357 ** Take a compressed entry, and install it on a page. Since we now know
358 ** where the entry will live, we decompress it and recompress it using
359 ** that knowledge (some compression routines may want to fish around
360 ** on the page, for example, or do something special for leaf nodes.)
363 gistPageAddItem(GISTSTATE *giststate,
368 OffsetNumber offsetNumber,
374 IndexTuple itup = (IndexTuple) item;
377 * recompress the item given that we now know the exact page and
378 * offset for insertion
380 gistdentryinit(giststate, dentry,
381 (((char *) itup) + sizeof(IndexTupleData)),
382 (Relation) 0, (Page) 0, (OffsetNumber) InvalidOffsetNumber,
383 IndexTupleSize(itup) - sizeof(IndexTupleData), FALSE);
384 gistcentryinit(giststate, &tmpcentry, dentry->pred, r, page,
385 offsetNumber, dentry->bytes, FALSE);
386 *newtup = gist_tuple_replacekey(r, *dentry, itup);
388 if (tmpcentry.pred != dentry->pred
389 && tmpcentry.pred != (((char *) itup) + sizeof(IndexTupleData)))
390 pfree(tmpcentry.pred);
392 return (PageAddItem(page, (Item) *newtup, IndexTupleSize(*newtup),
393 offsetNumber, flags));
397 static InsertIndexResult
398 gistdoinsert(Relation r,
399 IndexTuple itup, /* itup contains compressed entry */
400 GISTSTATE *giststate)
403 InsertIndexResult res;
412 /* 3rd arg is ignored for now */
413 blk = gistChooseSubtree(r, itup, 0, giststate, &stack, &buffer);
414 page = (Page) BufferGetPage(buffer);
416 if (gistnospace(page, itup))
418 /* need to do a split */
419 res = gistSplit(r, buffer, stack, itup, giststate);
420 gistfreestack(stack);
421 WriteBuffer(buffer); /* don't forget to release buffer! */
425 if (PageIsEmpty(page))
426 off = FirstOffsetNumber;
428 off = OffsetNumberNext(PageGetMaxOffsetNumber(page));
430 /* add the item and write the buffer */
431 l = gistPageAddItem(giststate, r, page, (Item) itup, IndexTupleSize(itup),
432 off, LP_USED, &tmpdentry, &newtup);
435 /* now expand the page boundary in the parent to include the new child */
436 gistAdjustKeys(r, stack, blk, tmpdentry.pred, tmpdentry.bytes, giststate);
437 gistfreestack(stack);
442 if (tmpdentry.pred != (((char *) itup) + sizeof(IndexTupleData)))
443 pfree(tmpdentry.pred);
445 /* build and return an InsertIndexResult for this insertion */
446 res = (InsertIndexResult) palloc(sizeof(InsertIndexResultData));
447 ItemPointerSet(&(res->pointerData), blk, l);
454 gistChooseSubtree(Relation r, IndexTuple itup, /* itup has compressed
457 GISTSTATE *giststate,
458 GISTSTACK **retstack /* out */ ,
459 Buffer *leafbuf /* out */ )
465 GISTPageOpaque opaque;
469 buffer = InvalidBuffer;
470 stack = (GISTSTACK *) NULL;
474 /* let go of current buffer before getting next */
475 if (buffer != InvalidBuffer)
476 ReleaseBuffer(buffer);
478 /* get next buffer */
479 buffer = ReadBuffer(r, blk);
480 page = (Page) BufferGetPage(buffer);
482 opaque = (GISTPageOpaque) PageGetSpecialPointer(page);
483 if (!(opaque->flags & F_LEAF))
488 n = (GISTSTACK *) palloc(sizeof(GISTSTACK));
489 n->gs_parent = stack;
491 n->gs_child = gistchoose(r, page, itup, giststate);
494 iid = PageGetItemId(page, n->gs_child);
495 which = (IndexTuple) PageGetItem(page, iid);
496 blk = ItemPointerGetBlockNumber(&(which->t_tid));
498 } while (!(opaque->flags & F_LEAF));
508 gistAdjustKeys(Relation r,
511 char *datum, /* datum is uncompressed */
513 GISTSTATE *giststate)
527 if (stk == (GISTSTACK *) NULL)
530 b = ReadBuffer(r, stk->gs_blk);
531 p = BufferGetPage(b);
533 oldud = (char *) PageGetItem(p, PageGetItemId(p, stk->gs_child));
534 tid = (IndexTuple) oldud;
535 size = IndexTupleSize((IndexTuple) oldud) - sizeof(IndexTupleData);
536 oldud += sizeof(IndexTupleData);
538 evec = (bytea *) palloc(2 * sizeof(GISTENTRY) + VARHDRSZ);
539 VARSIZE(evec) = 2 * sizeof(GISTENTRY) + VARHDRSZ;
541 /* insert decompressed oldud into entry vector */
542 gistdentryinit(giststate, &((GISTENTRY *) VARDATA(evec))[0],
543 oldud, r, p, stk->gs_child,
545 ev0p = &((GISTENTRY *) VARDATA(evec))[0];
547 /* insert datum entry into entry vector */
548 gistentryinit(((GISTENTRY *) VARDATA(evec))[1], datum,
549 (Relation) NULL, (Page) NULL, (OffsetNumber) 0, att_size, FALSE);
550 ev1p = &((GISTENTRY *) VARDATA(evec))[1];
552 /* form union of decompressed entries */
553 datum = (*fmgr_faddr(&giststate->unionFn)) (evec, &datumsize);
555 /* did union leave decompressed version of oldud unchanged? */
556 (*fmgr_faddr(&giststate->equalFn)) (ev0p->pred, datum, &result);
559 TupleDesc td = RelationGetDescr(r);
561 /* compress datum for storage on page */
562 gistcentryinit(giststate, ¢ry, datum, ev0p->rel, ev0p->page,
563 ev0p->offset, datumsize, FALSE);
564 if (td->attrs[0]->attlen >= 0)
566 memmove(oldud, centry.pred, att_size);
567 gistAdjustKeys(r, stk->gs_parent, stk->gs_blk, datum, att_size,
570 else if (VARSIZE(centry.pred) == VARSIZE(oldud))
572 memmove(oldud, centry.pred, VARSIZE(centry.pred));
573 gistAdjustKeys(r, stk->gs_parent, stk->gs_blk, datum, att_size,
580 * * new datum is not the same size as the old. * We have to
581 * delete the old entry and insert the new * one. Note that
582 * this may cause a split here!
585 ItemPointerData oldtid;
588 InsertIndexResult res;
590 /* delete old tuple */
591 ItemPointerSet(&oldtid, stk->gs_blk, stk->gs_child);
592 gistdelete(r, (ItemPointer) &oldtid);
594 /* generate and insert new tuple */
596 isnull = (char *) palloc(r->rd_rel->relnatts);
597 MemSet(isnull, ' ', r->rd_rel->relnatts);
598 newtup = (IndexTuple) index_formtuple(tupDesc,
599 (Datum *) ¢ry.pred, isnull);
601 /* set pointer in new tuple to point to current child */
602 ItemPointerSet(&oldtid, blk, 1);
603 newtup->t_tid = oldtid;
605 /* inserting the new entry also adjust keys above */
606 res = gistentryinsert(r, stk, newtup, giststate);
608 /* in stack, set info to point to new tuple */
609 stk->gs_blk = ItemPointerGetBlockNumber(&(res->pointerData));
610 stk->gs_child = ItemPointerGetOffsetNumber(&(res->pointerData));
616 if (centry.pred != datum)
625 * gistSplit -- split a page in the tree.
628 static InsertIndexResult
629 gistSplit(Relation r,
632 IndexTuple itup, /* contains compressed entry */
633 GISTSTATE *giststate)
647 OffsetNumber leftoff,
651 BlockNumber bufblock;
652 GISTPageOpaque opaque;
654 InsertIndexResult res;
664 isnull = (char *) palloc(r->rd_rel->relnatts);
665 for (blank = 0; blank < r->rd_rel->relnatts; blank++)
667 p = (Page) BufferGetPage(buffer);
668 opaque = (GISTPageOpaque) PageGetSpecialPointer(p);
672 * The root of the tree is the first block in the relation. If we're
673 * about to split the root, we need to do some hocus-pocus to enforce
677 if (BufferGetBlockNumber(buffer) == GISTP_ROOT)
679 leftbuf = ReadBuffer(r, P_NEW);
680 GISTInitBuffer(leftbuf, opaque->flags);
681 lbknum = BufferGetBlockNumber(leftbuf);
682 left = (Page) BufferGetPage(leftbuf);
687 IncrBufferRefCount(buffer);
688 lbknum = BufferGetBlockNumber(buffer);
689 left = (Page) PageGetTempPage(p, sizeof(GISTPageOpaqueData));
692 rightbuf = ReadBuffer(r, P_NEW);
693 GISTInitBuffer(rightbuf, opaque->flags);
694 rbknum = BufferGetBlockNumber(rightbuf);
695 right = (Page) BufferGetPage(rightbuf);
697 /* generate the item array */
698 maxoff = PageGetMaxOffsetNumber(p);
699 entryvec = (bytea *) palloc(VARHDRSZ + (maxoff + 2) * sizeof(GISTENTRY));
700 decompvec = (bool *) palloc(VARHDRSZ + (maxoff + 2) * sizeof(bool));
701 for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
703 item_1 = (IndexTuple) PageGetItem(p, PageGetItemId(p, i));
704 gistdentryinit(giststate, &((GISTENTRY *) VARDATA(entryvec))[i],
705 (((char *) item_1) + sizeof(IndexTupleData)),
707 IndexTupleSize(item_1) - sizeof(IndexTupleData), FALSE);
708 if ((char *) (((GISTENTRY *) VARDATA(entryvec))[i].pred)
709 == (((char *) item_1) + sizeof(IndexTupleData)))
710 decompvec[i] = FALSE;
715 /* add the new datum as the last entry */
716 gistdentryinit(giststate, &(((GISTENTRY *) VARDATA(entryvec))[maxoff + 1]),
717 (((char *) itup) + sizeof(IndexTupleData)),
718 (Relation) NULL, (Page) NULL,
719 (OffsetNumber) 0, tmpentry.bytes, FALSE);
720 if ((char *) (((GISTENTRY *) VARDATA(entryvec))[maxoff + 1]).pred !=
721 (((char *) itup) + sizeof(IndexTupleData)))
722 decompvec[maxoff + 1] = TRUE;
724 decompvec[maxoff + 1] = FALSE;
726 VARSIZE(entryvec) = (maxoff + 2) * sizeof(GISTENTRY) + VARHDRSZ;
728 /* now let the user-defined picksplit function set up the split vector */
729 (*fmgr_faddr(&giststate->picksplitFn)) (entryvec, &v);
731 /* compress ldatum and rdatum */
732 gistcentryinit(giststate, &tmpentry, v.spl_ldatum, (Relation) NULL,
733 (Page) NULL, (OffsetNumber) 0,
734 ((GISTENTRY *) VARDATA(entryvec))[i].bytes, FALSE);
735 if (v.spl_ldatum != tmpentry.pred)
737 v.spl_ldatum = tmpentry.pred;
739 gistcentryinit(giststate, &tmpentry, v.spl_rdatum, (Relation) NULL,
740 (Page) NULL, (OffsetNumber) 0,
741 ((GISTENTRY *) VARDATA(entryvec))[i].bytes, FALSE);
742 if (v.spl_rdatum != tmpentry.pred)
744 v.spl_rdatum = tmpentry.pred;
746 /* clean up the entry vector: its preds need to be deleted, too */
747 for (i = FirstOffsetNumber; i <= maxoff + 1; i = OffsetNumberNext(i))
749 pfree(((GISTENTRY *) VARDATA(entryvec))[i].pred);
753 leftoff = rightoff = FirstOffsetNumber;
754 maxoff = PageGetMaxOffsetNumber(p);
755 for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
757 itemid = PageGetItemId(p, i);
758 item = (IndexTuple) PageGetItem(p, itemid);
760 if (i == *(v.spl_left))
762 gistPageAddItem(giststate, r, left, (Item) item,
763 IndexTupleSize(item),
764 leftoff, LP_USED, &tmpdentry, &newtup);
765 leftoff = OffsetNumberNext(leftoff);
766 v.spl_left++; /* advance in left split vector */
768 if (tmpdentry.pred != (((char *) item) + sizeof(IndexTupleData)))
769 pfree(tmpdentry.pred);
770 if ((IndexTuple) item != newtup)
775 gistPageAddItem(giststate, r, right, (Item) item,
776 IndexTupleSize(item),
777 rightoff, LP_USED, &tmpdentry, &newtup);
778 rightoff = OffsetNumberNext(rightoff);
779 v.spl_right++; /* advance in right split vector */
781 if (tmpdentry.pred != (((char *) item) + sizeof(IndexTupleData)))
782 pfree(tmpdentry.pred);
788 /* build an InsertIndexResult for this insertion */
789 res = (InsertIndexResult) palloc(sizeof(InsertIndexResultData));
791 /* now insert the new index tuple */
792 if (*(v.spl_left) != FirstOffsetNumber)
794 gistPageAddItem(giststate, r, left, (Item) itup,
795 IndexTupleSize(itup),
796 leftoff, LP_USED, &tmpdentry, &newtup);
797 leftoff = OffsetNumberNext(leftoff);
798 ItemPointerSet(&(res->pointerData), lbknum, leftoff);
800 if (tmpdentry.pred != (((char *) itup) + sizeof(IndexTupleData)))
801 pfree(tmpdentry.pred);
807 gistPageAddItem(giststate, r, right, (Item) itup,
808 IndexTupleSize(itup),
809 rightoff, LP_USED, &tmpdentry, &newtup);
810 rightoff = OffsetNumberNext(rightoff);
811 ItemPointerSet(&(res->pointerData), rbknum, rightoff);
813 if (tmpdentry.pred != (((char *) itup) + sizeof(IndexTupleData)))
814 pfree(tmpdentry.pred);
819 if ((bufblock = BufferGetBlockNumber(buffer)) != GISTP_ROOT)
820 PageRestoreTempPage(left, p);
821 WriteBuffer(leftbuf);
822 WriteBuffer(rightbuf);
825 * Okay, the page is split. We have three things left to do:
827 * 1) Adjust any active scans on this index to cope with changes we
828 * introduced in its structure by splitting this page.
830 * 2) "Tighten" the bounding box of the pointer to the left page in the
831 * parent node in the tree, if any. Since we moved a bunch of stuff
832 * off the left page, we expect it to get smaller. This happens in
833 * the internal insertion routine.
835 * 3) Insert a pointer to the right page in the parent. This may cause
836 * the parent to split. If it does, we need to repeat steps one and
837 * two for each split node in the tree.
840 /* adjust active scans */
841 gistadjscans(r, GISTOP_SPLIT, bufblock, FirstOffsetNumber);
845 ltup = (IndexTuple) index_formtuple(tupDesc,
846 (Datum *) &(v.spl_ldatum), isnull);
847 rtup = (IndexTuple) index_formtuple(tupDesc,
848 (Datum *) &(v.spl_rdatum), isnull);
851 /* set pointers to new child pages in the internal index tuples */
852 ItemPointerSet(&(ltup->t_tid), lbknum, 1);
853 ItemPointerSet(&(rtup->t_tid), rbknum, 1);
855 gistintinsert(r, stack, ltup, rtup, giststate);
864 ** After a split, we need to overwrite the old entry's key in the parent,
865 ** and install install an entry for the new key into the parent.
868 gistintinsert(Relation r,
870 IndexTuple ltup, /* new version of entry for old page */
871 IndexTuple rtup, /* entry for new page */
872 GISTSTATE *giststate)
874 ItemPointerData ltid;
876 if (stk == (GISTSTACK *) NULL)
878 gistnewroot(giststate, r, ltup, rtup);
882 /* remove old left pointer, insert the 2 new entries */
883 ItemPointerSet(<id, stk->gs_blk, stk->gs_child);
884 gistdelete(r, (ItemPointer) <id);
885 gistentryinserttwo(r, stk, ltup, rtup, giststate);
890 ** Insert two entries onto one page, handling a split for either one!
893 gistentryinserttwo(Relation r, GISTSTACK *stk, IndexTuple ltup,
894 IndexTuple rtup, GISTSTATE *giststate)
898 InsertIndexResult res;
902 b = ReadBuffer(r, stk->gs_blk);
903 p = BufferGetPage(b);
905 if (gistnospace(p, ltup))
907 res = gistSplit(r, b, stk->gs_parent, ltup, giststate);
908 WriteBuffer(b); /* don't forget to release buffer! -
911 gistdoinsert(r, rtup, giststate);
915 gistPageAddItem(giststate, r, p, (Item) ltup,
916 IndexTupleSize(ltup), InvalidOffsetNumber,
917 LP_USED, &tmpentry, &newtup);
919 gistAdjustKeys(r, stk->gs_parent, stk->gs_blk, tmpentry.pred,
920 tmpentry.bytes, giststate);
922 if (tmpentry.pred != (((char *) ltup) + sizeof(IndexTupleData)))
923 pfree(tmpentry.pred);
926 gistentryinsert(r, stk, rtup, giststate);
932 ** Insert an entry onto a page
934 static InsertIndexResult
935 gistentryinsert(Relation r, GISTSTACK *stk, IndexTuple tup,
936 GISTSTATE *giststate)
940 InsertIndexResult res;
945 b = ReadBuffer(r, stk->gs_blk);
946 p = BufferGetPage(b);
948 if (gistnospace(p, tup))
950 res = gistSplit(r, b, stk->gs_parent, tup, giststate);
951 WriteBuffer(b); /* don't forget to release buffer! -
957 res = (InsertIndexResult) palloc(sizeof(InsertIndexResultData));
958 off = gistPageAddItem(giststate, r, p, (Item) tup, IndexTupleSize(tup),
959 InvalidOffsetNumber, LP_USED, &tmpentry, &newtup);
961 ItemPointerSet(&(res->pointerData), stk->gs_blk, off);
962 gistAdjustKeys(r, stk->gs_parent, stk->gs_blk, tmpentry.pred,
963 tmpentry.bytes, giststate);
965 if (tmpentry.pred != (((char *) tup) + sizeof(IndexTupleData)))
966 pfree(tmpentry.pred);
975 gistnewroot(GISTSTATE *giststate, Relation r, IndexTuple lt, IndexTuple rt)
982 b = ReadBuffer(r, GISTP_ROOT);
983 GISTInitBuffer(b, 0);
984 p = BufferGetPage(b);
985 gistPageAddItem(giststate, r, p, (Item) lt, IndexTupleSize(lt),
987 LP_USED, &tmpentry, &newtup);
989 if (tmpentry.pred != (((char *) lt) + sizeof(IndexTupleData)))
990 pfree(tmpentry.pred);
993 gistPageAddItem(giststate, r, p, (Item) rt, IndexTupleSize(rt),
994 OffsetNumberNext(FirstOffsetNumber), LP_USED,
997 if (tmpentry.pred != (((char *) rt) + sizeof(IndexTupleData)))
998 pfree(tmpentry.pred);
1005 GISTInitBuffer(Buffer b, uint32 f)
1007 GISTPageOpaque opaque;
1011 pageSize = BufferGetPageSize(b);
1013 page = BufferGetPage(b);
1014 MemSet(page, 0, (int) pageSize);
1015 PageInit(page, pageSize, sizeof(GISTPageOpaqueData));
1017 opaque = (GISTPageOpaque) PageGetSpecialPointer(page);
1023 ** find entry with lowest penalty
1026 gistchoose(Relation r, Page p, IndexTuple it, /* it has compressed entry */
1027 GISTSTATE *giststate)
1029 OffsetNumber maxoff;
1041 idsize = IndexTupleSize(it) - sizeof(IndexTupleData);
1042 id = ((char *) it) + sizeof(IndexTupleData);
1043 maxoff = PageGetMaxOffsetNumber(p);
1047 gistdentryinit(giststate, &identry, id, (Relation) NULL, (Page) NULL,
1048 (OffsetNumber) 0, idsize, FALSE);
1050 for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
1052 datum = (char *) PageGetItem(p, PageGetItemId(p, i));
1053 size = IndexTupleSize(datum) - sizeof(IndexTupleData);
1054 datum += sizeof(IndexTupleData);
1055 gistdentryinit(giststate, &entry, datum, r, p, i, size, FALSE);
1056 (*fmgr_faddr(&giststate->penaltyFn)) (&entry, &identry, &usize);
1057 if (which_grow < 0 || usize < which_grow)
1061 if (which_grow == 0)
1064 if (entry.pred != datum)
1067 if (identry.pred != id)
1068 pfree(identry.pred);
1074 gistnospace(Page p, IndexTuple it)
1076 return PageGetFreeSpace(p) < IndexTupleSize(it);
1080 gistfreestack(GISTSTACK *s)
1084 while (s != (GISTSTACK *) NULL)
1094 ** remove an entry from a page
1097 gistdelete(Relation r, ItemPointer tid)
1100 OffsetNumber offnum;
1105 * Notes in ExecUtils:ExecOpenIndices() Also note that only vacuum
1106 * deletes index tuples now...
1108 * RelationSetLockForWrite(r);
1111 blkno = ItemPointerGetBlockNumber(tid);
1112 offnum = ItemPointerGetOffsetNumber(tid);
1114 /* adjust any scans that will be affected by this deletion */
1115 gistadjscans(r, GISTOP_DEL, blkno, offnum);
1117 /* delete the index tuple */
1118 buf = ReadBuffer(r, blkno);
1119 page = BufferGetPage(buf);
1121 PageIndexTupleDelete(page, offnum);
1128 initGISTstate(GISTSTATE *giststate, Relation index)
1130 RegProcedure consistent_proc,
1134 RegProcedure penalty_proc,
1138 Form_pg_index itupform;
1140 consistent_proc = index_getprocid(index, 1, GIST_CONSISTENT_PROC);
1141 union_proc = index_getprocid(index, 1, GIST_UNION_PROC);
1142 compress_proc = index_getprocid(index, 1, GIST_COMPRESS_PROC);
1143 decompress_proc = index_getprocid(index, 1, GIST_DECOMPRESS_PROC);
1144 penalty_proc = index_getprocid(index, 1, GIST_PENALTY_PROC);
1145 picksplit_proc = index_getprocid(index, 1, GIST_PICKSPLIT_PROC);
1146 equal_proc = index_getprocid(index, 1, GIST_EQUAL_PROC);
1147 fmgr_info(consistent_proc, &giststate->consistentFn);
1148 fmgr_info(union_proc, &giststate->unionFn);
1149 fmgr_info(compress_proc, &giststate->compressFn);
1150 fmgr_info(decompress_proc, &giststate->decompressFn);
1151 fmgr_info(penalty_proc, &giststate->penaltyFn);
1152 fmgr_info(picksplit_proc, &giststate->picksplitFn);
1153 fmgr_info(equal_proc, &giststate->equalFn);
1155 /* see if key type is different from type of attribute being indexed */
1156 htup = SearchSysCacheTuple(INDEXRELID,
1157 ObjectIdGetDatum(RelationGetRelid(index)),
1159 itupform = (Form_pg_index) GETSTRUCT(htup);
1160 if (!HeapTupleIsValid(htup))
1161 elog(ERROR, "initGISTstate: index %u not found",
1162 RelationGetRelid(index));
1163 giststate->haskeytype = itupform->indhaskeytype;
1164 if (giststate->haskeytype)
1166 /* key type is different -- is it byval? */
1167 htup = SearchSysCacheTuple(ATTNUM,
1168 ObjectIdGetDatum(itupform->indexrelid),
1169 UInt16GetDatum(FirstOffsetNumber),
1171 if (!HeapTupleIsValid(htup))
1173 elog(ERROR, "initGISTstate: no attribute tuple %u %d",
1174 itupform->indexrelid, FirstOffsetNumber);
1177 giststate->keytypbyval = (((Form_pg_attribute) htup)->attbyval);
1180 giststate->keytypbyval = FALSE;
1186 ** Given an IndexTuple to be inserted on a page, this routine replaces
1187 ** the key with another key, which may involve generating a new IndexTuple
1188 ** if the sizes don't match
1191 gist_tuple_replacekey(Relation r, GISTENTRY entry, IndexTuple t)
1193 char *datum = (((char *) t) + sizeof(IndexTupleData));
1195 /* if new entry fits in index tuple, copy it in */
1196 if ((Size) entry.bytes < IndexTupleSize(t) - sizeof(IndexTupleData))
1198 memcpy(datum, entry.pred, entry.bytes);
1199 /* clear out old size */
1200 t->t_info &= 0xe000;
1201 /* or in new size */
1202 t->t_info |= MAXALIGN(entry.bytes + sizeof(IndexTupleData));
1208 /* generate a new index tuple for the compressed entry */
1209 TupleDesc tupDesc = r->rd_att;
1214 isnull = (char *) palloc(r->rd_rel->relnatts);
1215 for (blank = 0; blank < r->rd_rel->relnatts; blank++)
1216 isnull[blank] = ' ';
1217 newtup = (IndexTuple) index_formtuple(tupDesc,
1218 (Datum *) &(entry.pred),
1220 newtup->t_tid = t->t_tid;
1228 ** initialize a GiST entry with a decompressed version of pred
1231 gistdentryinit(GISTSTATE *giststate, GISTENTRY *e, char *pr, Relation r,
1232 Page pg, OffsetNumber o, int b, bool l)
1236 gistentryinit(*e, pr, r, pg, o, b, l);
1237 if (giststate->haskeytype)
1239 dep = (GISTENTRY *) ((*fmgr_faddr(&giststate->decompressFn)) (e));
1240 gistentryinit(*e, dep->pred, dep->rel, dep->page, dep->offset, dep->bytes,
1249 ** initialize a GiST entry with a compressed version of pred
1252 gistcentryinit(GISTSTATE *giststate, GISTENTRY *e, char *pr, Relation r,
1253 Page pg, OffsetNumber o, int b, bool l)
1257 gistentryinit(*e, pr, r, pg, o, b, l);
1258 if (giststate->haskeytype)
1260 cep = (GISTENTRY *) ((*fmgr_faddr(&giststate->compressFn)) (e));
1261 gistentryinit(*e, cep->pred, cep->rel, cep->page, cep->offset, cep->bytes,
1273 ** sloppy debugging support routine, requires recompilation with appropriate
1274 ** "out" method for the index keys. Could be fixed to find that info
1275 ** in the catalogs...
1278 _gistdump(Relation r)
1282 OffsetNumber offnum,
1285 BlockNumber nblocks;
1288 BlockNumber itblkno;
1289 OffsetNumber itoffno;
1293 nblocks = RelationGetNumberOfBlocks(r);
1294 for (blkno = 0; blkno < nblocks; blkno++)
1296 buf = ReadBuffer(r, blkno);
1297 page = BufferGetPage(buf);
1298 po = (GISTPageOpaque) PageGetSpecialPointer(page);
1299 maxoff = PageGetMaxOffsetNumber(page);
1300 printf("Page %d maxoff %d <%s>\n", blkno, maxoff,
1301 (po->flags & F_LEAF ? "LEAF" : "INTERNAL"));
1303 if (PageIsEmpty(page))
1309 for (offnum = FirstOffsetNumber;
1311 offnum = OffsetNumberNext(offnum))
1313 itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, offnum));
1314 itblkno = ItemPointerGetBlockNumber(&(itup->t_tid));
1315 itoffno = ItemPointerGetOffsetNumber(&(itup->t_tid));
1316 datum = ((char *) itup);
1317 datum += sizeof(IndexTupleData);
1318 /* get out function for type of key, and out it! */
1319 itkey = (char *) int_range_out((INTRANGE *) datum);
1320 /* itkey = " unable to print"; */
1321 printf("\t[%d] size %d heap <%d,%d> key:%s\n",
1322 offnum, IndexTupleSize(itup), itblkno, itoffno, itkey);
1331 int_range_out(INTRANGE *r)
1337 result = (char *) palloc(80);
1338 snprintf(result, 80, "[%d,%d): %d", r->lower, r->upper, r->flag);
1343 #endif /* defined GISTDEBUG */