1 /*-------------------------------------------------------------------------
4 * interface routines for the postgres GiST index access method.
9 * $Header: /cvsroot/pgsql/src/backend/access/gist/gist.c,v 1.48 1999/12/10 03:55:42 momjian 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 setheapoverride(true); /* so we can see the new pg_index tuple */
102 initGISTstate(&giststate, index);
103 setheapoverride(false);
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) == true)
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);
288 heap_close(heap, NoLock);
290 UpdateStats(hrelid, nh, true);
291 UpdateStats(irelid, ni, false);
296 UpdateIndexPredicate(irelid, oldPred, pred);
306 * gistinsert -- wrapper for GiST tuple insertion.
308 * This is the public interface routine for tuple insertion in GiSTs.
309 * It doesn't do any work; just locks the relation and passes the buck.
312 gistinsert(Relation r, Datum *datum, char *nulls, ItemPointer ht_ctid, Relation heapRel)
314 InsertIndexResult res;
321 initGISTstate(&giststate, r);
323 /* immediately compress keys to normalize */
324 compvec = (bool *) palloc(sizeof(bool) * r->rd_att->natts);
325 for (i = 0; i < r->rd_att->natts; i++)
327 gistcentryinit(&giststate, &tmpentry, (char *) datum[i],
328 (Relation) NULL, (Page) NULL, (OffsetNumber) 0,
329 -1 /* size is currently bogus */ , TRUE);
330 if (datum[i] != (Datum) tmpentry.pred && !(giststate.keytypbyval))
334 datum[i] = (Datum) tmpentry.pred;
336 itup = index_formtuple(RelationGetDescr(r), datum, nulls);
337 itup->t_tid = *ht_ctid;
340 * Notes in ExecUtils:ExecOpenIndices()
342 * RelationSetLockForWrite(r);
345 res = gistdoinsert(r, itup, &giststate);
346 for (i = 0; i < r->rd_att->natts; i++)
347 if (compvec[i] == TRUE)
348 pfree((char *) datum[i]);
356 ** Take a compressed entry, and install it on a page. Since we now know
357 ** where the entry will live, we decompress it and recompress it using
358 ** that knowledge (some compression routines may want to fish around
359 ** on the page, for example, or do something special for leaf nodes.)
362 gistPageAddItem(GISTSTATE *giststate,
367 OffsetNumber offsetNumber,
373 IndexTuple itup = (IndexTuple) item;
376 * recompress the item given that we now know the exact page and
377 * offset for insertion
379 gistdentryinit(giststate, dentry,
380 (((char *) itup) + sizeof(IndexTupleData)),
381 (Relation) 0, (Page) 0, (OffsetNumber) InvalidOffsetNumber,
382 IndexTupleSize(itup) - sizeof(IndexTupleData), FALSE);
383 gistcentryinit(giststate, &tmpcentry, dentry->pred, r, page,
384 offsetNumber, dentry->bytes, FALSE);
385 *newtup = gist_tuple_replacekey(r, *dentry, itup);
387 if (tmpcentry.pred != dentry->pred
388 && tmpcentry.pred != (((char *) itup) + sizeof(IndexTupleData)))
389 pfree(tmpcentry.pred);
391 return (PageAddItem(page, (Item) *newtup, IndexTupleSize(*newtup),
392 offsetNumber, flags));
396 static InsertIndexResult
397 gistdoinsert(Relation r,
398 IndexTuple itup, /* itup contains compressed entry */
399 GISTSTATE *giststate)
402 InsertIndexResult res;
411 /* 3rd arg is ignored for now */
412 blk = gistChooseSubtree(r, itup, 0, giststate, &stack, &buffer);
413 page = (Page) BufferGetPage(buffer);
415 if (gistnospace(page, itup))
417 /* need to do a split */
418 res = gistSplit(r, buffer, stack, itup, giststate);
419 gistfreestack(stack);
420 WriteBuffer(buffer); /* don't forget to release buffer! */
424 if (PageIsEmpty(page))
425 off = FirstOffsetNumber;
427 off = OffsetNumberNext(PageGetMaxOffsetNumber(page));
429 /* add the item and write the buffer */
430 l = gistPageAddItem(giststate, r, page, (Item) itup, IndexTupleSize(itup),
431 off, LP_USED, &tmpdentry, &newtup);
434 /* now expand the page boundary in the parent to include the new child */
435 gistAdjustKeys(r, stack, blk, tmpdentry.pred, tmpdentry.bytes, giststate);
436 gistfreestack(stack);
441 if (tmpdentry.pred != (((char *) itup) + sizeof(IndexTupleData)))
442 pfree(tmpdentry.pred);
444 /* build and return an InsertIndexResult for this insertion */
445 res = (InsertIndexResult) palloc(sizeof(InsertIndexResultData));
446 ItemPointerSet(&(res->pointerData), blk, l);
453 gistChooseSubtree(Relation r, IndexTuple itup, /* itup has compressed
456 GISTSTATE *giststate,
457 GISTSTACK **retstack /* out */ ,
458 Buffer *leafbuf /* out */ )
464 GISTPageOpaque opaque;
468 buffer = InvalidBuffer;
469 stack = (GISTSTACK *) NULL;
473 /* let go of current buffer before getting next */
474 if (buffer != InvalidBuffer)
475 ReleaseBuffer(buffer);
477 /* get next buffer */
478 buffer = ReadBuffer(r, blk);
479 page = (Page) BufferGetPage(buffer);
481 opaque = (GISTPageOpaque) PageGetSpecialPointer(page);
482 if (!(opaque->flags & F_LEAF))
487 n = (GISTSTACK *) palloc(sizeof(GISTSTACK));
488 n->gs_parent = stack;
490 n->gs_child = gistchoose(r, page, itup, giststate);
493 iid = PageGetItemId(page, n->gs_child);
494 which = (IndexTuple) PageGetItem(page, iid);
495 blk = ItemPointerGetBlockNumber(&(which->t_tid));
497 } while (!(opaque->flags & F_LEAF));
507 gistAdjustKeys(Relation r,
510 char *datum, /* datum is uncompressed */
512 GISTSTATE *giststate)
526 if (stk == (GISTSTACK *) NULL)
529 b = ReadBuffer(r, stk->gs_blk);
530 p = BufferGetPage(b);
532 oldud = (char *) PageGetItem(p, PageGetItemId(p, stk->gs_child));
533 tid = (IndexTuple) oldud;
534 size = IndexTupleSize((IndexTuple) oldud) - sizeof(IndexTupleData);
535 oldud += sizeof(IndexTupleData);
537 evec = (bytea *) palloc(2 * sizeof(GISTENTRY) + VARHDRSZ);
538 VARSIZE(evec) = 2 * sizeof(GISTENTRY) + VARHDRSZ;
540 /* insert decompressed oldud into entry vector */
541 gistdentryinit(giststate, &((GISTENTRY *) VARDATA(evec))[0],
542 oldud, r, p, stk->gs_child,
544 ev0p = &((GISTENTRY *) VARDATA(evec))[0];
546 /* insert datum entry into entry vector */
547 gistentryinit(((GISTENTRY *) VARDATA(evec))[1], datum,
548 (Relation) NULL, (Page) NULL, (OffsetNumber) 0, att_size, FALSE);
549 ev1p = &((GISTENTRY *) VARDATA(evec))[1];
551 /* form union of decompressed entries */
552 datum = (*fmgr_faddr(&giststate->unionFn)) (evec, &datumsize);
554 /* did union leave decompressed version of oldud unchanged? */
555 (*fmgr_faddr(&giststate->equalFn)) (ev0p->pred, datum, &result);
558 TupleDesc td = RelationGetDescr(r);
560 /* compress datum for storage on page */
561 gistcentryinit(giststate, ¢ry, datum, ev0p->rel, ev0p->page,
562 ev0p->offset, datumsize, FALSE);
563 if (td->attrs[0]->attlen >= 0)
565 memmove(oldud, centry.pred, att_size);
566 gistAdjustKeys(r, stk->gs_parent, stk->gs_blk, datum, att_size,
569 else if (VARSIZE(centry.pred) == VARSIZE(oldud))
571 memmove(oldud, centry.pred, VARSIZE(centry.pred));
572 gistAdjustKeys(r, stk->gs_parent, stk->gs_blk, datum, att_size,
579 * * new datum is not the same size as the old. * We have to
580 * delete the old entry and insert the new * one. Note that
581 * this may cause a split here!
584 ItemPointerData oldtid;
587 InsertIndexResult res;
589 /* delete old tuple */
590 ItemPointerSet(&oldtid, stk->gs_blk, stk->gs_child);
591 gistdelete(r, (ItemPointer) &oldtid);
593 /* generate and insert new tuple */
595 isnull = (char *) palloc(r->rd_rel->relnatts);
596 MemSet(isnull, ' ', r->rd_rel->relnatts);
597 newtup = (IndexTuple) index_formtuple(tupDesc,
598 (Datum *) ¢ry.pred, isnull);
600 /* set pointer in new tuple to point to current child */
601 ItemPointerSet(&oldtid, blk, 1);
602 newtup->t_tid = oldtid;
604 /* inserting the new entry also adjust keys above */
605 res = gistentryinsert(r, stk, newtup, giststate);
607 /* in stack, set info to point to new tuple */
608 stk->gs_blk = ItemPointerGetBlockNumber(&(res->pointerData));
609 stk->gs_child = ItemPointerGetOffsetNumber(&(res->pointerData));
615 if (centry.pred != datum)
624 * gistSplit -- split a page in the tree.
627 static InsertIndexResult
628 gistSplit(Relation r,
631 IndexTuple itup, /* contains compressed entry */
632 GISTSTATE *giststate)
646 OffsetNumber leftoff,
650 BlockNumber bufblock;
651 GISTPageOpaque opaque;
653 InsertIndexResult res;
663 isnull = (char *) palloc(r->rd_rel->relnatts);
664 for (blank = 0; blank < r->rd_rel->relnatts; blank++)
666 p = (Page) BufferGetPage(buffer);
667 opaque = (GISTPageOpaque) PageGetSpecialPointer(p);
671 * The root of the tree is the first block in the relation. If we're
672 * about to split the root, we need to do some hocus-pocus to enforce
676 if (BufferGetBlockNumber(buffer) == GISTP_ROOT)
678 leftbuf = ReadBuffer(r, P_NEW);
679 GISTInitBuffer(leftbuf, opaque->flags);
680 lbknum = BufferGetBlockNumber(leftbuf);
681 left = (Page) BufferGetPage(leftbuf);
686 IncrBufferRefCount(buffer);
687 lbknum = BufferGetBlockNumber(buffer);
688 left = (Page) PageGetTempPage(p, sizeof(GISTPageOpaqueData));
691 rightbuf = ReadBuffer(r, P_NEW);
692 GISTInitBuffer(rightbuf, opaque->flags);
693 rbknum = BufferGetBlockNumber(rightbuf);
694 right = (Page) BufferGetPage(rightbuf);
696 /* generate the item array */
697 maxoff = PageGetMaxOffsetNumber(p);
698 entryvec = (bytea *) palloc(VARHDRSZ + (maxoff + 2) * sizeof(GISTENTRY));
699 decompvec = (bool *) palloc(VARHDRSZ + (maxoff + 2) * sizeof(bool));
700 for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
702 item_1 = (IndexTuple) PageGetItem(p, PageGetItemId(p, i));
703 gistdentryinit(giststate, &((GISTENTRY *) VARDATA(entryvec))[i],
704 (((char *) item_1) + sizeof(IndexTupleData)),
706 IndexTupleSize(item_1) - sizeof(IndexTupleData), FALSE);
707 if ((char *) (((GISTENTRY *) VARDATA(entryvec))[i].pred)
708 == (((char *) item_1) + sizeof(IndexTupleData)))
709 decompvec[i] = FALSE;
714 /* add the new datum as the last entry */
715 gistdentryinit(giststate, &(((GISTENTRY *) VARDATA(entryvec))[maxoff + 1]),
716 (((char *) itup) + sizeof(IndexTupleData)),
717 (Relation) NULL, (Page) NULL,
718 (OffsetNumber) 0, tmpentry.bytes, FALSE);
719 if ((char *) (((GISTENTRY *) VARDATA(entryvec))[maxoff + 1]).pred !=
720 (((char *) itup) + sizeof(IndexTupleData)))
721 decompvec[maxoff + 1] = TRUE;
723 decompvec[maxoff + 1] = FALSE;
725 VARSIZE(entryvec) = (maxoff + 2) * sizeof(GISTENTRY) + VARHDRSZ;
727 /* now let the user-defined picksplit function set up the split vector */
728 (*fmgr_faddr(&giststate->picksplitFn)) (entryvec, &v);
730 /* compress ldatum and rdatum */
731 gistcentryinit(giststate, &tmpentry, v.spl_ldatum, (Relation) NULL,
732 (Page) NULL, (OffsetNumber) 0,
733 ((GISTENTRY *) VARDATA(entryvec))[i].bytes, FALSE);
734 if (v.spl_ldatum != tmpentry.pred)
736 v.spl_ldatum = tmpentry.pred;
738 gistcentryinit(giststate, &tmpentry, v.spl_rdatum, (Relation) NULL,
739 (Page) NULL, (OffsetNumber) 0,
740 ((GISTENTRY *) VARDATA(entryvec))[i].bytes, FALSE);
741 if (v.spl_rdatum != tmpentry.pred)
743 v.spl_rdatum = tmpentry.pred;
745 /* clean up the entry vector: its preds need to be deleted, too */
746 for (i = FirstOffsetNumber; i <= maxoff + 1; i = OffsetNumberNext(i))
748 pfree(((GISTENTRY *) VARDATA(entryvec))[i].pred);
752 leftoff = rightoff = FirstOffsetNumber;
753 maxoff = PageGetMaxOffsetNumber(p);
754 for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
756 itemid = PageGetItemId(p, i);
757 item = (IndexTuple) PageGetItem(p, itemid);
759 if (i == *(v.spl_left))
761 gistPageAddItem(giststate, r, left, (Item) item,
762 IndexTupleSize(item),
763 leftoff, LP_USED, &tmpdentry, &newtup);
764 leftoff = OffsetNumberNext(leftoff);
765 v.spl_left++; /* advance in left split vector */
767 if (tmpdentry.pred != (((char *) item) + sizeof(IndexTupleData)))
768 pfree(tmpdentry.pred);
769 if ((IndexTuple) item != newtup)
774 gistPageAddItem(giststate, r, right, (Item) item,
775 IndexTupleSize(item),
776 rightoff, LP_USED, &tmpdentry, &newtup);
777 rightoff = OffsetNumberNext(rightoff);
778 v.spl_right++; /* advance in right split vector */
780 if (tmpdentry.pred != (((char *) item) + sizeof(IndexTupleData)))
781 pfree(tmpdentry.pred);
787 /* build an InsertIndexResult for this insertion */
788 res = (InsertIndexResult) palloc(sizeof(InsertIndexResultData));
790 /* now insert the new index tuple */
791 if (*(v.spl_left) != FirstOffsetNumber)
793 gistPageAddItem(giststate, r, left, (Item) itup,
794 IndexTupleSize(itup),
795 leftoff, LP_USED, &tmpdentry, &newtup);
796 leftoff = OffsetNumberNext(leftoff);
797 ItemPointerSet(&(res->pointerData), lbknum, leftoff);
799 if (tmpdentry.pred != (((char *) itup) + sizeof(IndexTupleData)))
800 pfree(tmpdentry.pred);
806 gistPageAddItem(giststate, r, right, (Item) itup,
807 IndexTupleSize(itup),
808 rightoff, LP_USED, &tmpdentry, &newtup);
809 rightoff = OffsetNumberNext(rightoff);
810 ItemPointerSet(&(res->pointerData), rbknum, rightoff);
812 if (tmpdentry.pred != (((char *) itup) + sizeof(IndexTupleData)))
813 pfree(tmpdentry.pred);
818 if ((bufblock = BufferGetBlockNumber(buffer)) != GISTP_ROOT)
819 PageRestoreTempPage(left, p);
820 WriteBuffer(leftbuf);
821 WriteBuffer(rightbuf);
824 * Okay, the page is split. We have three things left to do:
826 * 1) Adjust any active scans on this index to cope with changes we
827 * introduced in its structure by splitting this page.
829 * 2) "Tighten" the bounding box of the pointer to the left page in the
830 * parent node in the tree, if any. Since we moved a bunch of stuff
831 * off the left page, we expect it to get smaller. This happens in
832 * the internal insertion routine.
834 * 3) Insert a pointer to the right page in the parent. This may cause
835 * the parent to split. If it does, we need to repeat steps one and
836 * two for each split node in the tree.
839 /* adjust active scans */
840 gistadjscans(r, GISTOP_SPLIT, bufblock, FirstOffsetNumber);
844 ltup = (IndexTuple) index_formtuple(tupDesc,
845 (Datum *) &(v.spl_ldatum), isnull);
846 rtup = (IndexTuple) index_formtuple(tupDesc,
847 (Datum *) &(v.spl_rdatum), isnull);
850 /* set pointers to new child pages in the internal index tuples */
851 ItemPointerSet(&(ltup->t_tid), lbknum, 1);
852 ItemPointerSet(&(rtup->t_tid), rbknum, 1);
854 gistintinsert(r, stack, ltup, rtup, giststate);
863 ** After a split, we need to overwrite the old entry's key in the parent,
864 ** and install install an entry for the new key into the parent.
867 gistintinsert(Relation r,
869 IndexTuple ltup, /* new version of entry for old page */
870 IndexTuple rtup, /* entry for new page */
871 GISTSTATE *giststate)
873 ItemPointerData ltid;
875 if (stk == (GISTSTACK *) NULL)
877 gistnewroot(giststate, r, ltup, rtup);
881 /* remove old left pointer, insert the 2 new entries */
882 ItemPointerSet(<id, stk->gs_blk, stk->gs_child);
883 gistdelete(r, (ItemPointer) <id);
884 gistentryinserttwo(r, stk, ltup, rtup, giststate);
889 ** Insert two entries onto one page, handling a split for either one!
892 gistentryinserttwo(Relation r, GISTSTACK *stk, IndexTuple ltup,
893 IndexTuple rtup, GISTSTATE *giststate)
897 InsertIndexResult res;
901 b = ReadBuffer(r, stk->gs_blk);
902 p = BufferGetPage(b);
904 if (gistnospace(p, ltup))
906 res = gistSplit(r, b, stk->gs_parent, ltup, giststate);
907 WriteBuffer(b); /* don't forget to release buffer! -
910 gistdoinsert(r, rtup, giststate);
914 gistPageAddItem(giststate, r, p, (Item) ltup,
915 IndexTupleSize(ltup), InvalidOffsetNumber,
916 LP_USED, &tmpentry, &newtup);
918 gistAdjustKeys(r, stk->gs_parent, stk->gs_blk, tmpentry.pred,
919 tmpentry.bytes, giststate);
921 if (tmpentry.pred != (((char *) ltup) + sizeof(IndexTupleData)))
922 pfree(tmpentry.pred);
925 gistentryinsert(r, stk, rtup, giststate);
931 ** Insert an entry onto a page
933 static InsertIndexResult
934 gistentryinsert(Relation r, GISTSTACK *stk, IndexTuple tup,
935 GISTSTATE *giststate)
939 InsertIndexResult res;
944 b = ReadBuffer(r, stk->gs_blk);
945 p = BufferGetPage(b);
947 if (gistnospace(p, tup))
949 res = gistSplit(r, b, stk->gs_parent, tup, giststate);
950 WriteBuffer(b); /* don't forget to release buffer! -
956 res = (InsertIndexResult) palloc(sizeof(InsertIndexResultData));
957 off = gistPageAddItem(giststate, r, p, (Item) tup, IndexTupleSize(tup),
958 InvalidOffsetNumber, LP_USED, &tmpentry, &newtup);
960 ItemPointerSet(&(res->pointerData), stk->gs_blk, off);
961 gistAdjustKeys(r, stk->gs_parent, stk->gs_blk, tmpentry.pred,
962 tmpentry.bytes, giststate);
964 if (tmpentry.pred != (((char *) tup) + sizeof(IndexTupleData)))
965 pfree(tmpentry.pred);
974 gistnewroot(GISTSTATE *giststate, Relation r, IndexTuple lt, IndexTuple rt)
981 b = ReadBuffer(r, GISTP_ROOT);
982 GISTInitBuffer(b, 0);
983 p = BufferGetPage(b);
984 gistPageAddItem(giststate, r, p, (Item) lt, IndexTupleSize(lt),
986 LP_USED, &tmpentry, &newtup);
988 if (tmpentry.pred != (((char *) lt) + sizeof(IndexTupleData)))
989 pfree(tmpentry.pred);
992 gistPageAddItem(giststate, r, p, (Item) rt, IndexTupleSize(rt),
993 OffsetNumberNext(FirstOffsetNumber), LP_USED,
996 if (tmpentry.pred != (((char *) rt) + sizeof(IndexTupleData)))
997 pfree(tmpentry.pred);
1004 GISTInitBuffer(Buffer b, uint32 f)
1006 GISTPageOpaque opaque;
1010 pageSize = BufferGetPageSize(b);
1012 page = BufferGetPage(b);
1013 MemSet(page, 0, (int) pageSize);
1014 PageInit(page, pageSize, sizeof(GISTPageOpaqueData));
1016 opaque = (GISTPageOpaque) PageGetSpecialPointer(page);
1022 ** find entry with lowest penalty
1025 gistchoose(Relation r, Page p, IndexTuple it, /* it has compressed entry */
1026 GISTSTATE *giststate)
1028 OffsetNumber maxoff;
1040 idsize = IndexTupleSize(it) - sizeof(IndexTupleData);
1041 id = ((char *) it) + sizeof(IndexTupleData);
1042 maxoff = PageGetMaxOffsetNumber(p);
1046 gistdentryinit(giststate, &identry, id, (Relation) NULL, (Page) NULL,
1047 (OffsetNumber) 0, idsize, FALSE);
1049 for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
1051 datum = (char *) PageGetItem(p, PageGetItemId(p, i));
1052 size = IndexTupleSize(datum) - sizeof(IndexTupleData);
1053 datum += sizeof(IndexTupleData);
1054 gistdentryinit(giststate, &entry, datum, r, p, i, size, FALSE);
1055 (*fmgr_faddr(&giststate->penaltyFn)) (&entry, &identry, &usize);
1056 if (which_grow < 0 || usize < which_grow)
1060 if (which_grow == 0)
1063 if (entry.pred != datum)
1066 if (identry.pred != id)
1067 pfree(identry.pred);
1073 gistnospace(Page p, IndexTuple it)
1075 return PageGetFreeSpace(p) < IndexTupleSize(it);
1079 gistfreestack(GISTSTACK *s)
1083 while (s != (GISTSTACK *) NULL)
1093 ** remove an entry from a page
1096 gistdelete(Relation r, ItemPointer tid)
1099 OffsetNumber offnum;
1104 * Notes in ExecUtils:ExecOpenIndices() Also note that only vacuum
1105 * deletes index tuples now...
1107 * RelationSetLockForWrite(r);
1110 blkno = ItemPointerGetBlockNumber(tid);
1111 offnum = ItemPointerGetOffsetNumber(tid);
1113 /* adjust any scans that will be affected by this deletion */
1114 gistadjscans(r, GISTOP_DEL, blkno, offnum);
1116 /* delete the index tuple */
1117 buf = ReadBuffer(r, blkno);
1118 page = BufferGetPage(buf);
1120 PageIndexTupleDelete(page, offnum);
1127 initGISTstate(GISTSTATE *giststate, Relation index)
1129 RegProcedure consistent_proc,
1133 RegProcedure penalty_proc,
1137 Form_pg_index itupform;
1139 consistent_proc = index_getprocid(index, 1, GIST_CONSISTENT_PROC);
1140 union_proc = index_getprocid(index, 1, GIST_UNION_PROC);
1141 compress_proc = index_getprocid(index, 1, GIST_COMPRESS_PROC);
1142 decompress_proc = index_getprocid(index, 1, GIST_DECOMPRESS_PROC);
1143 penalty_proc = index_getprocid(index, 1, GIST_PENALTY_PROC);
1144 picksplit_proc = index_getprocid(index, 1, GIST_PICKSPLIT_PROC);
1145 equal_proc = index_getprocid(index, 1, GIST_EQUAL_PROC);
1146 fmgr_info(consistent_proc, &giststate->consistentFn);
1147 fmgr_info(union_proc, &giststate->unionFn);
1148 fmgr_info(compress_proc, &giststate->compressFn);
1149 fmgr_info(decompress_proc, &giststate->decompressFn);
1150 fmgr_info(penalty_proc, &giststate->penaltyFn);
1151 fmgr_info(picksplit_proc, &giststate->picksplitFn);
1152 fmgr_info(equal_proc, &giststate->equalFn);
1154 /* see if key type is different from type of attribute being indexed */
1155 htup = SearchSysCacheTuple(INDEXRELID,
1156 ObjectIdGetDatum(RelationGetRelid(index)),
1158 itupform = (Form_pg_index) GETSTRUCT(htup);
1159 if (!HeapTupleIsValid(htup))
1160 elog(ERROR, "initGISTstate: index %u not found",
1161 RelationGetRelid(index));
1162 giststate->haskeytype = itupform->indhaskeytype;
1163 if (giststate->haskeytype)
1165 /* key type is different -- is it byval? */
1166 htup = SearchSysCacheTuple(ATTNUM,
1167 ObjectIdGetDatum(itupform->indexrelid),
1168 UInt16GetDatum(FirstOffsetNumber),
1170 if (!HeapTupleIsValid(htup))
1172 elog(ERROR, "initGISTstate: no attribute tuple %u %d",
1173 itupform->indexrelid, FirstOffsetNumber);
1176 giststate->keytypbyval = (((Form_pg_attribute) htup)->attbyval);
1179 giststate->keytypbyval = FALSE;
1185 ** Given an IndexTuple to be inserted on a page, this routine replaces
1186 ** the key with another key, which may involve generating a new IndexTuple
1187 ** if the sizes don't match
1190 gist_tuple_replacekey(Relation r, GISTENTRY entry, IndexTuple t)
1192 char *datum = (((char *) t) + sizeof(IndexTupleData));
1194 /* if new entry fits in index tuple, copy it in */
1195 if (entry.bytes < IndexTupleSize(t) - sizeof(IndexTupleData))
1197 memcpy(datum, entry.pred, entry.bytes);
1198 /* clear out old size */
1199 t->t_info &= 0xe000;
1200 /* or in new size */
1201 t->t_info |= MAXALIGN(entry.bytes + sizeof(IndexTupleData));
1207 /* generate a new index tuple for the compressed entry */
1208 TupleDesc tupDesc = r->rd_att;
1213 isnull = (char *) palloc(r->rd_rel->relnatts);
1214 for (blank = 0; blank < r->rd_rel->relnatts; blank++)
1215 isnull[blank] = ' ';
1216 newtup = (IndexTuple) index_formtuple(tupDesc,
1217 (Datum *) &(entry.pred),
1219 newtup->t_tid = t->t_tid;
1227 ** initialize a GiST entry with a decompressed version of pred
1230 gistdentryinit(GISTSTATE *giststate, GISTENTRY *e, char *pr, Relation r,
1231 Page pg, OffsetNumber o, int b, bool l)
1235 gistentryinit(*e, pr, r, pg, o, b, l);
1236 if (giststate->haskeytype)
1238 dep = (GISTENTRY *) ((*fmgr_faddr(&giststate->decompressFn)) (e));
1239 gistentryinit(*e, dep->pred, dep->rel, dep->page, dep->offset, dep->bytes,
1248 ** initialize a GiST entry with a compressed version of pred
1251 gistcentryinit(GISTSTATE *giststate, GISTENTRY *e, char *pr, Relation r,
1252 Page pg, OffsetNumber o, int b, bool l)
1256 gistentryinit(*e, pr, r, pg, o, b, l);
1257 if (giststate->haskeytype)
1259 cep = (GISTENTRY *) ((*fmgr_faddr(&giststate->compressFn)) (e));
1260 gistentryinit(*e, cep->pred, cep->rel, cep->page, cep->offset, cep->bytes,
1272 ** sloppy debugging support routine, requires recompilation with appropriate
1273 ** "out" method for the index keys. Could be fixed to find that info
1274 ** in the catalogs...
1277 _gistdump(Relation r)
1281 OffsetNumber offnum,
1284 BlockNumber nblocks;
1287 BlockNumber itblkno;
1288 OffsetNumber itoffno;
1292 nblocks = RelationGetNumberOfBlocks(r);
1293 for (blkno = 0; blkno < nblocks; blkno++)
1295 buf = ReadBuffer(r, blkno);
1296 page = BufferGetPage(buf);
1297 po = (GISTPageOpaque) PageGetSpecialPointer(page);
1298 maxoff = PageGetMaxOffsetNumber(page);
1299 printf("Page %d maxoff %d <%s>\n", blkno, maxoff,
1300 (po->flags & F_LEAF ? "LEAF" : "INTERNAL"));
1302 if (PageIsEmpty(page))
1308 for (offnum = FirstOffsetNumber;
1310 offnum = OffsetNumberNext(offnum))
1312 itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, offnum));
1313 itblkno = ItemPointerGetBlockNumber(&(itup->t_tid));
1314 itoffno = ItemPointerGetOffsetNumber(&(itup->t_tid));
1315 datum = ((char *) itup);
1316 datum += sizeof(IndexTupleData);
1317 /* get out function for type of key, and out it! */
1318 itkey = (char *) int_range_out((INTRANGE *) datum);
1319 /* itkey = " unable to print"; */
1320 printf("\t[%d] size %d heap <%d,%d> key:%s\n",
1321 offnum, IndexTupleSize(itup), itblkno, itoffno, itkey);
1330 int_range_out(INTRANGE *r)
1336 result = (char *) palloc(80);
1337 snprintf(result, 80, "[%d,%d): %d", r->lower, r->upper, r->flag);
1342 #endif /* defined GISTDEBUG */