1 /*-------------------------------------------------------------------------
4 * interface routines for the postgres GiST index access method.
7 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/access/gist/gist.c,v 1.94 2002/05/28 15:22:33 tgl Exp $
13 *-------------------------------------------------------------------------
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 "miscadmin.h"
25 #undef GIST_PAGEADDITEM
27 #define ATTSIZE( datum, TupDesc, i, isnull ) \
30 att_addlength(0, (TupDesc)->attrs[(i)-1]->attlen, (datum)) \
37 /* group flags ( in gistSplit ) */
38 #define LEFT_ADDED 0x01
39 #define RIGHT_ADDED 0x02
40 #define BOTH_ADDED ( LEFT_ADDED | RIGHT_ADDED )
43 * This defines only for shorter code, used in gistgetadjusted
44 * and gistadjsubkey only
46 #define FILLITEM(evp, isnullkey, okey, okeyb, rkey, rkeyb) do { \
48 gistentryinit((evp), rkey, r, (Page) NULL , \
49 (OffsetNumber) 0, rkeyb, FALSE); \
51 gistentryinit((evp), okey, r, (Page) NULL, \
52 (OffsetNumber) 0, okeyb, FALSE); \
56 #define FILLEV(isnull1, key1, key1b, isnull2, key2, key2b) do { \
57 FILLITEM(*ev0p, isnull1, key1, key1b, key2, key2b); \
58 FILLITEM(*ev1p, isnull2, key2, key2b, key1, key1b); \
61 /* Working state for gistbuild and its callback */
70 /* non-export function prototypes */
71 static void gistbuildCallback(Relation index,
77 static void gistdoinsert(Relation r,
79 InsertIndexResult *res,
80 GISTSTATE *GISTstate);
81 static int gistlayerinsert(Relation r, BlockNumber blkno,
84 InsertIndexResult *res,
85 GISTSTATE *giststate);
86 static OffsetNumber gistwritebuffer(Relation r,
91 static int gistnospace(Page page,
92 IndexTuple *itvec, int len);
93 static IndexTuple *gistreadbuffer(Buffer buffer, int *len);
94 static IndexTuple *gistjoinvector(
95 IndexTuple *itvec, int *len,
96 IndexTuple *additvec, int addlen);
97 static IndexTuple gistunion(Relation r, IndexTuple *itvec,
98 int len, GISTSTATE *giststate);
100 static IndexTuple gistgetadjusted(Relation r,
103 GISTSTATE *giststate);
104 static int gistfindgroup(GISTSTATE *giststate,
105 GISTENTRY *valvec, GIST_SPLITVEC *spl);
106 static void gistadjsubkey(Relation r,
107 IndexTuple *itup, int *len,
109 GISTSTATE *giststate);
110 static IndexTuple gistFormTuple(GISTSTATE *giststate,
111 Relation r, Datum attdata[], int datumsize[], bool isnull[]);
112 static IndexTuple *gistSplit(Relation r,
116 GISTSTATE *giststate,
117 InsertIndexResult *res);
118 static void gistnewroot(Relation r,
119 IndexTuple *itup, int len);
120 static void GISTInitBuffer(Buffer b, uint32 f);
121 static OffsetNumber gistchoose(Relation r, Page p,
123 GISTSTATE *giststate);
124 static void gistdelete(Relation r, ItemPointer tid);
126 #ifdef GIST_PAGEADDITEM
127 static IndexTuple gist_tuple_replacekey(Relation r,
128 GISTENTRY entry, IndexTuple t);
130 static void gistcentryinit(GISTSTATE *giststate, int nkey,
131 GISTENTRY *e, Datum k,
133 OffsetNumber o, int b, bool l, bool isNull);
134 static void gistDeCompressAtt(GISTSTATE *giststate, Relation r,
135 IndexTuple tuple, Page p, OffsetNumber o,
136 GISTENTRY attdata[], bool decompvec[], bool isnull[]);
137 static void gistFreeAtt(Relation r, GISTENTRY attdata[], bool decompvec[]);
138 static void gistpenalty(GISTSTATE *giststate, int attno,
139 GISTENTRY *key1, bool isNull1,
140 GISTENTRY *key2, bool isNull2,
146 static void gist_dumptree(Relation r, int level, BlockNumber blk, OffsetNumber coff);
150 * routine to build an index. Basically calls insert over and over
153 gistbuild(PG_FUNCTION_ARGS)
155 Relation heap = (Relation) PG_GETARG_POINTER(0);
156 Relation index = (Relation) PG_GETARG_POINTER(1);
157 IndexInfo *indexInfo = (IndexInfo *) PG_GETARG_POINTER(2);
159 GISTBuildState buildstate;
162 /* no locking is needed */
164 initGISTstate(&buildstate.giststate, index);
167 * We expect to be called exactly once for any index relation. If
168 * that's not the case, big trouble's what we have.
170 if (RelationGetNumberOfBlocks(index) != 0)
171 elog(ERROR, "%s already contains data",
172 RelationGetRelationName(index));
174 /* initialize the root page */
175 buffer = ReadBuffer(index, P_NEW);
176 GISTInitBuffer(buffer, F_LEAF);
179 /* build the index */
180 buildstate.numindexattrs = indexInfo->ii_NumIndexAttrs;
181 buildstate.indtuples = 0;
183 /* do the heap scan */
184 reltuples = IndexBuildHeapScan(heap, index, indexInfo,
185 gistbuildCallback, (void *) &buildstate);
187 /* okay, all heap tuples are indexed */
190 * Since we just counted the tuples in the heap, we update its stats
191 * in pg_class to guarantee that the planner takes advantage of the
192 * index we just created. But, only update statistics during normal
193 * index definitions, not for indices on system catalogs created
194 * during bootstrap processing. We must close the relations before
195 * updating statistics to guarantee that the relcache entries are
196 * flushed when we increment the command counter in UpdateStats(). But
197 * we do not release any locks on the relations; those will be held
198 * until end of transaction.
200 if (IsNormalProcessingMode())
202 Oid hrelid = RelationGetRelid(heap);
203 Oid irelid = RelationGetRelid(index);
205 heap_close(heap, NoLock);
207 UpdateStats(hrelid, reltuples);
208 UpdateStats(irelid, buildstate.indtuples);
211 freeGISTstate(&buildstate.giststate);
213 gist_dumptree(index, 0, GISTP_ROOT, 0);
220 * Per-tuple callback from IndexBuildHeapScan
223 gistbuildCallback(Relation index,
230 GISTBuildState *buildstate = (GISTBuildState *) state;
232 bool compvec[INDEX_MAX_KEYS];
236 /* GiST cannot index tuples with leading NULLs */
240 /* immediately compress keys to normalize */
241 for (i = 0; i < buildstate->numindexattrs; i++)
245 attdata[i] = (Datum) 0;
250 gistcentryinit(&buildstate->giststate, i, &tmpcentry, attdata[i],
251 (Relation) NULL, (Page) NULL, (OffsetNumber) 0,
252 -1 /* size is currently bogus */ , TRUE, FALSE);
253 if (attdata[i] != tmpcentry.key &&
254 !(isAttByVal(&buildstate->giststate, i)))
258 attdata[i] = tmpcentry.key;
262 /* form an index tuple and point it at the heap tuple */
263 itup = index_formtuple(buildstate->giststate.tupdesc, attdata, nulls);
264 itup->t_tid = htup->t_self;
267 * Since we already have the index relation locked, we call
268 * gistdoinsert directly. Normal access method calls dispatch through
269 * gistinsert, which locks the relation for write. This is the right
270 * thing to do if you're inserting single tups, but not when you're
271 * initializing the whole index at once.
273 gistdoinsert(index, itup, NULL, &buildstate->giststate);
275 buildstate->indtuples += 1;
277 for (i = 0; i < buildstate->numindexattrs; i++)
279 pfree(DatumGetPointer(attdata[i]));
285 * gistinsert -- wrapper for GiST tuple insertion.
287 * This is the public interface routine for tuple insertion in GiSTs.
288 * It doesn't do any work; just locks the relation and passes the buck.
291 gistinsert(PG_FUNCTION_ARGS)
293 Relation r = (Relation) PG_GETARG_POINTER(0);
294 Datum *datum = (Datum *) PG_GETARG_POINTER(1);
295 char *nulls = (char *) PG_GETARG_POINTER(2);
296 ItemPointer ht_ctid = (ItemPointer) PG_GETARG_POINTER(3);
298 Relation heapRel = (Relation) PG_GETARG_POINTER(4);
299 bool checkUnique = PG_GETARG_BOOL(5);
301 InsertIndexResult res;
306 bool compvec[INDEX_MAX_KEYS];
309 * Since GIST is not marked "amconcurrent" in pg_am, caller should
310 * have acquired exclusive lock on index relation. We need no locking
314 /* GiST cannot index tuples with leading NULLs */
318 PG_RETURN_POINTER(res);
321 initGISTstate(&giststate, r);
323 /* immediately compress keys to normalize */
324 for (i = 0; i < r->rd_att->natts; i++)
328 datum[i] = (Datum) 0;
333 gistcentryinit(&giststate, i, &tmpentry, datum[i],
334 (Relation) NULL, (Page) NULL, (OffsetNumber) 0,
335 -1 /* size is currently bogus */ , TRUE, FALSE);
336 if (datum[i] != tmpentry.key && !(isAttByVal(&giststate, i)))
340 datum[i] = tmpentry.key;
343 itup = index_formtuple(giststate.tupdesc, datum, nulls);
344 itup->t_tid = *ht_ctid;
346 res = (InsertIndexResult) palloc(sizeof(InsertIndexResultData));
347 gistdoinsert(r, itup, &res, &giststate);
349 for (i = 0; i < r->rd_att->natts; i++)
350 if (compvec[i] == TRUE)
351 pfree(DatumGetPointer(datum[i]));
353 freeGISTstate(&giststate);
355 PG_RETURN_POINTER(res);
358 #ifdef GIST_PAGEADDITEM
360 * Take a compressed entry, and install it on a page. Since we now know
361 * where the entry will live, we decompress it and recompress it using
362 * that knowledge (some compression routines may want to fish around
363 * on the page, for example, or do something special for leaf nodes.)
366 gistPageAddItem(GISTSTATE *giststate,
371 OffsetNumber offsetNumber,
377 IndexTuple itup = (IndexTuple) item;
383 * recompress the item given that we now know the exact page and
384 * offset for insertion
386 datum = index_getattr(itup, 1, r->rd_att, &IsNull);
387 gistdentryinit(giststate, 0, dentry, datum,
388 (Relation) 0, (Page) 0,
389 (OffsetNumber) InvalidOffsetNumber,
390 ATTSIZE(datum, r, 1, IsNull),
392 gistcentryinit(giststate, 0, &tmpcentry, dentry->key, r, page,
393 offsetNumber, dentry->bytes, FALSE);
394 *newtup = gist_tuple_replacekey(r, tmpcentry, itup);
395 retval = PageAddItem(page, (Item) *newtup, IndexTupleSize(*newtup),
396 offsetNumber, flags);
397 if (retval == InvalidOffsetNumber)
398 elog(ERROR, "gist: failed to add index item to %s",
399 RelationGetRelationName(r));
401 if (DatumGetPointer(tmpcentry.key) != NULL &&
402 tmpcentry.key != dentry->key &&
403 tmpcentry.key != datum)
404 pfree(DatumGetPointer(tmpcentry.key));
410 gistdoinsert(Relation r,
412 InsertIndexResult *res,
413 GISTSTATE *giststate)
420 instup = (IndexTuple *) palloc(sizeof(IndexTuple));
421 instup[0] = (IndexTuple) palloc(IndexTupleSize(itup));
422 memcpy(instup[0], itup, IndexTupleSize(itup));
424 ret = gistlayerinsert(r, GISTP_ROOT, &instup, &len, res, giststate);
426 gistnewroot(r, instup, len);
428 for (i = 0; i < len; i++)
434 gistlayerinsert(Relation r, BlockNumber blkno,
435 IndexTuple **itup, /* in - out, has compressed entry */
436 int *len, /* in - out */
437 InsertIndexResult *res, /* out */
438 GISTSTATE *giststate)
444 GISTPageOpaque opaque;
446 buffer = ReadBuffer(r, blkno);
447 page = (Page) BufferGetPage(buffer);
448 opaque = (GISTPageOpaque) PageGetSpecialPointer(page);
450 if (!(opaque->flags & F_LEAF))
452 /* internal page, so we must walk on tree */
456 ItemPointerData oldtid;
459 child = gistchoose(r, page, *(*itup), giststate);
460 iid = PageGetItemId(page, child);
461 oldtup = (IndexTuple) PageGetItem(page, iid);
462 nblkno = ItemPointerGetBlockNumber(&(oldtup->t_tid));
465 * After this call: 1. if child page was splited, then itup
466 * contains keys for each page 2. if child page wasn't splited,
467 * then itup contains additional for adjustement of current key
469 ret = gistlayerinsert(r, nblkno, itup, len, res, giststate);
471 /* nothing inserted in child */
472 if (!(ret & INSERTED))
474 ReleaseBuffer(buffer);
478 /* child does not splited */
479 if (!(ret & SPLITED))
481 IndexTuple newtup = gistgetadjusted(r, oldtup, (*itup)[0], giststate);
485 /* not need to update key */
486 ReleaseBuffer(buffer);
490 pfree((*itup)[0]); /* !!! */
494 /* key is modified, so old version must be deleted */
495 ItemPointerSet(&oldtid, blkno, child);
496 gistdelete(r, &oldtid);
499 * if child was splitted, new key for child will be inserted
500 * in the end list of child, so we must say to any scans
501 * that page is changed beginning from 'child' offset
504 gistadjscans(r, GISTOP_SPLIT, blkno, child);
509 if (gistnospace(page, (*itup), *len))
511 /* no space for insertion */
518 itvec = gistreadbuffer(buffer, &tlen);
519 itvec = gistjoinvector(itvec, &tlen, (*itup), *len);
521 newitup = gistSplit(r, buffer, itvec, &tlen, giststate,
522 (opaque->flags & F_LEAF) ? res : NULL); /* res only for
523 * inserting in leaf */
524 ReleaseBuffer(buffer);
526 pfree((*itup)[oldlen - 1]);
527 while ((--oldlen) > 0);
531 *len = tlen; /* now tlen >= 2 */
539 off = (PageIsEmpty(page)) ?
542 OffsetNumberNext(PageGetMaxOffsetNumber(page));
543 l = gistwritebuffer(r, page, (*itup), *len, off);
547 * set res if insert into leaf page, in this case, len = 1 always
549 if (res && (opaque->flags & F_LEAF))
550 ItemPointerSet(&((*res)->pointerData), blkno, l);
553 { /* previos insert ret & SPLITED != 0 */
557 * child was splited, so we must form union for insertion in
560 IndexTuple newtup = gistunion(r, (*itup), *len, giststate);
562 ItemPointerSet(&(newtup->t_tid), blkno, 1);
564 for (i = 0; i < *len; i++)
575 * Write itup vector to page, has no control of free space
578 gistwritebuffer(Relation r, Page page, IndexTuple *itup,
579 int len, OffsetNumber off)
581 OffsetNumber l = InvalidOffsetNumber;
584 #ifdef GIST_PAGEADDITEM
589 for (i = 0; i < len; i++)
591 #ifdef GIST_PAGEADDITEM
592 l = gistPageAddItem(giststate, r, page,
593 (Item) itup[i], IndexTupleSize(itup[i]),
594 off, LP_USED, &tmpdentry, &newtup);
595 off = OffsetNumberNext(off);
596 if (DatumGetPointer(tmpdentry.key) != NULL &&
597 tmpdentry.key != index_getattr(itup[i], 1, r->rd_att, &IsNull))
598 pfree(DatumGetPointer(tmpdentry.key));
599 if (itup[i] != newtup)
602 l = PageAddItem(page, (Item) itup[i], IndexTupleSize(itup[i]),
604 if (l == InvalidOffsetNumber)
605 elog(ERROR, "gist: failed to add index item to %s",
606 RelationGetRelationName(r));
613 * Check space for itup vector on page
616 gistnospace(Page page, IndexTuple *itvec, int len)
618 unsigned int size = 0;
621 for (i = 0; i < len; i++)
622 size += IndexTupleSize(itvec[i]) + sizeof(ItemIdData);
624 return (PageGetFreeSpace(page) < size);
628 * Read buffer into itup vector
631 gistreadbuffer(Buffer buffer, int *len /* out */ )
636 Page p = (Page) BufferGetPage(buffer);
638 maxoff = PageGetMaxOffsetNumber(p);
640 itvec = palloc(sizeof(IndexTuple) * maxoff);
641 for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
642 itvec[i - 1] = (IndexTuple) PageGetItem(p, PageGetItemId(p, i));
648 * join two vectors into one
651 gistjoinvector(IndexTuple *itvec, int *len, IndexTuple *additvec, int addlen)
653 itvec = (IndexTuple *) repalloc((void *) itvec, sizeof(IndexTuple) * ((*len) + addlen));
654 memmove(&itvec[*len], additvec, sizeof(IndexTuple) * addlen);
660 * return union of itup vector
663 gistunion(Relation r, IndexTuple *itvec, int len, GISTSTATE *giststate)
665 Datum attr[INDEX_MAX_KEYS];
666 bool whatfree[INDEX_MAX_KEYS];
667 char isnull[INDEX_MAX_KEYS];
674 GISTENTRY centry[INDEX_MAX_KEYS];
680 needfree = (bool *) palloc(((len == 1) ? 2 : len) * sizeof(bool));
681 /* workaround for 64-bit: ensure GISTENTRY array is maxaligned */
682 storage = (char*)palloc( ((len == 1) ? 2 : len) * sizeof(GISTENTRY) + MAXALIGN(VARHDRSZ));
683 evec = (bytea *) (storage + MAXALIGN(VARHDRSZ) - VARHDRSZ);
685 for (j = 0; j < r->rd_att->natts; j++)
688 for (i = 0; i < len; i++)
690 datum = index_getattr(itvec[i], j + 1, giststate->tupdesc, &IsNull);
694 gistdentryinit(giststate, j,
695 &((GISTENTRY *) VARDATA(evec))[reallen],
697 (Relation) NULL, (Page) NULL, (OffsetNumber) NULL,
698 ATTSIZE(datum, giststate->tupdesc, j + 1, IsNull), FALSE, IsNull);
699 if ((!isAttByVal(giststate, j)) &&
700 ((GISTENTRY *) VARDATA(evec))[reallen].key != datum)
701 needfree[reallen] = TRUE;
703 needfree[reallen] = FALSE;
717 VARATT_SIZEP(evec) = 2 * sizeof(GISTENTRY) + VARHDRSZ;
718 gistentryinit(((GISTENTRY *) VARDATA(evec))[1],
719 ((GISTENTRY *) VARDATA(evec))[0].key, r, (Page) NULL,
720 (OffsetNumber) 0, ((GISTENTRY *) VARDATA(evec))[0].bytes, FALSE);
724 VARATT_SIZEP(evec) = reallen * sizeof(GISTENTRY) + VARHDRSZ;
725 datum = FunctionCall2(&giststate->unionFn[j],
726 PointerGetDatum(evec),
727 PointerGetDatum(&datumsize));
729 for (i = 0; i < reallen; i++)
731 pfree(DatumGetPointer(((GISTENTRY *) VARDATA(evec))[i].key));
733 gistcentryinit(giststate, j, ¢ry[j], datum,
734 (Relation) NULL, (Page) NULL, (OffsetNumber) NULL,
735 datumsize, FALSE, FALSE);
737 attr[j] = centry[j].key;
738 if (!isAttByVal(giststate, j))
741 if (centry[j].key != datum)
742 pfree(DatumGetPointer(datum));
749 pfree(storage); /* pfree(evec); */
752 newtup = (IndexTuple) index_formtuple(giststate->tupdesc, attr, isnull);
753 for (j = 0; j < r->rd_att->natts; j++)
755 pfree(DatumGetPointer(attr[j]));
762 * Forms union of oldtup and addtup, if union == oldtup then return NULL
765 gistgetadjusted(Relation r, IndexTuple oldtup, IndexTuple addtup, GISTSTATE *giststate)
772 char isnull[INDEX_MAX_KEYS],
773 whatfree[INDEX_MAX_KEYS];
774 Datum attr[INDEX_MAX_KEYS];
775 GISTENTRY centry[INDEX_MAX_KEYS],
776 oldatt[INDEX_MAX_KEYS],
777 addatt[INDEX_MAX_KEYS],
780 bool olddec[INDEX_MAX_KEYS],
781 adddec[INDEX_MAX_KEYS];
782 bool oldisnull[INDEX_MAX_KEYS],
783 addisnull[INDEX_MAX_KEYS];
784 IndexTuple newtup = NULL;
788 /* workaround for 64-bit: ensure GISTENTRY array is maxaligned */
789 storage = (char*) palloc( 2 * sizeof(GISTENTRY) + MAXALIGN(VARHDRSZ));
790 evec = (bytea *) (storage + MAXALIGN(VARHDRSZ) - VARHDRSZ);
791 VARATT_SIZEP(evec) = 2 * sizeof(GISTENTRY) + VARHDRSZ;
792 ev0p = &((GISTENTRY *) VARDATA(evec))[0];
793 ev1p = &((GISTENTRY *) VARDATA(evec))[1];
795 gistDeCompressAtt(giststate, r, oldtup, (Page) NULL,
796 (OffsetNumber) 0, oldatt, olddec, oldisnull);
798 gistDeCompressAtt(giststate, r, addtup, (Page) NULL,
799 (OffsetNumber) 0, addatt, adddec, addisnull);
802 for (j = 0; j < r->rd_att->natts; j++)
804 if (oldisnull[j] && addisnull[j])
813 oldisnull[j], oldatt[j].key, oldatt[j].bytes,
814 addisnull[j], addatt[j].key, addatt[j].bytes
817 datum = FunctionCall2(&giststate->unionFn[j],
818 PointerGetDatum(evec),
819 PointerGetDatum(&datumsize));
821 if (oldisnull[j] || addisnull[j])
828 FunctionCall3(&giststate->equalFn[j],
831 PointerGetDatum(&result));
838 pfree(DatumGetPointer(oldatt[j].key));
840 pfree(DatumGetPointer(addatt[j].key));
842 gistcentryinit(giststate, j, ¢ry[j], datum,
843 (Relation) NULL, (Page) NULL, (OffsetNumber) NULL,
844 datumsize, FALSE, FALSE);
847 attr[j] = centry[j].key;
848 if ((!isAttByVal(giststate, j)))
851 if (centry[j].key != datum)
852 pfree(DatumGetPointer(datum));
858 pfree(storage); /* pfree(evec); */
862 /* need to update key */
863 newtup = (IndexTuple) index_formtuple(giststate->tupdesc, attr, isnull);
864 newtup->t_tid = oldtup->t_tid;
867 for (j = 0; j < r->rd_att->natts; j++)
869 pfree(DatumGetPointer(attr[j]));
875 gistunionsubkey(Relation r, GISTSTATE *giststate, IndexTuple *itvec, GIST_SPLITVEC *spl)
885 OffsetNumber *entries;
893 for (lr = 0; lr <= 1; lr++)
897 attrsize = spl->spl_lattrsize;
898 attr = spl->spl_lattr;
899 len = spl->spl_nleft;
900 entries = spl->spl_left;
901 isnull = spl->spl_lisnull;
905 attrsize = spl->spl_rattrsize;
906 attr = spl->spl_rattr;
907 len = spl->spl_nright;
908 entries = spl->spl_right;
909 isnull = spl->spl_risnull;
912 needfree = (bool *) palloc(((len == 1) ? 2 : len) * sizeof(bool));
913 /* workaround for 64-bit: ensure GISTENTRY array is maxaligned */
914 storage = (char*)palloc( ((len == 1) ? 2 : len) * sizeof(GISTENTRY) + MAXALIGN(VARHDRSZ));
915 evec = (bytea *) (storage + MAXALIGN(VARHDRSZ) - VARHDRSZ);
917 for (j = 1; j < r->rd_att->natts; j++)
920 for (i = 0; i < len; i++)
922 if (spl->spl_idgrp[entries[i]])
924 datum = index_getattr(itvec[entries[i] - 1], j + 1,
925 giststate->tupdesc, &IsNull);
928 gistdentryinit(giststate, j,
929 &((GISTENTRY *) VARDATA(evec))[reallen],
931 (Relation) NULL, (Page) NULL,
933 ATTSIZE(datum, giststate->tupdesc, j + 1, IsNull), FALSE, IsNull);
934 if ((!isAttByVal(giststate, j)) &&
935 ((GISTENTRY *) VARDATA(evec))[reallen].key != datum)
936 needfree[reallen] = TRUE;
938 needfree[reallen] = FALSE;
951 * ((GISTENTRY *) VARDATA(evec))[0].bytes may be not
952 * defined, so form union with itself
956 VARATT_SIZEP(evec) = 2 * sizeof(GISTENTRY) + VARHDRSZ;
957 memcpy((void *) &((GISTENTRY *) VARDATA(evec))[1],
958 (void *) &((GISTENTRY *) VARDATA(evec))[0],
962 VARATT_SIZEP(evec) = reallen * sizeof(GISTENTRY) + VARHDRSZ;
963 datum = FunctionCall2(&giststate->unionFn[j],
964 PointerGetDatum(evec),
965 PointerGetDatum(&datumsize));
969 for (i = 0; i < reallen; i++)
971 pfree(DatumGetPointer(((GISTENTRY *) VARDATA(evec))[i].key));
974 attrsize[j] = datumsize;
976 pfree(storage); /* pfree(evec); */
982 * find group in vector with equial value
985 gistfindgroup(GISTSTATE *giststate, GISTENTRY *valvec, GIST_SPLITVEC *spl)
994 * first key is always not null (see gistinsert), so we may not check
998 for (i = 0; i < spl->spl_nleft; i++)
1000 if (spl->spl_idgrp[spl->spl_left[i]])
1003 /* find all equal value in right part */
1004 for (j = 0; j < spl->spl_nright; j++)
1006 if (spl->spl_idgrp[spl->spl_right[j]])
1008 FunctionCall3(&giststate->equalFn[0],
1009 valvec[spl->spl_left[i]].key,
1010 valvec[spl->spl_right[j]].key,
1011 PointerGetDatum(&result));
1014 spl->spl_idgrp[spl->spl_right[j]] = curid;
1018 /* find all other equal value in left part */
1021 /* add current val to list of equial values */
1022 spl->spl_idgrp[spl->spl_left[i]] = curid;
1024 for (j = i + 1; j < spl->spl_nleft; j++)
1026 if (spl->spl_idgrp[spl->spl_left[j]])
1028 FunctionCall3(&giststate->equalFn[0],
1029 valvec[spl->spl_left[i]].key,
1030 valvec[spl->spl_left[j]].key,
1031 PointerGetDatum(&result));
1034 spl->spl_idgrp[spl->spl_left[j]] = curid;
1038 spl->spl_ngrp[curid] = len + 1;
1047 * Insert equivalent tuples to left or right page
1048 * with minimize penalty
1051 gistadjsubkey(Relation r,
1052 IndexTuple *itup, /* contains compressed entry */
1055 GISTSTATE *giststate
1059 OffsetNumber *curwpos;
1060 bool decfree[INDEX_MAX_KEYS];
1062 identry[INDEX_MAX_KEYS],
1070 bool isnull[INDEX_MAX_KEYS];
1076 curlen = v->spl_nleft;
1077 curwpos = v->spl_left;
1078 for (i = 0; i < v->spl_nleft; i++)
1079 if (v->spl_idgrp[v->spl_left[i]] == 0)
1081 *curwpos = v->spl_left[i];
1086 v->spl_nleft = curlen;
1088 curlen = v->spl_nright;
1089 curwpos = v->spl_right;
1090 for (i = 0; i < v->spl_nright; i++)
1091 if (v->spl_idgrp[v->spl_right[i]] == 0)
1093 *curwpos = v->spl_right[i];
1098 v->spl_nright = curlen;
1100 /* workaround for 64-bit: ensure GISTENTRY array is maxaligned */
1101 storage = (char*)palloc( 2 * sizeof(GISTENTRY) + MAXALIGN(VARHDRSZ));
1102 evec = (bytea *) (storage + MAXALIGN(VARHDRSZ) - VARHDRSZ);
1103 VARATT_SIZEP(evec) = 2 * sizeof(GISTENTRY) + VARHDRSZ;
1104 ev0p = &((GISTENTRY *) VARDATA(evec))[0];
1105 ev1p = &((GISTENTRY *) VARDATA(evec))[1];
1107 /* add equivalent tuple */
1108 for (i = 0; i < *len; i++)
1110 if (v->spl_idgrp[i + 1] == 0) /* already inserted */
1112 gistDeCompressAtt(giststate, r, itup[i], (Page) NULL, (OffsetNumber) 0,
1113 identry, decfree, isnull);
1115 v->spl_ngrp[v->spl_idgrp[i + 1]]--;
1116 if (v->spl_ngrp[v->spl_idgrp[i + 1]] == 0 &&
1117 (v->spl_grpflag[v->spl_idgrp[i + 1]] & BOTH_ADDED) != BOTH_ADDED)
1120 /* force last in group */
1122 lpenalty = (v->spl_grpflag[v->spl_idgrp[i + 1]] & LEFT_ADDED) ? 2.0 : 0.0;
1127 for (j = 1; j < r->rd_att->natts; j++)
1129 gistentryinit(entry, v->spl_lattr[j], r, (Page) NULL,
1130 (OffsetNumber) 0, v->spl_lattrsize[j], FALSE);
1131 gistpenalty(giststate, j, &entry, v->spl_lisnull[j],
1132 &identry[j], isnull[j], &lpenalty);
1134 gistentryinit(entry, v->spl_rattr[j], r, (Page) NULL,
1135 (OffsetNumber) 0, v->spl_rattrsize[j], FALSE);
1136 gistpenalty(giststate, j, &entry, v->spl_risnull[j],
1137 &identry[j], isnull[j], &rpenalty);
1139 if (lpenalty != rpenalty)
1144 if (lpenalty < rpenalty)
1146 v->spl_grpflag[v->spl_idgrp[i + 1]] |= LEFT_ADDED;
1147 v->spl_left[v->spl_nleft] = i + 1;
1149 for (j = 1; j < r->rd_att->natts; j++)
1151 if (isnull[j] && v->spl_lisnull[j])
1153 v->spl_lattr[j] = (Datum) 0;
1154 v->spl_lattrsize[j] = 0;
1159 v->spl_lisnull[j], v->spl_lattr[j], v->spl_lattrsize[j],
1160 isnull[j], identry[j].key, identry[j].bytes
1163 datum = FunctionCall2(&giststate->unionFn[j],
1164 PointerGetDatum(evec),
1165 PointerGetDatum(&datumsize));
1167 if ((!isAttByVal(giststate, j)) && !v->spl_lisnull[j])
1168 pfree(DatumGetPointer(v->spl_lattr[j]));
1169 v->spl_lattr[j] = datum;
1170 v->spl_lattrsize[j] = datumsize;
1171 v->spl_lisnull[j] = false;
1177 v->spl_grpflag[v->spl_idgrp[i + 1]] |= RIGHT_ADDED;
1178 v->spl_right[v->spl_nright] = i + 1;
1180 for (j = 1; j < r->rd_att->natts; j++)
1182 if (isnull[j] && v->spl_risnull[j])
1184 v->spl_rattr[j] = (Datum) 0;
1185 v->spl_rattrsize[j] = 0;
1190 v->spl_risnull[j], v->spl_rattr[j], v->spl_rattrsize[j],
1191 isnull[j], identry[j].key, identry[j].bytes
1194 datum = FunctionCall2(&giststate->unionFn[j],
1195 PointerGetDatum(evec),
1196 PointerGetDatum(&datumsize));
1198 if ((!isAttByVal(giststate, j)) && !v->spl_risnull[j])
1199 pfree(DatumGetPointer(v->spl_rattr[j]));
1201 v->spl_rattr[j] = datum;
1202 v->spl_rattrsize[j] = datumsize;
1203 v->spl_risnull[j] = false;
1208 gistFreeAtt(r, identry, decfree);
1210 pfree(storage); /* pfree(evec); */
1214 * gistSplit -- split a page in the tree.
1217 gistSplit(Relation r,
1219 IndexTuple *itup, /* contains compressed entry */
1221 GISTSTATE *giststate,
1222 InsertIndexResult *res)
1229 IndexTuple *lvectup,
1234 GISTPageOpaque opaque;
1246 p = (Page) BufferGetPage(buffer);
1247 opaque = (GISTPageOpaque) PageGetSpecialPointer(p);
1250 * The root of the tree is the first block in the relation. If we're
1251 * about to split the root, we need to do some hocus-pocus to enforce
1255 if (BufferGetBlockNumber(buffer) == GISTP_ROOT)
1257 leftbuf = ReadBuffer(r, P_NEW);
1258 GISTInitBuffer(leftbuf, opaque->flags);
1259 lbknum = BufferGetBlockNumber(leftbuf);
1260 left = (Page) BufferGetPage(leftbuf);
1265 IncrBufferRefCount(buffer);
1266 lbknum = BufferGetBlockNumber(buffer);
1267 left = (Page) PageGetTempPage(p, sizeof(GISTPageOpaqueData));
1270 rightbuf = ReadBuffer(r, P_NEW);
1271 GISTInitBuffer(rightbuf, opaque->flags);
1272 rbknum = BufferGetBlockNumber(rightbuf);
1273 right = (Page) BufferGetPage(rightbuf);
1275 /* generate the item array */
1276 /* workaround for 64-bit: ensure GISTENTRY array is maxaligned */
1277 storage = palloc(MAXALIGN(VARHDRSZ) + (*len + 1) * sizeof(GISTENTRY));
1278 entryvec = (bytea *) (storage + MAXALIGN(VARHDRSZ) - VARHDRSZ);
1279 decompvec = (bool *) palloc( (*len + 1) * sizeof(bool));
1280 VARATT_SIZEP(entryvec) = (*len + 1) * sizeof(GISTENTRY) + VARHDRSZ;
1281 for (i = 1; i <= *len; i++)
1283 datum = index_getattr(itup[i - 1], 1, giststate->tupdesc, &IsNull);
1284 gistdentryinit(giststate, 0, &((GISTENTRY *) VARDATA(entryvec))[i],
1286 ATTSIZE(datum, giststate->tupdesc, 1, IsNull), FALSE, IsNull);
1287 if ((!isAttByVal(giststate, 0)) && ((GISTENTRY *) VARDATA(entryvec))[i].key != datum)
1288 decompvec[i] = TRUE;
1290 decompvec[i] = FALSE;
1294 * now let the user-defined picksplit function set up the split
1295 * vector; in entryvec have no null value!!
1297 FunctionCall2(&giststate->picksplitFn[0],
1298 PointerGetDatum(entryvec),
1299 PointerGetDatum(&v));
1301 /* compatibility with old code */
1302 if (v.spl_left[v.spl_nleft - 1] == InvalidOffsetNumber)
1303 v.spl_left[v.spl_nleft - 1] = (OffsetNumber) *len;
1304 if (v.spl_right[v.spl_nright - 1] == InvalidOffsetNumber)
1305 v.spl_right[v.spl_nright - 1] = (OffsetNumber) *len;
1307 v.spl_lattr[0] = v.spl_ldatum;
1308 v.spl_rattr[0] = v.spl_rdatum;
1309 v.spl_lisnull[0] = false;
1310 v.spl_risnull[0] = false;
1313 * if index is multikey, then we must to try get smaller bounding box
1316 if (r->rd_att->natts > 1)
1318 v.spl_idgrp = (int *) palloc(sizeof(int) * (*len + 1));
1319 MemSet((void *) v.spl_idgrp, 0, sizeof(int) * (*len + 1));
1320 v.spl_grpflag = (char *) palloc(sizeof(char) * (*len + 1));
1321 MemSet((void *) v.spl_grpflag, 0, sizeof(char) * (*len + 1));
1322 v.spl_ngrp = (int *) palloc(sizeof(int) * (*len + 1));
1324 MaxGrpId = gistfindgroup(giststate, (GISTENTRY *) VARDATA(entryvec), &v);
1326 /* form union of sub keys for each page (l,p) */
1327 gistunionsubkey(r, giststate, itup, &v);
1330 * if possible, we insert equivalent tuples with control by
1331 * penalty for a subkey(s)
1334 gistadjsubkey(r, itup, len, &v, giststate);
1337 pfree(v.spl_grpflag);
1341 /* clean up the entry vector: its keys need to be deleted, too */
1342 for (i = 1; i <= *len; i++)
1344 pfree(DatumGetPointer(((GISTENTRY *) VARDATA(entryvec))[i].key));
1345 pfree(storage); /* pfree(entryvec); */
1348 /* form left and right vector */
1349 lvectup = (IndexTuple *) palloc(sizeof(IndexTuple) * v.spl_nleft);
1350 rvectup = (IndexTuple *) palloc(sizeof(IndexTuple) * v.spl_nright);
1352 for (i = 0; i < v.spl_nleft; i++)
1353 lvectup[i] = itup[v.spl_left[i] - 1];
1355 for (i = 0; i < v.spl_nright; i++)
1356 rvectup[i] = itup[v.spl_right[i] - 1];
1359 /* write on disk (may be need another split) */
1360 if (gistnospace(right, rvectup, v.spl_nright))
1362 nlen = v.spl_nright;
1363 newtup = gistSplit(r, rightbuf, rvectup, &nlen, giststate,
1364 (res && rvectup[nlen - 1] == itup[*len - 1]) ? res : NULL);
1365 ReleaseBuffer(rightbuf);
1366 for (j = 1; j < r->rd_att->natts; j++)
1367 if ((!isAttByVal(giststate, j)) && !v.spl_risnull[j])
1368 pfree(DatumGetPointer(v.spl_rattr[j]));
1374 l = gistwritebuffer(r, right, rvectup, v.spl_nright, FirstOffsetNumber);
1375 WriteBuffer(rightbuf);
1378 ItemPointerSet(&((*res)->pointerData), rbknum, l);
1381 newtup = (IndexTuple *) palloc(sizeof(IndexTuple) * 1);
1382 newtup[0] = gistFormTuple(giststate, r, v.spl_rattr, v.spl_rattrsize, v.spl_risnull);
1383 ItemPointerSet(&(newtup[0]->t_tid), rbknum, 1);
1387 if (gistnospace(left, lvectup, v.spl_nleft))
1389 int llen = v.spl_nleft;
1392 lntup = gistSplit(r, leftbuf, lvectup, &llen, giststate,
1393 (res && lvectup[llen - 1] == itup[*len - 1]) ? res : NULL);
1394 ReleaseBuffer(leftbuf);
1396 for (j = 1; j < r->rd_att->natts; j++)
1397 if ((!isAttByVal(giststate, j)) && !v.spl_lisnull[j])
1398 pfree(DatumGetPointer(v.spl_lattr[j]));
1400 newtup = gistjoinvector(newtup, &nlen, lntup, llen);
1407 l = gistwritebuffer(r, left, lvectup, v.spl_nleft, FirstOffsetNumber);
1408 if (BufferGetBlockNumber(buffer) != GISTP_ROOT)
1409 PageRestoreTempPage(left, p);
1411 WriteBuffer(leftbuf);
1414 ItemPointerSet(&((*res)->pointerData), lbknum, l);
1417 newtup = (IndexTuple *) repalloc((void *) newtup, sizeof(IndexTuple) * nlen);
1418 newtup[nlen - 1] = gistFormTuple(giststate, r, v.spl_lattr, v.spl_lattrsize, v.spl_lisnull);
1419 ItemPointerSet(&(newtup[nlen - 1]->t_tid), lbknum, 1);
1433 gistnewroot(Relation r, IndexTuple *itup, int len)
1438 b = ReadBuffer(r, GISTP_ROOT);
1439 GISTInitBuffer(b, 0);
1440 p = BufferGetPage(b);
1442 gistwritebuffer(r, p, itup, len, FirstOffsetNumber);
1447 GISTInitBuffer(Buffer b, uint32 f)
1449 GISTPageOpaque opaque;
1453 pageSize = BufferGetPageSize(b);
1455 page = BufferGetPage(b);
1457 PageInit(page, pageSize, sizeof(GISTPageOpaqueData));
1459 opaque = (GISTPageOpaque) PageGetSpecialPointer(page);
1465 ** find entry with lowest penalty
1468 gistchoose(Relation r, Page p, IndexTuple it, /* it has compressed entry */
1469 GISTSTATE *giststate)
1471 OffsetNumber maxoff;
1477 which_grow[INDEX_MAX_KEYS];
1479 identry[INDEX_MAX_KEYS];
1481 decompvec[INDEX_MAX_KEYS],
1482 isnull[INDEX_MAX_KEYS];
1485 maxoff = PageGetMaxOffsetNumber(p);
1489 gistDeCompressAtt(giststate, r,
1490 it, (Page) NULL, (OffsetNumber) 0,
1491 identry, decompvec, isnull);
1493 for (i = FirstOffsetNumber; i <= maxoff && sum_grow; i = OffsetNumberNext(i))
1495 IndexTuple itup = (IndexTuple) PageGetItem(p, PageGetItemId(p, i));
1498 for (j = 0; j < r->rd_att->natts; j++)
1500 datum = index_getattr(itup, j + 1, giststate->tupdesc, &IsNull);
1501 gistdentryinit(giststate, j, &entry, datum, r, p, i, ATTSIZE(datum, giststate->tupdesc, j + 1, IsNull), FALSE, IsNull);
1502 gistpenalty(giststate, j, &entry, IsNull, &identry[j], isnull[j], &usize);
1504 if ((!isAttByVal(giststate, j)) && entry.key != datum)
1505 pfree(DatumGetPointer(entry.key));
1507 if (which_grow[j] < 0 || usize < which_grow[j])
1510 which_grow[j] = usize;
1511 if (j < r->rd_att->natts - 1 && i == FirstOffsetNumber)
1512 which_grow[j + 1] = -1;
1513 sum_grow += which_grow[j];
1515 else if (which_grow[j] == usize)
1525 gistFreeAtt(r, identry, decompvec);
1530 gistfreestack(GISTSTACK *s)
1534 while (s != (GISTSTACK *) NULL)
1544 * Retail deletion of a single tuple.
1546 * NB: this is no longer called externally, but is still needed by
1547 * gistlayerinsert(). That dependency will have to be fixed if GIST
1548 * is ever going to allow concurrent insertions.
1551 gistdelete(Relation r, ItemPointer tid)
1554 OffsetNumber offnum;
1559 * Since GIST is not marked "amconcurrent" in pg_am, caller should
1560 * have acquired exclusive lock on index relation. We need no locking
1564 blkno = ItemPointerGetBlockNumber(tid);
1565 offnum = ItemPointerGetOffsetNumber(tid);
1567 /* adjust any scans that will be affected by this deletion */
1568 /* NB: this works only for scans in *this* backend! */
1569 gistadjscans(r, GISTOP_DEL, blkno, offnum);
1571 /* delete the index tuple */
1572 buf = ReadBuffer(r, blkno);
1573 page = BufferGetPage(buf);
1575 PageIndexTupleDelete(page, offnum);
1581 * Bulk deletion of all index entries pointing to a set of heap tuples.
1582 * The set of target tuples is specified via a callback routine that tells
1583 * whether any given heap tuple (identified by ItemPointer) is being deleted.
1585 * Result: a palloc'd struct containing statistical info for VACUUM displays.
1588 gistbulkdelete(PG_FUNCTION_ARGS)
1590 Relation rel = (Relation) PG_GETARG_POINTER(0);
1591 IndexBulkDeleteCallback callback = (IndexBulkDeleteCallback) PG_GETARG_POINTER(1);
1592 void *callback_state = (void *) PG_GETARG_POINTER(2);
1593 IndexBulkDeleteResult *result;
1594 BlockNumber num_pages;
1595 double tuples_removed;
1596 double num_index_tuples;
1597 IndexScanDesc iscan;
1600 num_index_tuples = 0;
1603 * Since GIST is not marked "amconcurrent" in pg_am, caller should
1604 * have acquired exclusive lock on index relation. We need no locking
1609 * XXX generic implementation --- should be improved!
1612 /* walk through the entire index */
1613 iscan = index_beginscan(NULL, rel, SnapshotAny, 0, (ScanKey) NULL);
1614 /* including killed tuples */
1615 iscan->ignore_killed_tuples = false;
1617 while (index_getnext_indexitem(iscan, ForwardScanDirection))
1619 if (callback(&iscan->xs_ctup.t_self, callback_state))
1621 ItemPointerData indextup = iscan->currentItemData;
1623 OffsetNumber offnum;
1627 blkno = ItemPointerGetBlockNumber(&indextup);
1628 offnum = ItemPointerGetOffsetNumber(&indextup);
1630 /* adjust any scans that will be affected by this deletion */
1631 gistadjscans(rel, GISTOP_DEL, blkno, offnum);
1633 /* delete the index tuple */
1634 buf = ReadBuffer(rel, blkno);
1635 page = BufferGetPage(buf);
1637 PageIndexTupleDelete(page, offnum);
1641 tuples_removed += 1;
1644 num_index_tuples += 1;
1647 index_endscan(iscan);
1649 /* return statistics */
1650 num_pages = RelationGetNumberOfBlocks(rel);
1652 result = (IndexBulkDeleteResult *) palloc(sizeof(IndexBulkDeleteResult));
1653 result->num_pages = num_pages;
1654 result->tuples_removed = tuples_removed;
1655 result->num_index_tuples = num_index_tuples;
1657 PG_RETURN_POINTER(result);
1662 initGISTstate(GISTSTATE *giststate, Relation index)
1666 if (index->rd_att->natts > INDEX_MAX_KEYS)
1667 elog(ERROR, "initGISTstate: numberOfAttributes %d > %d",
1668 index->rd_att->natts, INDEX_MAX_KEYS);
1670 giststate->tupdesc = index->rd_att;
1672 for (i = 0; i < index->rd_att->natts; i++)
1674 fmgr_info_copy(&(giststate->consistentFn[i]),
1675 index_getprocinfo(index, i + 1, GIST_CONSISTENT_PROC),
1676 CurrentMemoryContext);
1677 fmgr_info_copy(&(giststate->unionFn[i]),
1678 index_getprocinfo(index, i + 1, GIST_UNION_PROC),
1679 CurrentMemoryContext);
1680 fmgr_info_copy(&(giststate->compressFn[i]),
1681 index_getprocinfo(index, i + 1, GIST_COMPRESS_PROC),
1682 CurrentMemoryContext);
1683 fmgr_info_copy(&(giststate->decompressFn[i]),
1684 index_getprocinfo(index, i + 1, GIST_DECOMPRESS_PROC),
1685 CurrentMemoryContext);
1686 fmgr_info_copy(&(giststate->penaltyFn[i]),
1687 index_getprocinfo(index, i + 1, GIST_PENALTY_PROC),
1688 CurrentMemoryContext);
1689 fmgr_info_copy(&(giststate->picksplitFn[i]),
1690 index_getprocinfo(index, i + 1, GIST_PICKSPLIT_PROC),
1691 CurrentMemoryContext);
1692 fmgr_info_copy(&(giststate->equalFn[i]),
1693 index_getprocinfo(index, i + 1, GIST_EQUAL_PROC),
1694 CurrentMemoryContext);
1699 freeGISTstate(GISTSTATE *giststate)
1704 #ifdef GIST_PAGEADDITEM
1706 ** Given an IndexTuple to be inserted on a page, this routine replaces
1707 ** the key with another key, which may involve generating a new IndexTuple
1708 ** if the sizes don't match or if the null status changes.
1710 ** XXX this only works for a single-column index tuple!
1713 gist_tuple_replacekey(Relation r, GISTENTRY entry, IndexTuple t)
1716 Datum datum = index_getattr(t, 1, r->rd_att, &IsNull);
1719 * If new entry fits in index tuple, copy it in. To avoid worrying
1720 * about null-value bitmask, pass it off to the general
1721 * index_formtuple routine if either the previous or new value is
1724 if (!IsNull && DatumGetPointer(entry.key) != NULL &&
1725 (Size) entry.bytes <= ATTSIZE(datum, r, 1, IsNull))
1727 memcpy(DatumGetPointer(datum),
1728 DatumGetPointer(entry.key),
1730 /* clear out old size */
1731 t->t_info &= ~INDEX_SIZE_MASK;
1732 /* or in new size */
1733 t->t_info |= MAXALIGN(entry.bytes + sizeof(IndexTupleData));
1739 /* generate a new index tuple for the compressed entry */
1740 TupleDesc tupDesc = r->rd_att;
1744 isnull = DatumGetPointer(entry.key) != NULL ? ' ' : 'n';
1745 newtup = (IndexTuple) index_formtuple(tupDesc,
1748 newtup->t_tid = t->t_tid;
1755 ** initialize a GiST entry with a decompressed version of key
1758 gistdentryinit(GISTSTATE *giststate, int nkey, GISTENTRY *e,
1759 Datum k, Relation r, Page pg, OffsetNumber o,
1760 int b, bool l, bool isNull)
1766 gistentryinit(*e, k, r, pg, o, b, l);
1768 DatumGetPointer(FunctionCall1(&giststate->decompressFn[nkey],
1769 PointerGetDatum(e)));
1770 /* decompressFn may just return the given pointer */
1773 gistentryinit(*e, dep->key, dep->rel, dep->page, dep->offset,
1774 dep->bytes, dep->leafkey);
1779 gistentryinit(*e, (Datum) 0, r, pg, o, 0, l);
1784 ** initialize a GiST entry with a compressed version of key
1787 gistcentryinit(GISTSTATE *giststate, int nkey,
1788 GISTENTRY *e, Datum k, Relation r,
1789 Page pg, OffsetNumber o, int b, bool l, bool isNull)
1795 gistentryinit(*e, k, r, pg, o, b, l);
1797 DatumGetPointer(FunctionCall1(&giststate->compressFn[nkey],
1798 PointerGetDatum(e)));
1799 /* compressFn may just return the given pointer */
1802 gistentryinit(*e, cep->key, cep->rel, cep->page, cep->offset,
1803 cep->bytes, cep->leafkey);
1808 gistentryinit(*e, (Datum) 0, r, pg, o, 0, l);
1812 gistFormTuple(GISTSTATE *giststate, Relation r,
1813 Datum attdata[], int datumsize[], bool isnull[])
1816 char isnullchar[INDEX_MAX_KEYS];
1817 bool whatfree[INDEX_MAX_KEYS];
1818 GISTENTRY centry[INDEX_MAX_KEYS];
1819 Datum compatt[INDEX_MAX_KEYS];
1822 for (j = 0; j < r->rd_att->natts; j++)
1826 isnullchar[j] = 'n';
1827 compatt[j] = (Datum) 0;
1828 whatfree[j] = FALSE;
1832 gistcentryinit(giststate, j, ¢ry[j], attdata[j],
1833 (Relation) NULL, (Page) NULL, (OffsetNumber) NULL,
1834 datumsize[j], FALSE, FALSE);
1835 isnullchar[j] = ' ';
1836 compatt[j] = centry[j].key;
1837 if (!isAttByVal(giststate, j))
1840 if (centry[j].key != attdata[j])
1841 pfree(DatumGetPointer(attdata[j]));
1844 whatfree[j] = FALSE;
1848 tup = (IndexTuple) index_formtuple(giststate->tupdesc, compatt, isnullchar);
1849 for (j = 0; j < r->rd_att->natts; j++)
1851 pfree(DatumGetPointer(compatt[j]));
1857 gistDeCompressAtt(GISTSTATE *giststate, Relation r, IndexTuple tuple, Page p,
1858 OffsetNumber o, GISTENTRY attdata[], bool decompvec[], bool isnull[])
1863 for (i = 0; i < r->rd_att->natts; i++)
1865 datum = index_getattr(tuple, i + 1, giststate->tupdesc, &isnull[i]);
1866 gistdentryinit(giststate, i, &attdata[i],
1868 ATTSIZE(datum, giststate->tupdesc, i + 1, isnull[i]), FALSE, isnull[i]);
1869 if (isAttByVal(giststate, i))
1870 decompvec[i] = FALSE;
1873 if (attdata[i].key == datum || isnull[i])
1874 decompvec[i] = FALSE;
1876 decompvec[i] = TRUE;
1882 gistFreeAtt(Relation r, GISTENTRY attdata[], bool decompvec[])
1886 for (i = 0; i < r->rd_att->natts; i++)
1888 pfree(DatumGetPointer(attdata[i].key));
1892 gistpenalty(GISTSTATE *giststate, int attno,
1893 GISTENTRY *key1, bool isNull1,
1894 GISTENTRY *key2, bool isNull2, float *penalty)
1896 if (giststate->penaltyFn[attno].fn_strict && (isNull1 || isNull2))
1899 FunctionCall3(&giststate->penaltyFn[attno],
1900 PointerGetDatum(key1),
1901 PointerGetDatum(key2),
1902 PointerGetDatum(penalty));
1907 gist_dumptree(Relation r, int level, BlockNumber blk, OffsetNumber coff)
1911 GISTPageOpaque opaque;
1919 pred = (char *) palloc(sizeof(char) * level + 1);
1920 MemSet(pred, '\t', level);
1923 buffer = ReadBuffer(r, blk);
1924 page = (Page) BufferGetPage(buffer);
1925 opaque = (GISTPageOpaque) PageGetSpecialPointer(page);
1927 maxoff = PageGetMaxOffsetNumber(page);
1929 elog(DEBUG3, "%sPage: %d %s blk: %d maxoff: %d free: %d", pred,
1930 coff, (opaque->flags & F_LEAF) ? "LEAF" : "INTE", (int) blk,
1931 (int) maxoff, PageGetFreeSpace(page));
1933 for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
1935 iid = PageGetItemId(page, i);
1936 which = (IndexTuple) PageGetItem(page, iid);
1937 cblk = ItemPointerGetBlockNumber(&(which->t_tid));
1939 elog(DEBUG3, "%s Tuple. blk: %d size: %d", pred, (int) cblk,
1940 IndexTupleSize(which));
1943 if (!(opaque->flags & F_LEAF))
1944 gist_dumptree(r, level + 1, cblk, i);
1946 ReleaseBuffer(buffer);
1949 #endif /* defined GISTDEBUG */
1952 gist_redo(XLogRecPtr lsn, XLogRecord *record)
1954 elog(PANIC, "gist_redo: unimplemented");
1958 gist_undo(XLogRecPtr lsn, XLogRecord *record)
1960 elog(PANIC, "gist_undo: unimplemented");
1964 gist_desc(char *buf, uint8 xl_info, char *rec)