1 /*-------------------------------------------------------------------------
4 * interface routines for the postgres GiST index access method.
11 *-------------------------------------------------------------------------
17 #include <access/genam.h>
18 #include <access/gist.h>
19 #include <access/gistscan.h>
20 #include <access/heapam.h>
21 #include <catalog/index.h>
22 #include <executor/executor.h>
23 #include <storage/bufmgr.h>
24 #include <storage/bufpage.h>
25 #include <storage/lmgr.h>
26 #include <utils/syscache.h>
27 #include <utils/tqual.h>
30 #include <regex/utils.h>
35 /* non-export function prototypes */
36 static InsertIndexResult
37 gistdoinsert(Relation r, IndexTuple itup,
38 GISTSTATE *GISTstate);
39 static InsertIndexResult
40 gistentryinsert(Relation r, GISTSTACK *stk,
42 GISTSTATE *giststate);
44 gistentryinserttwo(Relation r, GISTSTACK *stk, IndexTuple ltup,
45 IndexTuple rtup, GISTSTATE *giststate);
47 gistAdjustKeys(Relation r, GISTSTACK *stk, BlockNumber blk,
48 char *datum, int att_size, GISTSTATE *giststate);
50 gistintinsert(Relation r, GISTSTACK *stk, IndexTuple ltup,
51 IndexTuple rtup, GISTSTATE *giststate);
52 static InsertIndexResult
53 gistSplit(Relation r, Buffer buffer,
54 GISTSTACK *stack, IndexTuple itup,
55 GISTSTATE *giststate);
57 gistnewroot(GISTSTATE *giststate, Relation r, IndexTuple lt,
59 static void GISTInitBuffer(Buffer b, uint32 f);
61 gistChooseSubtree(Relation r, IndexTuple itup, int level,
63 GISTSTACK **retstack, Buffer *leafbuf);
65 gistchoose(Relation r, Page p, IndexTuple it,
66 GISTSTATE *giststate);
67 static int gistnospace(Page p, IndexTuple it);
68 void gistdelete(Relation r, ItemPointer tid);
69 static IndexTuple gist_tuple_replacekey(Relation r, GISTENTRY entry, IndexTuple t);
71 gistcentryinit(GISTSTATE *giststate, GISTENTRY *e, char *pr,
72 Relation r, Page pg, OffsetNumber o, int b, bool l);
73 static char *int_range_out(INTRANGE *r);
76 ** routine to build an index. Basically calls insert over and over
79 gistbuild(Relation heap,
95 InsertIndexResult res;
102 #ifndef OMIT_PARTIAL_INDEX
103 ExprContext *econtext;
104 TupleTable tupleTable;
105 TupleTableSlot *slot;
114 Buffer buffer = InvalidBuffer;
117 /* GiSTs only know how to do stupid locking now */
118 RelationSetLockForWrite(index);
120 setheapoverride(true); /* so we can see the new pg_index tuple */
121 initGISTstate(&giststate, index);
122 setheapoverride(false);
124 pred = predInfo->pred;
125 oldPred = predInfo->oldPred;
128 * We expect to be called exactly once for any index relation. If
129 * that's not the case, big trouble's what we have.
132 if (oldPred == NULL && (nb = RelationGetNumberOfBlocks(index)) != 0)
133 elog(ERROR, "%s already contains data", index->rd_rel->relname.data);
135 /* initialize the root page (if this is a new index) */
138 buffer = ReadBuffer(index, P_NEW);
139 GISTInitBuffer(buffer, F_LEAF);
143 /* init the tuple descriptors and get set for a heap scan */
144 hd = RelationGetDescr(heap);
145 id = RelationGetDescr(index);
146 d = (Datum *) palloc(natts * sizeof(*d));
147 nulls = (bool *) palloc(natts * sizeof(*nulls));
150 * If this is a predicate (partial) index, we will need to evaluate
151 * the predicate using ExecQual, which requires the current tuple to
152 * be in a slot of a TupleTable. In addition, ExecQual must have an
153 * ExprContext referring to that slot. Here, we initialize dummy
154 * TupleTable and ExprContext objects for this purpose. --Nels, Feb
157 #ifndef OMIT_PARTIAL_INDEX
158 if (pred != NULL || oldPred != NULL)
160 tupleTable = ExecCreateTupleTable(1);
161 slot = ExecAllocTableSlot(tupleTable);
162 econtext = makeNode(ExprContext);
163 FillDummyExprContext(econtext, slot, hd, buffer);
166 /* shut the compiler up */
172 #endif /* OMIT_PARTIAL_INDEX */
173 /* int the tuples as we insert them */
176 scan = heap_beginscan(heap, 0, SnapshotNow, 0, (ScanKey) NULL);
178 while (HeapTupleIsValid(htup = heap_getnext(scan, 0)))
183 * If oldPred != NULL, this is an EXTEND INDEX command, so skip
184 * this tuple if it was already in the existing partial index
188 #ifndef OMIT_PARTIAL_INDEX
189 /* SetSlotContents(slot, htup); */
191 if (ExecQual((List *) oldPred, econtext) == true)
196 #endif /* OMIT_PARTIAL_INDEX */
200 * Skip this tuple if it doesn't satisfy the partial-index
205 #ifndef OMIT_PARTIAL_INDEX
206 /* SetSlotContents(slot, htup); */
208 if (ExecQual((List *) pred, econtext) == false)
210 #endif /* OMIT_PARTIAL_INDEX */
216 * For the current heap tuple, extract all the attributes we use
217 * in this index, and note which are null.
220 for (i = 1; i <= natts; i++)
226 * Offsets are from the start of the tuple, and are
227 * zero-based; indices are one-based. The next call returns i
228 * - 1. That's data hiding for you.
231 attoff = AttrNumberGetAttrOffset(i);
234 * d[attoff] = HeapTupleGetAttributeValue(htup, buffer,
236 d[attoff] = GetIndexValue(htup,
242 nulls[attoff] = (attnull ? 'n' : ' ');
245 /* immediately compress keys to normalize */
246 compvec = (bool *) palloc(sizeof(bool) * natts);
247 for (i = 0; i < natts; i++)
249 gistcentryinit(&giststate, &tmpcentry, (char *) d[i],
250 (Relation) NULL, (Page) NULL, (OffsetNumber) 0,
251 -1 /* size is currently bogus */ , TRUE);
252 if (d[i] != (Datum) tmpcentry.pred && !(giststate.keytypbyval))
256 d[i] = (Datum) tmpcentry.pred;
259 /* form an index tuple and point it at the heap tuple */
260 itup = index_formtuple(id, &d[0], nulls);
261 itup->t_tid = htup->t_ctid;
264 * Since we already have the index relation locked, we call
265 * gistdoinsert directly. Normal access method calls dispatch
266 * through gistinsert, which locks the relation for write. This
267 * is the right thing to do if you're inserting single tups, but
268 * not when you're initializing the whole index at once.
271 res = gistdoinsert(index, itup, &giststate);
272 for (i = 0; i < natts; i++)
273 if (compvec[i] == TRUE)
274 pfree((char *) d[i]);
280 /* okay, all heap tuples are indexed */
282 RelationUnsetLockForWrite(index);
284 if (pred != NULL || oldPred != NULL)
286 #ifndef OMIT_PARTIAL_INDEX
287 ExecDestroyTupleTable(tupleTable, true);
289 #endif /* OMIT_PARTIAL_INDEX */
293 * Since we just inted the tuples in the heap, we update its stats in
294 * pg_relation to guarantee that the planner takes advantage of the
295 * index we just created. UpdateStats() does a
296 * CommandinterIncrement(), which flushes changed entries from the
297 * system relcache. The act of constructing an index changes these
298 * heap and index tuples in the system catalogs, so they need to be
299 * flushed. We close them to guarantee that they will be.
302 hrelid = RelationGetRelid(heap);
303 irelid = RelationGetRelid(index);
307 UpdateStats(hrelid, nh, true);
308 UpdateStats(irelid, ni, false);
314 UpdateIndexPredicate(irelid, oldPred, pred);
323 * gistinsert -- wrapper for GiST tuple insertion.
325 * This is the public interface routine for tuple insertion in GiSTs.
326 * It doesn't do any work; just locks the relation and passes the buck.
329 gistinsert(Relation r, Datum *datum, char *nulls, ItemPointer ht_ctid, Relation heapRel)
331 InsertIndexResult res;
338 initGISTstate(&giststate, r);
340 /* immediately compress keys to normalize */
341 compvec = (bool *) palloc(sizeof(bool) * r->rd_att->natts);
342 for (i = 0; i < r->rd_att->natts; i++)
344 gistcentryinit(&giststate, &tmpentry, (char *) datum[i],
345 (Relation) NULL, (Page) NULL, (OffsetNumber) 0,
346 -1 /* size is currently bogus */ , TRUE);
347 if (datum[i] != (Datum) tmpentry.pred && !(giststate.keytypbyval))
351 datum[i] = (Datum) tmpentry.pred;
353 itup = index_formtuple(RelationGetDescr(r), datum, nulls);
354 itup->t_tid = *ht_ctid;
356 RelationSetLockForWrite(r);
357 res = gistdoinsert(r, itup, &giststate);
358 for (i = 0; i < r->rd_att->natts; i++)
359 if (compvec[i] == TRUE)
360 pfree((char *) datum[i]);
364 /* XXX two-phase locking -- don't unlock the relation until EOT */
369 ** Take a compressed entry, and install it on a page. Since we now know
370 ** where the entry will live, we decompress it and recompress it using
371 ** that knowledge (some compression routines may want to fish around
372 ** on the page, for example, or do something special for leaf nodes.)
375 gistPageAddItem(GISTSTATE *giststate,
380 OffsetNumber offsetNumber,
386 IndexTuple itup = (IndexTuple) item;
389 * recompress the item given that we now know the exact page and
390 * offset for insertion
392 gistdentryinit(giststate, dentry,
393 (((char *) itup) + sizeof(IndexTupleData)),
394 (Relation) 0, (Page) 0, (OffsetNumber) InvalidOffsetNumber,
395 IndexTupleSize(itup) - sizeof(IndexTupleData), FALSE);
396 gistcentryinit(giststate, &tmpcentry, dentry->pred, r, page,
397 offsetNumber, dentry->bytes, FALSE);
398 *newtup = gist_tuple_replacekey(r, *dentry, itup);
400 if (tmpcentry.pred != dentry->pred
401 && tmpcentry.pred != (((char *) itup) + sizeof(IndexTupleData)))
402 pfree(tmpcentry.pred);
404 return (PageAddItem(page, (Item) *newtup, IndexTupleSize(*newtup),
405 offsetNumber, flags));
409 static InsertIndexResult
410 gistdoinsert(Relation r,
411 IndexTuple itup, /* itup contains compressed entry */
412 GISTSTATE *giststate)
415 InsertIndexResult res;
424 /* 3rd arg is ignored for now */
425 blk = gistChooseSubtree(r, itup, 0, giststate, &stack, &buffer);
426 page = (Page) BufferGetPage(buffer);
428 if (gistnospace(page, itup))
430 /* need to do a split */
431 res = gistSplit(r, buffer, stack, itup, giststate);
432 gistfreestack(stack);
433 WriteBuffer(buffer); /* don't forget to release buffer! */
437 if (PageIsEmpty(page))
438 off = FirstOffsetNumber;
440 off = OffsetNumberNext(PageGetMaxOffsetNumber(page));
442 /* add the item and write the buffer */
443 l = gistPageAddItem(giststate, r, page, (Item) itup, IndexTupleSize(itup),
444 off, LP_USED, &tmpdentry, &newtup);
447 /* now expand the page boundary in the parent to include the new child */
448 gistAdjustKeys(r, stack, blk, tmpdentry.pred, tmpdentry.bytes, giststate);
449 gistfreestack(stack);
454 if (tmpdentry.pred != (((char *) itup) + sizeof(IndexTupleData)))
455 pfree(tmpdentry.pred);
457 /* build and return an InsertIndexResult for this insertion */
458 res = (InsertIndexResult) palloc(sizeof(InsertIndexResultData));
459 ItemPointerSet(&(res->pointerData), blk, l);
466 gistChooseSubtree(Relation r, IndexTuple itup, /* itup has compressed
469 GISTSTATE *giststate,
470 GISTSTACK **retstack /* out */ ,
471 Buffer *leafbuf /* out */ )
477 GISTPageOpaque opaque;
481 buffer = InvalidBuffer;
482 stack = (GISTSTACK *) NULL;
486 /* let go of current buffer before getting next */
487 if (buffer != InvalidBuffer)
488 ReleaseBuffer(buffer);
490 /* get next buffer */
491 buffer = ReadBuffer(r, blk);
492 page = (Page) BufferGetPage(buffer);
494 opaque = (GISTPageOpaque) PageGetSpecialPointer(page);
495 if (!(opaque->flags & F_LEAF))
500 n = (GISTSTACK *) palloc(sizeof(GISTSTACK));
501 n->gs_parent = stack;
503 n->gs_child = gistchoose(r, page, itup, giststate);
506 iid = PageGetItemId(page, n->gs_child);
507 which = (IndexTuple) PageGetItem(page, iid);
508 blk = ItemPointerGetBlockNumber(&(which->t_tid));
510 } while (!(opaque->flags & F_LEAF));
520 gistAdjustKeys(Relation r,
523 char *datum, /* datum is uncompressed */
525 GISTSTATE *giststate)
539 if (stk == (GISTSTACK *) NULL)
542 b = ReadBuffer(r, stk->gs_blk);
543 p = BufferGetPage(b);
545 oldud = (char *) PageGetItem(p, PageGetItemId(p, stk->gs_child));
546 tid = (IndexTuple) oldud;
547 size = IndexTupleSize((IndexTuple) oldud) - sizeof(IndexTupleData);
548 oldud += sizeof(IndexTupleData);
550 evec = (bytea *) palloc(2 * sizeof(GISTENTRY) + VARHDRSZ);
551 VARSIZE(evec) = 2 * sizeof(GISTENTRY) + VARHDRSZ;
553 /* insert decompressed oldud into entry vector */
554 gistdentryinit(giststate, &((GISTENTRY *) VARDATA(evec))[0],
555 oldud, r, p, stk->gs_child,
557 ev0p = &((GISTENTRY *) VARDATA(evec))[0];
559 /* insert datum entry into entry vector */
560 gistentryinit(((GISTENTRY *) VARDATA(evec))[1], datum,
561 (Relation) NULL, (Page) NULL, (OffsetNumber) 0, att_size, FALSE);
562 ev1p = &((GISTENTRY *) VARDATA(evec))[1];
564 /* form union of decompressed entries */
565 datum = (*fmgr_faddr(&giststate->unionFn)) (evec, &datumsize);
567 /* did union leave decompressed version of oldud unchanged? */
568 (*fmgr_faddr(&giststate->equalFn)) (ev0p->pred, datum, &result);
571 TupleDesc td = RelationGetDescr(r);
573 /* compress datum for storage on page */
574 gistcentryinit(giststate, ¢ry, datum, ev0p->rel, ev0p->page,
575 ev0p->offset, datumsize, FALSE);
576 if (td->attrs[0]->attlen >= 0)
578 memmove(oldud, centry.pred, att_size);
579 gistAdjustKeys(r, stk->gs_parent, stk->gs_blk, datum, att_size,
582 else if (VARSIZE(centry.pred) == VARSIZE(oldud))
584 memmove(oldud, centry.pred, VARSIZE(centry.pred));
585 gistAdjustKeys(r, stk->gs_parent, stk->gs_blk, datum, att_size,
592 * * new datum is not the same size as the old. * We have to
593 * delete the old entry and insert the new * one. Note that
594 * this may cause a split here!
597 ItemPointerData oldtid;
600 InsertIndexResult res;
602 /* delete old tuple */
603 ItemPointerSet(&oldtid, stk->gs_blk, stk->gs_child);
604 gistdelete(r, (ItemPointer) &oldtid);
606 /* generate and insert new tuple */
608 isnull = (char *) palloc(r->rd_rel->relnatts);
609 MemSet(isnull, ' ', r->rd_rel->relnatts);
610 newtup = (IndexTuple) index_formtuple(tupDesc,
611 (Datum *) ¢ry.pred, isnull);
613 /* set pointer in new tuple to point to current child */
614 ItemPointerSet(&oldtid, blk, 1);
615 newtup->t_tid = oldtid;
617 /* inserting the new entry also adjust keys above */
618 res = gistentryinsert(r, stk, newtup, giststate);
620 /* in stack, set info to point to new tuple */
621 stk->gs_blk = ItemPointerGetBlockNumber(&(res->pointerData));
622 stk->gs_child = ItemPointerGetOffsetNumber(&(res->pointerData));
628 if (centry.pred != datum)
637 * gistSplit -- split a page in the tree.
640 static InsertIndexResult
641 gistSplit(Relation r,
644 IndexTuple itup, /* contains compressed entry */
645 GISTSTATE *giststate)
659 OffsetNumber leftoff,
663 BlockNumber bufblock;
664 GISTPageOpaque opaque;
666 InsertIndexResult res;
676 isnull = (char *) palloc(r->rd_rel->relnatts);
677 for (blank = 0; blank < r->rd_rel->relnatts; blank++)
679 p = (Page) BufferGetPage(buffer);
680 opaque = (GISTPageOpaque) PageGetSpecialPointer(p);
684 * The root of the tree is the first block in the relation. If we're
685 * about to split the root, we need to do some hocus-pocus to enforce
689 if (BufferGetBlockNumber(buffer) == GISTP_ROOT)
691 leftbuf = ReadBuffer(r, P_NEW);
692 GISTInitBuffer(leftbuf, opaque->flags);
693 lbknum = BufferGetBlockNumber(leftbuf);
694 left = (Page) BufferGetPage(leftbuf);
699 IncrBufferRefCount(buffer);
700 lbknum = BufferGetBlockNumber(buffer);
701 left = (Page) PageGetTempPage(p, sizeof(GISTPageOpaqueData));
704 rightbuf = ReadBuffer(r, P_NEW);
705 GISTInitBuffer(rightbuf, opaque->flags);
706 rbknum = BufferGetBlockNumber(rightbuf);
707 right = (Page) BufferGetPage(rightbuf);
709 /* generate the item array */
710 maxoff = PageGetMaxOffsetNumber(p);
711 entryvec = (bytea *) palloc(VARHDRSZ + (maxoff + 2) * sizeof(GISTENTRY));
712 decompvec = (bool *) palloc(VARHDRSZ + (maxoff + 2) * sizeof(bool));
713 for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
715 item_1 = (IndexTuple) PageGetItem(p, PageGetItemId(p, i));
716 gistdentryinit(giststate, &((GISTENTRY *) VARDATA(entryvec))[i],
717 (((char *) item_1) + sizeof(IndexTupleData)),
719 IndexTupleSize(item_1) - sizeof(IndexTupleData), FALSE);
720 if ((char *) (((GISTENTRY *) VARDATA(entryvec))[i].pred)
721 == (((char *) item_1) + sizeof(IndexTupleData)))
722 decompvec[i] = FALSE;
727 /* add the new datum as the last entry */
728 gistdentryinit(giststate, &(((GISTENTRY *) VARDATA(entryvec))[maxoff + 1]),
729 (((char *) itup) + sizeof(IndexTupleData)),
730 (Relation) NULL, (Page) NULL,
731 (OffsetNumber) 0, tmpentry.bytes, FALSE);
732 if ((char *) (((GISTENTRY *) VARDATA(entryvec))[maxoff + 1]).pred !=
733 (((char *) itup) + sizeof(IndexTupleData)))
734 decompvec[maxoff + 1] = TRUE;
736 decompvec[maxoff + 1] = FALSE;
738 VARSIZE(entryvec) = (maxoff + 2) * sizeof(GISTENTRY) + VARHDRSZ;
740 /* now let the user-defined picksplit function set up the split vector */
741 (*fmgr_faddr(&giststate->picksplitFn)) (entryvec, &v);
743 /* compress ldatum and rdatum */
744 gistcentryinit(giststate, &tmpentry, v.spl_ldatum, (Relation) NULL,
745 (Page) NULL, (OffsetNumber) 0,
746 ((GISTENTRY *) VARDATA(entryvec))[i].bytes, FALSE);
747 if (v.spl_ldatum != tmpentry.pred)
749 v.spl_ldatum = tmpentry.pred;
751 gistcentryinit(giststate, &tmpentry, v.spl_rdatum, (Relation) NULL,
752 (Page) NULL, (OffsetNumber) 0,
753 ((GISTENTRY *) VARDATA(entryvec))[i].bytes, FALSE);
754 if (v.spl_rdatum != tmpentry.pred)
756 v.spl_rdatum = tmpentry.pred;
758 /* clean up the entry vector: its preds need to be deleted, too */
759 for (i = FirstOffsetNumber; i <= maxoff + 1; i = OffsetNumberNext(i))
761 pfree(((GISTENTRY *) VARDATA(entryvec))[i].pred);
765 leftoff = rightoff = FirstOffsetNumber;
766 maxoff = PageGetMaxOffsetNumber(p);
767 for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
769 itemid = PageGetItemId(p, i);
770 item = (IndexTuple) PageGetItem(p, itemid);
772 if (i == *(v.spl_left))
774 gistPageAddItem(giststate, r, left, (Item) item,
775 IndexTupleSize(item),
776 leftoff, LP_USED, &tmpdentry, &newtup);
777 leftoff = OffsetNumberNext(leftoff);
778 v.spl_left++; /* advance in left split vector */
780 if (tmpdentry.pred != (((char *) item) + sizeof(IndexTupleData)))
781 pfree(tmpdentry.pred);
782 if ((IndexTuple) item != newtup)
787 gistPageAddItem(giststate, r, right, (Item) item,
788 IndexTupleSize(item),
789 rightoff, LP_USED, &tmpdentry, &newtup);
790 rightoff = OffsetNumberNext(rightoff);
791 v.spl_right++; /* advance in right split vector */
793 if (tmpdentry.pred != (((char *) item) + sizeof(IndexTupleData)))
794 pfree(tmpdentry.pred);
800 /* build an InsertIndexResult for this insertion */
801 res = (InsertIndexResult) palloc(sizeof(InsertIndexResultData));
803 /* now insert the new index tuple */
804 if (*(v.spl_left) != FirstOffsetNumber)
806 gistPageAddItem(giststate, r, left, (Item) itup,
807 IndexTupleSize(itup),
808 leftoff, LP_USED, &tmpdentry, &newtup);
809 leftoff = OffsetNumberNext(leftoff);
810 ItemPointerSet(&(res->pointerData), lbknum, leftoff);
812 if (tmpdentry.pred != (((char *) itup) + sizeof(IndexTupleData)))
813 pfree(tmpdentry.pred);
819 gistPageAddItem(giststate, r, right, (Item) itup,
820 IndexTupleSize(itup),
821 rightoff, LP_USED, &tmpdentry, &newtup);
822 rightoff = OffsetNumberNext(rightoff);
823 ItemPointerSet(&(res->pointerData), rbknum, rightoff);
825 if (tmpdentry.pred != (((char *) itup) + sizeof(IndexTupleData)))
826 pfree(tmpdentry.pred);
831 if ((bufblock = BufferGetBlockNumber(buffer)) != GISTP_ROOT)
832 PageRestoreTempPage(left, p);
833 WriteBuffer(leftbuf);
834 WriteBuffer(rightbuf);
837 * Okay, the page is split. We have three things left to do:
839 * 1) Adjust any active scans on this index to cope with changes we
840 * introduced in its structure by splitting this page.
842 * 2) "Tighten" the bounding box of the pointer to the left page in the
843 * parent node in the tree, if any. Since we moved a bunch of stuff
844 * off the left page, we expect it to get smaller. This happens in
845 * the internal insertion routine.
847 * 3) Insert a pointer to the right page in the parent. This may cause
848 * the parent to split. If it does, we need to repeat steps one and
849 * two for each split node in the tree.
852 /* adjust active scans */
853 gistadjscans(r, GISTOP_SPLIT, bufblock, FirstOffsetNumber);
857 ltup = (IndexTuple) index_formtuple(tupDesc,
858 (Datum *) &(v.spl_ldatum), isnull);
859 rtup = (IndexTuple) index_formtuple(tupDesc,
860 (Datum *) &(v.spl_rdatum), isnull);
863 /* set pointers to new child pages in the internal index tuples */
864 ItemPointerSet(&(ltup->t_tid), lbknum, 1);
865 ItemPointerSet(&(rtup->t_tid), rbknum, 1);
867 gistintinsert(r, stack, ltup, rtup, giststate);
876 ** After a split, we need to overwrite the old entry's key in the parent,
877 ** and install install an entry for the new key into the parent.
880 gistintinsert(Relation r,
882 IndexTuple ltup, /* new version of entry for old page */
883 IndexTuple rtup, /* entry for new page */
884 GISTSTATE *giststate)
886 ItemPointerData ltid;
888 if (stk == (GISTSTACK *) NULL)
890 gistnewroot(giststate, r, ltup, rtup);
894 /* remove old left pointer, insert the 2 new entries */
895 ItemPointerSet(<id, stk->gs_blk, stk->gs_child);
896 gistdelete(r, (ItemPointer) <id);
897 gistentryinserttwo(r, stk, ltup, rtup, giststate);
902 ** Insert two entries onto one page, handling a split for either one!
905 gistentryinserttwo(Relation r, GISTSTACK *stk, IndexTuple ltup,
906 IndexTuple rtup, GISTSTATE *giststate)
910 InsertIndexResult res;
914 b = ReadBuffer(r, stk->gs_blk);
915 p = BufferGetPage(b);
917 if (gistnospace(p, ltup))
919 res = gistSplit(r, b, stk->gs_parent, ltup, giststate);
920 WriteBuffer(b); /* don't forget to release buffer! -
923 gistdoinsert(r, rtup, giststate);
927 gistPageAddItem(giststate, r, p, (Item) ltup,
928 IndexTupleSize(ltup), InvalidOffsetNumber,
929 LP_USED, &tmpentry, &newtup);
931 gistAdjustKeys(r, stk->gs_parent, stk->gs_blk, tmpentry.pred,
932 tmpentry.bytes, giststate);
934 if (tmpentry.pred != (((char *) ltup) + sizeof(IndexTupleData)))
935 pfree(tmpentry.pred);
938 gistentryinsert(r, stk, rtup, giststate);
944 ** Insert an entry onto a page
946 static InsertIndexResult
947 gistentryinsert(Relation r, GISTSTACK *stk, IndexTuple tup,
948 GISTSTATE *giststate)
952 InsertIndexResult res;
957 b = ReadBuffer(r, stk->gs_blk);
958 p = BufferGetPage(b);
960 if (gistnospace(p, tup))
962 res = gistSplit(r, b, stk->gs_parent, tup, giststate);
963 WriteBuffer(b); /* don't forget to release buffer! -
969 res = (InsertIndexResult) palloc(sizeof(InsertIndexResultData));
970 off = gistPageAddItem(giststate, r, p, (Item) tup, IndexTupleSize(tup),
971 InvalidOffsetNumber, LP_USED, &tmpentry, &newtup);
973 ItemPointerSet(&(res->pointerData), stk->gs_blk, off);
974 gistAdjustKeys(r, stk->gs_parent, stk->gs_blk, tmpentry.pred,
975 tmpentry.bytes, giststate);
977 if (tmpentry.pred != (((char *) tup) + sizeof(IndexTupleData)))
978 pfree(tmpentry.pred);
987 gistnewroot(GISTSTATE *giststate, Relation r, IndexTuple lt, IndexTuple rt)
994 b = ReadBuffer(r, GISTP_ROOT);
995 GISTInitBuffer(b, 0);
996 p = BufferGetPage(b);
997 gistPageAddItem(giststate, r, p, (Item) lt, IndexTupleSize(lt),
999 LP_USED, &tmpentry, &newtup);
1001 if (tmpentry.pred != (((char *) lt) + sizeof(IndexTupleData)))
1002 pfree(tmpentry.pred);
1005 gistPageAddItem(giststate, r, p, (Item) rt, IndexTupleSize(rt),
1006 OffsetNumberNext(FirstOffsetNumber), LP_USED,
1007 &tmpentry, &newtup);
1009 if (tmpentry.pred != (((char *) rt) + sizeof(IndexTupleData)))
1010 pfree(tmpentry.pred);
1017 GISTInitBuffer(Buffer b, uint32 f)
1019 GISTPageOpaque opaque;
1023 pageSize = BufferGetPageSize(b);
1025 page = BufferGetPage(b);
1026 MemSet(page, 0, (int) pageSize);
1027 PageInit(page, pageSize, sizeof(GISTPageOpaqueData));
1029 opaque = (GISTPageOpaque) PageGetSpecialPointer(page);
1035 ** find entry with lowest penalty
1038 gistchoose(Relation r, Page p, IndexTuple it, /* it has compressed entry */
1039 GISTSTATE *giststate)
1041 OffsetNumber maxoff;
1053 idsize = IndexTupleSize(it) - sizeof(IndexTupleData);
1054 id = ((char *) it) + sizeof(IndexTupleData);
1055 maxoff = PageGetMaxOffsetNumber(p);
1059 gistdentryinit(giststate, &identry, id, (Relation) NULL, (Page) NULL,
1060 (OffsetNumber) 0, idsize, FALSE);
1062 for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
1064 datum = (char *) PageGetItem(p, PageGetItemId(p, i));
1065 size = IndexTupleSize(datum) - sizeof(IndexTupleData);
1066 datum += sizeof(IndexTupleData);
1067 gistdentryinit(giststate, &entry, datum, r, p, i, size, FALSE);
1068 (*fmgr_faddr(&giststate->penaltyFn)) (&entry, &identry, &usize);
1069 if (which_grow < 0 || usize < which_grow)
1073 if (which_grow == 0)
1076 if (entry.pred != datum)
1079 if (identry.pred != id)
1080 pfree(identry.pred);
1086 gistnospace(Page p, IndexTuple it)
1088 return PageGetFreeSpace(p) < IndexTupleSize(it);
1092 gistfreestack(GISTSTACK *s)
1096 while (s != (GISTSTACK *) NULL)
1106 ** remove an entry from a page
1109 gistdelete(Relation r, ItemPointer tid)
1112 OffsetNumber offnum;
1116 /* must write-lock on delete */
1117 RelationSetLockForWrite(r);
1119 blkno = ItemPointerGetBlockNumber(tid);
1120 offnum = ItemPointerGetOffsetNumber(tid);
1122 /* adjust any scans that will be affected by this deletion */
1123 gistadjscans(r, GISTOP_DEL, blkno, offnum);
1125 /* delete the index tuple */
1126 buf = ReadBuffer(r, blkno);
1127 page = BufferGetPage(buf);
1129 PageIndexTupleDelete(page, offnum);
1133 /* XXX -- two-phase locking, don't release the write lock */
1137 initGISTstate(GISTSTATE *giststate, Relation index)
1139 RegProcedure consistent_proc,
1143 RegProcedure penalty_proc,
1147 Form_pg_index itupform;
1149 consistent_proc = index_getprocid(index, 1, GIST_CONSISTENT_PROC);
1150 union_proc = index_getprocid(index, 1, GIST_UNION_PROC);
1151 compress_proc = index_getprocid(index, 1, GIST_COMPRESS_PROC);
1152 decompress_proc = index_getprocid(index, 1, GIST_DECOMPRESS_PROC);
1153 penalty_proc = index_getprocid(index, 1, GIST_PENALTY_PROC);
1154 picksplit_proc = index_getprocid(index, 1, GIST_PICKSPLIT_PROC);
1155 equal_proc = index_getprocid(index, 1, GIST_EQUAL_PROC);
1156 fmgr_info(consistent_proc, &giststate->consistentFn);
1157 fmgr_info(union_proc, &giststate->unionFn);
1158 fmgr_info(compress_proc, &giststate->compressFn);
1159 fmgr_info(decompress_proc, &giststate->decompressFn);
1160 fmgr_info(penalty_proc, &giststate->penaltyFn);
1161 fmgr_info(picksplit_proc, &giststate->picksplitFn);
1162 fmgr_info(equal_proc, &giststate->equalFn);
1164 /* see if key type is different from type of attribute being indexed */
1165 htup = SearchSysCacheTuple(INDEXRELID,
1166 ObjectIdGetDatum(RelationGetRelid(index)),
1168 itupform = (Form_pg_index) GETSTRUCT(htup);
1169 if (!HeapTupleIsValid(htup))
1170 elog(ERROR, "initGISTstate: index %d not found",
1171 RelationGetRelid(index));
1172 giststate->haskeytype = itupform->indhaskeytype;
1173 if (giststate->haskeytype)
1175 /* key type is different -- is it byval? */
1176 htup = SearchSysCacheTuple(ATTNUM,
1177 ObjectIdGetDatum(itupform->indexrelid),
1178 UInt16GetDatum(FirstOffsetNumber),
1180 if (!HeapTupleIsValid(htup))
1182 elog(ERROR, "initGISTstate: no attribute tuple %d %d",
1183 itupform->indexrelid, FirstOffsetNumber);
1186 giststate->keytypbyval = (((Form_pg_attribute) htup)->attbyval);
1189 giststate->keytypbyval = FALSE;
1195 ** Given an IndexTuple to be inserted on a page, this routine replaces
1196 ** the key with another key, which may involve generating a new IndexTuple
1197 ** if the sizes don't match
1200 gist_tuple_replacekey(Relation r, GISTENTRY entry, IndexTuple t)
1202 char *datum = (((char *) t) + sizeof(IndexTupleData));
1204 /* if new entry fits in index tuple, copy it in */
1205 if (entry.bytes < IndexTupleSize(t) - sizeof(IndexTupleData))
1207 memcpy(datum, entry.pred, entry.bytes);
1208 /* clear out old size */
1209 t->t_info &= 0xe000;
1210 /* or in new size */
1211 t->t_info |= MAXALIGN(entry.bytes + sizeof(IndexTupleData));
1217 /* generate a new index tuple for the compressed entry */
1218 TupleDesc tupDesc = r->rd_att;
1223 isnull = (char *) palloc(r->rd_rel->relnatts);
1224 for (blank = 0; blank < r->rd_rel->relnatts; blank++)
1225 isnull[blank] = ' ';
1226 newtup = (IndexTuple) index_formtuple(tupDesc,
1227 (Datum *) &(entry.pred),
1229 newtup->t_tid = t->t_tid;
1237 ** initialize a GiST entry with a decompressed version of pred
1240 gistdentryinit(GISTSTATE *giststate, GISTENTRY *e, char *pr, Relation r,
1241 Page pg, OffsetNumber o, int b, bool l)
1245 gistentryinit(*e, pr, r, pg, o, b, l);
1246 if (giststate->haskeytype)
1248 dep = (GISTENTRY *) ((*fmgr_faddr(&giststate->decompressFn)) (e));
1249 gistentryinit(*e, dep->pred, dep->rel, dep->page, dep->offset, dep->bytes,
1258 ** initialize a GiST entry with a compressed version of pred
1261 gistcentryinit(GISTSTATE *giststate, GISTENTRY *e, char *pr, Relation r,
1262 Page pg, OffsetNumber o, int b, bool l)
1266 gistentryinit(*e, pr, r, pg, o, b, l);
1267 if (giststate->haskeytype)
1269 cep = (GISTENTRY *) ((*fmgr_faddr(&giststate->compressFn)) (e));
1270 gistentryinit(*e, cep->pred, cep->rel, cep->page, cep->offset, cep->bytes,
1282 ** sloppy debugging support routine, requires recompilation with appropriate
1283 ** "out" method for the index keys. Could be fixed to find that info
1284 ** in the catalogs...
1287 _gistdump(Relation r)
1291 OffsetNumber offnum,
1294 BlockNumber nblocks;
1297 BlockNumber itblkno;
1298 OffsetNumber itoffno;
1302 nblocks = RelationGetNumberOfBlocks(r);
1303 for (blkno = 0; blkno < nblocks; blkno++)
1305 buf = ReadBuffer(r, blkno);
1306 page = BufferGetPage(buf);
1307 po = (GISTPageOpaque) PageGetSpecialPointer(page);
1308 maxoff = PageGetMaxOffsetNumber(page);
1309 printf("Page %d maxoff %d <%s>\n", blkno, maxoff,
1310 (po->flags & F_LEAF ? "LEAF" : "INTERNAL"));
1312 if (PageIsEmpty(page))
1318 for (offnum = FirstOffsetNumber;
1320 offnum = OffsetNumberNext(offnum))
1322 itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, offnum));
1323 itblkno = ItemPointerGetBlockNumber(&(itup->t_tid));
1324 itoffno = ItemPointerGetOffsetNumber(&(itup->t_tid));
1325 datum = ((char *) itup);
1326 datum += sizeof(IndexTupleData);
1327 /* get out function for type of key, and out it! */
1328 itkey = (char *) int_range_out((INTRANGE *) datum);
1329 /* itkey = " unable to print"; */
1330 printf("\t[%d] size %d heap <%d,%d> key:%s\n",
1331 offnum, IndexTupleSize(itup), itblkno, itoffno, itkey);
1341 text_range_out(TXTRANGE *r)
1349 result = (char *) palloc(NAMEDATALEN + VARSIZE(TRLOWER(r)) + VARSIZE(TRUPPER(r))
1352 lower = (char *) palloc(VARSIZE(TRLOWER(r)) + 1 - VARHDRSZ);
1353 memcpy(lower, VARDATA(TRLOWER(r)), VARSIZE(TRLOWER(r)) - VARHDRSZ);
1354 lower[VARSIZE(TRLOWER(r)) - VARHDRSZ] = '\0';
1355 upper = (char *) palloc(VARSIZE(TRUPPER(r)) + 1 - VARHDRSZ);
1356 memcpy(upper, VARDATA(TRUPPER(r)), VARSIZE(TRUPPER(r)) - VARHDRSZ);
1357 upper[VARSIZE(TRUPPER(r)) - VARHDRSZ] = '\0';
1359 sprintf(result, "[%s,%s): %d", lower, upper, r->flag);
1368 int_range_out(INTRANGE *r)
1374 result = (char *) palloc(80);
1375 sprintf(result, "[%d,%d): %d", r->lower, r->upper, r->flag);
1380 #endif /* defined GISTDEBUG */