1
/*-------------------------------------------------------------------------
4
* interface routines for the postgres GiST index access method.
7
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
8
* Portions Copyright (c) 1994, Regents of the University of California
11
* $PostgreSQL: pgsql/src/backend/access/gist/gist.c,v 1.112 2004-12-31 21:59:10 pgsql 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 "commands/vacuum.h"
23
#include "miscadmin.h"
26
#undef GIST_PAGEADDITEM
28
#define ATTSIZE( datum, TupDesc, i, isnull ) \
31
att_addlength(0, (TupDesc)->attrs[(i)-1]->attlen, (datum)) \
38
/* group flags ( in gistSplit ) */
39
#define LEFT_ADDED 0x01
40
#define RIGHT_ADDED 0x02
41
#define BOTH_ADDED ( LEFT_ADDED | RIGHT_ADDED )
44
* This defines only for shorter code, used in gistgetadjusted
45
* and gistadjsubkey only
47
#define FILLITEM(evp, isnullkey, okey, okeyb, rkey, rkeyb) do { \
49
gistentryinit((evp), rkey, r, NULL, \
50
(OffsetNumber) 0, rkeyb, FALSE); \
52
gistentryinit((evp), okey, r, NULL, \
53
(OffsetNumber) 0, okeyb, FALSE); \
57
#define FILLEV(isnull1, key1, key1b, isnull2, key2, key2b) do { \
58
FILLITEM(*ev0p, isnull1, key1, key1b, key2, key2b); \
59
FILLITEM(*ev1p, isnull2, key2, key2b, key1, key1b); \
62
/* Working state for gistbuild and its callback */
71
/* non-export function prototypes */
72
static void gistbuildCallback(Relation index,
78
static void gistdoinsert(Relation r,
80
InsertIndexResult *res,
81
GISTSTATE *GISTstate);
82
static int gistlayerinsert(Relation r, BlockNumber blkno,
85
InsertIndexResult *res,
86
GISTSTATE *giststate);
87
static OffsetNumber gistwritebuffer(Relation r,
92
static int gistnospace(Page page,
93
IndexTuple *itvec, int len);
94
static IndexTuple *gistreadbuffer(Buffer buffer, int *len);
95
static IndexTuple *gistjoinvector(
96
IndexTuple *itvec, int *len,
97
IndexTuple *additvec, int addlen);
98
static IndexTuple gistunion(Relation r, IndexTuple *itvec,
99
int len, GISTSTATE *giststate);
101
static IndexTuple gistgetadjusted(Relation r,
104
GISTSTATE *giststate);
105
static int gistfindgroup(GISTSTATE *giststate,
106
GISTENTRY *valvec, GIST_SPLITVEC *spl);
107
static void gistadjsubkey(Relation r,
108
IndexTuple *itup, int *len,
110
GISTSTATE *giststate);
111
static IndexTuple gistFormTuple(GISTSTATE *giststate,
112
Relation r, Datum attdata[], int datumsize[], bool isnull[]);
113
static IndexTuple *gistSplit(Relation r,
117
GISTSTATE *giststate,
118
InsertIndexResult *res);
119
static void gistnewroot(Relation r,
120
IndexTuple *itup, int len);
121
static void GISTInitBuffer(Buffer b, uint32 f);
122
static OffsetNumber gistchoose(Relation r, Page p,
124
GISTSTATE *giststate);
125
static void gistdelete(Relation r, ItemPointer tid);
127
#ifdef GIST_PAGEADDITEM
128
static IndexTuple gist_tuple_replacekey(Relation r,
129
GISTENTRY entry, IndexTuple t);
131
static void gistcentryinit(GISTSTATE *giststate, int nkey,
132
GISTENTRY *e, Datum k,
134
OffsetNumber o, int b, bool l, bool isNull);
135
static void gistDeCompressAtt(GISTSTATE *giststate, Relation r,
136
IndexTuple tuple, Page p, OffsetNumber o,
137
GISTENTRY attdata[], bool decompvec[], bool isnull[]);
138
static void gistFreeAtt(Relation r, GISTENTRY attdata[], bool decompvec[]);
139
static void gistpenalty(GISTSTATE *giststate, int attno,
140
GISTENTRY *key1, bool isNull1,
141
GISTENTRY *key2, bool isNull2,
147
static void gist_dumptree(Relation r, int level, BlockNumber blk, OffsetNumber coff);
151
* routine to build an index. Basically calls insert over and over
154
gistbuild(PG_FUNCTION_ARGS)
156
Relation heap = (Relation) PG_GETARG_POINTER(0);
157
Relation index = (Relation) PG_GETARG_POINTER(1);
158
IndexInfo *indexInfo = (IndexInfo *) PG_GETARG_POINTER(2);
160
GISTBuildState buildstate;
163
/* no locking is needed */
165
initGISTstate(&buildstate.giststate, index);
168
* We expect to be called exactly once for any index relation. If
169
* that's not the case, big trouble's what we have.
171
if (RelationGetNumberOfBlocks(index) != 0)
172
elog(ERROR, "index \"%s\" already contains data",
173
RelationGetRelationName(index));
175
/* initialize the root page */
176
buffer = ReadBuffer(index, P_NEW);
177
GISTInitBuffer(buffer, F_LEAF);
180
/* build the index */
181
buildstate.numindexattrs = indexInfo->ii_NumIndexAttrs;
182
buildstate.indtuples = 0;
184
/* do the heap scan */
185
reltuples = IndexBuildHeapScan(heap, index, indexInfo,
186
gistbuildCallback, (void *) &buildstate);
188
/* okay, all heap tuples are indexed */
191
* Since we just counted the tuples in the heap, we update its stats
192
* in pg_class to guarantee that the planner takes advantage of the
193
* index we just created. But, only update statistics during normal
194
* index definitions, not for indices on system catalogs created
195
* during bootstrap processing. We must close the relations before
196
* updating statistics to guarantee that the relcache entries are
197
* flushed when we increment the command counter in UpdateStats(). But
198
* we do not release any locks on the relations; those will be held
199
* until end of transaction.
201
if (IsNormalProcessingMode())
203
Oid hrelid = RelationGetRelid(heap);
204
Oid irelid = RelationGetRelid(index);
206
heap_close(heap, NoLock);
208
UpdateStats(hrelid, reltuples);
209
UpdateStats(irelid, buildstate.indtuples);
212
freeGISTstate(&buildstate.giststate);
214
gist_dumptree(index, 0, GISTP_ROOT, 0);
221
* Per-tuple callback from IndexBuildHeapScan
224
gistbuildCallback(Relation index,
231
GISTBuildState *buildstate = (GISTBuildState *) state;
233
bool compvec[INDEX_MAX_KEYS];
237
/* GiST cannot index tuples with leading NULLs */
241
/* immediately compress keys to normalize */
242
for (i = 0; i < buildstate->numindexattrs; i++)
246
attdata[i] = (Datum) 0;
251
gistcentryinit(&buildstate->giststate, i, &tmpcentry, attdata[i],
252
NULL, NULL, (OffsetNumber) 0,
253
-1 /* size is currently bogus */ , TRUE, FALSE);
254
if (attdata[i] != tmpcentry.key &&
255
!(isAttByVal(&buildstate->giststate, i)))
259
attdata[i] = tmpcentry.key;
263
/* form an index tuple and point it at the heap tuple */
264
itup = index_formtuple(buildstate->giststate.tupdesc, attdata, nulls);
265
itup->t_tid = htup->t_self;
268
* Since we already have the index relation locked, we call
269
* gistdoinsert directly. Normal access method calls dispatch through
270
* gistinsert, which locks the relation for write. This is the right
271
* thing to do if you're inserting single tups, but not when you're
272
* initializing the whole index at once.
274
gistdoinsert(index, itup, NULL, &buildstate->giststate);
276
buildstate->indtuples += 1;
278
for (i = 0; i < buildstate->numindexattrs; i++)
280
pfree(DatumGetPointer(attdata[i]));
286
* gistinsert -- wrapper for GiST tuple insertion.
288
* This is the public interface routine for tuple insertion in GiSTs.
289
* It doesn't do any work; just locks the relation and passes the buck.
292
gistinsert(PG_FUNCTION_ARGS)
294
Relation r = (Relation) PG_GETARG_POINTER(0);
295
Datum *datum = (Datum *) PG_GETARG_POINTER(1);
296
char *nulls = (char *) PG_GETARG_POINTER(2);
297
ItemPointer ht_ctid = (ItemPointer) PG_GETARG_POINTER(3);
300
Relation heapRel = (Relation) PG_GETARG_POINTER(4);
301
bool checkUnique = PG_GETARG_BOOL(5);
303
InsertIndexResult res;
308
bool compvec[INDEX_MAX_KEYS];
311
* Since GIST is not marked "amconcurrent" in pg_am, caller should
312
* have acquired exclusive lock on index relation. We need no locking
316
/* GiST cannot index tuples with leading NULLs */
320
PG_RETURN_POINTER(res);
323
initGISTstate(&giststate, r);
325
/* immediately compress keys to normalize */
326
for (i = 0; i < r->rd_att->natts; i++)
330
datum[i] = (Datum) 0;
335
gistcentryinit(&giststate, i, &tmpentry, datum[i],
336
NULL, NULL, (OffsetNumber) 0,
337
-1 /* size is currently bogus */ , TRUE, FALSE);
338
if (datum[i] != tmpentry.key && !(isAttByVal(&giststate, i)))
342
datum[i] = tmpentry.key;
345
itup = index_formtuple(giststate.tupdesc, datum, nulls);
346
itup->t_tid = *ht_ctid;
348
res = (InsertIndexResult) palloc(sizeof(InsertIndexResultData));
349
gistdoinsert(r, itup, &res, &giststate);
351
for (i = 0; i < r->rd_att->natts; i++)
352
if (compvec[i] == TRUE)
353
pfree(DatumGetPointer(datum[i]));
355
freeGISTstate(&giststate);
357
PG_RETURN_POINTER(res);
360
#ifdef GIST_PAGEADDITEM
362
* Take a compressed entry, and install it on a page. Since we now know
363
* where the entry will live, we decompress it and recompress it using
364
* that knowledge (some compression routines may want to fish around
365
* on the page, for example, or do something special for leaf nodes.)
368
gistPageAddItem(GISTSTATE *giststate,
373
OffsetNumber offsetNumber,
379
IndexTuple itup = (IndexTuple) item;
385
* recompress the item given that we now know the exact page and
386
* offset for insertion
388
datum = index_getattr(itup, 1, r->rd_att, &IsNull);
389
gistdentryinit(giststate, 0, dentry, datum,
390
(Relation) 0, (Page) 0,
391
(OffsetNumber) InvalidOffsetNumber,
392
ATTSIZE(datum, r, 1, IsNull),
394
gistcentryinit(giststate, 0, &tmpcentry, dentry->key, r, page,
395
offsetNumber, dentry->bytes, FALSE);
396
*newtup = gist_tuple_replacekey(r, tmpcentry, itup);
397
retval = PageAddItem(page, (Item) *newtup, IndexTupleSize(*newtup),
398
offsetNumber, flags);
399
if (retval == InvalidOffsetNumber)
400
elog(ERROR, "failed to add index item to \"%s\"",
401
RelationGetRelationName(r));
403
if (DatumGetPointer(tmpcentry.key) != NULL &&
404
tmpcentry.key != dentry->key &&
405
tmpcentry.key != datum)
406
pfree(DatumGetPointer(tmpcentry.key));
412
gistdoinsert(Relation r,
414
InsertIndexResult *res,
415
GISTSTATE *giststate)
422
instup = (IndexTuple *) palloc(sizeof(IndexTuple));
423
instup[0] = (IndexTuple) palloc(IndexTupleSize(itup));
424
memcpy(instup[0], itup, IndexTupleSize(itup));
426
ret = gistlayerinsert(r, GISTP_ROOT, &instup, &len, res, giststate);
428
gistnewroot(r, instup, len);
430
for (i = 0; i < len; i++)
436
gistlayerinsert(Relation r, BlockNumber blkno,
437
IndexTuple **itup, /* in - out, has compressed entry */
438
int *len, /* in - out */
439
InsertIndexResult *res, /* out */
440
GISTSTATE *giststate)
446
GISTPageOpaque opaque;
448
buffer = ReadBuffer(r, blkno);
449
page = (Page) BufferGetPage(buffer);
450
opaque = (GISTPageOpaque) PageGetSpecialPointer(page);
452
if (!(opaque->flags & F_LEAF))
454
/* internal page, so we must walk on tree */
458
ItemPointerData oldtid;
461
child = gistchoose(r, page, *(*itup), giststate);
462
iid = PageGetItemId(page, child);
463
oldtup = (IndexTuple) PageGetItem(page, iid);
464
nblkno = ItemPointerGetBlockNumber(&(oldtup->t_tid));
467
* After this call: 1. if child page was splited, then itup
468
* contains keys for each page 2. if child page wasn't splited,
469
* then itup contains additional for adjustment of current key
471
ret = gistlayerinsert(r, nblkno, itup, len, res, giststate);
473
/* nothing inserted in child */
474
if (!(ret & INSERTED))
476
ReleaseBuffer(buffer);
480
/* child does not splited */
481
if (!(ret & SPLITED))
483
IndexTuple newtup = gistgetadjusted(r, oldtup, (*itup)[0], giststate);
487
/* not need to update key */
488
ReleaseBuffer(buffer);
492
pfree((*itup)[0]); /* !!! */
496
/* key is modified, so old version must be deleted */
497
ItemPointerSet(&oldtid, blkno, child);
498
gistdelete(r, &oldtid);
501
* if child was splitted, new key for child will be inserted in
502
* the end list of child, so we must say to any scans that page is
503
* changed beginning from 'child' offset
506
gistadjscans(r, GISTOP_SPLIT, blkno, child);
511
if (gistnospace(page, (*itup), *len))
513
/* no space for insertion */
520
itvec = gistreadbuffer(buffer, &tlen);
521
itvec = gistjoinvector(itvec, &tlen, (*itup), *len);
523
newitup = gistSplit(r, buffer, itvec, &tlen, giststate,
524
(opaque->flags & F_LEAF) ? res : NULL); /* res only for
525
* inserting in leaf */
526
ReleaseBuffer(buffer);
528
pfree((*itup)[oldlen - 1]);
529
while ((--oldlen) > 0);
533
*len = tlen; /* now tlen >= 2 */
541
off = (PageIsEmpty(page)) ?
544
OffsetNumberNext(PageGetMaxOffsetNumber(page));
545
l = gistwritebuffer(r, page, (*itup), *len, off);
549
* set res if insert into leaf page, in this case, len = 1 always
551
if (res && (opaque->flags & F_LEAF))
552
ItemPointerSet(&((*res)->pointerData), blkno, l);
555
{ /* previous insert ret & SPLITED != 0 */
559
* child was splited, so we must form union for insertion in
562
IndexTuple newtup = gistunion(r, (*itup), *len, giststate);
564
ItemPointerSet(&(newtup->t_tid), blkno, 1);
566
for (i = 0; i < *len; i++)
577
* Write itup vector to page, has no control of free space
580
gistwritebuffer(Relation r, Page page, IndexTuple *itup,
581
int len, OffsetNumber off)
583
OffsetNumber l = InvalidOffsetNumber;
586
#ifdef GIST_PAGEADDITEM
591
for (i = 0; i < len; i++)
593
#ifdef GIST_PAGEADDITEM
594
l = gistPageAddItem(giststate, r, page,
595
(Item) itup[i], IndexTupleSize(itup[i]),
596
off, LP_USED, &tmpdentry, &newtup);
597
off = OffsetNumberNext(off);
598
if (DatumGetPointer(tmpdentry.key) != NULL &&
599
tmpdentry.key != index_getattr(itup[i], 1, r->rd_att, &IsNull))
600
pfree(DatumGetPointer(tmpdentry.key));
601
if (itup[i] != newtup)
604
l = PageAddItem(page, (Item) itup[i], IndexTupleSize(itup[i]),
606
if (l == InvalidOffsetNumber)
607
elog(ERROR, "failed to add index item to \"%s\"",
608
RelationGetRelationName(r));
615
* Check space for itup vector on page
618
gistnospace(Page page, IndexTuple *itvec, int len)
620
unsigned int size = 0;
623
for (i = 0; i < len; i++)
624
size += IndexTupleSize(itvec[i]) + sizeof(ItemIdData);
626
return (PageGetFreeSpace(page) < size);
630
* Read buffer into itup vector
633
gistreadbuffer(Buffer buffer, int *len /* out */ )
638
Page p = (Page) BufferGetPage(buffer);
640
maxoff = PageGetMaxOffsetNumber(p);
642
itvec = palloc(sizeof(IndexTuple) * maxoff);
643
for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
644
itvec[i - 1] = (IndexTuple) PageGetItem(p, PageGetItemId(p, i));
650
* join two vectors into one
653
gistjoinvector(IndexTuple *itvec, int *len, IndexTuple *additvec, int addlen)
655
itvec = (IndexTuple *) repalloc((void *) itvec, sizeof(IndexTuple) * ((*len) + addlen));
656
memmove(&itvec[*len], additvec, sizeof(IndexTuple) * addlen);
662
* return union of itup vector
665
gistunion(Relation r, IndexTuple *itvec, int len, GISTSTATE *giststate)
667
Datum attr[INDEX_MAX_KEYS];
668
bool whatfree[INDEX_MAX_KEYS];
669
char isnull[INDEX_MAX_KEYS];
670
GistEntryVector *evec;
675
GISTENTRY centry[INDEX_MAX_KEYS];
681
needfree = (bool *) palloc(((len == 1) ? 2 : len) * sizeof(bool));
682
evec = (GistEntryVector *) palloc(((len == 1) ? 2 : len) * sizeof(GISTENTRY) + GEVHDRSZ);
684
for (j = 0; j < r->rd_att->natts; j++)
687
for (i = 0; i < len; i++)
689
datum = index_getattr(itvec[i], j + 1, giststate->tupdesc, &IsNull);
693
gistdentryinit(giststate, j,
694
&(evec->vector[reallen]),
696
NULL, NULL, (OffsetNumber) 0,
697
ATTSIZE(datum, giststate->tupdesc, j + 1, IsNull), FALSE, IsNull);
698
if ((!isAttByVal(giststate, j)) &&
699
evec->vector[reallen].key != datum)
700
needfree[reallen] = TRUE;
702
needfree[reallen] = FALSE;
717
gistentryinit(evec->vector[1],
718
evec->vector[0].key, r, NULL,
719
(OffsetNumber) 0, evec->vector[0].bytes, FALSE);
724
datum = FunctionCall2(&giststate->unionFn[j],
725
PointerGetDatum(evec),
726
PointerGetDatum(&datumsize));
728
for (i = 0; i < reallen; i++)
730
pfree(DatumGetPointer(evec->vector[i].key));
732
gistcentryinit(giststate, j, ¢ry[j], datum,
733
NULL, NULL, (OffsetNumber) 0,
734
datumsize, FALSE, FALSE);
736
attr[j] = centry[j].key;
737
if (!isAttByVal(giststate, j))
740
if (centry[j].key != datum)
741
pfree(DatumGetPointer(datum));
751
newtup = (IndexTuple) index_formtuple(giststate->tupdesc, attr, isnull);
752
for (j = 0; j < r->rd_att->natts; j++)
754
pfree(DatumGetPointer(attr[j]));
761
* Forms union of oldtup and addtup, if union == oldtup then return NULL
764
gistgetadjusted(Relation r, IndexTuple oldtup, IndexTuple addtup, GISTSTATE *giststate)
766
GistEntryVector *evec;
771
char isnull[INDEX_MAX_KEYS],
772
whatfree[INDEX_MAX_KEYS];
773
Datum attr[INDEX_MAX_KEYS];
774
GISTENTRY centry[INDEX_MAX_KEYS],
775
oldatt[INDEX_MAX_KEYS],
776
addatt[INDEX_MAX_KEYS],
779
bool olddec[INDEX_MAX_KEYS],
780
adddec[INDEX_MAX_KEYS];
781
bool oldisnull[INDEX_MAX_KEYS],
782
addisnull[INDEX_MAX_KEYS];
783
IndexTuple newtup = NULL;
786
evec = palloc(2 * sizeof(GISTENTRY) + GEVHDRSZ);
788
ev0p = &(evec->vector[0]);
789
ev1p = &(evec->vector[1]);
791
gistDeCompressAtt(giststate, r, oldtup, NULL,
792
(OffsetNumber) 0, oldatt, olddec, oldisnull);
794
gistDeCompressAtt(giststate, r, addtup, NULL,
795
(OffsetNumber) 0, addatt, adddec, addisnull);
798
for (j = 0; j < r->rd_att->natts; j++)
800
if (oldisnull[j] && addisnull[j])
809
oldisnull[j], oldatt[j].key, oldatt[j].bytes,
810
addisnull[j], addatt[j].key, addatt[j].bytes
813
datum = FunctionCall2(&giststate->unionFn[j],
814
PointerGetDatum(evec),
815
PointerGetDatum(&datumsize));
817
if (oldisnull[j] || addisnull[j])
824
FunctionCall3(&giststate->equalFn[j],
827
PointerGetDatum(&result));
834
pfree(DatumGetPointer(oldatt[j].key));
836
pfree(DatumGetPointer(addatt[j].key));
838
gistcentryinit(giststate, j, ¢ry[j], datum,
839
NULL, NULL, (OffsetNumber) 0,
840
datumsize, FALSE, FALSE);
843
attr[j] = centry[j].key;
844
if ((!isAttByVal(giststate, j)))
847
if (centry[j].key != datum)
848
pfree(DatumGetPointer(datum));
858
/* need to update key */
859
newtup = (IndexTuple) index_formtuple(giststate->tupdesc, attr, isnull);
860
newtup->t_tid = oldtup->t_tid;
863
for (j = 0; j < r->rd_att->natts; j++)
865
pfree(DatumGetPointer(attr[j]));
871
gistunionsubkey(Relation r, GISTSTATE *giststate, IndexTuple *itvec, GIST_SPLITVEC *spl)
881
OffsetNumber *entries;
882
GistEntryVector *evec;
888
for (lr = 0; lr <= 1; lr++)
892
attrsize = spl->spl_lattrsize;
893
attr = spl->spl_lattr;
894
len = spl->spl_nleft;
895
entries = spl->spl_left;
896
isnull = spl->spl_lisnull;
900
attrsize = spl->spl_rattrsize;
901
attr = spl->spl_rattr;
902
len = spl->spl_nright;
903
entries = spl->spl_right;
904
isnull = spl->spl_risnull;
907
needfree = (bool *) palloc(((len == 1) ? 2 : len) * sizeof(bool));
908
evec = palloc(((len == 1) ? 2 : len) * sizeof(GISTENTRY) + GEVHDRSZ);
910
for (j = 1; j < r->rd_att->natts; j++)
913
for (i = 0; i < len; i++)
915
if (spl->spl_idgrp[entries[i]])
917
datum = index_getattr(itvec[entries[i] - 1], j + 1,
918
giststate->tupdesc, &IsNull);
921
gistdentryinit(giststate, j,
922
&(evec->vector[reallen]),
924
NULL, NULL, (OffsetNumber) 0,
925
ATTSIZE(datum, giststate->tupdesc, j + 1, IsNull), FALSE, IsNull);
926
if ((!isAttByVal(giststate, j)) &&
927
evec->vector[reallen].key != datum)
928
needfree[reallen] = TRUE;
930
needfree[reallen] = FALSE;
943
* evec->vector[0].bytes may be not defined, so form union
949
memcpy((void *) &(evec->vector[1]),
950
(void *) &(evec->vector[0]),
955
datum = FunctionCall2(&giststate->unionFn[j],
956
PointerGetDatum(evec),
957
PointerGetDatum(&datumsize));
961
for (i = 0; i < reallen; i++)
963
pfree(DatumGetPointer(evec->vector[i].key));
966
attrsize[j] = datumsize;
974
* find group in vector with equial value
977
gistfindgroup(GISTSTATE *giststate, GISTENTRY *valvec, GIST_SPLITVEC *spl)
986
* first key is always not null (see gistinsert), so we may not check
990
for (i = 0; i < spl->spl_nleft; i++)
992
if (spl->spl_idgrp[spl->spl_left[i]])
995
/* find all equal value in right part */
996
for (j = 0; j < spl->spl_nright; j++)
998
if (spl->spl_idgrp[spl->spl_right[j]])
1000
FunctionCall3(&giststate->equalFn[0],
1001
valvec[spl->spl_left[i]].key,
1002
valvec[spl->spl_right[j]].key,
1003
PointerGetDatum(&result));
1006
spl->spl_idgrp[spl->spl_right[j]] = curid;
1010
/* find all other equal value in left part */
1013
/* add current val to list of equial values */
1014
spl->spl_idgrp[spl->spl_left[i]] = curid;
1016
for (j = i + 1; j < spl->spl_nleft; j++)
1018
if (spl->spl_idgrp[spl->spl_left[j]])
1020
FunctionCall3(&giststate->equalFn[0],
1021
valvec[spl->spl_left[i]].key,
1022
valvec[spl->spl_left[j]].key,
1023
PointerGetDatum(&result));
1026
spl->spl_idgrp[spl->spl_left[j]] = curid;
1030
spl->spl_ngrp[curid] = len + 1;
1039
* Insert equivalent tuples to left or right page
1040
* with minimize penalty
1043
gistadjsubkey(Relation r,
1044
IndexTuple *itup, /* contains compressed entry */
1047
GISTSTATE *giststate
1051
OffsetNumber *curwpos;
1052
bool decfree[INDEX_MAX_KEYS];
1054
identry[INDEX_MAX_KEYS],
1059
GistEntryVector *evec;
1061
bool isnull[INDEX_MAX_KEYS];
1067
curlen = v->spl_nleft;
1068
curwpos = v->spl_left;
1069
for (i = 0; i < v->spl_nleft; i++)
1070
if (v->spl_idgrp[v->spl_left[i]] == 0)
1072
*curwpos = v->spl_left[i];
1077
v->spl_nleft = curlen;
1079
curlen = v->spl_nright;
1080
curwpos = v->spl_right;
1081
for (i = 0; i < v->spl_nright; i++)
1082
if (v->spl_idgrp[v->spl_right[i]] == 0)
1084
*curwpos = v->spl_right[i];
1089
v->spl_nright = curlen;
1091
evec = palloc(2 * sizeof(GISTENTRY) + GEVHDRSZ);
1093
ev0p = &(evec->vector[0]);
1094
ev1p = &(evec->vector[1]);
1096
/* add equivalent tuple */
1097
for (i = 0; i < *len; i++)
1099
if (v->spl_idgrp[i + 1] == 0) /* already inserted */
1101
gistDeCompressAtt(giststate, r, itup[i], NULL, (OffsetNumber) 0,
1102
identry, decfree, isnull);
1104
v->spl_ngrp[v->spl_idgrp[i + 1]]--;
1105
if (v->spl_ngrp[v->spl_idgrp[i + 1]] == 0 &&
1106
(v->spl_grpflag[v->spl_idgrp[i + 1]] & BOTH_ADDED) != BOTH_ADDED)
1109
/* force last in group */
1111
lpenalty = (v->spl_grpflag[v->spl_idgrp[i + 1]] & LEFT_ADDED) ? 2.0 : 0.0;
1116
for (j = 1; j < r->rd_att->natts; j++)
1118
gistentryinit(entry, v->spl_lattr[j], r, NULL,
1119
(OffsetNumber) 0, v->spl_lattrsize[j], FALSE);
1120
gistpenalty(giststate, j, &entry, v->spl_lisnull[j],
1121
&identry[j], isnull[j], &lpenalty);
1123
gistentryinit(entry, v->spl_rattr[j], r, NULL,
1124
(OffsetNumber) 0, v->spl_rattrsize[j], FALSE);
1125
gistpenalty(giststate, j, &entry, v->spl_risnull[j],
1126
&identry[j], isnull[j], &rpenalty);
1128
if (lpenalty != rpenalty)
1133
if (lpenalty < rpenalty)
1135
v->spl_grpflag[v->spl_idgrp[i + 1]] |= LEFT_ADDED;
1136
v->spl_left[v->spl_nleft] = i + 1;
1138
for (j = 1; j < r->rd_att->natts; j++)
1140
if (isnull[j] && v->spl_lisnull[j])
1142
v->spl_lattr[j] = (Datum) 0;
1143
v->spl_lattrsize[j] = 0;
1148
v->spl_lisnull[j], v->spl_lattr[j], v->spl_lattrsize[j],
1149
isnull[j], identry[j].key, identry[j].bytes
1152
datum = FunctionCall2(&giststate->unionFn[j],
1153
PointerGetDatum(evec),
1154
PointerGetDatum(&datumsize));
1156
if ((!isAttByVal(giststate, j)) && !v->spl_lisnull[j])
1157
pfree(DatumGetPointer(v->spl_lattr[j]));
1158
v->spl_lattr[j] = datum;
1159
v->spl_lattrsize[j] = datumsize;
1160
v->spl_lisnull[j] = false;
1166
v->spl_grpflag[v->spl_idgrp[i + 1]] |= RIGHT_ADDED;
1167
v->spl_right[v->spl_nright] = i + 1;
1169
for (j = 1; j < r->rd_att->natts; j++)
1171
if (isnull[j] && v->spl_risnull[j])
1173
v->spl_rattr[j] = (Datum) 0;
1174
v->spl_rattrsize[j] = 0;
1179
v->spl_risnull[j], v->spl_rattr[j], v->spl_rattrsize[j],
1180
isnull[j], identry[j].key, identry[j].bytes
1183
datum = FunctionCall2(&giststate->unionFn[j],
1184
PointerGetDatum(evec),
1185
PointerGetDatum(&datumsize));
1187
if ((!isAttByVal(giststate, j)) && !v->spl_risnull[j])
1188
pfree(DatumGetPointer(v->spl_rattr[j]));
1190
v->spl_rattr[j] = datum;
1191
v->spl_rattrsize[j] = datumsize;
1192
v->spl_risnull[j] = false;
1197
gistFreeAtt(r, identry, decfree);
1203
* gistSplit -- split a page in the tree.
1206
gistSplit(Relation r,
1208
IndexTuple *itup, /* contains compressed entry */
1210
GISTSTATE *giststate,
1211
InsertIndexResult *res)
1218
IndexTuple *lvectup,
1223
GISTPageOpaque opaque;
1225
GistEntryVector *entryvec;
1234
p = (Page) BufferGetPage(buffer);
1235
opaque = (GISTPageOpaque) PageGetSpecialPointer(p);
1238
* The root of the tree is the first block in the relation. If we're
1239
* about to split the root, we need to do some hocus-pocus to enforce
1243
if (BufferGetBlockNumber(buffer) == GISTP_ROOT)
1245
leftbuf = ReadBuffer(r, P_NEW);
1246
GISTInitBuffer(leftbuf, opaque->flags);
1247
lbknum = BufferGetBlockNumber(leftbuf);
1248
left = (Page) BufferGetPage(leftbuf);
1253
IncrBufferRefCount(buffer);
1254
lbknum = BufferGetBlockNumber(buffer);
1255
left = (Page) PageGetTempPage(p, sizeof(GISTPageOpaqueData));
1258
rightbuf = ReadBuffer(r, P_NEW);
1259
GISTInitBuffer(rightbuf, opaque->flags);
1260
rbknum = BufferGetBlockNumber(rightbuf);
1261
right = (Page) BufferGetPage(rightbuf);
1263
/* generate the item array */
1264
entryvec = palloc(GEVHDRSZ + (*len + 1) * sizeof(GISTENTRY));
1265
entryvec->n = *len + 1;
1266
decompvec = (bool *) palloc((*len + 1) * sizeof(bool));
1267
for (i = 1; i <= *len; i++)
1269
datum = index_getattr(itup[i - 1], 1, giststate->tupdesc, &IsNull);
1270
gistdentryinit(giststate, 0, &(entryvec->vector[i]),
1272
ATTSIZE(datum, giststate->tupdesc, 1, IsNull), FALSE, IsNull);
1273
if ((!isAttByVal(giststate, 0)) && entryvec->vector[i].key != datum)
1274
decompvec[i] = TRUE;
1276
decompvec[i] = FALSE;
1280
* now let the user-defined picksplit function set up the split
1281
* vector; in entryvec have no null value!!
1283
FunctionCall2(&giststate->picksplitFn[0],
1284
PointerGetDatum(entryvec),
1285
PointerGetDatum(&v));
1287
/* compatibility with old code */
1288
if (v.spl_left[v.spl_nleft - 1] == InvalidOffsetNumber)
1289
v.spl_left[v.spl_nleft - 1] = (OffsetNumber) *len;
1290
if (v.spl_right[v.spl_nright - 1] == InvalidOffsetNumber)
1291
v.spl_right[v.spl_nright - 1] = (OffsetNumber) *len;
1293
v.spl_lattr[0] = v.spl_ldatum;
1294
v.spl_rattr[0] = v.spl_rdatum;
1295
v.spl_lisnull[0] = false;
1296
v.spl_risnull[0] = false;
1299
* if index is multikey, then we must to try get smaller bounding box
1302
if (r->rd_att->natts > 1)
1304
v.spl_idgrp = (int *) palloc0(sizeof(int) * (*len + 1));
1305
v.spl_grpflag = (char *) palloc0(sizeof(char) * (*len + 1));
1306
v.spl_ngrp = (int *) palloc(sizeof(int) * (*len + 1));
1308
MaxGrpId = gistfindgroup(giststate, entryvec->vector, &v);
1310
/* form union of sub keys for each page (l,p) */
1311
gistunionsubkey(r, giststate, itup, &v);
1314
* if possible, we insert equivalent tuples with control by
1315
* penalty for a subkey(s)
1318
gistadjsubkey(r, itup, len, &v, giststate);
1321
pfree(v.spl_grpflag);
1325
/* clean up the entry vector: its keys need to be deleted, too */
1326
for (i = 1; i <= *len; i++)
1328
pfree(DatumGetPointer(entryvec->vector[i].key));
1332
/* form left and right vector */
1333
lvectup = (IndexTuple *) palloc(sizeof(IndexTuple) * v.spl_nleft);
1334
rvectup = (IndexTuple *) palloc(sizeof(IndexTuple) * v.spl_nright);
1336
for (i = 0; i < v.spl_nleft; i++)
1337
lvectup[i] = itup[v.spl_left[i] - 1];
1339
for (i = 0; i < v.spl_nright; i++)
1340
rvectup[i] = itup[v.spl_right[i] - 1];
1343
/* write on disk (may be need another split) */
1344
if (gistnospace(right, rvectup, v.spl_nright))
1346
nlen = v.spl_nright;
1347
newtup = gistSplit(r, rightbuf, rvectup, &nlen, giststate,
1348
(res && rvectup[nlen - 1] == itup[*len - 1]) ? res : NULL);
1349
ReleaseBuffer(rightbuf);
1350
for (j = 1; j < r->rd_att->natts; j++)
1351
if ((!isAttByVal(giststate, j)) && !v.spl_risnull[j])
1352
pfree(DatumGetPointer(v.spl_rattr[j]));
1358
l = gistwritebuffer(r, right, rvectup, v.spl_nright, FirstOffsetNumber);
1359
WriteBuffer(rightbuf);
1362
ItemPointerSet(&((*res)->pointerData), rbknum, l);
1365
newtup = (IndexTuple *) palloc(sizeof(IndexTuple) * 1);
1366
newtup[0] = gistFormTuple(giststate, r, v.spl_rattr, v.spl_rattrsize, v.spl_risnull);
1367
ItemPointerSet(&(newtup[0]->t_tid), rbknum, 1);
1371
if (gistnospace(left, lvectup, v.spl_nleft))
1373
int llen = v.spl_nleft;
1376
lntup = gistSplit(r, leftbuf, lvectup, &llen, giststate,
1377
(res && lvectup[llen - 1] == itup[*len - 1]) ? res : NULL);
1378
ReleaseBuffer(leftbuf);
1380
for (j = 1; j < r->rd_att->natts; j++)
1381
if ((!isAttByVal(giststate, j)) && !v.spl_lisnull[j])
1382
pfree(DatumGetPointer(v.spl_lattr[j]));
1384
newtup = gistjoinvector(newtup, &nlen, lntup, llen);
1391
l = gistwritebuffer(r, left, lvectup, v.spl_nleft, FirstOffsetNumber);
1392
if (BufferGetBlockNumber(buffer) != GISTP_ROOT)
1393
PageRestoreTempPage(left, p);
1395
WriteBuffer(leftbuf);
1398
ItemPointerSet(&((*res)->pointerData), lbknum, l);
1401
newtup = (IndexTuple *) repalloc((void *) newtup, sizeof(IndexTuple) * nlen);
1402
newtup[nlen - 1] = gistFormTuple(giststate, r, v.spl_lattr, v.spl_lattrsize, v.spl_lisnull);
1403
ItemPointerSet(&(newtup[nlen - 1]->t_tid), lbknum, 1);
1417
gistnewroot(Relation r, IndexTuple *itup, int len)
1422
b = ReadBuffer(r, GISTP_ROOT);
1423
GISTInitBuffer(b, 0);
1424
p = BufferGetPage(b);
1426
gistwritebuffer(r, p, itup, len, FirstOffsetNumber);
1431
GISTInitBuffer(Buffer b, uint32 f)
1433
GISTPageOpaque opaque;
1437
pageSize = BufferGetPageSize(b);
1439
page = BufferGetPage(b);
1441
PageInit(page, pageSize, sizeof(GISTPageOpaqueData));
1443
opaque = (GISTPageOpaque) PageGetSpecialPointer(page);
1449
** find entry with lowest penalty
1452
gistchoose(Relation r, Page p, IndexTuple it, /* it has compressed entry */
1453
GISTSTATE *giststate)
1455
OffsetNumber maxoff;
1461
which_grow[INDEX_MAX_KEYS];
1463
identry[INDEX_MAX_KEYS];
1465
decompvec[INDEX_MAX_KEYS],
1466
isnull[INDEX_MAX_KEYS];
1469
maxoff = PageGetMaxOffsetNumber(p);
1473
gistDeCompressAtt(giststate, r,
1474
it, NULL, (OffsetNumber) 0,
1475
identry, decompvec, isnull);
1477
for (i = FirstOffsetNumber; i <= maxoff && sum_grow; i = OffsetNumberNext(i))
1479
IndexTuple itup = (IndexTuple) PageGetItem(p, PageGetItemId(p, i));
1482
for (j = 0; j < r->rd_att->natts; j++)
1484
datum = index_getattr(itup, j + 1, giststate->tupdesc, &IsNull);
1485
gistdentryinit(giststate, j, &entry, datum, r, p, i, ATTSIZE(datum, giststate->tupdesc, j + 1, IsNull), FALSE, IsNull);
1486
gistpenalty(giststate, j, &entry, IsNull, &identry[j], isnull[j], &usize);
1488
if ((!isAttByVal(giststate, j)) && entry.key != datum)
1489
pfree(DatumGetPointer(entry.key));
1491
if (which_grow[j] < 0 || usize < which_grow[j])
1494
which_grow[j] = usize;
1495
if (j < r->rd_att->natts - 1 && i == FirstOffsetNumber)
1496
which_grow[j + 1] = -1;
1497
sum_grow += which_grow[j];
1499
else if (which_grow[j] == usize)
1509
gistFreeAtt(r, identry, decompvec);
1514
gistfreestack(GISTSTACK *s)
1528
* Retail deletion of a single tuple.
1530
* NB: this is no longer called externally, but is still needed by
1531
* gistlayerinsert(). That dependency will have to be fixed if GIST
1532
* is ever going to allow concurrent insertions.
1535
gistdelete(Relation r, ItemPointer tid)
1538
OffsetNumber offnum;
1543
* Since GIST is not marked "amconcurrent" in pg_am, caller should
1544
* have acquired exclusive lock on index relation. We need no locking
1548
blkno = ItemPointerGetBlockNumber(tid);
1549
offnum = ItemPointerGetOffsetNumber(tid);
1551
/* adjust any scans that will be affected by this deletion */
1552
/* NB: this works only for scans in *this* backend! */
1553
gistadjscans(r, GISTOP_DEL, blkno, offnum);
1555
/* delete the index tuple */
1556
buf = ReadBuffer(r, blkno);
1557
page = BufferGetPage(buf);
1559
PageIndexTupleDelete(page, offnum);
1565
* Bulk deletion of all index entries pointing to a set of heap tuples.
1566
* The set of target tuples is specified via a callback routine that tells
1567
* whether any given heap tuple (identified by ItemPointer) is being deleted.
1569
* Result: a palloc'd struct containing statistical info for VACUUM displays.
1572
gistbulkdelete(PG_FUNCTION_ARGS)
1574
Relation rel = (Relation) PG_GETARG_POINTER(0);
1575
IndexBulkDeleteCallback callback = (IndexBulkDeleteCallback) PG_GETARG_POINTER(1);
1576
void *callback_state = (void *) PG_GETARG_POINTER(2);
1577
IndexBulkDeleteResult *result;
1578
BlockNumber num_pages;
1579
double tuples_removed;
1580
double num_index_tuples;
1581
IndexScanDesc iscan;
1584
num_index_tuples = 0;
1587
* Since GIST is not marked "amconcurrent" in pg_am, caller should
1588
* have acquired exclusive lock on index relation. We need no locking
1593
* XXX generic implementation --- should be improved!
1596
/* walk through the entire index */
1597
iscan = index_beginscan(NULL, rel, SnapshotAny, 0, NULL);
1598
/* including killed tuples */
1599
iscan->ignore_killed_tuples = false;
1601
while (index_getnext_indexitem(iscan, ForwardScanDirection))
1603
vacuum_delay_point();
1605
if (callback(&iscan->xs_ctup.t_self, callback_state))
1607
ItemPointerData indextup = iscan->currentItemData;
1609
OffsetNumber offnum;
1613
blkno = ItemPointerGetBlockNumber(&indextup);
1614
offnum = ItemPointerGetOffsetNumber(&indextup);
1616
/* adjust any scans that will be affected by this deletion */
1617
gistadjscans(rel, GISTOP_DEL, blkno, offnum);
1619
/* delete the index tuple */
1620
buf = ReadBuffer(rel, blkno);
1621
page = BufferGetPage(buf);
1623
PageIndexTupleDelete(page, offnum);
1627
tuples_removed += 1;
1630
num_index_tuples += 1;
1633
index_endscan(iscan);
1635
/* return statistics */
1636
num_pages = RelationGetNumberOfBlocks(rel);
1638
result = (IndexBulkDeleteResult *) palloc0(sizeof(IndexBulkDeleteResult));
1639
result->num_pages = num_pages;
1640
result->num_index_tuples = num_index_tuples;
1641
result->tuples_removed = tuples_removed;
1643
PG_RETURN_POINTER(result);
1648
initGISTstate(GISTSTATE *giststate, Relation index)
1652
if (index->rd_att->natts > INDEX_MAX_KEYS)
1653
elog(ERROR, "numberOfAttributes %d > %d",
1654
index->rd_att->natts, INDEX_MAX_KEYS);
1656
giststate->tupdesc = index->rd_att;
1658
for (i = 0; i < index->rd_att->natts; i++)
1660
fmgr_info_copy(&(giststate->consistentFn[i]),
1661
index_getprocinfo(index, i + 1, GIST_CONSISTENT_PROC),
1662
CurrentMemoryContext);
1663
fmgr_info_copy(&(giststate->unionFn[i]),
1664
index_getprocinfo(index, i + 1, GIST_UNION_PROC),
1665
CurrentMemoryContext);
1666
fmgr_info_copy(&(giststate->compressFn[i]),
1667
index_getprocinfo(index, i + 1, GIST_COMPRESS_PROC),
1668
CurrentMemoryContext);
1669
fmgr_info_copy(&(giststate->decompressFn[i]),
1670
index_getprocinfo(index, i + 1, GIST_DECOMPRESS_PROC),
1671
CurrentMemoryContext);
1672
fmgr_info_copy(&(giststate->penaltyFn[i]),
1673
index_getprocinfo(index, i + 1, GIST_PENALTY_PROC),
1674
CurrentMemoryContext);
1675
fmgr_info_copy(&(giststate->picksplitFn[i]),
1676
index_getprocinfo(index, i + 1, GIST_PICKSPLIT_PROC),
1677
CurrentMemoryContext);
1678
fmgr_info_copy(&(giststate->equalFn[i]),
1679
index_getprocinfo(index, i + 1, GIST_EQUAL_PROC),
1680
CurrentMemoryContext);
1685
freeGISTstate(GISTSTATE *giststate)
1690
#ifdef GIST_PAGEADDITEM
1692
** Given an IndexTuple to be inserted on a page, this routine replaces
1693
** the key with another key, which may involve generating a new IndexTuple
1694
** if the sizes don't match or if the null status changes.
1696
** XXX this only works for a single-column index tuple!
1699
gist_tuple_replacekey(Relation r, GISTENTRY entry, IndexTuple t)
1702
Datum datum = index_getattr(t, 1, r->rd_att, &IsNull);
1705
* If new entry fits in index tuple, copy it in. To avoid worrying
1706
* about null-value bitmask, pass it off to the general
1707
* index_formtuple routine if either the previous or new value is
1710
if (!IsNull && DatumGetPointer(entry.key) != NULL &&
1711
(Size) entry.bytes <= ATTSIZE(datum, r, 1, IsNull))
1713
memcpy(DatumGetPointer(datum),
1714
DatumGetPointer(entry.key),
1716
/* clear out old size */
1717
t->t_info &= ~INDEX_SIZE_MASK;
1718
/* or in new size */
1719
t->t_info |= MAXALIGN(entry.bytes + sizeof(IndexTupleData));
1725
/* generate a new index tuple for the compressed entry */
1726
TupleDesc tupDesc = r->rd_att;
1730
isnull = DatumGetPointer(entry.key) != NULL ? ' ' : 'n';
1731
newtup = (IndexTuple) index_formtuple(tupDesc,
1734
newtup->t_tid = t->t_tid;
1741
** initialize a GiST entry with a decompressed version of key
1744
gistdentryinit(GISTSTATE *giststate, int nkey, GISTENTRY *e,
1745
Datum k, Relation r, Page pg, OffsetNumber o,
1746
int b, bool l, bool isNull)
1752
gistentryinit(*e, k, r, pg, o, b, l);
1754
DatumGetPointer(FunctionCall1(&giststate->decompressFn[nkey],
1755
PointerGetDatum(e)));
1756
/* decompressFn may just return the given pointer */
1759
gistentryinit(*e, dep->key, dep->rel, dep->page, dep->offset,
1760
dep->bytes, dep->leafkey);
1765
gistentryinit(*e, (Datum) 0, r, pg, o, 0, l);
1770
** initialize a GiST entry with a compressed version of key
1773
gistcentryinit(GISTSTATE *giststate, int nkey,
1774
GISTENTRY *e, Datum k, Relation r,
1775
Page pg, OffsetNumber o, int b, bool l, bool isNull)
1781
gistentryinit(*e, k, r, pg, o, b, l);
1783
DatumGetPointer(FunctionCall1(&giststate->compressFn[nkey],
1784
PointerGetDatum(e)));
1785
/* compressFn may just return the given pointer */
1788
gistentryinit(*e, cep->key, cep->rel, cep->page, cep->offset,
1789
cep->bytes, cep->leafkey);
1794
gistentryinit(*e, (Datum) 0, r, pg, o, 0, l);
1798
gistFormTuple(GISTSTATE *giststate, Relation r,
1799
Datum attdata[], int datumsize[], bool isnull[])
1802
char isnullchar[INDEX_MAX_KEYS];
1803
bool whatfree[INDEX_MAX_KEYS];
1804
GISTENTRY centry[INDEX_MAX_KEYS];
1805
Datum compatt[INDEX_MAX_KEYS];
1808
for (j = 0; j < r->rd_att->natts; j++)
1812
isnullchar[j] = 'n';
1813
compatt[j] = (Datum) 0;
1814
whatfree[j] = FALSE;
1818
gistcentryinit(giststate, j, ¢ry[j], attdata[j],
1819
NULL, NULL, (OffsetNumber) 0,
1820
datumsize[j], FALSE, FALSE);
1821
isnullchar[j] = ' ';
1822
compatt[j] = centry[j].key;
1823
if (!isAttByVal(giststate, j))
1826
if (centry[j].key != attdata[j])
1827
pfree(DatumGetPointer(attdata[j]));
1830
whatfree[j] = FALSE;
1834
tup = (IndexTuple) index_formtuple(giststate->tupdesc, compatt, isnullchar);
1835
for (j = 0; j < r->rd_att->natts; j++)
1837
pfree(DatumGetPointer(compatt[j]));
1843
gistDeCompressAtt(GISTSTATE *giststate, Relation r, IndexTuple tuple, Page p,
1844
OffsetNumber o, GISTENTRY attdata[], bool decompvec[], bool isnull[])
1849
for (i = 0; i < r->rd_att->natts; i++)
1851
datum = index_getattr(tuple, i + 1, giststate->tupdesc, &isnull[i]);
1852
gistdentryinit(giststate, i, &attdata[i],
1854
ATTSIZE(datum, giststate->tupdesc, i + 1, isnull[i]), FALSE, isnull[i]);
1855
if (isAttByVal(giststate, i))
1856
decompvec[i] = FALSE;
1859
if (attdata[i].key == datum || isnull[i])
1860
decompvec[i] = FALSE;
1862
decompvec[i] = TRUE;
1868
gistFreeAtt(Relation r, GISTENTRY attdata[], bool decompvec[])
1872
for (i = 0; i < r->rd_att->natts; i++)
1874
pfree(DatumGetPointer(attdata[i].key));
1878
gistpenalty(GISTSTATE *giststate, int attno,
1879
GISTENTRY *key1, bool isNull1,
1880
GISTENTRY *key2, bool isNull2, float *penalty)
1882
if (giststate->penaltyFn[attno].fn_strict && (isNull1 || isNull2))
1885
FunctionCall3(&giststate->penaltyFn[attno],
1886
PointerGetDatum(key1),
1887
PointerGetDatum(key2),
1888
PointerGetDatum(penalty));
1893
gist_dumptree(Relation r, int level, BlockNumber blk, OffsetNumber coff)
1897
GISTPageOpaque opaque;
1905
pred = (char *) palloc(sizeof(char) * level + 1);
1906
MemSet(pred, '\t', level);
1909
buffer = ReadBuffer(r, blk);
1910
page = (Page) BufferGetPage(buffer);
1911
opaque = (GISTPageOpaque) PageGetSpecialPointer(page);
1913
maxoff = PageGetMaxOffsetNumber(page);
1915
elog(DEBUG4, "%sPage: %d %s blk: %d maxoff: %d free: %d", pred,
1916
coff, (opaque->flags & F_LEAF) ? "LEAF" : "INTE", (int) blk,
1917
(int) maxoff, PageGetFreeSpace(page));
1919
for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
1921
iid = PageGetItemId(page, i);
1922
which = (IndexTuple) PageGetItem(page, iid);
1923
cblk = ItemPointerGetBlockNumber(&(which->t_tid));
1925
elog(DEBUG4, "%s Tuple. blk: %d size: %d", pred, (int) cblk,
1926
IndexTupleSize(which));
1929
if (!(opaque->flags & F_LEAF))
1930
gist_dumptree(r, level + 1, cblk, i);
1932
ReleaseBuffer(buffer);
1935
#endif /* defined GISTDEBUG */
1938
gist_redo(XLogRecPtr lsn, XLogRecord *record)
1940
elog(PANIC, "gist_redo: unimplemented");
1944
gist_undo(XLogRecPtr lsn, XLogRecord *record)
1946
elog(PANIC, "gist_undo: unimplemented");
1950
gist_desc(char *buf, uint8 xl_info, char *rec)